/ runtime / IntlSegmentIterator.cpp
IntlSegmentIterator.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 "IntlSegmentIterator.h"
28  
29  #include "IteratorOperations.h"
30  #include "JSCInlines.h"
31  #include "ObjectConstructor.h"
32  #include <unicode/ucurr.h>
33  #include <unicode/uloc.h>
34  #include <wtf/unicode/icu/ICUHelpers.h>
35  
36  namespace JSC {
37  
38  const ClassInfo IntlSegmentIterator::s_info = { "Object", &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(IntlSegmentIterator) };
39  
40  IntlSegmentIterator* IntlSegmentIterator::create(VM& vm, Structure* structure, std::unique_ptr<UBreakIterator, UBreakIteratorDeleter>&& segmenter, Box<Vector<UChar>> buffer, JSString* string, IntlSegmenter::Granularity granularity)
41  {
42      auto* object = new (NotNull, allocateCell<IntlSegmentIterator>(vm.heap)) IntlSegmentIterator(vm, structure, WTFMove(segmenter), WTFMove(buffer), granularity);
43      object->finishCreation(vm, string);
44      return object;
45  }
46  
47  Structure* IntlSegmentIterator::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
48  {
49      return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
50  }
51  
52  IntlSegmentIterator::IntlSegmentIterator(VM& vm, Structure* structure, std::unique_ptr<UBreakIterator, UBreakIteratorDeleter>&& segmenter, Box<Vector<UChar>>&& buffer, IntlSegmenter::Granularity granularity)
53      : Base(vm, structure)
54      , m_segmenter(WTFMove(segmenter))
55      , m_buffer(WTFMove(buffer))
56      , m_granularity(granularity)
57  {
58  }
59  
60  void IntlSegmentIterator::finishCreation(VM& vm, JSString* string)
61  {
62      Base::finishCreation(vm);
63      ASSERT(inherits(vm, info()));
64      m_string.set(vm, this, string);
65  }
66  
67  void IntlSegmentIterator::visitChildren(JSCell* cell, SlotVisitor& visitor)
68  {
69      auto* thisObject = jsCast<IntlSegmentIterator*>(cell);
70      Base::visitChildren(thisObject, visitor);
71      visitor.append(thisObject->m_string);
72  }
73  
74  JSObject* IntlSegmentIterator::next(JSGlobalObject* globalObject)
75  {
76      VM& vm = globalObject->vm();
77      auto scope = DECLARE_THROW_SCOPE(vm);
78  
79      int32_t startIndex = ubrk_current(m_segmenter.get());
80      int32_t endIndex = ubrk_next(m_segmenter.get());
81      if (endIndex == UBRK_DONE)
82          return createIteratorResultObject(globalObject, jsUndefined(), true);
83      JSObject* object = IntlSegmenter::createSegmentDataObject(globalObject, m_string.get(), startIndex, endIndex, *m_segmenter, m_granularity);
84      RETURN_IF_EXCEPTION(scope, { });
85      return createIteratorResultObject(globalObject, object, false);
86  }
87  
88  } // namespace JSC