A high-performance, secure, extensible, and OCI-complaint JavaScript runtime for WasmEdge.

Overview

Run JavaScript in WebAssembly

Now supporting wasmedge socket for HTTP requests and Tensorflow in JavaScript programs!

Prerequisites

Install Rust and wasmedge CLI tool. Make sure that you use the -e all option to install the WasmEdge Tensorflow extensions if you want to try the Tensorflow examples below.

rustup target add wasm32-wasi

Hello js

Build

$ cargo build --target wasm32-wasi --release

Run

$ cd example_js
$ wasmedge --dir .:. ../target/wasm32-wasi/release/wasmedge_quickjs.wasm hello.js WasmEdge Runtime

HTTP Request

Build

$ cargo build --target wasm32-wasi --release

Run

HTTP client applications.

$ cd example_js
$ wasmedge --dir .:. ../target/wasm32-wasi/release/wasmedge_quickjs.wasm http_demo.js

Run and POST to a HTTP server.

# Start the server
$ cd example_js
$ nohup wasmedge --dir .:. ../target/wasm32-wasi/release/wasmedge_quickjs.wasm http_server_demo.js &

# Access the server
$ curl -d "WasmEdge" -X POST http://localhost:8000
echo:WasmEdge

These examples also show how to import another JavaScript file into the current program.

TensorFlow

Build

Note: Build the QuickJS interpreter with the WasmEdge Tensorflow extension.

$ cargo build --target wasm32-wasi --release --features=tensorflow

Run

$ cd example_js/tensorflow_lite_demo
$ wasmedge-tensorflow --dir .:. ../../target/wasm32-wasi/release/wasmedge_quickjs.wasm main.js

Optional: Get static-lib & binding.rs

If you want to build a custom libquickjs.a to export some static c function.

See [quickjs-wasi]

