IntlDateTimeFormatPrototype.cpp [plain text]
#include "config.h"
#include "IntlDateTimeFormatPrototype.h"
#include "BuiltinNames.h"
#include "DateConstructor.h"
#include "IntlDateTimeFormatInlines.h"
#include "JSBoundFunction.h"
#include "JSCInlines.h"
#include <wtf/DateMath.h>
namespace JSC {
static JSC_DECLARE_HOST_FUNCTION(IntlDateTimeFormatPrototypeGetterFormat);
static JSC_DECLARE_HOST_FUNCTION(IntlDateTimeFormatPrototypeFuncFormatRange);
static JSC_DECLARE_HOST_FUNCTION(IntlDateTimeFormatPrototypeFuncFormatRangeToParts);
static JSC_DECLARE_HOST_FUNCTION(IntlDateTimeFormatPrototypeFuncFormatToParts);
static JSC_DECLARE_HOST_FUNCTION(IntlDateTimeFormatPrototypeFuncResolvedOptions);
static JSC_DECLARE_HOST_FUNCTION(IntlDateTimeFormatFuncFormatDateTime);
}
#include "IntlDateTimeFormatPrototype.lut.h"
namespace JSC {
const ClassInfo IntlDateTimeFormatPrototype::s_info = { "Intl.DateTimeFormat", &Base::s_info, &dateTimeFormatPrototypeTable, nullptr, CREATE_METHOD_TABLE(IntlDateTimeFormatPrototype) };
IntlDateTimeFormatPrototype* IntlDateTimeFormatPrototype::create(VM& vm, JSGlobalObject* globalObject, Structure* structure)
{
IntlDateTimeFormatPrototype* object = new (NotNull, allocateCell<IntlDateTimeFormatPrototype>(vm.heap)) IntlDateTimeFormatPrototype(vm, structure);
object->finishCreation(vm, globalObject);
return object;
}
Structure* IntlDateTimeFormatPrototype::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
{
return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
}
IntlDateTimeFormatPrototype::IntlDateTimeFormatPrototype(VM& vm, Structure* structure)
: Base(vm, structure)
{
}
void IntlDateTimeFormatPrototype::finishCreation(VM& vm, JSGlobalObject* globalObject)
{
Base::finishCreation(vm);
ASSERT(inherits(vm, info()));
#if HAVE(ICU_U_DATE_INTERVAL_FORMAT_FORMAT_RANGE_TO_PARTS)
if (Options::useIntlDateTimeFormatRangeToParts())
JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("formatRangeToParts", IntlDateTimeFormatPrototypeFuncFormatRangeToParts, static_cast<unsigned>(PropertyAttribute::DontEnum), 2);
#else
UNUSED_PARAM(globalObject);
UNUSED_PARAM(&IntlDateTimeFormatPrototypeFuncFormatRangeToParts);
#endif
JSC_TO_STRING_TAG_WITHOUT_TRANSITION();
}
JSC_DEFINE_HOST_FUNCTION(IntlDateTimeFormatFuncFormatDateTime, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
IntlDateTimeFormat* format = jsCast<IntlDateTimeFormat*>(callFrame->thisValue());
JSValue date = callFrame->argument(0);
double value;
if (date.isUndefined())
value = dateNowImpl().toNumber(globalObject);
else {
value = WTF::timeClip(date.toNumber(globalObject));
RETURN_IF_EXCEPTION(scope, encodedJSValue());
}
RELEASE_AND_RETURN(scope, JSValue::encode(format->format(globalObject, value)));
}
JSC_DEFINE_HOST_FUNCTION(IntlDateTimeFormatPrototypeGetterFormat, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
auto* dtf = IntlDateTimeFormat::unwrapForOldFunctions(globalObject, callFrame->thisValue());
RETURN_IF_EXCEPTION(scope, { });
if (UNLIKELY(!dtf))
return JSValue::encode(throwTypeError(globalObject, scope, "Intl.DateTimeFormat.prototype.format called on value that's not an object initialized as a DateTimeFormat"_s));
JSBoundFunction* boundFormat = dtf->boundFormat();
if (!boundFormat) {
JSGlobalObject* globalObject = dtf->globalObject(vm);
JSFunction* targetObject = JSFunction::create(vm, globalObject, 1, "format"_s, IntlDateTimeFormatFuncFormatDateTime, NoIntrinsic);
boundFormat = JSBoundFunction::create(vm, globalObject, targetObject, dtf, nullptr, 1, nullptr);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
dtf->setBoundFormat(vm, boundFormat);
}
return JSValue::encode(boundFormat);
}
JSC_DEFINE_HOST_FUNCTION(IntlDateTimeFormatPrototypeFuncFormatToParts, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
auto* dateTimeFormat = jsDynamicCast<IntlDateTimeFormat*>(vm, callFrame->thisValue());
if (UNLIKELY(!dateTimeFormat))
return JSValue::encode(throwTypeError(globalObject, scope, "Intl.DateTimeFormat.prototype.formatToParts called on value that's not an object initialized as a DateTimeFormat"_s));
JSValue date = callFrame->argument(0);
double value;
if (date.isUndefined())
value = dateNowImpl().toNumber(globalObject);
else {
value = WTF::timeClip(date.toNumber(globalObject));
RETURN_IF_EXCEPTION(scope, encodedJSValue());
}
RELEASE_AND_RETURN(scope, JSValue::encode(dateTimeFormat->formatToParts(globalObject, value)));
}
JSC_DEFINE_HOST_FUNCTION(IntlDateTimeFormatPrototypeFuncFormatRange, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
auto* dateTimeFormat = jsDynamicCast<IntlDateTimeFormat*>(vm, callFrame->thisValue());
if (UNLIKELY(!dateTimeFormat))
return JSValue::encode(throwTypeError(globalObject, scope, "Intl.DateTimeFormat.prototype.formatRange called on value that's not an object initialized as a DateTimeFormat"_s));
JSValue startDateValue = callFrame->argument(0);
JSValue endDateValue = callFrame->argument(1);
if (startDateValue.isUndefined() || endDateValue.isUndefined())
return throwVMTypeError(globalObject, scope, "startDate or endDate is undefined"_s);
double startDate = startDateValue.toNumber(globalObject);
RETURN_IF_EXCEPTION(scope, { });
double endDate = endDateValue.toNumber(globalObject);
RETURN_IF_EXCEPTION(scope, { });
if (startDate > endDate)
return throwVMRangeError(globalObject, scope, "startDate is larger than endDate"_s);
RELEASE_AND_RETURN(scope, JSValue::encode(dateTimeFormat->formatRange(globalObject, startDate, endDate)));
}
JSC_DEFINE_HOST_FUNCTION(IntlDateTimeFormatPrototypeFuncFormatRangeToParts, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
auto* dateTimeFormat = jsDynamicCast<IntlDateTimeFormat*>(vm, callFrame->thisValue());
if (UNLIKELY(!dateTimeFormat))
return JSValue::encode(throwTypeError(globalObject, scope, "Intl.DateTimeFormat.prototype.formatRangeToParts called on value that's not an object initialized as a DateTimeFormat"_s));
JSValue startDateValue = callFrame->argument(0);
JSValue endDateValue = callFrame->argument(1);
if (startDateValue.isUndefined() || endDateValue.isUndefined())
return throwVMTypeError(globalObject, scope, "startDate or endDate is undefined"_s);
double startDate = startDateValue.toNumber(globalObject);
RETURN_IF_EXCEPTION(scope, { });
double endDate = endDateValue.toNumber(globalObject);
RETURN_IF_EXCEPTION(scope, { });
if (startDate > endDate)
return throwVMRangeError(globalObject, scope, "startDate is larger than endDate"_s);
RELEASE_AND_RETURN(scope, JSValue::encode(dateTimeFormat->formatRangeToParts(globalObject, startDate, endDate)));
}
JSC_DEFINE_HOST_FUNCTION(IntlDateTimeFormatPrototypeFuncResolvedOptions, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
auto* dateTimeFormat = IntlDateTimeFormat::unwrapForOldFunctions(globalObject, callFrame->thisValue());
RETURN_IF_EXCEPTION(scope, { });
if (UNLIKELY(!dateTimeFormat))
return JSValue::encode(throwTypeError(globalObject, scope, "Intl.DateTimeFormat.prototype.resolvedOptions called on value that's not an object initialized as a DateTimeFormat"_s));
RELEASE_AND_RETURN(scope, JSValue::encode(dateTimeFormat->resolvedOptions(globalObject)));
}
}