Skip to content

Commit

Permalink
CURRENT TESTS PASSED
Browse files Browse the repository at this point in the history
prepared:
(table (export "externref_table") 1 externref)
  • Loading branch information
pannous committed Dec 1, 2024
1 parent c22ee9b commit 3290f30
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
15 changes: 9 additions & 6 deletions docs/wasp.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,30 +199,33 @@ function smartResult(object) { // returns BigInt smart pointer to object
}
}


const refToIndexMap = new Map();
async function storeObject() {
// (table (export "externref_table") 1 externref)
const table = instance.exports.externref_table;

// Store a JavaScript object in the table
const obj = {message: "Hello from externref!"};
const ref = {message: "Hello from externref!"};
const index = table.length; // Get the current table size
table.grow(1); // Expand table by one slot
table.set(index, obj);
console.log("Stored object index:", index); // Outputs the index where `obj` is stored
table.set(index, ref);
refToIndexMap.set(ref, index);
console.log("Stored object index:", index); // Outputs the index where `ref` is stored
// NOW WE CAN PASS THE INDEX TO WASM AS SMARTY !

// Retrieve the object by index
const retrievedObj = table.get(index);
const retrievedObj = table.get(index); // Retrieve the object by index directly! ref=>object!
console.log(retrievedObj.message); // Outputs: "Hello from externref!"

/*
;; Retrieve the externref from the table and call the logger
(call $print_externref (table.get (local.get 0)))
(call $cast_to_node (table.get (local.get 0)))
*/
}

function getExternRefIndex(ref) {
return refToIndexMap.get(ref); // Retrieve the index for a given ref
}
async function storeFunction() {
// (table (export "funcref_table") 10 funcref)
// Access the exported table
Expand Down
4 changes: 3 additions & 1 deletion source/NodeTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ enum Valtype {
wasm_struct = 0x6b, // ≠ wasmtype_struct = 0x5f in type section
wasm_array = wasm_struct, // 0x6b ≠ wasmtype_array = 0x5e in type section
anyref = 0x6f,// was conceptually and namewise merged into externref
externref = 0x6f, // -0x11 111 ≠ 'ref'=wasm_struct!!
externref = 0x6f, // -0x11 111 (js)object ! ≠ 'ref'=wasm_struct!!
object = externref,
js_object = externref,
funcref = 0x70, // -0x10
func = 0x60,
string_ref = 0x64, // wasm stringref
Expand Down
2 changes: 1 addition & 1 deletion source/smart_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ typedef enum smartType4bit {
// foverflow=0x3,
symbola = 0x4, // ≈ stringa &memoryChars[payload]
json5 = 0x5,
reference = 0x6, // value = externref index in (table (export "externref_table") 1 externref)
ref_index = 0x6, // value = externref index in (table (export "externref_table") 1 externref) => js object !
// long60p = 0x6, // pointer to long60
septet = 0x7, // 7 hexes à 4 bit OR 7 bytes in smart64! 7 is NUMERIC through 0x7C…0x7F float32 etc
utf8char = 0x8, // UTF24 Unicode
Expand Down

0 comments on commit 3290f30

Please sign in to comment.