diff --git a/packages/xsnap/src/xsnap.c b/packages/xsnap/src/xsnap.c index 277bec2c213..4e33b97c20c 100644 --- a/packages/xsnap/src/xsnap.c +++ b/packages/xsnap/src/xsnap.c @@ -1389,10 +1389,12 @@ static int fxReadNetString(FILE *inStream, char** dest, size_t* len) } else { *(buf + *len) = 0; } - if (code != 0) { + if (code == 0) { + *dest = buf; + } else { + *dest = 0; free(buf); } - *dest = buf; } return code; } @@ -1473,12 +1475,14 @@ static void fx_issueCommand(xsMachine *the) size_t length; char* buf = NULL; - txSlot* arrayBuffer = &xsArg(0); - length = fxGetArrayBufferLength(the, arrayBuffer); + length = xsGetArrayBufferLength(xsArg(0)); buf = malloc(length); + if (!buf) { + fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT); + } - fxGetArrayBufferData(the, arrayBuffer, 0, buf, length); + xsGetArrayBufferData(xsArg(0), 0, buf, length); int writeError = fxWriteNetString(toParent, "?", buf, length); free(buf); @@ -1495,6 +1499,7 @@ static void fx_issueCommand(xsMachine *the) } xsResult = xsArrayBuffer(buf, len); + free(buf); } diff --git a/packages/xsnap/test/test-xs-perf.js b/packages/xsnap/test/test-xs-perf.js index ff442e72887..9cf903bad51 100644 --- a/packages/xsnap/test/test-xs-perf.js +++ b/packages/xsnap/test/test-xs-perf.js @@ -1,7 +1,27 @@ // eslint-disable-next-line import/no-extraneous-dependencies import test from 'ava'; +import * as childProcess from 'child_process'; +import * as os from 'os'; import { xsnap } from '../src/xsnap'; -import { options } from './test-xsnap'; + +const decoder = new TextDecoder(); + +const xsnapOptions = { + name: 'xsnap test worker', + spawn: childProcess.spawn, + os: os.type(), + stderr: 'inherit', + stdout: 'inherit', +}; + +export function options() { + const messages = []; + async function handleCommand(message) { + messages.push(decoder.decode(message)); + return new Uint8Array(); + } + return { ...xsnapOptions, handleCommand, messages }; +} test('meter details', async t => { const opts = options();