Giter VIP home page Giter VIP logo

rusty_jsc's Introduction

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

fn main() {
    let mut context = JSContext::default();
    match context.evaluate_script("'Hello World!'", 1) {
        Ok(value) => {
            println!("{}", value.to_string(&context).unwrap());
        }
        Err(e) => {
            println!("Uncaught: {}", e.to_string(&context).unwrap())
        }
    }
}

Callbacks from JavaScript to Rust

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

#[callback]
fn greet(
    ctx: JSContext,
    function: JSObject,
    this: JSObject,
    args: &[JSValue],
) -> Result<JSValue, JSValue> {
    Ok(JSValue::string(&ctx, format!("Hello, {}", args[0].to_string(&ctx).unwrap())))
}

fn main() {
    let mut context = JSContext::default();
    let callback = JSValue::callback(&context, Some(greet));
    let global = context.get_global_object();
    global.set_property(&context, "greet", callback).unwrap();

    match context.evaluate_script("greet('Tom')", 1) {
        Ok(value) => {
            println!("{}", value.to_string(&context).unwrap());
        }
        Err(e) => {
            println!("Uncaught: {}", e.to_string(&context).unwrap())
        }
    }
}

Passing functions to a callback

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

#[callback]
fn greet(
    ctx: JSContext,
    function: JSObject,
    this: JSObject,
    args: &[JSValue],
) -> Result<JSValue, JSValue> {
    // Parse the argument as a function and call it with an argument
    let callback_function = args[0].to_object(&ctx).unwrap().call(&ctx, None, &[JSValue::string(&ctx, "Tom")]).unwrap();
    Ok(callback_function)
}

fn main() {
    let mut context = JSContext::default();
    let callback = JSValue::callback(&context, Some(greet));
    let global = context.get_global_object();
    global.set_property(&context, "greet", callback).unwrap();

    match context.evaluate_script("greet((name) => 'Hello, ' + name)", 1) {
        Ok(value) => {
            println!("{}", value.to_string(&context).unwrap());
        }
        Err(e) => {
            println!("Uncaught: {}", e.to_string(&context).unwrap())
        }
    }
}

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.

rusty_jsc's People

Contributors

adrien-zinger avatar mishushakov avatar penberg avatar syrusakbary avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

rusty_jsc's Issues

Pass values to/from [callback] function?

I haven't been able to figure out how to pass value(s) to a Rust callback nor how to read an output value on the JS side from a Rust callback.

If you could provide a simple example for each of those that'd be very much appreciated!

WebCore

Hey, this looks great!

Applogies if this is a silly question, I’ve essentially just started exploring the space :)

Is it possible to load & call into WebCore APIs; eg fetch?

Also, perhaps relatedly, how I can discover what the context provides by default? I see the punjs (lol, A+) example runs a console.log script but 1) where is console coming from? 2) I don’t see it log, so is that effectively a noop since evaluateScript only prints the return value? Or do I have to define my own console global callback which passes thru to println?

Thanks !

Doesn't build on Windows

The build script for rusty-jsc-sys doesn't include a main() function on Windows, which means builds will fail with the following error on Windows:

error[E0601]: `main` function not found in crate `build_script_build`
 --> C:\Users\Work\.cargo\registry\src\github.com-1ecc6299db9ec823\rusty_jsc_sys-0.1.0\build.rs:9:2
  |
9 | }
  |  ^ consider adding a `main` function to `C:\Users\Work\.cargo\registry\src\github.com-1ecc6299db9ec823\rusty_jsc_sys-0.1.0\build.rs`

I suspect we'll need to do a bit more to make things link than just stubbing out the main() function, but I've never built JavaScriptCore from source so I'm not sure how to go about it.

Serde for JSC?

Are there any plans for adding a Serde serializer/deserializer for JSC values?

Get tags for released versions

Hi!

I'm not sure if the information is entirely recoverable but is there a chance to get tags to match the released versions? (or at least 0.1.0). While trying to workaround for some seemingly missing concepts (specifically Promises and Protection) I see there's a commit dated on Apr 25th that includes some of these developments but these are not on 0.1.0 even tho it's dated Apr 27th.

So if there's a way to get that it'd be terrific. Also to know if there are plans on releasing the latest work.

Thanks for the hard work! I'm still in my very early stages with rust but I'm enjoying working with this wrapper.

Building with cargo-wasi

Hello, I'm trying to use cargo-wasi to build a rust app that uses rusty_jsc, but I'm seeing this error when I run cargo wasi build:

❯ cargo wasi run 
   Compiling rusty_jsc_sys v0.0.2
   Compiling anyhow v1.0.66
   Compiling rusty_jsc_sys v0.0.3
error: library kind `framework` is only supported on Apple targets

error: could not compile `rusty_jsc_sys` due to previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `rusty_jsc_sys` due to previous error

My Cargo.toml is simple:

[package]
name = "project"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rusty_jsc = "0.0.2"
rusty_jsc_macros = "0.0.3"
rusty_jsc_sys = "0.0.3"

And the main.rs is just one of your examples:

use rusty_jsc::JSContext;

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

Any ideas? I'm new to the world of WASI so hopefully its something I just don't understand

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.