IntlDateTimeFormatPrototype.cpp [plain text]
#include "config.h"
#include "IntlDateTimeFormatPrototype.h"
#if ENABLE(INTL)
#include "BuiltinNames.h"
#include "DateConstructor.h"
#include "Error.h"
#include "IntlDateTimeFormat.h"
#include "IntlObject.h"
#include "JSBoundFunction.h"
#include "JSCInlines.h"
#include "JSObjectInlines.h"
#include <wtf/DateMath.h>
namespace JSC {
static EncodedJSValue JSC_HOST_CALL IntlDateTimeFormatPrototypeGetterFormat(ExecState*);
#if JSC_ICU_HAS_UFIELDPOSITER
static EncodedJSValue JSC_HOST_CALL IntlDateTimeFormatPrototypeFuncFormatToParts(ExecState*);
#endif
static EncodedJSValue JSC_HOST_CALL IntlDateTimeFormatPrototypeFuncResolvedOptions(ExecState*);
}
#include "IntlDateTimeFormatPrototype.lut.h"
namespace JSC {
const ClassInfo IntlDateTimeFormatPrototype::s_info = { "Object", &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, structure);
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, Structure*)
{
Base::finishCreation(vm);
#if JSC_ICU_HAS_UFIELDPOSITER
JSFunction* formatToPartsFunction = JSFunction::create(vm, globalObject, 1, vm.propertyNames->formatToParts.string(), IntlDateTimeFormatPrototypeFuncFormatToParts);
putDirectWithoutTransition(vm, vm.propertyNames->formatToParts, formatToPartsFunction, static_cast<unsigned>(PropertyAttribute::DontEnum));
#else
UNUSED_PARAM(globalObject);
#endif
putDirectWithoutTransition(vm, vm.propertyNames->toStringTagSymbol, jsString(&vm, "Object"), PropertyAttribute::DontEnum | PropertyAttribute::ReadOnly);
}
static EncodedJSValue JSC_HOST_CALL IntlDateTimeFormatFuncFormatDateTime(ExecState* state)
{
VM& vm = state->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
IntlDateTimeFormat* format = jsCast<IntlDateTimeFormat*>(state->thisValue());
JSValue date = state->argument(0);
double value;
if (date.isUndefined())
value = JSValue::decode(dateNow(state)).toNumber(state);
else {
value = WTF::timeClip(date.toNumber(state));
RETURN_IF_EXCEPTION(scope, encodedJSValue());
}
RELEASE_AND_RETURN(scope, JSValue::encode(format->format(*state, value)));
}
EncodedJSValue JSC_HOST_CALL IntlDateTimeFormatPrototypeGetterFormat(ExecState* state)
{
VM& vm = state->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
IntlDateTimeFormat* dtf = jsDynamicCast<IntlDateTimeFormat*>(vm, state->thisValue());
if (!dtf) {
JSValue value = state->thisValue().get(state, vm.propertyNames->builtinNames().intlSubstituteValuePrivateName());
RETURN_IF_EXCEPTION(scope, encodedJSValue());
dtf = jsDynamicCast<IntlDateTimeFormat*>(vm, value);
}
if (!dtf)
return JSValue::encode(throwTypeError(state, 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, state, globalObject, targetObject, dtf, nullptr, 1, "format"_s);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
dtf->setBoundFormat(vm, boundFormat);
}
return JSValue::encode(boundFormat);
}
#if JSC_ICU_HAS_UFIELDPOSITER
EncodedJSValue JSC_HOST_CALL IntlDateTimeFormatPrototypeFuncFormatToParts(ExecState* state)
{
VM& vm = state->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
IntlDateTimeFormat* dateTimeFormat = jsDynamicCast<IntlDateTimeFormat*>(vm, state->thisValue());
if (!dateTimeFormat)
return JSValue::encode(throwTypeError(state, scope, "Intl.DateTimeFormat.prototype.formatToParts called on value that's not an object initialized as a DateTimeFormat"_s));
JSValue date = state->argument(0);
double value;
if (date.isUndefined())
value = JSValue::decode(dateNow(state)).toNumber(state);
else {
value = WTF::timeClip(date.toNumber(state));
RETURN_IF_EXCEPTION(scope, encodedJSValue());
}
RELEASE_AND_RETURN(scope, JSValue::encode(dateTimeFormat->formatToParts(*state, value)));
}
#endif
EncodedJSValue JSC_HOST_CALL IntlDateTimeFormatPrototypeFuncResolvedOptions(ExecState* state)
{
VM& vm = state->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
IntlDateTimeFormat* dateTimeFormat = jsDynamicCast<IntlDateTimeFormat*>(vm, state->thisValue());
if (!dateTimeFormat) {
JSValue value = state->thisValue().get(state, vm.propertyNames->builtinNames().intlSubstituteValuePrivateName());
RETURN_IF_EXCEPTION(scope, encodedJSValue());
dateTimeFormat = jsDynamicCast<IntlDateTimeFormat*>(vm, value);
}
if (!dateTimeFormat)
return JSValue::encode(throwTypeError(state, 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(*state)));
}
}
#endif // ENABLE(INTL)