#javascriptcore #jsc #run-time #javascript

otter-jsc-core

Safe Rust wrappers for JavaScriptCore

3 releases

Uses new Rust 2024

0.1.2 Jan 15, 2026
0.1.1 Jan 15, 2026
0.1.0 Jan 15, 2026

#639 in Development tools


Used in 4 crates (via otter-runtime)

MIT license

83KB
1.5K SLoC

Safe wrappers for JavaScriptCore.

This crate provides memory-safe, RAII-based wrappers around the raw JSC FFI bindings in jsc-sys.

Example

use otter_jsc_core::JscContext;

let ctx = JscContext::new().unwrap();
let result = ctx.eval("1 + 1").unwrap();
assert_eq!(result.to_number().unwrap(), 2.0);

Thread Safety

All types in this crate are !Send and !Sync because JavaScriptCore contexts and values are not thread-safe. Attempting to use them from multiple threads causes undefined behavior.

For multi-threaded usage, use otter-runtime's EngineHandle which provides a thread-safe API by marshaling operations to dedicated runtime threads.

Example: Wrong (won't compile)

use otter_jsc_core::JscContext;
use std::thread;

let ctx = JscContext::new().unwrap();
thread::spawn(move || {
    ctx.eval("1 + 1"); // Error: JscContext is !Send
});

Example: Correct

use otter_runtime::EngineHandle;

let handle = engine.handle(); // EngineHandle is Send + Sync
tokio::spawn(async move {
    handle.eval("1 + 1").await; // OK: marshaled to runtime thread
});

otter-jsc-core

Safe Rust wrappers for JavaScriptCore.

Overview

This crate provides safe, ergonomic Rust bindings to JavaScriptCore (JSC). It wraps the low-level FFI bindings from otter-jsc-sys with proper memory management and error handling.

Features

  • Safe context and value management
  • Automatic memory management with RAII
  • JSON serialization/deserialization
  • Function callbacks from JavaScript to Rust
  • Property access and manipulation

Usage

use otter_jsc_core::{JscContext, JscValue};

fn main() -> Result<(), otter_jsc_core::JscError> {
    let ctx = JscContext::new()?;

    // Evaluate JavaScript
    let result = ctx.eval("1 + 2")?;
    println!("Result: {}", result.to_number()?);

    // Create values
    let obj = ctx.create_object()?;
    obj.set_property("name", ctx.create_string("Otter")?)?;

    Ok(())
}

Platform Support

See otter-jsc-sys for platform support details.

License

MIT

Dependencies

~0.5–4MB
~73K SLoC