Git Product home page Git Product logo

jsrt-rs's Introduction

jsrt-rs

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 with jsrt-sys.

Documentation

https://docs.rs/jsrt

Installation

Add this to your Cargo.toml:

[dependencies]
jsrt = "0.1.0"

and this to your crate root:

extern crate jsrt;

This library, by itself is simple and easily installed, but its jsrt-sys dependency is not. To ensure a successful build, please view the jsrt-sys build instructions.

Example

Hello World

extern crate jsrt;

fn main() {
  let runtime = jsrt::Runtime::new().unwrap();
  let context = jsrt::Context::new(&runtime).unwrap();
  let guard = context.make_current().unwrap();

  let result = jsrt::Script::eval(&guard, "(5 + 5)").unwrap();
  assert_eq!(result.to_integer(&guard), 10);
}

Function - Multiply

extern crate jsrt;

fn main() {
  let runtime = jsrt::Runtime::new().unwrap();
  let context = jsrt::Context::new(&runtime).unwrap();
  let guard = context.make_current().unwrap();

  let multiply = jsrt::value::Function::new(&guard, Box::new(|guard, info| {
      let result = info.arguments[0].to_integer(guard)
                 * info.arguments[1].to_integer(guard);
      Ok(jsrt::value::Number::new(guard, result).into())
  }));

  let result = multiply.call(&guard, &jsrt::value::null(&guard), &[
      jsrt::value::Number::new(&guard, 191).into(),
      jsrt::value::Number::new(&guard, 7).into(),
  ]).unwrap();

  assert_eq!(result.to_integer(&guard), 1337);
}

jsrt-rs's People

Contributors

darfink avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.