Add C-API helpers for embedders: property accessors, exception, promise#1475
Open
andreasrosdal wants to merge 1 commit into
Open
Conversation
…e convenience)
Adds header-only inline helpers to quickjs.h, motivated by patterns
repeated throughout the Nordstjernen browser's QuickJS binding:
JS_FreeCStringSafe no-op on a NULL pointer
JS_GetPropertyStrInt32 fetch+convert+free in one call
JS_GetPropertyStrInt64 "
JS_GetPropertyStrFloat64 "
JS_GetPropertyStrBool "
JS_GetExceptionAsString consume pending exception, format as
"<msg>" or "<msg>\n<stack>"
JS_ResolvePromise settle a JS_NewPromiseCapability pair
JS_RejectPromise "
Each helper consolidates a multi-line dance (get + null/undefined
check + convert + handle conversion error + free intermediate) into
one expression, and silently clears any pending exception raised by
the conversion so callers don't accidentally propagate it.
All additions are static inline; no ABI change, no quickjs.c changes.
api-test.c gains coverage for the new helpers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds eight header-only inline helpers to
quickjs.hthat collapsepatterns repeated dozens of times in real embedders. Motivated by an
audit of the Nordstjernen browser's QuickJS binding (
src/js.c,~10k lines), where the same five-to-twelve-line dances appear at
hundreds of call sites.
I am making a web browser here:
https://github.com/nordstjernen-web/nordstjernen
This PR was created using Claude.
New helpers:
JS_FreeCStringSafeif (s) JS_FreeCString(ctx, s);JS_GetPropertyStrInt32JS_ToInt32+ freeJS_GetPropertyStrInt64JS_GetPropertyStrFloat64JS_GetPropertyStrBoolJS_ToBool+ freeJS_GetExceptionAsStringJS_GetException+JS_ToCString+"stack"fetch + formatJS_ResolvePromiseJS_RejectPromiseEach helper:
JSValue.conversion (so embedders don't accidentally propagate a coercion
error from a getter).
null/undefinedproperties.
Why
JS_GetException+JS_ToCString+JS_GetPropertyStr("stack")isthe single most-repeated pattern in the Nordstjernen binding —
appearing in every timer, every microtask drain, every event
dispatch, every fetch callback. It's also where refcount bugs hurt
most, because it runs on the error path. Folding it into one call
makes that path both shorter and safer.
The typed property accessors target options-bag reads (timer
options,
fetch()init, event init, IntersectionObserver options,etc.), which embedders implement by hand today.
Compatibility
static inlineinquickjs.h.quickjs.c.Out of scope (follow-ups identified in the audit)
JS_SetContextDeadline).JS_ObjectForEachfor own-property iteration.into C.
Each touches more of the engine and warrants a separate PR.