Skip to content

Commit

Permalink
feat(xsnap): Add base 64 bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal committed Sep 11, 2021
1 parent 082db81 commit b057b0d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/xsnap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"ses": "^0.14.1"
},
"devDependencies": {
"@endo/base64": "^0.2.6",
"@rollup/plugin-node-resolve": "^6.1.0",
"ava": "^3.12.1",
"c8": "^7.7.2",
Expand Down
6 changes: 5 additions & 1 deletion packages/xsnap/src/xsrepl.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ async function main() {
let vat = xsnap({ ...xsnapOptions, handleCommand });

await vat.evaluate(`
const compartment = new Compartment();
const compartment = new Compartment({
TextEncoder,
TextDecoder,
Base64
});
function handleCommand(request) {
const command = String.fromArrayBuffer(request);
let result = compartment.evaluate(command);
Expand Down
56 changes: 56 additions & 0 deletions packages/xsnap/test/test-xs-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import test from 'ava';
import * as proc from 'child_process';
import * as os from 'os';
import { encodeBase64, decodeBase64 } from '@endo/base64';
import { xsnap } from '../src/xsnap.js';
import { options } from './message-tools.js';

Expand Down Expand Up @@ -50,6 +51,61 @@ test('simple TextEncoder / TextDecoder are available', async t => {
t.deepEqual(opts.messages, ['"Hello! 😊"', '"A\\u0000A"']);
});

test('Base64.encode', async t => {
const opts = options(io);
const vat = xsnap(opts);
t.teardown(() => vat.terminate());
await vat.evaluate(`
const encoder = new TextEncoder();
globalThis.handleCommand = inputBuffer => {
const outputString = Base64.encode(inputBuffer);
const outputUint8Array = encoder.encode(outputString);
globalThis.issueCommand(outputUint8Array.buffer);
};
`);
const inputUint8Array = new TextEncoder().encode('Hello, World! 😃🌍');
const expectedOutputString = encodeBase64(inputUint8Array);
await vat.issueCommand(inputUint8Array);
t.deepEqual(opts.messages, [expectedOutputString]);
});

test.failing('Base64.encode degenerate input case', async t => {
const opts = options(io);
const vat = xsnap(opts);
t.teardown(() => vat.terminate());
await vat.evaluate(`
const encoder = new TextEncoder();
globalThis.handleCommand = inputBuffer => {
const outputString = Base64.encode(inputBuffer);
const outputUint8Array = encoder.encode(outputString);
globalThis.issueCommand(outputUint8Array.buffer);
};
`);
const inputUint8Array = new Uint8Array();
const expectedOutputString = '';
await vat.issueCommand(inputUint8Array);
t.deepEqual(opts.messages, [expectedOutputString]);
});

test('Base64.decode', async t => {
const opts = options(io);
const vat = xsnap(opts);
t.teardown(() => vat.terminate());
await vat.evaluate(`
const decoder = new TextDecoder();
globalThis.handleCommand = inputBuffer => {
const inputString = decoder.decode(inputBuffer);
const outputBuffer = Base64.decode(inputString);
globalThis.issueCommand(outputBuffer);
};
`);
const expectedOutputString = 'Hello, World! 😃🌍 ';
const expectedOutputUint8Array = new TextEncoder().encode(expectedOutputString);
const inputString = encodeBase64(expectedOutputUint8Array);
await vat.issueStringCommand(inputString);
t.deepEqual(opts.messages, [expectedOutputString]);
});

test('bigint map key', async t => {
const opts = options(io);
const vat = xsnap(opts);
Expand Down

0 comments on commit b057b0d

Please sign in to comment.