1 /*
2 * Copyright (c) 2021 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 "ecmascript/builtins.h"
17
18 #include "ecmascript/base/error_type.h"
19 #include "ecmascript/base/number_helper.h"
20 #include "ecmascript/builtins/builtins_ark_tools.h"
21 #include "ecmascript/builtins/builtins_array.h"
22 #include "ecmascript/builtins/builtins_arraybuffer.h"
23 #include "ecmascript/builtins/builtins_async_function.h"
24 #include "ecmascript/builtins/builtins_bigint.h"
25 #include "ecmascript/builtins/builtins_boolean.h"
26 #include "ecmascript/builtins/builtins_collator.h"
27 #include "ecmascript/builtins/builtins_dataview.h"
28 #include "ecmascript/builtins/builtins_date.h"
29 #include "ecmascript/builtins/builtins_date_time_format.h"
30 #include "ecmascript/builtins/builtins_errors.h"
31 #include "ecmascript/builtins/builtins_function.h"
32 #include "ecmascript/builtins/builtins_generator.h"
33 #include "ecmascript/builtins/builtins_global.h"
34 #include "ecmascript/builtins/builtins_intl.h"
35 #include "ecmascript/builtins/builtins_iterator.h"
36 #include "ecmascript/builtins/builtins_json.h"
37 #include "ecmascript/builtins/builtins_locale.h"
38 #include "ecmascript/builtins/builtins_map.h"
39 #include "ecmascript/builtins/builtins_math.h"
40 #include "ecmascript/builtins/builtins_number.h"
41 #include "ecmascript/builtins/builtins_number_format.h"
42 #include "ecmascript/builtins/builtins_object.h"
43 #include "ecmascript/builtins/builtins_plural_rules.h"
44 #include "ecmascript/builtins/builtins_promise.h"
45 #include "ecmascript/builtins/builtins_promise_handler.h"
46 #include "ecmascript/builtins/builtins_promise_job.h"
47 #include "ecmascript/builtins/builtins_proxy.h"
48 #include "ecmascript/builtins/builtins_reflect.h"
49 #include "ecmascript/builtins/builtins_regexp.h"
50 #include "ecmascript/builtins/builtins_relative_time_format.h"
51 #include "ecmascript/builtins/builtins_set.h"
52 #include "ecmascript/builtins/builtins_string.h"
53 #include "ecmascript/builtins/builtins_string_iterator.h"
54 #include "ecmascript/builtins/builtins_symbol.h"
55 #include "ecmascript/builtins/builtins_typedarray.h"
56 #include "ecmascript/builtins/builtins_weak_map.h"
57 #include "ecmascript/builtins/builtins_weak_set.h"
58 #include "ecmascript/containers/containers_private.h"
59 #include "ecmascript/ecma_runtime_call_info.h"
60 #include "ecmascript/js_array.h"
61 #include "ecmascript/js_arraybuffer.h"
62 #include "ecmascript/js_api_arraylist.h"
63 #include "ecmascript/js_array_iterator.h"
64 #include "ecmascript/js_async_function.h"
65 #include "ecmascript/js_collator.h"
66 #include "ecmascript/js_dataview.h"
67 #include "ecmascript/js_date_time_format.h"
68 #include "ecmascript/js_for_in_iterator.h"
69 #include "ecmascript/js_function.h"
70 #include "ecmascript/js_handle.h"
71 #include "ecmascript/js_hclass.h"
72 #include "ecmascript/js_locale.h"
73 #include "ecmascript/js_map.h"
74 #include "ecmascript/js_map_iterator.h"
75 #include "ecmascript/js_number_format.h"
76 #include "ecmascript/js_plural_rules.h"
77 #include "ecmascript/js_primitive_ref.h"
78 #include "ecmascript/js_promise.h"
79 #include "ecmascript/js_regexp.h"
80 #include "ecmascript/js_relative_time_format.h"
81 #include "ecmascript/js_runtime_options.h"
82 #include "ecmascript/js_set.h"
83 #include "ecmascript/js_set_iterator.h"
84 #include "ecmascript/js_string_iterator.h"
85 #include "ecmascript/js_tagged_value.h"
86 #include "ecmascript/js_typed_array.h"
87 #include "ecmascript/js_weak_container.h"
88 #include "ecmascript/mem/mem.h"
89 #include "ecmascript/object_factory.h"
90 #include "ohos/init_data.h"
91
92 namespace panda::ecmascript {
93 using Number = builtins::BuiltinsNumber;
94 using BuiltinsBigInt = builtins::BuiltinsBigInt;
95 using Object = builtins::BuiltinsObject;
96 using Date = builtins::BuiltinsDate;
97 using Symbol = builtins::BuiltinsSymbol;
98 using Boolean = builtins::BuiltinsBoolean;
99 using BuiltinsMap = builtins::BuiltinsMap;
100 using BuiltinsSet = builtins::BuiltinsSet;
101 using BuiltinsWeakMap = builtins::BuiltinsWeakMap;
102 using BuiltinsWeakSet = builtins::BuiltinsWeakSet;
103 using BuiltinsArray = builtins::BuiltinsArray;
104 using BuiltinsTypedArray = builtins::BuiltinsTypedArray;
105 using BuiltinsIterator = builtins::BuiltinsIterator;
106
107 using Error = builtins::BuiltinsError;
108 using RangeError = builtins::BuiltinsRangeError;
109 using ReferenceError = builtins::BuiltinsReferenceError;
110 using TypeError = builtins::BuiltinsTypeError;
111 using URIError = builtins::BuiltinsURIError;
112 using SyntaxError = builtins::BuiltinsSyntaxError;
113 using EvalError = builtins::BuiltinsEvalError;
114 using ErrorType = base::ErrorType;
115 using Global = builtins::BuiltinsGlobal;
116 using BuiltinsString = builtins::BuiltinsString;
117 using StringIterator = builtins::BuiltinsStringIterator;
118 using RegExp = builtins::BuiltinsRegExp;
119 using Function = builtins::BuiltinsFunction;
120 using Math = builtins::BuiltinsMath;
121 using ArrayBuffer = builtins::BuiltinsArrayBuffer;
122 using Json = builtins::BuiltinsJson;
123 using Proxy = builtins::BuiltinsProxy;
124 using Reflect = builtins::BuiltinsReflect;
125 using AsyncFunction = builtins::BuiltinsAsyncFunction;
126 using GeneratorObject = builtins::BuiltinsGenerator;
127 using Promise = builtins::BuiltinsPromise;
128 using BuiltinsPromiseHandler = builtins::BuiltinsPromiseHandler;
129 using BuiltinsPromiseJob = builtins::BuiltinsPromiseJob;
130 using ErrorType = base::ErrorType;
131 using DataView = builtins::BuiltinsDataView;
132 using Intl = builtins::BuiltinsIntl;
133 using Locale = builtins::BuiltinsLocale;
134 using DateTimeFormat = builtins::BuiltinsDateTimeFormat;
135 using RelativeTimeFormat = builtins::BuiltinsRelativeTimeFormat;
136 using NumberFormat = builtins::BuiltinsNumberFormat;
137 using Collator = builtins::BuiltinsCollator;
138 using PluralRules = builtins::BuiltinsPluralRules;
139 using ContainersPrivate = containers::ContainersPrivate;
140
Initialize(const JSHandle<GlobalEnv> & env,JSThread * thread)141 void Builtins::Initialize(const JSHandle<GlobalEnv> &env, JSThread *thread)
142 {
143 thread_ = thread;
144 vm_ = thread->GetEcmaVM();
145 factory_ = vm_->GetFactory();
146 [[maybe_unused]] EcmaHandleScope scope(thread_);
147 JSHandle<JSTaggedValue> nullHandle(thread, JSTaggedValue::Null());
148
149 // Object.prototype[dynclass]
150 JSHandle<JSHClass> objPrototypeDynclass = factory_->NewEcmaDynClass(JSObject::SIZE, JSType::JS_OBJECT, nullHandle);
151
152 // Object.prototype
153 JSHandle<JSObject> objFuncPrototype = factory_->NewJSObject(objPrototypeDynclass);
154 JSHandle<JSTaggedValue> objFuncPrototypeVal(objFuncPrototype);
155
156 // Object.prototype_or_dynclass
157 JSHandle<JSHClass> objFuncDynclass =
158 factory_->NewEcmaDynClass(JSObject::SIZE, JSType::JS_OBJECT, objFuncPrototypeVal);
159
160 // GLobalObject.prototype_or_dynclass
161 JSHandle<JSHClass> globalObjFuncDynclass =
162 factory_->NewEcmaDynClass(JSObject::SIZE, JSType::JS_GLOBAL_OBJECT, 0);
163 globalObjFuncDynclass->SetPrototype(thread_, objFuncPrototypeVal.GetTaggedValue());
164 globalObjFuncDynclass->SetIsDictionaryMode(true);
165 // Function.prototype_or_dynclass
166 JSHandle<JSHClass> emptyFuncDynclass(
167 factory_->NewEcmaDynClass(JSFunction::SIZE, JSType::JS_FUNCTION, objFuncPrototypeVal));
168
169 // PrimitiveRef.prototype_or_dynclass
170 JSHandle<JSHClass> primRefObjDynclass =
171 factory_->NewEcmaDynClass(JSPrimitiveRef::SIZE, JSType::JS_PRIMITIVE_REF, objFuncPrototypeVal);
172
173 // init global object
174 JSHandle<JSObject> globalObject = factory_->NewNonMovableJSObject(globalObjFuncDynclass);
175 JSHandle<JSHClass> newGlobalDynclass = JSHClass::Clone(thread_, globalObjFuncDynclass);
176 globalObject->SetClass(newGlobalDynclass);
177 env->SetJSGlobalObject(thread_, globalObject);
178
179 // initialize Function, forbidden change order
180 InitializeFunction(env, emptyFuncDynclass);
181
182 JSHandle<JSObject> objFuncInstancePrototype = factory_->NewJSObject(objFuncDynclass);
183 JSHandle<JSTaggedValue> objFuncInstancePrototypeValue(objFuncInstancePrototype);
184 JSHandle<JSHClass> asyncFuncClass = factory_->CreateFunctionClass(
185 FunctionKind::ASYNC_FUNCTION, JSAsyncFunction::SIZE, JSType::JS_ASYNC_FUNCTION, objFuncInstancePrototypeValue);
186 env->SetAsyncFunctionClass(thread_, asyncFuncClass);
187
188 JSHandle<JSHClass> asyncAwaitStatusFuncClass =
189 factory_->CreateFunctionClass(FunctionKind::NORMAL_FUNCTION, JSAsyncAwaitStatusFunction::SIZE,
190 JSType::JS_ASYNC_AWAIT_STATUS_FUNCTION, env->GetFunctionPrototype());
191 env->SetAsyncAwaitStatusFunctionClass(thread_, asyncAwaitStatusFuncClass);
192
193 JSHandle<JSHClass> promiseReactionFuncClass = factory_->NewEcmaDynClass(
194 JSPromiseReactionsFunction::SIZE, JSType::JS_PROMISE_REACTIONS_FUNCTION, env->GetFunctionPrototype());
195 promiseReactionFuncClass->SetCallable(true);
196 promiseReactionFuncClass->SetExtensible(true);
197 env->SetPromiseReactionFunctionClass(thread_, promiseReactionFuncClass);
198
199 JSHandle<JSHClass> promiseExecutorFuncClass = factory_->NewEcmaDynClass(
200 JSPromiseExecutorFunction::SIZE, JSType::JS_PROMISE_EXECUTOR_FUNCTION, env->GetFunctionPrototype());
201 promiseExecutorFuncClass->SetCallable(true);
202 promiseExecutorFuncClass->SetExtensible(true);
203 env->SetPromiseExecutorFunctionClass(thread_, promiseExecutorFuncClass);
204
205 JSHandle<JSHClass> promiseAllResolveElementFunctionClass =
206 factory_->NewEcmaDynClass(JSPromiseAllResolveElementFunction::SIZE,
207 JSType::JS_PROMISE_ALL_RESOLVE_ELEMENT_FUNCTION, env->GetFunctionPrototype());
208 promiseAllResolveElementFunctionClass->SetCallable(true);
209 promiseAllResolveElementFunctionClass->SetExtensible(true);
210 env->SetPromiseAllResolveElementFunctionClass(thread_, promiseAllResolveElementFunctionClass);
211
212 JSHandle<JSHClass> proxyRevocFuncClass = factory_->NewEcmaDynClass(
213 JSProxyRevocFunction::SIZE, JSType::JS_PROXY_REVOC_FUNCTION, env->GetFunctionPrototype());
214 proxyRevocFuncClass->SetCallable(true);
215 proxyRevocFuncClass->SetExtensible(true);
216 env->SetProxyRevocFunctionClass(thread_, proxyRevocFuncClass);
217
218 // Object = new Function()
219 JSHandle<JSObject> objectFunction(
220 NewBuiltinConstructor(env, objFuncPrototype, Object::ObjectConstructor, "Object", FunctionLength::ONE));
221 objectFunction.GetObject<JSFunction>()->SetBuiltinsCtorMode();
222 objectFunction.GetObject<JSFunction>()->SetFunctionPrototype(thread_, objFuncDynclass.GetTaggedValue());
223 // initialize object method.
224 env->SetObjectFunction(thread_, objectFunction);
225 env->SetObjectFunctionPrototype(thread_, objFuncPrototype);
226
227 JSHandle<JSHClass> functionClass = factory_->CreateFunctionClass(FunctionKind::BASE_CONSTRUCTOR, JSFunction::SIZE,
228 JSType::JS_FUNCTION, env->GetFunctionPrototype());
229 env->SetFunctionClassWithProto(thread_, functionClass);
230 functionClass = factory_->CreateFunctionClass(FunctionKind::NORMAL_FUNCTION, JSFunction::SIZE, JSType::JS_FUNCTION,
231 env->GetFunctionPrototype());
232 env->SetFunctionClassWithoutProto(thread_, functionClass);
233 functionClass = factory_->CreateFunctionClass(FunctionKind::CLASS_CONSTRUCTOR, JSFunction::SIZE,
234 JSType::JS_FUNCTION, env->GetFunctionPrototype());
235 env->SetFunctionClassWithoutName(thread_, functionClass);
236
237 if (env == vm_->GetGlobalEnv()) {
238 InitializeAllTypeError(env, objFuncDynclass);
239 InitializeSymbol(env, primRefObjDynclass);
240 } else {
241 // error and symbol need to be shared when initialize realm
242 InitializeAllTypeErrorWithRealm(env);
243 InitializeSymbolWithRealm(env, primRefObjDynclass);
244 }
245
246 InitializeNumber(env, globalObject, primRefObjDynclass);
247 InitializeBigInt(env, objFuncDynclass);
248 InitializeDate(env, objFuncDynclass);
249 InitializeObject(env, objFuncPrototype, objectFunction);
250 InitializeBoolean(env, primRefObjDynclass);
251
252 InitializeRegExp(env);
253 InitializeSet(env, objFuncDynclass);
254 InitializeMap(env, objFuncDynclass);
255 InitializeWeakMap(env, objFuncDynclass);
256 InitializeWeakSet(env, objFuncDynclass);
257 InitializeArray(env, objFuncPrototypeVal);
258 InitializeTypedArray(env, objFuncDynclass);
259 InitializeString(env, primRefObjDynclass);
260 InitializeArrayBuffer(env, objFuncDynclass);
261 InitializeDataView(env, objFuncDynclass);
262
263 JSHandle<JSHClass> argumentsDynclass = factory_->CreateJSArguments();
264 env->SetArgumentsClass(thread_, argumentsDynclass);
265 SetArgumentsSharedAccessor(env);
266
267 InitializeGlobalObject(env, globalObject);
268 InitializeMath(env, objFuncPrototypeVal);
269 InitializeJson(env, objFuncPrototypeVal);
270 InitializeIterator(env, objFuncDynclass);
271 InitializeProxy(env);
272 InitializeReflect(env, objFuncPrototypeVal);
273 InitializeAsyncFunction(env, objFuncDynclass);
274 InitializeGenerator(env, objFuncDynclass);
275 InitializeGeneratorFunction(env, objFuncDynclass);
276 InitializePromise(env, objFuncDynclass);
277 InitializePromiseJob(env);
278
279 JSRuntimeOptions options = vm_->GetJSOptions();
280 std::string icuPath = options.GetIcuDataPath();
281 if (icuPath == "default") {
282 SetHwIcuDirectory();
283 } else {
284 u_setDataDirectory(icuPath.c_str());
285 }
286 InitializeIntl(env, objFuncPrototypeVal);
287 InitializeLocale(env);
288 InitializeDateTimeFormat(env);
289 InitializeNumberFormat(env);
290 InitializeRelativeTimeFormat(env);
291 InitializeCollator(env);
292 InitializePluralRules(env);
293
294 JSHandle<JSHClass> generatorFuncClass =
295 factory_->CreateFunctionClass(FunctionKind::GENERATOR_FUNCTION, JSFunction::SIZE, JSType::JS_GENERATOR_FUNCTION,
296 env->GetGeneratorFunctionPrototype());
297 env->SetGeneratorFunctionClass(thread_, generatorFuncClass);
298 env->SetObjectFunctionPrototypeClass(thread_, JSTaggedValue(objFuncPrototype->GetClass()));
299 thread_->ResetGuardians();
300 }
301
InitializeGlobalObject(const JSHandle<GlobalEnv> & env,const JSHandle<JSObject> & globalObject)302 void Builtins::InitializeGlobalObject(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &globalObject)
303 {
304 [[maybe_unused]] EcmaHandleScope scope(thread_);
305
306 // Global object test
307 SetFunction(env, globalObject, "print", Global::PrintEntrypoint, 0);
308 #if ECMASCRIPT_ENABLE_RUNTIME_STAT
309 SetFunction(env, globalObject, "startRuntimeStat", Global::StartRuntimeStat, 0);
310 SetFunction(env, globalObject, "stopRuntimeStat", Global::StopRuntimeStat, 0);
311 #endif
312
313 JSRuntimeOptions options = vm_->GetJSOptions();
314 if (options.IsEnableArkTools()) {
315 JSHandle<JSTaggedValue> arkTools(InitializeArkTools(env));
316 SetConstantObject(globalObject, "ArkTools", arkTools);
317 }
318
319 #if ECMASCRIPT_ENABLE_ARK_CONTAINER
320 // Set ArkPrivate
321 JSHandle<JSTaggedValue> arkPrivate(InitializeArkPrivate(env));
322 SetConstantObject(globalObject, "ArkPrivate", arkPrivate);
323 #endif
324
325 // Global object function
326 SetFunction(env, globalObject, "eval", Global::NotSupportEval, FunctionLength::ONE);
327 SetFunction(env, globalObject, "isFinite", Global::IsFinite, FunctionLength::ONE);
328 SetFunction(env, globalObject, "isNaN", Global::IsNaN, FunctionLength::ONE);
329 SetFunction(env, globalObject, "decodeURI", Global::DecodeURI, FunctionLength::ONE);
330 SetFunction(env, globalObject, "encodeURI", Global::EncodeURI, FunctionLength::ONE);
331 SetFunction(env, globalObject, "decodeURIComponent", Global::DecodeURIComponent, FunctionLength::ONE);
332 SetFunction(env, globalObject, "encodeURIComponent", Global::EncodeURIComponent, FunctionLength::ONE);
333
334 // Global object property
335 SetGlobalThis(globalObject, "globalThis", JSHandle<JSTaggedValue>::Cast(globalObject));
336 SetConstant(globalObject, "Infinity", JSTaggedValue(base::POSITIVE_INFINITY));
337 SetConstant(globalObject, "NaN", JSTaggedValue(base::NAN_VALUE));
338 SetConstant(globalObject, "undefined", JSTaggedValue::Undefined());
339 }
340
InitializeFunction(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & emptyFuncDynclass) const341 void Builtins::InitializeFunction(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &emptyFuncDynclass) const
342 {
343 [[maybe_unused]] EcmaHandleScope scope(thread_);
344 // Initialize Function.prototype
345 JSMethod *invokeSelf =
346 vm_->GetMethodForNativeFunction(reinterpret_cast<void *>(Function::FunctionPrototypeInvokeSelf));
347 JSHandle<JSFunction> funcFuncPrototype = factory_->NewJSFunctionByDynClass(invokeSelf, emptyFuncDynclass);
348 // ecma 19.2.3 The value of the name property of the Function prototype object is the empty String.
349 JSHandle<JSTaggedValue> emptyString(thread_->GlobalConstants()->GetHandledEmptyString());
350 JSHandle<JSTaggedValue> undefinedString(thread_, JSTaggedValue::Undefined());
351 JSFunction::SetFunctionName(thread_, JSHandle<JSFunctionBase>(funcFuncPrototype), emptyString, undefinedString);
352 // ecma 19.2.3 The value of the length property of the Function prototype object is 0.
353 JSFunction::SetFunctionLength(thread_, funcFuncPrototype, JSTaggedValue(FunctionLength::ZERO));
354
355 JSHandle<JSTaggedValue> funcFuncPrototypeValue(funcFuncPrototype);
356 // Function.prototype_or_dynclass
357 JSHandle<JSHClass> funcFuncIntanceDynclass =
358 factory_->NewEcmaDynClass(JSFunction::SIZE, JSType::JS_FUNCTION, funcFuncPrototypeValue);
359 funcFuncIntanceDynclass->SetConstructor(true);
360 JSHandle<JSFunction> function = JSHandle<JSFunction>::Cast(factory_->NewJSObject(funcFuncIntanceDynclass));
361 function->SetBuiltinsCtorMode();
362
363 // Function = new Function() (forbidden use NewBuiltinConstructor)
364 JSMethod *ctor = vm_->GetMethodForNativeFunction(reinterpret_cast<void *>(Function::FunctionConstructor));
365 JSHandle<JSFunction> funcFunc =
366 factory_->NewJSFunctionByDynClass(ctor, funcFuncIntanceDynclass, FunctionKind::BUILTIN_CONSTRUCTOR);
367
368 auto funcFuncPrototypeObj = JSHandle<JSObject>(funcFuncPrototype);
369 InitializeCtor(env, funcFuncPrototypeObj, funcFunc, "Function", FunctionLength::ONE);
370
371 funcFunc->SetFunctionPrototype(thread_, funcFuncIntanceDynclass.GetTaggedValue());
372 env->SetFunctionFunction(thread_, funcFunc);
373 env->SetFunctionPrototype(thread_, funcFuncPrototype);
374
375 JSHandle<JSHClass> normalFuncClass =
376 factory_->NewEcmaDynClass(JSFunction::SIZE, JSType::JS_FUNCTION, env->GetFunctionPrototype());
377 env->SetNormalFunctionClass(thread_, normalFuncClass);
378
379 JSHandle<JSHClass> jSIntlBoundFunctionClass =
380 factory_->CreateFunctionClass(FunctionKind::NORMAL_FUNCTION, JSIntlBoundFunction::SIZE,
381 JSType::JS_INTL_BOUND_FUNCTION, env->GetFunctionPrototype());
382 env->SetJSIntlBoundFunctionClass(thread_, jSIntlBoundFunctionClass);
383
384 JSHandle<JSHClass> constructorFunctionClass =
385 factory_->NewEcmaDynClass(JSFunction::SIZE, JSType::JS_FUNCTION, env->GetFunctionPrototype());
386 constructorFunctionClass->SetConstructor(true);
387 JSHandle<JSFunction> functionConstructor =
388 JSHandle<JSFunction>::Cast(factory_->NewJSObject(constructorFunctionClass));
389 functionConstructor->SetBuiltinsCtorMode();
390 env->SetConstructorFunctionClass(thread_, constructorFunctionClass);
391
392 StrictModeForbiddenAccessCallerArguments(env, funcFuncPrototypeObj);
393
394 // Function.prototype method
395 // 19.2.3.1 Function.prototype.apply ( thisArg, argArray )
396 SetFunction(env, funcFuncPrototypeObj, "apply", Function::FunctionPrototypeApply, FunctionLength::TWO);
397 // 19.2.3.2 Function.prototype.bind ( thisArg , ...args)
398 SetFunction(env, funcFuncPrototypeObj, "bind", Function::FunctionPrototypeBind, FunctionLength::ONE);
399 // 19.2.3.3 Function.prototype.call (thisArg , ...args)
400 SetFunction(env, funcFuncPrototypeObj, "call", Function::FunctionPrototypeCall, FunctionLength::ONE);
401 // 19.2.3.5 Function.prototype.toString ( )
402 SetFunction(env, funcFuncPrototypeObj, thread_->GlobalConstants()->GetHandledToStringString(),
403 Function::FunctionPrototypeToString, FunctionLength::ZERO);
404 }
405
InitializeObject(const JSHandle<GlobalEnv> & env,const JSHandle<JSObject> & objFuncPrototype,const JSHandle<JSObject> & objFunc)406 void Builtins::InitializeObject(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &objFuncPrototype,
407 const JSHandle<JSObject> &objFunc)
408 {
409 [[maybe_unused]] EcmaHandleScope scope(thread_);
410 // Object method.
411 // 19.1.2.1Object.assign ( target, ...sources )
412 SetFunction(env, objFunc, "assign", Object::Assign, FunctionLength::TWO);
413 // 19.1.2.2Object.create ( O [ , Properties ] )
414 SetFunction(env, objFunc, "create", Object::Create, FunctionLength::TWO);
415 // 19.1.2.3Object.defineProperties ( O, Properties )
416 SetFunction(env, objFunc, "defineProperties", Object::DefineProperties, FunctionLength::TWO);
417 // 19.1.2.4Object.defineProperty ( O, P, Attributes )
418 SetFunction(env, objFunc, "defineProperty", Object::DefineProperty, FunctionLength::THREE);
419 // 19.1.2.5Object.freeze ( O )
420 SetFunction(env, objFunc, "freeze", Object::Freeze, FunctionLength::ONE);
421 // 19.1.2.6Object.getOwnPropertyDescriptor ( O, P )
422 SetFunction(env, objFunc, "getOwnPropertyDescriptor", Object::GetOwnPropertyDesciptor, FunctionLength::TWO);
423 // 19.1.2.7Object.getOwnPropertyNames ( O )
424 SetFunction(env, objFunc, "getOwnPropertyNames", Object::GetOwnPropertyNames, FunctionLength::ONE);
425 // 19.1.2.8Object.getOwnPropertySymbols ( O )
426 SetFunction(env, objFunc, "getOwnPropertySymbols", Object::GetOwnPropertySymbols, FunctionLength::ONE);
427 // 19.1.2.9Object.getPrototypeOf ( O )
428 SetFunction(env, objFunc, "getPrototypeOf", Object::GetPrototypeOf, FunctionLength::ONE);
429 // 19.1.2.10Object.is ( value1, value2 )
430 SetFunction(env, objFunc, "is", Object::Is, 2);
431 // 19.1.2.11Object.isExtensible ( O )
432 SetFunction(env, objFunc, "isExtensible", Object::IsExtensible, FunctionLength::ONE);
433 // 19.1.2.12Object.isFrozen ( O )
434 SetFunction(env, objFunc, "isFrozen", Object::IsFrozen, FunctionLength::ONE);
435 // 19.1.2.13Object.isSealed ( O )
436 SetFunction(env, objFunc, "isSealed", Object::IsSealed, FunctionLength::ONE);
437 // 19.1.2.14 Object.keys(O)
438 SetFunction(env, objFunc, "keys", Object::Keys, FunctionLength::ONE);
439 // 19.1.2.15 Object.preventExtensions(O)
440 SetFunction(env, objFunc, "preventExtensions", Object::PreventExtensions, FunctionLength::ONE);
441 // 19.1.2.17 Object.seal(O)
442 SetFunction(env, objFunc, "seal", Object::Seal, FunctionLength::ONE);
443 // 19.1.2.18 Object.setPrototypeOf(O, proto)
444 SetFunction(env, objFunc, "setPrototypeOf", Object::SetPrototypeOf, FunctionLength::TWO);
445 // 20.1.2.5 Object.entries ( O )
446 SetFunction(env, objFunc, "entries", Object::Entries, FunctionLength::ONE);
447
448 // Object.property method
449 // 19.1.3.2 Object.prototype.hasOwnProperty(V)
450 SetFunction(env, objFuncPrototype, "hasOwnProperty", Object::HasOwnProperty, FunctionLength::ONE);
451 // 19.1.3.3 Object.prototype.isPrototypeOf(V)
452 SetFunction(env, objFuncPrototype, "isPrototypeOf", Object::IsPrototypeOf, FunctionLength::ONE);
453 // 19.1.3.4 Object.prototype.propertyIsEnumerable(V)
454 SetFunction(env, objFuncPrototype, "propertyIsEnumerable", Object::PropertyIsEnumerable, FunctionLength::ONE);
455 // 19.1.3.5 Object.prototype.toLocaleString([reserved1[, reserved2]])
456 SetFunction(env, objFuncPrototype, "toLocaleString", Object::ToLocaleString, FunctionLength::ZERO);
457 // 19.1.3.6 Object.prototype.toString()
458 SetFunction(env, objFuncPrototype, thread_->GlobalConstants()->GetHandledToStringString(), Object::ToString,
459 FunctionLength::ZERO);
460 // 19.1.3.7 Object.prototype.valueOf()
461 SetFunction(env, objFuncPrototype, thread_->GlobalConstants()->GetHandledValueOfString(), Object::ValueOf,
462 FunctionLength::ZERO);
463
464 SetFunction(env, objFuncPrototype, "createRealm", Object::CreateRealm, FunctionLength::ZERO);
465
466 // B.2.2.1 Object.prototype.__proto__
467 JSHandle<JSTaggedValue> protoKey(factory_->NewFromCanBeCompressString("__proto__"));
468 JSHandle<JSTaggedValue> protoGetter = CreateGetter(env, Object::ProtoGetter, "__proto__", FunctionLength::ZERO);
469 JSHandle<JSTaggedValue> protoSetter = CreateSetter(env, Object::ProtoSetter, "__proto__", FunctionLength::ONE);
470 SetAccessor(objFuncPrototype, protoKey, protoGetter, protoSetter);
471 }
472
InitializeSymbol(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncDynclass) const473 void Builtins::InitializeSymbol(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncDynclass) const
474 {
475 [[maybe_unused]] EcmaHandleScope scope(thread_);
476 const GlobalEnvConstants *globalConst = thread_->GlobalConstants();
477 // Symbol.prototype
478 JSHandle<JSObject> symbolFuncPrototype = factory_->NewJSObject(objFuncDynclass);
479 JSHandle<JSTaggedValue> symbolFuncPrototypeValue(symbolFuncPrototype);
480
481 // Symbol.prototype_or_dynclass
482 JSHandle<JSHClass> symbolFuncInstanceDynclass =
483 factory_->NewEcmaDynClass(JSPrimitiveRef::SIZE, JSType::JS_PRIMITIVE_REF, symbolFuncPrototypeValue);
484
485 // Symbol = new Function()
486 JSHandle<JSObject> symbolFunction(
487 NewBuiltinConstructor(env, symbolFuncPrototype, Symbol::SymbolConstructor, "Symbol", FunctionLength::ZERO));
488 JSHandle<JSFunction>(symbolFunction)->SetFunctionPrototype(thread_, symbolFuncInstanceDynclass.GetTaggedValue());
489
490 // "constructor" property on the prototype
491 JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString();
492 PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>::Cast(symbolFunction), true, false, true);
493 JSObject::DefineOwnProperty(thread_, symbolFuncPrototype, constructorKey, descriptor);
494
495 SetFunction(env, symbolFunction, "for", Symbol::For, FunctionLength::ONE);
496 SetFunction(env, symbolFunction, "keyFor", Symbol::KeyFor, FunctionLength::ONE);
497
498 // Symbol attribute
499 JSHandle<JSTaggedValue> hasInstanceSymbol(factory_->NewWellKnownSymbolWithChar("Symbol.hasInstance"));
500 SetNoneAttributeProperty(symbolFunction, "hasInstance", hasInstanceSymbol);
501 JSHandle<JSTaggedValue> isConcatSpreadableSymbol(factory_->NewWellKnownSymbolWithChar("Symbol.isConcatSpreadable"));
502 SetNoneAttributeProperty(symbolFunction, "isConcatSpreadable", isConcatSpreadableSymbol);
503 JSHandle<JSTaggedValue> toStringTagSymbol(factory_->NewWellKnownSymbolWithChar("Symbol.toStringTag"));
504 SetNoneAttributeProperty(symbolFunction, "toStringTag", toStringTagSymbol);
505 JSHandle<JSTaggedValue> iteratorSymbol(factory_->NewPublicSymbolWithChar("Symbol.iterator"));
506 SetNoneAttributeProperty(symbolFunction, "iterator", iteratorSymbol);
507 JSHandle<JSTaggedValue> matchSymbol(factory_->NewPublicSymbolWithChar("Symbol.match"));
508 SetNoneAttributeProperty(symbolFunction, "match", matchSymbol);
509 JSHandle<JSTaggedValue> replaceSymbol(factory_->NewPublicSymbolWithChar("Symbol.replace"));
510 SetNoneAttributeProperty(symbolFunction, "replace", replaceSymbol);
511 JSHandle<JSTaggedValue> searchSymbol(factory_->NewPublicSymbolWithChar("Symbol.search"));
512 SetNoneAttributeProperty(symbolFunction, "search", searchSymbol);
513 JSHandle<JSTaggedValue> speciesSymbol(factory_->NewPublicSymbolWithChar("Symbol.species"));
514 SetNoneAttributeProperty(symbolFunction, "species", speciesSymbol);
515 JSHandle<JSTaggedValue> splitSymbol(factory_->NewPublicSymbolWithChar("Symbol.split"));
516 SetNoneAttributeProperty(symbolFunction, "split", splitSymbol);
517 JSHandle<JSTaggedValue> toPrimitiveSymbol(factory_->NewPublicSymbolWithChar("Symbol.toPrimitive"));
518 SetNoneAttributeProperty(symbolFunction, "toPrimitive", toPrimitiveSymbol);
519 JSHandle<JSTaggedValue> unscopablesSymbol(factory_->NewPublicSymbolWithChar("Symbol.unscopables"));
520 SetNoneAttributeProperty(symbolFunction, "unscopables", unscopablesSymbol);
521
522 // symbol.prototype.description
523 PropertyDescriptor descriptionDesc(thread_);
524 JSHandle<JSTaggedValue> getterKey(factory_->NewFromCanBeCompressString("description"));
525 JSHandle<JSTaggedValue> getter(factory_->NewJSFunction(env, reinterpret_cast<void *>(Symbol::DescriptionGetter)));
526 SetGetter(symbolFuncPrototype, getterKey, getter);
527
528 // Setup symbol.prototype[@@toPrimitive]
529 SetFunctionAtSymbol<JSSymbol::SYMBOL_TO_PRIMITIVE_TYPE>(
530 env, symbolFuncPrototype, toPrimitiveSymbol, "[Symbol.toPrimitive]", Symbol::ToPrimitive, FunctionLength::ONE);
531 // install the Symbol.prototype methods
532 SetFunction(env, symbolFuncPrototype, thread_->GlobalConstants()->GetHandledToStringString(), Symbol::ToString,
533 FunctionLength::ZERO);
534 SetFunction(env, symbolFuncPrototype, thread_->GlobalConstants()->GetHandledValueOfString(), Symbol::ValueOf,
535 FunctionLength::ZERO);
536
537 env->SetSymbolFunction(thread_, symbolFunction);
538 env->SetHasInstanceSymbol(thread_, hasInstanceSymbol);
539 env->SetIsConcatSpreadableSymbol(thread_, isConcatSpreadableSymbol);
540 env->SetToStringTagSymbol(thread_, toStringTagSymbol);
541 env->SetIteratorSymbol(thread_, iteratorSymbol);
542 env->SetMatchSymbol(thread_, matchSymbol);
543 env->SetReplaceSymbol(thread_, replaceSymbol);
544 env->SetSearchSymbol(thread_, searchSymbol);
545 env->SetSpeciesSymbol(thread_, speciesSymbol);
546 env->SetSplitSymbol(thread_, splitSymbol);
547 env->SetToPrimitiveSymbol(thread_, toPrimitiveSymbol);
548 env->SetUnscopablesSymbol(thread_, unscopablesSymbol);
549
550 // Setup %SymbolPrototype%
551 SetStringTagSymbol(env, symbolFuncPrototype, "Symbol");
552
553 JSHandle<JSTaggedValue> holeySymbol(factory_->NewPrivateNameSymbolWithChar("holey"));
554 env->SetHoleySymbol(thread_, holeySymbol.GetTaggedValue());
555 JSHandle<JSTaggedValue> elementIcSymbol(factory_->NewPrivateNameSymbolWithChar("element-ic"));
556 env->SetElementICSymbol(thread_, elementIcSymbol.GetTaggedValue());
557
558 // ecma 19.2.3.6 Function.prototype[@@hasInstance] ( V )
559 JSHandle<JSObject> funcFuncPrototypeObj = JSHandle<JSObject>(env->GetFunctionPrototype());
560 SetFunctionAtSymbol<JSSymbol::SYMBOL_HAS_INSTANCE_TYPE>(
561 env, funcFuncPrototypeObj, env->GetHasInstanceSymbol(), "[Symbol.hasInstance]",
562 Function::FunctionPrototypeHasInstance, FunctionLength::ONE);
563 }
564
InitializeSymbolWithRealm(const JSHandle<GlobalEnv> & realm,const JSHandle<JSHClass> & objFuncInstanceDynclass)565 void Builtins::InitializeSymbolWithRealm(const JSHandle<GlobalEnv> &realm,
566 const JSHandle<JSHClass> &objFuncInstanceDynclass)
567 {
568 [[maybe_unused]] EcmaHandleScope scope(thread_);
569 JSHandle<GlobalEnv> env = vm_->GetGlobalEnv();
570 // Symbol.prototype
571 JSHandle<JSObject> symbolFuncPrototype = factory_->NewJSObject(objFuncInstanceDynclass);
572 JSHandle<JSTaggedValue> symbolFuncPrototypeValue(symbolFuncPrototype);
573
574 // Symbol.prototype_or_dynclass
575 JSHandle<JSHClass> symbolFuncInstanceDynclass =
576 factory_->NewEcmaDynClass(JSPrimitiveRef::SIZE, JSType::JS_PRIMITIVE_REF, symbolFuncPrototypeValue);
577
578 // Symbol = new Function()
579 JSHandle<JSObject> symbolFunction(
580 NewBuiltinConstructor(realm, symbolFuncPrototype, Symbol::SymbolConstructor, "Symbol", FunctionLength::ZERO));
581 JSHandle<JSFunction>(symbolFunction)->SetFunctionPrototype(thread_, symbolFuncInstanceDynclass.GetTaggedValue());
582
583 // "constructor" property on the prototype
584 JSHandle<JSTaggedValue> constructorKey = thread_->GlobalConstants()->GetHandledConstructorString();
585 PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>::Cast(symbolFunction), true, false, true);
586 JSObject::DefineOwnProperty(thread_, symbolFuncPrototype, constructorKey, descriptor);
587
588 SetFunction(realm, symbolFunction, "for", Symbol::For, FunctionLength::ONE);
589 SetFunction(realm, symbolFunction, "keyFor", Symbol::KeyFor, FunctionLength::ONE);
590
591 // Symbol attribute
592 SetNoneAttributeProperty(symbolFunction, "hasInstance", env->GetHasInstanceSymbol());
593 SetNoneAttributeProperty(symbolFunction, "isConcatSpreadable", env->GetIsConcatSpreadableSymbol());
594 SetNoneAttributeProperty(symbolFunction, "toStringTag", env->GetToStringTagSymbol());
595 SetNoneAttributeProperty(symbolFunction, "iterator", env->GetIteratorSymbol());
596 SetNoneAttributeProperty(symbolFunction, "match", env->GetMatchSymbol());
597 SetNoneAttributeProperty(symbolFunction, "replace", env->GetReplaceSymbol());
598 SetNoneAttributeProperty(symbolFunction, "search", env->GetSearchSymbol());
599 SetNoneAttributeProperty(symbolFunction, "species", env->GetSpeciesSymbol());
600 SetNoneAttributeProperty(symbolFunction, "split", env->GetSplitSymbol());
601 SetNoneAttributeProperty(symbolFunction, "toPrimitive", env->GetToPrimitiveSymbol());
602 SetNoneAttributeProperty(symbolFunction, "unscopables", env->GetUnscopablesSymbol());
603
604 // symbol.prototype.description
605 PropertyDescriptor descriptionDesc(thread_);
606 JSHandle<JSTaggedValue> getterKey(factory_->NewFromCanBeCompressString("description"));
607 JSHandle<JSTaggedValue> getter(factory_->NewJSFunction(realm, reinterpret_cast<void *>(Symbol::DescriptionGetter)));
608 SetGetter(symbolFuncPrototype, getterKey, getter);
609
610 // Setup symbol.prototype[@@toPrimitive]
611 SetFunctionAtSymbol<JSSymbol::SYMBOL_TO_PRIMITIVE_TYPE>(realm, symbolFuncPrototype, env->GetToPrimitiveSymbol(),
612 "[Symbol.toPrimitive]", Symbol::ToPrimitive,
613 FunctionLength::ONE);
614 // install the Symbol.prototype methods
615 SetFunction(realm, symbolFuncPrototype, thread_->GlobalConstants()->GetHandledToStringString(), Symbol::ToString,
616 FunctionLength::ZERO);
617 SetFunction(realm, symbolFuncPrototype, thread_->GlobalConstants()->GetHandledValueOfString(), Symbol::ValueOf,
618 FunctionLength::ZERO);
619
620 realm->SetSymbolFunction(thread_, symbolFunction);
621 realm->SetHasInstanceSymbol(thread_, env->GetHasInstanceSymbol());
622 realm->SetIsConcatSpreadableSymbol(thread_, env->GetIsConcatSpreadableSymbol());
623 realm->SetToStringTagSymbol(thread_, env->GetToStringTagSymbol());
624 realm->SetIteratorSymbol(thread_, env->GetIteratorSymbol());
625 realm->SetMatchSymbol(thread_, env->GetMatchSymbol());
626 realm->SetReplaceSymbol(thread_, env->GetReplaceSymbol());
627 realm->SetSearchSymbol(thread_, env->GetSearchSymbol());
628 realm->SetSpeciesSymbol(thread_, env->GetSpeciesSymbol());
629 realm->SetSplitSymbol(thread_, env->GetSplitSymbol());
630 realm->SetToPrimitiveSymbol(thread_, env->GetToPrimitiveSymbol());
631 realm->SetUnscopablesSymbol(thread_, env->GetUnscopablesSymbol());
632
633 // Setup %SymbolPrototype%
634 SetStringTagSymbol(realm, symbolFuncPrototype, "Symbol");
635
636 JSHandle<JSTaggedValue> holeySymbol(factory_->NewPrivateNameSymbolWithChar("holey"));
637 realm->SetHoleySymbol(thread_, holeySymbol.GetTaggedValue());
638 JSHandle<JSTaggedValue> elementIcSymbol(factory_->NewPrivateNameSymbolWithChar("element-ic"));
639 realm->SetElementICSymbol(thread_, elementIcSymbol.GetTaggedValue());
640
641 // ecma 19.2.3.6 Function.prototype[@@hasInstance] ( V )
642 JSHandle<JSObject> funcFuncPrototypeObj = JSHandle<JSObject>(realm->GetFunctionPrototype());
643 SetFunctionAtSymbol<JSSymbol::SYMBOL_HAS_INSTANCE_TYPE>(
644 realm, funcFuncPrototypeObj, realm->GetHasInstanceSymbol(), "[Symbol.hasInstance]",
645 Function::FunctionPrototypeHasInstance, FunctionLength::ONE);
646 }
647
InitializeNumber(const JSHandle<GlobalEnv> & env,const JSHandle<JSObject> & globalObject,const JSHandle<JSHClass> & primRefObjDynclass)648 void Builtins::InitializeNumber(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &globalObject,
649 const JSHandle<JSHClass> &primRefObjDynclass)
650 {
651 [[maybe_unused]] EcmaHandleScope scope(thread_);
652 // Number.prototype
653 JSHandle<JSTaggedValue> toObject(thread_, JSTaggedValue(FunctionLength::ZERO));
654 JSHandle<JSObject> numFuncPrototype =
655 JSHandle<JSObject>::Cast(factory_->NewJSPrimitiveRef(primRefObjDynclass, toObject));
656 JSHandle<JSTaggedValue> numFuncPrototypeValue(numFuncPrototype);
657
658 // Number.prototype_or_dynclass
659 JSHandle<JSHClass> numFuncInstanceClass =
660 factory_->NewEcmaDynClass(JSPrimitiveRef::SIZE, JSType::JS_PRIMITIVE_REF, numFuncPrototypeValue);
661
662 // Number = new Function()
663 JSHandle<JSObject> numFunction(
664 NewBuiltinConstructor(env, numFuncPrototype, Number::NumberConstructor, "Number", FunctionLength::ONE));
665 numFunction.GetObject<JSFunction>()->SetFunctionPrototype(thread_, numFuncInstanceClass.GetTaggedValue());
666
667 // Number.prototype method
668 SetFunction(env, numFuncPrototype, "toExponential", Number::ToExponential, FunctionLength::ONE);
669 SetFunction(env, numFuncPrototype, "toFixed", Number::ToFixed, FunctionLength::ONE);
670 SetFunction(env, numFuncPrototype, "toLocaleString", Number::ToLocaleString, FunctionLength::ZERO);
671 SetFunction(env, numFuncPrototype, "toPrecision", Number::ToPrecision, FunctionLength::ONE);
672 SetFunction(env, numFuncPrototype, thread_->GlobalConstants()->GetHandledToStringString(), Number::ToString,
673 FunctionLength::ONE);
674 SetFunction(env, numFuncPrototype, thread_->GlobalConstants()->GetHandledValueOfString(), Number::ValueOf,
675 FunctionLength::ZERO);
676
677 // Number method
678 SetFunction(env, numFunction, "isFinite", Number::IsFinite, FunctionLength::ONE);
679 SetFunction(env, numFunction, "isInteger", Number::IsInteger, FunctionLength::ONE);
680 SetFunction(env, numFunction, "isNaN", Number::IsNaN, FunctionLength::ONE);
681 SetFunction(env, numFunction, "isSafeInteger", Number::IsSafeInteger, FunctionLength::ONE);
682 SetFuncToObjAndGlobal(env, globalObject, numFunction, "parseFloat", Number::ParseFloat, FunctionLength::ONE);
683 SetFuncToObjAndGlobal(env, globalObject, numFunction, "parseInt", Number::ParseInt, FunctionLength::TWO);
684
685 // Number constant
686 const double epsilon = 2.220446049250313e-16;
687 const double maxSafeInteger = 9007199254740991;
688 const double maxValue = 1.7976931348623157e+308;
689 const double minValue = 5e-324;
690 const double positiveInfinity = std::numeric_limits<double>::infinity();
691 SetConstant(numFunction, "MAX_VALUE", JSTaggedValue(maxValue));
692 SetConstant(numFunction, "MIN_VALUE", JSTaggedValue(minValue));
693 SetConstant(numFunction, "NaN", JSTaggedValue(NAN));
694 SetConstant(numFunction, "NEGATIVE_INFINITY", JSTaggedValue(-positiveInfinity));
695 SetConstant(numFunction, "POSITIVE_INFINITY", JSTaggedValue(positiveInfinity));
696 SetConstant(numFunction, "MAX_SAFE_INTEGER", JSTaggedValue(maxSafeInteger));
697 SetConstant(numFunction, "MIN_SAFE_INTEGER", JSTaggedValue(-maxSafeInteger));
698 SetConstant(numFunction, "EPSILON", JSTaggedValue(epsilon));
699
700 env->SetNumberFunction(thread_, numFunction);
701 }
702
InitializeBigInt(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncDynclass) const703 void Builtins::InitializeBigInt(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncDynclass) const
704 {
705 [[maybe_unused]] EcmaHandleScope scope(thread_);
706 // BigInt.prototype
707 JSHandle<JSObject> bigIntFuncPrototype = factory_->NewJSObject(objFuncDynclass);
708 JSHandle<JSTaggedValue> bigIntFuncPrototypeValue(bigIntFuncPrototype);
709
710 // BigInt.prototype_or_dynclass
711 JSHandle<JSHClass> bigIntFuncInstanceDynclass =
712 factory_->NewEcmaDynClass(JSPrimitiveRef::SIZE, JSType::JS_PRIMITIVE_REF, bigIntFuncPrototypeValue);
713 // BigInt = new Function()
714 JSHandle<JSObject> bigIntFunction(
715 NewBuiltinConstructor(env, bigIntFuncPrototype,
716 BuiltinsBigInt::BigIntConstructor, "BigInt", FunctionLength::ONE));
717 JSHandle<JSFunction>(bigIntFunction)->SetFunctionPrototype(thread_, bigIntFuncInstanceDynclass.GetTaggedValue());
718
719 // BigInt.prototype method
720 SetFunction(env, bigIntFuncPrototype, "toLocaleString", BuiltinsBigInt::ToLocaleString, FunctionLength::ZERO);
721 SetFunction(env, bigIntFuncPrototype, "toString", BuiltinsBigInt::ToString, FunctionLength::ZERO);
722 SetFunction(env, bigIntFuncPrototype, "valueOf", BuiltinsBigInt::ValueOf, FunctionLength::ZERO);
723
724 // BigInt method
725 SetFunction(env, bigIntFunction, "asUintN", BuiltinsBigInt::AsUintN, FunctionLength::TWO);
726 SetFunction(env, bigIntFunction, "asIntN", BuiltinsBigInt::AsIntN, FunctionLength::TWO);
727
728 // @@ToStringTag
729 SetStringTagSymbol(env, bigIntFuncPrototype, "BigInt");
730 env->SetBigIntFunction(thread_, bigIntFunction);
731 }
732
733
InitializeDate(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncDynclass) const734 void Builtins::InitializeDate(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncDynclass) const
735 {
736 [[maybe_unused]] EcmaHandleScope scope(thread_);
737 const int utcLength = 7;
738 // Date.prototype
739 JSHandle<JSObject> dateFuncPrototype = factory_->NewJSObject(objFuncDynclass);
740 JSHandle<JSTaggedValue> dateFuncPrototypeValue(dateFuncPrototype);
741
742 // Date.prototype_or_dynclass
743 JSHandle<JSHClass> dateFuncInstanceDynclass =
744 factory_->NewEcmaDynClass(JSDate::SIZE, JSType::JS_DATE, dateFuncPrototypeValue);
745
746 // Date = new Function()
747 JSHandle<JSObject> dateFunction(
748 NewBuiltinConstructor(env, dateFuncPrototype, Date::DateConstructor, "Date", FunctionLength::ONE));
749 JSHandle<JSFunction>(dateFunction)->SetFunctionPrototype(thread_, dateFuncInstanceDynclass.GetTaggedValue());
750
751 // Date.prototype method
752 SetFunction(env, dateFuncPrototype, "getDate", Date::GetDate, FunctionLength::ZERO);
753 SetFunction(env, dateFuncPrototype, "getDay", Date::GetDay, FunctionLength::ZERO);
754 SetFunction(env, dateFuncPrototype, "getFullYear", Date::GetFullYear, FunctionLength::ZERO);
755 SetFunction(env, dateFuncPrototype, "getHours", Date::GetHours, FunctionLength::ZERO);
756 SetFunction(env, dateFuncPrototype, "getMilliseconds", Date::GetMilliseconds, FunctionLength::ZERO);
757 SetFunction(env, dateFuncPrototype, "getMinutes", Date::GetMinutes, FunctionLength::ZERO);
758 SetFunction(env, dateFuncPrototype, "getMonth", Date::GetMonth, FunctionLength::ZERO);
759 SetFunction(env, dateFuncPrototype, "getSeconds", Date::GetSeconds, FunctionLength::ZERO);
760 SetFunction(env, dateFuncPrototype, "getTime", Date::GetTime, FunctionLength::ZERO);
761 SetFunction(env, dateFuncPrototype, "getTimezoneOffset", Date::GetTimezoneOffset, FunctionLength::ZERO);
762 SetFunction(env, dateFuncPrototype, "getUTCDate", Date::GetUTCDate, FunctionLength::ZERO);
763 SetFunction(env, dateFuncPrototype, "getUTCDay", Date::GetUTCDay, FunctionLength::ZERO);
764 SetFunction(env, dateFuncPrototype, "getUTCFullYear", Date::GetUTCFullYear, FunctionLength::ZERO);
765 SetFunction(env, dateFuncPrototype, "getUTCHours", Date::GetUTCHours, FunctionLength::ZERO);
766 SetFunction(env, dateFuncPrototype, "getUTCMilliseconds", Date::GetUTCMilliseconds, FunctionLength::ZERO);
767 SetFunction(env, dateFuncPrototype, "getUTCMinutes", Date::GetUTCMinutes, FunctionLength::ZERO);
768 SetFunction(env, dateFuncPrototype, "getUTCMonth", Date::GetUTCMonth, FunctionLength::ZERO);
769 SetFunction(env, dateFuncPrototype, "getUTCSeconds", Date::GetUTCSeconds, FunctionLength::ZERO);
770
771 SetFunction(env, dateFuncPrototype, "setDate", Date::SetDate, FunctionLength::ONE);
772 SetFunction(env, dateFuncPrototype, "setFullYear", Date::SetFullYear, FunctionLength::THREE);
773 SetFunction(env, dateFuncPrototype, "setHours", Date::SetHours, FunctionLength::FOUR);
774 SetFunction(env, dateFuncPrototype, "setMilliseconds", Date::SetMilliseconds, FunctionLength::ONE);
775 SetFunction(env, dateFuncPrototype, "setMinutes", Date::SetMinutes, FunctionLength::THREE);
776 SetFunction(env, dateFuncPrototype, "setMonth", Date::SetMonth, FunctionLength::TWO);
777 SetFunction(env, dateFuncPrototype, "setSeconds", Date::SetSeconds, FunctionLength::TWO);
778 SetFunction(env, dateFuncPrototype, "setTime", Date::SetTime, FunctionLength::ONE);
779 SetFunction(env, dateFuncPrototype, "setUTCDate", Date::SetUTCDate, FunctionLength::ONE);
780 SetFunction(env, dateFuncPrototype, "setUTCFullYear", Date::SetUTCFullYear, FunctionLength::THREE);
781 SetFunction(env, dateFuncPrototype, "setUTCHours", Date::SetUTCHours, FunctionLength::FOUR);
782 SetFunction(env, dateFuncPrototype, "setUTCMilliseconds", Date::SetUTCMilliseconds, FunctionLength::ONE);
783 SetFunction(env, dateFuncPrototype, "setUTCMinutes", Date::SetUTCMinutes, FunctionLength::THREE);
784 SetFunction(env, dateFuncPrototype, "setUTCMonth", Date::SetUTCMonth, FunctionLength::TWO);
785 SetFunction(env, dateFuncPrototype, "setUTCSeconds", Date::SetUTCSeconds, FunctionLength::TWO);
786
787 SetFunction(env, dateFuncPrototype, "toDateString", Date::ToDateString, FunctionLength::ZERO);
788 SetFunction(env, dateFuncPrototype, "toISOString", Date::ToISOString, FunctionLength::ZERO);
789 SetFunction(env, dateFuncPrototype, "toJSON", Date::ToJSON, FunctionLength::ONE);
790 SetFunction(env, dateFuncPrototype, "toLocaleDateString", Date::ToLocaleDateString, FunctionLength::ZERO);
791 SetFunction(env, dateFuncPrototype, "toLocaleString", Date::ToLocaleString, FunctionLength::ZERO);
792 SetFunction(env, dateFuncPrototype, "toLocaleTimeString", Date::ToLocaleTimeString, FunctionLength::ZERO);
793 SetFunction(env, dateFuncPrototype, thread_->GlobalConstants()->GetHandledToStringString(), Date::ToString,
794 FunctionLength::ZERO);
795 SetFunction(env, dateFuncPrototype, "toTimeString", Date::ToTimeString, FunctionLength::ZERO);
796 SetFunction(env, dateFuncPrototype, "toUTCString", Date::ToUTCString, FunctionLength::ZERO);
797 SetFunction(env, dateFuncPrototype, thread_->GlobalConstants()->GetHandledValueOfString(), Date::ValueOf,
798 FunctionLength::ZERO);
799
800 SetFunctionAtSymbol(env, dateFuncPrototype, env->GetToPrimitiveSymbol(), "[Symbol.toPrimitive]", Date::ToPrimitive,
801 FunctionLength::ONE);
802
803 // Date method
804 SetFunction(env, dateFunction, "now", Date::Now, FunctionLength::ZERO);
805 SetFunction(env, dateFunction, "parse", Date::Parse, FunctionLength::ONE);
806 SetFunction(env, dateFunction, "UTC", Date::UTC, utcLength);
807
808 // Date.length
809 SetConstant(dateFunction, "length", JSTaggedValue(utcLength));
810
811 env->SetDateFunction(thread_, dateFunction);
812 }
813
InitializeBoolean(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & primRefObjDynclass) const814 void Builtins::InitializeBoolean(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &primRefObjDynclass) const
815 {
816 [[maybe_unused]] EcmaHandleScope scope(thread_);
817 // Boolean.prototype
818 JSHandle<JSTaggedValue> toObject(thread_, JSTaggedValue::False());
819 JSHandle<JSObject> booleanFuncPrototype =
820 JSHandle<JSObject>::Cast(factory_->NewJSPrimitiveRef(primRefObjDynclass, toObject));
821 JSHandle<JSTaggedValue> booleanFuncPrototypeValue(booleanFuncPrototype);
822
823 // Boolean.prototype_or_dynclass
824 JSHandle<JSHClass> booleanFuncInstanceDynclass =
825 factory_->NewEcmaDynClass(JSPrimitiveRef::SIZE, JSType::JS_PRIMITIVE_REF, booleanFuncPrototypeValue);
826
827 // new Boolean Function()
828 JSHandle<JSFunction> booleanFunction(
829 NewBuiltinConstructor(env, booleanFuncPrototype, Boolean::BooleanConstructor, "Boolean", FunctionLength::ONE));
830 booleanFunction->SetFunctionPrototype(thread_, booleanFuncInstanceDynclass.GetTaggedValue());
831
832 // Boolean.prototype method
833 SetFunction(env, booleanFuncPrototype, thread_->GlobalConstants()->GetHandledToStringString(),
834 Boolean::BooleanPrototypeToString, FunctionLength::ZERO);
835 SetFunction(env, booleanFuncPrototype, thread_->GlobalConstants()->GetHandledValueOfString(),
836 Boolean::BooleanPrototypeValueOf, FunctionLength::ZERO);
837
838 env->SetBooleanFunction(thread_, booleanFunction);
839 }
840
InitializeProxy(const JSHandle<GlobalEnv> & env)841 void Builtins::InitializeProxy(const JSHandle<GlobalEnv> &env)
842 {
843 JSHandle<JSObject> proxyFunction(InitializeExoticConstructor(env, Proxy::ProxyConstructor, "Proxy", 2));
844
845 // Proxy method
846 SetFunction(env, proxyFunction, "revocable", Proxy::Revocable, FunctionLength::TWO);
847 env->SetProxyFunction(thread_, proxyFunction);
848 }
849
InitializeExoticConstructor(const JSHandle<GlobalEnv> & env,EcmaEntrypoint ctorFunc,const char * name,int length)850 JSHandle<JSFunction> Builtins::InitializeExoticConstructor(const JSHandle<GlobalEnv> &env, EcmaEntrypoint ctorFunc,
851 const char *name, int length)
852 {
853 JSHandle<JSFunction> ctor =
854 factory_->NewJSFunction(env, reinterpret_cast<void *>(ctorFunc), FunctionKind::BUILTIN_PROXY_CONSTRUCTOR);
855
856 JSFunction::SetFunctionLength(thread_, ctor, JSTaggedValue(length));
857 JSHandle<JSTaggedValue> nameString(factory_->NewFromString(name));
858 JSFunction::SetFunctionName(thread_, JSHandle<JSFunctionBase>(ctor), nameString,
859 JSHandle<JSTaggedValue>(thread_, JSTaggedValue::Undefined()));
860
861 JSHandle<JSObject> globalObject(thread_, env->GetGlobalObject());
862 PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>(ctor), true, false, true);
863 JSObject::DefineOwnProperty(thread_, globalObject, nameString, descriptor);
864 return ctor;
865 }
866
InitializeAsyncFunction(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncDynclass) const867 void Builtins::InitializeAsyncFunction(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncDynclass) const
868 {
869 [[maybe_unused]] EcmaHandleScope scope(thread_);
870 const GlobalEnvConstants *globalConst = thread_->GlobalConstants();
871 // AsyncFunction.prototype
872 JSHandle<JSObject> asyncFuncPrototype = factory_->NewJSObject(objFuncDynclass);
873 JSObject::SetPrototype(thread_, asyncFuncPrototype, env->GetFunctionPrototype());
874 JSHandle<JSTaggedValue> async_func_prototype_value(asyncFuncPrototype);
875
876 // AsyncFunction.prototype_or_dynclass
877 JSHandle<JSHClass> asyncFuncInstanceDynclass =
878 factory_->NewEcmaDynClass(JSAsyncFunction::SIZE, JSType::JS_ASYNC_FUNCTION, async_func_prototype_value);
879
880 // AsyncFunction = new Function()
881 JSHandle<JSFunction> asyncFunction = NewBuiltinConstructor(
882 env, asyncFuncPrototype, AsyncFunction::AsyncFunctionConstructor, "AsyncFunction", FunctionLength::ONE);
883 JSObject::SetPrototype(thread_, JSHandle<JSObject>::Cast(asyncFunction), env->GetFunctionFunction());
884 JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString();
885 PropertyDescriptor asyncDesc(thread_, JSHandle<JSTaggedValue>::Cast(asyncFunction), false, false, true);
886 JSObject::DefineOwnProperty(thread_, asyncFuncPrototype, constructorKey, asyncDesc);
887 asyncFunction->SetProtoOrDynClass(thread_, asyncFuncInstanceDynclass.GetTaggedValue());
888
889 // AsyncFunction.prototype property
890 SetStringTagSymbol(env, asyncFuncPrototype, "AsyncFunction");
891 env->SetAsyncFunction(thread_, asyncFunction);
892 env->SetAsyncFunctionPrototype(thread_, asyncFuncPrototype);
893 }
894
InitializeAllTypeError(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncDynclass) const895 void Builtins::InitializeAllTypeError(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncDynclass) const
896 {
897 // Error.prototype
898 JSHandle<JSObject> errorFuncPrototype = factory_->NewJSObject(objFuncDynclass);
899 JSHandle<JSTaggedValue> errorFuncPrototypeValue(errorFuncPrototype);
900 // Error.prototype_or_dynclass
901 JSHandle<JSHClass> errorFuncInstanceDynclass =
902 factory_->NewEcmaDynClass(JSObject::SIZE, JSType::JS_ERROR, errorFuncPrototypeValue);
903 // Error() = new Function()
904 JSHandle<JSFunction> errorFunction(
905 NewBuiltinConstructor(env, errorFuncPrototype, Error::ErrorConstructor, "Error", FunctionLength::ONE));
906 errorFunction->SetFunctionPrototype(thread_, errorFuncInstanceDynclass.GetTaggedValue());
907
908 // Error.prototype method
909 SetFunction(env, errorFuncPrototype, thread_->GlobalConstants()->GetHandledToStringString(), Error::ToString,
910 FunctionLength::ZERO);
911
912 // Error.prototype Attribute
913 SetAttribute(errorFuncPrototype, "name", "Error");
914 SetAttribute(errorFuncPrototype, "message", "");
915 env->SetErrorFunction(thread_, errorFunction);
916
917 JSHandle<JSHClass> nativeErrorFuncClass =
918 factory_->NewEcmaDynClass(JSFunction::SIZE, JSType::JS_FUNCTION, env->GetErrorFunction());
919 nativeErrorFuncClass->SetConstructor(true);
920 JSHandle<JSFunction> function = JSHandle<JSFunction>::Cast(factory_->NewJSObject(nativeErrorFuncClass));
921 function->SetBuiltinsCtorMode();
922 env->SetNativeErrorFunctionClass(thread_, nativeErrorFuncClass);
923
924 JSHandle<JSHClass> errorNativeFuncInstanceDynclass =
925 factory_->NewEcmaDynClass(JSObject::SIZE, JSType::JS_OBJECT, errorFuncPrototypeValue);
926 InitializeError(env, errorNativeFuncInstanceDynclass, JSType::JS_RANGE_ERROR);
927 InitializeError(env, errorNativeFuncInstanceDynclass, JSType::JS_REFERENCE_ERROR);
928 InitializeError(env, errorNativeFuncInstanceDynclass, JSType::JS_TYPE_ERROR);
929 InitializeError(env, errorNativeFuncInstanceDynclass, JSType::JS_URI_ERROR);
930 InitializeError(env, errorNativeFuncInstanceDynclass, JSType::JS_SYNTAX_ERROR);
931 InitializeError(env, errorNativeFuncInstanceDynclass, JSType::JS_EVAL_ERROR);
932 }
933
InitializeAllTypeErrorWithRealm(const JSHandle<GlobalEnv> & realm) const934 void Builtins::InitializeAllTypeErrorWithRealm(const JSHandle<GlobalEnv> &realm) const
935 {
936 JSHandle<GlobalEnv> env = vm_->GetGlobalEnv();
937
938 realm->SetErrorFunction(thread_, env->GetErrorFunction());
939 realm->SetNativeErrorFunctionClass(thread_, env->GetNativeErrorFunctionClass());
940
941 SetErrorWithRealm(realm, JSType::JS_RANGE_ERROR);
942 SetErrorWithRealm(realm, JSType::JS_REFERENCE_ERROR);
943 SetErrorWithRealm(realm, JSType::JS_TYPE_ERROR);
944 SetErrorWithRealm(realm, JSType::JS_URI_ERROR);
945 SetErrorWithRealm(realm, JSType::JS_SYNTAX_ERROR);
946 SetErrorWithRealm(realm, JSType::JS_EVAL_ERROR);
947 }
948
SetErrorWithRealm(const JSHandle<GlobalEnv> & realm,const JSType & errorTag) const949 void Builtins::SetErrorWithRealm(const JSHandle<GlobalEnv> &realm, const JSType &errorTag) const
950 {
951 JSHandle<GlobalEnv> env = vm_->GetGlobalEnv();
952 JSHandle<JSObject> globalObject(thread_, realm->GetGlobalObject());
953 JSHandle<JSTaggedValue> nameString;
954 JSHandle<JSTaggedValue> nativeErrorFunction;
955 switch (errorTag) {
956 case JSType::JS_RANGE_ERROR:
957 nativeErrorFunction = env->GetRangeErrorFunction();
958 nameString = JSHandle<JSTaggedValue>(thread_->GlobalConstants()->GetHandledRangeErrorString());
959 realm->SetRangeErrorFunction(thread_, nativeErrorFunction);
960 break;
961 case JSType::JS_EVAL_ERROR:
962 nativeErrorFunction = env->GetEvalErrorFunction();
963 nameString = JSHandle<JSTaggedValue>(thread_->GlobalConstants()->GetHandledEvalErrorString());
964 realm->SetEvalErrorFunction(thread_, nativeErrorFunction);
965 break;
966 case JSType::JS_REFERENCE_ERROR:
967 nativeErrorFunction = env->GetReferenceErrorFunction();
968 nameString = JSHandle<JSTaggedValue>(thread_->GlobalConstants()->GetHandledReferenceErrorString());
969 realm->SetReferenceErrorFunction(thread_, nativeErrorFunction);
970 break;
971 case JSType::JS_TYPE_ERROR:
972 nativeErrorFunction = env->GetTypeErrorFunction();
973 nameString = JSHandle<JSTaggedValue>(thread_->GlobalConstants()->GetHandledTypeErrorString());
974 realm->SetTypeErrorFunction(thread_, nativeErrorFunction);
975 realm->SetThrowTypeError(thread_, env->GetThrowTypeError());
976 break;
977 case JSType::JS_URI_ERROR:
978 nativeErrorFunction = env->GetURIErrorFunction();
979 nameString = JSHandle<JSTaggedValue>(thread_->GlobalConstants()->GetHandledURIErrorString());
980 realm->SetURIErrorFunction(thread_, nativeErrorFunction);
981 break;
982 case JSType::JS_SYNTAX_ERROR:
983 nativeErrorFunction = env->GetSyntaxErrorFunction();
984 nameString = JSHandle<JSTaggedValue>(thread_->GlobalConstants()->GetHandledSyntaxErrorString());
985 realm->SetSyntaxErrorFunction(thread_, nativeErrorFunction);
986 break;
987 default:
988 break;
989 }
990 PropertyDescriptor descriptor(thread_, nativeErrorFunction, true, false, true);
991 JSObject::DefineOwnProperty(thread_, globalObject, nameString, descriptor);
992 }
993
GeneralUpdateError(ErrorParameter * error,EcmaEntrypoint constructor,EcmaEntrypoint method,const char * name,JSType type) const994 void Builtins::GeneralUpdateError(ErrorParameter *error, EcmaEntrypoint constructor, EcmaEntrypoint method,
995 const char *name, JSType type) const
996 {
997 error->nativeConstructor = constructor;
998 error->nativeMethod = method;
999 error->nativePropertyName = name;
1000 error->nativeJstype = type;
1001 }
1002
InitializeError(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncDynclass,const JSType & errorTag) const1003 void Builtins::InitializeError(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncDynclass,
1004 const JSType &errorTag) const
1005 {
1006 // NativeError.prototype
1007 JSHandle<JSObject> nativeErrorFuncPrototype = factory_->NewJSObject(objFuncDynclass);
1008 JSHandle<JSTaggedValue> nativeErrorFuncPrototypeValue(nativeErrorFuncPrototype);
1009
1010 ErrorParameter errorParameter{RangeError::RangeErrorConstructor, RangeError::ToString, "RangeError",
1011 JSType::JS_RANGE_ERROR};
1012 switch (errorTag) {
1013 case JSType::JS_RANGE_ERROR:
1014 GeneralUpdateError(&errorParameter, RangeError::RangeErrorConstructor, RangeError::ToString, "RangeError",
1015 JSType::JS_RANGE_ERROR);
1016 break;
1017 case JSType::JS_EVAL_ERROR:
1018 GeneralUpdateError(&errorParameter, EvalError::EvalErrorConstructor, EvalError::ToString, "EvalError",
1019 JSType::JS_EVAL_ERROR);
1020 break;
1021 case JSType::JS_REFERENCE_ERROR:
1022 GeneralUpdateError(&errorParameter, ReferenceError::ReferenceErrorConstructor, ReferenceError::ToString,
1023 "ReferenceError", JSType::JS_REFERENCE_ERROR);
1024 break;
1025 case JSType::JS_TYPE_ERROR:
1026 GeneralUpdateError(&errorParameter, TypeError::TypeErrorConstructor, TypeError::ToString, "TypeError",
1027 JSType::JS_TYPE_ERROR);
1028 break;
1029 case JSType::JS_URI_ERROR:
1030 GeneralUpdateError(&errorParameter, URIError::URIErrorConstructor, URIError::ToString, "URIError",
1031 JSType::JS_URI_ERROR);
1032 break;
1033 case JSType::JS_SYNTAX_ERROR:
1034 GeneralUpdateError(&errorParameter, SyntaxError::SyntaxErrorConstructor, SyntaxError::ToString,
1035 "SyntaxError", JSType::JS_SYNTAX_ERROR);
1036 break;
1037 default:
1038 break;
1039 }
1040
1041 // NativeError.prototype_or_dynclass
1042 JSHandle<JSHClass> nativeErrorFuncInstanceDynclass =
1043 factory_->NewEcmaDynClass(JSObject::SIZE, errorParameter.nativeJstype, nativeErrorFuncPrototypeValue);
1044
1045 // NativeError() = new Error()
1046 JSHandle<JSFunction> nativeErrorFunction =
1047 factory_->NewJSNativeErrorFunction(env, reinterpret_cast<void *>(errorParameter.nativeConstructor));
1048 InitializeCtor(env, nativeErrorFuncPrototype, nativeErrorFunction, errorParameter.nativePropertyName,
1049 FunctionLength::ONE);
1050
1051 nativeErrorFunction->SetFunctionPrototype(thread_, nativeErrorFuncInstanceDynclass.GetTaggedValue());
1052
1053 // NativeError.prototype method
1054 SetFunction(env, nativeErrorFuncPrototype, thread_->GlobalConstants()->GetHandledToStringString(),
1055 errorParameter.nativeMethod, FunctionLength::ZERO);
1056
1057 // Error.prototype Attribute
1058 SetAttribute(nativeErrorFuncPrototype, "name", errorParameter.nativePropertyName);
1059 SetAttribute(nativeErrorFuncPrototype, "message", "");
1060
1061 if (errorTag == JSType::JS_RANGE_ERROR) {
1062 env->SetRangeErrorFunction(thread_, nativeErrorFunction);
1063 } else if (errorTag == JSType::JS_REFERENCE_ERROR) {
1064 env->SetReferenceErrorFunction(thread_, nativeErrorFunction);
1065 } else if (errorTag == JSType::JS_TYPE_ERROR) {
1066 env->SetTypeErrorFunction(thread_, nativeErrorFunction);
1067 JSHandle<JSFunction> throwTypeErrorFunction =
1068 factory_->NewJSFunction(env, reinterpret_cast<void *>(TypeError::ThrowTypeError));
1069 JSFunction::SetFunctionLength(thread_, throwTypeErrorFunction, JSTaggedValue(1), false);
1070 JSObject::PreventExtensions(thread_, JSHandle<JSObject>::Cast(throwTypeErrorFunction));
1071 env->SetThrowTypeError(thread_, throwTypeErrorFunction);
1072 } else if (errorTag == JSType::JS_URI_ERROR) {
1073 env->SetURIErrorFunction(thread_, nativeErrorFunction);
1074 } else if (errorTag == JSType::JS_SYNTAX_ERROR) {
1075 env->SetSyntaxErrorFunction(thread_, nativeErrorFunction);
1076 } else {
1077 env->SetEvalErrorFunction(thread_, nativeErrorFunction);
1078 }
1079 } // namespace panda::ecmascript
1080
InitializeCtor(const JSHandle<GlobalEnv> & env,const JSHandle<JSObject> & prototype,const JSHandle<JSFunction> & ctor,const char * name,int length) const1081 void Builtins::InitializeCtor(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &prototype,
1082 const JSHandle<JSFunction> &ctor, const char *name, int length) const
1083 {
1084 const GlobalEnvConstants *globalConst = thread_->GlobalConstants();
1085 JSFunction::SetFunctionLength(thread_, ctor, JSTaggedValue(length));
1086 JSHandle<JSTaggedValue> nameString(factory_->NewFromString(name));
1087 JSFunction::SetFunctionName(thread_, JSHandle<JSFunctionBase>(ctor), nameString,
1088 JSHandle<JSTaggedValue>(thread_, JSTaggedValue::Undefined()));
1089 JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString();
1090 PropertyDescriptor descriptor1(thread_, JSHandle<JSTaggedValue>::Cast(ctor), true, false, true);
1091 JSObject::DefineOwnProperty(thread_, prototype, constructorKey, descriptor1);
1092
1093 /* set "prototype" in constructor */
1094 ctor->SetFunctionPrototype(thread_, prototype.GetTaggedValue());
1095
1096 if (!JSTaggedValue::SameValue(nameString, thread_->GlobalConstants()->GetHandledAsyncFunctionString())) {
1097 JSHandle<JSObject> globalObject(thread_, env->GetGlobalObject());
1098 PropertyDescriptor descriptor2(thread_, JSHandle<JSTaggedValue>::Cast(ctor), true, false, true);
1099 JSObject::DefineOwnProperty(thread_, globalObject, nameString, descriptor2);
1100 }
1101 }
1102
InitializeSet(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncDynclass) const1103 void Builtins::InitializeSet(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncDynclass) const
1104 {
1105 [[maybe_unused]] EcmaHandleScope scope(thread_);
1106 const GlobalEnvConstants *globalConst = thread_->GlobalConstants();
1107 // Set.prototype
1108 JSHandle<JSObject> setFuncPrototype = factory_->NewJSObject(objFuncDynclass);
1109 JSHandle<JSTaggedValue> setFuncPrototypeValue(setFuncPrototype);
1110 // Set.prototype_or_dynclass
1111 JSHandle<JSHClass> setFuncInstanceDynclass =
1112 factory_->NewEcmaDynClass(JSSet::SIZE, JSType::JS_SET, setFuncPrototypeValue);
1113 // Set() = new Function()
1114 JSHandle<JSTaggedValue> setFunction(
1115 NewBuiltinConstructor(env, setFuncPrototype, BuiltinsSet::SetConstructor, "Set", FunctionLength::ZERO));
1116 JSHandle<JSFunction>(setFunction)->SetFunctionPrototype(thread_, setFuncInstanceDynclass.GetTaggedValue());
1117
1118 // "constructor" property on the prototype
1119 JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString();
1120 JSObject::SetProperty(thread_, JSHandle<JSTaggedValue>(setFuncPrototype), constructorKey, setFunction);
1121 // set.prototype.add()
1122 SetFunction(env, setFuncPrototype, "add", BuiltinsSet::Add, FunctionLength::ONE);
1123 // set.prototype.clear()
1124 SetFunction(env, setFuncPrototype, "clear", BuiltinsSet::Clear, FunctionLength::ZERO);
1125 // set.prototype.delete()
1126 SetFunction(env, setFuncPrototype, "delete", BuiltinsSet::Delete, FunctionLength::ONE);
1127 // set.prototype.has()
1128 SetFunction(env, setFuncPrototype, "has", BuiltinsSet::Has, FunctionLength::ONE);
1129 // set.prototype.forEach()
1130 SetFunction(env, setFuncPrototype, "forEach", BuiltinsSet::ForEach, FunctionLength::ONE);
1131 // set.prototype.entries()
1132 SetFunction(env, setFuncPrototype, "entries", BuiltinsSet::Entries, FunctionLength::ZERO);
1133 // set.prototype.keys()
1134 SetFunction(env, setFuncPrototype, "values", BuiltinsSet::Values, FunctionLength::ZERO);
1135 // set.prototype.values()
1136 JSHandle<JSTaggedValue> keys(factory_->NewFromCanBeCompressString("keys"));
1137 JSHandle<JSTaggedValue> values(factory_->NewFromCanBeCompressString("values"));
1138 JSHandle<JSTaggedValue> valuesFunc =
1139 JSObject::GetMethod(thread_, JSHandle<JSTaggedValue>::Cast(setFuncPrototype), values);
1140 PropertyDescriptor descriptor(thread_, valuesFunc, true, false, true);
1141 JSObject::DefineOwnProperty(thread_, setFuncPrototype, keys, descriptor);
1142
1143 // @@ToStringTag
1144 SetStringTagSymbol(env, setFuncPrototype, "Set");
1145
1146 // 23.1.3.10get Set.prototype.size
1147 JSHandle<JSTaggedValue> sizeGetter = CreateGetter(env, BuiltinsSet::GetSize, "size", FunctionLength::ZERO);
1148 JSHandle<JSTaggedValue> sizeKey(factory_->NewFromCanBeCompressString("size"));
1149 SetGetter(setFuncPrototype, sizeKey, sizeGetter);
1150
1151 // 23.1.2.2get Set [ @@species ]
1152 JSHandle<JSTaggedValue> speciesSymbol = env->GetSpeciesSymbol();
1153 JSHandle<JSTaggedValue> speciesGetter =
1154 CreateGetter(env, BuiltinsSet::Species, "[Symbol.species]", FunctionLength::ZERO);
1155 SetGetter(JSHandle<JSObject>(setFunction), speciesSymbol, speciesGetter);
1156
1157 // %SetPrototype% [ @@iterator ]
1158 JSHandle<JSTaggedValue> iteratorSymbol = env->GetIteratorSymbol();
1159 JSObject::DefineOwnProperty(thread_, setFuncPrototype, iteratorSymbol, descriptor);
1160
1161 env->SetBuiltinsSetFunction(thread_, setFunction);
1162 }
1163
InitializeMap(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncDynclass) const1164 void Builtins::InitializeMap(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncDynclass) const
1165 {
1166 [[maybe_unused]] EcmaHandleScope scope(thread_);
1167 const GlobalEnvConstants *globalConst = thread_->GlobalConstants();
1168 // Map.prototype
1169 JSHandle<JSObject> mapFuncPrototype = factory_->NewJSObject(objFuncDynclass);
1170 JSHandle<JSTaggedValue> mapFuncPrototypeValue(mapFuncPrototype);
1171 // Map.prototype_or_dynclass
1172 JSHandle<JSHClass> mapFuncInstanceDynclass =
1173 factory_->NewEcmaDynClass(JSMap::SIZE, JSType::JS_MAP, mapFuncPrototypeValue);
1174 // Map() = new Function()
1175 JSHandle<JSTaggedValue> mapFunction(
1176 NewBuiltinConstructor(env, mapFuncPrototype, BuiltinsMap::MapConstructor, "Map", FunctionLength::ZERO));
1177 // Map().prototype = Map.Prototype & Map.prototype.constructor = Map()
1178 JSFunction::Cast(mapFunction->GetTaggedObject())
1179 ->SetFunctionPrototype(thread_, mapFuncInstanceDynclass.GetTaggedValue());
1180
1181 // "constructor" property on the prototype
1182 JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString();
1183 JSObject::SetProperty(thread_, JSHandle<JSTaggedValue>(mapFuncPrototype), constructorKey, mapFunction);
1184 // map.prototype.set()
1185 SetFunction(env, mapFuncPrototype, globalConst->GetHandledSetString(), BuiltinsMap::Set, FunctionLength::TWO);
1186 // map.prototype.clear()
1187 SetFunction(env, mapFuncPrototype, "clear", BuiltinsMap::Clear, FunctionLength::ZERO);
1188 // map.prototype.delete()
1189 SetFunction(env, mapFuncPrototype, "delete", BuiltinsMap::Delete, FunctionLength::ONE);
1190 // map.prototype.has()
1191 SetFunction(env, mapFuncPrototype, "has", BuiltinsMap::Has, FunctionLength::ONE);
1192 // map.prototype.get()
1193 SetFunction(env, mapFuncPrototype, thread_->GlobalConstants()->GetHandledGetString(), BuiltinsMap::Get,
1194 FunctionLength::ONE);
1195 // map.prototype.forEach()
1196 SetFunction(env, mapFuncPrototype, "forEach", BuiltinsMap::ForEach, FunctionLength::ONE);
1197 // map.prototype.keys()
1198 SetFunction(env, mapFuncPrototype, "keys", BuiltinsMap::Keys, FunctionLength::ZERO);
1199 // map.prototype.values()
1200 SetFunction(env, mapFuncPrototype, "values", BuiltinsMap::Values, FunctionLength::ZERO);
1201 // map.prototype.entries()
1202 SetFunction(env, mapFuncPrototype, "entries", BuiltinsMap::Entries, FunctionLength::ZERO);
1203 // @@ToStringTag
1204 SetStringTagSymbol(env, mapFuncPrototype, "Map");
1205
1206 // 23.1.3.10get Map.prototype.size
1207 JSHandle<JSTaggedValue> sizeGetter = CreateGetter(env, BuiltinsMap::GetSize, "size", FunctionLength::ZERO);
1208 JSHandle<JSTaggedValue> sizeKey(factory_->NewFromCanBeCompressString("size"));
1209 SetGetter(mapFuncPrototype, sizeKey, sizeGetter);
1210
1211 // 23.1.2.2get Map [ @@species ]
1212 JSHandle<JSTaggedValue> speciesSymbol = env->GetSpeciesSymbol();
1213 JSHandle<JSTaggedValue> speciesGetter =
1214 CreateGetter(env, BuiltinsMap::Species, "[Symbol.species]", FunctionLength::ZERO);
1215 SetGetter(JSHandle<JSObject>(mapFunction), speciesSymbol, speciesGetter);
1216
1217 // %MapPrototype% [ @@iterator ]
1218 JSHandle<JSTaggedValue> iteratorSymbol = env->GetIteratorSymbol();
1219 JSHandle<JSTaggedValue> entries(factory_->NewFromCanBeCompressString("entries"));
1220 JSHandle<JSTaggedValue> entriesFunc =
1221 JSObject::GetMethod(thread_, JSHandle<JSTaggedValue>::Cast(mapFuncPrototype), entries);
1222 PropertyDescriptor descriptor(thread_, entriesFunc, true, false, true);
1223 JSObject::DefineOwnProperty(thread_, mapFuncPrototype, iteratorSymbol, descriptor);
1224
1225 env->SetBuiltinsMapFunction(thread_, mapFunction);
1226 env->SetMapPrototype(thread_, mapFuncPrototype);
1227 }
1228
InitializeWeakMap(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncDynclass) const1229 void Builtins::InitializeWeakMap(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncDynclass) const
1230 {
1231 [[maybe_unused]] EcmaHandleScope scope(thread_);
1232 const GlobalEnvConstants *globalConst = thread_->GlobalConstants();
1233 // WeakMap.prototype
1234 JSHandle<JSObject> weakMapFuncPrototype = factory_->NewJSObject(objFuncDynclass);
1235 JSHandle<JSTaggedValue> weakMapFuncPrototypeValue(weakMapFuncPrototype);
1236 // WeakMap.prototype_or_dynclass
1237 JSHandle<JSHClass> weakMapFuncInstanceDynclass =
1238 factory_->NewEcmaDynClass(JSWeakMap::SIZE, JSType::JS_WEAK_MAP, weakMapFuncPrototypeValue);
1239 // WeakMap() = new Function()
1240 JSHandle<JSTaggedValue> weakMapFunction(NewBuiltinConstructor(
1241 env, weakMapFuncPrototype, BuiltinsWeakMap::WeakMapConstructor, "WeakMap", FunctionLength::ZERO));
1242 // WeakMap().prototype = WeakMap.Prototype & WeakMap.prototype.constructor = WeakMap()
1243 JSFunction::Cast(weakMapFunction->GetTaggedObject())
1244 ->SetProtoOrDynClass(thread_, weakMapFuncInstanceDynclass.GetTaggedValue());
1245
1246 // "constructor" property on the prototype
1247 JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString();
1248 JSObject::SetProperty(thread_, JSHandle<JSTaggedValue>(weakMapFuncPrototype), constructorKey, weakMapFunction);
1249 // weakmap.prototype.set()
1250 SetFunction(env, weakMapFuncPrototype, globalConst->GetHandledSetString(), BuiltinsWeakMap::Set,
1251 FunctionLength::TWO);
1252 // weakmap.prototype.delete()
1253 SetFunction(env, weakMapFuncPrototype, "delete", BuiltinsWeakMap::Delete, FunctionLength::ONE);
1254 // weakmap.prototype.has()
1255 SetFunction(env, weakMapFuncPrototype, "has", BuiltinsWeakMap::Has, FunctionLength::ONE);
1256 // weakmap.prototype.get()
1257 SetFunction(env, weakMapFuncPrototype, thread_->GlobalConstants()->GetHandledGetString(), BuiltinsWeakMap::Get,
1258 FunctionLength::ONE);
1259 // @@ToStringTag
1260 SetStringTagSymbol(env, weakMapFuncPrototype, "WeakMap");
1261
1262 env->SetBuiltinsWeakMapFunction(thread_, weakMapFunction);
1263 }
1264
InitializeWeakSet(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncDynclass) const1265 void Builtins::InitializeWeakSet(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncDynclass) const
1266 {
1267 [[maybe_unused]] EcmaHandleScope scope(thread_);
1268 const GlobalEnvConstants *globalConst = thread_->GlobalConstants();
1269 // Set.prototype
1270 JSHandle<JSObject> weakSetFuncPrototype = factory_->NewJSObject(objFuncDynclass);
1271 JSHandle<JSTaggedValue> weakSetFuncPrototypeValue(weakSetFuncPrototype);
1272 // Set.prototype_or_dynclass
1273 JSHandle<JSHClass> weakSetFuncInstanceDynclass =
1274 factory_->NewEcmaDynClass(JSWeakSet::SIZE, JSType::JS_WEAK_SET, weakSetFuncPrototypeValue);
1275 // Set() = new Function()
1276 JSHandle<JSTaggedValue> weakSetFunction(NewBuiltinConstructor(
1277 env, weakSetFuncPrototype, BuiltinsWeakSet::WeakSetConstructor, "WeakSet", FunctionLength::ZERO));
1278 JSHandle<JSFunction>(weakSetFunction)->SetProtoOrDynClass(thread_, weakSetFuncInstanceDynclass.GetTaggedValue());
1279
1280 // "constructor" property on the prototype
1281 JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString();
1282 JSObject::SetProperty(thread_, JSHandle<JSTaggedValue>(weakSetFuncPrototype), constructorKey, weakSetFunction);
1283 // set.prototype.add()
1284 SetFunction(env, weakSetFuncPrototype, "add", BuiltinsWeakSet::Add, FunctionLength::ONE);
1285 // set.prototype.delete()
1286 SetFunction(env, weakSetFuncPrototype, "delete", BuiltinsWeakSet::Delete, FunctionLength::ONE);
1287 // set.prototype.has()
1288 SetFunction(env, weakSetFuncPrototype, "has", BuiltinsWeakSet::Has, FunctionLength::ONE);
1289
1290 // @@ToStringTag
1291 SetStringTagSymbol(env, weakSetFuncPrototype, "WeakSet");
1292
1293 env->SetBuiltinsWeakSetFunction(thread_, weakSetFunction);
1294 }
1295
InitializeMath(const JSHandle<GlobalEnv> & env,const JSHandle<JSTaggedValue> & objFuncPrototypeVal) const1296 void Builtins::InitializeMath(const JSHandle<GlobalEnv> &env, const JSHandle<JSTaggedValue> &objFuncPrototypeVal) const
1297 {
1298 [[maybe_unused]] EcmaHandleScope scope(thread_);
1299 JSHandle<JSHClass> mathDynclass = factory_->NewEcmaDynClass(JSObject::SIZE, JSType::JS_OBJECT, objFuncPrototypeVal);
1300 JSHandle<JSObject> mathObject = factory_->NewJSObject(mathDynclass);
1301 SetFunction(env, mathObject, "abs", Math::Abs, FunctionLength::ONE);
1302 SetFunction(env, mathObject, "acos", Math::Acos, FunctionLength::ONE);
1303 SetFunction(env, mathObject, "acosh", Math::Acosh, FunctionLength::ONE);
1304 SetFunction(env, mathObject, "asin", Math::Asin, FunctionLength::ONE);
1305 SetFunction(env, mathObject, "asinh", Math::Asinh, FunctionLength::ONE);
1306 SetFunction(env, mathObject, "atan", Math::Atan, FunctionLength::ONE);
1307 SetFunction(env, mathObject, "atanh", Math::Atanh, FunctionLength::ONE);
1308 SetFunction(env, mathObject, "atan2", Math::Atan2, FunctionLength::TWO);
1309 SetFunction(env, mathObject, "cbrt", Math::Cbrt, FunctionLength::ONE);
1310 SetFunction(env, mathObject, "ceil", Math::Ceil, FunctionLength::ONE);
1311 SetFunction(env, mathObject, "clz32", Math::Clz32, FunctionLength::ONE);
1312 SetFunction(env, mathObject, "cos", Math::Cos, FunctionLength::ONE);
1313 SetFunction(env, mathObject, "cosh", Math::Cosh, FunctionLength::ONE);
1314 SetFunction(env, mathObject, "exp", Math::Exp, FunctionLength::ONE);
1315 SetFunction(env, mathObject, "expm1", Math::Expm1, FunctionLength::ONE);
1316 SetFunction(env, mathObject, "floor", Math::Floor, FunctionLength::ONE);
1317 SetFunction(env, mathObject, "fround", Math::Fround, FunctionLength::ONE);
1318 SetFunction(env, mathObject, "hypot", Math::Hypot, FunctionLength::TWO);
1319 SetFunction(env, mathObject, "imul", Math::Imul, FunctionLength::TWO);
1320 SetFunction(env, mathObject, "log", Math::Log, FunctionLength::ONE);
1321 SetFunction(env, mathObject, "log1p", Math::Log1p, FunctionLength::ONE);
1322 SetFunction(env, mathObject, "log10", Math::Log10, FunctionLength::ONE);
1323 SetFunction(env, mathObject, "log2", Math::Log2, FunctionLength::ONE);
1324 SetFunction(env, mathObject, "max", Math::Max, FunctionLength::TWO);
1325 SetFunction(env, mathObject, "min", Math::Min, FunctionLength::TWO);
1326 SetFunction(env, mathObject, "pow", Math::Pow, FunctionLength::TWO);
1327 SetFunction(env, mathObject, "random", Math::Random, FunctionLength::ZERO);
1328 SetFunction(env, mathObject, "round", Math::Round, FunctionLength::ONE);
1329 SetFunction(env, mathObject, "sign", Math::Sign, FunctionLength::ONE);
1330 SetFunction(env, mathObject, "sin", Math::Sin, FunctionLength::ONE);
1331 SetFunction(env, mathObject, "sinh", Math::Sinh, FunctionLength::ONE);
1332 SetFunction(env, mathObject, "sqrt", Math::Sqrt, FunctionLength::ONE);
1333 SetFunction(env, mathObject, "tan", Math::Tan, FunctionLength::ONE);
1334 SetFunction(env, mathObject, "tanh", Math::Tanh, FunctionLength::ONE);
1335 SetFunction(env, mathObject, "trunc", Math::Trunc, FunctionLength::ONE);
1336
1337 SetConstant(mathObject, "E", JSTaggedValue(Math::E));
1338 SetConstant(mathObject, "LN10", JSTaggedValue(Math::LN10));
1339 SetConstant(mathObject, "LN2", JSTaggedValue(Math::LN2));
1340 SetConstant(mathObject, "LOG10E", JSTaggedValue(Math::LOG10E));
1341 SetConstant(mathObject, "LOG2E", JSTaggedValue(Math::LOG2E));
1342 SetConstant(mathObject, "PI", JSTaggedValue(Math::PI));
1343 SetConstant(mathObject, "SQRT1_2", JSTaggedValue(Math::SQRT1_2));
1344 SetConstant(mathObject, "SQRT2", JSTaggedValue(Math::SQRT2));
1345
1346 JSHandle<JSTaggedValue> mathString(factory_->NewFromCanBeCompressString("Math"));
1347 JSHandle<JSObject> globalObject(thread_, env->GetGlobalObject());
1348 PropertyDescriptor mathDesc(thread_, JSHandle<JSTaggedValue>::Cast(mathObject), true, false, true);
1349 JSObject::DefineOwnProperty(thread_, globalObject, mathString, mathDesc);
1350 // @@ToStringTag
1351 SetStringTagSymbol(env, mathObject, "Math");
1352 env->SetMathFunction(thread_, mathObject);
1353 }
1354
InitializeJson(const JSHandle<GlobalEnv> & env,const JSHandle<JSTaggedValue> & objFuncPrototypeVal) const1355 void Builtins::InitializeJson(const JSHandle<GlobalEnv> &env, const JSHandle<JSTaggedValue> &objFuncPrototypeVal) const
1356 {
1357 [[maybe_unused]] EcmaHandleScope scope(thread_);
1358 JSHandle<JSHClass> jsonDynclass = factory_->NewEcmaDynClass(JSObject::SIZE, JSType::JS_OBJECT, objFuncPrototypeVal);
1359 JSHandle<JSObject> jsonObject = factory_->NewJSObject(jsonDynclass);
1360
1361 SetFunction(env, jsonObject, "parse", Json::Parse, FunctionLength::TWO);
1362 SetFunction(env, jsonObject, "stringify", Json::Stringify, FunctionLength::THREE);
1363
1364 PropertyDescriptor jsonDesc(thread_, JSHandle<JSTaggedValue>::Cast(jsonObject), true, false, true);
1365 JSHandle<JSTaggedValue> jsonString(factory_->NewFromCanBeCompressString("JSON"));
1366 JSHandle<JSObject> globalObject(thread_, env->GetGlobalObject());
1367 JSObject::DefineOwnProperty(thread_, globalObject, jsonString, jsonDesc);
1368 // @@ToStringTag
1369 SetStringTagSymbol(env, jsonObject, "JSON");
1370 env->SetJsonFunction(thread_, jsonObject);
1371 }
1372
InitializeString(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & primRefObjDynclass) const1373 void Builtins::InitializeString(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &primRefObjDynclass) const
1374 {
1375 [[maybe_unused]] EcmaHandleScope scope(thread_);
1376 // String.prototype
1377 JSHandle<JSTaggedValue> toObject(factory_->GetEmptyString());
1378 JSHandle<JSObject> stringFuncPrototype =
1379 JSHandle<JSObject>::Cast(factory_->NewJSPrimitiveRef(primRefObjDynclass, toObject));
1380 JSHandle<JSTaggedValue> stringFuncPrototypeValue(stringFuncPrototype);
1381
1382 // String.prototype_or_dynclass
1383 JSHandle<JSHClass> stringFuncInstanceDynclass =
1384 factory_->NewEcmaDynClass(JSPrimitiveRef::SIZE, JSType::JS_PRIMITIVE_REF, stringFuncPrototypeValue);
1385
1386 // String = new Function()
1387 JSHandle<JSObject> stringFunction(NewBuiltinConstructor(env, stringFuncPrototype, BuiltinsString::StringConstructor,
1388 "String", FunctionLength::ONE));
1389 stringFunction.GetObject<JSFunction>()->SetFunctionPrototype(thread_, stringFuncInstanceDynclass.GetTaggedValue());
1390
1391 // String.prototype method
1392 SetFunction(env, stringFuncPrototype, "charAt", BuiltinsString::CharAt, FunctionLength::ONE);
1393 SetFunction(env, stringFuncPrototype, "charCodeAt", BuiltinsString::CharCodeAt, FunctionLength::ONE);
1394 SetFunction(env, stringFuncPrototype, "codePointAt", BuiltinsString::CodePointAt, FunctionLength::ONE);
1395 SetFunction(env, stringFuncPrototype, "concat", BuiltinsString::Concat, FunctionLength::ONE);
1396 SetFunction(env, stringFuncPrototype, "endsWith", BuiltinsString::EndsWith, FunctionLength::ONE);
1397 SetFunction(env, stringFuncPrototype, "includes", BuiltinsString::Includes, FunctionLength::ONE);
1398 SetFunction(env, stringFuncPrototype, "indexOf", BuiltinsString::IndexOf, FunctionLength::ONE);
1399 SetFunction(env, stringFuncPrototype, "lastIndexOf", BuiltinsString::LastIndexOf, FunctionLength::ONE);
1400 SetFunction(env, stringFuncPrototype, "localeCompare", BuiltinsString::LocaleCompare, FunctionLength::ONE);
1401 SetFunction(env, stringFuncPrototype, "match", BuiltinsString::Match, FunctionLength::ONE);
1402 SetFunction(env, stringFuncPrototype, "repeat", BuiltinsString::Repeat, FunctionLength::ONE);
1403 SetFunction(env, stringFuncPrototype, "normalize", BuiltinsString::Normalize, FunctionLength::ZERO);
1404 SetFunction(env, stringFuncPrototype, "replace", BuiltinsString::Replace, FunctionLength::TWO);
1405 SetFunction(env, stringFuncPrototype, "search", BuiltinsString::Search, FunctionLength::ONE);
1406 SetFunction(env, stringFuncPrototype, "slice", BuiltinsString::Slice, FunctionLength::TWO);
1407 SetFunction(env, stringFuncPrototype, "split", BuiltinsString::Split, FunctionLength::TWO);
1408 SetFunction(env, stringFuncPrototype, "startsWith", BuiltinsString::StartsWith, FunctionLength::ONE);
1409 SetFunction(env, stringFuncPrototype, "substring", BuiltinsString::Substring, FunctionLength::TWO);
1410 SetFunction(env, stringFuncPrototype, "substr", BuiltinsString::SubStr, FunctionLength::TWO);
1411 SetFunction(env, stringFuncPrototype, "toLocaleLowerCase", BuiltinsString::ToLocaleLowerCase, FunctionLength::ZERO);
1412 SetFunction(env, stringFuncPrototype, "toLocaleUpperCase", BuiltinsString::ToLocaleUpperCase, FunctionLength::ZERO);
1413 SetFunction(env, stringFuncPrototype, "toLowerCase", BuiltinsString::ToLowerCase, FunctionLength::ZERO);
1414 SetFunction(env, stringFuncPrototype, thread_->GlobalConstants()->GetHandledToStringString(),
1415 BuiltinsString::ToString, FunctionLength::ZERO);
1416 SetFunction(env, stringFuncPrototype, "toUpperCase", BuiltinsString::ToUpperCase, FunctionLength::ZERO);
1417 SetFunction(env, stringFuncPrototype, "trim", BuiltinsString::Trim, FunctionLength::ZERO);
1418 SetFunction(env, stringFuncPrototype, thread_->GlobalConstants()->GetHandledValueOfString(),
1419 BuiltinsString::ValueOf, FunctionLength::ZERO);
1420 SetFunctionAtSymbol(env, stringFuncPrototype, env->GetIteratorSymbol(), "[Symbol.iterator]",
1421 BuiltinsString::GetStringIterator, FunctionLength::ZERO);
1422
1423 // String method
1424 SetFunction(env, stringFunction, "fromCharCode", BuiltinsString::FromCharCode, FunctionLength::ONE);
1425 SetFunction(env, stringFunction, "fromCodePoint", BuiltinsString::FromCodePoint, FunctionLength::ONE);
1426 SetFunction(env, stringFunction, "raw", BuiltinsString::Raw, FunctionLength::ONE);
1427
1428 // String.prototype.length
1429 JSHandle<JSTaggedValue> lengthGetter = CreateGetter(env, BuiltinsString::GetLength, "length", FunctionLength::ZERO);
1430 JSHandle<JSTaggedValue> lengthKey(factory_->NewFromCanBeCompressString("length"));
1431 SetGetter(stringFuncPrototype, lengthKey, lengthGetter);
1432
1433 env->SetStringFunction(thread_, stringFunction);
1434 }
1435
InitializeStringIterator(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & iteratorFuncDynclass) const1436 void Builtins::InitializeStringIterator(const JSHandle<GlobalEnv> &env,
1437 const JSHandle<JSHClass> &iteratorFuncDynclass) const
1438 {
1439 // StringIterator.prototype
1440 JSHandle<JSObject> strIterPrototype(factory_->NewJSObject(iteratorFuncDynclass));
1441
1442 // StringIterator.prototype_or_dynclass
1443 JSHandle<JSHClass> strIterFuncInstanceDynclass = factory_->NewEcmaDynClass(
1444 JSStringIterator::SIZE, JSType::JS_STRING_ITERATOR, JSHandle<JSTaggedValue>(strIterPrototype));
1445
1446 JSHandle<JSFunction> strIterFunction(
1447 factory_->NewJSFunction(env, static_cast<void *>(nullptr), FunctionKind::BASE_CONSTRUCTOR));
1448 strIterFunction->SetFunctionPrototype(thread_, strIterFuncInstanceDynclass.GetTaggedValue());
1449
1450 SetFunction(env, strIterPrototype, "next", StringIterator::Next, FunctionLength::ZERO);
1451 SetStringTagSymbol(env, strIterPrototype, "String Iterator");
1452
1453 env->SetStringIterator(thread_, strIterFunction);
1454 env->SetStringIteratorPrototype(thread_, strIterPrototype);
1455 }
1456
InitializeIterator(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncDynclass) const1457 void Builtins::InitializeIterator(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncDynclass) const
1458 {
1459 [[maybe_unused]] EcmaHandleScope scope(thread_);
1460 // Iterator.prototype
1461 JSHandle<JSObject> iteratorPrototype = factory_->NewJSObject(objFuncDynclass);
1462 // Iterator.prototype.next()
1463 SetFunction(env, iteratorPrototype, "next", BuiltinsIterator::Next, FunctionLength::ONE);
1464 // Iterator.prototype.return()
1465 SetFunction(env, iteratorPrototype, "return", BuiltinsIterator::Return, FunctionLength::ONE);
1466 // Iterator.prototype.throw()
1467 SetFunction(env, iteratorPrototype, "throw", BuiltinsIterator::Throw, FunctionLength::ONE);
1468 // %IteratorPrototype% [ @@iterator ]
1469 SetFunctionAtSymbol(env, iteratorPrototype, env->GetIteratorSymbol(), "[Symbol.iterator]",
1470 BuiltinsIterator::GetIteratorObj, FunctionLength::ZERO);
1471 env->SetIteratorPrototype(thread_, iteratorPrototype);
1472
1473 // Iterator.dynclass
1474 JSHandle<JSHClass> iteratorFuncDynclass =
1475 factory_->NewEcmaDynClass(JSObject::SIZE, JSType::JS_ITERATOR, JSHandle<JSTaggedValue>(iteratorPrototype));
1476
1477 InitializeForinIterator(env, iteratorFuncDynclass);
1478 InitializeSetIterator(env, iteratorFuncDynclass);
1479 InitializeMapIterator(env, iteratorFuncDynclass);
1480 InitializeArrayIterator(env, iteratorFuncDynclass);
1481 InitializeStringIterator(env, iteratorFuncDynclass);
1482 }
1483
InitializeForinIterator(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & iteratorFuncDynclass) const1484 void Builtins::InitializeForinIterator(const JSHandle<GlobalEnv> &env,
1485 const JSHandle<JSHClass> &iteratorFuncDynclass) const
1486 {
1487 [[maybe_unused]] EcmaHandleScope scope(thread_);
1488 // Iterator.prototype
1489 JSHandle<JSObject> forinIteratorPrototype = factory_->NewJSObject(iteratorFuncDynclass);
1490 JSHandle<JSHClass> dynclass = factory_->NewEcmaDynClass(JSForInIterator::SIZE, JSType::JS_FORIN_ITERATOR,
1491 JSHandle<JSTaggedValue>(forinIteratorPrototype));
1492
1493 // Iterator.prototype.next()
1494 SetFunction(env, forinIteratorPrototype, "next", JSForInIterator::Next, FunctionLength::ONE);
1495 env->SetForinIteratorPrototype(thread_, forinIteratorPrototype);
1496 env->SetForinIteratorClass(thread_, dynclass);
1497 }
1498
InitializeSetIterator(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & iteratorFuncDynclass) const1499 void Builtins::InitializeSetIterator(const JSHandle<GlobalEnv> &env,
1500 const JSHandle<JSHClass> &iteratorFuncDynclass) const
1501 {
1502 // SetIterator.prototype
1503 JSHandle<JSObject> setIteratorPrototype(factory_->NewJSObject(iteratorFuncDynclass));
1504 // Iterator.prototype.next()
1505 SetFunction(env, setIteratorPrototype, "next", JSSetIterator::Next, FunctionLength::ZERO);
1506 SetStringTagSymbol(env, setIteratorPrototype, "Set Iterator");
1507 env->SetSetIteratorPrototype(thread_, setIteratorPrototype);
1508 }
1509
InitializeMapIterator(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & iteratorFuncDynclass) const1510 void Builtins::InitializeMapIterator(const JSHandle<GlobalEnv> &env,
1511 const JSHandle<JSHClass> &iteratorFuncDynclass) const
1512 {
1513 // MapIterator.prototype
1514 JSHandle<JSObject> mapIteratorPrototype(factory_->NewJSObject(iteratorFuncDynclass));
1515 // Iterator.prototype.next()
1516 SetFunction(env, mapIteratorPrototype, "next", JSMapIterator::Next, FunctionLength::ZERO);
1517 SetStringTagSymbol(env, mapIteratorPrototype, "Map Iterator");
1518 env->SetMapIteratorPrototype(thread_, mapIteratorPrototype);
1519 }
InitializeArrayIterator(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & iteratorFuncDynclass) const1520 void Builtins::InitializeArrayIterator(const JSHandle<GlobalEnv> &env,
1521 const JSHandle<JSHClass> &iteratorFuncDynclass) const
1522 {
1523 // ArrayIterator.prototype
1524 JSHandle<JSObject> arrayIteratorPrototype(factory_->NewJSObject(iteratorFuncDynclass));
1525 // Iterator.prototype.next()
1526 SetFunction(env, arrayIteratorPrototype, "next", JSArrayIterator::Next, FunctionLength::ZERO);
1527 SetStringTagSymbol(env, arrayIteratorPrototype, "Array Iterator");
1528 env->SetArrayIteratorPrototype(thread_, arrayIteratorPrototype);
1529 }
1530
InitializeRegExp(const JSHandle<GlobalEnv> & env)1531 void Builtins::InitializeRegExp(const JSHandle<GlobalEnv> &env)
1532 {
1533 [[maybe_unused]] EcmaHandleScope scope(thread_);
1534 // RegExp.prototype
1535 JSHandle<JSTaggedValue> objFun = env->GetObjectFunction();
1536 JSHandle<JSObject> regPrototype = factory_->NewJSObjectByConstructor(JSHandle<JSFunction>(objFun), objFun);
1537 JSHandle<JSTaggedValue> regPrototypeValue(regPrototype);
1538
1539 // RegExp.prototype_or_dynclass
1540 JSHandle<JSHClass> regexpFuncInstanceDynclass = factory_->CreateJSRegExpInstanceClass(regPrototypeValue);
1541
1542 // RegExp = new Function()
1543 JSHandle<JSObject> regexpFunction(
1544 NewBuiltinConstructor(env, regPrototype, RegExp::RegExpConstructor, "RegExp", FunctionLength::TWO));
1545
1546 JSHandle<JSFunction>(regexpFunction)->SetFunctionPrototype(thread_, regexpFuncInstanceDynclass.GetTaggedValue());
1547
1548 // RegExp.prototype method
1549 SetFunction(env, regPrototype, "exec", RegExp::Exec, FunctionLength::ONE);
1550 SetFunction(env, regPrototype, "test", RegExp::Test, FunctionLength::ONE);
1551 SetFunction(env, regPrototype, thread_->GlobalConstants()->GetHandledToStringString(), RegExp::ToString,
1552 FunctionLength::ZERO);
1553
1554 JSHandle<JSTaggedValue> flagsGetter = CreateGetter(env, RegExp::GetFlags, "flags", FunctionLength::ZERO);
1555 JSHandle<JSTaggedValue> flagsKey(factory_->NewFromCanBeCompressString("flags"));
1556 SetGetter(regPrototype, flagsKey, flagsGetter);
1557
1558 JSHandle<JSTaggedValue> sourceGetter = CreateGetter(env, RegExp::GetSource, "source", FunctionLength::ZERO);
1559 JSHandle<JSTaggedValue> sourceKey(factory_->NewFromCanBeCompressString("source"));
1560 SetGetter(regPrototype, sourceKey, sourceGetter);
1561
1562 JSHandle<JSTaggedValue> globalGetter = CreateGetter(env, RegExp::GetGlobal, "global", FunctionLength::ZERO);
1563 JSHandle<JSTaggedValue> globalKey(factory_->NewFromCanBeCompressString("global"));
1564 SetGetter(regPrototype, globalKey, globalGetter);
1565
1566 JSHandle<JSTaggedValue> ignoreCaseGetter =
1567 CreateGetter(env, RegExp::GetIgnoreCase, "ignoreCase", FunctionLength::ZERO);
1568 JSHandle<JSTaggedValue> ignoreCaseKey(factory_->NewFromCanBeCompressString("ignoreCase"));
1569 SetGetter(regPrototype, ignoreCaseKey, ignoreCaseGetter);
1570
1571 JSHandle<JSTaggedValue> multilineGetter =
1572 CreateGetter(env, RegExp::GetMultiline, "multiline", FunctionLength::ZERO);
1573 JSHandle<JSTaggedValue> multilineKey(factory_->NewFromCanBeCompressString("multiline"));
1574 SetGetter(regPrototype, multilineKey, multilineGetter);
1575
1576 JSHandle<JSTaggedValue> dotAllGetter = CreateGetter(env, RegExp::GetDotAll, "dotAll", FunctionLength::ZERO);
1577 JSHandle<JSTaggedValue> dotAllKey(factory_->NewFromCanBeCompressString("dotAll"));
1578 SetGetter(regPrototype, dotAllKey, dotAllGetter);
1579
1580 JSHandle<JSTaggedValue> stickyGetter = CreateGetter(env, RegExp::GetSticky, "sticky", FunctionLength::ZERO);
1581 JSHandle<JSTaggedValue> stickyKey(factory_->NewFromCanBeCompressString("sticky"));
1582 SetGetter(regPrototype, stickyKey, stickyGetter);
1583
1584 JSHandle<JSTaggedValue> unicodeGetter = CreateGetter(env, RegExp::GetUnicode, "unicode", FunctionLength::ZERO);
1585 JSHandle<JSTaggedValue> unicodeKey(factory_->NewFromCanBeCompressString("unicode"));
1586 SetGetter(regPrototype, unicodeKey, unicodeGetter);
1587
1588 // Set RegExp [ @@species ]
1589 JSHandle<JSTaggedValue> speciesSymbol = env->GetSpeciesSymbol();
1590 JSHandle<JSTaggedValue> speciesGetter =
1591 CreateGetter(env, BuiltinsMap::Species, "[Symbol.species]", FunctionLength::ZERO);
1592 SetGetter(JSHandle<JSObject>(regexpFunction), speciesSymbol, speciesGetter);
1593
1594 // Set RegExp.prototype[@@split]
1595 SetFunctionAtSymbol(env, regPrototype, env->GetSplitSymbol(), "[Symbol.split]", RegExp::Split, FunctionLength::TWO);
1596 // Set RegExp.prototype[@@search]
1597 SetFunctionAtSymbol(env, regPrototype, env->GetSearchSymbol(), "[Symbol.search]", RegExp::Search,
1598 FunctionLength::ONE);
1599 // Set RegExp.prototype[@@match]
1600 SetFunctionAtSymbol(env, regPrototype, env->GetMatchSymbol(), "[Symbol.match]", RegExp::Match, FunctionLength::ONE);
1601 // Set RegExp.prototype[@@replace]
1602 SetFunctionAtSymbol(env, regPrototype, env->GetReplaceSymbol(), "[Symbol.replace]", RegExp::Replace,
1603 FunctionLength::TWO);
1604
1605 env->SetRegExpFunction(thread_, regexpFunction);
1606 auto globalConst = const_cast<GlobalEnvConstants *>(thread_->GlobalConstants());
1607 globalConst->SetConstant(ConstantIndex::JS_REGEXP_CLASS_INDEX, regexpFuncInstanceDynclass.GetTaggedValue());
1608 }
1609
InitializeArray(const JSHandle<GlobalEnv> & env,const JSHandle<JSTaggedValue> & objFuncPrototypeVal) const1610 void Builtins::InitializeArray(const JSHandle<GlobalEnv> &env, const JSHandle<JSTaggedValue> &objFuncPrototypeVal) const
1611 {
1612 [[maybe_unused]] EcmaHandleScope scope(thread_);
1613 // Arraybase.prototype
1614 JSHandle<JSHClass> arrBaseFuncInstanceDynclass = factory_->CreateJSArrayInstanceClass(objFuncPrototypeVal);
1615
1616 // Array.prototype
1617 JSHandle<JSObject> arrFuncPrototype = factory_->NewJSObject(arrBaseFuncInstanceDynclass);
1618 JSHandle<JSArray>::Cast(arrFuncPrototype)->SetLength(thread_, JSTaggedValue(FunctionLength::ZERO));
1619 auto accessor = thread_->GlobalConstants()->GetArrayLengthAccessor();
1620 JSArray::Cast(*arrFuncPrototype)->SetPropertyInlinedProps(thread_, JSArray::LENGTH_INLINE_PROPERTY_INDEX, accessor);
1621 JSHandle<JSTaggedValue> arrFuncPrototypeValue(arrFuncPrototype);
1622
1623 // Array.prototype_or_dynclass
1624 JSHandle<JSHClass> arrFuncInstanceDynclass = factory_->CreateJSArrayInstanceClass(arrFuncPrototypeValue);
1625
1626 // Array = new Function()
1627 JSHandle<JSObject> arrayFunction(
1628 NewBuiltinConstructor(env, arrFuncPrototype, BuiltinsArray::ArrayConstructor, "Array", FunctionLength::ONE));
1629 JSHandle<JSFunction> arrayFuncFunction(arrayFunction);
1630
1631 // Set the [[Realm]] internal slot of F to the running execution context's Realm
1632 JSHandle<LexicalEnv> lexicalEnv = factory_->NewLexicalEnv(0);
1633 lexicalEnv->SetParentEnv(thread_, env.GetTaggedValue());
1634 arrayFuncFunction->SetLexicalEnv(thread_, lexicalEnv.GetTaggedValue());
1635
1636 arrayFuncFunction->SetFunctionPrototype(thread_, arrFuncInstanceDynclass.GetTaggedValue());
1637
1638 // Array.prototype method
1639 SetFunction(env, arrFuncPrototype, "concat", BuiltinsArray::Concat, FunctionLength::ONE);
1640 SetFunction(env, arrFuncPrototype, "copyWithin", BuiltinsArray::CopyWithin, FunctionLength::TWO);
1641 SetFunction(env, arrFuncPrototype, "entries", BuiltinsArray::Entries, FunctionLength::ZERO);
1642 SetFunction(env, arrFuncPrototype, "every", BuiltinsArray::Every, FunctionLength::ONE);
1643 SetFunction(env, arrFuncPrototype, "fill", BuiltinsArray::Fill, FunctionLength::ONE);
1644 SetFunction(env, arrFuncPrototype, "filter", BuiltinsArray::Filter, FunctionLength::ONE);
1645 SetFunction(env, arrFuncPrototype, "find", BuiltinsArray::Find, FunctionLength::ONE);
1646 SetFunction(env, arrFuncPrototype, "findIndex", BuiltinsArray::FindIndex, FunctionLength::ONE);
1647 SetFunction(env, arrFuncPrototype, "forEach", BuiltinsArray::ForEach, FunctionLength::ONE);
1648 SetFunction(env, arrFuncPrototype, "indexOf", BuiltinsArray::IndexOf, FunctionLength::ONE);
1649 SetFunction(env, arrFuncPrototype, "join", BuiltinsArray::Join, FunctionLength::ONE);
1650 SetFunction(env, arrFuncPrototype, "keys", BuiltinsArray::Keys, FunctionLength::ZERO);
1651 SetFunction(env, arrFuncPrototype, "lastIndexOf", BuiltinsArray::LastIndexOf, FunctionLength::ONE);
1652 SetFunction(env, arrFuncPrototype, "map", BuiltinsArray::Map, FunctionLength::ONE);
1653 SetFunction(env, arrFuncPrototype, "pop", BuiltinsArray::Pop, FunctionLength::ZERO);
1654 SetFunction(env, arrFuncPrototype, "push", BuiltinsArray::Push, FunctionLength::ONE);
1655 SetFunction(env, arrFuncPrototype, "reduce", BuiltinsArray::Reduce, FunctionLength::ONE);
1656 SetFunction(env, arrFuncPrototype, "reduceRight", BuiltinsArray::ReduceRight, FunctionLength::ONE);
1657 SetFunction(env, arrFuncPrototype, "reverse", BuiltinsArray::Reverse, FunctionLength::ZERO);
1658 SetFunction(env, arrFuncPrototype, "shift", BuiltinsArray::Shift, FunctionLength::ZERO);
1659 SetFunction(env, arrFuncPrototype, "slice", BuiltinsArray::Slice, FunctionLength::TWO);
1660 SetFunction(env, arrFuncPrototype, "some", BuiltinsArray::Some, FunctionLength::ONE);
1661 SetFunction(env, arrFuncPrototype, "sort", BuiltinsArray::Sort, FunctionLength::ONE);
1662 SetFunction(env, arrFuncPrototype, "splice", BuiltinsArray::Splice, FunctionLength::TWO);
1663 SetFunction(env, arrFuncPrototype, thread_->GlobalConstants()->GetHandledToLocaleStringString(),
1664 BuiltinsArray::ToLocaleString, FunctionLength::ZERO);
1665 SetFunction(env, arrFuncPrototype, thread_->GlobalConstants()->GetHandledToStringString(), BuiltinsArray::ToString,
1666 FunctionLength::ZERO);
1667 SetFunction(env, arrFuncPrototype, "unshift", BuiltinsArray::Unshift, FunctionLength::ONE);
1668 SetFunction(env, arrFuncPrototype, "values", BuiltinsArray::Values, FunctionLength::ZERO);
1669
1670 // %ArrayPrototype% [ @@iterator ]
1671 JSHandle<JSTaggedValue> values(factory_->NewFromCanBeCompressString("values"));
1672 JSHandle<JSTaggedValue> iteratorSymbol = env->GetIteratorSymbol();
1673 JSHandle<JSTaggedValue> valuesFunc =
1674 JSObject::GetMethod(thread_, JSHandle<JSTaggedValue>::Cast(arrFuncPrototype), values);
1675 PropertyDescriptor iteartorDesc(thread_, valuesFunc, true, false, true);
1676 JSObject::DefineOwnProperty(thread_, arrFuncPrototype, iteratorSymbol, iteartorDesc);
1677
1678 // Array method
1679 SetFunction(env, arrayFunction, "from", BuiltinsArray::From, FunctionLength::ONE);
1680 SetFunction(env, arrayFunction, "isArray", BuiltinsArray::IsArray, FunctionLength::ONE);
1681 SetFunction(env, arrayFunction, "of", BuiltinsArray::Of, FunctionLength::ZERO);
1682
1683 // 22.1.2.5 get %Array% [ @@species ]
1684 JSHandle<JSTaggedValue> speciesSymbol = env->GetSpeciesSymbol();
1685 JSHandle<JSTaggedValue> speciesGetter =
1686 CreateGetter(env, BuiltinsArray::Species, "[Symbol.species]", FunctionLength::ZERO);
1687 SetGetter(JSHandle<JSObject>(arrayFunction), speciesSymbol, speciesGetter);
1688
1689 const int arrProtoLen = 0;
1690 JSHandle<JSTaggedValue> key_string = thread_->GlobalConstants()->GetHandledLengthString();
1691 PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>(thread_, JSTaggedValue(arrProtoLen)), true, false,
1692 false);
1693 JSObject::DefineOwnProperty(thread_, arrFuncPrototype, key_string, descriptor);
1694
1695 JSHandle<JSTaggedValue> valuesKey(factory_->NewFromCanBeCompressString("values"));
1696 PropertyDescriptor desc(thread_);
1697 JSObject::GetOwnProperty(thread_, arrFuncPrototype, valuesKey, desc);
1698
1699 // Array.prototype [ @@unscopables ]
1700 JSHandle<JSTaggedValue> unscopablesSymbol = env->GetUnscopablesSymbol();
1701 JSHandle<JSTaggedValue> unscopablesGetter =
1702 CreateGetter(env, BuiltinsArray::Unscopables, "[Symbol.unscopables]", FunctionLength::ZERO);
1703 SetGetter(JSHandle<JSObject>(arrFuncPrototype), unscopablesSymbol, unscopablesGetter);
1704
1705 env->SetArrayProtoValuesFunction(thread_, desc.GetValue());
1706 env->SetArrayFunction(thread_, arrayFunction);
1707 env->SetArrayPrototype(thread_, arrFuncPrototype);
1708 }
1709
InitializeTypedArray(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncDynclass) const1710 void Builtins::InitializeTypedArray(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncDynclass) const
1711 {
1712 [[maybe_unused]] EcmaHandleScope scope(thread_);
1713 // TypedArray.prototype
1714 JSHandle<JSObject> typedArrFuncPrototype = factory_->NewJSObject(objFuncDynclass);
1715 JSHandle<JSTaggedValue> typedArrFuncPrototypeValue(typedArrFuncPrototype);
1716
1717 // TypedArray.prototype_or_dynclass
1718 JSHandle<JSHClass> typedArrFuncInstanceDynclass = factory_->NewEcmaDynClass(
1719 panda::ecmascript::JSTypedArray::SIZE, JSType::JS_TYPED_ARRAY, typedArrFuncPrototypeValue);
1720
1721 // TypedArray = new Function()
1722 JSHandle<JSObject> typedArrayFunction(NewBuiltinConstructor(
1723 env, typedArrFuncPrototype, BuiltinsTypedArray::TypedArrayBaseConstructor, "TypedArray", FunctionLength::ZERO));
1724
1725 JSHandle<JSFunction>(typedArrayFunction)
1726 ->SetProtoOrDynClass(thread_, typedArrFuncInstanceDynclass.GetTaggedValue());
1727
1728 // TypedArray.prototype method
1729 SetFunction(env, typedArrFuncPrototype, "copyWithin", BuiltinsTypedArray::CopyWithin, FunctionLength::TWO);
1730 SetFunction(env, typedArrFuncPrototype, "entries", BuiltinsTypedArray::Entries, FunctionLength::ZERO);
1731 SetFunction(env, typedArrFuncPrototype, "every", BuiltinsTypedArray::Every, FunctionLength::ONE);
1732 SetFunction(env, typedArrFuncPrototype, "fill", BuiltinsTypedArray::Fill, FunctionLength::ONE);
1733 SetFunction(env, typedArrFuncPrototype, "filter", BuiltinsTypedArray::Filter, FunctionLength::ONE);
1734 SetFunction(env, typedArrFuncPrototype, "find", BuiltinsTypedArray::Find, FunctionLength::ONE);
1735 SetFunction(env, typedArrFuncPrototype, "findIndex", BuiltinsTypedArray::FindIndex, FunctionLength::ONE);
1736 SetFunction(env, typedArrFuncPrototype, "forEach", BuiltinsTypedArray::ForEach, FunctionLength::ONE);
1737 SetFunction(env, typedArrFuncPrototype, "indexOf", BuiltinsTypedArray::IndexOf, FunctionLength::ONE);
1738 SetFunction(env, typedArrFuncPrototype, "join", BuiltinsTypedArray::Join, FunctionLength::ONE);
1739 SetFunction(env, typedArrFuncPrototype, "keys", BuiltinsTypedArray::Keys, FunctionLength::ZERO);
1740 SetFunction(env, typedArrFuncPrototype, "lastIndexOf", BuiltinsTypedArray::LastIndexOf, FunctionLength::ONE);
1741 SetFunction(env, typedArrFuncPrototype, "map", BuiltinsTypedArray::Map, FunctionLength::ONE);
1742 SetFunction(env, typedArrFuncPrototype, "reduce", BuiltinsTypedArray::Reduce, FunctionLength::ONE);
1743 SetFunction(env, typedArrFuncPrototype, "reduceRight", BuiltinsTypedArray::ReduceRight, FunctionLength::ONE);
1744 SetFunction(env, typedArrFuncPrototype, "reverse", BuiltinsTypedArray::Reverse, FunctionLength::ZERO);
1745 SetFunction(env, typedArrFuncPrototype, "set", BuiltinsTypedArray::Set, FunctionLength::ONE);
1746 SetFunction(env, typedArrFuncPrototype, "slice", BuiltinsTypedArray::Slice, FunctionLength::TWO);
1747 SetFunction(env, typedArrFuncPrototype, "some", BuiltinsTypedArray::Some, FunctionLength::ONE);
1748 SetFunction(env, typedArrFuncPrototype, "sort", BuiltinsTypedArray::Sort, FunctionLength::ONE);
1749 SetFunction(env, typedArrFuncPrototype, "subarray", BuiltinsTypedArray::Subarray, FunctionLength::TWO);
1750 SetFunction(env, typedArrFuncPrototype, thread_->GlobalConstants()->GetHandledToLocaleStringString(),
1751 BuiltinsTypedArray::ToLocaleString, FunctionLength::ZERO);
1752 SetFunction(env, typedArrFuncPrototype, "values", BuiltinsTypedArray::Values, FunctionLength::ZERO);
1753
1754 JSHandle<JSTaggedValue> bufferGetter =
1755 CreateGetter(env, BuiltinsTypedArray::GetBuffer, "buffer", FunctionLength::ZERO);
1756 JSHandle<JSTaggedValue> bufferKey(factory_->NewFromCanBeCompressString("buffer"));
1757 SetGetter(typedArrFuncPrototype, bufferKey, bufferGetter);
1758
1759 JSHandle<JSTaggedValue> byteLengthGetter =
1760 CreateGetter(env, BuiltinsTypedArray::GetByteLength, "byteLength", FunctionLength::ZERO);
1761 JSHandle<JSTaggedValue> byteLengthKey(factory_->NewFromCanBeCompressString("byteLength"));
1762 SetGetter(typedArrFuncPrototype, byteLengthKey, byteLengthGetter);
1763
1764 JSHandle<JSTaggedValue> byteOffsetGetter =
1765 CreateGetter(env, BuiltinsTypedArray::GetByteOffset, "byteOffset", FunctionLength::ZERO);
1766 JSHandle<JSTaggedValue> byteOffsetKey(factory_->NewFromCanBeCompressString("byteOffset"));
1767 SetGetter(typedArrFuncPrototype, byteOffsetKey, byteOffsetGetter);
1768
1769 JSHandle<JSTaggedValue> lengthGetter =
1770 CreateGetter(env, BuiltinsTypedArray::GetLength, "length", FunctionLength::ZERO);
1771 JSHandle<JSTaggedValue> lengthKey(factory_->NewFromCanBeCompressString("length"));
1772 SetGetter(typedArrFuncPrototype, lengthKey, lengthGetter);
1773
1774 // %TypedArray%.prototype.toString()
1775 JSHandle<JSTaggedValue> arrFuncPrototype = env->GetArrayPrototype();
1776 JSHandle<JSTaggedValue> toStringFunc =
1777 JSObject::GetMethod(thread_, arrFuncPrototype, thread_->GlobalConstants()->GetHandledToStringString());
1778 PropertyDescriptor toStringDesc(thread_, toStringFunc, true, false, true);
1779 JSObject::DefineOwnProperty(thread_, typedArrFuncPrototype, thread_->GlobalConstants()->GetHandledToStringString(),
1780 toStringDesc);
1781
1782 // %TypedArray%.prototype [ @@iterator ] ( )
1783 JSHandle<JSTaggedValue> values(factory_->NewFromCanBeCompressString("values"));
1784 JSHandle<JSTaggedValue> iteratorSymbol = env->GetIteratorSymbol();
1785 JSHandle<JSTaggedValue> valuesFunc =
1786 JSObject::GetMethod(thread_, JSHandle<JSTaggedValue>::Cast(typedArrFuncPrototype), values);
1787 PropertyDescriptor iteartorDesc(thread_, valuesFunc, true, false, true);
1788 JSObject::DefineOwnProperty(thread_, typedArrFuncPrototype, iteratorSymbol, iteartorDesc);
1789
1790 // 22.2.3.31 get %TypedArray%.prototype [ @@toStringTag ]
1791 JSHandle<JSTaggedValue> toStringTagSymbol = env->GetToStringTagSymbol();
1792 JSHandle<JSTaggedValue> toStringTagGetter =
1793 CreateGetter(env, BuiltinsTypedArray::ToStringTag, "[Symbol.toStringTag]", FunctionLength::ZERO);
1794 SetGetter(typedArrFuncPrototype, toStringTagSymbol, toStringTagGetter);
1795
1796 // TypedArray method
1797 SetFunction(env, typedArrayFunction, "from", BuiltinsTypedArray::From, FunctionLength::ONE);
1798 SetFunction(env, typedArrayFunction, "of", BuiltinsTypedArray::Of, FunctionLength::ZERO);
1799
1800 // 22.2.2.4 get %TypedArray% [ @@species ]
1801 JSHandle<JSTaggedValue> speciesSymbol = env->GetSpeciesSymbol();
1802 JSHandle<JSTaggedValue> speciesGetter =
1803 CreateGetter(env, BuiltinsTypedArray::Species, "[Symbol.species]", FunctionLength::ZERO);
1804 SetGetter(JSHandle<JSObject>(typedArrayFunction), speciesSymbol, speciesGetter);
1805
1806 env->SetTypedArrayFunction(thread_, typedArrayFunction.GetTaggedValue());
1807 env->SetTypedArrayPrototype(thread_, typedArrFuncPrototype);
1808
1809 JSHandle<JSHClass> specificTypedArrayFuncClass =
1810 factory_->NewEcmaDynClass(JSFunction::SIZE, JSType::JS_FUNCTION, env->GetTypedArrayFunction());
1811 specificTypedArrayFuncClass->SetConstructor(true);
1812 JSHandle<JSFunction> function = JSHandle<JSFunction>::Cast(factory_->NewJSObject(specificTypedArrayFuncClass));
1813 function->SetBuiltinsCtorMode();
1814 env->SetSpecificTypedArrayFunctionClass(thread_, specificTypedArrayFuncClass);
1815
1816 InitializeInt8Array(env, typedArrFuncInstanceDynclass);
1817 InitializeUint8Array(env, typedArrFuncInstanceDynclass);
1818 InitializeUint8ClampedArray(env, typedArrFuncInstanceDynclass);
1819 InitializeInt16Array(env, typedArrFuncInstanceDynclass);
1820 InitializeUint16Array(env, typedArrFuncInstanceDynclass);
1821 InitializeInt32Array(env, typedArrFuncInstanceDynclass);
1822 InitializeUint32Array(env, typedArrFuncInstanceDynclass);
1823 InitializeFloat32Array(env, typedArrFuncInstanceDynclass);
1824 InitializeFloat64Array(env, typedArrFuncInstanceDynclass);
1825 }
1826
InitializeInt8Array(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncDynclass) const1827 void Builtins::InitializeInt8Array(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncDynclass) const
1828 {
1829 [[maybe_unused]] EcmaHandleScope scope(thread_);
1830 // Int8Array.prototype
1831 JSHandle<JSObject> int8ArrFuncPrototype = factory_->NewJSObject(objFuncDynclass);
1832 JSHandle<JSTaggedValue> int8ArrFuncPrototypeValue(int8ArrFuncPrototype);
1833
1834 // Int8Array.prototype_or_dynclass
1835 JSHandle<JSHClass> int8ArrFuncInstanceDynclass = factory_->NewEcmaDynClass(
1836 panda::ecmascript::JSTypedArray::SIZE, JSType::JS_INT8_ARRAY, int8ArrFuncPrototypeValue);
1837
1838 // Int8Array = new Function()
1839 JSHandle<JSFunction> int8ArrayFunction = factory_->NewSpecificTypedArrayFunction(
1840 env, reinterpret_cast<void *>(BuiltinsTypedArray::Int8ArrayConstructor));
1841 InitializeCtor(env, int8ArrFuncPrototype, int8ArrayFunction, "Int8Array", FunctionLength::THREE);
1842
1843 int8ArrayFunction->SetProtoOrDynClass(thread_, int8ArrFuncInstanceDynclass.GetTaggedValue());
1844
1845 const int bytesPerElement = 1;
1846 SetConstant(int8ArrFuncPrototype, "BYTES_PER_ELEMENT", JSTaggedValue(bytesPerElement));
1847 SetConstant(JSHandle<JSObject>(int8ArrayFunction), "BYTES_PER_ELEMENT", JSTaggedValue(bytesPerElement));
1848 env->SetInt8ArrayFunction(thread_, int8ArrayFunction);
1849 }
1850
InitializeUint8Array(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncDynclass) const1851 void Builtins::InitializeUint8Array(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncDynclass) const
1852 {
1853 [[maybe_unused]] EcmaHandleScope scope(thread_);
1854 // Uint8Array.prototype
1855 JSHandle<JSObject> uint8ArrFuncPrototype = factory_->NewJSObject(objFuncDynclass);
1856 JSHandle<JSTaggedValue> uint8ArrFuncPrototypeValue(uint8ArrFuncPrototype);
1857
1858 // Uint8Array.prototype_or_dynclass
1859 JSHandle<JSHClass> uint8ArrFuncInstanceDynclass = factory_->NewEcmaDynClass(
1860 panda::ecmascript::JSTypedArray::SIZE, JSType::JS_UINT8_ARRAY, uint8ArrFuncPrototypeValue);
1861
1862 // Uint8Array = new Function()
1863 JSHandle<JSFunction> uint8ArrayFunction = factory_->NewSpecificTypedArrayFunction(
1864 env, reinterpret_cast<void *>(BuiltinsTypedArray::Uint8ArrayConstructor));
1865 InitializeCtor(env, uint8ArrFuncPrototype, uint8ArrayFunction, "Uint8Array", FunctionLength::THREE);
1866
1867 uint8ArrayFunction->SetProtoOrDynClass(thread_, uint8ArrFuncInstanceDynclass.GetTaggedValue());
1868
1869 const int bytesPerElement = 1;
1870 SetConstant(uint8ArrFuncPrototype, "BYTES_PER_ELEMENT", JSTaggedValue(bytesPerElement));
1871 SetConstant(JSHandle<JSObject>(uint8ArrayFunction), "BYTES_PER_ELEMENT", JSTaggedValue(bytesPerElement));
1872 env->SetUint8ArrayFunction(thread_, uint8ArrayFunction);
1873 }
1874
InitializeUint8ClampedArray(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncDynclass) const1875 void Builtins::InitializeUint8ClampedArray(const JSHandle<GlobalEnv> &env,
1876 const JSHandle<JSHClass> &objFuncDynclass) const
1877 {
1878 [[maybe_unused]] EcmaHandleScope scope(thread_);
1879 // Uint8ClampedArray.prototype
1880 JSHandle<JSObject> uint8ClampedArrFuncPrototype = factory_->NewJSObject(objFuncDynclass);
1881 JSHandle<JSTaggedValue> uint8ClampedArrFuncPrototypeValue(uint8ClampedArrFuncPrototype);
1882
1883 // Uint8ClampedArray.prototype_or_dynclass
1884 JSHandle<JSHClass> uint8ClampedArrFuncInstanceDynclass =
1885 factory_->NewEcmaDynClass(panda::ecmascript::JSTypedArray::SIZE, JSType::JS_UINT8_CLAMPED_ARRAY,
1886 uint8ClampedArrFuncPrototypeValue);
1887
1888 // Uint8ClampedArray = new Function()
1889 JSHandle<JSFunction> uint8ClampedArrayFunction = factory_->NewSpecificTypedArrayFunction(
1890 env, reinterpret_cast<void *>(BuiltinsTypedArray::Uint8ClampedArrayConstructor));
1891 InitializeCtor(env, uint8ClampedArrFuncPrototype, uint8ClampedArrayFunction, "Uint8ClampedArray",
1892 FunctionLength::THREE);
1893
1894 uint8ClampedArrayFunction->SetProtoOrDynClass(thread_, uint8ClampedArrFuncInstanceDynclass.GetTaggedValue());
1895
1896 const int bytesPerElement = 1;
1897 SetConstant(uint8ClampedArrFuncPrototype, "BYTES_PER_ELEMENT", JSTaggedValue(bytesPerElement));
1898 SetConstant(JSHandle<JSObject>(uint8ClampedArrayFunction), "BYTES_PER_ELEMENT", JSTaggedValue(bytesPerElement));
1899 env->SetUint8ClampedArrayFunction(thread_, uint8ClampedArrayFunction);
1900 }
1901
InitializeInt16Array(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncDynclass) const1902 void Builtins::InitializeInt16Array(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncDynclass) const
1903 {
1904 [[maybe_unused]] EcmaHandleScope scope(thread_);
1905 // Int16Array.prototype
1906 JSHandle<JSObject> int16ArrFuncPrototype = factory_->NewJSObject(objFuncDynclass);
1907 JSHandle<JSTaggedValue> int16ArrFuncPrototypeValue(int16ArrFuncPrototype);
1908
1909 // Int16Array.prototype_or_dynclass
1910 JSHandle<JSHClass> int16ArrFuncInstanceDynclass = factory_->NewEcmaDynClass(
1911 panda::ecmascript::JSTypedArray::SIZE, JSType::JS_INT16_ARRAY, int16ArrFuncPrototypeValue);
1912
1913 // Int16Array = new Function()
1914 JSHandle<JSFunction> int16ArrayFunction = factory_->NewSpecificTypedArrayFunction(
1915 env, reinterpret_cast<void *>(BuiltinsTypedArray::Int16ArrayConstructor));
1916 InitializeCtor(env, int16ArrFuncPrototype, int16ArrayFunction, "Int16Array", FunctionLength::THREE);
1917
1918 int16ArrayFunction->SetProtoOrDynClass(thread_, int16ArrFuncInstanceDynclass.GetTaggedValue());
1919
1920 const int bytesPerElement = 2;
1921 SetConstant(int16ArrFuncPrototype, "BYTES_PER_ELEMENT", JSTaggedValue(bytesPerElement));
1922 SetConstant(JSHandle<JSObject>(int16ArrayFunction), "BYTES_PER_ELEMENT", JSTaggedValue(bytesPerElement));
1923 env->SetInt16ArrayFunction(thread_, int16ArrayFunction);
1924 }
1925
InitializeUint16Array(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncDynclass) const1926 void Builtins::InitializeUint16Array(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncDynclass) const
1927 {
1928 [[maybe_unused]] EcmaHandleScope scope(thread_);
1929 // Uint16Array.prototype
1930 JSHandle<JSObject> uint16ArrFuncPrototype = factory_->NewJSObject(objFuncDynclass);
1931 JSHandle<JSTaggedValue> uint16ArrFuncPrototypeValue(uint16ArrFuncPrototype);
1932
1933 // Uint16Array.prototype_or_dynclass
1934 JSHandle<JSHClass> uint16ArrFuncInstanceDynclass = factory_->NewEcmaDynClass(
1935 panda::ecmascript::JSTypedArray::SIZE, JSType::JS_UINT16_ARRAY, uint16ArrFuncPrototypeValue);
1936
1937 // Uint16Array = new Function()
1938 JSHandle<JSFunction> uint16ArrayFunction = factory_->NewSpecificTypedArrayFunction(
1939 env, reinterpret_cast<void *>(BuiltinsTypedArray::Uint16ArrayConstructor));
1940 InitializeCtor(env, uint16ArrFuncPrototype, uint16ArrayFunction, "Uint16Array", FunctionLength::THREE);
1941
1942 uint16ArrayFunction->SetProtoOrDynClass(thread_, uint16ArrFuncInstanceDynclass.GetTaggedValue());
1943
1944 const int bytesPerElement = 2;
1945 SetConstant(uint16ArrFuncPrototype, "BYTES_PER_ELEMENT", JSTaggedValue(bytesPerElement));
1946 SetConstant(JSHandle<JSObject>(uint16ArrayFunction), "BYTES_PER_ELEMENT", JSTaggedValue(bytesPerElement));
1947 env->SetUint16ArrayFunction(thread_, uint16ArrayFunction);
1948 }
1949
InitializeInt32Array(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncDynclass) const1950 void Builtins::InitializeInt32Array(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncDynclass) const
1951 {
1952 [[maybe_unused]] EcmaHandleScope scope(thread_);
1953 // Int32Array.prototype
1954 JSHandle<JSObject> int32ArrFuncPrototype = factory_->NewJSObject(objFuncDynclass);
1955 JSHandle<JSTaggedValue> int32ArrFuncPrototypeValue(int32ArrFuncPrototype);
1956
1957 // Int32Array.prototype_or_dynclass
1958 JSHandle<JSHClass> int32ArrFuncInstanceDynclass = factory_->NewEcmaDynClass(
1959 panda::ecmascript::JSTypedArray::SIZE, JSType::JS_INT32_ARRAY, int32ArrFuncPrototypeValue);
1960
1961 // Int32Array = new Function()
1962 JSHandle<JSFunction> int32ArrayFunction = factory_->NewSpecificTypedArrayFunction(
1963 env, reinterpret_cast<void *>(BuiltinsTypedArray::Int32ArrayConstructor));
1964 InitializeCtor(env, int32ArrFuncPrototype, int32ArrayFunction, "Int32Array", FunctionLength::THREE);
1965
1966 int32ArrayFunction->SetProtoOrDynClass(thread_, int32ArrFuncInstanceDynclass.GetTaggedValue());
1967
1968 const int bytesPerElement = 4;
1969 SetConstant(int32ArrFuncPrototype, "BYTES_PER_ELEMENT", JSTaggedValue(bytesPerElement));
1970 SetConstant(JSHandle<JSObject>(int32ArrayFunction), "BYTES_PER_ELEMENT", JSTaggedValue(bytesPerElement));
1971 env->SetInt32ArrayFunction(thread_, int32ArrayFunction);
1972 }
1973
InitializeUint32Array(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncDynclass) const1974 void Builtins::InitializeUint32Array(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncDynclass) const
1975 {
1976 [[maybe_unused]] EcmaHandleScope scope(thread_);
1977 // Uint32Array.prototype
1978 JSHandle<JSObject> uint32ArrFuncPrototype = factory_->NewJSObject(objFuncDynclass);
1979 JSHandle<JSTaggedValue> uint32ArrFuncPrototypeValue(uint32ArrFuncPrototype);
1980
1981 // Uint32Array.prototype_or_dynclass
1982 JSHandle<JSHClass> uint32ArrFuncInstanceDynclass = factory_->NewEcmaDynClass(
1983 panda::ecmascript::JSTypedArray::SIZE, JSType::JS_UINT32_ARRAY, uint32ArrFuncPrototypeValue);
1984
1985 // Uint32Array = new Function()
1986 JSHandle<JSFunction> uint32ArrayFunction = factory_->NewSpecificTypedArrayFunction(
1987 env, reinterpret_cast<void *>(BuiltinsTypedArray::Uint32ArrayConstructor));
1988 InitializeCtor(env, uint32ArrFuncPrototype, uint32ArrayFunction, "Uint32Array", FunctionLength::THREE);
1989
1990 uint32ArrayFunction->SetProtoOrDynClass(thread_, uint32ArrFuncInstanceDynclass.GetTaggedValue());
1991
1992 const int bytesPerElement = 4;
1993 SetConstant(uint32ArrFuncPrototype, "BYTES_PER_ELEMENT", JSTaggedValue(bytesPerElement));
1994 SetConstant(JSHandle<JSObject>(uint32ArrayFunction), "BYTES_PER_ELEMENT", JSTaggedValue(bytesPerElement));
1995 env->SetUint32ArrayFunction(thread_, uint32ArrayFunction);
1996 }
1997
InitializeFloat32Array(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncDynclass) const1998 void Builtins::InitializeFloat32Array(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncDynclass) const
1999 {
2000 [[maybe_unused]] EcmaHandleScope scope(thread_);
2001 // Float32Array.prototype
2002 JSHandle<JSObject> float32ArrFuncPrototype = factory_->NewJSObject(objFuncDynclass);
2003 JSHandle<JSTaggedValue> float32ArrFuncPrototypeValue(float32ArrFuncPrototype);
2004
2005 // Float32Array.prototype_or_dynclass
2006 JSHandle<JSHClass> float32ArrFuncInstanceDynclass = factory_->NewEcmaDynClass(
2007 panda::ecmascript::JSTypedArray::SIZE, JSType::JS_FLOAT32_ARRAY, float32ArrFuncPrototypeValue);
2008
2009 // Float32Array = new Function()
2010 JSHandle<JSFunction> float32ArrayFunction = factory_->NewSpecificTypedArrayFunction(
2011 env, reinterpret_cast<void *>(BuiltinsTypedArray::Float32ArrayConstructor));
2012 InitializeCtor(env, float32ArrFuncPrototype, float32ArrayFunction, "Float32Array", FunctionLength::THREE);
2013
2014 float32ArrayFunction->SetProtoOrDynClass(thread_, float32ArrFuncInstanceDynclass.GetTaggedValue());
2015
2016 const int bytesPerElement = 4;
2017 SetConstant(float32ArrFuncPrototype, "BYTES_PER_ELEMENT", JSTaggedValue(bytesPerElement));
2018 SetConstant(JSHandle<JSObject>(float32ArrayFunction), "BYTES_PER_ELEMENT", JSTaggedValue(bytesPerElement));
2019 env->SetFloat32ArrayFunction(thread_, float32ArrayFunction);
2020 }
2021
InitializeFloat64Array(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncDynclass) const2022 void Builtins::InitializeFloat64Array(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncDynclass) const
2023 {
2024 [[maybe_unused]] EcmaHandleScope scope(thread_);
2025 // Float64Array.prototype
2026 JSHandle<JSObject> float64ArrFuncPrototype = factory_->NewJSObject(objFuncDynclass);
2027 JSHandle<JSTaggedValue> float64ArrFuncPrototypeValue(float64ArrFuncPrototype);
2028
2029 // Float64Array.prototype_or_dynclass
2030 JSHandle<JSHClass> float64ArrFuncInstanceDynclass = factory_->NewEcmaDynClass(
2031 panda::ecmascript::JSTypedArray::SIZE, JSType::JS_FLOAT64_ARRAY, float64ArrFuncPrototypeValue);
2032
2033 // Float64Array = new Function()
2034 JSHandle<JSFunction> float64ArrayFunction = factory_->NewSpecificTypedArrayFunction(
2035 env, reinterpret_cast<void *>(BuiltinsTypedArray::Float64ArrayConstructor));
2036 InitializeCtor(env, float64ArrFuncPrototype, float64ArrayFunction, "Float64Array", FunctionLength::THREE);
2037
2038 float64ArrayFunction->SetProtoOrDynClass(thread_, float64ArrFuncInstanceDynclass.GetTaggedValue());
2039
2040 const int bytesPerElement = 8;
2041 SetConstant(float64ArrFuncPrototype, "BYTES_PER_ELEMENT", JSTaggedValue(bytesPerElement));
2042 SetConstant(JSHandle<JSObject>(float64ArrayFunction), "BYTES_PER_ELEMENT", JSTaggedValue(bytesPerElement));
2043 env->SetFloat64ArrayFunction(thread_, float64ArrayFunction);
2044 }
2045
InitializeArrayBuffer(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncDynclass) const2046 void Builtins::InitializeArrayBuffer(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncDynclass) const
2047 {
2048 [[maybe_unused]] EcmaHandleScope scope(thread_);
2049 // ArrayBuffer.prototype
2050 JSHandle<JSObject> arrayBufferFuncPrototype = factory_->NewJSObject(objFuncDynclass);
2051 JSHandle<JSTaggedValue> arrayBufferFuncPrototypeValue(arrayBufferFuncPrototype);
2052
2053 // ArrayBuffer.prototype_or_dynclass
2054 JSHandle<JSHClass> arrayBufferFuncInstanceDynclass =
2055 factory_->NewEcmaDynClass(JSArrayBuffer::SIZE, JSType::JS_ARRAY_BUFFER, arrayBufferFuncPrototypeValue);
2056
2057 // ArrayBuffer = new Function()
2058 JSHandle<JSObject> arrayBufferFunction(NewBuiltinConstructor(
2059 env, arrayBufferFuncPrototype, ArrayBuffer::ArrayBufferConstructor, "ArrayBuffer", FunctionLength::ONE));
2060
2061 JSHandle<JSFunction>(arrayBufferFunction)
2062 ->SetFunctionPrototype(thread_, arrayBufferFuncInstanceDynclass.GetTaggedValue());
2063
2064 // ArrayBuffer prototype method
2065 SetFunction(env, arrayBufferFuncPrototype, "slice", ArrayBuffer::Slice, FunctionLength::TWO);
2066
2067 // ArrayBuffer method
2068 SetFunction(env, arrayBufferFunction, "isView", ArrayBuffer::IsView, FunctionLength::ONE);
2069
2070 // 24.1.3.3 get ArrayBuffer[@@species]
2071 JSHandle<JSTaggedValue> speciesSymbol = env->GetSpeciesSymbol();
2072 JSHandle<JSTaggedValue> speciesGetter =
2073 CreateGetter(env, ArrayBuffer::Species, "[Symbol.species]", FunctionLength::ZERO);
2074 SetGetter(JSHandle<JSObject>(arrayBufferFunction), speciesSymbol, speciesGetter);
2075
2076 // 24.1.4.1 get ArrayBuffer.prototype.byteLength
2077 JSHandle<JSTaggedValue> lengthGetter =
2078 CreateGetter(env, ArrayBuffer::GetByteLength, "byteLength", FunctionLength::ZERO);
2079 JSHandle<JSTaggedValue> lengthKey(factory_->NewFromCanBeCompressString("byteLength"));
2080 SetGetter(arrayBufferFuncPrototype, lengthKey, lengthGetter);
2081
2082 // 24.1.4.4 ArrayBuffer.prototype[@@toStringTag]
2083 SetStringTagSymbol(env, arrayBufferFuncPrototype, "ArrayBuffer");
2084
2085 env->SetArrayBufferFunction(thread_, arrayBufferFunction.GetTaggedValue());
2086 }
2087
InitializeReflect(const JSHandle<GlobalEnv> & env,const JSHandle<JSTaggedValue> & objFuncPrototypeVal) const2088 void Builtins::InitializeReflect(const JSHandle<GlobalEnv> &env,
2089 const JSHandle<JSTaggedValue> &objFuncPrototypeVal) const
2090 {
2091 [[maybe_unused]] EcmaHandleScope scope(thread_);
2092 JSHandle<JSHClass> reflectDynclass =
2093 factory_->NewEcmaDynClass(JSObject::SIZE, JSType::JS_OBJECT, objFuncPrototypeVal);
2094 JSHandle<JSObject> reflectObject = factory_->NewJSObject(reflectDynclass);
2095
2096 SetFunction(env, reflectObject, "apply", Reflect::ReflectApply, FunctionLength::THREE);
2097 SetFunction(env, reflectObject, "construct", Reflect::ReflectConstruct, FunctionLength::TWO);
2098 SetFunction(env, reflectObject, "defineProperty", Reflect::ReflectDefineProperty, FunctionLength::THREE);
2099 SetFunction(env, reflectObject, "deleteProperty", Reflect::ReflectDeleteProperty, FunctionLength::TWO);
2100 SetFunction(env, reflectObject, "get", Reflect::ReflectGet, FunctionLength::TWO);
2101 SetFunction(env, reflectObject, "getOwnPropertyDescriptor", Reflect::ReflectGetOwnPropertyDescriptor,
2102 FunctionLength::TWO);
2103 SetFunction(env, reflectObject, "getPrototypeOf", Reflect::ReflectGetPrototypeOf, FunctionLength::ONE);
2104 SetFunction(env, reflectObject, "has", Reflect::ReflectHas, FunctionLength::TWO);
2105 SetFunction(env, reflectObject, "isExtensible", Reflect::ReflectIsExtensible, FunctionLength::ONE);
2106 SetFunction(env, reflectObject, "ownKeys", Reflect::ReflectOwnKeys, FunctionLength::ONE);
2107 SetFunction(env, reflectObject, "preventExtensions", Reflect::ReflectPreventExtensions, FunctionLength::ONE);
2108 SetFunction(env, reflectObject, "set", Reflect::ReflectSet, FunctionLength::THREE);
2109 SetFunction(env, reflectObject, "setPrototypeOf", Reflect::ReflectSetPrototypeOf, FunctionLength::TWO);
2110
2111 JSHandle<JSTaggedValue> reflectString(factory_->NewFromCanBeCompressString("Reflect"));
2112 JSHandle<JSObject> globalObject(thread_, env->GetGlobalObject());
2113 PropertyDescriptor reflectDesc(thread_, JSHandle<JSTaggedValue>::Cast(reflectObject), true, false, true);
2114 JSObject::DefineOwnProperty(thread_, globalObject, reflectString, reflectDesc);
2115
2116 // @@ToStringTag
2117 SetStringTagSymbol(env, reflectObject, "Reflect");
2118
2119 env->SetReflectFunction(thread_, reflectObject.GetTaggedValue());
2120 }
2121
InitializePromise(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & promiseFuncDynclass)2122 void Builtins::InitializePromise(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &promiseFuncDynclass)
2123 {
2124 [[maybe_unused]] EcmaHandleScope scope(thread_);
2125 // Promise.prototype
2126 JSHandle<JSObject> promiseFuncPrototype = factory_->NewJSObject(promiseFuncDynclass);
2127 JSHandle<JSTaggedValue> promiseFuncPrototypeValue(promiseFuncPrototype);
2128 // Promise.prototype_or_dynclass
2129 JSHandle<JSHClass> promiseFuncInstanceDynclass =
2130 factory_->NewEcmaDynClass(JSPromise::SIZE, JSType::JS_PROMISE, promiseFuncPrototypeValue);
2131 // Promise() = new Function()
2132 JSHandle<JSObject> promiseFunction(
2133 NewBuiltinConstructor(env, promiseFuncPrototype, Promise::PromiseConstructor, "Promise", FunctionLength::ONE));
2134 JSHandle<JSFunction>(promiseFunction)->SetFunctionPrototype(thread_, promiseFuncInstanceDynclass.GetTaggedValue());
2135
2136 // Promise method
2137 SetFunction(env, promiseFunction, "all", Promise::All, FunctionLength::ONE);
2138 SetFunction(env, promiseFunction, "race", Promise::Race, FunctionLength::ONE);
2139 SetFunction(env, promiseFunction, "resolve", Promise::Resolve, FunctionLength::ONE);
2140 SetFunction(env, promiseFunction, "reject", Promise::Reject, FunctionLength::ONE);
2141
2142 // promise.prototype method
2143 SetFunction(env, promiseFuncPrototype, "catch", Promise::Catch, FunctionLength::ONE);
2144 SetFunction(env, promiseFuncPrototype, "then", Promise::Then, FunctionLength::TWO);
2145
2146 // Promise.prototype [ @@toStringTag ]
2147 SetStringTagSymbol(env, promiseFuncPrototype, "Promise");
2148
2149 // Set Promise [@@species]
2150 JSHandle<JSTaggedValue> speciesSymbol(env->GetSpeciesSymbol());
2151 JSHandle<JSTaggedValue> speciesGetter =
2152 CreateGetter(env, Promise::GetSpecies, "[Symbol.species]", FunctionLength::ZERO);
2153 SetGetter(promiseFunction, speciesSymbol, speciesGetter);
2154
2155 env->SetPromiseFunction(thread_, promiseFunction);
2156 }
2157
InitializePromiseJob(const JSHandle<GlobalEnv> & env)2158 void Builtins::InitializePromiseJob(const JSHandle<GlobalEnv> &env)
2159 {
2160 JSHandle<JSTaggedValue> keyString(thread_->GlobalConstants()->GetHandledEmptyString());
2161 auto func = NewFunction(env, keyString, BuiltinsPromiseJob::PromiseReactionJob, FunctionLength::TWO);
2162 env->SetPromiseReactionJob(thread_, func);
2163 func = NewFunction(env, keyString, BuiltinsPromiseJob::PromiseResolveThenableJob, FunctionLength::THREE);
2164 env->SetPromiseResolveThenableJob(thread_, func);
2165 }
2166
InitializeDataView(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncDynclass) const2167 void Builtins::InitializeDataView(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncDynclass) const
2168 {
2169 [[maybe_unused]] EcmaHandleScope scope(thread_);
2170 // ArrayBuffer.prototype
2171 JSHandle<JSObject> dataViewFuncPrototype = factory_->NewJSObject(objFuncDynclass);
2172 JSHandle<JSTaggedValue> dataViewFuncPrototypeValue(dataViewFuncPrototype);
2173
2174 // ArrayBuffer.prototype_or_dynclass
2175 JSHandle<JSHClass> dataViewFuncInstanceDynclass =
2176 factory_->NewEcmaDynClass(JSDataView::SIZE, JSType::JS_DATA_VIEW, dataViewFuncPrototypeValue);
2177
2178 // ArrayBuffer = new Function()
2179 JSHandle<JSObject> dataViewFunction(NewBuiltinConstructor(env, dataViewFuncPrototype, DataView::DataViewConstructor,
2180 "DataView", FunctionLength::ONE));
2181
2182 JSHandle<JSFunction>(dataViewFunction)->SetProtoOrDynClass(thread_, dataViewFuncInstanceDynclass.GetTaggedValue());
2183 // DataView.prototype method
2184 SetFunction(env, dataViewFuncPrototype, "getFloat32", DataView::GetFloat32, FunctionLength::ONE);
2185 SetFunction(env, dataViewFuncPrototype, "getFloat64", DataView::GetFloat64, FunctionLength::ONE);
2186 SetFunction(env, dataViewFuncPrototype, "getInt8", DataView::GetInt8, FunctionLength::ONE);
2187 SetFunction(env, dataViewFuncPrototype, "getInt16", DataView::GetInt16, FunctionLength::ONE);
2188 SetFunction(env, dataViewFuncPrototype, "getInt32", DataView::GetInt32, FunctionLength::ONE);
2189 SetFunction(env, dataViewFuncPrototype, "getUint8", DataView::GetUint8, FunctionLength::ONE);
2190 SetFunction(env, dataViewFuncPrototype, "getUint16", DataView::GetUint16, FunctionLength::ONE);
2191 SetFunction(env, dataViewFuncPrototype, "getUint32", DataView::GetUint32, FunctionLength::ONE);
2192 SetFunction(env, dataViewFuncPrototype, "setFloat32", DataView::SetFloat32, FunctionLength::TWO);
2193 SetFunction(env, dataViewFuncPrototype, "setFloat64", DataView::SetFloat64, FunctionLength::TWO);
2194 SetFunction(env, dataViewFuncPrototype, "setInt8", DataView::SetInt8, FunctionLength::TWO);
2195 SetFunction(env, dataViewFuncPrototype, "setInt16", DataView::SetInt16, FunctionLength::TWO);
2196 SetFunction(env, dataViewFuncPrototype, "setInt32", DataView::SetInt32, FunctionLength::TWO);
2197 SetFunction(env, dataViewFuncPrototype, "setUint8", DataView::SetUint8, FunctionLength::TWO);
2198 SetFunction(env, dataViewFuncPrototype, "setUint16", DataView::SetUint16, FunctionLength::TWO);
2199 SetFunction(env, dataViewFuncPrototype, "setUint32", DataView::SetUint32, FunctionLength::TWO);
2200
2201 // 24.2.4.1 get DataView.prototype.buffer
2202 JSHandle<JSTaggedValue> bufferGetter = CreateGetter(env, DataView::GetBuffer, "buffer", FunctionLength::ZERO);
2203 JSHandle<JSTaggedValue> bufferKey(factory_->NewFromCanBeCompressString("buffer"));
2204 SetGetter(dataViewFuncPrototype, bufferKey, bufferGetter);
2205
2206 // 24.2.4.2 get DataView.prototype.byteLength
2207 JSHandle<JSTaggedValue> lengthGetter =
2208 CreateGetter(env, DataView::GetByteLength, "byteLength", FunctionLength::ZERO);
2209 JSHandle<JSTaggedValue> lengthKey(factory_->NewFromCanBeCompressString("byteLength"));
2210 SetGetter(dataViewFuncPrototype, lengthKey, lengthGetter);
2211
2212 // 24.2.4.3 get DataView.prototype.byteOffset
2213 JSHandle<JSTaggedValue> offsetGetter = CreateGetter(env, DataView::GetOffset, "byteOffset", FunctionLength::ZERO);
2214 JSHandle<JSTaggedValue> offsetKey(factory_->NewFromCanBeCompressString("byteOffset"));
2215 SetGetter(dataViewFuncPrototype, offsetKey, offsetGetter);
2216
2217 // 24.2.4.21 DataView.prototype[ @@toStringTag ]
2218 SetStringTagSymbol(env, dataViewFuncPrototype, "DataView");
2219 env->SetDataViewFunction(thread_, dataViewFunction.GetTaggedValue());
2220 }
2221
NewBuiltinConstructor(const JSHandle<GlobalEnv> & env,const JSHandle<JSObject> & prototype,EcmaEntrypoint ctorFunc,const char * name,int length) const2222 JSHandle<JSFunction> Builtins::NewBuiltinConstructor(const JSHandle<GlobalEnv> &env,
2223 const JSHandle<JSObject> &prototype, EcmaEntrypoint ctorFunc,
2224 const char *name, int length) const
2225 {
2226 JSHandle<JSFunction> ctor =
2227 factory_->NewJSFunction(env, reinterpret_cast<void *>(ctorFunc), FunctionKind::BUILTIN_CONSTRUCTOR);
2228 InitializeCtor(env, prototype, ctor, name, length);
2229 return ctor;
2230 }
2231
NewFunction(const JSHandle<GlobalEnv> & env,const JSHandle<JSTaggedValue> & key,EcmaEntrypoint func,int length) const2232 JSHandle<JSFunction> Builtins::NewFunction(const JSHandle<GlobalEnv> &env, const JSHandle<JSTaggedValue> &key,
2233 EcmaEntrypoint func, int length) const
2234 {
2235 JSHandle<JSFunction> function = factory_->NewJSFunction(env, reinterpret_cast<void *>(func));
2236 JSFunction::SetFunctionLength(thread_, function, JSTaggedValue(length));
2237 JSHandle<JSFunctionBase> baseFunction(function);
2238 JSHandle<JSTaggedValue> handleUndefine(thread_, JSTaggedValue::Undefined());
2239 JSFunction::SetFunctionName(thread_, baseFunction, key, handleUndefine);
2240 return function;
2241 }
2242
SetFunction(const JSHandle<GlobalEnv> & env,const JSHandle<JSObject> & obj,const char * key,EcmaEntrypoint func,int length) const2243 void Builtins::SetFunction(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &obj, const char *key,
2244 EcmaEntrypoint func, int length) const
2245 {
2246 JSHandle<JSTaggedValue> keyString(factory_->NewFromString(key));
2247 SetFunction(env, obj, keyString, func, length);
2248 }
2249
SetFunction(const JSHandle<GlobalEnv> & env,const JSHandle<JSObject> & obj,const JSHandle<JSTaggedValue> & key,EcmaEntrypoint func,int length) const2250 void Builtins::SetFunction(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &obj,
2251 const JSHandle<JSTaggedValue> &key, EcmaEntrypoint func, int length) const
2252 {
2253 JSHandle<JSFunction> function(NewFunction(env, key, func, length));
2254 PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>(function), true, false, true);
2255 JSObject::DefineOwnProperty(thread_, obj, key, descriptor);
2256 }
2257
SetFrozenFunction(const JSHandle<GlobalEnv> & env,const JSHandle<JSObject> & obj,const char * key,EcmaEntrypoint func,int length) const2258 void Builtins::SetFrozenFunction(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &obj, const char *key,
2259 EcmaEntrypoint func, int length) const
2260 {
2261 JSHandle<JSTaggedValue> keyString(factory_->NewFromString(key));
2262 JSHandle<JSFunction> function = NewFunction(env, keyString, func, length);
2263 PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>(function), false, false, false);
2264 JSObject::DefineOwnProperty(thread_, obj, keyString, descriptor);
2265 }
2266
2267 template<int flag>
SetFunctionAtSymbol(const JSHandle<GlobalEnv> & env,const JSHandle<JSObject> & obj,const JSHandle<JSTaggedValue> & symbol,const char * name,EcmaEntrypoint func,int length) const2268 void Builtins::SetFunctionAtSymbol(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &obj,
2269 const JSHandle<JSTaggedValue> &symbol, const char *name, EcmaEntrypoint func,
2270 int length) const
2271 {
2272 JSHandle<JSFunction> function = factory_->NewJSFunction(env, reinterpret_cast<void *>(func));
2273 JSFunction::SetFunctionLength(thread_, function, JSTaggedValue(length));
2274 JSHandle<JSTaggedValue> nameString(factory_->NewFromString(name));
2275 JSHandle<JSFunctionBase> baseFunction(function);
2276 JSHandle<JSTaggedValue> handleUndefine(thread_, JSTaggedValue::Undefined());
2277 JSFunction::SetFunctionName(thread_, baseFunction, nameString, handleUndefine);
2278 // NOLINTNEXTLINE(readability-braces-around-statements, bugprone-suspicious-semicolon)
2279 if constexpr (flag == JSSymbol::SYMBOL_TO_PRIMITIVE_TYPE) {
2280 PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>::Cast(function), false, false, true);
2281 JSObject::DefineOwnProperty(thread_, obj, symbol, descriptor);
2282 return;
2283 } else if constexpr (flag == JSSymbol::SYMBOL_HAS_INSTANCE_TYPE) { // NOLINTE(readability-braces-around-statements)
2284 // ecma 19.2.3.6 Function.prototype[@@hasInstance] has the attributes
2285 // { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.
2286 PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>::Cast(function), false, false, false);
2287 JSObject::DefineOwnProperty(thread_, obj, symbol, descriptor);
2288 return;
2289 }
2290 PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>::Cast(function), true, false, true);
2291 JSObject::DefineOwnProperty(thread_, obj, symbol, descriptor);
2292 }
2293
SetStringTagSymbol(const JSHandle<GlobalEnv> & env,const JSHandle<JSObject> & obj,const char * key) const2294 void Builtins::SetStringTagSymbol(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &obj, const char *key) const
2295 {
2296 JSHandle<JSTaggedValue> tag(factory_->NewFromString(key));
2297 JSHandle<JSTaggedValue> symbol = env->GetToStringTagSymbol();
2298 PropertyDescriptor desc(thread_, tag, false, false, true);
2299 JSObject::DefineOwnProperty(thread_, obj, symbol, desc);
2300 }
2301
CreateGetter(const JSHandle<GlobalEnv> & env,EcmaEntrypoint func,const char * name,int length) const2302 JSHandle<JSTaggedValue> Builtins::CreateGetter(const JSHandle<GlobalEnv> &env, EcmaEntrypoint func, const char *name,
2303 int length) const
2304 {
2305 JSHandle<JSFunction> function = factory_->NewJSFunction(env, reinterpret_cast<void *>(func));
2306 JSFunction::SetFunctionLength(thread_, function, JSTaggedValue(length));
2307 JSHandle<JSTaggedValue> funcName(factory_->NewFromString(name));
2308 JSHandle<JSTaggedValue> prefix = thread_->GlobalConstants()->GetHandledGetString();
2309 JSFunction::SetFunctionName(thread_, JSHandle<JSFunctionBase>(function), funcName, prefix);
2310 return JSHandle<JSTaggedValue>(function);
2311 }
2312
CreateSetter(const JSHandle<GlobalEnv> & env,EcmaEntrypoint func,const char * name,int length)2313 JSHandle<JSTaggedValue> Builtins::CreateSetter(const JSHandle<GlobalEnv> &env, EcmaEntrypoint func, const char *name,
2314 int length)
2315 {
2316 JSHandle<JSFunction> function = factory_->NewJSFunction(env, reinterpret_cast<void *>(func));
2317 JSFunction::SetFunctionLength(thread_, function, JSTaggedValue(length));
2318 JSHandle<JSTaggedValue> funcName(factory_->NewFromString(name));
2319 JSHandle<JSTaggedValue> prefix = thread_->GlobalConstants()->GetHandledSetString();
2320 JSFunction::SetFunctionName(thread_, JSHandle<JSFunctionBase>(function), funcName, prefix);
2321 return JSHandle<JSTaggedValue>(function);
2322 }
2323
SetConstant(const JSHandle<JSObject> & obj,const char * key,JSTaggedValue value) const2324 void Builtins::SetConstant(const JSHandle<JSObject> &obj, const char *key, JSTaggedValue value) const
2325 {
2326 JSHandle<JSTaggedValue> keyString(factory_->NewFromString(key));
2327 PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>(thread_, value), false, false, false);
2328 JSObject::DefineOwnProperty(thread_, obj, keyString, descriptor);
2329 }
2330
SetConstantObject(const JSHandle<JSObject> & obj,const char * key,JSHandle<JSTaggedValue> & value) const2331 void Builtins::SetConstantObject(const JSHandle<JSObject> &obj, const char *key, JSHandle<JSTaggedValue> &value) const
2332 {
2333 JSHandle<JSTaggedValue> keyString(factory_->NewFromString(key));
2334 PropertyDescriptor descriptor(thread_, value, false, false, false);
2335 JSObject::DefineOwnProperty(thread_, obj, keyString, descriptor);
2336 }
2337
SetGlobalThis(const JSHandle<JSObject> & obj,const char * key,const JSHandle<JSTaggedValue> & globalValue)2338 void Builtins::SetGlobalThis(const JSHandle<JSObject> &obj, const char *key, const JSHandle<JSTaggedValue> &globalValue)
2339 {
2340 JSHandle<JSTaggedValue> keyString(factory_->NewFromString(key));
2341 PropertyDescriptor descriptor(thread_, globalValue, true, false, true);
2342 JSObject::DefineOwnProperty(thread_, obj, keyString, descriptor);
2343 }
2344
SetAttribute(const JSHandle<JSObject> & obj,const char * key,const char * value) const2345 void Builtins::SetAttribute(const JSHandle<JSObject> &obj, const char *key, const char *value) const
2346 {
2347 JSHandle<JSTaggedValue> keyString(factory_->NewFromString(key));
2348 PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>(factory_->NewFromString(value)), true, false, true);
2349 JSObject::DefineOwnProperty(thread_, obj, keyString, descriptor);
2350 }
2351
SetNoneAttributeProperty(const JSHandle<JSObject> & obj,const char * key,const JSHandle<JSTaggedValue> & value) const2352 void Builtins::SetNoneAttributeProperty(const JSHandle<JSObject> &obj, const char *key,
2353 const JSHandle<JSTaggedValue> &value) const
2354 {
2355 JSHandle<JSTaggedValue> keyString(factory_->NewFromString(key));
2356 PropertyDescriptor des(thread_, value, false, false, false);
2357 JSObject::DefineOwnProperty(thread_, obj, keyString, des);
2358 }
2359
SetFuncToObjAndGlobal(const JSHandle<GlobalEnv> & env,const JSHandle<JSObject> & globalObject,const JSHandle<JSObject> & obj,const char * key,EcmaEntrypoint func,int length)2360 void Builtins::SetFuncToObjAndGlobal(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &globalObject,
2361 const JSHandle<JSObject> &obj, const char *key, EcmaEntrypoint func, int length)
2362 {
2363 JSHandle<JSFunction> function = factory_->NewJSFunction(env, reinterpret_cast<void *>(func));
2364 JSFunction::SetFunctionLength(thread_, function, JSTaggedValue(length));
2365 JSHandle<JSTaggedValue> keyString(factory_->NewFromString(key));
2366 JSHandle<JSFunctionBase> baseFunction(function);
2367 JSHandle<JSTaggedValue> handleUndefine(thread_, JSTaggedValue::Undefined());
2368 JSFunction::SetFunctionName(thread_, baseFunction, keyString, handleUndefine);
2369 PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>::Cast(function), true, false, true);
2370 JSObject::DefineOwnProperty(thread_, obj, keyString, descriptor);
2371 JSObject::DefineOwnProperty(thread_, globalObject, keyString, descriptor);
2372 }
2373
StrictModeForbiddenAccessCallerArguments(const JSHandle<GlobalEnv> & env,const JSHandle<JSObject> & prototype) const2374 void Builtins::StrictModeForbiddenAccessCallerArguments(const JSHandle<GlobalEnv> &env,
2375 const JSHandle<JSObject> &prototype) const
2376 {
2377 JSHandle<JSFunction> function =
2378 factory_->NewJSFunction(env, reinterpret_cast<void *>(JSFunction::AccessCallerArgumentsThrowTypeError));
2379
2380 JSHandle<JSTaggedValue> caller(factory_->NewFromCanBeCompressString("caller"));
2381 SetAccessor(prototype, caller, JSHandle<JSTaggedValue>::Cast(function), JSHandle<JSTaggedValue>::Cast(function));
2382
2383 JSHandle<JSTaggedValue> arguments(factory_->NewFromCanBeCompressString("arguments"));
2384 SetAccessor(prototype, arguments, JSHandle<JSTaggedValue>::Cast(function), JSHandle<JSTaggedValue>::Cast(function));
2385 }
2386
InitializeGeneratorFunction(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncDynclass) const2387 void Builtins::InitializeGeneratorFunction(const JSHandle<GlobalEnv> &env,
2388 const JSHandle<JSHClass> &objFuncDynclass) const
2389 {
2390 [[maybe_unused]] EcmaHandleScope scope(thread_);
2391 const GlobalEnvConstants *globalConst = thread_->GlobalConstants();
2392 JSHandle<JSObject> generatorFuncPrototype = factory_->NewJSObject(objFuncDynclass);
2393 JSHandle<JSTaggedValue> generatorFuncPrototypeValue(generatorFuncPrototype);
2394
2395 // 26.3.3.1 GeneratorFunction.prototype.constructor
2396 // GeneratorFunction.prototype_or_dynclass
2397 JSHandle<JSHClass> generatorFuncInstanceDynclass =
2398 factory_->NewEcmaDynClass(JSFunction::SIZE, JSType::JS_GENERATOR_FUNCTION, generatorFuncPrototypeValue);
2399 generatorFuncInstanceDynclass->SetCallable(true);
2400 generatorFuncInstanceDynclass->SetExtensible(true);
2401 // GeneratorFunction = new GeneratorFunction()
2402 JSHandle<JSFunction> generatorFunction =
2403 NewBuiltinConstructor(env, generatorFuncPrototype, GeneratorObject::GeneratorFunctionConstructor,
2404 "GeneratorFunction", FunctionLength::ONE);
2405 JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString();
2406 PropertyDescriptor generatorDesc(thread_, JSHandle<JSTaggedValue>::Cast(generatorFunction), false, false, true);
2407 JSObject::DefineOwnProperty(thread_, generatorFuncPrototype, constructorKey, generatorDesc);
2408 generatorFunction->SetProtoOrDynClass(thread_, generatorFuncInstanceDynclass.GetTaggedValue());
2409 env->SetGeneratorFunctionFunction(thread_, generatorFunction);
2410
2411 // 26.3.3.2 GeneratorFunction.prototype.prototype -> Generator prototype object.
2412 PropertyDescriptor descriptor(thread_, env->GetGeneratorPrototype(), false, false, true);
2413 JSObject::DefineOwnProperty(thread_, generatorFuncPrototype, globalConst->GetHandledPrototypeString(), descriptor);
2414
2415 // 26.3.3.3 GeneratorFunction.prototype[@@toStringTag]
2416 SetStringTagSymbol(env, generatorFuncPrototype, "GeneratorFunction");
2417
2418 // GeneratorFunction prototype __proto__ -> Function.
2419 JSObject::SetPrototype(thread_, generatorFuncPrototype, env->GetFunctionPrototype());
2420
2421 // 26.5.1.1 Generator.prototype.constructor -> %GeneratorFunction.prototype%.
2422 PropertyDescriptor generatorObjDesc(thread_, generatorFuncPrototypeValue, false, false, true);
2423 JSObject::DefineOwnProperty(thread_, JSHandle<JSObject>(env->GetInitialGenerator()),
2424 globalConst->GetHandledConstructorString(), generatorObjDesc);
2425
2426 // Generator instances prototype -> GeneratorFunction.prototype.prototype
2427 PropertyDescriptor generatorObjProtoDesc(thread_, generatorFuncPrototypeValue, true, false, false);
2428 JSObject::DefineOwnProperty(thread_, JSHandle<JSObject>(env->GetInitialGenerator()),
2429 globalConst->GetHandledPrototypeString(), generatorObjProtoDesc);
2430
2431 env->SetGeneratorFunctionPrototype(thread_, generatorFuncPrototype);
2432 }
2433
InitializeGenerator(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncDynclass) const2434 void Builtins::InitializeGenerator(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncDynclass) const
2435 {
2436 [[maybe_unused]] EcmaHandleScope scope(thread_);
2437 const GlobalEnvConstants *globalConst = thread_->GlobalConstants();
2438 JSHandle<JSObject> generatorFuncPrototype = factory_->NewJSObject(objFuncDynclass);
2439
2440 // GeneratorObject.prototype method
2441 // 26.5.1.2 Generator.prototype.next(value)
2442 SetFunction(env, generatorFuncPrototype, "next", GeneratorObject::GeneratorPrototypeNext, FunctionLength::ONE);
2443 // 26.5.1.3 Generator.prototype.return(value)
2444 SetFunction(env, generatorFuncPrototype, "return", GeneratorObject::GeneratorPrototypeReturn, FunctionLength::ONE);
2445 // 26.5.1.4 Generator.prototype.throw(exception)
2446 SetFunction(env, generatorFuncPrototype, "throw", GeneratorObject::GeneratorPrototypeThrow, FunctionLength::ONE);
2447
2448 // 26.5.1.5 Generator.prototype[@@toStringTag]
2449 SetStringTagSymbol(env, generatorFuncPrototype, "Generator");
2450
2451 // Generator with constructor, symbolTag, next/return/throw etc.
2452 PropertyDescriptor descriptor(thread_, env->GetIteratorPrototype(), true, false, false);
2453 JSObject::DefineOwnProperty(thread_, generatorFuncPrototype, globalConst->GetHandledPrototypeString(), descriptor);
2454 env->SetGeneratorPrototype(thread_, generatorFuncPrototype);
2455 JSObject::SetPrototype(thread_, generatorFuncPrototype, env->GetIteratorPrototype());
2456
2457 // Generator {}
2458 JSHandle<JSObject> initialGeneratorFuncPrototype = factory_->NewJSObject(objFuncDynclass);
2459 JSObject::SetPrototype(thread_, initialGeneratorFuncPrototype, JSHandle<JSTaggedValue>(generatorFuncPrototype));
2460 env->SetInitialGenerator(thread_, initialGeneratorFuncPrototype);
2461 }
2462
SetArgumentsSharedAccessor(const JSHandle<GlobalEnv> & env)2463 void Builtins::SetArgumentsSharedAccessor(const JSHandle<GlobalEnv> &env)
2464 {
2465 JSHandle<JSTaggedValue> throwFunction = env->GetThrowTypeError();
2466
2467 JSHandle<AccessorData> accessor = factory_->NewAccessorData();
2468 accessor->SetGetter(thread_, throwFunction);
2469 accessor->SetSetter(thread_, throwFunction);
2470 env->SetArgumentsCallerAccessor(thread_, accessor);
2471
2472 accessor = factory_->NewAccessorData();
2473 accessor->SetGetter(thread_, throwFunction);
2474 accessor->SetSetter(thread_, throwFunction);
2475 env->SetArgumentsCalleeAccessor(thread_, accessor);
2476 }
2477
SetAccessor(const JSHandle<JSObject> & obj,const JSHandle<JSTaggedValue> & key,const JSHandle<JSTaggedValue> & getter,const JSHandle<JSTaggedValue> & setter) const2478 void Builtins::SetAccessor(const JSHandle<JSObject> &obj, const JSHandle<JSTaggedValue> &key,
2479 const JSHandle<JSTaggedValue> &getter, const JSHandle<JSTaggedValue> &setter) const
2480 {
2481 JSHandle<AccessorData> accessor = factory_->NewAccessorData();
2482 accessor->SetGetter(thread_, getter);
2483 accessor->SetSetter(thread_, setter);
2484 PropertyAttributes attr = PropertyAttributes::DefaultAccessor(false, false, true);
2485 JSObject::AddAccessor(thread_, JSHandle<JSTaggedValue>::Cast(obj), key, accessor, attr);
2486 }
2487
SetGetter(const JSHandle<JSObject> & obj,const JSHandle<JSTaggedValue> & key,const JSHandle<JSTaggedValue> & getter) const2488 void Builtins::SetGetter(const JSHandle<JSObject> &obj, const JSHandle<JSTaggedValue> &key,
2489 const JSHandle<JSTaggedValue> &getter) const
2490 {
2491 JSHandle<AccessorData> accessor = factory_->NewAccessorData();
2492 accessor->SetGetter(thread_, getter);
2493 PropertyAttributes attr = PropertyAttributes::DefaultAccessor(false, false, true);
2494 JSObject::AddAccessor(thread_, JSHandle<JSTaggedValue>::Cast(obj), key, accessor, attr);
2495 }
2496
NewIntlConstructor(const JSHandle<GlobalEnv> & env,const JSHandle<JSObject> & prototype,EcmaEntrypoint ctorFunc,const char * name,int length)2497 JSHandle<JSFunction> Builtins::NewIntlConstructor(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &prototype,
2498 EcmaEntrypoint ctorFunc, const char *name, int length)
2499 {
2500 JSHandle<JSFunction> ctor =
2501 factory_->NewJSFunction(env, reinterpret_cast<void *>(ctorFunc), FunctionKind::BUILTIN_CONSTRUCTOR);
2502 InitializeIntlCtor(env, prototype, ctor, name, length);
2503 return ctor;
2504 }
2505
InitializeIntlCtor(const JSHandle<GlobalEnv> & env,const JSHandle<JSObject> & prototype,const JSHandle<JSFunction> & ctor,const char * name,int length)2506 void Builtins::InitializeIntlCtor(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &prototype,
2507 const JSHandle<JSFunction> &ctor, const char *name, int length)
2508 {
2509 const GlobalEnvConstants *globalConst = thread_->GlobalConstants();
2510 JSFunction::SetFunctionLength(thread_, ctor, JSTaggedValue(length));
2511 JSHandle<JSTaggedValue> nameString(factory_->NewFromString(name));
2512 JSFunction::SetFunctionName(thread_, JSHandle<JSFunctionBase>(ctor), nameString,
2513 JSHandle<JSTaggedValue>(thread_, JSTaggedValue::Undefined()));
2514 JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString();
2515 PropertyDescriptor descriptor1(thread_, JSHandle<JSTaggedValue>::Cast(ctor), true, false, true);
2516 JSObject::DefineOwnProperty(thread_, prototype, constructorKey, descriptor1);
2517
2518 // set "prototype" in constructor.
2519 ctor->SetFunctionPrototype(thread_, prototype.GetTaggedValue());
2520
2521 if (!JSTaggedValue::SameValue(nameString, thread_->GlobalConstants()->GetHandledAsyncFunctionString())) {
2522 JSHandle<JSObject> intlObject(thread_, env->GetIntlFunction().GetTaggedValue());
2523 PropertyDescriptor descriptor2(thread_, JSHandle<JSTaggedValue>::Cast(ctor), true, false, true);
2524 JSObject::DefineOwnProperty(thread_, intlObject, nameString, descriptor2);
2525 }
2526 }
2527
InitializeIntl(const JSHandle<GlobalEnv> & env,const JSHandle<JSTaggedValue> & objFuncPrototypeValue)2528 void Builtins::InitializeIntl(const JSHandle<GlobalEnv> &env, const JSHandle<JSTaggedValue> &objFuncPrototypeValue)
2529 {
2530 [[maybe_unused]] EcmaHandleScope scope(thread_);
2531 JSHandle<JSHClass> intlDynclass = factory_->NewEcmaDynClass(JSObject::SIZE, JSType::JS_INTL, objFuncPrototypeValue);
2532 JSHandle<JSObject> intlObject = factory_->NewJSObject(intlDynclass);
2533
2534 JSHandle<JSTaggedValue> initIntlSymbol(factory_->NewPublicSymbolWithChar("Symbol.IntlLegacyConstructedSymbol"));
2535 SetNoneAttributeProperty(intlObject, "fallbackSymbol", initIntlSymbol);
2536
2537 SetFunction(env, intlObject, "getCanonicalLocales", Intl::GetCanonicalLocales, FunctionLength::ONE);
2538
2539 // initial value of the "Intl" property of the global object.
2540 JSHandle<JSTaggedValue> intlString(factory_->NewFromString("Intl"));
2541 JSHandle<JSObject> globalObject(thread_, env->GetGlobalObject());
2542 PropertyDescriptor intlDesc(thread_, JSHandle<JSTaggedValue>::Cast(intlObject), true, false, true);
2543 JSObject::DefineOwnProperty(thread_, globalObject, intlString, intlDesc);
2544
2545 SetStringTagSymbol(env, intlObject, "Intl");
2546
2547 env->SetIntlFunction(thread_, intlObject);
2548 }
2549
InitializeDateTimeFormat(const JSHandle<GlobalEnv> & env)2550 void Builtins::InitializeDateTimeFormat(const JSHandle<GlobalEnv> &env)
2551 {
2552 [[maybe_unused]] EcmaHandleScope scope(thread_);
2553 // DateTimeFormat.prototype
2554 JSHandle<JSTaggedValue> objFun = env->GetObjectFunction();
2555 JSHandle<JSObject> dtfPrototype = factory_->NewJSObjectByConstructor(JSHandle<JSFunction>(objFun), objFun);
2556 JSHandle<JSTaggedValue> dtfPrototypeValue(dtfPrototype);
2557
2558 // DateTimeFormat.prototype_or_dynclass
2559 JSHandle<JSHClass> dtfFuncInstanceDynclass =
2560 factory_->NewEcmaDynClass(JSDateTimeFormat::SIZE, JSType::JS_DATE_TIME_FORMAT, dtfPrototypeValue);
2561
2562 // DateTimeFormat = new Function()
2563 // 13.4.1 Intl.DateTimeFormat.prototype.constructor
2564 JSHandle<JSObject> dtfFunction(NewIntlConstructor(env, dtfPrototype, DateTimeFormat::DateTimeFormatConstructor,
2565 "DateTimeFormat", FunctionLength::ZERO));
2566 JSHandle<JSFunction>(dtfFunction)->SetFunctionPrototype(thread_, JSTaggedValue(*dtfFuncInstanceDynclass));
2567
2568 // 13.3.2 Intl.DateTimeFormat.supportedLocalesOf ( locales [ , options ] )
2569 SetFunction(env, dtfFunction, "supportedLocalesOf", DateTimeFormat::SupportedLocalesOf, FunctionLength::ONE);
2570
2571 // DateTimeFormat.prototype method
2572 // 13.4.2 Intl.DateTimeFormat.prototype [ @@toStringTag ]
2573 SetStringTagSymbol(env, dtfPrototype, "Intl.DateTimeFormat");
2574 env->SetDateTimeFormatFunction(thread_, dtfFunction);
2575
2576 // 13.4.3 get Intl.DateTimeFormat.prototype.format
2577 JSHandle<JSTaggedValue> formatGetter = CreateGetter(env, DateTimeFormat::Format, "format", FunctionLength::ZERO);
2578 JSHandle<JSTaggedValue> formatSetter(thread_, JSTaggedValue::Undefined());
2579 SetAccessor(dtfPrototype, thread_->GlobalConstants()->GetHandledFormatString(), formatGetter, formatSetter);
2580
2581 // 13.4.4 Intl.DateTimeFormat.prototype.formatToParts ( date )
2582 SetFunction(env, dtfPrototype, "formatToParts", DateTimeFormat::FormatToParts, FunctionLength::ONE);
2583
2584 // 13.4.5 Intl.DateTimeFormat.prototype.resolvedOptions ()
2585 SetFunction(env, dtfPrototype, "resolvedOptions", DateTimeFormat::ResolvedOptions, FunctionLength::ZERO);
2586
2587 SetFunction(env, dtfPrototype, "formatRange", DateTimeFormat::FormatRange, FunctionLength::TWO);
2588
2589 SetFunction(env, dtfPrototype, "formatRangeToParts", DateTimeFormat::FormatRangeToParts, FunctionLength::TWO);
2590 }
2591
InitializeRelativeTimeFormat(const JSHandle<GlobalEnv> & env)2592 void Builtins::InitializeRelativeTimeFormat(const JSHandle<GlobalEnv> &env)
2593 {
2594 [[maybe_unused]] EcmaHandleScope scope(thread_);
2595 // RelativeTimeFormat.prototype
2596 JSHandle<JSTaggedValue> objFun = env->GetObjectFunction();
2597 JSHandle<JSObject> rtfPrototype = factory_->NewJSObjectByConstructor(JSHandle<JSFunction>(objFun), objFun);
2598 JSHandle<JSTaggedValue> rtfPrototypeValue(rtfPrototype);
2599
2600 // RelativeTimeFormat.prototype_or_dynclass
2601 JSHandle<JSHClass> rtfFuncInstanceDynclass =
2602 factory_->NewEcmaDynClass(JSRelativeTimeFormat::SIZE, JSType::JS_RELATIVE_TIME_FORMAT, rtfPrototypeValue);
2603
2604 // RelativeTimeFormat = new Function()
2605 // 14.2.1 Intl.RelativeTimeFormat.prototype.constructor
2606 JSHandle<JSObject> rtfFunction(NewIntlConstructor(env, rtfPrototype,
2607 RelativeTimeFormat::RelativeTimeFormatConstructor,
2608 "RelativeTimeFormat", FunctionLength::ZERO));
2609 JSHandle<JSFunction>(rtfFunction)->SetFunctionPrototype(thread_, JSTaggedValue(*rtfFuncInstanceDynclass));
2610
2611 // 14.3.2 Intl.RelativeTimeFormat.supportedLocalesOf ( locales [ , options ] )
2612 SetFunction(env, rtfFunction, "supportedLocalesOf", RelativeTimeFormat::SupportedLocalesOf, FunctionLength::ONE);
2613
2614 // RelativeTimeFormat.prototype method
2615 // 14.4.2 Intl.RelativeTimeFormat.prototype [ @@toStringTag ]
2616 SetStringTagSymbol(env, rtfPrototype, "Intl.RelativeTimeFormat");
2617 env->SetRelativeTimeFormatFunction(thread_, rtfFunction);
2618
2619 // 14.4.3 get Intl.RelativeTimeFormat.prototype.format
2620 SetFunction(env, rtfPrototype, "format", RelativeTimeFormat::Format, FunctionLength::TWO);
2621
2622 // 14.4.4 Intl.RelativeTimeFormat.prototype.formatToParts( value, unit )
2623 SetFunction(env, rtfPrototype, "formatToParts", RelativeTimeFormat::FormatToParts, FunctionLength::TWO);
2624
2625 // 14.4.5 Intl.RelativeTimeFormat.prototype.resolvedOptions ()
2626 SetFunction(env, rtfPrototype, "resolvedOptions", RelativeTimeFormat::ResolvedOptions, FunctionLength::ZERO);
2627 }
2628
InitializeNumberFormat(const JSHandle<GlobalEnv> & env)2629 void Builtins::InitializeNumberFormat(const JSHandle<GlobalEnv> &env)
2630 {
2631 [[maybe_unused]] EcmaHandleScope scope(thread_);
2632 // NumberFormat.prototype
2633 JSHandle<JSTaggedValue> objFun = env->GetObjectFunction();
2634 JSHandle<JSObject> nfPrototype = factory_->NewJSObjectByConstructor(JSHandle<JSFunction>(objFun), objFun);
2635 JSHandle<JSTaggedValue> nfPrototypeValue(nfPrototype);
2636
2637 // NumberFormat.prototype_or_dynclass
2638 JSHandle<JSHClass> nfFuncInstanceDynclass =
2639 factory_->NewEcmaDynClass(JSNumberFormat::SIZE, JSType::JS_NUMBER_FORMAT, nfPrototypeValue);
2640
2641 // NumberFormat = new Function()
2642 // 12.4.1 Intl.NumberFormat.prototype.constructor
2643 JSHandle<JSObject> nfFunction(NewIntlConstructor(env, nfPrototype, NumberFormat::NumberFormatConstructor,
2644 "NumberFormat", FunctionLength::ZERO));
2645 JSHandle<JSFunction>(nfFunction)->SetFunctionPrototype(thread_, JSTaggedValue(*nfFuncInstanceDynclass));
2646
2647 // 12.3.2 Intl.NumberFormat.supportedLocalesOf ( locales [ , options ] )
2648 SetFunction(env, nfFunction, "supportedLocalesOf", NumberFormat::SupportedLocalesOf, FunctionLength::ONE);
2649
2650 // NumberFormat.prototype method
2651 // 12.4.2 Intl.NumberFormat.prototype [ @@toStringTag ]
2652 SetStringTagSymbol(env, nfPrototype, "Intl.NumberFormat");
2653 env->SetNumberFormatFunction(thread_, nfFunction);
2654
2655 // 12.4.3 get Intl.NumberFormat.prototype.format
2656 JSHandle<JSTaggedValue> formatGetter = CreateGetter(env, NumberFormat::Format, "format", FunctionLength::ZERO);
2657 JSHandle<JSTaggedValue> formatSetter(thread_, JSTaggedValue::Undefined());
2658 SetAccessor(nfPrototype, thread_->GlobalConstants()->GetHandledFormatString(), formatGetter, formatSetter);
2659
2660 // 12.4.4 Intl.NumberFormat.prototype.formatToParts ( date )
2661 SetFunction(env, nfPrototype, "formatToParts", NumberFormat::FormatToParts, FunctionLength::ONE);
2662
2663 // 12.4.5 Intl.NumberFormat.prototype.resolvedOptions ()
2664 SetFunction(env, nfPrototype, "resolvedOptions", NumberFormat::ResolvedOptions, FunctionLength::ZERO);
2665 }
2666
InitializeLocale(const JSHandle<GlobalEnv> & env)2667 void Builtins::InitializeLocale(const JSHandle<GlobalEnv> &env)
2668 {
2669 [[maybe_unused]] EcmaHandleScope scope(thread_);
2670 // Locale.prototype
2671 JSHandle<JSTaggedValue> objFun = env->GetObjectFunction();
2672 JSHandle<JSObject> localePrototype = factory_->NewJSObjectByConstructor(JSHandle<JSFunction>(objFun), objFun);
2673 JSHandle<JSTaggedValue> localePrototypeValue(localePrototype);
2674
2675 // Locale.prototype_or_dynclass
2676 JSHandle<JSHClass> localeFuncInstanceDynclass =
2677 factory_->NewEcmaDynClass(JSLocale::SIZE, JSType::JS_LOCALE, localePrototypeValue);
2678
2679 // Locale = new Function()
2680 JSHandle<JSObject> localeFunction(
2681 NewIntlConstructor(env, localePrototype, Locale::LocaleConstructor, "Locale", FunctionLength::ONE));
2682 JSHandle<JSFunction>(localeFunction)->SetFunctionPrototype(thread_, JSTaggedValue(*localeFuncInstanceDynclass));
2683
2684 // Locale.prototype method
2685 SetFunction(env, localePrototype, "maximize", Locale::Maximize, FunctionLength::ZERO);
2686 SetFunction(env, localePrototype, "minimize", Locale::Minimize, FunctionLength::ZERO);
2687 SetFunction(env, localePrototype, "toString", Locale::ToString, FunctionLength::ZERO);
2688
2689 JSHandle<JSTaggedValue> baseNameGetter = CreateGetter(env, Locale::GetBaseName, "baseName", FunctionLength::ZERO);
2690 SetGetter(localePrototype, thread_->GlobalConstants()->GetHandledBaseNameString(), baseNameGetter);
2691
2692 JSHandle<JSTaggedValue> calendarGetter = CreateGetter(env, Locale::GetCalendar, "calendar", FunctionLength::ZERO);
2693 SetGetter(localePrototype, thread_->GlobalConstants()->GetHandledCalendarString(), calendarGetter);
2694
2695 JSHandle<JSTaggedValue> caseFirstGetter =
2696 CreateGetter(env, Locale::GetCaseFirst, "caseFirst", FunctionLength::ZERO);
2697 SetGetter(localePrototype, thread_->GlobalConstants()->GetHandledCaseFirstString(), caseFirstGetter);
2698
2699 JSHandle<JSTaggedValue> collationGetter =
2700 CreateGetter(env, Locale::GetCollation, "collation", FunctionLength::ZERO);
2701 SetGetter(localePrototype, thread_->GlobalConstants()->GetHandledCollationString(), collationGetter);
2702
2703 JSHandle<JSTaggedValue> hourCycleGetter =
2704 CreateGetter(env, Locale::GetHourCycle, "hourCycle", FunctionLength::ZERO);
2705 SetGetter(localePrototype, thread_->GlobalConstants()->GetHandledHourCycleString(), hourCycleGetter);
2706
2707 JSHandle<JSTaggedValue> numericGetter = CreateGetter(env, Locale::GetNumeric, "numeric", FunctionLength::ZERO);
2708 SetGetter(localePrototype, thread_->GlobalConstants()->GetHandledNumericString(), numericGetter);
2709
2710 JSHandle<JSTaggedValue> numberingSystemGetter =
2711 CreateGetter(env, Locale::GetNumberingSystem, "numberingSystem", FunctionLength::ZERO);
2712 SetGetter(localePrototype, thread_->GlobalConstants()->GetHandledNumberingSystemString(), numberingSystemGetter);
2713
2714 JSHandle<JSTaggedValue> languageGetter = CreateGetter(env, Locale::GetLanguage, "language", FunctionLength::ZERO);
2715 SetGetter(localePrototype, thread_->GlobalConstants()->GetHandledLanguageString(), languageGetter);
2716
2717 JSHandle<JSTaggedValue> scriptGetter = CreateGetter(env, Locale::GetScript, "script", FunctionLength::ZERO);
2718 SetGetter(localePrototype, thread_->GlobalConstants()->GetHandledScriptString(), scriptGetter);
2719
2720 JSHandle<JSTaggedValue> regionGetter = CreateGetter(env, Locale::GetRegion, "region", FunctionLength::ZERO);
2721 SetGetter(localePrototype, thread_->GlobalConstants()->GetHandledRegionString(), regionGetter);
2722
2723 // 10.3.2 Intl.Locale.prototype[ @@toStringTag ]
2724 SetStringTagSymbol(env, localePrototype, "Intl.Locale");
2725 env->SetLocaleFunction(thread_, localeFunction);
2726 }
2727
InitializeCollator(const JSHandle<GlobalEnv> & env)2728 void Builtins::InitializeCollator(const JSHandle<GlobalEnv> &env)
2729 {
2730 [[maybe_unused]] EcmaHandleScope scope(thread_);
2731 // Collator.prototype
2732 JSHandle<JSTaggedValue> objFun = env->GetObjectFunction();
2733 JSHandle<JSObject> collatorPrototype = factory_->NewJSObjectByConstructor(JSHandle<JSFunction>(objFun), objFun);
2734 JSHandle<JSTaggedValue> collatorPrototypeValue(collatorPrototype);
2735
2736 // Collator.prototype_or_dynclass
2737 JSHandle<JSHClass> collatorFuncInstanceDynclass =
2738 factory_->NewEcmaDynClass(JSCollator::SIZE, JSType::JS_COLLATOR, collatorPrototypeValue);
2739
2740 // Collator = new Function()
2741 // 11.1.2 Intl.Collator.prototype.constructor
2742 JSHandle<JSObject> collatorFunction(
2743 NewIntlConstructor(env, collatorPrototype, Collator::CollatorConstructor, "Collator", FunctionLength::ZERO));
2744 JSHandle<JSFunction>(collatorFunction)->SetFunctionPrototype(thread_, JSTaggedValue(*collatorFuncInstanceDynclass));
2745
2746 // 11.2.2 Intl.Collator.supportedLocalesOf ( locales [ , options ] )
2747 SetFunction(env, collatorFunction, "supportedLocalesOf", Collator::SupportedLocalesOf, FunctionLength::ONE);
2748
2749 // Collator.prototype method
2750 // 11.3.2 Intl.Collator.prototype [ @@toStringTag ]
2751 SetStringTagSymbol(env, collatorPrototype, "Intl.Collator");
2752 env->SetCollatorFunction(thread_, collatorFunction);
2753
2754 // 11.3.3 get Intl.Collator.prototype.compare
2755 JSHandle<JSTaggedValue> compareGetter = CreateGetter(env, Collator::Compare, "compare", FunctionLength::ZERO);
2756 JSHandle<JSTaggedValue> compareSetter(thread_, JSTaggedValue::Undefined());
2757 SetAccessor(collatorPrototype, thread_->GlobalConstants()->GetHandledCompareString(), compareGetter, compareSetter);
2758
2759 // 11.3.4 Intl.Collator.prototype.resolvedOptions ()
2760 SetFunction(env, collatorPrototype, "resolvedOptions", Collator::ResolvedOptions, FunctionLength::ZERO);
2761 }
2762
InitializePluralRules(const JSHandle<GlobalEnv> & env)2763 void Builtins::InitializePluralRules(const JSHandle<GlobalEnv> &env)
2764 {
2765 [[maybe_unused]] EcmaHandleScope scope(thread_);
2766 // PluralRules.prototype
2767 JSHandle<JSTaggedValue> objFun(env->GetObjectFunction());
2768 JSHandle<JSObject> prPrototype = factory_->NewJSObjectByConstructor(JSHandle<JSFunction>(objFun), objFun);
2769 JSHandle<JSTaggedValue> prPrototypeValue(prPrototype);
2770
2771 // PluralRules.prototype_or_dynclass
2772 JSHandle<JSHClass> prFuncInstanceDynclass =
2773 factory_->NewEcmaDynClass(JSPluralRules::SIZE, JSType::JS_PLURAL_RULES, prPrototypeValue);
2774
2775 // PluralRules = new Function()
2776 // 15.2.1 Intl.PluralRules.prototype.constructor
2777 JSHandle<JSObject> prFunction(
2778 NewIntlConstructor(env, prPrototype, PluralRules::PluralRulesConstructor, "PluralRules", FunctionLength::ZERO));
2779 JSHandle<JSFunction>(prFunction)->SetFunctionPrototype(thread_, JSTaggedValue(*prFuncInstanceDynclass));
2780
2781 // 15.3.2 Intl.PluralRules.supportedLocalesOf ( locales [ , options ] )
2782 SetFunction(env, prFunction, "supportedLocalesOf", PluralRules::SupportedLocalesOf, FunctionLength::ONE);
2783
2784 // PluralRules.prototype method
2785 // 15.4.2 Intl.PluralRules.prototype [ @@toStringTag ]
2786 SetStringTagSymbol(env, prPrototype, "Intl.PluralRules");
2787 env->SetPluralRulesFunction(thread_, prFunction);
2788
2789 // 15.4.3 get Intl.PluralRules.prototype.select
2790 SetFunction(env, prPrototype, "select", PluralRules::Select, FunctionLength::ONE);
2791
2792 // 15.4.5 Intl.PluralRules.prototype.resolvedOptions ()
2793 SetFunction(env, prPrototype, "resolvedOptions", PluralRules::ResolvedOptions, FunctionLength::ZERO);
2794 }
2795
InitializeArkTools(const JSHandle<GlobalEnv> & env) const2796 JSHandle<JSObject> Builtins::InitializeArkTools(const JSHandle<GlobalEnv> &env) const
2797 {
2798 JSHandle<JSObject> tools = factory_->NewEmptyJSObject();
2799 SetFunction(env, tools, "print", builtins::BuiltinsArkTools::ObjectDump, FunctionLength::ZERO);
2800 return tools;
2801 }
2802
InitializeArkPrivate(const JSHandle<GlobalEnv> & env) const2803 JSHandle<JSObject> Builtins::InitializeArkPrivate(const JSHandle<GlobalEnv> &env) const
2804 {
2805 JSHandle<JSObject> arkPrivate = factory_->NewEmptyJSObject();
2806 SetFrozenFunction(env, arkPrivate, "Load", ContainersPrivate::Load, FunctionLength::ZERO);
2807
2808 // It is used to provide non ECMA standard jsapi containers.
2809 SetConstant(arkPrivate, "ArrayList", JSTaggedValue(static_cast<int>(containers::ContainerTag::ArrayList)));
2810 SetConstant(arkPrivate, "Queue", JSTaggedValue(static_cast<int>(containers::ContainerTag::Queue)));
2811 SetConstant(arkPrivate, "Deque", JSTaggedValue(static_cast<int>(containers::ContainerTag::Deque)));
2812 SetConstant(arkPrivate, "Stack", JSTaggedValue(static_cast<int>(containers::ContainerTag::Stack)));
2813 SetConstant(arkPrivate, "Vector", JSTaggedValue(static_cast<int>(containers::ContainerTag::Vector)));
2814 SetConstant(arkPrivate, "List", JSTaggedValue(static_cast<int>(containers::ContainerTag::List)));
2815 SetConstant(arkPrivate, "LinkedList", JSTaggedValue(static_cast<int>(containers::ContainerTag::LinkedList)));
2816 SetConstant(arkPrivate, "TreeMap", JSTaggedValue(static_cast<int>(containers::ContainerTag::TreeMap)));
2817 SetConstant(arkPrivate, "TreeSet", JSTaggedValue(static_cast<int>(containers::ContainerTag::TreeSet)));
2818 SetConstant(arkPrivate, "HashMap", JSTaggedValue(static_cast<int>(containers::ContainerTag::HashMap)));
2819 SetConstant(arkPrivate, "HashSet", JSTaggedValue(static_cast<int>(containers::ContainerTag::HashSet)));
2820 SetConstant(arkPrivate, "LightWeightMap",
2821 JSTaggedValue(static_cast<int>(containers::ContainerTag::LightWeightMap)));
2822 SetConstant(arkPrivate, "LightWeightSet",
2823 JSTaggedValue(static_cast<int>(containers::ContainerTag::LightWeightSet)));
2824 SetConstant(arkPrivate, "PlainArray", JSTaggedValue(static_cast<int>(containers::ContainerTag::PlainArray)));
2825 return arkPrivate;
2826 }
2827 } // namespace panda::ecmascript
2828