Skip to content

Commit

Permalink
result = eval("getExternRefProperty($canvas,'width')"); // ok!!
Browse files Browse the repository at this point in the history
check_eq(result.value.longy,300);
  • Loading branch information
pannous committed Dec 27, 2023
1 parent dec6701 commit 7c12639
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 22 deletions.
14 changes: 13 additions & 1 deletion source/Angle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1902,8 +1902,20 @@ void preRegisterFunctions() {
#else
functions["fd_write"].module = new Module{.name="wasi_unstable"};
#endif

// DOM functions
functions["getElementById"].import();//.builtin();
functions["getElementById"].signature.add((Type) charp).returns((Type) externref);
functions["getElementById"].signature.add((Type) charp).returns((Type) externref /*!!*/);
functions["testExternRef"].import();//.builtin();
functions["testExternRef"].signature.add((Type) externref).returns(int32);//.returns((Type) externref);
functions["getExternRefProperty"].import();//.builtin();
functions["getExternRefProperty"].signature.add((Type) externref).add(charp).returns(i32);
// functions["getExternRefProperty"].signature.add((Type) externref).add(charp).returns(longs);
// functions["getExternRefProperty"].signature.add((Type) externref,"object").add(strings,"property").returns((Type)smarti64);

// functions["invokeExternRef"].import();//.builtin();
// functions["invokeExternRef"].signature.add((Type) externref).add(strings,"method").add(node,"params").returns((Type)Primitive::smarti64);


// functions["$"].import();//.builtin();
// functions["$"].signature.add((Type) strings).returns((Type) referencex);
Expand Down
14 changes: 7 additions & 7 deletions source/WebApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,8 @@ void bind(char *name, char *(*func)(char *)) {
}

void splitLog(const std::string s) {
view.set_title(s);
std::string item;
int index = 0;
std::cout << s << std::endl;
std::string item;
// while (index < 100) {
// item = webview::json_parse(s, "", index++);
// std::cout << item << std::endl;
Expand Down Expand Up @@ -263,12 +261,14 @@ int64 open_webview(String url = "") {
testWebview(s);
return s;
});// works, with
view.bind("alert", [](std::string s) -> std::string {
splitLog(s);
return s;
});// no native popup?
// view.bind("alert", [](std::string s) -> std::string {
// splitLog(s);
// return s;
// });// no native popup?
// why does alert('a') print 'a' alert(1) print 1, even though lambda type is string?
view.bind("log", [](std::string s) -> std::string {
std::cout << s << std::endl;
view.set_title(s);
splitLog(s);
return s;
});
Expand Down
60 changes: 51 additions & 9 deletions source/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,18 @@
}

function print(...x) {
results.value += x + "\n"
log(...x);// to stdout and window title!
let xs = Array.isArray(x) ? x.join(' ') : x;
results.value += xs + "\n"
log(String(xs));// to stdout and window title!
// log(JSON.stringify(xs));// to stdout and window title!
console.log(...x);
}

window.onload = function () {
// test()
};
</script>

<p id="message">RUNNING WASM TESTS</p>
<script type="text/javascript">
$ = id => document.getElementById(id), // .bind(document); for debugging
ignore = x => x
nop = x => ignore(x)
const string_header_64 = 0x0010000000000000n // n means BigInt("…")
Expand Down Expand Up @@ -190,6 +190,16 @@
}
identity = x => x

const utf8_decoder = new TextDecoder('utf8');

const string = function (pointer, length, format) {
let buffer = new Uint8Array(memory.buffer, 0, memory.buffer.byteLength);
if (!length) length = 0;
if (length <= 0) while (buffer[pointer + ++length]) ; // auto length
const arr = new Uint8Array(buffer.subarray(pointer, pointer + length));
return utf8_decoder.decode(arr);
};

