IntlNumberFormatPrototype.cpp [plain text]
#include "config.h"
#include "IntlNumberFormatPrototype.h"
#include "BuiltinNames.h"
#include "IntlNumberFormatInlines.h"
#include "JSBoundFunction.h"
#include "JSCInlines.h"
namespace JSC {
static JSC_DECLARE_HOST_FUNCTION(IntlNumberFormatPrototypeGetterFormat);
static JSC_DECLARE_HOST_FUNCTION(IntlNumberFormatPrototypeFuncFormatToParts);
static JSC_DECLARE_HOST_FUNCTION(IntlNumberFormatPrototypeFuncResolvedOptions);
static JSC_DECLARE_HOST_FUNCTION(IntlNumberFormatFuncFormat);
}
#include "IntlNumberFormatPrototype.lut.h"
namespace JSC {
const ClassInfo IntlNumberFormatPrototype::s_info = { "Intl.NumberFormat", &Base::s_info, &numberFormatPrototypeTable, nullptr, CREATE_METHOD_TABLE(IntlNumberFormatPrototype) };
IntlNumberFormatPrototype* IntlNumberFormatPrototype::create(VM& vm, JSGlobalObject*, Structure* structure)
{
IntlNumberFormatPrototype* object = new (NotNull, allocateCell<IntlNumberFormatPrototype>(vm.heap)) IntlNumberFormatPrototype(vm, structure);
object->finishCreation(vm);
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)
{
Base::finishCreation(vm);
ASSERT(inherits(vm, info()));
JSC_TO_STRING_TAG_WITHOUT_TRANSITION();
}
JSC_DEFINE_HOST_FUNCTION(IntlNumberFormatFuncFormat, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
auto* numberFormat = jsCast<IntlNumberFormat*>(callFrame->thisValue());
JSValue bigIntOrNumber = callFrame->argument(0).toNumeric(globalObject);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
scope.release();
if (bigIntOrNumber.isNumber()) {
double value = bigIntOrNumber.asNumber();
return JSValue::encode(numberFormat->format(globalObject, value));
}
#if USE(BIGINT32)
if (bigIntOrNumber.isBigInt32()) {
JSBigInt* value = JSBigInt::createFrom(globalObject, bigIntOrNumber.bigInt32AsInt32());
RETURN_IF_EXCEPTION(scope, { });
RELEASE_AND_RETURN(scope, JSValue::encode(numberFormat->format(globalObject, value)));
}
#endif
ASSERT(bigIntOrNumber.isHeapBigInt());
JSBigInt* value = bigIntOrNumber.asHeapBigInt();
return JSValue::encode(numberFormat->format(globalObject, value));
}
JSC_DEFINE_HOST_FUNCTION(IntlNumberFormatPrototypeGetterFormat, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
auto* nf = IntlNumberFormat::unwrapForOldFunctions(globalObject, callFrame->thisValue());
RETURN_IF_EXCEPTION(scope, { });
if (UNLIKELY(!nf))
return JSValue::encode(throwTypeError(globalObject, 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);
auto* targetObject = JSFunction::create(vm, globalObject, 1, "format"_s, IntlNumberFormatFuncFormat, NoIntrinsic);
boundFormat = JSBoundFunction::create(vm, globalObject, targetObject, nf, nullptr, 1, nullptr);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
nf->setBoundFormat(vm, boundFormat);
}
return JSValue::encode(boundFormat);
}
JSC_DEFINE_HOST_FUNCTION(IntlNumberFormatPrototypeFuncFormatToParts, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
auto* numberFormat = jsDynamicCast<IntlNumberFormat*>(vm, callFrame->thisValue());
if (UNLIKELY(!numberFormat))
return JSValue::encode(throwTypeError(globalObject, scope, "Intl.NumberFormat.prototype.formatToParts called on value that's not an object initialized as a NumberFormat"_s));
double value = callFrame->argument(0).toNumber(globalObject);
RETURN_IF_EXCEPTION(scope, encodedJSValue());
RELEASE_AND_RETURN(scope, JSValue::encode(numberFormat->formatToParts(globalObject, value)));
}
JSC_DEFINE_HOST_FUNCTION(IntlNumberFormatPrototypeFuncResolvedOptions, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
auto* numberFormat = IntlNumberFormat::unwrapForOldFunctions(globalObject, callFrame->thisValue());
RETURN_IF_EXCEPTION(scope, { });
if (UNLIKELY(!numberFormat))
return JSValue::encode(throwTypeError(globalObject, 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(globalObject)));
}
}