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/ecma_vm.h"
19 #include "ecmascript/global_env.h"
20 #include "ecmascript/js_displaynames.h"
21 #include "ecmascript/js_intl.h"
22 #include "ecmascript/js_locale.h"
23 #include "ecmascript/js_object.h"
24 #include "ecmascript/object_factory.h"
25
26 namespace panda::ecmascript::builtins {
27 // 12.2.1 Intl.DisplayNames ( [ locales [ , options ] ] )
DisplayNamesConstructor(EcmaRuntimeCallInfo * argv)28 JSTaggedValue BuiltinsDisplayNames::DisplayNamesConstructor(EcmaRuntimeCallInfo *argv)
29 {
30 JSThread *thread = argv->GetThread();
31 [[maybe_unused]] EcmaHandleScope scope(thread);
32 EcmaVM *ecmaVm = thread->GetEcmaVM();
33 ObjectFactory *factory = ecmaVm->GetFactory();
34
35 // 1. If NewTarget is undefined, throw a TypeError exception.
36 JSHandle<JSTaggedValue> newTarget = GetNewTarget(argv);
37 if (newTarget->IsUndefined()) {
38 THROW_TYPE_ERROR_AND_RETURN(thread, "newTarget is undefined", JSTaggedValue::Exception());
39 }
40
41 // 2. Let displayNames be ? OrdinaryCreateFromConstructor(NewTarget, "%DisplayNames.prototype%",
42 // « [[InitializedDisplayNames]], [[Locale]], [[Style]], [[Type]], [[Fallback]], [[Fields]] »).
43 JSHandle<JSTaggedValue> constructor = GetConstructor(argv);
44 JSHandle<JSDisplayNames> displayNames =
45 JSHandle<JSDisplayNames>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), newTarget));
46 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
47
48 // 3. Perform ? InitializeDisplayNames(displayNames, locales, options).
49 JSHandle<JSTaggedValue> locales = GetCallArg(argv, 0);
50 JSHandle<JSTaggedValue> options = GetCallArg(argv, 1);
51 JSDisplayNames::InitializeDisplayNames(thread, displayNames, locales, options);
52 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
53 return displayNames.GetTaggedValue();
54 }
55
56 // 12.3.2 Intl.DisplayNames.supportedLocalesOf ( locales [ , options ] )
SupportedLocalesOf(EcmaRuntimeCallInfo * argv)57 JSTaggedValue BuiltinsDisplayNames::SupportedLocalesOf(EcmaRuntimeCallInfo *argv)
58 {
59 JSThread *thread = argv->GetThread();
60 [[maybe_unused]] EcmaHandleScope scope(thread);
61
62 // 1. Let availableLocales be %DisplayNames%.[[AvailableLocales]].
63 JSHandle<TaggedArray> availableLocales = JSDisplayNames::GetAvailableLocales(thread);
64
65 // 2. Let requestedLocales be ? CanonicaliezLocaleList(locales).
66 JSHandle<JSTaggedValue> locales = GetCallArg(argv, 0);
67 JSHandle<TaggedArray> requestedLocales = JSLocale::CanonicalizeLocaleList(thread, locales);
68 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
69
70 // 3. Return ? SupportedLocales(availableLocales, requestedLocales, options).
71 JSHandle<JSTaggedValue> options = GetCallArg(argv, 1);
72 JSHandle<JSArray> result = JSLocale::SupportedLocales(thread, availableLocales, requestedLocales, options);
73 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
74 return result.GetTaggedValue();
75 }
76
77 // 12.4.3 get Intl.DisplayNames.prototype.of
Of(EcmaRuntimeCallInfo * argv)78 JSTaggedValue BuiltinsDisplayNames::Of(EcmaRuntimeCallInfo *argv)
79 {
80 JSThread *thread = argv->GetThread();
81 [[maybe_unused]] EcmaHandleScope scope(thread);
82 // 1. Let displayNames be this value.
83 JSHandle<JSTaggedValue> thisValue = GetThis(argv);
84
85 // 2. Perform ? RequireInternalSlot(displayNames, [[InitializedDisplayNames]]).
86 if (!thisValue->IsJSDisplayNames()) {
87 THROW_TYPE_ERROR_AND_RETURN(thread, "this is not dn object", JSTaggedValue::Exception());
88 }
89
90 // 3. Let code be ? ToString(code).
91 JSHandle<JSTaggedValue> codeValue = GetCallArg(argv, 0);
92 JSHandle<EcmaString> codeTemp = JSTaggedValue::ToString(thread, codeValue);
93 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
94
95 // 4. Let code be ? CanonicalCodeForDisplayNames(displayNames.[[Type]], code).
96 // 5. Let fields be displayNames.[[Fields]].
97 // 6. If fields has a field [[<code>]], return fields.[[<code>]].
98 JSHandle<JSDisplayNames> displayNames = JSHandle<JSDisplayNames>::Cast(thisValue);
99 TypednsOption typeOpt = displayNames->GetType();
100 JSHandle<EcmaString> code = JSDisplayNames::CanonicalCodeForDisplayNames(thread, displayNames, typeOpt, codeTemp);
101 std::string codeString = JSLocale::ConvertToStdString(code);
102 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
103 if (codeString.size()) {
104 JSHandle<JSTaggedValue> codeStr = JSHandle<JSTaggedValue>::Cast(code);
105 return codeStr.GetTaggedValue();
106 }
107
108 // 7. If displayNames.[[Fallback]] is "code", return code.
109 FallbackOption fallback = displayNames->GetFallback();
110 if (fallback == FallbackOption::CODE) {
111 return codeValue.GetTaggedValue();
112 }
113 // 8. Return undefined.
114 return JSTaggedValue::Undefined();
115 }
116
117 // 12.4.4 Intl.DisplayNames.prototype.resolvedOptions ()
ResolvedOptions(EcmaRuntimeCallInfo * argv)118 JSTaggedValue BuiltinsDisplayNames::ResolvedOptions(EcmaRuntimeCallInfo *argv)
119 {
120 JSThread *thread = argv->GetThread();
121 [[maybe_unused]] EcmaHandleScope scope(thread);
122
123 // 1. Let DisplayNames be the this value.
124 JSHandle<JSTaggedValue> thisValue = GetThis(argv);
125
126 // 2. Perform ? RequireInternalSlot(DisplayNames, [[InitializedDisplayNames]]).
127 if (!thisValue->IsJSDisplayNames()) {
128 THROW_TYPE_ERROR_AND_RETURN(thread, "this is not dn object", JSTaggedValue::Exception());
129 }
130
131 // 3. Let options be ! OrdinaryObjectCreate(%ObjectPrototype%).
132 auto ecmaVm = thread->GetEcmaVM();
133 JSHandle<GlobalEnv> env = ecmaVm->GetGlobalEnv();
134 ObjectFactory *factory = ecmaVm->GetFactory();
135 JSHandle<JSFunction> ctor(env->GetObjectFunction());
136 JSHandle<JSObject> options(factory->NewJSObjectByConstructor(ctor));
137
138 // 4. For each row of Table 8, except the header row, in table order, do
139 // Let p be the Property value of the current row.
140 // Let v be the value of displayNames's internal slot whose name is the Internal Slot value of the current row.
141 // Assert: v is not undefined.
142 // Perform ! CreateDataPropertyOrThrow(options, p, v).
143 JSHandle<JSDisplayNames> displayNames = JSHandle<JSDisplayNames>::Cast(thisValue);
144 JSDisplayNames::ResolvedOptions(thread, displayNames, options);
145 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
146
147 // 5. Return options.
148 return options.GetTaggedValue();
149 }
150 } // namespace panda::ecmascript::builtins