-
Notifications
You must be signed in to change notification settings - Fork 50
add nodejs and AssemblyScript memory share demo #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| node_modules/ | ||
| package-lock.json | ||
| build/ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| export function test(): void { | ||
| let str = "hello world"; | ||
| trace(str, str.length); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "extends": "assemblyscript/std/assembly.json", | ||
| "include": [ | ||
| "./**/*.ts" | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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" | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
| }, | ||
|
Comment on lines
+10
to
+15
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AS uses UTF16-LE. Instead of all of this better to use
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry, I will use a customer function instead of builtin |
||
| 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(); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrong usage of built-in
trace. Plaese see docs