The WasmEdge (previously known as Second State VM, SSVM) is a high-performance WebAssembly runtime optimized for server-side applications. This project provides support for accessing WasmEdge as a Node.js addon. It allows Node.js applications to call WebAssembly functions written in Rust or other high-performance languages. Why do you want to run WebAssembly on the server-side? The WasmEdge addon could interact with the wasm files generated by the rustwasmc compiler tool.
In the current stage, our prebuilt version only supports x86_64 and aarch64 Linux.
Or you could use --build-from-source
flag to build from the source during addon installation.
There are mappings of wasmedge-core versoins and corresponding WasmEdge versions:
wasmedge-core | WasmEdge |
---|---|
0.8.1 | 0.8.1 |
0.8.2-rc.1 | 0.8.1 |
0.8.2 | 0.8.2 |
0.8.3 | 0.8.2 |
0.9.0-rc.1 | 0.9.0-rc.1 |
0.9.0 | 0.9.0 |
Users should install the dependencies by the following requirments:
docker pull wasmedge/wasmedge
# Tools and libraries
sudo apt install -y \
software-properties-common \
cmake \
libboost-all-dev
# And you will need to install llvm for wasmedge-aot tools
sudo apt install -y \
llvm-dev \
liblld-10-dev
# WasmEdge supports both clang++ and g++ compilers
# You can choose one of them for building this project
sudo apt install -y gcc g++
sudo apt install -y clang
sudo apt list | grep llvm
# Expected output
...omitted...
llvm-dev/focal,now 1:10.0-50~exp1 amd64 [installed]
llvm-runtime/focal,now 1:10.0-50~exp1 amd64 [installed,automatic]
llvm/focal,now 1:10.0-50~exp1 amd64 [installed,automatic]
...omitted...
# If the version is 1:10.x, then your llvm version is correct.
strings /usr/lib/x86_64-linux-gnu/libstdc++.so.6 | grep GLIBCXX
# Expected output
...omitted...
GLIBCXX_3.4.24
GLIBCXX_3.4.25
GLIBCXX_3.4.26
GLIBCXX_3.4.27
GLIBCXX_3.4.28
GLIBCXX_DEBUG_MESSAGE_LENGTH
# If you can find GLIBCXX_3.4.28 in the output, then your libstdc++6 version is correct.
To run on MacOS you will need to build from source by following the steps mentioned below,
wasmedge-core
project to your local environmentnpm install
Please refer to Tutorial: A Wasm-Bindgen application.
Please refer to Tutorial: A standalone wasm32-wasi application.
wasmedge.VM(wasm, wasmedge_options) -> vm_instance
wasm
: Input wasm file, can be the following three formats:
/tmp/hello.wasm
)options
: An options object for setup the WasmEdge execution environment.
options
args
[]
.env
process.env
that Wasm application will get as its environment variables. Default: {}
.preopens
{'/sandbox': '/some/real/path/that/wasm/can/access'}
Default: {}
.EnableWasiStartFunction
main()
, you should set this to true
. Default: false
.EnableAOT
false
.EnableMeasurement
false
.AllowCommands
[]
.AllowAllCommands
AllowCommands
. Default: false
.vm_instance
: A WasmEdge instance.Start() -> Integer
_start()
and expect the return value type is Integer
which represents the error code from main()
.args
in wasi options
.let error_code = Start();
Run(function_name, args...) -> void
function_name
with args
and expect the return value type is void
.function_name
args
<Integer/String/Uint8Array>*: The function arguments. The delimiter is ,
Run("Print", 1234);
RunInt(function_name, args...) -> Integer
function_name
with args
and expect the return value type is Integer
(Int32).function_name
args
<Integer/String/Uint8Array>*: The function arguments. The delimiter is ,
let result = RunInt("Add", 1, 2);
// result should be 3
RunUInt(function_name, args...) -> Integer
function_name
with args
and expect the return value type is Integer
(UInt32).function_name
args
<Integer/String/Uint8Array>*: The function arguments. The delimiter is ,
let result = RunInt("Add", 1, 2);
// result should be 3
RunInt64(function_name, args...) -> BigInt
function_name
with args
and expect the return value type is BigInt
(Int64).function_name
args
<Integer/String/Uint8Array>*: The function arguments. The delimiter is ,
let result = RunInt("Add", 1, 2);
// result should be 3
RunUInt64(function_name, args...) -> BigInt
function_name
with args
and expect the return value type is BigInt
(UInt64).function_name
args
<Integer/String/Uint8Array>*: The function arguments. The delimiter is ,
let result = RunInt("Add", 1, 2);
// result should be 3
RunString(function_name, args...) -> String
function_name
with args
and expect the return value type is String
.function_name
args
<Integer/String/Uint8Array>*: The function arguments. The delimiter is ,
let result = RunString("PrintMathScore", "Amy", 98);
// result: "Amy’s math score is 98".
RunUint8Array(function_name, args...) -> Uint8Array
function_name
with args
and expect the return value type is Uint8Array
.function_name
args
<Integer/String/Uint8Array>*: The function arguments. The delimiter is ,
let result = RunUint8Array("Hash", "Hello, world!");
// result: "[12, 22, 33, 42, 51]".
Compile(output_filename) -> boolean
output_filename
.false
when the compilation failed.
// Compile only
let vm = wasmedge.VM("/path/to/wasm/file", options);
vm.Compile("/path/to/aot/file");
// When you want to run the compiled file let vm = wasmedge.VM("/path/to/aot/file", options); vm.RunXXX("Func", args);
#### `GetStatistics() -> Object`
* If you want to enable measurement, set the option `EnableMeasurement` to `true`. But please notice that enabling measurement will significantly affect performance.
* Get the statistics of execution runtime.
* Return Value `Statistics` <Object>
* `Measure` -> <Boolean>: To show if the measurement is enabled or not.
* `TotalExecutionTime` -> <Integer>: Total execution time (Wasm exeuction time + Host function execution time) in `` unit.
* `WasmExecutionTime` -> <Integer>: Wasm instructions execution time in `ns` unit.
* `HostFunctionExecutionTime` -> <Integer>: Host functions (e.g. eei or wasi functions) execution time in `ns` unit.
* `InstructionCount` -> <Integer>: The number of executed instructions in this execution.
* `TotalGasCost` -> <Integer>: The cost of this execution.
* `InstructionPerSecond` -> <Float>: The instructions per second of this execution.
```javascript
let result = RunInt("Add", 1, 2);
// result should be 3
let stat = GetStatistics();
/*
If the `EnableMeasurement: true`:
stat = Statistics: {
Measure: true,
TotalExecutionTime: 1512,
WasmExecutionTime: 1481,
HostFunctionExecutionTime: 31,
InstructionCount: 27972,
TotalGasCost: 27972,
InstructionPerSecond: 18887238.35246455
}
Else:
stat = Statistics: {
Measure: false
}
*/