Title: | Interface for the 'QuickJS' Lightweight 'JavaScript' Engine |
---|---|
Description: | An 'R' interface to the 'QuickJS' portable 'JavaScript' engine. The engine and all 'R' to 'JavaScript' interoperability is bundled within the package, requiring no dependencies beyond a 'C' compiler. |
Authors: | Andrew R. Johnson [aut, cre] , Fabrice Bellard [cph] (Author of QuickJS sources and headers), Charlie Gordon [cph] (Author of QuickJS sources and headers), QuickJS-NG Authors [cph] (QuickJS-NG sources and headers) |
Maintainer: | Andrew R. Johnson <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.4.0 |
Built: | 2024-11-02 06:14:36 UTC |
Source: | https://github.com/andrjohns/QuickJSR |
An interface to the QuickJS lightweight Javascript engine
Maintainer: Andrew R. Johnson [email protected] (ORCID)
Other contributors:
Fabrice Bellard (Author of QuickJS sources and headers) [copyright holder]
Charlie Gordon (Author of QuickJS sources and headers) [copyright holder]
Useful links:
Report bugs at https://github.com/andrjohns/QuickJSR/issues
Function for returning the C/C++ flags needed for compilation using the package's headers
cxxflags(to_console = FALSE)
cxxflags(to_console = FALSE)
to_console |
Whether the result should be returned as a string |
Character string of CXX flags, or print flags to console and invisibly return NULL (for use in package Makevars or similar)
Use the QuickJS C API to convert a JSON string to an R object
from_json(json)
from_json(json)
json |
JSON string to convert to an R object |
R object
An initialised context within which to evaluate Javascript scripts or commands.
JSContext
JSContext
An object of class list
of length 1.
A JSContext object containing an initialised JavaScript context for evaluating scripts/commands
Assign a value to a variable in the current context
assign(var_name, value)
assign(var_name, value)
var_name |
The name of the variable to assign |
value |
The value to assign to the variable |
An object of class NULL
of length 0.
No return value, called for side effects
## Not run: ctx <- JSContext$new() ctx$assign("a", 1) ctx$get("a") ## End(Not run)
## Not run: ctx <- JSContext$new() ctx$assign("a", 1) ctx$get("a") ## End(Not run)
Call a specified function in the JavaScript context with the provided arguments.
call(function_name, ...)
call(function_name, ...)
function_name |
The function to be called |
... |
The arguments to be passed to the function |
An object of class NULL
of length 0.
The result of calling the specified function
## Not run: ctx <- JSContext$new() ctx$source(code = "function add(a, b) { return a + b; }") ctx$call("add", 1, 2) ## End(Not run)
## Not run: ctx <- JSContext$new() ctx$source(code = "function add(a, b) { return a + b; }") ctx$call("add", 1, 2) ## End(Not run)
Get the value of a variable from the current context
get(var_name)
get(var_name)
var_name |
The name of the variable to retrieve |
An object of class NULL
of length 0.
The value of the variable
## Not run: ctx <- JSContext$new() ctx$source(code = "var a = 1;") ctx$get("a") ## End(Not run)
## Not run: ctx <- JSContext$new() ctx$source(code = "var a = 1;") ctx$get("a") ## End(Not run)
Evaluate a provided JavaScript file or string within the initialised context.
Note that this method should only be used for initialising functions or values
within the context, no values are returned from this function. See the $call()
method for returning values.
source(file = NULL, code = NULL)
source(file = NULL, code = NULL)
file |
A path to the JavaScript file to load |
code |
A single string of JavaScript to evaluate |
An object of class NULL
of length 0.
No return value, called for side effects
## Not run: ctx <- JSContext$new() ctx$source(file = "path/to/file.js") ctx$source(code = "1 + 2") ## End(Not run)
## Not run: ctx <- JSContext$new() ctx$source(file = "path/to/file.js") ctx$source(code = "1 + 2") ## End(Not run)
Checks whether JS code string is valid code in the current context
validate(code_string)
validate(code_string)
code_string |
The JS code to check |
An object of class NULL
of length 0.
A boolean indicating whether code is valid
## Not run: ctx <- JSContext$new() ctx$validate("1 + 2") ## End(Not run)
## Not run: ctx <- JSContext$new() ctx$validate("1 + 2") ## End(Not run)
Function for returning the flags needed for linking to the package
ldflags(to_console = FALSE)
ldflags(to_console = FALSE)
to_console |
Whether the result should be returned as a string |
Character string of linker flags, or print flags to console and invisibly return NULL (for use in package Makevars or similar)
Evaluate a single Javascript expression.
qjs_eval(eval_string)
qjs_eval(eval_string)
eval_string |
A single string of the expression to evaluate |
The result of the provided expression
# Return the sum of two numbers: qjs_eval("1 + 2") # Concatenate strings: qjs_eval("'1' + '2'") # Create lists from objects: qjs_eval("var t = {'a' : 1, 'b' : 2}; t")
# Return the sum of two numbers: qjs_eval("1 + 2") # Concatenate strings: qjs_eval("'1' + '2'") # Create lists from objects: qjs_eval("var t = {'a' : 1, 'b' : 2}; t")
Get the version of the bundled QuickJS library
quickjs_version()
quickjs_version()
Character string of the version of the bundled QuickJS library
Use the QuickJS C API to convert an R object to a JSON string
to_json(arg, auto_unbox = FALSE)
to_json(arg, auto_unbox = FALSE)
arg |
Argument to convert to JSON |
auto_unbox |
Automatically unbox single element vectors |
JSON string