V8 - A wrapper for the V8 Javascript engine


#1

Have you ever wanted to embed Javascript in your Rust application? I have wanted that for a long time, and I previously created the duk crate to that end. However, interpreting Javascript like Duktape does can be pretty slow, and people want to use ES6 features, which Duktape doesn’t support.

So I spent the last two months wrapping the V8 Javascript engine. V8 is used for example in node.js and Google Chrome to execute Javascript.

The wrapper is quite full-featured, supporting most compilation, execution and embedding use-cases (check out the rustdoc below!), but V8 has a huge surface area and it will take a long time for me alone to cover it all. Many other features are missing, (I have for example not tested the build on Mac OS X) so I would really appreciate contributions, large or small.

Example:

use v8::{self, value};

// Create a V8 heap
let isolate = v8::Isolate::new();
// Create a new context of execution
let context = v8::Context::new(&isolate);

// Load the source code that we want to evaluate
let source = value::String::from_str(&isolate, "'Hello, ' + 'World!'");

// Compile the source code.  `unwrap()` panics if the code is invalid,
// e.g. if there is a syntax  error.
let script = v8::Script::compile(&isolate, &context, &source).unwrap();

// Run the compiled script.  `unwrap()` panics if the code threw an
// exception.
let result = script.run(&context).unwrap();

// Convert the result to a value::String.
let result_str = result.to_string(&context);

// Success!
assert_eq!("Hello, World!", result_str.value());