aboutsummaryrefslogtreecommitdiff
path: root/src/zeroscript/App
diff options
context:
space:
mode:
Diffstat (limited to 'src/zeroscript/App')
-rw-r--r--src/zeroscript/App/main.js43
-rw-r--r--src/zeroscript/App/object-store.js210
-rw-r--r--src/zeroscript/App/root.zig104
-rw-r--r--src/zeroscript/App/symbols.js2
4 files changed, 359 insertions, 0 deletions
diff --git a/src/zeroscript/App/main.js b/src/zeroscript/App/main.js
new file mode 100644
index 0000000..265c886
--- /dev/null
+++ b/src/zeroscript/App/main.js
@@ -0,0 +1,43 @@
+const imports = {
+ object_store: {
+ process: ObjectStore.processWasm,
+ },
+ console: {
+ log(buffer, length) {
+ console.log(ObjectStore.decodeString(buffer, length))
+ },
+ warn(buffer, length) {
+ console.warn(ObjectStore.decodeString(buffer, length))
+ },
+ err(buffer, length) {
+ console.error(ObjectStore.decodeString(buffer, length))
+ },
+ debug(buffer, length) {
+ console.debug(ObjectStore.decodeString(buffer, length))
+ },
+ }
+}
+
+async function wasm_handle_event(target, name, event, pass_event) {
+ await wasm_ready;
+
+ const component = target.getRootNode().host;
+
+ if (pass_event) {
+ const id = ObjectStore.generateFreeId();
+ ObjectStore.registerObject(id, event);
+ wasm.exports[name](component[ZsPtr], id);
+ ObjectStore.free(event);
+ } else {
+ wasm.exports[name](component[ZsPtr]);
+ }
+}
+
+const wasm_ready = WebAssembly.instantiateStreaming(fetch("index.wasm"), imports).then(
+ ({ instance, module }) => {
+ wasm = {
+ exports: instance.exports,
+ memory: instance.exports.memory,
+ }
+ },
+);
diff --git a/src/zeroscript/App/object-store.js b/src/zeroscript/App/object-store.js
new file mode 100644
index 0000000..096d085
--- /dev/null
+++ b/src/zeroscript/App/object-store.js
@@ -0,0 +1,210 @@
+class ObjectStore {
+ static types = Object.freeze({
+ 0: "uninit",
+ 1: "object",
+ 2: "string",
+ 3: "int",
+ 4: "uint",
+ 5: "float",
+ 6: "undefined",
+
+ "uninit": 0,
+ "object": 1,
+ "string": 2,
+ "int": 3,
+ "uint": 4,
+ "float": 5,
+ "undefined": 6,
+ });
+
+ static methods = Object.freeze({
+ 0: 'free',
+ 1: 'getProp',
+ 2: 'setProp',
+ 3: 'delProp',
+ 4: 'callMethod',
+ 5: 'call',
+ 6: 'new',
+ });
+
+ static store = { 0: null };
+
+ static generateFreeId() {
+ let id = 0;
+
+ while (id in ObjectStore.store) {
+ id = Math.floor(Math.random() * (2**32 - 1))
+ }
+
+ return id;
+ }
+
+ static free(target) {
+ const id = target[ZsId];
+ delete target[ZsId];
+ delete ObjectStore.store[id];
+ }
+
+ static getProp(target, key) {
+ return target[key];
+ }
+
+ static setProp(target, key, value) {
+ target[key] = value;
+ }
+
+ static delProp(target, key) {
+ delete target[key];
+ }
+
+ static callMethod(target, method, ...args) {
+ return target[method](...args);
+ }
+
+ static call(target, args) {
+ return target(args);
+ }
+
+ static new(target, ...args) {
+ return new target(...args);
+ }
+
+ static decodeString(base, len) {
+ const slice = new Uint8Array(wasm.memory.buffer, base, len);
+ return new TextDecoder().decode(slice);
+ }
+
+ static decodeCall(view, offset) {
+ const kind = ObjectStore.methods[view.getUint8(offset, true)]
+
+ if (!kind) throw "unknown method, cannot decode";
+
+ return {
+ kind: kind,
+ num_args: view.getUint8(offset + 1, true),
+ }
+ }
+
+ static decodeValue(view, offset) {
+ const kind = ObjectStore.types[view.getUint8(offset, true)];
+
+ if (!kind) throw "unknown type, cannot decode";
+
+ if (kind === 'string') {
+ const ptr = view.getUint32(offset + 8, true)
+ const len = view.getUint32(offset + 12, true)
+ return ObjectStore.decodeString(ptr, len);
+ }
+
+ if (kind === 'int') {
+ return view.getInt32(offset + 8, true);
+ }
+
+ if (kind === 'uint') {
+ return view.getUint32(offset + 8, true);
+ }
+
+ if (kind === 'object') {
+ const id = view.getUint32(offset + 8, true);
+ return ObjectStore.store[id];
+ }
+
+ if (kind === 'float') {
+ return view.getFloat64(offset + 8, true);
+ }
+ }
+
+ static registerObject(id, obj) {
+ ObjectStore.store[id] = obj;
+ Object.defineProperty(obj, ZsId, {
+ enumerable: false,
+ writable: false,
+ configurable: true,
+ value: id,
+ })
+ }
+
+ static writeValue(ptr, value) {
+ const mem = new DataView(wasm.memory.buffer, ptr);
+
+ if (typeof value === 'object' || typeof value === 'function') {
+ mem.setUint8(0, ObjectStore.types['object'], true);
+ let id = value == null ? 0 : value[ZsId];
+
+ if (id === undefined) {
+ id = ObjectStore.generateFreeId();
+ ObjectStore.registerObject(id, value);
+ }
+
+ mem.setUint32(8, id, true);
+
+ return;
+ }
+
+ if (typeof value === 'number' && Number.isInteger(value) && value >= 0) {
+ mem.setUint8(0, ObjectStore.types['uint'], true);
+ mem.setUint32(8, value, true)
+ return;
+ }
+
+ if (typeof value === 'number' && Number.isInteger(value)) {
+ mem.setUint8(0, ObjectStore.types['int'], true);
+ mem.setInt32(8, value, true)
+ return;
+ }
+
+ if (typeof value === 'number') {
+ mem.setUint8(0, ObjectStore.types['float'], true);
+ mem.setFloat64(8, value, true)
+ return;
+ }
+
+ if (typeof value === 'string') {
+ const string_ptr = wasm.exports.valueAllocString(ptr, value.length);
+ const encoder = new TextEncoder();
+ const string = encoder.encode(value);
+ const memory = new Uint8Array(wasm.memory.buffer);
+ memory.set(string, string_ptr);
+ return;
+ }
+
+ if (typeof value === 'undefined') {
+ mem.setUint8(0, ObjectStore.types['undefined'], true);
+ return;
+ }
+ }
+
+ static processWasm(
+ calls_base,
+ values_base,
+ result_base,
+ length,
+ ) {
+ const offset = {
+ call: 0,
+ value: 0,
+ result: 0,
+ }
+
+ const calls_view = new DataView(wasm.memory.buffer, calls_base);
+ const values_view = new DataView(wasm.memory.buffer, values_base);
+ const results_view = new DataView(wasm.memory.buffer, result_base);
+
+ for (let i = 0; i < length; ++i) {
+ const method = ObjectStore.decodeCall(calls_view, offset.call);
+ offset.call += 2;
+
+ let args = new Array(method.num_args).fill().map(_ => {
+ const value = ObjectStore.decodeValue(values_view, offset.value);
+ offset.value += 16;
+ return value;
+ });
+
+ const result = ObjectStore[method.kind](...args);
+ const result_ptr = results_view.getUint32(offset.result, true);
+ offset.result += 4;
+
+ ObjectStore.writeValue(result_ptr, result);
+ }
+ }
+}
diff --git a/src/zeroscript/App/root.zig b/src/zeroscript/App/root.zig
new file mode 100644
index 0000000..52cbfc2
--- /dev/null
+++ b/src/zeroscript/App/root.zig
@@ -0,0 +1,104 @@
+const builtin = @import("builtin");
+const std = @import("std");
+const Element = @import("../root.zig").html.Element;
+const Document = @import("../root.zig").html.Document;
+const Fragment = @import("../root.zig").html.Fragment;
+const Component = @import("../root.zig").Component;
+
+pub const Options = struct {
+ name: []const u8,
+ globals: []const Global,
+ components: []const Component,
+ body: Fragment,
+ theme: []const u8,
+
+ pub const Global = struct {
+ name: []const u8,
+ type: type,
+ };
+};
+
+pub fn App(comptime options: Options) type {
+ if (builtin.target.cpu.arch.isWasm()) {
+ var names: [options.globals.len][]const u8 = undefined;
+ var types: [options.globals.len]type = undefined;
+ var attrs: [options.globals.len]std.builtin.Type.StructField.Attributes = undefined;
+
+ for (options.globals, 0..) |field, index| {
+ names[index] = field.name;
+ types[index] = field.type;
+ attrs[index] = .{};
+ }
+
+ const G = @Struct(.auto, null, &names, &types, &attrs);
+ return struct {
+ globals: G = g: {
+ var gl: G = undefined;
+
+ for (options.globals, 1..) |field, id| {
+ @field(gl, field.name) = @ptrFromInt(id);
+ }
+
+ break :g gl;
+ },
+
+ pub const init: @This() = .{};
+
+ comptime {
+ for (options.components) |component| {
+ component.exportLinks();
+ }
+ }
+ };
+ } else {
+ const components: []const Element = (comp: {
+ var cs: []const Element = &.{};
+
+ for (options.components) |component| {
+ cs = cs ++ component.toElement();
+ }
+
+ break :comp cs;
+ });
+
+ return struct {
+ const node: Document = .{
+ .root = .node("html", .{}, &.{
+ .node("head", .{}, &.{
+ .node("title", .{}, &.{.text(options.name)}),
+ .void("meta", .{
+ .name = "viewport",
+ .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{
+ } ++ components ++ options.body.elements,
+ ),
+ }),
+ };
+
+ html: []const u8 = std.fmt.comptimePrint("{f}", .{node}),
+
+ pub const init: @This() = .{};
+ };
+ }
+}
diff --git a/src/zeroscript/App/symbols.js b/src/zeroscript/App/symbols.js
new file mode 100644
index 0000000..3df42b4
--- /dev/null
+++ b/src/zeroscript/App/symbols.js
@@ -0,0 +1,2 @@
+const ZsId = Symbol("zeroscript::id");
+const ZsPtr = Symbol("zeroscript::ptr");