run quickjs-wasi/lib/build.sh
Comments
  • feat: node fs module

    feat: node fs module

    • https://github.com/WasmEdge/WasmEdge/issues/1834
    • [x] FileHandle
    • [x] fsPromises.access
    • [x] fsPromises.appendFile
    • [x] fsPromises.chmod Unsupported
    • [x] fsPromises.chown Unsupported
    • [x] fsPromises.copyFile
    • [x] fsPromises.cp
    • [x] fsPromises.lchmod Unsupported
    • [x] fsPromises.lchown Unsupported
    • [x] fsPromises.lutimes
    • [x] fsPromises.link
    • [x] fsPromises.lstat
    • [x] fsPromises.mkdir
    • [x] fsPromises.mkdtemp
    • [x] fsPromises.open
    • [x] fsPromises.opendir
    • [x] fsPromises.readdir
    • [x] fsPromises.readFile
    • [x] fsPromises.readlink
    • [x] fsPromises.realpath
    • [x] fsPromises.rename
    • [x] fsPromises.rmdir
    • [x] fsPromises.rm
    • [x] fsPromises.stat
    • [x] fsPromises.symlink
    • [x] fsPromises.truncate
    • [x] fsPromises.unlink
    • [x] fsPromises.utimes
    • [x] fsPromises.watch
    • [x] fsPromises.writeFile
    • [x] fsPromises.constants
    • [x] fs.access
    • [x] fs.accessSync
    • [x] fs.appendFile
    • [x] fs.appendFileSync
    • [x] fs.chmod Unsupported
    • [x] fs.chmodSync Unsupported
    • [x] fs.chown Unsupported
    • [x] fs.chownSync Unsupported
    • [x] fs.close
    • [x] fs.closeSync
    • [x] fs.copyFile
    • [x] fs.copyFileSync
    • [x] fs.cp
    • [x] fs.cpSync
    • [x] fs.createReadStream
    • [x] fs.createWriteStream
    • [x] fs.exists
    • [x] fs.existsSync
    • [x] fs.fchmod Unsupported
    • [x] fs.fchmodSync Unsupported
    • [x] fs.fchown Unsupported
    • [x] fs.fchownSync Unsupported
    • [x] fs.fdatasync
    • [x] fs.fdatasyncSync
    • [x] fs.fstat
    • [x] fs.fstatSync
    • [x] fs.fsync
    • [x] fs.fsyncSync
    • [x] fs.ftruncate
    • [x] fs.ftruncateSync
    • [x] fs.futimes
    • [x] fs.futimesSync
    • [x] fs.lchmod Unsupported
    • [x] fs.lchmodSync Unsupported
    • [x] fs.lchown Unsupported
    • [x] fs.lchownSync Unsupported
    • [x] fs.link
    • [x] fs.linkSync
    • [x] fs.lstat
    • [x] fs.lstatSync
    • [x] fs.lutimes
    • [x] fs.lutimesSync
    • [x] fs.mkdir
    • [x] fs.mkdirSync
    • [x] fs.mkdtemp
    • [x] fs.mkdtempSync
    • [x] fs.open
    • [x] fs.openSync
    • [x] fs.opendir
    • [x] fs.opendirSync
    • [x] fs.read
    • [x] fs.readSync
    • [x] fs.readdir
    • [x] fs.readdirSync
    • [x] fs.readFile
    • [x] fs.readFileSync
    • [x] fs.readlink
    • [x] fs.readlinkSync
    • [x] fs.readv
    • [x] fs.readvSync
    • [x] fs.realpath
    • [x] fs.realpathSync
    • [x] fs.rename
    • [x] fs.renameSync
    • [x] fs.rmdir
    • [x] fs.rmdirSync
    • [x] fs.rm
    • [x] fs.rmSync
    • [x] fs.stat
    • [x] fs.statSync
    • [x] fs.symlink
    • [x] fs.symlinkSync
    • [x] fs.truncate
    • [x] fs.truncateSync
    • [x] fs.unlink
    • [x] fs.unlinkSync
    • [x] fs.unwatchFile Unsupported
    • [x] fs.utimes
    • [x] fs.utimesSync
    • [x] fs.watch Unsupported
    • [x] fs.watchFile Unsupported
    • [x] fs.write
    • [x] fs.writeSync
    • [x] fs.writeFile
    • [x] fs.writeFileSync
    • [x] fs.writev
    • [x] fs.writevSync
    • [x] fs.Dir
    • [x] fs.Dirent
    • [ ] fs.FSWatcher Unsupported
    • [ ] fs.StatWatcher Unsupported
    • [x] fs.ReadStream
    • [x] fs.Stats
    • [x] fs.WriteStream
    • [x] fs.constants
    opened by Puellaquae 17
  • React SSR (from book) not working anymore

    React SSR (from book) not working anymore

    I have created a new React App and am using the wasmedge quickjs docs to build another SSR demo.

    When I run the App via WasmEdge like this.

    wasmedge --dir .:. wasmedge_quickjs.wasm ./server-build/index.js
    

    I get the following error

    TypeError: not a function
        at <anonymous> (./server-build/index.js:7568)
    

    The error is pointing to the following line in the machine generated index.js file that is located in the server-build dir

    var u = new aa.TextEncoder();
    
    opened by tpmccallum 11
  • Hydrate is not a function

    Hydrate is not a function

    Hi I have a few observations whilst building a new React SSR application.

    I get an error (in the client's browser console) as shown below, when the app is started. Interestingly, the page still loads successfully. But I am not wanting to just ignore this browser console error (in case we need to address this)

    Uncaught TypeError: n.createRoot(...).hydrate is not a function
    

    Here is my understanding of render vs hydrate

    Render provides a way for the app to render a React element into the DOM and return a reference to the component.

    Hydrate is essentially the same as render. However, hydrate is used to hydrate a container whose HTML contents were rendered by React's ReactDOMServer.

    The original code in the src/index.js file (which the create-react-app command generated) is as follows

    const root = ReactDOM.createRoot(document.getElementById('root'));
    root.render(
      <React.StrictMode>
        <App />
      </React.StrictMode>
    );
    

    Interestingly the above original code works even when we have already implemented all of the other SSR conversions (server/index.js and rollup.config.js etc.) and are starting the server using wasmedge command (as apposed to npm).

    Please note, that there is no error in the browser console with the above code (root.render).

    We of course change the root.render to root.hydrate in our documentation so I went ahead and did that. The root.hydrate generates the above error. I then also tried importing and using ReactDOMServer instead of just ReactDOM but the error still persists.

    Do you have any ideas about solving this error. Thanks so much Tim

    opened by tpmccallum 6
  • Question on Javascript example

    Question on Javascript example

    Following the the instruction to run the example code in windows 10 environment but an error message. _wasmedge --dir .:. ../target/wasm32-wasi/release/wasmedge_quickjs.wasm hello.js WasmEdge Runtime error message: [error] Bind guest directory failed:52

    Could you let me know the problem? thanks

    opened by Shawn-Fan 5
  • feat: node os module

    feat: node os module

    Implement node os module in js, https://github.com/WasmEdge/WasmEdge/issues/1535 . Module name "os" has been used by quickjs, and in windows "node:os" is not a valid name, so I named "nodeos" temporarily. os.constants and os.networkInterfaces() return empty data. os.getPriority(), os.loadavg() and os.userInfo() return fake data. os.cpus() is unimplemented and will throw error. os.totalmem() always return 4294967296 (4GiB, as much as wasm32 can use) and os.freemem() is totalmem() - memory.size.

    opened by Puellaquae 4
  • Dome Hello World in readme may not work

    Dome Hello World in readme may not work

    If run

    $ cd example_js
    $ wasmedge --dir .:. ../target/wasm32-wasi/release/wasmedge_quickjs.wasm hello.js WasmEdge Runtime
    

    which written in readme will show ReferenceError: could not load module filename 'process'. The module files are not loaded into wasm environment (or maybe there has other way to access the module files that I don't know).

    Or use this?

    $ wasmedge --dir .:. ./target/wasm32-wasi/release/wasmedge_quickjs.wasm example_js/hello.js WasmEdge Runtime
    
    opened by Puellaquae 4
  • Performance of React SSR

    Performance of React SSR

    Although the React App is very small, the performance of rendering it via wasmedge-quickjs is rather slow, compared to rendering it via a local node process:

    # macbook pro 2019, i7, 32GB RAM
    ❯ hyperfine "wasmedge --dir .:. ../../target/wasm32-wasi/release/wasmedge_quickjs.wasm dist/main.js" "node dist/main.js"
    Benchmark #1: wasmedge --dir .:. ../../target/wasm32-wasi/release/wasmedge_quickjs.wasm dist/main.js
      Time (mean ± σ):      1.843 s ±  0.024 s    [User: 1.793 s, System: 0.032 s]
      Range (min … max):    1.807 s …  1.877 s    10 runs
    
    Benchmark #2: node dist/main.js
      Time (mean ± σ):      54.5 ms ±   2.8 ms    [User: 42.9 ms, System: 8.9 ms]
      Range (min … max):    51.4 ms …  64.7 ms    51 runs
    
    Summary
      'node dist/main.js' ran
       33.80 ± 1.76 times faster than 'wasmedge --dir .:. ../../target/wasm32-wasi/release/wasmedge_quickjs.wasm dist/main.js' 
    

    Are there any options to improve the performance for SSR in wasm?

    opened by manuschillerdev 4
  • Add the support of `fs` and `env`

    Add the support of `fs` and `env`

    https://github.com/second-state/wasmedge-quickjs/blob/main/example_js/react18_ssr/main.mjs#L8 Currently, the CSS can only be imported as a module in the example.

    But in reality, the assets will be built with some random file names, something like main.073c9b0a.css. This requires the server can read the content from the file system.

    As Wasi has supplied the files and env API, it would be great to add modules like 'wasi_fs' and 'wasi_env`.

    opened by DarumaDocker 3
  • Unable to start server - OS error 28 on Ubuntu 20.04

    Unable to start server - OS error 28 on Ubuntu 20.04

    Originates from this line: https://github.com/second-state/wasmedge-quickjs/blob/main/modules/http.js#L764

    Machine details:

    ❯ uname -sr
    Linux 5.15.0-46-generic
    
    ❯ uname -a
    Linux bropletop 5.15.0-46-generic #49~20.04.1-Ubuntu SMP Thu Aug 4 19:15:44 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
    
    ❯ wasmedge --version
    wasmedge version 0.9.1
    
    ❯ cargo --version
    cargo 1.62.0 (a748cf5a3 2022-06-08)
    

    Using a pre-built image of the much simpler HTTP app works for me via cri-o and crun, but I'm not aware of such an image for the react examples, and I am unable to run the the simple HTTP example bytecode locally either (same error).

    opened by jcuffe 2
  • Error: instantiation failed: unknown import, Code: 0x62

    Error: instantiation failed: unknown import, Code: 0x62

    I have cloned this React blog software [1] which is also part of a comprehensive YouTube tutorial about React [2]. After building it using the standard npm commands (to make sure it worked) I then went ahead and update the source code and config to be rendered server side. I used the SSR [3] instructions from the WasmEdge book.

    Everything was successful right up until the final command to start the server i.e. wasmedge --dir .:. wasmedge_quickjs.wasm ./server-build/index.js

    Here is the error which was returned from that command.

    [2022-04-26 10:31:26.387] [error] instantiation failed: unknown import, Code: 0x62
    [2022-04-26 10:31:26.387] [error]     When linking module: "wasi_snapshot_preview1" , function name: "sock_setsockopt"
    [2022-04-26 10:31:26.387] [error]     At AST node: import description
    [2022-04-26 10:31:26.387] [error]     When linking module: "wasi_snapshot_preview1" , function name: "sock_setsockopt"
    [2022-04-26 10:31:26.387] [error]     At AST node: import description
    [2022-04-26 10:31:26.387] [error]     At AST node: import section
    [2022-04-26 10:31:26.387] [error]     At AST node: module
    

    [1] https://github.com/iamshaunjp/Complete-React-Tutorial/tree/lesson-32/dojo-blog [2] https://www.youtube.com/watch?v=j942wKiXFu8&list=PL4cUxeGkcC9gZD-Tvwfod2gaISzfRiP9d&index=1 [3] https://wasmedge.org/book/en/dev/js/ssr.html

    opened by tpmccallum 2
  • Unable to run examples on M1 mac: `cannot execute binary file`

    Unable to run examples on M1 mac: `cannot execute binary file`

    Hi there! This project looks very very cool. However, I've tried running cargo run in the various example_js folders, but none of them seem to work.

    target/wasm32-wasi/debug/embed_js_module.wasm: target/wasm32-wasi/debug/embed_js_module.wasm: cannot execute binary file
    

    I also tried embedding some examples' rust code in an existing rust project of mine, and got errors containing:

    ...out/libquickjs.a, building for macOS-arm64 but attempting to link with file built for unknown-unsupported file format (...)
              Undefined symbols for architecture arm64:
                "_JS_AddModuleExport", referenced fro...
    

    I'm assuming there is no arm64 support yet? Or am I missing some sort of runtime dependency?

    opened by joepio 2
  • [Feature Request] Standalone QuickJS server

    [Feature Request] Standalone QuickJS server

    If you have a standalone HTTP/HTTPS server module for QuickJS it would be helpful to publish that code standalone, so users don't necessarily need WasmEdge to import and run the server. Thanks.

    opened by guest271314 0
  • feat: node tty, readline module

    feat: node tty, readline module

    Node's tty and readline module is implemented. getWindowSize and setRawMode is unsupported now.

    As test, because test required assert module is in fs's pr, it will be added after fs's pr merged.

    opened by Puellaquae 0
  • Provide pre-built files in the release assets

    Provide pre-built files in the release assets

    Hi,

    I have a few questions:

    1. Is the wasmedge-quickjs stable now?
    2. I would like to know if is it possible to have a release with all the pre-built files, because some people may not want to re-build the quickjs on their host environment.
    3. Is it possible to have version tags and changelogs when some new features are introduced?
    opened by hydai 1
  • The Demo React SSR with renderToPipeableStream may not work as expected.

    The Demo React SSR with renderToPipeableStream may not work as expected.

    Just follow the tutorial. Run the command nohup wasmedge --dir .:. /path/to/wasmedge_quickjs.wasm dist/main.mjs, and then try to visit "http://localhost:8001". Request pending and nothing returns. But It's worked fine with command node dist/main.mjs.

    opened by ahayaSKBINCN 2
  • Demo Hello World setTimeout() does not seem to be working

    Demo Hello World setTimeout() does not seem to be working

    This does not seem to be working from this example on OSX

    setTimeout(() => {
        print('timeout 2s');
    }, 2000);
    

    Originally posted by @vishalchandra in https://github.com/second-state/wasmedge-quickjs/issues/62#issuecomment-1275054954

    opened by vishalchandra 5
Releases(v0.4.0-alpha)
  • v0.4.0-alpha(Jul 29, 2022)

    You must install WasmEdge first. See how to run JavaScript apps here.

    Both binary files in this release are cross-platform WebAssembly bytecode files. But they are optimized for Linux x86_64 with pre-AOT-compiled binaries. If you want to optimize them for a different OS / CPU, you could AOT compile them with wasmedgec on the target platform first.

    • wasmedge_quickjs.wasm is the standard JavaScript runtime.
    • wasmedge_quickjs_tf.wasm contains the WasmEdge tensorflow API extensions.
    Source code(tar.gz)
    Source code(zip)
    wasmedge_quickjs.wasm(1.71 MB)
    wasmedge_quickjs_tf.wasm(2.14 MB)
Owner
Second State
Fast, safe, portable & serverless. Deploy Rust functions in edge computing, Jamstack, SaaS and service mesh applications.
Second State
An experimental Dapr SDK in Rust. It is designed to run in WasmEdge applications.

Dapr SDK for WebAssembly Written in Rust, the dapr_sdk_wasi crate can be compiled into WebAssembly and runs in the WasmEdge Runtime. Introduction | Ho

Second State 18 Jan 3, 2023
🚀Wasmer is a fast and secure WebAssembly runtime that enables super lightweight containers to run anywhere

Wasmer is a fast and secure WebAssembly runtime that enables super lightweight containers to run anywhere: from Desktop to the Cloud, Edge and IoT devices.

Wasmer 14.1k Jan 8, 2023
Rust WebGL2 wrapper with a focus on making high-performance WebAssembly graphics code easier to write and maintain

Limelight Limelight is a WebGL2 wrapper with a focus on making high-performance WebAssembly graphics code easier to write and maintain. demo.mov live

drifting in space 27 Dec 30, 2022
Lumen - A new compiler and runtime for BEAM languages

An alternative BEAM implementation, designed for WebAssembly

Lumen 3.1k Dec 26, 2022
WebAssembly to Lua translator, with runtime

This is a WIP (read: absolutely not ready for serious work) tool for translating WebAssembly into Lua. Support is specifically for LuaJIT, with the se

null 43 Dec 31, 2022
Wasm runtime written in Rust

Wasm runtime written in Rust

Teppei Fukuda 1 Oct 29, 2021
Wasmtime - Standalone JIT-style runtime for WebAssembly, using Cranelift

wasmtime A standalone runtime for WebAssembly A Bytecode Alliance project Guide | Contributing | Website | Chat Installation The Wasmtime CLI can be i

Bytecode Alliance 11.1k Jan 2, 2023
Standalone JIT-style runtime for WebAssembly, using Cranelift

wasmtime A standalone runtime for WebAssembly A Bytecode Alliance project Guide | Contributing | Website | Chat Installation The Wasmtime CLI can be i

Bytecode Alliance 11.1k Dec 31, 2022
Lunatic is an Erlang-inspired runtime for WebAssembly

Lunatic is a universal runtime for fast, robust and scalable server-side applications. It's inspired by Erlang and can be used from any language that

Lunatic 3.7k Jan 9, 2023
Client for integrating private analytics in fast and reliable libraries and apps using Rust and WebAssembly

TelemetryDeck Client Client for integrating private analytics in fast and reliable libraries and apps using Rust and WebAssembly The library provides

Konstantin 2 Apr 20, 2022
A console and web-based Gomoku written in Rust and WebAssembly

?? rust-gomoku A console and web-based Gomoku written in Rust and WebAssembly Getting started with cargo & npm Install required program, run # install

namkyu1999 2 Jan 4, 2022
Let's combine wasi-nn and witx-bindgen and see how it goes!

WASI-NN Experiment (API Docs) Experiments with wasmtime, the wasi-nn proposal, and tract. Getting Started To use this experiment, you will first need

Hammer of the Gods 3 Nov 4, 2022
darkforest is a console and web-based Roguelike written in Rust and WebAssembly.

darkforest darkforest is a console and web-based Roguelike written in Rust and WebAssembly. Key Features TBA Quick Start TBA How To Contribute Contrib

Chris Ohk 5 Oct 5, 2021
Autogenerated async RPC bindings that instantly connect a JS frontend to a Rust backend service via WebSockets and WASM.

Turbocharger Autogenerated async RPC bindings that instantly connect a JS frontend to a Rust backend service via WebSockets and WASM. See https://gith

null 28 Jan 2, 2023
Simple file sharing with client-side encryption, powered by Rust and WebAssembly

Hako Simple file sharing with client-side encryption, powered by Rust and WebAssembly Not feature-packed, but basic functionalities are just working.

Jaehyeon Park 30 Nov 25, 2022
A handy calculator, based on Rust and WebAssembly.

qubit ?? Visit Website To Use Calculator Example ?? Visit Website To Use Calculator 2 + 2

Abhimanyu Sharma 55 Dec 26, 2022
Compiler infrastructure and toolchain library for WebAssembly

Binaryen Binaryen is a compiler and toolchain infrastructure library for WebAssembly, written in C++. It aims to make compiling to WebAssembly easy, f

WebAssembly 6.1k Dec 30, 2022
Zaplib is an open-source library for speeding up web applications using Rust and WebAssembly.

⚡ Zaplib Zaplib is an open-source library for speeding up web applications using Rust and WebAssembly. It lets you write high-performance code in Rust

Zaplib 1.2k Jan 5, 2023
A template for kick starting a Rust and WebAssembly project using wasm-pack.

A template for kick starting a Rust and WebAssembly project using wasm-pack.

Haoxi Tan 1 Feb 14, 2022