Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
pannous committed Nov 23, 2024
1 parent e90ec0d commit 6664529
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 63 deletions.
33 changes: 20 additions & 13 deletions docs/wasp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* WASP: WebAssembly Programming Language API/ABI
* version="1.0.26",
* version="1.0.27",
* This file contains the javascript counterpoint to the WASP runtime,
* offering host functions to wasi/wasp modules, like download() and run_wasm()
* Converts wasm types to/from JS objects via node() and string() as a shim for wasm GC types
Expand Down Expand Up @@ -33,14 +33,14 @@ let string_header_32 = 0x10000000
let array_header_32 = 0x40000000
let node_header_32 = 0x80000000

function download(url) {
function download(url, binary = false) {
if (typeof url != "string") url = chars(url)
// console.log("download", url)
let xhr = new XMLHttpRequest();
xhr.open('GET', url, false);
xhr.send();
if (xhr.status === 200)
return chars(xhr.responseText.trim()) // to be used in WASM as string! use fetch() in JS
return binary ? bytes(new Uint8Array(xhr.response)) : chars(xhr.responseText.trim()) // to be used in WASM as string! use fetch() in JS
else
return null;
}
Expand Down Expand Up @@ -84,6 +84,12 @@ const fd_write = function (fd, c_io_vector, iovs_count, nwritten) {
};


function getWasmFunclet(funclet_name, size_p) {
[pointer, bytes_size] = download(lib_folder_url + chars(funclet_name)+".wasm", binary = true)
set_int(size_p, bytes_size)
return pointer
}

function parse(data) {
let node_pointer = runtime_exports.Parse(chars(data))// also calls run()!
let nod = new node(node_pointer);
Expand Down Expand Up @@ -188,6 +194,7 @@ let imports = {
},
// HTML DOM JS functions
download,
getWasmFunclet,
init_graphics: nop, // canvas init by default
requestAnimationFrame: nop, // todo
getDocumentBody: () => document.body,
Expand Down Expand Up @@ -239,12 +246,7 @@ let imports = {
},

exit: terminate, // should be wasi.proc_exit!
// pow: Math.pow,
pow: (x, y) => x ** y,
pow: (x, y) => {
console.log("POW", x, y);
return x ** y
},
print: x => console.log(string(x)),
puti: x => console.log(x), // allows debugging of ints without format String allocation!
js_demangle: x => x,
Expand Down Expand Up @@ -348,15 +350,20 @@ function load_chars(pointer, length = -1, format = 'utf8', module_memory) {
}
}

function bytes(data) {
if (!data) return 0;// MAKE SURE!
buffer = new Uint8Array(memory.buffer, HEAP_END, data.length);
buffer.set(data, 0);
let c = HEAP_END
HEAP_END += data.length;
return [c, data.length]
}

function chars(data) {
if (!data) return 0;// MAKE SURE!
if (typeof data != "string") return load_chars(data)
const uint8array = new TextEncoder("utf-8").encode(data + "\0");
buffer = new Uint8Array(memory.buffer, HEAP_END, uint8array.length);
buffer.set(uint8array, 0);
let c = HEAP_END
HEAP_END += uint8array.length;
return c;
return bytes(uint8array)[0] // pointer without length
}

function set_int(address, val) {
Expand Down
19 changes: 13 additions & 6 deletions source/Angle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1468,6 +1468,7 @@ Type preEvaluateType(Node &node, Function &context) {
return mapType(node.name);
}
if (node.kind == operators) {
if(node.name == "")return float64;// todo generalize
Node &lhs = node[0];
auto lhs_type = preEvaluateType(lhs, context);
if (node.length == 1)
Expand Down Expand Up @@ -1732,22 +1733,28 @@ Function *findLibraryFunction(String name, bool searchAliases) {
}
#endif
if (contains(funclet_list, name)) {
#if WASM

#if MY_WASM
size_t byte_count = 0;
bytes funclet_bytes = getWasmFunclet(name,&byte_count);// load from host
Module &funclet_module = read_wasm(funclet_bytes, byte_count);
funclet_module.code.name = name;
funclet_module.name = name;
module_cache.add(name.hash(), &funclet_module);
#elif WASM
// todo("getWaspFunclet get library function signature from wasp");
warn("WASP function "s + name + " getWaspFunclet todo");
auto pFunction = getWaspFunction(name).clone();
pFunction->import();
return use_required(pFunction);

// auto funclet = getWaspFunclet(name);// todo get library function signature from wasp
// todo("getWaspFunclet get library function signature from wasp");
// return 0;
#else
// todo simplify by using read_wasm in MY_WASM?
Module &funclet_module = read_wasm(findFile(name, "lib"));
// check(funclet_module.functions.has(name));
#endif
auto funclet = funclet_module.functions[name];
addLibrary(&funclet_module);
return use_required(&funclet);
#endif
}
if (name.in(function_list) and libraries.size() == 0)
libraries.add(&loadModule("wasp-runtime.wasm"));// on demand
Expand Down
2 changes: 2 additions & 0 deletions source/wasm_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,8 @@ List<String> arguments();

#if MY_WASM
extern "C" void registerWasmFunction(chars name, chars mangled);
extern "C" void registerWasmFunclet(chars name, bytes funclet, size_t size);// called by host
extern "C" bytes getWasmFunclet(chars name, size_t* size);// calling host
extern "C" chars download(chars name);// curl wget sync download via js / runtime!
#endif

Expand Down
Loading

0 comments on commit 6664529

Please sign in to comment.