From 9d65dbe2410e1856c3ac1fa6ff7eb921bb24ec0c Mon Sep 17 00:00:00 2001 From: Dan Connolly Date: Mon, 6 Sep 2021 10:36:58 -0500 Subject: [PATCH] feat(xsnap): integrate native TextEncoder / TextDecoder - test null handling in TextDecoder - update xsnap meter details, determinism hashes in tests for xs-meter-11 --- packages/SwingSet/test/test-xsnap-store.js | 6 ++--- packages/xsnap/lib/ses-boot-debug.js | 1 - packages/xsnap/lib/ses-boot.js | 1 - packages/xsnap/lib/text-shim.js | 30 ---------------------- packages/xsnap/test/test-replay.js | 2 +- packages/xsnap/test/test-xs-js.js | 23 ++++++++++++++--- packages/xsnap/test/test-xs-perf.js | 4 +-- 7 files changed, 26 insertions(+), 41 deletions(-) delete mode 100644 packages/xsnap/lib/text-shim.js diff --git a/packages/SwingSet/test/test-xsnap-store.js b/packages/SwingSet/test/test-xsnap-store.js index 5be15c88693..9fc1f824c6d 100644 --- a/packages/SwingSet/test/test-xsnap-store.js +++ b/packages/SwingSet/test/test-xsnap-store.js @@ -155,7 +155,7 @@ test('XS + SES snapshots are deterministic', async t => { t.is( h1, - '9255590eabf7884184a2c6ad435a543f66cdcc80209cc251085f22cdf9d1e5f5', + '879fe6ca1c58473713eba28201eb1397e663c78278f41b07c2cc6c77935a6850', 'initial snapshot', ); @@ -167,7 +167,7 @@ test('XS + SES snapshots are deterministic', async t => { const h2 = await store.save(vat.snapshot); t.is( h2, - '3b87e5509acf7e8ae40cca0686d0a64aeca6ed32df1279f6afa608651debd492', + '566457a540de7e295294e24319a10d62716dba304e4283a65315b64690d56cad', 'after SES boot', ); @@ -175,7 +175,7 @@ test('XS + SES snapshots are deterministic', async t => { const h3 = await store.save(vat.snapshot); t.is( h3, - '2aaab1080ea8c3c6aa9cd3faeaf2d89c68f01252c5d325e2f7694f53eca6f36d', + 'ec5fac2d8c3756a10898dd36064fd235e2961bdef51039f7fe4c9b7a90a656f1', 'after use of harden()', ); }); diff --git a/packages/xsnap/lib/ses-boot-debug.js b/packages/xsnap/lib/ses-boot-debug.js index 906765e3334..a5de25639a3 100644 --- a/packages/xsnap/lib/ses-boot-debug.js +++ b/packages/xsnap/lib/ses-boot-debug.js @@ -1,5 +1,4 @@ import './console-shim.js'; -import './text-shim.js'; import '@agoric/eventual-send/shim.js'; import './lockdown-shim-debug.js'; diff --git a/packages/xsnap/lib/ses-boot.js b/packages/xsnap/lib/ses-boot.js index 0796526edb5..de6878a5f30 100644 --- a/packages/xsnap/lib/ses-boot.js +++ b/packages/xsnap/lib/ses-boot.js @@ -1,5 +1,4 @@ import './console-shim.js'; -import './text-shim.js'; import '@agoric/eventual-send/shim.js'; import './lockdown-shim.js'; diff --git a/packages/xsnap/lib/text-shim.js b/packages/xsnap/lib/text-shim.js deleted file mode 100644 index 0c90cc4a9bf..00000000000 --- a/packages/xsnap/lib/text-shim.js +++ /dev/null @@ -1,30 +0,0 @@ -/* global globalThis */ -/* eslint-disable max-classes-per-file */ -/* eslint-disable class-methods-use-this */ - -// Save this XS extension before SES shim deletes it. -const { fromString } = ArrayBuffer; -const { fromArrayBuffer } = String; - -class TextEncoder { - encode(text) { - return new Uint8Array(fromString(text)); - } -} - -class TextDecoder { - decode(bytes) { - // TODO: the following condition can be removed in a future update of XS. - // https://github.com/Agoric/agoric-sdk/issues/3362 - if (ArrayBuffer.isView(bytes)) { - bytes = bytes.buffer.slice( - bytes.byteOffset, - bytes.byteOffset + bytes.byteLength, - ); - } - return fromArrayBuffer(bytes); - } -} - -globalThis.TextEncoder = TextEncoder; -globalThis.TextDecoder = TextDecoder; diff --git a/packages/xsnap/test/test-replay.js b/packages/xsnap/test/test-replay.js index 2fabd2df697..f7b443a00d0 100644 --- a/packages/xsnap/test/test-replay.js +++ b/packages/xsnap/test/test-replay.js @@ -21,7 +21,7 @@ const transcript1 = [ '/xsnap-tests/00001-evaluate.dat', 'issueCommand(ArrayBuffer.fromString("Hello, World!"));', ], - ['/xsnap-tests/00002-command.dat', '{"compute":54'], + ['/xsnap-tests/00002-command.dat', '{"compute":58'], ['/xsnap-tests/00003-reply.dat', ''], ]; diff --git a/packages/xsnap/test/test-xs-js.js b/packages/xsnap/test/test-xs-js.js index d29a015e730..4988994a027 100644 --- a/packages/xsnap/test/test-xs-js.js +++ b/packages/xsnap/test/test-xs-js.js @@ -36,12 +36,27 @@ test('accept std regex range', async t => { await vat.terminate(); }); +test('simple TextEncoder / TextDecoder are available', async t => { + const opts = options(io); + const vat = xsnap(opts); + t.teardown(() => vat.terminate()); + await vat.evaluate(` + const encoder = new TextEncoder(); + const decoder = new TextDecoder(); + const send = it => issueCommand(encoder.encode(JSON.stringify(it)).buffer); + send("Hello! 😊") + send(decoder.decode(new Uint8Array([65, 0, 65]).buffer)); + `); + t.deepEqual(opts.messages, ['"Hello! 😊"', '"A\\u0000A"']); +}); + test('bigint map key', async t => { const opts = options(io); const vat = xsnap(opts); t.teardown(() => vat.terminate()); await vat.evaluate(` - const send = it => issueCommand(ArrayBuffer.fromString(JSON.stringify(it))); + const encoder = new TextEncoder(); + const send = it => issueCommand(encoder.encode(JSON.stringify(it)).buffer); const store = new Map([[1n, "abc"]]); send(store.get(1n)) `); @@ -53,7 +68,8 @@ test('bigint toString', async t => { const vat = xsnap(opts); t.teardown(() => vat.terminate()); await vat.evaluate(` - const send = it => issueCommand(ArrayBuffer.fromString(JSON.stringify(it))); + const encoder = new TextEncoder(); + const send = it => issueCommand(encoder.encode(JSON.stringify(it)).buffer); const txt = \`number: 1 2 3 bigint: \${0n} \${1n} \${BigInt(2)} \${BigInt(3)} .\`; send(txt) `); @@ -65,7 +81,8 @@ test('keyword in destructuring', async t => { const vat = xsnap(opts); t.teardown(() => vat.terminate()); await vat.evaluate(` - const send = it => issueCommand(ArrayBuffer.fromString(JSON.stringify(it))); + const encoder = new TextEncoder(); + const send = it => issueCommand(encoder.encode(JSON.stringify(it)).buffer); const { default: d, in: i } = { default: 1, in: 2 }; send({ d, i }) `); diff --git a/packages/xsnap/test/test-xs-perf.js b/packages/xsnap/test/test-xs-perf.js index 5128f3537d1..f332084676c 100644 --- a/packages/xsnap/test/test-xs-perf.js +++ b/packages/xsnap/test/test-xs-perf.js @@ -45,7 +45,7 @@ test('meter details', async t => { t.like( meters, - { compute: 1_260_182, allocate: 42_074_144 }, + { compute: 1_380_192, allocate: 42_074_144 }, 'compute, allocate meters should be stable; update METER_TYPE?', ); @@ -93,7 +93,7 @@ test('metering regex - REDOS', async t => { 'aaaaaaaaa!'.match(/^(([a-z])+.)+/) `); const { meterUsage: meters } = result; - t.like(meters, { compute: 149 }); + t.like(meters, { compute: 153 }); }); test('meter details are still available with no limit', async t => {