Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions memshare/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
package-lock.json
build/
18 changes: 18 additions & 0 deletions memshare/README.md
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`
22 changes: 22 additions & 0 deletions memshare/asconfig.json
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"
}
}
4 changes: 4 additions & 0 deletions memshare/assembly/index.ts
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);
Copy link
Member

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

}
6 changes: 6 additions & 0 deletions memshare/assembly/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "assemblyscript/std/assembly.json",
"include": [
"./**/*.ts"
]
}
25 changes: 25 additions & 0 deletions memshare/package.json
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"
}
}
}
28 changes: 28 additions & 0 deletions memshare/run.cjs
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AS uses UTF16-LE. Instead of all of this better to use --bindgen esm which already included proper trace and other bindings. See docs

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, I will use a customer function instead of builtin trace

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();
});