diff options
Diffstat (limited to 'src/z/app')
| -rw-r--r-- | src/z/app/main.js | 48 |
1 files changed, 28 insertions, 20 deletions
diff --git a/src/z/app/main.js b/src/z/app/main.js index 07fa9b8..9747bfe 100644 --- a/src/z/app/main.js +++ b/src/z/app/main.js @@ -4,7 +4,7 @@ function decodeString(base, length) { } class ObjectStore { - static id_name = "+-store-id"; + static id_name = "@id"; static types = Object.freeze({ 0: "uninit", @@ -31,9 +31,10 @@ class ObjectStore { 3: 'delProp', 4: 'callMethod', 5: 'call', + 6: 'new', }); - static store = { 0: null, 1: window }; + static store = { 0: null }; static generateFreeId() { let id = 0; @@ -45,8 +46,9 @@ class ObjectStore { return id; } - static free(id) { - delete ObjectStore.store[id][ObjectStore.id_name]; + static free(target) { + const id = target[ObjectStore.id_name]; + delete target[ObjectStore.id_name]; delete ObjectStore.store[id]; } @@ -70,12 +72,8 @@ class ObjectStore { return target(args); } - static decodeValue(view, offset) { - if (ObjectStore.types[kind] === 'string') { - const ptr = view.getUint32((offset * 4) * 2, true); - const len = view.getUint32((offset * 4) * 2 + 4, true); - return decodeString(ptr, len); - } + static new(target, ...args) { + return new target(...args); } static decodeCall(view, offset) { @@ -118,17 +116,26 @@ class ObjectStore { } } + 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') { + 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.store[id] = value; - value[ObjectStore.id_name] = id; + ObjectStore.registerObject(id, value); } mem.setUint32(8, id, true); @@ -137,20 +144,21 @@ class ObjectStore { } if (typeof value === 'number' && Number.isInteger(value) && value >= 0) { - // uint - throw "unimplemented"; + mem.setUint8(0, ObjectStore.types['uint'], true); + mem.setUint32(8, value, true) return; } if (typeof value === 'number' && Number.isInteger(value)) { - // int - throw "unimplemented"; + mem.setUint8(0, ObjectStore.types['int'], true); + mem.setInt32(8, value, true) return; } if (typeof value === 'number') { // float - throw "unimplemented"; + mem.setUint8(0, ObjectStore.types['float'], true); + mem.setFloat64(8, value, true) return; } @@ -162,7 +170,7 @@ class ObjectStore { if (typeof value === 'undefined') { // undefined - throw "unimplemented"; + mem.setUint8(0, ObjectStore.types['undefined'], true); return; } } @@ -238,7 +246,7 @@ WebAssembly.instantiateStreaming(fetch("index.wasm"), imports).then( IsizeArray: is64 ? BigInt64Array : Int32Array, } - window[ObjectStore.id_name] = 1; + ObjectStore.registerObject(1, window); wasm.exports.run(); }, |