You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add atob, btoa, structuredClone, queueMicrotask and performance #16
To avoid duplication across embedders, implement atob, btoa, structuredClone1, queueMicrotask and performance as quickjs-ng C APIs and/or JS globals. We also have to add DOMException but that seems okay to me. We can make them opt-in when the JSContext is created.
Node.js uses uv_hrtime() and that's basically a wrapper around clock_gettime(CLOCK_MONOTONIC) or QueryPerformanceCounter(). Seems okay to me to use that here as well.
Yeah but clock_gettime is not everywhere just like QueryPerformanceCounter(). I suggest to not make performance or any other platform specific things to be part of the core library. Sort of extension to standard lib is more useful. With proper defines which allows you to opt-out of these things if needed.
Why not? Implementing Atomics already requires some if-defing around, and if all we need is to support 2 implementations of performance.now, that's a drop in a bucket compared to the size of quickjs.c.
Brain dump, since I've been thinking about these a bit the past week.
Both atob/btoa and structuredClone depend on DOMException for error reporting, and if we are to do it we might as well make it compliant, so we should have DOMException I guess.
I noticed no other engine provides structuredClone, it seems to always be implemented by the embedder for some reason. If we can do it, then why not? (IMHO).
It uses come c++ stdlib and an external library for printing, but I'll remove all of that before a create a pr.
I'm a little confused about how I should go about testing this? I could write something in api-test.c, or I could simply add the atob/btoa to the globalThis and write a javascript test in /tests/
I don't know enough about how the arraybuffer work is going to know if this is helpful or a distraction.
Actual Question: Assuming I'm going to make a pr to add atob and btoa; where would you like me to implement the tests to ensure it works correctly?
Alternatively, if this is already in the works I can find another issue to focus on.
I'd add them to the global object by default, perhaps with the option to not do that with some helper JS_AddIntrinsicAtob for those who want to use the raw context.
This way tests can be written in JS.
Error handling is something we need to solve though. In the spec they can throw DOMException, so we likely need to implement that first or as part of this work.
Activity
saghul commentedon Nov 5, 2023
Doesn't the performance.now put us into a position where we need to start branching out to platform specific code?
Is there a minimal implementation we could support and then decide to use it at context creation time with flags perhaps?
M0n7y5 commentedon Nov 5, 2023
Perhaps you can fetch counter from processor? Maybe RDTSC on X86 would help? That would be compiler specific which is not that bad imho.
M0n7y5 commentedon Nov 5, 2023
Nwm it needs TSC which is platform specific. That means it belongs to std runtime implementation.
bnoordhuis commentedon Nov 5, 2023
Node.js uses uv_hrtime() and that's basically a wrapper around clock_gettime(CLOCK_MONOTONIC) or QueryPerformanceCounter(). Seems okay to me to use that here as well.
saghul commentedon Nov 5, 2023
Yep I had that in mind too. Does also work on macOS and BSDs? I haven't checked the implementation in a while :-)
If it's simple let's do it !
bnoordhuis commentedon Nov 6, 2023
Even macOS has clock_gettime as of 10.12 so I don't think there are any blockers.
saghul commentedon Nov 6, 2023
Awesome!
M0n7y5 commentedon Nov 6, 2023
Yeah but clock_gettime is not everywhere just like QueryPerformanceCounter(). I suggest to not make performance or any other platform specific things to be part of the core library. Sort of extension to standard lib is more useful. With proper defines which allows you to opt-out of these things if needed.
saghul commentedon Nov 6, 2023
Why not? Implementing
Atomicsalready requires some if-defing around, and if all we need is to support 2 implementations ofperformance.now, that's a drop in a bucket compared to the size of quickjs.c.bnoordhuis commentedon Nov 6, 2023
Where is it missing? Linux, macOS, (Free|Net|Open|Dragonfly)BSD, Solaris, Fuchsia, QNX and Emscripten all have it.
Add performance.{now,timeOrigin}
Add performance.{now,timeOrigin}
Add queueMicrotask
Add queueMicrotask
Add queueMicrotask
saghul commentedon Mar 7, 2024
Brain dump, since I've been thinking about these a bit the past week.
Both atob/btoa and structuredClone depend on DOMException for error reporting, and if we are to do it we might as well make it compliant, so we should have DOMException I guess.
I noticed no other engine provides structuredClone, it seems to always be implemented by the embedder for some reason. If we can do it, then why not? (IMHO).
Last, atob/btoa require a base64 encoder/decoder, which we will also need to add for https://github.com/tc39/proposal-arraybuffer-base64 so I guess it ties it up nicely...
Add performance.{now,timeOrigin}
Add queueMicrotask
graham commentedon Apr 20, 2025
I took a shot at implementing atob/btoa here: https://gist.github.com/graham/0f6f0f7de6c2839155c7f36ee7625588
It uses come c++ stdlib and an external library for printing, but I'll remove all of that before a create a pr.
I'm a little confused about how I should go about testing this? I could write something in
api-test.c, or I could simply add the atob/btoa to the globalThis and write a javascript test in/tests/I don't know enough about how the arraybuffer work is going to know if this is helpful or a distraction.
Actual Question: Assuming I'm going to make a pr to add atob and btoa; where would you like me to implement the tests to ensure it works correctly?
Alternatively, if this is already in the works I can find another issue to focus on.
saghul commentedon Apr 20, 2025
Looks good!
I'd add them to the global object by default, perhaps with the option to not do that with some helper JS_AddIntrinsicAtob for those who want to use the raw context.
This way tests can be written in JS.
Error handling is something we need to solve though. In the spec they can throw DOMException, so we likely need to implement that first or as part of this work.
Issue quickjs-ng#16 references not having atob and btoa (enc/dec base64)
graham commentedon Apr 21, 2025
I took another shot at this: #1032
Removed all the c++ and tried to make it as much like the other modules as I could. My initial implementation is pretty basic, after completing it I found: https://stackoverflow.com/questions/342409/ as well as a couple other articles about how to make this more performant if there is interest.
This is my first contribution so feel free to be extra picky if I missed a style or organization detail. (any feedback is welcome)
flawrist commentedon Nov 30, 2025
Guys, I have QuickJS version 2025-04-26 and try to execute it on Windows 7 as follows
However it works with
nodeanddenoinstead ofqjs, and I see the output.Does it mean QuickJS is missing
atob()function? :(bnoordhuis commentedon Dec 1, 2025
Yes, but see #1143.
flawrist commentedon Dec 1, 2025
@bnoordhuis, thank you for pointing out the encouraging work of @aabbdev
I hope it will work on Windows 7.
aabbdev commentedon Dec 1, 2025
@sergeevabc When I have time; to be merged I need to pull the master the implementation is ready