Skip to content

Commit cac4b67

Browse files
committed
very strange things are happening?
1 parent c2abf47 commit cac4b67

1 file changed

Lines changed: 26 additions & 16 deletions

File tree

Plugins/PackageToJS/Templates/runtime.mjs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,15 @@ const decode = (kind, payload1, payload2, objectSpace) => {
4949
// Note:
5050
// `decodeValues` assumes that the size of RawJSValue is 16.
5151
const decodeArray = (ptr, length, memory, objectSpace) => {
52+
const basePtr = ptr >>> 0;
53+
const count = length >>> 0;
5254
// fast path for empty array
53-
if (length === 0) {
55+
if (count === 0) {
5456
return [];
5557
}
5658
let result = [];
57-
for (let index = 0; index < length; index++) {
58-
const base = ptr + 16 * index;
59+
for (let index = 0; index < count; index++) {
60+
const base = basePtr + 16 * index;
5961
const kind = memory.getUint32(base, true);
6062
const payload1 = memory.getUint32(base + 4, true);
6163
const payload2 = memory.getFloat64(base + 8, true);
@@ -69,25 +71,27 @@ const decodeArray = (ptr, length, memory, objectSpace) => {
6971
// This function should be used only when kind flag is stored in memory.
7072
const write = (value, kind_ptr, payload1_ptr, payload2_ptr, is_exception, memory, objectSpace) => {
7173
const kind = writeAndReturnKindBits(value, payload1_ptr, payload2_ptr, is_exception, memory, objectSpace);
72-
memory.setUint32(kind_ptr, kind, true);
74+
memory.setUint32(kind_ptr >>> 0, kind, true);
7375
};
7476
const writeAndReturnKindBits = (value, payload1_ptr, payload2_ptr, is_exception, memory, objectSpace) => {
7577
const exceptionBit = (is_exception ? 1 : 0) << 31;
78+
const payload1Offset = payload1_ptr >>> 0;
79+
const payload2Offset = payload2_ptr >>> 0;
7680
if (value === null) {
7781
return exceptionBit | 4 /* Kind.Null */;
7882
}
7983
const writeRef = (kind) => {
80-
memory.setUint32(payload1_ptr, objectSpace.retain(value), true);
84+
memory.setUint32(payload1Offset, objectSpace.retain(value), true);
8185
return exceptionBit | kind;
8286
};
8387
const type = typeof value;
8488
switch (type) {
8589
case "boolean": {
86-
memory.setUint32(payload1_ptr, value ? 1 : 0, true);
90+
memory.setUint32(payload1Offset, value ? 1 : 0, true);
8791
return exceptionBit | 0 /* Kind.Boolean */;
8892
}
8993
case "number": {
90-
memory.setFloat64(payload2_ptr, value, true);
94+
memory.setFloat64(payload2Offset, value, true);
9195
return exceptionBit | 2 /* Kind.Number */;
9296
}
9397
case "string": {
@@ -114,9 +118,11 @@ const writeAndReturnKindBits = (value, payload1_ptr, payload2_ptr, is_exception,
114118
throw new Error("Unreachable");
115119
};
116120
function decodeObjectRefs(ptr, length, memory) {
117-
const result = new Array(length);
118-
for (let i = 0; i < length; i++) {
119-
result[i] = memory.getUint32(ptr + 4 * i, true);
121+
const basePtr = ptr >>> 0;
122+
const count = length >>> 0;
123+
const result = new Array(count);
124+
for (let i = 0; i < count; i++) {
125+
result[i] = memory.getUint32(basePtr + 4 * i, true);
120126
}
121127
return result;
122128
}
@@ -617,25 +623,29 @@ class SwiftRuntime {
617623
const memory = this.memory;
618624
const bytes = this.textEncoder.encode(memory.getObject(ref));
619625
const bytes_ptr = memory.retain(bytes);
620-
this.getDataView().setUint32(bytes_ptr_result, bytes_ptr, true);
626+
this.getDataView().setUint32(bytes_ptr_result >>> 0, bytes_ptr, true);
621627
return bytes.length;
622628
},
623629
swjs_decode_string:
624630
// NOTE: TextDecoder can't decode typed arrays backed by SharedArrayBuffer
625631
this.options.sharedMemory == true
626632
? (bytes_ptr, length) => {
627-
const bytes = this.getUint8Array().slice(bytes_ptr, bytes_ptr + length);
633+
const bytesOffset = bytes_ptr >>> 0;
634+
const byteLength = length >>> 0;
635+
const bytes = this.getUint8Array().slice(bytesOffset, bytesOffset + byteLength);
628636
const string = this.textDecoder.decode(bytes);
629637
return this.memory.retain(string);
630638
}
631639
: (bytes_ptr, length) => {
632-
const bytes = this.getUint8Array().subarray(bytes_ptr, bytes_ptr + length);
640+
const bytesOffset = bytes_ptr >>> 0;
641+
const byteLength = length >>> 0;
642+
const bytes = this.getUint8Array().subarray(bytesOffset, bytesOffset + byteLength);
633643
const string = this.textDecoder.decode(bytes);
634644
return this.memory.retain(string);
635645
},
636646
swjs_load_string: (ref, buffer) => {
637647
const bytes = this.memory.getObject(ref);
638-
this.getUint8Array().set(bytes, buffer);
648+
this.getUint8Array().set(bytes, buffer >>> 0);
639649
},
640650
swjs_call_function: (ref, argv, argc, payload1_ptr, payload2_ptr) => {
641651
const memory = this.memory;
@@ -739,7 +749,7 @@ class SwiftRuntime {
739749
// See https://github.com/swiftwasm/swift/issues/5599
740750
return this.memory.retain(new ArrayType());
741751
}
742-
const array = new ArrayType(this.wasmMemory.buffer, elementsPtr, length);
752+
const array = new ArrayType(this.wasmMemory.buffer, elementsPtr >>> 0, length >>> 0);
743753
// Call `.slice()` to copy the memory
744754
return this.memory.retain(array.slice());
745755
},
@@ -750,7 +760,7 @@ class SwiftRuntime {
750760
const memory = this.memory;
751761
const typedArray = memory.getObject(ref);
752762
const bytes = new Uint8Array(typedArray.buffer);
753-
this.getUint8Array().set(bytes, buffer);
763+
this.getUint8Array().set(bytes, buffer >>> 0);
754764
},
755765
swjs_release: (ref) => {
756766
this.memory.release(ref);

0 commit comments

Comments
 (0)