From c815f77b09940dadf0f7212af1917ee667c34b94 Mon Sep 17 00:00:00 2001 From: chen ruixiang Date: Mon, 22 Aug 2022 17:38:07 +0800 Subject: [PATCH] add nodejs and AssemblyScript memory share demo --- memshare/.gitignore | 3 +++ memshare/README.md | 18 ++++++++++++++++++ memshare/asconfig.json | 22 ++++++++++++++++++++++ memshare/assembly/index.ts | 4 ++++ memshare/assembly/tsconfig.json | 6 ++++++ memshare/package.json | 25 +++++++++++++++++++++++++ memshare/run.cjs | 28 ++++++++++++++++++++++++++++ 7 files changed, 106 insertions(+) create mode 100644 memshare/.gitignore create mode 100644 memshare/README.md create mode 100644 memshare/asconfig.json create mode 100644 memshare/assembly/index.ts create mode 100644 memshare/assembly/tsconfig.json create mode 100644 memshare/package.json create mode 100644 memshare/run.cjs diff --git a/memshare/.gitignore b/memshare/.gitignore new file mode 100644 index 0000000..7d245c9 --- /dev/null +++ b/memshare/.gitignore @@ -0,0 +1,3 @@ +node_modules/ +package-lock.json +build/ \ No newline at end of file diff --git a/memshare/README.md b/memshare/README.md new file mode 100644 index 0000000..570908d --- /dev/null +++ b/memshare/README.md @@ -0,0 +1,18 @@ +# Memory Share Demo +## Description +This Demo is using Nodejs for implementation. +The main purpose is share memory between Nodejs side and AssemblyScript side. +1. after run the demo, it will print: + ``` + hello world + hhllo world + ``` +2. the first `hello world` is from the function `trace`, after AS call `trace` function, we get the offset of the string. +3. the second `hhllo world` is that, after we get the position of string, we can change the value with this offset position. so if AS call `trace` again, actually the value of linear memory has been changed. + +Technically we can use shared memory without limitation, this `trace` function is a string demo, you can also use this method to share objects (need to embed it on Nodejs side) in linear memory. + +## Build +`npm install` +## Run Demo +`npm run test` diff --git a/memshare/asconfig.json b/memshare/asconfig.json new file mode 100644 index 0000000..8776597 --- /dev/null +++ b/memshare/asconfig.json @@ -0,0 +1,22 @@ +{ + "targets": { + "debug": { + "outFile": "build/debug.wasm", + "textFile": "build/debug.wat", + "sourceMap": true, + "debug": true + }, + "release": { + "outFile": "build/release.wasm", + "textFile": "build/release.wat", + "sourceMap": true, + "optimizeLevel": 3, + "shrinkLevel": 0, + "converge": false, + "noAssert": false + } + }, + "options": { + "bindings": "esm" + } +} \ No newline at end of file diff --git a/memshare/assembly/index.ts b/memshare/assembly/index.ts new file mode 100644 index 0000000..f6cad2c --- /dev/null +++ b/memshare/assembly/index.ts @@ -0,0 +1,4 @@ +export function test(): void { + let str = "hello world"; + trace(str, str.length); +} \ No newline at end of file diff --git a/memshare/assembly/tsconfig.json b/memshare/assembly/tsconfig.json new file mode 100644 index 0000000..e28fcf2 --- /dev/null +++ b/memshare/assembly/tsconfig.json @@ -0,0 +1,6 @@ +{ + "extends": "assemblyscript/std/assembly.json", + "include": [ + "./**/*.ts" + ] +} \ No newline at end of file diff --git a/memshare/package.json b/memshare/package.json new file mode 100644 index 0000000..9b9bef6 --- /dev/null +++ b/memshare/package.json @@ -0,0 +1,25 @@ +{ + "name": "as_demo", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "npm run asbuild && node run.cjs", + "asbuild:debug": "asc assembly/index.ts --target debug ", + "asbuild:release": "asc assembly/index.ts --target release ", + "asbuild": "npm run asbuild:debug && npm run asbuild:release", + "start": "npx serve ." + }, + "author": "", + "license": "ISC", + "devDependencies": { + "assemblyscript": "0.20.19" + }, + "type": "module", + "exports": { + ".": { + "import": "./build/release.js", + "types": "./build/release.d.ts" + } + } +} \ No newline at end of file diff --git a/memshare/run.cjs b/memshare/run.cjs new file mode 100644 index 0000000..81617eb --- /dev/null +++ b/memshare/run.cjs @@ -0,0 +1,28 @@ +const fs = require('fs'); +const wasmBuffer = fs.readFileSync('build/debug.wasm'); + +const imports = {}; +imports.env = imports.env || {}; +var sharedMemory = new WebAssembly.Memory({ initial: 1 });//request one page memory +var globalOffset=0; +Object.assign(imports.env, { + memory: sharedMemory, + trace: function (offset, length) { + var bytes = new Uint8Array(sharedMemory.buffer, offset, length*2); + globalOffset = offset; + var string = new TextDecoder('utf8').decode(bytes); + console.log(string); + }, + abort(_msg, _file, line, column) { + console.error("abort called at index.ts:" + line + ":" + column); + } +}); + +WebAssembly.instantiate(wasmBuffer, imports).then(wasmModule => { + const { test,memory } = wasmModule.instance.exports; + sharedMemory = memory; + test(); + var bytes = new Uint8Array(sharedMemory.buffer, globalOffset, 6); + bytes[2] = 104; + test(); +});