summaryrefslogtreecommitdiff
path: root/test/parallel/test-vm-basic.js
diff options
context:
space:
mode:
authorUjjwal Sharma <usharma1998@gmail.com>2018-06-28 12:05:19 +0530
committerMichaƫl Zasso <targos@protonmail.com>2018-08-29 16:41:01 +0200
commit1abbe0a2123e8333922894258389c2d5c1568472 (patch)
tree606df1005680a3769966b19f98c740ff5576f748 /test/parallel/test-vm-basic.js
parent1a25f9639a9668d8ea90022b0f3d3b47d29971b6 (diff)
downloadnode-new-1abbe0a2123e8333922894258389c2d5c1568472.tar.gz
vm: add bindings for v8::CompileFunctionInContext
Adds a method compileFunction to the vm module, which serves as a binding for v8::CompileFunctionInContext with appropriate args for specifying the details, and provide params for the wrapper. Eventually, we would be changing Module._compile to use this internally over the standard Module.wrap PR-URL: https://github.com/nodejs/node/pull/21571 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: John-David Dalton <john.david.dalton@gmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'test/parallel/test-vm-basic.js')
-rw-r--r--test/parallel/test-vm-basic.js142
1 files changed, 142 insertions, 0 deletions
diff --git a/test/parallel/test-vm-basic.js b/test/parallel/test-vm-basic.js
index 2dfc1e9cb7..c5adde4e5b 100644
--- a/test/parallel/test-vm-basic.js
+++ b/test/parallel/test-vm-basic.js
@@ -128,3 +128,145 @@ const vm = require('vm');
'Received type object'
});
});
+
+// vm.compileFunction
+{
+ assert.strictEqual(
+ vm.compileFunction('console.log("Hello, World!")').toString(),
+ 'function () {\nconsole.log("Hello, World!")\n}'
+ );
+
+ assert.strictEqual(
+ vm.compileFunction(
+ 'return p + q + r + s + t',
+ ['p', 'q', 'r', 's', 't']
+ )('ab', 'cd', 'ef', 'gh', 'ij'),
+ 'abcdefghij'
+ );
+
+ vm.compileFunction('return'); // Should not throw on 'return'
+
+ common.expectsError(() => {
+ vm.compileFunction(
+ '});\n\n(function() {\nconsole.log(1);\n})();\n\n(function() {'
+ );
+ }, {
+ type: SyntaxError,
+ message: 'Unexpected token }'
+ });
+
+ // Tests for failed argument validation
+ common.expectsError(() => vm.compileFunction(), {
+ type: TypeError,
+ code: 'ERR_INVALID_ARG_TYPE',
+ message: 'The "code" argument must be of type string. ' +
+ 'Received type undefined'
+ });
+
+ vm.compileFunction(''); // Should pass without params or options
+
+ common.expectsError(() => vm.compileFunction('', null), {
+ type: TypeError,
+ code: 'ERR_INVALID_ARG_TYPE',
+ message: 'The "params" argument must be of type Array. ' +
+ 'Received type object'
+ });
+
+ // vm.compileFunction('', undefined, null);
+
+ const optionTypes = {
+ 'filename': 'string',
+ 'columnOffset': 'number',
+ 'lineOffset': 'number',
+ 'cachedData': 'Uint8Array',
+ 'produceCachedData': 'boolean',
+ };
+
+ for (const option in optionTypes) {
+ common.expectsError(() => {
+ vm.compileFunction('', undefined, { [option]: null });
+ }, {
+ type: TypeError,
+ code: 'ERR_INVALID_ARG_TYPE',
+ message: `The "options.${option}" property must be of type ` +
+ `${optionTypes[option]}. Received type object`
+ });
+ }
+
+ // Testing for context-based failures
+ [Boolean(), Number(), null, String(), Symbol(), {}].forEach(
+ (value) => {
+ common.expectsError(() => {
+ vm.compileFunction('', undefined, { parsingContext: value });
+ }, {
+ type: TypeError,
+ code: 'ERR_INVALID_ARG_TYPE',
+ message: 'The "options.parsingContext" property must be of type ' +
+ `Context. Received type ${typeof value}`
+ });
+ }
+ );
+
+ assert.strictEqual(
+ vm.compileFunction(
+ 'return a;',
+ undefined,
+ { contextExtensions: [{ a: 5 }] }
+ )(),
+ 5
+ );
+
+ common.expectsError(() => {
+ vm.compileFunction('', undefined, { contextExtensions: null });
+ }, {
+ type: TypeError,
+ code: 'ERR_INVALID_ARG_TYPE',
+ message: 'The "options.contextExtensions" property must be of type Array' +
+ '. Received type object'
+ });
+
+ common.expectsError(() => {
+ vm.compileFunction('', undefined, { contextExtensions: [0] });
+ }, {
+ type: TypeError,
+ code: 'ERR_INVALID_ARG_TYPE',
+ message: 'The "options.contextExtensions[0]" property must be of type ' +
+ 'object. Received type number'
+ });
+
+ const oldLimit = Error.stackTraceLimit;
+ // Setting value to run the last three tests
+ Error.stackTraceLimit = 1;
+
+ common.expectsError(() => {
+ vm.compileFunction('throw new Error("Sample Error")')();
+ }, {
+ message: 'Sample Error',
+ stack: 'Error: Sample Error\n at <anonymous>:1:7'
+ });
+
+ common.expectsError(() => {
+ vm.compileFunction(
+ 'throw new Error("Sample Error")',
+ [],
+ { lineOffset: 3 }
+ )();
+ }, {
+ message: 'Sample Error',
+ stack: 'Error: Sample Error\n at <anonymous>:4:7'
+ });
+
+ common.expectsError(() => {
+ vm.compileFunction(
+ 'throw new Error("Sample Error")',
+ [],
+ { columnOffset: 3 }
+ )();
+ }, {
+ message: 'Sample Error',
+ stack: 'Error: Sample Error\n at <anonymous>:1:10'
+ });
+
+ // Resetting value
+ Error.stackTraceLimit = oldLimit;
+}