IntlPluralRulesPrototype.cpp
1 /* 2 * Copyright (C) 2018 Andy VanWagoner (andy@vanwagoner.family) 3 * Copyright (C) 2019 Apple Inc. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' 15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 16 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS 18 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 24 * THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include "config.h" 28 #include "IntlPluralRulesPrototype.h" 29 30 #include "IntlPluralRules.h" 31 #include "JSCInlines.h" 32 33 namespace JSC { 34 35 static JSC_DECLARE_HOST_FUNCTION(IntlPluralRulesPrototypeFuncSelect); 36 static JSC_DECLARE_HOST_FUNCTION(IntlPluralRulesPrototypeFuncResolvedOptions); 37 38 } 39 40 #include "IntlPluralRulesPrototype.lut.h" 41 42 namespace JSC { 43 44 const ClassInfo IntlPluralRulesPrototype::s_info = { "Intl.PluralRules", &Base::s_info, &pluralRulesPrototypeTable, nullptr, CREATE_METHOD_TABLE(IntlPluralRulesPrototype) }; 45 46 /* Source for IntlPluralRulesPrototype.lut.h 47 @begin pluralRulesPrototypeTable 48 select IntlPluralRulesPrototypeFuncSelect DontEnum|Function 1 49 resolvedOptions IntlPluralRulesPrototypeFuncResolvedOptions DontEnum|Function 0 50 @end 51 */ 52 53 IntlPluralRulesPrototype* IntlPluralRulesPrototype::create(VM& vm, JSGlobalObject*, Structure* structure) 54 { 55 IntlPluralRulesPrototype* object = new (NotNull, allocateCell<IntlPluralRulesPrototype>(vm.heap)) IntlPluralRulesPrototype(vm, structure); 56 object->finishCreation(vm); 57 return object; 58 } 59 60 Structure* IntlPluralRulesPrototype::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype) 61 { 62 return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info()); 63 } 64 65 IntlPluralRulesPrototype::IntlPluralRulesPrototype(VM& vm, Structure* structure) 66 : Base(vm, structure) 67 { 68 } 69 70 void IntlPluralRulesPrototype::finishCreation(VM& vm) 71 { 72 Base::finishCreation(vm); 73 ASSERT(inherits(vm, info())); 74 JSC_TO_STRING_TAG_WITHOUT_TRANSITION(); 75 } 76 77 JSC_DEFINE_HOST_FUNCTION(IntlPluralRulesPrototypeFuncSelect, (JSGlobalObject* globalObject, CallFrame* callFrame)) 78 { 79 VM& vm = globalObject->vm(); 80 auto scope = DECLARE_THROW_SCOPE(vm); 81 82 // 13.4.3 Intl.PluralRules.prototype.select (value) 83 // https://tc39.github.io/ecma402/#sec-intl.pluralrules.prototype.select 84 IntlPluralRules* pluralRules = jsDynamicCast<IntlPluralRules*>(vm, callFrame->thisValue()); 85 86 if (!pluralRules) 87 return JSValue::encode(throwTypeError(globalObject, scope, "Intl.PluralRules.prototype.select called on value that's not an object initialized as a PluralRules"_s)); 88 89 double value = callFrame->argument(0).toNumber(globalObject); 90 RETURN_IF_EXCEPTION(scope, encodedJSValue()); 91 92 RELEASE_AND_RETURN(scope, JSValue::encode(pluralRules->select(globalObject, value))); 93 } 94 95 JSC_DEFINE_HOST_FUNCTION(IntlPluralRulesPrototypeFuncResolvedOptions, (JSGlobalObject* globalObject, CallFrame* callFrame)) 96 { 97 VM& vm = globalObject->vm(); 98 auto scope = DECLARE_THROW_SCOPE(vm); 99 100 // 13.4.4 Intl.PluralRules.prototype.resolvedOptions () 101 // https://tc39.github.io/ecma402/#sec-intl.pluralrules.prototype.resolvedoptions 102 IntlPluralRules* pluralRules = jsDynamicCast<IntlPluralRules*>(vm, callFrame->thisValue()); 103 104 if (!pluralRules) 105 return JSValue::encode(throwTypeError(globalObject, scope, "Intl.PluralRules.prototype.resolvedOptions called on value that's not an object initialized as a PluralRules"_s)); 106 107 RELEASE_AND_RETURN(scope, JSValue::encode(pluralRules->resolvedOptions(globalObject))); 108 } 109 110 } // namespace JSC