IntlSegmenterPrototype.cpp
1 /* 2 * Copyright (C) 2020 Apple Inc. All rights reserved. 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 "IntlSegmenterPrototype.h" 28 29 #include "IntlSegmenter.h" 30 #include "JSCInlines.h" 31 32 namespace JSC { 33 34 static JSC_DECLARE_HOST_FUNCTION(IntlSegmenterPrototypeFuncSegment); 35 static JSC_DECLARE_HOST_FUNCTION(IntlSegmenterPrototypeFuncResolvedOptions); 36 37 } 38 39 #include "IntlSegmenterPrototype.lut.h" 40 41 namespace JSC { 42 43 const ClassInfo IntlSegmenterPrototype::s_info = { "Intl.Segmenter", &Base::s_info, &segmenterPrototypeTable, nullptr, CREATE_METHOD_TABLE(IntlSegmenterPrototype) }; 44 45 /* Source for IntlSegmenterPrototype.lut.h 46 @begin segmenterPrototypeTable 47 segment IntlSegmenterPrototypeFuncSegment DontEnum|Function 1 48 resolvedOptions IntlSegmenterPrototypeFuncResolvedOptions DontEnum|Function 0 49 @end 50 */ 51 52 IntlSegmenterPrototype* IntlSegmenterPrototype::create(VM& vm, Structure* structure) 53 { 54 auto* object = new (NotNull, allocateCell<IntlSegmenterPrototype>(vm.heap)) IntlSegmenterPrototype(vm, structure); 55 object->finishCreation(vm); 56 return object; 57 } 58 59 Structure* IntlSegmenterPrototype::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype) 60 { 61 return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info()); 62 } 63 64 IntlSegmenterPrototype::IntlSegmenterPrototype(VM& vm, Structure* structure) 65 : Base(vm, structure) 66 { 67 } 68 69 void IntlSegmenterPrototype::finishCreation(VM& vm) 70 { 71 Base::finishCreation(vm); 72 ASSERT(inherits(vm, info())); 73 JSC_TO_STRING_TAG_WITHOUT_TRANSITION(); 74 } 75 76 // https://tc39.es/proposal-intl-segmenter/#sec-intl.segmenter.prototype.segment 77 JSC_DEFINE_HOST_FUNCTION(IntlSegmenterPrototypeFuncSegment, (JSGlobalObject* globalObject, CallFrame* callFrame)) 78 { 79 VM& vm = globalObject->vm(); 80 auto scope = DECLARE_THROW_SCOPE(vm); 81 82 auto* segmenter = jsDynamicCast<IntlSegmenter*>(vm, callFrame->thisValue()); 83 if (!segmenter) 84 return throwVMTypeError(globalObject, scope, "Intl.Segmenter.prototype.segment called on value that's not an object initialized as a Segmenter"_s); 85 86 RELEASE_AND_RETURN(scope, JSValue::encode(segmenter->segment(globalObject, callFrame->argument(0)))); 87 } 88 89 // https://tc39.es/proposal-intl-segmenter/#sec-Intl.Segmenter.prototype.resolvedOptions 90 JSC_DEFINE_HOST_FUNCTION(IntlSegmenterPrototypeFuncResolvedOptions, (JSGlobalObject* globalObject, CallFrame* callFrame)) 91 { 92 VM& vm = globalObject->vm(); 93 auto scope = DECLARE_THROW_SCOPE(vm); 94 95 auto* segmenter = jsDynamicCast<IntlSegmenter*>(vm, callFrame->thisValue()); 96 if (!segmenter) 97 return throwVMTypeError(globalObject, scope, "Intl.Segmenter.prototype.resolvedOptions called on value that's not an object initialized as a Segmenter"_s); 98 99 RELEASE_AND_RETURN(scope, JSValue::encode(segmenter->resolvedOptions(globalObject))); 100 } 101 102 } // namespace JSC