Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 62 additions & 32 deletions ffi/boa_ffi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

`boa_ffi` exposes Boa through opaque C handles generated by Diplomat. The current
API supports context lifecycle and value handles with type inspection and
number/null/undefined construction, plus structured error construction and
inspection. It does not yet execute JavaScript. The API is experimental and may change.
boolean/number/string/null/undefined construction and inspection, plus structured
error construction and inspection. It does not yet execute JavaScript. The API is
experimental and may change.

## Build and test

Expand All @@ -26,13 +27,14 @@ host explicitly in `target/<host>/debug/`, honors `CARGO_TARGET_DIR`, and runs
against that build's library; `--ci` reuses Cargo's `ci` profile build. Windows
support is not covered by this runner.

Rust tests cover initialization failure, context cleanup, value types, numeric
round trips, retained error data and inspection without JavaScript execution.
Rust tests cover initialization failure, context cleanup, value types, boolean,
numeric and string round trips (including unpaired UTF-16 surrogates), retained
error data and inspection without JavaScript execution.
Object retention is checked across garbage collection. The C tests exercise
context/value/error lifecycle, numeric edge cases and message encoding through
the generated headers and shared library. They check results explicitly, so
defining `NDEBUG` does not disable the tests. A dedicated CI job runs header
regeneration and the C tests across macOS and Linux.
context/value/error lifecycle, boolean/numeric/string edge cases and message
encoding through the generated headers and shared library. They check results
explicitly, so defining `NDEBUG` does not disable the tests. A dedicated CI job
runs header regeneration and the C tests across macOS and Linux.

## Regenerate headers

Expand Down Expand Up @@ -75,35 +77,63 @@ int main(void) {

## Value handles

`boa_JsValue_from_number(double)`, `boa_JsValue_from_null()` and
`boa_JsValue_from_undefined()` each return one owned, non-null value handle. They
require no context, and these primitive values remain usable after a context
is destroyed. Allocation failures and unexpected Rust panics may abort the process.
Primitive constructors (`boa_JsValue_from_boolean`, `boa_JsValue_from_number`,
`boa_JsValue_from_string`, `boa_JsValue_from_null`, `boa_JsValue_from_undefined`)
require no context and return an owned `JsValue *` handle. All getters borrow a
live, non-null handle without coercing types, and return a tagged result whose
`is_ok` field must be checked before reading `ok` or `err`.

- **Type inspection (`boa_JsValue_get_type`):** Returns the ECMAScript language
type (`JsValueType`). Unlike JS `typeof`, `null` and `undefined` are distinct
and functions have type `Object`. Object identity is preserved.
- **Booleans (`from_boolean` / `as_boolean`):** Only boolean values return
`is_ok == true`; truthy and falsy non-booleans return `is_ok == false`.
- **Numbers (`from_number` / `as_number`):** Only number values return
`is_ok == true`, including `NaN`, infinities, and signed zeros (`NaN` payload
bits are not guaranteed).
- **Strings (`from_string` / `as_string` / `as_string_lossy`):**
- `from_string` copies a UTF-8 `DiplomatStringView` using its explicit byte
length (including any `'\0'` bytes) and returns `NULL` on invalid UTF-8.
Empty strings may pass `{.data = NULL, .len = 0}`.
- `as_string` and `as_string_lossy` encode the string as UTF-8 into a
`DiplomatWrite` buffer (from `diplomat_runtime.h`). On failure, nothing is
written. `as_string` reports `JsStringError_NotAString` or
`JsStringError_InvalidEncoding` (unpaired UTF-16 surrogates);
`as_string_lossy` replaces unpaired surrogates with `U+FFFD` and only fails
on non-strings.
- Growable buffers (`diplomat_buffer_write_create`) are not NUL-terminated
and JS strings may contain `'\0'`, so use `diplomat_buffer_write_len()`.
Fixed stack buffers (`diplomat_simple_write`) append a trailing `'\0'` and
set `write.grow_failed` on overflow.
- **Ownership & lifetime:** Keep handles on their creating thread. Primitive
values remain valid after a `BoaContext` is destroyed. Destroy each owned
handle once with `boa_JsValue_destroy()` (never `NULL` or `free()`).
Allocation failures and unexpected Rust panics may abort the program.

```c
JsValue *value = boa_JsValue_from_number(7.0);
boa_JsValue_as_number_result number = boa_JsValue_as_number(value);
if (number.is_ok) {
printf("%g\n", number.ok);
JsValue *num = boa_JsValue_from_number(7.0);
boa_JsValue_as_number_result n = boa_JsValue_as_number(num);
if (n.is_ok) {
printf("%g\n", n.ok);
}
boa_JsValue_destroy(num);

const char text[] = "hello";
JsValue *str = boa_JsValue_from_string(
(DiplomatStringView){.data = text, .len = sizeof(text) - 1});
if (str != NULL) {
DiplomatWrite *write = diplomat_buffer_write_create(0);
boa_JsValue_as_string_result res = boa_JsValue_as_string(str, write);
if (res.is_ok) {
fwrite(diplomat_buffer_write_get_bytes(write), 1,
diplomat_buffer_write_len(write), stdout);
fputc('\n', stdout);
}
diplomat_buffer_write_destroy(write);
boa_JsValue_destroy(str);
}
boa_JsValue_destroy(value);
```

`boa_JsValue_get_type()` borrows the handle and returns its `JsValueType`. It reports
the ECMAScript language type: null and undefined are distinct, and functions
have the object type. This differs from JavaScript's `typeof` operator.
The underlying handle retains the original Boa value, including object identity.

`boa_JsValue_as_number()` borrows the handle and returns a tagged optional double.
Check `is_ok` before reading `ok`; only number values have a numeric payload.
Null and undefined produce an absent result without coercion. NaN is a present
number, as are infinities and signed zeros. NaN payload bits are not guaranteed.

All value calls, including destruction, must stay on the creating thread. Getters
require a live, non-null value handle and do not consume it. Destroy each owned
handle exactly once with `boa_JsValue_destroy()`, and invalidate every pointer alias
afterward. `boa_JsValue_destroy(NULL)` and C's `free()` are invalid for these handles.

## Errors

`JsError` retains Boa's original error and its stored representation:
Expand Down
23 changes: 23 additions & 0 deletions ffi/boa_ffi/bindings/c/JsStringError.d.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions ffi/boa_ffi/bindings/c/JsStringError.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions ffi/boa_ffi/bindings/c/JsValue.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading