/* * Copyright (C) 2020 Sony Interactive Entertainment Inc. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. */ #include "config.h" #include "IntlRelativeTimeFormat.h" #include "IntlNumberFormatInlines.h" #include "IntlObjectInlines.h" #include "JSCInlines.h" #include "ObjectConstructor.h" #include namespace JSC { const ClassInfo IntlRelativeTimeFormat::s_info = { "Object", &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(IntlRelativeTimeFormat) }; namespace IntlRelativeTimeFormatInternal { } IntlRelativeTimeFormat* IntlRelativeTimeFormat::create(VM& vm, Structure* structure) { auto* format = new (NotNull, allocateCell(vm.heap)) IntlRelativeTimeFormat(vm, structure); format->finishCreation(vm); return format; } Structure* IntlRelativeTimeFormat::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype) { return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info()); } IntlRelativeTimeFormat::IntlRelativeTimeFormat(VM& vm, Structure* structure) : Base(vm, structure) { } void IntlRelativeTimeFormat::finishCreation(VM& vm) { Base::finishCreation(vm); ASSERT(inherits(vm, info())); } void IntlRelativeTimeFormat::visitChildren(JSCell* cell, SlotVisitor& visitor) { auto* thisObject = jsCast(cell); ASSERT_GC_OBJECT_INHERITS(thisObject, info()); Base::visitChildren(thisObject, visitor); } Vector IntlRelativeTimeFormat::localeData(const String& locale, RelevantExtensionKey key) { ASSERT_UNUSED(key, key == RelevantExtensionKey::Nu); return numberingSystemsForLocale(locale); } // https://tc39.es/ecma402/#sec-InitializeRelativeTimeFormat void IntlRelativeTimeFormat::initializeRelativeTimeFormat(JSGlobalObject* globalObject, JSValue locales, JSValue optionsValue) { VM& vm = globalObject->vm(); auto scope = DECLARE_THROW_SCOPE(vm); Vector requestedLocales = canonicalizeLocaleList(globalObject, locales); RETURN_IF_EXCEPTION(scope, void()); JSObject* options; if (optionsValue.isUndefined()) options = constructEmptyObject(vm, globalObject->nullPrototypeObjectStructure()); else { options = optionsValue.toObject(globalObject); RETURN_IF_EXCEPTION(scope, void()); } ResolveLocaleOptions localeOptions; LocaleMatcher localeMatcher = intlOption(globalObject, options, vm.propertyNames->localeMatcher, { { "lookup"_s, LocaleMatcher::Lookup }, { "best fit"_s, LocaleMatcher::BestFit } }, "localeMatcher must be either \"lookup\" or \"best fit\""_s, LocaleMatcher::BestFit); RETURN_IF_EXCEPTION(scope, void()); String numberingSystem = intlStringOption(globalObject, options, vm.propertyNames->numberingSystem, { }, nullptr, nullptr); RETURN_IF_EXCEPTION(scope, void()); if (!numberingSystem.isNull()) { if (!isUnicodeLocaleIdentifierType(numberingSystem)) { throwRangeError(globalObject, scope, "numberingSystem is not a well-formed numbering system value"_s); return; } localeOptions[static_cast(RelevantExtensionKey::Nu)] = numberingSystem; } const HashSet& availableLocales = intlRelativeTimeFormatAvailableLocales(); auto resolved = resolveLocale(globalObject, availableLocales, requestedLocales, localeMatcher, localeOptions, { RelevantExtensionKey::Nu }, localeData); m_locale = resolved.locale; if (m_locale.isEmpty()) { throwTypeError(globalObject, scope, "failed to initialize RelativeTimeFormat due to invalid locale"_s); return; } m_numberingSystem = resolved.extensions[static_cast(RelevantExtensionKey::Nu)]; CString dataLocaleWithExtensions = makeString(resolved.dataLocale, "-u-nu-", m_numberingSystem).utf8(); m_style = intlOption