Skip to content

Commit

Permalink
Extended Symbol support (justjake#105)
Browse files Browse the repository at this point in the history
* Symbol utilities

* rebuild

* update CHANGLOG.md

* rebuild docs
  • Loading branch information
justjake authored and menduz committed May 4, 2023
1 parent 7cfd599 commit fcfe5a8
Show file tree
Hide file tree
Showing 26 changed files with 558 additions and 112 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

## v0.22.0 (unreleased)

- [#78](https://github.com/justjake/quickjs-emscripten/pull/78), [#105](https://github.com/justjake/quickjs-emscripten/pull/105) (thanks to @ayaboy) add Symbol helpers `context.newUniqueSymbol`, `context.newSymbolFor`, as well as support for symbols in `context.dump`.
- [#104](https://github.com/justjake/quickjs-emscripten/pull/104) BigInt support.

- [#100](https://github.com/justjake/quickjs-emscripten/pull/100) **Breaking change** upgrade Emscripten version and switch to `async import(...)` for loading variants.

We also drop support for older browsers and Node versions:

- Node >= 16 is required
Expand Down
63 changes: 63 additions & 0 deletions c/interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,69 @@ JSBorrowedChar *QTS_GetString(JSContext *ctx, JSValueConst *value) {
return JS_ToCString(ctx, *value);
}

JSValue qts_get_symbol_key(JSContext *ctx, JSValueConst *value) {
JSValue global = JS_GetGlobalObject(ctx);
JSValue Symbol = JS_GetPropertyStr(ctx, global, "Symbol");
JS_FreeValue(ctx, global);

JSValue Symbol_keyFor = JS_GetPropertyStr(ctx, Symbol, "keyFor");
JSValue key = JS_Call(ctx, Symbol_keyFor, Symbol, 1, value);
JS_FreeValue(ctx, Symbol_keyFor);
JS_FreeValue(ctx, Symbol);
return key;
}

JSValue *QTS_NewSymbol(JSContext *ctx, BorrowedHeapChar *description, int isGlobal) {
JSValue global = JS_GetGlobalObject(ctx);
JSValue Symbol = JS_GetPropertyStr(ctx, global, "Symbol");
JS_FreeValue(ctx, global);
JSValue descriptionValue = JS_NewString(ctx, description);
JSValue symbol;

if (isGlobal != 0) {
JSValue Symbol_for = JS_GetPropertyStr(ctx, Symbol, "for");
symbol = JS_Call(ctx, Symbol_for, Symbol, 1, &descriptionValue);
JS_FreeValue(ctx, descriptionValue);
JS_FreeValue(ctx, Symbol_for);
JS_FreeValue(ctx, Symbol);
return jsvalue_to_heap(symbol);
}

symbol = JS_Call(ctx, Symbol, JS_UNDEFINED, 1, &descriptionValue);
JS_FreeValue(ctx, descriptionValue);
JS_FreeValue(ctx, Symbol);

return jsvalue_to_heap(symbol);
}

MaybeAsync(JSBorrowedChar *) QTS_GetSymbolDescriptionOrKey(JSContext *ctx, JSValueConst *value) {
JSBorrowedChar *result;

JSValue key = qts_get_symbol_key(ctx, value);
if (!JS_IsUndefined(key)) {
result = JS_ToCString(ctx, key);
JS_FreeValue(ctx, key);
return result;
}

JSValue description = JS_GetPropertyStr(ctx, *value, "description");
result = JS_ToCString(ctx, description);
JS_FreeValue(ctx, description);
return result;
}

int QTS_IsGlobalSymbol(JSContext *ctx, JSValueConst *value) {
JSValue key = qts_get_symbol_key(ctx, value);
int undefined = JS_IsUndefined(key);
JS_FreeValue(ctx, key);

if (undefined) {
return 0;
} else {
return 1;
}
}

int QTS_IsJobPending(JSRuntime *rt) {
return JS_IsJobPending(rt);
}
Expand Down
10 changes: 6 additions & 4 deletions doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -569,8 +569,11 @@ the Javascript parts of the library without setting up the Emscripten toolchain.

Intermediate object files from QuickJS end up in ./build/quickjs/.

This project uses `emscripten 3.1.7` via Docker. You will need a working `docker`
install to build the Emscripten artifacts.
This project uses `emscripten 3.1.32`.

- On ARM64, you should install `emscripten` on your machine. For example on macOS, `brew install emscripten`.
- If _the correct version of emcc_ is not in your PATH, compilation falls back to using Docker.
On ARM64, this is 10-50x slower than native compilation, but it's just fine on x64.

Related NPM scripts:

Expand All @@ -585,8 +588,7 @@ Related NPM scripts:
The ./ts directory contains Typescript types and wraps the generated Emscripten
FFI in a more usable interface.

You'll need `node` and `npm` or `yarn`. Install dependencies with `npm install`
or `yarn install`.
You'll need `node` and `yarn`. Install dependencies with `yarn install`.

- `yarn build` produces ./dist.
- `yarn test` runs the tests.
Expand Down
Loading

0 comments on commit fcfe5a8

Please sign in to comment.