aboutsummaryrefslogtreecommitdiff
path: root/src/zilch/App/object-store.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/zilch/App/object-store.js')
-rw-r--r--src/zilch/App/object-store.js180
1 files changed, 0 insertions, 180 deletions
diff --git a/src/zilch/App/object-store.js b/src/zilch/App/object-store.js
deleted file mode 100644
index 9a68478..0000000
--- a/src/zilch/App/object-store.js
+++ /dev/null
@@ -1,180 +0,0 @@
-class ObjectStore {
- static types = Object.freeze({
- 0: "object",
- 1: "string",
- 2: "int",
- 3: "uint",
- 4: "float",
- 5: "undefined",
- 6: "future",
-
- "object": 0,
- "string": 1,
- "int": 2,
- "uint": 3,
- "float": 4,
- "undefined": 5,
- "future": 6,
- });
-
- static store = { 0: null };
- static methods = {};
-
- static generateFreeId() {
- let id = 0;
-
- while (id in ObjectStore.store) {
- id = Math.floor(Math.random() * (2**32 - 1))
- }
-
- return id;
- }
-
- 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.getUint32(offset, true)]
-
- if (!kind) throw "unknown method, cannot decode";
-
- return {
- kind: kind,
- num_args: view.getUint8(offset + 4, true),
- }
- }
-
- static decodeValue(view, offset, results) {
- 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);
- }
-
- if (kind === 'future') {
- return results[view.getUint32(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) {
- if (ptr === 0) return;
-
- 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.valueAllocBuffer(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);
- const results = [];
-
- for (let i = 0; i < length; ++i) {
- const method = ObjectStore.decodeCall(calls_view, offset.call);
- offset.call += wasm.size_of.Call;
-
- let args = new Array(method.num_args).fill().map(_ => {
- const value = ObjectStore.decodeValue(values_view, offset.value, results);
- offset.value += wasm.size_of.Value;
- return value;
- });
-
- const result = method.kind(...args);
- const result_ptr = results_view.getUint32(offset.result, true);
- offset.result += wasm.size_of.Ptr;
-
- results.push(result);
-
- ObjectStore.writeValue(result_ptr, result);
- }
- }
-}