openworkers-glue-v8

Crates.ioopenworkers-glue-v8
lib.rsopenworkers-glue-v8
version0.4.0
created_at2026-01-17 17:48:50.176212+00
updated_at2026-01-18 11:50:46.225266+00
descriptionV8 Glue - Rust to V8 binding macros for OpenWorkers
homepage
repositoryhttps://github.com/openworkers/glue-v8
max_upload_size
id2050856
size94,927
(max-lt)

documentation

README

gv8 - V8 Glue for OpenWorkers

Proc-macro crate that generates V8 callback boilerplate from Rust functions.

Usage

use std::rc::Rc;

// Basic function
#[gv8::method]
fn add(_scope: &mut v8::PinScope, a: f64, b: f64) -> f64 {
    a + b
}

// With state from context slot
#[gv8::method(state = Rc<MyState>)]
fn get_count(_scope: &mut v8::PinScope, state: &Rc<MyState>) -> i32 {
    state.count.get()
}

// With Result (Err throws JS exception)
#[gv8::method]
fn parse(_scope: &mut v8::PinScope, input: String) -> Result<f64, String> {
    input.parse().map_err(|e| format!("{}", e))
}

// With Promise (returns JS Promise)
#[gv8::method(promise)]
fn async_op(_scope: &mut v8::PinScope, val: i32) -> Result<i32, String> {
    if val > 0 { Ok(val * 2) } else { Err("must be positive".into()) }
}

// Optional parameters
#[gv8::method]
fn greet(_scope: &mut v8::PinScope, name: String, title: Option<String>) -> String {
    match title {
        Some(t) => format!("{} {}", t, name),
        None => name,
    }
}

Generated Code

The macro generates a {fn_name}_v8 wrapper function that:

  • Extracts arguments from FunctionCallbackArguments
  • Converts types using serde_v8
  • Handles V8 Local types directly (Function, Uint8Array, etc.)
  • Retrieves state from context slots
  • Converts return values back to V8
  • Throws exceptions on errors

Supported Types

Parameters:

  • Primitives: i32, u32, f64, bool, String
  • Optional: Option<T> (None for undefined/null/missing)
  • V8 Local types: v8::Local<v8::Function>, v8::Local<v8::Uint8Array>, etc.
  • Any type implementing serde::Deserialize

Return types:

  • Primitives and String
  • Result<T, E> (Err throws exception)
  • Any type implementing serde::Serialize

Attributes:

  • state = Rc<T> - Extract state from context slot
  • promise - Return a JS Promise
  • name = "jsName" - Custom JS function name

Running Tests

cargo test
Commit count: 7

cargo fmt