diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-10-09 13:51:51 +0200 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-10-09 13:51:51 +0200 |
| commit | 09e8d4548361ece6151b914709c39e254eba65e3 (patch) | |
| tree | 5a6038dd219110c53e699109e03a5490260c3560 /src/z/App | |
| parent | d0f90548f5edb32225af4a1f79413a2a56d186c8 (diff) | |
framework: use js symbol for object tagging
Diffstat (limited to 'src/z/App')
| -rw-r--r-- | src/z/App/main.js | 4 | ||||
| -rw-r--r-- | src/z/App/object-store.js | 10 | ||||
| -rw-r--r-- | src/z/App/root.zig | 29 | ||||
| -rw-r--r-- | src/z/App/symbols.js | 2 |
4 files changed, 23 insertions, 22 deletions
diff --git a/src/z/App/main.js b/src/z/App/main.js index 183e544..265c886 100644 --- a/src/z/App/main.js +++ b/src/z/App/main.js @@ -26,10 +26,10 @@ async function wasm_handle_event(target, name, event, pass_event) { if (pass_event) { const id = ObjectStore.generateFreeId(); ObjectStore.registerObject(id, event); - wasm.exports[name](component["@ptr"], id); + wasm.exports[name](component[ZsPtr], id); ObjectStore.free(event); } else { - wasm.exports[name](component["@ptr"]); + wasm.exports[name](component[ZsPtr]); } } diff --git a/src/z/App/object-store.js b/src/z/App/object-store.js index ae581f8..096d085 100644 --- a/src/z/App/object-store.js +++ b/src/z/App/object-store.js @@ -1,6 +1,4 @@ class ObjectStore { - static id_name = "@id"; - static types = Object.freeze({ 0: "uninit", 1: "object", @@ -42,8 +40,8 @@ class ObjectStore { } static free(target) { - const id = target[ObjectStore.id_name]; - delete target[ObjectStore.id_name]; + const id = target[ZsId]; + delete target[ZsId]; delete ObjectStore.store[id]; } @@ -118,7 +116,7 @@ class ObjectStore { static registerObject(id, obj) { ObjectStore.store[id] = obj; - Object.defineProperty(obj, ObjectStore.id_name, { + Object.defineProperty(obj, ZsId, { enumerable: false, writable: false, configurable: true, @@ -131,7 +129,7 @@ class ObjectStore { if (typeof value === 'object' || typeof value === 'function') { mem.setUint8(0, ObjectStore.types['object'], true); - let id = value == null ? 0 : value[ObjectStore.id_name]; + let id = value == null ? 0 : value[ZsId]; if (id === undefined) { id = ObjectStore.generateFreeId(); diff --git a/src/z/App/root.zig b/src/z/App/root.zig index 8681c4a..52cbfc2 100644 --- a/src/z/App/root.zig +++ b/src/z/App/root.zig @@ -71,25 +71,26 @@ pub fn App(comptime options: Options) type { .content = "width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0", }), .node("style", .{}, &.{.text(options.theme)}), + .node("script", .{}, &.{.text(@embedFile("symbols.js"))}), + .node("script", .{}, &.{.text(@embedFile("object-store.js"))}), + .node("script", .{}, &.{.text(@embedFile("main.js"))}), + .node("script", .{}, &.{.text(reg: { + var register_code: []const u8 = ""; + + for (options.globals, 1..) |field, id| { + register_code = register_code ++ std.fmt.comptimePrint( + "ObjectStore.registerObject({}, {s});", + .{ id, field.name }, + ); + } + + break :reg register_code; + })}), }), .node( "body", .{}, &[_]Element{ - .node("script", .{}, &.{.text(@embedFile("object-store.js"))}), - .node("script", .{}, &.{.text(reg: { - var register_code: []const u8 = ""; - - for (options.globals, 1..) |field, id| { - register_code = register_code ++ std.fmt.comptimePrint( - "ObjectStore.registerObject({}, {s});", - .{ id, field.name }, - ); - } - - break :reg register_code; - })}), - .node("script", .{}, &.{.text(@embedFile("main.js"))}), } ++ components ++ options.body.elements, ), }), diff --git a/src/z/App/symbols.js b/src/z/App/symbols.js new file mode 100644 index 0000000..3df42b4 --- /dev/null +++ b/src/z/App/symbols.js @@ -0,0 +1,2 @@ +const ZsId = Symbol("zeroscript::id"); +const ZsPtr = Symbol("zeroscript::ptr"); |