From 48a93e580f32ba74a612a8f079caad2bea996216 Mon Sep 17 00:00:00 2001 From: XmiliaH Date: Thu, 24 Feb 2022 21:39:12 +0100 Subject: [PATCH] Smaller imrovements --- lib/setup-sandbox.js | 3 ++- lib/transformer.js | 17 ++++++++++------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/lib/setup-sandbox.js b/lib/setup-sandbox.js index de49fdd..2149cc3 100644 --- a/lib/setup-sandbox.js +++ b/lib/setup-sandbox.js @@ -308,7 +308,8 @@ const withProxy = localObjectFreeze({ const interanState = localObjectFreeze({ __proto__: null, wrapWith(x) { - return new LocalProxy(x, withProxy); + if (x === null || x === undefined) return x; + return new LocalProxy(localObject(x), withProxy); }, handleException: ensureThis, import(what) { diff --git a/lib/transformer.js b/lib/transformer.js index e336525..f33f6ab 100644 --- a/lib/transformer.js +++ b/lib/transformer.js @@ -16,7 +16,7 @@ function makeNiceSyntaxError(message, code, filename, location, tokenizer) { while (end < code.length && !acornIsNewLine(code.charCodeAt(end))) { end++; } - let markerEnd = tokenizer.end; + let markerEnd = tokenizer.start === location ? tokenizer.end : location + 1; if (!markerEnd || markerEnd > end) markerEnd = end; let markerLen = markerEnd - location; if (markerLen <= 0) markerLen = 1; @@ -101,7 +101,11 @@ function transformer(args, body, isAsync, isGenerator, filename) { let internStateValiable = undefined; acornWalkFull(ast, (node, state, type) => { - if (type === 'CatchClause') { + if (type === 'Function') { + if (node.async) hasAsync = true; + } + const nodeType = node.type; + if (nodeType === 'CatchClause') { const param = node.param; if (param) { const name = assertType(param, 'Identifier').name; @@ -115,7 +119,7 @@ function transformer(args, body, isAsync, isGenerator, filename) { }); } } - } else if (type === 'WithStatement') { + } else if (nodeType === 'WithStatement') { insertions.push({ __proto__: null, pos: node.object.start, @@ -128,27 +132,26 @@ function transformer(args, body, isAsync, isGenerator, filename) { order: TO_RIGHT, code: ')' }); - } else if (type === 'Identifier') { + } else if (nodeType === 'Identifier') { if (node.name === INTERNAL_STATE_NAME) { if (internStateValiable === undefined || internStateValiable.start > node.start) { internStateValiable = node; } } - } else if (type === 'ImportExpression') { + } else if (nodeType === 'ImportExpression') { insertions.push({ __proto__: null, pos: node.start, order: TO_RIGHT, code: INTERNAL_STATE_NAME + '.' }); - } else if (type === 'Function') { - if (node.async) hasAsync = true; } }); if (internStateValiable) { throw makeNiceSyntaxError('Use of internal vm2 state variable', code, filename, internStateValiable.start, { __proto__: null, + start: internStateValiable.start, end: internStateValiable.end }); }