Skip to content

Commit 59e0dda

Browse files
RichardRichard
authored andcommitted
adding array helper
1 parent 52a7be0 commit 59e0dda

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,14 @@ JSFunction fnLog = js_register_function(
5656
js_invoke_function_2(fnLog, "Hello World!");
5757
```
5858
59-
In your JS function `context` is passed in to handle most issues you'll encounter
59+
In your JS function context `this` several functions handle most issues you'll encounter
6060
61-
* `context.readUtf8FromMemory(start,length)` - Extract utf-8 text from your program's memory.
62-
* `context.writeUtf8ToMemory(start,str)` - Write utf-8 to a memory location you are sure it should go.
63-
* `context.readCStringFromMemory(start,length)` - Extract C string text from your program's memory.
64-
* `context.writeCStringToMemory(start,str)` - Write C string to a memory location you are sure it should go.
65-
* `context.storeObject(object)` - Store an object in your context for later reference, get a handle you can give to WebAssembly.
66-
* `context.getObject(handle)` - Retreive and object from your context with a handle.
67-
* `context.releaseObject(handle)` - Release a stored object so it's memory can be freed.
68-
* `context.module` - Get access to your program so you can call methods on it.
61+
* `readUtf8FromMemory(start,length)` - Extract utf-8 text from your program's memory.
62+
* `writeUtf8ToMemory(start,str)` - Write utf-8 to a memory location you are sure it should go.
63+
* `readCStringFromMemory(start,length)` - Extract C string text from your program's memory.
64+
* `writeCStringToMemory(start,str)` - Write C string to a memory location you are sure it should go.
65+
* `readUint8ArrayFromMemory` - Read a list of uint8 from a pointer to a location of a number of elements, followed by a pointer to bytes in memory.
66+
* `storeObject(object)` - Store an object in your context for later reference, get a handle you can give to WebAssembly.
67+
* `getObject(handle)` - Retreive and object from your context with a handle.
68+
* `releaseObject(handle)` - Release a stored object so it's memory can be freed.
69+
* `module` - Get access to your program so you can call methods on it.

js-wasm.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ async function load_and_run_wasm(wasmURL) {
3131
let text = this.utf8dec.decode(memory.subarray(start, start + len));
3232
return text;
3333
},
34+
readUint8ArrayFromMemory(start) {
35+
const data32 = new Uint32Array(this.module.instance.exports.memory.buffer);
36+
const ptr = data32[start / 4];
37+
const length = data32[ptr / 4];
38+
let b = mem.slice(ptr + 4, ptr + 4 + length);
39+
return new Uint8Array(b);
40+
},
3441
writeUtf8ToMemory: function (start, str) {
3542
let bytes = utf8enc.encode(str);
3643
let len = bytes.length;

0 commit comments

Comments
 (0)