chakracore-rs is an iditiomatic wrapper for ChakraCore, written in Rust.
This repository contains two crates:
This is a wrapper around the JavaScript Runtime (JSRT), used in Microsoft Edge and node-chakracore. The library is still in pre-release and is not yet stable. The tests try to cover as much functionality as possible but memory leaks and segfaults may occur. If you want a more stable library, use the underlying API directly; chakracore-sys.
Add this to your Cargo.toml
:
[dependencies]
chakracore = "0.2"
... and this to your crate root:
extern crate chakracore as js;
NOTE: See additional build instructions for chakracore-sys
extern crate chakracore as js;
fn main() {
let runtime = js::Runtime::new().unwrap();
let context = js::Context::new(&runtime).unwrap();
let guard = context.make_current().unwrap();
let result = js::script::eval(&guard, "5 + 5").unwrap();
assert_eq!(result.to_integer(&guard), 10);
}
extern crate chakracore as js;
fn main() {
let runtime = js::Runtime::new().unwrap();
let context = js::Context::new(&runtime).unwrap();
let guard = context.make_current().unwrap();
let multiply = js::value::Function::new(&guard, Box::new(|guard, info| {
let result = info.arguments[0].to_integer(guard)
* info.arguments[1].to_integer(guard);
Ok(js::value::Number::new(guard, result).into())
}));
let result = multiply.call(&guard, &[
&js::value::Number::new(&guard, 191).into(),
&js::value::Number::new(&guard, 7).into(),
]).unwrap();
assert_eq!(result.to_integer(&guard), 1337);
}
This library handles the static and dynamic linking of the JavaScript Runtime. The rust bindings are generated (on the fly) for the interface, therefore the entire API is exposed and accessable.
A Hello World example can be found in src/lib.rs.
An example of the generated bindings can be found here.
This library builds the ChakraCore component in the source tree. It is cloned by the build script and built in test-mode (same as release, but includes more runtime checks). If custom build settings are desired, ChakraCore can be built manually, out of tree, and specified using two environment variables:
CHAKRA_SOURCE
: The root of the ChakraCore checkout.CHAKRA_BUILD
: The bin
directory of the build.
%CHAKRA_SOURCE%\Build\VcBuild\bin\{BUILD_TYPE}
.$CHAKRA_SOURCE/BuildLinux/{BUILD_TYPE}
.This script has not been tested with the --embed-icu
option.
By default, this library links ChakraCore dynamically. There is a feature
called static
that builds it by linking to the generated archive instead.
On windows, only shared library builds are available as of this time (see
#279).
The library naturally shares all of ChakraCore's dependencies. Beyond this,
rust-bindgen is used in the build
script, which requires clang-3.8
or later. On Unix pkg-config
is required as
well.
clang-3.8
or later. Downloads can be found
here.PATH
during installation.rustup install stable-msvc
).$ brew install cmake icu4c llvm38 pkg-config
# apt-get install -y build-essential cmake clang libunwind8-dev \
# libicu-dev llvm-3.8-dev libclang-3.8-dev pkg-config liblzma-dev
Ensure that you are running in a Visual Studio command line environment,
either by sourcing vcvarsall.bat
, or by building from the Visual
Studio Command Prompt.
$ cargo test -vv
$ cargo test -vv [--features static]
In case you find yourself stuck in the build process, open an issue.
This library has been built on macOS 10.12 x86_64
, Ubuntu 16.10 x86_64
and
Windows 10 x86_x64
.