Skip to content

Issue #16 references not having atob and btoa (enc/dec base64) #1032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

graham
Copy link

@graham graham commented Apr 21, 2025

I've started with naive implementation, there is significant discussion about how to make this faster:

https://stackoverflow.com/questions/342409/

If there is interest I rewrite with some of these tricks.

I've started with naive implementation, there is significant
discussion about how to make this faster:

https://stackoverflow.com/questions/342409/

If there is interest I rewrite with some of these tricks.
Copy link
Contributor

@bnoordhuis bnoordhuis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some comments. General style note: our code is usually pretty dense with not too many blank lines or comments. If it's clear from context what a line of code is doing, it doesn't need a comment.

// Calculate output length (including padding)
size_t out_len = 4 * ((len + 2) / 3) + 1;
// Allocate memory for output string (plus null terminator)
char* out = (char*)js_mallocz(ctx, out_len);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary cast but more substantially, allocating should preferably be done by the caller, because then the caller can use e.g. a stack-allocated buffer for small inputs.

size_t i, j;

for (i = 0, j = 0; i < len; i += 3, j += 4) {
uint32_t triple = (bin[i] << 16);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
uint32_t triple = (bin[i] << 16);
uint32_t triple = (uint8_t)bin[i] << 16;

Otherwise if char is signed and bin[i] == -42 (for example), you get unexpected output; plus, shifting into the sign bit is ill-defined behavior in C. If you were to shift by 24 bits, you'd have to write it as (uint32_t)(uint8_t)bin[i] << 16 (or change bin to uint8_t *)

JSValue v;

if (argc > 0) {
fmt_str = JS_ToCString(ctx, argv[0]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fmt_str = JS_ToCString(ctx, argv[0]);
fmt_str = JS_ToCString(ctx, argv[0]);
if (!fmt_str)
return JS_EXCEPTION;

@bptato
Copy link
Contributor

bptato commented Apr 22, 2025

Could you please adjust this to comply with the standard? At first glance it's missing at least the "remove ASCII whitespace" step.
As for DOMException, I have a half-finished version for NG lying around somewhere. Will try to get it into a submittable state soon.

Also, atob/btoa should at least be static to avoid namespace pollution. Or a public API, but then not with these names.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants