Skip to content

Commit

Permalink
Fix options evaluation order tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiltd committed Nov 4, 2024
1 parent 90d68d4 commit c64f6a1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
56 changes: 28 additions & 28 deletions builtins/web/blob.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "blob.h"
#include "builtin.h"
#include "js/ArrayBuffer.h"
#include "js/BigInt.h"
#include "js/Conversions.h"
#include "js/experimental/TypedData.h"
#include "js/TypeDecls.h"
Expand Down Expand Up @@ -50,16 +49,20 @@ bool validate_type(T* chars, size_t strlen) {
// 1. If type contains any characters outside the range U+0020 to U+007E, then set t to the empty string.
// 2. Convert every character in type to ASCII lowercase.
JSString* normalize_type(JSContext *cx, HandleValue value) {
if (value.isNull()) {
return JS::ToString(cx, value);
}
JS::RootedString value_str(cx);

if (value.isUndefined() || !value.isString()) {
if (value.isObject() || value.isString()) {
value_str = JS::ToString(cx, value);
if (!value_str) {
return nullptr;
}
} else if (value.isNull()) {
return JS::ToString(cx, value);
} else {
return JS_GetEmptyString(cx);
}

JS::RootedString s(cx, value.toString());
auto str = JS::StringToLinearString(cx, s);
auto str = JS::StringToLinearString(cx, value_str);
if (!str) {
return nullptr;
}
Expand Down Expand Up @@ -386,10 +389,10 @@ bool Blob::init_options(JSContext *cx, HandleObject self, HandleValue initv) {
// - `type`: the MIME type of the data that will be stored into the blob,
// - `endings`: how to interpret newline characters (\n) within the contents.
JS::RootedObject opts(cx, init_val.toObjectOrNull());
bool has_type, has_endings;
bool has_endings, has_type;

if (!JS_HasProperty(cx, opts, "type", &has_type) ||
!JS_HasProperty(cx, opts, "endings", &has_endings)) {
if (!JS_HasProperty(cx, opts, "endings", &has_endings) ||
!JS_HasProperty(cx, opts, "type", &has_type)) {
return false;
}

Expand All @@ -398,19 +401,6 @@ bool Blob::init_options(JSContext *cx, HandleObject self, HandleValue initv) {
return true;
}

if (has_type) {
JS::RootedValue type(cx);
if (!JS_GetProperty(cx, opts, "type", &type)) {
return false;
}

auto type_str = normalize_type(cx, type);
if (!type_str) {
return false;
}
SetReservedSlot(self, static_cast<uint32_t>(Slots::Type), PrivateValue(type_str));
}

if (has_endings) {
JS::RootedValue endings_val(cx);
bool is_transparent, is_native;
Expand All @@ -424,13 +414,23 @@ bool Blob::init_options(JSContext *cx, HandleObject self, HandleValue initv) {
return false;
}

if (!is_transparent && !is_native) {
// Use defaults
return true;
if (is_transparent || is_native) {
auto endings = is_native ? Blob::Endings::Native : Blob::Endings::Transparent;
SetReservedSlot(self, static_cast<uint32_t>(Slots::Endings), JS::Int32Value(endings));
}
}

auto endings = is_native ? Blob::Endings::Native : Blob::Endings::Transparent;
SetReservedSlot(self, static_cast<uint32_t>(Slots::Endings), JS::Int32Value(endings));
if (has_type) {
JS::RootedValue type(cx);
if (!JS_GetProperty(cx, opts, "type", &type)) {
return false;
}

auto type_str = normalize_type(cx, type);
if (!type_str) {
return false;
}
SetReservedSlot(self, static_cast<uint32_t>(Slots::Type), PrivateValue(type_str));
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
"status": "PASS"
},
"options properties should be accessed in lexicographic order.": {
"status": "FAIL"
"status": "PASS"
},
"Arguments should be evaluated from left to right.": {
"status": "FAIL"
Expand Down Expand Up @@ -188,4 +188,4 @@
"Blob with type \"image/png\"": {
"status": "PASS"
}
}
}

0 comments on commit c64f6a1

Please sign in to comment.