IntlLocaleConstructor.cpp
1 /* 2 * Copyright (C) 2020 Sony Interactive Entertainment Inc. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 23 * THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #include "config.h" 27 #include "IntlLocaleConstructor.h" 28 29 #include "IntlLocale.h" 30 #include "IntlLocalePrototype.h" 31 #include "JSCInlines.h" 32 33 namespace JSC { 34 35 STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(IntlLocaleConstructor); 36 37 const ClassInfo IntlLocaleConstructor::s_info = { "Function", &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(IntlLocaleConstructor) }; 38 39 IntlLocaleConstructor* IntlLocaleConstructor::create(VM& vm, Structure* structure, IntlLocalePrototype* localePrototype) 40 { 41 auto* constructor = new (NotNull, allocateCell<IntlLocaleConstructor>(vm.heap)) IntlLocaleConstructor(vm, structure); 42 constructor->finishCreation(vm, localePrototype); 43 return constructor; 44 } 45 46 Structure* IntlLocaleConstructor::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype) 47 { 48 return Structure::create(vm, globalObject, prototype, TypeInfo(InternalFunctionType, StructureFlags), info()); 49 } 50 51 static JSC_DECLARE_HOST_FUNCTION(callIntlLocale); 52 static JSC_DECLARE_HOST_FUNCTION(constructIntlLocale); 53 54 IntlLocaleConstructor::IntlLocaleConstructor(VM& vm, Structure* structure) 55 : Base(vm, structure, callIntlLocale, constructIntlLocale) 56 { 57 } 58 59 void IntlLocaleConstructor::finishCreation(VM& vm, IntlLocalePrototype* localePrototype) 60 { 61 Base::finishCreation(vm, 1, "Locale"_s, PropertyAdditionMode::WithoutStructureTransition); 62 putDirectWithoutTransition(vm, vm.propertyNames->prototype, localePrototype, PropertyAttribute::DontEnum | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly); 63 localePrototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, this, static_cast<unsigned>(PropertyAttribute::DontEnum)); 64 } 65 66 // https://tc39.es/ecma402/#sec-Intl.Locale 67 JSC_DEFINE_HOST_FUNCTION(constructIntlLocale, (JSGlobalObject* globalObject, CallFrame* callFrame)) 68 { 69 VM& vm = globalObject->vm(); 70 auto scope = DECLARE_THROW_SCOPE(vm); 71 72 JSObject* newTarget = asObject(callFrame->newTarget()); 73 Structure* structure = newTarget == callFrame->jsCallee() 74 ? globalObject->localeStructure() 75 : InternalFunction::createSubclassStructure(globalObject, newTarget, getFunctionRealm(vm, newTarget)->localeStructure()); 76 RETURN_IF_EXCEPTION(scope, { }); 77 78 IntlLocale* locale = IntlLocale::create(vm, structure); 79 ASSERT(locale); 80 81 JSValue tag = callFrame->argument(0); 82 if (!tag.isString() && !tag.isObject()) 83 return throwVMTypeError(globalObject, scope, "First argument to Intl.Locale must be a string or an object"_s); 84 85 scope.release(); 86 locale->initializeLocale(globalObject, tag, callFrame->argument(1)); 87 return JSValue::encode(locale); 88 } 89 90 // https://tc39.es/ecma402/#sec-Intl.Locale 91 JSC_DEFINE_HOST_FUNCTION(callIntlLocale, (JSGlobalObject* globalObject, CallFrame*)) 92 { 93 VM& vm = globalObject->vm(); 94 auto scope = DECLARE_THROW_SCOPE(vm); 95 96 return JSValue::encode(throwConstructorCannotBeCalledAsFunctionTypeError(globalObject, scope, "Locale")); 97 } 98 99 } // namespace JSC