The v8 function (formerly called new_context) creates a
new V8 context. A context provides an execution environment that allows
separate, unrelated, JavaScript code to run in a single instance of V8, like a
tab in a browser.
character vector indicating name(s) of the global environment. Use NULL for no name.
console
expose console API (console.log, console.warn, console.error).
typed_arrays
enable support for typed arrays (part of ECMA6). This adds a bunch of additional
functions to the global namespace.
Details
V8 contexts cannot be serialized but creating a new contexts and sourcing code
is very cheap. You can run as many parallel v8 contexts as you want. R packages
that use V8 can use a separate V8 context for each object or function call.
The ct$eval method evaluates a string of raw code in the same way
as eval would do in JavaScript. It returns a string with console output.
The ct$get, ct$assign and ct$call functions
on the other hand automatically convert arguments and return value from/to JSON,
unless an argument has been wrapped in JS(), see examples.
The ct$validate function is used to test if a piece of code is valid
JavaScript syntax within the context, and always returns TRUE or FALSE.
JSON is used for all data interchange between R and JavaScript. Therefore you can
(and should) only exchange data types that have a sensible JSON representation.
All arguments and objects are automatically converted according to the mapping
described in Ooms (2014), and implemented
by the jsonlite package in fromJSON and toJSON.
The name of the global object (i.e. global in node and window
in browsers) can be set with the global argument. A context always have a global
scope, even when no name is set. When a context is initiated with global = NULL,
the global environment can be reached by evaluating this in the global scope,
for example: ct$eval("Object.keys(this)").
Methods
console()
starts an interactive console
eval(src)
evaluates a string with JavaScript source code
validate(src)
test if a string of JavaScript code is syntactically valid
source(file)
evaluates a file with JavaScript code
get(name, ...)
convert a JavaScript to R via JSON. Optional arguments (...) are passed to fromJSON to set JSON coercion options.
assign(name, value)
copy an R object to JavaScript via JSON
call(fun, ...)
call a JavaScript function with arguments .... Arguments which are not wrapped in JS() automatically get converted to JSON