V8 version 0.5: typed arrays and sql.js

February 15, 2015


Earlier this month, V8 version 0.5 appeared on CRAN. This version adds support typed arrays as specified in ECMA 6 in order to support high performance computing and libraries compiled with emscripten. A big thanks goes to Kenton Russell (@timelyportfolio) for suggesting these features.

Example: sql.js

These new features increase the amount of JavaScript libraries that will run out-of-the-box on V8. For example, sql.js is a port of SQLite to JavaScript, by compiling the SQLite C code with Emscripten:

# Load V8
library(V8)
stopifnot(packageVersion("V8") >= "0.5")

# Create JavaScript context and load sql.js
ct <- new_context()
ct$source("https://raw.githubusercontent.com/kripken/sql.js/master/js/sql.js")
 
# Evaluate JavaScript code
ct$eval('
var db = new SQL.Database()
db.run("CREATE TABLE hello (person char, age int);")
db.run("INSERT INTO hello VALUES (\'jerry\', 34);")
db.run("INSERT INTO hello VALUES (\'mary\', 27);")
db.run("INSERT INTO hello VALUES (\'joe\', 65);")
db.run("INSERT INTO hello VALUES (\'anna\', 18);")

// query:
var out = []
var stmt = db.prepare("SELECT * FROM hello WHERE age < 40");
while (stmt.step()) out.push(stmt.getAsObject());
')
 
# Copy the object from JavaScript to R
data <- ct$get("out")
print(data)

More V8 fun

Several other examples are available on gist, for example cheerio (html parsing), turf.js (geojson), viz.js and KaTeX. I am working on several packages that implement actual bindings to JavaScript libraries using V8. The first ones have just landed on CRAN: minimist and js.

To learn more, have a look at the vignettes:

Questions, suggestions? Find me on twitter or github.