#include "config.h"
#include "DatePrototype.h"
#include "DateConversion.h"
#include "DateInstance.h"
#include "Error.h"
#include "IntegrityInlines.h"
#include "JSCBuiltins.h"
#include "JSCInlines.h"
#include "JSDateMath.h"
#include "JSGlobalObject.h"
#include "JSObject.h"
#include "JSString.h"
#include <wtf/Assertions.h>
namespace JSC {
EncodedJSValue JSC_HOST_CALL dateProtoFuncGetDate(JSGlobalObject*, CallFrame*);
EncodedJSValue JSC_HOST_CALL dateProtoFuncGetDay(JSGlobalObject*, CallFrame*);
EncodedJSValue JSC_HOST_CALL dateProtoFuncGetFullYear(JSGlobalObject*, CallFrame*);
EncodedJSValue JSC_HOST_CALL dateProtoFuncGetHours(JSGlobalObject*, CallFrame*);
EncodedJSValue JSC_HOST_CALL dateProtoFuncGetMilliSeconds(JSGlobalObject*, CallFrame*);
EncodedJSValue JSC_HOST_CALL dateProtoFuncGetMinutes(JSGlobalObject*, CallFrame*);
EncodedJSValue JSC_HOST_CALL dateProtoFuncGetMonth(JSGlobalObject*, CallFrame*);
EncodedJSValue JSC_HOST_CALL dateProtoFuncGetSeconds(JSGlobalObject*, CallFrame*);
EncodedJSValue JSC_HOST_CALL dateProtoFuncGetTimezoneOffset(JSGlobalObject*, CallFrame*);
EncodedJSValue JSC_HOST_CALL dateProtoFuncGetUTCDate(JSGlobalObject*, CallFrame*);
EncodedJSValue JSC_HOST_CALL dateProtoFuncGetUTCDay(JSGlobalObject*, CallFrame*);
EncodedJSValue JSC_HOST_CALL dateProtoFuncGetUTCFullYear(JSGlobalObject*, CallFrame*);
EncodedJSValue JSC_HOST_CALL dateProtoFuncGetUTCHours(JSGlobalObject*, CallFrame*);
EncodedJSValue JSC_HOST_CALL dateProtoFuncGetUTCMilliseconds(JSGlobalObject*, CallFrame*);
EncodedJSValue JSC_HOST_CALL dateProtoFuncGetUTCMinutes(JSGlobalObject*, CallFrame*);
EncodedJSValue JSC_HOST_CALL dateProtoFuncGetUTCMonth(JSGlobalObject*, CallFrame*);
EncodedJSValue JSC_HOST_CALL dateProtoFuncGetUTCSeconds(JSGlobalObject*, CallFrame*);
EncodedJSValue JSC_HOST_CALL dateProtoFuncGetYear(JSGlobalObject*, CallFrame*);
EncodedJSValue JSC_HOST_CALL dateProtoFuncSetDate(JSGlobalObject*, CallFrame*);
EncodedJSValue JSC_HOST_CALL dateProtoFuncSetFullYear(JSGlobalObject*, CallFrame*);
EncodedJSValue JSC_HOST_CALL dateProtoFuncSetHours(JSGlobalObject*, CallFrame*);
EncodedJSValue JSC_HOST_CALL dateProtoFuncSetMilliSeconds(JSGlobalObject*, CallFrame*);
EncodedJSValue JSC_HOST_CALL dateProtoFuncSetMinutes(JSGlobalObject*, CallFrame*);
EncodedJSValue JSC_HOST_CALL dateProtoFuncSetMonth(JSGlobalObject*, CallFrame*);
EncodedJSValue JSC_HOST_CALL dateProtoFuncSetSeconds(JSGlobalObject*, CallFrame*);
EncodedJSValue JSC_HOST_CALL dateProtoFuncSetTime(JSGlobalObject*, CallFrame*);
EncodedJSValue JSC_HOST_CALL dateProtoFuncSetUTCDate(JSGlobalObject*, CallFrame*);
EncodedJSValue JSC_HOST_CALL dateProtoFuncSetUTCFullYear(JSGlobalObject*, CallFrame*);
EncodedJSValue JSC_HOST_CALL dateProtoFuncSetUTCHours(JSGlobalObject*, CallFrame*);
EncodedJSValue JSC_HOST_CALL dateProtoFuncSetUTCMilliseconds(JSGlobalObject*, CallFrame*);
EncodedJSValue JSC_HOST_CALL dateProtoFuncSetUTCMinutes(JSGlobalObject*, CallFrame*);
EncodedJSValue JSC_HOST_CALL dateProtoFuncSetUTCMonth(JSGlobalObject*, CallFrame*);
EncodedJSValue JSC_HOST_CALL dateProtoFuncSetUTCSeconds(JSGlobalObject*, CallFrame*);
EncodedJSValue JSC_HOST_CALL dateProtoFuncSetYear(JSGlobalObject*, CallFrame*);
EncodedJSValue JSC_HOST_CALL dateProtoFuncToDateString(JSGlobalObject*, CallFrame*);
EncodedJSValue JSC_HOST_CALL dateProtoFuncToPrimitiveSymbol(JSGlobalObject*, CallFrame*);
EncodedJSValue JSC_HOST_CALL dateProtoFuncToString(JSGlobalObject*, CallFrame*);
EncodedJSValue JSC_HOST_CALL dateProtoFuncToTimeString(JSGlobalObject*, CallFrame*);
EncodedJSValue JSC_HOST_CALL dateProtoFuncToUTCString(JSGlobalObject*, CallFrame*);
EncodedJSValue JSC_HOST_CALL dateProtoFuncToISOString(JSGlobalObject*, CallFrame*);
EncodedJSValue JSC_HOST_CALL dateProtoFuncToJSON(JSGlobalObject*, CallFrame*);
}
#include "DatePrototype.lut.h"
namespace JSC {
static EncodedJSValue formateDateInstance(JSGlobalObject* globalObject, CallFrame* callFrame, DateTimeFormat format, bool asUTCVariant)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
auto& cache = vm.dateCache;
JSValue thisValue = callFrame->thisValue();
auto* thisDateObj = jsDynamicCast<DateInstance*>(vm, thisValue);
if (UNLIKELY(!thisDateObj))
return throwVMTypeError(globalObject, scope);
Integrity::auditStructureID(vm, thisDateObj->structureID());
const GregorianDateTime* gregorianDateTime = asUTCVariant
? thisDateObj->gregorianDateTimeUTC(cache)
: thisDateObj->gregorianDateTime(cache);
if (!gregorianDateTime)
return JSValue::encode(jsNontrivialString(vm, String("Invalid Date"_s)));
return JSValue::encode(jsNontrivialString(vm, formatDateTime(*gregorianDateTime, format, asUTCVariant)));
}
static bool fillStructuresUsingTimeArgs(JSGlobalObject* globalObject, CallFrame* callFrame, int maxArgs, double* ms, GregorianDateTime* t)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
double milliseconds = 0;
bool ok = true;
int idx = 0;
int numArgs = callFrame->argumentCount();
if (numArgs > maxArgs)
numArgs = maxArgs;
if (maxArgs >= 4 && idx < numArgs) {
t->setHour(0);
double hours = callFrame->uncheckedArgument(idx++).toIntegerPreserveNaN(globalObject);
RETURN_IF_EXCEPTION(scope, false);
ok = std::isfinite(hours);
milliseconds += hours * msPerHour;
}
if (maxArgs >= 3 && idx < numArgs && ok) {
t->setMinute(0);
double minutes = callFrame->uncheckedArgument(idx++).toIntegerPreserveNaN(globalObject);
RETURN_IF_EXCEPTION(scope, false);
ok = std::isfinite(minutes);
milliseconds += minutes * msPerMinute;
}
if (maxArgs >= 2 && idx < numArgs && ok) {
t->setSecond(0);
double seconds = callFrame->uncheckedArgument(idx++).toIntegerPreserveNaN(globalObject);
RETURN_IF_EXCEPTION(scope, false);
ok = std::isfinite(seconds);
milliseconds += seconds * msPerSecond;
}
if (!ok)
return false;
if (idx < numArgs) {
double millis = callFrame->uncheckedArgument(idx).toIntegerPreserveNaN(globalObject);
RETURN_IF_EXCEPTION(scope, false);
ok = std::isfinite(millis);
milliseconds += millis;
} else
milliseconds += *ms;
*ms = milliseconds;
return ok;
}
static bool fillStructuresUsingDateArgs(JSGlobalObject* globalObject, CallFrame* callFrame, int maxArgs, double *ms, GregorianDateTime *t)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
int idx = 0;
bool ok = true;
int numArgs = callFrame->argumentCount();
if (numArgs > maxArgs)
numArgs = maxArgs;
if (maxArgs >= 3 && idx < numArgs) {
double years = callFrame->uncheckedArgument(idx++).toIntegerPreserveNaN(globalObject);
RETURN_IF_EXCEPTION(scope, false);
ok = std::isfinite(years);
t->setYear(toInt32(years));
}
if (maxArgs >= 2 && idx < numArgs && ok) {
double months = callFrame->uncheckedArgument(idx++).toIntegerPreserveNaN(globalObject);
RETURN_IF_EXCEPTION(scope, false);
ok = std::isfinite(months);
t->setMonth(toInt32(months));
}
if (idx < numArgs && ok) {
double days = callFrame->uncheckedArgument(idx++).toIntegerPreserveNaN(globalObject);
RETURN_IF_EXCEPTION(scope, false);
ok = std::isfinite(days);
t->setMonthDay(0);
*ms += days * msPerDay;
}
return ok;
}
const ClassInfo DatePrototype::s_info = {"Object", &Base::s_info, &datePrototypeTable, nullptr, CREATE_METHOD_TABLE(DatePrototype)};
DatePrototype::DatePrototype(VM& vm, Structure* structure)
: Base(vm, structure)
{
}
void DatePrototype::finishCreation(VM& vm, JSGlobalObject* globalObject)
{
Base::finishCreation(vm);
ASSERT(inherits(vm, info()));
Identifier toUTCStringName = Identifier::fromString(vm, "toUTCString"_s);
JSFunction* toUTCStringFunction = JSFunction::create(vm, globalObject, 0, toUTCStringName.string(), dateProtoFuncToUTCString);
putDirectWithoutTransition(vm, toUTCStringName, toUTCStringFunction, static_cast<unsigned>(PropertyAttribute::DontEnum));
putDirectWithoutTransition(vm, Identifier::fromString(vm, "toGMTString"_s), toUTCStringFunction, static_cast<unsigned>(PropertyAttribute::DontEnum));
JSFunction* toPrimitiveFunction = JSFunction::create(vm, globalObject, 1, "[Symbol.toPrimitive]"_s, dateProtoFuncToPrimitiveSymbol, NoIntrinsic);
putDirectWithoutTransition(vm, vm.propertyNames->toPrimitiveSymbol, toPrimitiveFunction, PropertyAttribute::DontEnum | PropertyAttribute::ReadOnly);
}
EncodedJSValue JSC_HOST_CALL dateProtoFuncToString(JSGlobalObject* globalObject, CallFrame* callFrame)
{
const bool asUTCVariant = false;
return formateDateInstance(globalObject, callFrame, DateTimeFormatDateAndTime, asUTCVariant);
}
EncodedJSValue JSC_HOST_CALL dateProtoFuncToUTCString(JSGlobalObject* globalObject, CallFrame* callFrame)
{
const bool asUTCVariant = true;
return formateDateInstance(globalObject, callFrame, DateTimeFormatDateAndTime, asUTCVariant);
}
EncodedJSValue JSC_HOST_CALL dateProtoFuncToISOString(JSGlobalObject* globalObject, CallFrame* callFrame)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = callFrame->thisValue();
auto* thisDateObj = jsDynamicCast<DateInstance*>(vm, thisValue);
if (UNLIKELY(!thisDateObj))
return throwVMTypeError(globalObject, scope);
Integrity::auditStructureID(vm, thisDateObj->structureID());
if (!std::isfinite(thisDateObj->internalNumber()))
return throwVMError(globalObject, scope, createRangeError(globalObject, "Invalid Date"_s));
const GregorianDateTime* gregorianDateTime = thisDateObj->gregorianDateTimeUTC(vm.dateCache);
if (!gregorianDateTime)
return JSValue::encode(jsNontrivialString(vm, String("Invalid Date"_s)));
char buffer[28];
int ms = static_cast<int>(fmod(thisDateObj->internalNumber(), msPerSecond));
if (ms < 0)
ms += msPerSecond;
int charactersWritten;
if (gregorianDateTime->year() > 9999 || gregorianDateTime->year() < 0)
charactersWritten = snprintf(buffer, sizeof(buffer), "%+07d-%02d-%02dT%02d:%02d:%02d.%03dZ", gregorianDateTime->year(), gregorianDateTime->month() + 1, gregorianDateTime->monthDay(), gregorianDateTime->hour(), gregorianDateTime->minute(), gregorianDateTime->second(), ms);
else
charactersWritten = snprintf(buffer, sizeof(buffer), "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", gregorianDateTime->year(), gregorianDateTime->month() + 1, gregorianDateTime->monthDay(), gregorianDateTime->hour(), gregorianDateTime->minute(), gregorianDateTime->second(), ms);
ASSERT(charactersWritten > 0 && static_cast<unsigned>(charactersWritten) < sizeof(buffer));
if (static_cast<unsigned>(charactersWritten) >= sizeof(buffer))
return JSValue::encode(jsEmptyString(vm));
return JSValue::encode(jsNontrivialString(vm, String(buffer, charactersWritten)));
}
EncodedJSValue JSC_HOST_CALL dateProtoFuncToDateString(JSGlobalObject* globalObject, CallFrame* callFrame)
{
const bool asUTCVariant = false;
return formateDateInstance(globalObject, callFrame, DateTimeFormatDate, asUTCVariant);
}
EncodedJSValue JSC_HOST_CALL dateProtoFuncToTimeString(JSGlobalObject* globalObject, CallFrame* callFrame)
{
const bool asUTCVariant = false;
return formateDateInstance(globalObject, callFrame, DateTimeFormatTime, asUTCVariant);
}
EncodedJSValue JSC_HOST_CALL dateProtoFuncToPrimitiveSymbol(JSGlobalObject* globalObject, CallFrame* callFrame)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = callFrame->thisValue();
if (!thisValue.isObject())
return throwVMTypeError(globalObject, scope, "Date.prototype[Symbol.toPrimitive] expected |this| to be an object.");
JSObject* thisObject = jsCast<JSObject*>(thisValue);
Integrity::auditStructureID(vm, thisObject->structureID());
if (!callFrame->argumentCount())
return throwVMTypeError(globalObject, scope, "Date.prototype[Symbol.toPrimitive] expected a first argument.");
JSValue hintValue = callFrame->uncheckedArgument(0);
PreferredPrimitiveType type = toPreferredPrimitiveType(globalObject, hintValue);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
if (type == NoPreference)
type = PreferString;
RELEASE_AND_RETURN(scope, JSValue::encode(thisObject->ordinaryToPrimitive(globalObject, type)));
}
EncodedJSValue JSC_HOST_CALL dateProtoFuncGetTime(JSGlobalObject* globalObject, CallFrame* callFrame)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = callFrame->thisValue();
auto* thisDateObj = jsDynamicCast<DateInstance*>(vm, thisValue);
if (UNLIKELY(!thisDateObj))
return throwVMTypeError(globalObject, scope);
return JSValue::encode(jsNumber(thisDateObj->internalNumber()));
}
EncodedJSValue JSC_HOST_CALL dateProtoFuncGetFullYear(JSGlobalObject* globalObject, CallFrame* callFrame)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = callFrame->thisValue();
auto* thisDateObj = jsDynamicCast<DateInstance*>(vm, thisValue);
if (UNLIKELY(!thisDateObj))
return throwVMTypeError(globalObject, scope);
const GregorianDateTime* gregorianDateTime = thisDateObj->gregorianDateTime(vm.dateCache);
if (!gregorianDateTime)
return JSValue::encode(jsNaN());
return JSValue::encode(jsNumber(gregorianDateTime->year()));
}
EncodedJSValue JSC_HOST_CALL dateProtoFuncGetUTCFullYear(JSGlobalObject* globalObject, CallFrame* callFrame)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = callFrame->thisValue();
auto* thisDateObj = jsDynamicCast<DateInstance*>(vm, thisValue);
if (UNLIKELY(!thisDateObj))
return throwVMTypeError(globalObject, scope);
const GregorianDateTime* gregorianDateTime = thisDateObj->gregorianDateTimeUTC(vm.dateCache);
if (!gregorianDateTime)
return JSValue::encode(jsNaN());
return JSValue::encode(jsNumber(gregorianDateTime->year()));
}
EncodedJSValue JSC_HOST_CALL dateProtoFuncGetMonth(JSGlobalObject* globalObject, CallFrame* callFrame)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = callFrame->thisValue();
auto* thisDateObj = jsDynamicCast<DateInstance*>(vm, thisValue);
if (UNLIKELY(!thisDateObj))
return throwVMTypeError(globalObject, scope);
const GregorianDateTime* gregorianDateTime = thisDateObj->gregorianDateTime(vm.dateCache);
if (!gregorianDateTime)
return JSValue::encode(jsNaN());
return JSValue::encode(jsNumber(gregorianDateTime->month()));
}
EncodedJSValue JSC_HOST_CALL dateProtoFuncGetUTCMonth(JSGlobalObject* globalObject, CallFrame* callFrame)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = callFrame->thisValue();
auto* thisDateObj = jsDynamicCast<DateInstance*>(vm, thisValue);
if (UNLIKELY(!thisDateObj))
return throwVMTypeError(globalObject, scope);
const GregorianDateTime* gregorianDateTime = thisDateObj->gregorianDateTimeUTC(vm.dateCache);
if (!gregorianDateTime)
return JSValue::encode(jsNaN());
return JSValue::encode(jsNumber(gregorianDateTime->month()));
}
EncodedJSValue JSC_HOST_CALL dateProtoFuncGetDate(JSGlobalObject* globalObject, CallFrame* callFrame)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = callFrame->thisValue();
auto* thisDateObj = jsDynamicCast<DateInstance*>(vm, thisValue);
if (UNLIKELY(!thisDateObj))
return throwVMTypeError(globalObject, scope);
const GregorianDateTime* gregorianDateTime = thisDateObj->gregorianDateTime(vm.dateCache);
if (!gregorianDateTime)
return JSValue::encode(jsNaN());
return JSValue::encode(jsNumber(gregorianDateTime->monthDay()));
}
EncodedJSValue JSC_HOST_CALL dateProtoFuncGetUTCDate(JSGlobalObject* globalObject, CallFrame* callFrame)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = callFrame->thisValue();
auto* thisDateObj = jsDynamicCast<DateInstance*>(vm, thisValue);
if (UNLIKELY(!thisDateObj))
return throwVMTypeError(globalObject, scope);
const GregorianDateTime* gregorianDateTime = thisDateObj->gregorianDateTimeUTC(vm.dateCache);
if (!gregorianDateTime)
return JSValue::encode(jsNaN());
return JSValue::encode(jsNumber(gregorianDateTime->monthDay()));
}
EncodedJSValue JSC_HOST_CALL dateProtoFuncGetDay(JSGlobalObject* globalObject, CallFrame* callFrame)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = callFrame->thisValue();
auto* thisDateObj = jsDynamicCast<DateInstance*>(vm, thisValue);
if (UNLIKELY(!thisDateObj))
return throwVMTypeError(globalObject, scope);
const GregorianDateTime* gregorianDateTime = thisDateObj->gregorianDateTime(vm.dateCache);
if (!gregorianDateTime)
return JSValue::encode(jsNaN());
return JSValue::encode(jsNumber(gregorianDateTime->weekDay()));
}
EncodedJSValue JSC_HOST_CALL dateProtoFuncGetUTCDay(JSGlobalObject* globalObject, CallFrame* callFrame)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = callFrame->thisValue();
auto* thisDateObj = jsDynamicCast<DateInstance*>(vm, thisValue);
if (UNLIKELY(!thisDateObj))
return throwVMTypeError(globalObject, scope);
const GregorianDateTime* gregorianDateTime = thisDateObj->gregorianDateTimeUTC(vm.dateCache);
if (!gregorianDateTime)
return JSValue::encode(jsNaN());
return JSValue::encode(jsNumber(gregorianDateTime->weekDay()));
}
EncodedJSValue JSC_HOST_CALL dateProtoFuncGetHours(JSGlobalObject* globalObject, CallFrame* callFrame)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = callFrame->thisValue();
auto* thisDateObj = jsDynamicCast<DateInstance*>(vm, thisValue);
if (UNLIKELY(!thisDateObj))
return throwVMTypeError(globalObject, scope);
const GregorianDateTime* gregorianDateTime = thisDateObj->gregorianDateTime(vm.dateCache);
if (!gregorianDateTime)
return JSValue::encode(jsNaN());
return JSValue::encode(jsNumber(gregorianDateTime->hour()));
}
EncodedJSValue JSC_HOST_CALL dateProtoFuncGetUTCHours(JSGlobalObject* globalObject, CallFrame* callFrame)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = callFrame->thisValue();
auto* thisDateObj = jsDynamicCast<DateInstance*>(vm, thisValue);
if (UNLIKELY(!thisDateObj))
return throwVMTypeError(globalObject, scope);
const GregorianDateTime* gregorianDateTime = thisDateObj->gregorianDateTimeUTC(vm.dateCache);
if (!gregorianDateTime)
return JSValue::encode(jsNaN());
return JSValue::encode(jsNumber(gregorianDateTime->hour()));
}
EncodedJSValue JSC_HOST_CALL dateProtoFuncGetMinutes(JSGlobalObject* globalObject, CallFrame* callFrame)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = callFrame->thisValue();
auto* thisDateObj = jsDynamicCast<DateInstance*>(vm, thisValue);
if (UNLIKELY(!thisDateObj))
return throwVMTypeError(globalObject, scope);
const GregorianDateTime* gregorianDateTime = thisDateObj->gregorianDateTime(vm.dateCache);
if (!gregorianDateTime)
return JSValue::encode(jsNaN());
return JSValue::encode(jsNumber(gregorianDateTime->minute()));
}
EncodedJSValue JSC_HOST_CALL dateProtoFuncGetUTCMinutes(JSGlobalObject* globalObject, CallFrame* callFrame)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = callFrame->thisValue();
auto* thisDateObj = jsDynamicCast<DateInstance*>(vm, thisValue);
if (UNLIKELY(!thisDateObj))
return throwVMTypeError(globalObject, scope);
const GregorianDateTime* gregorianDateTime = thisDateObj->gregorianDateTimeUTC(vm.dateCache);
if (!gregorianDateTime)
return JSValue::encode(jsNaN());
return JSValue::encode(jsNumber(gregorianDateTime->minute()));
}
EncodedJSValue JSC_HOST_CALL dateProtoFuncGetSeconds(JSGlobalObject* globalObject, CallFrame* callFrame)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = callFrame->thisValue();
auto* thisDateObj = jsDynamicCast<DateInstance*>(vm, thisValue);
if (UNLIKELY(!thisDateObj))
return throwVMTypeError(globalObject, scope);
const GregorianDateTime* gregorianDateTime = thisDateObj->gregorianDateTime(vm.dateCache);
if (!gregorianDateTime)
return JSValue::encode(jsNaN());
return JSValue::encode(jsNumber(gregorianDateTime->second()));
}
EncodedJSValue JSC_HOST_CALL dateProtoFuncGetUTCSeconds(JSGlobalObject* globalObject, CallFrame* callFrame)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = callFrame->thisValue();
auto* thisDateObj = jsDynamicCast<DateInstance*>(vm, thisValue);
if (UNLIKELY(!thisDateObj))
return throwVMTypeError(globalObject, scope);
const GregorianDateTime* gregorianDateTime = thisDateObj->gregorianDateTimeUTC(vm.dateCache);
if (!gregorianDateTime)
return JSValue::encode(jsNaN());
return JSValue::encode(jsNumber(gregorianDateTime->second()));
}
EncodedJSValue JSC_HOST_CALL dateProtoFuncGetMilliSeconds(JSGlobalObject* globalObject, CallFrame* callFrame)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = callFrame->thisValue();
auto* thisDateObj = jsDynamicCast<DateInstance*>(vm, thisValue);
if (UNLIKELY(!thisDateObj))
return throwVMTypeError(globalObject, scope);
double milli = thisDateObj->internalNumber();
if (std::isnan(milli))
return JSValue::encode(jsNaN());
double secs = floor(milli / msPerSecond);
double ms = milli - secs * msPerSecond;
return JSValue::encode(jsNumber(static_cast<int32_t>(ms)));
}
EncodedJSValue JSC_HOST_CALL dateProtoFuncGetUTCMilliseconds(JSGlobalObject* globalObject, CallFrame* callFrame)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = callFrame->thisValue();
auto* thisDateObj = jsDynamicCast<DateInstance*>(vm, thisValue);
if (UNLIKELY(!thisDateObj))
return throwVMTypeError(globalObject, scope);
double milli = thisDateObj->internalNumber();
if (std::isnan(milli))
return JSValue::encode(jsNaN());
double secs = floor(milli / msPerSecond);
double ms = milli - secs * msPerSecond;
return JSValue::encode(jsNumber(static_cast<int32_t>(ms)));
}
EncodedJSValue JSC_HOST_CALL dateProtoFuncGetTimezoneOffset(JSGlobalObject* globalObject, CallFrame* callFrame)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = callFrame->thisValue();
auto* thisDateObj = jsDynamicCast<DateInstance*>(vm, thisValue);
if (UNLIKELY(!thisDateObj))
return throwVMTypeError(globalObject, scope);
const GregorianDateTime* gregorianDateTime = thisDateObj->gregorianDateTime(vm.dateCache);
if (!gregorianDateTime)
return JSValue::encode(jsNaN());
return JSValue::encode(jsNumber(-gregorianDateTime->utcOffsetInMinute()));
}
EncodedJSValue JSC_HOST_CALL dateProtoFuncSetTime(JSGlobalObject* globalObject, CallFrame* callFrame)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = callFrame->thisValue();
auto* thisDateObj = jsDynamicCast<DateInstance*>(vm, thisValue);
if (UNLIKELY(!thisDateObj))
return throwVMTypeError(globalObject, scope);
double milli = timeClip(callFrame->argument(0).toNumber(globalObject));
RETURN_IF_EXCEPTION(scope, encodedJSValue());
thisDateObj->setInternalNumber(milli);
return JSValue::encode(jsNumber(milli));
}
static EncodedJSValue setNewValueFromTimeArgs(JSGlobalObject* globalObject, CallFrame* callFrame, int numArgsToUse, WTF::TimeType inputTimeType)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
auto& cache = vm.dateCache;
JSValue thisValue = callFrame->thisValue();
auto* thisDateObj = jsDynamicCast<DateInstance*>(vm, thisValue);
if (UNLIKELY(!thisDateObj))
return throwVMTypeError(globalObject, scope);
double milli = thisDateObj->internalNumber();
if (!callFrame->argumentCount() || std::isnan(milli)) {
thisDateObj->setInternalNumber(PNaN);
return JSValue::encode(jsNaN());
}
double secs = floor(milli / msPerSecond);
double ms = milli - secs * msPerSecond;
const GregorianDateTime* other = inputTimeType == WTF::UTCTime
? thisDateObj->gregorianDateTimeUTC(cache)
: thisDateObj->gregorianDateTime(cache);
if (!other)
return JSValue::encode(jsNaN());
GregorianDateTime gregorianDateTime(*other);
bool success = fillStructuresUsingTimeArgs(globalObject, callFrame, numArgsToUse, &ms, &gregorianDateTime);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
if (!success) {
thisDateObj->setInternalNumber(PNaN);
return JSValue::encode(jsNaN());
}
double newUTCDate = gregorianDateTimeToMS(cache, gregorianDateTime, ms, inputTimeType);
double result = timeClip(newUTCDate);
thisDateObj->setInternalNumber(result);
return JSValue::encode(jsNumber(result));
}
static EncodedJSValue setNewValueFromDateArgs(JSGlobalObject* globalObject, CallFrame* callFrame, int numArgsToUse, WTF::TimeType inputTimeType)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
auto& cache = vm.dateCache;
JSValue thisValue = callFrame->thisValue();
auto* thisDateObj = jsDynamicCast<DateInstance*>(vm, thisValue);
if (UNLIKELY(!thisDateObj))
return throwVMTypeError(globalObject, scope);
if (!callFrame->argumentCount()) {
thisDateObj->setInternalNumber(PNaN);
return JSValue::encode(jsNaN());
}
double milli = thisDateObj->internalNumber();
double ms = 0;
GregorianDateTime gregorianDateTime;
if (numArgsToUse == 3 && std::isnan(milli))
msToGregorianDateTime(cache, 0, WTF::UTCTime, gregorianDateTime);
else {
ms = milli - floor(milli / msPerSecond) * msPerSecond;
const GregorianDateTime* other = inputTimeType == WTF::UTCTime
? thisDateObj->gregorianDateTimeUTC(cache)
: thisDateObj->gregorianDateTime(cache);
if (!other)
return JSValue::encode(jsNaN());
gregorianDateTime = *other;
}
bool success = fillStructuresUsingDateArgs(globalObject, callFrame, numArgsToUse, &ms, &gregorianDateTime);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
if (!success) {
thisDateObj->setInternalNumber(PNaN);
return JSValue::encode(jsNaN());
}
double newUTCDate = gregorianDateTimeToMS(cache, gregorianDateTime, ms, inputTimeType);
double result = timeClip(newUTCDate);
thisDateObj->setInternalNumber(result);
return JSValue::encode(jsNumber(result));
}
EncodedJSValue JSC_HOST_CALL dateProtoFuncSetMilliSeconds(JSGlobalObject* globalObject, CallFrame* callFrame)
{
return setNewValueFromTimeArgs(globalObject, callFrame, 1, WTF::LocalTime);
}
EncodedJSValue JSC_HOST_CALL dateProtoFuncSetUTCMilliseconds(JSGlobalObject* globalObject, CallFrame* callFrame)
{
return setNewValueFromTimeArgs(globalObject, callFrame, 1, WTF::UTCTime);
}
EncodedJSValue JSC_HOST_CALL dateProtoFuncSetSeconds(JSGlobalObject* globalObject, CallFrame* callFrame)
{
return setNewValueFromTimeArgs(globalObject, callFrame, 2, WTF::LocalTime);
}
EncodedJSValue JSC_HOST_CALL dateProtoFuncSetUTCSeconds(JSGlobalObject* globalObject, CallFrame* callFrame)
{
return setNewValueFromTimeArgs(globalObject, callFrame, 2, WTF::UTCTime);
}
EncodedJSValue JSC_HOST_CALL dateProtoFuncSetMinutes(JSGlobalObject* globalObject, CallFrame* callFrame)
{
return setNewValueFromTimeArgs(globalObject, callFrame, 3, WTF::LocalTime);
}
EncodedJSValue JSC_HOST_CALL dateProtoFuncSetUTCMinutes(JSGlobalObject* globalObject, CallFrame* callFrame)
{
return setNewValueFromTimeArgs(globalObject, callFrame, 3, WTF::UTCTime);
}
EncodedJSValue JSC_HOST_CALL dateProtoFuncSetHours(JSGlobalObject* globalObject, CallFrame* callFrame)
{
return setNewValueFromTimeArgs(globalObject, callFrame, 4, WTF::LocalTime);
}
EncodedJSValue JSC_HOST_CALL dateProtoFuncSetUTCHours(JSGlobalObject* globalObject, CallFrame* callFrame)
{
return setNewValueFromTimeArgs(globalObject, callFrame, 4, WTF::UTCTime);
}
EncodedJSValue JSC_HOST_CALL dateProtoFuncSetDate(JSGlobalObject* globalObject, CallFrame* callFrame)
{
return setNewValueFromDateArgs(globalObject, callFrame, 1, WTF::LocalTime);
}
EncodedJSValue JSC_HOST_CALL dateProtoFuncSetUTCDate(JSGlobalObject* globalObject, CallFrame* callFrame)
{
return setNewValueFromDateArgs(globalObject, callFrame, 1, WTF::UTCTime);
}
EncodedJSValue JSC_HOST_CALL dateProtoFuncSetMonth(JSGlobalObject* globalObject, CallFrame* callFrame)
{
return setNewValueFromDateArgs(globalObject, callFrame, 2, WTF::LocalTime);
}
EncodedJSValue JSC_HOST_CALL dateProtoFuncSetUTCMonth(JSGlobalObject* globalObject, CallFrame* callFrame)
{
return setNewValueFromDateArgs(globalObject, callFrame, 2, WTF::UTCTime);
}
EncodedJSValue JSC_HOST_CALL dateProtoFuncSetFullYear(JSGlobalObject* globalObject, CallFrame* callFrame)
{
return setNewValueFromDateArgs(globalObject, callFrame, 3, WTF::LocalTime);
}
EncodedJSValue JSC_HOST_CALL dateProtoFuncSetUTCFullYear(JSGlobalObject* globalObject, CallFrame* callFrame)
{
return setNewValueFromDateArgs(globalObject, callFrame, 3, WTF::UTCTime);
}
EncodedJSValue JSC_HOST_CALL dateProtoFuncSetYear(JSGlobalObject* globalObject, CallFrame* callFrame)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
auto& cache = vm.dateCache;
JSValue thisValue = callFrame->thisValue();
auto* thisDateObj = jsDynamicCast<DateInstance*>(vm, thisValue);
if (UNLIKELY(!thisDateObj))
return throwVMTypeError(globalObject, scope);
if (!callFrame->argumentCount()) {
thisDateObj->setInternalNumber(PNaN);
return JSValue::encode(jsNaN());
}
double milli = thisDateObj->internalNumber();
double ms = 0;
GregorianDateTime gregorianDateTime;
if (std::isnan(milli))
msToGregorianDateTime(cache, 0, WTF::UTCTime, gregorianDateTime);
else {
double secs = floor(milli / msPerSecond);
ms = milli - secs * msPerSecond;
if (const GregorianDateTime* other = thisDateObj->gregorianDateTime(cache))
gregorianDateTime = *other;
}
double year = callFrame->argument(0).toIntegerPreserveNaN(globalObject);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
if (!std::isfinite(year)) {
thisDateObj->setInternalNumber(PNaN);
return JSValue::encode(jsNaN());
}
gregorianDateTime.setYear(toInt32((year >= 0 && year <= 99) ? (year + 1900) : year));
double timeInMilliseconds = gregorianDateTimeToMS(cache, gregorianDateTime, ms, WTF::LocalTime);
double result = timeClip(timeInMilliseconds);
thisDateObj->setInternalNumber(result);
return JSValue::encode(jsNumber(result));
}
EncodedJSValue JSC_HOST_CALL dateProtoFuncGetYear(JSGlobalObject* globalObject, CallFrame* callFrame)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = callFrame->thisValue();
auto* thisDateObj = jsDynamicCast<DateInstance*>(vm, thisValue);
if (UNLIKELY(!thisDateObj))
return throwVMTypeError(globalObject, scope);
const GregorianDateTime* gregorianDateTime = thisDateObj->gregorianDateTime(vm.dateCache);
if (!gregorianDateTime)
return JSValue::encode(jsNaN());
return JSValue::encode(jsNumber(gregorianDateTime->year() - 1900));
}
EncodedJSValue JSC_HOST_CALL dateProtoFuncToJSON(JSGlobalObject* globalObject, CallFrame* callFrame)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = callFrame->thisValue();
JSObject* object = thisValue.toObject(globalObject);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
JSValue timeValue = object->toPrimitive(globalObject, PreferNumber);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
if (timeValue.isNumber() && !std::isfinite(timeValue.asNumber()))
return JSValue::encode(jsNull());
JSValue toISOValue = object->get(globalObject, vm.propertyNames->toISOString);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
auto callData = getCallData(vm, toISOValue);
if (callData.type == CallData::Type::None)
return throwVMTypeError(globalObject, scope, "toISOString is not a function"_s);
JSValue result = call(globalObject, asObject(toISOValue), callData, object, *vm.emptyList);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
return JSValue::encode(result);
}
}