Skip to content

Commit

Permalink
Fix double parsing in JS
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Mar 4, 2024
1 parent a275341 commit 5779afc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
5 changes: 5 additions & 0 deletions javascript/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,8 @@ test("integer (3 nodes)", () => {
const result = parse("18446744073709552000");
assert(result.value.statements.body[0].value == 18446744073709552000n);
});

test("double", () => {
const result = parse("1.0");
assert(result.value.statements.body[0].value == 1.0);
});
19 changes: 18 additions & 1 deletion templates/javascript/src/deserialize.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@ const MAJOR_VERSION = 0;
const MINOR_VERSION = 24;
const PATCH_VERSION = 0;

// The DataView getFloat64 function takes an optional second argument that
// specifies whether the number is little-endian or big-endian. It does not
// appear to have a native endian mode, so we need to determine the endianness
// of the system at runtime.
const LITTLE_ENDIAN = (() => {
let uint32 = new Uint32Array([0x11223344]);
let uint8 = new Uint8Array(uint32.buffer);

if (uint8[0] === 0x44) {
return true;
} else if (uInt8[0] === 0x11) {
return false;
} else {
throw new Error("Mixed endianness");
}
})();

class SerializationBuffer {
constructor(source, array) {
this.source = source;
Expand Down Expand Up @@ -103,7 +120,7 @@ class SerializationBuffer {
view.setUint8(index, this.readByte());
}

return view.getFloat64(0);
return view.getFloat64(0, LITTLE_ENDIAN);
}
}

Expand Down

0 comments on commit 5779afc

Please sign in to comment.