aboutsummaryrefslogtreecommitdiff
path: root/src/z/app
diff options
context:
space:
mode:
Diffstat (limited to 'src/z/app')
-rw-r--r--src/z/app/main.js254
-rw-r--r--src/z/app/root.zig20
2 files changed, 0 insertions, 274 deletions
diff --git a/src/z/app/main.js b/src/z/app/main.js
deleted file mode 100644
index e15f783..0000000
--- a/src/z/app/main.js
+++ /dev/null
@@ -1,254 +0,0 @@
-class ObjectStore {
- static id_name = "@id";
-
- 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[ObjectStore.id_name];
- delete target[ObjectStore.id_name];
- 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, ObjectStore.id_name, {
- 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[ObjectStore.id_name];
-
- 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);
- }
- }
-}
-
-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))
- },
- }
-}
-
-WebAssembly.instantiateStreaming(fetch("index.wasm"), imports).then(
- ({ instance, module }) => {
- const is64 = new Uint8Array(
- instance.exports.memory.buffer,
- instance.exports.bit_length,
- 1,
- )[0] == 8
-
- window.wasm = {
- exports: instance.exports,
- memory: instance.exports.memory,
- is64: is64,
- UsizeArray: is64 ? BigUint64Array : Uint32Array,
- IsizeArray: is64 ? BigInt64Array : Int32Array,
- }
-
- ObjectStore.registerObject(1, window);
-
- wasm.exports.run();
- },
-);
diff --git a/src/z/app/root.zig b/src/z/app/root.zig
deleted file mode 100644
index 03760c4..0000000
--- a/src/z/app/root.zig
+++ /dev/null
@@ -1,20 +0,0 @@
-const html = @import("html");
-
-pub const Options = struct {
- name: []const u8
-};
-
-pub fn create(options: Options) []const u8 {
- return html.Element.native(.html, .{}, .children(&.{
- .native(.head, .{}, .children(&.{
- .native(.title, .{}, .content(options.name)),
- .native(.meta, .{
- .name = "viewport",
- .content = "width=device-width, initial-scale=1.0",
- }, .void),
- })),
- .native(.body, .{}, .children(&.{
- .native(.script, .{}, .content(@embedFile("main.js"))),
- })),
- })).toDocument();
-}