• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "builtins_displaynames.h"
17 
18 #include "ecmascript/intl/locale_helper.h"
19 #include "ecmascript/js_displaynames.h"
20 namespace panda::ecmascript::builtins {
21 // 12.2.1 Intl.DisplayNames ( [ locales [ , options ] ] )
DisplayNamesConstructor(EcmaRuntimeCallInfo * argv)22 JSTaggedValue BuiltinsDisplayNames::DisplayNamesConstructor(EcmaRuntimeCallInfo *argv)
23 {
24     JSThread *thread = argv->GetThread();
25     BUILTINS_API_TRACE(thread, DisplayNames, Constructor);
26     [[maybe_unused]] EcmaHandleScope scope(thread);
27     EcmaVM *ecmaVm = thread->GetEcmaVM();
28     ObjectFactory *factory = ecmaVm->GetFactory();
29 
30     // 1. If NewTarget is undefined, throw a TypeError exception.
31     JSHandle<JSTaggedValue> newTarget = GetNewTarget(argv);
32     if (newTarget->IsUndefined()) {
33         THROW_TYPE_ERROR_AND_RETURN(thread, "newTarget is undefined", JSTaggedValue::Exception());
34     }
35 
36     // 2. Let displayNames be ? OrdinaryCreateFromConstructor(NewTarget, "%DisplayNames.prototype%",
37     // « [[InitializedDisplayNames]], [[Locale]], [[Style]], [[Type]], [[Fallback]], [[Fields]] »).
38     JSHandle<JSTaggedValue> constructor = GetConstructor(argv);
39     JSHandle<JSObject> newObject = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), newTarget);
40     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
41     JSHandle<JSDisplayNames> displayNames = JSHandle<JSDisplayNames>::Cast(newObject);
42 
43     // 3. Perform ? InitializeDisplayNames(displayNames, locales, options).
44     JSHandle<JSTaggedValue> locales = GetCallArg(argv, 0);
45     JSHandle<JSTaggedValue> options = GetCallArg(argv, 1);
46     JSDisplayNames::InitializeDisplayNames(thread, displayNames, locales, options);
47     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
48     return displayNames.GetTaggedValue();
49 }
50 
51 // 12.3.2 Intl.DisplayNames.supportedLocalesOf ( locales [ , options ] )
SupportedLocalesOf(EcmaRuntimeCallInfo * argv)52 JSTaggedValue BuiltinsDisplayNames::SupportedLocalesOf(EcmaRuntimeCallInfo *argv)
53 {
54     JSThread *thread = argv->GetThread();
55     BUILTINS_API_TRACE(thread, DisplayNames, SupportedLocalesOf);
56     [[maybe_unused]] EcmaHandleScope scope(thread);
57 
58     // 1. Let availableLocales be %DisplayNames%.[[AvailableLocales]].
59     JSHandle<TaggedArray> availableLocales = JSDisplayNames::GetAvailableLocales(thread);
60 
61     // 2. Let requestedLocales be ? CanonicaliezLocaleList(locales).
62     JSHandle<JSTaggedValue> locales = GetCallArg(argv, 0);
63     JSHandle<TaggedArray> requestedLocales = intl::LocaleHelper::CanonicalizeLocaleList(thread, locales);
64     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
65 
66     // 3. Return ? SupportedLocales(availableLocales, requestedLocales, options).
67     JSHandle<JSTaggedValue> options = GetCallArg(argv, 1);
68     JSHandle<JSArray> result = JSLocale::SupportedLocales(thread, availableLocales, requestedLocales, options);
69     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
70     return result.GetTaggedValue();
71 }
72 
73 // 12.4.3 get Intl.DisplayNames.prototype.of
Of(EcmaRuntimeCallInfo * argv)74 JSTaggedValue BuiltinsDisplayNames::Of(EcmaRuntimeCallInfo *argv)
75 {
76     JSThread *thread = argv->GetThread();
77     BUILTINS_API_TRACE(thread, DisplayNames, Of);
78     [[maybe_unused]] EcmaHandleScope scope(thread);
79     // 1. Let displayNames be this value.
80     JSHandle<JSTaggedValue> thisValue = GetThis(argv);
81 
82     // 2. Perform ? RequireInternalSlot(displayNames, [[InitializedDisplayNames]]).
83     if (!thisValue->IsJSDisplayNames()) {
84         THROW_TYPE_ERROR_AND_RETURN(thread, "this is not dn object", JSTaggedValue::Exception());
85     }
86 
87     // 3. Let code be ? ToString(code).
88     JSHandle<JSTaggedValue> codeValue = GetCallArg(argv, 0);
89     JSHandle<EcmaString> codeTemp = JSTaggedValue::ToString(thread, codeValue);
90     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
91 
92     // 4. Let code be ? CanonicalCodeForDisplayNames(displayNames.[[Type]], code).
93     // 5. Let fields be displayNames.[[Fields]].
94     // 6. If fields has a field [[<code>]], return fields.[[<code>]].
95     JSHandle<JSDisplayNames> displayNames = JSHandle<JSDisplayNames>::Cast(thisValue);
96     TypednsOption typeOpt = displayNames->GetType();
97     JSHandle<EcmaString> code = JSDisplayNames::CanonicalCodeForDisplayNames(thread, displayNames, typeOpt, codeTemp);
98     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
99     std::string codeString = intl::LocaleHelper::ConvertToStdString(thread, code);
100     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
101     if (codeString.size()) {
102         JSHandle<JSTaggedValue> codeStr = JSHandle<JSTaggedValue>::Cast(code);
103         return codeStr.GetTaggedValue();
104     }
105 
106     // 7. If displayNames.[[Fallback]] is "code", return code.
107     FallbackOption fallback = displayNames->GetFallback();
108     if (fallback == FallbackOption::CODE) {
109         return codeValue.GetTaggedValue();
110     }
111     // 8. Return undefined.
112     return JSTaggedValue::Undefined();
113 }
114 
115 // 12.4.4 Intl.DisplayNames.prototype.resolvedOptions ()
ResolvedOptions(EcmaRuntimeCallInfo * argv)116 JSTaggedValue BuiltinsDisplayNames::ResolvedOptions(EcmaRuntimeCallInfo *argv)
117 {
118     JSThread *thread = argv->GetThread();
119     BUILTINS_API_TRACE(thread, DisplayNames, ResolvedOptions);
120     [[maybe_unused]] EcmaHandleScope scope(thread);
121 
122     // 1. Let DisplayNames be the this value.
123     JSHandle<JSTaggedValue> thisValue = GetThis(argv);
124 
125     // 2. Perform ? RequireInternalSlot(DisplayNames, [[InitializedDisplayNames]]).
126     if (!thisValue->IsJSDisplayNames()) {
127         THROW_TYPE_ERROR_AND_RETURN(thread, "this is not dn object", JSTaggedValue::Exception());
128     }
129 
130     // 3. Let options be ! OrdinaryObjectCreate(%ObjectPrototype%).
131     auto ecmaVm = thread->GetEcmaVM();
132     JSHandle<GlobalEnv> env = ecmaVm->GetGlobalEnv();
133     ObjectFactory *factory = ecmaVm->GetFactory();
134     JSHandle<JSFunction> ctor(env->GetObjectFunction());
135     JSHandle<JSObject> options(factory->NewJSObjectByConstructor(ctor));
136 
137     // 4. For each row of Table 8, except the header row, in table order, do
138     // Let p be the Property value of the current row.
139     // Let v be the value of displayNames's internal slot whose name is the Internal Slot value of the current row.
140     // Assert: v is not undefined.
141     // Perform ! CreateDataPropertyOrThrow(options, p, v).
142     JSHandle<JSDisplayNames> displayNames = JSHandle<JSDisplayNames>::Cast(thisValue);
143     JSDisplayNames::ResolvedOptions(thread, displayNames, options);
144     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
145 
146     // 5. Return options.
147     return options.GetTaggedValue();
148 }
149 }  // namespace panda::ecmascript::builtins