IntlNumberFormatPrototype.cpp [plain text]
#include "config.h"
#include "IntlNumberFormatPrototype.h"
#if ENABLE(INTL)
#include "BuiltinNames.h"
#include "Error.h"
#include "IntlNumberFormat.h"
#include "JSBoundFunction.h"
#include "JSCInlines.h"
#include "JSObjectInlines.h"
#include "Options.h"
namespace JSC {
static EncodedJSValue JSC_HOST_CALL IntlNumberFormatPrototypeGetterFormat(ExecState*);
#if HAVE(ICU_FORMAT_DOUBLE_FOR_FIELDS)
static EncodedJSValue JSC_HOST_CALL IntlNumberFormatPrototypeFuncFormatToParts(ExecState*);
#endif
static EncodedJSValue JSC_HOST_CALL IntlNumberFormatPrototypeFuncResolvedOptions(ExecState*);
}
#include "IntlNumberFormatPrototype.lut.h"
namespace JSC {
const ClassInfo IntlNumberFormatPrototype::s_info = { "Object", &Base::s_info, &numberFormatPrototypeTable, nullptr, CREATE_METHOD_TABLE(IntlNumberFormatPrototype) };
IntlNumberFormatPrototype* IntlNumberFormatPrototype::create(VM& vm, JSGlobalObject* globalObject, Structure* structure)
{
IntlNumberFormatPrototype* object = new (NotNull, allocateCell<IntlNumberFormatPrototype>(vm.heap)) IntlNumberFormatPrototype(vm, structure);
object->finishCreation(vm, globalObject, structure);
return object;
}
Structure* IntlNumberFormatPrototype::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
{
return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
}
IntlNumberFormatPrototype::IntlNumberFormatPrototype(VM& vm, Structure* structure)
: Base(vm, structure)
{
}
void IntlNumberFormatPrototype::finishCreation(VM& vm, JSGlobalObject* globalObject, Structure*)
{
Base::finishCreation(vm);
#if HAVE(ICU_FORMAT_DOUBLE_FOR_FIELDS)
if (Options::useIntlNumberFormatToParts())
JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->formatToParts, IntlNumberFormatPrototypeFuncFormatToParts, static_cast<unsigned>(PropertyAttribute::DontEnum), 1);
#else
UNUSED_PARAM(globalObject);
#endif
putDirectWithoutTransition(vm, vm.propertyNames->toStringTagSymbol, jsString(&vm, "Object"), PropertyAttribute::DontEnum | PropertyAttribute::ReadOnly);
}
static EncodedJSValue JSC_HOST_CALL IntlNumberFormatFuncFormatNumber(ExecState* state)
{
VM& vm = state->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
IntlNumberFormat* numberFormat = jsCast<IntlNumberFormat*>(state->thisValue());
double number = state->argument(0).toNumber(state);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
RELEASE_AND_RETURN(scope, JSValue::encode(numberFormat->formatNumber(*state, number)));
}
EncodedJSValue JSC_HOST_CALL IntlNumberFormatPrototypeGetterFormat(ExecState* state)
{
VM& vm = state->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
IntlNumberFormat* nf = jsDynamicCast<IntlNumberFormat*>(vm, state->thisValue());
if (!nf) {
JSValue value = state->thisValue().get(state, vm.propertyNames->builtinNames().intlSubstituteValuePrivateName());
RETURN_IF_EXCEPTION(scope, encodedJSValue());
nf = jsDynamicCast<IntlNumberFormat*>(vm, value);
}
if (!nf)
return JSValue::encode(throwTypeError(state, scope, "Intl.NumberFormat.prototype.format called on value that's not an object initialized as a NumberFormat"_s));
JSBoundFunction* boundFormat = nf->boundFormat();
if (!boundFormat) {
JSGlobalObject* globalObject = nf->globalObject(vm);
JSFunction* targetObject = JSFunction::create(vm, globalObject, 1, "format"_s, IntlNumberFormatFuncFormatNumber, NoIntrinsic);
boundFormat = JSBoundFunction::create(vm, state, globalObject, targetObject, nf, nullptr, 1, "format"_s);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
nf->setBoundFormat(vm, boundFormat);
}
return JSValue::encode(boundFormat);
}
#if HAVE(ICU_FORMAT_DOUBLE_FOR_FIELDS)
EncodedJSValue JSC_HOST_CALL IntlNumberFormatPrototypeFuncFormatToParts(ExecState* state)
{
VM& vm = state->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
IntlNumberFormat* numberFormat = jsDynamicCast<IntlNumberFormat*>(vm, state->thisValue());
if (!numberFormat)
return JSValue::encode(throwTypeError(state, scope, "Intl.NumberFormat.prototype.formatToParts called on value that's not an object initialized as a NumberFormat"_s));
double value = state->argument(0).toNumber(state);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
RELEASE_AND_RETURN(scope, JSValue::encode(numberFormat->formatToParts(*state, value)));
}
#endif
EncodedJSValue JSC_HOST_CALL IntlNumberFormatPrototypeFuncResolvedOptions(ExecState* state)
{
VM& vm = state->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
IntlNumberFormat* numberFormat = jsDynamicCast<IntlNumberFormat*>(vm, state->thisValue());
if (!numberFormat) {
JSValue value = state->thisValue().get(state, vm.propertyNames->builtinNames().intlSubstituteValuePrivateName());
RETURN_IF_EXCEPTION(scope, encodedJSValue());
numberFormat = jsDynamicCast<IntlNumberFormat*>(vm, value);
}
if (!numberFormat)
return JSValue::encode(throwTypeError(state, scope, "Intl.NumberFormat.prototype.resolvedOptions called on value that's not an object initialized as a NumberFormat"_s));
RELEASE_AND_RETURN(scope, JSValue::encode(numberFormat->resolvedOptions(*state)));
}
}
#endif // ENABLE(INTL)