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