From 5e249d6f49eaa390c4844435a48478d537a263c6 Mon Sep 17 00:00:00 2001 From: Quodss Date: Tue, 12 Nov 2024 19:35:30 +0100 Subject: [PATCH 01/22] init commit --- build.zig | 6 +++ build.zig.zon | 3 ++ ext/wasm3/build.zig | 83 ++++++++++++++++++++++++++++++++++++++++ ext/wasm3/build.zig.zon | 16 ++++++++ pkg/noun/build.zig | 7 ++++ pkg/noun/build.zig.zon | 3 ++ pkg/noun/jets/e/urwasm.c | 29 ++++++++++++++ pkg/noun/jets/tree.c | 50 ++++++++++++++++++++++++ pkg/noun/jets/w.h | 2 + 9 files changed, 199 insertions(+) create mode 100644 ext/wasm3/build.zig create mode 100644 ext/wasm3/build.zig.zon create mode 100644 pkg/noun/jets/e/urwasm.c diff --git a/build.zig b/build.zig index 45e5d663b5..0342efafab 100644 --- a/build.zig +++ b/build.zig @@ -361,6 +361,11 @@ fn buildBinary( .optimize = optimize, }); + const wasm3 = b.dependency("wasm3", .{ + .target = target, + .optimize = optimize, + }); + // // Build Artifact // @@ -416,6 +421,7 @@ fn buildBinary( urbit.linkLibrary(sigsegv.artifact("sigsegv")); urbit.linkLibrary(urcrypt.artifact("urcrypt")); urbit.linkLibrary(whereami.artifact("whereami")); + urbit.linkLibrary(wasm3.artifact("wasm3")); if (t.isDarwin()) { // Requires llvm@18 homebrew installation diff --git a/build.zig.zon b/build.zig.zon index b3e3900e00..a038ed9e12 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -56,6 +56,9 @@ .url = "https://github.com/allyourcodebase/zlib/archive/0918e87b7629b9c6a50a08edd0ce30d849758faf.tar.gz", .hash = "122034ab2a12adf8016ffa76e48b4be3245ffd305193edba4d83058adbcfa749c107", }, + .wasm3 = .{ + .path = "./ext/wasm3", + }, }, .paths = .{ "", diff --git a/ext/wasm3/build.zig b/ext/wasm3/build.zig new file mode 100644 index 0000000000..ef39defc01 --- /dev/null +++ b/ext/wasm3/build.zig @@ -0,0 +1,83 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + const t = target.result; + + const softfloat = b.dependency("softfloat", .{ + .target = target, + .optimize = optimize, + }); + + const wasm3_c = b.dependency("wasm3", .{ + .target = target, + .optimize = optimize, + }); + + const wasm3 = b.addStaticLibrary(.{ + .name = "wasm3", + .target = target, + .optimize = optimize, + }); + + wasm3.linkLibC(); + + wasm3.addIncludePath(wasm3_c.path("source/")); + + const common_flags = [_][]const u8{ + "-std=c99", + "-Wall", + "-Wextra", + "-Wparentheses", + "-Wundef", + "-Wpointer-arith", + "-Wstrict-aliasing=2", + "-Werror=implicit-function-declaration", + }; + + const mac_flags = common_flags ++ [_][]const u8{ + "-fPIC", + "-c", + }; + + wasm3.addCSourceFiles(.{ + .root = wasm3_c.path("source/"), + .files = &.{ + "m3_bind.c", + "m3_code.c", + "m3_compile.c", + "m3_core.c", + "m3_emit.c", + "m3_env.c", + "m3_exec.c", + "m3_function.c", + "m3_info.c", + "m3_module.c", + "m3_optimize.c", + "m3_parse.c", + }, + .flags = if (t.os.tag == .macos) &mac_flags else &common_flags, + }); + + wasm3.installHeader(wasm3_c.path("source/m3_config_platforms.h"), "m3_config_platforms.h"); + wasm3.installHeader(wasm3_c.path("source/m3_bind.h"), "m3_bind.h"); + wasm3.installHeader(wasm3_c.path("source/m3_code.h"), "m3_code.h"); + wasm3.installHeader(wasm3_c.path("source/m3_compile.h"), "m3_compile.h"); + wasm3.installHeader(wasm3_c.path("source/m3_config.h"), "m3_config.h"); + wasm3.installHeader(wasm3_c.path("source/m3_core.h"), "m3_core.h"); + wasm3.installHeader(wasm3_c.path("source/m3_emit.h"), "m3_emit.h"); + wasm3.installHeader(wasm3_c.path("source/m3_env.h"), "m3_env.h"); + wasm3.installHeader(wasm3_c.path("source/m3_exception.h"), "m3_exception.h"); + wasm3.installHeader(wasm3_c.path("source/m3_exec.h"), "m3_exec.h"); + wasm3.installHeader(wasm3_c.path("source/m3_exec_defs.h"), "m3_exec_defs.h"); + wasm3.installHeader(wasm3_c.path("source/m3_function.h"), "m3_function.h"); + wasm3.installHeader(wasm3_c.path("source/m3_info.h"), "m3_info.h"); + wasm3.installHeader(wasm3_c.path("source/m3_math_utils.h"), "m3_math_utils.h"); + wasm3.installHeader(wasm3_c.path("source/wasm3.h"), "wasm3.h"); + wasm3.installHeader(wasm3_c.path("source/wasm3_defs.h"), "wasm3_defs.h"); + + wasm3.linkLibrary(softfloat.artifact("softfloat")); + + b.installArtifact(wasm3); +} diff --git a/ext/wasm3/build.zig.zon b/ext/wasm3/build.zig.zon new file mode 100644 index 0000000000..edc404d56a --- /dev/null +++ b/ext/wasm3/build.zig.zon @@ -0,0 +1,16 @@ +.{ + .name = "wasm3", + .version = "0.0.1", + .dependencies = .{ + .softfloat = .{ + .path = "../softfloat", + }, + .wasm3 = .{ + .url = "https://github.com/Quodss/wasm3/archive/refs/heads/urwasm.tar.gz", + .hash = "122051e4183102eb4653556d0bcbf0908203ebdaa2c105df181e63a569f05f702baa", + }, + }, + .paths = .{ + "", + }, +} \ No newline at end of file diff --git a/pkg/noun/build.zig b/pkg/noun/build.zig index 1c816ea7f8..99d9c89213 100644 --- a/pkg/noun/build.zig +++ b/pkg/noun/build.zig @@ -104,6 +104,11 @@ pub fn build(b: *std.Build) !void { .optimize = optimize, }); + const wasm3 = b.dependency("wasm3", .{ + .target = target, + .optimize = optimize, + }); + pkg_noun.linkLibC(); pkg_noun.linkLibrary(pkg_c3.artifact("c3")); @@ -123,6 +128,7 @@ pub fn build(b: *std.Build) !void { pkg_noun.linkLibrary(urcrypt.artifact("urcrypt")); pkg_noun.linkLibrary(whereami.artifact("whereami")); pkg_noun.linkLibrary(zlib.artifact("z")); + pkg_noun.linkLibrary(wasm3.artifact("wasm3")); pkg_noun.addIncludePath(b.path("")); if (t.os.tag.isDarwin()) @@ -310,6 +316,7 @@ const c_source_files = [_][]const u8{ "jets/e/slaw.c", "jets/e/tape.c", "jets/e/trip.c", + "jets/e/urwasm.c", "jets/f/cell.c", "jets/f/comb.c", "jets/f/cons.c", diff --git a/pkg/noun/build.zig.zon b/pkg/noun/build.zig.zon index 305efb9c97..3982a2a467 100644 --- a/pkg/noun/build.zig.zon +++ b/pkg/noun/build.zig.zon @@ -53,6 +53,9 @@ .url = "https://github.com/allyourcodebase/zlib/archive/0918e87b7629b9c6a50a08edd0ce30d849758faf.tar.gz", .hash = "122034ab2a12adf8016ffa76e48b4be3245ffd305193edba4d83058adbcfa749c107", }, + .wasm3 = .{ + .path = "../../ext/wasm3", + }, }, .paths = .{ "", diff --git a/pkg/noun/jets/e/urwasm.c b/pkg/noun/jets/e/urwasm.c new file mode 100644 index 0000000000..3e9c8c9971 --- /dev/null +++ b/pkg/noun/jets/e/urwasm.c @@ -0,0 +1,29 @@ +/// @file + +#include "jets/k.h" +#include "jets/q.h" +#include "jets/w.h" + +#include "noun.h" + +#include "wasm3.h" +#include "m3_env.h" + +u3_weak +u3we_lia_run(u3_noun cor) +{ + if (c3__none == u3x_at(u3x_sam_7, cor)) { + return u3_none; + } + + // prepare batteries and contexts for comparisons + // update seed (stateless phase) + // parse wasm3 + // TODO validate wasm3 + // instantiate wasm3 + // statefully reduce the monad by matching + // the batteries and checking the contexts if necessary (call, memread, memwrite) + // return the final yield and the updated seed + u3_noun tar_mold = u3nt(u3nq(8, u3nc(0, 6), 8, u3nt(u3nq(5, u3nc(0, 14), 0, 2), 0, 6)), 0, 0); // *:~ + return u3_none; +} diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index 59a93204bb..c2b8f06e11 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -2359,6 +2359,55 @@ static u3j_core _138_hex_chacha_d[] = {} }; +// urwasm jet registration +// +static u3j_harm _138_hex_lia_run_a[] = {{".2", u3we_lia_run, c3y}, {}}; + +static u3j_core _138_hex_lia_core_d[] = { + { "run", 7, _138_hex_lia_run_a, 0, no_hashes }, + {} +}; + +static u3j_core _138_hex_lia_monad_d[] = { + { "core", 3, 0, _138_hex_lia_core_d, no_hashes }, + {} +}; + +static u3j_core _138_hex_wasm_engine_d[] = { + { "monad", 3, 0, _138_hex_lia_monad_d, no_hashes }, + {} +}; + +static u3j_core _138_hex_wasm_op_def_d[] = { + { "wasm-engine", 3, 0, _138_hex_wasm_engine_d, no_hashes }, + {} +}; + +static u3j_core _138_hex_wasm_validator_d[] = { + { "wasm-op-def", 3, 0, _138_hex_wasm_op_def_d, no_hashes }, + {} +}; + +static u3j_core _138_hex_wasm_parser_d[] = { + { "validator", 3, 0, _138_hex_wasm_validator_d, no_hashes }, + {} +}; + +static u3j_core _138_hex_lia_sur_d[] = { + { "wasm-parser", 3, 0, _138_hex_wasm_parser_d, no_hashes }, + {} +}; + +static u3j_core _138_hex_wasm_engine_sur_d[] = { + { "monad-sur", 3, 0, _138_hex_lia_sur_d, no_hashes }, + {} +}; + +static u3j_core _138_hex_wasm_sur_d[] = { + { "engine-sur", 3, 0, _138_hex_wasm_engine_sur_d, no_hashes }, + {} +}; + static u3j_core _138_hex_d[] = { { "lore", 63, _140_hex_lore_a, 0, no_hashes }, { "leer", 63, _140_hex_leer_a, 0, no_hashes }, @@ -2378,6 +2427,7 @@ static u3j_core _138_hex_d[] = { "secp", 6, 0, _140_hex_secp_d, no_hashes }, { "mimes", 31, 0, _140_hex_mimes_d, no_hashes }, { "json", 31, 0, _139_hex_json_d, no_hashes }, + { "wasm-sur", 3, 0, _138_hex_wasm_sur_d, no_hashes }, {} }; diff --git a/pkg/noun/jets/w.h b/pkg/noun/jets/w.h index 78494314fe..3149852c83 100644 --- a/pkg/noun/jets/w.h +++ b/pkg/noun/jets/w.h @@ -304,6 +304,8 @@ u3_noun u3we_crc32(u3_noun); + u3_noun u3we_lia_run(u3_noun); + /** Tier 6. **/ u3_noun u3wf_bull(u3_noun); From 3ba29c75a91f51573ad77e3316e3cf67fe3baff9 Mon Sep 17 00:00:00 2001 From: Quodss Date: Wed, 13 Nov 2024 20:20:48 +0100 Subject: [PATCH 02/22] monadic jet --- pkg/noun/jets/e/urwasm.c | 560 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 550 insertions(+), 10 deletions(-) diff --git a/pkg/noun/jets/e/urwasm.c b/pkg/noun/jets/e/urwasm.c index 3e9c8c9971..ac92d4c656 100644 --- a/pkg/noun/jets/e/urwasm.c +++ b/pkg/noun/jets/e/urwasm.c @@ -9,21 +9,561 @@ #include "wasm3.h" #include "m3_env.h" + +const c3_w TAS_I32 = c3_s3('i','3','2'); +const c3_w TAS_I64 = c3_s3('i','6','4'); +const c3_w TAS_F32 = c3_s3('f','3','2'); +const c3_w TAS_F64 = c3_s3('f','6','4'); + +const M3Result m3Lia_Arrow = "non-zero yield from import arrow"; + +typedef struct { + u3_noun call_bat; + u3_noun memread_bat; + u3_noun memwrite_bat; + u3_noun call_ext_bat; + u3_noun try_bat; + u3_noun catch_bat; + u3_noun return_bat; + u3_noun call_ctx; + u3_noun memread_ctx; + u3_noun memwrite_ctx; +} match_data_struct; + +typedef struct { + IM3Module wasm_module; // p + u3_noun lia_shop; // r + u3_noun import; // q + match_data_struct* match; + u3_noun arrow_yil; +} lia_state; + +// TRANSFER result +static u3_noun +_atoms_from_stack(const void** valptrs, c3_w n, c3_y* types) { + u3_noun out = u3_nul; + while (n) { + n--; + switch (types[n]) { // TODO 64 bit vere + case c_m3Type_i32: { + out = u3nc( + u3i_word(*(c3_w*)valptrs[n]), + out + ); + break; + } + case c_m3Type_i64: { + out = u3nc( + u3i_chub(*(c3_d*)valptrs[n]), + out + ); + break; + } + case c_m3Type_f32: { + out = u3nc( + u3i_word(*(c3_w*)valptrs[n]), + out + ); + break; + } + case c_m3Type_f64: { + out = u3nc( + u3i_chub(*(c3_d*)valptrs[n]), + out + ); + break; + } + default: return u3m_bail(c3__fail); + } + } + return out; +} + +// RETAIN coins +static void +_atoms_to_stack( + u3_noun atoms, + const void** valptrs, + c3_w n, + c3_y* types +) { + for (c3_w i = 0; i < n; i++) { + if (c3y == u3ud(atoms)) { + u3m_bail(c3__fail); + } + u3_noun atom; + u3r_cell(atoms, &atom, &atoms); + switch (types[i]) { + case c_m3Type_i32: { + *(c3_w*)valptrs[i] = u3r_word(0, atom); + break; + } + case c_m3Type_i64: { + *(c3_d*)valptrs[i] = u3r_chub(0, atom); + break; + } + case c_m3Type_f32: { + *(c3_w*)valptrs[i] = u3r_word(0, atom); + break; + } + case c_m3Type_f64: { + *(c3_d*)valptrs[i] = u3r_chub(0, atom); + break; + } + default: u3m_bail(c3__fail); + } + } + if (u3_nul != atoms) u3m_bail(c3__fail); +} + +// TRANSFER result +static u3_noun +_coins_from_stack(const void** valptrs, c3_w n, c3_y* types) { + u3_noun out = u3_nul; + while (n) { + n--; + switch (types[n]) { // TODO 64 bit vere + case c_m3Type_i32: { + out = u3nc( + u3nc(TAS_I32, u3i_word(*(c3_w*)valptrs[n])), + out + ); + break; + } + case c_m3Type_i64: { + out = u3nc( + u3nc(TAS_I64, u3i_chub(*(c3_d*)valptrs[n])), + out + ); + break; + } + case c_m3Type_f32: { + out = u3nc( + u3nc(TAS_F32, u3i_word(*(c3_w*)valptrs[n])), + out + ); + break; + } + case c_m3Type_f64: { + out = u3nc( + u3nc(TAS_F64, u3i_chub(*(c3_d*)valptrs[n])), + out + ); + break; + } + default: return u3m_bail(c3__fail); + } + } + return out; +} + +// RETAIN coins +static void +_coins_to_stack( + u3_noun coins, + const void** valptrs, + c3_w n, + c3_y* types +) { + for (c3_w i = 0; i < n; i++) { + if (c3y == u3ud(coins)) { + u3m_bail(c3__fail); + } + u3_noun coin; + u3r_cell(coins, &coin, &coins); + if (c3y == u3ud(coin)) { + u3m_bail(c3__fail); + } + u3_noun tag, value; + u3r_cell(coin, &tag, &value); + if (c3n == u3ud(value)) { + u3m_bail(c3__fail); + } + switch (types[i]) { + case c_m3Type_i32: { + if (TAS_I32 != tag) u3m_bail(c3__fail); + *(c3_w*)valptrs[i] = u3r_word(0, value); + break; + } + case c_m3Type_i64: { + if (TAS_I64 != tag) u3m_bail(c3__fail); + *(c3_d*)valptrs[i] = u3r_chub(0, value); + break; + } + case c_m3Type_f32: { + if (TAS_F32 != tag) u3m_bail(c3__fail); + *(c3_w*)valptrs[i] = u3r_word(0, value); + break; + } + case c_m3Type_f64: { + if (TAS_F64 != tag) u3m_bail(c3__fail); + *(c3_d*)valptrs[i] = u3r_chub(0, value); + break; + } + default: u3m_bail(c3__fail); + } + } +} + +// RETAIN args, TRANSFER result +static u3_noun +_reduce_monad(u3_noun monad, lia_state* sat) { + u3_noun monad_bat = u3h(monad); + if (c3y == u3r_sing(monad_bat, sat->match->call_bat)) { + if (c3n == u3r_sing(u3at(63, monad), sat->match->call_ctx)) { + return u3m_bail(c3__fail); + } + // call + u3_noun name = u3at(124, monad); + u3_noun args = u3at(125, monad); + c3_c* name_c = u3r_string(name); + M3Result result; + + IM3Function f; + result = m3_FindFunction(&f, sat->wasm_module->runtime, name_c); + + if (result) return u3m_bail(c3__fail); + + c3_w n_in = f->funcType->numArgs; + c3_w n_out = f->funcType->numRets; + c3_y* types = f->funcType->types; + const void **valptrs_in = u3a_calloc(n_in, sizeof(void*)); // TO FREE + const void **valptrs_out = u3a_calloc(n_out, sizeof(void*)); // TO FREE + + _atoms_to_stack(args, valptrs_in, n_in, (types+n_out)); + result = m3_Call(f, n_in, valptrs_in); + + if (result == m3Lia_Arrow) { + u3_noun yil = sat->arrow_yil; + sat->arrow_yil = 0; + if (yil == 0) return u3m_bail(c3__fail); + return yil; + } + else if (result) return u3m_bail(c3__fail); + + result = m3_GetResults(f, n_out, valptrs_out); + if (result) return u3m_bail(c3__fail); + + u3_noun out = _atoms_from_stack(valptrs_out, n_out, types); + c3_free(name_c); + + return u3nc(0, out); + + } + else if (c3y == u3r_sing(monad_bat, sat->match->memread_bat)) { + if (c3n == u3r_sing(u3at(63, monad), sat->match->memread_ctx)) { + return u3m_bail(c3__fail); + } + // memread + u3_noun ptr = u3at(124, monad); + u3_noun len = u3at(125, monad); + + c3_l ptr_l = (c3y == u3a_is_cat(ptr)) + ? ptr + : u3m_bail(c3__fail); + c3_l len_l = (c3y == u3a_is_cat(len)) + ? len + : u3m_bail(c3__fail); + c3_w len_buf_w; + c3_y* buf_y = m3_GetMemory(sat->wasm_module->runtime, &len_buf_w, 0); + + if ( (buf_y == NULL) || (ptr_l + len_l > len_buf_w) ) { + return u3m_bail(c3__fail); + } + + return u3nt(0, len_l, u3i_bytes(len_l, (buf_y + ptr_l))); + + } + else if (c3y == u3r_sing(monad_bat, sat->match->memwrite_bat)) { + if (c3n == u3r_sing(u3at(63, monad), sat->match->memwrite_ctx)) { + return u3m_bail(c3__fail); + } + // memwrite + u3_noun ptr = u3at(124, monad); + u3_noun len = u3at(250, monad); + u3_noun src = u3at(251, monad); + + c3_l ptr_l = (c3y == u3a_is_cat(ptr)) + ? ptr + : u3m_bail(c3__fail); + c3_l len_l = (c3y == u3a_is_cat(len)) + ? len + : u3m_bail(c3__fail); + c3_w len_buf_w; + c3_y* buf_y = m3_GetMemory(sat->wasm_module->runtime, &len_buf_w, 0); + + if ( (buf_y == NULL) || (ptr_l + len_l > len_buf_w) ) { + return u3m_bail(c3__fail); + } + + u3r_bytes(0, len_l, (buf_y + ptr_l), src); + + return u3nc(0, 0); + } + else if (c3y == u3r_sing(monad_bat, sat->match->call_ext_bat)) { + // call-ext + u3_noun name = u3at(124, monad); + u3_noun args = u3at(125, monad); + if (u3_nul == sat->lia_shop) { + return u3nt(1, u3k(name), u3k(args)); + } + else { + u3_noun lia_buy; + u3x_cell(sat->lia_shop, &lia_buy, &sat->lia_shop); + return u3nc(0, u3k(lia_buy)); + } + } + else if (c3y == u3r_sing(monad_bat, sat->match->try_bat)) { + // try + u3_noun monad_b = u3at(60, monad); + u3_noun cont = u3at(61, monad); + + while (1) { + u3_noun yil = _reduce_monad(monad_b, sat); // TO LOSE + if (0 != u3h(yil)) { + return yil; + } + u3_noun monad_c = u3n_slam_on(u3k(cont), u3k(u3t(yil))); + if (c3y == u3r_sing(u3h(monad_c), sat->match->try_bat)) { + monad_b = u3at(60, monad_c); + cont = u3at(61, monad_c); + } + else { + return _reduce_monad(monad_c, sat); + } + } + } + else if (c3y == u3r_sing(monad_bat, sat->match->catch_bat)) { + // catch + u3_noun monad_try = u3at(120, monad); + u3_noun monad_catch = u3at(121, monad); + u3_noun cont = u3at(61, monad); + + while (1) { + u3_noun yil_try = _reduce_monad(monad_try, sat); // TO LOSE + if (0 == u3h(yil_try)) { + u3_noun monad_c = u3n_slam_on(u3k(cont), u3k(u3t(yil_try))); + if (c3y == u3r_sing(u3h(monad_c), sat->match->catch_bat)) { + monad_try = u3at(120, monad_c); + monad_catch = u3at(121, monad_c); + cont = u3at(61, monad_c); + } + else { + return _reduce_monad(monad_c, sat); + } + } + else if (1 == u3h(yil_try)) { + return yil_try; + } + else { + u3_noun yil_catch = _reduce_monad(monad_catch, sat); + if (0 != u3h(yil_catch)) { + return yil_catch; + } + u3_noun monad_c = u3n_slam_on(u3k(cont), u3k(u3t(yil_catch))); + if (c3y == u3r_sing(u3h(monad_c), sat->match->catch_bat)) { + monad_try = u3at(120, monad_c); + monad_catch = u3at(121, monad_c); + cont = u3at(61, monad_c); + } + else { + _reduce_monad(monad_c, sat); + } + } + } + } + else if (c3y == u3r_sing(monad_bat, sat->match->return_bat)) { + // return + return u3nc(0, u3at(30, monad)); + } + else { + return u3m_bail(c3__fail); + } +} + +static const void * +_link_wasm_with_arrow_map( + IM3Runtime king_runtime, + IM3ImportContext _ctx, + uint64_t * _sp, + void * _mem +) { + const char *mod = _ctx->function->import.moduleUtf8; + const char *name = _ctx->function->import.fieldUtf8; + lia_state* sat = _ctx->userdata; + + M3Result result; + + u3_noun key = u3nc(u3i_string(mod), u3i_string(name)); + u3_noun arrow = u3kdb_got(sat->import, key); + c3_w n_in = _ctx->function->funcType->numArgs; + c3_w n_out = _ctx->function->funcType->numRets; + c3_y* types = _ctx->function->funcType->types; + const void **valptrs_in = u3a_calloc(n_in, sizeof(void*)); // TO FREE + for (c3_w i = 0; i < n_in; i++) { + valptrs_in[i] = &_sp[i+n_out]; + } + const void **valptrs_out = u3a_calloc(n_out, sizeof(void*)); // TO FREE + for (c3_w i = 0; i < n_out; i++) { + valptrs_out[i] = &_sp[i]; + } + + u3_noun coin_wasm_list = _coins_from_stack(valptrs_in, n_in, (types+n_out)); + + u3_noun yil = _reduce_monad(u3n_slam_on(arrow, coin_wasm_list), sat); // TO LOSE + + if (0 != u3h(yil)) { + sat->arrow_yil = yil; + return m3Lia_Arrow; + } else { + _coins_to_stack(u3t(yil), valptrs_out, n_out, types); + return m3Err_none; + } +} + u3_weak u3we_lia_run(u3_noun cor) { if (c3__none == u3x_at(u3x_sam_7, cor)) { return u3_none; } + + u3_noun input = u3x_at(u3x_sam_2, cor); + u3_noun seed = u3x_at(u3x_sam_6, cor); - // prepare batteries and contexts for comparisons - // update seed (stateless phase) - // parse wasm3 - // TODO validate wasm3 - // instantiate wasm3 - // statefully reduce the monad by matching - // the batteries and checking the contexts if necessary (call, memread, memwrite) - // return the final yield and the updated seed - u3_noun tar_mold = u3nt(u3nq(8, u3nc(0, 6), 8, u3nt(u3nq(5, u3nc(0, 14), 0, 2), 0, 6)), 0, 0); // *:~ - return u3_none; + u3_noun runnable = u3j_kink(u3k(u3at(7, cor)), 372); + u3_noun try_gate = u3j_kink(u3k(runnable), 21); + + u3_noun seed_new; // TO RETURN + u3_noun input_tag = u3h(input); + if (input_tag == c3y) { + u3_noun tar_mold = u3nt( + u3nq(8, u3nc(0, 6), 8, u3nt(u3nq(5, u3nc(0, 14), 0, 2), 0, 6)), + 0, 0 + ); // *:~ + u3_noun p_input_gate = u3nt(u3nc(0, 7), 0, u3k(u3t(input))); // =>(p.input |=(* +>)) + u3_noun past_new = u3n_slam_on( + u3n_slam_on(u3k(try_gate), tar_mold), + u3nc( + u3k(u3x_at(6, seed)), + p_input_gate + ) + ); + seed_new = u3nq( + u3k(u3x_at(2, seed)), + past_new, + u3k(u3x_at(14, seed)), + u3k(u3x_at(15, seed)) + ); + } + else if (input_tag == c3n) { + seed_new = u3nq( + u3k(u3x_at(2, seed)), + u3x_at(6, seed), + u3kb_weld( + u3k(u3x_at(14, seed)), + u3nc(u3k(u3t(input)), u3_nul) + ), + u3k(u3x_at(15, seed)) + ); + } + else { + return u3m_bail(c3__fail); + } + + u3_noun call_script = u3j_kink(u3j_kink(u3k(u3at(7, cor)), 20), 2); // TO LOSE // sample @ 62 + u3_noun memread_script = u3j_kink(u3j_kink(u3k(u3at(7, cor)), 374), 2); // TO LOSE // sample @ 62 + u3_noun memwrite_script = u3j_kink(u3j_kink(u3k(u3at(7, cor)), 92), 2); // TO LOSE // sample @ 62 + u3_noun call_ext_script = u3j_kink(u3j_kink(u3k(u3at(7, cor)), 2986), 2); // TO LOSE // sample @ 62 + u3_noun try_script = u3j_kink(u3j_kink(try_gate, 2), 2); // TO LOSE // sample @ 30 + u3_noun catch_script = u3j_kink(u3j_kink(u3j_kink(u3k(runnable), 4), 2), 2); // TO LOSE // sample @ 30 + u3_noun return_script = u3j_kink(u3j_kink(runnable, 20), 2); // TO LOSE // sample @ 30 + + u3_noun call_bat = u3h(call_script); + u3_noun memread_bat = u3h(memread_script); + u3_noun memwrite_bat = u3h(memwrite_script); + u3_noun call_ext_bat = u3h(call_ext_script); + u3_noun try_bat = u3h(try_script); + u3_noun catch_bat = u3h(catch_script); + u3_noun return_bat = u3h(return_script); + + u3_noun call_ctx = u3at(63, call_script); + u3_noun memread_ctx = u3at(63, memread_script); + u3_noun memwrite_ctx = u3at(63, memwrite_script); + + match_data_struct match = { + call_bat, + memread_bat, + memwrite_bat, + call_ext_bat, + try_bat, + catch_bat, + return_bat, + call_ctx, + memread_ctx, + memwrite_ctx + }; + + u3_noun octs = u3x_at(2, seed); + u3_noun p_octs, q_octs; + u3x_cell(octs, &p_octs, &q_octs); + + c3_w bin_len_w = (c3y == u3a_is_cat(p_octs)) ? p_octs : u3m_bail(c3__fail); + c3_y* bin_y = u3r_bytes_alloc(0, bin_len_w, u3x_atom(q_octs)); + + m3_SetAllocators(u3a_calloc, u3a_free, u3a_realloc); + + IM3Environment wasm3_env = m3_NewEnvironment(); + if (!wasm3_env) { + fprintf(stderr, "env is null\r\n"); + return u3m_bail(c3__fail); + } + M3Result result; + + IM3Runtime wasm3_runtime = m3_NewRuntime(wasm3_env, 2097152, NULL); + if (!wasm3_runtime) { + fprintf(stderr, "runtime is null\r\n"); + return u3m_bail(c3__fail); + } + + IM3Module wasm3_module; + result = m3_ParseModule(wasm3_env, + &wasm3_module, + bin_y, + bin_len_w); + if (result) { + fprintf(stderr, "parse binary error: %s\r\n", result); + return u3m_bail(c3__fail); + } + // TODO validate + result = m3_LoadModule(wasm3_runtime, wasm3_module); + if (result) { + fprintf(stderr, "load module error: %s\r\n", result); + return u3m_bail(c3__fail); + } + + c3_w n_imports = wasm3_module->numFuncImports; + u3_noun monad = u3x_at(6, seed); + u3_noun lia_shop = u3x_at(14, seed); + u3_noun import = u3x_at(15, seed); + + lia_state sat = {wasm3_module, lia_shop, import, &match, 0}; + + for (c3_w i = 0; i < n_imports; i++) { + M3Function f = wasm3_module->functions[i]; + const char * mod = f.import.moduleUtf8; + const char * name = f.import.fieldUtf8; + result = m3_LinkRawFunctionEx(wasm3_module, + mod, name, NULL, + &_link_wasm_with_arrow_map, + (void *)&sat); + if (result) { + fprintf(stderr, "link error"); + return u3m_bail(c3__fail); + } + } + + u3_noun yil; + yil = _reduce_monad(monad, &sat); + return u3nc(yil, seed_new); } From 11dbc050c9600f3583261e98970f49a5545248d5 Mon Sep 17 00:00:00 2001 From: Quodss Date: Thu, 14 Nov 2024 17:32:11 +0100 Subject: [PATCH 03/22] jet works in 3.0 but crashes in 3.2 --- pkg/noun/jets/e/urwasm.c | 229 +++++++++++++++++++-------------------- 1 file changed, 111 insertions(+), 118 deletions(-) diff --git a/pkg/noun/jets/e/urwasm.c b/pkg/noun/jets/e/urwasm.c index ac92d4c656..d47ebf1487 100644 --- a/pkg/noun/jets/e/urwasm.c +++ b/pkg/noun/jets/e/urwasm.c @@ -10,10 +10,10 @@ #include "m3_env.h" -const c3_w TAS_I32 = c3_s3('i','3','2'); -const c3_w TAS_I64 = c3_s3('i','6','4'); -const c3_w TAS_F32 = c3_s3('f','3','2'); -const c3_w TAS_F64 = c3_s3('f','6','4'); +const c3_w c3__i32 = c3_s3('i','3','2'); +const c3_w c3__i64 = c3_s3('i','6','4'); +const c3_w c3__f32 = c3_s3('f','3','2'); +const c3_w c3__f64 = c3_s3('f','6','4'); const M3Result m3Lia_Arrow = "non-zero yield from import arrow"; @@ -47,30 +47,22 @@ _atoms_from_stack(const void** valptrs, c3_w n, c3_y* types) { switch (types[n]) { // TODO 64 bit vere case c_m3Type_i32: { out = u3nc( - u3i_word(*(c3_w*)valptrs[n]), - out - ); + u3i_word(*(c3_w*)valptrs[n]), out); break; } case c_m3Type_i64: { out = u3nc( - u3i_chub(*(c3_d*)valptrs[n]), - out - ); + u3i_chub(*(c3_d*)valptrs[n]), out); break; } case c_m3Type_f32: { out = u3nc( - u3i_word(*(c3_w*)valptrs[n]), - out - ); + u3i_word(*(c3_w*)valptrs[n]), out); break; } case c_m3Type_f64: { out = u3nc( - u3i_chub(*(c3_d*)valptrs[n]), - out - ); + u3i_chub(*(c3_d*)valptrs[n]), out); break; } default: return u3m_bail(c3__fail); @@ -92,7 +84,10 @@ _atoms_to_stack( u3m_bail(c3__fail); } u3_noun atom; - u3r_cell(atoms, &atom, &atoms); + u3x_cell(atoms, &atom, &atoms); + if (c3n == u3ud(atom)) { + u3m_bail(c3__fail); + } switch (types[i]) { case c_m3Type_i32: { *(c3_w*)valptrs[i] = u3r_word(0, atom); @@ -124,31 +119,19 @@ _coins_from_stack(const void** valptrs, c3_w n, c3_y* types) { n--; switch (types[n]) { // TODO 64 bit vere case c_m3Type_i32: { - out = u3nc( - u3nc(TAS_I32, u3i_word(*(c3_w*)valptrs[n])), - out - ); + out = u3nc(u3nc(c3__i32, u3i_word(*(c3_w*)valptrs[n])), out); break; } case c_m3Type_i64: { - out = u3nc( - u3nc(TAS_I64, u3i_chub(*(c3_d*)valptrs[n])), - out - ); + out = u3nc(u3nc(c3__i64, u3i_chub(*(c3_d*)valptrs[n])), out); break; } case c_m3Type_f32: { - out = u3nc( - u3nc(TAS_F32, u3i_word(*(c3_w*)valptrs[n])), - out - ); + out = u3nc(u3nc(c3__f32, u3i_word(*(c3_w*)valptrs[n])), out); break; } case c_m3Type_f64: { - out = u3nc( - u3nc(TAS_F64, u3i_chub(*(c3_d*)valptrs[n])), - out - ); + out = u3nc(u3nc(c3__f64, u3i_chub(*(c3_d*)valptrs[n])), out); break; } default: return u3m_bail(c3__fail); @@ -170,39 +153,40 @@ _coins_to_stack( u3m_bail(c3__fail); } u3_noun coin; - u3r_cell(coins, &coin, &coins); + u3x_cell(coins, &coin, &coins); if (c3y == u3ud(coin)) { u3m_bail(c3__fail); } u3_noun tag, value; - u3r_cell(coin, &tag, &value); + u3x_cell(coin, &tag, &value); if (c3n == u3ud(value)) { u3m_bail(c3__fail); } switch (types[i]) { case c_m3Type_i32: { - if (TAS_I32 != tag) u3m_bail(c3__fail); + if (c3__i32 != tag) u3m_bail(c3__fail); *(c3_w*)valptrs[i] = u3r_word(0, value); break; } case c_m3Type_i64: { - if (TAS_I64 != tag) u3m_bail(c3__fail); + if (c3__i64 != tag) u3m_bail(c3__fail); *(c3_d*)valptrs[i] = u3r_chub(0, value); break; } case c_m3Type_f32: { - if (TAS_F32 != tag) u3m_bail(c3__fail); + if (c3__f32 != tag) u3m_bail(c3__fail); *(c3_w*)valptrs[i] = u3r_word(0, value); break; } case c_m3Type_f64: { - if (TAS_F64 != tag) u3m_bail(c3__fail); + if (c3__f64 != tag) u3m_bail(c3__fail); *(c3_d*)valptrs[i] = u3r_chub(0, value); break; } default: u3m_bail(c3__fail); } } + if (u3_nul != coins) u3m_bail(c3__fail); } // RETAIN args, TRANSFER result @@ -216,6 +200,7 @@ _reduce_monad(u3_noun monad, lia_state* sat) { // call u3_noun name = u3at(124, monad); u3_noun args = u3at(125, monad); + c3_c* name_c = u3r_string(name); M3Result result; @@ -227,11 +212,22 @@ _reduce_monad(u3_noun monad, lia_state* sat) { c3_w n_in = f->funcType->numArgs; c3_w n_out = f->funcType->numRets; c3_y* types = f->funcType->types; + + const c3_d *vals_in = u3a_calloc(n_in, sizeof(c3_d)); // TO FREE const void **valptrs_in = u3a_calloc(n_in, sizeof(void*)); // TO FREE + for (c3_w i = 0; i < n_in; i++) valptrs_in[i] = &vals_in[i]; + + const c3_d *vals_out = u3a_calloc(n_out, sizeof(c3_d)); // TO FREE const void **valptrs_out = u3a_calloc(n_out, sizeof(void*)); // TO FREE + for (c3_w i = 0; i < n_out; i++) valptrs_out[i] = &vals_out[i]; _atoms_to_stack(args, valptrs_in, n_in, (types+n_out)); - result = m3_Call(f, n_in, valptrs_in); + + fprintf(stderr, "\r\nprepared call: %s\r\n", name_c); + + result = m3_Call(f, n_in, valptrs_in); // break pkg/noun/jets/e/urwasm.c:228 + + fprintf(stderr, "\r\ncall ended\r\n"); if (result == m3Lia_Arrow) { u3_noun yil = sat->arrow_yil; @@ -239,11 +235,16 @@ _reduce_monad(u3_noun monad, lia_state* sat) { if (yil == 0) return u3m_bail(c3__fail); return yil; } - else if (result) return u3m_bail(c3__fail); + else if (result) { + fprintf(stderr, "\r\ncall failed: %s \r\n", name_c); + return u3m_bail(c3__fail); + } result = m3_GetResults(f, n_out, valptrs_out); if (result) return u3m_bail(c3__fail); + fprintf(stderr, "\r\ngetting results\r\n"); + u3_noun out = _atoms_from_stack(valptrs_out, n_out, types); c3_free(name_c); @@ -258,12 +259,15 @@ _reduce_monad(u3_noun monad, lia_state* sat) { u3_noun ptr = u3at(124, monad); u3_noun len = u3at(125, monad); + u3m_p("read ptr", ptr); + u3m_p("len", len); + c3_l ptr_l = (c3y == u3a_is_cat(ptr)) - ? ptr - : u3m_bail(c3__fail); + ? ptr + : u3m_bail(c3__fail); c3_l len_l = (c3y == u3a_is_cat(len)) - ? len - : u3m_bail(c3__fail); + ? len + : u3m_bail(c3__fail); c3_w len_buf_w; c3_y* buf_y = m3_GetMemory(sat->wasm_module->runtime, &len_buf_w, 0); @@ -284,11 +288,11 @@ _reduce_monad(u3_noun monad, lia_state* sat) { u3_noun src = u3at(251, monad); c3_l ptr_l = (c3y == u3a_is_cat(ptr)) - ? ptr - : u3m_bail(c3__fail); + ? ptr + : u3m_bail(c3__fail); c3_l len_l = (c3y == u3a_is_cat(len)) - ? len - : u3m_bail(c3__fail); + ? len + : u3m_bail(c3__fail); c3_w len_buf_w; c3_y* buf_y = m3_GetMemory(sat->wasm_module->runtime, &len_buf_w, 0); @@ -296,7 +300,7 @@ _reduce_monad(u3_noun monad, lia_state* sat) { return u3m_bail(c3__fail); } - u3r_bytes(0, len_l, (buf_y + ptr_l), src); + u3r_bytes(0, len_l, (buf_y + ptr_l), u3x_atom(src)); return u3nc(0, 0); } @@ -318,20 +322,12 @@ _reduce_monad(u3_noun monad, lia_state* sat) { u3_noun monad_b = u3at(60, monad); u3_noun cont = u3at(61, monad); - while (1) { - u3_noun yil = _reduce_monad(monad_b, sat); // TO LOSE - if (0 != u3h(yil)) { - return yil; - } - u3_noun monad_c = u3n_slam_on(u3k(cont), u3k(u3t(yil))); - if (c3y == u3r_sing(u3h(monad_c), sat->match->try_bat)) { - monad_b = u3at(60, monad_c); - cont = u3at(61, monad_c); - } - else { - return _reduce_monad(monad_c, sat); - } + u3_noun yil = _reduce_monad(monad_b, sat); // TO LOSE + if (0 != u3h(yil)) { + return yil; } + u3_noun monad_cont = u3n_slam_on(u3k(cont), u3k(u3t(yil))); + return _reduce_monad(monad_cont, sat); } else if (c3y == u3r_sing(monad_bat, sat->match->catch_bat)) { // catch @@ -339,37 +335,21 @@ _reduce_monad(u3_noun monad, lia_state* sat) { u3_noun monad_catch = u3at(121, monad); u3_noun cont = u3at(61, monad); - while (1) { - u3_noun yil_try = _reduce_monad(monad_try, sat); // TO LOSE - if (0 == u3h(yil_try)) { - u3_noun monad_c = u3n_slam_on(u3k(cont), u3k(u3t(yil_try))); - if (c3y == u3r_sing(u3h(monad_c), sat->match->catch_bat)) { - monad_try = u3at(120, monad_c); - monad_catch = u3at(121, monad_c); - cont = u3at(61, monad_c); - } - else { - return _reduce_monad(monad_c, sat); - } - } - else if (1 == u3h(yil_try)) { - return yil_try; - } - else { - u3_noun yil_catch = _reduce_monad(monad_catch, sat); - if (0 != u3h(yil_catch)) { - return yil_catch; - } - u3_noun monad_c = u3n_slam_on(u3k(cont), u3k(u3t(yil_catch))); - if (c3y == u3r_sing(u3h(monad_c), sat->match->catch_bat)) { - monad_try = u3at(120, monad_c); - monad_catch = u3at(121, monad_c); - cont = u3at(61, monad_c); - } - else { - _reduce_monad(monad_c, sat); - } + u3_noun yil_try = _reduce_monad(monad_try, sat); // TO LOSE + if (0 == u3h(yil_try)) { + u3_noun monad_cont = u3n_slam_on(u3k(cont), u3k(u3t(yil_try))); + return _reduce_monad(monad_cont, sat); + } + else if (1 == u3h(yil_try)) { + return yil_try; + } + else { + u3_noun yil_catch = _reduce_monad(monad_catch, sat); + if (0 != u3h(yil_catch)) { + return yil_catch; } + u3_noun monad_cont = u3n_slam_on(u3k(cont), u3k(u3t(yil_catch))); + return _reduce_monad(monad_cont, sat); } } else if (c3y == u3r_sing(monad_bat, sat->match->return_bat)) { @@ -395,7 +375,7 @@ _link_wasm_with_arrow_map( M3Result result; u3_noun key = u3nc(u3i_string(mod), u3i_string(name)); - u3_noun arrow = u3kdb_got(sat->import, key); + u3_noun arrow = u3kdb_got(u3k(sat->import), key); c3_w n_in = _ctx->function->funcType->numArgs; c3_w n_out = _ctx->function->funcType->numRets; c3_y* types = _ctx->function->funcType->types; @@ -424,9 +404,14 @@ _link_wasm_with_arrow_map( u3_weak u3we_lia_run(u3_noun cor) { + fprintf(stderr, "\r\njet entry\r\n"); + if (c3__none == u3x_at(u3x_sam_7, cor)) { return u3_none; } + + // enter subroad, 4MB safety buffer + // u3m_hate(1 << 20); u3_noun input = u3x_at(u3x_sam_2, cor); u3_noun seed = u3x_at(u3x_sam_6, cor); @@ -435,15 +420,13 @@ u3we_lia_run(u3_noun cor) u3_noun try_gate = u3j_kink(u3k(runnable), 21); u3_noun seed_new; // TO RETURN - u3_noun input_tag = u3h(input); + u3_noun input_tag, p_input; + u3x_cell(input, &input_tag, &p_input); + if (input_tag == c3y) { - u3_noun tar_mold = u3nt( - u3nq(8, u3nc(0, 6), 8, u3nt(u3nq(5, u3nc(0, 14), 0, 2), 0, 6)), - 0, 0 - ); // *:~ - u3_noun p_input_gate = u3nt(u3nc(0, 7), 0, u3k(u3t(input))); // =>(p.input |=(* +>)) + u3_noun p_input_gate = u3nt(u3nc(0, 7), 0, u3k(p_input)); // =>(p.input |=(* +>)) u3_noun past_new = u3n_slam_on( - u3n_slam_on(u3k(try_gate), tar_mold), + u3j_kink(u3k(try_gate), 2), u3nc( u3k(u3x_at(6, seed)), p_input_gate @@ -462,7 +445,7 @@ u3we_lia_run(u3_noun cor) u3x_at(6, seed), u3kb_weld( u3k(u3x_at(14, seed)), - u3nc(u3k(u3t(input)), u3_nul) + u3nc(u3k(p_input), u3_nul) ), u3k(u3x_at(15, seed)) ); @@ -471,13 +454,13 @@ u3we_lia_run(u3_noun cor) return u3m_bail(c3__fail); } - u3_noun call_script = u3j_kink(u3j_kink(u3k(u3at(7, cor)), 20), 2); // TO LOSE // sample @ 62 - u3_noun memread_script = u3j_kink(u3j_kink(u3k(u3at(7, cor)), 374), 2); // TO LOSE // sample @ 62 - u3_noun memwrite_script = u3j_kink(u3j_kink(u3k(u3at(7, cor)), 92), 2); // TO LOSE // sample @ 62 - u3_noun call_ext_script = u3j_kink(u3j_kink(u3k(u3at(7, cor)), 2986), 2); // TO LOSE // sample @ 62 - u3_noun try_script = u3j_kink(u3j_kink(try_gate, 2), 2); // TO LOSE // sample @ 30 - u3_noun catch_script = u3j_kink(u3j_kink(u3j_kink(u3k(runnable), 4), 2), 2); // TO LOSE // sample @ 30 - u3_noun return_script = u3j_kink(u3j_kink(runnable, 20), 2); // TO LOSE // sample @ 30 + u3_noun call_script = u3j_kink(u3j_kink(u3k(u3at(7, cor)), 20), 2); // TO LOSE + u3_noun memread_script = u3j_kink(u3j_kink(u3k(u3at(7, cor)), 374), 2); // TO LOSE + u3_noun memwrite_script = u3j_kink(u3j_kink(u3k(u3at(7, cor)), 92), 2); // TO LOSE + u3_noun call_ext_script = u3j_kink(u3j_kink(u3k(u3at(7, cor)), 2986), 2); // TO LOSE + u3_noun try_script = u3j_kink(u3j_kink(try_gate, 2), 2); // TO LOSE + u3_noun catch_script = u3j_kink(u3j_kink(u3j_kink(u3k(runnable), 4), 2), 2); // TO LOSE + u3_noun return_script = u3j_kink(u3j_kink(runnable, 20), 2); // TO LOSE u3_noun call_bat = u3h(call_script); u3_noun memread_bat = u3h(memread_script); @@ -504,7 +487,7 @@ u3we_lia_run(u3_noun cor) memwrite_ctx }; - u3_noun octs = u3x_at(2, seed); + u3_noun octs = u3x_at(2, seed_new); u3_noun p_octs, q_octs; u3x_cell(octs, &p_octs, &q_octs); @@ -519,8 +502,10 @@ u3we_lia_run(u3_noun cor) return u3m_bail(c3__fail); } M3Result result; - - IM3Runtime wasm3_runtime = m3_NewRuntime(wasm3_env, 2097152, NULL); + + fprintf(stderr, "\r\nstart initialization\r\n"); + + IM3Runtime wasm3_runtime = m3_NewRuntime(wasm3_env, 1 << 13, NULL); if (!wasm3_runtime) { fprintf(stderr, "runtime is null\r\n"); return u3m_bail(c3__fail); @@ -535,7 +520,9 @@ u3we_lia_run(u3_noun cor) fprintf(stderr, "parse binary error: %s\r\n", result); return u3m_bail(c3__fail); } + // TODO validate + result = m3_LoadModule(wasm3_runtime, wasm3_module); if (result) { fprintf(stderr, "load module error: %s\r\n", result); @@ -543,9 +530,9 @@ u3we_lia_run(u3_noun cor) } c3_w n_imports = wasm3_module->numFuncImports; - u3_noun monad = u3x_at(6, seed); - u3_noun lia_shop = u3x_at(14, seed); - u3_noun import = u3x_at(15, seed); + u3_noun monad = u3x_at(6, seed_new); + u3_noun lia_shop = u3x_at(14, seed_new); + u3_noun import = u3x_at(15, seed_new); lia_state sat = {wasm3_module, lia_shop, import, &match, 0}; @@ -562,8 +549,14 @@ u3we_lia_run(u3_noun cor) return u3m_bail(c3__fail); } } + fprintf(stderr, "\r\ninitialized state\r\n"); + + u3_noun yil = _reduce_monad(monad, &sat); + + + // // exit subroad, copying the result + // u3_noun pro = u3m_love(u3nc(yil, seed_new)); + u3_noun pro = u3nc(yil, seed_new); - u3_noun yil; - yil = _reduce_monad(monad, &sat); - return u3nc(yil, seed_new); + return pro; } From 54e6ff2705f8d33b0a461e6ba03080c33713dd8d Mon Sep 17 00:00:00 2001 From: Quodss Date: Thu, 14 Nov 2024 18:04:09 +0100 Subject: [PATCH 04/22] no sanitize --- ext/wasm3/build.zig | 1 + 1 file changed, 1 insertion(+) diff --git a/ext/wasm3/build.zig b/ext/wasm3/build.zig index ef39defc01..8393f82927 100644 --- a/ext/wasm3/build.zig +++ b/ext/wasm3/build.zig @@ -34,6 +34,7 @@ pub fn build(b: *std.Build) void { "-Wpointer-arith", "-Wstrict-aliasing=2", "-Werror=implicit-function-declaration", + "-fno-sanitize=all", }; const mac_flags = common_flags ++ [_][]const u8{ From 239d4b99fefce26c6ee03cc50c1d172e7c542d78 Mon Sep 17 00:00:00 2001 From: Quodss Date: Thu, 14 Nov 2024 19:44:55 +0100 Subject: [PATCH 05/22] flags, cleanup --- ext/wasm3/build.zig | 2 ++ ext/wasm3/build.zig.zon | 2 +- pkg/noun/jets/e/urwasm.c | 14 -------------- 3 files changed, 3 insertions(+), 15 deletions(-) diff --git a/ext/wasm3/build.zig b/ext/wasm3/build.zig index 8393f82927..d8db34a6ce 100644 --- a/ext/wasm3/build.zig +++ b/ext/wasm3/build.zig @@ -35,6 +35,8 @@ pub fn build(b: *std.Build) void { "-Wstrict-aliasing=2", "-Werror=implicit-function-declaration", "-fno-sanitize=all", + // "-fsanitize=undefined", + // "-fno-sanitize-trap=undefined", }; const mac_flags = common_flags ++ [_][]const u8{ diff --git a/ext/wasm3/build.zig.zon b/ext/wasm3/build.zig.zon index edc404d56a..51e7849f58 100644 --- a/ext/wasm3/build.zig.zon +++ b/ext/wasm3/build.zig.zon @@ -6,7 +6,7 @@ .path = "../softfloat", }, .wasm3 = .{ - .url = "https://github.com/Quodss/wasm3/archive/refs/heads/urwasm.tar.gz", + .url = "https://github.com/Quodss/wasm3/archive/004f1f654c9384074f79c5e0f8a7717aa0829289.tar.gz", .hash = "122051e4183102eb4653556d0bcbf0908203ebdaa2c105df181e63a569f05f702baa", }, }, diff --git a/pkg/noun/jets/e/urwasm.c b/pkg/noun/jets/e/urwasm.c index d47ebf1487..e272dfcac4 100644 --- a/pkg/noun/jets/e/urwasm.c +++ b/pkg/noun/jets/e/urwasm.c @@ -222,13 +222,9 @@ _reduce_monad(u3_noun monad, lia_state* sat) { for (c3_w i = 0; i < n_out; i++) valptrs_out[i] = &vals_out[i]; _atoms_to_stack(args, valptrs_in, n_in, (types+n_out)); - - fprintf(stderr, "\r\nprepared call: %s\r\n", name_c); result = m3_Call(f, n_in, valptrs_in); // break pkg/noun/jets/e/urwasm.c:228 - fprintf(stderr, "\r\ncall ended\r\n"); - if (result == m3Lia_Arrow) { u3_noun yil = sat->arrow_yil; sat->arrow_yil = 0; @@ -243,8 +239,6 @@ _reduce_monad(u3_noun monad, lia_state* sat) { result = m3_GetResults(f, n_out, valptrs_out); if (result) return u3m_bail(c3__fail); - fprintf(stderr, "\r\ngetting results\r\n"); - u3_noun out = _atoms_from_stack(valptrs_out, n_out, types); c3_free(name_c); @@ -259,9 +253,6 @@ _reduce_monad(u3_noun monad, lia_state* sat) { u3_noun ptr = u3at(124, monad); u3_noun len = u3at(125, monad); - u3m_p("read ptr", ptr); - u3m_p("len", len); - c3_l ptr_l = (c3y == u3a_is_cat(ptr)) ? ptr : u3m_bail(c3__fail); @@ -404,8 +395,6 @@ _link_wasm_with_arrow_map( u3_weak u3we_lia_run(u3_noun cor) { - fprintf(stderr, "\r\njet entry\r\n"); - if (c3__none == u3x_at(u3x_sam_7, cor)) { return u3_none; } @@ -503,8 +492,6 @@ u3we_lia_run(u3_noun cor) } M3Result result; - fprintf(stderr, "\r\nstart initialization\r\n"); - IM3Runtime wasm3_runtime = m3_NewRuntime(wasm3_env, 1 << 13, NULL); if (!wasm3_runtime) { fprintf(stderr, "runtime is null\r\n"); @@ -549,7 +536,6 @@ u3we_lia_run(u3_noun cor) return u3m_bail(c3__fail); } } - fprintf(stderr, "\r\ninitialized state\r\n"); u3_noun yil = _reduce_monad(monad, &sat); From 3e6ca966d9ffbb5efb1dfe8e5c86e40e661e63e2 Mon Sep 17 00:00:00 2001 From: Quodss Date: Fri, 15 Nov 2024 18:27:58 +0100 Subject: [PATCH 06/22] cleanup, subroads, proper allocations --- pkg/c3/motes.h | 4 + pkg/noun/jets/e/urwasm.c | 452 +++++++++++++++++++++++---------------- 2 files changed, 274 insertions(+), 182 deletions(-) diff --git a/pkg/c3/motes.h b/pkg/c3/motes.h index 7ae7ebcd75..86cdccbb20 100644 --- a/pkg/c3/motes.h +++ b/pkg/c3/motes.h @@ -402,6 +402,8 @@ # define c3__exit c3_s4('e','x','i','t') # define c3__eyre c3_s4('e','y','r','e') # define c3__f c3_s1('f') +# define c3__f32 c3_s3('f','3','2') +# define c3__f64 c3_s3('f','6','4') # define c3__fx c3_s2('f','x') # define c3__fab c3_s3('f','a','b') # define c3__face c3_s4('f','a','c','e') @@ -596,6 +598,8 @@ # define c3__hxtr c3_s4('h','x','t','r') # define c3__hxts c3_s4('h','x','t','s') # define c3__i c3_s1('i') +# define c3__i32 c3_s3('i','3','2') +# define c3__i64 c3_s3('i','6','4') # define c3__ic c3_s2('i','c') # define c3__ice c3_s3('i','c','e') # define c3__iced c3_s4('i','c','e','d') diff --git a/pkg/noun/jets/e/urwasm.c b/pkg/noun/jets/e/urwasm.c index e272dfcac4..2006f4eb60 100644 --- a/pkg/noun/jets/e/urwasm.c +++ b/pkg/noun/jets/e/urwasm.c @@ -9,13 +9,7 @@ #include "wasm3.h" #include "m3_env.h" - -const c3_w c3__i32 = c3_s3('i','3','2'); -const c3_w c3__i64 = c3_s3('i','6','4'); -const c3_w c3__f32 = c3_s3('f','3','2'); -const c3_w c3__f64 = c3_s3('f','6','4'); - -const M3Result m3Lia_Arrow = "non-zero yield from import arrow"; +static const M3Result m3Lia_Arrow = "non-zero yield from import arrow"; typedef struct { u3_noun call_bat; @@ -40,32 +34,38 @@ typedef struct { // TRANSFER result static u3_noun -_atoms_from_stack(const void** valptrs, c3_w n, c3_y* types) { +_atoms_from_stack(const void** valptrs, c3_w n, c3_y* types) +{ u3_noun out = u3_nul; - while (n) { + while (n) + { n--; - switch (types[n]) { // TODO 64 bit vere - case c_m3Type_i32: { - out = u3nc( - u3i_word(*(c3_w*)valptrs[n]), out); + switch (types[n]) + { // TODO 64 bit vere + case c_m3Type_i32: + { + out = u3nc(u3i_word(*(c3_w*)valptrs[n]), out); break; } - case c_m3Type_i64: { - out = u3nc( - u3i_chub(*(c3_d*)valptrs[n]), out); + case c_m3Type_i64: + { + out = u3nc(u3i_chub(*(c3_d*)valptrs[n]), out); break; } - case c_m3Type_f32: { - out = u3nc( - u3i_word(*(c3_w*)valptrs[n]), out); + case c_m3Type_f32: + { + out = u3nc(u3i_word(*(c3_w*)valptrs[n]), out); break; } - case c_m3Type_f64: { - out = u3nc( - u3i_chub(*(c3_d*)valptrs[n]), out); + case c_m3Type_f64: + { + out = u3nc(u3i_chub(*(c3_d*)valptrs[n]), out); break; } - default: return u3m_bail(c3__fail); + default: + { + return u3m_bail(c3__fail); + } } } return out; @@ -73,68 +73,88 @@ _atoms_from_stack(const void** valptrs, c3_w n, c3_y* types) { // RETAIN coins static void -_atoms_to_stack( - u3_noun atoms, - const void** valptrs, - c3_w n, - c3_y* types -) { - for (c3_w i = 0; i < n; i++) { - if (c3y == u3ud(atoms)) { +_atoms_to_stack(u3_noun atoms, const void** valptrs, c3_w n, c3_y* types) +{ + for (c3_w i = 0; i < n; i++) + { + if (c3y == u3ud(atoms)) + { u3m_bail(c3__fail); } u3_noun atom; u3x_cell(atoms, &atom, &atoms); - if (c3n == u3ud(atom)) { + if (c3n == u3ud(atom)) + { u3m_bail(c3__fail); } - switch (types[i]) { - case c_m3Type_i32: { + switch (types[i]) + { + case c_m3Type_i32: + { *(c3_w*)valptrs[i] = u3r_word(0, atom); break; } - case c_m3Type_i64: { + case c_m3Type_i64: + { *(c3_d*)valptrs[i] = u3r_chub(0, atom); break; } - case c_m3Type_f32: { + case c_m3Type_f32: + { *(c3_w*)valptrs[i] = u3r_word(0, atom); break; } - case c_m3Type_f64: { + case c_m3Type_f64: + { *(c3_d*)valptrs[i] = u3r_chub(0, atom); break; } - default: u3m_bail(c3__fail); + default: + { + u3m_bail(c3__fail); + } } } - if (u3_nul != atoms) u3m_bail(c3__fail); + if (u3_nul != atoms) + { + u3m_bail(c3__fail); + } } // TRANSFER result static u3_noun -_coins_from_stack(const void** valptrs, c3_w n, c3_y* types) { +_coins_from_stack(const void** valptrs, c3_w n, c3_y* types) +{ u3_noun out = u3_nul; - while (n) { + while (n) + { n--; - switch (types[n]) { // TODO 64 bit vere - case c_m3Type_i32: { + switch (types[n]) + { // TODO 64 bit vere + case c_m3Type_i32: + { out = u3nc(u3nc(c3__i32, u3i_word(*(c3_w*)valptrs[n])), out); break; } - case c_m3Type_i64: { + case c_m3Type_i64: + { out = u3nc(u3nc(c3__i64, u3i_chub(*(c3_d*)valptrs[n])), out); break; } - case c_m3Type_f32: { + case c_m3Type_f32: + { out = u3nc(u3nc(c3__f32, u3i_word(*(c3_w*)valptrs[n])), out); break; } - case c_m3Type_f64: { + case c_m3Type_f64: + { out = u3nc(u3nc(c3__f64, u3i_chub(*(c3_d*)valptrs[n])), out); break; } - default: return u3m_bail(c3__fail); + default: + { + return u3m_bail(c3__fail); + } } } return out; @@ -142,135 +162,177 @@ _coins_from_stack(const void** valptrs, c3_w n, c3_y* types) { // RETAIN coins static void -_coins_to_stack( - u3_noun coins, - const void** valptrs, - c3_w n, - c3_y* types -) { - for (c3_w i = 0; i < n; i++) { - if (c3y == u3ud(coins)) { +_coins_to_stack(u3_noun coins, const void** valptrs, c3_w n, c3_y* types) +{ + for (c3_w i = 0; i < n; i++) + { + if (c3y == u3ud(coins)) + { u3m_bail(c3__fail); } u3_noun coin; u3x_cell(coins, &coin, &coins); - if (c3y == u3ud(coin)) { + if (c3y == u3ud(coin)) + { u3m_bail(c3__fail); } u3_noun tag, value; u3x_cell(coin, &tag, &value); - if (c3n == u3ud(value)) { + if (c3n == u3ud(value)) + { u3m_bail(c3__fail); } - switch (types[i]) { - case c_m3Type_i32: { - if (c3__i32 != tag) u3m_bail(c3__fail); + switch (types[i]) + { + case c_m3Type_i32: + { + if (c3__i32 != tag) + { + u3m_bail(c3__fail); + } *(c3_w*)valptrs[i] = u3r_word(0, value); break; } - case c_m3Type_i64: { - if (c3__i64 != tag) u3m_bail(c3__fail); + case c_m3Type_i64: + { + if (c3__i64 != tag) + { + u3m_bail(c3__fail); + } *(c3_d*)valptrs[i] = u3r_chub(0, value); break; } - case c_m3Type_f32: { - if (c3__f32 != tag) u3m_bail(c3__fail); + case c_m3Type_f32: + { + if (c3__f32 != tag) + { + u3m_bail(c3__fail); + } *(c3_w*)valptrs[i] = u3r_word(0, value); break; } - case c_m3Type_f64: { - if (c3__f64 != tag) u3m_bail(c3__fail); + case c_m3Type_f64: + { + if (c3__f64 != tag) + { + u3m_bail(c3__fail); + } *(c3_d*)valptrs[i] = u3r_chub(0, value); break; } - default: u3m_bail(c3__fail); + default: + { + u3m_bail(c3__fail); + } } } - if (u3_nul != coins) u3m_bail(c3__fail); + if (u3_nul != coins) + { + u3m_bail(c3__fail); + } } // RETAIN args, TRANSFER result static u3_noun -_reduce_monad(u3_noun monad, lia_state* sat) { +_reduce_monad(u3_noun monad, lia_state* sat) +{ u3_noun monad_bat = u3h(monad); - if (c3y == u3r_sing(monad_bat, sat->match->call_bat)) { - if (c3n == u3r_sing(u3at(63, monad), sat->match->call_ctx)) { + if (c3y == u3r_sing(monad_bat, sat->match->call_bat)) + { + if (c3n == u3r_sing(u3at(63, monad), sat->match->call_ctx)) + { return u3m_bail(c3__fail); } // call - u3_noun name = u3at(124, monad); + u3_atom name = u3x_atom(u3at(124, monad)); u3_noun args = u3at(125, monad); - c3_c* name_c = u3r_string(name); + c3_w met_w = u3r_met(3, name); + c3_c* name_c = u3a_malloc(met_w + 1); + u3r_bytes(0, met_w, (c3_y*)name_c, name); + name_c[met_w] = 0; + M3Result result; IM3Function f; result = m3_FindFunction(&f, sat->wasm_module->runtime, name_c); - if (result) return u3m_bail(c3__fail); + if (result) + { + return u3m_bail(c3__fail); + } c3_w n_in = f->funcType->numArgs; c3_w n_out = f->funcType->numRets; c3_y* types = f->funcType->types; - const c3_d *vals_in = u3a_calloc(n_in, sizeof(c3_d)); // TO FREE - const void **valptrs_in = u3a_calloc(n_in, sizeof(void*)); // TO FREE - for (c3_w i = 0; i < n_in; i++) valptrs_in[i] = &vals_in[i]; + const c3_d *vals_in = u3a_calloc(n_in, sizeof(c3_d)); + const void **valptrs_in = u3a_calloc(n_in, sizeof(void*)); + for (c3_w i = 0; i < n_in; i++) + { + valptrs_in[i] = &vals_in[i]; + } - const c3_d *vals_out = u3a_calloc(n_out, sizeof(c3_d)); // TO FREE - const void **valptrs_out = u3a_calloc(n_out, sizeof(void*)); // TO FREE - for (c3_w i = 0; i < n_out; i++) valptrs_out[i] = &vals_out[i]; + const c3_d *vals_out = u3a_calloc(n_out, sizeof(c3_d)); + const void **valptrs_out = u3a_calloc(n_out, sizeof(void*)); + for (c3_w i = 0; i < n_out; i++) + { + valptrs_out[i] = &vals_out[i]; + } _atoms_to_stack(args, valptrs_in, n_in, (types+n_out)); - result = m3_Call(f, n_in, valptrs_in); // break pkg/noun/jets/e/urwasm.c:228 + result = m3_Call(f, n_in, valptrs_in); - if (result == m3Lia_Arrow) { + if (result == m3Lia_Arrow) + { u3_noun yil = sat->arrow_yil; sat->arrow_yil = 0; - if (yil == 0) return u3m_bail(c3__fail); + if (yil == 0) + { + return u3m_bail(c3__fail); + } return yil; } - else if (result) { + else if (result) + { fprintf(stderr, "\r\ncall failed: %s \r\n", name_c); return u3m_bail(c3__fail); } - result = m3_GetResults(f, n_out, valptrs_out); - if (result) return u3m_bail(c3__fail); + if (result) + { + return u3m_bail(c3__fail); + } u3_noun out = _atoms_from_stack(valptrs_out, n_out, types); - c3_free(name_c); - return u3nc(0, out); - } - else if (c3y == u3r_sing(monad_bat, sat->match->memread_bat)) { - if (c3n == u3r_sing(u3at(63, monad), sat->match->memread_ctx)) { + else if (c3y == u3r_sing(monad_bat, sat->match->memread_bat)) + { + if (c3n == u3r_sing(u3at(63, monad), sat->match->memread_ctx)) + { return u3m_bail(c3__fail); } // memread u3_noun ptr = u3at(124, monad); u3_noun len = u3at(125, monad); - c3_l ptr_l = (c3y == u3a_is_cat(ptr)) - ? ptr - : u3m_bail(c3__fail); - c3_l len_l = (c3y == u3a_is_cat(len)) - ? len - : u3m_bail(c3__fail); + c3_l ptr_l = (c3y == u3a_is_cat(ptr)) ? ptr : u3m_bail(c3__fail); + c3_l len_l = (c3y == u3a_is_cat(len)) ? len : u3m_bail(c3__fail); c3_w len_buf_w; c3_y* buf_y = m3_GetMemory(sat->wasm_module->runtime, &len_buf_w, 0); - if ( (buf_y == NULL) || (ptr_l + len_l > len_buf_w) ) { + if ( (buf_y == NULL) || (ptr_l + len_l > len_buf_w) ) + { return u3m_bail(c3__fail); } - return u3nt(0, len_l, u3i_bytes(len_l, (buf_y + ptr_l))); - } - else if (c3y == u3r_sing(monad_bat, sat->match->memwrite_bat)) { - if (c3n == u3r_sing(u3at(63, monad), sat->match->memwrite_ctx)) { + else if (c3y == u3r_sing(monad_bat, sat->match->memwrite_bat)) + { + if (c3n == u3r_sing(u3at(63, monad), sat->match->memwrite_ctx)) + { return u3m_bail(c3__fail); } // memwrite @@ -278,16 +340,14 @@ _reduce_monad(u3_noun monad, lia_state* sat) { u3_noun len = u3at(250, monad); u3_noun src = u3at(251, monad); - c3_l ptr_l = (c3y == u3a_is_cat(ptr)) - ? ptr - : u3m_bail(c3__fail); - c3_l len_l = (c3y == u3a_is_cat(len)) - ? len - : u3m_bail(c3__fail); + c3_l ptr_l = (c3y == u3a_is_cat(ptr)) ? ptr : u3m_bail(c3__fail); + c3_l len_l = (c3y == u3a_is_cat(len)) ? len : u3m_bail(c3__fail); + c3_w len_buf_w; c3_y* buf_y = m3_GetMemory(sat->wasm_module->runtime, &len_buf_w, 0); - if ( (buf_y == NULL) || (ptr_l + len_l > len_buf_w) ) { + if ( (buf_y == NULL) || (ptr_l + len_l > len_buf_w) ) + { return u3m_bail(c3__fail); } @@ -295,59 +355,71 @@ _reduce_monad(u3_noun monad, lia_state* sat) { return u3nc(0, 0); } - else if (c3y == u3r_sing(monad_bat, sat->match->call_ext_bat)) { + else if (c3y == u3r_sing(monad_bat, sat->match->call_ext_bat)) + { // call-ext u3_noun name = u3at(124, monad); u3_noun args = u3at(125, monad); - if (u3_nul == sat->lia_shop) { + if (u3_nul == sat->lia_shop) + { return u3nt(1, u3k(name), u3k(args)); } - else { + else + { u3_noun lia_buy; u3x_cell(sat->lia_shop, &lia_buy, &sat->lia_shop); return u3nc(0, u3k(lia_buy)); } } - else if (c3y == u3r_sing(monad_bat, sat->match->try_bat)) { + else if (c3y == u3r_sing(monad_bat, sat->match->try_bat)) + { // try u3_noun monad_b = u3at(60, monad); u3_noun cont = u3at(61, monad); - u3_noun yil = _reduce_monad(monad_b, sat); // TO LOSE - if (0 != u3h(yil)) { + u3_noun yil = _reduce_monad(monad_b, sat); + if (0 != u3h(yil)) + { return yil; } u3_noun monad_cont = u3n_slam_on(u3k(cont), u3k(u3t(yil))); return _reduce_monad(monad_cont, sat); } - else if (c3y == u3r_sing(monad_bat, sat->match->catch_bat)) { + else if (c3y == u3r_sing(monad_bat, sat->match->catch_bat)) + { // catch u3_noun monad_try = u3at(120, monad); u3_noun monad_catch = u3at(121, monad); u3_noun cont = u3at(61, monad); - u3_noun yil_try = _reduce_monad(monad_try, sat); // TO LOSE - if (0 == u3h(yil_try)) { + u3_noun yil_try = _reduce_monad(monad_try, sat); + if (0 == u3h(yil_try)) + { u3_noun monad_cont = u3n_slam_on(u3k(cont), u3k(u3t(yil_try))); return _reduce_monad(monad_cont, sat); } - else if (1 == u3h(yil_try)) { + else if (1 == u3h(yil_try)) + { return yil_try; } - else { + else + { u3_noun yil_catch = _reduce_monad(monad_catch, sat); - if (0 != u3h(yil_catch)) { + if (0 != u3h(yil_catch)) + { return yil_catch; } u3_noun monad_cont = u3n_slam_on(u3k(cont), u3k(u3t(yil_catch))); return _reduce_monad(monad_cont, sat); } } - else if (c3y == u3r_sing(monad_bat, sat->match->return_bat)) { + else if (c3y == u3r_sing(monad_bat, sat->match->return_bat)) + { // return return u3nc(0, u3at(30, monad)); } - else { + else + { return u3m_bail(c3__fail); } } @@ -358,7 +430,8 @@ _link_wasm_with_arrow_map( IM3ImportContext _ctx, uint64_t * _sp, void * _mem -) { +) +{ const char *mod = _ctx->function->import.moduleUtf8; const char *name = _ctx->function->import.fieldUtf8; lia_state* sat = _ctx->userdata; @@ -370,23 +443,28 @@ _link_wasm_with_arrow_map( c3_w n_in = _ctx->function->funcType->numArgs; c3_w n_out = _ctx->function->funcType->numRets; c3_y* types = _ctx->function->funcType->types; - const void **valptrs_in = u3a_calloc(n_in, sizeof(void*)); // TO FREE - for (c3_w i = 0; i < n_in; i++) { - valptrs_in[i] = &_sp[i+n_out]; - } - const void **valptrs_out = u3a_calloc(n_out, sizeof(void*)); // TO FREE - for (c3_w i = 0; i < n_out; i++) { - valptrs_out[i] = &_sp[i]; - } + const void **valptrs_in = u3a_calloc(n_in, sizeof(void*)); + for (c3_w i = 0; i < n_in; i++) + { + valptrs_in[i] = &_sp[i+n_out]; + } + const void **valptrs_out = u3a_calloc(n_out, sizeof(void*)); + for (c3_w i = 0; i < n_out; i++) + { + valptrs_out[i] = &_sp[i]; + } u3_noun coin_wasm_list = _coins_from_stack(valptrs_in, n_in, (types+n_out)); - u3_noun yil = _reduce_monad(u3n_slam_on(arrow, coin_wasm_list), sat); // TO LOSE + u3_noun yil = _reduce_monad(u3n_slam_on(arrow, coin_wasm_list), sat); - if (0 != u3h(yil)) { + if (0 != u3h(yil)) + { sat->arrow_yil = yil; return m3Lia_Arrow; - } else { + } + else + { _coins_to_stack(u3t(yil), valptrs_out, n_out, types); return m3Err_none; } @@ -395,61 +473,66 @@ _link_wasm_with_arrow_map( u3_weak u3we_lia_run(u3_noun cor) { - if (c3__none == u3x_at(u3x_sam_7, cor)) { + if (c3__none == u3at(u3x_sam_7, cor)) + { return u3_none; } // enter subroad, 4MB safety buffer - // u3m_hate(1 << 20); + u3m_hate(1 << 20); - u3_noun input = u3x_at(u3x_sam_2, cor); - u3_noun seed = u3x_at(u3x_sam_6, cor); + u3_noun input = u3at(u3x_sam_2, cor); + u3_noun seed = u3at(u3x_sam_6, cor); u3_noun runnable = u3j_kink(u3k(u3at(7, cor)), 372); u3_noun try_gate = u3j_kink(u3k(runnable), 21); + u3_noun try_gate_inner = u3j_kink(try_gate, 2); - u3_noun seed_new; // TO RETURN + u3_noun seed_new; u3_noun input_tag, p_input; u3x_cell(input, &input_tag, &p_input); - if (input_tag == c3y) { + if (input_tag == c3y) + { u3_noun p_input_gate = u3nt(u3nc(0, 7), 0, u3k(p_input)); // =>(p.input |=(* +>)) u3_noun past_new = u3n_slam_on( - u3j_kink(u3k(try_gate), 2), + u3k(try_gate_inner), u3nc( - u3k(u3x_at(6, seed)), + u3k(u3at(6, seed)), p_input_gate ) ); seed_new = u3nq( - u3k(u3x_at(2, seed)), + u3k(u3at(2, seed)), past_new, - u3k(u3x_at(14, seed)), - u3k(u3x_at(15, seed)) + u3k(u3at(14, seed)), + u3k(u3at(15, seed)) ); } - else if (input_tag == c3n) { + else if (input_tag == c3n) + { seed_new = u3nq( - u3k(u3x_at(2, seed)), - u3x_at(6, seed), + u3k(u3at(2, seed)), + u3k(u3at(6, seed)), u3kb_weld( - u3k(u3x_at(14, seed)), + u3k(u3at(14, seed)), u3nc(u3k(p_input), u3_nul) ), - u3k(u3x_at(15, seed)) + u3k(u3at(15, seed)) ); } - else { + else + { return u3m_bail(c3__fail); } - u3_noun call_script = u3j_kink(u3j_kink(u3k(u3at(7, cor)), 20), 2); // TO LOSE - u3_noun memread_script = u3j_kink(u3j_kink(u3k(u3at(7, cor)), 374), 2); // TO LOSE - u3_noun memwrite_script = u3j_kink(u3j_kink(u3k(u3at(7, cor)), 92), 2); // TO LOSE - u3_noun call_ext_script = u3j_kink(u3j_kink(u3k(u3at(7, cor)), 2986), 2); // TO LOSE - u3_noun try_script = u3j_kink(u3j_kink(try_gate, 2), 2); // TO LOSE - u3_noun catch_script = u3j_kink(u3j_kink(u3j_kink(u3k(runnable), 4), 2), 2); // TO LOSE - u3_noun return_script = u3j_kink(u3j_kink(runnable, 20), 2); // TO LOSE + u3_noun call_script = u3j_kink(u3j_kink(u3k(u3at(7, cor)), 20), 2); + u3_noun memread_script = u3j_kink(u3j_kink(u3k(u3at(7, cor)), 374), 2); + u3_noun memwrite_script = u3j_kink(u3j_kink(u3k(u3at(7, cor)), 92), 2); + u3_noun call_ext_script = u3j_kink(u3j_kink(u3k(u3at(7, cor)), 2986), 2); + u3_noun try_script = u3j_kink(try_gate_inner, 2); + u3_noun catch_script = u3j_kink(u3j_kink(u3j_kink(u3k(runnable), 4), 2), 2); + u3_noun return_script = u3j_kink(u3j_kink(runnable, 20), 2); u3_noun call_bat = u3h(call_script); u3_noun memread_bat = u3h(memread_script); @@ -476,7 +559,7 @@ u3we_lia_run(u3_noun cor) memwrite_ctx }; - u3_noun octs = u3x_at(2, seed_new); + u3_noun octs = u3at(2, seed_new); u3_noun p_octs, q_octs; u3x_cell(octs, &p_octs, &q_octs); @@ -486,24 +569,25 @@ u3we_lia_run(u3_noun cor) m3_SetAllocators(u3a_calloc, u3a_free, u3a_realloc); IM3Environment wasm3_env = m3_NewEnvironment(); - if (!wasm3_env) { + if (!wasm3_env) + { fprintf(stderr, "env is null\r\n"); return u3m_bail(c3__fail); } M3Result result; - IM3Runtime wasm3_runtime = m3_NewRuntime(wasm3_env, 1 << 13, NULL); - if (!wasm3_runtime) { + // 2MB stack + IM3Runtime wasm3_runtime = m3_NewRuntime(wasm3_env, 1 << 21, NULL); + if (!wasm3_runtime) + { fprintf(stderr, "runtime is null\r\n"); return u3m_bail(c3__fail); } IM3Module wasm3_module; - result = m3_ParseModule(wasm3_env, - &wasm3_module, - bin_y, - bin_len_w); - if (result) { + result = m3_ParseModule(wasm3_env, &wasm3_module, bin_y, bin_len_w); + if (result) + { fprintf(stderr, "parse binary error: %s\r\n", result); return u3m_bail(c3__fail); } @@ -511,27 +595,33 @@ u3we_lia_run(u3_noun cor) // TODO validate result = m3_LoadModule(wasm3_runtime, wasm3_module); - if (result) { + if (result) + { fprintf(stderr, "load module error: %s\r\n", result); return u3m_bail(c3__fail); } c3_w n_imports = wasm3_module->numFuncImports; - u3_noun monad = u3x_at(6, seed_new); - u3_noun lia_shop = u3x_at(14, seed_new); - u3_noun import = u3x_at(15, seed_new); + u3_noun monad = u3at(6, seed_new); + u3_noun lia_shop = u3at(14, seed_new); + u3_noun import = u3at(15, seed_new); lia_state sat = {wasm3_module, lia_shop, import, &match, 0}; - for (c3_w i = 0; i < n_imports; i++) { + for (c3_w i = 0; i < n_imports; i++) + { M3Function f = wasm3_module->functions[i]; const char * mod = f.import.moduleUtf8; const char * name = f.import.fieldUtf8; - result = m3_LinkRawFunctionEx(wasm3_module, - mod, name, NULL, - &_link_wasm_with_arrow_map, - (void *)&sat); - if (result) { + + result = m3_LinkRawFunctionEx( + wasm3_module, mod, name, + NULL, &_link_wasm_with_arrow_map, + (void *)&sat + ); + + if (result) + { fprintf(stderr, "link error"); return u3m_bail(c3__fail); } @@ -539,10 +629,8 @@ u3we_lia_run(u3_noun cor) u3_noun yil = _reduce_monad(monad, &sat); - - // // exit subroad, copying the result - // u3_noun pro = u3m_love(u3nc(yil, seed_new)); - u3_noun pro = u3nc(yil, seed_new); + // exit subroad, copying the result + u3_noun pro = u3m_love(u3nc(yil, seed_new)); return pro; } From 0bf44207eaf086f68bbe6ffa2a49bb839ae0884c Mon Sep 17 00:00:00 2001 From: Quodss Date: Wed, 27 Nov 2024 20:14:35 +0100 Subject: [PATCH 07/22] validation, deps, cleanup --- ext/wasm3/build.zig | 3 +- ext/wasm3/build.zig.zon | 4 +-- pkg/noun/jets/e/urwasm.c | 62 ++++++++++++++++------------------------ 3 files changed, 29 insertions(+), 40 deletions(-) diff --git a/ext/wasm3/build.zig b/ext/wasm3/build.zig index d8db34a6ce..2c5c8337b1 100644 --- a/ext/wasm3/build.zig +++ b/ext/wasm3/build.zig @@ -57,8 +57,8 @@ pub fn build(b: *std.Build) void { "m3_function.c", "m3_info.c", "m3_module.c", - "m3_optimize.c", "m3_parse.c", + "m3_validate.c", }, .flags = if (t.os.tag == .macos) &mac_flags else &common_flags, }); @@ -79,6 +79,7 @@ pub fn build(b: *std.Build) void { wasm3.installHeader(wasm3_c.path("source/m3_math_utils.h"), "m3_math_utils.h"); wasm3.installHeader(wasm3_c.path("source/wasm3.h"), "wasm3.h"); wasm3.installHeader(wasm3_c.path("source/wasm3_defs.h"), "wasm3_defs.h"); + wasm3.installHeader(wasm3_c.path("source/m3_validate.h"), "m3_validate.h"); wasm3.linkLibrary(softfloat.artifact("softfloat")); diff --git a/ext/wasm3/build.zig.zon b/ext/wasm3/build.zig.zon index 51e7849f58..b01fbb66b0 100644 --- a/ext/wasm3/build.zig.zon +++ b/ext/wasm3/build.zig.zon @@ -6,8 +6,8 @@ .path = "../softfloat", }, .wasm3 = .{ - .url = "https://github.com/Quodss/wasm3/archive/004f1f654c9384074f79c5e0f8a7717aa0829289.tar.gz", - .hash = "122051e4183102eb4653556d0bcbf0908203ebdaa2c105df181e63a569f05f702baa", + .url = "https://github.com/Quodss/wasm3/archive/79d3e166304c475e0a6774b6f3d65870f08b2056.tar.gz", + .hash = "1220021f7b4dff3fd9e64e852033c0b161a51b60736e0c78ace457b3d7c7d2c65d00", }, }, .paths = .{ diff --git a/pkg/noun/jets/e/urwasm.c b/pkg/noun/jets/e/urwasm.c index 2006f4eb60..32a80e267c 100644 --- a/pkg/noun/jets/e/urwasm.c +++ b/pkg/noun/jets/e/urwasm.c @@ -8,6 +8,7 @@ #include "wasm3.h" #include "m3_env.h" +#include "m3_validate.h" static const M3Result m3Lia_Arrow = "non-zero yield from import arrow"; @@ -34,29 +35,20 @@ typedef struct { // TRANSFER result static u3_noun -_atoms_from_stack(const void** valptrs, c3_w n, c3_y* types) +_atoms_from_stack(void** valptrs, c3_w n, c3_y* types) { u3_noun out = u3_nul; - while (n) + while (n--) { - n--; switch (types[n]) { // TODO 64 bit vere case c_m3Type_i32: - { - out = u3nc(u3i_word(*(c3_w*)valptrs[n]), out); - break; - } - case c_m3Type_i64: - { - out = u3nc(u3i_chub(*(c3_d*)valptrs[n]), out); - break; - } case c_m3Type_f32: { out = u3nc(u3i_word(*(c3_w*)valptrs[n]), out); break; } + case c_m3Type_i64: case c_m3Type_f64: { out = u3nc(u3i_chub(*(c3_d*)valptrs[n]), out); @@ -73,7 +65,7 @@ _atoms_from_stack(const void** valptrs, c3_w n, c3_y* types) // RETAIN coins static void -_atoms_to_stack(u3_noun atoms, const void** valptrs, c3_w n, c3_y* types) +_atoms_to_stack(u3_noun atoms, void** valptrs, c3_w n, c3_y* types) { for (c3_w i = 0; i < n; i++) { @@ -90,20 +82,12 @@ _atoms_to_stack(u3_noun atoms, const void** valptrs, c3_w n, c3_y* types) switch (types[i]) { case c_m3Type_i32: - { - *(c3_w*)valptrs[i] = u3r_word(0, atom); - break; - } - case c_m3Type_i64: - { - *(c3_d*)valptrs[i] = u3r_chub(0, atom); - break; - } case c_m3Type_f32: { *(c3_w*)valptrs[i] = u3r_word(0, atom); break; } + case c_m3Type_i64: case c_m3Type_f64: { *(c3_d*)valptrs[i] = u3r_chub(0, atom); @@ -123,12 +107,11 @@ _atoms_to_stack(u3_noun atoms, const void** valptrs, c3_w n, c3_y* types) // TRANSFER result static u3_noun -_coins_from_stack(const void** valptrs, c3_w n, c3_y* types) +_coins_from_stack(void** valptrs, c3_w n, c3_y* types) { u3_noun out = u3_nul; - while (n) + while (n--) { - n--; switch (types[n]) { // TODO 64 bit vere case c_m3Type_i32: @@ -162,7 +145,7 @@ _coins_from_stack(const void** valptrs, c3_w n, c3_y* types) // RETAIN coins static void -_coins_to_stack(u3_noun coins, const void** valptrs, c3_w n, c3_y* types) +_coins_to_stack(u3_noun coins, void** valptrs, c3_w n, c3_y* types) { for (c3_w i = 0; i < n; i++) { @@ -266,15 +249,15 @@ _reduce_monad(u3_noun monad, lia_state* sat) c3_w n_out = f->funcType->numRets; c3_y* types = f->funcType->types; - const c3_d *vals_in = u3a_calloc(n_in, sizeof(c3_d)); - const void **valptrs_in = u3a_calloc(n_in, sizeof(void*)); + c3_d *vals_in = u3a_calloc(n_in, sizeof(c3_d)); + void **valptrs_in = u3a_calloc(n_in, sizeof(void*)); for (c3_w i = 0; i < n_in; i++) { valptrs_in[i] = &vals_in[i]; } - const c3_d *vals_out = u3a_calloc(n_out, sizeof(c3_d)); - const void **valptrs_out = u3a_calloc(n_out, sizeof(void*)); + c3_d *vals_out = u3a_calloc(n_out, sizeof(c3_d)); + void **valptrs_out = u3a_calloc(n_out, sizeof(void*)); for (c3_w i = 0; i < n_out; i++) { valptrs_out[i] = &vals_out[i]; @@ -282,7 +265,7 @@ _reduce_monad(u3_noun monad, lia_state* sat) _atoms_to_stack(args, valptrs_in, n_in, (types+n_out)); - result = m3_Call(f, n_in, valptrs_in); + result = m3_Call(f, n_in, (const void**)valptrs_in); if (result == m3Lia_Arrow) { @@ -299,7 +282,7 @@ _reduce_monad(u3_noun monad, lia_state* sat) fprintf(stderr, "\r\ncall failed: %s \r\n", name_c); return u3m_bail(c3__fail); } - result = m3_GetResults(f, n_out, valptrs_out); + result = m3_GetResults(f, n_out, (const void**)valptrs_out); if (result) { return u3m_bail(c3__fail); @@ -426,7 +409,7 @@ _reduce_monad(u3_noun monad, lia_state* sat) static const void * _link_wasm_with_arrow_map( - IM3Runtime king_runtime, + IM3Runtime runtime, IM3ImportContext _ctx, uint64_t * _sp, void * _mem @@ -443,12 +426,12 @@ _link_wasm_with_arrow_map( c3_w n_in = _ctx->function->funcType->numArgs; c3_w n_out = _ctx->function->funcType->numRets; c3_y* types = _ctx->function->funcType->types; - const void **valptrs_in = u3a_calloc(n_in, sizeof(void*)); + void **valptrs_in = u3a_calloc(n_in, sizeof(void*)); for (c3_w i = 0; i < n_in; i++) { valptrs_in[i] = &_sp[i+n_out]; } - const void **valptrs_out = u3a_calloc(n_out, sizeof(void*)); + void **valptrs_out = u3a_calloc(n_out, sizeof(void*)); for (c3_w i = 0; i < n_out; i++) { valptrs_out[i] = &_sp[i]; @@ -592,8 +575,6 @@ u3we_lia_run(u3_noun cor) return u3m_bail(c3__fail); } - // TODO validate - result = m3_LoadModule(wasm3_runtime, wasm3_module); if (result) { @@ -601,6 +582,13 @@ u3we_lia_run(u3_noun cor) return u3m_bail(c3__fail); } + result = m3_ValidateModule(wasm3_module); + if (result) + { + fprintf(stderr, "validation error: %s\r\n", result); + return u3m_bail(c3__fail); + } + c3_w n_imports = wasm3_module->numFuncImports; u3_noun monad = u3at(6, seed_new); u3_noun lia_shop = u3at(14, seed_new); From 551dafc3944fcfd762b800905019b5bee32769fb Mon Sep 17 00:00:00 2001 From: Quodss Date: Thu, 28 Nov 2024 21:09:31 +0100 Subject: [PATCH 08/22] wasm3 dep update --- ext/wasm3/build.zig.zon | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/wasm3/build.zig.zon b/ext/wasm3/build.zig.zon index b01fbb66b0..3cc22b9978 100644 --- a/ext/wasm3/build.zig.zon +++ b/ext/wasm3/build.zig.zon @@ -6,8 +6,8 @@ .path = "../softfloat", }, .wasm3 = .{ - .url = "https://github.com/Quodss/wasm3/archive/79d3e166304c475e0a6774b6f3d65870f08b2056.tar.gz", - .hash = "1220021f7b4dff3fd9e64e852033c0b161a51b60736e0c78ace457b3d7c7d2c65d00", + .url = "https://github.com/Quodss/wasm3/archive/fbcfd07accb1814243e29be9ff40b138271a32fb.tar.gz", + .hash = "12201af1a0dafe0a16c03c51cc6bcc362f0e2fd0208eccaf21f98e7e3003782bc765", }, }, .paths = .{ From 6c6c1b664bd31d66ee004f647745b00f32cd76e4 Mon Sep 17 00:00:00 2001 From: Quodss Date: Fri, 29 Nov 2024 11:43:21 +0100 Subject: [PATCH 09/22] call start function manually --- pkg/noun/jets/e/urwasm.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/pkg/noun/jets/e/urwasm.c b/pkg/noun/jets/e/urwasm.c index 32a80e267c..9c263e3beb 100644 --- a/pkg/noun/jets/e/urwasm.c +++ b/pkg/noun/jets/e/urwasm.c @@ -615,7 +615,27 @@ u3we_lia_run(u3_noun cor) } } - u3_noun yil = _reduce_monad(monad, &sat); + u3_noun yil; + + result = m3_RunStart(wasm3_module); + + if (result == m3Lia_Arrow) + { + yil = sat.arrow_yil; + sat.arrow_yil = 0; + if (yil == 0) + { + return u3m_bail(c3__fail); + } + } + else if (result) + { + return u3m_bail(c3__fail); + } + else + { + yil = _reduce_monad(monad, &sat); + } // exit subroad, copying the result u3_noun pro = u3m_love(u3nc(yil, seed_new)); From cab281ed6819178e8fa8103c02efdc328cf6271d Mon Sep 17 00:00:00 2001 From: Quodss Date: Fri, 29 Nov 2024 13:45:25 +0100 Subject: [PATCH 10/22] versioned jet tags --- pkg/noun/jets/tree.c | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index c2b8f06e11..193998a73f 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -2363,48 +2363,43 @@ static u3j_core _138_hex_chacha_d[] = // static u3j_harm _138_hex_lia_run_a[] = {{".2", u3we_lia_run, c3y}, {}}; -static u3j_core _138_hex_lia_core_d[] = { - { "run", 7, _138_hex_lia_run_a, 0, no_hashes }, - {} -}; - static u3j_core _138_hex_lia_monad_d[] = { - { "core", 3, 0, _138_hex_lia_core_d, no_hashes }, + { "run-v0", 7, _138_hex_lia_run_a, 0, no_hashes }, {} }; static u3j_core _138_hex_wasm_engine_d[] = { - { "monad", 3, 0, _138_hex_lia_monad_d, no_hashes }, + { "monad-v0", 3, 0, _138_hex_lia_monad_d, no_hashes }, {} }; static u3j_core _138_hex_wasm_op_def_d[] = { - { "wasm-engine", 3, 0, _138_hex_wasm_engine_d, no_hashes }, + { "wasm-engine-v0", 3, 0, _138_hex_wasm_engine_d, no_hashes }, {} }; static u3j_core _138_hex_wasm_validator_d[] = { - { "wasm-op-def", 3, 0, _138_hex_wasm_op_def_d, no_hashes }, + { "wasm-op-def-v0", 3, 0, _138_hex_wasm_op_def_d, no_hashes }, {} }; static u3j_core _138_hex_wasm_parser_d[] = { - { "validator", 3, 0, _138_hex_wasm_validator_d, no_hashes }, + { "validator-v0", 3, 0, _138_hex_wasm_validator_d, no_hashes }, {} }; static u3j_core _138_hex_lia_sur_d[] = { - { "wasm-parser", 3, 0, _138_hex_wasm_parser_d, no_hashes }, + { "wasm-parser-v0", 3, 0, _138_hex_wasm_parser_d, no_hashes }, {} }; static u3j_core _138_hex_wasm_engine_sur_d[] = { - { "monad-sur", 3, 0, _138_hex_lia_sur_d, no_hashes }, + { "monad-sur-v0", 3, 0, _138_hex_lia_sur_d, no_hashes }, {} }; static u3j_core _138_hex_wasm_sur_d[] = { - { "engine-sur", 3, 0, _138_hex_wasm_engine_sur_d, no_hashes }, + { "engine-sur-v0", 3, 0, _138_hex_wasm_engine_sur_d, no_hashes }, {} }; @@ -2427,7 +2422,7 @@ static u3j_core _138_hex_d[] = { "secp", 6, 0, _140_hex_secp_d, no_hashes }, { "mimes", 31, 0, _140_hex_mimes_d, no_hashes }, { "json", 31, 0, _139_hex_json_d, no_hashes }, - { "wasm-sur", 3, 0, _138_hex_wasm_sur_d, no_hashes }, + { "wasm-sur-v0", 3, 0, _138_hex_wasm_sur_d, no_hashes }, {} }; From e5eac5ab0577cfa3dd7652a373eea9317208b6d4 Mon Sep 17 00:00:00 2001 From: Quodss Date: Fri, 29 Nov 2024 18:11:52 +0100 Subject: [PATCH 11/22] wasm3 update dep --- ext/wasm3/build.zig.zon | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/wasm3/build.zig.zon b/ext/wasm3/build.zig.zon index 3cc22b9978..76a88ceac1 100644 --- a/ext/wasm3/build.zig.zon +++ b/ext/wasm3/build.zig.zon @@ -6,8 +6,8 @@ .path = "../softfloat", }, .wasm3 = .{ - .url = "https://github.com/Quodss/wasm3/archive/fbcfd07accb1814243e29be9ff40b138271a32fb.tar.gz", - .hash = "12201af1a0dafe0a16c03c51cc6bcc362f0e2fd0208eccaf21f98e7e3003782bc765", + .url = "https://github.com/Quodss/wasm3/archive/9190b2b29f947dd46d7c214ed10f8cdf4392f767.tar.gz", + .hash = "12209ab537d0d9159764b98beb4d6206ee042a73bdfbd2ce1cde2e9b4819488a5470", }, }, .paths = .{ From 83f8c69b5eeac197202df57aab033dae8bd40bff Mon Sep 17 00:00:00 2001 From: Quodss Date: Fri, 29 Nov 2024 19:05:31 +0100 Subject: [PATCH 12/22] wasm3 update dep --- ext/wasm3/build.zig.zon | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ext/wasm3/build.zig.zon b/ext/wasm3/build.zig.zon index 76a88ceac1..5c87884289 100644 --- a/ext/wasm3/build.zig.zon +++ b/ext/wasm3/build.zig.zon @@ -6,8 +6,9 @@ .path = "../softfloat", }, .wasm3 = .{ - .url = "https://github.com/Quodss/wasm3/archive/9190b2b29f947dd46d7c214ed10f8cdf4392f767.tar.gz", - .hash = "12209ab537d0d9159764b98beb4d6206ee042a73bdfbd2ce1cde2e9b4819488a5470", + // .path = "../../../../wasm3" + .url = "https://github.com/Quodss/wasm3/archive/8db7a4427af5fe669e55543d183dfdc7ac070057.tar.gz", + .hash = "12209700c64ff2aa65b506cf12c8d0975919855467f9c3c2524b77df927221766ad4", }, }, .paths = .{ From 0e2cb27a7be180e1cd4dfaf3ff69fbece723c66a Mon Sep 17 00:00:00 2001 From: Quodss Date: Sat, 30 Nov 2024 18:32:48 +0100 Subject: [PATCH 13/22] wasm3 dep update --- ext/wasm3/build.zig.zon | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/wasm3/build.zig.zon b/ext/wasm3/build.zig.zon index 5c87884289..30f49a0f9f 100644 --- a/ext/wasm3/build.zig.zon +++ b/ext/wasm3/build.zig.zon @@ -7,8 +7,8 @@ }, .wasm3 = .{ // .path = "../../../../wasm3" - .url = "https://github.com/Quodss/wasm3/archive/8db7a4427af5fe669e55543d183dfdc7ac070057.tar.gz", - .hash = "12209700c64ff2aa65b506cf12c8d0975919855467f9c3c2524b77df927221766ad4", + .url = "https://github.com/Quodss/wasm3/archive/0733e3299ded55e3f61c6a3d34b03f52a007a97c.tar.gz", + .hash = "12209a3f38cac8dc5d84983a599ae288cd0cdba31ea765043b31470a84f99e12a4f8", }, }, .paths = .{ From 6d3055247aad052f9c95baaf9160096e8c819e1e Mon Sep 17 00:00:00 2001 From: Quodss Date: Sun, 1 Dec 2024 12:08:56 +0100 Subject: [PATCH 14/22] wasm3 update --- ext/wasm3/build.zig.zon | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/wasm3/build.zig.zon b/ext/wasm3/build.zig.zon index 30f49a0f9f..620c4d4205 100644 --- a/ext/wasm3/build.zig.zon +++ b/ext/wasm3/build.zig.zon @@ -7,8 +7,8 @@ }, .wasm3 = .{ // .path = "../../../../wasm3" - .url = "https://github.com/Quodss/wasm3/archive/0733e3299ded55e3f61c6a3d34b03f52a007a97c.tar.gz", - .hash = "12209a3f38cac8dc5d84983a599ae288cd0cdba31ea765043b31470a84f99e12a4f8", + .url = "https://github.com/Quodss/wasm3/archive/cf46812335facdcf27bcd8271808a3894b2e8cd1.tar.gz", + .hash = "122022701340f5e3b4679a55068bec4c79c8edab8edb84813e97300ab9189ad0621e", }, }, .paths = .{ From 9b27e3f4d2ec8edeb0e7e623975d2a3f221e3ae7 Mon Sep 17 00:00:00 2001 From: Quodss Date: Tue, 3 Dec 2024 16:05:27 +0100 Subject: [PATCH 15/22] wasm3 update --- ext/wasm3/build.zig.zon | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/wasm3/build.zig.zon b/ext/wasm3/build.zig.zon index 620c4d4205..8f215886c9 100644 --- a/ext/wasm3/build.zig.zon +++ b/ext/wasm3/build.zig.zon @@ -7,8 +7,8 @@ }, .wasm3 = .{ // .path = "../../../../wasm3" - .url = "https://github.com/Quodss/wasm3/archive/cf46812335facdcf27bcd8271808a3894b2e8cd1.tar.gz", - .hash = "122022701340f5e3b4679a55068bec4c79c8edab8edb84813e97300ab9189ad0621e", + .url = "https://github.com/Quodss/wasm3/archive/5cfd55cdc3b9b5f7d8ac4768155ae96b73222e8e.tar.gz", + .hash = "122032987041f49b62512047a92e8f272c80ae755264fcc24b6f4a3d788dc0406982", }, }, .paths = .{ From eb540b4a84b3cd4bba5e1679eb93624384fce5ee Mon Sep 17 00:00:00 2001 From: Quodss Date: Wed, 4 Dec 2024 16:25:28 +0100 Subject: [PATCH 16/22] polymorphic run-once jet and registration --- pkg/noun/jets/e/urwasm.c | 149 +++++++++++++++++++++++++++++++++++++++ pkg/noun/jets/tree.c | 8 +++ pkg/noun/jets/w.h | 1 + 3 files changed, 158 insertions(+) diff --git a/pkg/noun/jets/e/urwasm.c b/pkg/noun/jets/e/urwasm.c index 9c263e3beb..57c23cbba3 100644 --- a/pkg/noun/jets/e/urwasm.c +++ b/pkg/noun/jets/e/urwasm.c @@ -642,3 +642,152 @@ u3we_lia_run(u3_noun cor) return pro; } + +u3_weak +u3we_lia_run_once(u3_noun cor) +{ + if (c3__none == u3at(u3x_sam_6, cor)) + { + return u3_none; + } + + // enter subroad, 4MB safety buffer + u3m_hate(1 << 20); + + u3_noun runnable = u3j_kink(u3k(u3at(63, cor)), 372); + u3_noun try_gate = u3j_kink(u3k(runnable), 21); + u3_noun try_gate_inner = u3j_kink(try_gate, 2); + + u3_noun call_script = u3j_kink(u3j_kink(u3k(u3at(63, cor)), 20), 2); + u3_noun memread_script = u3j_kink(u3j_kink(u3k(u3at(63, cor)), 374), 2); + u3_noun memwrite_script = u3j_kink(u3j_kink(u3k(u3at(63, cor)), 92), 2); + u3_noun call_ext_script = u3j_kink(u3j_kink(u3k(u3at(63, cor)), 2986), 2); + u3_noun try_script = u3j_kink(try_gate_inner, 2); + u3_noun catch_script = u3j_kink(u3j_kink(u3j_kink(u3k(runnable), 4), 2), 2); + u3_noun return_script = u3j_kink(u3j_kink(runnable, 20), 2); + + u3_noun call_bat = u3h(call_script); + u3_noun memread_bat = u3h(memread_script); + u3_noun memwrite_bat = u3h(memwrite_script); + u3_noun call_ext_bat = u3h(call_ext_script); + u3_noun try_bat = u3h(try_script); + u3_noun catch_bat = u3h(catch_script); + u3_noun return_bat = u3h(return_script); + + u3_noun call_ctx = u3at(63, call_script); + u3_noun memread_ctx = u3at(63, memread_script); + u3_noun memwrite_ctx = u3at(63, memwrite_script); + + match_data_struct match = { + call_bat, + memread_bat, + memwrite_bat, + call_ext_bat, + try_bat, + catch_bat, + return_bat, + call_ctx, + memread_ctx, + memwrite_ctx + }; + + u3_noun octs = u3at(u3x_sam_4, cor); + u3_noun p_octs, q_octs; + u3x_cell(octs, &p_octs, &q_octs); + + c3_w bin_len_w = (c3y == u3a_is_cat(p_octs)) ? p_octs : u3m_bail(c3__fail); + c3_y* bin_y = u3r_bytes_alloc(0, bin_len_w, u3x_atom(q_octs)); + + m3_SetAllocators(u3a_calloc, u3a_free, u3a_realloc); + + IM3Environment wasm3_env = m3_NewEnvironment(); + if (!wasm3_env) + { + fprintf(stderr, "env is null\r\n"); + return u3m_bail(c3__fail); + } + M3Result result; + + // 2MB stack + IM3Runtime wasm3_runtime = m3_NewRuntime(wasm3_env, 1 << 21, NULL); + if (!wasm3_runtime) + { + fprintf(stderr, "runtime is null\r\n"); + return u3m_bail(c3__fail); + } + + IM3Module wasm3_module; + result = m3_ParseModule(wasm3_env, &wasm3_module, bin_y, bin_len_w); + if (result) + { + fprintf(stderr, "parse binary error: %s\r\n", result); + return u3m_bail(c3__fail); + } + + result = m3_LoadModule(wasm3_runtime, wasm3_module); + if (result) + { + fprintf(stderr, "load module error: %s\r\n", result); + return u3m_bail(c3__fail); + } + + result = m3_ValidateModule(wasm3_module); + if (result) + { + fprintf(stderr, "validation error: %s\r\n", result); + return u3m_bail(c3__fail); + } + + c3_w n_imports = wasm3_module->numFuncImports; + u3_noun monad = u3at(u3x_sam_7, cor); + u3_noun lia_shop = u3_nul; + u3_noun import = u3at(u3x_sam_5, cor); + + lia_state sat = {wasm3_module, lia_shop, import, &match, 0}; + + for (c3_w i = 0; i < n_imports; i++) + { + M3Function f = wasm3_module->functions[i]; + const char * mod = f.import.moduleUtf8; + const char * name = f.import.fieldUtf8; + + result = m3_LinkRawFunctionEx( + wasm3_module, mod, name, + NULL, &_link_wasm_with_arrow_map, + (void *)&sat + ); + + if (result) + { + fprintf(stderr, "link error"); + return u3m_bail(c3__fail); + } + } + + u3_noun yil; + + result = m3_RunStart(wasm3_module); + + if (result == m3Lia_Arrow) + { + yil = sat.arrow_yil; + sat.arrow_yil = 0; + if (yil == 0) + { + return u3m_bail(c3__fail); + } + } + else if (result) + { + return u3m_bail(c3__fail); + } + else + { + yil = _reduce_monad(monad, &sat); + } + + // exit subroad, copying the result + u3_noun pro = u3m_love(yil); + + return pro; +} diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index 193998a73f..d03cbd8ef4 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -2363,8 +2363,16 @@ static u3j_core _138_hex_chacha_d[] = // static u3j_harm _138_hex_lia_run_a[] = {{".2", u3we_lia_run, c3y}, {}}; +static u3j_harm _138_hex_lia_run_once_inner_a[] = {{".2", u3we_lia_run_once, c3y}, {}}; + +static u3j_core _138_hex_lia_run_once_d[] = { + { "run-once-inner-v0", 15, _138_hex_lia_run_once_inner_a, 0, no_hashes }, + {} +}; + static u3j_core _138_hex_lia_monad_d[] = { { "run-v0", 7, _138_hex_lia_run_a, 0, no_hashes }, + { "run-once-v0", 7, 0, _138_hex_lia_run_once_d, no_hashes }, {} }; diff --git a/pkg/noun/jets/w.h b/pkg/noun/jets/w.h index 3149852c83..befa2a7b30 100644 --- a/pkg/noun/jets/w.h +++ b/pkg/noun/jets/w.h @@ -305,6 +305,7 @@ u3_noun u3we_crc32(u3_noun); u3_noun u3we_lia_run(u3_noun); + u3_noun u3we_lia_run_once(u3_noun); /** Tier 6. **/ From 6eb41c29a0be57a581ba9869b3c1e808310b0a4b Mon Sep 17 00:00:00 2001 From: Quodss Date: Sat, 7 Dec 2024 11:50:23 +0100 Subject: [PATCH 17/22] mug core, proper memory management, remove subroad --- pkg/noun/jets/e/urwasm.c | 216 ++++++++++++++++++++++++++++----------- 1 file changed, 159 insertions(+), 57 deletions(-) diff --git a/pkg/noun/jets/e/urwasm.c b/pkg/noun/jets/e/urwasm.c index 57c23cbba3..4fa5f5f004 100644 --- a/pkg/noun/jets/e/urwasm.c +++ b/pkg/noun/jets/e/urwasm.c @@ -10,6 +10,8 @@ #include "m3_env.h" #include "m3_validate.h" +// #define URWASM_SUBROAD + static const M3Result m3Lia_Arrow = "non-zero yield from import arrow"; typedef struct { @@ -33,7 +35,6 @@ typedef struct { u3_noun arrow_yil; } lia_state; -// TRANSFER result static u3_noun _atoms_from_stack(void** valptrs, c3_w n, c3_y* types) { @@ -63,7 +64,7 @@ _atoms_from_stack(void** valptrs, c3_w n, c3_y* types) return out; } -// RETAIN coins +// RETAIN argument static void _atoms_to_stack(u3_noun atoms, void** valptrs, c3_w n, c3_y* types) { @@ -105,7 +106,6 @@ _atoms_to_stack(u3_noun atoms, void** valptrs, c3_w n, c3_y* types) } } -// TRANSFER result static u3_noun _coins_from_stack(void** valptrs, c3_w n, c3_y* types) { @@ -143,7 +143,7 @@ _coins_from_stack(void** valptrs, c3_w n, c3_y* types) return out; } -// RETAIN coins +// RETAIN argument static void _coins_to_stack(u3_noun coins, void** valptrs, c3_w n, c3_y* types) { @@ -215,7 +215,7 @@ _coins_to_stack(u3_noun coins, void** valptrs, c3_w n, c3_y* types) } } -// RETAIN args, TRANSFER result + static u3_noun _reduce_monad(u3_noun monad, lia_state* sat) { @@ -249,15 +249,15 @@ _reduce_monad(u3_noun monad, lia_state* sat) c3_w n_out = f->funcType->numRets; c3_y* types = f->funcType->types; - c3_d *vals_in = u3a_calloc(n_in, sizeof(c3_d)); - void **valptrs_in = u3a_calloc(n_in, sizeof(void*)); + c3_d *vals_in = u3a_calloc(n_in, sizeof(c3_d)); + void **valptrs_in = u3a_calloc(n_in, sizeof(void*)); for (c3_w i = 0; i < n_in; i++) { valptrs_in[i] = &vals_in[i]; } - c3_d *vals_out = u3a_calloc(n_out, sizeof(c3_d)); - void **valptrs_out = u3a_calloc(n_out, sizeof(void*)); + c3_d *vals_out = u3a_calloc(n_out, sizeof(c3_d)); + void **valptrs_out = u3a_calloc(n_out, sizeof(void*)); for (c3_w i = 0; i < n_out; i++) { valptrs_out[i] = &vals_out[i]; @@ -267,29 +267,40 @@ _reduce_monad(u3_noun monad, lia_state* sat) result = m3_Call(f, n_in, (const void**)valptrs_in); + u3_noun yil; + if (result == m3Lia_Arrow) { - u3_noun yil = sat->arrow_yil; + yil = sat->arrow_yil; sat->arrow_yil = 0; if (yil == 0) { return u3m_bail(c3__fail); } - return yil; } else if (result) { fprintf(stderr, "\r\ncall failed: %s \r\n", name_c); return u3m_bail(c3__fail); } - result = m3_GetResults(f, n_out, (const void**)valptrs_out); - if (result) + else { - return u3m_bail(c3__fail); + result = m3_GetResults(f, n_out, (const void**)valptrs_out); + if (result) + { + return u3m_bail(c3__fail); + } + yil = u3nc(0, _atoms_from_stack(valptrs_out, n_out, types)); } - u3_noun out = _atoms_from_stack(valptrs_out, n_out, types); - return u3nc(0, out); + u3a_free(name_c); + u3a_free(vals_in); + u3a_free(valptrs_in); + u3a_free(vals_out); + u3a_free(valptrs_out); + u3z(monad); + + return yil; } else if (c3y == u3r_sing(monad_bat, sat->match->memread_bat)) { @@ -310,6 +321,8 @@ _reduce_monad(u3_noun monad, lia_state* sat) { return u3m_bail(c3__fail); } + + u3z(monad); return u3nt(0, len_l, u3i_bytes(len_l, (buf_y + ptr_l))); } else if (c3y == u3r_sing(monad_bat, sat->match->memwrite_bat)) @@ -336,6 +349,7 @@ _reduce_monad(u3_noun monad, lia_state* sat) u3r_bytes(0, len_l, (buf_y + ptr_l), u3x_atom(src)); + u3z(monad); return u3nc(0, 0); } else if (c3y == u3r_sing(monad_bat, sat->match->call_ext_bat)) @@ -345,12 +359,15 @@ _reduce_monad(u3_noun monad, lia_state* sat) u3_noun args = u3at(125, monad); if (u3_nul == sat->lia_shop) { - return u3nt(1, u3k(name), u3k(args)); + u3_noun yil = u3nt(1, u3k(name), u3k(args)); + u3z(monad); + return yil; } else { u3_noun lia_buy; u3x_cell(sat->lia_shop, &lia_buy, &sat->lia_shop); + u3z(monad); return u3nc(0, u3k(lia_buy)); } } @@ -360,12 +377,15 @@ _reduce_monad(u3_noun monad, lia_state* sat) u3_noun monad_b = u3at(60, monad); u3_noun cont = u3at(61, monad); - u3_noun yil = _reduce_monad(monad_b, sat); + u3_noun yil = _reduce_monad(u3k(monad_b), sat); if (0 != u3h(yil)) { + u3z(monad); return yil; } u3_noun monad_cont = u3n_slam_on(u3k(cont), u3k(u3t(yil))); + u3z(yil); + u3z(monad); return _reduce_monad(monad_cont, sat); } else if (c3y == u3r_sing(monad_bat, sat->match->catch_bat)) @@ -375,31 +395,40 @@ _reduce_monad(u3_noun monad, lia_state* sat) u3_noun monad_catch = u3at(121, monad); u3_noun cont = u3at(61, monad); - u3_noun yil_try = _reduce_monad(monad_try, sat); + u3_noun yil_try = _reduce_monad(u3k(monad_try), sat); if (0 == u3h(yil_try)) { u3_noun monad_cont = u3n_slam_on(u3k(cont), u3k(u3t(yil_try))); + u3z(yil_try); + u3z(monad); return _reduce_monad(monad_cont, sat); } else if (1 == u3h(yil_try)) { + u3z(monad); return yil_try; } else { - u3_noun yil_catch = _reduce_monad(monad_catch, sat); + u3z(yil_try); + u3_noun yil_catch = _reduce_monad(u3k(monad_catch), sat); if (0 != u3h(yil_catch)) { + u3z(monad); return yil_catch; } u3_noun monad_cont = u3n_slam_on(u3k(cont), u3k(u3t(yil_catch))); + u3z(yil_catch); + u3z(monad); return _reduce_monad(monad_cont, sat); } } else if (c3y == u3r_sing(monad_bat, sat->match->return_bat)) { // return - return u3nc(0, u3at(30, monad)); + u3_noun yil = u3nc(0, u3k(u3at(30, monad))); + u3z(monad); + return yil; } else { @@ -407,6 +436,7 @@ _reduce_monad(u3_noun monad, lia_state* sat) } } +// TRANSFERS sat->arrow_yil if m3Lia_Arrow is thrown static const void * _link_wasm_with_arrow_map( IM3Runtime runtime, @@ -426,12 +456,12 @@ _link_wasm_with_arrow_map( c3_w n_in = _ctx->function->funcType->numArgs; c3_w n_out = _ctx->function->funcType->numRets; c3_y* types = _ctx->function->funcType->types; - void **valptrs_in = u3a_calloc(n_in, sizeof(void*)); + void **valptrs_in = u3a_calloc(n_in, sizeof(void*)); for (c3_w i = 0; i < n_in; i++) { valptrs_in[i] = &_sp[i+n_out]; } - void **valptrs_out = u3a_calloc(n_out, sizeof(void*)); + void **valptrs_out = u3a_calloc(n_out, sizeof(void*)); for (c3_w i = 0; i < n_out; i++) { valptrs_out[i] = &_sp[i]; @@ -444,11 +474,16 @@ _link_wasm_with_arrow_map( if (0 != u3h(yil)) { sat->arrow_yil = yil; + u3a_free(valptrs_in); + u3a_free(valptrs_out); return m3Lia_Arrow; } else { _coins_to_stack(u3t(yil), valptrs_out, n_out, types); + u3z(yil); + u3a_free(valptrs_in); + u3a_free(valptrs_out); return m3Err_none; } } @@ -461,8 +496,14 @@ u3we_lia_run(u3_noun cor) return u3_none; } + #ifdef URWASM_SUBROAD + // enter subroad, 4MB safety buffer u3m_hate(1 << 20); + + #endif + + u3r_mug(cor); u3_noun input = u3at(u3x_sam_2, cor); u3_noun seed = u3at(u3x_sam_6, cor); @@ -509,25 +550,34 @@ u3we_lia_run(u3_noun cor) return u3m_bail(c3__fail); } - u3_noun call_script = u3j_kink(u3j_kink(u3k(u3at(7, cor)), 20), 2); - u3_noun memread_script = u3j_kink(u3j_kink(u3k(u3at(7, cor)), 374), 2); - u3_noun memwrite_script = u3j_kink(u3j_kink(u3k(u3at(7, cor)), 92), 2); - u3_noun call_ext_script = u3j_kink(u3j_kink(u3k(u3at(7, cor)), 2986), 2); - u3_noun try_script = u3j_kink(try_gate_inner, 2); - u3_noun catch_script = u3j_kink(u3j_kink(u3j_kink(u3k(runnable), 4), 2), 2); - u3_noun return_script = u3j_kink(u3j_kink(runnable, 20), 2); + u3_noun call_script = u3j_kink(u3j_kink(u3k(u3at(7, cor)), 20), 2); + u3_noun memread_script = u3j_kink(u3j_kink(u3k(u3at(7, cor)), 374), 2); + u3_noun memwrite_script = u3j_kink(u3j_kink(u3k(u3at(7, cor)), 92), 2); + u3_noun call_ext_script = u3j_kink(u3j_kink(u3k(u3at(7, cor)), 2986), 2); + u3_noun try_script = u3j_kink(try_gate_inner, 2); + u3_noun catch_script = u3j_kink(u3j_kink(u3j_kink(u3k(runnable), 4), 2), 2); + u3_noun return_script = u3j_kink(u3j_kink(runnable, 20), 2); - u3_noun call_bat = u3h(call_script); - u3_noun memread_bat = u3h(memread_script); - u3_noun memwrite_bat = u3h(memwrite_script); - u3_noun call_ext_bat = u3h(call_ext_script); - u3_noun try_bat = u3h(try_script); - u3_noun catch_bat = u3h(catch_script); - u3_noun return_bat = u3h(return_script); - - u3_noun call_ctx = u3at(63, call_script); - u3_noun memread_ctx = u3at(63, memread_script); - u3_noun memwrite_ctx = u3at(63, memwrite_script); + u3_noun call_bat = u3k(u3h(call_script)); + u3_noun memread_bat = u3k(u3h(memread_script)); + u3_noun memwrite_bat = u3k(u3h(memwrite_script)); + u3_noun call_ext_bat = u3k(u3h(call_ext_script)); + u3_noun try_bat = u3k(u3h(try_script)); + u3_noun catch_bat = u3k(u3h(catch_script)); + u3_noun return_bat = u3k(u3h(return_script)); + + u3_noun call_ctx = u3k(u3at(63, call_script)); + u3_noun memread_ctx = u3k(u3at(63, memread_script)); + u3_noun memwrite_ctx = u3k(u3at(63, memwrite_script)); + + u3z(call_script); + u3z(memread_script); + u3z(memwrite_script); + u3z(call_ext_script); + u3z(try_script); + u3z(catch_script); + u3z(return_script); + match_data_struct match = { call_bat, @@ -539,7 +589,7 @@ u3we_lia_run(u3_noun cor) return_bat, call_ctx, memread_ctx, - memwrite_ctx + memwrite_ctx, }; u3_noun octs = u3at(2, seed_new); @@ -634,11 +684,31 @@ u3we_lia_run(u3_noun cor) } else { - yil = _reduce_monad(monad, &sat); + yil = _reduce_monad(u3k(monad), &sat); } + m3_FreeRuntime(wasm3_runtime); + m3_FreeEnvironment(wasm3_env); + + u3a_free(bin_y); + + u3z(match.call_bat); + u3z(match.memread_bat); + u3z(match.memwrite_bat); + u3z(match.call_ext_bat); + u3z(match.try_bat); + u3z(match.catch_bat); + u3z(match.return_bat); + u3z(match.call_ctx); + u3z(match.memread_ctx); + u3z(match.memwrite_ctx); + + #ifdef URWASM_SUBROAD // exit subroad, copying the result u3_noun pro = u3m_love(u3nc(yil, seed_new)); + #else + u3_noun pro = u3nc(yil, seed_new); + #endif return pro; } @@ -651,8 +721,12 @@ u3we_lia_run_once(u3_noun cor) return u3_none; } + #ifdef URWASM_SUBROAD // enter subroad, 4MB safety buffer u3m_hate(1 << 20); + #endif + + u3r_mug(cor); u3_noun runnable = u3j_kink(u3k(u3at(63, cor)), 372); u3_noun try_gate = u3j_kink(u3k(runnable), 21); @@ -666,18 +740,26 @@ u3we_lia_run_once(u3_noun cor) u3_noun catch_script = u3j_kink(u3j_kink(u3j_kink(u3k(runnable), 4), 2), 2); u3_noun return_script = u3j_kink(u3j_kink(runnable, 20), 2); - u3_noun call_bat = u3h(call_script); - u3_noun memread_bat = u3h(memread_script); - u3_noun memwrite_bat = u3h(memwrite_script); - u3_noun call_ext_bat = u3h(call_ext_script); - u3_noun try_bat = u3h(try_script); - u3_noun catch_bat = u3h(catch_script); - u3_noun return_bat = u3h(return_script); - - u3_noun call_ctx = u3at(63, call_script); - u3_noun memread_ctx = u3at(63, memread_script); - u3_noun memwrite_ctx = u3at(63, memwrite_script); - + u3_noun call_bat = u3k(u3h(call_script)); + u3_noun memread_bat = u3k(u3h(memread_script)); + u3_noun memwrite_bat = u3k(u3h(memwrite_script)); + u3_noun call_ext_bat = u3k(u3h(call_ext_script)); + u3_noun try_bat = u3k(u3h(try_script)); + u3_noun catch_bat = u3k(u3h(catch_script)); + u3_noun return_bat = u3k(u3h(return_script)); + + u3_noun call_ctx = u3k(u3at(63, call_script)); + u3_noun memread_ctx = u3k(u3at(63, memread_script)); + u3_noun memwrite_ctx = u3k(u3at(63, memwrite_script)); + + u3z(call_script); + u3z(memread_script); + u3z(memwrite_script); + u3z(call_ext_script); + u3z(try_script); + u3z(catch_script); + u3z(return_script); + match_data_struct match = { call_bat, memread_bat, @@ -688,7 +770,7 @@ u3we_lia_run_once(u3_noun cor) return_bat, call_ctx, memread_ctx, - memwrite_ctx + memwrite_ctx, }; u3_noun octs = u3at(u3x_sam_4, cor); @@ -783,11 +865,31 @@ u3we_lia_run_once(u3_noun cor) } else { - yil = _reduce_monad(monad, &sat); + yil = _reduce_monad(u3k(monad), &sat); } + m3_FreeRuntime(wasm3_runtime); + m3_FreeEnvironment(wasm3_env); + + u3a_free(bin_y); + + u3z(match.call_bat); + u3z(match.memread_bat); + u3z(match.memwrite_bat); + u3z(match.call_ext_bat); + u3z(match.try_bat); + u3z(match.catch_bat); + u3z(match.return_bat); + u3z(match.call_ctx); + u3z(match.memread_ctx); + u3z(match.memwrite_ctx); + + #ifdef URWASM_SUBROAD // exit subroad, copying the result u3_noun pro = u3m_love(yil); + #else + u3_noun pro = yil; + #endif return pro; } From 6db1584cbbe5537e6e79c8e7705e5d85d3ba8a6e Mon Sep 17 00:00:00 2001 From: Quodss Date: Wed, 11 Dec 2024 20:16:03 +0100 Subject: [PATCH 18/22] more monadic ops, macros, printfs, deterministic traps, wasm3 update --- ext/wasm3/build.zig.zon | 4 +- pkg/noun/jets/e/urwasm.c | 619 +++++++++++++++++++++++++++++++-------- 2 files changed, 501 insertions(+), 122 deletions(-) diff --git a/ext/wasm3/build.zig.zon b/ext/wasm3/build.zig.zon index 8f215886c9..c30308e042 100644 --- a/ext/wasm3/build.zig.zon +++ b/ext/wasm3/build.zig.zon @@ -7,8 +7,8 @@ }, .wasm3 = .{ // .path = "../../../../wasm3" - .url = "https://github.com/Quodss/wasm3/archive/5cfd55cdc3b9b5f7d8ac4768155ae96b73222e8e.tar.gz", - .hash = "122032987041f49b62512047a92e8f272c80ae755264fcc24b6f4a3d788dc0406982", + .url = "https://github.com/Quodss/wasm3/archive/3455357e2ead521bb6037ff2352b97a27984d5b9.tar.gz", + .hash = "122065d12d05da26733a8a789f35e3f7e6561f085a84f4fe5569c0337f620bc0dfc9", }, }, .paths = .{ diff --git a/pkg/noun/jets/e/urwasm.c b/pkg/noun/jets/e/urwasm.c index 4fa5f5f004..669492e918 100644 --- a/pkg/noun/jets/e/urwasm.c +++ b/pkg/noun/jets/e/urwasm.c @@ -11,6 +11,33 @@ #include "m3_validate.h" // #define URWASM_SUBROAD +// #define URWASM_STATEFUL + +#define ONCE_CTX 63 +#define RUN_CTX 7 +#define AX_RUNNABLE 3004 +#define AX_CALL 20 +#define AX_MEMREAD 3006 +#define AX_MEMWRITE 750 +#define AX_CALL_EXT 1498 +#define AX_GLOBAL_SET 92 +#define AX_GLOBAL_GET 186 +#define AX_MEM_GROW 12021 +#define AX_MEM_SIZE 748 +#define AX_TRY 21 +#define AX_CATCH 4 +#define AX_RETURN 20 +#define ARROW_CTX 63 +#define MONAD_CTX 15 + +#define arr_sam 62 +#define arr_sam_2 124 +#define arr_sam_3 125 +#define arr_sam_6 250 +#define arr_sam_7 251 + +#define ERR(string) ("\r\n\033[31m>>> " string "\033[0m\r\n") +#define WUT(string) ("\r\n\033[33m>> " string "\033[0m\r\n") static const M3Result m3Lia_Arrow = "non-zero yield from import arrow"; @@ -22,9 +49,18 @@ typedef struct { u3_noun try_bat; u3_noun catch_bat; u3_noun return_bat; + u3_noun global_set_bat; + u3_noun global_get_bat; + u3_noun mem_grow_bat; + u3_noun mem_size_bat; +// u3_noun call_ctx; u3_noun memread_ctx; u3_noun memwrite_ctx; + u3_noun global_set_ctx; + u3_noun global_get_ctx; + u3_noun mem_grow_ctx; + u3_noun mem_size_ctx; } match_data_struct; typedef struct { @@ -65,20 +101,20 @@ _atoms_from_stack(void** valptrs, c3_w n, c3_y* types) } // RETAIN argument -static void +static c3_o _atoms_to_stack(u3_noun atoms, void** valptrs, c3_w n, c3_y* types) { for (c3_w i = 0; i < n; i++) { if (c3y == u3ud(atoms)) { - u3m_bail(c3__fail); + return c3n; } u3_noun atom; u3x_cell(atoms, &atom, &atoms); if (c3n == u3ud(atom)) { - u3m_bail(c3__fail); + return u3m_bail(c3__fail); } switch (types[i]) { @@ -96,14 +132,11 @@ _atoms_to_stack(u3_noun atoms, void** valptrs, c3_w n, c3_y* types) } default: { - u3m_bail(c3__fail); + return u3m_bail(c3__fail); } } } - if (u3_nul != atoms) - { - u3m_bail(c3__fail); - } + return __(u3_nul == atoms); } static u3_noun @@ -144,26 +177,26 @@ _coins_from_stack(void** valptrs, c3_w n, c3_y* types) } // RETAIN argument -static void +static c3_o _coins_to_stack(u3_noun coins, void** valptrs, c3_w n, c3_y* types) { for (c3_w i = 0; i < n; i++) { if (c3y == u3ud(coins)) { - u3m_bail(c3__fail); + return c3n; } u3_noun coin; u3x_cell(coins, &coin, &coins); if (c3y == u3ud(coin)) { - u3m_bail(c3__fail); + return u3m_bail(c3__fail); } u3_noun tag, value; u3x_cell(coin, &tag, &value); if (c3n == u3ud(value)) { - u3m_bail(c3__fail); + return u3m_bail(c3__fail); } switch (types[i]) { @@ -171,7 +204,7 @@ _coins_to_stack(u3_noun coins, void** valptrs, c3_w n, c3_y* types) { if (c3__i32 != tag) { - u3m_bail(c3__fail); + return c3n; } *(c3_w*)valptrs[i] = u3r_word(0, value); break; @@ -180,7 +213,7 @@ _coins_to_stack(u3_noun coins, void** valptrs, c3_w n, c3_y* types) { if (c3__i64 != tag) { - u3m_bail(c3__fail); + return c3n; } *(c3_d*)valptrs[i] = u3r_chub(0, value); break; @@ -189,7 +222,7 @@ _coins_to_stack(u3_noun coins, void** valptrs, c3_w n, c3_y* types) { if (c3__f32 != tag) { - u3m_bail(c3__fail); + return c3n; } *(c3_w*)valptrs[i] = u3r_word(0, value); break; @@ -198,37 +231,52 @@ _coins_to_stack(u3_noun coins, void** valptrs, c3_w n, c3_y* types) { if (c3__f64 != tag) { - u3m_bail(c3__fail); + return c3n; } *(c3_d*)valptrs[i] = u3r_chub(0, value); break; } default: { - u3m_bail(c3__fail); + return u3m_bail(c3__fail); } } } - if (u3_nul != coins) + return __(u3_nul == coins); +} + +static c3_t +_deterministic_trap(M3Result result) +{ + if ( result == m3Err_trapOutOfBoundsMemoryAccess + || result == m3Err_trapDivisionByZero + || result == m3Err_trapIntegerOverflow + || result == m3Err_trapIntegerConversion + || result == m3Err_trapIndirectCallTypeMismatch + || result == m3Err_trapTableIndexOutOfRange + || result == m3Err_trapTableElementIsNull ) { - u3m_bail(c3__fail); + return 1; + } + else + { + return 0; } } - static u3_noun _reduce_monad(u3_noun monad, lia_state* sat) { u3_noun monad_bat = u3h(monad); if (c3y == u3r_sing(monad_bat, sat->match->call_bat)) { - if (c3n == u3r_sing(u3at(63, monad), sat->match->call_ctx)) + if (c3n == u3r_sing(u3at(ARROW_CTX, monad), sat->match->call_ctx)) { return u3m_bail(c3__fail); } // call - u3_atom name = u3x_atom(u3at(124, monad)); - u3_noun args = u3at(125, monad); + u3_atom name = u3x_atom(u3at(arr_sam_2, monad)); + u3_noun args = u3at(arr_sam_3, monad); c3_w met_w = u3r_met(3, name); c3_c* name_c = u3a_malloc(met_w + 1); @@ -242,6 +290,7 @@ _reduce_monad(u3_noun monad, lia_state* sat) if (result) { + fprintf(stderr, ERR("function %s search error: %s"), name_c, result); return u3m_bail(c3__fail); } @@ -263,7 +312,11 @@ _reduce_monad(u3_noun monad, lia_state* sat) valptrs_out[i] = &vals_out[i]; } - _atoms_to_stack(args, valptrs_in, n_in, (types+n_out)); + if (c3n == _atoms_to_stack(args, valptrs_in, n_in, (types+n_out))) + { + fprintf(stderr, ERR("function %s wrong number of args"), name_c); + return u3m_bail(c3__fail); + } result = m3_Call(f, n_in, (const void**)valptrs_in); @@ -278,9 +331,14 @@ _reduce_monad(u3_noun monad, lia_state* sat) return u3m_bail(c3__fail); } } + else if (_deterministic_trap(result)) + { + fprintf(stderr, WUT("%s call trapped: %s"), name_c, result); + yil = u3nc(2, 0); + } else if (result) { - fprintf(stderr, "\r\ncall failed: %s \r\n", name_c); + fprintf(stderr, ERR("%s call failed: %s"), name_c, result); return u3m_bail(c3__fail); } else @@ -288,6 +346,7 @@ _reduce_monad(u3_noun monad, lia_state* sat) result = m3_GetResults(f, n_out, (const void**)valptrs_out); if (result) { + fprintf(stderr, ERR("function %s failed to get results"), name_c); return u3m_bail(c3__fail); } yil = u3nc(0, _atoms_from_stack(valptrs_out, n_out, types)); @@ -304,50 +363,64 @@ _reduce_monad(u3_noun monad, lia_state* sat) } else if (c3y == u3r_sing(monad_bat, sat->match->memread_bat)) { - if (c3n == u3r_sing(u3at(63, monad), sat->match->memread_ctx)) + if (c3n == u3r_sing(u3at(ARROW_CTX, monad), sat->match->memread_ctx)) { return u3m_bail(c3__fail); } // memread - u3_noun ptr = u3at(124, monad); - u3_noun len = u3at(125, monad); + u3_atom ptr = u3x_atom(u3at(arr_sam_2, monad)); + u3_noun len = u3at(arr_sam_3, monad); - c3_l ptr_l = (c3y == u3a_is_cat(ptr)) ? ptr : u3m_bail(c3__fail); + c3_w ptr_w = u3r_word(0, ptr); c3_l len_l = (c3y == u3a_is_cat(len)) ? len : u3m_bail(c3__fail); c3_w len_buf_w; c3_y* buf_y = m3_GetMemory(sat->wasm_module->runtime, &len_buf_w, 0); - if ( (buf_y == NULL) || (ptr_l + len_l > len_buf_w) ) + if (buf_y == NULL) + { + fprintf(stderr, ERR("memread failed to get memory")); + return u3m_bail(c3__fail); + } + + if (ptr_w + len_l > len_buf_w) { + fprintf(stderr, ERR("memread out of bounds")); return u3m_bail(c3__fail); } u3z(monad); - return u3nt(0, len_l, u3i_bytes(len_l, (buf_y + ptr_l))); + return u3nt(0, len_l, u3i_bytes(len_l, (buf_y + ptr_w))); } else if (c3y == u3r_sing(monad_bat, sat->match->memwrite_bat)) { - if (c3n == u3r_sing(u3at(63, monad), sat->match->memwrite_ctx)) + if (c3n == u3r_sing(u3at(ARROW_CTX, monad), sat->match->memwrite_ctx)) { return u3m_bail(c3__fail); } // memwrite - u3_noun ptr = u3at(124, monad); - u3_noun len = u3at(250, monad); - u3_noun src = u3at(251, monad); + u3_atom ptr = u3x_atom(u3at(arr_sam_2, monad)); + u3_noun len = u3at(arr_sam_6, monad); + u3_noun src = u3at(arr_sam_7, monad); - c3_l ptr_l = (c3y == u3a_is_cat(ptr)) ? ptr : u3m_bail(c3__fail); + c3_w ptr_w = u3r_word(0, ptr); c3_l len_l = (c3y == u3a_is_cat(len)) ? len : u3m_bail(c3__fail); c3_w len_buf_w; c3_y* buf_y = m3_GetMemory(sat->wasm_module->runtime, &len_buf_w, 0); - if ( (buf_y == NULL) || (ptr_l + len_l > len_buf_w) ) + if (buf_y == NULL) { + fprintf(stderr, ERR("memwrite failed to get memory")); return u3m_bail(c3__fail); } - u3r_bytes(0, len_l, (buf_y + ptr_l), u3x_atom(src)); + if (ptr_w + len_l > len_buf_w) + { + fprintf(stderr, ERR("memwrite out of bounds")); + return u3m_bail(c3__fail); + } + + u3r_bytes(0, len_l, (buf_y + ptr_w), u3x_atom(src)); u3z(monad); return u3nc(0, 0); @@ -355,8 +428,8 @@ _reduce_monad(u3_noun monad, lia_state* sat) else if (c3y == u3r_sing(monad_bat, sat->match->call_ext_bat)) { // call-ext - u3_noun name = u3at(124, monad); - u3_noun args = u3at(125, monad); + u3_noun name = u3at(arr_sam_2, monad); + u3_noun args = u3at(arr_sam_3, monad); if (u3_nul == sat->lia_shop) { u3_noun yil = u3nt(1, u3k(name), u3k(args)); @@ -376,17 +449,27 @@ _reduce_monad(u3_noun monad, lia_state* sat) // try u3_noun monad_b = u3at(60, monad); u3_noun cont = u3at(61, monad); - - u3_noun yil = _reduce_monad(u3k(monad_b), sat); - if (0 != u3h(yil)) + u3_weak yil; + u3_noun monad_cont; { - u3z(monad); - return yil; + yil = _reduce_monad(u3k(monad_b), sat); + if (0 == u3h(yil)) + { + monad_cont = u3n_slam_on(u3k(cont), u3k(u3t(yil))); + u3z(yil); + yil = u3_none; + } } - u3_noun monad_cont = u3n_slam_on(u3k(cont), u3k(u3t(yil))); - u3z(yil); + u3z(monad); - return _reduce_monad(monad_cont, sat); + if (u3_none == yil) + { + return _reduce_monad(monad_cont, sat); + } + else + { + return yil; + } } else if (c3y == u3r_sing(monad_bat, sat->match->catch_bat)) { @@ -394,33 +477,38 @@ _reduce_monad(u3_noun monad, lia_state* sat) u3_noun monad_try = u3at(120, monad); u3_noun monad_catch = u3at(121, monad); u3_noun cont = u3at(61, monad); + u3_weak yil; + u3_noun monad_cont; - u3_noun yil_try = _reduce_monad(u3k(monad_try), sat); - if (0 == u3h(yil_try)) { - u3_noun monad_cont = u3n_slam_on(u3k(cont), u3k(u3t(yil_try))); - u3z(yil_try); - u3z(monad); - return _reduce_monad(monad_cont, sat); + yil = _reduce_monad(u3k(monad_try), sat); + if (0 == u3h(yil)) + { + monad_cont = u3n_slam_on(u3k(cont), u3k(u3t(yil))); + u3z(yil); + yil = u3_none; + } + else if (2 == u3h(yil)) + { + u3z(yil); + yil = _reduce_monad(u3k(monad_catch), sat); + if (0 == u3h(yil)) + { + monad_cont = u3n_slam_on(u3k(cont), u3k(u3t(yil))); + u3z(yil); + yil = u3_none; + } + } } - else if (1 == u3h(yil_try)) + + u3z(monad); + if (u3_none == yil) { - u3z(monad); - return yil_try; + return _reduce_monad(monad_cont, sat); } else { - u3z(yil_try); - u3_noun yil_catch = _reduce_monad(u3k(monad_catch), sat); - if (0 != u3h(yil_catch)) - { - u3z(monad); - return yil_catch; - } - u3_noun monad_cont = u3n_slam_on(u3k(cont), u3k(u3t(yil_catch))); - u3z(yil_catch); - u3z(monad); - return _reduce_monad(monad_cont, sat); + return yil; } } else if (c3y == u3r_sing(monad_bat, sat->match->return_bat)) @@ -430,6 +518,185 @@ _reduce_monad(u3_noun monad, lia_state* sat) u3z(monad); return yil; } + else if (c3y == u3r_sing(monad_bat, sat->match->global_set_bat)) + { + if (c3n == u3r_sing(u3at(ARROW_CTX, monad), sat->match->global_set_ctx)) + { + return u3m_bail(c3__fail); + } + // global-set + u3_atom name = u3x_atom(u3at(arr_sam_2, monad)); + u3_atom value = u3x_atom(u3at(arr_sam_3, monad)); + + c3_w met_w = u3r_met(3, name); + c3_c* name_c = u3a_malloc(met_w + 1); + u3r_bytes(0, met_w, (c3_y*)name_c, name); + name_c[met_w] = 0; + + IM3Global glob = m3_FindGlobal(sat->wasm_module, name_c); + + if (!glob) + { + fprintf(stderr, ERR("global %s not found"), name_c); + return u3m_bail(c3__fail); + } + + M3TaggedValue glob_value; + M3Result result = m3_GetGlobal(glob, &glob_value); + if (result) + { + fprintf(stderr, ERR("couldn't get global %s: %s"), name_c, result); + return u3m_bail(c3__fail); + } + switch (glob_value.type) + { + default: + { + return u3m_bail(c3__fail); + } + case c_m3Type_i32: + { + glob_value.value.i32 = u3r_word(0, value); + break; + } + case c_m3Type_i64: + { + glob_value.value.i64 = u3r_chub(0, value); + break; + } + case c_m3Type_f32: + { + glob_value.value.i32 = u3r_word(0, value); + break; + } + case c_m3Type_f64: + { + glob_value.value.i64 = u3r_chub(0, value); + break; + } + } + result = m3_SetGlobal(glob, &glob_value); + if (result) + { + fprintf(stderr, ERR("couldn't set global %s: %s"), name_c, result); + return u3m_bail(c3__fail); + } + u3z(monad); + u3a_free(name_c); + return u3nc(0, 0); + } + else if (c3y == u3r_sing(monad_bat, sat->match->global_get_bat)) + { + if (c3n == u3r_sing(u3at(ARROW_CTX, monad), sat->match->global_get_ctx)) + { + return u3m_bail(c3__fail); + } + // global-get + u3_atom name = u3x_atom(u3at(arr_sam, monad)); + + c3_w met_w = u3r_met(3, name); + c3_c* name_c = u3a_malloc(met_w + 1); + u3r_bytes(0, met_w, (c3_y*)name_c, name); + name_c[met_w] = 0; + + IM3Global glob = m3_FindGlobal(sat->wasm_module, name_c); + if (!glob) + { + fprintf(stderr, ERR("global %s not found"), name_c); + return u3m_bail(c3__fail); + } + + M3TaggedValue glob_value; + M3Result result = m3_GetGlobal(glob, &glob_value); + if (result) + { + fprintf(stderr, ERR("couldn't get global %s: %s"), name_c, result); + return u3m_bail(c3__fail); + } + + u3_noun out; + switch (glob_value.type) + { + default: + { + return u3m_bail(c3__fail); + } + case c_m3Type_i32: + { + out = u3i_word(glob_value.value.i32); + break; + } + case c_m3Type_i64: + { + out = u3i_chub(glob_value.value.i64); + break; + } + case c_m3Type_f32: + { + out = u3i_word(glob_value.value.f32); + break; + } + case c_m3Type_f64: + { + out = u3i_chub(glob_value.value.f64); + break; + } + } + + u3z(monad); + u3a_free(name_c); + return u3nc(0, out); + + } + else if (c3y == u3r_sing(monad_bat, sat->match->mem_size_bat)) + { + if (c3n == u3r_sing(u3at(MONAD_CTX, monad), sat->match->mem_size_ctx)) + { + return u3m_bail(c3__fail); + } + // memory-size + if (!sat->wasm_module->memoryInfo.hasMemory) + { + fprintf(stderr, ERR("memsize no memory")); + return u3m_bail(c3__fail); + } + c3_w num_pages = sat->wasm_module->runtime->memory.numPages; + + u3z(monad); + return u3nc(0, u3i_word(num_pages)); + } + else if (c3y == u3r_sing(monad_bat, sat->match->mem_grow_bat)) + { + if (c3n == u3r_sing(u3at(ARROW_CTX, monad), sat->match->mem_grow_ctx)) + { + return u3m_bail(c3__fail); + } + // memory-grow + if (!sat->wasm_module->memoryInfo.hasMemory) + { + fprintf(stderr, ERR("memgrow no memory")); + return u3m_bail(c3__fail); + } + + u3_noun delta = u3at(arr_sam, monad); + + c3_l delta_l = (c3y == u3a_is_cat(delta)) ? delta : u3m_bail(c3__fail); + + c3_w n_pages = sat->wasm_module->runtime->memory.numPages; + c3_w required_pages = n_pages + delta_l; + + M3Result result = ResizeMemory(sat->wasm_module->runtime, required_pages); + + if (result) + { + fprintf(stderr, ERR("failed to resize memory: %s"), result); + return u3m_bail(c3__fail); + } + + u3z(monad); + return u3nc(0, u3i_word(n_pages)); + + } else { return u3m_bail(c3__fail); @@ -449,10 +716,13 @@ _link_wasm_with_arrow_map( const char *name = _ctx->function->import.fieldUtf8; lia_state* sat = _ctx->userdata; - M3Result result; - u3_noun key = u3nc(u3i_string(mod), u3i_string(name)); - u3_noun arrow = u3kdb_got(u3k(sat->import), key); + u3_weak arrow = u3kdb_get(u3k(sat->import), key); + if (u3_none == arrow) + { + fprintf(stderr, ERR("import not found: %s/%s"), mod, name); + return m3Err_functionImportMissing; + } c3_w n_in = _ctx->function->funcType->numArgs; c3_w n_out = _ctx->function->funcType->numRets; c3_y* types = _ctx->function->funcType->types; @@ -480,17 +750,31 @@ _link_wasm_with_arrow_map( } else { - _coins_to_stack(u3t(yil), valptrs_out, n_out, types); - u3z(yil); - u3a_free(valptrs_in); - u3a_free(valptrs_out); - return m3Err_none; + if (c3n == _coins_to_stack(u3t(yil), valptrs_out, n_out, types)) + { + u3z(yil); + u3a_free(valptrs_in); + u3a_free(valptrs_out); + fprintf(stderr, ERR("import result type mismatch: %s/%s"), mod, name); + return "import result type mismatch"; + } + else + { + u3z(yil); + u3a_free(valptrs_in); + u3a_free(valptrs_out); + return m3Err_none; + } } } u3_weak u3we_lia_run(u3_noun cor) { +#ifndef URWASM_STATEFUL + return u3_none; +} +#else if (c3__none == u3at(u3x_sam_7, cor)) { return u3_none; @@ -508,8 +792,8 @@ u3we_lia_run(u3_noun cor) u3_noun input = u3at(u3x_sam_2, cor); u3_noun seed = u3at(u3x_sam_6, cor); - u3_noun runnable = u3j_kink(u3k(u3at(7, cor)), 372); - u3_noun try_gate = u3j_kink(u3k(runnable), 21); + u3_noun runnable = u3j_kink(u3k(u3at(RUN_CTX, cor)), AX_RUNNABLE); + u3_noun try_gate = u3j_kink(u3k(runnable), AX_TRY); u3_noun try_gate_inner = u3j_kink(try_gate, 2); u3_noun seed_new; @@ -550,13 +834,18 @@ u3we_lia_run(u3_noun cor) return u3m_bail(c3__fail); } - u3_noun call_script = u3j_kink(u3j_kink(u3k(u3at(7, cor)), 20), 2); - u3_noun memread_script = u3j_kink(u3j_kink(u3k(u3at(7, cor)), 374), 2); - u3_noun memwrite_script = u3j_kink(u3j_kink(u3k(u3at(7, cor)), 92), 2); - u3_noun call_ext_script = u3j_kink(u3j_kink(u3k(u3at(7, cor)), 2986), 2); + u3_noun call_script = u3j_kink(u3j_kink(u3k(u3at(RUN_CTX, cor)), AX_CALL), 2); + u3_noun memread_script = u3j_kink(u3j_kink(u3k(u3at(RUN_CTX, cor)), AX_MEMREAD), 2); + u3_noun memwrite_script = u3j_kink(u3j_kink(u3k(u3at(RUN_CTX, cor)), AX_MEMWRITE), 2); + u3_noun call_ext_script = u3j_kink(u3j_kink(u3k(u3at(RUN_CTX, cor)), AX_CALL_EXT), 2); + u3_noun global_set_script = u3j_kink(u3j_kink(u3k(u3at(RUN_CTX, cor)), AX_GLOBAL_SET), 2); + u3_noun global_get_script = u3j_kink(u3j_kink(u3k(u3at(RUN_CTX, cor)), AX_GLOBAL_GET), 2); + u3_noun mem_grow_script = u3j_kink(u3j_kink(u3k(u3at(RUN_CTX, cor)), AX_MEM_GROW), 2); + u3_noun mem_size_script = u3j_kink(u3k(u3at(RUN_CTX, cor)), AX_MEM_SIZE); + u3_noun try_script = u3j_kink(try_gate_inner, 2); - u3_noun catch_script = u3j_kink(u3j_kink(u3j_kink(u3k(runnable), 4), 2), 2); - u3_noun return_script = u3j_kink(u3j_kink(runnable, 20), 2); + u3_noun catch_script = u3j_kink(u3j_kink(u3j_kink(u3k(runnable), AX_CATCH), 2), 2); + u3_noun return_script = u3j_kink(u3j_kink(runnable, AX_RETURN), 2); u3_noun call_bat = u3k(u3h(call_script)); u3_noun memread_bat = u3k(u3h(memread_script)); @@ -565,10 +854,18 @@ u3we_lia_run(u3_noun cor) u3_noun try_bat = u3k(u3h(try_script)); u3_noun catch_bat = u3k(u3h(catch_script)); u3_noun return_bat = u3k(u3h(return_script)); - - u3_noun call_ctx = u3k(u3at(63, call_script)); - u3_noun memread_ctx = u3k(u3at(63, memread_script)); - u3_noun memwrite_ctx = u3k(u3at(63, memwrite_script)); + u3_noun global_set_bat = u3k(u3h(global_set_script)); + u3_noun global_get_bat = u3k(u3h(global_get_script)); + u3_noun mem_grow_bat = u3k(u3h(mem_grow_script)); + u3_noun mem_size_bat = u3k(u3h(mem_size_script)); + + u3_noun call_ctx = u3k(u3at(ARROW_CTX, call_script)); + u3_noun memread_ctx = u3k(u3at(ARROW_CTX, memread_script)); + u3_noun memwrite_ctx = u3k(u3at(ARROW_CTX, memwrite_script)); + u3_noun global_set_ctx = u3k(u3at(ARROW_CTX, global_set_script)); + u3_noun global_get_ctx = u3k(u3at(ARROW_CTX, global_get_script)); + u3_noun mem_grow_ctx = u3k(u3at(ARROW_CTX, mem_grow_script)); + u3_noun mem_size_ctx = u3k(u3at(MONAD_CTX, mem_grow_script)); u3z(call_script); u3z(memread_script); @@ -577,8 +874,11 @@ u3we_lia_run(u3_noun cor) u3z(try_script); u3z(catch_script); u3z(return_script); - - + u3z(global_set_script); + u3z(global_get_script); + u3z(mem_grow_script); + u3z(mem_size_script); + match_data_struct match = { call_bat, memread_bat, @@ -587,9 +887,18 @@ u3we_lia_run(u3_noun cor) try_bat, catch_bat, return_bat, + global_set_bat, + global_get_bat, + mem_grow_bat, + mem_size_bat, + // call_ctx, memread_ctx, memwrite_ctx, + global_set_ctx, + global_get_ctx, + mem_grow_ctx, + mem_size_ctx, }; u3_noun octs = u3at(2, seed_new); @@ -599,21 +908,28 @@ u3we_lia_run(u3_noun cor) c3_w bin_len_w = (c3y == u3a_is_cat(p_octs)) ? p_octs : u3m_bail(c3__fail); c3_y* bin_y = u3r_bytes_alloc(0, bin_len_w, u3x_atom(q_octs)); - m3_SetAllocators(u3a_calloc, u3a_free, u3a_realloc); + M3Result result; + + result = m3_SetAllocators(u3a_calloc, u3a_free, u3a_realloc); + + if (result) + { + fprintf(stderr, ERR("set allocators fail: %s"), result); + return u3m_bail(c3__fail); + } IM3Environment wasm3_env = m3_NewEnvironment(); if (!wasm3_env) { - fprintf(stderr, "env is null\r\n"); + fprintf(stderr, ERR("env is null")); return u3m_bail(c3__fail); } - M3Result result; // 2MB stack IM3Runtime wasm3_runtime = m3_NewRuntime(wasm3_env, 1 << 21, NULL); if (!wasm3_runtime) { - fprintf(stderr, "runtime is null\r\n"); + fprintf(stderr, ERR("runtime is null")); return u3m_bail(c3__fail); } @@ -621,21 +937,21 @@ u3we_lia_run(u3_noun cor) result = m3_ParseModule(wasm3_env, &wasm3_module, bin_y, bin_len_w); if (result) { - fprintf(stderr, "parse binary error: %s\r\n", result); + fprintf(stderr, ERR("parse binary error: %s"), result); return u3m_bail(c3__fail); } result = m3_LoadModule(wasm3_runtime, wasm3_module); if (result) { - fprintf(stderr, "load module error: %s\r\n", result); + fprintf(stderr, ERR("load module error: %s"), result); return u3m_bail(c3__fail); } result = m3_ValidateModule(wasm3_module); if (result) { - fprintf(stderr, "validation error: %s\r\n", result); + fprintf(stderr, ERR("validation error: %s"), result); return u3m_bail(c3__fail); } @@ -660,7 +976,7 @@ u3we_lia_run(u3_noun cor) if (result) { - fprintf(stderr, "link error"); + fprintf(stderr, ERR("link error: %s"), result); return u3m_bail(c3__fail); } } @@ -678,8 +994,14 @@ u3we_lia_run(u3_noun cor) return u3m_bail(c3__fail); } } + else if (_deterministic_trap(result)) + { + fprintf(stderr, WUT("start function call trapped: %s"), result); + yil = u3nc(2, 0); + } else if (result) { + fprintf(stderr, ERR("start function failed: %s"), result); return u3m_bail(c3__fail); } else @@ -699,9 +1021,18 @@ u3we_lia_run(u3_noun cor) u3z(match.try_bat); u3z(match.catch_bat); u3z(match.return_bat); + u3z(match.global_set_bat); + u3z(match.global_get_bat); + u3z(match.mem_grow_bat); + u3z(match.mem_size_bat); + u3z(match.call_ctx); u3z(match.memread_ctx); u3z(match.memwrite_ctx); + u3z(global_set_ctx); + u3z(global_get_ctx); + u3z(mem_grow_ctx); + u3z(mem_size_ctx); #ifdef URWASM_SUBROAD // exit subroad, copying the result @@ -713,6 +1044,8 @@ u3we_lia_run(u3_noun cor) return pro; } +#endif // URWASM_STATEFUL + u3_weak u3we_lia_run_once(u3_noun cor) { @@ -728,17 +1061,20 @@ u3we_lia_run_once(u3_noun cor) u3r_mug(cor); - u3_noun runnable = u3j_kink(u3k(u3at(63, cor)), 372); - u3_noun try_gate = u3j_kink(u3k(runnable), 21); - u3_noun try_gate_inner = u3j_kink(try_gate, 2); + u3_noun runnable = u3j_kink(u3k(u3at(ONCE_CTX, cor)), AX_RUNNABLE); - u3_noun call_script = u3j_kink(u3j_kink(u3k(u3at(63, cor)), 20), 2); - u3_noun memread_script = u3j_kink(u3j_kink(u3k(u3at(63, cor)), 374), 2); - u3_noun memwrite_script = u3j_kink(u3j_kink(u3k(u3at(63, cor)), 92), 2); - u3_noun call_ext_script = u3j_kink(u3j_kink(u3k(u3at(63, cor)), 2986), 2); - u3_noun try_script = u3j_kink(try_gate_inner, 2); - u3_noun catch_script = u3j_kink(u3j_kink(u3j_kink(u3k(runnable), 4), 2), 2); - u3_noun return_script = u3j_kink(u3j_kink(runnable, 20), 2); + u3_noun call_script = u3j_kink(u3j_kink(u3k(u3at(ONCE_CTX, cor)), AX_CALL), 2); + u3_noun memread_script = u3j_kink(u3j_kink(u3k(u3at(ONCE_CTX, cor)), AX_MEMREAD), 2); + u3_noun memwrite_script = u3j_kink(u3j_kink(u3k(u3at(ONCE_CTX, cor)), AX_MEMWRITE), 2); + u3_noun call_ext_script = u3j_kink(u3j_kink(u3k(u3at(ONCE_CTX, cor)), AX_CALL_EXT), 2); + u3_noun global_set_script = u3j_kink(u3j_kink(u3k(u3at(ONCE_CTX, cor)), AX_GLOBAL_SET), 2); + u3_noun global_get_script = u3j_kink(u3j_kink(u3k(u3at(ONCE_CTX, cor)), AX_GLOBAL_GET), 2); + u3_noun mem_grow_script = u3j_kink(u3j_kink(u3k(u3at(ONCE_CTX, cor)), AX_MEM_GROW), 2); + u3_noun mem_size_script = u3j_kink(u3k(u3at(ONCE_CTX, cor)), AX_MEM_SIZE); + + u3_noun try_script = u3j_kink(u3j_kink(u3j_kink(u3k(runnable), AX_TRY), 2), 2); + u3_noun catch_script = u3j_kink(u3j_kink(u3j_kink(u3k(runnable), AX_CATCH), 2), 2); + u3_noun return_script = u3j_kink(u3j_kink(runnable, AX_RETURN), 2); u3_noun call_bat = u3k(u3h(call_script)); u3_noun memread_bat = u3k(u3h(memread_script)); @@ -747,10 +1083,18 @@ u3we_lia_run_once(u3_noun cor) u3_noun try_bat = u3k(u3h(try_script)); u3_noun catch_bat = u3k(u3h(catch_script)); u3_noun return_bat = u3k(u3h(return_script)); - - u3_noun call_ctx = u3k(u3at(63, call_script)); - u3_noun memread_ctx = u3k(u3at(63, memread_script)); - u3_noun memwrite_ctx = u3k(u3at(63, memwrite_script)); + u3_noun global_set_bat = u3k(u3h(global_set_script)); + u3_noun global_get_bat = u3k(u3h(global_get_script)); + u3_noun mem_grow_bat = u3k(u3h(mem_grow_script)); + u3_noun mem_size_bat = u3k(u3h(mem_size_script)); + + u3_noun call_ctx = u3k(u3at(ARROW_CTX, call_script)); + u3_noun memread_ctx = u3k(u3at(ARROW_CTX, memread_script)); + u3_noun memwrite_ctx = u3k(u3at(ARROW_CTX, memwrite_script)); + u3_noun global_set_ctx = u3k(u3at(ARROW_CTX, global_set_script)); + u3_noun global_get_ctx = u3k(u3at(ARROW_CTX, global_get_script)); + u3_noun mem_grow_ctx = u3k(u3at(ARROW_CTX, mem_grow_script)); + u3_noun mem_size_ctx = u3k(u3at(MONAD_CTX, mem_grow_script)); u3z(call_script); u3z(memread_script); @@ -759,6 +1103,10 @@ u3we_lia_run_once(u3_noun cor) u3z(try_script); u3z(catch_script); u3z(return_script); + u3z(global_set_script); + u3z(global_get_script); + u3z(mem_grow_script); + u3z(mem_size_script); match_data_struct match = { call_bat, @@ -768,9 +1116,18 @@ u3we_lia_run_once(u3_noun cor) try_bat, catch_bat, return_bat, + global_set_bat, + global_get_bat, + mem_grow_bat, + mem_size_bat, + // call_ctx, memread_ctx, memwrite_ctx, + global_set_ctx, + global_get_ctx, + mem_grow_ctx, + mem_size_ctx, }; u3_noun octs = u3at(u3x_sam_4, cor); @@ -780,21 +1137,28 @@ u3we_lia_run_once(u3_noun cor) c3_w bin_len_w = (c3y == u3a_is_cat(p_octs)) ? p_octs : u3m_bail(c3__fail); c3_y* bin_y = u3r_bytes_alloc(0, bin_len_w, u3x_atom(q_octs)); - m3_SetAllocators(u3a_calloc, u3a_free, u3a_realloc); + M3Result result; + + result = m3_SetAllocators(u3a_calloc, u3a_free, u3a_realloc); + + if (result) + { + fprintf(stderr, ERR("set allocators fail: %s"), result); + return u3m_bail(c3__fail); + } IM3Environment wasm3_env = m3_NewEnvironment(); if (!wasm3_env) { - fprintf(stderr, "env is null\r\n"); + fprintf(stderr, ERR("env is null")); return u3m_bail(c3__fail); } - M3Result result; // 2MB stack IM3Runtime wasm3_runtime = m3_NewRuntime(wasm3_env, 1 << 21, NULL); if (!wasm3_runtime) { - fprintf(stderr, "runtime is null\r\n"); + fprintf(stderr, ERR("runtime is null")); return u3m_bail(c3__fail); } @@ -802,21 +1166,21 @@ u3we_lia_run_once(u3_noun cor) result = m3_ParseModule(wasm3_env, &wasm3_module, bin_y, bin_len_w); if (result) { - fprintf(stderr, "parse binary error: %s\r\n", result); + fprintf(stderr, ERR("parse binary error: %s"), result); return u3m_bail(c3__fail); } result = m3_LoadModule(wasm3_runtime, wasm3_module); if (result) { - fprintf(stderr, "load module error: %s\r\n", result); + fprintf(stderr, ERR("load module error: %s"), result); return u3m_bail(c3__fail); } result = m3_ValidateModule(wasm3_module); if (result) { - fprintf(stderr, "validation error: %s\r\n", result); + fprintf(stderr, ERR("validation error: %s"), result); return u3m_bail(c3__fail); } @@ -841,7 +1205,7 @@ u3we_lia_run_once(u3_noun cor) if (result) { - fprintf(stderr, "link error"); + fprintf(stderr, ERR("link error: %s"), result); return u3m_bail(c3__fail); } } @@ -859,8 +1223,14 @@ u3we_lia_run_once(u3_noun cor) return u3m_bail(c3__fail); } } + else if (_deterministic_trap(result)) + { + fprintf(stderr, WUT("start function call trapped: %s"), result); + yil = u3nc(2, 0); + } else if (result) { + fprintf(stderr, ERR("start function failed: %s"), result); return u3m_bail(c3__fail); } else @@ -880,9 +1250,18 @@ u3we_lia_run_once(u3_noun cor) u3z(match.try_bat); u3z(match.catch_bat); u3z(match.return_bat); + u3z(match.global_set_bat); + u3z(match.global_get_bat); + u3z(match.mem_grow_bat); + u3z(match.mem_size_bat); + u3z(match.call_ctx); u3z(match.memread_ctx); u3z(match.memwrite_ctx); + u3z(global_set_ctx); + u3z(global_get_ctx); + u3z(mem_grow_ctx); + u3z(mem_size_ctx); #ifdef URWASM_SUBROAD // exit subroad, copying the result From 88a411cc82cd098f26e446822582b6918fe7f34c Mon Sep 17 00:00:00 2001 From: Quodss Date: Thu, 12 Dec 2024 15:17:20 +0100 Subject: [PATCH 19/22] fix copypaste typo, update wasm3 --- ext/wasm3/build.zig.zon | 4 ++-- pkg/noun/jets/e/urwasm.c | 34 +++++++++++++++++++--------------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/ext/wasm3/build.zig.zon b/ext/wasm3/build.zig.zon index c30308e042..8ff49866f0 100644 --- a/ext/wasm3/build.zig.zon +++ b/ext/wasm3/build.zig.zon @@ -7,8 +7,8 @@ }, .wasm3 = .{ // .path = "../../../../wasm3" - .url = "https://github.com/Quodss/wasm3/archive/3455357e2ead521bb6037ff2352b97a27984d5b9.tar.gz", - .hash = "122065d12d05da26733a8a789f35e3f7e6561f085a84f4fe5569c0337f620bc0dfc9", + .url = "https://github.com/Quodss/wasm3/archive/a8ccdb9a96eda023730e5b876c37e1550a4d18f9.tar.gz", + .hash = "1220ccd506e41df3d84563f3d997911e6d628ed7d7490854193215c6ac2dc69a3586", }, }, .paths = .{ diff --git a/pkg/noun/jets/e/urwasm.c b/pkg/noun/jets/e/urwasm.c index 669492e918..44469a82f5 100644 --- a/pkg/noun/jets/e/urwasm.c +++ b/pkg/noun/jets/e/urwasm.c @@ -1059,22 +1059,26 @@ u3we_lia_run_once(u3_noun cor) u3m_hate(1 << 20); #endif - u3r_mug(cor); + u3_noun ctx = u3at(ONCE_CTX, cor); + u3r_mug(ctx); u3_noun runnable = u3j_kink(u3k(u3at(ONCE_CTX, cor)), AX_RUNNABLE); - - u3_noun call_script = u3j_kink(u3j_kink(u3k(u3at(ONCE_CTX, cor)), AX_CALL), 2); - u3_noun memread_script = u3j_kink(u3j_kink(u3k(u3at(ONCE_CTX, cor)), AX_MEMREAD), 2); - u3_noun memwrite_script = u3j_kink(u3j_kink(u3k(u3at(ONCE_CTX, cor)), AX_MEMWRITE), 2); - u3_noun call_ext_script = u3j_kink(u3j_kink(u3k(u3at(ONCE_CTX, cor)), AX_CALL_EXT), 2); - u3_noun global_set_script = u3j_kink(u3j_kink(u3k(u3at(ONCE_CTX, cor)), AX_GLOBAL_SET), 2); - u3_noun global_get_script = u3j_kink(u3j_kink(u3k(u3at(ONCE_CTX, cor)), AX_GLOBAL_GET), 2); - u3_noun mem_grow_script = u3j_kink(u3j_kink(u3k(u3at(ONCE_CTX, cor)), AX_MEM_GROW), 2); - u3_noun mem_size_script = u3j_kink(u3k(u3at(ONCE_CTX, cor)), AX_MEM_SIZE); - - u3_noun try_script = u3j_kink(u3j_kink(u3j_kink(u3k(runnable), AX_TRY), 2), 2); - u3_noun catch_script = u3j_kink(u3j_kink(u3j_kink(u3k(runnable), AX_CATCH), 2), 2); - u3_noun return_script = u3j_kink(u3j_kink(runnable, AX_RETURN), 2); + + #define KICK1(TRAP) u3j_kink(TRAP, 2) + #define KICK2(TRAP) u3j_kink(KICK1(TRAP), 2) + + u3_noun call_script = KICK1(u3j_kink(u3k(ctx), AX_CALL)); + u3_noun memread_script = KICK1(u3j_kink(u3k(ctx), AX_MEMREAD)); + u3_noun memwrite_script = KICK1(u3j_kink(u3k(ctx), AX_MEMWRITE)); + u3_noun call_ext_script = KICK1(u3j_kink(u3k(ctx), AX_CALL_EXT)); + u3_noun global_set_script = KICK1(u3j_kink(u3k(ctx), AX_GLOBAL_SET)); + u3_noun global_get_script = KICK1(u3j_kink(u3k(ctx), AX_GLOBAL_GET)); + u3_noun mem_grow_script = KICK1(u3j_kink(u3k(ctx), AX_MEM_GROW)); + u3_noun mem_size_script = u3j_kink(u3k(ctx), AX_MEM_SIZE); + + u3_noun try_script = KICK2(u3j_kink(u3k(runnable), AX_TRY)); + u3_noun catch_script = KICK2(u3j_kink(u3k(runnable), AX_CATCH)); + u3_noun return_script = KICK1(u3j_kink(u3k(runnable), AX_RETURN)); u3_noun call_bat = u3k(u3h(call_script)); u3_noun memread_bat = u3k(u3h(memread_script)); @@ -1094,7 +1098,7 @@ u3we_lia_run_once(u3_noun cor) u3_noun global_set_ctx = u3k(u3at(ARROW_CTX, global_set_script)); u3_noun global_get_ctx = u3k(u3at(ARROW_CTX, global_get_script)); u3_noun mem_grow_ctx = u3k(u3at(ARROW_CTX, mem_grow_script)); - u3_noun mem_size_ctx = u3k(u3at(MONAD_CTX, mem_grow_script)); + u3_noun mem_size_ctx = u3k(u3at(MONAD_CTX, mem_size_script)); u3z(call_script); u3z(memread_script); From 1e060b400c0bc86ddc33761a542f79f15c55f96d Mon Sep 17 00:00:00 2001 From: Quodss Date: Thu, 12 Dec 2024 15:42:53 +0100 Subject: [PATCH 20/22] plug leak --- pkg/noun/jets/e/urwasm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/noun/jets/e/urwasm.c b/pkg/noun/jets/e/urwasm.c index 44469a82f5..8620622bdc 100644 --- a/pkg/noun/jets/e/urwasm.c +++ b/pkg/noun/jets/e/urwasm.c @@ -1078,7 +1078,7 @@ u3we_lia_run_once(u3_noun cor) u3_noun try_script = KICK2(u3j_kink(u3k(runnable), AX_TRY)); u3_noun catch_script = KICK2(u3j_kink(u3k(runnable), AX_CATCH)); - u3_noun return_script = KICK1(u3j_kink(u3k(runnable), AX_RETURN)); + u3_noun return_script = KICK1(u3j_kink(runnable, AX_RETURN)); u3_noun call_bat = u3k(u3h(call_script)); u3_noun memread_bat = u3k(u3h(memread_script)); From a7a4d76c7f5b8e8b4999025f3643e30f7ddfd425 Mon Sep 17 00:00:00 2001 From: Quodss Date: Thu, 12 Dec 2024 22:04:13 +0100 Subject: [PATCH 21/22] 137 registration --- pkg/noun/jets/137/tree.c | 51 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/pkg/noun/jets/137/tree.c b/pkg/noun/jets/137/tree.c index fec03edf1f..e8c7c35b7d 100644 --- a/pkg/noun/jets/137/tree.c +++ b/pkg/noun/jets/137/tree.c @@ -843,6 +843,56 @@ static u3j_core _137_non_d[] = }; +static u3j_harm _137_hex_lia_run_a[] = {{".2", u3we_lia_run, c3y}, {}}; + +static u3j_harm _137_hex_lia_run_once_inner_a[] = {{".2", u3we_lia_run_once, c3y}, {}}; + +static u3j_core _137_hex_lia_run_once_d[] = { + { "run-once-inner-v0", 15, _137_hex_lia_run_once_inner_a, 0, no_hashes }, + {} +}; + +static u3j_core _137_hex_lia_monad_d[] = { + { "run-v0", 7, _137_hex_lia_run_a, 0, no_hashes }, + { "run-once-v0", 7, 0, _137_hex_lia_run_once_d, no_hashes }, + {} +}; + +static u3j_core _137_hex_wasm_engine_d[] = { + { "monad-v0", 3, 0, _137_hex_lia_monad_d, no_hashes }, + {} +}; + +static u3j_core _137_hex_wasm_op_def_d[] = { + { "wasm-engine-v0", 3, 0, _137_hex_wasm_engine_d, no_hashes }, + {} +}; + +static u3j_core _137_hex_wasm_validator_d[] = { + { "wasm-op-def-v0", 3, 0, _137_hex_wasm_op_def_d, no_hashes }, + {} +}; + +static u3j_core _137_hex_wasm_parser_d[] = { + { "validator-v0", 3, 0, _137_hex_wasm_validator_d, no_hashes }, + {} +}; + +static u3j_core _137_hex_lia_sur_d[] = { + { "wasm-parser-v0", 3, 0, _137_hex_wasm_parser_d, no_hashes }, + {} +}; + +static u3j_core _137_hex_wasm_engine_sur_d[] = { + { "monad-sur-v0", 3, 0, _137_hex_lia_sur_d, no_hashes }, + {} +}; + +static u3j_core _137_hex_wasm_sur_d[] = { + { "engine-sur-v0", 3, 0, _137_hex_wasm_engine_sur_d, no_hashes }, + {} +}; + static u3j_core _137_hex_d[] = { { "non", 7, 0, _137_non_d, no_hashes }, @@ -869,6 +919,7 @@ static u3j_core _137_hex_d[] = { "secp", 6, 0, _137_hex_secp_d, no_hashes }, { "mimes", 31, 0, _137_hex_mimes_d, no_hashes }, { "json", 31, 0, _137_hex_json_d, no_hashes }, + { "wasm-sur-v0", 3, 0, _137_hex_wasm_sur_d, no_hashes }, {} }; From c6e82209b6f7d6049e2ad75a61e16601e6f9544f Mon Sep 17 00:00:00 2001 From: Quodss Date: Sat, 14 Dec 2024 01:48:27 +0100 Subject: [PATCH 22/22] wasm3 update, exit on missing import --- ext/wasm3/build.zig.zon | 4 ++-- pkg/noun/jets/e/urwasm.c | 33 ++++++++++++++++++--------------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/ext/wasm3/build.zig.zon b/ext/wasm3/build.zig.zon index 8ff49866f0..5e68b4c439 100644 --- a/ext/wasm3/build.zig.zon +++ b/ext/wasm3/build.zig.zon @@ -7,8 +7,8 @@ }, .wasm3 = .{ // .path = "../../../../wasm3" - .url = "https://github.com/Quodss/wasm3/archive/a8ccdb9a96eda023730e5b876c37e1550a4d18f9.tar.gz", - .hash = "1220ccd506e41df3d84563f3d997911e6d628ed7d7490854193215c6ac2dc69a3586", + .url = "https://github.com/Quodss/wasm3/archive/91f1479cce7b62a516ef289ee97d68eec1029627.tar.gz", + .hash = "1220ce5d34dc6d9274a00d8407c85d1260557639374f163dbb20fed9c9583897fe0c", }, }, .paths = .{ diff --git a/pkg/noun/jets/e/urwasm.c b/pkg/noun/jets/e/urwasm.c index 8620622bdc..05462fa5b9 100644 --- a/pkg/noun/jets/e/urwasm.c +++ b/pkg/noun/jets/e/urwasm.c @@ -336,6 +336,10 @@ _reduce_monad(u3_noun monad, lia_state* sat) fprintf(stderr, WUT("%s call trapped: %s"), name_c, result); yil = u3nc(2, 0); } + else if (result == m3Err_functionImportMissing) + { + return u3m_bail(c3__exit); + } else if (result) { fprintf(stderr, ERR("%s call failed: %s"), name_c, result); @@ -741,31 +745,26 @@ _link_wasm_with_arrow_map( u3_noun yil = _reduce_monad(u3n_slam_on(arrow, coin_wasm_list), sat); + M3Result result = m3Err_none; + if (0 != u3h(yil)) { sat->arrow_yil = yil; - u3a_free(valptrs_in); - u3a_free(valptrs_out); - return m3Lia_Arrow; + result = m3Lia_Arrow; } else { - if (c3n == _coins_to_stack(u3t(yil), valptrs_out, n_out, types)) + c3_o pushed = _coins_to_stack(u3t(yil), valptrs_out, n_out, types); + u3z(yil); + if (c3n == pushed) { - u3z(yil); - u3a_free(valptrs_in); - u3a_free(valptrs_out); fprintf(stderr, ERR("import result type mismatch: %s/%s"), mod, name); - return "import result type mismatch"; - } - else - { - u3z(yil); - u3a_free(valptrs_in); - u3a_free(valptrs_out); - return m3Err_none; + result = "import result type mismatch"; } } + u3a_free(valptrs_in); + u3a_free(valptrs_out); + return result; } u3_weak @@ -1232,6 +1231,10 @@ u3we_lia_run_once(u3_noun cor) fprintf(stderr, WUT("start function call trapped: %s"), result); yil = u3nc(2, 0); } + else if (result == m3Err_functionImportMissing) + { + return u3m_bail(c3__exit); + } else if (result) { fprintf(stderr, ERR("start function failed: %s"), result);