Skip to content

Commit

Permalink
Parse Date object
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiltd committed Jan 8, 2025
1 parent 12f720c commit 15f709f
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions builtins/web/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
#include "blob.h"

#include "host_api.h"
#include "jsfriendapi.h"
#include "js/CallAndConstruct.h"
#include "js/CallArgs.h"
#include "js/TypeDecls.h"

#include "mozilla/Assertions.h"

namespace {

bool init_last_modified(JSContext *cx, HandleValue initv, MutableHandleValue rval) {
Expand All @@ -28,7 +26,25 @@ bool init_last_modified(JSContext *cx, HandleValue initv, MutableHandleValue rva
return false;
}

if (ts.isNumber()) {
if (ts.isObject()) {
RootedObject date(cx, &ts.toObject());
bool is_valid = false;

if (!js::DateIsValid(cx, date, &is_valid)) {
return false;
}

if (is_valid) {
double ms_since_epoch;
if (!js::DateGetMsecSinceEpoch(cx, date, &ms_since_epoch)) {
return false;
}

rval.setNumber(ms_since_epoch);
return true;

}
} else if (ts.isNumber()) {
rval.set(ts);
return true;
}
Expand All @@ -41,7 +57,7 @@ bool init_last_modified(JSContext *cx, HandleValue initv, MutableHandleValue rva
auto ns_since_epoch = host_api::WallClock::now();
auto ms_since_epoch = ns_since_epoch / 1000000ULL;

rval.setInt32(ms_since_epoch);
rval.setNumber(ms_since_epoch);
return true;
}

Expand Down Expand Up @@ -92,7 +108,7 @@ bool File::lastModified_get(JSContext *cx, unsigned argc, JS::Value *vp) {
}

auto lastModified =
JS::GetReservedSlot(self, static_cast<size_t>(Slots::LastModified)).toInt32();
JS::GetReservedSlot(self, static_cast<size_t>(Slots::LastModified)).toNumber();
args.rval().setNumber(lastModified);
return true;
}
Expand Down

0 comments on commit 15f709f

Please sign in to comment.