/ runtime / IntlRelativeTimeFormatPrototype.cpp
IntlRelativeTimeFormatPrototype.cpp
  1  /*
  2   * Copyright (C) 2020 Sony Interactive Entertainment Inc.
  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 "IntlRelativeTimeFormatPrototype.h"
 28  
 29  #include "IntlRelativeTimeFormat.h"
 30  #include "JSCInlines.h"
 31  
 32  namespace JSC {
 33  
 34  static JSC_DECLARE_HOST_FUNCTION(IntlRelativeTimeFormatPrototypeFuncFormat);
 35  static JSC_DECLARE_HOST_FUNCTION(IntlRelativeTimeFormatPrototypeFuncFormatToParts);
 36  static JSC_DECLARE_HOST_FUNCTION(IntlRelativeTimeFormatPrototypeFuncResolvedOptions);
 37  
 38  }
 39  
 40  #include "IntlRelativeTimeFormatPrototype.lut.h"
 41  
 42  namespace JSC {
 43  
 44  const ClassInfo IntlRelativeTimeFormatPrototype::s_info = { "Intl.RelativeTimeFormat", &Base::s_info, &relativeTimeFormatPrototypeTable, nullptr, CREATE_METHOD_TABLE(IntlRelativeTimeFormatPrototype) };
 45  
 46  /* Source for IntlRelativeTimeFormatPrototype.lut.h
 47  @begin relativeTimeFormatPrototypeTable
 48    format           IntlRelativeTimeFormatPrototypeFuncFormat           DontEnum|Function 2
 49    formatToParts    IntlRelativeTimeFormatPrototypeFuncFormatToParts    DontEnum|Function 2
 50    resolvedOptions  IntlRelativeTimeFormatPrototypeFuncResolvedOptions  DontEnum|Function 0
 51  @end
 52  */
 53  
 54  IntlRelativeTimeFormatPrototype* IntlRelativeTimeFormatPrototype::create(VM& vm, Structure* structure)
 55  {
 56      auto* object = new (NotNull, allocateCell<IntlRelativeTimeFormatPrototype>(vm.heap)) IntlRelativeTimeFormatPrototype(vm, structure);
 57      object->finishCreation(vm);
 58      return object;
 59  }
 60  
 61  Structure* IntlRelativeTimeFormatPrototype::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
 62  {
 63      return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
 64  }
 65  
 66  IntlRelativeTimeFormatPrototype::IntlRelativeTimeFormatPrototype(VM& vm, Structure* structure)
 67      : Base(vm, structure)
 68  {
 69  }
 70  
 71  void IntlRelativeTimeFormatPrototype::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/ecma402/#sec-Intl.RelativeTimeFormat.prototype.format
 79  JSC_DEFINE_HOST_FUNCTION(IntlRelativeTimeFormatPrototypeFuncFormat, (JSGlobalObject* globalObject, CallFrame* callFrame))
 80  {
 81      VM& vm = globalObject->vm();
 82      auto scope = DECLARE_THROW_SCOPE(vm);
 83  
 84      auto* relativeTimeFormat = jsDynamicCast<IntlRelativeTimeFormat*>(vm, callFrame->thisValue());
 85      if (!relativeTimeFormat)
 86          return JSValue::encode(throwTypeError(globalObject, scope, "Intl.RelativeTimeFormat.prototype.format called on value that's not an object initialized as a RelativeTimeFormat"_s));
 87  
 88      double value = callFrame->argument(0).toNumber(globalObject);
 89      RETURN_IF_EXCEPTION(scope, encodedJSValue());
 90  
 91      String unit = callFrame->argument(1).toWTFString(globalObject);
 92      RETURN_IF_EXCEPTION(scope, encodedJSValue());
 93  
 94      RELEASE_AND_RETURN(scope, JSValue::encode(relativeTimeFormat->format(globalObject, value, unit)));
 95  }
 96  
 97  // https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat.prototype.formatToParts
 98  JSC_DEFINE_HOST_FUNCTION(IntlRelativeTimeFormatPrototypeFuncFormatToParts, (JSGlobalObject* globalObject, CallFrame* callFrame))
 99  {
100      VM& vm = globalObject->vm();
101      auto scope = DECLARE_THROW_SCOPE(vm);
102  
103      auto* relativeTimeFormat = jsDynamicCast<IntlRelativeTimeFormat*>(vm, callFrame->thisValue());
104      if (!relativeTimeFormat)
105          return JSValue::encode(throwTypeError(globalObject, scope, "Intl.RelativeTimeFormat.prototype.formatToParts called on value that's not an object initialized as a RelativeTimeFormat"_s));
106  
107      double value = callFrame->argument(0).toNumber(globalObject);
108      RETURN_IF_EXCEPTION(scope, encodedJSValue());
109  
110      String unit = callFrame->argument(1).toWTFString(globalObject);
111      RETURN_IF_EXCEPTION(scope, encodedJSValue());
112  
113      RELEASE_AND_RETURN(scope, JSValue::encode(relativeTimeFormat->formatToParts(globalObject, value, unit)));
114  }
115  
116  // https://tc39.es/ecma402/#sec-intl.relativetimeformat.prototype.resolvedoptions
117  JSC_DEFINE_HOST_FUNCTION(IntlRelativeTimeFormatPrototypeFuncResolvedOptions, (JSGlobalObject* globalObject, CallFrame* callFrame))
118  {
119      VM& vm = globalObject->vm();
120      auto scope = DECLARE_THROW_SCOPE(vm);
121  
122      auto* relativeTimeFormat = jsDynamicCast<IntlRelativeTimeFormat*>(vm, callFrame->thisValue());
123      if (!relativeTimeFormat)
124          return JSValue::encode(throwTypeError(globalObject, scope, "Intl.RelativeTimeFormat.prototype.resolvedOptions called on value that's not an object initialized as a RelativeTimeFormat"_s));
125  
126      RELEASE_AND_RETURN(scope, JSValue::encode(relativeTimeFormat->resolvedOptions(globalObject)));
127  }
128  
129  } // namespace JSC