4 releases

0.1.0 Apr 28, 2023
0.0.3 Jan 14, 2023
0.0.2 Sep 2, 2022
0.0.1 Aug 27, 2022

#1152 in Web programming

Download history 457/week @ 2024-08-31 318/week @ 2024-09-07 528/week @ 2024-09-14 481/week @ 2024-09-21 618/week @ 2024-09-28 514/week @ 2024-10-05 1510/week @ 2024-10-12 1118/week @ 2024-10-19 1321/week @ 2024-10-26 1228/week @ 2024-11-02 587/week @ 2024-11-09 940/week @ 2024-11-16 521/week @ 2024-11-23 517/week @ 2024-11-30 892/week @ 2024-12-07 510/week @ 2024-12-14

2,871 downloads per month
Used in 5 crates (3 directly)

MIT license

63KB
1.5K SLoC

JavaScriptCore API for Rust

crates docs

This library provides a Rust API for the JavaScriptCore engine with the following goals:

  • High-level API like the JavaScriptCore API for Swift
  • Wrap the low-level C++ API instead of jsc to avoid the dependency to GTK.

Getting Started

Implementing a JavaScript runtime

Please check out PunJS for an example of how to implement a JavaScript runtime with rusty_jsc.

Evaluating a JavaScript script

use rusty_jsc::JSContext;

let mut context = JSContext::default();
let value = context.evaluate_script("'hello, world'", 1);
if let Some(value) = value {
    println!("{}", value.to_string(&context));
    // Prints:
    // hello, world
}

Callbacks from JavaScript to Rust

use rusty_jsc::{JSContext, JSValue};
use rusty_jsc_macros::callback;

// The JavaScript code calls this Rust function.
#[callback]
fn foo(_context: JSContext) {
    println!("hello from Rust land!");
}

fn main() {
    let mut context = JSContext::default();
    let callback = JSValue::callback(&context, Some(foo));
    let mut global = context.get_global_object();
    global.set_property(&context, "foo".to_string(), callback);
    context.evaluate_script("foo()", 1);
    // Prints:
    // hello from Rust land!
}

FAQ

What about the other JavaScriptCore bindings for Rust?

The wrappers in rusty_jsc are built against <JavaScriptCore/JavaScript.h> header rather than the jsc variant that requires GTK.

Why JavaScriptCore when there's already rusty_v8?

Bun has shown that JavaScriptCore is a worthy contender to V8 on the server-side, so let's bring it over to the Rust ecosystem as well.

How were the C++ low-level bindings generated?

I first used bindgen to do the rough conversion of JavaScript/JavaScript.h header and then cleaned it up by hand. The plan is to maintain the low-level bindings by hand.

Dependencies

~1.5MB
~37K SLoC