Skip to content

Commit 1bbefd7

Browse files
authored
Extended Symbol support (justjake#105)
* Symbol utilities * rebuild * update CHANGLOG.md * rebuild docs
1 parent f562def commit 1bbefd7

28 files changed

+558
-112
lines changed

CHANGELOG.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
## v0.22.0 (unreleased)
44

5+
- [#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`.
56
- [#104](https://github.com/justjake/quickjs-emscripten/pull/104) BigInt support.
6-
77
- [#100](https://github.com/justjake/quickjs-emscripten/pull/100) **Breaking change** upgrade Emscripten version and switch to `async import(...)` for loading variants.
8-
98
We also drop support for older browsers and Node versions:
109

1110
- Node >= 16 is required

c/interface.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,69 @@ JSBorrowedChar *QTS_GetString(JSContext *ctx, JSValueConst *value) {
319319
return JS_ToCString(ctx, *value);
320320
}
321321

322+
JSValue qts_get_symbol_key(JSContext *ctx, JSValueConst *value) {
323+
JSValue global = JS_GetGlobalObject(ctx);
324+
JSValue Symbol = JS_GetPropertyStr(ctx, global, "Symbol");
325+
JS_FreeValue(ctx, global);
326+
327+
JSValue Symbol_keyFor = JS_GetPropertyStr(ctx, Symbol, "keyFor");
328+
JSValue key = JS_Call(ctx, Symbol_keyFor, Symbol, 1, value);
329+
JS_FreeValue(ctx, Symbol_keyFor);
330+
JS_FreeValue(ctx, Symbol);
331+
return key;
332+
}
333+
334+
JSValue *QTS_NewSymbol(JSContext *ctx, BorrowedHeapChar *description, int isGlobal) {
335+
JSValue global = JS_GetGlobalObject(ctx);
336+
JSValue Symbol = JS_GetPropertyStr(ctx, global, "Symbol");
337+
JS_FreeValue(ctx, global);
338+
JSValue descriptionValue = JS_NewString(ctx, description);
339+
JSValue symbol;
340+
341+
if (isGlobal != 0) {
342+
JSValue Symbol_for = JS_GetPropertyStr(ctx, Symbol, "for");
343+
symbol = JS_Call(ctx, Symbol_for, Symbol, 1, &descriptionValue);
344+
JS_FreeValue(ctx, descriptionValue);
345+
JS_FreeValue(ctx, Symbol_for);
346+
JS_FreeValue(ctx, Symbol);
347+
return jsvalue_to_heap(symbol);
348+
}
349+
350+
symbol = JS_Call(ctx, Symbol, JS_UNDEFINED, 1, &descriptionValue);
351+
JS_FreeValue(ctx, descriptionValue);
352+
JS_FreeValue(ctx, Symbol);
353+
354+
return jsvalue_to_heap(symbol);
355+
}
356+
357+
MaybeAsync(JSBorrowedChar *) QTS_GetSymbolDescriptionOrKey(JSContext *ctx, JSValueConst *value) {
358+
JSBorrowedChar *result;
359+
360+
JSValue key = qts_get_symbol_key(ctx, value);
361+
if (!JS_IsUndefined(key)) {
362+
result = JS_ToCString(ctx, key);
363+
JS_FreeValue(ctx, key);
364+
return result;
365+
}
366+
367+
JSValue description = JS_GetPropertyStr(ctx, *value, "description");
368+
result = JS_ToCString(ctx, description);
369+
JS_FreeValue(ctx, description);
370+
return result;
371+
}
372+
373+
int QTS_IsGlobalSymbol(JSContext *ctx, JSValueConst *value) {
374+
JSValue key = qts_get_symbol_key(ctx, value);
375+
int undefined = JS_IsUndefined(key);
376+
JS_FreeValue(ctx, key);
377+
378+
if (undefined) {
379+
return 0;
380+
} else {
381+
return 1;
382+
}
383+
}
384+
322385
int QTS_IsJobPending(JSRuntime *rt) {
323386
return JS_IsJobPending(rt);
324387
}

doc/README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -569,8 +569,11 @@ the Javascript parts of the library without setting up the Emscripten toolchain.
569569

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

572-
This project uses `emscripten 3.1.7` via Docker. You will need a working `docker`
573-
install to build the Emscripten artifacts.
572+
This project uses `emscripten 3.1.32`.
573+
574+
- On ARM64, you should install `emscripten` on your machine. For example on macOS, `brew install emscripten`.
575+
- If _the correct version of emcc_ is not in your PATH, compilation falls back to using Docker.
576+
On ARM64, this is 10-50x slower than native compilation, but it's just fine on x64.
574577

575578
Related NPM scripts:
576579

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

588-
You'll need `node` and `npm` or `yarn`. Install dependencies with `npm install`
589-
or `yarn install`.
591+
You'll need `node` and `yarn`. Install dependencies with `yarn install`.
590592

591593
- `yarn build` produces ./dist.
592594
- `yarn test` runs the tests.

0 commit comments

Comments
 (0)