implementing-jsc-classes-rust

Create JavaScript classes backed by Rust using Bun's bindings generator.

95.8k|5.0k|Updated Apr 14, 2021
One-click install
npx skills add https://github.com/oven-sh/bun --skill implementing-jsc-classes-rust
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: implementing-jsc-classes-rust
Source: https://github.com/oven-sh/bun/tree/main/.claude/skills/implementing-jsc-classes-rust
Command: npx skills add https://github.com/oven-sh/bun --skill implementing-jsc-classes-rust

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Implementing new JavaScript APIs in Bun's Rust runtime requires bridging JavaScriptCore with native code, which involves repetitive boilerplate for constructors, prototypes, getters, and finalizers. This Skill provides the canonical patterns and signatures for using Bun's .classes.ts bindings generator and #[bun_jsc::JsClass] proc-macro.

Core Features & Use Cases

  • Class Definition via .classes.ts: Declare constructors, prototype methods, getters, finalize hooks, and pending activity flags in a TypeScript schema.
  • Rust Implementation Patterns: Apply canonical method signatures for constructor, host_fn methods, getters, and has_pending_activity that the codegen expects.
  • JS Value Handling: Store JavaScript values safely through WriteBarrier slots using *_set_cached and *_get_cached accessors instead of raw JSValue fields.
  • Use Case: When adding a new built-in module like Glob or Cron to Bun, define the JS surface in a .classes.ts file, implement the Rust struct with #[bun_jsc::JsClass], and run bun bd to regenerate the C++ and Rust bindings.

Quick Start

Create a new .classes.ts file defining the class schema, add the corresponding Rust struct annotated with #[bun_jsc::JsClass], and run bun bd to regenerate the bindings.

Frequently Asked Questions about implementing-jsc-classes-rust

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I implement a new JavaScript class in Bun's Rust runtime?

Define the class schema in a `.classes.ts` file using `define({ name, construct, proto })`, then implement the Rust struct with `#[bun_jsc::JsClass]` and matching `constructor` and `host_fn` methods. Run `bun bd` to regenerate the C++ and Rust bindings.

What is the difference between `finalize` and `Drop` in Bun JSC classes?

The blanket `JsFinalize` default just drops the `Box<Self>`. Override `finalize(self: Box<Self>)` only when you must release a JS handle or defer cleanup to a heap helper like `bun_ptr::finalize_js_box`. Always implement it as an inherent method, not via `impl JsFinalize`.

How do I store JavaScript values in a Rust struct without leaking memory?

Declare a slot in `.classes.ts` via `values: [...]` or a `cache: true` getter, then read and write through the generated `*_set_cached` and `*_get_cached` accessors. The slot is a `WriteBarrier` visited by the GC, so the value stays alive without a `Strong` reference.

Why does `cargo check -p bun_runtime` fail with missing method errors?

The generated bindings call inherent methods directly, so a missing or mis-typed hook like `constructor`, `host_fn`, or `has_pending_activity` becomes a compile error. Verify the signatures match the canonical table in the Skill documentation.

When should I use `hasPendingActivity` in a Bun JSC class?

Set `hasPendingActivity: true` in the `.classes.ts` schema and implement `pub fn has_pending_activity(&self) -> bool` when async work is in flight. The GC keeps the object alive while the atomic counter is non-zero, preventing premature collection.