async function wasmx(code) {
try {
// let table = new WebAssembly.Table({initial: table_size, maximum: table_size, element: "anyfunc"});
Expand All @@ -198,7 +208,35 @@
env: {
memory,
// table,
getElementById: id => document.getElementById(id),
getElementById: id => {
print("getElementById", id, string(id))
return document.getElementById(string(id))
},
testExternRef: ref => {
print("testExternRef OK ", ref);
return 42;
},
getExternRefProperty: (ref, prop0) => {
let prop = string(prop0)
print("CALLING getExternRefProperty", ref, prop)
if (ref && typeof ref[prop] !== 'undefined') {
print("getExternRefProperty OK ", ref, prop, ref[prop])
return ref[prop];
} else {
throw new Error(`'${prop}' is not a property of the provided reference`);
}
},
invokeExternRef: (ref, fun, params) => {
print("invokeExternRef", ref, fun, params)
// Check if 'fun' is a valid method of 'ref'
if (ref && typeof ref[fun] === 'function') {
// Call the method with the provided parameters
return ref[fun](...params);
} else {
// Handle the case where 'fun' is not a valid method
throw new Error(`'${fun}' is not a function of the provided reference`);
}
},
square: x => x * x, // usually native wasm, better test with 'grow'!
pow: Math.pow, // usually wasp lib, better test other function
powd: Math.pow,
Expand Down Expand Up @@ -261,14 +299,17 @@
//const module = await WebAssembly.compileStreaming(fetch('program.wasm'));
const instance = await WebAssembly.instantiate(module, imports, memory);
memory = instance.memory || module.memory || instance.exports.memory || memory

//const { instance } = await WebAssembly.instantiateStreaming(fetch('program.wasm'));
// print("Exports: ", instance.exports);//show what we've got
let main = instance.exports.main || instance.exports._start;
if (main) {
result = main()
let result = main()
print("result")
print(result)
print(smartResult(result))
wasm_done(result);// pass over raw result
wasm_done(String(result));// pass over raw result
// wasm_done(JSON.stringify(result));// pass over raw result
} else {
wasm_error("NO MAIN!");
}
Expand All @@ -281,7 +322,8 @@
}
}

document.addEventListener("DOMContentLoaded", () => test());
window.onload = () => test()
// window.onload = () => run(code_input.value)
</script>

<style>
Expand Down
16 changes: 12 additions & 4 deletions source/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,19 @@ void testDom() {
assert_equals(result.kind, call);
result = eval("getElementById('canvas');");
// print(typeName(result.kind));
assert_equals(result.kind, longs); // todo: can't use smart pointers for elusive externref
// assert_equals(result.kind, longs); // todo: can't use smart pointers for elusive externref
assert_equals(result.kind, bools); // todo: can't use smart pointers for elusive externref

// assert_equals(result.kind, (int64) externref); // todo: can't use smart pointers for elusive externref
// result = eval("document.getElementById('canvas');");
result = analyze(parse("$canvas"));
assert_equals(result.kind, (int64) externref);
result = eval("testExternRef($canvas)"); // ok!!
check(result.value.longy = 42);

result = eval("getExternRefProperty($canvas,'width')"); // ok!!
check_eq(result.value.longy, 300);

// embedder.trace('canvas = document.getElementById("canvas");')
// print(nod);
}
Expand Down Expand Up @@ -3289,9 +3297,9 @@ void testCurrent() {
// assert_emit("print('hi')", 0)
// assert_emit("puts('hi')", 8)
testDom();
//#if WEBAPP
// return;
//#endif
#if WEBAPP
return;
#endif
// exit(1);
testOldRandomBugs();

Expand Down
2 changes: 1 addition & 1 deletion source/wasm_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,6 @@ extern "C" chars download(chars name);// curl wget sync download via js / runtim


#if WEBAPP

void console_log(const char *s); // print from wasp to browser console ( in addition to stdout )
//int testExternRef(void* externRef); // defined in wasp.js
#endif

0 comments on commit 7c12639

Please sign in to comment.