From 2937a79c450f63449d6604c3a3c332888810ab1c Mon Sep 17 00:00:00 2001 From: Ujjwal Sharma Date: Thu, 28 Jun 2018 12:05:19 +0530 Subject: [PATCH] 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 Reviewed-By: Tiancheng "Timothy" Gu Reviewed-By: John-David Dalton Reviewed-By: Gus Caplan Reviewed-By: James M Snell --- doc/api/vm.md | 28 +++++++ lib/vm.js | 95 ++++++++++++++++++++++ src/node_contextify.cc | 139 ++++++++++++++++++++++++++++++++ src/node_contextify.h | 2 + test/parallel/test-vm-basic.js | 142 +++++++++++++++++++++++++++++++++ 5 files changed, 406 insertions(+) diff --git a/doc/api/vm.md b/doc/api/vm.md index defa029821aa00..e7aafb9e74e65c 100644 --- a/doc/api/vm.md +++ b/doc/api/vm.md @@ -637,6 +637,34 @@ console.log(globalVar); // 1000 ``` +## vm.compileFunction(code[, params[, options]]) + +* `code` {string} The body of the function to compile. +* `params` {string[]} An array of strings containing all parameters for the + function. +* `options` {Object} + * `filename` {string} Specifies the filename used in stack traces produced + by this script. **Default:** `''`. + * `lineOffset` {number} Specifies the line number offset that is displayed + in stack traces produced by this script. **Default:** `0`. + * `columnOffset` {number} Specifies the column number offset that is displayed + in stack traces produced by this script. **Default:** `0`. + * `cachedData` {Buffer} Provides an optional `Buffer` with V8's code cache + data for the supplied source. + * `produceCachedData` {boolean} Specifies whether to produce new cache data. + **Default:** `false`. + * `parsingContext` {Object} The sandbox/context in which the said function + should be compiled in. + * `contextExtensions` {Object[]} An array containing a collection of context + extensions (objects wrapping the current scope) to be applied while + compiling. **Default:** `[]`. + +Compiles the given code into the provided context/sandbox (if no context is +supplied, the current context is used), and returns it wrapped inside a +function with the given `params`. + ## vm.createContext([sandbox[, options]])