IntlRelativeTimeFormatConstructor.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 "IntlRelativeTimeFormatConstructor.h" 28 29 #include "IntlObject.h" 30 #include "IntlRelativeTimeFormat.h" 31 #include "IntlRelativeTimeFormatPrototype.h" 32 #include "JSCInlines.h" 33 34 namespace JSC { 35 36 STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(IntlRelativeTimeFormatConstructor); 37 38 static JSC_DECLARE_HOST_FUNCTION(IntlRelativeTimeFormatConstructorFuncSupportedLocalesOf); 39 40 } 41 42 #include "IntlRelativeTimeFormatConstructor.lut.h" 43 44 namespace JSC { 45 46 const ClassInfo IntlRelativeTimeFormatConstructor::s_info = { "Function", &InternalFunction::s_info, &relativeTimeFormatConstructorTable, nullptr, CREATE_METHOD_TABLE(IntlRelativeTimeFormatConstructor) }; 47 48 /* Source for IntlRelativeTimeFormatConstructor.lut.h 49 @begin relativeTimeFormatConstructorTable 50 supportedLocalesOf IntlRelativeTimeFormatConstructorFuncSupportedLocalesOf DontEnum|Function 1 51 @end 52 */ 53 54 IntlRelativeTimeFormatConstructor* IntlRelativeTimeFormatConstructor::create(VM& vm, Structure* structure, IntlRelativeTimeFormatPrototype* relativeTimeFormatPrototype) 55 { 56 auto* constructor = new (NotNull, allocateCell<IntlRelativeTimeFormatConstructor>(vm.heap)) IntlRelativeTimeFormatConstructor(vm, structure); 57 constructor->finishCreation(vm, relativeTimeFormatPrototype); 58 return constructor; 59 } 60 61 Structure* IntlRelativeTimeFormatConstructor::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype) 62 { 63 return Structure::create(vm, globalObject, prototype, TypeInfo(InternalFunctionType, StructureFlags), info()); 64 } 65 66 static JSC_DECLARE_HOST_FUNCTION(callIntlRelativeTimeFormat); 67 static JSC_DECLARE_HOST_FUNCTION(constructIntlRelativeTimeFormat); 68 69 IntlRelativeTimeFormatConstructor::IntlRelativeTimeFormatConstructor(VM& vm, Structure* structure) 70 : Base(vm, structure, callIntlRelativeTimeFormat, constructIntlRelativeTimeFormat) 71 { 72 } 73 74 void IntlRelativeTimeFormatConstructor::finishCreation(VM& vm, IntlRelativeTimeFormatPrototype* relativeTimeFormatPrototype) 75 { 76 Base::finishCreation(vm, 0, "RelativeTimeFormat"_s, PropertyAdditionMode::WithoutStructureTransition); 77 putDirectWithoutTransition(vm, vm.propertyNames->prototype, relativeTimeFormatPrototype, PropertyAttribute::DontEnum | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly); 78 relativeTimeFormatPrototype->putDirectWithoutTransition(vm, vm.propertyNames->constructor, this, static_cast<unsigned>(PropertyAttribute::DontEnum)); 79 } 80 81 // https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat 82 JSC_DEFINE_HOST_FUNCTION(constructIntlRelativeTimeFormat, (JSGlobalObject* globalObject, CallFrame* callFrame)) 83 { 84 VM& vm = globalObject->vm(); 85 auto scope = DECLARE_THROW_SCOPE(vm); 86 87 JSObject* newTarget = asObject(callFrame->newTarget()); 88 Structure* structure = newTarget == callFrame->jsCallee() 89 ? globalObject->relativeTimeFormatStructure() 90 : InternalFunction::createSubclassStructure(globalObject, newTarget, getFunctionRealm(vm, newTarget)->relativeTimeFormatStructure()); 91 RETURN_IF_EXCEPTION(scope, { }); 92 93 IntlRelativeTimeFormat* relativeTimeFormat = IntlRelativeTimeFormat::create(vm, structure); 94 ASSERT(relativeTimeFormat); 95 96 scope.release(); 97 relativeTimeFormat->initializeRelativeTimeFormat(globalObject, callFrame->argument(0), callFrame->argument(1)); 98 return JSValue::encode(relativeTimeFormat); 99 } 100 101 // https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat 102 JSC_DEFINE_HOST_FUNCTION(callIntlRelativeTimeFormat, (JSGlobalObject* globalObject, CallFrame*)) 103 { 104 VM& vm = globalObject->vm(); 105 auto scope = DECLARE_THROW_SCOPE(vm); 106 107 return JSValue::encode(throwConstructorCannotBeCalledAsFunctionTypeError(globalObject, scope, "RelativeTimeFormat")); 108 } 109 110 // https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat.supportedLocalesOf 111 JSC_DEFINE_HOST_FUNCTION(IntlRelativeTimeFormatConstructorFuncSupportedLocalesOf, (JSGlobalObject* globalObject, CallFrame* callFrame)) 112 { 113 VM& vm = globalObject->vm(); 114 auto scope = DECLARE_THROW_SCOPE(vm); 115 116 auto& availableLocales = intlRelativeTimeFormatAvailableLocales(); 117 118 auto requestedLocales = canonicalizeLocaleList(globalObject, callFrame->argument(0)); 119 RETURN_IF_EXCEPTION(scope, encodedJSValue()); 120 121 RELEASE_AND_RETURN(scope, JSValue::encode(supportedLocales(globalObject, availableLocales, requestedLocales, callFrame->argument(1)))); 122 } 123 124 } // namespace JSC