` element with an id of `"root"`. It is then sent back as HTTP response.
-- Other files from the `build` directory are read and served as needed at the requests of the browser.
+- ReactDOMServer's `renderToString` is used to render the `
` to an HTML string.
+- The `index.html` file from the app's `build` output directory is loaded as a template. The app's content is injected into the `
` element with an id of `"root"`. It is then sent back as HTTP response.
+- Other files from the `build` directory are read and served as needed at the requests of the browser.
### Step 3 — Build and deploy
@@ -472,7 +472,7 @@ Create a new Babel configuration file named `.babelrc.json` in the project's roo
```json
{
- "presets": ["@babel/preset-env", "@babel/preset-react"]
+ "presets": ["@babel/preset-env", "@babel/preset-react"]
}
```
@@ -481,39 +481,39 @@ Create a webpack config for the server that uses Babel Loader to transpile the c
```js
const path = require('path');
module.exports = {
- entry: './server/index.js',
- externals: [
- { wasi_http: 'wasi_http' },
- { wasi_net: 'wasi_net' },
- { std: 'std' },
- ],
- output: {
- path: path.resolve('server-build'),
- filename: 'index.js',
- chunkFormat: 'module',
- library: {
- type: 'module',
- },
- },
- experiments: {
- outputModule: true,
- },
- module: {
- rules: [
- {
- test: /\.js$/,
- use: 'babel-loader',
- },
- {
- test: /\.css$/,
- use: ['css-loader'],
- },
- {
- test: /\.svg$/,
- use: ['svg-url-loader'],
- },
- ],
+ entry: './server/index.js',
+ externals: [
+ { wasi_http: 'wasi_http' },
+ { wasi_net: 'wasi_net' },
+ { std: 'std' },
+ ],
+ output: {
+ path: path.resolve('server-build'),
+ filename: 'index.js',
+ chunkFormat: 'module',
+ library: {
+ type: 'module',
},
+ },
+ experiments: {
+ outputModule: true,
+ },
+ module: {
+ rules: [
+ {
+ test: /\.js$/,
+ use: 'babel-loader',
+ },
+ {
+ test: /\.css$/,
+ use: ['css-loader'],
+ },
+ {
+ test: /\.svg$/,
+ use: ['svg-url-loader'],
+ },
+ ],
+ },
};
```
@@ -537,8 +537,8 @@ Now, revisit `package.json` and add helper npm scripts. Add `dev:build-server`,
},
```
-- The `dev:build-server` script sets the environment to `"development"` and invokes webpack with the configuration file you created earlier.
-- The `dev:start-server` script runs the WasmEdge server from the `wasmedge` CLI tool to serve the built output. The `wasmedge_quickjs.wasm` program contains the QuickJS runtime. [Learn more](hello_world)
+- The `dev:build-server` script sets the environment to `"development"` and invokes webpack with the configuration file you created earlier.
+- The `dev:start-server` script runs the WasmEdge server from the `wasmedge` CLI tool to serve the built output. The `wasmedge_quickjs.wasm` program contains the QuickJS runtime. [Learn more](hello_world)
Now you can run the following commands to build the client-side app, bundle and transpile the server code, and start up the server on `:8002`.
@@ -583,35 +583,35 @@ const css = require('rollup-plugin-import-css');
const svg = require('rollup-plugin-svg');
const babelOptions = {
- babelrc: false,
- presets: ['@babel/preset-react'],
- babelHelpers: 'bundled',
+ babelrc: false,
+ presets: ['@babel/preset-react'],
+ babelHelpers: 'bundled',
};
module.exports = [
- {
- input: './server/index.js',
- output: {
- file: 'server-build/index.js',
- format: 'esm',
- },
- external: ['std', 'wasi_net', 'wasi_http'],
- plugins: [
- plugin_async(),
- babel(babelOptions),
- nodeResolve({ preferBuiltins: true }),
- commonjs({ ignoreDynamicRequires: false }),
- css(),
- svg({ base64: true }),
- globals(),
- builtins(),
- replace({
- preventAssignment: true,
- 'process.env.NODE_ENV': JSON.stringify('production'),
- 'process.env.NODE_DEBUG': JSON.stringify(''),
- }),
- ],
+ {
+ input: './server/index.js',
+ output: {
+ file: 'server-build/index.js',
+ format: 'esm',
},
+ external: ['std', 'wasi_net', 'wasi_http'],
+ plugins: [
+ plugin_async(),
+ babel(babelOptions),
+ nodeResolve({ preferBuiltins: true }),
+ commonjs({ ignoreDynamicRequires: false }),
+ css(),
+ svg({ base64: true }),
+ globals(),
+ builtins(),
+ replace({
+ preventAssignment: true,
+ 'process.env.NODE_ENV': JSON.stringify('production'),
+ 'process.env.NODE_DEBUG': JSON.stringify(''),
+ }),
+ ],
+ },
];
```
@@ -651,8 +651,8 @@ Now, revisit `package.json` and add helper npm scripts. Add `dev:build-server`,
},
```
-- The `dev:build-server` script sets the environment to `"development"` and invokes webpack with the configuration file you created earlier.
-- The `dev:start-server` script runs the WasmEdge server from the `wasmedge` CLI tool to serve the built output. The `wasmedge_quickjs.wasm` program contains the QuickJS runtime. [Learn more](hello_world)
+- The `dev:build-server` script sets the environment to `"development"` and invokes webpack with the configuration file you created earlier.
+- The `dev:start-server` script runs the WasmEdge server from the `wasmedge` CLI tool to serve the built output. The `wasmedge_quickjs.wasm` program contains the QuickJS runtime. [Learn more](hello_world)
Now you can run the following commands to build the client-side app, bundle and transpile the server code, and start up the server on `:8002`.
diff --git a/i18n/zh/docusaurus-plugin-content-docs/current/develop/javascript/tensorflow.md b/i18n/zh/docusaurus-plugin-content-docs/current/develop/javascript/tensorflow.md
index 80717e92..86c3b743 100644
--- a/i18n/zh/docusaurus-plugin-content-docs/current/develop/javascript/tensorflow.md
+++ b/i18n/zh/docusaurus-plugin-content-docs/current/develop/javascript/tensorflow.md
@@ -22,7 +22,7 @@ let img_rgb = img.to_rgb().resize(192, 192);
let rgb_pix = img_rgb.pixels();
let session = new TensorflowLiteSession(
- __dirname + '/lite-model_aiy_vision_classifier_food_V1_1.tflite',
+ __dirname + '/lite-model_aiy_vision_classifier_food_V1_1.tflite',
);
session.add_input('input', rgb_pix);
session.run();
@@ -31,16 +31,16 @@ let output_view = new Uint8Array(output);
let max = 0;
let max_idx = 0;
for (var i in output_view) {
- let v = output_view[i];
- if (v > max) {
- max = v;
- max_idx = i;
- }
+ let v = output_view[i];
+ if (v > max) {
+ max = v;
+ max_idx = i;
+ }
}
let label_file = std.open(__dirname + '/aiy_food_V1_labelmap.txt', 'r');
let label = '';
for (var i = 0; i <= max_idx; i++) {
- label = label_file.getline();
+ label = label_file.getline();
}
label_file.close();
diff --git a/i18n/zh/docusaurus-plugin-content-docs/current/develop/overview.md b/i18n/zh/docusaurus-plugin-content-docs/current/develop/overview.md
index 76678931..5bfcb8b3 100644
--- a/i18n/zh/docusaurus-plugin-content-docs/current/develop/overview.md
+++ b/i18n/zh/docusaurus-plugin-content-docs/current/develop/overview.md
@@ -11,17 +11,17 @@ WasmEdge Runtime 为 WebAssembly 字节码程序提供了一个定义明确的
我们将带领你了解什么是 WasmEdge,以及如何用不同的语言开发 Wasm 应用程序。我们将在这一本书中涵盖以下内容。
-- [快速入门](/category/getting-started-with-wasmEdge)
-- 介绍[WasmEdge Runtime](/category/what-is-wasmedge)
-- [建立和运行 WasmEdge 应用程序](/category/building-and-running-wasmedge-apps)
-- 使用你的编程语言中开发 WebAssembly 应用程序,包括[Rust](/category/develop-wasm-apps-in-rust)、[C/C++](/category/develop-wasm-apps-in-cc)、[JavaScript](/category/develop-wasm-apps-in-javascript) 和 [Go](/category/develop-wasm-apps-in-go) 。
-- [使用现有的容器工具部署 Wasm 应用程序](/category/deploy-wasmedge-apps in-kubernetes)
+- [快速入门](/category/getting-started-with-wasmEdge)
+- 介绍[WasmEdge Runtime](/category/what-is-wasmedge)
+- [建立和运行 WasmEdge 应用程序](/category/building-and-running-wasmedge-apps)
+- 使用你的编程语言中开发 WebAssembly 应用程序,包括[Rust](/category/develop-wasm-apps-in-rust)、[C/C++](/category/develop-wasm-apps-in-cc)、[JavaScript](/category/develop-wasm-apps-in-javascript) 和 [Go](/category/develop-wasm-apps-in-go) 。
+- [使用现有的容器工具部署 Wasm 应用程序](/category/deploy-wasmedge-apps in-kubernetes)
除了这份指南以外,我们还有关于嵌入 WasmEdge 到 Host 程序和如何为 WasmEdge 做贡献的指南。
如果您发现一些问题或有任何反馈,您可以通过以下方式与我们联系。
-- [Discord Chat](https://discord.gg/U4B5sFTkFc)
-- [创建一个 GitHub Issue 以获得技术支持](https://github.com/WasmEdge/WasmEdge/issues)
-- [提交一个 GitHub Discussion](https://github.com/WasmEdge/WasmEdge/discussions)
-- [在 Twitter 上关注 @realwasmedge](https://twitter.com/realwasmedge)
+- [Discord Chat](https://discord.gg/U4B5sFTkFc)
+- [创建一个 GitHub Issue 以获得技术支持](https://github.com/WasmEdge/WasmEdge/issues)
+- [提交一个 GitHub Discussion](https://github.com/WasmEdge/WasmEdge/discussions)
+- [在 Twitter 上关注 @realwasmedge](https://twitter.com/realwasmedge)
diff --git a/i18n/zh/docusaurus-plugin-content-docs/current/develop/rust/_category_.json b/i18n/zh/docusaurus-plugin-content-docs/current/develop/rust/_category_.json
index d27abb5a..3ad69ec2 100644
--- a/i18n/zh/docusaurus-plugin-content-docs/current/develop/rust/_category_.json
+++ b/i18n/zh/docusaurus-plugin-content-docs/current/develop/rust/_category_.json
@@ -1,8 +1,8 @@
{
- "label": "Develop Wasm Apps in Rust",
- "position": 4,
- "link": {
- "type": "generated-index",
- "description": "Rust is the first class citizen in WebAssembly ecosystem. In this chapter, we will learn how to create Wasm apps in Rust."
- }
+ "label": "Develop Wasm Apps in Rust",
+ "position": 4,
+ "link": {
+ "type": "generated-index",
+ "description": "Rust is the first class citizen in WebAssembly ecosystem. In this chapter, we will learn how to create Wasm apps in Rust."
+ }
}
diff --git a/i18n/zh/docusaurus-plugin-content-docs/current/develop/rust/ai_inference/_category_.json b/i18n/zh/docusaurus-plugin-content-docs/current/develop/rust/ai_inference/_category_.json
index c3d25162..342bc64e 100644
--- a/i18n/zh/docusaurus-plugin-content-docs/current/develop/rust/ai_inference/_category_.json
+++ b/i18n/zh/docusaurus-plugin-content-docs/current/develop/rust/ai_inference/_category_.json
@@ -1,8 +1,8 @@
{
- "label": "4.7 AI Inference",
- "position": 8,
- "link": {
- "type": "generated-index",
- "description": "Run AI Inference function based on TensorFlow, OpenVINO, Pytorch, and TensorFlow-lite in WasmEdge."
- }
+ "label": "4.7 AI Inference",
+ "position": 8,
+ "link": {
+ "type": "generated-index",
+ "description": "Run AI Inference function based on TensorFlow, OpenVINO, Pytorch, and TensorFlow-lite in WasmEdge."
+ }
}
diff --git a/i18n/zh/docusaurus-plugin-content-docs/current/develop/rust/command.md b/i18n/zh/docusaurus-plugin-content-docs/current/develop/rust/command.md
index 57edaba6..61774dfc 100644
--- a/i18n/zh/docusaurus-plugin-content-docs/current/develop/rust/command.md
+++ b/i18n/zh/docusaurus-plugin-content-docs/current/develop/rust/command.md
@@ -10,8 +10,8 @@ Now, why limit ourselves to standard library functions? The same approach can be
The command line program can
-- Take input via command line arguments, as well as the `STDIN` stream.
-- Return value and data via the `STDOUT` stream.
+- Take input via command line arguments, as well as the `STDIN` stream.
+- Return value and data via the `STDOUT` stream.
Application developers for WasmEdge can use our Rust interface crate to access this functionality. In `Cargo.toml`, make sure that you have this dependency.
diff --git a/i18n/zh/docusaurus-plugin-content-docs/current/develop/rust/dapr.md b/i18n/zh/docusaurus-plugin-content-docs/current/develop/rust/dapr.md
index a5bdec4d..ef8f4247 100644
--- a/i18n/zh/docusaurus-plugin-content-docs/current/develop/rust/dapr.md
+++ b/i18n/zh/docusaurus-plugin-content-docs/current/develop/rust/dapr.md
@@ -16,8 +16,8 @@ Before we started, make sure [you have Rust and WasmEdge installed](setup).
You also need to install the following tools.
-- [Dapr CLI installed](https://docs.dapr.io/getting-started/install-dapr-cli/)
-- the [MySQL](https://dev.mysql.com/doc/mysql-installation-excerpt/5.7/en/) or [MariaDB](https://mariadb.com/kb/en/getting-installing-and-upgrading-mariadb/) or [TiDB](https://docs.pingcap.com/tidb/dev/quick-start-with-tidb) databases installed
+- [Dapr CLI installed](https://docs.dapr.io/getting-started/install-dapr-cli/)
+- the [MySQL](https://dev.mysql.com/doc/mysql-installation-excerpt/5.7/en/) or [MariaDB](https://mariadb.com/kb/en/getting-installing-and-upgrading-mariadb/) or [TiDB](https://docs.pingcap.com/tidb/dev/quick-start-with-tidb) databases installed
## The template project explanation
@@ -29,15 +29,15 @@ This application consists of three microservices and a standalone web page that
The WasmEdge's Dapr SDK is used to access Dapr sidecars from the microservice apps. Specifically, the [grayscale](https://github.com/second-state/dapr-wasm/tree/main/image-api-grayscale) microservice takes an image from an HTTP POST, turns it into grayscale, and returns the result image data in the HTTP response.
-- It uses Dapr to discover and invoke the [events](https://github.com/second-state/dapr-wasm/tree/main/events-service) microservice to record every successful user request.
-- It also stores each user’s IP address and last timestamp data in its Dapr sidecar’s state database. That allows the service to rate limit users if needed.
+- It uses Dapr to discover and invoke the [events](https://github.com/second-state/dapr-wasm/tree/main/events-service) microservice to record every successful user request.
+- It also stores each user’s IP address and last timestamp data in its Dapr sidecar’s state database. That allows the service to rate limit users if needed.
The [classify](https://github.com/second-state/dapr-wasm/tree/main/image-api-classify) microservices takes an image from an HTTP POST, runs a Tensorflow model against it to classify the object on the image, and returns the result as a text label in the HTTP response. You can learn more about AI inference in Rust and WasmEdge [here](https://wasmedge.org/docs/category/ai-inference). It uses its own Dapr sidecar the same way as the [grayscale](https://github.com/second-state/dapr-wasm/tree/main/image-api-grayscale) microservice.
The [events](https://github.com/second-state/dapr-wasm/tree/main/events-service) microservice takes JSON data from a HTTP POST and saves it to an external MySQL database for later analysis.
-- It uses Dapr to make itself discoverable by name by other microservices that need to record events.
-- It also uses its Dapr sidecar to store secrets such as the MySQL database credentials.
+- It uses Dapr to make itself discoverable by name by other microservices that need to record events.
+- It also uses its Dapr sidecar to store secrets such as the MySQL database credentials.
Ok, enough concepts for the template project. Let's go ahead.
diff --git a/i18n/zh/docusaurus-plugin-content-docs/current/develop/rust/hello_world.md b/i18n/zh/docusaurus-plugin-content-docs/current/develop/rust/hello_world.md
index 9e74de9b..caa105a0 100644
--- a/i18n/zh/docusaurus-plugin-content-docs/current/develop/rust/hello_world.md
+++ b/i18n/zh/docusaurus-plugin-content-docs/current/develop/rust/hello_world.md
@@ -71,8 +71,8 @@ $ wasmedge --reactor target/wasm32-wasi/debug/add.wasm add 2 2
Of course, in most cases, you will not call functions using CLI arguments. Instead, you will probably need to use a [language SDK from WasmEdge] to call the function, pass call parameters, and receive return values. Below are some SDK examples for complex call parameters and return values.
-- [Use wasmedge-bindgen in a Go host app]
-- [Use direct memory passing in a Go host app]
+- [Use wasmedge-bindgen in a Go host app]
+- [Use direct memory passing in a Go host app]
## Improve the Performance
diff --git a/i18n/zh/docusaurus-plugin-content-docs/current/develop/rust/http_service/_category_.json b/i18n/zh/docusaurus-plugin-content-docs/current/develop/rust/http_service/_category_.json
index bf05105e..57873f50 100644
--- a/i18n/zh/docusaurus-plugin-content-docs/current/develop/rust/http_service/_category_.json
+++ b/i18n/zh/docusaurus-plugin-content-docs/current/develop/rust/http_service/_category_.json
@@ -1,8 +1,8 @@
{
- "label": "4.3 HTTP services",
- "position": 4,
- "link": {
- "type": "generated-index",
- "description": "Create HTTP request with your familiar Rust framework and running in WasmEdge."
- }
+ "label": "4.3 HTTP services",
+ "position": 4,
+ "link": {
+ "type": "generated-index",
+ "description": "Create HTTP request with your familiar Rust framework and running in WasmEdge."
+ }
}
diff --git a/i18n/zh/docusaurus-plugin-content-docs/current/develop/rust/socket_networking/_category_.json b/i18n/zh/docusaurus-plugin-content-docs/current/develop/rust/socket_networking/_category_.json
index 13a90768..b2b28efd 100644
--- a/i18n/zh/docusaurus-plugin-content-docs/current/develop/rust/socket_networking/_category_.json
+++ b/i18n/zh/docusaurus-plugin-content-docs/current/develop/rust/socket_networking/_category_.json
@@ -1,8 +1,8 @@
{
- "label": "4.4 Socket networking",
- "position": 5,
- "link": {
- "type": "generated-index",
- "description": "Run Tokio-based async Rust applications in WasmEdge."
- }
+ "label": "4.4 Socket networking",
+ "position": 5,
+ "link": {
+ "type": "generated-index",
+ "description": "Run Tokio-based async Rust applications in WasmEdge."
+ }
}
diff --git a/i18n/zh/docusaurus-plugin-content-docs/current/develop/wasmedge/_category_.json b/i18n/zh/docusaurus-plugin-content-docs/current/develop/wasmedge/_category_.json
index c15c105e..e0373089 100644
--- a/i18n/zh/docusaurus-plugin-content-docs/current/develop/wasmedge/_category_.json
+++ b/i18n/zh/docusaurus-plugin-content-docs/current/develop/wasmedge/_category_.json
@@ -1,8 +1,8 @@
{
- "label": "What is WasmEdge",
- "position": 2,
- "link": {
- "type": "generated-index",
- "description": "WasmEdge is a WebAssembly runtime optimized for cloud-native and edge-native applications. It is a safe, fast, lightweight, and portable alternative to Linux containers in the cloud. It is also an ideal runtime for serverless functions, user-defined functions, embedded functions in application platforms and data pipelines."
- }
+ "label": "What is WasmEdge",
+ "position": 2,
+ "link": {
+ "type": "generated-index",
+ "description": "WasmEdge is a WebAssembly runtime optimized for cloud-native and edge-native applications. It is a safe, fast, lightweight, and portable alternative to Linux containers in the cloud. It is also an ideal runtime for serverless functions, user-defined functions, embedded functions in application platforms and data pipelines."
+ }
}
diff --git a/i18n/zh/docusaurus-plugin-content-docs/current/develop/wasmedge/extensions/_category_.json b/i18n/zh/docusaurus-plugin-content-docs/current/develop/wasmedge/extensions/_category_.json
index 61c4df95..9c8bd3cc 100644
--- a/i18n/zh/docusaurus-plugin-content-docs/current/develop/wasmedge/extensions/_category_.json
+++ b/i18n/zh/docusaurus-plugin-content-docs/current/develop/wasmedge/extensions/_category_.json
@@ -1,8 +1,8 @@
{
- "label": "WasmEdge Extensions",
- "position": 2,
- "link": {
- "type": "generated-index",
- "description": "WasmEdge supports all standard WebAssembly and WASI specifications. In addition, it supports features and APIs that are crucial for the cloud-native use cases. WasmEdge is a superset of standard Wasm. Through its extension SDK, developers can easily add features they need to the WasmEdge Runtime as plugins."
- }
+ "label": "WasmEdge Extensions",
+ "position": 2,
+ "link": {
+ "type": "generated-index",
+ "description": "WasmEdge supports all standard WebAssembly and WASI specifications. In addition, it supports features and APIs that are crucial for the cloud-native use cases. WasmEdge is a superset of standard Wasm. Through its extension SDK, developers can easily add features they need to the WasmEdge Runtime as plugins."
+ }
}
diff --git a/i18n/zh/docusaurus-plugin-content-docs/current/develop/wasmedge/extensions/proposals.md b/i18n/zh/docusaurus-plugin-content-docs/current/develop/wasmedge/extensions/proposals.md
index 6c17f4d4..3b5507d2 100644
--- a/i18n/zh/docusaurus-plugin-content-docs/current/develop/wasmedge/extensions/proposals.md
+++ b/i18n/zh/docusaurus-plugin-content-docs/current/develop/wasmedge/extensions/proposals.md
@@ -24,10 +24,10 @@ WasmEdge supports the following [WebAssembly proposals](https://github.com/WebAs
The following proposals is under development and may be supported in the future:
-- [Component Model][]
-- [Exception handling][]
-- [Garbage collection][]
-- [WebAssembly C and C++ API][]
+- [Component Model][]
+- [Exception handling][]
+- [Garbage collection][]
+- [WebAssembly C and C++ API][]
[Import/Export of Mutable Globals]: https://github.com/WebAssembly/mutable-global
[Non-trapping float-to-int conversions]: https://github.com/WebAssembly/nontrapping-float-to-int-conversions
@@ -58,7 +58,7 @@ WasmEdge implements the following [WASI proposals](https://github.com/WebAssembl
The following proposals is under development and may be supported in the future:
-- WebAssembly GC Proposal
+- WebAssembly GC Proposal
[Sockets]: https://github.com/WebAssembly/wasi-sockets
[Crypto]: https://github.com/WebAssembly/wasi-crypto
diff --git a/i18n/zh/docusaurus-plugin-content-docs/current/develop/wasmedge/extensions/unique_extensions.md b/i18n/zh/docusaurus-plugin-content-docs/current/develop/wasmedge/extensions/unique_extensions.md
index b97475a8..59c189c4 100644
--- a/i18n/zh/docusaurus-plugin-content-docs/current/develop/wasmedge/extensions/unique_extensions.md
+++ b/i18n/zh/docusaurus-plugin-content-docs/current/develop/wasmedge/extensions/unique_extensions.md
@@ -8,12 +8,12 @@ A challenge for running Wasm apps on the server side is the lack of support for
Through its support for networking socket and related APIs, WasmEdge can run Rust applications that
-- [provide HTTP services](https://github.com/WasmEdge/wasmedge_hyper_demo)
-- [access external web services](https://github.com/WasmEdge/wasmedge_reqwest_demo)
-- [connect to databases](https://github.com/WasmEdge/wasmedge-db-examples)
-- [connect to messging queues](https://github.com/docker/awesome-compose/tree/master/wasmedge-kafka-mysql)
-- [support database driven microservices](https://github.com/second-state/microservice-rust-mysql)
-- [support microservices with Dapr sidecars](https://github.com/second-state/dapr-wasm)
+- [provide HTTP services](https://github.com/WasmEdge/wasmedge_hyper_demo)
+- [access external web services](https://github.com/WasmEdge/wasmedge_reqwest_demo)
+- [connect to databases](https://github.com/WasmEdge/wasmedge-db-examples)
+- [connect to messging queues](https://github.com/docker/awesome-compose/tree/master/wasmedge-kafka-mysql)
+- [support database driven microservices](https://github.com/second-state/microservice-rust-mysql)
+- [support microservices with Dapr sidecars](https://github.com/second-state/dapr-wasm)
Furthermore, the networking socket APIs also allow us to support the node.js API, including both the `server` and `fetch()`, in WasmEdge's JavaScript runtime.
diff --git a/i18n/zh/docusaurus-plugin-content-docs/current/develop/wasmedge/features.md b/i18n/zh/docusaurus-plugin-content-docs/current/develop/wasmedge/features.md
index cb6597c7..aae2f015 100644
--- a/i18n/zh/docusaurus-plugin-content-docs/current/develop/wasmedge/features.md
+++ b/i18n/zh/docusaurus-plugin-content-docs/current/develop/wasmedge/features.md
@@ -10,32 +10,32 @@ WasmEdge ([a sandbox project under CNCF](https://www.cncf.io/projects/wasmedge/)
Taking advantage of [the LLVM-based AoT compiler](../build-and-run/aot), WasmEdge is the fastest WebAssembly runtime on the market.
-- [A Lightweight Design for High-performance Serverless Computing](https://arxiv.org/abs/2010.07115), published on IEEE Software, Jan 2021. [https://arxiv.org/abs/2010.07115](https://arxiv.org/abs/2010.07115)
-- [Performance Analysis for Arm vs. x86 CPUs in the Cloud](https://www.infoq.com/articles/arm-vs-x86-cloud-performance/), published on infoQ.com, Jan 2021. [https://www.infoq.com/articles/arm-vs-x86-cloud-performance/](https://www.infoq.com/articles/arm-vs-x86-cloud-performance/)
-- [WasmEdge is the fastest WebAssembly Runtime in Suborbital Reactr test suite](https://blog.suborbital.dev/suborbital-wasmedge), Dec 2021
+- [A Lightweight Design for High-performance Serverless Computing](https://arxiv.org/abs/2010.07115), published on IEEE Software, Jan 2021. [https://arxiv.org/abs/2010.07115](https://arxiv.org/abs/2010.07115)
+- [Performance Analysis for Arm vs. x86 CPUs in the Cloud](https://www.infoq.com/articles/arm-vs-x86-cloud-performance/), published on infoQ.com, Jan 2021. [https://www.infoq.com/articles/arm-vs-x86-cloud-performance/](https://www.infoq.com/articles/arm-vs-x86-cloud-performance/)
+- [WasmEdge is the fastest WebAssembly Runtime in Suborbital Reactr test suite](https://blog.suborbital.dev/suborbital-wasmedge), Dec 2021
## Cloud-native Extensions
Besides WASI and the standard WebAssembly proposal, WasmEdge has some cloud-native extensions.
-- non-blocking network sockets and web services with Rust, C, and JavaScript SDK
-- MySQL-based database driver
-- Key value store
-- Gas meter for resource limitation
-- WasmEdge-bindgen for complex para passing
-- AI inference with TensorFlow Lite, Pytorch, and OpenVINO
+- non-blocking network sockets and web services with Rust, C, and JavaScript SDK
+- MySQL-based database driver
+- Key value store
+- Gas meter for resource limitation
+- WasmEdge-bindgen for complex para passing
+- AI inference with TensorFlow Lite, Pytorch, and OpenVINO
## JavaScript Support
Through the [WasmEdge-Quickjs](https://github.com/second-state/wasmedge-quickjs) project, WasmEdge could run a JavaScript program, lowering the bar for developing a Wasm app.
-- ES6 module and std API support
-- NPM module support
-- Native JS API in Rust
-- Node.js API Support
-- Async networking
-- Fetch API
-- React SSR
+- ES6 module and std API support
+- NPM module support
+- Native JS API in Rust
+- Node.js API Support
+- Async networking
+- Fetch API
+- React SSR
## Cloud native orchestration
@@ -55,15 +55,15 @@ WasmEdge supports a wide range of operating systems and hardware platforms. It a
WasmEdge now supports:
-- Linux (x86_64 and aarch64)
-- MacOS (x86_64 and M1)
-- Windows
-- Android
-- seL4
-- OpenWrt
-- OpenHarmony
-- Raspberry Pi
-- RISC-V (WIP)
+- Linux (x86_64 and aarch64)
+- MacOS (x86_64 and M1)
+- Windows
+- Android
+- seL4
+- OpenWrt
+- OpenHarmony
+- Raspberry Pi
+- RISC-V (WIP)
## Easy extensible
diff --git a/i18n/zh/docusaurus-plugin-content-docs/current/embed/c++/_category_.json b/i18n/zh/docusaurus-plugin-content-docs/current/embed/c++/_category_.json
index 76bbd452..c10014ae 100644
--- a/i18n/zh/docusaurus-plugin-content-docs/current/embed/c++/_category_.json
+++ b/i18n/zh/docusaurus-plugin-content-docs/current/embed/c++/_category_.json
@@ -1,8 +1,8 @@
{
- "label": "C++ SDK for Embedding Wasm Functions",
- "position": 9,
- "link": {
- "type": "generated-index",
- "description": "In this chapter, we will learn how to Embed WasmEdge in your C++ host applications."
- }
+ "label": "C++ SDK for Embedding Wasm Functions",
+ "position": 9,
+ "link": {
+ "type": "generated-index",
+ "description": "In this chapter, we will learn how to Embed WasmEdge in your C++ host applications."
+ }
}
diff --git a/i18n/zh/docusaurus-plugin-content-docs/current/embed/c/_category_.json b/i18n/zh/docusaurus-plugin-content-docs/current/embed/c/_category_.json
index 2d615f6a..1a27ae33 100644
--- a/i18n/zh/docusaurus-plugin-content-docs/current/embed/c/_category_.json
+++ b/i18n/zh/docusaurus-plugin-content-docs/current/embed/c/_category_.json
@@ -1,8 +1,8 @@
{
- "label": "C SDK for Embedding Wasm Functions",
- "position": 4,
- "link": {
- "type": "generated-index",
- "description": "In this chapter, we will learn how to embed WasmEdge with the C API."
- }
+ "label": "C SDK for Embedding Wasm Functions",
+ "position": 4,
+ "link": {
+ "type": "generated-index",
+ "description": "In this chapter, we will learn how to embed WasmEdge with the C API."
+ }
}
diff --git a/i18n/zh/docusaurus-plugin-content-docs/current/embed/c/externref.md b/i18n/zh/docusaurus-plugin-content-docs/current/embed/c/externref.md
index 40863e15..9034ecc9 100644
--- a/i18n/zh/docusaurus-plugin-content-docs/current/embed/c/externref.md
+++ b/i18n/zh/docusaurus-plugin-content-docs/current/embed/c/externref.md
@@ -216,25 +216,25 @@ int main() {
1. Install the WasmEdge shared library.
- Please refer to the [Installation](/develop/build-and-run/install) for details.
+ Please refer to the [Installation](/develop/build-and-run/install) for details.
2. Prepare the WASM file and the `main.c` source file as above.
3. Compile
- ```bash
- gcc main.c -lwasmedge
- # Or you can use g++ for the C++ case, or use the clang.
- ```
+ ```bash
+ gcc main.c -lwasmedge
+ # Or you can use g++ for the C++ case, or use the clang.
+ ```
4. Run the Test
- ```bash
- $ ./a.out
- Test 1 -- `call_add` -- 1234 + 5678 = 6912
- Test 2 -- `call_mul` -- 789 * 4321 = 3409269
- Test 3 -- `call_mul` -- 8256 ^ 2 = 68161536
- ```
+ ```bash
+ $ ./a.out
+ Test 1 -- `call_add` -- 1234 + 5678 = 6912
+ Test 2 -- `call_mul` -- 789 * 4321 = 3409269
+ Test 3 -- `call_mul` -- 8256 ^ 2 = 68161536
+ ```
## Wasm module with External References
diff --git a/i18n/zh/docusaurus-plugin-content-docs/current/embed/c/intro.md b/i18n/zh/docusaurus-plugin-content-docs/current/embed/c/intro.md
index 475f032a..429515e3 100644
--- a/i18n/zh/docusaurus-plugin-content-docs/current/embed/c/intro.md
+++ b/i18n/zh/docusaurus-plugin-content-docs/current/embed/c/intro.md
@@ -114,14 +114,14 @@ sys 0m0.011s
## API References
-- [0.12.0](/embed/c/reference/0.12.0)
-- [0.11.2](/embed/c/reference/0.11.2)
-- [0.10.1](/embed/c/reference/0.10.1)
-- [0.9.1](/embed/c/reference/0.9.1)
+- [0.12.0](/embed/c/reference/0.12.0)
+- [0.11.2](/embed/c/reference/0.11.2)
+- [0.10.1](/embed/c/reference/0.10.1)
+- [0.9.1](/embed/c/reference/0.9.1)
## Examples
-- Link with the [WasmEdge library](/embed/c/library)
-- Use the [external reference](/embed/c/externref) of WebAssembly input and output in C/C++
-- Implement the [host functions](/embed/c/host_function) in C/C++
-- [Multiple WASM Module Example](/embed/c/multiple_modules.md)
+- Link with the [WasmEdge library](/embed/c/library)
+- Use the [external reference](/embed/c/externref) of WebAssembly input and output in C/C++
+- Implement the [host functions](/embed/c/host_function) in C/C++
+- [Multiple WASM Module Example](/embed/c/multiple_modules.md)
diff --git a/i18n/zh/docusaurus-plugin-content-docs/current/embed/c/multiple_modules.md b/i18n/zh/docusaurus-plugin-content-docs/current/embed/c/multiple_modules.md
index b5476a21..6f8e2a63 100644
--- a/i18n/zh/docusaurus-plugin-content-docs/current/embed/c/multiple_modules.md
+++ b/i18n/zh/docusaurus-plugin-content-docs/current/embed/c/multiple_modules.md
@@ -75,466 +75,466 @@ With the `WasmEdge_VMContext`, developers can instantiate and execute WASM quick
1. Register and instantiate the `lib.wasm` from file directly
- ```c
- #include
- #include
-
- int main() {
- /* The result. */
- WasmEdge_Result Res;
-
- /* The params and returns. */
- WasmEdge_Value Params[2], Returns[1];
-
- /* Create the VM context. */
- WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(NULL, NULL);
-
- /* Register the `lib.wasm` from file with the module name "math". */
- WasmEdge_String ModuleName = WasmEdge_StringCreateByCString("math");
- Res = WasmEdge_VMRegisterModuleFromFile(VMCxt, ModuleName, "lib.wasm");
- WasmEdge_StringDelete(ModuleName);
- if (!WasmEdge_ResultOK(Res)) {
- WasmEdge_VMDelete(VMCxt);
- printf("Register lib.wasm error: %s\n", WasmEdge_ResultGetMessage(Res));
- return -1;
- }
-
- /* Instantiate the `test.wasm`. */
- /*
- * Developers can use the APIs such as `WasmEdge_VMRunWasmFromFile` to
- * instantiate and execute quickly.
- */
- Res = WasmEdge_VMLoadWasmFromFile(VMCxt, "test.wasm");
- if (!WasmEdge_ResultOK(Res)) {
- WasmEdge_VMDelete(VMCxt);
- printf("Load test.wasm error: %s\n", WasmEdge_ResultGetMessage(Res));
- return -1;
- }
- Res = WasmEdge_VMValidate(VMCxt);
- if (!WasmEdge_ResultOK(Res)) {
- WasmEdge_VMDelete(VMCxt);
- printf("Validate test.wasm error: %s\n",
- WasmEdge_ResultGetMessage(Res));
- return -1;
- }
- Res = WasmEdge_VMInstantiate(VMCxt);
- if (!WasmEdge_ResultOK(Res)) {
- WasmEdge_VMDelete(VMCxt);
- printf("Instantiate test.wasm error: %s\n",
- WasmEdge_ResultGetMessage(Res));
- return -1;
- }
-
- /* Invoke the functions. */
- /* Invoke the "add_and_square" to calculate (123 + 456)^2 */
- WasmEdge_String FuncName =
- WasmEdge_StringCreateByCString("add_and_square");
- Params[0] = WasmEdge_ValueGenI32(123);
- Params[1] = WasmEdge_ValueGenI32(456);
- Res = WasmEdge_VMExecute(VMCxt, FuncName, Params, 2, Returns, 1);
- WasmEdge_StringDelete(FuncName);
- if (WasmEdge_ResultOK(Res)) {
- printf("Get the '(%d + %d)^2' result: %d\n", 123, 456,
- WasmEdge_ValueGetI32(Returns[0]));
- } else {
- printf("Execute 'add_and_square' error: %s\n",
- WasmEdge_ResultGetMessage(Res));
- }
- /* Invoke the "sum_of_squares" to calculate (77^2 + 88^2) */
- FuncName = WasmEdge_StringCreateByCString("sum_of_squares");
- Params[0] = WasmEdge_ValueGenI32(77);
- Params[1] = WasmEdge_ValueGenI32(88);
- Res = WasmEdge_VMExecute(VMCxt, FuncName, Params, 2, Returns, 1);
- WasmEdge_StringDelete(FuncName);
- if (WasmEdge_ResultOK(Res)) {
- printf("Get the '%d^2 + %d^2' result: %d\n", 77, 88,
- WasmEdge_ValueGetI32(Returns[0]));
- } else {
- printf("Execute 'sum_of_squares' error: %s\n",
- WasmEdge_ResultGetMessage(Res));
- }
-
- /* Resources deallocations. */
- WasmEdge_VMDelete(VMCxt);
- return 0;
- }
- ```
-
- Then compile and execute:
-
- ```bash
- $ gcc test.c -lwasmedge
- $ ./a.out
- Get the '(123 + 456)^2' result: 335241
- Get the '77^2 + 88^2' result: 13673
- ```
+ ```c
+ #include
+ #include
+
+ int main() {
+ /* The result. */
+ WasmEdge_Result Res;
+
+ /* The params and returns. */
+ WasmEdge_Value Params[2], Returns[1];
+
+ /* Create the VM context. */
+ WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(NULL, NULL);
+
+ /* Register the `lib.wasm` from file with the module name "math". */
+ WasmEdge_String ModuleName = WasmEdge_StringCreateByCString("math");
+ Res = WasmEdge_VMRegisterModuleFromFile(VMCxt, ModuleName, "lib.wasm");
+ WasmEdge_StringDelete(ModuleName);
+ if (!WasmEdge_ResultOK(Res)) {
+ WasmEdge_VMDelete(VMCxt);
+ printf("Register lib.wasm error: %s\n", WasmEdge_ResultGetMessage(Res));
+ return -1;
+ }
+
+ /* Instantiate the `test.wasm`. */
+ /*
+ * Developers can use the APIs such as `WasmEdge_VMRunWasmFromFile` to
+ * instantiate and execute quickly.
+ */
+ Res = WasmEdge_VMLoadWasmFromFile(VMCxt, "test.wasm");
+ if (!WasmEdge_ResultOK(Res)) {
+ WasmEdge_VMDelete(VMCxt);
+ printf("Load test.wasm error: %s\n", WasmEdge_ResultGetMessage(Res));
+ return -1;
+ }
+ Res = WasmEdge_VMValidate(VMCxt);
+ if (!WasmEdge_ResultOK(Res)) {
+ WasmEdge_VMDelete(VMCxt);
+ printf("Validate test.wasm error: %s\n",
+ WasmEdge_ResultGetMessage(Res));
+ return -1;
+ }
+ Res = WasmEdge_VMInstantiate(VMCxt);
+ if (!WasmEdge_ResultOK(Res)) {
+ WasmEdge_VMDelete(VMCxt);
+ printf("Instantiate test.wasm error: %s\n",
+ WasmEdge_ResultGetMessage(Res));
+ return -1;
+ }
+
+ /* Invoke the functions. */
+ /* Invoke the "add_and_square" to calculate (123 + 456)^2 */
+ WasmEdge_String FuncName =
+ WasmEdge_StringCreateByCString("add_and_square");
+ Params[0] = WasmEdge_ValueGenI32(123);
+ Params[1] = WasmEdge_ValueGenI32(456);
+ Res = WasmEdge_VMExecute(VMCxt, FuncName, Params, 2, Returns, 1);
+ WasmEdge_StringDelete(FuncName);
+ if (WasmEdge_ResultOK(Res)) {
+ printf("Get the '(%d + %d)^2' result: %d\n", 123, 456,
+ WasmEdge_ValueGetI32(Returns[0]));
+ } else {
+ printf("Execute 'add_and_square' error: %s\n",
+ WasmEdge_ResultGetMessage(Res));
+ }
+ /* Invoke the "sum_of_squares" to calculate (77^2 + 88^2) */
+ FuncName = WasmEdge_StringCreateByCString("sum_of_squares");
+ Params[0] = WasmEdge_ValueGenI32(77);
+ Params[1] = WasmEdge_ValueGenI32(88);
+ Res = WasmEdge_VMExecute(VMCxt, FuncName, Params, 2, Returns, 1);
+ WasmEdge_StringDelete(FuncName);
+ if (WasmEdge_ResultOK(Res)) {
+ printf("Get the '%d^2 + %d^2' result: %d\n", 77, 88,
+ WasmEdge_ValueGetI32(Returns[0]));
+ } else {
+ printf("Execute 'sum_of_squares' error: %s\n",
+ WasmEdge_ResultGetMessage(Res));
+ }
+
+ /* Resources deallocations. */
+ WasmEdge_VMDelete(VMCxt);
+ return 0;
+ }
+ ```
+
+ Then compile and execute:
+
+ ```bash
+ $ gcc test.c -lwasmedge
+ $ ./a.out
+ Get the '(123 + 456)^2' result: 335241
+ Get the '77^2 + 88^2' result: 13673
+ ```
2. Register and instantiate the `lib.wasm` from buffer
- ```c
- #include
- #include
-
- int main() {
- /* The result. */
- WasmEdge_Result Res;
-
- /* The params and returns. */
- WasmEdge_Value Params[2], Returns[1];
-
- /* The `lib.wasm` buffer example. */
- /* Developers can also load the buffer from file. */
- uint8_t WASM[] = {/* WASM header */
- 0x00, 0x61, 0x73, 0x6D, 0x01, 0x00, 0x00, 0x00,
- /* Type section */
- 0x01, 0x07, 0x01,
- /* function type {i32, i32} -> {i32} */
- 0x60, 0x02, 0x7F, 0x7F, 0x01, 0x7F,
- /* Function section */
- 0x03, 0x03, 0x02, 0x00, 0x00,
- /* Export section */
- 0x07, 0x0D, 0x02,
- /* export function: "add" */
- 0x03, 0x61, 0x64, 0x64, 0x00, 0x00,
- /* export function: "mul" */
- 0x03, 0x6D, 0x75, 0x6C, 0x00, 0x01,
- /* Code section */
- 0x0A, 0x11, 0x02,
- /* "add" code body */
- 0x07, 0x00, 0x20, 0x00, 0x20, 0x01, 0x6A, 0x0B,
- /* "mul" code body */
- 0x07, 0x00, 0x20, 0x00, 0x20, 0x01, 0x6C, 0x0B};
-
- /* Create the VM context. */
- WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(NULL, NULL);
-
- /* Register the `lib.wasm` from the buffer with the module name "math". */
- WasmEdge_String ModuleName = WasmEdge_StringCreateByCString("math");
- Res = WasmEdge_VMRegisterModuleFromBuffer(VMCxt, ModuleName, WASM,
- sizeof(WASM));
- WasmEdge_StringDelete(ModuleName);
- if (!WasmEdge_ResultOK(Res)) {
- WasmEdge_VMDelete(VMCxt);
- printf("Register lib.wasm error: %s\n", WasmEdge_ResultGetMessage(Res));
- return -1;
- }
-
- /* Instantiate the `test.wasm`. */
- /*
- * Developers can use the APIs such as `WasmEdge_VMRunWasmFromFile` to
- * instantiate and execute quickly.
- */
- Res = WasmEdge_VMLoadWasmFromFile(VMCxt, "test.wasm");
- if (!WasmEdge_ResultOK(Res)) {
- WasmEdge_VMDelete(VMCxt);
- printf("Load test.wasm error: %s\n", WasmEdge_ResultGetMessage(Res));
- return -1;
- }
- Res = WasmEdge_VMValidate(VMCxt);
- if (!WasmEdge_ResultOK(Res)) {
- WasmEdge_VMDelete(VMCxt);
- printf("Validate test.wasm error: %s\n",
- WasmEdge_ResultGetMessage(Res));
- return -1;
- }
- Res = WasmEdge_VMInstantiate(VMCxt);
- if (!WasmEdge_ResultOK(Res)) {
- WasmEdge_VMDelete(VMCxt);
- printf("Instantiate test.wasm error: %s\n",
- WasmEdge_ResultGetMessage(Res));
- return -1;
- }
-
- /* Invoke the functions. */
- /* Invoke the "add_and_square" to calculate (123 + 456)^2 */
- WasmEdge_String FuncName =
- WasmEdge_StringCreateByCString("add_and_square");
- Params[0] = WasmEdge_ValueGenI32(123);
- Params[1] = WasmEdge_ValueGenI32(456);
- Res = WasmEdge_VMExecute(VMCxt, FuncName, Params, 2, Returns, 1);
- WasmEdge_StringDelete(FuncName);
- if (WasmEdge_ResultOK(Res)) {
- printf("Get the '(%d + %d)^2' result: %d\n", 123, 456,
- WasmEdge_ValueGetI32(Returns[0]));
- } else {
- printf("Execute 'add_and_square' error: %s\n",
- WasmEdge_ResultGetMessage(Res));
- }
- /* Invoke the "sum_of_squares" to calculate (77^2 + 88^2) */
- FuncName = WasmEdge_StringCreateByCString("sum_of_squares");
- Params[0] = WasmEdge_ValueGenI32(77);
- Params[1] = WasmEdge_ValueGenI32(88);
- Res = WasmEdge_VMExecute(VMCxt, FuncName, Params, 2, Returns, 1);
- WasmEdge_StringDelete(FuncName);
- if (WasmEdge_ResultOK(Res)) {
- printf("Get the '%d^2 + %d^2' result: %d\n", 77, 88,
- WasmEdge_ValueGetI32(Returns[0]));
- } else {
- printf("Execute 'sum_of_squares' error: %s\n",
- WasmEdge_ResultGetMessage(Res));
- }
-
- /* Resources deallocations. */
- WasmEdge_VMDelete(VMCxt);
- return 0;
- }
- ```
-
- Then compile and execute:
-
- ```bash
- $ gcc test.c -lwasmedge
- $ ./a.out
- Get the '(123 + 456)^2' result: 335241
- Get the '77^2 + 88^2' result: 13673
- ```
+ ```c
+ #include
+ #include
+
+ int main() {
+ /* The result. */
+ WasmEdge_Result Res;
+
+ /* The params and returns. */
+ WasmEdge_Value Params[2], Returns[1];
+
+ /* The `lib.wasm` buffer example. */
+ /* Developers can also load the buffer from file. */
+ uint8_t WASM[] = {/* WASM header */
+ 0x00, 0x61, 0x73, 0x6D, 0x01, 0x00, 0x00, 0x00,
+ /* Type section */
+ 0x01, 0x07, 0x01,
+ /* function type {i32, i32} -> {i32} */
+ 0x60, 0x02, 0x7F, 0x7F, 0x01, 0x7F,
+ /* Function section */
+ 0x03, 0x03, 0x02, 0x00, 0x00,
+ /* Export section */
+ 0x07, 0x0D, 0x02,
+ /* export function: "add" */
+ 0x03, 0x61, 0x64, 0x64, 0x00, 0x00,
+ /* export function: "mul" */
+ 0x03, 0x6D, 0x75, 0x6C, 0x00, 0x01,
+ /* Code section */
+ 0x0A, 0x11, 0x02,
+ /* "add" code body */
+ 0x07, 0x00, 0x20, 0x00, 0x20, 0x01, 0x6A, 0x0B,
+ /* "mul" code body */
+ 0x07, 0x00, 0x20, 0x00, 0x20, 0x01, 0x6C, 0x0B};
+
+ /* Create the VM context. */
+ WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(NULL, NULL);
+
+ /* Register the `lib.wasm` from the buffer with the module name "math". */
+ WasmEdge_String ModuleName = WasmEdge_StringCreateByCString("math");
+ Res = WasmEdge_VMRegisterModuleFromBuffer(VMCxt, ModuleName, WASM,
+ sizeof(WASM));
+ WasmEdge_StringDelete(ModuleName);
+ if (!WasmEdge_ResultOK(Res)) {
+ WasmEdge_VMDelete(VMCxt);
+ printf("Register lib.wasm error: %s\n", WasmEdge_ResultGetMessage(Res));
+ return -1;
+ }
+
+ /* Instantiate the `test.wasm`. */
+ /*
+ * Developers can use the APIs such as `WasmEdge_VMRunWasmFromFile` to
+ * instantiate and execute quickly.
+ */
+ Res = WasmEdge_VMLoadWasmFromFile(VMCxt, "test.wasm");
+ if (!WasmEdge_ResultOK(Res)) {
+ WasmEdge_VMDelete(VMCxt);
+ printf("Load test.wasm error: %s\n", WasmEdge_ResultGetMessage(Res));
+ return -1;
+ }
+ Res = WasmEdge_VMValidate(VMCxt);
+ if (!WasmEdge_ResultOK(Res)) {
+ WasmEdge_VMDelete(VMCxt);
+ printf("Validate test.wasm error: %s\n",
+ WasmEdge_ResultGetMessage(Res));
+ return -1;
+ }
+ Res = WasmEdge_VMInstantiate(VMCxt);
+ if (!WasmEdge_ResultOK(Res)) {
+ WasmEdge_VMDelete(VMCxt);
+ printf("Instantiate test.wasm error: %s\n",
+ WasmEdge_ResultGetMessage(Res));
+ return -1;
+ }
+
+ /* Invoke the functions. */
+ /* Invoke the "add_and_square" to calculate (123 + 456)^2 */
+ WasmEdge_String FuncName =
+ WasmEdge_StringCreateByCString("add_and_square");
+ Params[0] = WasmEdge_ValueGenI32(123);
+ Params[1] = WasmEdge_ValueGenI32(456);
+ Res = WasmEdge_VMExecute(VMCxt, FuncName, Params, 2, Returns, 1);
+ WasmEdge_StringDelete(FuncName);
+ if (WasmEdge_ResultOK(Res)) {
+ printf("Get the '(%d + %d)^2' result: %d\n", 123, 456,
+ WasmEdge_ValueGetI32(Returns[0]));
+ } else {
+ printf("Execute 'add_and_square' error: %s\n",
+ WasmEdge_ResultGetMessage(Res));
+ }
+ /* Invoke the "sum_of_squares" to calculate (77^2 + 88^2) */
+ FuncName = WasmEdge_StringCreateByCString("sum_of_squares");
+ Params[0] = WasmEdge_ValueGenI32(77);
+ Params[1] = WasmEdge_ValueGenI32(88);
+ Res = WasmEdge_VMExecute(VMCxt, FuncName, Params, 2, Returns, 1);
+ WasmEdge_StringDelete(FuncName);
+ if (WasmEdge_ResultOK(Res)) {
+ printf("Get the '%d^2 + %d^2' result: %d\n", 77, 88,
+ WasmEdge_ValueGetI32(Returns[0]));
+ } else {
+ printf("Execute 'sum_of_squares' error: %s\n",
+ WasmEdge_ResultGetMessage(Res));
+ }
+
+ /* Resources deallocations. */
+ WasmEdge_VMDelete(VMCxt);
+ return 0;
+ }
+ ```
+
+ Then compile and execute:
+
+ ```bash
+ $ gcc test.c -lwasmedge
+ $ ./a.out
+ Get the '(123 + 456)^2' result: 335241
+ Get the '77^2 + 88^2' result: 13673
+ ```
3. Load the `lib.wasm` to an AST Context first
- ```c
- #include
- #include
-
- int main() {
- /* The result. */
- WasmEdge_Result Res;
-
- /* The params and returns. */
- WasmEdge_Value Params[2], Returns[1];
-
- /* Assume that the `lib.wasm` has loaded first. */
- WasmEdge_LoaderContext *LoadCxt = WasmEdge_LoaderCreate(NULL);
- WasmEdge_ASTModuleContext *LibASTCxt = NULL;
- Res = WasmEdge_LoaderParseFromFile(LoadCxt, &LibASTCxt, "lib.wasm");
- WasmEdge_LoaderDelete(LoadCxt);
- if (!WasmEdge_ResultOK(Res)) {
- printf("Load lib.wasm error: %s\n", WasmEdge_ResultGetMessage(Res));
- return -1;
- }
-
- /* Create the VM context. */
- WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(NULL, NULL);
-
- /* Register the loaded AST context with the module name "math". */
- WasmEdge_String ModuleName = WasmEdge_StringCreateByCString("math");
- Res =
- WasmEdge_VMRegisterModuleFromASTModule(VMCxt, ModuleName, LibASTCxt);
- WasmEdge_StringDelete(ModuleName);
- WasmEdge_ASTModuleDelete(LibASTCxt);
- if (!WasmEdge_ResultOK(Res)) {
- WasmEdge_VMDelete(VMCxt);
- printf("Register lib.wasm error: %s\n", WasmEdge_ResultGetMessage(Res));
- return -1;
- }
-
- /* Instantiate the `test.wasm`. */
- /*
- * Developers can use the APIs such as `WasmEdge_VMRunWasmFromFile` to
- * instantiate and execute quickly.
- */
- Res = WasmEdge_VMLoadWasmFromFile(VMCxt, "test.wasm");
- if (!WasmEdge_ResultOK(Res)) {
- WasmEdge_VMDelete(VMCxt);
- printf("Load test.wasm error: %s\n", WasmEdge_ResultGetMessage(Res));
- return -1;
- }
- Res = WasmEdge_VMValidate(VMCxt);
- if (!WasmEdge_ResultOK(Res)) {
- WasmEdge_VMDelete(VMCxt);
- printf("Validate test.wasm error: %s\n",
- WasmEdge_ResultGetMessage(Res));
- return -1;
- }
- Res = WasmEdge_VMInstantiate(VMCxt);
- if (!WasmEdge_ResultOK(Res)) {
- WasmEdge_VMDelete(VMCxt);
- printf("Instantiate test.wasm error: %s\n",
- WasmEdge_ResultGetMessage(Res));
- return -1;
- }
-
- /* Invoke the functions. */
- /* Invoke the "add_and_square" to calculate (123 + 456)^2 */
- WasmEdge_String FuncName =
- WasmEdge_StringCreateByCString("add_and_square");
- Params[0] = WasmEdge_ValueGenI32(123);
- Params[1] = WasmEdge_ValueGenI32(456);
- Res = WasmEdge_VMExecute(VMCxt, FuncName, Params, 2, Returns, 1);
- WasmEdge_StringDelete(FuncName);
- if (WasmEdge_ResultOK(Res)) {
- printf("Get the '(%d + %d)^2' result: %d\n", 123, 456,
- WasmEdge_ValueGetI32(Returns[0]));
- } else {
- printf("Execute 'add_and_square' error: %s\n",
- WasmEdge_ResultGetMessage(Res));
- }
- /* Invoke the "sum_of_squares" to calculate (77^2 + 88^2) */
- FuncName = WasmEdge_StringCreateByCString("sum_of_squares");
- Params[0] = WasmEdge_ValueGenI32(77);
- Params[1] = WasmEdge_ValueGenI32(88);
- Res = WasmEdge_VMExecute(VMCxt, FuncName, Params, 2, Returns, 1);
- WasmEdge_StringDelete(FuncName);
- if (WasmEdge_ResultOK(Res)) {
- printf("Get the '%d^2 + %d^2' result: %d\n", 77, 88,
- WasmEdge_ValueGetI32(Returns[0]));
- } else {
- printf("Execute 'sum_of_squares' error: %s\n",
- WasmEdge_ResultGetMessage(Res));
- }
-
- /* Resources deallocations. */
- WasmEdge_VMDelete(VMCxt);
- return 0;
- }
- ```
-
- Then compile and execute:
-
- ```bash
- $ gcc test.c -lwasmedge
- $ ./a.out
- Get the '(123 + 456)^2' result: 335241
- Get the '77^2 + 88^2' result: 13673
- ```
+ ```c
+ #include
+ #include
+
+ int main() {
+ /* The result. */
+ WasmEdge_Result Res;
+
+ /* The params and returns. */
+ WasmEdge_Value Params[2], Returns[1];
+
+ /* Assume that the `lib.wasm` has loaded first. */
+ WasmEdge_LoaderContext *LoadCxt = WasmEdge_LoaderCreate(NULL);
+ WasmEdge_ASTModuleContext *LibASTCxt = NULL;
+ Res = WasmEdge_LoaderParseFromFile(LoadCxt, &LibASTCxt, "lib.wasm");
+ WasmEdge_LoaderDelete(LoadCxt);
+ if (!WasmEdge_ResultOK(Res)) {
+ printf("Load lib.wasm error: %s\n", WasmEdge_ResultGetMessage(Res));
+ return -1;
+ }
+
+ /* Create the VM context. */
+ WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(NULL, NULL);
+
+ /* Register the loaded AST context with the module name "math". */
+ WasmEdge_String ModuleName = WasmEdge_StringCreateByCString("math");
+ Res =
+ WasmEdge_VMRegisterModuleFromASTModule(VMCxt, ModuleName, LibASTCxt);
+ WasmEdge_StringDelete(ModuleName);
+ WasmEdge_ASTModuleDelete(LibASTCxt);
+ if (!WasmEdge_ResultOK(Res)) {
+ WasmEdge_VMDelete(VMCxt);
+ printf("Register lib.wasm error: %s\n", WasmEdge_ResultGetMessage(Res));
+ return -1;
+ }
+
+ /* Instantiate the `test.wasm`. */
+ /*
+ * Developers can use the APIs such as `WasmEdge_VMRunWasmFromFile` to
+ * instantiate and execute quickly.
+ */
+ Res = WasmEdge_VMLoadWasmFromFile(VMCxt, "test.wasm");
+ if (!WasmEdge_ResultOK(Res)) {
+ WasmEdge_VMDelete(VMCxt);
+ printf("Load test.wasm error: %s\n", WasmEdge_ResultGetMessage(Res));
+ return -1;
+ }
+ Res = WasmEdge_VMValidate(VMCxt);
+ if (!WasmEdge_ResultOK(Res)) {
+ WasmEdge_VMDelete(VMCxt);
+ printf("Validate test.wasm error: %s\n",
+ WasmEdge_ResultGetMessage(Res));
+ return -1;
+ }
+ Res = WasmEdge_VMInstantiate(VMCxt);
+ if (!WasmEdge_ResultOK(Res)) {
+ WasmEdge_VMDelete(VMCxt);
+ printf("Instantiate test.wasm error: %s\n",
+ WasmEdge_ResultGetMessage(Res));
+ return -1;
+ }
+
+ /* Invoke the functions. */
+ /* Invoke the "add_and_square" to calculate (123 + 456)^2 */
+ WasmEdge_String FuncName =
+ WasmEdge_StringCreateByCString("add_and_square");
+ Params[0] = WasmEdge_ValueGenI32(123);
+ Params[1] = WasmEdge_ValueGenI32(456);
+ Res = WasmEdge_VMExecute(VMCxt, FuncName, Params, 2, Returns, 1);
+ WasmEdge_StringDelete(FuncName);
+ if (WasmEdge_ResultOK(Res)) {
+ printf("Get the '(%d + %d)^2' result: %d\n", 123, 456,
+ WasmEdge_ValueGetI32(Returns[0]));
+ } else {
+ printf("Execute 'add_and_square' error: %s\n",
+ WasmEdge_ResultGetMessage(Res));
+ }
+ /* Invoke the "sum_of_squares" to calculate (77^2 + 88^2) */
+ FuncName = WasmEdge_StringCreateByCString("sum_of_squares");
+ Params[0] = WasmEdge_ValueGenI32(77);
+ Params[1] = WasmEdge_ValueGenI32(88);
+ Res = WasmEdge_VMExecute(VMCxt, FuncName, Params, 2, Returns, 1);
+ WasmEdge_StringDelete(FuncName);
+ if (WasmEdge_ResultOK(Res)) {
+ printf("Get the '%d^2 + %d^2' result: %d\n", 77, 88,
+ WasmEdge_ValueGetI32(Returns[0]));
+ } else {
+ printf("Execute 'sum_of_squares' error: %s\n",
+ WasmEdge_ResultGetMessage(Res));
+ }
+
+ /* Resources deallocations. */
+ WasmEdge_VMDelete(VMCxt);
+ return 0;
+ }
+ ```
+
+ Then compile and execute:
+
+ ```bash
+ $ gcc test.c -lwasmedge
+ $ ./a.out
+ Get the '(123 + 456)^2' result: 335241
+ Get the '77^2 + 88^2' result: 13673
+ ```
4. Instantiate the `lib.wasm` first
- ```c
- #include
- #include
-
- int main() {
- /* The result. */
- WasmEdge_Result Res;
-
- /* The params and returns. */
- WasmEdge_Value Params[2], Returns[1];
-
- /* Create the VM context. */
- WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(NULL, NULL);
-
- /* Assume that the `lib.wasm` has instantiated first. */
- WasmEdge_LoaderContext *LoadCxt = WasmEdge_LoaderCreate(NULL);
- WasmEdge_ValidatorContext *ValidCxt = WasmEdge_ValidatorCreate(NULL);
- WasmEdge_ExecutorContext *ExecCxt = WasmEdge_ExecutorCreate(NULL, NULL);
- WasmEdge_StoreContext *StoreCxt = WasmEdge_StoreCreate();
- WasmEdge_ASTModuleContext *LibASTCxt = NULL;
- WasmEdge_ModuleInstanceContext *LibInstCxt = NULL;
- Res = WasmEdge_LoaderParseFromFile(LoadCxt, &LibASTCxt, "lib.wasm");
- WasmEdge_LoaderDelete(LoadCxt);
- if (!WasmEdge_ResultOK(Res)) {
- printf("Load lib.wasm error: %s\n", WasmEdge_ResultGetMessage(Res));
- return -1;
- }
- Res = WasmEdge_ValidatorValidate(ValidCxt, LibASTCxt);
- WasmEdge_ValidatorDelete(ValidCxt);
- if (!WasmEdge_ResultOK(Res)) {
- WasmEdge_ASTModuleDelete(LibASTCxt);
- printf("Validate lib.wasm error: %s\n", WasmEdge_ResultGetMessage(Res));
- return -1;
- }
- /*
- * The module name is determined when instantiation.
- * If use the `WasmEdge_ExecutorInstantiate` API, the module name will be
- * "".
- */
- WasmEdge_String ModuleName = WasmEdge_StringCreateByCString("math");
- Res = WasmEdge_ExecutorRegister(ExecCxt, &LibInstCxt, StoreCxt, LibASTCxt,
- ModuleName);
- WasmEdge_ExecutorDelete(ExecCxt);
- WasmEdge_ASTModuleDelete(LibASTCxt);
- WasmEdge_StringDelete(ModuleName);
- WasmEdge_StoreDelete(StoreCxt);
- if (!WasmEdge_ResultOK(Res)) {
- printf("Instantiate lib.wasm error: %s\n",
- WasmEdge_ResultGetMessage(Res));
- return -1;
- }
-
- /* Register the module instance with the module name "math". */
- /* The module name has determined when instantiating the `lib.wasm`. */
- Res = WasmEdge_VMRegisterModuleFromImport(VMCxt, LibInstCxt);
- if (!WasmEdge_ResultOK(Res)) {
- WasmEdge_VMDelete(VMCxt);
- WasmEdge_ModuleInstanceDelete(LibInstCxt);
- printf("Register lib.wasm error: %s\n", WasmEdge_ResultGetMessage(Res));
- return -1;
- }
-
- /* Instantiate the `test.wasm`. */
- /*
- * Developers can use the APIs such as `WasmEdge_VMRunWasmFromFile` to
- * instantiate and execute quickly.
- */
- Res = WasmEdge_VMLoadWasmFromFile(VMCxt, "test.wasm");
- if (!WasmEdge_ResultOK(Res)) {
- WasmEdge_VMDelete(VMCxt);
- WasmEdge_ModuleInstanceDelete(LibInstCxt);
- printf("Load test.wasm error: %s\n", WasmEdge_ResultGetMessage(Res));
- return -1;
- }
- Res = WasmEdge_VMValidate(VMCxt);
- if (!WasmEdge_ResultOK(Res)) {
- WasmEdge_VMDelete(VMCxt);
- WasmEdge_ModuleInstanceDelete(LibInstCxt);
- printf("Validate test.wasm error: %s\n",
- WasmEdge_ResultGetMessage(Res));
- return -1;
- }
- Res = WasmEdge_VMInstantiate(VMCxt);
- if (!WasmEdge_ResultOK(Res)) {
- WasmEdge_VMDelete(VMCxt);
- WasmEdge_ModuleInstanceDelete(LibInstCxt);
- printf("Instantiate test.wasm error: %s\n",
- WasmEdge_ResultGetMessage(Res));
- return -1;
- }
-
- /* Invoke the functions. */
- /* Invoke the "add_and_square" to calculate (123 + 456)^2 */
- WasmEdge_String FuncName =
- WasmEdge_StringCreateByCString("add_and_square");
- Params[0] = WasmEdge_ValueGenI32(123);
- Params[1] = WasmEdge_ValueGenI32(456);
- Res = WasmEdge_VMExecute(VMCxt, FuncName, Params, 2, Returns, 1);
- WasmEdge_StringDelete(FuncName);
- if (WasmEdge_ResultOK(Res)) {
- printf("Get the '(%d + %d)^2' result: %d\n", 123, 456,
- WasmEdge_ValueGetI32(Returns[0]));
- } else {
- printf("Execute 'add_and_square' error: %s\n",
- WasmEdge_ResultGetMessage(Res));
- }
- /* Invoke the "sum_of_squares" to calculate (77^2 + 88^2) */
- FuncName = WasmEdge_StringCreateByCString("sum_of_squares");
- Params[0] = WasmEdge_ValueGenI32(77);
- Params[1] = WasmEdge_ValueGenI32(88);
- Res = WasmEdge_VMExecute(VMCxt, FuncName, Params, 2, Returns, 1);
- WasmEdge_StringDelete(FuncName);
- if (WasmEdge_ResultOK(Res)) {
- printf("Get the '%d^2 + %d^2' result: %d\n", 77, 88,
- WasmEdge_ValueGetI32(Returns[0]));
- } else {
- printf("Execute 'sum_of_squares' error: %s\n",
- WasmEdge_ResultGetMessage(Res));
- }
-
- /* Resources deallocations. */
- WasmEdge_VMDelete(VMCxt);
- /* The imported module instance should be destroyed. */
- WasmEdge_ModuleInstanceDelete(LibInstCxt);
- return 0;
- }
- ```
-
- Then compile and execute:
-
- ```bash
- $ gcc test.c -lwasmedge
- $ ./a.out
- Get the '(123 + 456)^2' result: 335241
- Get the '77^2 + 88^2' result: 13673
- ```
+ ```c
+ #include
+ #include
+
+ int main() {
+ /* The result. */
+ WasmEdge_Result Res;
+
+ /* The params and returns. */
+ WasmEdge_Value Params[2], Returns[1];
+
+ /* Create the VM context. */
+ WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(NULL, NULL);
+
+ /* Assume that the `lib.wasm` has instantiated first. */
+ WasmEdge_LoaderContext *LoadCxt = WasmEdge_LoaderCreate(NULL);
+ WasmEdge_ValidatorContext *ValidCxt = WasmEdge_ValidatorCreate(NULL);
+ WasmEdge_ExecutorContext *ExecCxt = WasmEdge_ExecutorCreate(NULL, NULL);
+ WasmEdge_StoreContext *StoreCxt = WasmEdge_StoreCreate();
+ WasmEdge_ASTModuleContext *LibASTCxt = NULL;
+ WasmEdge_ModuleInstanceContext *LibInstCxt = NULL;
+ Res = WasmEdge_LoaderParseFromFile(LoadCxt, &LibASTCxt, "lib.wasm");
+ WasmEdge_LoaderDelete(LoadCxt);
+ if (!WasmEdge_ResultOK(Res)) {
+ printf("Load lib.wasm error: %s\n", WasmEdge_ResultGetMessage(Res));
+ return -1;
+ }
+ Res = WasmEdge_ValidatorValidate(ValidCxt, LibASTCxt);
+ WasmEdge_ValidatorDelete(ValidCxt);
+ if (!WasmEdge_ResultOK(Res)) {
+ WasmEdge_ASTModuleDelete(LibASTCxt);
+ printf("Validate lib.wasm error: %s\n", WasmEdge_ResultGetMessage(Res));
+ return -1;
+ }
+ /*
+ * The module name is determined when instantiation.
+ * If use the `WasmEdge_ExecutorInstantiate` API, the module name will be
+ * "".
+ */
+ WasmEdge_String ModuleName = WasmEdge_StringCreateByCString("math");
+ Res = WasmEdge_ExecutorRegister(ExecCxt, &LibInstCxt, StoreCxt, LibASTCxt,
+ ModuleName);
+ WasmEdge_ExecutorDelete(ExecCxt);
+ WasmEdge_ASTModuleDelete(LibASTCxt);
+ WasmEdge_StringDelete(ModuleName);
+ WasmEdge_StoreDelete(StoreCxt);
+ if (!WasmEdge_ResultOK(Res)) {
+ printf("Instantiate lib.wasm error: %s\n",
+ WasmEdge_ResultGetMessage(Res));
+ return -1;
+ }
+
+ /* Register the module instance with the module name "math". */
+ /* The module name has determined when instantiating the `lib.wasm`. */
+ Res = WasmEdge_VMRegisterModuleFromImport(VMCxt, LibInstCxt);
+ if (!WasmEdge_ResultOK(Res)) {
+ WasmEdge_VMDelete(VMCxt);
+ WasmEdge_ModuleInstanceDelete(LibInstCxt);
+ printf("Register lib.wasm error: %s\n", WasmEdge_ResultGetMessage(Res));
+ return -1;
+ }
+
+ /* Instantiate the `test.wasm`. */
+ /*
+ * Developers can use the APIs such as `WasmEdge_VMRunWasmFromFile` to
+ * instantiate and execute quickly.
+ */
+ Res = WasmEdge_VMLoadWasmFromFile(VMCxt, "test.wasm");
+ if (!WasmEdge_ResultOK(Res)) {
+ WasmEdge_VMDelete(VMCxt);
+ WasmEdge_ModuleInstanceDelete(LibInstCxt);
+ printf("Load test.wasm error: %s\n", WasmEdge_ResultGetMessage(Res));
+ return -1;
+ }
+ Res = WasmEdge_VMValidate(VMCxt);
+ if (!WasmEdge_ResultOK(Res)) {
+ WasmEdge_VMDelete(VMCxt);
+ WasmEdge_ModuleInstanceDelete(LibInstCxt);
+ printf("Validate test.wasm error: %s\n",
+ WasmEdge_ResultGetMessage(Res));
+ return -1;
+ }
+ Res = WasmEdge_VMInstantiate(VMCxt);
+ if (!WasmEdge_ResultOK(Res)) {
+ WasmEdge_VMDelete(VMCxt);
+ WasmEdge_ModuleInstanceDelete(LibInstCxt);
+ printf("Instantiate test.wasm error: %s\n",
+ WasmEdge_ResultGetMessage(Res));
+ return -1;
+ }
+
+ /* Invoke the functions. */
+ /* Invoke the "add_and_square" to calculate (123 + 456)^2 */
+ WasmEdge_String FuncName =
+ WasmEdge_StringCreateByCString("add_and_square");
+ Params[0] = WasmEdge_ValueGenI32(123);
+ Params[1] = WasmEdge_ValueGenI32(456);
+ Res = WasmEdge_VMExecute(VMCxt, FuncName, Params, 2, Returns, 1);
+ WasmEdge_StringDelete(FuncName);
+ if (WasmEdge_ResultOK(Res)) {
+ printf("Get the '(%d + %d)^2' result: %d\n", 123, 456,
+ WasmEdge_ValueGetI32(Returns[0]));
+ } else {
+ printf("Execute 'add_and_square' error: %s\n",
+ WasmEdge_ResultGetMessage(Res));
+ }
+ /* Invoke the "sum_of_squares" to calculate (77^2 + 88^2) */
+ FuncName = WasmEdge_StringCreateByCString("sum_of_squares");
+ Params[0] = WasmEdge_ValueGenI32(77);
+ Params[1] = WasmEdge_ValueGenI32(88);
+ Res = WasmEdge_VMExecute(VMCxt, FuncName, Params, 2, Returns, 1);
+ WasmEdge_StringDelete(FuncName);
+ if (WasmEdge_ResultOK(Res)) {
+ printf("Get the '%d^2 + %d^2' result: %d\n", 77, 88,
+ WasmEdge_ValueGetI32(Returns[0]));
+ } else {
+ printf("Execute 'sum_of_squares' error: %s\n",
+ WasmEdge_ResultGetMessage(Res));
+ }
+
+ /* Resources deallocations. */
+ WasmEdge_VMDelete(VMCxt);
+ /* The imported module instance should be destroyed. */
+ WasmEdge_ModuleInstanceDelete(LibInstCxt);
+ return 0;
+ }
+ ```
+
+ Then compile and execute:
+
+ ```bash
+ $ gcc test.c -lwasmedge
+ $ ./a.out
+ Get the '(123 + 456)^2' result: 335241
+ Get the '77^2 + 88^2' result: 13673
+ ```
## Linking WASM Modules By the Executor Context
diff --git a/i18n/zh/docusaurus-plugin-content-docs/current/embed/c/reference/0.10.1.md b/i18n/zh/docusaurus-plugin-content-docs/current/embed/c/reference/0.10.1.md
index 8aeb0ed0..e91cb0db 100644
--- a/i18n/zh/docusaurus-plugin-content-docs/current/embed/c/reference/0.10.1.md
+++ b/i18n/zh/docusaurus-plugin-content-docs/current/embed/c/reference/0.10.1.md
@@ -10,41 +10,41 @@ sidebar_position: 5
## Table of Contents
-- [WasmEdge Installation](#wasmedge-installation)
- - [Download And Install](#download-and-install)
- - [Compile Sources](#compile-sources)
-- [WasmEdge Basics](#wasmedge-basics)
- - [Version](#version)
- - [Logging Settings](#logging-settings)
- - [Value Types](#value-types)
- - [Strings](#strings)
- - [Results](#results)
- - [Contexts](#contexts)
- - [WASM data structures](#wasm-data-structures)
- - [Async](#async)
- - [Configurations](#configurations)
- - [Statistics](#statistics)
- - [Tools driver](#tools-driver)
-- [WasmEdge VM](#wasmedge-vm)
- - [WASM Execution Example With VM Context](#wasm-execution-example-with-vm-context)
- - [VM Creations](#vm-creations)
- - [Preregistrations](#preregistrations)
- - [Host Module Registrations](#host-module-registrations)
- - [WASM Registrations And Executions](#wasm-registrations-and-executions)
- - [Asynchronous execution](#asynchronous-execution)
- - [Instance Tracing](#instance-tracing)
-- [WasmEdge Runtime](#wasmedge-runtime)
- - [WASM Execution Example Step-By-Step](#wasm-execution-example-step-by-step)
- - [Loader](#loader)
- - [Validator](#validator)
- - [Executor](#executor)
- - [AST Module](#ast-module)
- - [Store](#store)
- - [Instances](#instances)
- - [Host Functions](#host-functions)
-- [WasmEdge AOT Compiler](#wasmedge-aot-compiler)
- - [Compilation Example](#compilation-example)
- - [Compiler Options](#compiler-options)
+- [WasmEdge Installation](#wasmedge-installation)
+ - [Download And Install](#download-and-install)
+ - [Compile Sources](#compile-sources)
+- [WasmEdge Basics](#wasmedge-basics)
+ - [Version](#version)
+ - [Logging Settings](#logging-settings)
+ - [Value Types](#value-types)
+ - [Strings](#strings)
+ - [Results](#results)
+ - [Contexts](#contexts)
+ - [WASM data structures](#wasm-data-structures)
+ - [Async](#async)
+ - [Configurations](#configurations)
+ - [Statistics](#statistics)
+ - [Tools driver](#tools-driver)
+- [WasmEdge VM](#wasmedge-vm)
+ - [WASM Execution Example With VM Context](#wasm-execution-example-with-vm-context)
+ - [VM Creations](#vm-creations)
+ - [Preregistrations](#preregistrations)
+ - [Host Module Registrations](#host-module-registrations)
+ - [WASM Registrations And Executions](#wasm-registrations-and-executions)
+ - [Asynchronous execution](#asynchronous-execution)
+ - [Instance Tracing](#instance-tracing)
+- [WasmEdge Runtime](#wasmedge-runtime)
+ - [WASM Execution Example Step-By-Step](#wasm-execution-example-step-by-step)
+ - [Loader](#loader)
+ - [Validator](#validator)
+ - [Executor](#executor)
+ - [AST Module](#ast-module)
+ - [Store](#store)
+ - [Instances](#instances)
+ - [Host Functions](#host-functions)
+- [WasmEdge AOT Compiler](#wasmedge-aot-compiler)
+ - [Compilation Example](#compilation-example)
+ - [Compiler Options](#compiler-options)
## WasmEdge Installation
@@ -64,27 +64,27 @@ After the installation of WasmEdge, the following guide can help you to test for
1. Prepare the test C file (and assumed saved as `test.c`):
- ```c
- #include
- #include
- int main() {
- printf("WasmEdge version: %s\n", WasmEdge_VersionGet());
- return 0;
- }
- ```
+ ```c
+ #include
+ #include
+ int main() {
+ printf("WasmEdge version: %s\n", WasmEdge_VersionGet());
+ return 0;
+ }
+ ```
2. Compile the file with `gcc` or `clang`.
- ```bash
- gcc test.c -lwasmedge_c
- ```
+ ```bash
+ gcc test.c -lwasmedge_c
+ ```
3. Run and get the expected output.
- ```bash
- $ ./a.out
- WasmEdge version: 0.10.1
- ```
+ ```bash
+ $ ./a.out
+ WasmEdge version: 0.10.1
+ ```
## WasmEdge Basics
@@ -112,53 +112,53 @@ In WasmEdge, developers should convert the values to `WasmEdge_Value` objects th
1. Number types: `i32`, `i64`, `f32`, `f64`, and `v128` for the `SIMD` proposal
- ```c
- WasmEdge_Value Val;
- Val = WasmEdge_ValueGenI32(123456);
- printf("%d\n", WasmEdge_ValueGetI32(Val));
- /* Will print "123456" */
- Val = WasmEdge_ValueGenI64(1234567890123LL);
- printf("%ld\n", WasmEdge_ValueGetI64(Val));
- /* Will print "1234567890123" */
- Val = WasmEdge_ValueGenF32(123.456f);
- printf("%f\n", WasmEdge_ValueGetF32(Val));
- /* Will print "123.456001" */
- Val = WasmEdge_ValueGenF64(123456.123456789);
- printf("%.10f\n", WasmEdge_ValueGetF64(Val));
- /* Will print "123456.1234567890" */
- ```
+ ```c
+ WasmEdge_Value Val;
+ Val = WasmEdge_ValueGenI32(123456);
+ printf("%d\n", WasmEdge_ValueGetI32(Val));
+ /* Will print "123456" */
+ Val = WasmEdge_ValueGenI64(1234567890123LL);
+ printf("%ld\n", WasmEdge_ValueGetI64(Val));
+ /* Will print "1234567890123" */
+ Val = WasmEdge_ValueGenF32(123.456f);
+ printf("%f\n", WasmEdge_ValueGetF32(Val));
+ /* Will print "123.456001" */
+ Val = WasmEdge_ValueGenF64(123456.123456789);
+ printf("%.10f\n", WasmEdge_ValueGetF64(Val));
+ /* Will print "123456.1234567890" */
+ ```
2. Reference types: `funcref` and `externref` for the `Reference-Types` proposal
- ```c
- WasmEdge_Value Val;
- void *Ptr;
- bool IsNull;
- uint32_t Num = 10;
- /* Generate a externref to NULL. */
- Val = WasmEdge_ValueGenNullRef(WasmEdge_RefType_ExternRef);
- IsNull = WasmEdge_ValueIsNullRef(Val);
- /* The `IsNull` will be `TRUE`. */
- Ptr = WasmEdge_ValueGetExternRef(Val);
- /* The `Ptr` will be `NULL`. */
-
- /* Get the function instance by creation or from module instance. */
- const WasmEdge_FunctionInstanceContext *FuncCxt = ...;
- /* Generate a funcref with the given function instance context. */
- Val = WasmEdge_ValueGenFuncRef(FuncCxt);
- const WasmEdge_FunctionInstanceContext *GotFuncCxt = WasmEdge_ValueGetFuncRef(Val);
- /* The `GotFuncCxt` will be the same as `FuncCxt`. */
-
- /* Generate a externref to `Num`. */
- Val = WasmEdge_ValueGenExternRef(&Num);
- Ptr = WasmEdge_ValueGetExternRef(Val);
- /* The `Ptr` will be `&Num`. */
- printf("%u\n", *(uint32_t *)Ptr);
- /* Will print "10" */
- Num += 55;
- printf("%u\n", *(uint32_t *)Ptr);
- /* Will print "65" */
- ```
+ ```c
+ WasmEdge_Value Val;
+ void *Ptr;
+ bool IsNull;
+ uint32_t Num = 10;
+ /* Generate a externref to NULL. */
+ Val = WasmEdge_ValueGenNullRef(WasmEdge_RefType_ExternRef);
+ IsNull = WasmEdge_ValueIsNullRef(Val);
+ /* The `IsNull` will be `TRUE`. */
+ Ptr = WasmEdge_ValueGetExternRef(Val);
+ /* The `Ptr` will be `NULL`. */
+
+ /* Get the function instance by creation or from module instance. */
+ const WasmEdge_FunctionInstanceContext *FuncCxt = ...;
+ /* Generate a funcref with the given function instance context. */
+ Val = WasmEdge_ValueGenFuncRef(FuncCxt);
+ const WasmEdge_FunctionInstanceContext *GotFuncCxt = WasmEdge_ValueGetFuncRef(Val);
+ /* The `GotFuncCxt` will be the same as `FuncCxt`. */
+
+ /* Generate a externref to `Num`. */
+ Val = WasmEdge_ValueGenExternRef(&Num);
+ Ptr = WasmEdge_ValueGetExternRef(Val);
+ /* The `Ptr` will be `&Num`. */
+ printf("%u\n", *(uint32_t *)Ptr);
+ /* Will print "10" */
+ Num += 55;
+ printf("%u\n", *(uint32_t *)Ptr);
+ /* Will print "65" */
+ ```
### Strings
@@ -166,49 +166,49 @@ The `WasmEdge_String` object is for the instance names when invoking a WASM func
1. Create a `WasmEdge_String` from a C string (`const char *` with NULL termination) or a buffer with length.
- The content of the C string or buffer will be copied into the `WasmEdge_String` object.
+ The content of the C string or buffer will be copied into the `WasmEdge_String` object.
- ```c
- char Buf[4] = {50, 55, 60, 65};
- WasmEdge_String Str1 = WasmEdge_StringCreateByCString("test");
- WasmEdge_String Str2 = WasmEdge_StringCreateByBuffer(Buf, 4);
- /* The objects should be deleted by `WasmEdge_StringDelete()`. */
- WasmEdge_StringDelete(Str1);
- WasmEdge_StringDelete(Str2);
- ```
+ ```c
+ char Buf[4] = {50, 55, 60, 65};
+ WasmEdge_String Str1 = WasmEdge_StringCreateByCString("test");
+ WasmEdge_String Str2 = WasmEdge_StringCreateByBuffer(Buf, 4);
+ /* The objects should be deleted by `WasmEdge_StringDelete()`. */
+ WasmEdge_StringDelete(Str1);
+ WasmEdge_StringDelete(Str2);
+ ```
2. Wrap a `WasmEdge_String` to a buffer with length.
- The content will not be copied, and the caller should guarantee the life cycle of the input buffer.
+ The content will not be copied, and the caller should guarantee the life cycle of the input buffer.
- ```c
- const char CStr[] = "test";
- WasmEdge_String Str = WasmEdge_StringWrap(CStr, 4);
- /* The object should __NOT__ be deleted by `WasmEdge_StringDelete()`. */
- ```
+ ```c
+ const char CStr[] = "test";
+ WasmEdge_String Str = WasmEdge_StringWrap(CStr, 4);
+ /* The object should __NOT__ be deleted by `WasmEdge_StringDelete()`. */
+ ```
3. String comparison
- ```c
- const char CStr[] = "abcd";
- char Buf[4] = {0x61, 0x62, 0x63, 0x64};
- WasmEdge_String Str1 = WasmEdge_StringWrap(CStr, 4);
- WasmEdge_String Str2 = WasmEdge_StringCreateByBuffer(Buf, 4);
- bool IsEq = WasmEdge_StringIsEqual(Str1, Str2);
- /* The `IsEq` will be `TRUE`. */
- WasmEdge_StringDelete(Str2);
- ```
+ ```c
+ const char CStr[] = "abcd";
+ char Buf[4] = {0x61, 0x62, 0x63, 0x64};
+ WasmEdge_String Str1 = WasmEdge_StringWrap(CStr, 4);
+ WasmEdge_String Str2 = WasmEdge_StringCreateByBuffer(Buf, 4);
+ bool IsEq = WasmEdge_StringIsEqual(Str1, Str2);
+ /* The `IsEq` will be `TRUE`. */
+ WasmEdge_StringDelete(Str2);
+ ```
4. Convert to C string
- ```c
- char Buf[256];
- WasmEdge_String Str = WasmEdge_StringCreateByCString("test_wasmedge_string");
- uint32_t StrLength = WasmEdge_StringCopy(Str, Buf, sizeof(Buf));
- /* StrLength will be 20 */
- printf("String: %s\n", Buf);
- /* Will print "test_wasmedge_string". */
- ```
+ ```c
+ char Buf[256];
+ WasmEdge_String Str = WasmEdge_StringCreateByCString("test_wasmedge_string");
+ uint32_t StrLength = WasmEdge_StringCopy(Str, Buf, sizeof(Buf));
+ /* StrLength will be 20 */
+ printf("String: %s\n", Buf);
+ /* Will print "test_wasmedge_string". */
+ ```
### Results
@@ -243,145 +243,145 @@ The WASM data structures are used for creating instances or can be queried from
1. Limit
- The `WasmEdge_Limit` struct is defined in the header:
+ The `WasmEdge_Limit` struct is defined in the header:
- ```c
- /// Struct of WASM limit.
- typedef struct WasmEdge_Limit {
- /// Boolean to describe has max value or not.
- bool HasMax;
- /// Boolean to describe is shared memory or not.
- bool Shared;
- /// Minimum value.
- uint32_t Min;
- /// Maximum value. Will be ignored if the `HasMax` is false.
- uint32_t Max;
- } WasmEdge_Limit;
- ```
+ ```c
+ /// Struct of WASM limit.
+ typedef struct WasmEdge_Limit {
+ /// Boolean to describe has max value or not.
+ bool HasMax;
+ /// Boolean to describe is shared memory or not.
+ bool Shared;
+ /// Minimum value.
+ uint32_t Min;
+ /// Maximum value. Will be ignored if the `HasMax` is false.
+ uint32_t Max;
+ } WasmEdge_Limit;
+ ```
- Developers can initialize the struct by assigning it's value, and the `Max` value is needed to be larger or equal to the `Min` value. The API `WasmEdge_LimitIsEqual()` is provided to compare with 2 `WasmEdge_Limit` structs.
+ Developers can initialize the struct by assigning it's value, and the `Max` value is needed to be larger or equal to the `Min` value. The API `WasmEdge_LimitIsEqual()` is provided to compare with 2 `WasmEdge_Limit` structs.
2. Function type context
- The `Function Type` context is used for the `Function` creation, checking the value types of a `Function` instance, or getting the function type with function name from VM. Developers can use the `Function Type` context APIs to get the parameter or return value types information.
+ The `Function Type` context is used for the `Function` creation, checking the value types of a `Function` instance, or getting the function type with function name from VM. Developers can use the `Function Type` context APIs to get the parameter or return value types information.
- ```c
- enum WasmEdge_ValType ParamList[2] = { WasmEdge_ValType_I32, WasmEdge_ValType_I64 };
- enum WasmEdge_ValType ReturnList[1] = { WasmEdge_ValType_FuncRef };
- WasmEdge_FunctionTypeContext *FuncTypeCxt = WasmEdge_FunctionTypeCreate(ParamList, 2, ReturnList, 1);
+ ```c
+ enum WasmEdge_ValType ParamList[2] = { WasmEdge_ValType_I32, WasmEdge_ValType_I64 };
+ enum WasmEdge_ValType ReturnList[1] = { WasmEdge_ValType_FuncRef };
+ WasmEdge_FunctionTypeContext *FuncTypeCxt = WasmEdge_FunctionTypeCreate(ParamList, 2, ReturnList, 1);
- enum WasmEdge_ValType Buf[16];
- uint32_t ParamLen = WasmEdge_FunctionTypeGetParametersLength(FuncTypeCxt);
- /* `ParamLen` will be 2. */
- uint32_t GotParamLen = WasmEdge_FunctionTypeGetParameters(FuncTypeCxt, Buf, 16);
- /* `GotParamLen` will be 2, and `Buf[0]` and `Buf[1]` will be the same as `ParamList`. */
- uint32_t ReturnLen = WasmEdge_FunctionTypeGetReturnsLength(FuncTypeCxt);
- /* `ReturnLen` will be 1. */
- uint32_t GotReturnLen = WasmEdge_FunctionTypeGetReturns(FuncTypeCxt, Buf, 16);
- /* `GotReturnLen` will be 1, and `Buf[0]` will be the same as `ReturnList`. */
+ enum WasmEdge_ValType Buf[16];
+ uint32_t ParamLen = WasmEdge_FunctionTypeGetParametersLength(FuncTypeCxt);
+ /* `ParamLen` will be 2. */
+ uint32_t GotParamLen = WasmEdge_FunctionTypeGetParameters(FuncTypeCxt, Buf, 16);
+ /* `GotParamLen` will be 2, and `Buf[0]` and `Buf[1]` will be the same as `ParamList`. */
+ uint32_t ReturnLen = WasmEdge_FunctionTypeGetReturnsLength(FuncTypeCxt);
+ /* `ReturnLen` will be 1. */
+ uint32_t GotReturnLen = WasmEdge_FunctionTypeGetReturns(FuncTypeCxt, Buf, 16);
+ /* `GotReturnLen` will be 1, and `Buf[0]` will be the same as `ReturnList`. */
- WasmEdge_FunctionTypeDelete(FuncTypeCxt);
- ```
+ WasmEdge_FunctionTypeDelete(FuncTypeCxt);
+ ```
3. Table type context
- The `Table Type` context is used for `Table` instance creation or getting information from `Table` instances.
+ The `Table Type` context is used for `Table` instance creation or getting information from `Table` instances.
- ```c
- WasmEdge_Limit TabLim = {.HasMax = true, .Shared = false, .Min = 10, .Max = 20};
- WasmEdge_TableTypeContext *TabTypeCxt = WasmEdge_TableTypeCreate(WasmEdge_RefType_ExternRef, TabLim);
+ ```c
+ WasmEdge_Limit TabLim = {.HasMax = true, .Shared = false, .Min = 10, .Max = 20};
+ WasmEdge_TableTypeContext *TabTypeCxt = WasmEdge_TableTypeCreate(WasmEdge_RefType_ExternRef, TabLim);
- enum WasmEdge_RefType GotRefType = WasmEdge_TableTypeGetRefType(TabTypeCxt);
- /* `GotRefType` will be WasmEdge_RefType_ExternRef. */
- WasmEdge_Limit GotTabLim = WasmEdge_TableTypeGetLimit(TabTypeCxt);
- /* `GotTabLim` will be the same value as `TabLim`. */
+ enum WasmEdge_RefType GotRefType = WasmEdge_TableTypeGetRefType(TabTypeCxt);
+ /* `GotRefType` will be WasmEdge_RefType_ExternRef. */
+ WasmEdge_Limit GotTabLim = WasmEdge_TableTypeGetLimit(TabTypeCxt);
+ /* `GotTabLim` will be the same value as `TabLim`. */
- WasmEdge_TableTypeDelete(TabTypeCxt);
- ```
+ WasmEdge_TableTypeDelete(TabTypeCxt);
+ ```
4. Memory type context
- The `Memory Type` context is used for `Memory` instance creation or getting information from `Memory` instances.
+ The `Memory Type` context is used for `Memory` instance creation or getting information from `Memory` instances.
- ```c
- WasmEdge_Limit MemLim = {.HasMax = true, .Shared = false, .Min = 10, .Max = 20};
- WasmEdge_MemoryTypeContext *MemTypeCxt = WasmEdge_MemoryTypeCreate(MemLim);
+ ```c
+ WasmEdge_Limit MemLim = {.HasMax = true, .Shared = false, .Min = 10, .Max = 20};
+ WasmEdge_MemoryTypeContext *MemTypeCxt = WasmEdge_MemoryTypeCreate(MemLim);
- WasmEdge_Limit GotMemLim = WasmEdge_MemoryTypeGetLimit(MemTypeCxt);
- /* `GotMemLim` will be the same value as `MemLim`. */
+ WasmEdge_Limit GotMemLim = WasmEdge_MemoryTypeGetLimit(MemTypeCxt);
+ /* `GotMemLim` will be the same value as `MemLim`. */
- WasmEdge_MemoryTypeDelete(MemTypeCxt)
- ```
+ WasmEdge_MemoryTypeDelete(MemTypeCxt)
+ ```
5. Global type context
- The `Global Type` context is used for `Global` instance creation or getting information from `Global` instances.
+ The `Global Type` context is used for `Global` instance creation or getting information from `Global` instances.
- ```c
- WasmEdge_GlobalTypeContext *GlobTypeCxt = WasmEdge_GlobalTypeCreate(WasmEdge_ValType_F64, WasmEdge_Mutability_Var);
+ ```c
+ WasmEdge_GlobalTypeContext *GlobTypeCxt = WasmEdge_GlobalTypeCreate(WasmEdge_ValType_F64, WasmEdge_Mutability_Var);
- WasmEdge_ValType GotValType = WasmEdge_GlobalTypeGetValType(GlobTypeCxt);
- /* `GotValType` will be WasmEdge_ValType_F64. */
- WasmEdge_Mutability GotValMut = WasmEdge_GlobalTypeGetMutability(GlobTypeCxt);
- /* `GotValMut` will be WasmEdge_Mutability_Var. */
+ WasmEdge_ValType GotValType = WasmEdge_GlobalTypeGetValType(GlobTypeCxt);
+ /* `GotValType` will be WasmEdge_ValType_F64. */
+ WasmEdge_Mutability GotValMut = WasmEdge_GlobalTypeGetMutability(GlobTypeCxt);
+ /* `GotValMut` will be WasmEdge_Mutability_Var. */
- WasmEdge_GlobalTypeDelete(GlobTypeCxt);
- ```
+ WasmEdge_GlobalTypeDelete(GlobTypeCxt);
+ ```
6. Import type context
- The `Import Type` context is used for getting the imports information from a [AST Module](#ast-module). Developers can get the external type (`function`, `table`, `memory`, or `global`), import module name, and external name from an `Import Type` context. The details about querying `Import Type` contexts will be introduced in the [AST Module](#ast-module).
+ The `Import Type` context is used for getting the imports information from a [AST Module](#ast-module). Developers can get the external type (`function`, `table`, `memory`, or `global`), import module name, and external name from an `Import Type` context. The details about querying `Import Type` contexts will be introduced in the [AST Module](#ast-module).
- ```c
- WasmEdge_ASTModuleContext *ASTCxt = ...;
- /* Assume that `ASTCxt` is returned by the `WasmEdge_LoaderContext` for the result of loading a WASM file. */
- const WasmEdge_ImportTypeContext *ImpType = ...;
- /* Assume that `ImpType` is queried from the `ASTCxt` for the import. */
+ ```c
+ WasmEdge_ASTModuleContext *ASTCxt = ...;
+ /* Assume that `ASTCxt` is returned by the `WasmEdge_LoaderContext` for the result of loading a WASM file. */
+ const WasmEdge_ImportTypeContext *ImpType = ...;
+ /* Assume that `ImpType` is queried from the `ASTCxt` for the import. */
- enum WasmEdge_ExternalType ExtType = WasmEdge_ImportTypeGetExternalType(ImpType);
- /*
- * The `ExtType` can be one of `WasmEdge_ExternalType_Function`, `WasmEdge_ExternalType_Table`,
- * `WasmEdge_ExternalType_Memory`, or `WasmEdge_ExternalType_Global`.
- */
- WasmEdge_String ModName = WasmEdge_ImportTypeGetModuleName(ImpType);
- WasmEdge_String ExtName = WasmEdge_ImportTypeGetExternalName(ImpType);
- /* The `ModName` and `ExtName` should not be destroyed and the string buffers are binded into the `ASTCxt`. */
- const WasmEdge_FunctionTypeContext *FuncTypeCxt = WasmEdge_ImportTypeGetFunctionType(ASTCxt, ImpType);
- /* If the `ExtType` is not `WasmEdge_ExternalType_Function`, the `FuncTypeCxt` will be NULL. */
- const WasmEdge_TableTypeContext *TabTypeCxt = WasmEdge_ImportTypeGetTableType(ASTCxt, ImpType);
- /* If the `ExtType` is not `WasmEdge_ExternalType_Table`, the `TabTypeCxt` will be NULL. */
- const WasmEdge_MemoryTypeContext *MemTypeCxt = WasmEdge_ImportTypeGetMemoryType(ASTCxt, ImpType);
- /* If the `ExtType` is not `WasmEdge_ExternalType_Memory`, the `MemTypeCxt` will be NULL. */
- const WasmEdge_GlobalTypeContext *GlobTypeCxt = WasmEdge_ImportTypeGetGlobalType(ASTCxt, ImpType);
- /* If the `ExtType` is not `WasmEdge_ExternalType_Global`, the `GlobTypeCxt` will be NULL. */
- ```
+ enum WasmEdge_ExternalType ExtType = WasmEdge_ImportTypeGetExternalType(ImpType);
+ /*
+ * The `ExtType` can be one of `WasmEdge_ExternalType_Function`, `WasmEdge_ExternalType_Table`,
+ * `WasmEdge_ExternalType_Memory`, or `WasmEdge_ExternalType_Global`.
+ */
+ WasmEdge_String ModName = WasmEdge_ImportTypeGetModuleName(ImpType);
+ WasmEdge_String ExtName = WasmEdge_ImportTypeGetExternalName(ImpType);
+ /* The `ModName` and `ExtName` should not be destroyed and the string buffers are binded into the `ASTCxt`. */
+ const WasmEdge_FunctionTypeContext *FuncTypeCxt = WasmEdge_ImportTypeGetFunctionType(ASTCxt, ImpType);
+ /* If the `ExtType` is not `WasmEdge_ExternalType_Function`, the `FuncTypeCxt` will be NULL. */
+ const WasmEdge_TableTypeContext *TabTypeCxt = WasmEdge_ImportTypeGetTableType(ASTCxt, ImpType);
+ /* If the `ExtType` is not `WasmEdge_ExternalType_Table`, the `TabTypeCxt` will be NULL. */
+ const WasmEdge_MemoryTypeContext *MemTypeCxt = WasmEdge_ImportTypeGetMemoryType(ASTCxt, ImpType);
+ /* If the `ExtType` is not `WasmEdge_ExternalType_Memory`, the `MemTypeCxt` will be NULL. */
+ const WasmEdge_GlobalTypeContext *GlobTypeCxt = WasmEdge_ImportTypeGetGlobalType(ASTCxt, ImpType);
+ /* If the `ExtType` is not `WasmEdge_ExternalType_Global`, the `GlobTypeCxt` will be NULL. */
+ ```
7. Export type context
- The `Export Type` context is used for getting the exports information from a [AST Module](#ast-module). Developers can get the external type (`function`, `table`, `memory`, or `global`) and external name from an `Export Type` context. The details about querying `Export Type` contexts will be introduced in the [AST Module](#ast-module).
+ The `Export Type` context is used for getting the exports information from a [AST Module](#ast-module). Developers can get the external type (`function`, `table`, `memory`, or `global`) and external name from an `Export Type` context. The details about querying `Export Type` contexts will be introduced in the [AST Module](#ast-module).
- ```c
- WasmEdge_ASTModuleContext *ASTCxt = ...;
- /* Assume that `ASTCxt` is returned by the `WasmEdge_LoaderContext` for the result of loading a WASM file. */
- const WasmEdge_ExportTypeContext *ExpType = ...;
- /* Assume that `ExpType` is queried from the `ASTCxt` for the export. */
+ ```c
+ WasmEdge_ASTModuleContext *ASTCxt = ...;
+ /* Assume that `ASTCxt` is returned by the `WasmEdge_LoaderContext` for the result of loading a WASM file. */
+ const WasmEdge_ExportTypeContext *ExpType = ...;
+ /* Assume that `ExpType` is queried from the `ASTCxt` for the export. */
- enum WasmEdge_ExternalType ExtType = WasmEdge_ExportTypeGetExternalType(ExpType);
- /*
- * The `ExtType` can be one of `WasmEdge_ExternalType_Function`, `WasmEdge_ExternalType_Table`,
- * `WasmEdge_ExternalType_Memory`, or `WasmEdge_ExternalType_Global`.
- */
- WasmEdge_String ExtName = WasmEdge_ExportTypeGetExternalName(ExpType);
- /* The `ExtName` should not be destroyed and the string buffer is binded into the `ASTCxt`. */
- const WasmEdge_FunctionTypeContext *FuncTypeCxt = WasmEdge_ExportTypeGetFunctionType(ASTCxt, ExpType);
- /* If the `ExtType` is not `WasmEdge_ExternalType_Function`, the `FuncTypeCxt` will be NULL. */
- const WasmEdge_TableTypeContext *TabTypeCxt = WasmEdge_ExportTypeGetTableType(ASTCxt, ExpType);
- /* If the `ExtType` is not `WasmEdge_ExternalType_Table`, the `TabTypeCxt` will be NULL. */
- const WasmEdge_MemoryTypeContext *MemTypeCxt = WasmEdge_ExportTypeGetMemoryType(ASTCxt, ExpType);
- /* If the `ExtType` is not `WasmEdge_ExternalType_Memory`, the `MemTypeCxt` will be NULL. */
- const WasmEdge_GlobalTypeContext *GlobTypeCxt = WasmEdge_ExportTypeGetGlobalType(ASTCxt, ExpType);
- /* If the `ExtType` is not `WasmEdge_ExternalType_Global`, the `GlobTypeCxt` will be NULL. */
- ```
+ enum WasmEdge_ExternalType ExtType = WasmEdge_ExportTypeGetExternalType(ExpType);
+ /*
+ * The `ExtType` can be one of `WasmEdge_ExternalType_Function`, `WasmEdge_ExternalType_Table`,
+ * `WasmEdge_ExternalType_Memory`, or `WasmEdge_ExternalType_Global`.
+ */
+ WasmEdge_String ExtName = WasmEdge_ExportTypeGetExternalName(ExpType);
+ /* The `ExtName` should not be destroyed and the string buffer is binded into the `ASTCxt`. */
+ const WasmEdge_FunctionTypeContext *FuncTypeCxt = WasmEdge_ExportTypeGetFunctionType(ASTCxt, ExpType);
+ /* If the `ExtType` is not `WasmEdge_ExternalType_Function`, the `FuncTypeCxt` will be NULL. */
+ const WasmEdge_TableTypeContext *TabTypeCxt = WasmEdge_ExportTypeGetTableType(ASTCxt, ExpType);
+ /* If the `ExtType` is not `WasmEdge_ExternalType_Table`, the `TabTypeCxt` will be NULL. */
+ const WasmEdge_MemoryTypeContext *MemTypeCxt = WasmEdge_ExportTypeGetMemoryType(ASTCxt, ExpType);
+ /* If the `ExtType` is not `WasmEdge_ExternalType_Memory`, the `MemTypeCxt` will be NULL. */
+ const WasmEdge_GlobalTypeContext *GlobTypeCxt = WasmEdge_ExportTypeGetGlobalType(ASTCxt, ExpType);
+ /* If the `ExtType` is not `WasmEdge_ExternalType_Global`, the `GlobTypeCxt` will be NULL. */
+ ```
### Async
@@ -389,54 +389,54 @@ After calling the [asynchronous execution APIs](#asynchronous-execution), develo
1. Wait for the asynchronous execution
- Developers can wait the execution until finished:
-
- ```c
- WasmEdge_Async *Async = ...; /* Ignored. Asynchronous execute a function. */
- /* Blocking and waiting for the execution. */
- WasmEdge_AsyncWait(Async);
- WasmEdge_AsyncDelete(Async);
- ```
-
- Or developers can wait for a time limit. If the time limit exceeded, developers can choose to cancel the execution. For the interruptible execution in AOT mode, developers should set `TRUE` through the `WasmEdge_ConfigureCompilerSetInterruptible()` API into the configure context for the AOT compiler.
-
- ```c
- WasmEdge_Async *Async = ...; /* Ignored. Asynchronous execute a function. */
- /* Blocking and waiting for the execution for 1 second. */
- bool IsEnd = WasmEdge_AsyncWaitFor(Async, 1000);
- if (IsEnd) {
- /* The execution finished. Developers can get the result. */
- WasmEdge_Result Res = WasmEdge_AsyncGet(/* ... Ignored */);
- } else {
- /* The time limit exceeded. Developers can keep waiting or cancel the execution. */
- WasmEdge_AsyncCancel(Async);
- WasmEdge_Result Res = WasmEdge_AsyncGet(Async, 0, NULL);
- /* The result error code will be `WasmEdge_ErrCode_Interrupted`. */
- }
- WasmEdge_AsyncDelete(Async);
- ```
+ Developers can wait the execution until finished:
+
+ ```c
+ WasmEdge_Async *Async = ...; /* Ignored. Asynchronous execute a function. */
+ /* Blocking and waiting for the execution. */
+ WasmEdge_AsyncWait(Async);
+ WasmEdge_AsyncDelete(Async);
+ ```
+
+ Or developers can wait for a time limit. If the time limit exceeded, developers can choose to cancel the execution. For the interruptible execution in AOT mode, developers should set `TRUE` through the `WasmEdge_ConfigureCompilerSetInterruptible()` API into the configure context for the AOT compiler.
+
+ ```c
+ WasmEdge_Async *Async = ...; /* Ignored. Asynchronous execute a function. */
+ /* Blocking and waiting for the execution for 1 second. */
+ bool IsEnd = WasmEdge_AsyncWaitFor(Async, 1000);
+ if (IsEnd) {
+ /* The execution finished. Developers can get the result. */
+ WasmEdge_Result Res = WasmEdge_AsyncGet(/* ... Ignored */);
+ } else {
+ /* The time limit exceeded. Developers can keep waiting or cancel the execution. */
+ WasmEdge_AsyncCancel(Async);
+ WasmEdge_Result Res = WasmEdge_AsyncGet(Async, 0, NULL);
+ /* The result error code will be `WasmEdge_ErrCode_Interrupted`. */
+ }
+ WasmEdge_AsyncDelete(Async);
+ ```
2. Get the execution result of the asynchronous execution
- Developers can use the `WasmEdge_AsyncGetReturnsLength()` API to get the return value list length. This function will block and wait for the execution. If the execution has finished, this function will return the length immediately. If the execution failed, this function will return `0`. This function can help the developers to create the buffer to get the return values. If developers have already known the buffer length, they can skip this function and use the `WasmEdge_AsyncGet()` API to get the result.
+ Developers can use the `WasmEdge_AsyncGetReturnsLength()` API to get the return value list length. This function will block and wait for the execution. If the execution has finished, this function will return the length immediately. If the execution failed, this function will return `0`. This function can help the developers to create the buffer to get the return values. If developers have already known the buffer length, they can skip this function and use the `WasmEdge_AsyncGet()` API to get the result.
- ```c
- WasmEdge_Async *Async = ...; /* Ignored. Asynchronous execute a function. */
- /* Blocking and waiting for the execution and get the return value list length. */
- uint32_t Arity = WasmEdge_AsyncGetReturnsLength(Async);
- WasmEdge_AsyncDelete(Async);
- ```
+ ```c
+ WasmEdge_Async *Async = ...; /* Ignored. Asynchronous execute a function. */
+ /* Blocking and waiting for the execution and get the return value list length. */
+ uint32_t Arity = WasmEdge_AsyncGetReturnsLength(Async);
+ WasmEdge_AsyncDelete(Async);
+ ```
- The `WasmEdge_AsyncGet()` API will block and wait for the execution. If the execution has finished, this function will fill the return values into the buffer and return the execution result immediately.
+ The `WasmEdge_AsyncGet()` API will block and wait for the execution. If the execution has finished, this function will fill the return values into the buffer and return the execution result immediately.
- ```c
- WasmEdge_Async *Async = ...; /* Ignored. Asynchronous execute a function. */
- /* Blocking and waiting for the execution and get the return values. */
- const uint32_t BUF_LEN = 256;
- WasmEdge_Value Buf[BUF_LEN];
- WasmEdge_Result Res = WasmEdge_AsyncGet(Async, Buf, BUF_LEN);
- WasmEdge_AsyncDelete(Async);
- ```
+ ```c
+ WasmEdge_Async *Async = ...; /* Ignored. Asynchronous execute a function. */
+ /* Blocking and waiting for the execution and get the return values. */
+ const uint32_t BUF_LEN = 256;
+ WasmEdge_Value Buf[BUF_LEN];
+ WasmEdge_Result Res = WasmEdge_AsyncGet(Async, Buf, BUF_LEN);
+ WasmEdge_AsyncDelete(Async);
+ ```
### Configurations
@@ -444,156 +444,156 @@ The configuration context, `WasmEdge_ConfigureContext`, manages the configuratio
1. Proposals
- WasmEdge supports turning on or off the WebAssembly proposals. This configuration is effective in any contexts created with the `Configure` context.
-
- ```c
- enum WasmEdge_Proposal {
- WasmEdge_Proposal_ImportExportMutGlobals = 0,
- WasmEdge_Proposal_NonTrapFloatToIntConversions,
- WasmEdge_Proposal_SignExtensionOperators,
- WasmEdge_Proposal_MultiValue,
- WasmEdge_Proposal_BulkMemoryOperations,
- WasmEdge_Proposal_ReferenceTypes,
- WasmEdge_Proposal_SIMD,
- WasmEdge_Proposal_TailCall,
- WasmEdge_Proposal_MultiMemories,
- WasmEdge_Proposal_Annotations,
- WasmEdge_Proposal_Memory64,
- WasmEdge_Proposal_ExceptionHandling,
- WasmEdge_Proposal_ExtendedConst,
- WasmEdge_Proposal_Threads,
- WasmEdge_Proposal_FunctionReferences
- };
- ```
-
- Developers can add or remove the proposals into the `Configure` context.
-
- ```c
- /*
- * By default, the following proposals have turned on initially:
- * * Import/Export of mutable globals
- * * Non-trapping float-to-int conversions
- * * Sign-extension operators
- * * Multi-value returns
- * * Bulk memory operations
- * * Reference types
- * * Fixed-width SIMD
- *
- * For the current WasmEdge version, the following proposals are supported
- * (turned of by default) additionally:
- * * Tail-call
- * * Multiple memories
- * * Extended-const
- * * Threads
- */
- WasmEdge_ConfigureContext *ConfCxt = WasmEdge_ConfigureCreate();
- WasmEdge_ConfigureAddProposal(ConfCxt, WasmEdge_Proposal_MultiMemories);
- WasmEdge_ConfigureRemoveProposal(ConfCxt, WasmEdge_Proposal_ReferenceTypes);
- bool IsBulkMem = WasmEdge_ConfigureHasProposal(ConfCxt, WasmEdge_Proposal_BulkMemoryOperations);
- /* The `IsBulkMem` will be `TRUE`. */
- WasmEdge_ConfigureDelete(ConfCxt);
- ```
+ WasmEdge supports turning on or off the WebAssembly proposals. This configuration is effective in any contexts created with the `Configure` context.
+
+ ```c
+ enum WasmEdge_Proposal {
+ WasmEdge_Proposal_ImportExportMutGlobals = 0,
+ WasmEdge_Proposal_NonTrapFloatToIntConversions,
+ WasmEdge_Proposal_SignExtensionOperators,
+ WasmEdge_Proposal_MultiValue,
+ WasmEdge_Proposal_BulkMemoryOperations,
+ WasmEdge_Proposal_ReferenceTypes,
+ WasmEdge_Proposal_SIMD,
+ WasmEdge_Proposal_TailCall,
+ WasmEdge_Proposal_MultiMemories,
+ WasmEdge_Proposal_Annotations,
+ WasmEdge_Proposal_Memory64,
+ WasmEdge_Proposal_ExceptionHandling,
+ WasmEdge_Proposal_ExtendedConst,
+ WasmEdge_Proposal_Threads,
+ WasmEdge_Proposal_FunctionReferences
+ };
+ ```
+
+ Developers can add or remove the proposals into the `Configure` context.
+
+ ```c
+ /*
+ * By default, the following proposals have turned on initially:
+ * * Import/Export of mutable globals
+ * * Non-trapping float-to-int conversions
+ * * Sign-extension operators
+ * * Multi-value returns
+ * * Bulk memory operations
+ * * Reference types
+ * * Fixed-width SIMD
+ *
+ * For the current WasmEdge version, the following proposals are supported
+ * (turned of by default) additionally:
+ * * Tail-call
+ * * Multiple memories
+ * * Extended-const
+ * * Threads
+ */
+ WasmEdge_ConfigureContext *ConfCxt = WasmEdge_ConfigureCreate();
+ WasmEdge_ConfigureAddProposal(ConfCxt, WasmEdge_Proposal_MultiMemories);
+ WasmEdge_ConfigureRemoveProposal(ConfCxt, WasmEdge_Proposal_ReferenceTypes);
+ bool IsBulkMem = WasmEdge_ConfigureHasProposal(ConfCxt, WasmEdge_Proposal_BulkMemoryOperations);
+ /* The `IsBulkMem` will be `TRUE`. */
+ WasmEdge_ConfigureDelete(ConfCxt);
+ ```
2. Host registrations
- This configuration is used for the `VM` context to turn on the `WASI` or `wasmedge_process` supports and only effective in `VM` contexts.
+ This configuration is used for the `VM` context to turn on the `WASI` or `wasmedge_process` supports and only effective in `VM` contexts.
- ```c
- enum WasmEdge_HostRegistration {
- WasmEdge_HostRegistration_Wasi = 0,
- WasmEdge_HostRegistration_WasmEdge_Process
- };
- ```
+ ```c
+ enum WasmEdge_HostRegistration {
+ WasmEdge_HostRegistration_Wasi = 0,
+ WasmEdge_HostRegistration_WasmEdge_Process
+ };
+ ```
- The details will be introduced in the [preregistrations of VM context](#preregistrations).
+ The details will be introduced in the [preregistrations of VM context](#preregistrations).
- ```c
- WasmEdge_ConfigureContext *ConfCxt = WasmEdge_ConfigureCreate();
- bool IsHostWasi = WasmEdge_ConfigureHasHostRegistration(ConfCxt, WasmEdge_HostRegistration_Wasi);
- /* The `IsHostWasi` will be `FALSE`. */
- WasmEdge_ConfigureAddHostRegistration(ConfCxt, WasmEdge_HostRegistration_Wasi);
- IsHostWasi = WasmEdge_ConfigureHasHostRegistration(ConfCxt, WasmEdge_HostRegistration_Wasi);
- /* The `IsHostWasi` will be `TRUE`. */
- WasmEdge_ConfigureDelete(ConfCxt);
- ```
+ ```c
+ WasmEdge_ConfigureContext *ConfCxt = WasmEdge_ConfigureCreate();
+ bool IsHostWasi = WasmEdge_ConfigureHasHostRegistration(ConfCxt, WasmEdge_HostRegistration_Wasi);
+ /* The `IsHostWasi` will be `FALSE`. */
+ WasmEdge_ConfigureAddHostRegistration(ConfCxt, WasmEdge_HostRegistration_Wasi);
+ IsHostWasi = WasmEdge_ConfigureHasHostRegistration(ConfCxt, WasmEdge_HostRegistration_Wasi);
+ /* The `IsHostWasi` will be `TRUE`. */
+ WasmEdge_ConfigureDelete(ConfCxt);
+ ```
3. Maximum memory pages
- Developers can limit the page size of memory instances by this configuration. When growing the page size of memory instances in WASM execution and exceeding the limited size, the page growing will fail. This configuration is only effective in the `Executor` and `VM` contexts.
+ Developers can limit the page size of memory instances by this configuration. When growing the page size of memory instances in WASM execution and exceeding the limited size, the page growing will fail. This configuration is only effective in the `Executor` and `VM` contexts.
- ```c
- WasmEdge_ConfigureContext *ConfCxt = WasmEdge_ConfigureCreate();
- uint32_t PageSize = WasmEdge_ConfigureGetMaxMemoryPage(ConfCxt);
- /* By default, the maximum memory page size is 65536. */
- WasmEdge_ConfigureSetMaxMemoryPage(ConfCxt, 1024);
- /* Limit the memory size of each memory instance with not larger than 1024 pages (64 MiB). */
- PageSize = WasmEdge_ConfigureGetMaxMemoryPage(ConfCxt);
- /* The `PageSize` will be 1024. */
- WasmEdge_ConfigureDelete(ConfCxt);
- ```
+ ```c
+ WasmEdge_ConfigureContext *ConfCxt = WasmEdge_ConfigureCreate();
+ uint32_t PageSize = WasmEdge_ConfigureGetMaxMemoryPage(ConfCxt);
+ /* By default, the maximum memory page size is 65536. */
+ WasmEdge_ConfigureSetMaxMemoryPage(ConfCxt, 1024);
+ /* Limit the memory size of each memory instance with not larger than 1024 pages (64 MiB). */
+ PageSize = WasmEdge_ConfigureGetMaxMemoryPage(ConfCxt);
+ /* The `PageSize` will be 1024. */
+ WasmEdge_ConfigureDelete(ConfCxt);
+ ```
4. AOT compiler options
- The AOT compiler options configure the behavior about optimization level, output format, dump IR, and generic binary.
-
- ```c
- enum WasmEdge_CompilerOptimizationLevel {
- // Disable as many optimizations as possible.
- WasmEdge_CompilerOptimizationLevel_O0 = 0,
- // Optimize quickly without destroying debuggability.
- WasmEdge_CompilerOptimizationLevel_O1,
- // Optimize for fast execution as much as possible without triggering
- // significant incremental compile time or code size growth.
- WasmEdge_CompilerOptimizationLevel_O2,
- // Optimize for fast execution as much as possible.
- WasmEdge_CompilerOptimizationLevel_O3,
- // Optimize for small code size as much as possible without triggering
- // significant incremental compile time or execution time slowdowns.
- WasmEdge_CompilerOptimizationLevel_Os,
- // Optimize for small code size as much as possible.
- WasmEdge_CompilerOptimizationLevel_Oz
- };
-
- enum WasmEdge_CompilerOutputFormat {
- // Native dynamic library format.
- WasmEdge_CompilerOutputFormat_Native = 0,
- // WebAssembly with AOT compiled codes in custom section.
- WasmEdge_CompilerOutputFormat_Wasm
- };
- ```
-
- These configurations are only effective in `Compiler` contexts.
-
- ```c
- WasmEdge_ConfigureContext *ConfCxt = WasmEdge_ConfigureCreate();
- /* By default, the optimization level is O3. */
- WasmEdge_ConfigureCompilerSetOptimizationLevel(ConfCxt, WasmEdge_CompilerOptimizationLevel_O2);
- /* By default, the output format is universal WASM. */
- WasmEdge_ConfigureCompilerSetOutputFormat(ConfCxt, WasmEdge_CompilerOutputFormat_Native);
- /* By default, the dump IR is `FALSE`. */
- WasmEdge_ConfigureCompilerSetDumpIR(ConfCxt, TRUE);
- /* By default, the generic binary is `FALSE`. */
- WasmEdge_ConfigureCompilerSetGenericBinary(ConfCxt, TRUE);
- /* By default, the interruptible is `FALSE`.
- /* Set this option to `TRUE` to support the interruptible execution in AOT mode. */
- WasmEdge_ConfigureCompilerSetInterruptible(ConfCxt, TRUE);
- WasmEdge_ConfigureDelete(ConfCxt);
- ```
+ The AOT compiler options configure the behavior about optimization level, output format, dump IR, and generic binary.
+
+ ```c
+ enum WasmEdge_CompilerOptimizationLevel {
+ // Disable as many optimizations as possible.
+ WasmEdge_CompilerOptimizationLevel_O0 = 0,
+ // Optimize quickly without destroying debuggability.
+ WasmEdge_CompilerOptimizationLevel_O1,
+ // Optimize for fast execution as much as possible without triggering
+ // significant incremental compile time or code size growth.
+ WasmEdge_CompilerOptimizationLevel_O2,
+ // Optimize for fast execution as much as possible.
+ WasmEdge_CompilerOptimizationLevel_O3,
+ // Optimize for small code size as much as possible without triggering
+ // significant incremental compile time or execution time slowdowns.
+ WasmEdge_CompilerOptimizationLevel_Os,
+ // Optimize for small code size as much as possible.
+ WasmEdge_CompilerOptimizationLevel_Oz
+ };
+
+ enum WasmEdge_CompilerOutputFormat {
+ // Native dynamic library format.
+ WasmEdge_CompilerOutputFormat_Native = 0,
+ // WebAssembly with AOT compiled codes in custom section.
+ WasmEdge_CompilerOutputFormat_Wasm
+ };
+ ```
+
+ These configurations are only effective in `Compiler` contexts.
+
+ ```c
+ WasmEdge_ConfigureContext *ConfCxt = WasmEdge_ConfigureCreate();
+ /* By default, the optimization level is O3. */
+ WasmEdge_ConfigureCompilerSetOptimizationLevel(ConfCxt, WasmEdge_CompilerOptimizationLevel_O2);
+ /* By default, the output format is universal WASM. */
+ WasmEdge_ConfigureCompilerSetOutputFormat(ConfCxt, WasmEdge_CompilerOutputFormat_Native);
+ /* By default, the dump IR is `FALSE`. */
+ WasmEdge_ConfigureCompilerSetDumpIR(ConfCxt, TRUE);
+ /* By default, the generic binary is `FALSE`. */
+ WasmEdge_ConfigureCompilerSetGenericBinary(ConfCxt, TRUE);
+ /* By default, the interruptible is `FALSE`.
+ /* Set this option to `TRUE` to support the interruptible execution in AOT mode. */
+ WasmEdge_ConfigureCompilerSetInterruptible(ConfCxt, TRUE);
+ WasmEdge_ConfigureDelete(ConfCxt);
+ ```
5. Statistics options
- The statistics options configure the behavior about instruction counting, cost measuring, and time measuring in both runtime and AOT compiler. These configurations are effective in `Compiler`, `VM`, and `Executor` contexts.
+ The statistics options configure the behavior about instruction counting, cost measuring, and time measuring in both runtime and AOT compiler. These configurations are effective in `Compiler`, `VM`, and `Executor` contexts.
- ```c
- WasmEdge_ConfigureContext *ConfCxt = WasmEdge_ConfigureCreate();
- /* By default, the instruction counting is `FALSE` when running a compiled-WASM or a pure-WASM. */
- WasmEdge_ConfigureStatisticsSetInstructionCounting(ConfCxt, TRUE);
- /* By default, the cost measurement is `FALSE` when running a compiled-WASM or a pure-WASM. */
- WasmEdge_ConfigureStatisticsSetCostMeasuring(ConfCxt, TRUE);
- /* By default, the time measurement is `FALSE` when running a compiled-WASM or a pure-WASM. */
- WasmEdge_ConfigureStatisticsSetTimeMeasuring(ConfCxt, TRUE);
- WasmEdge_ConfigureDelete(ConfCxt);
- ```
+ ```c
+ WasmEdge_ConfigureContext *ConfCxt = WasmEdge_ConfigureCreate();
+ /* By default, the instruction counting is `FALSE` when running a compiled-WASM or a pure-WASM. */
+ WasmEdge_ConfigureStatisticsSetInstructionCounting(ConfCxt, TRUE);
+ /* By default, the cost measurement is `FALSE` when running a compiled-WASM or a pure-WASM. */
+ WasmEdge_ConfigureStatisticsSetCostMeasuring(ConfCxt, TRUE);
+ /* By default, the time measurement is `FALSE` when running a compiled-WASM or a pure-WASM. */
+ WasmEdge_ConfigureStatisticsSetTimeMeasuring(ConfCxt, TRUE);
+ WasmEdge_ConfigureDelete(ConfCxt);
+ ```
### Statistics
@@ -603,47 +603,47 @@ Before using statistics, the statistics configuration must be set. Otherwise, th
1. Instruction counter
- The instruction counter can help developers to profile the performance of WASM running. Developers can retrieve the `Statistics` context from the `VM` context, or create a new one for the `Executor` creation. The details will be introduced in the next partitions.
+ The instruction counter can help developers to profile the performance of WASM running. Developers can retrieve the `Statistics` context from the `VM` context, or create a new one for the `Executor` creation. The details will be introduced in the next partitions.
- ```c
- WasmEdge_StatisticsContext *StatCxt = WasmEdge_StatisticsCreate();
- /*
- * ...
- * After running the WASM functions with the `Statistics` context
- */
- uint32_t Count = WasmEdge_StatisticsGetInstrCount(StatCxt);
- double IPS = WasmEdge_StatisticsGetInstrPerSecond(StatCxt);
- WasmEdge_StatisticsDelete(StatCxt);
- ```
+ ```c
+ WasmEdge_StatisticsContext *StatCxt = WasmEdge_StatisticsCreate();
+ /*
+ * ...
+ * After running the WASM functions with the `Statistics` context
+ */
+ uint32_t Count = WasmEdge_StatisticsGetInstrCount(StatCxt);
+ double IPS = WasmEdge_StatisticsGetInstrPerSecond(StatCxt);
+ WasmEdge_StatisticsDelete(StatCxt);
+ ```
2. Cost table
- The cost table is to accumulate the cost of instructions with their weights. Developers can set the cost table array (the indices are the byte code value of instructions, and the values are the cost of instructions) into the `Statistics` context. If the cost limit value is set, the execution will return the `cost limit exceeded` error immediately when exceeds the cost limit in runtime.
-
- ```c
- WasmEdge_StatisticsContext *StatCxt = WasmEdge_StatisticsCreate();
- uint64_t CostTable[16] = {
- 0, 0,
- 10, /* 0x02: Block */
- 11, /* 0x03: Loop */
- 12, /* 0x04: If */
- 12, /* 0x05: Else */
- 0, 0, 0, 0, 0, 0,
- 20, /* 0x0C: Br */
- 21, /* 0x0D: Br_if */
- 22, /* 0x0E: Br_table */
- 0
- };
- /* Developers can set the costs of each instruction. The value not covered will be 0. */
- WasmEdge_StatisticsSetCostTable(StatCxt, CostTable, 16);
- WasmEdge_StatisticsSetCostLimit(StatCxt, 5000000);
- /*
- * ...
- * After running the WASM functions with the `Statistics` context
- */
- uint64_t Cost = WasmEdge_StatisticsGetTotalCost(StatCxt);
- WasmEdge_StatisticsDelete(StatCxt);
- ```
+ The cost table is to accumulate the cost of instructions with their weights. Developers can set the cost table array (the indices are the byte code value of instructions, and the values are the cost of instructions) into the `Statistics` context. If the cost limit value is set, the execution will return the `cost limit exceeded` error immediately when exceeds the cost limit in runtime.
+
+ ```c
+ WasmEdge_StatisticsContext *StatCxt = WasmEdge_StatisticsCreate();
+ uint64_t CostTable[16] = {
+ 0, 0,
+ 10, /* 0x02: Block */
+ 11, /* 0x03: Loop */
+ 12, /* 0x04: If */
+ 12, /* 0x05: Else */
+ 0, 0, 0, 0, 0, 0,
+ 20, /* 0x0C: Br */
+ 21, /* 0x0D: Br_if */
+ 22, /* 0x0E: Br_table */
+ 0
+ };
+ /* Developers can set the costs of each instruction. The value not covered will be 0. */
+ WasmEdge_StatisticsSetCostTable(StatCxt, CostTable, 16);
+ WasmEdge_StatisticsSetCostLimit(StatCxt, 5000000);
+ /*
+ * ...
+ * After running the WASM functions with the `Statistics` context
+ */
+ uint64_t Cost = WasmEdge_StatisticsGetTotalCost(StatCxt);
+ WasmEdge_StatisticsDelete(StatCxt);
+ ```
### Tools Driver
@@ -695,161 +695,161 @@ The following shows the example of running the WASM for getting the Fibonacci. T
1. Run WASM functions rapidly
- Assume that the WASM file [`fibonacci.wasm`](https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/examples/wasm/fibonacci.wasm) is copied into the current directory, and the C file `test.c` is as following:
-
- ```c
- #include
- #include
- int main() {
- /* Create the configure context and add the WASI support. */
- /* This step is not necessary unless you need WASI support. */
- WasmEdge_ConfigureContext *ConfCxt = WasmEdge_ConfigureCreate();
- WasmEdge_ConfigureAddHostRegistration(ConfCxt, WasmEdge_HostRegistration_Wasi);
- /* The configure and store context to the VM creation can be NULL. */
- WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(ConfCxt, NULL);
-
- /* The parameters and returns arrays. */
- WasmEdge_Value Params[1] = { WasmEdge_ValueGenI32(5) };
- WasmEdge_Value Returns[1];
- /* Function name. */
- WasmEdge_String FuncName = WasmEdge_StringCreateByCString("fib");
- /* Run the WASM function from file. */
- WasmEdge_Result Res = WasmEdge_VMRunWasmFromFile(VMCxt, "fibonacci.wasm", FuncName, Params, 1, Returns, 1);
- /*
- * Developers can run the WASM binary from buffer with the `WasmEdge_VMRunWasmFromBuffer()` API,
- * or from `WasmEdge_ASTModuleContext` object with the `WasmEdge_VMRunWasmFromASTModule()` API.
- */
-
- if (WasmEdge_ResultOK(Res)) {
- printf("Get the result: %d\n", WasmEdge_ValueGetI32(Returns[0]));
- } else {
- printf("Error message: %s\n", WasmEdge_ResultGetMessage(Res));
- }
-
- /* Resources deallocations. */
- WasmEdge_VMDelete(VMCxt);
- WasmEdge_ConfigureDelete(ConfCxt);
- WasmEdge_StringDelete(FuncName);
- return 0;
- }
- ```
-
- Then you can compile and run: (the 5th Fibonacci number is 8 in 0-based index)
-
- ```bash
- $ gcc test.c -lwasmedge_c
- $ ./a.out
- Get the result: 8
- ```
+ Assume that the WASM file [`fibonacci.wasm`](https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/examples/wasm/fibonacci.wasm) is copied into the current directory, and the C file `test.c` is as following:
+
+ ```c
+ #include
+ #include
+ int main() {
+ /* Create the configure context and add the WASI support. */
+ /* This step is not necessary unless you need WASI support. */
+ WasmEdge_ConfigureContext *ConfCxt = WasmEdge_ConfigureCreate();
+ WasmEdge_ConfigureAddHostRegistration(ConfCxt, WasmEdge_HostRegistration_Wasi);
+ /* The configure and store context to the VM creation can be NULL. */
+ WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(ConfCxt, NULL);
+
+ /* The parameters and returns arrays. */
+ WasmEdge_Value Params[1] = { WasmEdge_ValueGenI32(5) };
+ WasmEdge_Value Returns[1];
+ /* Function name. */
+ WasmEdge_String FuncName = WasmEdge_StringCreateByCString("fib");
+ /* Run the WASM function from file. */
+ WasmEdge_Result Res = WasmEdge_VMRunWasmFromFile(VMCxt, "fibonacci.wasm", FuncName, Params, 1, Returns, 1);
+ /*
+ * Developers can run the WASM binary from buffer with the `WasmEdge_VMRunWasmFromBuffer()` API,
+ * or from `WasmEdge_ASTModuleContext` object with the `WasmEdge_VMRunWasmFromASTModule()` API.
+ */
+
+ if (WasmEdge_ResultOK(Res)) {
+ printf("Get the result: %d\n", WasmEdge_ValueGetI32(Returns[0]));
+ } else {
+ printf("Error message: %s\n", WasmEdge_ResultGetMessage(Res));
+ }
+
+ /* Resources deallocations. */
+ WasmEdge_VMDelete(VMCxt);
+ WasmEdge_ConfigureDelete(ConfCxt);
+ WasmEdge_StringDelete(FuncName);
+ return 0;
+ }
+ ```
+
+ Then you can compile and run: (the 5th Fibonacci number is 8 in 0-based index)
+
+ ```bash
+ $ gcc test.c -lwasmedge_c
+ $ ./a.out
+ Get the result: 8
+ ```
2. Instantiate and run WASM functions manually
- Besides the above example, developers can run the WASM functions step-by-step with `VM` context APIs:
-
- ```c
- #include
- #include
- int main() {
- /* Create the configure context and add the WASI support. */
- /* This step is not necessary unless you need the WASI support. */
- WasmEdge_ConfigureContext *ConfCxt = WasmEdge_ConfigureCreate();
- WasmEdge_ConfigureAddHostRegistration(ConfCxt, WasmEdge_HostRegistration_Wasi);
- /* The configure and store context to the VM creation can be NULL. */
- WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(ConfCxt, NULL);
-
- /* The parameters and returns arrays. */
- WasmEdge_Value Params[1] = { WasmEdge_ValueGenI32(10) };
- WasmEdge_Value Returns[1];
- /* Function name. */
- WasmEdge_String FuncName = WasmEdge_StringCreateByCString("fib");
- /* Result. */
- WasmEdge_Result Res;
-
- /* Step 1: Load WASM file. */
- Res = WasmEdge_VMLoadWasmFromFile(VMCxt, "fibonacci.wasm");
- /*
- * Developers can load the WASM binary from buffer with the `WasmEdge_VMLoadWasmFromBuffer()` API,
- * or from `WasmEdge_ASTModuleContext` object with the `WasmEdge_VMLoadWasmFromASTModule()` API.
- */
- if (!WasmEdge_ResultOK(Res)) {
- printf("Loading phase failed: %s\n", WasmEdge_ResultGetMessage(Res));
- return 1;
- }
- /* Step 2: Validate the WASM module. */
- Res = WasmEdge_VMValidate(VMCxt);
- if (!WasmEdge_ResultOK(Res)) {
- printf("Validation phase failed: %s\n", WasmEdge_ResultGetMessage(Res));
- return 1;
- }
- /* Step 3: Instantiate the WASM module. */
- Res = WasmEdge_VMInstantiate(VMCxt);
- /*
- * Developers can load, validate, and instantiate another WASM module to replace the
- * instantiated one. In this case, the old module will be cleared, but the registered
- * modules are still kept.
- */
- if (!WasmEdge_ResultOK(Res)) {
- printf("Instantiation phase failed: %s\n", WasmEdge_ResultGetMessage(Res));
- return 1;
- }
- /* Step 4: Execute WASM functions. You can execute functions repeatedly after instantiation. */
- Res = WasmEdge_VMExecute(VMCxt, FuncName, Params, 1, Returns, 1);
- if (WasmEdge_ResultOK(Res)) {
- printf("Get the result: %d\n", WasmEdge_ValueGetI32(Returns[0]));
- } else {
- printf("Execution phase failed: %s\n", WasmEdge_ResultGetMessage(Res));
- }
-
- /* Resources deallocations. */
- WasmEdge_VMDelete(VMCxt);
- WasmEdge_ConfigureDelete(ConfCxt);
- WasmEdge_StringDelete(FuncName);
- return 0;
- }
- ```
-
- Then you can compile and run: (the 10th Fibonacci number is 89 in 0-based index)
-
- ```bash
- $ gcc test.c -lwasmedge_c
- $ ./a.out
- Get the result: 89
- ```
-
- The following graph explains the status of the `VM` context.
-
- ```text
- |========================|
- |------->| VM: Initiated |
- | |========================|
- | |
- | LoadWasm
- | |
- | v
- | |========================|
- |--------| VM: Loaded |<-------|
- | |========================| |
- | | ^ |
- | Validate | |
- Cleanup | LoadWasm |
- | v | LoadWasm
- | |========================| |
- |--------| VM: Validated | |
- | |========================| |
- | | ^ |
- | Instantiate | |
- | | RegisterModule |
- | v | |
- | |========================| |
- |--------| VM: Instantiated |--------|
- |========================|
- | ^
- | |
- --------------
- Instantiate, Execute, ExecuteRegistered
- ```
-
- The status of the `VM` context would be `Inited` when created. After loading WASM successfully, the status will be `Loaded`. After validating WASM successfully, the status will be `Validated`. After instantiating WASM successfully, the status will be `Instantiated`, and developers can invoke functions. Developers can register WASM or module instances in any status, but they should instantiate WASM again. Developers can also load WASM in any status, and they should validate and instantiate the WASM module before function invocation. When in the `Instantiated` status, developers can instantiate the WASM module again to reset the old WASM runtime structures.
+ Besides the above example, developers can run the WASM functions step-by-step with `VM` context APIs:
+
+ ```c
+ #include
+ #include
+ int main() {
+ /* Create the configure context and add the WASI support. */
+ /* This step is not necessary unless you need the WASI support. */
+ WasmEdge_ConfigureContext *ConfCxt = WasmEdge_ConfigureCreate();
+ WasmEdge_ConfigureAddHostRegistration(ConfCxt, WasmEdge_HostRegistration_Wasi);
+ /* The configure and store context to the VM creation can be NULL. */
+ WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(ConfCxt, NULL);
+
+ /* The parameters and returns arrays. */
+ WasmEdge_Value Params[1] = { WasmEdge_ValueGenI32(10) };
+ WasmEdge_Value Returns[1];
+ /* Function name. */
+ WasmEdge_String FuncName = WasmEdge_StringCreateByCString("fib");
+ /* Result. */
+ WasmEdge_Result Res;
+
+ /* Step 1: Load WASM file. */
+ Res = WasmEdge_VMLoadWasmFromFile(VMCxt, "fibonacci.wasm");
+ /*
+ * Developers can load the WASM binary from buffer with the `WasmEdge_VMLoadWasmFromBuffer()` API,
+ * or from `WasmEdge_ASTModuleContext` object with the `WasmEdge_VMLoadWasmFromASTModule()` API.
+ */
+ if (!WasmEdge_ResultOK(Res)) {
+ printf("Loading phase failed: %s\n", WasmEdge_ResultGetMessage(Res));
+ return 1;
+ }
+ /* Step 2: Validate the WASM module. */
+ Res = WasmEdge_VMValidate(VMCxt);
+ if (!WasmEdge_ResultOK(Res)) {
+ printf("Validation phase failed: %s\n", WasmEdge_ResultGetMessage(Res));
+ return 1;
+ }
+ /* Step 3: Instantiate the WASM module. */
+ Res = WasmEdge_VMInstantiate(VMCxt);
+ /*
+ * Developers can load, validate, and instantiate another WASM module to replace the
+ * instantiated one. In this case, the old module will be cleared, but the registered
+ * modules are still kept.
+ */
+ if (!WasmEdge_ResultOK(Res)) {
+ printf("Instantiation phase failed: %s\n", WasmEdge_ResultGetMessage(Res));
+ return 1;
+ }
+ /* Step 4: Execute WASM functions. You can execute functions repeatedly after instantiation. */
+ Res = WasmEdge_VMExecute(VMCxt, FuncName, Params, 1, Returns, 1);
+ if (WasmEdge_ResultOK(Res)) {
+ printf("Get the result: %d\n", WasmEdge_ValueGetI32(Returns[0]));
+ } else {
+ printf("Execution phase failed: %s\n", WasmEdge_ResultGetMessage(Res));
+ }
+
+ /* Resources deallocations. */
+ WasmEdge_VMDelete(VMCxt);
+ WasmEdge_ConfigureDelete(ConfCxt);
+ WasmEdge_StringDelete(FuncName);
+ return 0;
+ }
+ ```
+
+ Then you can compile and run: (the 10th Fibonacci number is 89 in 0-based index)
+
+ ```bash
+ $ gcc test.c -lwasmedge_c
+ $ ./a.out
+ Get the result: 89
+ ```
+
+ The following graph explains the status of the `VM` context.
+
+ ```text
+ |========================|
+ |------->| VM: Initiated |
+ | |========================|
+ | |
+ | LoadWasm
+ | |
+ | v
+ | |========================|
+ |--------| VM: Loaded |<-------|
+ | |========================| |
+ | | ^ |
+ | Validate | |
+ Cleanup | LoadWasm |
+ | v | LoadWasm
+ | |========================| |
+ |--------| VM: Validated | |
+ | |========================| |
+ | | ^ |
+ | Instantiate | |
+ | | RegisterModule |
+ | v | |
+ | |========================| |
+ |--------| VM: Instantiated |--------|
+ |========================|
+ | ^
+ | |
+ --------------
+ Instantiate, Execute, ExecuteRegistered
+ ```
+
+ The status of the `VM` context would be `Inited` when created. After loading WASM successfully, the status will be `Loaded`. After validating WASM successfully, the status will be `Validated`. After instantiating WASM successfully, the status will be `Instantiated`, and developers can invoke functions. Developers can register WASM or module instances in any status, but they should instantiate WASM again. Developers can also load WASM in any status, and they should validate and instantiate the WASM module before function invocation. When in the `Instantiated` status, developers can instantiate the WASM module again to reset the old WASM runtime structures.
### VM Creations
@@ -877,88 +877,88 @@ WasmEdge provides the following built-in pre-registrations.
1. [WASI (WebAssembly System Interface)](https://github.com/WebAssembly/WASI)
- Developers can turn on the WASI support for VM in the `Configure` context.
+ Developers can turn on the WASI support for VM in the `Configure` context.
- ```c
- WasmEdge_ConfigureContext *ConfCxt = WasmEdge_ConfigureCreate();
- WasmEdge_ConfigureAddHostRegistration(ConfCxt, WasmEdge_HostRegistration_Wasi);
- WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(ConfCxt, NULL);
- /* The following API can retrieve the pre-registration module instances from the VM context. */
- /* This API will return `NULL` if the corresponding pre-registration is not set into the configuration. */
- WasmEdge_ModuleInstanceContext *WasiModule =
- WasmEdge_VMGetImportModuleContext(VMCxt, WasmEdge_HostRegistration_Wasi);
- /* Initialize the WASI. */
- WasmEdge_ModuleInstanceInitWASI(WasiModule, /* ... ignored */ );
- WasmEdge_VMDelete(VMCxt);
- WasmEdge_ConfigureDelete(ConfCxt);
- ```
+ ```c
+ WasmEdge_ConfigureContext *ConfCxt = WasmEdge_ConfigureCreate();
+ WasmEdge_ConfigureAddHostRegistration(ConfCxt, WasmEdge_HostRegistration_Wasi);
+ WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(ConfCxt, NULL);
+ /* The following API can retrieve the pre-registration module instances from the VM context. */
+ /* This API will return `NULL` if the corresponding pre-registration is not set into the configuration. */
+ WasmEdge_ModuleInstanceContext *WasiModule =
+ WasmEdge_VMGetImportModuleContext(VMCxt, WasmEdge_HostRegistration_Wasi);
+ /* Initialize the WASI. */
+ WasmEdge_ModuleInstanceInitWASI(WasiModule, /* ... ignored */ );
+ WasmEdge_VMDelete(VMCxt);
+ WasmEdge_ConfigureDelete(ConfCxt);
+ ```
- And also can create the WASI module instance from API. The details will be introduced in the [Host Functions](#host-functions) and the [Host Module Registrations](#host-module-registrations).
+ And also can create the WASI module instance from API. The details will be introduced in the [Host Functions](#host-functions) and the [Host Module Registrations](#host-module-registrations).
2. [WasmEdge_Process](https://crates.io/crates/wasmedge_process_interface)
- This pre-registration is for the process interface for WasmEdge on `Rust` sources. After turning on this pre-registration, the VM will support the `wasmedge_process` plugin.
+ This pre-registration is for the process interface for WasmEdge on `Rust` sources. After turning on this pre-registration, the VM will support the `wasmedge_process` plugin.
- ```c
- WasmEdge_ConfigureContext *ConfCxt = WasmEdge_ConfigureCreate();
- WasmEdge_ConfigureAddHostRegistration(ConfCxt, WasmEdge_HostRegistration_WasmEdge_Process);
- WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(ConfCxt, NULL);
- /* The following API can retrieve the pre-registration module instances from the VM context. */
- /* This API will return `NULL` if the corresponding pre-registration is not set into the configuration or the plugin load failed. */
- WasmEdge_ModuleInstanceContext *ProcModule =
- WasmEdge_VMGetImportModuleContext(VMCxt, WasmEdge_HostRegistration_WasmEdge_Process);
- /* Initialize the WasmEdge_Process. */
- WasmEdge_ModuleInstanceInitWasmEdgeProcess(ProcModule, /* ... ignored */ );
- WasmEdge_VMDelete(VMCxt);
- WasmEdge_ConfigureDelete(ConfCxt);
- ```
+ ```c
+ WasmEdge_ConfigureContext *ConfCxt = WasmEdge_ConfigureCreate();
+ WasmEdge_ConfigureAddHostRegistration(ConfCxt, WasmEdge_HostRegistration_WasmEdge_Process);
+ WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(ConfCxt, NULL);
+ /* The following API can retrieve the pre-registration module instances from the VM context. */
+ /* This API will return `NULL` if the corresponding pre-registration is not set into the configuration or the plugin load failed. */
+ WasmEdge_ModuleInstanceContext *ProcModule =
+ WasmEdge_VMGetImportModuleContext(VMCxt, WasmEdge_HostRegistration_WasmEdge_Process);
+ /* Initialize the WasmEdge_Process. */
+ WasmEdge_ModuleInstanceInitWasmEdgeProcess(ProcModule, /* ... ignored */ );
+ WasmEdge_VMDelete(VMCxt);
+ WasmEdge_ConfigureDelete(ConfCxt);
+ ```
- And also can create the WasmEdge_Process module instance from API. The details will be introduced in the [Host Functions](#host-functions) and the [Host Module Registrations](#host-module-registrations).
+ And also can create the WasmEdge_Process module instance from API. The details will be introduced in the [Host Functions](#host-functions) and the [Host Module Registrations](#host-module-registrations).
3. [WASI-NN proposal](https://github.com/WebAssembly/wasi-nn) (`0.10.1` or upper only)
- Developers can turn on the WASI-NN proposal support for VM in the `Configure` context.
+ Developers can turn on the WASI-NN proposal support for VM in the `Configure` context.
- > Note: Please check that the [dependencies and prerequisites](/develop/build-and-run/install.md#wasi-nn-plugin-with-openvino™-backend) are satisfied.
+ > Note: Please check that the [dependencies and prerequisites](/develop/build-and-run/install.md#wasi-nn-plugin-with-openvino™-backend) are satisfied.
- ```c
- WasmEdge_ConfigureContext *ConfCxt = WasmEdge_ConfigureCreate();
- WasmEdge_ConfigureAddHostRegistration(ConfCxt, WasmEdge_HostRegistration_WasiNN);
- WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(ConfCxt, NULL);
- /* The following API can retrieve the pre-registration module instances from the VM context. */
- /* This API will return `NULL` if the corresponding pre-registration is not set into the configuration or the plugin load failed. */
- WasmEdge_ModuleInstanceContext *NNModule =
- WasmEdge_VMGetImportModuleContext(VMCxt, WasmEdge_HostRegistration_WasiNN);
- WasmEdge_VMDelete(VMCxt);
- WasmEdge_ConfigureDelete(ConfCxt);
- ```
+ ```c
+ WasmEdge_ConfigureContext *ConfCxt = WasmEdge_ConfigureCreate();
+ WasmEdge_ConfigureAddHostRegistration(ConfCxt, WasmEdge_HostRegistration_WasiNN);
+ WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(ConfCxt, NULL);
+ /* The following API can retrieve the pre-registration module instances from the VM context. */
+ /* This API will return `NULL` if the corresponding pre-registration is not set into the configuration or the plugin load failed. */
+ WasmEdge_ModuleInstanceContext *NNModule =
+ WasmEdge_VMGetImportModuleContext(VMCxt, WasmEdge_HostRegistration_WasiNN);
+ WasmEdge_VMDelete(VMCxt);
+ WasmEdge_ConfigureDelete(ConfCxt);
+ ```
- And also can create the WASI-NN module instance from API. The details will be introduced in the [Host Functions](#host-functions) and the [Host Module Registrations](#host-module-registrations).
+ And also can create the WASI-NN module instance from API. The details will be introduced in the [Host Functions](#host-functions) and the [Host Module Registrations](#host-module-registrations).
4. [WASI-Crypto proposal](https://github.com/WebAssembly/wasi-crypto) (`0.10.1` or upper only)
- Developers can turn on the WASI-Crypto proposal support for VM in the `Configure` context.
-
- > Note: Please check that the [dependencies and prerequisites](/develop/build-and-run/install.md#wasi-crypto-plugin) are satisfied.
-
- ```c
- WasmEdge_ConfigureContext *ConfCxt = WasmEdge_ConfigureCreate();
- /* The WASI-Crypto related configures are suggested to turn on together. */
- WasmEdge_ConfigureAddHostRegistration(ConfCxt, WasmEdge_HostRegistration_WasiCrypto_Common);
- WasmEdge_ConfigureAddHostRegistration(ConfCxt, WasmEdge_HostRegistration_WasiCrypto_AsymmetricCommon);
- WasmEdge_ConfigureAddHostRegistration(ConfCxt, WasmEdge_HostRegistration_WasiCrypto_Kx);
- WasmEdge_ConfigureAddHostRegistration(ConfCxt, WasmEdge_HostRegistration_WasiCrypto_Signatures);
- WasmEdge_ConfigureAddHostRegistration(ConfCxt, WasmEdge_HostRegistration_WasiCrypto_Symmetric);
- WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(ConfCxt, NULL);
- /* The following API can retrieve the pre-registration module instances from the VM context. */
- /* This API will return `NULL` if the corresponding pre-registration is not set into the configuration or the plugin load failed. */
- WasmEdge_ModuleInstanceContext *CryptoCommonModule =
- WasmEdge_VMGetImportModuleContext(VMCxt, WasmEdge_HostRegistration_WasiCrypto_Common);
- WasmEdge_VMDelete(VMCxt);
- WasmEdge_ConfigureDelete(ConfCxt);
- ```
-
- And also can create the WASI-Crypto module instance from API. The details will be introduced in the [Host Functions](#host-functions) and the [Host Module Registrations](#host-module-registrations).
+ Developers can turn on the WASI-Crypto proposal support for VM in the `Configure` context.
+
+ > Note: Please check that the [dependencies and prerequisites](/develop/build-and-run/install.md#wasi-crypto-plugin) are satisfied.
+
+ ```c
+ WasmEdge_ConfigureContext *ConfCxt = WasmEdge_ConfigureCreate();
+ /* The WASI-Crypto related configures are suggested to turn on together. */
+ WasmEdge_ConfigureAddHostRegistration(ConfCxt, WasmEdge_HostRegistration_WasiCrypto_Common);
+ WasmEdge_ConfigureAddHostRegistration(ConfCxt, WasmEdge_HostRegistration_WasiCrypto_AsymmetricCommon);
+ WasmEdge_ConfigureAddHostRegistration(ConfCxt, WasmEdge_HostRegistration_WasiCrypto_Kx);
+ WasmEdge_ConfigureAddHostRegistration(ConfCxt, WasmEdge_HostRegistration_WasiCrypto_Signatures);
+ WasmEdge_ConfigureAddHostRegistration(ConfCxt, WasmEdge_HostRegistration_WasiCrypto_Symmetric);
+ WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(ConfCxt, NULL);
+ /* The following API can retrieve the pre-registration module instances from the VM context. */
+ /* This API will return `NULL` if the corresponding pre-registration is not set into the configuration or the plugin load failed. */
+ WasmEdge_ModuleInstanceContext *CryptoCommonModule =
+ WasmEdge_VMGetImportModuleContext(VMCxt, WasmEdge_HostRegistration_WasiCrypto_Common);
+ WasmEdge_VMDelete(VMCxt);
+ WasmEdge_ConfigureDelete(ConfCxt);
+ ```
+
+ And also can create the WASI-Crypto module instance from API. The details will be introduced in the [Host Functions](#host-functions) and the [Host Module Registrations](#host-module-registrations).
### Host Module Registrations
@@ -985,225 +985,225 @@ In WebAssembly, the instances in WASM modules can be exported and can be importe
1. Register the WASM modules with exported module names
- Unless the module instances have already contained the module names, every WASM module should be named uniquely when registering. Assume that the WASM file [`fibonacci.wasm`](https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/examples/wasm/fibonacci.wasm) is copied into the current directory.
+ Unless the module instances have already contained the module names, every WASM module should be named uniquely when registering. Assume that the WASM file [`fibonacci.wasm`](https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/examples/wasm/fibonacci.wasm) is copied into the current directory.
+
+ ```c
+ WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(NULL, NULL);
+ WasmEdge_String ModName = WasmEdge_StringCreateByCString("mod");
+ WasmEdge_Result Res = WasmEdge_VMRegisterModuleFromFile(VMCxt, ModName, "fibonacci.wasm");
+ /*
+ * Developers can register the WASM module from buffer with the `WasmEdge_VMRegisterModuleFromBuffer()` API,
+ * or from `WasmEdge_ASTModuleContext` object with the `WasmEdge_VMRegisterModuleFromASTModule()` API.
+ */
+ /*
+ * The result status should be checked.
+ * The error will occur if the WASM module instantiation failed or the module name conflicts.
+ */
+ WasmEdge_StringDelete(ModName);
+ WasmEdge_VMDelete(VMCxt);
+ ```
+
+2. Execute the functions in registered WASM modules
- ```c
- WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(NULL, NULL);
- WasmEdge_String ModName = WasmEdge_StringCreateByCString("mod");
- WasmEdge_Result Res = WasmEdge_VMRegisterModuleFromFile(VMCxt, ModName, "fibonacci.wasm");
- /*
+ Assume that the C file `test.c` is as follows:
+
+ ```c
+ #include
+ #include
+ int main() {
+ WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(NULL, NULL);
+
+ /* The parameters and returns arrays. */
+ WasmEdge_Value Params[1] = { WasmEdge_ValueGenI32(20) };
+ WasmEdge_Value Returns[1];
+ /* Names. */
+ WasmEdge_String ModName = WasmEdge_StringCreateByCString("mod");
+ WasmEdge_String FuncName = WasmEdge_StringCreateByCString("fib");
+ /* Result. */
+ WasmEdge_Result Res;
+
+ /* Register the WASM module into VM. */
+ Res = WasmEdge_VMRegisterModuleFromFile(VMCxt, ModName, "fibonacci.wasm");
+ /*
* Developers can register the WASM module from buffer with the `WasmEdge_VMRegisterModuleFromBuffer()` API,
* or from `WasmEdge_ASTModuleContext` object with the `WasmEdge_VMRegisterModuleFromASTModule()` API.
*/
- /*
- * The result status should be checked.
- * The error will occur if the WASM module instantiation failed or the module name conflicts.
+ if (!WasmEdge_ResultOK(Res)) {
+ printf("WASM registration failed: %s\n", WasmEdge_ResultGetMessage(Res));
+ return 1;
+ }
+ /*
+ * The function "fib" in the "fibonacci.wasm" was exported with the module name "mod".
+ * As the same as host functions, other modules can import the function `"mod" "fib"`.
*/
- WasmEdge_StringDelete(ModName);
- WasmEdge_VMDelete(VMCxt);
- ```
-2. Execute the functions in registered WASM modules
+ /*
+ * Execute WASM functions in registered modules.
+ * Unlike the execution of functions, the registered functions can be invoked without
+ * `WasmEdge_VMInstantiate()` because the WASM module was instantiated when registering.
+ * Developers can also invoke the host functions directly with this API.
+ */
+ Res = WasmEdge_VMExecuteRegistered(VMCxt, ModName, FuncName, Params, 1, Returns, 1);
+ if (WasmEdge_ResultOK(Res)) {
+ printf("Get the result: %d\n", WasmEdge_ValueGetI32(Returns[0]));
+ } else {
+ printf("Execution phase failed: %s\n", WasmEdge_ResultGetMessage(Res));
+ }
+ WasmEdge_StringDelete(ModName);
+ WasmEdge_StringDelete(FuncName);
+ WasmEdge_VMDelete(VMCxt);
+ return 0;
+ }
+ ```
+
+ Then you can compile and run: (the 20th Fibonacci number is 89 in 0-based index)
+
+ ```bash
+ $ gcc test.c -lwasmedge_c
+ $ ./a.out
+ Get the result: 10946
+ ```
- Assume that the C file `test.c` is as follows:
-
- ```c
- #include
- #include
- int main() {
- WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(NULL, NULL);
-
- /* The parameters and returns arrays. */
- WasmEdge_Value Params[1] = { WasmEdge_ValueGenI32(20) };
- WasmEdge_Value Returns[1];
- /* Names. */
- WasmEdge_String ModName = WasmEdge_StringCreateByCString("mod");
- WasmEdge_String FuncName = WasmEdge_StringCreateByCString("fib");
- /* Result. */
- WasmEdge_Result Res;
-
- /* Register the WASM module into VM. */
- Res = WasmEdge_VMRegisterModuleFromFile(VMCxt, ModName, "fibonacci.wasm");
- /*
- * Developers can register the WASM module from buffer with the `WasmEdge_VMRegisterModuleFromBuffer()` API,
- * or from `WasmEdge_ASTModuleContext` object with the `WasmEdge_VMRegisterModuleFromASTModule()` API.
- */
- if (!WasmEdge_ResultOK(Res)) {
- printf("WASM registration failed: %s\n", WasmEdge_ResultGetMessage(Res));
- return 1;
- }
- /*
- * The function "fib" in the "fibonacci.wasm" was exported with the module name "mod".
- * As the same as host functions, other modules can import the function `"mod" "fib"`.
+### Asynchronous Execution
+
+1. Asynchronously run WASM functions rapidly
+
+ Assume that the WASM file [`fibonacci.wasm`](https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/examples/wasm/fibonacci.wasm) is copied into the current directory, and the C file `test.c` is as following:
+
+ ```c
+ #include
+ #include
+ int main() {
+ /* Create the VM context. */
+ WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(NULL, NULL);
+
+ /* The parameters and returns arrays. */
+ WasmEdge_Value Params[1] = { WasmEdge_ValueGenI32(20) };
+ WasmEdge_Value Returns[1];
+ /* Function name. */
+ WasmEdge_String FuncName = WasmEdge_StringCreateByCString("fib");
+ /* Asynchronously run the WASM function from file and get the `WasmEdge_Async` object. */
+ WasmEdge_Async *Async = WasmEdge_VMAsyncRunWasmFromFile(VMCxt, "fibonacci.wasm", FuncName, Params, 1);
+ /*
+ * Developers can run the WASM binary from buffer with the `WasmEdge_VMAsyncRunWasmFromBuffer()` API,
+ * or from `WasmEdge_ASTModuleContext` object with the `WasmEdge_VMAsyncRunWasmFromASTModule()` API.
*/
- /*
- * Execute WASM functions in registered modules.
- * Unlike the execution of functions, the registered functions can be invoked without
- * `WasmEdge_VMInstantiate()` because the WASM module was instantiated when registering.
- * Developers can also invoke the host functions directly with this API.
+ /* Wait for the execution. */
+ WasmEdge_AsyncWait(Async);
+ /*
+ * Developers can also use the `WasmEdge_AsyncGetReturnsLength()` or `WasmEdge_AsyncGet()` APIs
+ * to wait for the asynchronous execution. These APIs will wait until the execution finished.
*/
- Res = WasmEdge_VMExecuteRegistered(VMCxt, ModName, FuncName, Params, 1, Returns, 1);
- if (WasmEdge_ResultOK(Res)) {
- printf("Get the result: %d\n", WasmEdge_ValueGetI32(Returns[0]));
- } else {
- printf("Execution phase failed: %s\n", WasmEdge_ResultGetMessage(Res));
- }
- WasmEdge_StringDelete(ModName);
- WasmEdge_StringDelete(FuncName);
- WasmEdge_VMDelete(VMCxt);
- return 0;
- }
- ```
-
- Then you can compile and run: (the 20th Fibonacci number is 89 in 0-based index)
-
- ```bash
- $ gcc test.c -lwasmedge_c
- $ ./a.out
- Get the result: 10946
- ```
-### Asynchronous Execution
+ /* Check the return values length. */
+ uint32_t Arity = WasmEdge_AsyncGetReturnsLength(Async);
+ /* The `Arity` should be 1. Developers can skip this step if they have known the return arity. */
-1. Asynchronously run WASM functions rapidly
+ /* Get the result. */
+ WasmEdge_Result Res = WasmEdge_AsyncGet(Async, Returns, Arity);
+
+ if (WasmEdge_ResultOK(Res)) {
+ printf("Get the result: %d\n", WasmEdge_ValueGetI32(Returns[0]));
+ } else {
+ printf("Error message: %s\n", WasmEdge_ResultGetMessage(Res));
+ }
+
+ /* Resources deallocations. */
+ WasmEdge_AsyncDelete(Async);
+ WasmEdge_VMDelete(VMCxt);
+ WasmEdge_StringDelete(FuncName);
+ return 0;
+ }
+ ```
- Assume that the WASM file [`fibonacci.wasm`](https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/examples/wasm/fibonacci.wasm) is copied into the current directory, and the C file `test.c` is as following:
-
- ```c
- #include
- #include
- int main() {
- /* Create the VM context. */
- WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(NULL, NULL);
-
- /* The parameters and returns arrays. */
- WasmEdge_Value Params[1] = { WasmEdge_ValueGenI32(20) };
- WasmEdge_Value Returns[1];
- /* Function name. */
- WasmEdge_String FuncName = WasmEdge_StringCreateByCString("fib");
- /* Asynchronously run the WASM function from file and get the `WasmEdge_Async` object. */
- WasmEdge_Async *Async = WasmEdge_VMAsyncRunWasmFromFile(VMCxt, "fibonacci.wasm", FuncName, Params, 1);
- /*
- * Developers can run the WASM binary from buffer with the `WasmEdge_VMAsyncRunWasmFromBuffer()` API,
- * or from `WasmEdge_ASTModuleContext` object with the `WasmEdge_VMAsyncRunWasmFromASTModule()` API.
- */
-
- /* Wait for the execution. */
- WasmEdge_AsyncWait(Async);
- /*
- * Developers can also use the `WasmEdge_AsyncGetReturnsLength()` or `WasmEdge_AsyncGet()` APIs
- * to wait for the asynchronous execution. These APIs will wait until the execution finished.
- */
-
- /* Check the return values length. */
- uint32_t Arity = WasmEdge_AsyncGetReturnsLength(Async);
- /* The `Arity` should be 1. Developers can skip this step if they have known the return arity. */
-
- /* Get the result. */
- WasmEdge_Result Res = WasmEdge_AsyncGet(Async, Returns, Arity);
-
- if (WasmEdge_ResultOK(Res)) {
- printf("Get the result: %d\n", WasmEdge_ValueGetI32(Returns[0]));
- } else {
- printf("Error message: %s\n", WasmEdge_ResultGetMessage(Res));
- }
-
- /* Resources deallocations. */
- WasmEdge_AsyncDelete(Async);
- WasmEdge_VMDelete(VMCxt);
- WasmEdge_StringDelete(FuncName);
- return 0;
- }
- ```
-
- Then you can compile and run: (the 20th Fibonacci number is 10946 in 0-based index)
-
- ```bash
- $ gcc test.c -lwasmedge_c
- $ ./a.out
- Get the result: 10946
- ```
+ Then you can compile and run: (the 20th Fibonacci number is 10946 in 0-based index)
+
+ ```bash
+ $ gcc test.c -lwasmedge_c
+ $ ./a.out
+ Get the result: 10946
+ ```
2. Instantiate and asynchronously run WASM functions manually
- Besides the above example, developers can run the WASM functions step-by-step with `VM` context APIs:
-
- ```c
- #include
- #include
- int main() {
- /* Create the VM context. */
- WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(NULL, NULL);
-
- /* The parameters and returns arrays. */
- WasmEdge_Value Params[1] = { WasmEdge_ValueGenI32(25) };
- WasmEdge_Value Returns[1];
- /* Function name. */
- WasmEdge_String FuncName = WasmEdge_StringCreateByCString("fib");
- /* Result. */
- WasmEdge_Result Res;
-
- /* Step 1: Load WASM file. */
- Res = WasmEdge_VMLoadWasmFromFile(VMCxt, "fibonacci.wasm");
- /*
- * Developers can load the WASM binary from buffer with the `WasmEdge_VMLoadWasmFromBuffer()` API,
- * or from `WasmEdge_ASTModuleContext` object with the `WasmEdge_VMLoadWasmFromASTModule()` API.
- */
- if (!WasmEdge_ResultOK(Res)) {
- printf("Loading phase failed: %s\n", WasmEdge_ResultGetMessage(Res));
- return 1;
- }
- /* Step 2: Validate the WASM module. */
- Res = WasmEdge_VMValidate(VMCxt);
- if (!WasmEdge_ResultOK(Res)) {
- printf("Validation phase failed: %s\n", WasmEdge_ResultGetMessage(Res));
- return 1;
- }
- /* Step 3: Instantiate the WASM module. */
- Res = WasmEdge_VMInstantiate(VMCxt);
- /*
- * Developers can load, validate, and instantiate another WASM module to replace the
- * instantiated one. In this case, the old module will be cleared, but the registered
- * modules are still kept.
- */
- if (!WasmEdge_ResultOK(Res)) {
- printf("Instantiation phase failed: %s\n", WasmEdge_ResultGetMessage(Res));
- return 1;
- }
- /* Step 4: Asynchronously execute the WASM function and get the `WasmEdge_Async` object. */
- WasmEdge_Async *Async = WasmEdge_VMAsyncExecute(VMCxt, FuncName, Params, 1);
- /*
- * Developers can execute functions repeatedly after instantiation.
- * For invoking the registered functions, you can use the `WasmEdge_VMAsyncExecuteRegistered()` API.
- */
-
- /* Wait and check the return values length. */
- uint32_t Arity = WasmEdge_AsyncGetReturnsLength(Async);
- /* The `Arity` should be 1. Developers can skip this step if they have known the return arity. */
-
- /* Get the result. */
- Res = WasmEdge_AsyncGet(Async, Returns, Arity);
- if (WasmEdge_ResultOK(Res)) {
- printf("Get the result: %d\n", WasmEdge_ValueGetI32(Returns[0]));
- } else {
- printf("Execution phase failed: %s\n", WasmEdge_ResultGetMessage(Res));
- }
-
- /* Resources deallocations. */
- WasmEdge_AsyncDelete(Async);
- WasmEdge_VMDelete(VMCxt);
- WasmEdge_StringDelete(FuncName);
- }
- ```
-
- Then you can compile and run: (the 25th Fibonacci number is 121393 in 0-based index)
-
- ```bash
- $ gcc test.c -lwasmedge_c
- $ ./a.out
- Get the result: 121393
- ```
+ Besides the above example, developers can run the WASM functions step-by-step with `VM` context APIs:
+
+ ```c
+ #include
+ #include
+ int main() {
+ /* Create the VM context. */
+ WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(NULL, NULL);
+
+ /* The parameters and returns arrays. */
+ WasmEdge_Value Params[1] = { WasmEdge_ValueGenI32(25) };
+ WasmEdge_Value Returns[1];
+ /* Function name. */
+ WasmEdge_String FuncName = WasmEdge_StringCreateByCString("fib");
+ /* Result. */
+ WasmEdge_Result Res;
+
+ /* Step 1: Load WASM file. */
+ Res = WasmEdge_VMLoadWasmFromFile(VMCxt, "fibonacci.wasm");
+ /*
+ * Developers can load the WASM binary from buffer with the `WasmEdge_VMLoadWasmFromBuffer()` API,
+ * or from `WasmEdge_ASTModuleContext` object with the `WasmEdge_VMLoadWasmFromASTModule()` API.
+ */
+ if (!WasmEdge_ResultOK(Res)) {
+ printf("Loading phase failed: %s\n", WasmEdge_ResultGetMessage(Res));
+ return 1;
+ }
+ /* Step 2: Validate the WASM module. */
+ Res = WasmEdge_VMValidate(VMCxt);
+ if (!WasmEdge_ResultOK(Res)) {
+ printf("Validation phase failed: %s\n", WasmEdge_ResultGetMessage(Res));
+ return 1;
+ }
+ /* Step 3: Instantiate the WASM module. */
+ Res = WasmEdge_VMInstantiate(VMCxt);
+ /*
+ * Developers can load, validate, and instantiate another WASM module to replace the
+ * instantiated one. In this case, the old module will be cleared, but the registered
+ * modules are still kept.
+ */
+ if (!WasmEdge_ResultOK(Res)) {
+ printf("Instantiation phase failed: %s\n", WasmEdge_ResultGetMessage(Res));
+ return 1;
+ }
+ /* Step 4: Asynchronously execute the WASM function and get the `WasmEdge_Async` object. */
+ WasmEdge_Async *Async = WasmEdge_VMAsyncExecute(VMCxt, FuncName, Params, 1);
+ /*
+ * Developers can execute functions repeatedly after instantiation.
+ * For invoking the registered functions, you can use the `WasmEdge_VMAsyncExecuteRegistered()` API.
+ */
+
+ /* Wait and check the return values length. */
+ uint32_t Arity = WasmEdge_AsyncGetReturnsLength(Async);
+ /* The `Arity` should be 1. Developers can skip this step if they have known the return arity. */
+
+ /* Get the result. */
+ Res = WasmEdge_AsyncGet(Async, Returns, Arity);
+ if (WasmEdge_ResultOK(Res)) {
+ printf("Get the result: %d\n", WasmEdge_ValueGetI32(Returns[0]));
+ } else {
+ printf("Execution phase failed: %s\n", WasmEdge_ResultGetMessage(Res));
+ }
+
+ /* Resources deallocations. */
+ WasmEdge_AsyncDelete(Async);
+ WasmEdge_VMDelete(VMCxt);
+ WasmEdge_StringDelete(FuncName);
+ }
+ ```
+
+ Then you can compile and run: (the 25th Fibonacci number is 121393 in 0-based index)
+
+ ```bash
+ $ gcc test.c -lwasmedge_c
+ $ ./a.out
+ Get the result: 121393
+ ```
### Instance Tracing
@@ -1211,126 +1211,126 @@ Sometimes the developers may have requirements to get the instances of the WASM
1. Store
- If the `VM` context is created without assigning a `Store` context, the `VM` context will allocate and own a `Store` context.
+ If the `VM` context is created without assigning a `Store` context, the `VM` context will allocate and own a `Store` context.
- ```c
- WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(NULL, NULL);
- WasmEdge_StoreContext *StoreCxt = WasmEdge_VMGetStoreContext(VMCxt);
- /* The object should __NOT__ be deleted by `WasmEdge_StoreDelete()`. */
- WasmEdge_VMDelete(VMCxt);
- ```
+ ```c
+ WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(NULL, NULL);
+ WasmEdge_StoreContext *StoreCxt = WasmEdge_VMGetStoreContext(VMCxt);
+ /* The object should __NOT__ be deleted by `WasmEdge_StoreDelete()`. */
+ WasmEdge_VMDelete(VMCxt);
+ ```
- Developers can also create the `VM` context with a `Store` context. In this case, developers should guarantee the life cycle of the `Store` context. Please refer to the [Store Contexts](#store) for the details about the `Store` context APIs.
+ Developers can also create the `VM` context with a `Store` context. In this case, developers should guarantee the life cycle of the `Store` context. Please refer to the [Store Contexts](#store) for the details about the `Store` context APIs.
- ```c
- WasmEdge_StoreContext *StoreCxt = WasmEdge_StoreCreate();
- WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(NULL, StoreCxt);
- WasmEdge_StoreContext *StoreCxtMock = WasmEdge_VMGetStoreContext(VMCxt);
- /* The `StoreCxt` and the `StoreCxtMock` are the same. */
- WasmEdge_VMDelete(VMCxt);
- WasmEdge_StoreDelete(StoreCxt);
- ```
+ ```c
+ WasmEdge_StoreContext *StoreCxt = WasmEdge_StoreCreate();
+ WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(NULL, StoreCxt);
+ WasmEdge_StoreContext *StoreCxtMock = WasmEdge_VMGetStoreContext(VMCxt);
+ /* The `StoreCxt` and the `StoreCxtMock` are the same. */
+ WasmEdge_VMDelete(VMCxt);
+ WasmEdge_StoreDelete(StoreCxt);
+ ```
2. List exported functions
- After the WASM module instantiation, developers can use the `WasmEdge_VMExecute()` API to invoke the exported WASM functions. For this purpose, developers may need information about the exported WASM function list. Please refer to the [Instances in runtime](#instances) for the details about the function types. Assume that the WASM file [`fibonacci.wasm`](https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/examples/wasm/fibonacci.wasm) is copied into the current directory, and the C file `test.c` is as following:
-
- ```c
- #include
- #include
- int main() {
- WasmEdge_StoreContext *StoreCxt = WasmEdge_StoreCreate();
- WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(NULL, StoreCxt);
-
- WasmEdge_VMLoadWasmFromFile(VMCxt, "fibonacci.wasm");
- WasmEdge_VMValidate(VMCxt);
- WasmEdge_VMInstantiate(VMCxt);
-
- /* List the exported functions. */
- /* Get the number of exported functions. */
- uint32_t FuncNum = WasmEdge_VMGetFunctionListLength(VMCxt);
- /* Create the name buffers and the function type buffers. */
- const uint32_t BUF_LEN = 256;
- WasmEdge_String FuncNames[BUF_LEN];
- WasmEdge_FunctionTypeContext *FuncTypes[BUF_LEN];
- /*
- * Get the export function list.
- * If the function list length is larger than the buffer length, the overflowed data will be discarded.
- * The `FuncNames` and `FuncTypes` can be NULL if developers don't need them.
- */
- uint32_t RealFuncNum = WasmEdge_VMGetFunctionList(VMCxt, FuncNames, FuncTypes, BUF_LEN);
-
- for (uint32_t I = 0; I < RealFuncNum && I < BUF_LEN; I++) {
- char Buf[BUF_LEN];
- uint32_t Size = WasmEdge_StringCopy(FuncNames[I], Buf, sizeof(Buf));
- printf("Get exported function string length: %u, name: %s\n", Size, Buf);
- /*
- * The function names should be __NOT__ destroyed.
- * The returned function type contexts should __NOT__ be destroyed.
- */
- }
- return 0;
- }
- ```
-
- Then you can compile and run: (the only exported function in `fibonacci.wasm` is `fib`)
-
- ```bash
- $ gcc test.c -lwasmedge_c
- $ ./a.out
- Get exported function string length: 3, name: fib
- ```
-
- If developers want to get the exported function names in the registered WASM modules, please retrieve the `Store` context from the `VM` context and refer to the APIs of [Store Contexts](#store) to list the registered functions by the module name.
+ After the WASM module instantiation, developers can use the `WasmEdge_VMExecute()` API to invoke the exported WASM functions. For this purpose, developers may need information about the exported WASM function list. Please refer to the [Instances in runtime](#instances) for the details about the function types. Assume that the WASM file [`fibonacci.wasm`](https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/examples/wasm/fibonacci.wasm) is copied into the current directory, and the C file `test.c` is as following:
+
+ ```c
+ #include
+ #include
+ int main() {
+ WasmEdge_StoreContext *StoreCxt = WasmEdge_StoreCreate();
+ WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(NULL, StoreCxt);
+
+ WasmEdge_VMLoadWasmFromFile(VMCxt, "fibonacci.wasm");
+ WasmEdge_VMValidate(VMCxt);
+ WasmEdge_VMInstantiate(VMCxt);
+
+ /* List the exported functions. */
+ /* Get the number of exported functions. */
+ uint32_t FuncNum = WasmEdge_VMGetFunctionListLength(VMCxt);
+ /* Create the name buffers and the function type buffers. */
+ const uint32_t BUF_LEN = 256;
+ WasmEdge_String FuncNames[BUF_LEN];
+ WasmEdge_FunctionTypeContext *FuncTypes[BUF_LEN];
+ /*
+ * Get the export function list.
+ * If the function list length is larger than the buffer length, the overflowed data will be discarded.
+ * The `FuncNames` and `FuncTypes` can be NULL if developers don't need them.
+ */
+ uint32_t RealFuncNum = WasmEdge_VMGetFunctionList(VMCxt, FuncNames, FuncTypes, BUF_LEN);
+
+ for (uint32_t I = 0; I < RealFuncNum && I < BUF_LEN; I++) {
+ char Buf[BUF_LEN];
+ uint32_t Size = WasmEdge_StringCopy(FuncNames[I], Buf, sizeof(Buf));
+ printf("Get exported function string length: %u, name: %s\n", Size, Buf);
+ /*
+ * The function names should be __NOT__ destroyed.
+ * The returned function type contexts should __NOT__ be destroyed.
+ */
+ }
+ return 0;
+ }
+ ```
+
+ Then you can compile and run: (the only exported function in `fibonacci.wasm` is `fib`)
+
+ ```bash
+ $ gcc test.c -lwasmedge_c
+ $ ./a.out
+ Get exported function string length: 3, name: fib
+ ```
+
+ If developers want to get the exported function names in the registered WASM modules, please retrieve the `Store` context from the `VM` context and refer to the APIs of [Store Contexts](#store) to list the registered functions by the module name.
3. Get function types
- The `VM` context provides APIs to find the function type by function name. Please refer to the [Instances in runtime](#instances) for the details about the function types.
+ The `VM` context provides APIs to find the function type by function name. Please refer to the [Instances in runtime](#instances) for the details about the function types.
- ```c
- /*
- * ...
- * Assume that a WASM module is instantiated in `VMCxt`.
- */
- WasmEdge_String FuncName = WasmEdge_StringCreateByCString("fib");
- const WasmEdge_FunctionTypeContext *FuncType = WasmEdge_VMGetFunctionType(VMCxt, FuncName);
- /*
- * Developers can get the function types of functions in the registered modules
- * via the `WasmEdge_VMGetFunctionTypeRegistered()` API with the module name.
- * If the function is not found, these APIs will return `NULL`.
- * The returned function type contexts should __NOT__ be destroyed.
- */
- WasmEdge_StringDelete(FuncName);
- ```
+ ```c
+ /*
+ * ...
+ * Assume that a WASM module is instantiated in `VMCxt`.
+ */
+ WasmEdge_String FuncName = WasmEdge_StringCreateByCString("fib");
+ const WasmEdge_FunctionTypeContext *FuncType = WasmEdge_VMGetFunctionType(VMCxt, FuncName);
+ /*
+ * Developers can get the function types of functions in the registered modules
+ * via the `WasmEdge_VMGetFunctionTypeRegistered()` API with the module name.
+ * If the function is not found, these APIs will return `NULL`.
+ * The returned function type contexts should __NOT__ be destroyed.
+ */
+ WasmEdge_StringDelete(FuncName);
+ ```
4. Get the active module
- After the WASM module instantiation, an anonymous module is instantiated and owned by the `VM` context. Developers may need to retrieve it to get the instances beyond the module. Then developers can use the `WasmEdge_VMGetActiveModule()` API to get that anonymous module instance. Please refer to the [Module instance](#instances) for the details about the module instance APIs.
+ After the WASM module instantiation, an anonymous module is instantiated and owned by the `VM` context. Developers may need to retrieve it to get the instances beyond the module. Then developers can use the `WasmEdge_VMGetActiveModule()` API to get that anonymous module instance. Please refer to the [Module instance](#instances) for the details about the module instance APIs.
- ```c
- /*
- * ...
- * Assume that a WASM module is instantiated in `VMCxt`.
- */
- const WasmEdge_ModuleInstanceContext *ModCxt = WasmEdge_VMGetActiveModule(VMCxt);
- /*
- * If there's no WASM module instantiated, this API will return `NULL`.
- * The returned module instance context should __NOT__ be destroyed.
- */
- ```
+ ```c
+ /*
+ * ...
+ * Assume that a WASM module is instantiated in `VMCxt`.
+ */
+ const WasmEdge_ModuleInstanceContext *ModCxt = WasmEdge_VMGetActiveModule(VMCxt);
+ /*
+ * If there's no WASM module instantiated, this API will return `NULL`.
+ * The returned module instance context should __NOT__ be destroyed.
+ */
+ ```
5. Get the components
- The `VM` context is composed by the `Loader`, `Validator`, and `Executor` contexts. For the developers who want to use these contexts without creating another instances, these APIs can help developers to get them from the `VM` context. The get contexts are owned by the `VM` context, and developers should not call their delete functions.
+ The `VM` context is composed by the `Loader`, `Validator`, and `Executor` contexts. For the developers who want to use these contexts without creating another instances, these APIs can help developers to get them from the `VM` context. The get contexts are owned by the `VM` context, and developers should not call their delete functions.
- ```c
- WasmEdge_LoaderContext *LoadCxt = WasmEdge_VMGetLoaderContext(VMCxt);
- /* The object should __NOT__ be deleted by `WasmEdge_LoaderDelete()`. */
- WasmEdge_ValidatorContext *ValidCxt = WasmEdge_VMGetValidatorContext(VMCxt);
- /* The object should __NOT__ be deleted by `WasmEdge_ValidatorDelete()`. */
- WasmEdge_ExecutorContext *ExecCxt = WasmEdge_VMGetExecutorContext(VMCxt);
- /* The object should __NOT__ be deleted by `WasmEdge_ExecutorDelete()`. */
- ```
+ ```c
+ WasmEdge_LoaderContext *LoadCxt = WasmEdge_VMGetLoaderContext(VMCxt);
+ /* The object should __NOT__ be deleted by `WasmEdge_LoaderDelete()`. */
+ WasmEdge_ValidatorContext *ValidCxt = WasmEdge_VMGetValidatorContext(VMCxt);
+ /* The object should __NOT__ be deleted by `WasmEdge_ValidatorDelete()`. */
+ WasmEdge_ExecutorContext *ExecCxt = WasmEdge_VMGetExecutorContext(VMCxt);
+ /* The object should __NOT__ be deleted by `WasmEdge_ExecutorDelete()`. */
+ ```
## WasmEdge Runtime
@@ -1500,129 +1500,129 @@ The `Executor` context is the executor for both WASM and compiled-WASM. This obj
1. Instantiate and register an `AST module` as a named `Module` instance
- As the same of [registering host modules](#host-module-registrations) or [importing WASM modules](#wasm-registrations-and-executions) in `VM` contexts, developers can instantiate and register an `AST module` contexts into the `Store` context as a named `Module` instance by the `Executor` APIs. After the registration, the result `Module` instance is exported with the given module name and can be linked when instantiating another module. For the details about the `Module` instances APIs, please refer to the [Instances](#instances).
-
- ```c
- /*
- * ...
- * Assume that the `ASTCxt` is the output AST module context from the loader context
- * and has passed the validation.
- * Assume that the `ConfCxt` is the configure context.
- */
- /* Create the statistics context. This step is not necessary. */
- WasmEdge_StatisticsContext *StatCxt = WasmEdge_StatisticsCreate();
- /* Create the executor context. The configure and the statistics contexts can be NULL. */
- WasmEdge_ExecutorContext *ExecCxt = WasmEdge_ExecutorCreate(ConfCxt, StatCxt);
- /* Create the store context. The store context is the object to link the modules for imports and exports. */
- WasmEdge_StoreContext *StoreCxt = WasmEdge_StoreCreate();
- /* Result. */
- WasmEdge_Result Res;
-
- WasmEdge_String ModName = WasmEdge_StringCreateByCString("mod");
- /* The output module instance. */
- WasmEdge_ModuleInstanceContext *ModCxt = NULL;
- /* Register the WASM module into the store with the export module name "mod". */
- Res = WasmEdge_ExecutorRegister(ExecCxt, &ModCxt, StoreCxt, ASTCxt, ModName);
- if (!WasmEdge_ResultOK(Res)) {
- printf("WASM registration failed: %s\n", WasmEdge_ResultGetMessage(Res));
- return -1;
- }
- WasmEdge_StringDelete(ModName);
-
- /* ... */
-
- /* After the execution, the resources should be released. */
- WasmEdge_ExecutorDelete(ExecCxt);
- WasmEdge_StatisticsDelete(StatCxt);
- WasmEdge_StoreDelete(StoreCxt);
- WasmEdge_ModuleInstanceDelete(ModCxt);
- ```
+ As the same of [registering host modules](#host-module-registrations) or [importing WASM modules](#wasm-registrations-and-executions) in `VM` contexts, developers can instantiate and register an `AST module` contexts into the `Store` context as a named `Module` instance by the `Executor` APIs. After the registration, the result `Module` instance is exported with the given module name and can be linked when instantiating another module. For the details about the `Module` instances APIs, please refer to the [Instances](#instances).
+
+ ```c
+ /*
+ * ...
+ * Assume that the `ASTCxt` is the output AST module context from the loader context
+ * and has passed the validation.
+ * Assume that the `ConfCxt` is the configure context.
+ */
+ /* Create the statistics context. This step is not necessary. */
+ WasmEdge_StatisticsContext *StatCxt = WasmEdge_StatisticsCreate();
+ /* Create the executor context. The configure and the statistics contexts can be NULL. */
+ WasmEdge_ExecutorContext *ExecCxt = WasmEdge_ExecutorCreate(ConfCxt, StatCxt);
+ /* Create the store context. The store context is the object to link the modules for imports and exports. */
+ WasmEdge_StoreContext *StoreCxt = WasmEdge_StoreCreate();
+ /* Result. */
+ WasmEdge_Result Res;
+
+ WasmEdge_String ModName = WasmEdge_StringCreateByCString("mod");
+ /* The output module instance. */
+ WasmEdge_ModuleInstanceContext *ModCxt = NULL;
+ /* Register the WASM module into the store with the export module name "mod". */
+ Res = WasmEdge_ExecutorRegister(ExecCxt, &ModCxt, StoreCxt, ASTCxt, ModName);
+ if (!WasmEdge_ResultOK(Res)) {
+ printf("WASM registration failed: %s\n", WasmEdge_ResultGetMessage(Res));
+ return -1;
+ }
+ WasmEdge_StringDelete(ModName);
+
+ /* ... */
+
+ /* After the execution, the resources should be released. */
+ WasmEdge_ExecutorDelete(ExecCxt);
+ WasmEdge_StatisticsDelete(StatCxt);
+ WasmEdge_StoreDelete(StoreCxt);
+ WasmEdge_ModuleInstanceDelete(ModCxt);
+ ```
2. Register an existing `Module` instance and export the module name
- Besides instantiating and registering an `AST module` contexts, developers can register an existing `Module` instance into the store with exporting the module name (which is in the `Module` instance already). This case occurs when developers create a `Module` instance for the host functions and want to register it for linking. For the details about the construction of host functions in `Module` instances, please refer to the [Host Functions](#host-functions).
-
- ```c
- /*
+ Besides instantiating and registering an `AST module` contexts, developers can register an existing `Module` instance into the store with exporting the module name (which is in the `Module` instance already). This case occurs when developers create a `Module` instance for the host functions and want to register it for linking. For the details about the construction of host functions in `Module` instances, please refer to the [Host Functions](#host-functions).
+
+ ```c
+ /*
+ * ...
+ * Assume that the `ASTCxt` is the output AST module context from the loader context
+ * and has passed the validation.
+ * Assume that the `ConfCxt` is the configure context.
+ */
+ /* Create the statistics context. This step is not necessary. */
+ WasmEdge_StatisticsContext *StatCxt = WasmEdge_StatisticsCreate();
+ /* Create the executor context. The configure and the statistics contexts can be NULL. */
+ WasmEdge_ExecutorContext *ExecCxt = WasmEdge_ExecutorCreate(ConfCxt, StatCxt);
+ /* Create the store context. The store context is the object to link the modules for imports and exports. */
+ WasmEdge_StoreContext *StoreCxt = WasmEdge_StoreCreate();
+ /* Result. */
+ WasmEdge_Result Res;
+
+ /* Create a module instance for host functions. */
+ WasmEdge_String ModName = WasmEdge_StringCreateByCString("host-module");
+ WasmEdge_ModuleInstanceContext *HostModCxt = WasmEdge_ModuleInstanceCreate(ModName);
+ WasmEdge_StringDelete(ModName);
+ /*
* ...
- * Assume that the `ASTCxt` is the output AST module context from the loader context
- * and has passed the validation.
- * Assume that the `ConfCxt` is the configure context.
+ * Create and add the host functions, tables, memories, and globals into the module instance.
*/
- /* Create the statistics context. This step is not necessary. */
- WasmEdge_StatisticsContext *StatCxt = WasmEdge_StatisticsCreate();
- /* Create the executor context. The configure and the statistics contexts can be NULL. */
- WasmEdge_ExecutorContext *ExecCxt = WasmEdge_ExecutorCreate(ConfCxt, StatCxt);
- /* Create the store context. The store context is the object to link the modules for imports and exports. */
- WasmEdge_StoreContext *StoreCxt = WasmEdge_StoreCreate();
- /* Result. */
- WasmEdge_Result Res;
-
- /* Create a module instance for host functions. */
- WasmEdge_String ModName = WasmEdge_StringCreateByCString("host-module");
- WasmEdge_ModuleInstanceContext *HostModCxt = WasmEdge_ModuleInstanceCreate(ModName);
- WasmEdge_StringDelete(ModName);
- /*
- * ...
- * Create and add the host functions, tables, memories, and globals into the module instance.
- */
- /* Register the module instance into store with the exported module name. */
- /* The export module name is in the module instance already. */
- Res = WasmEdge_ExecutorRegisterImport(ExecCxt, StoreCxt, HostModCxt);
- if (!WasmEdge_ResultOK(Res)) {
- printf("WASM registration failed: %s\n", WasmEdge_ResultGetMessage(Res));
- return -1;
- }
+ /* Register the module instance into store with the exported module name. */
+ /* The export module name is in the module instance already. */
+ Res = WasmEdge_ExecutorRegisterImport(ExecCxt, StoreCxt, HostModCxt);
+ if (!WasmEdge_ResultOK(Res)) {
+ printf("WASM registration failed: %s\n", WasmEdge_ResultGetMessage(Res));
+ return -1;
+ }
- /* ... */
+ /* ... */
- /* After the execution, the resources should be released. */
- WasmEdge_ExecutorDelete(ExecCxt);
- WasmEdge_StatisticsDelete(StatCxt);
- WasmEdge_StoreDelete(StoreCxt);
- WasmEdge_ModuleInstanceDelete(ModCxt);
- ```
+ /* After the execution, the resources should be released. */
+ WasmEdge_ExecutorDelete(ExecCxt);
+ WasmEdge_StatisticsDelete(StatCxt);
+ WasmEdge_StoreDelete(StoreCxt);
+ WasmEdge_ModuleInstanceDelete(ModCxt);
+ ```
3. Instantiate an `AST module` to an anonymous `Module` instance
- WASM or compiled-WASM modules should be instantiated before the function invocation. Before instantiating a WASM module, please check the [import section](https://webassembly.github.io/spec/core/syntax/modules.html#syntax-import) for ensuring the imports are registered into the `Store` context for linking.
-
- ```c
- /*
- * ...
- * Assume that the `ASTCxt` is the output AST module context from the loader context
- * and has passed the validation.
- * Assume that the `ConfCxt` is the configure context.
- */
- /* Create the statistics context. This step is not necessary. */
- WasmEdge_StatisticsContext *StatCxt = WasmEdge_StatisticsCreate();
- /* Create the executor context. The configure and the statistics contexts can be NULL. */
- WasmEdge_ExecutorContext *ExecCxt = WasmEdge_ExecutorCreate(ConfCxt, StatCxt);
- /* Create the store context. The store context is the object to link the modules for imports and exports. */
- WasmEdge_StoreContext *StoreCxt = WasmEdge_StoreCreate();
-
- /* The output module instance. */
- WasmEdge_ModuleInstanceContext *ModCxt = NULL;
- /* Instantiate the WASM module. */
- WasmEdge_Result Res = WasmEdge_ExecutorInstantiate(ExecCxt, &ModCxt, StoreCxt, ASTCxt);
- if (!WasmEdge_ResultOK(Res)) {
- printf("WASM instantiation failed: %s\n", WasmEdge_ResultGetMessage(Res));
- return -1;
- }
-
- /* ... */
-
- /* After the execution, the resources should be released. */
- WasmEdge_ExecutorDelete(ExecCxt);
- WasmEdge_StatisticsDelete(StatCxt);
- WasmEdge_StoreDelete(StoreCxt);
- WasmEdge_ModuleInstanceDelete(ModCxt);
- ```
+ WASM or compiled-WASM modules should be instantiated before the function invocation. Before instantiating a WASM module, please check the [import section](https://webassembly.github.io/spec/core/syntax/modules.html#syntax-import) for ensuring the imports are registered into the `Store` context for linking.
+
+ ```c
+ /*
+ * ...
+ * Assume that the `ASTCxt` is the output AST module context from the loader context
+ * and has passed the validation.
+ * Assume that the `ConfCxt` is the configure context.
+ */
+ /* Create the statistics context. This step is not necessary. */
+ WasmEdge_StatisticsContext *StatCxt = WasmEdge_StatisticsCreate();
+ /* Create the executor context. The configure and the statistics contexts can be NULL. */
+ WasmEdge_ExecutorContext *ExecCxt = WasmEdge_ExecutorCreate(ConfCxt, StatCxt);
+ /* Create the store context. The store context is the object to link the modules for imports and exports. */
+ WasmEdge_StoreContext *StoreCxt = WasmEdge_StoreCreate();
+
+ /* The output module instance. */
+ WasmEdge_ModuleInstanceContext *ModCxt = NULL;
+ /* Instantiate the WASM module. */
+ WasmEdge_Result Res = WasmEdge_ExecutorInstantiate(ExecCxt, &ModCxt, StoreCxt, ASTCxt);
+ if (!WasmEdge_ResultOK(Res)) {
+ printf("WASM instantiation failed: %s\n", WasmEdge_ResultGetMessage(Res));
+ return -1;
+ }
+
+ /* ... */
+
+ /* After the execution, the resources should be released. */
+ WasmEdge_ExecutorDelete(ExecCxt);
+ WasmEdge_StatisticsDelete(StatCxt);
+ WasmEdge_StoreDelete(StoreCxt);
+ WasmEdge_ModuleInstanceDelete(ModCxt);
+ ```
4. Invoke functions
- After registering or instantiating and get the result `Module` instance, developers can retrieve the exported `Function` instances from the `Module` instance for invocation. For the details about the `Module` instances APIs, please refer to the [Instances](#instances). Please refer to the [example above](#wasm-execution-example-step-by-step) for the `Function` instance invocation with the `WasmEdge_ExecutorInvoke()` API.
+ After registering or instantiating and get the result `Module` instance, developers can retrieve the exported `Function` instances from the `Module` instance for invocation. For the details about the `Module` instances APIs, please refer to the [Instances](#instances). Please refer to the [example above](#wasm-execution-example-step-by-step) for the `Function` instance invocation with the `WasmEdge_ExecutorInvoke()` API.
### AST Module
@@ -1692,179 +1692,179 @@ The instances are the runtime structures of WASM. Developers can retrieve the `M
1. Module instance
- After instantiating or registering an `AST module` context, developers will get a `Module` instance as the result, and have the responsibility to destroy it when not in use. A `Module` instance can also be created for the host module. Please refer to the [host function](#host-functions) for the details. `Module` instance provides APIs to list and find the exported instances in the module.
-
- ```c
- /*
- * ...
- * Instantiate a WASM module via the executor context and get the `ModCxt` as the output module instance.
- */
-
- /* Try to list the exported instance of the instantiated WASM module. */
- /* Take the function instances for example here. */
- uint32_t FuncNum = WasmEdge_ModuleInstanceListFunctionLength(ModCxt);
- /* Create the name buffers. */
- const uint32_t BUF_LEN = 256;
- WasmEdge_String FuncNames[BUF_LEN];
- /* If the list length is larger than the buffer length, the overflowed data will be discarded. */
- uint32_t RealFuncNum = WasmEdge_ModuleInstanceListFunction(ModCxt, FuncNames, BUF_LEN);
- for (uint32_t I = 0; I < RealFuncNum && I < BUF_LEN; I++) {
- /* Working with the function name `FuncNames[I]` ... */
- /* The function names should __NOT__ be destroyed. */
- }
-
- /* Try to find the exported instance of the instantiated WASM module. */
- /* Take the function instances for example here. */
- /* Function name. */
- WasmEdge_String FuncName = WasmEdge_StringCreateByCString("fib");
- WasmEdge_FunctionInstanceContext *FuncCxt = WasmEdge_ModuleInstanceFindFunction(ModCxt, FuncName);
- /* `FuncCxt` will be `NULL` if the function not found. */
- /* The returned instance is owned by the module instance context and should __NOT__ be destroyed. */
- WasmEdge_StringDelete(FuncName);
- ```
+ After instantiating or registering an `AST module` context, developers will get a `Module` instance as the result, and have the responsibility to destroy it when not in use. A `Module` instance can also be created for the host module. Please refer to the [host function](#host-functions) for the details. `Module` instance provides APIs to list and find the exported instances in the module.
+
+ ```c
+ /*
+ * ...
+ * Instantiate a WASM module via the executor context and get the `ModCxt` as the output module instance.
+ */
+
+ /* Try to list the exported instance of the instantiated WASM module. */
+ /* Take the function instances for example here. */
+ uint32_t FuncNum = WasmEdge_ModuleInstanceListFunctionLength(ModCxt);
+ /* Create the name buffers. */
+ const uint32_t BUF_LEN = 256;
+ WasmEdge_String FuncNames[BUF_LEN];
+ /* If the list length is larger than the buffer length, the overflowed data will be discarded. */
+ uint32_t RealFuncNum = WasmEdge_ModuleInstanceListFunction(ModCxt, FuncNames, BUF_LEN);
+ for (uint32_t I = 0; I < RealFuncNum && I < BUF_LEN; I++) {
+ /* Working with the function name `FuncNames[I]` ... */
+ /* The function names should __NOT__ be destroyed. */
+ }
+
+ /* Try to find the exported instance of the instantiated WASM module. */
+ /* Take the function instances for example here. */
+ /* Function name. */
+ WasmEdge_String FuncName = WasmEdge_StringCreateByCString("fib");
+ WasmEdge_FunctionInstanceContext *FuncCxt = WasmEdge_ModuleInstanceFindFunction(ModCxt, FuncName);
+ /* `FuncCxt` will be `NULL` if the function not found. */
+ /* The returned instance is owned by the module instance context and should __NOT__ be destroyed. */
+ WasmEdge_StringDelete(FuncName);
+ ```
2. Function instance
- [Host functions](https://webassembly.github.io/spec/core/exec/runtime.html#syntax-hostfunc) are functions outside WebAssembly and passed to WASM modules as imports. In WasmEdge, developers can create the `Function` contexts for host functions and add them into an `Module` instance context for registering into a `VM` or a `Store`. Developers can retrieve the `Function Type` from the `Function` contexts through the API. For the details of the `Host Function` guide, please refer to the [next chapter](#host-functions).
+ [Host functions](https://webassembly.github.io/spec/core/exec/runtime.html#syntax-hostfunc) are functions outside WebAssembly and passed to WASM modules as imports. In WasmEdge, developers can create the `Function` contexts for host functions and add them into an `Module` instance context for registering into a `VM` or a `Store`. Developers can retrieve the `Function Type` from the `Function` contexts through the API. For the details of the `Host Function` guide, please refer to the [next chapter](#host-functions).
- ```c
- /* Retrieve the function instance from the module instance context. */
- WasmEdge_FunctionInstanceContext *FuncCxt = ...;
- WasmEdge_FunctionTypeContext *FuncTypeCxt = WasmEdge_FunctionInstanceGetFunctionType(FuncCxt);
- /* The `FuncTypeCxt` is owned by the `FuncCxt` and should __NOT__ be destroyed. */
+ ```c
+ /* Retrieve the function instance from the module instance context. */
+ WasmEdge_FunctionInstanceContext *FuncCxt = ...;
+ WasmEdge_FunctionTypeContext *FuncTypeCxt = WasmEdge_FunctionInstanceGetFunctionType(FuncCxt);
+ /* The `FuncTypeCxt` is owned by the `FuncCxt` and should __NOT__ be destroyed. */
- /* For the function instance creation, please refer to the `Host Function` guide. */
- ```
+ /* For the function instance creation, please refer to the `Host Function` guide. */
+ ```
3. Table instance
- In WasmEdge, developers can create the `Table` contexts and add them into an `Module` instance context for registering into a `VM` or a `Store`. The `Table` contexts supply APIs to control the data in table instances.
-
- ```c
- WasmEdge_Limit TabLimit = {.HasMax = true, .Shared = false, .Min = 10, .Max = 20};
- /* Create the table type with limit and the `FuncRef` element type. */
- WasmEdge_TableTypeContext *TabTypeCxt = WasmEdge_TableTypeCreate(WasmEdge_RefType_FuncRef, TabLimit);
- /* Create the table instance with table type. */
- WasmEdge_TableInstanceContext *HostTable = WasmEdge_TableInstanceCreate(TabTypeCxt);
- /* Delete the table type. */
- WasmEdge_TableTypeDelete(TabTypeCxt);
- WasmEdge_Result Res;
- WasmEdge_Value Data;
-
- TabTypeCxt = WasmEdge_TableInstanceGetTableType(HostTable);
- /* The `TabTypeCxt` got from table instance is owned by the `HostTable` and should __NOT__ be destroyed. */
- enum WasmEdge_RefType RefType = WasmEdge_TableTypeGetRefType(TabTypeCxt);
- /* `RefType` will be `WasmEdge_RefType_FuncRef`. */
- Data = WasmEdge_ValueGenFuncRef(5);
- Res = WasmEdge_TableInstanceSetData(HostTable, Data, 3);
- /* Set the function index 5 to the table[3]. */
- /*
- * This will get an "out of bounds table access" error
- * because the position (13) is out of the table size (10):
- * Res = WasmEdge_TableInstanceSetData(HostTable, Data, 13);
- */
- Res = WasmEdge_TableInstanceGetData(HostTable, &Data, 3);
- /* Get the FuncRef value of the table[3]. */
- /*
- * This will get an "out of bounds table access" error
- * because the position (13) is out of the table size (10):
- * Res = WasmEdge_TableInstanceGetData(HostTable, &Data, 13);
- */
+ In WasmEdge, developers can create the `Table` contexts and add them into an `Module` instance context for registering into a `VM` or a `Store`. The `Table` contexts supply APIs to control the data in table instances.
+
+ ```c
+ WasmEdge_Limit TabLimit = {.HasMax = true, .Shared = false, .Min = 10, .Max = 20};
+ /* Create the table type with limit and the `FuncRef` element type. */
+ WasmEdge_TableTypeContext *TabTypeCxt = WasmEdge_TableTypeCreate(WasmEdge_RefType_FuncRef, TabLimit);
+ /* Create the table instance with table type. */
+ WasmEdge_TableInstanceContext *HostTable = WasmEdge_TableInstanceCreate(TabTypeCxt);
+ /* Delete the table type. */
+ WasmEdge_TableTypeDelete(TabTypeCxt);
+ WasmEdge_Result Res;
+ WasmEdge_Value Data;
+
+ TabTypeCxt = WasmEdge_TableInstanceGetTableType(HostTable);
+ /* The `TabTypeCxt` got from table instance is owned by the `HostTable` and should __NOT__ be destroyed. */
+ enum WasmEdge_RefType RefType = WasmEdge_TableTypeGetRefType(TabTypeCxt);
+ /* `RefType` will be `WasmEdge_RefType_FuncRef`. */
+ Data = WasmEdge_ValueGenFuncRef(5);
+ Res = WasmEdge_TableInstanceSetData(HostTable, Data, 3);
+ /* Set the function index 5 to the table[3]. */
+ /*
+ * This will get an "out of bounds table access" error
+ * because the position (13) is out of the table size (10):
+ * Res = WasmEdge_TableInstanceSetData(HostTable, Data, 13);
+ */
+ Res = WasmEdge_TableInstanceGetData(HostTable, &Data, 3);
+ /* Get the FuncRef value of the table[3]. */
+ /*
+ * This will get an "out of bounds table access" error
+ * because the position (13) is out of the table size (10):
+ * Res = WasmEdge_TableInstanceGetData(HostTable, &Data, 13);
+ */
- uint32_t Size = WasmEdge_TableInstanceGetSize(HostTable);
- /* `Size` will be 10. */
- Res = WasmEdge_TableInstanceGrow(HostTable, 6);
- /* Grow the table size of 6, the table size will be 16. */
- /*
- * This will get an "out of bounds table access" error because
- * the size (16 + 6) will reach the table limit(20):
- * Res = WasmEdge_TableInstanceGrow(HostTable, 6);
- */
+ uint32_t Size = WasmEdge_TableInstanceGetSize(HostTable);
+ /* `Size` will be 10. */
+ Res = WasmEdge_TableInstanceGrow(HostTable, 6);
+ /* Grow the table size of 6, the table size will be 16. */
+ /*
+ * This will get an "out of bounds table access" error because
+ * the size (16 + 6) will reach the table limit(20):
+ * Res = WasmEdge_TableInstanceGrow(HostTable, 6);
+ */
- WasmEdge_TableInstanceDelete(HostTable);
- ```
+ WasmEdge_TableInstanceDelete(HostTable);
+ ```
4. Memory instance
- In WasmEdge, developers can create the `Memory` contexts and add them into an `Module` instance context for registering into a `VM` or a `Store`. The `Memory` contexts supply APIs to control the data in memory instances.
-
- ```c
- WasmEdge_Limit MemLimit = {.HasMax = true, .Shared = false, .Min = 1, .Max = 5};
- /* Create the memory type with limit. The memory page size is 64KiB. */
- WasmEdge_MemoryTypeContext *MemTypeCxt = WasmEdge_MemoryTypeCreate(MemLimit);
- /* Create the memory instance with memory type. */
- WasmEdge_MemoryInstanceContext *HostMemory = WasmEdge_MemoryInstanceCreate(MemTypeCxt);
- /* Delete the memory type. */
- WasmEdge_MemoryTypeDelete(MemTypeCxt);
- WasmEdge_Result Res;
- uint8_t Buf[256];
-
- Buf[0] = 0xAA;
- Buf[1] = 0xBB;
- Buf[2] = 0xCC;
- Res = WasmEdge_MemoryInstanceSetData(HostMemory, Buf, 0x1000, 3);
- /* Set the data[0:2] to the memory[4096:4098]. */
- /*
- * This will get an "out of bounds memory access" error
- * because [65535:65537] is out of 1 page size (65536):
- * Res = WasmEdge_MemoryInstanceSetData(HostMemory, Buf, 0xFFFF, 3);
- */
- Buf[0] = 0;
- Buf[1] = 0;
- Buf[2] = 0;
- Res = WasmEdge_MemoryInstanceGetData(HostMemory, Buf, 0x1000, 3);
- /* Get the memory[4096:4098]. Buf[0:2] will be `{0xAA, 0xBB, 0xCC}`. */
- /*
- * This will get an "out of bounds memory access" error
- * because [65535:65537] is out of 1 page size (65536):
- * Res = WasmEdge_MemoryInstanceSetData(HostMemory, Buf, 0xFFFF, 3);
- */
+ In WasmEdge, developers can create the `Memory` contexts and add them into an `Module` instance context for registering into a `VM` or a `Store`. The `Memory` contexts supply APIs to control the data in memory instances.
+
+ ```c
+ WasmEdge_Limit MemLimit = {.HasMax = true, .Shared = false, .Min = 1, .Max = 5};
+ /* Create the memory type with limit. The memory page size is 64KiB. */
+ WasmEdge_MemoryTypeContext *MemTypeCxt = WasmEdge_MemoryTypeCreate(MemLimit);
+ /* Create the memory instance with memory type. */
+ WasmEdge_MemoryInstanceContext *HostMemory = WasmEdge_MemoryInstanceCreate(MemTypeCxt);
+ /* Delete the memory type. */
+ WasmEdge_MemoryTypeDelete(MemTypeCxt);
+ WasmEdge_Result Res;
+ uint8_t Buf[256];
+
+ Buf[0] = 0xAA;
+ Buf[1] = 0xBB;
+ Buf[2] = 0xCC;
+ Res = WasmEdge_MemoryInstanceSetData(HostMemory, Buf, 0x1000, 3);
+ /* Set the data[0:2] to the memory[4096:4098]. */
+ /*
+ * This will get an "out of bounds memory access" error
+ * because [65535:65537] is out of 1 page size (65536):
+ * Res = WasmEdge_MemoryInstanceSetData(HostMemory, Buf, 0xFFFF, 3);
+ */
+ Buf[0] = 0;
+ Buf[1] = 0;
+ Buf[2] = 0;
+ Res = WasmEdge_MemoryInstanceGetData(HostMemory, Buf, 0x1000, 3);
+ /* Get the memory[4096:4098]. Buf[0:2] will be `{0xAA, 0xBB, 0xCC}`. */
+ /*
+ * This will get an "out of bounds memory access" error
+ * because [65535:65537] is out of 1 page size (65536):
+ * Res = WasmEdge_MemoryInstanceSetData(HostMemory, Buf, 0xFFFF, 3);
+ */
- uint32_t PageSize = WasmEdge_MemoryInstanceGetPageSize(HostMemory);
- /* `PageSize` will be 1. */
- Res = WasmEdge_MemoryInstanceGrowPage(HostMemory, 2);
- /* Grow the page size of 2, the page size of the memory instance will be 3. */
- /*
- * This will get an "out of bounds memory access" error because
- * the page size (3 + 3) will reach the memory limit(5):
- * Res = WasmEdge_MemoryInstanceGrowPage(HostMemory, 3);
- */
+ uint32_t PageSize = WasmEdge_MemoryInstanceGetPageSize(HostMemory);
+ /* `PageSize` will be 1. */
+ Res = WasmEdge_MemoryInstanceGrowPage(HostMemory, 2);
+ /* Grow the page size of 2, the page size of the memory instance will be 3. */
+ /*
+ * This will get an "out of bounds memory access" error because
+ * the page size (3 + 3) will reach the memory limit(5):
+ * Res = WasmEdge_MemoryInstanceGrowPage(HostMemory, 3);
+ */
- WasmEdge_MemoryInstanceDelete(HostMemory);
- ```
+ WasmEdge_MemoryInstanceDelete(HostMemory);
+ ```
5. Global instance
- In WasmEdge, developers can create the `Global` contexts and add them into an `Module` instance context for registering into a `VM` or a `Store`. The `Global` contexts supply APIs to control the value in global instances.
-
- ```c
- WasmEdge_Value Val = WasmEdge_ValueGenI64(1000);
- /* Create the global type with value type and mutation. */
- WasmEdge_GlobalTypeContext *GlobTypeCxt = WasmEdge_GlobalTypeCreate(WasmEdge_ValType_I64, WasmEdge_Mutability_Var);
- /* Create the global instance with value and global type. */
- WasmEdge_GlobalInstanceContext *HostGlobal = WasmEdge_GlobalInstanceCreate(GlobTypeCxt, Val);
- /* Delete the global type. */
- WasmEdge_GlobalTypeDelete(GlobTypeCxt);
- WasmEdge_Result Res;
-
- GlobTypeCxt = WasmEdge_GlobalInstanceGetGlobalType(HostGlobal);
- /* The `GlobTypeCxt` got from global instance is owned by the `HostGlobal` and should __NOT__ be destroyed. */
- enum WasmEdge_ValType ValType = WasmEdge_GlobalTypeGetValType(GlobTypeCxt);
- /* `ValType` will be `WasmEdge_ValType_I64`. */
- enum WasmEdge_Mutability ValMut = WasmEdge_GlobalTypeGetMutability(GlobTypeCxt);
- /* `ValMut` will be `WasmEdge_Mutability_Var`. */
-
- WasmEdge_GlobalInstanceSetValue(HostGlobal, WasmEdge_ValueGenI64(888));
- /*
- * Set the value u64(888) to the global.
- * This function will do nothing if the value type mismatched or
- * the global mutability is `WasmEdge_Mutability_Const`.
- */
- WasmEdge_Value GlobVal = WasmEdge_GlobalInstanceGetValue(HostGlobal);
- /* Get the value (888 now) of the global context. */
+ In WasmEdge, developers can create the `Global` contexts and add them into an `Module` instance context for registering into a `VM` or a `Store`. The `Global` contexts supply APIs to control the value in global instances.
+
+ ```c
+ WasmEdge_Value Val = WasmEdge_ValueGenI64(1000);
+ /* Create the global type with value type and mutation. */
+ WasmEdge_GlobalTypeContext *GlobTypeCxt = WasmEdge_GlobalTypeCreate(WasmEdge_ValType_I64, WasmEdge_Mutability_Var);
+ /* Create the global instance with value and global type. */
+ WasmEdge_GlobalInstanceContext *HostGlobal = WasmEdge_GlobalInstanceCreate(GlobTypeCxt, Val);
+ /* Delete the global type. */
+ WasmEdge_GlobalTypeDelete(GlobTypeCxt);
+ WasmEdge_Result Res;
+
+ GlobTypeCxt = WasmEdge_GlobalInstanceGetGlobalType(HostGlobal);
+ /* The `GlobTypeCxt` got from global instance is owned by the `HostGlobal` and should __NOT__ be destroyed. */
+ enum WasmEdge_ValType ValType = WasmEdge_GlobalTypeGetValType(GlobTypeCxt);
+ /* `ValType` will be `WasmEdge_ValType_I64`. */
+ enum WasmEdge_Mutability ValMut = WasmEdge_GlobalTypeGetMutability(GlobTypeCxt);
+ /* `ValMut` will be `WasmEdge_Mutability_Var`. */
+
+ WasmEdge_GlobalInstanceSetValue(HostGlobal, WasmEdge_ValueGenI64(888));
+ /*
+ * Set the value u64(888) to the global.
+ * This function will do nothing if the value type mismatched or
+ * the global mutability is `WasmEdge_Mutability_Const`.
+ */
+ WasmEdge_Value GlobVal = WasmEdge_GlobalInstanceGetValue(HostGlobal);
+ /* Get the value (888 now) of the global context. */
- WasmEdge_GlobalInstanceDelete(HostGlobal);
- ```
+ WasmEdge_GlobalInstanceDelete(HostGlobal);
+ ```
### Host Functions
@@ -1872,402 +1872,402 @@ The instances are the runtime structures of WASM. Developers can retrieve the `M
1. Host function allocation
- Developers can define C functions with the following function signature as the host function body:
-
- ```c
- typedef WasmEdge_Result (*WasmEdge_HostFunc_t)(
- void *Data,
- WasmEdge_MemoryInstanceContext *MemCxt,
- const WasmEdge_Value *Params,
- WasmEdge_Value *Returns);
- ```
-
- The example of an `add` host function to add 2 `i32` values:
-
- ```c
- WasmEdge_Result Add(void *, WasmEdge_MemoryInstanceContext *,
- const WasmEdge_Value *In, WasmEdge_Value *Out) {
- /*
- * Params: {i32, i32}
- * Returns: {i32}
- * Developers should take care about the function type.
- */
- /* Retrieve the value 1. */
- int32_t Val1 = WasmEdge_ValueGetI32(In[0]);
- /* Retrieve the value 2. */
- int32_t Val2 = WasmEdge_ValueGetI32(In[1]);
- /* Output value 1 is Val1 + Val2. */
- Out[0] = WasmEdge_ValueGenI32(Val1 + Val2);
- /* Return the status of success. */
- return WasmEdge_Result_Success;
- }
- ```
-
- Then developers can create `Function` context with the host function body and the function type:
-
- ```c
- enum WasmEdge_ValType ParamList[2] = { WasmEdge_ValType_I32, WasmEdge_ValType_I32 };
- enum WasmEdge_ValType ReturnList[1] = { WasmEdge_ValType_I32 };
- /* Create a function type: {i32, i32} -> {i32}. */
- WasmEdge_FunctionTypeContext *HostFType = WasmEdge_FunctionTypeCreate(ParamList, 2, ReturnList, 1);
- /*
- * Create a function context with the function type and host function body.
- * The `Cost` parameter can be 0 if developers do not need the cost measuring.
+ Developers can define C functions with the following function signature as the host function body:
+
+ ```c
+ typedef WasmEdge_Result (*WasmEdge_HostFunc_t)(
+ void *Data,
+ WasmEdge_MemoryInstanceContext *MemCxt,
+ const WasmEdge_Value *Params,
+ WasmEdge_Value *Returns);
+ ```
+
+ The example of an `add` host function to add 2 `i32` values:
+
+ ```c
+ WasmEdge_Result Add(void *, WasmEdge_MemoryInstanceContext *,
+ const WasmEdge_Value *In, WasmEdge_Value *Out) {
+ /*
+ * Params: {i32, i32}
+ * Returns: {i32}
+ * Developers should take care about the function type.
*/
- WasmEdge_FunctionInstanceContext *HostFunc = WasmEdge_FunctionInstanceCreate(HostFType, Add, NULL, 0);
- /*
- * The third parameter is the pointer to the additional data.
- * Developers should guarantee the life cycle of the data, and it can be
- * `NULL` if the external data is not needed.
- */
- WasmEdge_FunctionTypeDelete(HostType);
+ /* Retrieve the value 1. */
+ int32_t Val1 = WasmEdge_ValueGetI32(In[0]);
+ /* Retrieve the value 2. */
+ int32_t Val2 = WasmEdge_ValueGetI32(In[1]);
+ /* Output value 1 is Val1 + Val2. */
+ Out[0] = WasmEdge_ValueGenI32(Val1 + Val2);
+ /* Return the status of success. */
+ return WasmEdge_Result_Success;
+ }
+ ```
+
+ Then developers can create `Function` context with the host function body and the function type:
+
+ ```c
+ enum WasmEdge_ValType ParamList[2] = { WasmEdge_ValType_I32, WasmEdge_ValType_I32 };
+ enum WasmEdge_ValType ReturnList[1] = { WasmEdge_ValType_I32 };
+ /* Create a function type: {i32, i32} -> {i32}. */
+ WasmEdge_FunctionTypeContext *HostFType = WasmEdge_FunctionTypeCreate(ParamList, 2, ReturnList, 1);
+ /*
+ * Create a function context with the function type and host function body.
+ * The `Cost` parameter can be 0 if developers do not need the cost measuring.
+ */
+ WasmEdge_FunctionInstanceContext *HostFunc = WasmEdge_FunctionInstanceCreate(HostFType, Add, NULL, 0);
+ /*
+ * The third parameter is the pointer to the additional data.
+ * Developers should guarantee the life cycle of the data, and it can be
+ * `NULL` if the external data is not needed.
+ */
+ WasmEdge_FunctionTypeDelete(HostType);
- /* If the function instance is not added into a module instance context, it should be deleted. */
- WasmEdge_FunctionInstanceDelete(HostFunc);
- ```
+ /* If the function instance is not added into a module instance context, it should be deleted. */
+ WasmEdge_FunctionInstanceDelete(HostFunc);
+ ```
2. Construct a module instance with host instances
- Besides creating a `Module` instance by registering or instantiating a WASM module, developers can create a `Module` instance with a module name and add the `Function`, `Memory`, `Table`, and `Global` instances into it with their exporting names.
-
- ```c
- /* Host function body definition. */
- WasmEdge_Result Add(void *Data, WasmEdge_MemoryInstanceContext *MemCxt,
- const WasmEdge_Value *In, WasmEdge_Value *Out) {
- int32_t Val1 = WasmEdge_ValueGetI32(In[0]);
- int32_t Val2 = WasmEdge_ValueGetI32(In[1]);
- Out[0] = WasmEdge_ValueGenI32(Val1 + Val2);
- return WasmEdge_Result_Success;
- }
-
- /* Create a module instance. */
- WasmEdge_String ExportName = WasmEdge_StringCreateByCString("module");
- WasmEdge_ModuleInstanceContext *HostModCxt = WasmEdge_ModuleInstanceCreate(ExportName);
- WasmEdge_StringDelete(ExportName);
-
- /* Create and add a function instance into the module instance. */
- enum WasmEdge_ValType ParamList[2] = { WasmEdge_ValType_I32, WasmEdge_ValType_I32 };
- enum WasmEdge_ValType ReturnList[1] = { WasmEdge_ValType_I32 };
- WasmEdge_FunctionTypeContext *HostFType =
- WasmEdge_FunctionTypeCreate(ParamList, 2, ReturnList, 1);
- WasmEdge_FunctionInstanceContext *HostFunc =
- WasmEdge_FunctionInstanceCreate(HostFType, Add, NULL, 0);
- /*
- * The third parameter is the pointer to the additional data object.
- * Developers should guarantee the life cycle of the data, and it can be
- * `NULL` if the external data is not needed.
- */
- WasmEdge_FunctionTypeDelete(HostFType);
- WasmEdge_String FuncName = WasmEdge_StringCreateByCString("add");
- WasmEdge_ModuleInstanceAddFunction(HostModCxt, FuncName, HostFunc);
- WasmEdge_StringDelete(FuncName);
-
- /* Create and add a table instance into the import object. */
- WasmEdge_Limit TableLimit = {.HasMax = true, .Shared = false, .Min = 10, .Max = 20};
- WasmEdge_TableTypeContext *HostTType =
- WasmEdge_TableTypeCreate(WasmEdge_RefType_FuncRef, TableLimit);
- WasmEdge_TableInstanceContext *HostTable = WasmEdge_TableInstanceCreate(HostTType);
- WasmEdge_TableTypeDelete(HostTType);
- WasmEdge_String TableName = WasmEdge_StringCreateByCString("table");
- WasmEdge_ModuleInstanceAddTable(HostModCxt, TableName, HostTable);
- WasmEdge_StringDelete(TableName);
-
- /* Create and add a memory instance into the import object. */
- WasmEdge_Limit MemoryLimit = {.HasMax = true, .Shared = false, .Min = 1, .Max = 2};
- WasmEdge_MemoryTypeContext *HostMType = WasmEdge_MemoryTypeCreate(MemoryLimit);
- WasmEdge_MemoryInstanceContext *HostMemory = WasmEdge_MemoryInstanceCreate(HostMType);
- WasmEdge_MemoryTypeDelete(HostMType);
- WasmEdge_String MemoryName = WasmEdge_StringCreateByCString("memory");
- WasmEdge_ModuleInstanceAddMemory(HostModCxt, MemoryName, HostMemory);
- WasmEdge_StringDelete(MemoryName);
-
- /* Create and add a global instance into the module instance. */
- WasmEdge_GlobalTypeContext *HostGType =
- WasmEdge_GlobalTypeCreate(WasmEdge_ValType_I32, WasmEdge_Mutability_Var);
- WasmEdge_GlobalInstanceContext *HostGlobal =
- WasmEdge_GlobalInstanceCreate(HostGType, WasmEdge_ValueGenI32(666));
- WasmEdge_GlobalTypeDelete(HostGType);
- WasmEdge_String GlobalName = WasmEdge_StringCreateByCString("global");
- WasmEdge_ModuleInstanceAddGlobal(HostModCxt, GlobalName, HostGlobal);
- WasmEdge_StringDelete(GlobalName);
-
- /*
- * The module instance should be deleted.
- * Developers should __NOT__ destroy the instances added into the module instance contexts.
- */
- WasmEdge_ModuleInstanceDelete(HostModCxt);
- ```
+ Besides creating a `Module` instance by registering or instantiating a WASM module, developers can create a `Module` instance with a module name and add the `Function`, `Memory`, `Table`, and `Global` instances into it with their exporting names.
+
+ ```c
+ /* Host function body definition. */
+ WasmEdge_Result Add(void *Data, WasmEdge_MemoryInstanceContext *MemCxt,
+ const WasmEdge_Value *In, WasmEdge_Value *Out) {
+ int32_t Val1 = WasmEdge_ValueGetI32(In[0]);
+ int32_t Val2 = WasmEdge_ValueGetI32(In[1]);
+ Out[0] = WasmEdge_ValueGenI32(Val1 + Val2);
+ return WasmEdge_Result_Success;
+ }
+
+ /* Create a module instance. */
+ WasmEdge_String ExportName = WasmEdge_StringCreateByCString("module");
+ WasmEdge_ModuleInstanceContext *HostModCxt = WasmEdge_ModuleInstanceCreate(ExportName);
+ WasmEdge_StringDelete(ExportName);
+
+ /* Create and add a function instance into the module instance. */
+ enum WasmEdge_ValType ParamList[2] = { WasmEdge_ValType_I32, WasmEdge_ValType_I32 };
+ enum WasmEdge_ValType ReturnList[1] = { WasmEdge_ValType_I32 };
+ WasmEdge_FunctionTypeContext *HostFType =
+ WasmEdge_FunctionTypeCreate(ParamList, 2, ReturnList, 1);
+ WasmEdge_FunctionInstanceContext *HostFunc =
+ WasmEdge_FunctionInstanceCreate(HostFType, Add, NULL, 0);
+ /*
+ * The third parameter is the pointer to the additional data object.
+ * Developers should guarantee the life cycle of the data, and it can be
+ * `NULL` if the external data is not needed.
+ */
+ WasmEdge_FunctionTypeDelete(HostFType);
+ WasmEdge_String FuncName = WasmEdge_StringCreateByCString("add");
+ WasmEdge_ModuleInstanceAddFunction(HostModCxt, FuncName, HostFunc);
+ WasmEdge_StringDelete(FuncName);
+
+ /* Create and add a table instance into the import object. */
+ WasmEdge_Limit TableLimit = {.HasMax = true, .Shared = false, .Min = 10, .Max = 20};
+ WasmEdge_TableTypeContext *HostTType =
+ WasmEdge_TableTypeCreate(WasmEdge_RefType_FuncRef, TableLimit);
+ WasmEdge_TableInstanceContext *HostTable = WasmEdge_TableInstanceCreate(HostTType);
+ WasmEdge_TableTypeDelete(HostTType);
+ WasmEdge_String TableName = WasmEdge_StringCreateByCString("table");
+ WasmEdge_ModuleInstanceAddTable(HostModCxt, TableName, HostTable);
+ WasmEdge_StringDelete(TableName);
+
+ /* Create and add a memory instance into the import object. */
+ WasmEdge_Limit MemoryLimit = {.HasMax = true, .Shared = false, .Min = 1, .Max = 2};
+ WasmEdge_MemoryTypeContext *HostMType = WasmEdge_MemoryTypeCreate(MemoryLimit);
+ WasmEdge_MemoryInstanceContext *HostMemory = WasmEdge_MemoryInstanceCreate(HostMType);
+ WasmEdge_MemoryTypeDelete(HostMType);
+ WasmEdge_String MemoryName = WasmEdge_StringCreateByCString("memory");
+ WasmEdge_ModuleInstanceAddMemory(HostModCxt, MemoryName, HostMemory);
+ WasmEdge_StringDelete(MemoryName);
+
+ /* Create and add a global instance into the module instance. */
+ WasmEdge_GlobalTypeContext *HostGType =
+ WasmEdge_GlobalTypeCreate(WasmEdge_ValType_I32, WasmEdge_Mutability_Var);
+ WasmEdge_GlobalInstanceContext *HostGlobal =
+ WasmEdge_GlobalInstanceCreate(HostGType, WasmEdge_ValueGenI32(666));
+ WasmEdge_GlobalTypeDelete(HostGType);
+ WasmEdge_String GlobalName = WasmEdge_StringCreateByCString("global");
+ WasmEdge_ModuleInstanceAddGlobal(HostModCxt, GlobalName, HostGlobal);
+ WasmEdge_StringDelete(GlobalName);
+
+ /*
+ * The module instance should be deleted.
+ * Developers should __NOT__ destroy the instances added into the module instance contexts.
+ */
+ WasmEdge_ModuleInstanceDelete(HostModCxt);
+ ```
3. Specified module instance
- `WasmEdge_ModuleInstanceCreateWASI()` API can create and initialize the `WASI` module instance.
+ `WasmEdge_ModuleInstanceCreateWASI()` API can create and initialize the `WASI` module instance.
- `WasmEdge_ModuleInstanceCreateWasiNN()` API can create and initialize the `wasi_ephemeral_nn` module instance for `WASI-NN` plugin (`0.10.1` or upper only).
+ `WasmEdge_ModuleInstanceCreateWasiNN()` API can create and initialize the `wasi_ephemeral_nn` module instance for `WASI-NN` plugin (`0.10.1` or upper only).
- `WasmEdge_ModuleInstanceCreateWasiCryptoCommon()` API can create and initialize the `wasi_ephemeral_crypto_common` module instance for `WASI-Crypto` plugin (`0.10.1` or upper only).
+ `WasmEdge_ModuleInstanceCreateWasiCryptoCommon()` API can create and initialize the `wasi_ephemeral_crypto_common` module instance for `WASI-Crypto` plugin (`0.10.1` or upper only).
- `WasmEdge_ModuleInstanceCreateWasiCryptoAsymmetricCommon()` API can create and initialize the `wasi_ephemeral_crypto_asymmetric_common` module instance for `WASI-Crypto` plugin (`0.10.1` or upper only).
+ `WasmEdge_ModuleInstanceCreateWasiCryptoAsymmetricCommon()` API can create and initialize the `wasi_ephemeral_crypto_asymmetric_common` module instance for `WASI-Crypto` plugin (`0.10.1` or upper only).
- `WasmEdge_ModuleInstanceCreateWasiCryptoKx()` API can create and initialize the `wasi_ephemeral_crypto_kx` module instance for `WASI-Crypto` plugin (`0.10.1` or upper only).
+ `WasmEdge_ModuleInstanceCreateWasiCryptoKx()` API can create and initialize the `wasi_ephemeral_crypto_kx` module instance for `WASI-Crypto` plugin (`0.10.1` or upper only).
- `WasmEdge_ModuleInstanceCreateWasiCryptoSignatures()` API can create and initialize the `wasi_ephemeral_crypto_signatures` module instance for `WASI-Crypto` plugin (`0.10.1` or upper only).
+ `WasmEdge_ModuleInstanceCreateWasiCryptoSignatures()` API can create and initialize the `wasi_ephemeral_crypto_signatures` module instance for `WASI-Crypto` plugin (`0.10.1` or upper only).
- `WasmEdge_ModuleInstanceCreateWasiCryptoSymmetric()` API can create and initialize the `wasi_ephemeral_crypto_symmetric` module instance for `WASI-Crypto` plugin (`0.10.1` or upper only).
+ `WasmEdge_ModuleInstanceCreateWasiCryptoSymmetric()` API can create and initialize the `wasi_ephemeral_crypto_symmetric` module instance for `WASI-Crypto` plugin (`0.10.1` or upper only).
- `WasmEdge_ModuleInstanceCreateWasmEdgeProcess()` API can create and initialize the `wasmedge_process` module instance for `wasmedge_process` plugin.
+ `WasmEdge_ModuleInstanceCreateWasmEdgeProcess()` API can create and initialize the `wasmedge_process` module instance for `wasmedge_process` plugin.
- Developers can create these module instance contexts and register them into the `Store` or `VM` contexts rather than adjust the settings in the `Configure` contexts.
+ Developers can create these module instance contexts and register them into the `Store` or `VM` contexts rather than adjust the settings in the `Configure` contexts.
- > Note: For the `WASI-NN` plugin, please check that the [dependencies and prerequests](/develop/build-and-run/install.md#wasi-nn-plugin-with-openvino™-backend) are satisfied. Note: For the `WASI-Crypto` plugin, please check that the [dependencies and prerequests](/develop/build-and-run/install.md#wasi-crypto-plugin) are satisfied. And the 5 modules are recommended to all be created and registered together.
+ > Note: For the `WASI-NN` plugin, please check that the [dependencies and prerequests](/develop/build-and-run/install.md#wasi-nn-plugin-with-openvino™-backend) are satisfied. Note: For the `WASI-Crypto` plugin, please check that the [dependencies and prerequests](/develop/build-and-run/install.md#wasi-crypto-plugin) are satisfied. And the 5 modules are recommended to all be created and registered together.
- ```c
- WasmEdge_ModuleInstanceContext *WasiModCxt = WasmEdge_ModuleInstanceCreateWASI( /* ... ignored */ );
- WasmEdge_ModuleInstanceContext *ProcModCxt = WasmEdge_ModuleInstanceCreateWasmEdgeProcess( /* ... ignored */ );
- WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(NULL, NULL);
- /* Register the WASI and WasmEdge_Process into the VM context. */
- WasmEdge_VMRegisterModuleFromImport(VMCxt, WasiModCxt);
- WasmEdge_VMRegisterModuleFromImport(VMCxt, ProcModCxt);
- /* Get the WASI exit code. */
- uint32_t ExitCode = WasmEdge_ModuleInstanceWASIGetExitCode(WasiModCxt);
- /*
- * The `ExitCode` will be EXIT_SUCCESS if the execution has no error.
- * Otherwise, it will return with the related exit code.
- */
- WasmEdge_VMDelete(VMCxt);
- /* The module instances should be deleted. */
- WasmEdge_ModuleInstanceDelete(WasiModCxt);
- WasmEdge_ModuleInstanceDelete(ProcModCxt);
- ```
+ ```c
+ WasmEdge_ModuleInstanceContext *WasiModCxt = WasmEdge_ModuleInstanceCreateWASI( /* ... ignored */ );
+ WasmEdge_ModuleInstanceContext *ProcModCxt = WasmEdge_ModuleInstanceCreateWasmEdgeProcess( /* ... ignored */ );
+ WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(NULL, NULL);
+ /* Register the WASI and WasmEdge_Process into the VM context. */
+ WasmEdge_VMRegisterModuleFromImport(VMCxt, WasiModCxt);
+ WasmEdge_VMRegisterModuleFromImport(VMCxt, ProcModCxt);
+ /* Get the WASI exit code. */
+ uint32_t ExitCode = WasmEdge_ModuleInstanceWASIGetExitCode(WasiModCxt);
+ /*
+ * The `ExitCode` will be EXIT_SUCCESS if the execution has no error.
+ * Otherwise, it will return with the related exit code.
+ */
+ WasmEdge_VMDelete(VMCxt);
+ /* The module instances should be deleted. */
+ WasmEdge_ModuleInstanceDelete(WasiModCxt);
+ WasmEdge_ModuleInstanceDelete(ProcModCxt);
+ ```
4. Example
- Assume that a simple WASM from the WAT as following:
-
- ```wasm
- (module
- (type $t0 (func (param i32 i32) (result i32)))
- (import "extern" "func-add" (func $f-add (type $t0)))
- (func (export "addTwo") (param i32 i32) (result i32)
- local.get 0
- local.get 1
- call $f-add)
- )
- ```
-
- And the `test.c` as following:
-
- ```c
- #include
- #include
-
- /* Host function body definition. */
- WasmEdge_Result Add(void *Data, WasmEdge_MemoryInstanceContext *MemCxt,
- const WasmEdge_Value *In, WasmEdge_Value *Out) {
- int32_t Val1 = WasmEdge_ValueGetI32(In[0]);
- int32_t Val2 = WasmEdge_ValueGetI32(In[1]);
- printf("Host function \"Add\": %d + %d\n", Val1, Val2);
- Out[0] = WasmEdge_ValueGenI32(Val1 + Val2);
- return WasmEdge_Result_Success;
- }
-
- int main() {
- /* Create the VM context. */
- WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(NULL, NULL);
-
- /* The WASM module buffer. */
- uint8_t WASM[] = {
- /* WASM header */
- 0x00, 0x61, 0x73, 0x6D, 0x01, 0x00, 0x00, 0x00,
- /* Type section */
- 0x01, 0x07, 0x01,
- /* function type {i32, i32} -> {i32} */
- 0x60, 0x02, 0x7F, 0x7F, 0x01, 0x7F,
- /* Import section */
- 0x02, 0x13, 0x01,
- /* module name: "extern" */
- 0x06, 0x65, 0x78, 0x74, 0x65, 0x72, 0x6E,
- /* extern name: "func-add" */
- 0x08, 0x66, 0x75, 0x6E, 0x63, 0x2D, 0x61, 0x64, 0x64,
- /* import desc: func 0 */
- 0x00, 0x00,
- /* Function section */
- 0x03, 0x02, 0x01, 0x00,
- /* Export section */
- 0x07, 0x0A, 0x01,
- /* export name: "addTwo" */
- 0x06, 0x61, 0x64, 0x64, 0x54, 0x77, 0x6F,
- /* export desc: func 0 */
- 0x00, 0x01,
- /* Code section */
- 0x0A, 0x0A, 0x01,
- /* code body */
- 0x08, 0x00, 0x20, 0x00, 0x20, 0x01, 0x10, 0x00, 0x0B
- };
-
- /* Create the module instance. */
- WasmEdge_String ExportName = WasmEdge_StringCreateByCString("extern");
- WasmEdge_ModuleInstanceContext *HostModCxt = WasmEdge_ModuleInstanceCreate(ExportName);
- enum WasmEdge_ValType ParamList[2] = { WasmEdge_ValType_I32, WasmEdge_ValType_I32 };
- enum WasmEdge_ValType ReturnList[1] = { WasmEdge_ValType_I32 };
- WasmEdge_FunctionTypeContext *HostFType = WasmEdge_FunctionTypeCreate(ParamList, 2, ReturnList, 1);
- WasmEdge_FunctionInstanceContext *HostFunc = WasmEdge_FunctionInstanceCreate(HostFType, Add, NULL, 0);
- WasmEdge_FunctionTypeDelete(HostFType);
- WasmEdge_String HostFuncName = WasmEdge_StringCreateByCString("func-add");
- WasmEdge_ModuleInstanceAddFunction(HostModCxt, HostFuncName, HostFunc);
- WasmEdge_StringDelete(HostFuncName);
-
- WasmEdge_VMRegisterModuleFromImport(VMCxt, HostModCxt);
-
- /* The parameters and returns arrays. */
- WasmEdge_Value Params[2] = { WasmEdge_ValueGenI32(1234), WasmEdge_ValueGenI32(5678) };
- WasmEdge_Value Returns[1];
- /* Function name. */
- WasmEdge_String FuncName = WasmEdge_StringCreateByCString("addTwo");
- /* Run the WASM function from file. */
- WasmEdge_Result Res = WasmEdge_VMRunWasmFromBuffer(
- VMCxt, WASM, sizeof(WASM), FuncName, Params, 2, Returns, 1);
-
- if (WasmEdge_ResultOK(Res)) {
- printf("Get the result: %d\n", WasmEdge_ValueGetI32(Returns[0]));
- } else {
- printf("Error message: %s\n", WasmEdge_ResultGetMessage(Res));
- }
-
- /* Resources deallocations. */
- WasmEdge_VMDelete(VMCxt);
- WasmEdge_StringDelete(FuncName);
- WasmEdge_ModuleInstanceDelete(HostModCxt);
- return 0;
- }
- ```
-
- Then you can compile and run: (the result of 1234 + 5678 is 6912)
-
- ```bash
- $ gcc test.c -lwasmedge_c
- $ ./a.out
- Host function "Add": 1234 + 5678
- Get the result: 6912
- ```
+ Assume that a simple WASM from the WAT as following:
+
+ ```wasm
+ (module
+ (type $t0 (func (param i32 i32) (result i32)))
+ (import "extern" "func-add" (func $f-add (type $t0)))
+ (func (export "addTwo") (param i32 i32) (result i32)
+ local.get 0
+ local.get 1
+ call $f-add)
+ )
+ ```
+
+ And the `test.c` as following:
+
+ ```c
+ #include
+ #include
+
+ /* Host function body definition. */
+ WasmEdge_Result Add(void *Data, WasmEdge_MemoryInstanceContext *MemCxt,
+ const WasmEdge_Value *In, WasmEdge_Value *Out) {
+ int32_t Val1 = WasmEdge_ValueGetI32(In[0]);
+ int32_t Val2 = WasmEdge_ValueGetI32(In[1]);
+ printf("Host function \"Add\": %d + %d\n", Val1, Val2);
+ Out[0] = WasmEdge_ValueGenI32(Val1 + Val2);
+ return WasmEdge_Result_Success;
+ }
+
+ int main() {
+ /* Create the VM context. */
+ WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(NULL, NULL);
+
+ /* The WASM module buffer. */
+ uint8_t WASM[] = {
+ /* WASM header */
+ 0x00, 0x61, 0x73, 0x6D, 0x01, 0x00, 0x00, 0x00,
+ /* Type section */
+ 0x01, 0x07, 0x01,
+ /* function type {i32, i32} -> {i32} */
+ 0x60, 0x02, 0x7F, 0x7F, 0x01, 0x7F,
+ /* Import section */
+ 0x02, 0x13, 0x01,
+ /* module name: "extern" */
+ 0x06, 0x65, 0x78, 0x74, 0x65, 0x72, 0x6E,
+ /* extern name: "func-add" */
+ 0x08, 0x66, 0x75, 0x6E, 0x63, 0x2D, 0x61, 0x64, 0x64,
+ /* import desc: func 0 */
+ 0x00, 0x00,
+ /* Function section */
+ 0x03, 0x02, 0x01, 0x00,
+ /* Export section */
+ 0x07, 0x0A, 0x01,
+ /* export name: "addTwo" */
+ 0x06, 0x61, 0x64, 0x64, 0x54, 0x77, 0x6F,
+ /* export desc: func 0 */
+ 0x00, 0x01,
+ /* Code section */
+ 0x0A, 0x0A, 0x01,
+ /* code body */
+ 0x08, 0x00, 0x20, 0x00, 0x20, 0x01, 0x10, 0x00, 0x0B
+ };
+
+ /* Create the module instance. */
+ WasmEdge_String ExportName = WasmEdge_StringCreateByCString("extern");
+ WasmEdge_ModuleInstanceContext *HostModCxt = WasmEdge_ModuleInstanceCreate(ExportName);
+ enum WasmEdge_ValType ParamList[2] = { WasmEdge_ValType_I32, WasmEdge_ValType_I32 };
+ enum WasmEdge_ValType ReturnList[1] = { WasmEdge_ValType_I32 };
+ WasmEdge_FunctionTypeContext *HostFType = WasmEdge_FunctionTypeCreate(ParamList, 2, ReturnList, 1);
+ WasmEdge_FunctionInstanceContext *HostFunc = WasmEdge_FunctionInstanceCreate(HostFType, Add, NULL, 0);
+ WasmEdge_FunctionTypeDelete(HostFType);
+ WasmEdge_String HostFuncName = WasmEdge_StringCreateByCString("func-add");
+ WasmEdge_ModuleInstanceAddFunction(HostModCxt, HostFuncName, HostFunc);
+ WasmEdge_StringDelete(HostFuncName);
+
+ WasmEdge_VMRegisterModuleFromImport(VMCxt, HostModCxt);
+
+ /* The parameters and returns arrays. */
+ WasmEdge_Value Params[2] = { WasmEdge_ValueGenI32(1234), WasmEdge_ValueGenI32(5678) };
+ WasmEdge_Value Returns[1];
+ /* Function name. */
+ WasmEdge_String FuncName = WasmEdge_StringCreateByCString("addTwo");
+ /* Run the WASM function from file. */
+ WasmEdge_Result Res = WasmEdge_VMRunWasmFromBuffer(
+ VMCxt, WASM, sizeof(WASM), FuncName, Params, 2, Returns, 1);
+
+ if (WasmEdge_ResultOK(Res)) {
+ printf("Get the result: %d\n", WasmEdge_ValueGetI32(Returns[0]));
+ } else {
+ printf("Error message: %s\n", WasmEdge_ResultGetMessage(Res));
+ }
+
+ /* Resources deallocations. */
+ WasmEdge_VMDelete(VMCxt);
+ WasmEdge_StringDelete(FuncName);
+ WasmEdge_ModuleInstanceDelete(HostModCxt);
+ return 0;
+ }
+ ```
+
+ Then you can compile and run: (the result of 1234 + 5678 is 6912)
+
+ ```bash
+ $ gcc test.c -lwasmedge_c
+ $ ./a.out
+ Host function "Add": 1234 + 5678
+ Get the result: 6912
+ ```
5. Host Data Example
- Developers can set a external data object to the `Function` context, and access to the object in the function body. Assume that a simple WASM from the WAT as following:
-
- ```wasm
- (module
- (type $t0 (func (param i32 i32) (result i32)))
- (import "extern" "func-add" (func $f-add (type $t0)))
- (func (export "addTwo") (param i32 i32) (result i32)
- local.get 0
- local.get 1
- call $f-add)
- )
- ```
-
- And the `test.c` as following:
-
- ```c
- #include
- #include
-
- /* Host function body definition. */
- WasmEdge_Result Add(void *Data, WasmEdge_MemoryInstanceContext *MemCxt,
- const WasmEdge_Value *In, WasmEdge_Value *Out) {
- int32_t Val1 = WasmEdge_ValueGetI32(In[0]);
- int32_t Val2 = WasmEdge_ValueGetI32(In[1]);
- printf("Host function \"Add\": %d + %d\n", Val1, Val2);
- Out[0] = WasmEdge_ValueGenI32(Val1 + Val2);
- /* Also set the result to the data. */
- int32_t *DataPtr = (int32_t *)Data;
- *DataPtr = Val1 + Val2;
- return WasmEdge_Result_Success;
- }
-
- int main() {
- /* Create the VM context. */
- WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(NULL, NULL);
-
- /* The WASM module buffer. */
- uint8_t WASM[] = {
- /* WASM header */
- 0x00, 0x61, 0x73, 0x6D, 0x01, 0x00, 0x00, 0x00,
- /* Type section */
- 0x01, 0x07, 0x01,
- /* function type {i32, i32} -> {i32} */
- 0x60, 0x02, 0x7F, 0x7F, 0x01, 0x7F,
- /* Import section */
- 0x02, 0x13, 0x01,
- /* module name: "extern" */
- 0x06, 0x65, 0x78, 0x74, 0x65, 0x72, 0x6E,
- /* extern name: "func-add" */
- 0x08, 0x66, 0x75, 0x6E, 0x63, 0x2D, 0x61, 0x64, 0x64,
- /* import desc: func 0 */
- 0x00, 0x00,
- /* Function section */
- 0x03, 0x02, 0x01, 0x00,
- /* Export section */
- 0x07, 0x0A, 0x01,
- /* export name: "addTwo" */
- 0x06, 0x61, 0x64, 0x64, 0x54, 0x77, 0x6F,
- /* export desc: func 0 */
- 0x00, 0x01,
- /* Code section */
- 0x0A, 0x0A, 0x01,
- /* code body */
- 0x08, 0x00, 0x20, 0x00, 0x20, 0x01, 0x10, 0x00, 0x0B
- };
-
- /* The external data object: an integer. */
- int32_t Data;
-
- /* Create the module instance. */
- WasmEdge_String ExportName = WasmEdge_StringCreateByCString("extern");
- WasmEdge_ModuleInstanceContext *HostModCxt = WasmEdge_ModuleInstanceCreate(ExportName);
- enum WasmEdge_ValType ParamList[2] = { WasmEdge_ValType_I32, WasmEdge_ValType_I32 };
- enum WasmEdge_ValType ReturnList[1] = { WasmEdge_ValType_I32 };
- WasmEdge_FunctionTypeContext *HostFType = WasmEdge_FunctionTypeCreate(ParamList, 2, ReturnList, 1);
- WasmEdge_FunctionInstanceContext *HostFunc = WasmEdge_FunctionInstanceCreate(HostFType, Add, &Data, 0);
- WasmEdge_FunctionTypeDelete(HostFType);
- WasmEdge_String HostFuncName = WasmEdge_StringCreateByCString("func-add");
- WasmEdge_ModuleInstanceAddFunction(HostModCxt, HostFuncName, HostFunc);
- WasmEdge_StringDelete(HostFuncName);
-
- WasmEdge_VMRegisterModuleFromImport(VMCxt, HostModCxt);
-
- /* The parameters and returns arrays. */
- WasmEdge_Value Params[2] = { WasmEdge_ValueGenI32(1234), WasmEdge_ValueGenI32(5678) };
- WasmEdge_Value Returns[1];
- /* Function name. */
- WasmEdge_String FuncName = WasmEdge_StringCreateByCString("addTwo");
- /* Run the WASM function from file. */
- WasmEdge_Result Res = WasmEdge_VMRunWasmFromBuffer(
- VMCxt, WASM, sizeof(WASM), FuncName, Params, 2, Returns, 1);
-
- if (WasmEdge_ResultOK(Res)) {
- printf("Get the result: %d\n", WasmEdge_ValueGetI32(Returns[0]));
- } else {
- printf("Error message: %s\n", WasmEdge_ResultGetMessage(Res));
- }
- printf("Data value: %d\n", Data);
-
- /* Resources deallocations. */
- WasmEdge_VMDelete(VMCxt);
- WasmEdge_StringDelete(FuncName);
- WasmEdge_ModuleInstanceDelete(HostModCxt);
- return 0;
- }
- ```
-
- Then you can compile and run: (the result of 1234 + 5678 is 6912)
-
- ```bash
- $ gcc test.c -lwasmedge_c
- $ ./a.out
- Host function "Add": 1234 + 5678
- Get the result: 6912
- Data value: 6912
- ```
+ Developers can set a external data object to the `Function` context, and access to the object in the function body. Assume that a simple WASM from the WAT as following:
+
+ ```wasm
+ (module
+ (type $t0 (func (param i32 i32) (result i32)))
+ (import "extern" "func-add" (func $f-add (type $t0)))
+ (func (export "addTwo") (param i32 i32) (result i32)
+ local.get 0
+ local.get 1
+ call $f-add)
+ )
+ ```
+
+ And the `test.c` as following:
+
+ ```c
+ #include
+ #include
+
+ /* Host function body definition. */
+ WasmEdge_Result Add(void *Data, WasmEdge_MemoryInstanceContext *MemCxt,
+ const WasmEdge_Value *In, WasmEdge_Value *Out) {
+ int32_t Val1 = WasmEdge_ValueGetI32(In[0]);
+ int32_t Val2 = WasmEdge_ValueGetI32(In[1]);
+ printf("Host function \"Add\": %d + %d\n", Val1, Val2);
+ Out[0] = WasmEdge_ValueGenI32(Val1 + Val2);
+ /* Also set the result to the data. */
+ int32_t *DataPtr = (int32_t *)Data;
+ *DataPtr = Val1 + Val2;
+ return WasmEdge_Result_Success;
+ }
+
+ int main() {
+ /* Create the VM context. */
+ WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(NULL, NULL);
+
+ /* The WASM module buffer. */
+ uint8_t WASM[] = {
+ /* WASM header */
+ 0x00, 0x61, 0x73, 0x6D, 0x01, 0x00, 0x00, 0x00,
+ /* Type section */
+ 0x01, 0x07, 0x01,
+ /* function type {i32, i32} -> {i32} */
+ 0x60, 0x02, 0x7F, 0x7F, 0x01, 0x7F,
+ /* Import section */
+ 0x02, 0x13, 0x01,
+ /* module name: "extern" */
+ 0x06, 0x65, 0x78, 0x74, 0x65, 0x72, 0x6E,
+ /* extern name: "func-add" */
+ 0x08, 0x66, 0x75, 0x6E, 0x63, 0x2D, 0x61, 0x64, 0x64,
+ /* import desc: func 0 */
+ 0x00, 0x00,
+ /* Function section */
+ 0x03, 0x02, 0x01, 0x00,
+ /* Export section */
+ 0x07, 0x0A, 0x01,
+ /* export name: "addTwo" */
+ 0x06, 0x61, 0x64, 0x64, 0x54, 0x77, 0x6F,
+ /* export desc: func 0 */
+ 0x00, 0x01,
+ /* Code section */
+ 0x0A, 0x0A, 0x01,
+ /* code body */
+ 0x08, 0x00, 0x20, 0x00, 0x20, 0x01, 0x10, 0x00, 0x0B
+ };
+
+ /* The external data object: an integer. */
+ int32_t Data;
+
+ /* Create the module instance. */
+ WasmEdge_String ExportName = WasmEdge_StringCreateByCString("extern");
+ WasmEdge_ModuleInstanceContext *HostModCxt = WasmEdge_ModuleInstanceCreate(ExportName);
+ enum WasmEdge_ValType ParamList[2] = { WasmEdge_ValType_I32, WasmEdge_ValType_I32 };
+ enum WasmEdge_ValType ReturnList[1] = { WasmEdge_ValType_I32 };
+ WasmEdge_FunctionTypeContext *HostFType = WasmEdge_FunctionTypeCreate(ParamList, 2, ReturnList, 1);
+ WasmEdge_FunctionInstanceContext *HostFunc = WasmEdge_FunctionInstanceCreate(HostFType, Add, &Data, 0);
+ WasmEdge_FunctionTypeDelete(HostFType);
+ WasmEdge_String HostFuncName = WasmEdge_StringCreateByCString("func-add");
+ WasmEdge_ModuleInstanceAddFunction(HostModCxt, HostFuncName, HostFunc);
+ WasmEdge_StringDelete(HostFuncName);
+
+ WasmEdge_VMRegisterModuleFromImport(VMCxt, HostModCxt);
+
+ /* The parameters and returns arrays. */
+ WasmEdge_Value Params[2] = { WasmEdge_ValueGenI32(1234), WasmEdge_ValueGenI32(5678) };
+ WasmEdge_Value Returns[1];
+ /* Function name. */
+ WasmEdge_String FuncName = WasmEdge_StringCreateByCString("addTwo");
+ /* Run the WASM function from file. */
+ WasmEdge_Result Res = WasmEdge_VMRunWasmFromBuffer(
+ VMCxt, WASM, sizeof(WASM), FuncName, Params, 2, Returns, 1);
+
+ if (WasmEdge_ResultOK(Res)) {
+ printf("Get the result: %d\n", WasmEdge_ValueGetI32(Returns[0]));
+ } else {
+ printf("Error message: %s\n", WasmEdge_ResultGetMessage(Res));
+ }
+ printf("Data value: %d\n", Data);
+
+ /* Resources deallocations. */
+ WasmEdge_VMDelete(VMCxt);
+ WasmEdge_StringDelete(FuncName);
+ WasmEdge_ModuleInstanceDelete(HostModCxt);
+ return 0;
+ }
+ ```
+
+ Then you can compile and run: (the result of 1234 + 5678 is 6912)
+
+ ```bash
+ $ gcc test.c -lwasmedge_c
+ $ ./a.out
+ Host function "Add": 1234 + 5678
+ Get the result: 6912
+ Data value: 6912
+ ```
## WasmEdge AOT Compiler
diff --git a/i18n/zh/docusaurus-plugin-content-docs/current/embed/c/reference/0.11.2.md b/i18n/zh/docusaurus-plugin-content-docs/current/embed/c/reference/0.11.2.md
index e5b5eab0..be0909bc 100644
--- a/i18n/zh/docusaurus-plugin-content-docs/current/embed/c/reference/0.11.2.md
+++ b/i18n/zh/docusaurus-plugin-content-docs/current/embed/c/reference/0.11.2.md
@@ -16,42 +16,42 @@ sidebar_position: 3
## Table of Contents
-- [WasmEdge Installation](#wasmedge-installation)
- - [Download And Install](#download-and-install)
- - [Compile Sources](#compile-sources)
- - [ABI Compatibility](#abi-compatibility)
-- [WasmEdge Basics](#wasmedge-basics)
- - [Version](#version)
- - [Logging Settings](#logging-settings)
- - [Value Types](#value-types)
- - [Strings](#strings)
- - [Results](#results)
- - [Contexts](#contexts)
- - [WASM data structures](#wasm-data-structures)
- - [Async](#async)
- - [Configurations](#configurations)
- - [Statistics](#statistics)
- - [Tools driver](#tools-driver)
-- [WasmEdge VM](#wasmedge-vm)
- - [WASM Execution Example With VM Context](#wasm-execution-example-with-vm-context)
- - [VM Creations](#vm-creations)
- - [Preregistrations](#preregistrations)
- - [Host Module Registrations](#host-module-registrations)
- - [WASM Registrations And Executions](#wasm-registrations-and-executions)
- - [Asynchronous execution](#asynchronous-execution)
- - [Instance Tracing](#instance-tracing)
-- [WasmEdge Runtime](#wasmedge-runtime)
- - [WASM Execution Example Step-By-Step](#wasm-execution-example-step-by-step)
- - [Loader](#loader)
- - [Validator](#validator)
- - [Executor](#executor)
- - [AST Module](#ast-module)
- - [Store](#store)
- - [Instances](#instances)
- - [Host Functions](#host-functions)
-- [WasmEdge AOT Compiler](#wasmedge-aot-compiler)
- - [Compilation Example](#compilation-example)
- - [Compiler Options](#compiler-options)
+- [WasmEdge Installation](#wasmedge-installation)
+ - [Download And Install](#download-and-install)
+ - [Compile Sources](#compile-sources)
+ - [ABI Compatibility](#abi-compatibility)
+- [WasmEdge Basics](#wasmedge-basics)
+ - [Version](#version)
+ - [Logging Settings](#logging-settings)
+ - [Value Types](#value-types)
+ - [Strings](#strings)
+ - [Results](#results)
+ - [Contexts](#contexts)
+ - [WASM data structures](#wasm-data-structures)
+ - [Async](#async)
+ - [Configurations](#configurations)
+ - [Statistics](#statistics)
+ - [Tools driver](#tools-driver)
+- [WasmEdge VM](#wasmedge-vm)
+ - [WASM Execution Example With VM Context](#wasm-execution-example-with-vm-context)
+ - [VM Creations](#vm-creations)
+ - [Preregistrations](#preregistrations)
+ - [Host Module Registrations](#host-module-registrations)
+ - [WASM Registrations And Executions](#wasm-registrations-and-executions)
+ - [Asynchronous execution](#asynchronous-execution)
+ - [Instance Tracing](#instance-tracing)
+- [WasmEdge Runtime](#wasmedge-runtime)
+ - [WASM Execution Example Step-By-Step](#wasm-execution-example-step-by-step)
+ - [Loader](#loader)
+ - [Validator](#validator)
+ - [Executor](#executor)
+ - [AST Module](#ast-module)
+ - [Store](#store)
+ - [Instances](#instances)
+ - [Host Functions](#host-functions)
+- [WasmEdge AOT Compiler](#wasmedge-aot-compiler)
+ - [Compilation Example](#compilation-example)
+ - [Compiler Options](#compiler-options)
## WasmEdge Installation
@@ -71,27 +71,27 @@ After the installation of WasmEdge, the following guide can help you to test for
1. Prepare the test C file (and assumed saved as `test.c`):
- ```c
- #include
- #include
- int main() {
- printf("WasmEdge version: %s\n", WasmEdge_VersionGet());
- return 0;
- }
- ```
+ ```c
+ #include
+ #include
+ int main() {
+ printf("WasmEdge version: %s\n", WasmEdge_VersionGet());
+ return 0;
+ }
+ ```
2. Compile the file with `gcc` or `clang`.
- ```bash
- gcc test.c -lwasmedge
- ```
+ ```bash
+ gcc test.c -lwasmedge
+ ```
3. Run and get the expected output.
- ```bash
- $ ./a.out
- WasmEdge version: 0.11.2
- ```
+ ```bash
+ $ ./a.out
+ WasmEdge version: 0.11.2
+ ```
### ABI Compatibility
@@ -133,54 +133,54 @@ In WasmEdge, developers should convert the values to `WasmEdge_Value` objects th
1. Number types: `i32`, `i64`, `f32`, `f64`, and `v128` for the `SIMD` proposal
- ```c
- WasmEdge_Value Val;
- Val = WasmEdge_ValueGenI32(123456);
- printf("%d\n", WasmEdge_ValueGetI32(Val));
- /* Will print "123456" */
- Val = WasmEdge_ValueGenI64(1234567890123LL);
- printf("%ld\n", WasmEdge_ValueGetI64(Val));
- /* Will print "1234567890123" */
- Val = WasmEdge_ValueGenF32(123.456f);
- printf("%f\n", WasmEdge_ValueGetF32(Val));
- /* Will print "123.456001" */
- Val = WasmEdge_ValueGenF64(123456.123456789);
- printf("%.10f\n", WasmEdge_ValueGetF64(Val));
- /* Will print "123456.1234567890" */
- ```
+ ```c
+ WasmEdge_Value Val;
+ Val = WasmEdge_ValueGenI32(123456);
+ printf("%d\n", WasmEdge_ValueGetI32(Val));
+ /* Will print "123456" */
+ Val = WasmEdge_ValueGenI64(1234567890123LL);
+ printf("%ld\n", WasmEdge_ValueGetI64(Val));
+ /* Will print "1234567890123" */
+ Val = WasmEdge_ValueGenF32(123.456f);
+ printf("%f\n", WasmEdge_ValueGetF32(Val));
+ /* Will print "123.456001" */
+ Val = WasmEdge_ValueGenF64(123456.123456789);
+ printf("%.10f\n", WasmEdge_ValueGetF64(Val));
+ /* Will print "123456.1234567890" */
+ ```
2. Reference types: `funcref` and `externref` for the `Reference-Types` proposal
- ```c
- WasmEdge_Value Val;
- void *Ptr;
- bool IsNull;
- uint32_t Num = 10;
- /* Generate a externref to NULL. */
- Val = WasmEdge_ValueGenNullRef(WasmEdge_RefType_ExternRef);
- IsNull = WasmEdge_ValueIsNullRef(Val);
- /* The `IsNull` will be `TRUE`. */
- Ptr = WasmEdge_ValueGetExternRef(Val);
- /* The `Ptr` will be `NULL`. */
-
- /* Get the function instance by creation or from module instance. */
- const WasmEdge_FunctionInstanceContext *FuncCxt = ...;
- /* Generate a funcref with the given function instance context. */
- Val = WasmEdge_ValueGenFuncRef(FuncCxt);
- const WasmEdge_FunctionInstanceContext *GotFuncCxt =
- WasmEdge_ValueGetFuncRef(Val);
- /* The `GotFuncCxt` will be the same as `FuncCxt`. */
-
- /* Generate a externref to `Num`. */
- Val = WasmEdge_ValueGenExternRef(&Num);
- Ptr = WasmEdge_ValueGetExternRef(Val);
- /* The `Ptr` will be `&Num`. */
- printf("%u\n", *(uint32_t *)Ptr);
- /* Will print "10" */
- Num += 55;
- printf("%u\n", *(uint32_t *)Ptr);
- /* Will print "65" */
- ```
+ ```c
+ WasmEdge_Value Val;
+ void *Ptr;
+ bool IsNull;
+ uint32_t Num = 10;
+ /* Generate a externref to NULL. */
+ Val = WasmEdge_ValueGenNullRef(WasmEdge_RefType_ExternRef);
+ IsNull = WasmEdge_ValueIsNullRef(Val);
+ /* The `IsNull` will be `TRUE`. */
+ Ptr = WasmEdge_ValueGetExternRef(Val);
+ /* The `Ptr` will be `NULL`. */
+
+ /* Get the function instance by creation or from module instance. */
+ const WasmEdge_FunctionInstanceContext *FuncCxt = ...;
+ /* Generate a funcref with the given function instance context. */
+ Val = WasmEdge_ValueGenFuncRef(FuncCxt);
+ const WasmEdge_FunctionInstanceContext *GotFuncCxt =
+ WasmEdge_ValueGetFuncRef(Val);
+ /* The `GotFuncCxt` will be the same as `FuncCxt`. */
+
+ /* Generate a externref to `Num`. */
+ Val = WasmEdge_ValueGenExternRef(&Num);
+ Ptr = WasmEdge_ValueGetExternRef(Val);
+ /* The `Ptr` will be `&Num`. */
+ printf("%u\n", *(uint32_t *)Ptr);
+ /* Will print "10" */
+ Num += 55;
+ printf("%u\n", *(uint32_t *)Ptr);
+ /* Will print "65" */
+ ```
### Strings
@@ -188,50 +188,50 @@ The `WasmEdge_String` object is for the instance names when invoking a WASM func
1. Create a `WasmEdge_String` from a C string (`const char *` with NULL termination) or a buffer with length.
- The content of the C string or buffer will be copied into the `WasmEdge_String` object.
+ The content of the C string or buffer will be copied into the `WasmEdge_String` object.
- ```c
- char Buf[4] = {50, 55, 60, 65};
- WasmEdge_String Str1 = WasmEdge_StringCreateByCString("test");
- WasmEdge_String Str2 = WasmEdge_StringCreateByBuffer(Buf, 4);
- /* The objects should be deleted by `WasmEdge_StringDelete()`. */
- WasmEdge_StringDelete(Str1);
- WasmEdge_StringDelete(Str2);
- ```
+ ```c
+ char Buf[4] = {50, 55, 60, 65};
+ WasmEdge_String Str1 = WasmEdge_StringCreateByCString("test");
+ WasmEdge_String Str2 = WasmEdge_StringCreateByBuffer(Buf, 4);
+ /* The objects should be deleted by `WasmEdge_StringDelete()`. */
+ WasmEdge_StringDelete(Str1);
+ WasmEdge_StringDelete(Str2);
+ ```
2. Wrap a `WasmEdge_String` to a buffer with length.
- The content will not be copied, and the caller should guarantee the life cycle of the input buffer.
+ The content will not be copied, and the caller should guarantee the life cycle of the input buffer.
- ```c
- const char CStr[] = "test";
- WasmEdge_String Str = WasmEdge_StringWrap(CStr, 4);
- /* The object should __NOT__ be deleted by `WasmEdge_StringDelete()`. */
- ```
+ ```c
+ const char CStr[] = "test";
+ WasmEdge_String Str = WasmEdge_StringWrap(CStr, 4);
+ /* The object should __NOT__ be deleted by `WasmEdge_StringDelete()`. */
+ ```
3. String comparison
- ```c
- const char CStr[] = "abcd";
- char Buf[4] = {0x61, 0x62, 0x63, 0x64};
- WasmEdge_String Str1 = WasmEdge_StringWrap(CStr, 4);
- WasmEdge_String Str2 = WasmEdge_StringCreateByBuffer(Buf, 4);
- bool IsEq = WasmEdge_StringIsEqual(Str1, Str2);
- /* The `IsEq` will be `TRUE`. */
- WasmEdge_StringDelete(Str2);
- ```
+ ```c
+ const char CStr[] = "abcd";
+ char Buf[4] = {0x61, 0x62, 0x63, 0x64};
+ WasmEdge_String Str1 = WasmEdge_StringWrap(CStr, 4);
+ WasmEdge_String Str2 = WasmEdge_StringCreateByBuffer(Buf, 4);
+ bool IsEq = WasmEdge_StringIsEqual(Str1, Str2);
+ /* The `IsEq` will be `TRUE`. */
+ WasmEdge_StringDelete(Str2);
+ ```
4. Convert to C string
- ```c
- char Buf[256];
- WasmEdge_String Str =
- WasmEdge_StringCreateByCString("test_wasmedge_string");
- uint32_t StrLength = WasmEdge_StringCopy(Str, Buf, sizeof(Buf));
- /* StrLength will be 20 */
- printf("String: %s\n", Buf);
- /* Will print "test_wasmedge_string". */
- ```
+ ```c
+ char Buf[256];
+ WasmEdge_String Str =
+ WasmEdge_StringCreateByCString("test_wasmedge_string");
+ uint32_t StrLength = WasmEdge_StringCopy(Str, Buf, sizeof(Buf));
+ /* StrLength will be 20 */
+ printf("String: %s\n", Buf);
+ /* Will print "test_wasmedge_string". */
+ ```
### Results
@@ -275,204 +275,204 @@ The WASM data structures are used for creating instances or can be queried from
1. Limit
- The `WasmEdge_Limit` struct is defined in the header:
+ The `WasmEdge_Limit` struct is defined in the header:
- ```c
- /// Struct of WASM limit.
- typedef struct WasmEdge_Limit {
- /// Boolean to describe has max value or not.
- bool HasMax;
- /// Boolean to describe is shared memory or not.
- bool Shared;
- /// Minimum value.
- uint32_t Min;
- /// Maximum value. Will be ignored if the `HasMax` is false.
- uint32_t Max;
- } WasmEdge_Limit;
- ```
+ ```c
+ /// Struct of WASM limit.
+ typedef struct WasmEdge_Limit {
+ /// Boolean to describe has max value or not.
+ bool HasMax;
+ /// Boolean to describe is shared memory or not.
+ bool Shared;
+ /// Minimum value.
+ uint32_t Min;
+ /// Maximum value. Will be ignored if the `HasMax` is false.
+ uint32_t Max;
+ } WasmEdge_Limit;
+ ```
- Developers can initialize the struct by assigning it's value, and the `Max` value is needed to be larger or equal to the `Min` value. The API `WasmEdge_LimitIsEqual()` is provided to compare with 2 `WasmEdge_Limit` structs.
+ Developers can initialize the struct by assigning it's value, and the `Max` value is needed to be larger or equal to the `Min` value. The API `WasmEdge_LimitIsEqual()` is provided to compare with 2 `WasmEdge_Limit` structs.
2. Function type context
- The `Function Type` context is used for the `Function` creation, checking the value types of a `Function` instance, or getting the function type with function name from VM. Developers can use the `Function Type` context APIs to get the parameter or return value types information.
-
- ```c
- enum WasmEdge_ValType ParamList[2] = {WasmEdge_ValType_I32,
- WasmEdge_ValType_I64};
- enum WasmEdge_ValType ReturnList[1] = {WasmEdge_ValType_FuncRef};
- WasmEdge_FunctionTypeContext *FuncTypeCxt =
- WasmEdge_FunctionTypeCreate(ParamList, 2, ReturnList, 1);
-
- enum WasmEdge_ValType Buf[16];
- uint32_t ParamLen = WasmEdge_FunctionTypeGetParametersLength(FuncTypeCxt);
- /* `ParamLen` will be 2. */
- uint32_t GotParamLen =
- WasmEdge_FunctionTypeGetParameters(FuncTypeCxt, Buf, 16);
- /* `GotParamLen` will be 2, and `Buf[0]` and `Buf[1]` will be the same as
- * `ParamList`. */
- uint32_t ReturnLen = WasmEdge_FunctionTypeGetReturnsLength(FuncTypeCxt);
- /* `ReturnLen` will be 1. */
- uint32_t GotReturnLen =
- WasmEdge_FunctionTypeGetReturns(FuncTypeCxt, Buf, 16);
- /* `GotReturnLen` will be 1, and `Buf[0]` will be the same as `ReturnList`.
- */
-
- WasmEdge_FunctionTypeDelete(FuncTypeCxt);
- ```
+ The `Function Type` context is used for the `Function` creation, checking the value types of a `Function` instance, or getting the function type with function name from VM. Developers can use the `Function Type` context APIs to get the parameter or return value types information.
+
+ ```c
+ enum WasmEdge_ValType ParamList[2] = {WasmEdge_ValType_I32,
+ WasmEdge_ValType_I64};
+ enum WasmEdge_ValType ReturnList[1] = {WasmEdge_ValType_FuncRef};
+ WasmEdge_FunctionTypeContext *FuncTypeCxt =
+ WasmEdge_FunctionTypeCreate(ParamList, 2, ReturnList, 1);
+
+ enum WasmEdge_ValType Buf[16];
+ uint32_t ParamLen = WasmEdge_FunctionTypeGetParametersLength(FuncTypeCxt);
+ /* `ParamLen` will be 2. */
+ uint32_t GotParamLen =
+ WasmEdge_FunctionTypeGetParameters(FuncTypeCxt, Buf, 16);
+ /* `GotParamLen` will be 2, and `Buf[0]` and `Buf[1]` will be the same as
+ * `ParamList`. */
+ uint32_t ReturnLen = WasmEdge_FunctionTypeGetReturnsLength(FuncTypeCxt);
+ /* `ReturnLen` will be 1. */
+ uint32_t GotReturnLen =
+ WasmEdge_FunctionTypeGetReturns(FuncTypeCxt, Buf, 16);
+ /* `GotReturnLen` will be 1, and `Buf[0]` will be the same as `ReturnList`.
+ */
+
+ WasmEdge_FunctionTypeDelete(FuncTypeCxt);
+ ```
3. Table type context
- The `Table Type` context is used for `Table` instance creation or getting information from `Table` instances.
+ The `Table Type` context is used for `Table` instance creation or getting information from `Table` instances.
- ```c
- WasmEdge_Limit TabLim = {
- .HasMax = true, .Shared = false, .Min = 10, .Max = 20};
- WasmEdge_TableTypeContext *TabTypeCxt =
- WasmEdge_TableTypeCreate(WasmEdge_RefType_ExternRef, TabLim);
+ ```c
+ WasmEdge_Limit TabLim = {
+ .HasMax = true, .Shared = false, .Min = 10, .Max = 20};
+ WasmEdge_TableTypeContext *TabTypeCxt =
+ WasmEdge_TableTypeCreate(WasmEdge_RefType_ExternRef, TabLim);
- enum WasmEdge_RefType GotRefType = WasmEdge_TableTypeGetRefType(TabTypeCxt);
- /* `GotRefType` will be WasmEdge_RefType_ExternRef. */
- WasmEdge_Limit GotTabLim = WasmEdge_TableTypeGetLimit(TabTypeCxt);
- /* `GotTabLim` will be the same value as `TabLim`. */
+ enum WasmEdge_RefType GotRefType = WasmEdge_TableTypeGetRefType(TabTypeCxt);
+ /* `GotRefType` will be WasmEdge_RefType_ExternRef. */
+ WasmEdge_Limit GotTabLim = WasmEdge_TableTypeGetLimit(TabTypeCxt);
+ /* `GotTabLim` will be the same value as `TabLim`. */
- WasmEdge_TableTypeDelete(TabTypeCxt);
- ```
+ WasmEdge_TableTypeDelete(TabTypeCxt);
+ ```
4. Memory type context
- The `Memory Type` context is used for `Memory` instance creation or getting information from `Memory` instances.
+ The `Memory Type` context is used for `Memory` instance creation or getting information from `Memory` instances.
- ```c
- WasmEdge_Limit MemLim = {
- .HasMax = true, .Shared = false, .Min = 10, .Max = 20};
- WasmEdge_MemoryTypeContext *MemTypeCxt = WasmEdge_MemoryTypeCreate(MemLim);
+ ```c
+ WasmEdge_Limit MemLim = {
+ .HasMax = true, .Shared = false, .Min = 10, .Max = 20};
+ WasmEdge_MemoryTypeContext *MemTypeCxt = WasmEdge_MemoryTypeCreate(MemLim);
- WasmEdge_Limit GotMemLim = WasmEdge_MemoryTypeGetLimit(MemTypeCxt);
- /* `GotMemLim` will be the same value as `MemLim`. */
+ WasmEdge_Limit GotMemLim = WasmEdge_MemoryTypeGetLimit(MemTypeCxt);
+ /* `GotMemLim` will be the same value as `MemLim`. */
- WasmEdge_MemoryTypeDelete(MemTypeCxt)
- ```
+ WasmEdge_MemoryTypeDelete(MemTypeCxt)
+ ```
5. Global type context
- The `Global Type` context is used for `Global` instance creation or getting information from `Global` instances.
+ The `Global Type` context is used for `Global` instance creation or getting information from `Global` instances.
- ```c
- WasmEdge_GlobalTypeContext *GlobTypeCxt = WasmEdge_GlobalTypeCreate(
- WasmEdge_ValType_F64, WasmEdge_Mutability_Var);
+ ```c
+ WasmEdge_GlobalTypeContext *GlobTypeCxt = WasmEdge_GlobalTypeCreate(
+ WasmEdge_ValType_F64, WasmEdge_Mutability_Var);
- WasmEdge_ValType GotValType = WasmEdge_GlobalTypeGetValType(GlobTypeCxt);
- /* `GotValType` will be WasmEdge_ValType_F64. */
- WasmEdge_Mutability GotValMut =
- WasmEdge_GlobalTypeGetMutability(GlobTypeCxt);
- /* `GotValMut` will be WasmEdge_Mutability_Var. */
+ WasmEdge_ValType GotValType = WasmEdge_GlobalTypeGetValType(GlobTypeCxt);
+ /* `GotValType` will be WasmEdge_ValType_F64. */
+ WasmEdge_Mutability GotValMut =
+ WasmEdge_GlobalTypeGetMutability(GlobTypeCxt);
+ /* `GotValMut` will be WasmEdge_Mutability_Var. */
- WasmEdge_GlobalTypeDelete(GlobTypeCxt);
- ```
+ WasmEdge_GlobalTypeDelete(GlobTypeCxt);
+ ```
6. Import type context
- The `Import Type` context is used for getting the imports information from a [AST Module](#ast-module). Developers can get the external type (`function`, `table`, `memory`, or `global`), import module name, and external name from an `Import Type` context. The details about querying `Import Type` contexts will be introduced in the [AST Module](#ast-module).
-
- ```c
- WasmEdge_ASTModuleContext *ASTCxt = ...;
- /*
- * Assume that `ASTCxt` is returned by the `WasmEdge_LoaderContext` for the
- * result of loading a WASM file.
- */
- const WasmEdge_ImportTypeContext *ImpType = ...;
- /* Assume that `ImpType` is queried from the `ASTCxt` for the import. */
-
- enum WasmEdge_ExternalType ExtType =
- WasmEdge_ImportTypeGetExternalType(ImpType);
- /*
- * The `ExtType` can be one of `WasmEdge_ExternalType_Function`,
- * `WasmEdge_ExternalType_Table`, `WasmEdge_ExternalType_Memory`, or
- * `WasmEdge_ExternalType_Global`.
- */
- WasmEdge_String ModName = WasmEdge_ImportTypeGetModuleName(ImpType);
- WasmEdge_String ExtName = WasmEdge_ImportTypeGetExternalName(ImpType);
- /*
- * The `ModName` and `ExtName` should not be destroyed and the string
- * buffers are binded into the `ASTCxt`.
- */
- const WasmEdge_FunctionTypeContext *FuncTypeCxt =
- WasmEdge_ImportTypeGetFunctionType(ASTCxt, ImpType);
- /*
- * If the `ExtType` is not `WasmEdge_ExternalType_Function`, the
- * `FuncTypeCxt` will be NULL.
- */
- const WasmEdge_TableTypeContext *TabTypeCxt =
- WasmEdge_ImportTypeGetTableType(ASTCxt, ImpType);
- /*
- * If the `ExtType` is not `WasmEdge_ExternalType_Table`, the `TabTypeCxt`
- * will be NULL.
- */
- const WasmEdge_MemoryTypeContext *MemTypeCxt =
- WasmEdge_ImportTypeGetMemoryType(ASTCxt, ImpType);
- /*
- * If the `ExtType` is not `WasmEdge_ExternalType_Memory`, the `MemTypeCxt`
- * will be NULL.
- */
- const WasmEdge_GlobalTypeContext *GlobTypeCxt =
- WasmEdge_ImportTypeGetGlobalType(ASTCxt, ImpType);
- /*
- * If the `ExtType` is not `WasmEdge_ExternalType_Global`, the `GlobTypeCxt`
- * will be NULL.
- */
- ```
+ The `Import Type` context is used for getting the imports information from a [AST Module](#ast-module). Developers can get the external type (`function`, `table`, `memory`, or `global`), import module name, and external name from an `Import Type` context. The details about querying `Import Type` contexts will be introduced in the [AST Module](#ast-module).
+
+ ```c
+ WasmEdge_ASTModuleContext *ASTCxt = ...;
+ /*
+ * Assume that `ASTCxt` is returned by the `WasmEdge_LoaderContext` for the
+ * result of loading a WASM file.
+ */
+ const WasmEdge_ImportTypeContext *ImpType = ...;
+ /* Assume that `ImpType` is queried from the `ASTCxt` for the import. */
+
+ enum WasmEdge_ExternalType ExtType =
+ WasmEdge_ImportTypeGetExternalType(ImpType);
+ /*
+ * The `ExtType` can be one of `WasmEdge_ExternalType_Function`,
+ * `WasmEdge_ExternalType_Table`, `WasmEdge_ExternalType_Memory`, or
+ * `WasmEdge_ExternalType_Global`.
+ */
+ WasmEdge_String ModName = WasmEdge_ImportTypeGetModuleName(ImpType);
+ WasmEdge_String ExtName = WasmEdge_ImportTypeGetExternalName(ImpType);
+ /*
+ * The `ModName` and `ExtName` should not be destroyed and the string
+ * buffers are binded into the `ASTCxt`.
+ */
+ const WasmEdge_FunctionTypeContext *FuncTypeCxt =
+ WasmEdge_ImportTypeGetFunctionType(ASTCxt, ImpType);
+ /*
+ * If the `ExtType` is not `WasmEdge_ExternalType_Function`, the
+ * `FuncTypeCxt` will be NULL.
+ */
+ const WasmEdge_TableTypeContext *TabTypeCxt =
+ WasmEdge_ImportTypeGetTableType(ASTCxt, ImpType);
+ /*
+ * If the `ExtType` is not `WasmEdge_ExternalType_Table`, the `TabTypeCxt`
+ * will be NULL.
+ */
+ const WasmEdge_MemoryTypeContext *MemTypeCxt =
+ WasmEdge_ImportTypeGetMemoryType(ASTCxt, ImpType);
+ /*
+ * If the `ExtType` is not `WasmEdge_ExternalType_Memory`, the `MemTypeCxt`
+ * will be NULL.
+ */
+ const WasmEdge_GlobalTypeContext *GlobTypeCxt =
+ WasmEdge_ImportTypeGetGlobalType(ASTCxt, ImpType);
+ /*
+ * If the `ExtType` is not `WasmEdge_ExternalType_Global`, the `GlobTypeCxt`
+ * will be NULL.
+ */
+ ```
7. Export type context
- The `Export Type` context is used for getting the exports information from a [AST Module](#ast-module). Developers can get the external type (`function`, `table`, `memory`, or `global`) and external name from an `Export Type` context. The details about querying `Export Type` contexts will be introduced in the [AST Module](#ast-module).
-
- ```c
- WasmEdge_ASTModuleContext *ASTCxt = ...;
- /*
- * Assume that `ASTCxt` is returned by the `WasmEdge_LoaderContext` for the
- * result of loading a WASM file.
- */
- const WasmEdge_ExportTypeContext *ExpType = ...;
- /* Assume that `ExpType` is queried from the `ASTCxt` for the export. */
-
- enum WasmEdge_ExternalType ExtType =
- WasmEdge_ExportTypeGetExternalType(ExpType);
- /*
- * The `ExtType` can be one of `WasmEdge_ExternalType_Function`,
- * `WasmEdge_ExternalType_Table`, `WasmEdge_ExternalType_Memory`, or
- * `WasmEdge_ExternalType_Global`.
- */
- WasmEdge_String ExtName = WasmEdge_ExportTypeGetExternalName(ExpType);
- /*
- * The `ExtName` should not be destroyed and the string buffer is binded
- * into the `ASTCxt`.
- */
- const WasmEdge_FunctionTypeContext *FuncTypeCxt =
- WasmEdge_ExportTypeGetFunctionType(ASTCxt, ExpType);
- /*
- * If the `ExtType` is not `WasmEdge_ExternalType_Function`, the
- * `FuncTypeCxt` will be NULL.
- */
- const WasmEdge_TableTypeContext *TabTypeCxt =
- WasmEdge_ExportTypeGetTableType(ASTCxt, ExpType);
- /*
- * If the `ExtType` is not `WasmEdge_ExternalType_Table`, the `TabTypeCxt`
- * will be NULL.
- */
- const WasmEdge_MemoryTypeContext *MemTypeCxt =
- WasmEdge_ExportTypeGetMemoryType(ASTCxt, ExpType);
- /*
- * If the `ExtType` is not `WasmEdge_ExternalType_Memory`, the `MemTypeCxt`
- * will be NULL.
- */
- const WasmEdge_GlobalTypeContext *GlobTypeCxt =
- WasmEdge_ExportTypeGetGlobalType(ASTCxt, ExpType);
- /*
- * If the `ExtType` is not `WasmEdge_ExternalType_Global`, the `GlobTypeCxt`
- * will be NULL.
- */
- ```
+ The `Export Type` context is used for getting the exports information from a [AST Module](#ast-module). Developers can get the external type (`function`, `table`, `memory`, or `global`) and external name from an `Export Type` context. The details about querying `Export Type` contexts will be introduced in the [AST Module](#ast-module).
+
+ ```c
+ WasmEdge_ASTModuleContext *ASTCxt = ...;
+ /*
+ * Assume that `ASTCxt` is returned by the `WasmEdge_LoaderContext` for the
+ * result of loading a WASM file.
+ */
+ const WasmEdge_ExportTypeContext *ExpType = ...;
+ /* Assume that `ExpType` is queried from the `ASTCxt` for the export. */
+
+ enum WasmEdge_ExternalType ExtType =
+ WasmEdge_ExportTypeGetExternalType(ExpType);
+ /*
+ * The `ExtType` can be one of `WasmEdge_ExternalType_Function`,
+ * `WasmEdge_ExternalType_Table`, `WasmEdge_ExternalType_Memory`, or
+ * `WasmEdge_ExternalType_Global`.
+ */
+ WasmEdge_String ExtName = WasmEdge_ExportTypeGetExternalName(ExpType);
+ /*
+ * The `ExtName` should not be destroyed and the string buffer is binded
+ * into the `ASTCxt`.
+ */
+ const WasmEdge_FunctionTypeContext *FuncTypeCxt =
+ WasmEdge_ExportTypeGetFunctionType(ASTCxt, ExpType);
+ /*
+ * If the `ExtType` is not `WasmEdge_ExternalType_Function`, the
+ * `FuncTypeCxt` will be NULL.
+ */
+ const WasmEdge_TableTypeContext *TabTypeCxt =
+ WasmEdge_ExportTypeGetTableType(ASTCxt, ExpType);
+ /*
+ * If the `ExtType` is not `WasmEdge_ExternalType_Table`, the `TabTypeCxt`
+ * will be NULL.
+ */
+ const WasmEdge_MemoryTypeContext *MemTypeCxt =
+ WasmEdge_ExportTypeGetMemoryType(ASTCxt, ExpType);
+ /*
+ * If the `ExtType` is not `WasmEdge_ExternalType_Memory`, the `MemTypeCxt`
+ * will be NULL.
+ */
+ const WasmEdge_GlobalTypeContext *GlobTypeCxt =
+ WasmEdge_ExportTypeGetGlobalType(ASTCxt, ExpType);
+ /*
+ * If the `ExtType` is not `WasmEdge_ExternalType_Global`, the `GlobTypeCxt`
+ * will be NULL.
+ */
+ ```
### Async
@@ -480,60 +480,60 @@ After calling the [asynchronous execution APIs](#asynchronous-execution), develo
1. Wait for the asynchronous execution
- Developers can wait the execution until finished:
-
- ```c
- WasmEdge_Async *Async = ...; /* Ignored. Asynchronous execute a function. */
- /* Blocking and waiting for the execution. */
- WasmEdge_AsyncWait(Async);
- WasmEdge_AsyncDelete(Async);
- ```
-
- Or developers can wait for a time limit. If the time limit exceeded, developers can choose to cancel the execution. For the interruptible execution in AOT mode, developers should set `TRUE` through the `WasmEdge_ConfigureCompilerSetInterruptible()` API into the configure context for the AOT compiler.
-
- ```c
- WasmEdge_Async *Async = ...; /* Ignored. Asynchronous execute a function. */
- /* Blocking and waiting for the execution for 1 second. */
- bool IsEnd = WasmEdge_AsyncWaitFor(Async, 1000);
- if (IsEnd) {
- /* The execution finished. Developers can get the result. */
- WasmEdge_Result Res = WasmEdge_AsyncGet(/* ... Ignored */);
- } else {
- /*
- * The time limit exceeded. Developers can keep waiting or cancel the
- * execution.
- */
- WasmEdge_AsyncCancel(Async);
- WasmEdge_Result Res = WasmEdge_AsyncGet(Async, 0, NULL);
- /* The result error code will be `WasmEdge_ErrCode_Interrupted`. */
- }
- WasmEdge_AsyncDelete(Async);
- ```
+ Developers can wait the execution until finished:
+
+ ```c
+ WasmEdge_Async *Async = ...; /* Ignored. Asynchronous execute a function. */
+ /* Blocking and waiting for the execution. */
+ WasmEdge_AsyncWait(Async);
+ WasmEdge_AsyncDelete(Async);
+ ```
+
+ Or developers can wait for a time limit. If the time limit exceeded, developers can choose to cancel the execution. For the interruptible execution in AOT mode, developers should set `TRUE` through the `WasmEdge_ConfigureCompilerSetInterruptible()` API into the configure context for the AOT compiler.
+
+ ```c
+ WasmEdge_Async *Async = ...; /* Ignored. Asynchronous execute a function. */
+ /* Blocking and waiting for the execution for 1 second. */
+ bool IsEnd = WasmEdge_AsyncWaitFor(Async, 1000);
+ if (IsEnd) {
+ /* The execution finished. Developers can get the result. */
+ WasmEdge_Result Res = WasmEdge_AsyncGet(/* ... Ignored */);
+ } else {
+ /*
+ * The time limit exceeded. Developers can keep waiting or cancel the
+ * execution.
+ */
+ WasmEdge_AsyncCancel(Async);
+ WasmEdge_Result Res = WasmEdge_AsyncGet(Async, 0, NULL);
+ /* The result error code will be `WasmEdge_ErrCode_Interrupted`. */
+ }
+ WasmEdge_AsyncDelete(Async);
+ ```
2. Get the execution result of the asynchronous execution
- Developers can use the `WasmEdge_AsyncGetReturnsLength()` API to get the return value list length. This function will block and wait for the execution. If the execution has finished, this function will return the length immediately. If the execution failed, this function will return `0`. This function can help the developers to create the buffer to get the return values. If developers have already known the buffer length, they can skip this function and use the `WasmEdge_AsyncGet()` API to get the result.
-
- ```c
- WasmEdge_Async *Async = ...; /* Ignored. Asynchronous execute a function. */
- /*
- * Blocking and waiting for the execution and get the return value list
- * length.
- */
- uint32_t Arity = WasmEdge_AsyncGetReturnsLength(Async);
- WasmEdge_AsyncDelete(Async);
- ```
-
- The `WasmEdge_AsyncGet()` API will block and wait for the execution. If the execution has finished, this function will fill the return values into the buffer and return the execution result immediately.
-
- ```c
- WasmEdge_Async *Async = ...; /* Ignored. Asynchronous execute a function. */
- /* Blocking and waiting for the execution and get the return values. */
- const uint32_t BUF_LEN = 256;
- WasmEdge_Value Buf[BUF_LEN];
- WasmEdge_Result Res = WasmEdge_AsyncGet(Async, Buf, BUF_LEN);
- WasmEdge_AsyncDelete(Async);
- ```
+ Developers can use the `WasmEdge_AsyncGetReturnsLength()` API to get the return value list length. This function will block and wait for the execution. If the execution has finished, this function will return the length immediately. If the execution failed, this function will return `0`. This function can help the developers to create the buffer to get the return values. If developers have already known the buffer length, they can skip this function and use the `WasmEdge_AsyncGet()` API to get the result.
+
+ ```c
+ WasmEdge_Async *Async = ...; /* Ignored. Asynchronous execute a function. */
+ /*
+ * Blocking and waiting for the execution and get the return value list
+ * length.
+ */
+ uint32_t Arity = WasmEdge_AsyncGetReturnsLength(Async);
+ WasmEdge_AsyncDelete(Async);
+ ```
+
+ The `WasmEdge_AsyncGet()` API will block and wait for the execution. If the execution has finished, this function will fill the return values into the buffer and return the execution result immediately.
+
+ ```c
+ WasmEdge_Async *Async = ...; /* Ignored. Asynchronous execute a function. */
+ /* Blocking and waiting for the execution and get the return values. */
+ const uint32_t BUF_LEN = 256;
+ WasmEdge_Value Buf[BUF_LEN];
+ WasmEdge_Result Res = WasmEdge_AsyncGet(Async, Buf, BUF_LEN);
+ WasmEdge_AsyncDelete(Async);
+ ```
### Configurations
@@ -541,189 +541,189 @@ The configuration context, `WasmEdge_ConfigureContext`, manages the configuratio
1. Proposals
- WasmEdge supports turning on or off the WebAssembly proposals. This configuration is effective in any contexts created with the `Configure` context.
-
- ```c
- enum WasmEdge_Proposal {
- WasmEdge_Proposal_ImportExportMutGlobals = 0,
- WasmEdge_Proposal_NonTrapFloatToIntConversions,
- WasmEdge_Proposal_SignExtensionOperators,
- WasmEdge_Proposal_MultiValue,
- WasmEdge_Proposal_BulkMemoryOperations,
- WasmEdge_Proposal_ReferenceTypes,
- WasmEdge_Proposal_SIMD,
- WasmEdge_Proposal_TailCall,
- WasmEdge_Proposal_MultiMemories,
- WasmEdge_Proposal_Annotations,
- WasmEdge_Proposal_Memory64,
- WasmEdge_Proposal_ExceptionHandling,
- WasmEdge_Proposal_ExtendedConst,
- WasmEdge_Proposal_Threads,
- WasmEdge_Proposal_FunctionReferences
- };
- ```
-
- Developers can add or remove the proposals into the `Configure` context.
-
- ```c
- /*
- * By default, the following proposals have turned on initially:
- * * Import/Export of mutable globals
- * * Non-trapping float-to-int conversions
- * * Sign-extension operators
- * * Multi-value returns
- * * Bulk memory operations
- * * Reference types
- * * Fixed-width SIMD
- *
- * For the current WasmEdge version, the following proposals are supported
- * (turned off by default) additionally:
- * * Tail-call
- * * Multiple memories
- * * Extended-const
- * * Threads
- */
- WasmEdge_ConfigureContext *ConfCxt = WasmEdge_ConfigureCreate();
- WasmEdge_ConfigureAddProposal(ConfCxt, WasmEdge_Proposal_MultiMemories);
- WasmEdge_ConfigureRemoveProposal(ConfCxt, WasmEdge_Proposal_ReferenceTypes);
- bool IsBulkMem = WasmEdge_ConfigureHasProposal(
- ConfCxt, WasmEdge_Proposal_BulkMemoryOperations);
- /* The `IsBulkMem` will be `TRUE`. */
- WasmEdge_ConfigureDelete(ConfCxt);
- ```
+ WasmEdge supports turning on or off the WebAssembly proposals. This configuration is effective in any contexts created with the `Configure` context.
+
+ ```c
+ enum WasmEdge_Proposal {
+ WasmEdge_Proposal_ImportExportMutGlobals = 0,
+ WasmEdge_Proposal_NonTrapFloatToIntConversions,
+ WasmEdge_Proposal_SignExtensionOperators,
+ WasmEdge_Proposal_MultiValue,
+ WasmEdge_Proposal_BulkMemoryOperations,
+ WasmEdge_Proposal_ReferenceTypes,
+ WasmEdge_Proposal_SIMD,
+ WasmEdge_Proposal_TailCall,
+ WasmEdge_Proposal_MultiMemories,
+ WasmEdge_Proposal_Annotations,
+ WasmEdge_Proposal_Memory64,
+ WasmEdge_Proposal_ExceptionHandling,
+ WasmEdge_Proposal_ExtendedConst,
+ WasmEdge_Proposal_Threads,
+ WasmEdge_Proposal_FunctionReferences
+ };
+ ```
+
+ Developers can add or remove the proposals into the `Configure` context.
+
+ ```c
+ /*
+ * By default, the following proposals have turned on initially:
+ * * Import/Export of mutable globals
+ * * Non-trapping float-to-int conversions
+ * * Sign-extension operators
+ * * Multi-value returns
+ * * Bulk memory operations
+ * * Reference types
+ * * Fixed-width SIMD
+ *
+ * For the current WasmEdge version, the following proposals are supported
+ * (turned off by default) additionally:
+ * * Tail-call
+ * * Multiple memories
+ * * Extended-const
+ * * Threads
+ */
+ WasmEdge_ConfigureContext *ConfCxt = WasmEdge_ConfigureCreate();
+ WasmEdge_ConfigureAddProposal(ConfCxt, WasmEdge_Proposal_MultiMemories);
+ WasmEdge_ConfigureRemoveProposal(ConfCxt, WasmEdge_Proposal_ReferenceTypes);
+ bool IsBulkMem = WasmEdge_ConfigureHasProposal(
+ ConfCxt, WasmEdge_Proposal_BulkMemoryOperations);
+ /* The `IsBulkMem` will be `TRUE`. */
+ WasmEdge_ConfigureDelete(ConfCxt);
+ ```
2. Host registrations
- This configuration is used for the `VM` context to turn on the `WASI` or `wasmedge_process` supports and only effective in `VM` contexts.
-
- ```c
- enum WasmEdge_HostRegistration {
- WasmEdge_HostRegistration_Wasi = 0,
- WasmEdge_HostRegistration_WasmEdge_Process
- };
- ```
-
- The details will be introduced in the [preregistrations of VM context](#preregistrations).
-
- ```c
- WasmEdge_ConfigureContext *ConfCxt = WasmEdge_ConfigureCreate();
- bool IsHostWasi = WasmEdge_ConfigureHasHostRegistration(
- ConfCxt, WasmEdge_HostRegistration_Wasi);
- /* The `IsHostWasi` will be `FALSE`. */
- WasmEdge_ConfigureAddHostRegistration(ConfCxt,
- WasmEdge_HostRegistration_Wasi);
- IsHostWasi = WasmEdge_ConfigureHasHostRegistration(
- ConfCxt, WasmEdge_HostRegistration_Wasi);
- /* The `IsHostWasi` will be `TRUE`. */
- WasmEdge_ConfigureDelete(ConfCxt);
- ```
+ This configuration is used for the `VM` context to turn on the `WASI` or `wasmedge_process` supports and only effective in `VM` contexts.
+
+ ```c
+ enum WasmEdge_HostRegistration {
+ WasmEdge_HostRegistration_Wasi = 0,
+ WasmEdge_HostRegistration_WasmEdge_Process
+ };
+ ```
+
+ The details will be introduced in the [preregistrations of VM context](#preregistrations).
+
+ ```c
+ WasmEdge_ConfigureContext *ConfCxt = WasmEdge_ConfigureCreate();
+ bool IsHostWasi = WasmEdge_ConfigureHasHostRegistration(
+ ConfCxt, WasmEdge_HostRegistration_Wasi);
+ /* The `IsHostWasi` will be `FALSE`. */
+ WasmEdge_ConfigureAddHostRegistration(ConfCxt,
+ WasmEdge_HostRegistration_Wasi);
+ IsHostWasi = WasmEdge_ConfigureHasHostRegistration(
+ ConfCxt, WasmEdge_HostRegistration_Wasi);
+ /* The `IsHostWasi` will be `TRUE`. */
+ WasmEdge_ConfigureDelete(ConfCxt);
+ ```
3. Maximum memory pages
- Developers can limit the page size of memory instances by this configuration. When growing the page size of memory instances in WASM execution and exceeding the limited size, the page growing will fail. This configuration is only effective in the `Executor` and `VM` contexts.
-
- ```c
- WasmEdge_ConfigureContext *ConfCxt = WasmEdge_ConfigureCreate();
- uint32_t PageSize = WasmEdge_ConfigureGetMaxMemoryPage(ConfCxt);
- /* By default, the maximum memory page size is 65536. */
- WasmEdge_ConfigureSetMaxMemoryPage(ConfCxt, 1024);
- /*
- * Limit the memory size of each memory instance with not larger than 1024
- * pages (64 MiB).
- */
- PageSize = WasmEdge_ConfigureGetMaxMemoryPage(ConfCxt);
- /* The `PageSize` will be 1024. */
- WasmEdge_ConfigureDelete(ConfCxt);
- ```
+ Developers can limit the page size of memory instances by this configuration. When growing the page size of memory instances in WASM execution and exceeding the limited size, the page growing will fail. This configuration is only effective in the `Executor` and `VM` contexts.
+
+ ```c
+ WasmEdge_ConfigureContext *ConfCxt = WasmEdge_ConfigureCreate();
+ uint32_t PageSize = WasmEdge_ConfigureGetMaxMemoryPage(ConfCxt);
+ /* By default, the maximum memory page size is 65536. */
+ WasmEdge_ConfigureSetMaxMemoryPage(ConfCxt, 1024);
+ /*
+ * Limit the memory size of each memory instance with not larger than 1024
+ * pages (64 MiB).
+ */
+ PageSize = WasmEdge_ConfigureGetMaxMemoryPage(ConfCxt);
+ /* The `PageSize` will be 1024. */
+ WasmEdge_ConfigureDelete(ConfCxt);
+ ```
4. Forcibly interpreter mode (`0.11.2` or upper only)
- If developers want to execute the WASM file or the AOT compiled WASM in interpreter mode forcibly, they can turn on the configuration.
+ If developers want to execute the WASM file or the AOT compiled WASM in interpreter mode forcibly, they can turn on the configuration.
- ```c
- WasmEdge_ConfigureContext *ConfCxt = WasmEdge_ConfigureCreate();
- bool IsForceInterp = WasmEdge_ConfigureIsForceInterpreter(ConfCxt);
- /* By default, The `IsForceInterp` will be `FALSE`. */
- WasmEdge_ConfigureSetForceInterpreter(ConfCxt, TRUE);
- IsForceInterp = WasmEdge_ConfigureIsForceInterpreter(ConfCxt);
- /* The `IsForceInterp` will be `TRUE`. */
- WasmEdge_ConfigureDelete(ConfCxt);
- ```
+ ```c
+ WasmEdge_ConfigureContext *ConfCxt = WasmEdge_ConfigureCreate();
+ bool IsForceInterp = WasmEdge_ConfigureIsForceInterpreter(ConfCxt);
+ /* By default, The `IsForceInterp` will be `FALSE`. */
+ WasmEdge_ConfigureSetForceInterpreter(ConfCxt, TRUE);
+ IsForceInterp = WasmEdge_ConfigureIsForceInterpreter(ConfCxt);
+ /* The `IsForceInterp` will be `TRUE`. */
+ WasmEdge_ConfigureDelete(ConfCxt);
+ ```
5. AOT compiler options
- The AOT compiler options configure the behavior about optimization level, output format, dump IR, and generic binary.
-
- ```c
- enum WasmEdge_CompilerOptimizationLevel {
- // Disable as many optimizations as possible.
- WasmEdge_CompilerOptimizationLevel_O0 = 0,
- // Optimize quickly without destroying debuggability.
- WasmEdge_CompilerOptimizationLevel_O1,
- // Optimize for fast execution as much as possible without triggering
- // significant incremental compile time or code size growth.
- WasmEdge_CompilerOptimizationLevel_O2,
- // Optimize for fast execution as much as possible.
- WasmEdge_CompilerOptimizationLevel_O3,
- // Optimize for small code size as much as possible without triggering
- // significant incremental compile time or execution time slowdowns.
- WasmEdge_CompilerOptimizationLevel_Os,
- // Optimize for small code size as much as possible.
- WasmEdge_CompilerOptimizationLevel_Oz
- };
-
- enum WasmEdge_CompilerOutputFormat {
- // Native dynamic library format.
- WasmEdge_CompilerOutputFormat_Native = 0,
- // WebAssembly with AOT compiled codes in custom section.
- WasmEdge_CompilerOutputFormat_Wasm
- };
- ```
-
- These configurations are only effective in `Compiler` contexts.
-
- ```c
- WasmEdge_ConfigureContext *ConfCxt = WasmEdge_ConfigureCreate();
- /* By default, the optimization level is O3. */
- WasmEdge_ConfigureCompilerSetOptimizationLevel(
- ConfCxt, WasmEdge_CompilerOptimizationLevel_O2);
- /* By default, the output format is universal WASM. */
- WasmEdge_ConfigureCompilerSetOutputFormat(
- ConfCxt, WasmEdge_CompilerOutputFormat_Native);
- /* By default, the dump IR is `FALSE`. */
- WasmEdge_ConfigureCompilerSetDumpIR(ConfCxt, TRUE);
- /* By default, the generic binary is `FALSE`. */
- WasmEdge_ConfigureCompilerSetGenericBinary(ConfCxt, TRUE);
- /* By default, the interruptible is `FALSE`.
- /* Set this option to `TRUE` to support the interruptible execution in AOT
- mode. */
- WasmEdge_ConfigureCompilerSetInterruptible(ConfCxt, TRUE);
- WasmEdge_ConfigureDelete(ConfCxt);
- ```
+ The AOT compiler options configure the behavior about optimization level, output format, dump IR, and generic binary.
+
+ ```c
+ enum WasmEdge_CompilerOptimizationLevel {
+ // Disable as many optimizations as possible.
+ WasmEdge_CompilerOptimizationLevel_O0 = 0,
+ // Optimize quickly without destroying debuggability.
+ WasmEdge_CompilerOptimizationLevel_O1,
+ // Optimize for fast execution as much as possible without triggering
+ // significant incremental compile time or code size growth.
+ WasmEdge_CompilerOptimizationLevel_O2,
+ // Optimize for fast execution as much as possible.
+ WasmEdge_CompilerOptimizationLevel_O3,
+ // Optimize for small code size as much as possible without triggering
+ // significant incremental compile time or execution time slowdowns.
+ WasmEdge_CompilerOptimizationLevel_Os,
+ // Optimize for small code size as much as possible.
+ WasmEdge_CompilerOptimizationLevel_Oz
+ };
+
+ enum WasmEdge_CompilerOutputFormat {
+ // Native dynamic library format.
+ WasmEdge_CompilerOutputFormat_Native = 0,
+ // WebAssembly with AOT compiled codes in custom section.
+ WasmEdge_CompilerOutputFormat_Wasm
+ };
+ ```
+
+ These configurations are only effective in `Compiler` contexts.
+
+ ```c
+ WasmEdge_ConfigureContext *ConfCxt = WasmEdge_ConfigureCreate();
+ /* By default, the optimization level is O3. */
+ WasmEdge_ConfigureCompilerSetOptimizationLevel(
+ ConfCxt, WasmEdge_CompilerOptimizationLevel_O2);
+ /* By default, the output format is universal WASM. */
+ WasmEdge_ConfigureCompilerSetOutputFormat(
+ ConfCxt, WasmEdge_CompilerOutputFormat_Native);
+ /* By default, the dump IR is `FALSE`. */
+ WasmEdge_ConfigureCompilerSetDumpIR(ConfCxt, TRUE);
+ /* By default, the generic binary is `FALSE`. */
+ WasmEdge_ConfigureCompilerSetGenericBinary(ConfCxt, TRUE);
+ /* By default, the interruptible is `FALSE`.
+ /* Set this option to `TRUE` to support the interruptible execution in AOT
+ mode. */
+ WasmEdge_ConfigureCompilerSetInterruptible(ConfCxt, TRUE);
+ WasmEdge_ConfigureDelete(ConfCxt);
+ ```
6. Statistics options
- The statistics options configure the behavior about instruction counting, cost measuring, and time measuring in both runtime and AOT compiler. These configurations are effective in `Compiler`, `VM`, and `Executor` contexts.
-
- ```c
- WasmEdge_ConfigureContext *ConfCxt = WasmEdge_ConfigureCreate();
- /*
- * By default, the instruction counting is `FALSE` when running a
- * compiled-WASM or a pure-WASM.
- */
- WasmEdge_ConfigureStatisticsSetInstructionCounting(ConfCxt, TRUE);
- /*
- * By default, the cost measurement is `FALSE` when running a compiled-WASM
- * or a pure-WASM.
- */
- WasmEdge_ConfigureStatisticsSetCostMeasuring(ConfCxt, TRUE);
- /*
- * By default, the time measurement is `FALSE` when running a compiled-WASM
- * or a pure-WASM.
- */
- WasmEdge_ConfigureStatisticsSetTimeMeasuring(ConfCxt, TRUE);
- WasmEdge_ConfigureDelete(ConfCxt);
- ```
+ The statistics options configure the behavior about instruction counting, cost measuring, and time measuring in both runtime and AOT compiler. These configurations are effective in `Compiler`, `VM`, and `Executor` contexts.
+
+ ```c
+ WasmEdge_ConfigureContext *ConfCxt = WasmEdge_ConfigureCreate();
+ /*
+ * By default, the instruction counting is `FALSE` when running a
+ * compiled-WASM or a pure-WASM.
+ */
+ WasmEdge_ConfigureStatisticsSetInstructionCounting(ConfCxt, TRUE);
+ /*
+ * By default, the cost measurement is `FALSE` when running a compiled-WASM
+ * or a pure-WASM.
+ */
+ WasmEdge_ConfigureStatisticsSetCostMeasuring(ConfCxt, TRUE);
+ /*
+ * By default, the time measurement is `FALSE` when running a compiled-WASM
+ * or a pure-WASM.
+ */
+ WasmEdge_ConfigureStatisticsSetTimeMeasuring(ConfCxt, TRUE);
+ WasmEdge_ConfigureDelete(ConfCxt);
+ ```
### Statistics
@@ -733,50 +733,50 @@ Before using statistics, the statistics configuration must be set. Otherwise, th
1. Instruction counter
- The instruction counter can help developers to profile the performance of WASM running. Developers can retrieve the `Statistics` context from the `VM` context, or create a new one for the `Executor` creation. The details will be introduced in the next partitions.
+ The instruction counter can help developers to profile the performance of WASM running. Developers can retrieve the `Statistics` context from the `VM` context, or create a new one for the `Executor` creation. The details will be introduced in the next partitions.
- ```c
- WasmEdge_StatisticsContext *StatCxt = WasmEdge_StatisticsCreate();
- /*
- * ...
- * After running the WASM functions with the `Statistics` context
- */
- uint32_t Count = WasmEdge_StatisticsGetInstrCount(StatCxt);
- double IPS = WasmEdge_StatisticsGetInstrPerSecond(StatCxt);
- WasmEdge_StatisticsDelete(StatCxt);
- ```
+ ```c
+ WasmEdge_StatisticsContext *StatCxt = WasmEdge_StatisticsCreate();
+ /*
+ * ...
+ * After running the WASM functions with the `Statistics` context
+ */
+ uint32_t Count = WasmEdge_StatisticsGetInstrCount(StatCxt);
+ double IPS = WasmEdge_StatisticsGetInstrPerSecond(StatCxt);
+ WasmEdge_StatisticsDelete(StatCxt);
+ ```
2. Cost table
- The cost table is to accumulate the cost of instructions with their weights. Developers can set the cost table array (the indices are the byte code value of instructions, and the values are the cost of instructions) into the `Statistics` context. If the cost limit value is set, the execution will return the `cost limit exceeded` error immediately when exceeds the cost limit in runtime.
-
- ```c
- WasmEdge_StatisticsContext *StatCxt = WasmEdge_StatisticsCreate();
- uint64_t CostTable[16] = {
- 0, 0,
- 10, /* 0x02: Block */
- 11, /* 0x03: Loop */
- 12, /* 0x04: If */
- 12, /* 0x05: Else */
- 0, 0, 0, 0, 0, 0,
- 20, /* 0x0C: Br */
- 21, /* 0x0D: Br_if */
- 22, /* 0x0E: Br_table */
- 0
- };
- /*
- * Developers can set the costs of each instruction. The value not
- * covered will be 0.
- */
- WasmEdge_StatisticsSetCostTable(StatCxt, CostTable, 16);
- WasmEdge_StatisticsSetCostLimit(StatCxt, 5000000);
- /*
- * ...
- * After running the WASM functions with the `Statistics` context
- */
- uint64_t Cost = WasmEdge_StatisticsGetTotalCost(StatCxt);
- WasmEdge_StatisticsDelete(StatCxt);
- ```
+ The cost table is to accumulate the cost of instructions with their weights. Developers can set the cost table array (the indices are the byte code value of instructions, and the values are the cost of instructions) into the `Statistics` context. If the cost limit value is set, the execution will return the `cost limit exceeded` error immediately when exceeds the cost limit in runtime.
+
+ ```c
+ WasmEdge_StatisticsContext *StatCxt = WasmEdge_StatisticsCreate();
+ uint64_t CostTable[16] = {
+ 0, 0,
+ 10, /* 0x02: Block */
+ 11, /* 0x03: Loop */
+ 12, /* 0x04: If */
+ 12, /* 0x05: Else */
+ 0, 0, 0, 0, 0, 0,
+ 20, /* 0x0C: Br */
+ 21, /* 0x0D: Br_if */
+ 22, /* 0x0E: Br_table */
+ 0
+ };
+ /*
+ * Developers can set the costs of each instruction. The value not
+ * covered will be 0.
+ */
+ WasmEdge_StatisticsSetCostTable(StatCxt, CostTable, 16);
+ WasmEdge_StatisticsSetCostLimit(StatCxt, 5000000);
+ /*
+ * ...
+ * After running the WASM functions with the `Statistics` context
+ */
+ uint64_t Cost = WasmEdge_StatisticsGetTotalCost(StatCxt);
+ WasmEdge_StatisticsDelete(StatCxt);
+ ```
### Tools Driver
@@ -828,172 +828,172 @@ The following shows the example of running the WASM for getting the Fibonacci. T
1. Run WASM functions rapidly
- Assume that the WASM file [`fibonacci.wasm`](https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/examples/wasm/fibonacci.wasm) is copied into the current directory, and the C file `test.c` is as following:
-
- ```c
- #include
- #include
- int main() {
- /* Create the configure context and add the WASI support. */
- /* This step is not necessary unless you need WASI support. */
- WasmEdge_ConfigureContext *ConfCxt = WasmEdge_ConfigureCreate();
- WasmEdge_ConfigureAddHostRegistration(ConfCxt,
- WasmEdge_HostRegistration_Wasi);
- /* The configure and store context to the VM creation can be NULL. */
- WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(ConfCxt, NULL);
-
- /* The parameters and returns arrays. */
- WasmEdge_Value Params[1] = {WasmEdge_ValueGenI32(5)};
- WasmEdge_Value Returns[1];
- /* Function name. */
- WasmEdge_String FuncName = WasmEdge_StringCreateByCString("fib");
- /* Run the WASM function from file. */
- WasmEdge_Result Res = WasmEdge_VMRunWasmFromFile(
- VMCxt, "fibonacci.wasm", FuncName, Params, 1, Returns, 1);
- /*
- * Developers can run the WASM binary from buffer with the
- * `WasmEdge_VMRunWasmFromBuffer()` API, or from
- * `WasmEdge_ASTModuleContext` object with the
- * `WasmEdge_VMRunWasmFromASTModule()` API.
- */
-
- if (WasmEdge_ResultOK(Res)) {
- printf("Get the result: %d\n", WasmEdge_ValueGetI32(Returns[0]));
- } else {
- printf("Error message: %s\n", WasmEdge_ResultGetMessage(Res));
- }
-
- /* Resources deallocations. */
- WasmEdge_VMDelete(VMCxt);
- WasmEdge_ConfigureDelete(ConfCxt);
- WasmEdge_StringDelete(FuncName);
- return 0;
- }
- ```
-
- Then you can compile and run: (the 5th Fibonacci number is 8 in 0-based index)
-
- ```bash
- $ gcc test.c -lwasmedge
- $ ./a.out
- Get the result: 8
- ```
+ Assume that the WASM file [`fibonacci.wasm`](https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/examples/wasm/fibonacci.wasm) is copied into the current directory, and the C file `test.c` is as following:
+
+ ```c
+ #include
+ #include