1 /*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ecmascript/builtins/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_from_sync_iterator.h"
24 #include "ecmascript/builtins/builtins_async_function.h"
25 #include "ecmascript/builtins/builtins_async_iterator.h"
26 #include "ecmascript/builtins/builtins_async_generator.h"
27 #include "ecmascript/builtins/builtins_atomics.h"
28 #include "ecmascript/builtins/builtins_bigint.h"
29 #include "ecmascript/builtins/builtins_boolean.h"
30 #include "ecmascript/builtins/builtins_cjs_module.h"
31 #include "ecmascript/builtins/builtins_cjs_require.h"
32 #include "ecmascript/builtins/builtins_cjs_exports.h"
33 #include "ecmascript/builtins/builtins_collator.h"
34 #include "ecmascript/builtins/builtins_dataview.h"
35 #include "ecmascript/builtins/builtins_date.h"
36 #include "ecmascript/builtins/builtins_date_time_format.h"
37 #include "ecmascript/builtins/builtins_errors.h"
38 #include "ecmascript/builtins/builtins_finalization_registry.h"
39 #include "ecmascript/builtins/builtins_function.h"
40 #include "ecmascript/builtins/builtins_generator.h"
41 #include "ecmascript/builtins/builtins_global.h"
42 #include "ecmascript/builtins/builtins_intl.h"
43 #include "ecmascript/builtins/builtins_iterator.h"
44 #include "ecmascript/builtins/builtins_json.h"
45 #include "ecmascript/builtins/builtins_locale.h"
46 #include "ecmascript/builtins/builtins_map.h"
47 #include "ecmascript/builtins/builtins_math.h"
48 #include "ecmascript/builtins/builtins_number.h"
49 #include "ecmascript/builtins/builtins_number_format.h"
50 #include "ecmascript/builtins/builtins_object.h"
51 #include "ecmascript/builtins/builtins_plural_rules.h"
52 #include "ecmascript/builtins/builtins_displaynames.h"
53 #include "ecmascript/builtins/builtins_list_format.h"
54 #include "ecmascript/builtins/builtins_promise.h"
55 #include "ecmascript/builtins/builtins_promise_handler.h"
56 #include "ecmascript/builtins/builtins_promise_job.h"
57 #include "ecmascript/builtins/builtins_proxy.h"
58 #include "ecmascript/builtins/builtins_reflect.h"
59 #include "ecmascript/builtins/builtins_regexp.h"
60 #include "ecmascript/builtins/builtins_relative_time_format.h"
61 #include "ecmascript/builtins/builtins_set.h"
62 #include "ecmascript/builtins/builtins_sharedarraybuffer.h"
63 #include "ecmascript/builtins/builtins_string.h"
64 #include "ecmascript/builtins/builtins_string_iterator.h"
65 #include "ecmascript/builtins/builtins_symbol.h"
66 #include "ecmascript/builtins/builtins_typedarray.h"
67 #include "ecmascript/builtins/builtins_weak_map.h"
68 #include "ecmascript/builtins/builtins_weak_ref.h"
69 #include "ecmascript/builtins/builtins_weak_set.h"
70 #include "ecmascript/containers/containers_private.h"
71 #include "ecmascript/ecma_runtime_call_info.h"
72 #include "ecmascript/js_array.h"
73 #include "ecmascript/js_arraybuffer.h"
74 #include "ecmascript/js_array_iterator.h"
75 #include "ecmascript/js_async_function.h"
76 #include "ecmascript/js_async_generator_object.h"
77 #include "ecmascript/js_collator.h"
78 #include "ecmascript/js_dataview.h"
79 #include "ecmascript/js_date_time_format.h"
80 #include "ecmascript/js_for_in_iterator.h"
81 #include "ecmascript/js_finalization_registry.h"
82 #include "ecmascript/js_function.h"
83 #include "ecmascript/js_handle.h"
84 #include "ecmascript/js_hclass.h"
85 #include "ecmascript/js_locale.h"
86 #include "ecmascript/js_map.h"
87 #include "ecmascript/js_map_iterator.h"
88 #include "ecmascript/js_number_format.h"
89 #include "ecmascript/js_plural_rules.h"
90 #include "ecmascript/js_displaynames.h"
91 #include "ecmascript/js_list_format.h"
92 #include "ecmascript/js_primitive_ref.h"
93 #include "ecmascript/js_promise.h"
94 #include "ecmascript/js_regexp.h"
95 #include "ecmascript/js_regexp_iterator.h"
96 #include "ecmascript/js_relative_time_format.h"
97 #include "ecmascript/js_runtime_options.h"
98 #include "ecmascript/js_set.h"
99 #include "ecmascript/js_set_iterator.h"
100 #include "ecmascript/js_string_iterator.h"
101 #include "ecmascript/js_async_from_sync_iterator.h"
102 #include "ecmascript/js_tagged_value.h"
103 #include "ecmascript/js_typed_array.h"
104 #include "ecmascript/js_weak_container.h"
105 #include "ecmascript/js_weak_ref.h"
106 #include "ecmascript/mem/mem.h"
107 #include "ecmascript/module/js_module_namespace.h"
108 #include "ecmascript/require/js_cjs_module.h"
109 #include "ecmascript/require/js_cjs_module_cache.h"
110 #include "ecmascript/require/js_cjs_require.h"
111 #include "ecmascript/require/js_cjs_exports.h"
112 #include "ecmascript/napi/include/jsnapi.h"
113 #include "ecmascript/object_factory.h"
114
115 #include "ohos/init_data.h"
116
117 namespace panda::ecmascript {
118 using Number = builtins::BuiltinsNumber;
119 using BuiltinsBigInt = builtins::BuiltinsBigInt;
120 using Object = builtins::BuiltinsObject;
121 using Date = builtins::BuiltinsDate;
122 using Symbol = builtins::BuiltinsSymbol;
123 using Boolean = builtins::BuiltinsBoolean;
124 using BuiltinsMap = builtins::BuiltinsMap;
125 using BuiltinsSet = builtins::BuiltinsSet;
126 using BuiltinsWeakMap = builtins::BuiltinsWeakMap;
127 using BuiltinsWeakSet = builtins::BuiltinsWeakSet;
128 using BuiltinsWeakRef = builtins::BuiltinsWeakRef;
129 using BuiltinsFinalizationRegistry = builtins::BuiltinsFinalizationRegistry;
130 using BuiltinsArray = builtins::BuiltinsArray;
131 using BuiltinsTypedArray = builtins::BuiltinsTypedArray;
132 using BuiltinsIterator = builtins::BuiltinsIterator;
133
134 using Error = builtins::BuiltinsError;
135 using RangeError = builtins::BuiltinsRangeError;
136 using ReferenceError = builtins::BuiltinsReferenceError;
137 using TypeError = builtins::BuiltinsTypeError;
138 using AggregateError = builtins::BuiltinsAggregateError;
139 using URIError = builtins::BuiltinsURIError;
140 using SyntaxError = builtins::BuiltinsSyntaxError;
141 using EvalError = builtins::BuiltinsEvalError;
142 using OOMError = builtins::BuiltinsOOMError;
143 using ErrorType = base::ErrorType;
144 using RandomGenerator = base::RandomGenerator;
145 using Global = builtins::BuiltinsGlobal;
146 using BuiltinsString = builtins::BuiltinsString;
147 using StringIterator = builtins::BuiltinsStringIterator;
148 using BuiltinsAsyncFromSyncIterator = builtins::BuiltinsAsyncFromSyncIterator;
149 using RegExp = builtins::BuiltinsRegExp;
150 using Function = builtins::BuiltinsFunction;
151 using Math = builtins::BuiltinsMath;
152 using Atomics = builtins::BuiltinsAtomics;
153 using ArrayBuffer = builtins::BuiltinsArrayBuffer;
154 using Json = builtins::BuiltinsJson;
155 using Proxy = builtins::BuiltinsProxy;
156 using Reflect = builtins::BuiltinsReflect;
157 using AsyncFunction = builtins::BuiltinsAsyncFunction;
158 using GeneratorObject = builtins::BuiltinsGenerator;
159 using Promise = builtins::BuiltinsPromise;
160 using BuiltinsPromiseHandler = builtins::BuiltinsPromiseHandler;
161 using BuiltinsPromiseJob = builtins::BuiltinsPromiseJob;
162 using ErrorType = base::ErrorType;
163 using DataView = builtins::BuiltinsDataView;
164 using Intl = builtins::BuiltinsIntl;
165 using Locale = builtins::BuiltinsLocale;
166 using DateTimeFormat = builtins::BuiltinsDateTimeFormat;
167 using RelativeTimeFormat = builtins::BuiltinsRelativeTimeFormat;
168 using NumberFormat = builtins::BuiltinsNumberFormat;
169 using Collator = builtins::BuiltinsCollator;
170 using PluralRules = builtins::BuiltinsPluralRules;
171 using DisplayNames = builtins::BuiltinsDisplayNames;
172 using ListFormat = builtins::BuiltinsListFormat;
173 using BuiltinsCjsModule = builtins::BuiltinsCjsModule;
174 using BuiltinsCjsExports = builtins::BuiltinsCjsExports;
175 using BuiltinsCjsRequire = builtins::BuiltinsCjsRequire;
176
177 using ContainersPrivate = containers::ContainersPrivate;
178 using SharedArrayBuffer = builtins::BuiltinsSharedArrayBuffer;
179
180 using BuiltinsAsyncIterator = builtins::BuiltinsAsyncIterator;
181 using AsyncGeneratorObject = builtins::BuiltinsAsyncGenerator;
182
Initialize(const JSHandle<GlobalEnv> & env,JSThread * thread)183 void Builtins::Initialize(const JSHandle<GlobalEnv> &env, JSThread *thread)
184 {
185 thread_ = thread;
186 vm_ = thread->GetEcmaVM();
187 factory_ = vm_->GetFactory();
188 [[maybe_unused]] EcmaHandleScope scope(thread_);
189 JSHandle<JSTaggedValue> nullHandle(thread, JSTaggedValue::Null());
190
191 // Object.prototype[hclass]
192 JSHandle<JSHClass> objPrototypeHClass = factory_->NewEcmaHClass(JSObject::SIZE, JSType::JS_OBJECT, nullHandle);
193
194 // Object.prototype
195 JSHandle<JSObject> objFuncPrototype = factory_->NewJSObject(objPrototypeHClass);
196 JSHandle<JSTaggedValue> objFuncPrototypeVal(objFuncPrototype);
197
198 // Object.prototype_or_hclass
199 JSHandle<JSHClass> objFuncClass =
200 factory_->NewEcmaHClass(JSObject::SIZE, JSType::JS_OBJECT, objFuncPrototypeVal);
201
202 // GLobalObject.prototype_or_hclass
203 JSHandle<JSHClass> globalObjFuncClass =
204 factory_->NewEcmaHClass(JSObject::SIZE, JSType::JS_GLOBAL_OBJECT, 0);
205 globalObjFuncClass->SetPrototype(thread_, objFuncPrototypeVal.GetTaggedValue());
206 globalObjFuncClass->SetIsDictionaryMode(true);
207 // Function.prototype_or_hclass
208 JSHandle<JSHClass> emptyFuncClass(
209 factory_->NewEcmaHClass(JSFunction::SIZE, JSType::JS_FUNCTION, objFuncPrototypeVal));
210
211 // PrimitiveRef.prototype_or_hclass
212 JSHandle<JSHClass> primRefObjHClass =
213 factory_->NewEcmaHClass(JSPrimitiveRef::SIZE, JSType::JS_PRIMITIVE_REF, objFuncPrototypeVal);
214
215 // init global object
216 JSHandle<JSObject> globalObject = factory_->NewNonMovableJSObject(globalObjFuncClass);
217 env->SetJSGlobalObject(thread_, globalObject);
218
219 // init global patch
220 JSHandle<TaggedArray> globalPatch = factory_->EmptyArray();
221 env->SetGlobalPatch(thread, globalPatch);
222
223 // initialize Function, forbidden change order
224 InitializeFunction(env, emptyFuncClass);
225
226 JSHandle<JSHClass> asyncAwaitStatusFuncClass =
227 factory_->CreateFunctionClass(FunctionKind::NORMAL_FUNCTION, JSAsyncAwaitStatusFunction::SIZE,
228 JSType::JS_ASYNC_AWAIT_STATUS_FUNCTION, env->GetFunctionPrototype());
229 env->SetAsyncAwaitStatusFunctionClass(thread_, asyncAwaitStatusFuncClass);
230
231 JSHandle<JSHClass> asyncGeneratorResNextRetProRstFtnClass =
232 factory_->NewEcmaHClass(JSAsyncGeneratorResNextRetProRstFtn::SIZE,
233 JSType::JS_ASYNC_GENERATOR_RESUME_NEXT_RETURN_PROCESSOR_RST_FTN,
234 env->GetFunctionPrototype());
235 asyncGeneratorResNextRetProRstFtnClass->SetCallable(true);
236 asyncGeneratorResNextRetProRstFtnClass->SetExtensible(true);
237 env->SetAsyncGeneratorResNextRetProRstFtnClass(thread_, asyncGeneratorResNextRetProRstFtnClass);
238
239 JSHandle<JSHClass> proxyRevocFuncClass = factory_->NewEcmaHClass(
240 JSProxyRevocFunction::SIZE, JSType::JS_PROXY_REVOC_FUNCTION, env->GetFunctionPrototype());
241 proxyRevocFuncClass->SetCallable(true);
242 proxyRevocFuncClass->SetExtensible(true);
243 env->SetProxyRevocFunctionClass(thread_, proxyRevocFuncClass);
244
245 // Object = new Function()
246 JSHandle<JSObject> objectFunction(
247 NewBuiltinConstructor(env, objFuncPrototype, Object::ObjectConstructor, "Object", FunctionLength::ONE));
248 objectFunction.GetObject<JSFunction>()->SetFunctionPrototype(thread_, objFuncClass.GetTaggedValue());
249 // initialize object method.
250 env->SetObjectFunction(thread_, objectFunction);
251 env->SetObjectFunctionPrototype(thread_, objFuncPrototype);
252
253 JSHandle<JSHClass> FunctionClass = factory_->CreateFunctionClass(FunctionKind::BASE_CONSTRUCTOR, JSFunction::SIZE,
254 JSType::JS_FUNCTION, env->GetFunctionPrototype());
255 env->SetFunctionClassWithProto(thread_, FunctionClass);
256 FunctionClass = factory_->CreateFunctionClass(FunctionKind::NORMAL_FUNCTION, JSFunction::SIZE, JSType::JS_FUNCTION,
257 env->GetFunctionPrototype());
258 env->SetFunctionClassWithoutProto(thread_, FunctionClass);
259 FunctionClass = factory_->CreateFunctionClass(FunctionKind::CLASS_CONSTRUCTOR, JSFunction::SIZE,
260 JSType::JS_FUNCTION, env->GetFunctionPrototype());
261 env->SetFunctionClassWithoutName(thread_, FunctionClass);
262
263 if (env == vm_->GetGlobalEnv()) {
264 InitializeAllTypeError(env, objFuncClass);
265 InitializeSymbol(env, primRefObjHClass);
266 InitializeBigInt(env, primRefObjHClass);
267 } else {
268 // error and symbol need to be shared when initialize realm
269 InitializeAllTypeErrorWithRealm(env);
270 InitializeSymbolWithRealm(env, primRefObjHClass);
271 InitializeBigIntWithRealm(env);
272 }
273
274 InitializeNumber(env, globalObject, primRefObjHClass);
275 InitializeDate(env, objFuncClass);
276 InitializeObject(env, objFuncPrototype, objectFunction);
277 InitializeBoolean(env, primRefObjHClass);
278
279 InitializeRegExp(env);
280 InitializeSet(env, objFuncClass);
281 InitializeMap(env, objFuncClass);
282 InitializeWeakMap(env, objFuncClass);
283 InitializeWeakSet(env, objFuncClass);
284 InitializeWeakRef(env, objFuncClass);
285 InitializeFinalizationRegistry(env, objFuncClass);
286 InitializeArray(env, objFuncPrototypeVal);
287 InitializeTypedArray(env, objFuncClass);
288 InitializeString(env, primRefObjHClass);
289 InitializeArrayBuffer(env, objFuncClass);
290 InitializeDataView(env, objFuncClass);
291 InitializeSharedArrayBuffer(env, objFuncClass);
292
293 JSHandle<JSHClass> argumentsClass = factory_->CreateJSArguments();
294 env->SetArgumentsClass(thread_, argumentsClass);
295 SetArgumentsSharedAccessor(env);
296
297 InitializeGlobalObject(env, globalObject);
298 InitializeMath(env, objFuncPrototypeVal);
299 InitializeAtomics(env, objFuncPrototypeVal);
300 InitializeJson(env, objFuncPrototypeVal);
301 InitializeIterator(env, objFuncClass);
302 InitializeAsyncIterator(env, objFuncClass);
303 InitializeAsyncFromSyncIterator(env, objFuncClass);
304 InitializeProxy(env);
305 InitializeReflect(env, objFuncPrototypeVal);
306 InitializeAsyncFunction(env, objFuncClass);
307 InitializeGenerator(env, objFuncClass);
308 InitializeAsyncGenerator(env, objFuncClass);
309 InitializeGeneratorFunction(env, objFuncClass);
310 InitializeAsyncGeneratorFunction(env, objFuncClass);
311 InitializePromise(env, objFuncClass);
312 InitializePromiseJob(env);
313 InitializeIntl(env, objFuncPrototypeVal);
314 InitializeLocale(env);
315 InitializeDateTimeFormat(env);
316 InitializeNumberFormat(env);
317 InitializeRelativeTimeFormat(env);
318 InitializeCollator(env);
319 InitializePluralRules(env);
320 InitializeDisplayNames(env);
321 InitializeListFormat(env);
322
323 InitializeModuleNamespace(env, objFuncClass);
324 InitializeCjsModule(env);
325 InitializeCjsExports(env);
326 InitializeCjsRequire(env);
327 InitializeDefaultExportOfScript(env);
328 JSHandle<JSHClass> generatorFuncClass =
329 factory_->CreateFunctionClass(FunctionKind::GENERATOR_FUNCTION, JSFunction::SIZE, JSType::JS_GENERATOR_FUNCTION,
330 env->GetGeneratorFunctionPrototype());
331 env->SetGeneratorFunctionClass(thread_, generatorFuncClass);
332
333 JSHandle<JSHClass> asyncGenetatorFuncClass =
334 factory_->CreateFunctionClass(FunctionKind::ASYNC_GENERATOR_FUNCTION, JSFunction::SIZE,
335 JSType::JS_ASYNC_GENERATOR_FUNCTION, env->GetAsyncGeneratorFunctionPrototype());
336 env->SetAsyncGeneratorFunctionClass(thread_, asyncGenetatorFuncClass);
337 env->SetObjectFunctionPrototypeClass(thread_, JSTaggedValue(objFuncPrototype->GetClass()));
338 JSHandle<JSHClass> asyncFuncClass = factory_->CreateFunctionClass(
339 FunctionKind::ASYNC_FUNCTION, JSAsyncFunction::SIZE, JSType::JS_ASYNC_FUNCTION,
340 env->GetAsyncFunctionPrototype());
341 env->SetAsyncFunctionClass(thread_, asyncFuncClass);
342 thread_->ResetGuardians();
343 }
InitializeForSnapshot(JSThread * thread)344 void Builtins::InitializeForSnapshot(JSThread *thread)
345 {
346 thread_ = thread;
347 vm_ = thread->GetEcmaVM();
348 factory_ = vm_->GetFactory();
349
350 // Initialize ArkTools
351 if (vm_->GetJSOptions().EnableArkTools()) {
352 auto env = vm_->GetGlobalEnv();
353 auto globalObject = JSHandle<JSObject>::Cast(env->GetJSGlobalObject());
354 JSHandle<JSTaggedValue> arkTools(InitializeArkTools(env));
355 SetConstantObject(globalObject, "ArkTools", arkTools);
356 }
357 }
358
InitializeGlobalObject(const JSHandle<GlobalEnv> & env,const JSHandle<JSObject> & globalObject)359 void Builtins::InitializeGlobalObject(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &globalObject)
360 {
361 [[maybe_unused]] EcmaHandleScope scope(thread_);
362
363 // Global object test
364 SetFunction(env, globalObject, "print", Global::PrintEntrypoint, 0);
365 #if ECMASCRIPT_ENABLE_RUNTIME_STAT
366 SetFunction(env, globalObject, "startRuntimeStat", Global::StartRuntimeStat, 0);
367 SetFunction(env, globalObject, "stopRuntimeStat", Global::StopRuntimeStat, 0);
368 #endif
369
370 if (vm_->GetJSOptions().EnableArkTools()) {
371 JSHandle<JSTaggedValue> arkTools(InitializeArkTools(env));
372 SetConstantObject(globalObject, "ArkTools", arkTools);
373 }
374
375 #if ECMASCRIPT_ENABLE_ARK_CONTAINER
376 // Set ArkPrivate
377 JSHandle<JSTaggedValue> arkPrivate(InitializeArkPrivate(env));
378 SetConstantObject(globalObject, "ArkPrivate", arkPrivate);
379 #endif
380
381 // Global object function
382 SetFunction(env, globalObject, "eval", Global::NotSupportEval, FunctionLength::ONE);
383 SetFunction(env, globalObject, "isFinite", Global::IsFinite, FunctionLength::ONE);
384 SetFunction(env, globalObject, "isNaN", Global::IsNaN, FunctionLength::ONE);
385 SetFunction(env, globalObject, "decodeURI", Global::DecodeURI, FunctionLength::ONE);
386 SetFunction(env, globalObject, "encodeURI", Global::EncodeURI, FunctionLength::ONE);
387 SetFunction(env, globalObject, "decodeURIComponent", Global::DecodeURIComponent, FunctionLength::ONE);
388 SetFunction(env, globalObject, "encodeURIComponent", Global::EncodeURIComponent, FunctionLength::ONE);
389
390 // Global object property
391 SetGlobalThis(globalObject, "globalThis", JSHandle<JSTaggedValue>::Cast(globalObject));
392 SetConstant(globalObject, "Infinity", JSTaggedValue(base::POSITIVE_INFINITY));
393 SetConstant(globalObject, "NaN", JSTaggedValue(base::NAN_VALUE));
394 SetConstant(globalObject, "undefined", JSTaggedValue::Undefined());
395 }
396
InitializeFunction(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & emptyFuncClass) const397 void Builtins::InitializeFunction(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &emptyFuncClass) const
398 {
399 [[maybe_unused]] EcmaHandleScope scope(thread_);
400 // Initialize Function.prototype
401 JSHandle<JSFunction> funcFuncPrototype = factory_->NewJSFunctionByHClass(
402 reinterpret_cast<void *>(Function::FunctionPrototypeInvokeSelf),
403 emptyFuncClass);
404 // ecma 19.2.3 The value of the name property of the Function prototype object is the empty String.
405 JSHandle<JSTaggedValue> emptyString(thread_->GlobalConstants()->GetHandledEmptyString());
406 JSHandle<JSTaggedValue> undefinedString(thread_, JSTaggedValue::Undefined());
407 JSFunction::SetFunctionName(thread_, JSHandle<JSFunctionBase>(funcFuncPrototype), emptyString, undefinedString);
408 // ecma 19.2.3 The value of the length property of the Function prototype object is 0.
409 JSFunction::SetFunctionLength(thread_, funcFuncPrototype, JSTaggedValue(FunctionLength::ZERO));
410
411 JSHandle<JSTaggedValue> funcFuncPrototypeValue(funcFuncPrototype);
412 // Function.prototype_or_hclass
413 JSHandle<JSHClass> funcFuncIntanceHClass =
414 factory_->NewEcmaHClass(JSFunction::SIZE, JSType::JS_FUNCTION, funcFuncPrototypeValue);
415 funcFuncIntanceHClass->SetConstructor(true);
416
417 // Function = new Function() (forbidden use NewBuiltinConstructor)
418 JSHandle<JSFunction> funcFunc =
419 factory_->NewJSFunctionByHClass(reinterpret_cast<void *>(Function::FunctionConstructor),
420 funcFuncIntanceHClass, FunctionKind::BUILTIN_CONSTRUCTOR);
421
422 auto funcFuncPrototypeObj = JSHandle<JSObject>(funcFuncPrototype);
423 InitializeCtor(env, funcFuncPrototypeObj, funcFunc, "Function", FunctionLength::ONE);
424
425 funcFunc->SetFunctionPrototype(thread_, funcFuncIntanceHClass.GetTaggedValue());
426 env->SetFunctionFunction(thread_, funcFunc);
427 env->SetFunctionPrototype(thread_, funcFuncPrototype);
428
429 JSHandle<JSHClass> normalFuncClass =
430 factory_->NewEcmaHClass(JSFunction::SIZE, JSType::JS_FUNCTION, env->GetFunctionPrototype());
431 env->SetNormalFunctionClass(thread_, normalFuncClass);
432
433 JSHandle<JSHClass> jSIntlBoundFunctionClass =
434 factory_->CreateFunctionClass(FunctionKind::NORMAL_FUNCTION, JSIntlBoundFunction::SIZE,
435 JSType::JS_INTL_BOUND_FUNCTION, env->GetFunctionPrototype());
436 env->SetJSIntlBoundFunctionClass(thread_, jSIntlBoundFunctionClass);
437
438 JSHandle<JSHClass> constructorFunctionClass =
439 factory_->NewEcmaHClass(JSFunction::SIZE, JSType::JS_FUNCTION, env->GetFunctionPrototype());
440 constructorFunctionClass->SetConstructor(true);
441 env->SetConstructorFunctionClass(thread_, constructorFunctionClass);
442
443 StrictModeForbiddenAccessCallerArguments(env, funcFuncPrototypeObj);
444
445 // Function.prototype method
446 // 19.2.3.1 Function.prototype.apply ( thisArg, argArray )
447 SetFunction(env, funcFuncPrototypeObj, "apply", Function::FunctionPrototypeApply, FunctionLength::TWO);
448 // 19.2.3.2 Function.prototype.bind ( thisArg , ...args)
449 SetFunction(env, funcFuncPrototypeObj, "bind", Function::FunctionPrototypeBind, FunctionLength::ONE);
450 // 19.2.3.3 Function.prototype.call (thisArg , ...args)
451 SetFunction(env, funcFuncPrototypeObj, "call", Function::FunctionPrototypeCall, FunctionLength::ONE);
452 // 19.2.3.5 Function.prototype.toString ( )
453 SetFunction(env, funcFuncPrototypeObj, thread_->GlobalConstants()->GetHandledToStringString(),
454 Function::FunctionPrototypeToString, FunctionLength::ZERO);
455 }
456
InitializeObject(const JSHandle<GlobalEnv> & env,const JSHandle<JSObject> & objFuncPrototype,const JSHandle<JSObject> & objFunc)457 void Builtins::InitializeObject(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &objFuncPrototype,
458 const JSHandle<JSObject> &objFunc)
459 {
460 [[maybe_unused]] EcmaHandleScope scope(thread_);
461 // Object method.
462 // 19.1.2.1Object.assign ( target, ...sources )
463 SetFunction(env, objFunc, "assign", Object::Assign, FunctionLength::TWO);
464 // 19.1.2.2Object.create ( O [ , Properties ] )
465 SetFunction(env, objFunc, "create", Object::Create, FunctionLength::TWO);
466 // 19.1.2.3Object.defineProperties ( O, Properties )
467 SetFunction(env, objFunc, "defineProperties", Object::DefineProperties, FunctionLength::TWO);
468 // 19.1.2.4Object.defineProperty ( O, P, Attributes )
469 SetFunction(env, objFunc, "defineProperty", Object::DefineProperty, FunctionLength::THREE);
470 // 19.1.2.5Object.freeze ( O )
471 SetFunction(env, objFunc, "freeze", Object::Freeze, FunctionLength::ONE);
472 // 19.1.2.6Object.getOwnPropertyDescriptor ( O, P )
473 SetFunction(env, objFunc, "getOwnPropertyDescriptor", Object::GetOwnPropertyDescriptor, FunctionLength::TWO);
474 // 19.1.2.7Object.getOwnPropertyNames ( O )
475 SetFunction(env, objFunc, "getOwnPropertyNames", Object::GetOwnPropertyNames, FunctionLength::ONE);
476 // 19.1.2.8Object.getOwnPropertySymbols ( O )
477 SetFunction(env, objFunc, "getOwnPropertySymbols", Object::GetOwnPropertySymbols, FunctionLength::ONE);
478 // 19.1.2.9Object.getPrototypeOf ( O )
479 SetFunction(env, objFunc, "getPrototypeOf", Object::GetPrototypeOf, FunctionLength::ONE);
480 // 19.1.2.10Object.is ( value1, value2 )
481 SetFunction(env, objFunc, "is", Object::Is, 2);
482 // 19.1.2.11Object.isExtensible ( O )
483 SetFunction(env, objFunc, "isExtensible", Object::IsExtensible, FunctionLength::ONE);
484 // 19.1.2.12Object.isFrozen ( O )
485 SetFunction(env, objFunc, "isFrozen", Object::IsFrozen, FunctionLength::ONE);
486 // 19.1.2.13Object.isSealed ( O )
487 SetFunction(env, objFunc, "isSealed", Object::IsSealed, FunctionLength::ONE);
488 // 19.1.2.14 Object.keys(O)
489 SetFunction(env, objFunc, "keys", Object::Keys, FunctionLength::ONE);
490 // 20.1.2.22 Object.values(O)
491 SetFunction(env, objFunc, "values", Object::Values, FunctionLength::ONE);
492 // 19.1.2.15 Object.preventExtensions(O)
493 SetFunction(env, objFunc, "preventExtensions", Object::PreventExtensions, FunctionLength::ONE);
494 // 19.1.2.17 Object.seal(O)
495 SetFunction(env, objFunc, "seal", Object::Seal, FunctionLength::ONE);
496 // 19.1.2.18 Object.setPrototypeOf(O, proto)
497 SetFunction(env, objFunc, "setPrototypeOf", Object::SetPrototypeOf, FunctionLength::TWO);
498 // 20.1.2.5 Object.entries ( O )
499 SetFunction(env, objFunc, "entries", Object::Entries, FunctionLength::ONE);
500 // 20.1.2.7 Object.fromEntries ( iterable )
501 SetFunction(env, objFunc, "fromEntries", Object::FromEntries, FunctionLength::ONE);
502
503 // Object.property method
504 // 19.1.3.2 Object.prototype.hasOwnProperty(V)
505 SetFunction(env, objFuncPrototype, "hasOwnProperty", Object::HasOwnProperty, FunctionLength::ONE);
506 // 19.1.3.3 Object.prototype.isPrototypeOf(V)
507 SetFunction(env, objFuncPrototype, "isPrototypeOf", Object::IsPrototypeOf, FunctionLength::ONE);
508 // 19.1.3.4 Object.prototype.propertyIsEnumerable(V)
509 SetFunction(env, objFuncPrototype, "propertyIsEnumerable", Object::PropertyIsEnumerable, FunctionLength::ONE);
510 // 19.1.3.5 Object.prototype.toLocaleString([reserved1[, reserved2]])
511 SetFunction(env, objFuncPrototype, "toLocaleString", Object::ToLocaleString, FunctionLength::ZERO);
512 // 19.1.3.6 Object.prototype.toString()
513 SetFunction(env, objFuncPrototype, thread_->GlobalConstants()->GetHandledToStringString(), Object::ToString,
514 FunctionLength::ZERO);
515 // 19.1.3.7 Object.prototype.valueOf()
516 SetFunction(env, objFuncPrototype, thread_->GlobalConstants()->GetHandledValueOfString(), Object::ValueOf,
517 FunctionLength::ZERO);
518
519 SetFunction(env, objFuncPrototype, "createRealm", Object::CreateRealm, FunctionLength::ZERO);
520
521 // B.2.2.1 Object.prototype.__proto__
522 JSHandle<JSTaggedValue> protoKey(factory_->NewFromASCII("__proto__"));
523 JSHandle<JSTaggedValue> protoGetter = CreateGetter(env, Object::ProtoGetter, "__proto__", FunctionLength::ZERO);
524 JSHandle<JSTaggedValue> protoSetter = CreateSetter(env, Object::ProtoSetter, "__proto__", FunctionLength::ONE);
525 SetAccessor(objFuncPrototype, protoKey, protoGetter, protoSetter);
526 }
527
InitializeSymbol(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncClass) const528 void Builtins::InitializeSymbol(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncClass) const
529 {
530 [[maybe_unused]] EcmaHandleScope scope(thread_);
531 const GlobalEnvConstants *globalConst = thread_->GlobalConstants();
532 // Symbol.prototype
533 JSHandle<JSObject> symbolFuncPrototype = factory_->NewJSObjectWithInit(objFuncClass);
534 JSHandle<JSTaggedValue> symbolFuncPrototypeValue(symbolFuncPrototype);
535
536 // Symbol.prototype_or_hclass
537 JSHandle<JSHClass> symbolFuncInstanceHClass =
538 factory_->NewEcmaHClass(JSPrimitiveRef::SIZE, JSType::JS_PRIMITIVE_REF, symbolFuncPrototypeValue);
539
540 // Symbol = new Function()
541 JSHandle<JSObject> symbolFunction(
542 NewBuiltinConstructor(env, symbolFuncPrototype, Symbol::SymbolConstructor, "Symbol", FunctionLength::ZERO));
543 JSHandle<JSFunction>(symbolFunction)->SetFunctionPrototype(thread_, symbolFuncInstanceHClass.GetTaggedValue());
544
545 // "constructor" property on the prototype
546 JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString();
547 PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>::Cast(symbolFunction), true, false, true);
548 JSObject::DefineOwnProperty(thread_, symbolFuncPrototype, constructorKey, descriptor);
549
550 SetFunction(env, symbolFunction, "for", Symbol::For, FunctionLength::ONE);
551 SetFunction(env, symbolFunction, "keyFor", Symbol::KeyFor, FunctionLength::ONE);
552
553 // Symbol attribute
554 JSHandle<JSTaggedValue> hasInstanceSymbol(factory_->NewWellKnownSymbolWithChar("Symbol.hasInstance"));
555 SetNoneAttributeProperty(symbolFunction, "hasInstance", hasInstanceSymbol);
556 JSHandle<JSTaggedValue> isConcatSpreadableSymbol(factory_->NewWellKnownSymbolWithChar("Symbol.isConcatSpreadable"));
557 SetNoneAttributeProperty(symbolFunction, "isConcatSpreadable", isConcatSpreadableSymbol);
558 JSHandle<JSTaggedValue> toStringTagSymbol(factory_->NewWellKnownSymbolWithChar("Symbol.toStringTag"));
559 SetNoneAttributeProperty(symbolFunction, "toStringTag", toStringTagSymbol);
560 JSHandle<JSTaggedValue> iteratorSymbol(factory_->NewPublicSymbolWithChar("Symbol.iterator"));
561 SetNoneAttributeProperty(symbolFunction, "iterator", iteratorSymbol);
562 JSHandle<JSTaggedValue> asyncIteratorSymbol(factory_->NewPublicSymbolWithChar("Symbol.asyncIterator"));
563 SetNoneAttributeProperty(symbolFunction, "asyncIterator", asyncIteratorSymbol);
564 JSHandle<JSTaggedValue> matchSymbol(factory_->NewPublicSymbolWithChar("Symbol.match"));
565 SetNoneAttributeProperty(symbolFunction, "match", matchSymbol);
566 JSHandle<JSTaggedValue> matchAllSymbol(factory_->NewPublicSymbolWithChar("Symbol.matchAll"));
567 SetNoneAttributeProperty(symbolFunction, "matchAll", matchAllSymbol);
568 JSHandle<JSTaggedValue> replaceSymbol(factory_->NewPublicSymbolWithChar("Symbol.replace"));
569 SetNoneAttributeProperty(symbolFunction, "replace", replaceSymbol);
570 JSHandle<JSTaggedValue> searchSymbol(factory_->NewPublicSymbolWithChar("Symbol.search"));
571 SetNoneAttributeProperty(symbolFunction, "search", searchSymbol);
572 JSHandle<JSTaggedValue> speciesSymbol(factory_->NewPublicSymbolWithChar("Symbol.species"));
573 SetNoneAttributeProperty(symbolFunction, "species", speciesSymbol);
574 JSHandle<JSTaggedValue> splitSymbol(factory_->NewPublicSymbolWithChar("Symbol.split"));
575 SetNoneAttributeProperty(symbolFunction, "split", splitSymbol);
576 JSHandle<JSTaggedValue> toPrimitiveSymbol(factory_->NewPublicSymbolWithChar("Symbol.toPrimitive"));
577 SetNoneAttributeProperty(symbolFunction, "toPrimitive", toPrimitiveSymbol);
578 JSHandle<JSTaggedValue> unscopablesSymbol(factory_->NewPublicSymbolWithChar("Symbol.unscopables"));
579 SetNoneAttributeProperty(symbolFunction, "unscopables", unscopablesSymbol);
580 JSHandle<JSTaggedValue> attachSymbol(factory_->NewPublicSymbolWithChar("Symbol.attach"));
581 SetNoneAttributeProperty(symbolFunction, "attach", attachSymbol);
582 JSHandle<JSTaggedValue> detachSymbol(factory_->NewPublicSymbolWithChar("Symbol.detach"));
583 SetNoneAttributeProperty(symbolFunction, "detach", detachSymbol);
584
585 // symbol.prototype.description
586 PropertyDescriptor descriptionDesc(thread_);
587 JSHandle<JSTaggedValue> getterKey(factory_->NewFromASCII("description"));
588 JSHandle<JSTaggedValue> getter(factory_->NewJSFunction(env, reinterpret_cast<void *>(Symbol::DescriptionGetter)));
589 SetGetter(symbolFuncPrototype, getterKey, getter);
590
591 // Setup symbol.prototype[@@toPrimitive]
592 SetFunctionAtSymbol<JSSymbol::SYMBOL_TO_PRIMITIVE_TYPE>(
593 env, symbolFuncPrototype, toPrimitiveSymbol, "[Symbol.toPrimitive]", Symbol::ToPrimitive, FunctionLength::ONE);
594 // install the Symbol.prototype methods
595 SetFunction(env, symbolFuncPrototype, thread_->GlobalConstants()->GetHandledToStringString(), Symbol::ToString,
596 FunctionLength::ZERO);
597 SetFunction(env, symbolFuncPrototype, thread_->GlobalConstants()->GetHandledValueOfString(), Symbol::ValueOf,
598 FunctionLength::ZERO);
599
600 env->SetSymbolFunction(thread_, symbolFunction);
601 env->SetHasInstanceSymbol(thread_, hasInstanceSymbol);
602 env->SetIsConcatSpreadableSymbol(thread_, isConcatSpreadableSymbol);
603 env->SetToStringTagSymbol(thread_, toStringTagSymbol);
604 env->SetIteratorSymbol(thread_, iteratorSymbol);
605 env->SetAsyncIteratorSymbol(thread_, asyncIteratorSymbol);
606 env->SetMatchSymbol(thread_, matchSymbol);
607 env->SetMatchAllSymbol(thread_, matchAllSymbol);
608 env->SetReplaceSymbol(thread_, replaceSymbol);
609 env->SetSearchSymbol(thread_, searchSymbol);
610 env->SetSpeciesSymbol(thread_, speciesSymbol);
611 env->SetSplitSymbol(thread_, splitSymbol);
612 env->SetToPrimitiveSymbol(thread_, toPrimitiveSymbol);
613 env->SetUnscopablesSymbol(thread_, unscopablesSymbol);
614 env->SetAttachSymbol(thread_, attachSymbol);
615 env->SetDetachSymbol(thread_, detachSymbol);
616
617 // Setup %SymbolPrototype%
618 SetStringTagSymbol(env, symbolFuncPrototype, "Symbol");
619
620 JSHandle<JSTaggedValue> holeySymbol(factory_->NewPrivateNameSymbolWithChar("holey"));
621 env->SetHoleySymbol(thread_, holeySymbol.GetTaggedValue());
622 JSHandle<JSTaggedValue> elementIcSymbol(factory_->NewPrivateNameSymbolWithChar("element-ic"));
623 env->SetElementICSymbol(thread_, elementIcSymbol.GetTaggedValue());
624
625 // ecma 19.2.3.6 Function.prototype[@@hasInstance] ( V )
626 JSHandle<JSObject> funcFuncPrototypeObj = JSHandle<JSObject>(env->GetFunctionPrototype());
627 SetFunctionAtSymbol<JSSymbol::SYMBOL_HAS_INSTANCE_TYPE>(
628 env, funcFuncPrototypeObj, env->GetHasInstanceSymbol(), "[Symbol.hasInstance]",
629 Function::FunctionPrototypeHasInstance, FunctionLength::ONE);
630 }
631
InitializeSymbolWithRealm(const JSHandle<GlobalEnv> & realm,const JSHandle<JSHClass> & objFuncInstanceHClass)632 void Builtins::InitializeSymbolWithRealm(const JSHandle<GlobalEnv> &realm,
633 const JSHandle<JSHClass> &objFuncInstanceHClass)
634 {
635 [[maybe_unused]] EcmaHandleScope scope(thread_);
636 JSHandle<GlobalEnv> env = vm_->GetGlobalEnv();
637 // Symbol.prototype
638 JSHandle<JSObject> symbolFuncPrototype = factory_->NewJSObjectWithInit(objFuncInstanceHClass);
639 JSHandle<JSTaggedValue> symbolFuncPrototypeValue(symbolFuncPrototype);
640
641 // Symbol.prototype_or_hclass
642 JSHandle<JSHClass> symbolFuncInstanceHClass =
643 factory_->NewEcmaHClass(JSPrimitiveRef::SIZE, JSType::JS_PRIMITIVE_REF, symbolFuncPrototypeValue);
644
645 // Symbol = new Function()
646 JSHandle<JSObject> symbolFunction(
647 NewBuiltinConstructor(realm, symbolFuncPrototype, Symbol::SymbolConstructor, "Symbol", FunctionLength::ZERO));
648 JSHandle<JSFunction>(symbolFunction)->SetFunctionPrototype(thread_, symbolFuncInstanceHClass.GetTaggedValue());
649
650 // "constructor" property on the prototype
651 JSHandle<JSTaggedValue> constructorKey = thread_->GlobalConstants()->GetHandledConstructorString();
652 PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>::Cast(symbolFunction), true, false, true);
653 JSObject::DefineOwnProperty(thread_, symbolFuncPrototype, constructorKey, descriptor);
654
655 SetFunction(realm, symbolFunction, "for", Symbol::For, FunctionLength::ONE);
656 SetFunction(realm, symbolFunction, "keyFor", Symbol::KeyFor, FunctionLength::ONE);
657
658 // Symbol attribute
659 SetNoneAttributeProperty(symbolFunction, "hasInstance", env->GetHasInstanceSymbol());
660 SetNoneAttributeProperty(symbolFunction, "isConcatSpreadable", env->GetIsConcatSpreadableSymbol());
661 SetNoneAttributeProperty(symbolFunction, "toStringTag", env->GetToStringTagSymbol());
662 SetNoneAttributeProperty(symbolFunction, "iterator", env->GetIteratorSymbol());
663 SetNoneAttributeProperty(symbolFunction, "asyncIterator", env->GetAsyncIteratorSymbol());
664 SetNoneAttributeProperty(symbolFunction, "match", env->GetMatchSymbol());
665 SetNoneAttributeProperty(symbolFunction, "matchAll", env->GetMatchAllSymbol());
666 SetNoneAttributeProperty(symbolFunction, "replace", env->GetReplaceSymbol());
667 SetNoneAttributeProperty(symbolFunction, "search", env->GetSearchSymbol());
668 SetNoneAttributeProperty(symbolFunction, "species", env->GetSpeciesSymbol());
669 SetNoneAttributeProperty(symbolFunction, "split", env->GetSplitSymbol());
670 SetNoneAttributeProperty(symbolFunction, "toPrimitive", env->GetToPrimitiveSymbol());
671 SetNoneAttributeProperty(symbolFunction, "unscopables", env->GetUnscopablesSymbol());
672 SetNoneAttributeProperty(symbolFunction, "attach", env->GetAttachSymbol());
673 SetNoneAttributeProperty(symbolFunction, "detach", env->GetDetachSymbol());
674
675 // symbol.prototype.description
676 PropertyDescriptor descriptionDesc(thread_);
677 JSHandle<JSTaggedValue> getterKey(factory_->NewFromASCII("description"));
678 JSHandle<JSTaggedValue> getter(factory_->NewJSFunction(realm, reinterpret_cast<void *>(Symbol::DescriptionGetter)));
679 SetGetter(symbolFuncPrototype, getterKey, getter);
680
681 // Setup symbol.prototype[@@toPrimitive]
682 SetFunctionAtSymbol<JSSymbol::SYMBOL_TO_PRIMITIVE_TYPE>(realm, symbolFuncPrototype, env->GetToPrimitiveSymbol(),
683 "[Symbol.toPrimitive]", Symbol::ToPrimitive,
684 FunctionLength::ONE);
685 // install the Symbol.prototype methods
686 SetFunction(realm, symbolFuncPrototype, thread_->GlobalConstants()->GetHandledToStringString(), Symbol::ToString,
687 FunctionLength::ZERO);
688 SetFunction(realm, symbolFuncPrototype, thread_->GlobalConstants()->GetHandledValueOfString(), Symbol::ValueOf,
689 FunctionLength::ZERO);
690
691 realm->SetSymbolFunction(thread_, symbolFunction);
692 realm->SetHasInstanceSymbol(thread_, env->GetHasInstanceSymbol());
693 realm->SetIsConcatSpreadableSymbol(thread_, env->GetIsConcatSpreadableSymbol());
694 realm->SetToStringTagSymbol(thread_, env->GetToStringTagSymbol());
695 realm->SetIteratorSymbol(thread_, env->GetIteratorSymbol());
696 realm->SetAsyncIteratorSymbol(thread_, env->GetAsyncIteratorSymbol());
697 realm->SetMatchSymbol(thread_, env->GetMatchSymbol());
698 realm->SetMatchAllSymbol(thread_, env->GetMatchAllSymbol());
699 realm->SetReplaceSymbol(thread_, env->GetReplaceSymbol());
700 realm->SetSearchSymbol(thread_, env->GetSearchSymbol());
701 realm->SetSpeciesSymbol(thread_, env->GetSpeciesSymbol());
702 realm->SetSplitSymbol(thread_, env->GetSplitSymbol());
703 realm->SetToPrimitiveSymbol(thread_, env->GetToPrimitiveSymbol());
704 realm->SetUnscopablesSymbol(thread_, env->GetUnscopablesSymbol());
705 realm->SetAttachSymbol(thread_, env->GetAttachSymbol());
706 realm->SetDetachSymbol(thread_, env->GetDetachSymbol());
707
708 // Setup %SymbolPrototype%
709 SetStringTagSymbol(realm, symbolFuncPrototype, "Symbol");
710
711 JSHandle<JSTaggedValue> holeySymbol(factory_->NewPrivateNameSymbolWithChar("holey"));
712 realm->SetHoleySymbol(thread_, holeySymbol.GetTaggedValue());
713 JSHandle<JSTaggedValue> elementIcSymbol(factory_->NewPrivateNameSymbolWithChar("element-ic"));
714 realm->SetElementICSymbol(thread_, elementIcSymbol.GetTaggedValue());
715
716 // ecma 19.2.3.6 Function.prototype[@@hasInstance] ( V )
717 JSHandle<JSObject> funcFuncPrototypeObj = JSHandle<JSObject>(realm->GetFunctionPrototype());
718 SetFunctionAtSymbol<JSSymbol::SYMBOL_HAS_INSTANCE_TYPE>(
719 realm, funcFuncPrototypeObj, realm->GetHasInstanceSymbol(), "[Symbol.hasInstance]",
720 Function::FunctionPrototypeHasInstance, FunctionLength::ONE);
721 }
722
InitializeNumber(const JSHandle<GlobalEnv> & env,const JSHandle<JSObject> & globalObject,const JSHandle<JSHClass> & primRefObjHClass)723 void Builtins::InitializeNumber(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &globalObject,
724 const JSHandle<JSHClass> &primRefObjHClass)
725 {
726 [[maybe_unused]] EcmaHandleScope scope(thread_);
727 // Number.prototype
728 JSHandle<JSTaggedValue> toObject(thread_, JSTaggedValue(FunctionLength::ZERO));
729 JSHandle<JSObject> numFuncPrototype =
730 JSHandle<JSObject>::Cast(factory_->NewJSPrimitiveRef(primRefObjHClass, toObject));
731 JSHandle<JSTaggedValue> numFuncPrototypeValue(numFuncPrototype);
732
733 // Number.prototype_or_hclass
734 JSHandle<JSHClass> numFuncInstanceHClass =
735 factory_->NewEcmaHClass(JSPrimitiveRef::SIZE, JSType::JS_PRIMITIVE_REF, numFuncPrototypeValue);
736
737 // Number = new Function()
738 JSHandle<JSObject> numFunction(
739 NewBuiltinConstructor(env, numFuncPrototype, Number::NumberConstructor, "Number", FunctionLength::ONE));
740 numFunction.GetObject<JSFunction>()->SetFunctionPrototype(thread_, numFuncInstanceHClass.GetTaggedValue());
741
742 // Number.prototype method
743 SetFunction(env, numFuncPrototype, "toExponential", Number::ToExponential, FunctionLength::ONE);
744 SetFunction(env, numFuncPrototype, "toFixed", Number::ToFixed, FunctionLength::ONE);
745 SetFunction(env, numFuncPrototype, "toLocaleString", Number::ToLocaleString, FunctionLength::ZERO);
746 SetFunction(env, numFuncPrototype, "toPrecision", Number::ToPrecision, FunctionLength::ONE);
747 SetFunction(env, numFuncPrototype, thread_->GlobalConstants()->GetHandledToStringString(), Number::ToString,
748 FunctionLength::ONE);
749 SetFunction(env, numFuncPrototype, thread_->GlobalConstants()->GetHandledValueOfString(), Number::ValueOf,
750 FunctionLength::ZERO);
751
752 // Number method
753 SetFunction(env, numFunction, "isFinite", Number::IsFinite, FunctionLength::ONE);
754 SetFunction(env, numFunction, "isInteger", Number::IsInteger, FunctionLength::ONE);
755 SetFunction(env, numFunction, "isNaN", Number::IsNaN, FunctionLength::ONE);
756 SetFunction(env, numFunction, "isSafeInteger", Number::IsSafeInteger, FunctionLength::ONE);
757 SetFuncToObjAndGlobal(env, globalObject, numFunction, "parseFloat", Number::ParseFloat, FunctionLength::ONE);
758 SetFuncToObjAndGlobal(env, globalObject, numFunction, "parseInt", Number::ParseInt, FunctionLength::TWO);
759
760 // Number constant
761 const double epsilon = 2.220446049250313e-16;
762 const double maxSafeInteger = 9007199254740991;
763 const double maxValue = 1.7976931348623157e+308;
764 const double minValue = 5e-324;
765 const double positiveInfinity = std::numeric_limits<double>::infinity();
766 SetConstant(numFunction, "MAX_VALUE", JSTaggedValue(maxValue));
767 SetConstant(numFunction, "MIN_VALUE", JSTaggedValue(minValue));
768 SetConstant(numFunction, "NaN", JSTaggedValue(NAN));
769 SetConstant(numFunction, "NEGATIVE_INFINITY", JSTaggedValue(-positiveInfinity));
770 SetConstant(numFunction, "POSITIVE_INFINITY", JSTaggedValue(positiveInfinity));
771 SetConstant(numFunction, "MAX_SAFE_INTEGER", JSTaggedValue(maxSafeInteger));
772 SetConstant(numFunction, "MIN_SAFE_INTEGER", JSTaggedValue(-maxSafeInteger));
773 SetConstant(numFunction, "EPSILON", JSTaggedValue(epsilon));
774
775 env->SetNumberFunction(thread_, numFunction);
776 }
InitializeBigIntWithRealm(const JSHandle<GlobalEnv> & realm) const777 void Builtins::InitializeBigIntWithRealm(const JSHandle<GlobalEnv> &realm) const
778 {
779 [[maybe_unused]] EcmaHandleScope scope(thread_);
780 JSHandle<GlobalEnv> env = vm_->GetGlobalEnv();
781 realm->SetBigIntFunction(thread_, env->GetBigIntFunction());
782
783 JSHandle<JSTaggedValue> nameString(factory_->NewFromASCII("BigInt"));
784 JSHandle<JSObject> globalObject(thread_, realm->GetGlobalObject());
785 PropertyDescriptor descriptor(thread_, env->GetBigIntFunction(), true, false, true);
786 JSObject::DefineOwnProperty(thread_, globalObject, nameString, descriptor);
787 }
788
InitializeBigInt(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & primRefObjHClass) const789 void Builtins::InitializeBigInt(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &primRefObjHClass) const
790 {
791 [[maybe_unused]] EcmaHandleScope scope(thread_);
792 // BigInt.prototype
793 JSHandle<JSObject> bigIntFuncPrototype = factory_->NewJSObjectWithInit(primRefObjHClass);
794 JSHandle<JSTaggedValue> bigIntFuncPrototypeValue(bigIntFuncPrototype);
795
796 // BigInt.prototype_or_hclass
797 JSHandle<JSHClass> bigIntFuncInstanceHClass =
798 factory_->NewEcmaHClass(JSPrimitiveRef::SIZE, JSType::JS_PRIMITIVE_REF, bigIntFuncPrototypeValue);
799 // BigInt = new Function()
800 JSHandle<JSObject> bigIntFunction(
801 NewBuiltinConstructor(env, bigIntFuncPrototype,
802 BuiltinsBigInt::BigIntConstructor, "BigInt", FunctionLength::ONE));
803 JSHandle<JSFunction>(bigIntFunction)->SetFunctionPrototype(thread_, bigIntFuncInstanceHClass.GetTaggedValue());
804
805 // BigInt.prototype method
806 SetFunction(env, bigIntFuncPrototype, "toLocaleString", BuiltinsBigInt::ToLocaleString, FunctionLength::ZERO);
807 SetFunction(env, bigIntFuncPrototype, "toString", BuiltinsBigInt::ToString, FunctionLength::ZERO);
808 SetFunction(env, bigIntFuncPrototype, "valueOf", BuiltinsBigInt::ValueOf, FunctionLength::ZERO);
809
810 // BigInt method
811 SetFunction(env, bigIntFunction, "asUintN", BuiltinsBigInt::AsUintN, FunctionLength::TWO);
812 SetFunction(env, bigIntFunction, "asIntN", BuiltinsBigInt::AsIntN, FunctionLength::TWO);
813
814 // @@ToStringTag
815 SetStringTagSymbol(env, bigIntFuncPrototype, "BigInt");
816 env->SetBigIntFunction(thread_, bigIntFunction);
817 }
818
InitializeDate(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncClass) const819 void Builtins::InitializeDate(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncClass) const
820 {
821 [[maybe_unused]] EcmaHandleScope scope(thread_);
822 constexpr int utcLength = 7;
823 // Date.prototype
824 JSHandle<JSObject> dateFuncPrototype = factory_->NewJSObjectWithInit(objFuncClass);
825 JSHandle<JSTaggedValue> dateFuncPrototypeValue(dateFuncPrototype);
826
827 // Date.prototype_or_hclass
828 JSHandle<JSHClass> dateFuncInstanceHClass =
829 factory_->NewEcmaHClass(JSDate::SIZE, JSType::JS_DATE, dateFuncPrototypeValue);
830
831 // Date = new Function()
832 JSHandle<JSObject> dateFunction(
833 NewBuiltinConstructor(env, dateFuncPrototype, Date::DateConstructor, "Date", FunctionLength::ONE,
834 BUILTINS_STUB_ID(DateConstructor)));
835 JSHandle<JSFunction>(dateFunction)->SetFunctionPrototype(thread_, dateFuncInstanceHClass.GetTaggedValue());
836
837 // Date.prototype method
838 SetFunction(env, dateFuncPrototype, "getDate", Date::GetDate, FunctionLength::ZERO);
839 SetFunction(env, dateFuncPrototype, "getDay", Date::GetDay, FunctionLength::ZERO);
840 SetFunction(env, dateFuncPrototype, "getFullYear", Date::GetFullYear, FunctionLength::ZERO);
841 SetFunction(env, dateFuncPrototype, "getHours", Date::GetHours, FunctionLength::ZERO);
842 SetFunction(env, dateFuncPrototype, "getMilliseconds", Date::GetMilliseconds, FunctionLength::ZERO);
843 SetFunction(env, dateFuncPrototype, "getMinutes", Date::GetMinutes, FunctionLength::ZERO);
844 SetFunction(env, dateFuncPrototype, "getMonth", Date::GetMonth, FunctionLength::ZERO);
845 SetFunction(env, dateFuncPrototype, "getSeconds", Date::GetSeconds, FunctionLength::ZERO);
846 SetFunction(env, dateFuncPrototype, "getTime", Date::GetTime, FunctionLength::ZERO);
847 SetFunction(env, dateFuncPrototype, "getTimezoneOffset", Date::GetTimezoneOffset, FunctionLength::ZERO);
848 SetFunction(env, dateFuncPrototype, "getUTCDate", Date::GetUTCDate, FunctionLength::ZERO);
849 SetFunction(env, dateFuncPrototype, "getUTCDay", Date::GetUTCDay, FunctionLength::ZERO);
850 SetFunction(env, dateFuncPrototype, "getUTCFullYear", Date::GetUTCFullYear, FunctionLength::ZERO);
851 SetFunction(env, dateFuncPrototype, "getUTCHours", Date::GetUTCHours, FunctionLength::ZERO);
852 SetFunction(env, dateFuncPrototype, "getUTCMilliseconds", Date::GetUTCMilliseconds, FunctionLength::ZERO);
853 SetFunction(env, dateFuncPrototype, "getUTCMinutes", Date::GetUTCMinutes, FunctionLength::ZERO);
854 SetFunction(env, dateFuncPrototype, "getUTCMonth", Date::GetUTCMonth, FunctionLength::ZERO);
855 SetFunction(env, dateFuncPrototype, "getUTCSeconds", Date::GetUTCSeconds, FunctionLength::ZERO);
856
857 SetFunction(env, dateFuncPrototype, "setDate", Date::SetDate, FunctionLength::ONE);
858 SetFunction(env, dateFuncPrototype, "setFullYear", Date::SetFullYear, FunctionLength::THREE);
859 SetFunction(env, dateFuncPrototype, "setHours", Date::SetHours, FunctionLength::FOUR);
860 SetFunction(env, dateFuncPrototype, "setMilliseconds", Date::SetMilliseconds, FunctionLength::ONE);
861 SetFunction(env, dateFuncPrototype, "setMinutes", Date::SetMinutes, FunctionLength::THREE);
862 SetFunction(env, dateFuncPrototype, "setMonth", Date::SetMonth, FunctionLength::TWO);
863 SetFunction(env, dateFuncPrototype, "setSeconds", Date::SetSeconds, FunctionLength::TWO);
864 SetFunction(env, dateFuncPrototype, "setTime", Date::SetTime, FunctionLength::ONE);
865 SetFunction(env, dateFuncPrototype, "setUTCDate", Date::SetUTCDate, FunctionLength::ONE);
866 SetFunction(env, dateFuncPrototype, "setUTCFullYear", Date::SetUTCFullYear, FunctionLength::THREE);
867 SetFunction(env, dateFuncPrototype, "setUTCHours", Date::SetUTCHours, FunctionLength::FOUR);
868 SetFunction(env, dateFuncPrototype, "setUTCMilliseconds", Date::SetUTCMilliseconds, FunctionLength::ONE);
869 SetFunction(env, dateFuncPrototype, "setUTCMinutes", Date::SetUTCMinutes, FunctionLength::THREE);
870 SetFunction(env, dateFuncPrototype, "setUTCMonth", Date::SetUTCMonth, FunctionLength::TWO);
871 SetFunction(env, dateFuncPrototype, "setUTCSeconds", Date::SetUTCSeconds, FunctionLength::TWO);
872
873 SetFunction(env, dateFuncPrototype, "toDateString", Date::ToDateString, FunctionLength::ZERO);
874 SetFunction(env, dateFuncPrototype, "toISOString", Date::ToISOString, FunctionLength::ZERO);
875 SetFunction(env, dateFuncPrototype, "toJSON", Date::ToJSON, FunctionLength::ONE);
876 SetFunction(env, dateFuncPrototype, "toLocaleDateString", Date::ToLocaleDateString, FunctionLength::ZERO);
877 SetFunction(env, dateFuncPrototype, "toLocaleString", Date::ToLocaleString, FunctionLength::ZERO);
878 SetFunction(env, dateFuncPrototype, "toLocaleTimeString", Date::ToLocaleTimeString, FunctionLength::ZERO);
879 SetFunction(env, dateFuncPrototype, thread_->GlobalConstants()->GetHandledToStringString(), Date::ToString,
880 FunctionLength::ZERO);
881 SetFunction(env, dateFuncPrototype, "toTimeString", Date::ToTimeString, FunctionLength::ZERO);
882 SetFunction(env, dateFuncPrototype, "toUTCString", Date::ToUTCString, FunctionLength::ZERO);
883 SetFunction(env, dateFuncPrototype, thread_->GlobalConstants()->GetHandledValueOfString(), Date::ValueOf,
884 FunctionLength::ZERO);
885
886 SetFunctionAtSymbol(env, dateFuncPrototype, env->GetToPrimitiveSymbol(), "[Symbol.toPrimitive]", Date::ToPrimitive,
887 FunctionLength::ONE);
888
889 // Date method
890 SetFunction(env, dateFunction, "now", Date::Now, FunctionLength::ZERO);
891 SetFunction(env, dateFunction, "parse", Date::Parse, FunctionLength::ONE);
892 SetFunction(env, dateFunction, "UTC", Date::UTC, utcLength);
893
894 // Date.length
895 SetConstant(dateFunction, "length", JSTaggedValue(utcLength));
896
897 env->SetDateFunction(thread_, dateFunction);
898 }
899
InitializeBoolean(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & primRefObjHClass) const900 void Builtins::InitializeBoolean(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &primRefObjHClass) const
901 {
902 [[maybe_unused]] EcmaHandleScope scope(thread_);
903 // Boolean.prototype
904 JSHandle<JSTaggedValue> toObject(thread_, JSTaggedValue::False());
905 JSHandle<JSObject> booleanFuncPrototype =
906 JSHandle<JSObject>::Cast(factory_->NewJSPrimitiveRef(primRefObjHClass, toObject));
907 JSHandle<JSTaggedValue> booleanFuncPrototypeValue(booleanFuncPrototype);
908
909 // Boolean.prototype_or_hclass
910 JSHandle<JSHClass> booleanFuncInstanceHClass =
911 factory_->NewEcmaHClass(JSPrimitiveRef::SIZE, JSType::JS_PRIMITIVE_REF, booleanFuncPrototypeValue);
912
913 // new Boolean Function()
914 JSHandle<JSFunction> booleanFunction = NewBuiltinConstructor(env, booleanFuncPrototype, Boolean::BooleanConstructor,
915 "Boolean", FunctionLength::ONE, BUILTINS_STUB_ID(BooleanConstructor));
916 booleanFunction->SetFunctionPrototype(thread_, booleanFuncInstanceHClass.GetTaggedValue());
917
918 // Boolean.prototype method
919 SetFunction(env, booleanFuncPrototype, thread_->GlobalConstants()->GetHandledToStringString(),
920 Boolean::BooleanPrototypeToString, FunctionLength::ZERO);
921 SetFunction(env, booleanFuncPrototype, thread_->GlobalConstants()->GetHandledValueOfString(),
922 Boolean::BooleanPrototypeValueOf, FunctionLength::ZERO);
923
924 env->SetBooleanFunction(thread_, booleanFunction);
925 }
926
InitializeProxy(const JSHandle<GlobalEnv> & env)927 void Builtins::InitializeProxy(const JSHandle<GlobalEnv> &env)
928 {
929 JSHandle<JSObject> proxyFunction(InitializeExoticConstructor(env, Proxy::ProxyConstructor, "Proxy", 2));
930
931 // Proxy method
932 SetFunction(env, proxyFunction, "revocable", Proxy::Revocable, FunctionLength::TWO);
933 env->SetProxyFunction(thread_, proxyFunction);
934 }
935
InitializeExoticConstructor(const JSHandle<GlobalEnv> & env,EcmaEntrypoint ctorFunc,const char * name,int length)936 JSHandle<JSFunction> Builtins::InitializeExoticConstructor(const JSHandle<GlobalEnv> &env, EcmaEntrypoint ctorFunc,
937 const char *name, int length)
938 {
939 JSHandle<JSFunction> ctor =
940 factory_->NewJSFunction(env, reinterpret_cast<void *>(ctorFunc), FunctionKind::BUILTIN_PROXY_CONSTRUCTOR);
941
942 JSFunction::SetFunctionLength(thread_, ctor, JSTaggedValue(length));
943 JSHandle<JSTaggedValue> nameString(factory_->NewFromUtf8(name));
944 JSFunction::SetFunctionName(thread_, JSHandle<JSFunctionBase>(ctor), nameString,
945 JSHandle<JSTaggedValue>(thread_, JSTaggedValue::Undefined()));
946
947 JSHandle<JSObject> globalObject(thread_, env->GetGlobalObject());
948 PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>(ctor), true, false, true);
949 JSObject::DefineOwnProperty(thread_, globalObject, nameString, descriptor);
950 return ctor;
951 }
952
InitializeAsyncFunction(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncClass) const953 void Builtins::InitializeAsyncFunction(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncClass) const
954 {
955 [[maybe_unused]] EcmaHandleScope scope(thread_);
956 const GlobalEnvConstants *globalConst = thread_->GlobalConstants();
957 // AsyncFunction.prototype
958 JSHandle<JSObject> asyncFuncPrototype = factory_->NewJSObjectWithInit(objFuncClass);
959 JSObject::SetPrototype(thread_, asyncFuncPrototype, env->GetFunctionPrototype());
960 JSHandle<JSTaggedValue> async_func_prototype_value(asyncFuncPrototype);
961
962 // AsyncFunction.prototype_or_hclass
963 JSHandle<JSHClass> asyncFuncInstanceHClass =
964 factory_->NewEcmaHClass(JSAsyncFunction::SIZE, JSType::JS_ASYNC_FUNCTION, async_func_prototype_value);
965
966 // AsyncFunction = new Function()
967 JSHandle<JSFunction> asyncFunction = NewBuiltinConstructor(
968 env, asyncFuncPrototype, AsyncFunction::AsyncFunctionConstructor, "AsyncFunction", FunctionLength::ONE);
969 JSObject::SetPrototype(thread_, JSHandle<JSObject>::Cast(asyncFunction), env->GetFunctionFunction());
970 JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString();
971 PropertyDescriptor asyncDesc(thread_, JSHandle<JSTaggedValue>::Cast(asyncFunction), false, false, true);
972 JSObject::DefineOwnProperty(thread_, asyncFuncPrototype, constructorKey, asyncDesc);
973 asyncFunction->SetProtoOrHClass(thread_, asyncFuncInstanceHClass.GetTaggedValue());
974
975 // AsyncFunction.prototype property
976 SetStringTagSymbol(env, asyncFuncPrototype, "AsyncFunction");
977 env->SetAsyncFunction(thread_, asyncFunction);
978 env->SetAsyncFunctionPrototype(thread_, asyncFuncPrototype);
979 }
980
InitializeAllTypeError(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncClass) const981 void Builtins::InitializeAllTypeError(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncClass) const
982 {
983 // Error.prototype
984 JSHandle<JSObject> errorFuncPrototype = factory_->NewJSObjectWithInit(objFuncClass);
985 JSHandle<JSTaggedValue> errorFuncPrototypeValue(errorFuncPrototype);
986 // Error.prototype_or_hclass
987 JSHandle<JSHClass> errorFuncInstanceHClass =
988 factory_->NewEcmaHClass(JSObject::SIZE, JSType::JS_ERROR, errorFuncPrototypeValue);
989 // Error() = new Function()
990 JSHandle<JSFunction> errorFunction(
991 NewBuiltinConstructor(env, errorFuncPrototype, Error::ErrorConstructor, "Error", FunctionLength::ONE));
992 errorFunction->SetFunctionPrototype(thread_, errorFuncInstanceHClass.GetTaggedValue());
993
994 // Error.prototype method
995 SetFunction(env, errorFuncPrototype, thread_->GlobalConstants()->GetHandledToStringString(), Error::ToString,
996 FunctionLength::ZERO);
997
998 // Error.prototype Attribute
999 SetAttribute(errorFuncPrototype, "name", "Error");
1000 SetAttribute(errorFuncPrototype, "message", "");
1001 env->SetErrorFunction(thread_, errorFunction);
1002
1003 JSHandle<JSHClass> nativeErrorFuncClass =
1004 factory_->NewEcmaHClass(JSFunction::SIZE, JSType::JS_FUNCTION, env->GetErrorFunction());
1005 nativeErrorFuncClass->SetConstructor(true);
1006 env->SetNativeErrorFunctionClass(thread_, nativeErrorFuncClass);
1007
1008 JSHandle<JSHClass> errorNativeFuncInstanceHClass =
1009 factory_->NewEcmaHClass(JSObject::SIZE, JSType::JS_OBJECT, errorFuncPrototypeValue);
1010 InitializeError(env, errorNativeFuncInstanceHClass, JSType::JS_RANGE_ERROR);
1011 InitializeError(env, errorNativeFuncInstanceHClass, JSType::JS_REFERENCE_ERROR);
1012 InitializeError(env, errorNativeFuncInstanceHClass, JSType::JS_TYPE_ERROR);
1013 InitializeError(env, errorNativeFuncInstanceHClass, JSType::JS_AGGREGATE_ERROR);
1014 InitializeError(env, errorNativeFuncInstanceHClass, JSType::JS_URI_ERROR);
1015 InitializeError(env, errorNativeFuncInstanceHClass, JSType::JS_SYNTAX_ERROR);
1016 InitializeError(env, errorNativeFuncInstanceHClass, JSType::JS_EVAL_ERROR);
1017 InitializeError(env, errorNativeFuncInstanceHClass, JSType::JS_OOM_ERROR);
1018 }
1019
InitializeAllTypeErrorWithRealm(const JSHandle<GlobalEnv> & realm) const1020 void Builtins::InitializeAllTypeErrorWithRealm(const JSHandle<GlobalEnv> &realm) const
1021 {
1022 JSHandle<GlobalEnv> env = vm_->GetGlobalEnv();
1023
1024 realm->SetErrorFunction(thread_, env->GetErrorFunction());
1025 realm->SetNativeErrorFunctionClass(thread_, env->GetNativeErrorFunctionClass());
1026
1027 SetErrorWithRealm(realm, JSType::JS_RANGE_ERROR);
1028 SetErrorWithRealm(realm, JSType::JS_REFERENCE_ERROR);
1029 SetErrorWithRealm(realm, JSType::JS_TYPE_ERROR);
1030 SetErrorWithRealm(realm, JSType::JS_AGGREGATE_ERROR);
1031 SetErrorWithRealm(realm, JSType::JS_URI_ERROR);
1032 SetErrorWithRealm(realm, JSType::JS_SYNTAX_ERROR);
1033 SetErrorWithRealm(realm, JSType::JS_EVAL_ERROR);
1034 SetErrorWithRealm(realm, JSType::JS_OOM_ERROR);
1035 }
1036
SetErrorWithRealm(const JSHandle<GlobalEnv> & realm,const JSType & errorTag) const1037 void Builtins::SetErrorWithRealm(const JSHandle<GlobalEnv> &realm, const JSType &errorTag) const
1038 {
1039 JSHandle<GlobalEnv> env = vm_->GetGlobalEnv();
1040 JSHandle<JSObject> globalObject(thread_, realm->GetGlobalObject());
1041 JSHandle<JSTaggedValue> nameString;
1042 JSHandle<JSTaggedValue> nativeErrorFunction;
1043 switch (errorTag) {
1044 case JSType::JS_RANGE_ERROR:
1045 nativeErrorFunction = env->GetRangeErrorFunction();
1046 nameString = JSHandle<JSTaggedValue>(thread_->GlobalConstants()->GetHandledRangeErrorString());
1047 realm->SetRangeErrorFunction(thread_, nativeErrorFunction);
1048 break;
1049 case JSType::JS_EVAL_ERROR:
1050 nativeErrorFunction = env->GetEvalErrorFunction();
1051 nameString = JSHandle<JSTaggedValue>(thread_->GlobalConstants()->GetHandledEvalErrorString());
1052 realm->SetEvalErrorFunction(thread_, nativeErrorFunction);
1053 break;
1054 case JSType::JS_REFERENCE_ERROR:
1055 nativeErrorFunction = env->GetReferenceErrorFunction();
1056 nameString = JSHandle<JSTaggedValue>(thread_->GlobalConstants()->GetHandledReferenceErrorString());
1057 realm->SetReferenceErrorFunction(thread_, nativeErrorFunction);
1058 break;
1059 case JSType::JS_TYPE_ERROR:
1060 nativeErrorFunction = env->GetTypeErrorFunction();
1061 nameString = JSHandle<JSTaggedValue>(thread_->GlobalConstants()->GetHandledTypeErrorString());
1062 realm->SetTypeErrorFunction(thread_, nativeErrorFunction);
1063 realm->SetThrowTypeError(thread_, env->GetThrowTypeError());
1064 break;
1065 case JSType::JS_AGGREGATE_ERROR:
1066 nativeErrorFunction = env->GetAggregateErrorFunction();
1067 nameString = JSHandle<JSTaggedValue>(thread_->GlobalConstants()->GetHandledAggregateErrorString());
1068 realm->SetAggregateErrorFunction(thread_, nativeErrorFunction);
1069 break;
1070 case JSType::JS_URI_ERROR:
1071 nativeErrorFunction = env->GetURIErrorFunction();
1072 nameString = JSHandle<JSTaggedValue>(thread_->GlobalConstants()->GetHandledURIErrorString());
1073 realm->SetURIErrorFunction(thread_, nativeErrorFunction);
1074 break;
1075 case JSType::JS_SYNTAX_ERROR:
1076 nativeErrorFunction = env->GetSyntaxErrorFunction();
1077 nameString = JSHandle<JSTaggedValue>(thread_->GlobalConstants()->GetHandledSyntaxErrorString());
1078 realm->SetSyntaxErrorFunction(thread_, nativeErrorFunction);
1079 break;
1080 case JSType::JS_OOM_ERROR:
1081 nativeErrorFunction = env->GetOOMErrorFunction();
1082 nameString = JSHandle<JSTaggedValue>(thread_->GlobalConstants()->GetHandledOOMErrorString());
1083 realm->SetOOMErrorFunction(thread_, nativeErrorFunction);
1084 break;
1085 default:
1086 break;
1087 }
1088 PropertyDescriptor descriptor(thread_, nativeErrorFunction, true, false, true);
1089 JSObject::DefineOwnProperty(thread_, globalObject, nameString, descriptor);
1090 }
1091
GeneralUpdateError(ErrorParameter * error,EcmaEntrypoint constructor,EcmaEntrypoint method,const char * name,JSType type) const1092 void Builtins::GeneralUpdateError(ErrorParameter *error, EcmaEntrypoint constructor, EcmaEntrypoint method,
1093 const char *name, JSType type) const
1094 {
1095 error->nativeConstructor = constructor;
1096 error->nativeMethod = method;
1097 error->nativePropertyName = name;
1098 error->nativeJstype = type;
1099 }
1100
InitializeError(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncClass,const JSType & errorTag) const1101 void Builtins::InitializeError(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncClass,
1102 const JSType &errorTag) const
1103 {
1104 // NativeError.prototype
1105 JSHandle<JSObject> nativeErrorFuncPrototype = factory_->NewJSObjectWithInit(objFuncClass);
1106 JSHandle<JSTaggedValue> nativeErrorFuncPrototypeValue(nativeErrorFuncPrototype);
1107
1108 ErrorParameter errorParameter{RangeError::RangeErrorConstructor, RangeError::ToString, "RangeError",
1109 JSType::JS_RANGE_ERROR};
1110 switch (errorTag) {
1111 case JSType::JS_RANGE_ERROR:
1112 GeneralUpdateError(&errorParameter, RangeError::RangeErrorConstructor, RangeError::ToString, "RangeError",
1113 JSType::JS_RANGE_ERROR);
1114 break;
1115 case JSType::JS_EVAL_ERROR:
1116 GeneralUpdateError(&errorParameter, EvalError::EvalErrorConstructor, EvalError::ToString, "EvalError",
1117 JSType::JS_EVAL_ERROR);
1118 break;
1119 case JSType::JS_REFERENCE_ERROR:
1120 GeneralUpdateError(&errorParameter, ReferenceError::ReferenceErrorConstructor, ReferenceError::ToString,
1121 "ReferenceError", JSType::JS_REFERENCE_ERROR);
1122 break;
1123 case JSType::JS_TYPE_ERROR:
1124 GeneralUpdateError(&errorParameter, TypeError::TypeErrorConstructor, TypeError::ToString, "TypeError",
1125 JSType::JS_TYPE_ERROR);
1126 break;
1127 case JSType::JS_AGGREGATE_ERROR:
1128 GeneralUpdateError(&errorParameter, AggregateError::AggregateErrorConstructor, AggregateError::ToString,
1129 "AggregateError", JSType::JS_AGGREGATE_ERROR);
1130 break;
1131 case JSType::JS_URI_ERROR:
1132 GeneralUpdateError(&errorParameter, URIError::URIErrorConstructor, URIError::ToString, "URIError",
1133 JSType::JS_URI_ERROR);
1134 break;
1135 case JSType::JS_SYNTAX_ERROR:
1136 GeneralUpdateError(&errorParameter, SyntaxError::SyntaxErrorConstructor, SyntaxError::ToString,
1137 "SyntaxError", JSType::JS_SYNTAX_ERROR);
1138 break;
1139 case JSType::JS_OOM_ERROR:
1140 GeneralUpdateError(&errorParameter, OOMError::OOMErrorConstructor, OOMError::ToString,
1141 "OutOfMemoryError", JSType::JS_OOM_ERROR);
1142 break;
1143 default:
1144 break;
1145 }
1146
1147 // NativeError.prototype_or_hclass
1148 JSHandle<JSHClass> nativeErrorFuncInstanceHClass =
1149 factory_->NewEcmaHClass(JSObject::SIZE, errorParameter.nativeJstype, nativeErrorFuncPrototypeValue);
1150
1151 // NativeError() = new Error()
1152 FunctionLength functionLength = FunctionLength::ONE;
1153 if (errorTag == JSType::JS_AGGREGATE_ERROR) {
1154 functionLength = FunctionLength::TWO;
1155 }
1156 JSHandle<JSFunction> nativeErrorFunction =
1157 factory_->NewJSNativeErrorFunction(env, reinterpret_cast<void *>(errorParameter.nativeConstructor));
1158 InitializeCtor(env, nativeErrorFuncPrototype, nativeErrorFunction, errorParameter.nativePropertyName,
1159 functionLength);
1160
1161 nativeErrorFunction->SetFunctionPrototype(thread_, nativeErrorFuncInstanceHClass.GetTaggedValue());
1162
1163 // NativeError.prototype method
1164 SetFunction(env, nativeErrorFuncPrototype, thread_->GlobalConstants()->GetHandledToStringString(),
1165 errorParameter.nativeMethod, FunctionLength::ZERO);
1166
1167 // Error.prototype Attribute
1168 SetAttribute(nativeErrorFuncPrototype, "name", errorParameter.nativePropertyName);
1169 SetAttribute(nativeErrorFuncPrototype, "message", "");
1170
1171 if (errorTag == JSType::JS_RANGE_ERROR) {
1172 env->SetRangeErrorFunction(thread_, nativeErrorFunction);
1173 } else if (errorTag == JSType::JS_REFERENCE_ERROR) {
1174 env->SetReferenceErrorFunction(thread_, nativeErrorFunction);
1175 } else if (errorTag == JSType::JS_TYPE_ERROR) {
1176 env->SetTypeErrorFunction(thread_, nativeErrorFunction);
1177 JSHandle<JSFunction> throwTypeErrorFunction =
1178 factory_->NewJSFunction(env, reinterpret_cast<void *>(TypeError::ThrowTypeError));
1179 JSFunction::SetFunctionLength(thread_, throwTypeErrorFunction, JSTaggedValue(1), false);
1180 JSObject::PreventExtensions(thread_, JSHandle<JSObject>::Cast(throwTypeErrorFunction));
1181 env->SetThrowTypeError(thread_, throwTypeErrorFunction);
1182 } else if (errorTag == JSType::JS_AGGREGATE_ERROR) {
1183 env->SetAggregateErrorFunction(thread_, nativeErrorFunction);
1184 } else if (errorTag == JSType::JS_URI_ERROR) {
1185 env->SetURIErrorFunction(thread_, nativeErrorFunction);
1186 } else if (errorTag == JSType::JS_SYNTAX_ERROR) {
1187 env->SetSyntaxErrorFunction(thread_, nativeErrorFunction);
1188 } else if (errorTag == JSType::JS_EVAL_ERROR) {
1189 env->SetEvalErrorFunction(thread_, nativeErrorFunction);
1190 } else {
1191 env->SetOOMErrorFunction(thread_, nativeErrorFunction);
1192 }
1193 } // namespace panda::ecmascript
1194
InitializeCtor(const JSHandle<GlobalEnv> & env,const JSHandle<JSObject> & prototype,const JSHandle<JSFunction> & ctor,const char * name,int length) const1195 void Builtins::InitializeCtor(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &prototype,
1196 const JSHandle<JSFunction> &ctor, const char *name, int length) const
1197 {
1198 const GlobalEnvConstants *globalConst = thread_->GlobalConstants();
1199 JSFunction::SetFunctionLength(thread_, ctor, JSTaggedValue(length));
1200 JSHandle<JSTaggedValue> nameString(factory_->NewFromUtf8(name));
1201 JSFunction::SetFunctionName(thread_, JSHandle<JSFunctionBase>(ctor), nameString,
1202 JSHandle<JSTaggedValue>(thread_, JSTaggedValue::Undefined()));
1203 JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString();
1204 PropertyDescriptor descriptor1(thread_, JSHandle<JSTaggedValue>::Cast(ctor), true, false, true);
1205 JSObject::DefineOwnProperty(thread_, prototype, constructorKey, descriptor1);
1206
1207 /* set "prototype" in constructor */
1208 ctor->SetFunctionPrototype(thread_, prototype.GetTaggedValue());
1209
1210 if (!JSTaggedValue::SameValue(nameString, thread_->GlobalConstants()->GetHandledAsyncFunctionString())) {
1211 JSHandle<JSObject> globalObject(thread_, env->GetGlobalObject());
1212 PropertyDescriptor descriptor2(thread_, JSHandle<JSTaggedValue>::Cast(ctor), true, false, true);
1213 JSObject::DefineOwnProperty(thread_, globalObject, nameString, descriptor2);
1214 }
1215 }
1216
InitializeSet(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncClass) const1217 void Builtins::InitializeSet(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncClass) const
1218 {
1219 [[maybe_unused]] EcmaHandleScope scope(thread_);
1220 const GlobalEnvConstants *globalConst = thread_->GlobalConstants();
1221 // Set.prototype
1222 JSHandle<JSObject> setFuncPrototype = factory_->NewJSObjectWithInit(objFuncClass);
1223 JSHandle<JSTaggedValue> setFuncPrototypeValue(setFuncPrototype);
1224 // Set.prototype_or_hclass
1225 JSHandle<JSHClass> setFuncInstanceHClass =
1226 factory_->NewEcmaHClass(JSSet::SIZE, JSType::JS_SET, setFuncPrototypeValue);
1227 // Set() = new Function()
1228 JSHandle<JSTaggedValue> setFunction(
1229 NewBuiltinConstructor(env, setFuncPrototype, BuiltinsSet::SetConstructor, "Set", FunctionLength::ZERO));
1230 JSHandle<JSFunction>(setFunction)->SetFunctionPrototype(thread_, setFuncInstanceHClass.GetTaggedValue());
1231
1232 // "constructor" property on the prototype
1233 JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString();
1234 JSObject::SetProperty(thread_, JSHandle<JSTaggedValue>(setFuncPrototype), constructorKey, setFunction);
1235 // set.prototype.add()
1236 SetFunction(env, setFuncPrototype, "add", BuiltinsSet::Add, FunctionLength::ONE);
1237 // set.prototype.clear()
1238 SetFunction(env, setFuncPrototype, "clear", BuiltinsSet::Clear, FunctionLength::ZERO);
1239 // set.prototype.delete()
1240 SetFunction(env, setFuncPrototype, "delete", BuiltinsSet::Delete, FunctionLength::ONE);
1241 // set.prototype.has()
1242 SetFunction(env, setFuncPrototype, "has", BuiltinsSet::Has, FunctionLength::ONE);
1243 // set.prototype.forEach()
1244 SetFunction(env, setFuncPrototype, "forEach", BuiltinsSet::ForEach, FunctionLength::ONE);
1245 // set.prototype.entries()
1246 SetFunction(env, setFuncPrototype, "entries", BuiltinsSet::Entries, FunctionLength::ZERO);
1247 // set.prototype.keys()
1248 SetFunction(env, setFuncPrototype, "values", BuiltinsSet::Values, FunctionLength::ZERO);
1249 // set.prototype.values()
1250 JSHandle<JSTaggedValue> keys(factory_->NewFromASCII("keys"));
1251 JSHandle<JSTaggedValue> values(factory_->NewFromASCII("values"));
1252 JSHandle<JSTaggedValue> valuesFunc =
1253 JSObject::GetMethod(thread_, JSHandle<JSTaggedValue>::Cast(setFuncPrototype), values);
1254 PropertyDescriptor descriptor(thread_, valuesFunc, true, false, true);
1255 JSObject::DefineOwnProperty(thread_, setFuncPrototype, keys, descriptor);
1256
1257 // @@ToStringTag
1258 SetStringTagSymbol(env, setFuncPrototype, "Set");
1259
1260 // 23.1.3.10get Set.prototype.size
1261 JSHandle<JSTaggedValue> sizeGetter = CreateGetter(env, BuiltinsSet::GetSize, "size", FunctionLength::ZERO);
1262 JSHandle<JSTaggedValue> sizeKey(factory_->NewFromASCII("size"));
1263 SetGetter(setFuncPrototype, sizeKey, sizeGetter);
1264
1265 // 23.1.2.2get Set [ @@species ]
1266 JSHandle<JSTaggedValue> speciesSymbol = env->GetSpeciesSymbol();
1267 JSHandle<JSTaggedValue> speciesGetter =
1268 CreateGetter(env, BuiltinsSet::Species, "[Symbol.species]", FunctionLength::ZERO);
1269 SetGetter(JSHandle<JSObject>(setFunction), speciesSymbol, speciesGetter);
1270
1271 // %SetPrototype% [ @@iterator ]
1272 JSHandle<JSTaggedValue> iteratorSymbol = env->GetIteratorSymbol();
1273 JSObject::DefineOwnProperty(thread_, setFuncPrototype, iteratorSymbol, descriptor);
1274
1275 env->SetBuiltinsSetFunction(thread_, setFunction);
1276 }
1277
InitializeMap(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncClass) const1278 void Builtins::InitializeMap(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncClass) const
1279 {
1280 [[maybe_unused]] EcmaHandleScope scope(thread_);
1281 const GlobalEnvConstants *globalConst = thread_->GlobalConstants();
1282 // Map.prototype
1283 JSHandle<JSObject> mapFuncPrototype = factory_->NewJSObjectWithInit(objFuncClass);
1284 JSHandle<JSTaggedValue> mapFuncPrototypeValue(mapFuncPrototype);
1285 // Map.prototype_or_hclass
1286 JSHandle<JSHClass> mapFuncInstanceHClass =
1287 factory_->NewEcmaHClass(JSMap::SIZE, JSType::JS_MAP, mapFuncPrototypeValue);
1288 // Map() = new Function()
1289 JSHandle<JSTaggedValue> mapFunction(
1290 NewBuiltinConstructor(env, mapFuncPrototype, BuiltinsMap::MapConstructor, "Map", FunctionLength::ZERO));
1291 // Map().prototype = Map.Prototype & Map.prototype.constructor = Map()
1292 JSFunction::Cast(mapFunction->GetTaggedObject())
1293 ->SetFunctionPrototype(thread_, mapFuncInstanceHClass.GetTaggedValue());
1294
1295 // "constructor" property on the prototype
1296 JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString();
1297 JSObject::SetProperty(thread_, JSHandle<JSTaggedValue>(mapFuncPrototype), constructorKey, mapFunction);
1298 // map.prototype.set()
1299 SetFunction(env, mapFuncPrototype, globalConst->GetHandledSetString(), BuiltinsMap::Set, FunctionLength::TWO);
1300 // map.prototype.clear()
1301 SetFunction(env, mapFuncPrototype, "clear", BuiltinsMap::Clear, FunctionLength::ZERO);
1302 // map.prototype.delete()
1303 SetFunction(env, mapFuncPrototype, "delete", BuiltinsMap::Delete, FunctionLength::ONE);
1304 // map.prototype.has()
1305 SetFunction(env, mapFuncPrototype, "has", BuiltinsMap::Has, FunctionLength::ONE);
1306 // map.prototype.get()
1307 SetFunction(env, mapFuncPrototype, thread_->GlobalConstants()->GetHandledGetString(), BuiltinsMap::Get,
1308 FunctionLength::ONE);
1309 // map.prototype.forEach()
1310 SetFunction(env, mapFuncPrototype, "forEach", BuiltinsMap::ForEach, FunctionLength::ONE);
1311 // map.prototype.keys()
1312 SetFunction(env, mapFuncPrototype, "keys", BuiltinsMap::Keys, FunctionLength::ZERO);
1313 // map.prototype.values()
1314 SetFunction(env, mapFuncPrototype, "values", BuiltinsMap::Values, FunctionLength::ZERO);
1315 // map.prototype.entries()
1316 SetFunction(env, mapFuncPrototype, "entries", BuiltinsMap::Entries, FunctionLength::ZERO);
1317 // @@ToStringTag
1318 SetStringTagSymbol(env, mapFuncPrototype, "Map");
1319
1320 // 23.1.3.10get Map.prototype.size
1321 JSHandle<JSTaggedValue> sizeGetter = CreateGetter(env, BuiltinsMap::GetSize, "size", FunctionLength::ZERO);
1322 JSHandle<JSTaggedValue> sizeKey(factory_->NewFromASCII("size"));
1323 SetGetter(mapFuncPrototype, sizeKey, sizeGetter);
1324
1325 // 23.1.2.2get Map [ @@species ]
1326 JSHandle<JSTaggedValue> speciesSymbol = env->GetSpeciesSymbol();
1327 JSHandle<JSTaggedValue> speciesGetter =
1328 CreateGetter(env, BuiltinsMap::Species, "[Symbol.species]", FunctionLength::ZERO);
1329 SetGetter(JSHandle<JSObject>(mapFunction), speciesSymbol, speciesGetter);
1330
1331 // %MapPrototype% [ @@iterator ]
1332 JSHandle<JSTaggedValue> iteratorSymbol = env->GetIteratorSymbol();
1333 JSHandle<JSTaggedValue> entries(factory_->NewFromASCII("entries"));
1334 JSHandle<JSTaggedValue> entriesFunc =
1335 JSObject::GetMethod(thread_, JSHandle<JSTaggedValue>::Cast(mapFuncPrototype), entries);
1336 PropertyDescriptor descriptor(thread_, entriesFunc, true, false, true);
1337 JSObject::DefineOwnProperty(thread_, mapFuncPrototype, iteratorSymbol, descriptor);
1338
1339 env->SetBuiltinsMapFunction(thread_, mapFunction);
1340 env->SetMapPrototype(thread_, mapFuncPrototype);
1341 }
1342
InitializeWeakMap(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncClass) const1343 void Builtins::InitializeWeakMap(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncClass) const
1344 {
1345 [[maybe_unused]] EcmaHandleScope scope(thread_);
1346 const GlobalEnvConstants *globalConst = thread_->GlobalConstants();
1347 // WeakMap.prototype
1348 JSHandle<JSObject> weakMapFuncPrototype = factory_->NewJSObjectWithInit(objFuncClass);
1349 JSHandle<JSTaggedValue> weakMapFuncPrototypeValue(weakMapFuncPrototype);
1350 // WeakMap.prototype_or_hclass
1351 JSHandle<JSHClass> weakMapFuncInstanceHClass =
1352 factory_->NewEcmaHClass(JSWeakMap::SIZE, JSType::JS_WEAK_MAP, weakMapFuncPrototypeValue);
1353 // WeakMap() = new Function()
1354 JSHandle<JSTaggedValue> weakMapFunction(NewBuiltinConstructor(
1355 env, weakMapFuncPrototype, BuiltinsWeakMap::WeakMapConstructor, "WeakMap", FunctionLength::ZERO));
1356 // WeakMap().prototype = WeakMap.Prototype & WeakMap.prototype.constructor = WeakMap()
1357 JSFunction::Cast(weakMapFunction->GetTaggedObject())
1358 ->SetProtoOrHClass(thread_, weakMapFuncInstanceHClass.GetTaggedValue());
1359
1360 // "constructor" property on the prototype
1361 JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString();
1362 JSObject::SetProperty(thread_, JSHandle<JSTaggedValue>(weakMapFuncPrototype), constructorKey, weakMapFunction);
1363 // weakmap.prototype.set()
1364 SetFunction(env, weakMapFuncPrototype, globalConst->GetHandledSetString(), BuiltinsWeakMap::Set,
1365 FunctionLength::TWO);
1366 // weakmap.prototype.delete()
1367 SetFunction(env, weakMapFuncPrototype, "delete", BuiltinsWeakMap::Delete, FunctionLength::ONE);
1368 // weakmap.prototype.has()
1369 SetFunction(env, weakMapFuncPrototype, "has", BuiltinsWeakMap::Has, FunctionLength::ONE);
1370 // weakmap.prototype.get()
1371 SetFunction(env, weakMapFuncPrototype, thread_->GlobalConstants()->GetHandledGetString(), BuiltinsWeakMap::Get,
1372 FunctionLength::ONE);
1373 // @@ToStringTag
1374 SetStringTagSymbol(env, weakMapFuncPrototype, "WeakMap");
1375
1376 env->SetBuiltinsWeakMapFunction(thread_, weakMapFunction);
1377 }
1378
InitializeWeakSet(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncClass) const1379 void Builtins::InitializeWeakSet(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncClass) const
1380 {
1381 [[maybe_unused]] EcmaHandleScope scope(thread_);
1382 const GlobalEnvConstants *globalConst = thread_->GlobalConstants();
1383 // Set.prototype
1384 JSHandle<JSObject> weakSetFuncPrototype = factory_->NewJSObjectWithInit(objFuncClass);
1385 JSHandle<JSTaggedValue> weakSetFuncPrototypeValue(weakSetFuncPrototype);
1386 // Set.prototype_or_hclass
1387 JSHandle<JSHClass> weakSetFuncInstanceHClass =
1388 factory_->NewEcmaHClass(JSWeakSet::SIZE, JSType::JS_WEAK_SET, weakSetFuncPrototypeValue);
1389 // Set() = new Function()
1390 JSHandle<JSTaggedValue> weakSetFunction(NewBuiltinConstructor(
1391 env, weakSetFuncPrototype, BuiltinsWeakSet::WeakSetConstructor, "WeakSet", FunctionLength::ZERO));
1392 JSHandle<JSFunction>(weakSetFunction)->SetProtoOrHClass(thread_, weakSetFuncInstanceHClass.GetTaggedValue());
1393
1394 // "constructor" property on the prototype
1395 JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString();
1396 JSObject::SetProperty(thread_, JSHandle<JSTaggedValue>(weakSetFuncPrototype), constructorKey, weakSetFunction);
1397 // set.prototype.add()
1398 SetFunction(env, weakSetFuncPrototype, "add", BuiltinsWeakSet::Add, FunctionLength::ONE);
1399 // set.prototype.delete()
1400 SetFunction(env, weakSetFuncPrototype, "delete", BuiltinsWeakSet::Delete, FunctionLength::ONE);
1401 // set.prototype.has()
1402 SetFunction(env, weakSetFuncPrototype, "has", BuiltinsWeakSet::Has, FunctionLength::ONE);
1403
1404 // @@ToStringTag
1405 SetStringTagSymbol(env, weakSetFuncPrototype, "WeakSet");
1406
1407 env->SetBuiltinsWeakSetFunction(thread_, weakSetFunction);
1408 }
1409
InitializeAtomics(const JSHandle<GlobalEnv> & env,const JSHandle<JSTaggedValue> & objFuncPrototypeVal) const1410 void Builtins::InitializeAtomics(const JSHandle<GlobalEnv> &env,
1411 const JSHandle<JSTaggedValue> &objFuncPrototypeVal) const
1412 {
1413 [[maybe_unused]] EcmaHandleScope scope(thread_);
1414 JSHandle<JSHClass> atomicsHClass = factory_->NewEcmaHClass(JSObject::SIZE, JSType::JS_OBJECT,
1415 objFuncPrototypeVal);
1416 JSHandle<JSObject> atomicsObject = factory_->NewJSObject(atomicsHClass);
1417 SetFunction(env, atomicsObject, "add", Atomics::Add, FunctionLength::THREE);
1418 SetFunction(env, atomicsObject, "and", Atomics::And, FunctionLength::THREE);
1419 SetFunction(env, atomicsObject, "sub", Atomics::Sub, FunctionLength::THREE);
1420 SetFunction(env, atomicsObject, "or", Atomics::Or, FunctionLength::THREE);
1421 SetFunction(env, atomicsObject, "xor", Atomics::Xor, FunctionLength::THREE);
1422 SetFunction(env, atomicsObject, "compareExchange", Atomics::CompareExchange, FunctionLength::FOUR);
1423 SetFunction(env, atomicsObject, "exchange", Atomics::Exchange, FunctionLength::THREE);
1424 SetFunction(env, atomicsObject, "isLockFree", Atomics::IsLockFree, FunctionLength::ONE);
1425 SetFunction(env, atomicsObject, "load", Atomics::Load, FunctionLength::TWO);
1426 SetFunction(env, atomicsObject, "store", Atomics::Store, FunctionLength::THREE);
1427 SetFunction(env, atomicsObject, "wait", Atomics::Wait, FunctionLength::FOUR);
1428 SetFunction(env, atomicsObject, "notify", Atomics::Notify, FunctionLength::THREE);
1429 JSHandle<JSTaggedValue> atomicsString(factory_->NewFromASCII("Atomics"));
1430 JSHandle<JSObject> globalObject(thread_, env->GetGlobalObject());
1431 PropertyDescriptor atomicsDesc(thread_, JSHandle<JSTaggedValue>::Cast(atomicsObject), true, false, true);
1432 JSObject::DefineOwnProperty(thread_, globalObject, atomicsString, atomicsDesc);
1433 // @@ToStringTag
1434 SetStringTagSymbol(env, atomicsObject, "Atomics");
1435 env->SetAtomicsFunction(thread_, atomicsObject);
1436 }
1437
InitializeWeakRef(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncClass) const1438 void Builtins::InitializeWeakRef(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncClass) const
1439 {
1440 [[maybe_unused]] EcmaHandleScope scope(thread_);
1441 const GlobalEnvConstants *globalConst = thread_->GlobalConstants();
1442 // WeakRef.prototype
1443 JSHandle<JSObject> weakRefFuncPrototype = factory_->NewJSObject(objFuncClass);
1444 JSHandle<JSTaggedValue> weakRefFuncPrototypeValue(weakRefFuncPrototype);
1445 // WeakRef.prototype_or_hclass
1446 JSHandle<JSHClass> weakRefFuncInstanceHClass =
1447 factory_->NewEcmaHClass(JSWeakRef::SIZE, JSType::JS_WEAK_REF, weakRefFuncPrototypeValue);
1448 // WeakRef() = new Function()
1449 JSHandle<JSTaggedValue> weakRefFunction(NewBuiltinConstructor(
1450 env, weakRefFuncPrototype, BuiltinsWeakRef::WeakRefConstructor, "WeakRef", FunctionLength::ONE));
1451 JSHandle<JSFunction>(weakRefFunction)->SetProtoOrHClass(thread_, weakRefFuncInstanceHClass.GetTaggedValue());
1452
1453 // "constructor" property on the prototype
1454 JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString();
1455 JSObject::SetProperty(thread_, JSHandle<JSTaggedValue>(weakRefFuncPrototype), constructorKey, weakRefFunction);
1456 // WeakRef.prototype.deref()
1457 SetFunction(env, weakRefFuncPrototype, "deref", BuiltinsWeakRef::Deref, FunctionLength::ZERO);
1458
1459 // @@ToStringTag
1460 SetStringTagSymbol(env, weakRefFuncPrototype, "WeakRef");
1461
1462 env->SetBuiltinsWeakRefFunction(thread_, weakRefFunction);
1463 }
1464
InitializeFinalizationRegistry(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncClass) const1465 void Builtins::InitializeFinalizationRegistry(const JSHandle<GlobalEnv> &env,
1466 const JSHandle<JSHClass> &objFuncClass) const
1467 {
1468 [[maybe_unused]] EcmaHandleScope scope(thread_);
1469 const GlobalEnvConstants *globalConst = thread_->GlobalConstants();
1470 // FinalizationRegistry.prototype
1471 JSHandle<JSObject> finalizationRegistryFuncPrototype = factory_->NewJSObject(objFuncClass);
1472 JSHandle<JSTaggedValue> finalizationRegistryFuncPrototypeValue(finalizationRegistryFuncPrototype);
1473 // FinalizationRegistry.prototype_or_hclass
1474 JSHandle<JSHClass> finalizationRegistryFuncInstanceHClass =
1475 factory_->NewEcmaHClass(JSFinalizationRegistry::SIZE, JSType::JS_FINALIZATION_REGISTRY,
1476 finalizationRegistryFuncPrototypeValue);
1477 // FinalizationRegistry() = new Function()
1478 JSHandle<JSTaggedValue> finalizationRegistryFunction(NewBuiltinConstructor(
1479 env, finalizationRegistryFuncPrototype, BuiltinsFinalizationRegistry::FinalizationRegistryConstructor,
1480 "FinalizationRegistry", FunctionLength::ONE));
1481 JSHandle<JSFunction>(finalizationRegistryFunction)->SetProtoOrHClass(
1482 thread_, finalizationRegistryFuncInstanceHClass.GetTaggedValue());
1483
1484 // "constructor" property on the prototype
1485 JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString();
1486 JSObject::SetProperty(thread_, JSHandle<JSTaggedValue>(finalizationRegistryFuncPrototype),
1487 constructorKey, finalizationRegistryFunction);
1488 // FinalizationRegistry.prototype.deref()
1489 SetFunction(env, finalizationRegistryFuncPrototype, "register",
1490 BuiltinsFinalizationRegistry::Register, FunctionLength::TWO);
1491 SetFunction(env, finalizationRegistryFuncPrototype, "unregister",
1492 BuiltinsFinalizationRegistry::Unregister, FunctionLength::ONE);
1493 // @@ToStringTag
1494 SetStringTagSymbol(env, finalizationRegistryFuncPrototype, "FinalizationRegistry");
1495
1496 env->SetBuiltinsFinalizationRegistryFunction(thread_, finalizationRegistryFunction);
1497 }
1498
InitializeMath(const JSHandle<GlobalEnv> & env,const JSHandle<JSTaggedValue> & objFuncPrototypeVal) const1499 void Builtins::InitializeMath(const JSHandle<GlobalEnv> &env, const JSHandle<JSTaggedValue> &objFuncPrototypeVal) const
1500 {
1501 [[maybe_unused]] EcmaHandleScope scope(thread_);
1502 JSHandle<JSHClass> mathClass = factory_->NewEcmaHClass(JSObject::SIZE, JSType::JS_OBJECT, objFuncPrototypeVal);
1503 JSHandle<JSObject> mathObject = factory_->NewJSObjectWithInit(mathClass);
1504 RandomGenerator::InitRandom();
1505 SetFunction(env, mathObject, "abs", Math::Abs, FunctionLength::ONE, BUILTINS_STUB_ID(ABS));
1506 SetFunction(env, mathObject, "acos", Math::Acos, FunctionLength::ONE, BUILTINS_STUB_ID(ACOS));
1507 SetFunction(env, mathObject, "acosh", Math::Acosh, FunctionLength::ONE);
1508 SetFunction(env, mathObject, "asin", Math::Asin, FunctionLength::ONE);
1509 SetFunction(env, mathObject, "asinh", Math::Asinh, FunctionLength::ONE);
1510 SetFunction(env, mathObject, "atan", Math::Atan, FunctionLength::ONE, BUILTINS_STUB_ID(ATAN));
1511 SetFunction(env, mathObject, "atanh", Math::Atanh, FunctionLength::ONE);
1512 SetFunction(env, mathObject, "atan2", Math::Atan2, FunctionLength::TWO);
1513 SetFunction(env, mathObject, "cbrt", Math::Cbrt, FunctionLength::ONE);
1514 SetFunction(env, mathObject, "ceil", Math::Ceil, FunctionLength::ONE);
1515 SetFunction(env, mathObject, "clz32", Math::Clz32, FunctionLength::ONE);
1516 SetFunction(env, mathObject, "cos", Math::Cos, FunctionLength::ONE, BUILTINS_STUB_ID(COS));
1517 SetFunction(env, mathObject, "cosh", Math::Cosh, FunctionLength::ONE);
1518 SetFunction(env, mathObject, "exp", Math::Exp, FunctionLength::ONE);
1519 SetFunction(env, mathObject, "expm1", Math::Expm1, FunctionLength::ONE);
1520 SetFunction(env, mathObject, "floor", Math::Floor, FunctionLength::ONE, BUILTINS_STUB_ID(FLOOR));
1521 SetFunction(env, mathObject, "fround", Math::Fround, FunctionLength::ONE);
1522 SetFunction(env, mathObject, "hypot", Math::Hypot, FunctionLength::TWO);
1523 SetFunction(env, mathObject, "imul", Math::Imul, FunctionLength::TWO);
1524 SetFunction(env, mathObject, "log", Math::Log, FunctionLength::ONE);
1525 SetFunction(env, mathObject, "log1p", Math::Log1p, FunctionLength::ONE);
1526 SetFunction(env, mathObject, "log10", Math::Log10, FunctionLength::ONE);
1527 SetFunction(env, mathObject, "log2", Math::Log2, FunctionLength::ONE);
1528 SetFunction(env, mathObject, "max", Math::Max, FunctionLength::TWO);
1529 SetFunction(env, mathObject, "min", Math::Min, FunctionLength::TWO);
1530 SetFunction(env, mathObject, "pow", Math::Pow, FunctionLength::TWO);
1531 SetFunction(env, mathObject, "random", Math::Random, FunctionLength::ZERO);
1532 SetFunction(env, mathObject, "round", Math::Round, FunctionLength::ONE);
1533 SetFunction(env, mathObject, "sign", Math::Sign, FunctionLength::ONE);
1534 SetFunction(env, mathObject, "sin", Math::Sin, FunctionLength::ONE, BUILTINS_STUB_ID(SIN));
1535 SetFunction(env, mathObject, "sinh", Math::Sinh, FunctionLength::ONE);
1536 SetFunction(env, mathObject, "sqrt", Math::Sqrt, FunctionLength::ONE, BUILTINS_STUB_ID(SQRT));
1537 SetFunction(env, mathObject, "tan", Math::Tan, FunctionLength::ONE);
1538 SetFunction(env, mathObject, "tanh", Math::Tanh, FunctionLength::ONE);
1539 SetFunction(env, mathObject, "trunc", Math::Trunc, FunctionLength::ONE);
1540
1541 SetConstant(mathObject, "E", JSTaggedValue(Math::E));
1542 SetConstant(mathObject, "LN10", JSTaggedValue(Math::LN10));
1543 SetConstant(mathObject, "LN2", JSTaggedValue(Math::LN2));
1544 SetConstant(mathObject, "LOG10E", JSTaggedValue(Math::LOG10E));
1545 SetConstant(mathObject, "LOG2E", JSTaggedValue(Math::LOG2E));
1546 SetConstant(mathObject, "PI", JSTaggedValue(Math::PI));
1547 SetConstant(mathObject, "SQRT1_2", JSTaggedValue(Math::SQRT1_2));
1548 SetConstant(mathObject, "SQRT2", JSTaggedValue(Math::SQRT2));
1549
1550 JSHandle<JSTaggedValue> mathString(factory_->NewFromASCII("Math"));
1551 JSHandle<JSObject> globalObject(thread_, env->GetGlobalObject());
1552 PropertyDescriptor mathDesc(thread_, JSHandle<JSTaggedValue>::Cast(mathObject), true, false, true);
1553 JSObject::DefineOwnProperty(thread_, globalObject, mathString, mathDesc);
1554 // @@ToStringTag
1555 SetStringTagSymbol(env, mathObject, "Math");
1556 env->SetMathFunction(thread_, mathObject);
1557 }
1558
InitializeJson(const JSHandle<GlobalEnv> & env,const JSHandle<JSTaggedValue> & objFuncPrototypeVal) const1559 void Builtins::InitializeJson(const JSHandle<GlobalEnv> &env, const JSHandle<JSTaggedValue> &objFuncPrototypeVal) const
1560 {
1561 [[maybe_unused]] EcmaHandleScope scope(thread_);
1562 JSHandle<JSHClass> jsonHClass = factory_->NewEcmaHClass(JSObject::SIZE, JSType::JS_OBJECT, objFuncPrototypeVal);
1563 JSHandle<JSObject> jsonObject = factory_->NewJSObjectWithInit(jsonHClass);
1564
1565 SetFunction(env, jsonObject, "parse", Json::Parse, FunctionLength::TWO);
1566 SetFunction(env, jsonObject, "stringify", Json::Stringify, FunctionLength::THREE);
1567
1568 PropertyDescriptor jsonDesc(thread_, JSHandle<JSTaggedValue>::Cast(jsonObject), true, false, true);
1569 JSHandle<JSTaggedValue> jsonString(factory_->NewFromASCII("JSON"));
1570 JSHandle<JSObject> globalObject(thread_, env->GetGlobalObject());
1571 JSObject::DefineOwnProperty(thread_, globalObject, jsonString, jsonDesc);
1572 // @@ToStringTag
1573 SetStringTagSymbol(env, jsonObject, "JSON");
1574 env->SetJsonFunction(thread_, jsonObject);
1575 }
1576
InitializeString(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & primRefObjHClass) const1577 void Builtins::InitializeString(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &primRefObjHClass) const
1578 {
1579 [[maybe_unused]] EcmaHandleScope scope(thread_);
1580 // String.prototype
1581 JSHandle<JSTaggedValue> toObject(factory_->GetEmptyString());
1582 JSHandle<JSObject> stringFuncPrototype =
1583 JSHandle<JSObject>::Cast(factory_->NewJSPrimitiveRef(primRefObjHClass, toObject));
1584 JSHandle<JSTaggedValue> stringFuncPrototypeValue(stringFuncPrototype);
1585
1586 // String.prototype_or_hclass
1587 JSHandle<JSHClass> stringFuncInstanceHClass =
1588 factory_->NewEcmaHClass(JSPrimitiveRef::SIZE, JSType::JS_PRIMITIVE_REF, stringFuncPrototypeValue);
1589
1590 // String = new Function()
1591 JSHandle<JSObject> stringFunction(NewBuiltinConstructor(env, stringFuncPrototype, BuiltinsString::StringConstructor,
1592 "String", FunctionLength::ONE));
1593 stringFunction.GetObject<JSFunction>()->SetFunctionPrototype(thread_, stringFuncInstanceHClass.GetTaggedValue());
1594
1595 // String.prototype method
1596 SetFunction(env, stringFuncPrototype, "charAt", BuiltinsString::CharAt, FunctionLength::ONE,
1597 BUILTINS_STUB_ID(CharAt));
1598 SetFunction(env, stringFuncPrototype, "charCodeAt", BuiltinsString::CharCodeAt, FunctionLength::ONE,
1599 BUILTINS_STUB_ID(CharCodeAt));
1600 SetFunction(env, stringFuncPrototype, "codePointAt", BuiltinsString::CodePointAt, FunctionLength::ONE);
1601 SetFunction(env, stringFuncPrototype, "concat", BuiltinsString::Concat, FunctionLength::ONE);
1602 SetFunction(env, stringFuncPrototype, "endsWith", BuiltinsString::EndsWith, FunctionLength::ONE);
1603 SetFunction(env, stringFuncPrototype, "includes", BuiltinsString::Includes, FunctionLength::ONE);
1604 SetFunction(env, stringFuncPrototype, "indexOf", BuiltinsString::IndexOf, FunctionLength::ONE,
1605 BUILTINS_STUB_ID(IndexOf));
1606 SetFunction(env, stringFuncPrototype, "lastIndexOf", BuiltinsString::LastIndexOf, FunctionLength::ONE);
1607 SetFunction(env, stringFuncPrototype, "localeCompare", BuiltinsString::LocaleCompare, FunctionLength::ONE);
1608 SetFunction(env, stringFuncPrototype, "match", BuiltinsString::Match, FunctionLength::ONE);
1609 SetFunction(env, stringFuncPrototype, "matchAll", BuiltinsString::MatchAll, FunctionLength::ONE);
1610 SetFunction(env, stringFuncPrototype, "repeat", BuiltinsString::Repeat, FunctionLength::ONE);
1611 SetFunction(env, stringFuncPrototype, "normalize", BuiltinsString::Normalize, FunctionLength::ZERO);
1612 SetFunction(env, stringFuncPrototype, "padStart", BuiltinsString::PadStart, FunctionLength::ONE);
1613 SetFunction(env, stringFuncPrototype, "padEnd", BuiltinsString::PadEnd, FunctionLength::ONE);
1614 SetFunction(env, stringFuncPrototype, "replace", BuiltinsString::Replace, FunctionLength::TWO);
1615 SetFunction(env, stringFuncPrototype, "replaceAll", BuiltinsString::ReplaceAll, FunctionLength::TWO);
1616 SetFunction(env, stringFuncPrototype, "search", BuiltinsString::Search, FunctionLength::ONE);
1617 SetFunction(env, stringFuncPrototype, "slice", BuiltinsString::Slice, FunctionLength::TWO);
1618 SetFunction(env, stringFuncPrototype, "split", BuiltinsString::Split, FunctionLength::TWO);
1619 SetFunction(env, stringFuncPrototype, "startsWith", BuiltinsString::StartsWith, FunctionLength::ONE);
1620 SetFunction(env, stringFuncPrototype, "substring", BuiltinsString::Substring, FunctionLength::TWO,
1621 BUILTINS_STUB_ID(Substring));
1622 SetFunction(env, stringFuncPrototype, "substr", BuiltinsString::SubStr, FunctionLength::TWO);
1623 SetFunction(env, stringFuncPrototype, "toLocaleLowerCase", BuiltinsString::ToLocaleLowerCase, FunctionLength::ZERO);
1624 SetFunction(env, stringFuncPrototype, "toLocaleUpperCase", BuiltinsString::ToLocaleUpperCase, FunctionLength::ZERO);
1625 SetFunction(env, stringFuncPrototype, "toLowerCase", BuiltinsString::ToLowerCase, FunctionLength::ZERO);
1626 SetFunction(env, stringFuncPrototype, thread_->GlobalConstants()->GetHandledToStringString(),
1627 BuiltinsString::ToString, FunctionLength::ZERO);
1628 SetFunction(env, stringFuncPrototype, "toUpperCase", BuiltinsString::ToUpperCase, FunctionLength::ZERO);
1629 SetFunction(env, stringFuncPrototype, "trim", BuiltinsString::Trim, FunctionLength::ZERO);
1630 SetFunction(env, stringFuncPrototype, "trimStart", BuiltinsString::TrimStart, FunctionLength::ZERO);
1631 SetFunction(env, stringFuncPrototype, "trimEnd", BuiltinsString::TrimEnd, FunctionLength::ZERO);
1632 SetFunction(env, stringFuncPrototype, "trimLeft", BuiltinsString::TrimLeft, FunctionLength::ZERO);
1633 SetFunction(env, stringFuncPrototype, "trimRight", BuiltinsString::TrimRight, FunctionLength::ZERO);
1634 SetFunction(env, stringFuncPrototype, thread_->GlobalConstants()->GetHandledValueOfString(),
1635 BuiltinsString::ValueOf, FunctionLength::ZERO);
1636 SetFunctionAtSymbol(env, stringFuncPrototype, env->GetIteratorSymbol(), "[Symbol.iterator]",
1637 BuiltinsString::GetStringIterator, FunctionLength::ZERO);
1638
1639 // String method
1640 SetFunction(env, stringFunction, "fromCharCode", BuiltinsString::FromCharCode, FunctionLength::ONE);
1641 SetFunction(env, stringFunction, "fromCodePoint", BuiltinsString::FromCodePoint, FunctionLength::ONE);
1642 SetFunction(env, stringFunction, "raw", BuiltinsString::Raw, FunctionLength::ONE);
1643
1644 // String.prototype.length
1645 JSHandle<JSTaggedValue> lengthGetter = CreateGetter(env, BuiltinsString::GetLength, "length", FunctionLength::ZERO);
1646 JSHandle<JSTaggedValue> lengthKey(factory_->NewFromASCII("length"));
1647 SetGetter(stringFuncPrototype, lengthKey, lengthGetter);
1648
1649 env->SetStringFunction(thread_, stringFunction);
1650 }
1651
InitializeStringIterator(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & iteratorFuncClass) const1652 void Builtins::InitializeStringIterator(const JSHandle<GlobalEnv> &env,
1653 const JSHandle<JSHClass> &iteratorFuncClass) const
1654 {
1655 // StringIterator.prototype
1656 JSHandle<JSObject> strIterPrototype(factory_->NewJSObjectWithInit(iteratorFuncClass));
1657
1658 // StringIterator.prototype_or_hclass
1659 JSHandle<JSHClass> strIterFuncInstanceHClass = factory_->NewEcmaHClass(
1660 JSStringIterator::SIZE, JSType::JS_STRING_ITERATOR, JSHandle<JSTaggedValue>(strIterPrototype));
1661
1662 JSHandle<JSFunction> strIterFunction(
1663 factory_->NewJSFunction(env, static_cast<void *>(nullptr), FunctionKind::BASE_CONSTRUCTOR));
1664 strIterFunction->SetFunctionPrototype(thread_, strIterFuncInstanceHClass.GetTaggedValue());
1665
1666 SetFunction(env, strIterPrototype, "next", StringIterator::Next, FunctionLength::ZERO);
1667 SetStringTagSymbol(env, strIterPrototype, "String Iterator");
1668
1669 env->SetStringIterator(thread_, strIterFunction);
1670 env->SetStringIteratorPrototype(thread_, strIterPrototype);
1671 }
1672
InitializeAsyncFromSyncIterator(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & iteratorFuncClass) const1673 void Builtins::InitializeAsyncFromSyncIterator(const JSHandle<GlobalEnv> &env,
1674 const JSHandle<JSHClass> &iteratorFuncClass) const
1675 {
1676 [[maybe_unused]] EcmaHandleScope scope(thread_);
1677
1678 JSHandle<JSObject> asyncItPrototype = factory_->NewJSObjectWithInit(iteratorFuncClass);
1679 SetFunction(env, asyncItPrototype, "next", BuiltinsAsyncFromSyncIterator::Next, FunctionLength::ONE);
1680 SetFunction(env, asyncItPrototype, "return", BuiltinsAsyncFromSyncIterator::Return, FunctionLength::ONE);
1681 SetFunction(env, asyncItPrototype, "throw", BuiltinsAsyncFromSyncIterator::Throw, FunctionLength::ONE);
1682 JSHandle<JSHClass> hclass = factory_->NewEcmaHClass(JSAsyncFromSyncIterator::SIZE,
1683 JSType::JS_ASYNC_FROM_SYNC_ITERATOR,
1684 JSHandle<JSTaggedValue>(asyncItPrototype));
1685 JSHandle<JSFunction> iterFunction(
1686 factory_->NewJSFunction(env, static_cast<void *>(nullptr), FunctionKind::BASE_CONSTRUCTOR));
1687 iterFunction->SetFunctionPrototype(thread_, hclass.GetTaggedValue());
1688 env->SetAsyncFromSyncIterator(thread_, iterFunction);
1689 env->SetAsyncFromSyncIteratorPrototype(thread_, asyncItPrototype);
1690
1691 JSHandle<JSHClass> asyncFromSyncIterUnwarpClass =
1692 factory_->NewEcmaHClass(JSAsyncFromSyncIterUnwarpFunction::SIZE,
1693 JSType::JS_ASYNC_FROM_SYNC_ITER_UNWARP_FUNCTION,
1694 env->GetFunctionPrototype());
1695 asyncFromSyncIterUnwarpClass->SetCallable(true);
1696 asyncFromSyncIterUnwarpClass->SetExtensible(true);
1697 env->SetAsyncFromSyncIterUnwarpClass(thread_, asyncFromSyncIterUnwarpClass);
1698 }
1699
InitializeIterator(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncClass) const1700 void Builtins::InitializeIterator(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncClass) const
1701 {
1702 [[maybe_unused]] EcmaHandleScope scope(thread_);
1703 // Iterator.prototype
1704 JSHandle<JSObject> iteratorPrototype = factory_->NewJSObjectWithInit(objFuncClass);
1705 // Iterator.prototype.next()
1706 SetFunction(env, iteratorPrototype, "next", BuiltinsIterator::Next, FunctionLength::ONE);
1707 // Iterator.prototype.return()
1708 SetFunction(env, iteratorPrototype, "return", BuiltinsIterator::Return, FunctionLength::ONE);
1709 // Iterator.prototype.throw()
1710 SetFunction(env, iteratorPrototype, "throw", BuiltinsIterator::Throw, FunctionLength::ONE);
1711 // %IteratorPrototype% [ @@iterator ]
1712 SetFunctionAtSymbol(env, iteratorPrototype, env->GetIteratorSymbol(), "[Symbol.iterator]",
1713 BuiltinsIterator::GetIteratorObj, FunctionLength::ZERO);
1714 env->SetIteratorPrototype(thread_, iteratorPrototype);
1715
1716 // Iterator.hclass
1717 JSHandle<JSHClass> iteratorFuncClass =
1718 factory_->NewEcmaHClass(JSObject::SIZE, JSType::JS_ITERATOR, JSHandle<JSTaggedValue>(iteratorPrototype));
1719
1720 auto globalConst = const_cast<GlobalEnvConstants *>(thread_->GlobalConstants());
1721 globalConst->SetConstant(ConstantIndex::JS_API_ITERATOR_FUNC_CLASS_INDEX, iteratorFuncClass);
1722
1723 // Iterator result hclass
1724 JSHandle<JSHClass> iterResultHClass = factory_->CreateIteratorResultInstanceClass();
1725 globalConst->SetConstant(ConstantIndex::ITERATOR_RESULT_CLASS, iterResultHClass);
1726
1727 // ues for CloseIterator
1728 JSHandle<CompletionRecord> record =
1729 factory_->NewCompletionRecord(CompletionRecordType::NORMAL, globalConst->GetHandledUndefined());
1730 globalConst->SetConstant(ConstantIndex::UNDEFINED_COMPLRTION_RECORD_INDEX, record);
1731
1732 InitializeForinIterator(env, iteratorFuncClass);
1733 InitializeSetIterator(env, iteratorFuncClass);
1734 InitializeMapIterator(env, iteratorFuncClass);
1735 InitializeArrayIterator(env, iteratorFuncClass);
1736 InitializeStringIterator(env, iteratorFuncClass);
1737 InitializeRegexpIterator(env, iteratorFuncClass);
1738 }
1739
InitializeAsyncIterator(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncDynclass) const1740 void Builtins::InitializeAsyncIterator(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncDynclass) const
1741 {
1742 [[maybe_unused]] EcmaHandleScope scope(thread_);
1743 // AsyncIterator.prototype
1744 JSHandle<JSObject> asyncIteratorPrototype = factory_->NewJSObjectWithInit(objFuncDynclass);
1745 // AsyncIterator.prototype.next()
1746 SetFunction(env, asyncIteratorPrototype, "next", BuiltinsAsyncIterator::Next, FunctionLength::ONE);
1747 // AsyncIterator.prototype.return()
1748 SetFunction(env, asyncIteratorPrototype, "return", BuiltinsAsyncIterator::Return, FunctionLength::ONE);
1749 // AsyncIterator.prototype.throw()
1750 SetFunction(env, asyncIteratorPrototype, "throw", BuiltinsAsyncIterator::Throw, FunctionLength::ONE);
1751 // %AsyncIteratorPrototype% [ @@AsyncIterator ]
1752 SetFunctionAtSymbol(env, asyncIteratorPrototype, env->GetAsyncIteratorSymbol(), "[Symbol.asyncIterator]",
1753 BuiltinsAsyncIterator::GetAsyncIteratorObj, FunctionLength::ZERO);
1754 env->SetAsyncIteratorPrototype(thread_, asyncIteratorPrototype);
1755
1756 // AsyncIterator.dynclass
1757 JSHandle<JSHClass> asyncIteratorFuncDynclass =
1758 factory_->NewEcmaHClass(JSObject::SIZE,
1759 JSType::JS_ASYNCITERATOR, JSHandle<JSTaggedValue>(asyncIteratorPrototype));
1760
1761 auto globalConst = const_cast<GlobalEnvConstants *>(thread_->GlobalConstants());
1762 globalConst->SetConstant(ConstantIndex::JS_API_ASYNCITERATOR_FUNC_CLASS_INDEX, asyncIteratorFuncDynclass);
1763 }
1764
InitializeForinIterator(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & iteratorFuncClass) const1765 void Builtins::InitializeForinIterator(const JSHandle<GlobalEnv> &env,
1766 const JSHandle<JSHClass> &iteratorFuncClass) const
1767 {
1768 [[maybe_unused]] EcmaHandleScope scope(thread_);
1769 // Iterator.prototype
1770 JSHandle<JSObject> forinIteratorPrototype = factory_->NewJSObjectWithInit(iteratorFuncClass);
1771 JSHandle<JSHClass> hclass = factory_->NewEcmaHClass(JSForInIterator::SIZE, JSType::JS_FORIN_ITERATOR,
1772 JSHandle<JSTaggedValue>(forinIteratorPrototype));
1773
1774 // Iterator.prototype.next()
1775 SetFunction(env, forinIteratorPrototype, "next", JSForInIterator::Next, FunctionLength::ONE);
1776 env->SetForinIteratorPrototype(thread_, forinIteratorPrototype);
1777 env->SetForinIteratorClass(thread_, hclass);
1778 }
1779
InitializeSetIterator(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & iteratorFuncClass) const1780 void Builtins::InitializeSetIterator(const JSHandle<GlobalEnv> &env,
1781 const JSHandle<JSHClass> &iteratorFuncClass) const
1782 {
1783 // SetIterator.prototype
1784 JSHandle<JSObject> setIteratorPrototype(factory_->NewJSObjectWithInit(iteratorFuncClass));
1785 // Iterator.prototype.next()
1786 SetFunction(env, setIteratorPrototype, "next", JSSetIterator::Next, FunctionLength::ZERO);
1787 SetStringTagSymbol(env, setIteratorPrototype, "Set Iterator");
1788 env->SetSetIteratorPrototype(thread_, setIteratorPrototype);
1789 }
1790
InitializeMapIterator(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & iteratorFuncClass) const1791 void Builtins::InitializeMapIterator(const JSHandle<GlobalEnv> &env,
1792 const JSHandle<JSHClass> &iteratorFuncClass) const
1793 {
1794 // MapIterator.prototype
1795 JSHandle<JSObject> mapIteratorPrototype(factory_->NewJSObjectWithInit(iteratorFuncClass));
1796 // Iterator.prototype.next()
1797 SetFunction(env, mapIteratorPrototype, "next", JSMapIterator::Next, FunctionLength::ZERO);
1798 SetStringTagSymbol(env, mapIteratorPrototype, "Map Iterator");
1799 env->SetMapIteratorPrototype(thread_, mapIteratorPrototype);
1800 }
InitializeArrayIterator(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & iteratorFuncClass) const1801 void Builtins::InitializeArrayIterator(const JSHandle<GlobalEnv> &env,
1802 const JSHandle<JSHClass> &iteratorFuncClass) const
1803 {
1804 // ArrayIterator.prototype
1805 JSHandle<JSObject> arrayIteratorPrototype(factory_->NewJSObjectWithInit(iteratorFuncClass));
1806 // Iterator.prototype.next()
1807 SetFunction(env, arrayIteratorPrototype, "next", JSArrayIterator::Next, FunctionLength::ZERO);
1808 SetStringTagSymbol(env, arrayIteratorPrototype, "Array Iterator");
1809 env->SetArrayIteratorPrototype(thread_, arrayIteratorPrototype);
1810 }
1811
InitializeRegexpIterator(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & iteratorFuncClass) const1812 void Builtins::InitializeRegexpIterator(const JSHandle<GlobalEnv> &env,
1813 const JSHandle<JSHClass> &iteratorFuncClass) const
1814 {
1815 // RegExpIterator.prototype
1816 JSHandle<JSObject> regExpIteratorPrototype(factory_->NewJSObject(iteratorFuncClass));
1817 // Iterator.prototype.next()
1818 SetFunction(env, regExpIteratorPrototype, "next", JSRegExpIterator::Next, FunctionLength::ZERO);
1819 SetStringTagSymbol(env, regExpIteratorPrototype, "RegExp String Iterator");
1820 env->SetRegExpIteratorPrototype(thread_, regExpIteratorPrototype);
1821 }
1822
InitializeRegExp(const JSHandle<GlobalEnv> & env)1823 void Builtins::InitializeRegExp(const JSHandle<GlobalEnv> &env)
1824 {
1825 [[maybe_unused]] EcmaHandleScope scope(thread_);
1826 // RegExp.prototype
1827 JSHandle<JSFunction> objFun(env->GetObjectFunction());
1828 JSHandle<JSObject> regPrototype = factory_->NewJSObjectByConstructor(objFun);
1829 JSHandle<JSTaggedValue> regPrototypeValue(regPrototype);
1830
1831 // RegExp.prototype_or_hclass
1832 JSHandle<JSHClass> regexpFuncInstanceHClass = factory_->CreateJSRegExpInstanceClass(regPrototypeValue);
1833
1834 // RegExp = new Function()
1835 JSHandle<JSObject> regexpFunction(
1836 NewBuiltinConstructor(env, regPrototype, RegExp::RegExpConstructor, "RegExp", FunctionLength::TWO));
1837
1838 // initialize RegExp.$1 .. $9 static and read-only attributes
1839 InitializeGlobalRegExp(regexpFunction);
1840
1841 JSHandle<JSFunction>(regexpFunction)->SetFunctionPrototype(thread_, regexpFuncInstanceHClass.GetTaggedValue());
1842
1843 const GlobalEnvConstants *globalConstants = thread_->GlobalConstants();
1844 // RegExp.prototype method
1845 SetFunction(env, regPrototype, "exec", RegExp::Exec, FunctionLength::ONE);
1846 SetFunction(env, regPrototype, "test", RegExp::Test, FunctionLength::ONE);
1847 SetFunction(env, regPrototype, globalConstants->GetHandledToStringString(), RegExp::ToString,
1848 FunctionLength::ZERO);
1849
1850 JSHandle<JSTaggedValue> flagsGetter = CreateGetter(env, RegExp::GetFlags, "flags", FunctionLength::ZERO);
1851 JSHandle<JSTaggedValue> flagsKey(globalConstants->GetHandledFlagsString());
1852 SetGetter(regPrototype, flagsKey, flagsGetter);
1853
1854 JSHandle<JSTaggedValue> sourceGetter = CreateGetter(env, RegExp::GetSource, "source", FunctionLength::ZERO);
1855 JSHandle<JSTaggedValue> sourceKey(globalConstants->GetHandledSourceString());
1856 SetGetter(regPrototype, sourceKey, sourceGetter);
1857
1858 JSHandle<JSTaggedValue> globalGetter = CreateGetter(env, RegExp::GetGlobal, "global", FunctionLength::ZERO);
1859 JSHandle<JSTaggedValue> globalKey(globalConstants->GetHandledGlobalString());
1860 SetGetter(regPrototype, globalKey, globalGetter);
1861
1862 JSHandle<JSTaggedValue> ignoreCaseGetter =
1863 CreateGetter(env, RegExp::GetIgnoreCase, "ignoreCase", FunctionLength::ZERO);
1864 JSHandle<JSTaggedValue> ignoreCaseKey(factory_->NewFromASCII("ignoreCase"));
1865 SetGetter(regPrototype, ignoreCaseKey, ignoreCaseGetter);
1866
1867 JSHandle<JSTaggedValue> multilineGetter =
1868 CreateGetter(env, RegExp::GetMultiline, "multiline", FunctionLength::ZERO);
1869 JSHandle<JSTaggedValue> multilineKey(factory_->NewFromASCII("multiline"));
1870 SetGetter(regPrototype, multilineKey, multilineGetter);
1871
1872 JSHandle<JSTaggedValue> dotAllGetter = CreateGetter(env, RegExp::GetDotAll, "dotAll", FunctionLength::ZERO);
1873 JSHandle<JSTaggedValue> dotAllKey(factory_->NewFromASCII("dotAll"));
1874 SetGetter(regPrototype, dotAllKey, dotAllGetter);
1875
1876 JSHandle<JSTaggedValue> stickyGetter = CreateGetter(env, RegExp::GetSticky, "sticky", FunctionLength::ZERO);
1877 JSHandle<JSTaggedValue> stickyKey(globalConstants->GetHandledStickyString());
1878 SetGetter(regPrototype, stickyKey, stickyGetter);
1879
1880 JSHandle<JSTaggedValue> unicodeGetter = CreateGetter(env, RegExp::GetUnicode, "unicode", FunctionLength::ZERO);
1881 JSHandle<JSTaggedValue> unicodeKey(globalConstants->GetHandledUnicodeString());
1882 SetGetter(regPrototype, unicodeKey, unicodeGetter);
1883
1884 // Set RegExp [ @@species ]
1885 JSHandle<JSTaggedValue> speciesSymbol = env->GetSpeciesSymbol();
1886 JSHandle<JSTaggedValue> speciesGetter =
1887 CreateGetter(env, BuiltinsMap::Species, "[Symbol.species]", FunctionLength::ZERO);
1888 SetGetter(JSHandle<JSObject>(regexpFunction), speciesSymbol, speciesGetter);
1889
1890 // Set RegExp.prototype[@@split]
1891 SetFunctionAtSymbol(env, regPrototype, env->GetSplitSymbol(), "[Symbol.split]", RegExp::Split, FunctionLength::TWO);
1892 // Set RegExp.prototype[@@search]
1893 SetFunctionAtSymbol(env, regPrototype, env->GetSearchSymbol(), "[Symbol.search]", RegExp::Search,
1894 FunctionLength::ONE);
1895 // Set RegExp.prototype[@@match]
1896 SetFunctionAtSymbol(env, regPrototype, env->GetMatchSymbol(), "[Symbol.match]", RegExp::Match, FunctionLength::ONE);
1897 // Set RegExp.prototype[@@matchAll]
1898 SetFunctionAtSymbol(env, regPrototype, env->GetMatchAllSymbol(), "[Symbol.matchAll]", RegExp::MatchAll,
1899 FunctionLength::ONE);
1900 // Set RegExp.prototype[@@replace]
1901 SetFunctionAtSymbol(env, regPrototype, env->GetReplaceSymbol(), "[Symbol.replace]", RegExp::Replace,
1902 FunctionLength::TWO);
1903
1904 env->SetRegExpFunction(thread_, regexpFunction);
1905 auto globalConst = const_cast<GlobalEnvConstants *>(thread_->GlobalConstants());
1906 globalConst->SetConstant(ConstantIndex::JS_REGEXP_CLASS_INDEX, regexpFuncInstanceHClass.GetTaggedValue());
1907 }
1908
InitializeArray(const JSHandle<GlobalEnv> & env,const JSHandle<JSTaggedValue> & objFuncPrototypeVal) const1909 void Builtins::InitializeArray(const JSHandle<GlobalEnv> &env, const JSHandle<JSTaggedValue> &objFuncPrototypeVal) const
1910 {
1911 [[maybe_unused]] EcmaHandleScope scope(thread_);
1912 // Arraybase.prototype
1913 JSHandle<JSHClass> arrBaseFuncInstanceHClass = factory_->CreateJSArrayInstanceClass(objFuncPrototypeVal);
1914
1915 // Array.prototype
1916 JSHandle<JSObject> arrFuncPrototype = factory_->NewJSObjectWithInit(arrBaseFuncInstanceHClass);
1917 JSHandle<JSArray>::Cast(arrFuncPrototype)->SetLength(thread_, JSTaggedValue(FunctionLength::ZERO));
1918 auto accessor = thread_->GlobalConstants()->GetArrayLengthAccessor();
1919 JSArray::Cast(*arrFuncPrototype)->SetPropertyInlinedProps(thread_, JSArray::LENGTH_INLINE_PROPERTY_INDEX, accessor);
1920 JSHandle<JSTaggedValue> arrFuncPrototypeValue(arrFuncPrototype);
1921
1922 // Array.prototype_or_hclass
1923 JSHandle<JSHClass> arrFuncInstanceHClass = factory_->CreateJSArrayInstanceClass(arrFuncPrototypeValue);
1924
1925 // Array = new Function()
1926 JSHandle<JSObject> arrayFunction(
1927 NewBuiltinConstructor(env, arrFuncPrototype, BuiltinsArray::ArrayConstructor, "Array", FunctionLength::ONE,
1928 BUILTINS_STUB_ID(ArrayConstructor)));
1929 JSHandle<JSFunction> arrayFuncFunction(arrayFunction);
1930
1931 // Set the [[Realm]] internal slot of F to the running execution context's Realm
1932 JSHandle<LexicalEnv> lexicalEnv = factory_->NewLexicalEnv(0);
1933 lexicalEnv->SetParentEnv(thread_, env.GetTaggedValue());
1934 arrayFuncFunction->SetLexicalEnv(thread_, lexicalEnv.GetTaggedValue());
1935
1936 arrayFuncFunction->SetFunctionPrototype(thread_, arrFuncInstanceHClass.GetTaggedValue());
1937
1938 // Array.prototype method
1939 SetFunction(env, arrFuncPrototype, "concat", BuiltinsArray::Concat, FunctionLength::ONE);
1940 SetFunction(env, arrFuncPrototype, "copyWithin", BuiltinsArray::CopyWithin, FunctionLength::TWO);
1941 SetFunction(env, arrFuncPrototype, "entries", BuiltinsArray::Entries, FunctionLength::ZERO);
1942 SetFunction(env, arrFuncPrototype, "every", BuiltinsArray::Every, FunctionLength::ONE);
1943 SetFunction(env, arrFuncPrototype, "fill", BuiltinsArray::Fill, FunctionLength::ONE);
1944 SetFunction(env, arrFuncPrototype, "filter", BuiltinsArray::Filter, FunctionLength::ONE);
1945 SetFunction(env, arrFuncPrototype, "find", BuiltinsArray::Find, FunctionLength::ONE);
1946 SetFunction(env, arrFuncPrototype, "findIndex", BuiltinsArray::FindIndex, FunctionLength::ONE);
1947 SetFunction(env, arrFuncPrototype, "forEach", BuiltinsArray::ForEach, FunctionLength::ONE);
1948 SetFunction(env, arrFuncPrototype, "indexOf", BuiltinsArray::IndexOf, FunctionLength::ONE);
1949 SetFunction(env, arrFuncPrototype, "join", BuiltinsArray::Join, FunctionLength::ONE);
1950 SetFunction(env, arrFuncPrototype, "keys", BuiltinsArray::Keys, FunctionLength::ZERO);
1951 SetFunction(env, arrFuncPrototype, "lastIndexOf", BuiltinsArray::LastIndexOf, FunctionLength::ONE);
1952 SetFunction(env, arrFuncPrototype, "map", BuiltinsArray::Map, FunctionLength::ONE);
1953 SetFunction(env, arrFuncPrototype, "pop", BuiltinsArray::Pop, FunctionLength::ZERO);
1954 SetFunction(env, arrFuncPrototype, "push", BuiltinsArray::Push, FunctionLength::ONE);
1955 SetFunction(env, arrFuncPrototype, "reduce", BuiltinsArray::Reduce, FunctionLength::ONE);
1956 SetFunction(env, arrFuncPrototype, "reduceRight", BuiltinsArray::ReduceRight, FunctionLength::ONE);
1957 SetFunction(env, arrFuncPrototype, "reverse", BuiltinsArray::Reverse, FunctionLength::ZERO);
1958 SetFunction(env, arrFuncPrototype, "shift", BuiltinsArray::Shift, FunctionLength::ZERO);
1959 SetFunction(env, arrFuncPrototype, "slice", BuiltinsArray::Slice, FunctionLength::TWO);
1960 SetFunction(env, arrFuncPrototype, "some", BuiltinsArray::Some, FunctionLength::ONE);
1961 SetFunction(env, arrFuncPrototype, "sort", BuiltinsArray::Sort, FunctionLength::ONE);
1962 SetFunction(env, arrFuncPrototype, "splice", BuiltinsArray::Splice, FunctionLength::TWO);
1963 SetFunction(env, arrFuncPrototype, thread_->GlobalConstants()->GetHandledToLocaleStringString(),
1964 BuiltinsArray::ToLocaleString, FunctionLength::ZERO);
1965 SetFunction(env, arrFuncPrototype, thread_->GlobalConstants()->GetHandledToStringString(), BuiltinsArray::ToString,
1966 FunctionLength::ZERO);
1967 SetFunction(env, arrFuncPrototype, "unshift", BuiltinsArray::Unshift, FunctionLength::ONE);
1968 SetFunction(env, arrFuncPrototype, "values", BuiltinsArray::Values, FunctionLength::ZERO);
1969 SetFunction(env, arrFuncPrototype, "includes", BuiltinsArray::Includes, FunctionLength::ONE);
1970 SetFunction(env, arrFuncPrototype, "flat", BuiltinsArray::Flat, FunctionLength::ZERO);
1971 SetFunction(env, arrFuncPrototype, "flatMap", BuiltinsArray::FlatMap, FunctionLength::ONE);
1972
1973 // %ArrayPrototype% [ @@iterator ]
1974 JSHandle<JSTaggedValue> values(factory_->NewFromASCII("values"));
1975 JSHandle<JSTaggedValue> iteratorSymbol = env->GetIteratorSymbol();
1976 JSHandle<JSTaggedValue> valuesFunc =
1977 JSObject::GetMethod(thread_, JSHandle<JSTaggedValue>::Cast(arrFuncPrototype), values);
1978 PropertyDescriptor iteartorDesc(thread_, valuesFunc, true, false, true);
1979 JSObject::DefineOwnProperty(thread_, arrFuncPrototype, iteratorSymbol, iteartorDesc);
1980
1981 // Array method
1982 SetFunction(env, arrayFunction, "from", BuiltinsArray::From, FunctionLength::ONE);
1983 SetFunction(env, arrayFunction, "isArray", BuiltinsArray::IsArray, FunctionLength::ONE);
1984 SetFunction(env, arrayFunction, "of", BuiltinsArray::Of, FunctionLength::ZERO);
1985
1986 // 22.1.2.5 get %Array% [ @@species ]
1987 JSHandle<JSTaggedValue> speciesSymbol = env->GetSpeciesSymbol();
1988 JSHandle<JSTaggedValue> speciesGetter =
1989 CreateGetter(env, BuiltinsArray::Species, "[Symbol.species]", FunctionLength::ZERO);
1990 SetGetter(JSHandle<JSObject>(arrayFunction), speciesSymbol, speciesGetter);
1991
1992 constexpr int arrProtoLen = 0;
1993 JSHandle<JSTaggedValue> key_string = thread_->GlobalConstants()->GetHandledLengthString();
1994 PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>(thread_, JSTaggedValue(arrProtoLen)), true, false,
1995 false);
1996 JSObject::DefineOwnProperty(thread_, arrFuncPrototype, key_string, descriptor);
1997
1998 JSHandle<JSTaggedValue> valuesKey(factory_->NewFromASCII("values"));
1999 PropertyDescriptor desc(thread_);
2000 JSObject::GetOwnProperty(thread_, arrFuncPrototype, valuesKey, desc);
2001
2002 // Array.prototype [ @@unscopables ]
2003 JSHandle<JSTaggedValue> unscopablesSymbol = env->GetUnscopablesSymbol();
2004 JSHandle<JSTaggedValue> unscopablesGetter =
2005 CreateGetter(env, BuiltinsArray::Unscopables, "[Symbol.unscopables]", FunctionLength::ZERO);
2006 SetGetter(JSHandle<JSObject>(arrFuncPrototype), unscopablesSymbol, unscopablesGetter);
2007
2008 env->SetArrayProtoValuesFunction(thread_, desc.GetValue());
2009 env->SetArrayFunction(thread_, arrayFunction);
2010 env->SetArrayPrototype(thread_, arrFuncPrototype);
2011 }
2012
InitializeTypedArray(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncClass) const2013 void Builtins::InitializeTypedArray(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncClass) const
2014 {
2015 [[maybe_unused]] EcmaHandleScope scope(thread_);
2016 // TypedArray.prototype
2017 JSHandle<JSObject> typedArrFuncPrototype = factory_->NewJSObjectWithInit(objFuncClass);
2018 JSHandle<JSTaggedValue> typedArrFuncPrototypeValue(typedArrFuncPrototype);
2019
2020 // TypedArray.prototype_or_hclass
2021 JSHandle<JSHClass> typedArrFuncInstanceHClass = factory_->NewEcmaHClass(
2022 panda::ecmascript::JSTypedArray::SIZE, JSType::JS_TYPED_ARRAY, typedArrFuncPrototypeValue);
2023
2024 // TypedArray = new Function()
2025 JSHandle<JSObject> typedArrayFunction(NewBuiltinConstructor(
2026 env, typedArrFuncPrototype, BuiltinsTypedArray::TypedArrayBaseConstructor, "TypedArray", FunctionLength::ZERO));
2027
2028 JSHandle<JSFunction>(typedArrayFunction)
2029 ->SetProtoOrHClass(thread_, typedArrFuncInstanceHClass.GetTaggedValue());
2030
2031 // TypedArray.prototype method
2032 SetFunction(env, typedArrFuncPrototype, "copyWithin", BuiltinsTypedArray::CopyWithin, FunctionLength::TWO);
2033 SetFunction(env, typedArrFuncPrototype, "entries", BuiltinsTypedArray::Entries, FunctionLength::ZERO);
2034 SetFunction(env, typedArrFuncPrototype, "every", BuiltinsTypedArray::Every, FunctionLength::ONE);
2035 SetFunction(env, typedArrFuncPrototype, "fill", BuiltinsTypedArray::Fill, FunctionLength::ONE);
2036 SetFunction(env, typedArrFuncPrototype, "filter", BuiltinsTypedArray::Filter, FunctionLength::ONE);
2037 SetFunction(env, typedArrFuncPrototype, "find", BuiltinsTypedArray::Find, FunctionLength::ONE);
2038 SetFunction(env, typedArrFuncPrototype, "findIndex", BuiltinsTypedArray::FindIndex, FunctionLength::ONE);
2039 SetFunction(env, typedArrFuncPrototype, "forEach", BuiltinsTypedArray::ForEach, FunctionLength::ONE);
2040 SetFunction(env, typedArrFuncPrototype, "indexOf", BuiltinsTypedArray::IndexOf, FunctionLength::ONE);
2041 SetFunction(env, typedArrFuncPrototype, "join", BuiltinsTypedArray::Join, FunctionLength::ONE);
2042 SetFunction(env, typedArrFuncPrototype, "keys", BuiltinsTypedArray::Keys, FunctionLength::ZERO);
2043 SetFunction(env, typedArrFuncPrototype, "lastIndexOf", BuiltinsTypedArray::LastIndexOf, FunctionLength::ONE);
2044 SetFunction(env, typedArrFuncPrototype, "map", BuiltinsTypedArray::Map, FunctionLength::ONE);
2045 SetFunction(env, typedArrFuncPrototype, "reduce", BuiltinsTypedArray::Reduce, FunctionLength::ONE);
2046 SetFunction(env, typedArrFuncPrototype, "reduceRight", BuiltinsTypedArray::ReduceRight, FunctionLength::ONE);
2047 SetFunction(env, typedArrFuncPrototype, "reverse", BuiltinsTypedArray::Reverse, FunctionLength::ZERO);
2048 SetFunction(env, typedArrFuncPrototype, "set", BuiltinsTypedArray::Set, FunctionLength::ONE);
2049 SetFunction(env, typedArrFuncPrototype, "slice", BuiltinsTypedArray::Slice, FunctionLength::TWO);
2050 SetFunction(env, typedArrFuncPrototype, "some", BuiltinsTypedArray::Some, FunctionLength::ONE);
2051 SetFunction(env, typedArrFuncPrototype, "sort", BuiltinsTypedArray::Sort, FunctionLength::ONE);
2052 SetFunction(env, typedArrFuncPrototype, "subarray", BuiltinsTypedArray::Subarray, FunctionLength::TWO);
2053 SetFunction(env, typedArrFuncPrototype, thread_->GlobalConstants()->GetHandledToLocaleStringString(),
2054 BuiltinsTypedArray::ToLocaleString, FunctionLength::ZERO);
2055 SetFunction(env, typedArrFuncPrototype, "values", BuiltinsTypedArray::Values, FunctionLength::ZERO);
2056 SetFunction(env, typedArrFuncPrototype, "includes", BuiltinsTypedArray::Includes, FunctionLength::ONE);
2057
2058 JSHandle<JSTaggedValue> bufferGetter =
2059 CreateGetter(env, BuiltinsTypedArray::GetBuffer, "buffer", FunctionLength::ZERO);
2060 JSHandle<JSTaggedValue> bufferKey(factory_->NewFromASCII("buffer"));
2061 SetGetter(typedArrFuncPrototype, bufferKey, bufferGetter);
2062
2063 JSHandle<JSTaggedValue> byteLengthGetter =
2064 CreateGetter(env, BuiltinsTypedArray::GetByteLength, "byteLength", FunctionLength::ZERO);
2065 JSHandle<JSTaggedValue> byteLengthKey(factory_->NewFromASCII("byteLength"));
2066 SetGetter(typedArrFuncPrototype, byteLengthKey, byteLengthGetter);
2067
2068 JSHandle<JSTaggedValue> byteOffsetGetter =
2069 CreateGetter(env, BuiltinsTypedArray::GetByteOffset, "byteOffset", FunctionLength::ZERO);
2070 JSHandle<JSTaggedValue> byteOffsetKey(factory_->NewFromASCII("byteOffset"));
2071 SetGetter(typedArrFuncPrototype, byteOffsetKey, byteOffsetGetter);
2072
2073 JSHandle<JSTaggedValue> lengthGetter =
2074 CreateGetter(env, BuiltinsTypedArray::GetLength, "length", FunctionLength::ZERO);
2075 JSHandle<JSTaggedValue> lengthKey(factory_->NewFromASCII("length"));
2076 SetGetter(typedArrFuncPrototype, lengthKey, lengthGetter);
2077
2078 // %TypedArray%.prototype.toString()
2079 JSHandle<JSTaggedValue> arrFuncPrototype = env->GetArrayPrototype();
2080 JSHandle<JSTaggedValue> toStringFunc =
2081 JSObject::GetMethod(thread_, arrFuncPrototype, thread_->GlobalConstants()->GetHandledToStringString());
2082 PropertyDescriptor toStringDesc(thread_, toStringFunc, true, false, true);
2083 JSObject::DefineOwnProperty(thread_, typedArrFuncPrototype, thread_->GlobalConstants()->GetHandledToStringString(),
2084 toStringDesc);
2085
2086 // %TypedArray%.prototype [ @@iterator ] ( )
2087 JSHandle<JSTaggedValue> values(factory_->NewFromASCII("values"));
2088 JSHandle<JSTaggedValue> iteratorSymbol = env->GetIteratorSymbol();
2089 JSHandle<JSTaggedValue> valuesFunc =
2090 JSObject::GetMethod(thread_, JSHandle<JSTaggedValue>::Cast(typedArrFuncPrototype), values);
2091 PropertyDescriptor iteartorDesc(thread_, valuesFunc, true, false, true);
2092 JSObject::DefineOwnProperty(thread_, typedArrFuncPrototype, iteratorSymbol, iteartorDesc);
2093
2094 // 22.2.3.31 get %TypedArray%.prototype [ @@toStringTag ]
2095 JSHandle<JSTaggedValue> toStringTagSymbol = env->GetToStringTagSymbol();
2096 JSHandle<JSTaggedValue> toStringTagGetter =
2097 CreateGetter(env, BuiltinsTypedArray::ToStringTag, "[Symbol.toStringTag]", FunctionLength::ZERO);
2098 SetGetter(typedArrFuncPrototype, toStringTagSymbol, toStringTagGetter);
2099
2100 // TypedArray method
2101 SetFunction(env, typedArrayFunction, "from", BuiltinsTypedArray::From, FunctionLength::ONE);
2102 SetFunction(env, typedArrayFunction, "of", BuiltinsTypedArray::Of, FunctionLength::ZERO);
2103
2104 // 22.2.2.4 get %TypedArray% [ @@species ]
2105 JSHandle<JSTaggedValue> speciesSymbol = env->GetSpeciesSymbol();
2106 JSHandle<JSTaggedValue> speciesGetter =
2107 CreateGetter(env, BuiltinsTypedArray::Species, "[Symbol.species]", FunctionLength::ZERO);
2108 SetGetter(JSHandle<JSObject>(typedArrayFunction), speciesSymbol, speciesGetter);
2109
2110 env->SetTypedArrayFunction(thread_, typedArrayFunction.GetTaggedValue());
2111 env->SetTypedArrayPrototype(thread_, typedArrFuncPrototype);
2112
2113 JSHandle<JSHClass> specificTypedArrayFuncClass =
2114 factory_->NewEcmaHClass(JSFunction::SIZE, JSType::JS_FUNCTION, env->GetTypedArrayFunction());
2115 specificTypedArrayFuncClass->SetConstructor(true);
2116 env->SetSpecificTypedArrayFunctionClass(thread_, specificTypedArrayFuncClass);
2117
2118 InitializeInt8Array(env, typedArrFuncInstanceHClass);
2119 InitializeUint8Array(env, typedArrFuncInstanceHClass);
2120 InitializeUint8ClampedArray(env, typedArrFuncInstanceHClass);
2121 InitializeInt16Array(env, typedArrFuncInstanceHClass);
2122 InitializeUint16Array(env, typedArrFuncInstanceHClass);
2123 InitializeInt32Array(env, typedArrFuncInstanceHClass);
2124 InitializeUint32Array(env, typedArrFuncInstanceHClass);
2125 InitializeFloat32Array(env, typedArrFuncInstanceHClass);
2126 InitializeFloat64Array(env, typedArrFuncInstanceHClass);
2127 InitializeBigInt64Array(env, typedArrFuncInstanceHClass);
2128 InitializeBigUint64Array(env, typedArrFuncInstanceHClass);
2129 }
2130
InitializeInt8Array(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncClass) const2131 void Builtins::InitializeInt8Array(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncClass) const
2132 {
2133 [[maybe_unused]] EcmaHandleScope scope(thread_);
2134 // Int8Array.prototype
2135 JSHandle<JSObject> int8ArrFuncPrototype = factory_->NewJSObjectWithInit(objFuncClass);
2136 JSHandle<JSTaggedValue> int8ArrFuncPrototypeValue(int8ArrFuncPrototype);
2137
2138 // Int8Array.prototype_or_hclass
2139 JSHandle<JSHClass> int8ArrFuncInstanceHClass = factory_->NewEcmaHClass(
2140 panda::ecmascript::JSTypedArray::SIZE, JSType::JS_INT8_ARRAY, int8ArrFuncPrototypeValue);
2141
2142 // Int8Array = new Function()
2143 JSHandle<JSFunction> int8ArrayFunction = factory_->NewSpecificTypedArrayFunction(
2144 env, reinterpret_cast<void *>(BuiltinsTypedArray::Int8ArrayConstructor));
2145 InitializeCtor(env, int8ArrFuncPrototype, int8ArrayFunction, "Int8Array", FunctionLength::THREE);
2146
2147 int8ArrayFunction->SetProtoOrHClass(thread_, int8ArrFuncInstanceHClass.GetTaggedValue());
2148
2149 constexpr int bytesPerElement = 1;
2150 SetConstant(int8ArrFuncPrototype, "BYTES_PER_ELEMENT", JSTaggedValue(bytesPerElement));
2151 SetConstant(JSHandle<JSObject>(int8ArrayFunction), "BYTES_PER_ELEMENT", JSTaggedValue(bytesPerElement));
2152 env->SetInt8ArrayFunction(thread_, int8ArrayFunction);
2153 }
2154
InitializeUint8Array(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncClass) const2155 void Builtins::InitializeUint8Array(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncClass) const
2156 {
2157 [[maybe_unused]] EcmaHandleScope scope(thread_);
2158 // Uint8Array.prototype
2159 JSHandle<JSObject> uint8ArrFuncPrototype = factory_->NewJSObjectWithInit(objFuncClass);
2160 JSHandle<JSTaggedValue> uint8ArrFuncPrototypeValue(uint8ArrFuncPrototype);
2161
2162 // Uint8Array.prototype_or_hclass
2163 JSHandle<JSHClass> uint8ArrFuncInstanceHClass = factory_->NewEcmaHClass(
2164 panda::ecmascript::JSTypedArray::SIZE, JSType::JS_UINT8_ARRAY, uint8ArrFuncPrototypeValue);
2165
2166 // Uint8Array = new Function()
2167 JSHandle<JSFunction> uint8ArrayFunction = factory_->NewSpecificTypedArrayFunction(
2168 env, reinterpret_cast<void *>(BuiltinsTypedArray::Uint8ArrayConstructor));
2169 InitializeCtor(env, uint8ArrFuncPrototype, uint8ArrayFunction, "Uint8Array", FunctionLength::THREE);
2170
2171 uint8ArrayFunction->SetProtoOrHClass(thread_, uint8ArrFuncInstanceHClass.GetTaggedValue());
2172
2173 constexpr int bytesPerElement = 1;
2174 SetConstant(uint8ArrFuncPrototype, "BYTES_PER_ELEMENT", JSTaggedValue(bytesPerElement));
2175 SetConstant(JSHandle<JSObject>(uint8ArrayFunction), "BYTES_PER_ELEMENT", JSTaggedValue(bytesPerElement));
2176 env->SetUint8ArrayFunction(thread_, uint8ArrayFunction);
2177 }
2178
InitializeUint8ClampedArray(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncClass) const2179 void Builtins::InitializeUint8ClampedArray(const JSHandle<GlobalEnv> &env,
2180 const JSHandle<JSHClass> &objFuncClass) const
2181 {
2182 [[maybe_unused]] EcmaHandleScope scope(thread_);
2183 // Uint8ClampedArray.prototype
2184 JSHandle<JSObject> uint8ClampedArrFuncPrototype = factory_->NewJSObjectWithInit(objFuncClass);
2185 JSHandle<JSTaggedValue> uint8ClampedArrFuncPrototypeValue(uint8ClampedArrFuncPrototype);
2186
2187 // Uint8ClampedArray.prototype_or_hclass
2188 JSHandle<JSHClass> uint8ClampedArrFuncInstanceHClass =
2189 factory_->NewEcmaHClass(panda::ecmascript::JSTypedArray::SIZE, JSType::JS_UINT8_CLAMPED_ARRAY,
2190 uint8ClampedArrFuncPrototypeValue);
2191
2192 // Uint8ClampedArray = new Function()
2193 JSHandle<JSFunction> uint8ClampedArrayFunction = factory_->NewSpecificTypedArrayFunction(
2194 env, reinterpret_cast<void *>(BuiltinsTypedArray::Uint8ClampedArrayConstructor));
2195 InitializeCtor(env, uint8ClampedArrFuncPrototype, uint8ClampedArrayFunction, "Uint8ClampedArray",
2196 FunctionLength::THREE);
2197
2198 uint8ClampedArrayFunction->SetProtoOrHClass(thread_, uint8ClampedArrFuncInstanceHClass.GetTaggedValue());
2199
2200 constexpr int bytesPerElement = 1;
2201 SetConstant(uint8ClampedArrFuncPrototype, "BYTES_PER_ELEMENT", JSTaggedValue(bytesPerElement));
2202 SetConstant(JSHandle<JSObject>(uint8ClampedArrayFunction), "BYTES_PER_ELEMENT", JSTaggedValue(bytesPerElement));
2203 env->SetUint8ClampedArrayFunction(thread_, uint8ClampedArrayFunction);
2204 }
2205
InitializeInt16Array(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncClass) const2206 void Builtins::InitializeInt16Array(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncClass) const
2207 {
2208 [[maybe_unused]] EcmaHandleScope scope(thread_);
2209 // Int16Array.prototype
2210 JSHandle<JSObject> int16ArrFuncPrototype = factory_->NewJSObjectWithInit(objFuncClass);
2211 JSHandle<JSTaggedValue> int16ArrFuncPrototypeValue(int16ArrFuncPrototype);
2212
2213 // Int16Array.prototype_or_hclass
2214 JSHandle<JSHClass> int16ArrFuncInstanceHClass = factory_->NewEcmaHClass(
2215 panda::ecmascript::JSTypedArray::SIZE, JSType::JS_INT16_ARRAY, int16ArrFuncPrototypeValue);
2216
2217 // Int16Array = new Function()
2218 JSHandle<JSFunction> int16ArrayFunction = factory_->NewSpecificTypedArrayFunction(
2219 env, reinterpret_cast<void *>(BuiltinsTypedArray::Int16ArrayConstructor));
2220 InitializeCtor(env, int16ArrFuncPrototype, int16ArrayFunction, "Int16Array", FunctionLength::THREE);
2221
2222 int16ArrayFunction->SetProtoOrHClass(thread_, int16ArrFuncInstanceHClass.GetTaggedValue());
2223
2224 constexpr int bytesPerElement = 2;
2225 SetConstant(int16ArrFuncPrototype, "BYTES_PER_ELEMENT", JSTaggedValue(bytesPerElement));
2226 SetConstant(JSHandle<JSObject>(int16ArrayFunction), "BYTES_PER_ELEMENT", JSTaggedValue(bytesPerElement));
2227 env->SetInt16ArrayFunction(thread_, int16ArrayFunction);
2228 }
2229
InitializeUint16Array(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncClass) const2230 void Builtins::InitializeUint16Array(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncClass) const
2231 {
2232 [[maybe_unused]] EcmaHandleScope scope(thread_);
2233 // Uint16Array.prototype
2234 JSHandle<JSObject> uint16ArrFuncPrototype = factory_->NewJSObjectWithInit(objFuncClass);
2235 JSHandle<JSTaggedValue> uint16ArrFuncPrototypeValue(uint16ArrFuncPrototype);
2236
2237 // Uint16Array.prototype_or_hclass
2238 JSHandle<JSHClass> uint16ArrFuncInstanceHClass = factory_->NewEcmaHClass(
2239 panda::ecmascript::JSTypedArray::SIZE, JSType::JS_UINT16_ARRAY, uint16ArrFuncPrototypeValue);
2240
2241 // Uint16Array = new Function()
2242 JSHandle<JSFunction> uint16ArrayFunction = factory_->NewSpecificTypedArrayFunction(
2243 env, reinterpret_cast<void *>(BuiltinsTypedArray::Uint16ArrayConstructor));
2244 InitializeCtor(env, uint16ArrFuncPrototype, uint16ArrayFunction, "Uint16Array", FunctionLength::THREE);
2245
2246 uint16ArrayFunction->SetProtoOrHClass(thread_, uint16ArrFuncInstanceHClass.GetTaggedValue());
2247
2248 constexpr int bytesPerElement = 2;
2249 SetConstant(uint16ArrFuncPrototype, "BYTES_PER_ELEMENT", JSTaggedValue(bytesPerElement));
2250 SetConstant(JSHandle<JSObject>(uint16ArrayFunction), "BYTES_PER_ELEMENT", JSTaggedValue(bytesPerElement));
2251 env->SetUint16ArrayFunction(thread_, uint16ArrayFunction);
2252 }
2253
InitializeInt32Array(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncClass) const2254 void Builtins::InitializeInt32Array(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncClass) const
2255 {
2256 [[maybe_unused]] EcmaHandleScope scope(thread_);
2257 // Int32Array.prototype
2258 JSHandle<JSObject> int32ArrFuncPrototype = factory_->NewJSObjectWithInit(objFuncClass);
2259 JSHandle<JSTaggedValue> int32ArrFuncPrototypeValue(int32ArrFuncPrototype);
2260
2261 // Int32Array.prototype_or_hclass
2262 JSHandle<JSHClass> int32ArrFuncInstanceHClass = factory_->NewEcmaHClass(
2263 panda::ecmascript::JSTypedArray::SIZE, JSType::JS_INT32_ARRAY, int32ArrFuncPrototypeValue);
2264
2265 // Int32Array = new Function()
2266 JSHandle<JSFunction> int32ArrayFunction = factory_->NewSpecificTypedArrayFunction(
2267 env, reinterpret_cast<void *>(BuiltinsTypedArray::Int32ArrayConstructor));
2268 InitializeCtor(env, int32ArrFuncPrototype, int32ArrayFunction, "Int32Array", FunctionLength::THREE);
2269
2270 int32ArrayFunction->SetProtoOrHClass(thread_, int32ArrFuncInstanceHClass.GetTaggedValue());
2271
2272 constexpr int bytesPerElement = 4;
2273 SetConstant(int32ArrFuncPrototype, "BYTES_PER_ELEMENT", JSTaggedValue(bytesPerElement));
2274 SetConstant(JSHandle<JSObject>(int32ArrayFunction), "BYTES_PER_ELEMENT", JSTaggedValue(bytesPerElement));
2275 env->SetInt32ArrayFunction(thread_, int32ArrayFunction);
2276 }
2277
InitializeUint32Array(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncClass) const2278 void Builtins::InitializeUint32Array(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncClass) const
2279 {
2280 [[maybe_unused]] EcmaHandleScope scope(thread_);
2281 // Uint32Array.prototype
2282 JSHandle<JSObject> uint32ArrFuncPrototype = factory_->NewJSObjectWithInit(objFuncClass);
2283 JSHandle<JSTaggedValue> uint32ArrFuncPrototypeValue(uint32ArrFuncPrototype);
2284
2285 // Uint32Array.prototype_or_hclass
2286 JSHandle<JSHClass> uint32ArrFuncInstanceHClass = factory_->NewEcmaHClass(
2287 panda::ecmascript::JSTypedArray::SIZE, JSType::JS_UINT32_ARRAY, uint32ArrFuncPrototypeValue);
2288
2289 // Uint32Array = new Function()
2290 JSHandle<JSFunction> uint32ArrayFunction = factory_->NewSpecificTypedArrayFunction(
2291 env, reinterpret_cast<void *>(BuiltinsTypedArray::Uint32ArrayConstructor));
2292 InitializeCtor(env, uint32ArrFuncPrototype, uint32ArrayFunction, "Uint32Array", FunctionLength::THREE);
2293
2294 uint32ArrayFunction->SetProtoOrHClass(thread_, uint32ArrFuncInstanceHClass.GetTaggedValue());
2295
2296 constexpr int bytesPerElement = 4;
2297 SetConstant(uint32ArrFuncPrototype, "BYTES_PER_ELEMENT", JSTaggedValue(bytesPerElement));
2298 SetConstant(JSHandle<JSObject>(uint32ArrayFunction), "BYTES_PER_ELEMENT", JSTaggedValue(bytesPerElement));
2299 env->SetUint32ArrayFunction(thread_, uint32ArrayFunction);
2300 }
2301
InitializeFloat32Array(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncClass) const2302 void Builtins::InitializeFloat32Array(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncClass) const
2303 {
2304 [[maybe_unused]] EcmaHandleScope scope(thread_);
2305 // Float32Array.prototype
2306 JSHandle<JSObject> float32ArrFuncPrototype = factory_->NewJSObjectWithInit(objFuncClass);
2307 JSHandle<JSTaggedValue> float32ArrFuncPrototypeValue(float32ArrFuncPrototype);
2308
2309 // Float32Array.prototype_or_hclass
2310 JSHandle<JSHClass> float32ArrFuncInstanceHClass = factory_->NewEcmaHClass(
2311 panda::ecmascript::JSTypedArray::SIZE, JSType::JS_FLOAT32_ARRAY, float32ArrFuncPrototypeValue);
2312
2313 // Float32Array = new Function()
2314 JSHandle<JSFunction> float32ArrayFunction = factory_->NewSpecificTypedArrayFunction(
2315 env, reinterpret_cast<void *>(BuiltinsTypedArray::Float32ArrayConstructor));
2316 InitializeCtor(env, float32ArrFuncPrototype, float32ArrayFunction, "Float32Array", FunctionLength::THREE);
2317
2318 float32ArrayFunction->SetProtoOrHClass(thread_, float32ArrFuncInstanceHClass.GetTaggedValue());
2319
2320 constexpr int bytesPerElement = 4;
2321 SetConstant(float32ArrFuncPrototype, "BYTES_PER_ELEMENT", JSTaggedValue(bytesPerElement));
2322 SetConstant(JSHandle<JSObject>(float32ArrayFunction), "BYTES_PER_ELEMENT", JSTaggedValue(bytesPerElement));
2323 env->SetFloat32ArrayFunction(thread_, float32ArrayFunction);
2324 }
2325
InitializeFloat64Array(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncClass) const2326 void Builtins::InitializeFloat64Array(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncClass) const
2327 {
2328 [[maybe_unused]] EcmaHandleScope scope(thread_);
2329 // Float64Array.prototype
2330 JSHandle<JSObject> float64ArrFuncPrototype = factory_->NewJSObjectWithInit(objFuncClass);
2331 JSHandle<JSTaggedValue> float64ArrFuncPrototypeValue(float64ArrFuncPrototype);
2332
2333 // Float64Array.prototype_or_hclass
2334 JSHandle<JSHClass> float64ArrFuncInstanceHClass = factory_->NewEcmaHClass(
2335 panda::ecmascript::JSTypedArray::SIZE, JSType::JS_FLOAT64_ARRAY, float64ArrFuncPrototypeValue);
2336
2337 // Float64Array = new Function()
2338 JSHandle<JSFunction> float64ArrayFunction = factory_->NewSpecificTypedArrayFunction(
2339 env, reinterpret_cast<void *>(BuiltinsTypedArray::Float64ArrayConstructor));
2340 InitializeCtor(env, float64ArrFuncPrototype, float64ArrayFunction, "Float64Array", FunctionLength::THREE);
2341
2342 float64ArrayFunction->SetProtoOrHClass(thread_, float64ArrFuncInstanceHClass.GetTaggedValue());
2343
2344 constexpr int bytesPerElement = 8;
2345 SetConstant(float64ArrFuncPrototype, "BYTES_PER_ELEMENT", JSTaggedValue(bytesPerElement));
2346 SetConstant(JSHandle<JSObject>(float64ArrayFunction), "BYTES_PER_ELEMENT", JSTaggedValue(bytesPerElement));
2347 env->SetFloat64ArrayFunction(thread_, float64ArrayFunction);
2348 }
2349
InitializeBigInt64Array(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncClass) const2350 void Builtins::InitializeBigInt64Array(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncClass) const
2351 {
2352 [[maybe_unused]] EcmaHandleScope scope(thread_);
2353 // BigInt64Array.prototype
2354 JSHandle<JSObject> bigInt64ArrFuncPrototype = factory_->NewJSObjectWithInit(objFuncClass);
2355 JSHandle<JSTaggedValue> bigInt64ArrFuncPrototypeValue(bigInt64ArrFuncPrototype);
2356
2357 // BigInt64Array.prototype_or_hclass
2358 JSHandle<JSHClass> bigInt64ArrFuncInstanceHClass = factory_->NewEcmaHClass(
2359 panda::ecmascript::JSTypedArray::SIZE, JSType::JS_BIGINT64_ARRAY, bigInt64ArrFuncPrototypeValue);
2360
2361 // BigInt64Array = new Function()
2362 JSHandle<JSFunction> bigInt64ArrayFunction = factory_->NewSpecificTypedArrayFunction(
2363 env, reinterpret_cast<void *>(BuiltinsTypedArray::BigInt64ArrayConstructor));
2364 InitializeCtor(env, bigInt64ArrFuncPrototype, bigInt64ArrayFunction, "BigInt64Array", FunctionLength::THREE);
2365
2366 bigInt64ArrayFunction->SetProtoOrHClass(thread_, bigInt64ArrFuncInstanceHClass.GetTaggedValue());
2367
2368 constexpr int bytesPerElement = 8;
2369 SetConstant(bigInt64ArrFuncPrototype, "BYTES_PER_ELEMENT", JSTaggedValue(bytesPerElement));
2370 SetConstant(JSHandle<JSObject>(bigInt64ArrayFunction), "BYTES_PER_ELEMENT", JSTaggedValue(bytesPerElement));
2371 env->SetBigInt64ArrayFunction(thread_, bigInt64ArrayFunction);
2372 }
2373
InitializeBigUint64Array(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncClass) const2374 void Builtins::InitializeBigUint64Array(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncClass) const
2375 {
2376 [[maybe_unused]] EcmaHandleScope scope(thread_);
2377 // BigUint64Array.prototype
2378 JSHandle<JSObject> bigUint64ArrFuncPrototype = factory_->NewJSObjectWithInit(objFuncClass);
2379 JSHandle<JSTaggedValue> bigUint64ArrFuncPrototypeValue(bigUint64ArrFuncPrototype);
2380
2381 // BigUint64Array.prototype_or_hclass
2382 JSHandle<JSHClass> bigUint64ArrFuncInstanceHClass = factory_->NewEcmaHClass(
2383 panda::ecmascript::JSTypedArray::SIZE, JSType::JS_BIGUINT64_ARRAY, bigUint64ArrFuncPrototypeValue);
2384
2385 // BigUint64Array = new Function()
2386 JSHandle<JSFunction> bigUint64ArrayFunction = factory_->NewSpecificTypedArrayFunction(
2387 env, reinterpret_cast<void *>(BuiltinsTypedArray::BigUint64ArrayConstructor));
2388 InitializeCtor(env, bigUint64ArrFuncPrototype, bigUint64ArrayFunction, "BigUint64Array", FunctionLength::THREE);
2389
2390 bigUint64ArrayFunction->SetProtoOrHClass(thread_, bigUint64ArrFuncInstanceHClass.GetTaggedValue());
2391
2392 constexpr int bytesPerElement = 8;
2393 SetConstant(bigUint64ArrFuncPrototype, "BYTES_PER_ELEMENT", JSTaggedValue(bytesPerElement));
2394 SetConstant(JSHandle<JSObject>(bigUint64ArrayFunction), "BYTES_PER_ELEMENT", JSTaggedValue(bytesPerElement));
2395 env->SetBigUint64ArrayFunction(thread_, bigUint64ArrayFunction);
2396 }
2397
InitializeArrayBuffer(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncClass) const2398 void Builtins::InitializeArrayBuffer(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncClass) const
2399 {
2400 [[maybe_unused]] EcmaHandleScope scope(thread_);
2401 // ArrayBuffer.prototype
2402 JSHandle<JSObject> arrayBufferFuncPrototype = factory_->NewJSObjectWithInit(objFuncClass);
2403 JSHandle<JSTaggedValue> arrayBufferFuncPrototypeValue(arrayBufferFuncPrototype);
2404
2405 // ArrayBuffer.prototype_or_hclass
2406 JSHandle<JSHClass> arrayBufferFuncInstanceHClass =
2407 factory_->NewEcmaHClass(JSArrayBuffer::SIZE, JSType::JS_ARRAY_BUFFER, arrayBufferFuncPrototypeValue);
2408
2409 // ArrayBuffer = new Function()
2410 JSHandle<JSObject> arrayBufferFunction(NewBuiltinConstructor(
2411 env, arrayBufferFuncPrototype, ArrayBuffer::ArrayBufferConstructor, "ArrayBuffer", FunctionLength::ONE));
2412
2413 JSHandle<JSFunction>(arrayBufferFunction)
2414 ->SetFunctionPrototype(thread_, arrayBufferFuncInstanceHClass.GetTaggedValue());
2415
2416 // ArrayBuffer prototype method
2417 SetFunction(env, arrayBufferFuncPrototype, "slice", ArrayBuffer::Slice, FunctionLength::TWO);
2418
2419 // ArrayBuffer method
2420 SetFunction(env, arrayBufferFunction, "isView", ArrayBuffer::IsView, FunctionLength::ONE);
2421
2422 // 24.1.3.3 get ArrayBuffer[@@species]
2423 JSHandle<JSTaggedValue> speciesSymbol = env->GetSpeciesSymbol();
2424 JSHandle<JSTaggedValue> speciesGetter =
2425 CreateGetter(env, ArrayBuffer::Species, "[Symbol.species]", FunctionLength::ZERO);
2426 SetGetter(JSHandle<JSObject>(arrayBufferFunction), speciesSymbol, speciesGetter);
2427
2428 // 24.1.4.1 get ArrayBuffer.prototype.byteLength
2429 JSHandle<JSTaggedValue> lengthGetter =
2430 CreateGetter(env, ArrayBuffer::GetByteLength, "byteLength", FunctionLength::ZERO);
2431 JSHandle<JSTaggedValue> lengthKey(factory_->NewFromASCII("byteLength"));
2432 SetGetter(arrayBufferFuncPrototype, lengthKey, lengthGetter);
2433
2434 // 24.1.4.4 ArrayBuffer.prototype[@@toStringTag]
2435 SetStringTagSymbol(env, arrayBufferFuncPrototype, "ArrayBuffer");
2436
2437 env->SetArrayBufferFunction(thread_, arrayBufferFunction.GetTaggedValue());
2438 }
2439
InitializeReflect(const JSHandle<GlobalEnv> & env,const JSHandle<JSTaggedValue> & objFuncPrototypeVal) const2440 void Builtins::InitializeReflect(const JSHandle<GlobalEnv> &env,
2441 const JSHandle<JSTaggedValue> &objFuncPrototypeVal) const
2442 {
2443 [[maybe_unused]] EcmaHandleScope scope(thread_);
2444 JSHandle<JSHClass> reflectHClass =
2445 factory_->NewEcmaHClass(JSObject::SIZE, JSType::JS_OBJECT, objFuncPrototypeVal);
2446 JSHandle<JSObject> reflectObject = factory_->NewJSObjectWithInit(reflectHClass);
2447
2448 SetFunction(env, reflectObject, "apply", Reflect::ReflectApply, FunctionLength::THREE);
2449 SetFunction(env, reflectObject, "construct", Reflect::ReflectConstruct, FunctionLength::TWO);
2450 SetFunction(env, reflectObject, "defineProperty", Reflect::ReflectDefineProperty, FunctionLength::THREE);
2451 SetFunction(env, reflectObject, "deleteProperty", Reflect::ReflectDeleteProperty, FunctionLength::TWO);
2452 SetFunction(env, reflectObject, "get", Reflect::ReflectGet, FunctionLength::TWO);
2453 SetFunction(env, reflectObject, "getOwnPropertyDescriptor", Reflect::ReflectGetOwnPropertyDescriptor,
2454 FunctionLength::TWO);
2455 SetFunction(env, reflectObject, "getPrototypeOf", Reflect::ReflectGetPrototypeOf, FunctionLength::ONE);
2456 SetFunction(env, reflectObject, "has", Reflect::ReflectHas, FunctionLength::TWO);
2457 SetFunction(env, reflectObject, "isExtensible", Reflect::ReflectIsExtensible, FunctionLength::ONE);
2458 SetFunction(env, reflectObject, "ownKeys", Reflect::ReflectOwnKeys, FunctionLength::ONE);
2459 SetFunction(env, reflectObject, "preventExtensions", Reflect::ReflectPreventExtensions, FunctionLength::ONE);
2460 SetFunction(env, reflectObject, "set", Reflect::ReflectSet, FunctionLength::THREE);
2461 SetFunction(env, reflectObject, "setPrototypeOf", Reflect::ReflectSetPrototypeOf, FunctionLength::TWO);
2462
2463 JSHandle<JSTaggedValue> reflectString(factory_->NewFromASCII("Reflect"));
2464 JSHandle<JSObject> globalObject(thread_, env->GetGlobalObject());
2465 PropertyDescriptor reflectDesc(thread_, JSHandle<JSTaggedValue>::Cast(reflectObject), true, false, true);
2466 JSObject::DefineOwnProperty(thread_, globalObject, reflectString, reflectDesc);
2467
2468 // @@ToStringTag
2469 SetStringTagSymbol(env, reflectObject, "Reflect");
2470
2471 env->SetReflectFunction(thread_, reflectObject.GetTaggedValue());
2472 }
2473
InitializeSharedArrayBuffer(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncClass) const2474 void Builtins::InitializeSharedArrayBuffer(const JSHandle<GlobalEnv> &env,
2475 const JSHandle<JSHClass> &objFuncClass) const
2476 {
2477 [[maybe_unused]] EcmaHandleScope scope(thread_);
2478 // SharedArrayBuffer.prototype
2479 JSHandle<JSObject> sharedArrayBufferFuncPrototype = factory_->NewJSObjectWithInit(objFuncClass);
2480 JSHandle<JSTaggedValue> sharedArrayBufferFuncPrototypeValue(sharedArrayBufferFuncPrototype);
2481
2482 // SharedArrayBuffer.prototype_or_hclass
2483 JSHandle<JSHClass> sharedArrayBufferFuncInstanceHClass =
2484 factory_->NewEcmaHClass(
2485 JSArrayBuffer::SIZE, JSType::JS_SHARED_ARRAY_BUFFER, sharedArrayBufferFuncPrototypeValue);
2486
2487 // SharedArrayBuffer = new Function()
2488 JSHandle<JSObject> SharedArrayBufferFunction(NewBuiltinConstructor(env, sharedArrayBufferFuncPrototype,
2489 SharedArrayBuffer::SharedArrayBufferConstructor, "SharedArrayBuffer", FunctionLength::ONE));
2490
2491 JSHandle<JSFunction>(SharedArrayBufferFunction)
2492 ->SetFunctionPrototype(thread_, sharedArrayBufferFuncInstanceHClass.GetTaggedValue());
2493
2494 // SharedArrayBuffer prototype method
2495 SetFunction(env, sharedArrayBufferFuncPrototype, "slice", SharedArrayBuffer::Slice, FunctionLength::TWO);
2496
2497 // SharedArrayBuffer method
2498 SetFunction(env, SharedArrayBufferFunction,
2499 "IsSharedArrayBuffer", SharedArrayBuffer::IsSharedArrayBuffer, FunctionLength::ONE);
2500
2501 // 25.2.3.2 get SharedArrayBuffer [ @@species ]
2502 JSHandle<JSTaggedValue> speciesSymbol = env->GetSpeciesSymbol();
2503 JSHandle<JSTaggedValue> speciesGetter =
2504 CreateGetter(env, SharedArrayBuffer::Species, "[Symbol.species]", FunctionLength::ZERO);
2505 SetGetter(JSHandle<JSObject>(SharedArrayBufferFunction), speciesSymbol, speciesGetter);
2506
2507 // 25.2.4.1 get SharedArrayBuffer.prototype.byteLength
2508 JSHandle<JSTaggedValue> lengthGetter =
2509 CreateGetter(env, SharedArrayBuffer::GetByteLength, "byteLength", FunctionLength::ZERO);
2510 JSHandle<JSTaggedValue> lengthKey(factory_->NewFromASCII("byteLength"));
2511 SetGetter(sharedArrayBufferFuncPrototype, lengthKey, lengthGetter);
2512
2513 // 25.2.4.4 SharedArrayBuffer.prototype [ @@toStringTag ]
2514 SetStringTagSymbol(env, sharedArrayBufferFuncPrototype, "SharedArrayBuffer");
2515
2516 env->SetSharedArrayBufferFunction(thread_, SharedArrayBufferFunction.GetTaggedValue());
2517 }
2518
InitializePromise(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & promiseFuncClass)2519 void Builtins::InitializePromise(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &promiseFuncClass)
2520 {
2521 [[maybe_unused]] EcmaHandleScope scope(thread_);
2522 // Promise.prototype
2523 JSHandle<JSObject> promiseFuncPrototype = factory_->NewJSObjectWithInit(promiseFuncClass);
2524 JSHandle<JSTaggedValue> promiseFuncPrototypeValue(promiseFuncPrototype);
2525 // Promise.prototype_or_hclass
2526 JSHandle<JSHClass> promiseFuncInstanceHClass =
2527 factory_->NewEcmaHClass(JSPromise::SIZE, JSType::JS_PROMISE, promiseFuncPrototypeValue);
2528 // Promise() = new Function()
2529 JSHandle<JSObject> promiseFunction(
2530 NewBuiltinConstructor(env, promiseFuncPrototype, Promise::PromiseConstructor, "Promise", FunctionLength::ONE));
2531 JSHandle<JSFunction>(promiseFunction)->SetFunctionPrototype(thread_, promiseFuncInstanceHClass.GetTaggedValue());
2532
2533 // Promise method
2534 SetFunction(env, promiseFunction, "all", Promise::All, FunctionLength::ONE);
2535 SetFunction(env, promiseFunction, "race", Promise::Race, FunctionLength::ONE);
2536 SetFunction(env, promiseFunction, "resolve", Promise::Resolve, FunctionLength::ONE);
2537 SetFunction(env, promiseFunction, "reject", Promise::Reject, FunctionLength::ONE);
2538 SetFunction(env, promiseFunction, "any", Promise::Any, FunctionLength::ONE);
2539 SetFunction(env, promiseFunction, "allSettled", Promise::AllSettled, FunctionLength::ONE);
2540
2541 // promise.prototype method
2542 SetFunction(env, promiseFuncPrototype, "catch", Promise::Catch, FunctionLength::ONE);
2543 SetFunction(env, promiseFuncPrototype, "then", Promise::Then, FunctionLength::TWO);
2544 SetFunction(env, promiseFuncPrototype, "finally", Promise::Finally, FunctionLength::ONE);
2545
2546 // Promise.prototype [ @@toStringTag ]
2547 SetStringTagSymbol(env, promiseFuncPrototype, "Promise");
2548
2549 // Set Promise [@@species]
2550 JSHandle<JSTaggedValue> speciesSymbol(env->GetSpeciesSymbol());
2551 JSHandle<JSTaggedValue> speciesGetter =
2552 CreateGetter(env, Promise::GetSpecies, "[Symbol.species]", FunctionLength::ZERO);
2553 SetGetter(promiseFunction, speciesSymbol, speciesGetter);
2554
2555 env->SetPromiseFunction(thread_, promiseFunction);
2556 InitializeForPromiseFuncClass(env);
2557 }
2558
2559
InitializeForPromiseFuncClass(const JSHandle<GlobalEnv> & env)2560 void Builtins::InitializeForPromiseFuncClass(const JSHandle<GlobalEnv> &env)
2561 {
2562 vm_ = thread_->GetEcmaVM();
2563 factory_ = vm_->GetFactory();
2564
2565 JSHandle<JSHClass> promiseReactionFuncClass = factory_->NewEcmaHClass(
2566 JSPromiseReactionsFunction::SIZE, JSType::JS_PROMISE_REACTIONS_FUNCTION, env->GetFunctionPrototype());
2567 promiseReactionFuncClass->SetCallable(true);
2568 promiseReactionFuncClass->SetExtensible(true);
2569 env->SetPromiseReactionFunctionClass(thread_, promiseReactionFuncClass);
2570
2571 JSHandle<JSHClass> promiseExecutorFuncClass = factory_->NewEcmaHClass(
2572 JSPromiseExecutorFunction::SIZE, JSType::JS_PROMISE_EXECUTOR_FUNCTION, env->GetFunctionPrototype());
2573 promiseExecutorFuncClass->SetCallable(true);
2574 promiseExecutorFuncClass->SetExtensible(true);
2575 env->SetPromiseExecutorFunctionClass(thread_, promiseExecutorFuncClass);
2576
2577 JSHandle<JSHClass> promiseAllResolveElementFunctionClass =
2578 factory_->NewEcmaHClass(JSPromiseAllResolveElementFunction::SIZE,
2579 JSType::JS_PROMISE_ALL_RESOLVE_ELEMENT_FUNCTION, env->GetFunctionPrototype());
2580 promiseAllResolveElementFunctionClass->SetCallable(true);
2581 promiseAllResolveElementFunctionClass->SetExtensible(true);
2582 env->SetPromiseAllResolveElementFunctionClass(thread_, promiseAllResolveElementFunctionClass);
2583
2584 JSHandle<JSHClass> promiseAnyRejectElementFunctionClass =
2585 factory_->NewEcmaHClass(JSPromiseAnyRejectElementFunction::SIZE,
2586 JSType::JS_PROMISE_ANY_REJECT_ELEMENT_FUNCTION, env->GetFunctionPrototype());
2587 promiseAnyRejectElementFunctionClass->SetCallable(true);
2588 promiseAnyRejectElementFunctionClass->SetExtensible(true);
2589 env->SetPromiseAnyRejectElementFunctionClass(thread_, promiseAnyRejectElementFunctionClass);
2590
2591 JSHandle<JSHClass> promiseAllSettledElementFunctionClass =
2592 factory_->NewEcmaHClass(JSPromiseAllSettledElementFunction::SIZE,
2593 JSType::JS_PROMISE_ALL_SETTLED_ELEMENT_FUNCTION, env->GetFunctionPrototype());
2594 promiseAllSettledElementFunctionClass->SetCallable(true);
2595 promiseAllSettledElementFunctionClass->SetExtensible(true);
2596 env->SetPromiseAllSettledElementFunctionClass(thread_, promiseAllSettledElementFunctionClass);
2597
2598 JSHandle<JSHClass> promiseFinallyFunctionClass =
2599 factory_->NewEcmaHClass(JSPromiseFinallyFunction::SIZE,
2600 JSType::JS_PROMISE_FINALLY_FUNCTION, env->GetFunctionPrototype());
2601 promiseFinallyFunctionClass->SetCallable(true);
2602 promiseFinallyFunctionClass->SetExtensible(true);
2603 env->SetPromiseFinallyFunctionClass(thread_, promiseFinallyFunctionClass);
2604
2605 JSHandle<JSHClass> promiseValueThunkOrThrowerFunctionClass =
2606 factory_->NewEcmaHClass(JSPromiseValueThunkOrThrowerFunction::SIZE,
2607 JSType::JS_PROMISE_VALUE_THUNK_OR_THROWER_FUNCTION, env->GetFunctionPrototype());
2608 promiseValueThunkOrThrowerFunctionClass->SetCallable(true);
2609 promiseValueThunkOrThrowerFunctionClass->SetExtensible(true);
2610 env->SetPromiseValueThunkOrThrowerFunctionClass(thread_, promiseValueThunkOrThrowerFunctionClass);
2611 }
2612
InitializePromiseJob(const JSHandle<GlobalEnv> & env)2613 void Builtins::InitializePromiseJob(const JSHandle<GlobalEnv> &env)
2614 {
2615 JSHandle<JSTaggedValue> keyString(thread_->GlobalConstants()->GetHandledEmptyString());
2616 auto func = NewFunction(env, keyString, BuiltinsPromiseJob::PromiseReactionJob, FunctionLength::TWO);
2617 env->SetPromiseReactionJob(thread_, func);
2618 func = NewFunction(env, keyString, BuiltinsPromiseJob::PromiseResolveThenableJob, FunctionLength::THREE);
2619 env->SetPromiseResolveThenableJob(thread_, func);
2620 func = NewFunction(env, keyString, BuiltinsPromiseJob::DynamicImportJob, FunctionLength::FOUR);
2621 env->SetDynamicImportJob(thread_, func);
2622 }
2623
InitializeDataView(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncClass) const2624 void Builtins::InitializeDataView(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncClass) const
2625 {
2626 [[maybe_unused]] EcmaHandleScope scope(thread_);
2627 // ArrayBuffer.prototype
2628 JSHandle<JSObject> dataViewFuncPrototype = factory_->NewJSObjectWithInit(objFuncClass);
2629 JSHandle<JSTaggedValue> dataViewFuncPrototypeValue(dataViewFuncPrototype);
2630
2631 // ArrayBuffer.prototype_or_hclass
2632 JSHandle<JSHClass> dataViewFuncInstanceHClass =
2633 factory_->NewEcmaHClass(JSDataView::SIZE, JSType::JS_DATA_VIEW, dataViewFuncPrototypeValue);
2634
2635 // ArrayBuffer = new Function()
2636 JSHandle<JSObject> dataViewFunction(NewBuiltinConstructor(env, dataViewFuncPrototype, DataView::DataViewConstructor,
2637 "DataView", FunctionLength::ONE));
2638
2639 JSHandle<JSFunction>(dataViewFunction)->SetProtoOrHClass(thread_, dataViewFuncInstanceHClass.GetTaggedValue());
2640 // DataView.prototype method
2641 SetFunction(env, dataViewFuncPrototype, "getFloat32", DataView::GetFloat32, FunctionLength::ONE);
2642 SetFunction(env, dataViewFuncPrototype, "getFloat64", DataView::GetFloat64, FunctionLength::ONE);
2643 SetFunction(env, dataViewFuncPrototype, "getInt8", DataView::GetInt8, FunctionLength::ONE);
2644 SetFunction(env, dataViewFuncPrototype, "getInt16", DataView::GetInt16, FunctionLength::ONE);
2645 SetFunction(env, dataViewFuncPrototype, "getInt32", DataView::GetInt32, FunctionLength::ONE);
2646 SetFunction(env, dataViewFuncPrototype, "getUint8", DataView::GetUint8, FunctionLength::ONE);
2647 SetFunction(env, dataViewFuncPrototype, "getUint16", DataView::GetUint16, FunctionLength::ONE);
2648 SetFunction(env, dataViewFuncPrototype, "getUint32", DataView::GetUint32, FunctionLength::ONE);
2649 SetFunction(env, dataViewFuncPrototype, "getBigInt64", DataView::GetBigInt64, FunctionLength::ONE);
2650 SetFunction(env, dataViewFuncPrototype, "getBigUint64", DataView::GetBigUint64, FunctionLength::ONE);
2651 SetFunction(env, dataViewFuncPrototype, "setFloat32", DataView::SetFloat32, FunctionLength::TWO);
2652 SetFunction(env, dataViewFuncPrototype, "setFloat64", DataView::SetFloat64, FunctionLength::TWO);
2653 SetFunction(env, dataViewFuncPrototype, "setInt8", DataView::SetInt8, FunctionLength::TWO);
2654 SetFunction(env, dataViewFuncPrototype, "setInt16", DataView::SetInt16, FunctionLength::TWO);
2655 SetFunction(env, dataViewFuncPrototype, "setInt32", DataView::SetInt32, FunctionLength::TWO);
2656 SetFunction(env, dataViewFuncPrototype, "setUint8", DataView::SetUint8, FunctionLength::TWO);
2657 SetFunction(env, dataViewFuncPrototype, "setUint16", DataView::SetUint16, FunctionLength::TWO);
2658 SetFunction(env, dataViewFuncPrototype, "setUint32", DataView::SetUint32, FunctionLength::TWO);
2659 SetFunction(env, dataViewFuncPrototype, "setBigInt64", DataView::SetBigInt64, FunctionLength::TWO);
2660 SetFunction(env, dataViewFuncPrototype, "setBigUint64", DataView::SetBigUint64, FunctionLength::TWO);
2661
2662 // 24.2.4.1 get DataView.prototype.buffer
2663 JSHandle<JSTaggedValue> bufferGetter = CreateGetter(env, DataView::GetBuffer, "buffer", FunctionLength::ZERO);
2664 JSHandle<JSTaggedValue> bufferKey(factory_->NewFromASCII("buffer"));
2665 SetGetter(dataViewFuncPrototype, bufferKey, bufferGetter);
2666
2667 // 24.2.4.2 get DataView.prototype.byteLength
2668 JSHandle<JSTaggedValue> lengthGetter =
2669 CreateGetter(env, DataView::GetByteLength, "byteLength", FunctionLength::ZERO);
2670 JSHandle<JSTaggedValue> lengthKey(factory_->NewFromASCII("byteLength"));
2671 SetGetter(dataViewFuncPrototype, lengthKey, lengthGetter);
2672
2673 // 24.2.4.3 get DataView.prototype.byteOffset
2674 JSHandle<JSTaggedValue> offsetGetter = CreateGetter(env, DataView::GetOffset, "byteOffset", FunctionLength::ZERO);
2675 JSHandle<JSTaggedValue> offsetKey(factory_->NewFromASCII("byteOffset"));
2676 SetGetter(dataViewFuncPrototype, offsetKey, offsetGetter);
2677
2678 // 24.2.4.21 DataView.prototype[ @@toStringTag ]
2679 SetStringTagSymbol(env, dataViewFuncPrototype, "DataView");
2680 env->SetDataViewFunction(thread_, dataViewFunction.GetTaggedValue());
2681 }
2682
NewBuiltinConstructor(const JSHandle<GlobalEnv> & env,const JSHandle<JSObject> & prototype,EcmaEntrypoint ctorFunc,const char * name,int length,kungfu::BuiltinsStubCSigns::ID builtinId) const2683 JSHandle<JSFunction> Builtins::NewBuiltinConstructor(const JSHandle<GlobalEnv> &env,
2684 const JSHandle<JSObject> &prototype, EcmaEntrypoint ctorFunc,
2685 const char *name, int length,
2686 kungfu::BuiltinsStubCSigns::ID builtinId) const
2687 {
2688 JSHandle<JSFunction> ctor =
2689 factory_->NewJSFunction(env, reinterpret_cast<void *>(ctorFunc), FunctionKind::BUILTIN_CONSTRUCTOR, builtinId);
2690 InitializeCtor(env, prototype, ctor, name, length);
2691 return ctor;
2692 }
2693
NewBuiltinCjsCtor(const JSHandle<GlobalEnv> & env,const JSHandle<JSObject> & prototype,EcmaEntrypoint ctorFunc,const char * name,int length) const2694 JSHandle<JSFunction> Builtins::NewBuiltinCjsCtor(const JSHandle<GlobalEnv> &env,
2695 const JSHandle<JSObject> &prototype, EcmaEntrypoint ctorFunc,
2696 const char *name, int length) const
2697 {
2698 JSHandle<JSFunction> ctor =
2699 factory_->NewJSFunction(env, reinterpret_cast<void *>(ctorFunc), FunctionKind::BUILTIN_CONSTRUCTOR);
2700
2701 const GlobalEnvConstants *globalConst = thread_->GlobalConstants();
2702 JSFunction::SetFunctionLength(thread_, ctor, JSTaggedValue(length));
2703 JSHandle<JSTaggedValue> nameString(factory_->NewFromUtf8(name));
2704 JSFunction::SetFunctionName(thread_, JSHandle<JSFunctionBase>(ctor), nameString,
2705 JSHandle<JSTaggedValue>(thread_, JSTaggedValue::Undefined()));
2706 JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString();
2707 PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>::Cast(ctor), true, false, true);
2708 JSObject::DefineOwnProperty(thread_, prototype, constructorKey, descriptor);
2709
2710 return ctor;
2711 }
2712
NewFunction(const JSHandle<GlobalEnv> & env,const JSHandle<JSTaggedValue> & key,EcmaEntrypoint func,int length,kungfu::BuiltinsStubCSigns::ID builtinId) const2713 JSHandle<JSFunction> Builtins::NewFunction(const JSHandle<GlobalEnv> &env, const JSHandle<JSTaggedValue> &key,
2714 EcmaEntrypoint func, int length,
2715 kungfu::BuiltinsStubCSigns::ID builtinId) const
2716 {
2717 JSHandle<JSFunction> function = factory_->NewJSFunction(env, reinterpret_cast<void *>(func),
2718 FunctionKind::NORMAL_FUNCTION, builtinId);
2719 JSFunction::SetFunctionLength(thread_, function, JSTaggedValue(length));
2720 JSHandle<JSFunctionBase> baseFunction(function);
2721 JSHandle<JSTaggedValue> handleUndefine(thread_, JSTaggedValue::Undefined());
2722 JSFunction::SetFunctionName(thread_, baseFunction, key, handleUndefine);
2723 return function;
2724 }
2725
SetFunction(const JSHandle<GlobalEnv> & env,const JSHandle<JSObject> & obj,const char * key,EcmaEntrypoint func,int length,kungfu::BuiltinsStubCSigns::ID builtinId) const2726 void Builtins::SetFunction(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &obj, const char *key,
2727 EcmaEntrypoint func, int length, kungfu::BuiltinsStubCSigns::ID builtinId) const
2728 {
2729 JSHandle<JSTaggedValue> keyString(factory_->NewFromUtf8(key));
2730 SetFunction(env, obj, keyString, func, length, builtinId);
2731 }
2732
SetFunction(const JSHandle<GlobalEnv> & env,const JSHandle<JSObject> & obj,const JSHandle<JSTaggedValue> & key,EcmaEntrypoint func,int length,kungfu::BuiltinsStubCSigns::ID builtinId) const2733 void Builtins::SetFunction(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &obj,
2734 const JSHandle<JSTaggedValue> &key, EcmaEntrypoint func, int length,
2735 kungfu::BuiltinsStubCSigns::ID builtinId) const
2736 {
2737 JSHandle<JSFunction> function(NewFunction(env, key, func, length, builtinId));
2738 PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>(function), true, false, true);
2739 JSObject::DefineOwnProperty(thread_, obj, key, descriptor);
2740 }
2741
SetFrozenFunction(const JSHandle<GlobalEnv> & env,const JSHandle<JSObject> & obj,const char * key,EcmaEntrypoint func,int length) const2742 void Builtins::SetFrozenFunction(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &obj, const char *key,
2743 EcmaEntrypoint func, int length) const
2744 {
2745 JSHandle<JSTaggedValue> keyString(factory_->NewFromUtf8(key));
2746 JSHandle<JSFunction> function = NewFunction(env, keyString, func, length);
2747 PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>(function), false, false, false);
2748 JSObject::DefineOwnProperty(thread_, obj, keyString, descriptor);
2749 }
2750
2751 template<int flag>
SetFunctionAtSymbol(const JSHandle<GlobalEnv> & env,const JSHandle<JSObject> & obj,const JSHandle<JSTaggedValue> & symbol,const char * name,EcmaEntrypoint func,int length) const2752 void Builtins::SetFunctionAtSymbol(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &obj,
2753 const JSHandle<JSTaggedValue> &symbol, const char *name, EcmaEntrypoint func,
2754 int length) const
2755 {
2756 JSHandle<JSFunction> function = factory_->NewJSFunction(env, reinterpret_cast<void *>(func));
2757 JSFunction::SetFunctionLength(thread_, function, JSTaggedValue(length));
2758 JSHandle<JSTaggedValue> nameString(factory_->NewFromUtf8(name));
2759 JSHandle<JSFunctionBase> baseFunction(function);
2760 JSHandle<JSTaggedValue> handleUndefine(thread_, JSTaggedValue::Undefined());
2761 JSFunction::SetFunctionName(thread_, baseFunction, nameString, handleUndefine);
2762 // NOLINTNEXTLINE(readability-braces-around-statements, bugprone-suspicious-semicolon)
2763 if constexpr (flag == JSSymbol::SYMBOL_TO_PRIMITIVE_TYPE) {
2764 PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>::Cast(function), false, false, true);
2765 JSObject::DefineOwnProperty(thread_, obj, symbol, descriptor);
2766 return;
2767 } else if constexpr (flag == JSSymbol::SYMBOL_HAS_INSTANCE_TYPE) { // NOLINTE(readability-braces-around-statements)
2768 // ecma 19.2.3.6 Function.prototype[@@hasInstance] has the attributes
2769 // { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.
2770 PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>::Cast(function), false, false, false);
2771 JSObject::DefineOwnProperty(thread_, obj, symbol, descriptor);
2772 return;
2773 }
2774 PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>::Cast(function), true, false, true);
2775 JSObject::DefineOwnProperty(thread_, obj, symbol, descriptor);
2776 }
2777
SetStringTagSymbol(const JSHandle<GlobalEnv> & env,const JSHandle<JSObject> & obj,const char * key) const2778 void Builtins::SetStringTagSymbol(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &obj, const char *key) const
2779 {
2780 JSHandle<JSTaggedValue> tag(factory_->NewFromUtf8(key));
2781 JSHandle<JSTaggedValue> symbol = env->GetToStringTagSymbol();
2782 PropertyDescriptor desc(thread_, tag, false, false, true);
2783 JSObject::DefineOwnProperty(thread_, obj, symbol, desc);
2784 }
2785
CreateGetter(const JSHandle<GlobalEnv> & env,EcmaEntrypoint func,const char * name,int length) const2786 JSHandle<JSTaggedValue> Builtins::CreateGetter(const JSHandle<GlobalEnv> &env, EcmaEntrypoint func, const char *name,
2787 int length) const
2788 {
2789 JSHandle<JSFunction> function = factory_->NewJSFunction(env, reinterpret_cast<void *>(func));
2790 JSFunction::SetFunctionLength(thread_, function, JSTaggedValue(length));
2791 JSHandle<JSTaggedValue> funcName(factory_->NewFromUtf8(name));
2792 JSHandle<JSTaggedValue> prefix = thread_->GlobalConstants()->GetHandledGetString();
2793 JSFunction::SetFunctionName(thread_, JSHandle<JSFunctionBase>(function), funcName, prefix);
2794 return JSHandle<JSTaggedValue>(function);
2795 }
2796
CreateSetter(const JSHandle<GlobalEnv> & env,EcmaEntrypoint func,const char * name,int length)2797 JSHandle<JSTaggedValue> Builtins::CreateSetter(const JSHandle<GlobalEnv> &env, EcmaEntrypoint func, const char *name,
2798 int length)
2799 {
2800 JSHandle<JSFunction> function = factory_->NewJSFunction(env, reinterpret_cast<void *>(func));
2801 JSFunction::SetFunctionLength(thread_, function, JSTaggedValue(length));
2802 JSHandle<JSTaggedValue> funcName(factory_->NewFromUtf8(name));
2803 JSHandle<JSTaggedValue> prefix = thread_->GlobalConstants()->GetHandledSetString();
2804 JSFunction::SetFunctionName(thread_, JSHandle<JSFunctionBase>(function), funcName, prefix);
2805 return JSHandle<JSTaggedValue>(function);
2806 }
2807
SetConstant(const JSHandle<JSObject> & obj,const char * key,JSTaggedValue value) const2808 void Builtins::SetConstant(const JSHandle<JSObject> &obj, const char *key, JSTaggedValue value) const
2809 {
2810 JSHandle<JSTaggedValue> keyString(factory_->NewFromUtf8(key));
2811 PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>(thread_, value), false, false, false);
2812 JSObject::DefineOwnProperty(thread_, obj, keyString, descriptor);
2813 }
2814
SetConstantObject(const JSHandle<JSObject> & obj,const char * key,JSHandle<JSTaggedValue> & value) const2815 void Builtins::SetConstantObject(const JSHandle<JSObject> &obj, const char *key, JSHandle<JSTaggedValue> &value) const
2816 {
2817 JSHandle<JSTaggedValue> keyString(factory_->NewFromUtf8(key));
2818 PropertyDescriptor descriptor(thread_, value, false, false, false);
2819 JSObject::DefineOwnProperty(thread_, obj, keyString, descriptor);
2820 }
2821
SetNonConstantObject(const JSHandle<JSObject> & obj,const char * key,JSHandle<JSTaggedValue> & value) const2822 void Builtins::SetNonConstantObject(const JSHandle<JSObject> &obj, const char *key,
2823 JSHandle<JSTaggedValue> &value) const
2824 {
2825 JSHandle<JSTaggedValue> keyString(factory_->NewFromUtf8(key));
2826 PropertyDescriptor descriptor(thread_, value, true, true, true);
2827 JSObject::DefineOwnProperty(thread_, obj, keyString, descriptor);
2828 }
2829
SetGlobalThis(const JSHandle<JSObject> & obj,const char * key,const JSHandle<JSTaggedValue> & globalValue)2830 void Builtins::SetGlobalThis(const JSHandle<JSObject> &obj, const char *key, const JSHandle<JSTaggedValue> &globalValue)
2831 {
2832 JSHandle<JSTaggedValue> keyString(factory_->NewFromUtf8(key));
2833 PropertyDescriptor descriptor(thread_, globalValue, true, false, true);
2834 JSObject::DefineOwnProperty(thread_, obj, keyString, descriptor);
2835 }
2836
SetAttribute(const JSHandle<JSObject> & obj,const char * key,const char * value) const2837 void Builtins::SetAttribute(const JSHandle<JSObject> &obj, const char *key, const char *value) const
2838 {
2839 JSHandle<JSTaggedValue> keyString(factory_->NewFromUtf8(key));
2840 PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>(factory_->NewFromUtf8(value)), true, false, true);
2841 JSObject::DefineOwnProperty(thread_, obj, keyString, descriptor);
2842 }
2843
SetNoneAttributeProperty(const JSHandle<JSObject> & obj,const char * key,const JSHandle<JSTaggedValue> & value) const2844 void Builtins::SetNoneAttributeProperty(const JSHandle<JSObject> &obj, const char *key,
2845 const JSHandle<JSTaggedValue> &value) const
2846 {
2847 JSHandle<JSTaggedValue> keyString(factory_->NewFromUtf8(key));
2848 PropertyDescriptor des(thread_, value, false, false, false);
2849 JSObject::DefineOwnProperty(thread_, obj, keyString, des);
2850 }
2851
SetFuncToObjAndGlobal(const JSHandle<GlobalEnv> & env,const JSHandle<JSObject> & globalObject,const JSHandle<JSObject> & obj,const char * key,EcmaEntrypoint func,int length)2852 void Builtins::SetFuncToObjAndGlobal(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &globalObject,
2853 const JSHandle<JSObject> &obj, const char *key, EcmaEntrypoint func, int length)
2854 {
2855 JSHandle<JSFunction> function = factory_->NewJSFunction(env, reinterpret_cast<void *>(func));
2856 JSFunction::SetFunctionLength(thread_, function, JSTaggedValue(length));
2857 JSHandle<JSTaggedValue> keyString(factory_->NewFromUtf8(key));
2858 JSHandle<JSFunctionBase> baseFunction(function);
2859 JSHandle<JSTaggedValue> handleUndefine(thread_, JSTaggedValue::Undefined());
2860 JSFunction::SetFunctionName(thread_, baseFunction, keyString, handleUndefine);
2861 PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>::Cast(function), true, false, true);
2862 JSObject::DefineOwnProperty(thread_, obj, keyString, descriptor);
2863 JSObject::DefineOwnProperty(thread_, globalObject, keyString, descriptor);
2864 }
2865
StrictModeForbiddenAccessCallerArguments(const JSHandle<GlobalEnv> & env,const JSHandle<JSObject> & prototype) const2866 void Builtins::StrictModeForbiddenAccessCallerArguments(const JSHandle<GlobalEnv> &env,
2867 const JSHandle<JSObject> &prototype) const
2868 {
2869 JSHandle<JSFunction> function =
2870 factory_->NewJSFunction(env, reinterpret_cast<void *>(JSFunction::AccessCallerArgumentsThrowTypeError));
2871
2872 JSHandle<JSTaggedValue> caller(factory_->NewFromASCII("caller"));
2873 SetAccessor(prototype, caller, JSHandle<JSTaggedValue>::Cast(function), JSHandle<JSTaggedValue>::Cast(function));
2874
2875 JSHandle<JSTaggedValue> arguments(factory_->NewFromASCII("arguments"));
2876 SetAccessor(prototype, arguments, JSHandle<JSTaggedValue>::Cast(function), JSHandle<JSTaggedValue>::Cast(function));
2877 }
2878
InitializeGeneratorFunction(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncClass) const2879 void Builtins::InitializeGeneratorFunction(const JSHandle<GlobalEnv> &env,
2880 const JSHandle<JSHClass> &objFuncClass) const
2881 {
2882 [[maybe_unused]] EcmaHandleScope scope(thread_);
2883 const GlobalEnvConstants *globalConst = thread_->GlobalConstants();
2884 JSHandle<JSObject> generatorFuncPrototype = factory_->NewJSObjectWithInit(objFuncClass);
2885 JSHandle<JSTaggedValue> generatorFuncPrototypeValue(generatorFuncPrototype);
2886
2887 // 26.3.3.1 GeneratorFunction.prototype.constructor
2888 // GeneratorFunction.prototype_or_hclass
2889 JSHandle<JSHClass> generatorFuncInstanceHClass =
2890 factory_->NewEcmaHClass(JSFunction::SIZE, JSType::JS_GENERATOR_FUNCTION, generatorFuncPrototypeValue);
2891 generatorFuncInstanceHClass->SetCallable(true);
2892 generatorFuncInstanceHClass->SetExtensible(true);
2893 // GeneratorFunction = new GeneratorFunction()
2894 JSHandle<JSFunction> generatorFunction =
2895 NewBuiltinConstructor(env, generatorFuncPrototype, GeneratorObject::GeneratorFunctionConstructor,
2896 "GeneratorFunction", FunctionLength::ONE);
2897 JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString();
2898 PropertyDescriptor generatorDesc(thread_, JSHandle<JSTaggedValue>::Cast(generatorFunction), false, false, true);
2899 JSObject::DefineOwnProperty(thread_, generatorFuncPrototype, constructorKey, generatorDesc);
2900 generatorFunction->SetProtoOrHClass(thread_, generatorFuncInstanceHClass.GetTaggedValue());
2901 env->SetGeneratorFunctionFunction(thread_, generatorFunction);
2902
2903 // 26.3.3.2 GeneratorFunction.prototype.prototype -> Generator prototype object.
2904 PropertyDescriptor descriptor(thread_, env->GetGeneratorPrototype(), false, false, true);
2905 JSObject::DefineOwnProperty(thread_, generatorFuncPrototype, globalConst->GetHandledPrototypeString(), descriptor);
2906
2907 // 26.3.3.3 GeneratorFunction.prototype[@@toStringTag]
2908 SetStringTagSymbol(env, generatorFuncPrototype, "GeneratorFunction");
2909
2910 // GeneratorFunction prototype __proto__ -> Function.
2911 JSObject::SetPrototype(thread_, generatorFuncPrototype, env->GetFunctionPrototype());
2912
2913 // 26.5.1.1 Generator.prototype.constructor -> %GeneratorFunction.prototype%.
2914 PropertyDescriptor generatorObjDesc(thread_, generatorFuncPrototypeValue, false, false, true);
2915 JSObject::DefineOwnProperty(thread_, JSHandle<JSObject>(env->GetInitialGenerator()),
2916 globalConst->GetHandledConstructorString(), generatorObjDesc);
2917
2918 // Generator instances prototype -> GeneratorFunction.prototype.prototype
2919 PropertyDescriptor generatorObjProtoDesc(thread_, generatorFuncPrototypeValue, true, false, false);
2920 JSObject::DefineOwnProperty(thread_, JSHandle<JSObject>(env->GetInitialGenerator()),
2921 globalConst->GetHandledPrototypeString(), generatorObjProtoDesc);
2922
2923 env->SetGeneratorFunctionPrototype(thread_, generatorFuncPrototype);
2924 }
2925
InitializeAsyncGeneratorFunction(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncClass) const2926 void Builtins::InitializeAsyncGeneratorFunction(const JSHandle<GlobalEnv> &env,
2927 const JSHandle<JSHClass> &objFuncClass) const
2928 {
2929 [[maybe_unused]] EcmaHandleScope scope(thread_);
2930 const GlobalEnvConstants *globalConst = thread_->GlobalConstants();
2931 JSHandle<JSObject> asyncGeneratorFuncPrototype = factory_->NewJSObjectWithInit(objFuncClass);
2932 JSHandle<JSTaggedValue> asyncGeneratorFuncPrototypeValue(asyncGeneratorFuncPrototype);
2933 // 27.4.3.1 AsyncGeneratorFunction.prototype.constructor
2934 JSHandle<JSHClass> asyncGeneratorFuncInstanceHClass =
2935 factory_->NewEcmaHClass(JSFunction::SIZE, JSType::JS_ASYNC_GENERATOR_FUNCTION,
2936 asyncGeneratorFuncPrototypeValue);
2937 asyncGeneratorFuncInstanceHClass->SetCallable(true);
2938 asyncGeneratorFuncInstanceHClass->SetExtensible(true);
2939 JSHandle<JSFunction> asyncGeneratorFunction =
2940 NewBuiltinConstructor(env, asyncGeneratorFuncPrototype,
2941 AsyncGeneratorObject::AsyncGeneratorFunctionConstructor, "AsyncGeneratorFunction",
2942 FunctionLength::ONE);
2943 JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString();
2944 PropertyDescriptor asyncGeneratorDesc(thread_, JSHandle<JSTaggedValue>::Cast(asyncGeneratorFunction),
2945 false, false, true);
2946 JSObject::DefineOwnProperty(thread_, asyncGeneratorFuncPrototype, constructorKey, asyncGeneratorDesc);
2947 asyncGeneratorFunction->SetProtoOrHClass(thread_, asyncGeneratorFuncInstanceHClass.GetTaggedValue());
2948 env->SetAsyncGeneratorFunctionFunction(thread_, asyncGeneratorFunction);
2949
2950 // 27.4.3.2 AsyncGeneratorFunction.prototype.prototype
2951 PropertyDescriptor descriptor(thread_, env->GetAsyncGeneratorPrototype(), false, false, true);
2952 JSObject::DefineOwnProperty(thread_, asyncGeneratorFuncPrototype, globalConst->GetHandledPrototypeString(),
2953 descriptor);
2954
2955 // 27.4.3.3 AsyncGeneratorFunction.prototype [ @@toStringTag ]
2956 SetStringTagSymbol(env, asyncGeneratorFuncPrototype, "AsyncGeneratorFunction");
2957 // AsyncGeneratorFunction prototype __proto__ -> Function.
2958 JSObject::SetPrototype(thread_, asyncGeneratorFuncPrototype, env->GetFunctionPrototype());
2959
2960 PropertyDescriptor asyncGeneratorObjDesc(thread_, asyncGeneratorFuncPrototypeValue, false, false, true);
2961 JSObject::DefineOwnProperty(thread_, JSHandle<JSObject>(env->GetInitialAsyncGenerator()),
2962 globalConst->GetHandledConstructorString(), asyncGeneratorObjDesc);
2963
2964 PropertyDescriptor asyncGeneratorObjProtoDesc(thread_, asyncGeneratorFuncPrototypeValue, true, false, false);
2965 JSObject::DefineOwnProperty(thread_, JSHandle<JSObject>(env->GetInitialAsyncGenerator()),
2966 globalConst->GetHandledPrototypeString(), asyncGeneratorObjProtoDesc);
2967
2968 env->SetAsyncGeneratorFunctionPrototype(thread_, asyncGeneratorFuncPrototype);
2969 }
2970
InitializeGenerator(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncClass) const2971 void Builtins::InitializeGenerator(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncClass) const
2972 {
2973 [[maybe_unused]] EcmaHandleScope scope(thread_);
2974 const GlobalEnvConstants *globalConst = thread_->GlobalConstants();
2975 JSHandle<JSObject> generatorFuncPrototype = factory_->NewJSObjectWithInit(objFuncClass);
2976
2977 // GeneratorObject.prototype method
2978 // 26.5.1.2 Generator.prototype.next(value)
2979 SetFunction(env, generatorFuncPrototype, "next", GeneratorObject::GeneratorPrototypeNext, FunctionLength::ONE);
2980 // 26.5.1.3 Generator.prototype.return(value)
2981 SetFunction(env, generatorFuncPrototype, "return", GeneratorObject::GeneratorPrototypeReturn, FunctionLength::ONE);
2982 // 26.5.1.4 Generator.prototype.throw(exception)
2983 SetFunction(env, generatorFuncPrototype, "throw", GeneratorObject::GeneratorPrototypeThrow, FunctionLength::ONE);
2984
2985 // 26.5.1.5 Generator.prototype[@@toStringTag]
2986 SetStringTagSymbol(env, generatorFuncPrototype, "Generator");
2987
2988 // Generator with constructor, symbolTag, next/return/throw etc.
2989 PropertyDescriptor descriptor(thread_, env->GetIteratorPrototype(), true, false, false);
2990 JSObject::DefineOwnProperty(thread_, generatorFuncPrototype, globalConst->GetHandledPrototypeString(), descriptor);
2991 env->SetGeneratorPrototype(thread_, generatorFuncPrototype);
2992 JSObject::SetPrototype(thread_, generatorFuncPrototype, env->GetIteratorPrototype());
2993
2994 // Generator {}
2995 JSHandle<JSObject> initialGeneratorFuncPrototype = factory_->NewJSObjectWithInit(objFuncClass);
2996 JSObject::SetPrototype(thread_, initialGeneratorFuncPrototype, JSHandle<JSTaggedValue>(generatorFuncPrototype));
2997 env->SetInitialGenerator(thread_, initialGeneratorFuncPrototype);
2998 }
2999
InitializeAsyncGenerator(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncClass) const3000 void Builtins::InitializeAsyncGenerator(const JSHandle<GlobalEnv> &env,
3001 const JSHandle<JSHClass> &objFuncClass) const
3002 {
3003 [[maybe_unused]] EcmaHandleScope scope(thread_);
3004
3005 const GlobalEnvConstants *globalConst = thread_->GlobalConstants();
3006 JSHandle<JSObject> asyncGeneratorFuncPrototype = factory_->NewJSObjectWithInit(objFuncClass);
3007
3008 // GeneratorObject.prototype method
3009 // 27.6.1.2 AsyncGenerator.prototype.next ( value )
3010 SetFunction(env, asyncGeneratorFuncPrototype, "next", AsyncGeneratorObject::AsyncGeneratorPrototypeNext,
3011 FunctionLength::ONE);
3012 // 27.6.1.3 AsyncGenerator.prototype.return ( value )
3013 SetFunction(env, asyncGeneratorFuncPrototype, "return", AsyncGeneratorObject::AsyncGeneratorPrototypeReturn,
3014 FunctionLength::ONE);
3015 // 27.6.1.4 AsyncGenerator.prototype.throw ( exception )
3016 SetFunction(env, asyncGeneratorFuncPrototype, "throw", AsyncGeneratorObject::AsyncGeneratorPrototypeThrow,
3017 FunctionLength::ONE);
3018
3019 // 27.6.1.5 AsyncGenerator.prototype [ @@toStringTag ]
3020 SetStringTagSymbol(env, asyncGeneratorFuncPrototype, "AsyncGenerator");
3021
3022 PropertyDescriptor descriptor(thread_, env->GetAsyncIteratorPrototype(), true, false, false);
3023 JSObject::DefineOwnProperty(thread_, asyncGeneratorFuncPrototype,
3024 globalConst->GetHandledPrototypeString(), descriptor);
3025 env->SetAsyncGeneratorPrototype(thread_, asyncGeneratorFuncPrototype);
3026 JSObject::SetPrototype(thread_, asyncGeneratorFuncPrototype, env->GetAsyncIteratorPrototype());
3027
3028 JSHandle<JSObject> initialAsyncGeneratorFuncPrototype = factory_->NewJSObjectWithInit(objFuncClass);
3029 JSObject::SetPrototype(thread_, initialAsyncGeneratorFuncPrototype,
3030 JSHandle<JSTaggedValue>(asyncGeneratorFuncPrototype));
3031 env->SetInitialAsyncGenerator(thread_, initialAsyncGeneratorFuncPrototype);
3032 }
3033
SetArgumentsSharedAccessor(const JSHandle<GlobalEnv> & env)3034 void Builtins::SetArgumentsSharedAccessor(const JSHandle<GlobalEnv> &env)
3035 {
3036 JSHandle<JSTaggedValue> throwFunction = env->GetThrowTypeError();
3037
3038 JSHandle<AccessorData> accessor = factory_->NewAccessorData();
3039 accessor->SetGetter(thread_, throwFunction);
3040 accessor->SetSetter(thread_, throwFunction);
3041 env->SetArgumentsCallerAccessor(thread_, accessor);
3042
3043 accessor = factory_->NewAccessorData();
3044 accessor->SetGetter(thread_, throwFunction);
3045 accessor->SetSetter(thread_, throwFunction);
3046 env->SetArgumentsCalleeAccessor(thread_, accessor);
3047 }
3048
SetAccessor(const JSHandle<JSObject> & obj,const JSHandle<JSTaggedValue> & key,const JSHandle<JSTaggedValue> & getter,const JSHandle<JSTaggedValue> & setter) const3049 void Builtins::SetAccessor(const JSHandle<JSObject> &obj, const JSHandle<JSTaggedValue> &key,
3050 const JSHandle<JSTaggedValue> &getter, const JSHandle<JSTaggedValue> &setter) const
3051 {
3052 JSHandle<AccessorData> accessor = factory_->NewAccessorData();
3053 accessor->SetGetter(thread_, getter);
3054 accessor->SetSetter(thread_, setter);
3055 PropertyAttributes attr = PropertyAttributes::DefaultAccessor(false, false, true);
3056 JSObject::AddAccessor(thread_, JSHandle<JSTaggedValue>::Cast(obj), key, accessor, attr);
3057 }
3058
SetGetter(const JSHandle<JSObject> & obj,const JSHandle<JSTaggedValue> & key,const JSHandle<JSTaggedValue> & getter) const3059 void Builtins::SetGetter(const JSHandle<JSObject> &obj, const JSHandle<JSTaggedValue> &key,
3060 const JSHandle<JSTaggedValue> &getter) const
3061 {
3062 JSHandle<AccessorData> accessor = factory_->NewAccessorData();
3063 accessor->SetGetter(thread_, getter);
3064 PropertyAttributes attr = PropertyAttributes::DefaultAccessor(false, false, true);
3065 JSObject::AddAccessor(thread_, JSHandle<JSTaggedValue>::Cast(obj), key, accessor, attr);
3066 }
3067
NewIntlConstructor(const JSHandle<GlobalEnv> & env,const JSHandle<JSObject> & prototype,EcmaEntrypoint ctorFunc,const char * name,int length)3068 JSHandle<JSFunction> Builtins::NewIntlConstructor(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &prototype,
3069 EcmaEntrypoint ctorFunc, const char *name, int length)
3070 {
3071 JSHandle<JSFunction> ctor =
3072 factory_->NewJSFunction(env, reinterpret_cast<void *>(ctorFunc), FunctionKind::BUILTIN_CONSTRUCTOR);
3073 InitializeIntlCtor(env, prototype, ctor, name, length);
3074 return ctor;
3075 }
3076
InitializeIntlCtor(const JSHandle<GlobalEnv> & env,const JSHandle<JSObject> & prototype,const JSHandle<JSFunction> & ctor,const char * name,int length)3077 void Builtins::InitializeIntlCtor(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &prototype,
3078 const JSHandle<JSFunction> &ctor, const char *name, int length)
3079 {
3080 const GlobalEnvConstants *globalConst = thread_->GlobalConstants();
3081 JSFunction::SetFunctionLength(thread_, ctor, JSTaggedValue(length));
3082 JSHandle<JSTaggedValue> nameString(factory_->NewFromUtf8(name));
3083 JSFunction::SetFunctionName(thread_, JSHandle<JSFunctionBase>(ctor), nameString,
3084 JSHandle<JSTaggedValue>(thread_, JSTaggedValue::Undefined()));
3085 JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString();
3086 PropertyDescriptor descriptor1(thread_, JSHandle<JSTaggedValue>::Cast(ctor), true, false, true);
3087 JSObject::DefineOwnProperty(thread_, prototype, constructorKey, descriptor1);
3088
3089 // set "prototype" in constructor.
3090 ctor->SetFunctionPrototype(thread_, prototype.GetTaggedValue());
3091
3092 if (!JSTaggedValue::SameValue(nameString, thread_->GlobalConstants()->GetHandledAsyncFunctionString())) {
3093 JSHandle<JSObject> intlObject(thread_, env->GetIntlFunction().GetTaggedValue());
3094 PropertyDescriptor descriptor2(thread_, JSHandle<JSTaggedValue>::Cast(ctor), true, false, true);
3095 JSObject::DefineOwnProperty(thread_, intlObject, nameString, descriptor2);
3096 }
3097 }
3098
InitializeIntl(const JSHandle<GlobalEnv> & env,const JSHandle<JSTaggedValue> & objFuncPrototypeValue)3099 void Builtins::InitializeIntl(const JSHandle<GlobalEnv> &env, const JSHandle<JSTaggedValue> &objFuncPrototypeValue)
3100 {
3101 [[maybe_unused]] EcmaHandleScope scope(thread_);
3102 JSHandle<JSHClass> intlHClass = factory_->NewEcmaHClass(JSIntl::SIZE, JSType::JS_INTL, objFuncPrototypeValue);
3103 JSHandle<JSObject> intlObject = factory_->NewJSObjectWithInit(intlHClass);
3104
3105 JSHandle<JSTaggedValue> initIntlSymbol(factory_->NewPublicSymbolWithChar("Symbol.IntlLegacyConstructedSymbol"));
3106 SetNoneAttributeProperty(intlObject, "fallbackSymbol", initIntlSymbol);
3107
3108 SetFunction(env, intlObject, "getCanonicalLocales", Intl::GetCanonicalLocales, FunctionLength::ONE);
3109
3110 // initial value of the "Intl" property of the global object.
3111 JSHandle<JSTaggedValue> intlString(factory_->NewFromASCII("Intl"));
3112 JSHandle<JSObject> globalObject(thread_, env->GetGlobalObject());
3113 PropertyDescriptor intlDesc(thread_, JSHandle<JSTaggedValue>::Cast(intlObject), true, false, true);
3114 JSObject::DefineOwnProperty(thread_, globalObject, intlString, intlDesc);
3115
3116 SetStringTagSymbol(env, intlObject, "Intl");
3117
3118 env->SetIntlFunction(thread_, intlObject);
3119 }
3120
InitializeDateTimeFormat(const JSHandle<GlobalEnv> & env)3121 void Builtins::InitializeDateTimeFormat(const JSHandle<GlobalEnv> &env)
3122 {
3123 [[maybe_unused]] EcmaHandleScope scope(thread_);
3124 // DateTimeFormat.prototype
3125 JSHandle<JSFunction> objFun(env->GetObjectFunction());
3126 JSHandle<JSObject> dtfPrototype = factory_->NewJSObjectByConstructor(objFun);
3127 JSHandle<JSTaggedValue> dtfPrototypeValue(dtfPrototype);
3128
3129 // DateTimeFormat.prototype_or_hclass
3130 JSHandle<JSHClass> dtfFuncInstanceHClass =
3131 factory_->NewEcmaHClass(JSDateTimeFormat::SIZE, JSType::JS_DATE_TIME_FORMAT, dtfPrototypeValue);
3132
3133 // DateTimeFormat = new Function()
3134 // 13.4.1 Intl.DateTimeFormat.prototype.constructor
3135 JSHandle<JSObject> dtfFunction(NewIntlConstructor(env, dtfPrototype, DateTimeFormat::DateTimeFormatConstructor,
3136 "DateTimeFormat", FunctionLength::ZERO));
3137 JSHandle<JSFunction>(dtfFunction)->SetFunctionPrototype(thread_, JSTaggedValue(*dtfFuncInstanceHClass));
3138
3139 // 13.3.2 Intl.DateTimeFormat.supportedLocalesOf ( locales [ , options ] )
3140 SetFunction(env, dtfFunction, "supportedLocalesOf", DateTimeFormat::SupportedLocalesOf, FunctionLength::ONE);
3141
3142 // DateTimeFormat.prototype method
3143 // 13.4.2 Intl.DateTimeFormat.prototype [ @@toStringTag ]
3144 SetStringTagSymbol(env, dtfPrototype, "Intl.DateTimeFormat");
3145 env->SetDateTimeFormatFunction(thread_, dtfFunction);
3146
3147 // 13.4.3 get Intl.DateTimeFormat.prototype.format
3148 JSHandle<JSTaggedValue> formatGetter = CreateGetter(env, DateTimeFormat::Format, "format", FunctionLength::ZERO);
3149 JSHandle<JSTaggedValue> formatSetter(thread_, JSTaggedValue::Undefined());
3150 SetAccessor(dtfPrototype, thread_->GlobalConstants()->GetHandledFormatString(), formatGetter, formatSetter);
3151
3152 // 13.4.4 Intl.DateTimeFormat.prototype.formatToParts ( date )
3153 SetFunction(env, dtfPrototype, "formatToParts", DateTimeFormat::FormatToParts, FunctionLength::ONE);
3154
3155 // 13.4.5 Intl.DateTimeFormat.prototype.resolvedOptions ()
3156 SetFunction(env, dtfPrototype, "resolvedOptions", DateTimeFormat::ResolvedOptions, FunctionLength::ZERO);
3157
3158 SetFunction(env, dtfPrototype, "formatRange", DateTimeFormat::FormatRange, FunctionLength::TWO);
3159
3160 SetFunction(env, dtfPrototype, "formatRangeToParts", DateTimeFormat::FormatRangeToParts, FunctionLength::TWO);
3161 }
3162
InitializeRelativeTimeFormat(const JSHandle<GlobalEnv> & env)3163 void Builtins::InitializeRelativeTimeFormat(const JSHandle<GlobalEnv> &env)
3164 {
3165 [[maybe_unused]] EcmaHandleScope scope(thread_);
3166 // RelativeTimeFormat.prototype
3167 JSHandle<JSFunction> objFun(env->GetObjectFunction());
3168 JSHandle<JSObject> rtfPrototype = factory_->NewJSObjectByConstructor(objFun);
3169 JSHandle<JSTaggedValue> rtfPrototypeValue(rtfPrototype);
3170
3171 // RelativeTimeFormat.prototype_or_hclass
3172 JSHandle<JSHClass> rtfFuncInstanceHClass =
3173 factory_->NewEcmaHClass(JSRelativeTimeFormat::SIZE, JSType::JS_RELATIVE_TIME_FORMAT, rtfPrototypeValue);
3174
3175 // RelativeTimeFormat = new Function()
3176 // 14.2.1 Intl.RelativeTimeFormat.prototype.constructor
3177 JSHandle<JSObject> rtfFunction(NewIntlConstructor(env, rtfPrototype,
3178 RelativeTimeFormat::RelativeTimeFormatConstructor,
3179 "RelativeTimeFormat", FunctionLength::ZERO));
3180 JSHandle<JSFunction>(rtfFunction)->SetFunctionPrototype(thread_, JSTaggedValue(*rtfFuncInstanceHClass));
3181
3182 // 14.3.2 Intl.RelativeTimeFormat.supportedLocalesOf ( locales [ , options ] )
3183 SetFunction(env, rtfFunction, "supportedLocalesOf", RelativeTimeFormat::SupportedLocalesOf, FunctionLength::ONE);
3184
3185 // RelativeTimeFormat.prototype method
3186 // 14.4.2 Intl.RelativeTimeFormat.prototype [ @@toStringTag ]
3187 SetStringTagSymbol(env, rtfPrototype, "Intl.RelativeTimeFormat");
3188 env->SetRelativeTimeFormatFunction(thread_, rtfFunction);
3189
3190 // 14.4.3 get Intl.RelativeTimeFormat.prototype.format
3191 SetFunction(env, rtfPrototype, "format", RelativeTimeFormat::Format, FunctionLength::TWO);
3192
3193 // 14.4.4 Intl.RelativeTimeFormat.prototype.formatToParts( value, unit )
3194 SetFunction(env, rtfPrototype, "formatToParts", RelativeTimeFormat::FormatToParts, FunctionLength::TWO);
3195
3196 // 14.4.5 Intl.RelativeTimeFormat.prototype.resolvedOptions ()
3197 SetFunction(env, rtfPrototype, "resolvedOptions", RelativeTimeFormat::ResolvedOptions, FunctionLength::ZERO);
3198 }
3199
InitializeNumberFormat(const JSHandle<GlobalEnv> & env)3200 void Builtins::InitializeNumberFormat(const JSHandle<GlobalEnv> &env)
3201 {
3202 [[maybe_unused]] EcmaHandleScope scope(thread_);
3203 // NumberFormat.prototype
3204 JSHandle<JSFunction> objFun(env->GetObjectFunction());
3205 JSHandle<JSObject> nfPrototype = factory_->NewJSObjectByConstructor(objFun);
3206 JSHandle<JSTaggedValue> nfPrototypeValue(nfPrototype);
3207
3208 // NumberFormat.prototype_or_hclass
3209 JSHandle<JSHClass> nfFuncInstanceHClass =
3210 factory_->NewEcmaHClass(JSNumberFormat::SIZE, JSType::JS_NUMBER_FORMAT, nfPrototypeValue);
3211
3212 // NumberFormat = new Function()
3213 // 12.4.1 Intl.NumberFormat.prototype.constructor
3214 JSHandle<JSObject> nfFunction(NewIntlConstructor(env, nfPrototype, NumberFormat::NumberFormatConstructor,
3215 "NumberFormat", FunctionLength::ZERO));
3216 JSHandle<JSFunction>(nfFunction)->SetFunctionPrototype(thread_, JSTaggedValue(*nfFuncInstanceHClass));
3217
3218 // 12.3.2 Intl.NumberFormat.supportedLocalesOf ( locales [ , options ] )
3219 SetFunction(env, nfFunction, "supportedLocalesOf", NumberFormat::SupportedLocalesOf, FunctionLength::ONE);
3220
3221 // NumberFormat.prototype method
3222 // 12.4.2 Intl.NumberFormat.prototype [ @@toStringTag ]
3223 SetStringTagSymbol(env, nfPrototype, "Intl.NumberFormat");
3224 env->SetNumberFormatFunction(thread_, nfFunction);
3225
3226 // 12.4.3 get Intl.NumberFormat.prototype.format
3227 JSHandle<JSTaggedValue> formatGetter = CreateGetter(env, NumberFormat::Format, "format", FunctionLength::ZERO);
3228 JSHandle<JSTaggedValue> formatSetter(thread_, JSTaggedValue::Undefined());
3229 SetAccessor(nfPrototype, thread_->GlobalConstants()->GetHandledFormatString(), formatGetter, formatSetter);
3230
3231 // 12.4.4 Intl.NumberFormat.prototype.formatToParts ( date )
3232 SetFunction(env, nfPrototype, "formatToParts", NumberFormat::FormatToParts, FunctionLength::ONE);
3233
3234 // 12.4.5 Intl.NumberFormat.prototype.resolvedOptions ()
3235 SetFunction(env, nfPrototype, "resolvedOptions", NumberFormat::ResolvedOptions, FunctionLength::ZERO);
3236 }
3237
InitializeLocale(const JSHandle<GlobalEnv> & env)3238 void Builtins::InitializeLocale(const JSHandle<GlobalEnv> &env)
3239 {
3240 [[maybe_unused]] EcmaHandleScope scope(thread_);
3241 // Locale.prototype
3242 JSHandle<JSFunction> objFun(env->GetObjectFunction());
3243 JSHandle<JSObject> localePrototype = factory_->NewJSObjectByConstructor(objFun);
3244 JSHandle<JSTaggedValue> localePrototypeValue(localePrototype);
3245
3246 // Locale.prototype_or_hclass
3247 JSHandle<JSHClass> localeFuncInstanceHClass =
3248 factory_->NewEcmaHClass(JSLocale::SIZE, JSType::JS_LOCALE, localePrototypeValue);
3249
3250 // Locale = new Function()
3251 JSHandle<JSObject> localeFunction(
3252 NewIntlConstructor(env, localePrototype, Locale::LocaleConstructor, "Locale", FunctionLength::ONE));
3253 JSHandle<JSFunction>(localeFunction)->SetFunctionPrototype(thread_, JSTaggedValue(*localeFuncInstanceHClass));
3254
3255 // Locale.prototype method
3256 SetFunction(env, localePrototype, "maximize", Locale::Maximize, FunctionLength::ZERO);
3257 SetFunction(env, localePrototype, "minimize", Locale::Minimize, FunctionLength::ZERO);
3258 SetFunction(env, localePrototype, "toString", Locale::ToString, FunctionLength::ZERO);
3259
3260 JSHandle<JSTaggedValue> baseNameGetter = CreateGetter(env, Locale::GetBaseName, "baseName", FunctionLength::ZERO);
3261 SetGetter(localePrototype, thread_->GlobalConstants()->GetHandledBaseNameString(), baseNameGetter);
3262
3263 JSHandle<JSTaggedValue> calendarGetter = CreateGetter(env, Locale::GetCalendar, "calendar", FunctionLength::ZERO);
3264 SetGetter(localePrototype, thread_->GlobalConstants()->GetHandledCalendarString(), calendarGetter);
3265
3266 JSHandle<JSTaggedValue> caseFirstGetter =
3267 CreateGetter(env, Locale::GetCaseFirst, "caseFirst", FunctionLength::ZERO);
3268 SetGetter(localePrototype, thread_->GlobalConstants()->GetHandledCaseFirstString(), caseFirstGetter);
3269
3270 JSHandle<JSTaggedValue> collationGetter =
3271 CreateGetter(env, Locale::GetCollation, "collation", FunctionLength::ZERO);
3272 SetGetter(localePrototype, thread_->GlobalConstants()->GetHandledCollationString(), collationGetter);
3273
3274 JSHandle<JSTaggedValue> hourCycleGetter =
3275 CreateGetter(env, Locale::GetHourCycle, "hourCycle", FunctionLength::ZERO);
3276 SetGetter(localePrototype, thread_->GlobalConstants()->GetHandledHourCycleString(), hourCycleGetter);
3277
3278 JSHandle<JSTaggedValue> numericGetter = CreateGetter(env, Locale::GetNumeric, "numeric", FunctionLength::ZERO);
3279 SetGetter(localePrototype, thread_->GlobalConstants()->GetHandledNumericString(), numericGetter);
3280
3281 JSHandle<JSTaggedValue> numberingSystemGetter =
3282 CreateGetter(env, Locale::GetNumberingSystem, "numberingSystem", FunctionLength::ZERO);
3283 SetGetter(localePrototype, thread_->GlobalConstants()->GetHandledNumberingSystemString(), numberingSystemGetter);
3284
3285 JSHandle<JSTaggedValue> languageGetter = CreateGetter(env, Locale::GetLanguage, "language", FunctionLength::ZERO);
3286 SetGetter(localePrototype, thread_->GlobalConstants()->GetHandledLanguageString(), languageGetter);
3287
3288 JSHandle<JSTaggedValue> scriptGetter = CreateGetter(env, Locale::GetScript, "script", FunctionLength::ZERO);
3289 SetGetter(localePrototype, thread_->GlobalConstants()->GetHandledScriptString(), scriptGetter);
3290
3291 JSHandle<JSTaggedValue> regionGetter = CreateGetter(env, Locale::GetRegion, "region", FunctionLength::ZERO);
3292 SetGetter(localePrototype, thread_->GlobalConstants()->GetHandledRegionString(), regionGetter);
3293
3294 // 10.3.2 Intl.Locale.prototype[ @@toStringTag ]
3295 SetStringTagSymbol(env, localePrototype, "Intl.Locale");
3296 env->SetLocaleFunction(thread_, localeFunction);
3297 }
3298
InitializeCollator(const JSHandle<GlobalEnv> & env)3299 void Builtins::InitializeCollator(const JSHandle<GlobalEnv> &env)
3300 {
3301 [[maybe_unused]] EcmaHandleScope scope(thread_);
3302 // Collator.prototype
3303 JSHandle<JSFunction> objFun(env->GetObjectFunction());
3304 JSHandle<JSObject> collatorPrototype = factory_->NewJSObjectByConstructor(objFun);
3305 JSHandle<JSTaggedValue> collatorPrototypeValue(collatorPrototype);
3306
3307 // Collator.prototype_or_hclass
3308 JSHandle<JSHClass> collatorFuncInstanceHClass =
3309 factory_->NewEcmaHClass(JSCollator::SIZE, JSType::JS_COLLATOR, collatorPrototypeValue);
3310
3311 // Collator = new Function()
3312 // 11.1.2 Intl.Collator.prototype.constructor
3313 JSHandle<JSObject> collatorFunction(
3314 NewIntlConstructor(env, collatorPrototype, Collator::CollatorConstructor, "Collator", FunctionLength::ZERO));
3315 JSHandle<JSFunction>(collatorFunction)->SetFunctionPrototype(thread_, JSTaggedValue(*collatorFuncInstanceHClass));
3316
3317 // 11.2.2 Intl.Collator.supportedLocalesOf ( locales [ , options ] )
3318 SetFunction(env, collatorFunction, "supportedLocalesOf", Collator::SupportedLocalesOf, FunctionLength::ONE);
3319
3320 // Collator.prototype method
3321 // 11.3.2 Intl.Collator.prototype [ @@toStringTag ]
3322 SetStringTagSymbol(env, collatorPrototype, "Intl.Collator");
3323 env->SetCollatorFunction(thread_, collatorFunction);
3324
3325 // 11.3.3 get Intl.Collator.prototype.compare
3326 JSHandle<JSTaggedValue> compareGetter = CreateGetter(env, Collator::Compare, "compare", FunctionLength::ZERO);
3327 JSHandle<JSTaggedValue> compareSetter(thread_, JSTaggedValue::Undefined());
3328 SetAccessor(collatorPrototype, thread_->GlobalConstants()->GetHandledCompareString(), compareGetter, compareSetter);
3329
3330 // 11.3.4 Intl.Collator.prototype.resolvedOptions ()
3331 SetFunction(env, collatorPrototype, "resolvedOptions", Collator::ResolvedOptions, FunctionLength::ZERO);
3332 }
3333
InitializePluralRules(const JSHandle<GlobalEnv> & env)3334 void Builtins::InitializePluralRules(const JSHandle<GlobalEnv> &env)
3335 {
3336 [[maybe_unused]] EcmaHandleScope scope(thread_);
3337 // PluralRules.prototype
3338 JSHandle<JSFunction> objFun(env->GetObjectFunction());
3339 JSHandle<JSObject> prPrototype = factory_->NewJSObjectByConstructor(objFun);
3340 JSHandle<JSTaggedValue> prPrototypeValue(prPrototype);
3341
3342 // PluralRules.prototype_or_hclass
3343 JSHandle<JSHClass> prFuncInstanceHClass =
3344 factory_->NewEcmaHClass(JSPluralRules::SIZE, JSType::JS_PLURAL_RULES, prPrototypeValue);
3345
3346 // PluralRules = new Function()
3347 // 15.2.1 Intl.PluralRules.prototype.constructor
3348 JSHandle<JSObject> prFunction(
3349 NewIntlConstructor(env, prPrototype, PluralRules::PluralRulesConstructor, "PluralRules", FunctionLength::ZERO));
3350 JSHandle<JSFunction>(prFunction)->SetFunctionPrototype(thread_, JSTaggedValue(*prFuncInstanceHClass));
3351
3352 // 15.3.2 Intl.PluralRules.supportedLocalesOf ( locales [ , options ] )
3353 SetFunction(env, prFunction, "supportedLocalesOf", PluralRules::SupportedLocalesOf, FunctionLength::ONE);
3354
3355 // PluralRules.prototype method
3356 // 15.4.2 Intl.PluralRules.prototype [ @@toStringTag ]
3357 SetStringTagSymbol(env, prPrototype, "Intl.PluralRules");
3358 env->SetPluralRulesFunction(thread_, prFunction);
3359
3360 // 15.4.3 get Intl.PluralRules.prototype.select
3361 SetFunction(env, prPrototype, "select", PluralRules::Select, FunctionLength::ONE);
3362
3363 // 15.4.5 Intl.PluralRules.prototype.resolvedOptions ()
3364 SetFunction(env, prPrototype, "resolvedOptions", PluralRules::ResolvedOptions, FunctionLength::ZERO);
3365 }
3366
InitializeDisplayNames(const JSHandle<GlobalEnv> & env)3367 void Builtins::InitializeDisplayNames(const JSHandle<GlobalEnv> &env)
3368 {
3369 [[maybe_unused]] EcmaHandleScope scope(thread_);
3370 // DisplayNames.prototype
3371 JSHandle<JSFunction> objFun(env->GetObjectFunction());
3372 JSHandle<JSObject> dnPrototype = factory_->NewJSObjectByConstructor(objFun);
3373 JSHandle<JSTaggedValue> dnPrototypeValue(dnPrototype);
3374
3375 // DisplayNames.prototype_or_hclass
3376 JSHandle<JSHClass> dnFuncInstanceHClass =
3377 factory_->NewEcmaHClass(JSDisplayNames::SIZE, JSType::JS_DISPLAYNAMES, dnPrototypeValue);
3378
3379 // DisplayNames = new Function()
3380 // 12.4.1 Intl.DisplayNames.prototype.constructor
3381 JSHandle<JSObject> dnFunction(NewIntlConstructor(env, dnPrototype, DisplayNames::DisplayNamesConstructor,
3382 "DisplayNames", FunctionLength::TWO));
3383 JSHandle<JSFunction>(dnFunction)->SetFunctionPrototype(thread_, JSTaggedValue(*dnFuncInstanceHClass));
3384
3385 // 12.3.2 Intl.DisplayNames.supportedLocalesOf ( locales [ , options ] )
3386 SetFunction(env, dnFunction, "supportedLocalesOf", DisplayNames::SupportedLocalesOf, FunctionLength::ONE);
3387
3388 // DisplayNames.prototype method
3389 // 12.4.2 Intl.DisplayNames.prototype[ @@toStringTag ]
3390 SetStringTagSymbol(env, dnPrototype, "Intl.DisplayNames");
3391 env->SetDisplayNamesFunction(thread_, dnFunction);
3392
3393 // 12.4.3 get Intl.DisplayNames.prototype.of
3394 SetFunction(env, dnPrototype, "of", DisplayNames::Of, FunctionLength::ONE);
3395
3396 // 12.4.4 Intl.DisplayNames.prototype.resolvedOptions ()
3397 SetFunction(env, dnPrototype, "resolvedOptions", DisplayNames::ResolvedOptions, FunctionLength::ZERO);
3398 }
3399
InitializeListFormat(const JSHandle<GlobalEnv> & env)3400 void Builtins::InitializeListFormat(const JSHandle<GlobalEnv> &env)
3401 {
3402 [[maybe_unused]] EcmaHandleScope scope(thread_);
3403 // JSListFormat.prototype
3404 JSHandle<JSFunction> objFun(env->GetObjectFunction());
3405 JSHandle<JSObject> lfPrototype = factory_->NewJSObjectByConstructor(objFun);
3406 JSHandle<JSTaggedValue> lfPrototypeValue(lfPrototype);
3407
3408 // JSListFormat.prototype_or_hclass
3409 JSHandle<JSHClass> lfFuncInstanceHClass =
3410 factory_->NewEcmaHClass(JSListFormat::SIZE, JSType::JS_LIST_FORMAT, lfPrototypeValue);
3411
3412 // JSListFormat = new Function()
3413 // 13.4.1 Intl.ListFormat.prototype.constructor
3414 JSHandle<JSObject> lfFunction(NewIntlConstructor(env, lfPrototype, ListFormat::ListFormatConstructor,
3415 "ListFormat", FunctionLength::ZERO));
3416 JSHandle<JSFunction>(lfFunction)->SetFunctionPrototype(thread_, JSTaggedValue(*lfFuncInstanceHClass));
3417
3418 // 13.3.2 Intl.ListFormat.supportedLocalesOf ( locales [ , options ] )
3419 SetFunction(env, lfFunction, "supportedLocalesOf", ListFormat::SupportedLocalesOf, FunctionLength::ONE);
3420
3421 // ListFormat.prototype method
3422 // 13.4.2 Intl.ListFormat.prototype [ @@toStringTag ]
3423 SetStringTagSymbol(env, lfPrototype, "Intl.ListFormat");
3424 env->SetListFormatFunction(thread_, lfFunction);
3425
3426 // 13.4.3 get Intl.ListFormat.prototype.format( list )
3427 SetFunction(env, lfPrototype, "format", ListFormat::Format, FunctionLength::ONE);
3428
3429 // 13.4.4 Intl.ListFormat.prototype.formatToParts( list )
3430 SetFunction(env, lfPrototype, "formatToParts", ListFormat::FormatToParts, FunctionLength::ONE);
3431
3432 // 13.4.5 Intl.ListFormat.prototype.resolvedOptions()
3433 SetFunction(env, lfPrototype, "resolvedOptions", ListFormat::ResolvedOptions, FunctionLength::ZERO);
3434 }
3435
InitializeArkTools(const JSHandle<GlobalEnv> & env) const3436 JSHandle<JSObject> Builtins::InitializeArkTools(const JSHandle<GlobalEnv> &env) const
3437 {
3438 JSHandle<JSObject> tools = factory_->NewEmptyJSObject();
3439 SetFunction(env, tools, "print", builtins::BuiltinsArkTools::ObjectDump, FunctionLength::ZERO);
3440 SetFunction(env, tools, "compareHClass", builtins::BuiltinsArkTools::CompareHClass, FunctionLength::TWO);
3441 SetFunction(env, tools, "dumpHClass", builtins::BuiltinsArkTools::DumpHClass, FunctionLength::ONE);
3442 SetFunction(env, tools, "isTSHClass", builtins::BuiltinsArkTools::IsTSHClass, FunctionLength::ONE);
3443 SetFunction(env, tools, "getHClass", builtins::BuiltinsArkTools::GetHClass, FunctionLength::ONE);
3444 SetFunction(env, tools, "forceFullGC", builtins::BuiltinsArkTools::ForceFullGC, FunctionLength::ZERO);
3445 SetFunction(env, tools, "removeAOTFlag", builtins::BuiltinsArkTools::RemoveAOTFlag, FunctionLength::ONE);
3446 #if defined(ECMASCRIPT_SUPPORT_CPUPROFILER)
3447 SetFunction(env, tools, "startCpuProf", builtins::BuiltinsArkTools::StartCpuProfiler, FunctionLength::ZERO);
3448 SetFunction(env, tools, "stopCpuProf", builtins::BuiltinsArkTools::StopCpuProfiler, FunctionLength::ZERO);
3449 #endif
3450 SetFunction(env, tools, "isPrototype", builtins::BuiltinsArkTools::IsPrototype, FunctionLength::ONE);
3451 return tools;
3452 }
3453
InitializeGlobalRegExp(JSHandle<JSObject> & obj) const3454 void Builtins::InitializeGlobalRegExp(JSHandle<JSObject> &obj) const
3455 {
3456 JSHandle<JSTaggedValue> emptyString = thread_->GlobalConstants()->GetHandledEmptyString();
3457 JSHandle<JSTaggedValue> newBox1 = JSHandle<JSTaggedValue>(factory_->NewPropertyBox(emptyString));
3458 SetConstantObject(obj, "$1", newBox1);
3459 JSHandle<JSTaggedValue> newBox2 = JSHandle<JSTaggedValue>(factory_->NewPropertyBox(emptyString));
3460 SetConstantObject(obj, "$2", newBox2);
3461 JSHandle<JSTaggedValue> newBox3 = JSHandle<JSTaggedValue>(factory_->NewPropertyBox(emptyString));
3462 SetConstantObject(obj, "$3", newBox3);
3463 JSHandle<JSTaggedValue> newBox4 = JSHandle<JSTaggedValue>(factory_->NewPropertyBox(emptyString));
3464 SetConstantObject(obj, "$4", newBox4);
3465 JSHandle<JSTaggedValue> newBox5 = JSHandle<JSTaggedValue>(factory_->NewPropertyBox(emptyString));
3466 SetConstantObject(obj, "$5", newBox5);
3467 JSHandle<JSTaggedValue> newBox6 = JSHandle<JSTaggedValue>(factory_->NewPropertyBox(emptyString));
3468 SetConstantObject(obj, "$6", newBox6);
3469 JSHandle<JSTaggedValue> newBox7 = JSHandle<JSTaggedValue>(factory_->NewPropertyBox(emptyString));
3470 SetConstantObject(obj, "$7", newBox7);
3471 JSHandle<JSTaggedValue> newBox8 = JSHandle<JSTaggedValue>(factory_->NewPropertyBox(emptyString));
3472 SetConstantObject(obj, "$8", newBox8);
3473 JSHandle<JSTaggedValue> newBox9 = JSHandle<JSTaggedValue>(factory_->NewPropertyBox(emptyString));
3474 SetConstantObject(obj, "$9", newBox9);
3475 }
3476
InitializeArkPrivate(const JSHandle<GlobalEnv> & env) const3477 JSHandle<JSObject> Builtins::InitializeArkPrivate(const JSHandle<GlobalEnv> &env) const
3478 {
3479 JSHandle<JSObject> arkPrivate = factory_->NewEmptyJSObject();
3480 SetFrozenFunction(env, arkPrivate, "Load", ContainersPrivate::Load, FunctionLength::ZERO);
3481
3482 // It is used to provide non ECMA standard jsapi containers.
3483 SetConstant(arkPrivate, "ArrayList", JSTaggedValue(static_cast<int>(containers::ContainerTag::ArrayList)));
3484 SetConstant(arkPrivate, "Queue", JSTaggedValue(static_cast<int>(containers::ContainerTag::Queue)));
3485 SetConstant(arkPrivate, "Deque", JSTaggedValue(static_cast<int>(containers::ContainerTag::Deque)));
3486 SetConstant(arkPrivate, "Stack", JSTaggedValue(static_cast<int>(containers::ContainerTag::Stack)));
3487 SetConstant(arkPrivate, "Vector", JSTaggedValue(static_cast<int>(containers::ContainerTag::Vector)));
3488 SetConstant(arkPrivate, "List", JSTaggedValue(static_cast<int>(containers::ContainerTag::List)));
3489 SetConstant(arkPrivate, "LinkedList", JSTaggedValue(static_cast<int>(containers::ContainerTag::LinkedList)));
3490 SetConstant(arkPrivate, "TreeMap", JSTaggedValue(static_cast<int>(containers::ContainerTag::TreeMap)));
3491 SetConstant(arkPrivate, "TreeSet", JSTaggedValue(static_cast<int>(containers::ContainerTag::TreeSet)));
3492 SetConstant(arkPrivate, "HashMap", JSTaggedValue(static_cast<int>(containers::ContainerTag::HashMap)));
3493 SetConstant(arkPrivate, "HashSet", JSTaggedValue(static_cast<int>(containers::ContainerTag::HashSet)));
3494 SetConstant(arkPrivate, "LightWeightMap",
3495 JSTaggedValue(static_cast<int>(containers::ContainerTag::LightWeightMap)));
3496 SetConstant(arkPrivate, "LightWeightSet",
3497 JSTaggedValue(static_cast<int>(containers::ContainerTag::LightWeightSet)));
3498 SetConstant(arkPrivate, "PlainArray", JSTaggedValue(static_cast<int>(containers::ContainerTag::PlainArray)));
3499 return arkPrivate;
3500 }
3501
InitializeModuleNamespace(const JSHandle<GlobalEnv> & env,const JSHandle<JSHClass> & objFuncClass) const3502 void Builtins::InitializeModuleNamespace(const JSHandle<GlobalEnv> &env,
3503 const JSHandle<JSHClass> &objFuncClass) const
3504 {
3505 [[maybe_unused]] EcmaHandleScope scope(thread_);
3506 // ModuleNamespace.prototype
3507 JSHandle<JSObject> moduleNamespacePrototype = factory_->NewJSObjectWithInit(objFuncClass);
3508 JSHandle<JSTaggedValue> moduleNamespacePrototypeValue(moduleNamespacePrototype);
3509
3510 // ModuleNamespace.prototype_or_hclass
3511 JSHandle<JSHClass> moduleNamespaceHClass =
3512 factory_->NewEcmaHClass(ModuleNamespace::SIZE, JSType::JS_MODULE_NAMESPACE, moduleNamespacePrototypeValue);
3513 moduleNamespaceHClass->SetPrototype(thread_, JSTaggedValue::Null());
3514 env->SetModuleNamespaceClass(thread_, moduleNamespaceHClass.GetTaggedValue());
3515
3516 // moduleNamespace.prototype [ @@toStringTag ]
3517 SetStringTagSymbol(env, moduleNamespacePrototype, "Module");
3518 }
3519
InitializeCjsModule(const JSHandle<GlobalEnv> & env) const3520 void Builtins::InitializeCjsModule(const JSHandle<GlobalEnv> &env) const
3521 {
3522 [[maybe_unused]] EcmaHandleScope scope(thread_);
3523 // CjsModule.prototype
3524 JSHandle<JSFunction> objFun(env->GetObjectFunction());
3525 JSHandle<JSObject> cjsModulePrototype = factory_->NewJSObjectByConstructor(objFun);
3526 JSHandle<JSTaggedValue> cjsModulePrototypeValue(cjsModulePrototype);
3527
3528 // CjsModule.prototype_or_hclass
3529 JSHandle<JSHClass> cjsModuleHClass =
3530 factory_->NewEcmaHClass(CjsModule::SIZE, JSType::JS_CJS_MODULE, cjsModulePrototypeValue);
3531
3532 // CjsModule.prototype.Constructor
3533 JSHandle<JSObject> cjsModuleFunction(
3534 NewBuiltinCjsCtor(env, cjsModulePrototype, BuiltinsCjsModule::CjsModuleConstructor, "Module",
3535 FunctionLength::TWO));
3536
3537 JSHandle<JSFunction>(cjsModuleFunction)->SetFunctionPrototype(thread_, cjsModuleHClass.GetTaggedValue());
3538
3539 // CjsModule method
3540 SetFunction(env, cjsModuleFunction, "_load", BuiltinsCjsModule::Load, FunctionLength::ONE);
3541 SetFunction(env, cjsModuleFunction, "_resolveFilename", BuiltinsCjsModule::ResolveFilename, FunctionLength::ONE);
3542
3543 // CjsModule.prototype method
3544 SetFunction(env, cjsModulePrototype, "require", BuiltinsCjsModule::Require, FunctionLength::ONE);
3545 SetFunction(env, cjsModulePrototype, "getExportsForCircularRequire",
3546 BuiltinsCjsModule::GetExportsForCircularRequire, FunctionLength::ONE);
3547 SetFunction(env, cjsModulePrototype, "updateChildren", BuiltinsCjsModule::UpdateChildren, FunctionLength::ONE);
3548
3549 JSHandle<JSTaggedValue> id(thread_->GlobalConstants()->GetHandledEmptyString());
3550 JSHandle<JSTaggedValue> path(thread_->GlobalConstants()->GetHandledEmptyString());
3551 JSHandle<JSTaggedValue> exports(factory_->NewEmptyJSObject());
3552 JSHandle<JSTaggedValue> parent(factory_->NewEmptyJSObject());
3553 JSHandle<JSTaggedValue> filename(thread_->GlobalConstants()->GetHandledEmptyString());
3554 JSHandle<JSTaggedValue> loaded(factory_->NewEmptyJSObject());
3555 JSHandle<JSTaggedValue> children(factory_->NewEmptyJSObject());
3556 JSHandle<JSTaggedValue> cache = JSHandle<JSTaggedValue>::Cast(CjsModuleCache::Create(thread_,
3557 CjsModuleCache::DEAULT_DICTIONART_CAPACITY));
3558
3559 // CjsModule.prototype members
3560 SetNonConstantObject(cjsModulePrototype, "id", id);
3561 SetNonConstantObject(cjsModulePrototype, "path", path);
3562 SetNonConstantObject(cjsModulePrototype, "exports", exports);
3563 SetNonConstantObject(cjsModulePrototype, "parent", parent);
3564 SetNonConstantObject(cjsModulePrototype, "filename", filename);
3565 SetNonConstantObject(cjsModulePrototype, "loaded", loaded);
3566 SetNonConstantObject(cjsModulePrototype, "children", children);
3567
3568 // CjsModule members
3569 SetNonConstantObject(cjsModuleFunction, "_cache", cache);
3570
3571 env->SetCjsModuleFunction(thread_, cjsModuleFunction);
3572 }
3573
InitializeCjsExports(const JSHandle<GlobalEnv> & env) const3574 void Builtins::InitializeCjsExports(const JSHandle<GlobalEnv> &env) const
3575 {
3576 [[maybe_unused]] EcmaHandleScope scope(thread_);
3577
3578 // CjsExports.prototype
3579 JSHandle<JSFunction> objFun(env->GetObjectFunction());
3580 JSHandle<JSObject> cjsExportsPrototype = factory_->NewJSObjectByConstructor(objFun);
3581 JSHandle<JSTaggedValue> cjsExportsPrototypeValue(cjsExportsPrototype);
3582
3583 // CjsExports.prototype_or_hclass
3584 JSHandle<JSHClass> cjsExportsHClass =
3585 factory_->NewEcmaHClass(CjsExports::SIZE, JSType::JS_CJS_EXPORTS, cjsExportsPrototypeValue);
3586
3587 // CjsExports.prototype.Constructor
3588 JSHandle<JSObject> cjsExportsFunction(
3589 NewBuiltinCjsCtor(env, cjsExportsPrototype, BuiltinsCjsExports::CjsExportsConstructor, "Exports",
3590 FunctionLength::TWO));
3591
3592 JSHandle<JSFunction>(cjsExportsFunction)->SetFunctionPrototype(thread_, cjsExportsHClass.GetTaggedValue());
3593
3594 env->SetCjsExportsFunction(thread_, cjsExportsFunction);
3595 }
3596
InitializeCjsRequire(const JSHandle<GlobalEnv> & env) const3597 void Builtins::InitializeCjsRequire(const JSHandle<GlobalEnv> &env) const
3598 {
3599 [[maybe_unused]] EcmaHandleScope scope(thread_);
3600 // CjsRequire.prototype
3601 JSHandle<JSFunction> objFun(env->GetObjectFunction());
3602 JSHandle<JSObject> cjsRequirePrototype = factory_->NewJSObjectByConstructor(objFun);
3603 JSHandle<JSTaggedValue> cjsRequirePrototypeValue(cjsRequirePrototype);
3604
3605 // CjsExports.prototype_or_hclass
3606 JSHandle<JSHClass> cjsRequireHClass =
3607 factory_->NewEcmaHClass(CjsRequire::SIZE, JSType::JS_CJS_REQUIRE, cjsRequirePrototypeValue);
3608
3609 // CjsExports.prototype.Constructor
3610 JSHandle<JSFunction> cjsRequireFunction =
3611 NewBuiltinCjsCtor(env, cjsRequirePrototype, BuiltinsCjsRequire::CjsRequireConstructor, "require",
3612 FunctionLength::ONE);
3613 JSHandle<JSFunction>(cjsRequireFunction)->SetFunctionPrototype(thread_, cjsRequireHClass.GetTaggedValue());
3614
3615 // CjsModule.prototype method
3616 SetFunction(env, cjsRequirePrototype, "Main", BuiltinsCjsRequire::Main, FunctionLength::ONE);
3617
3618 env->SetCjsRequireFunction(thread_, cjsRequireFunction);
3619 }
3620
InitializeDefaultExportOfScript(const JSHandle<GlobalEnv> & env) const3621 void Builtins::InitializeDefaultExportOfScript(const JSHandle<GlobalEnv> &env) const
3622 {
3623 JSHandle<JSFunction> builtinObj(env->GetObjectFunction());
3624 JSHandle<JSTaggedValue> emptyObj(factory_->NewJSObjectByConstructor(builtinObj));
3625 JSHandle<JSTaggedValue> defaultKey(factory_->NewFromUtf8("default"));
3626
3627 JSHandle<TaggedArray> props(factory_->NewTaggedArray(2)); // 2 : two propertise
3628 props->Set(thread_, 0, defaultKey);
3629 props->Set(thread_, 1, emptyObj);
3630 JSHandle<JSHClass> hclass = factory_->CreateObjectClass(props, 1);
3631 JSHandle<JSObject> obj = factory_->NewJSObject(hclass);
3632 obj->SetPropertyInlinedProps(thread_, 0, props->Get(1));
3633 env->SetExportOfScript(thread_, obj);
3634 return;
3635 }
3636 } // namespace panda::ecmascript
3637