Skip to content

Commit

Permalink
Add File::create function
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiltd committed Dec 23, 2024
1 parent 9b26b51 commit eeab147
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
4 changes: 4 additions & 0 deletions builtins/web/blob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,8 @@ JSObject *Blob::create(JSContext *cx, UniqueChars data, size_t data_len, HandleS
SetReservedSlot(self, static_cast<uint32_t>(Slots::Type), JS::StringValue(type));
SetReservedSlot(self, static_cast<uint32_t>(Slots::Endings), JS::Int32Value(LineEndings::Transparent));
SetReservedSlot(self, static_cast<uint32_t>(Slots::Readers), JS::PrivateValue(new ReadersMap));
SetReservedSlot(self, static_cast<uint32_t>(Slots::Reserved1), JS::NullValue());
SetReservedSlot(self, static_cast<uint32_t>(Slots::Reserved2), JS::NullValue());
return self;
}

Expand All @@ -695,6 +697,8 @@ bool Blob::constructor(JSContext *cx, unsigned argc, JS::Value *vp) {
SetReservedSlot(self, static_cast<uint32_t>(Slots::Endings), JS::Int32Value(LineEndings::Transparent));
SetReservedSlot(self, static_cast<uint32_t>(Slots::Data), JS::PrivateValue(new ByteBuffer));
SetReservedSlot(self, static_cast<uint32_t>(Slots::Readers), JS::PrivateValue(new ReadersMap));
SetReservedSlot(self, static_cast<uint32_t>(Slots::Reserved1), JS::NullValue());
SetReservedSlot(self, static_cast<uint32_t>(Slots::Reserved2), JS::NullValue());

// Walk the blob parts and append them to the blob's buffer.
if (blobParts.isNull()) {
Expand Down
2 changes: 1 addition & 1 deletion builtins/web/blob.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Blob : public TraceableBuiltinImpl<Blob> {
static const JSPropertySpec properties[];

static constexpr unsigned ctor_length = 0;
enum Slots { Data, Type, Endings, Readers, Reserved2, Reserved1, Count };
enum Slots { Data, Type, Endings, Readers, Reserved1, Reserved2, Count };
enum LineEndings { Transparent, Native };

using HeapObj = Heap<JSObject *>;
Expand Down
43 changes: 29 additions & 14 deletions builtins/web/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,26 +102,26 @@ bool File::lastModified_get(JSContext *cx, unsigned argc, JS::Value *vp) {
}

bool File::is_instance(const JSObject *obj) {
return obj != nullptr && (JS::GetClass(obj) == &class_ || JS::GetClass(obj) == &Blob::class_);
return obj != nullptr
&& JS::GetClass(obj) == &Blob::class_
&& !JS::GetReservedSlot(
(JSObject *)obj,
static_cast<size_t>(ParentSlots::Name)).isNullOrUndefined();
}

bool File::is_instance(const Value val) { return val.isObject() && is_instance(&val.toObject()); }

bool File::constructor(JSContext *cx, unsigned argc, JS::Value *vp) {
CTOR_HEADER("File", 2);

RootedValue fileBits(cx, args.get(0));
RootedValue fileName(cx, args.get(1));
RootedValue opts(cx, args.get(2));
bool File::is_instance(const Value val) {
return val.isObject() && is_instance(&val.toObject());
}

JSObject *File::create(JSContext *cx, HandleValue fileBits, HandleValue fileName, HandleValue opts) {
RootedObject blob_ctor(cx, JS_GetConstructor(cx, Blob::proto_obj));
if (!blob_ctor) {
return false;
return nullptr;
}

RootedObject this_ctor(cx, JS_GetConstructor(cx, File::proto_obj));
if (!this_ctor) {
return false;
return nullptr;
}

MOZ_ASSERT(JS::IsConstructor(blob_ctor));
Expand All @@ -138,13 +138,13 @@ bool File::constructor(JSContext *cx, unsigned argc, JS::Value *vp) {
RootedValue blob_ctor_val(cx, JS::ObjectValue(*blob_ctor));
RootedObject self(cx);
if (!JS::Construct(cx, blob_ctor_val, this_ctor, blob_args, &self)) {
return false;
return nullptr;
}

// 2. Let n be the fileName argument to the constructor.
RootedString name(cx, JS::ToString(cx, fileName));
if (!name) {
return false;
return nullptr;
}

// 3. Process `FilePropertyBag` dictionary argument by running the following substeps:
Expand All @@ -154,7 +154,7 @@ bool File::constructor(JSContext *cx, unsigned argc, JS::Value *vp) {
// milliseconds since the Unix Epoch.
RootedValue lastModified(cx);
if (!init_last_modified(cx, opts, &lastModified)) {
return false;
return nullptr;
}

// Return a new File object F such that:
Expand All @@ -169,6 +169,21 @@ bool File::constructor(JSContext *cx, unsigned argc, JS::Value *vp) {
SetReservedSlot(self, static_cast<uint32_t>(ParentSlots::Name), JS::StringValue(name));
SetReservedSlot(self, static_cast<uint32_t>(ParentSlots::LastModified), lastModified);

return self;
}

bool File::constructor(JSContext *cx, unsigned argc, JS::Value *vp) {
CTOR_HEADER("File", 2);

RootedValue fileBits(cx, args.get(0));
RootedValue fileName(cx, args.get(1));
RootedValue opts(cx, args.get(2));

RootedObject self(cx, create(cx, fileBits, fileName, opts));
if (!self) {
return false;
}

args.rval().setObject(*self);
return true;
}
Expand Down
1 change: 1 addition & 0 deletions builtins/web/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class File : public BuiltinImpl<File> {
static bool is_instance(const JSObject *obj);
static bool is_instance(const Value val);

static JSObject *create(JSContext *cx, HandleValue fileBits, HandleValue fileName, HandleValue opts);
static bool init_class(JSContext *cx, HandleObject global);
static bool constructor(JSContext *cx, unsigned argc, Value *vp);
};
Expand Down

0 comments on commit eeab147

Please sign in to comment.