darfink / chakracore-rs

An idiomatic Rust wrapper for the JSRT interface
39 stars 10 forks source link
chakracore javascript rust

NOTE: This project is not currently maintained, due to ChakraCore itself is no longer being actively developed.

# `chakracore-rs` [![crates.io version][crate-shield]][crate] [![Documentation][docs-shield]][docs] [![Language (Rust)][rust-shield]][rust]

chakracore-rs is an iditiomatic wrapper for ChakraCore, written in Rust.

This repository contains two crates:

chakracore

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.

Installation

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

Examples

Hello World

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);
}

Function - Multiply

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);
}

chakracore-sys

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.

Requirements

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:

This script has not been tested with the --embed-icu option.

Static/Shared

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).

Prerequisites

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.

Windows
macOS
$ brew install cmake icu4c llvm38 pkg-config
Debian-based linuxes
# apt-get install -y build-essential cmake clang libunwind8-dev \
#     libicu-dev llvm-3.8-dev libclang-3.8-dev pkg-config liblzma-dev

Building

In case you find yourself stuck in the build process, open an issue.

Status

This library has been built on macOS 10.12 x86_64, Ubuntu 16.10 x86_64 and Windows 10 x86_x64.