Skip to content
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ emsdk-cache
.output
packages/internal-tsconfig/*.d.*ts
packages/internal-tsconfig/*.*js
vendor
vendor
CLAUDE.md
96 changes: 52 additions & 44 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -1,45 +1,53 @@
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**",
"/opt/homebrew/Cellar/emscripten/3.1.7/libexec/system/include/**",
"/opt/homebrew/Cellar/emscripten/3.1.7/libexec/cache/sysroot/include/**",
"/opt/homebrew/Cellar/emscripten/3.1.7/libexec/llvm/lib/clang/15.0.0/include/**",
"/opt/homebrew/Cellar/emscripten/3.1.7/libexec/cache/sysroot/include/SDL",
"/opt/homebrew/Cellar/emscripten/3.1.7/libexec/cache/sysroot/include/compat",
"/opt/homebrew/Cellar/emscripten/3.1.7/libexec/llvm/lib/clang/15.0.0/include",
"/opt/homebrew/Cellar/emscripten/3.1.7/libexec/cache/sysroot/include"
],
"defines": ["QTS_ASYNCIFY=1", "QTS_DEBUG_MODE=1", "__EMSCRIPTEN__=1"],
"macFrameworkPath": [
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c17",
"cppStandard": "c++98",
"intelliSenseMode": "macos-clang-arm64",
"compilerArgs": [],
"mergeConfigurations": false,
"browse": {
"path": ["${workspaceFolder}/**"],
"limitSymbolsToIncludedHeaders": true
},
"configurationProvider": "ms-vscode.makefile-tools"
},
{
"name": "Emscripten",
"includePath": ["${workspaceFolder}/**"],
"defines": [],
"macFrameworkPath": [
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/opt/homebrew/bin/emcc",
"cStandard": "c17",
"cppStandard": "c++98",
"intelliSenseMode": "macos-clang-arm64"
}
],
"version": 4
}
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**",
"/opt/homebrew/Cellar/emscripten/5.0.1/libexec/system/include/**",
"/opt/homebrew/Cellar/emscripten/5.0.1/libexec/cache/sysroot/include/**",
"/opt/homebrew/Cellar/emscripten/5.0.1/libexec/llvm/lib/clang/23/include/**",
"/opt/homebrew/Cellar/emscripten/5.0.1/libexec/cache/sysroot/include/SDL",
"/opt/homebrew/Cellar/emscripten/5.0.1/libexec/cache/sysroot/include/compat",
"/opt/homebrew/Cellar/emscripten/5.0.1/libexec/llvm/lib/clang/23/include",
"/opt/homebrew/Cellar/emscripten/5.0.1/libexec/cache/sysroot/include"
],
"defines": [
"QTS_ASYNCIFY=1",
"QTS_DEBUG_MODE=1",
"__EMSCRIPTEN__=1"
],
"macFrameworkPath": [
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c17",
"cppStandard": "c++98",
"intelliSenseMode": "macos-clang-arm64",
"compilerArgs": [],
"mergeConfigurations": false,
"browse": {
"path": [
"${workspaceFolder}/**"
],
"limitSymbolsToIncludedHeaders": true
},
"configurationProvider": "ms-vscode.makefile-tools"
},
{
"name": "Emscripten",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"macFrameworkPath": [
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/opt/homebrew/bin/emcc",
"cStandard": "c17",
"cppStandard": "c++98",
"intelliSenseMode": "macos-clang-arm64"
}
],
"version": 4
}
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
# Changelog

## v0.32.0

- [#227](https://github.com/justjake/quickjs-emscripten/pull/227)

- Re-works function binding in `newFunction` to use a different proxying strategy based on a new abstraction, `HostRef`. HostRef allows tracking references of host values from guest handles. Once all references to the HostRef object are disposed (either in the host, or GC'd in the guest), the host value will be dereferenced and become garbage collectable.
- Functions passed to `newFunction` are interned in the runtime's `HostRefMap` until dereferenced.
- `HostRef` is also exposed directly for use from `QuickJSContext`, see `newHostRef`, `toHostRef`, `unwrapHostRef` in the docs.
- Added `QuickJSContext.newConstructorFunction`, such functions will have their `this` set to the newly constructed object, although they may also return a new object.

- [#244](https://github.com/justjake/quickjs-emscripten/pull/244) Upgrade [quickjs-ng](https://github.com/quickjs-ng/quickjs) to [v0.12.1](https://github.com/quickjs-ng/quickjs/releases/tag/v0.12.1).
- [#243](https://github.com/justjake/quickjs-emscripten/pull/243) (thanks to @josh-) Upgrade [bellard/quickjs](https://github.com/bellard/quickjs) to [2025-09-13+f1139494](https://github.com/bellard/quickjs/commit/f1139494d18a2053630c5ed3384a42bb70db3c53). Review the [QuickJS changelog](https://github.com/bellard/quickjs/blob/f1139494d18a2053630c5ed3384a42bb70db3c53/Changelog) for details.
- Some intrinsics like `BigInt` are now always enabled.
- BigNum extension removed.
- [#242](https://github.com/justjake/quickjs-emscripten/pull/242) Upgrade to Emscripten 5.0.1.
- [#221](https://github.com/justjake/quickjs-emscripten/pull/221) (thanks to @melbourne2991) Support generator return values in QuickJSIterator.

## v0.31.0

- [#137](https://github.com/justjake/quickjs-emscripten/pull/137)
Expand Down
84 changes: 84 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,87 @@ Other test filters:
- `scripts/generate.ts` - Generates FFI bindings and symbols
- `templates/Variant.mk` - Makefile template for variants
- `c/interface.c` - C interface to QuickJS exposed to JavaScript

## QuickJS C API Tips

### Value Ownership

QuickJS has strict ownership semantics. Functions either "consume" (take ownership of) or "borrow" values:

**Functions that CONSUME values (caller must NOT free afterward):**

- `JS_DefinePropertyValue` - consumes `val`
- `JS_DefinePropertyValueStr` - consumes `val`
- `JS_DefinePropertyValueUint32` - consumes `val`
- `JS_SetPropertyValue` - consumes `val`
- `JS_SetPropertyStr` - consumes `val`
- `JS_Throw` - consumes the error value

**Functions that DUP values internally (caller SHOULD free afterward):**

- `JS_NewCFunctionData` - calls `JS_DupValue` on data values, so free your reference after
- `JS_SetProperty` - dups the value

**Common double-free bug pattern:**

```c
// WRONG - double free!
JSValue val = JS_NewString(ctx, "hello");
JS_DefinePropertyValueStr(ctx, obj, "name", val, JS_PROP_CONFIGURABLE);
JS_FreeValue(ctx, val); // BUG: val was already consumed!

// CORRECT
JSValue val = JS_NewString(ctx, "hello");
JS_DefinePropertyValueStr(ctx, obj, "name", val, JS_PROP_CONFIGURABLE);
// No JS_FreeValue needed - value is consumed
```

### quickjs vs quickjs-ng Differences

Some functions have different signatures between bellard/quickjs and quickjs-ng:

```c
// bellard/quickjs - class ID is global
JS_NewClassID(&class_id);

// quickjs-ng - class ID is per-runtime
JS_NewClassID(rt, &class_id);
```

Use `#ifdef QTS_USE_QUICKJS_NG` for compatibility:

```c
#ifdef QTS_USE_QUICKJS_NG
JS_NewClassID(rt, &class_id);
#else
JS_NewClassID(&class_id);
#endif
```

### Class Registration

- `JS_NewClassID` allocates a new class ID (only call once globally or per-runtime for ng)
- `JS_NewClass` registers the class definition with a runtime
- `JS_IsRegisteredClass` checks if a class is already registered with a runtime
- Class prototypes default to `JS_NULL` for new classes - set with `JS_SetClassProto` if needed

### Disposal Order

When disposing resources, order matters for finalizers:

```typescript
// CORRECT: Free runtime first so GC finalizers can call back to JS
const rt = new Lifetime(ffi.QTS_NewRuntime(), undefined, (rt_ptr) => {
ffi.QTS_FreeRuntime(rt_ptr); // 1. Free runtime - runs GC finalizers
callbacks.deleteRuntime(rt_ptr); // 2. Then delete callbacks
});
```

### GC and Prevent Corruption Assertions

If you see assertions like:
- `Assertion failed: i != 0, at: quickjs.c, JS_FreeAtomStruct` - atom hash corruption (often double-free)
- `Assertion failed: list_empty(&rt->gc_obj_list)` - objects leaked
- `Assertion failed: p->gc_obj_type == JS_GC_OBJ_TYPE_JS_OBJECT` - memory corruption

These usually indicate memory management bugs: double-frees, use-after-free, or missing frees.
Loading