Coder Social home page Coder Social logo

pnprasadkundeti / ssvm-nodejs-starter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from second-state/wasmedge-nodejs-starter

0.0 1.0 0.0 438 KB

A template project to run Rust functions in Node.js through the Second State WebAssembly engine.

Home Page: https://www.secondstate.io/articles/getting-started-with-rust-function/

JavaScript 32.97% Rust 37.11% Dockerfile 29.92%

ssvm-nodejs-starter's Introduction

The functions example

In this example, we demonstrate how to call Rust functions in SSVM from JavaScript. Read about Getting started with Rust functions in Node.js.

Prerequisites

If you have not done so already, follow these simple instructions to install Rust, Node.js, SSVM, and ssvmup.

Create new project

$ cargo new --lib functions
$ cd functions

Change the cargo config file

The Cargo.toml file shows the dependencies. Note the dependency for wasm-bindgen, which is required for invoking these Rust functions from JavaScript. The dependency for serde and serde-json allows us to work with JSON strings to represent complex data types.

Write Rust code

The supported data types are:

  • Call parameters can be any combo of i32, String, &str, Vec, and &[u8]
  • Return value can be i32 or String or Vec
  • For complex data types, such as structs, you could return a JSON string

The src/lib.rs file contains a few Rust functions that showcase different combinations of input / output parameters, including using JSON strings to serialize and deserialize complex data types.

Build the WASM bytecode

$ ssvmup build

Node.js app

The node/app.js file shows how to call the Rust functions, running inside the Second State VM (SSVM), from JavaScript inside Node.js.

Test

node app.js

Result

hello SSVM
...
246
Uint8Array [87, ... 203]
Uint8Array [126, ... 27]

ssvm-nodejs-starter's People

Contributors

juntao avatar alabulei1 avatar hydai avatar pnprasadkundeti avatar vrixyz avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.