Skip to content

Commit

Permalink
fix(xsnap): Account for TypedArray and subarrays in Text shim
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal committed Jun 19, 2021
1 parent e792430 commit 3531132
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
12 changes: 10 additions & 2 deletions packages/xsnap/lib/text-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,16 @@ class TextEncoder {
}

class TextDecoder {
decode(bs) {
return fromArrayBuffer(bs);
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);
}
}

Expand Down
22 changes: 22 additions & 0 deletions packages/xsnap/test/test-boot-lockdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,25 @@ test('SES deep stacks work on xsnap', async t => {
t.is(typeof msg, 'string');
t.assert(msg.length >= 1);
});

test('TextDecoder under xsnap handles TypedArray and subarrays', async t => {
const bootScript = await ld.asset('../dist/bundle-ses-boot.umd.js');
const opts = options(io);
const vat = xsnap(opts);
await vat.evaluate(bootScript);
await vat.evaluate(`
const decoder = new TextDecoder();
const encoder = new TextEncoder();
const send = msg => issueCommand(encoder.encode(JSON.stringify(msg)).buffer);
const string = '0123456789';
const bytes = encoder.encode(string).subarray(1, 4);
send(bytes instanceof Uint8Array);
send(ArrayBuffer.isView(bytes));
const restring = decoder.decode(bytes);
send(restring === string.slice(1, 4));
`);
for (const pass of opts.messages.map(s => JSON.parse(s))) {
t.assert(pass);
}
});

0 comments on commit 3531132

Please sign in to comment.