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