Skip to main content

Crate glue_v8

Crate glue_v8 

Source
Expand description

V8 Glue - Rust to V8 binding macros for OpenWorkers

Generate V8 callback boilerplate from Rust functions.

§Functions with state

State is passed via FunctionTemplate data (External containing Rc). Use {fn_name}_v8_template(scope, &state) to register the function.

#[glue_v8::method(state = Rc<TimerState>)]
fn schedule_timeout(state: &Rc<TimerState>, id: u64, delay: u64) {
    let _ = state.scheduler_tx.send(ScheduleTimeout(id, delay));
}

// Registration:
let state = Rc::new(TimerState { ... });
let func = schedule_timeout_v8_template(scope, &state)
    .get_function(scope).unwrap();

§Fast API Support

The fast attribute enables V8 Fast API for functions with primitive arguments. Fast API can be ~10x faster for hot functions.

#[glue_v8::method(fast)]
fn add(a: i32, b: i32) -> i32 {
    a + b
}

// Registration:
let template = add_v8_template(scope, None);
let func = template.get_function(scope).unwrap();

Requirements for Fast API:

  • Only primitive types: bool, i32, u32, i64, u64, f32, f64
  • No scope parameter (cannot use V8 APIs in fast path)
  • Return type must be a primitive or void

Attribute Macros§

method
Generate a V8 callback wrapper for a Rust function.