Skip to content
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

feat: allow direct base64 call against HandleValue #69

Merged
merged 2 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions builtins/web/base64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace builtins {
namespace web {
namespace base64 {

JS::Result<std::string> convertJSValueToByteString(JSContext *cx, JS::Handle<JS::Value> v) {
JS::Result<std::string> valueToJSByteString(JSContext *cx, JS::Handle<JS::Value> v) {
JS::RootedString s(cx);
if (v.isString()) {
s = v.toString();
Expand Down Expand Up @@ -54,10 +54,10 @@ JS::Result<std::string> convertJSValueToByteString(JSContext *cx, JS::Handle<JS:
return byteString;
}

JS::Result<std::string> convertJSValueToByteString(JSContext *cx, std::string v) {
JS::Result<std::string> stringToJSByteString(JSContext *cx, std::string v) {
JS::RootedValue s(cx);
s.setString(JS_NewStringCopyN(cx, v.c_str(), v.length()));
return convertJSValueToByteString(cx, s);
return valueToJSByteString(cx, s);
}

// Maps an encoded character to a value in the Base64 alphabet, per
Expand Down Expand Up @@ -295,7 +295,7 @@ bool atob(JSContext *cx, unsigned argc, Value *vp) {
if (!args.requireAtLeast(cx, "atob", 1)) {
return false;
}
auto dataResult = convertJSValueToByteString(cx, args.get(0));
auto dataResult = valueToJSByteString(cx, args.get(0));
if (dataResult.isErr()) {
return false;
}
Expand Down Expand Up @@ -405,7 +405,7 @@ bool btoa(JSContext *cx, unsigned argc, Value *vp) {
// Note: We do not check if data contains any character whose code point is
// greater than U+00FF before calling convertJSValueToByteString as
// convertJSValueToByteString does the same check
auto byteStringResult = convertJSValueToByteString(cx, data);
auto byteStringResult = valueToJSByteString(cx, data);
if (byteStringResult.isErr()) {
return false;
}
Expand Down
3 changes: 2 additions & 1 deletion builtins/web/base64.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ extern const char base64URLEncodeTable[65];
std::string forgivingBase64Encode(std::string_view data, const char *encodeTable);
JS::Result<std::string> forgivingBase64Decode(std::string_view data, const uint8_t *decodeTable);

JS::Result<std::string> convertJSValueToByteString(JSContext *cx, std::string v);
JS::Result<std::string> valueToJSByteString(JSContext *cx, HandleValue v);
JS::Result<std::string> stringToJSByteString(JSContext *cx, std::string v);

} // namespace base64
} // namespace web
Expand Down
4 changes: 2 additions & 2 deletions builtins/web/crypto/crypto-algorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ std::unique_ptr<CryptoKeyRSAComponents> createRSAPublicKeyFromJWK(JSContext *cx,
if (modulus.starts_with('0')) {
modulus = modulus.erase(0, 1);
}
auto dataResult = base64::convertJSValueToByteString(cx, jwk->e.value());
auto dataResult = base64::stringToJSByteString(cx, jwk->e.value());
if (dataResult.isErr()) {
DOMException::raise(cx, "Data provided to an operation does not meet requirements",
"DataError");
Expand Down Expand Up @@ -258,7 +258,7 @@ std::unique_ptr<CryptoKeyRSAComponents> createRSAPrivateKeyFromJWK(JSContext *cx
if (modulus.starts_with('0')) {
modulus = modulus.erase(0, 1);
}
auto dataResult = base64::convertJSValueToByteString(cx, jwk->e.value());
auto dataResult = base64::stringToJSByteString(cx, jwk->e.value());
if (dataResult.isErr()) {
DOMException::raise(cx, "Data provided to an operation does not meet requirements",
"DataError");
Expand Down
Loading