Giter Site home page Giter Site logo

rust-assemblyscript's Introduction

Rust <-> assemblyscript example

This repository showcases the following:

  1. How to ompile example assemblyscript to wasm
    • it has a function transform that takes string and returns didderent string
    • it has a host defined function called log that sends a string to rust side for logging
  2. How to use WebAssembly runtime (wasmer) to setup and call this module
    • call wasm module change with http response from some json web api
    • print the output in rust side

Running instruction

Prerequisities:

  • nodejs
  • nix
  • cargo

And then run:

cd transformation
npm install
npm run asbuild # this will create required wasm binaries

cd ..

# run the host side
cargo run
   Compiling wasm v0.1.0 (/home/flakm/programming/modivo/wasm)
    Finished dev [unoptimized + debuginfo] target(s) in 2.54s
     Running `target/debug/wasm`
Compiling module...
Creating the imported function...
[0] my_response: status: 200, body: {"name":"John", "age":30}
[1] my_response: status: 200, body: {"tag":"@John"}

TODOS

Pain points

  • Error handling is painful since we loose a lot of information when code throws error during serialization issue
  • Sharing memory is complicated for example if we want to provide host functions to wasm module that will reach back to some memory in host from the guest it will require dereferencing a raw pointer that is leaked (kind of like with FFI) or some kind of hash map backed storage.

Interesting material

  1. Wasmer rust - rust example: https://github.com/wasmerio/wasmer-rust-example/blob/master/src/main.rs

    wasmer is an open-source runtime for executing WebAssembly on the Server.

  2. AssemblyScript A TypeScript-like language for WebAssembly https://www.assemblyscript.org/

  1. wasm-bindgen rust crate enabling high level interactions between wasm modules allows for similar code:
use wasm_bindgen::prelude::*;

// Import the `window.alert` function from the Web.
#[wasm_bindgen]
extern "C" {
    fn alert(s: &str);
}

// Export a `greet` function from Rust to JavaScript, that alerts a
// hello message.
#[wasm_bindgen]
pub fn greet(name: &str) {
    alert(&format!("Hello, {}!", name));
}

Set up transformation

https://www.assemblyscript.org/getting-started.html#setting-up-a-new-project

rust-assemblyscript's People

Contributors

flakm avatar

Watchers

 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.