Skip to content

Add atob, btoa, structuredClone, queueMicrotask and performance #16

Open

Description

@bnoordhuis

Continuing from saghul/txiki.js#418:

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.

1 TBD how to handle transferables

Activity

  1. added
    enhancement
    on Nov 5, 2023
  2. saghul commented on Nov 5, 2023

    @saghul
    Contributor

    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?

  3. M0n7y5 commented on Nov 5, 2023

    @M0n7y5

    Perhaps you can fetch counter from processor? Maybe RDTSC on X86 would help? That would be compiler specific which is not that bad imho.

  4. M0n7y5 commented on Nov 5, 2023

    @M0n7y5

    Nwm it needs TSC which is platform specific. That means it belongs to std runtime implementation.

  5. bnoordhuis commented on Nov 5, 2023

    @bnoordhuis
    ContributorAuthor

    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.

  6. saghul commented on Nov 5, 2023

    @saghul
    Contributor

    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 !

  7. bnoordhuis commented on Nov 6, 2023

    @bnoordhuis
    ContributorAuthor

    Even macOS has clock_gettime as of 10.12 so I don't think there are any blockers.

  8. saghul commented on Nov 6, 2023

    @saghul
    Contributor

    Awesome!

  9. M0n7y5 commented on Nov 6, 2023

    @M0n7y5

    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.

  10. saghul commented on Nov 6, 2023

    @saghul
    Contributor

    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.

  11. bnoordhuis commented on Nov 6, 2023

    @bnoordhuis
    ContributorAuthor

    clock_gettime is not everywhere

    Where is it missing? Linux, macOS, (Free|Net|Open|Dragonfly)BSD, Solaris, Fuchsia, QNX and Emscripten all have it.

  12. saghul commented on Mar 7, 2024

    @saghul
    Contributor

    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...

  13. graham commented on Apr 20, 2025

    @graham

    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.

  14. saghul commented on Apr 20, 2025

    @saghul
    Contributor

    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.

  15. added a commit that references this issue on Apr 21, 2025
  16. graham commented on Apr 21, 2025

    @graham

    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)

  17. flawrist commented on Nov 30, 2025

    @flawrist

    Guys, I have QuickJS version 2025-04-26 and try to execute it on Windows 7 as follows

    $ qjs script.txt
    ReferenceError: 'atob' is not defined
        at <eval> (script.txt:9:13)

    However it works with node and deno instead of qjs, and I see the output.

    Does it mean QuickJS is missing atob() function? :(

  18. bnoordhuis commented on Dec 1, 2025

    @bnoordhuis
    ContributorAuthor

    Yes, but see #1143.

  19. flawrist commented on Dec 1, 2025

    @flawrist

    @bnoordhuis, thank you for pointing out the encouraging work of @aabbdev
    I hope it will work on Windows 7.

  20. aabbdev commented on Dec 1, 2025

    @aabbdev

    @sergeevabc When I have time; to be merged I need to pull the master the implementation is ready

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions