1/* 2 * Copyright (c) 2021-2024 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/builtins/builtins_ark_tools.h" 19#include "ecmascript/builtins/builtins_array.h" 20#include "ecmascript/builtins/builtins_arraybuffer.h" 21#include "ecmascript/builtins/builtins_async_from_sync_iterator.h" 22#include "ecmascript/builtins/builtins_async_function.h" 23#include "ecmascript/builtins/builtins_async_iterator.h" 24#include "ecmascript/builtins/builtins_async_generator.h" 25#include "ecmascript/builtins/builtins_atomics.h" 26#include "ecmascript/builtins/builtins_bigint.h" 27#include "ecmascript/builtins/builtins_boolean.h" 28#include "ecmascript/builtins/builtins_cjs_module.h" 29#include "ecmascript/builtins/builtins_cjs_require.h" 30#include "ecmascript/builtins/builtins_cjs_exports.h" 31#include "ecmascript/builtins/builtins_dataview.h" 32#include "ecmascript/builtins/builtins_date.h" 33#include "ecmascript/builtins/builtins_errors.h" 34#include "ecmascript/builtins/builtins_finalization_registry.h" 35#include "ecmascript/builtins/builtins_function.h" 36#include "ecmascript/builtins/builtins_gc.h" 37#include "ecmascript/builtins/builtins_generator.h" 38#include "ecmascript/builtins/builtins_global.h" 39#include "ecmascript/builtins/builtins_iterator.h" 40#include "ecmascript/builtins/builtins_json.h" 41#include "ecmascript/builtins/builtins_map.h" 42#include "ecmascript/builtins/builtins_math.h" 43#include "ecmascript/builtins/builtins_number.h" 44#include "ecmascript/builtins/builtins_object.h" 45#include "ecmascript/builtins/builtins_promise.h" 46#include "ecmascript/builtins/builtins_promise_handler.h" 47#include "ecmascript/builtins/builtins_promise_job.h" 48#include "ecmascript/builtins/builtins_proxy.h" 49#include "ecmascript/builtins/builtins_reflect.h" 50#include "ecmascript/builtins/builtins_regexp.h" 51#include "ecmascript/builtins/builtins_set.h" 52#include "ecmascript/builtins/builtins_sharedarraybuffer.h" 53#include "ecmascript/builtins/builtins_shared_typedarray.h" 54#include "ecmascript/builtins/builtins_string_iterator.h" 55#include "ecmascript/builtins/builtins_symbol.h" 56#include "ecmascript/builtins/builtins_typedarray.h" 57#include "ecmascript/builtins/builtins_weak_map.h" 58#include "ecmascript/builtins/builtins_weak_ref.h" 59#include "ecmascript/builtins/builtins_weak_set.h" 60#include "ecmascript/containers/containers_private.h" 61#include "ecmascript/dfx/native_module_failure_info.h" 62#include "ecmascript/js_arraybuffer.h" 63#include "ecmascript/js_array_iterator.h" 64#include "ecmascript/js_async_function.h" 65#include "ecmascript/js_async_generator_object.h" 66#include "ecmascript/js_for_in_iterator.h" 67#include "ecmascript/js_finalization_registry.h" 68#include "ecmascript/js_map.h" 69#include "ecmascript/js_map_iterator.h" 70#include "ecmascript/js_primitive_ref.h" 71#include "ecmascript/js_regexp_iterator.h" 72#include "ecmascript/js_set.h" 73#include "ecmascript/js_set_iterator.h" 74#include "ecmascript/js_string_iterator.h" 75#include "ecmascript/js_async_from_sync_iterator.h" 76#include "ecmascript/js_weak_container.h" 77#include "ecmascript/js_weak_ref.h" 78#include "ecmascript/require/js_cjs_module_cache.h" 79#include "ecmascript/require/js_cjs_require.h" 80#include "ecmascript/shared_objects/js_shared_array_iterator.h" 81#include "ecmascript/shared_objects/js_shared_map_iterator.h" 82#include "ecmascript/shared_objects/js_shared_set_iterator.h" 83#include "ecmascript/marker_cell.h" 84#include "ecmascript/runtime.h" 85#ifdef ARK_SUPPORT_INTL 86#include "ecmascript/builtins/builtins_collator.h" 87#include "ecmascript/builtins/builtins_date_time_format.h" 88#include "ecmascript/builtins/builtins_displaynames.h" 89#include "ecmascript/builtins/builtins_intl.h" 90#include "ecmascript/builtins/builtins_list_format.h" 91#include "ecmascript/builtins/builtins_locale.h" 92#include "ecmascript/builtins/builtins_number_format.h" 93#include "ecmascript/builtins/builtins_plural_rules.h" 94#include "ecmascript/builtins/builtins_relative_time_format.h" 95#include "ecmascript/builtins/builtins_segmenter.h" 96#include "ecmascript/builtins/builtins_segments.h" 97#include "ecmascript/builtins/builtins_segment_iterator.h" 98#include "ecmascript/js_collator.h" 99#include "ecmascript/js_date_time_format.h" 100#include "ecmascript/js_displaynames.h" 101#include "ecmascript/js_list_format.h" 102#include "ecmascript/js_number_format.h" 103#include "ecmascript/js_plural_rules.h" 104#include "ecmascript/js_segments.h" 105#include "ecmascript/js_segment_iterator.h" 106#endif 107 108#include "ohos/init_data.h" 109 110namespace panda::ecmascript { 111using Number = builtins::BuiltinsNumber; 112using BuiltinsBigInt = builtins::BuiltinsBigInt; 113using Object = builtins::BuiltinsObject; 114using Date = builtins::BuiltinsDate; 115using Symbol = builtins::BuiltinsSymbol; 116using Boolean = builtins::BuiltinsBoolean; 117using BuiltinsLazyCallback = builtins::BuiltinsLazyCallback; 118using BuiltinsMap = builtins::BuiltinsMap; 119using BuiltinsSet = builtins::BuiltinsSet; 120using BuiltinsWeakMap = builtins::BuiltinsWeakMap; 121using BuiltinsWeakSet = builtins::BuiltinsWeakSet; 122using BuiltinsWeakRef = builtins::BuiltinsWeakRef; 123using BuiltinsFinalizationRegistry = builtins::BuiltinsFinalizationRegistry; 124using BuiltinsArray = builtins::BuiltinsArray; 125using BuiltinsTypedArray = builtins::BuiltinsTypedArray; 126using BuiltinsIterator = builtins::BuiltinsIterator; 127 128using Error = builtins::BuiltinsError; 129using RangeError = builtins::BuiltinsRangeError; 130using ReferenceError = builtins::BuiltinsReferenceError; 131using TypeError = builtins::BuiltinsTypeError; 132using AggregateError = builtins::BuiltinsAggregateError; 133using URIError = builtins::BuiltinsURIError; 134using SyntaxError = builtins::BuiltinsSyntaxError; 135using EvalError = builtins::BuiltinsEvalError; 136using OOMError = builtins::BuiltinsOOMError; 137using TerminationError = builtins::BuiltinsTerminationError; 138using ErrorType = base::ErrorType; 139using RandomGenerator = base::RandomGenerator; 140using Global = builtins::BuiltinsGlobal; 141using BuiltinsString = builtins::BuiltinsString; 142using StringIterator = builtins::BuiltinsStringIterator; 143using BuiltinsAsyncFromSyncIterator = builtins::BuiltinsAsyncFromSyncIterator; 144using RegExp = builtins::BuiltinsRegExp; 145using Function = builtins::BuiltinsFunction; 146using Math = builtins::BuiltinsMath; 147using Atomics = builtins::BuiltinsAtomics; 148using ArrayBuffer = builtins::BuiltinsArrayBuffer; 149using Json = builtins::BuiltinsJson; 150using SendableJson = builtins::BuiltinsSendableJson; 151using BigIntJson = builtins::BuiltinsBigIntJson; 152using Proxy = builtins::BuiltinsProxy; 153using Reflect = builtins::BuiltinsReflect; 154using AsyncFunction = builtins::BuiltinsAsyncFunction; 155using GeneratorObject = builtins::BuiltinsGenerator; 156using Promise = builtins::BuiltinsPromise; 157using BuiltinsPromiseHandler = builtins::BuiltinsPromiseHandler; 158using BuiltinsPromiseJob = builtins::BuiltinsPromiseJob; 159using ErrorType = base::ErrorType; 160using DataView = builtins::BuiltinsDataView; 161#ifdef ARK_SUPPORT_INTL 162using Intl = builtins::BuiltinsIntl; 163using Locale = builtins::BuiltinsLocale; 164using DateTimeFormat = builtins::BuiltinsDateTimeFormat; 165using RelativeTimeFormat = builtins::BuiltinsRelativeTimeFormat; 166using NumberFormat = builtins::BuiltinsNumberFormat; 167using Collator = builtins::BuiltinsCollator; 168using PluralRules = builtins::BuiltinsPluralRules; 169using DisplayNames = builtins::BuiltinsDisplayNames; 170using Segmenter = builtins::BuiltinsSegmenter; 171using Segments = builtins::BuiltinsSegments; 172using SegmentIterator = builtins::BuiltinsSegmentIterator; 173using ListFormat = builtins::BuiltinsListFormat; 174#endif 175using BuiltinsCjsModule = builtins::BuiltinsCjsModule; 176using BuiltinsCjsExports = builtins::BuiltinsCjsExports; 177using BuiltinsCjsRequire = builtins::BuiltinsCjsRequire; 178 179using ContainersPrivate = containers::ContainersPrivate; 180using SharedArrayBuffer = builtins::BuiltinsSharedArrayBuffer; 181 182using BuiltinsAsyncIterator = builtins::BuiltinsAsyncIterator; 183using AsyncGeneratorObject = builtins::BuiltinsAsyncGenerator; 184 185static constexpr size_t REGEXP_INLINE_PROPS = 18; 186 187void Builtins::Initialize(const JSHandle<GlobalEnv> &env, JSThread *thread, bool lazyInit, bool isRealm) 188{ 189 thread->CheckSafepointIfSuspended(); 190 thread_ = thread; 191 vm_ = thread->GetEcmaVM(); 192 factory_ = vm_->GetFactory(); 193 sHeap_ = SharedHeap::GetInstance(); 194 [[maybe_unused]] EcmaHandleScope scope(thread_); 195 JSHandle<JSTaggedValue> nullHandle(thread, JSTaggedValue::Null()); 196 197 // Object.prototype[hclass] 198 JSHandle<JSHClass> objPrototypeHClass = factory_->NewEcmaHClass(JSObject::SIZE, JSType::JS_OBJECT, nullHandle); 199 200 // Object.prototype 201 JSHandle<JSObject> objFuncPrototype = factory_->NewJSObject(objPrototypeHClass); 202 JSHandle<JSTaggedValue> objFuncPrototypeVal(objFuncPrototype); 203 204 // Object.prototype_or_hclass 205 JSHandle<JSHClass> objFuncClass = 206 factory_->NewEcmaHClass(JSObject::SIZE, JSType::JS_OBJECT, objFuncPrototypeVal); 207 env->SetObjectFunctionClass(thread_, objFuncClass); 208 209 // GLobalObject.prototype_or_hclass 210 JSHandle<JSHClass> globalObjFuncClass = 211 factory_->NewEcmaHClass(JSObject::SIZE, JSType::JS_GLOBAL_OBJECT, 0); 212 globalObjFuncClass->SetPrototype(thread_, objFuncPrototypeVal.GetTaggedValue()); 213 globalObjFuncClass->SetIsDictionaryMode(true); 214 215 // PrimitiveRef.prototype_or_hclass 216 JSHandle<JSHClass> primRefObjHClass = 217 factory_->NewEcmaHClass(JSPrimitiveRef::SIZE, JSType::JS_PRIMITIVE_REF, objFuncPrototypeVal); 218 219 // init global object 220 JSHandle<JSObject> globalObject = factory_->NewNonMovableJSObject(globalObjFuncClass); 221 env->SetJSGlobalObject(thread_, globalObject); 222 223 // init global patch 224 JSHandle<TaggedArray> globalPatch = factory_->EmptyArray(); 225 env->SetGlobalPatch(thread, globalPatch); 226 227 auto runtimeGlobalEnv = Runtime::GetInstance()->GetGlobalEnv(); 228 if (runtimeGlobalEnv.IsHole()) { 229 InitializeSSymbolAttributes(env); 230 InitializeSObjectAndSFunction(env); 231 } else { 232 CopySObjectAndSFunction(env, runtimeGlobalEnv); 233 RegisterSendableContainers(env); 234 } 235 InitializeFunction(env, objFuncPrototypeVal); 236 237 thread->CheckSafepointIfSuspended(); 238 JSHandle<JSHClass> asyncAwaitStatusFuncClass = 239 factory_->CreateFunctionClass(FunctionKind::NORMAL_FUNCTION, JSAsyncAwaitStatusFunction::SIZE, 240 JSType::JS_ASYNC_AWAIT_STATUS_FUNCTION, env->GetFunctionPrototype()); 241 env->SetAsyncAwaitStatusFunctionClass(thread_, asyncAwaitStatusFuncClass); 242 243 JSHandle<JSHClass> asyncGeneratorResNextRetProRstFtnClass = 244 factory_->NewEcmaHClass(JSAsyncGeneratorResNextRetProRstFtn::SIZE, 245 JSType::JS_ASYNC_GENERATOR_RESUME_NEXT_RETURN_PROCESSOR_RST_FTN, 246 env->GetFunctionPrototype()); 247 asyncGeneratorResNextRetProRstFtnClass->SetCallable(true); 248 asyncGeneratorResNextRetProRstFtnClass->SetExtensible(true); 249 env->SetAsyncGeneratorResNextRetProRstFtnClass(thread_, asyncGeneratorResNextRetProRstFtnClass); 250 251 JSHandle<JSHClass> proxyRevocFuncClass = factory_->NewEcmaHClass( 252 JSProxyRevocFunction::SIZE, JSType::JS_PROXY_REVOC_FUNCTION, env->GetFunctionPrototype()); 253 proxyRevocFuncClass->SetCallable(true); 254 proxyRevocFuncClass->SetExtensible(true); 255 env->SetProxyRevocFunctionClass(thread_, proxyRevocFuncClass); 256 257 // Object = new Function() 258 JSHandle<JSObject> objectFunction( 259 NewBuiltinConstructor(env, objFuncPrototype, Object::ObjectConstructor, "Object", 260 FunctionLength::ONE, BUILTINS_STUB_ID(ObjectConstructor))); 261 JSFunction::SetFunctionPrototypeOrInstanceHClass(thread_, JSHandle<JSFunction>(objectFunction), 262 objFuncClass.GetTaggedValue()); 263 // initialize object method. 264 env->SetObjectFunction(thread_, objectFunction); 265 env->SetObjectFunctionPrototype(thread_, objFuncPrototype); 266 thread_->SetInitialBuiltinHClass( 267 BuiltinTypeId::OBJECT, objectFunction->GetJSHClass(), *objFuncClass, objFuncPrototype->GetJSHClass()); 268 269 JSHandle<JSHClass> functionClass = factory_->CreateFunctionClass(FunctionKind::BASE_CONSTRUCTOR, JSFunction::SIZE, 270 JSType::JS_FUNCTION, env->GetFunctionPrototype()); 271 env->SetFunctionClassWithProto(thread_, functionClass); 272 functionClass = factory_->CreateFunctionClass(FunctionKind::NORMAL_FUNCTION, JSFunction::SIZE, JSType::JS_FUNCTION, 273 env->GetFunctionPrototype()); 274 env->SetFunctionClassWithoutProto(thread_, functionClass); 275 functionClass = factory_->CreateFunctionClass(FunctionKind::CLASS_CONSTRUCTOR, JSFunction::SIZE, 276 JSType::JS_FUNCTION, env->GetFunctionPrototype()); 277 env->SetFunctionClassWithoutName(thread_, functionClass); 278 279 thread->CheckSafepointIfSuspended(); 280 functionClass = factory_->CreateBoundFunctionClass(); 281 env->SetBoundFunctionClass(thread_, functionClass); 282 if (!isRealm) { 283 InitializeAllTypeError(env, objFuncClass); 284 InitializeSymbol(env, primRefObjHClass); 285 InitializeBigInt(env, primRefObjHClass); 286 } else { 287 // error and symbol need to be shared when initialize realm 288 InitializeAllTypeErrorWithRealm(env); 289 InitializeSymbolWithRealm(env, primRefObjHClass); 290 InitializeBigIntWithRealm(env); 291 } 292 293 thread->CheckSafepointIfSuspended(); 294 InitializeArray(env, objFuncPrototypeVal); 295 if (lazyInit) { 296 LazyInitializeDate(env); 297 LazyInitializeSet(env); 298 LazyInitializeMap(env); 299 LazyInitializeWeakMap(env); 300 LazyInitializeWeakSet(env); 301 LazyInitializeWeakRef(env); 302 LazyInitializeFinalizationRegistry(env); 303 LazyInitializeTypedArray(env); 304 LazyInitializeArrayBuffer(env); 305 LazyInitializeDataView(env); 306 LazyInitializeSharedArrayBuffer(env); 307 } else { 308 InitializeDate(env, objFuncPrototypeVal); 309 InitializeSet(env, objFuncPrototypeVal); 310 InitializeMap(env, objFuncPrototypeVal); 311 InitializeWeakMap(env, objFuncClass); 312 InitializeWeakSet(env, objFuncClass); 313 InitializeWeakRef(env, objFuncClass); 314 InitializeFinalizationRegistry(env, objFuncClass); 315 InitializeTypedArray(env, objFuncPrototypeVal); 316 InitializeArrayBuffer(env, objFuncClass); 317 InitializeDataView(env, objFuncPrototypeVal); 318 InitializeSharedArrayBuffer(env, objFuncClass); 319 } 320 thread->CheckSafepointIfSuspended(); 321 InitializeNumber(env, globalObject, primRefObjHClass); 322 InitializeObject(env, objFuncPrototype, objectFunction); 323 InitializeBoolean(env, primRefObjHClass); 324 InitializeRegExp(env); 325 InitializeString(env, objFuncPrototypeVal); 326 JSHandle<JSHClass> argumentsClass = factory_->CreateJSArguments(env); 327 env->SetArgumentsClass(thread_, argumentsClass); 328 SetArgumentsSharedAccessor(env); 329 330 InitializeMath(env, objFuncPrototypeVal); 331 InitializeGlobalObject(env, globalObject); 332 InitializeAtomics(env, objFuncPrototypeVal); 333 InitializeJson(env, objFuncPrototypeVal); 334 InitializeIterator(env, objFuncClass); 335 InitializeAsyncIterator(env, objFuncClass); 336 InitializeAsyncFromSyncIterator(env, objFuncClass); 337 InitializeProxy(env); 338 InitializeReflect(env, objFuncPrototypeVal); 339 InitializeAsyncFunction(env, objFuncClass); 340 InitializeGenerator(env, objFuncClass); 341 InitializeAsyncGenerator(env, objFuncClass); 342 InitializeGeneratorFunction(env, objFuncClass); 343 InitializeAsyncGeneratorFunction(env, objFuncClass); 344 InitializePromise(env, objFuncClass); 345 InitializePromiseJob(env); 346 thread->CheckSafepointIfSuspended(); 347#ifdef ARK_SUPPORT_INTL 348 InitializeIntl(env, objFuncPrototypeVal); 349 if (lazyInit) { 350 LazyInitializeLocale(env); 351 LazyInitializeDateTimeFormat(env); 352 LazyInitializeNumberFormat(env); 353 LazyInitializeRelativeTimeFormat(env); 354 LazyInitializeCollator(env); 355 LazyInitializePluralRules(env); 356 LazyInitializeDisplayNames(env); 357 LazyInitializeListFormat(env); 358 LazyInitializeSegments(env); 359 LazyInitializeSegmenter(env); 360 } else { 361 InitializeLocale(env); 362 InitializeDateTimeFormat(env); 363 InitializeNumberFormat(env); 364 InitializeRelativeTimeFormat(env); 365 InitializeCollator(env); 366 InitializePluralRules(env); 367 InitializeDisplayNames(env); 368 InitializeListFormat(env); 369 InitializeSegments(env); 370 InitializeSegmenter(env); 371 } 372 thread->CheckSafepointIfSuspended(); 373#endif 374 InitializeModuleNamespace(env, objFuncClass); 375 InitializeNativeModuleFailureInfo(env, objFuncClass); 376 InitializeCjsModule(env); 377 InitializeCjsExports(env); 378 InitializeCjsRequire(env); 379 InitializeDefaultExportOfScript(env); 380 InitializePropertyDetector(env, lazyInit); 381 JSHandle<JSHClass> generatorFuncClass = 382 factory_->CreateFunctionClass(FunctionKind::GENERATOR_FUNCTION, JSFunction::SIZE, JSType::JS_GENERATOR_FUNCTION, 383 env->GetGeneratorFunctionPrototype()); 384 env->SetGeneratorFunctionClass(thread_, generatorFuncClass); 385 386 JSHandle<JSHClass> asyncGenetatorFuncClass = 387 factory_->CreateFunctionClass(FunctionKind::ASYNC_GENERATOR_FUNCTION, JSFunction::SIZE, 388 JSType::JS_ASYNC_GENERATOR_FUNCTION, env->GetAsyncGeneratorFunctionPrototype()); 389 env->SetAsyncGeneratorFunctionClass(thread_, asyncGenetatorFuncClass); 390 env->SetObjectFunctionPrototypeClass(thread_, JSTaggedValue(objFuncPrototype->GetClass())); 391 JSHandle<JSHClass> asyncFuncClass = factory_->CreateFunctionClass( 392 FunctionKind::ASYNC_FUNCTION, JSAsyncFunction::SIZE, JSType::JS_ASYNC_FUNCTION, 393 env->GetAsyncFunctionPrototype()); 394 env->SetAsyncFunctionClass(thread_, asyncFuncClass); 395 thread_->ResetGuardians(); 396 397 thread->CheckSafepointIfSuspended(); 398 if (vm_->GetJSOptions().IsEnableLoweringBuiltin()) { 399 if (!lazyInit) { 400 thread_->InitializeBuiltinObject(); 401 } 402 } 403} 404 405void Builtins::InitializePropertyDetector(const JSHandle<GlobalEnv> &env, bool lazyInit) const 406{ 407#define INITIALIZE_PROPERTY_DETECTOR(type, name, index) \ 408 JSHandle<MarkerCell> name##detector = factory_->NewMarkerCell(); \ 409 if (lazyInit) { \ 410 name##detector->InvalidatePropertyDetector(); \ 411 } \ 412 env->Set##name(thread_, name##detector); 413 GLOBAL_ENV_DETECTOR_FIELDS(INITIALIZE_PROPERTY_DETECTOR) 414#undef INITIALIZE_PROPERTY_DETECTOR 415} 416 417void Builtins::SetLazyAccessor(const JSHandle<JSObject> &object, const JSHandle<JSTaggedValue> &key, 418 const JSHandle<AccessorData> &accessor) const 419{ 420 PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>::Cast(accessor), true, false, true); 421 JSObject::DefineOwnProperty(thread_, object, key, descriptor); 422} 423 424void Builtins::InitializeForSnapshot(JSThread *thread) 425{ 426 thread_ = thread; 427 vm_ = thread->GetEcmaVM(); 428 factory_ = vm_->GetFactory(); 429 430 // Initialize ArkTools 431 if (vm_->GetJSOptions().EnableArkTools()) { 432 auto env = vm_->GetGlobalEnv(); 433 auto globalObject = JSHandle<JSObject>::Cast(env->GetJSGlobalObject()); 434 JSHandle<JSTaggedValue> arkTools(InitializeArkTools(env)); 435 SetConstantObject(globalObject, "ArkTools", arkTools); 436 } 437} 438 439void Builtins::InitializeGlobalObject(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &globalObject) 440{ 441 [[maybe_unused]] EcmaHandleScope scope(thread_); 442 443 // Global object test 444 SetFunction(env, globalObject, "print", Global::PrintEntrypoint, 0); 445 SetFunction(env, globalObject, "markModuleCollectable", Global::MarkModuleCollectable, 0); 446 SetFunction(env, globalObject, "isSendable", Global::IsSendable, 0); 447 SetFunction(env, globalObject, "loadNativeModule", Global::LoadNativeModule, 0); 448#if ECMASCRIPT_ENABLE_RUNTIME_STAT 449 SetFunction(env, globalObject, "startRuntimeStat", Global::StartRuntimeStat, 0); 450 SetFunction(env, globalObject, "stopRuntimeStat", Global::StopRuntimeStat, 0); 451#endif 452 453#if ECMASCRIPT_ENABLE_OPT_CODE_PROFILER 454 SetFunction(env, globalObject, "printOptStat", Global::PrintOptStat, 0); 455#endif 456 457#if ECMASCRIPT_ENABLE_FUNCTION_CALL_TIMER 458 SetFunction(env, globalObject, "printFunctionCallStat", Global::PrintFunctionCallStat, 0); 459#endif 460 461 if (vm_->GetJSOptions().EnableArkTools()) { 462 JSHandle<JSTaggedValue> arkTools(InitializeArkTools(env)); 463 SetConstantObject(globalObject, "ArkTools", arkTools); 464 } 465 466#if ECMASCRIPT_ENABLE_ARK_CONTAINER 467 // Set ArkPrivate 468 JSHandle<JSTaggedValue> arkPrivate(InitializeArkPrivate(env)); 469 SetConstantObject(globalObject, "ArkPrivate", arkPrivate); 470#endif 471 472 // Global object function 473 SetFunction(env, globalObject, "eval", Global::NotSupportEval, FunctionLength::ONE); 474 SetFunction(env, globalObject, "isFinite", Global::IsFinite, FunctionLength::ONE, 475 kungfu::BuiltinsStubCSigns::GlobalIsFinite); 476 SetFunction(env, globalObject, "isNaN", Global::IsNaN, FunctionLength::ONE, 477 kungfu::BuiltinsStubCSigns::GlobalIsNan); 478 SetFunction(env, globalObject, "decodeURI", Global::DecodeURI, FunctionLength::ONE); 479 SetFunction(env, globalObject, "encodeURI", Global::EncodeURI, FunctionLength::ONE); 480 SetFunction(env, globalObject, "escape", Global::Escape, FunctionLength::ONE); 481 SetFunction(env, globalObject, "unescape", Global::Unescape, FunctionLength::ONE); 482 SetFunction(env, globalObject, "decodeURIComponent", Global::DecodeURIComponent, FunctionLength::ONE, 483 kungfu::BuiltinsStubCSigns::GlobalDecodeURIComponent); 484 SetFunction(env, globalObject, "encodeURIComponent", Global::EncodeURIComponent, FunctionLength::ONE); 485 SetFunction(env, globalObject, "__getCurrentModuleName__", Global::GetCurrentModuleName, FunctionLength::ZERO); 486 SetFunction(env, globalObject, "__getCurrentBundleName__", Global::GetCurrentBundleName, FunctionLength::ZERO); 487 488 // Global object property 489 SetGlobalThis(globalObject, "globalThis", JSHandle<JSTaggedValue>::Cast(globalObject)); 490 SetConstant(globalObject, "Infinity", JSTaggedValue(base::POSITIVE_INFINITY)); 491 SetConstant(globalObject, "NaN", JSTaggedValue(base::NAN_VALUE)); 492 SetConstant(globalObject, "undefined", JSTaggedValue::Undefined()); 493} 494 495JSHandle<JSHClass> Builtins::CreateFunctionPrototypeHClass(const JSHandle<GlobalEnv> &env, 496 const JSHandle<JSTaggedValue> &ObjPrototypeVal) const 497{ 498 uint32_t index = 0; 499 PropertyAttributes attributes = PropertyAttributes::Default(false, false, false); 500 attributes.SetIsInlinedProps(true); 501 attributes.SetRepresentation(Representation::TAGGED); 502 auto properties = Function::GetFunctionPrototypeProperties(); 503 uint32_t length = properties.size(); 504 JSHandle<LayoutInfo> layout = factory_->CreateLayoutInfo(length); 505 JSHandle<JSTaggedValue> keyString; 506 for (const base::BuiltinsPropertyConfig &each : properties) { 507 attributes.SetOffset(index); 508 attributes.SetIsAccessor(each.GetIsAccessor()); 509 attributes.SetWritable(each.GetWritable()); 510 attributes.SetEnumerable(each.GetEnumerable()); 511 attributes.SetConfigurable(each.GetConfigurable()); 512 if (each.GetName() == "[Symbol.hasInstance]") { 513 keyString = env->GetHasInstanceSymbol(); 514 } else { 515 keyString = JSHandle<JSTaggedValue>(factory_->NewFromUtf8ReadOnly(each.GetName())); 516 } 517 layout->AddKey(thread_, index++, keyString.GetTaggedValue(), attributes); 518 } 519 JSHandle<JSHClass> objPrototypeHClass = 520 factory_->NewEcmaHClass(JSFunction::SIZE, length, JSType::JS_FUNCTION, ObjPrototypeVal, 521 JSHandle<JSTaggedValue>(layout)); 522 objPrototypeHClass->SetCallable(true); 523 return objPrototypeHClass; 524} 525 526JSHandle<JSHClass> Builtins::CreateFunctionHClass(const JSHandle<JSFunction> &funcPrototype) const 527{ 528 uint32_t index = 0; 529 PropertyAttributes attributes = PropertyAttributes::Default(false, false, false); 530 attributes.SetIsInlinedProps(true); 531 attributes.SetRepresentation(Representation::TAGGED); 532 auto properties = Function::GetFunctionProperties(); 533 uint32_t length = properties.size(); 534 JSHandle<LayoutInfo> layout = factory_->CreateLayoutInfo(length); 535 for (const base::BuiltinsPropertyConfig &each : properties) { 536 attributes.SetOffset(index); 537 attributes.SetIsAccessor(each.GetIsAccessor()); 538 attributes.SetWritable(each.GetWritable()); 539 attributes.SetEnumerable(each.GetEnumerable()); 540 attributes.SetConfigurable(each.GetConfigurable()); 541 JSHandle<JSTaggedValue> keyString(factory_->NewFromUtf8ReadOnly(each.GetName())); 542 layout->AddKey(thread_, index++, keyString.GetTaggedValue(), attributes); 543 } 544 JSHandle<JSHClass> objPrototypeHClass = 545 factory_->NewEcmaHClass(JSFunction::SIZE, length, JSType::JS_FUNCTION, 546 JSHandle<JSTaggedValue>(funcPrototype), JSHandle<JSTaggedValue>(layout)); 547 objPrototypeHClass->SetConstructor(true); 548 objPrototypeHClass->SetCallable(true); 549 return objPrototypeHClass; 550} 551 552void Builtins::SetFunctionName(const JSHandle<JSFunction> &ctor, std::string_view name) const 553{ 554 JSHandle<JSTaggedValue> nameString(factory_->NewFromUtf8ReadOnly(name)); 555 SetSFunctionName(ctor, nameString); 556} 557 558void Builtins::SetFunctionName(const JSHandle<JSFunction> &ctor, const JSHandle<JSTaggedValue> &name) const 559{ 560 auto nameIndex = JSFunction::NAME_INLINE_PROPERTY_INDEX; 561 ctor->SetPropertyInlinedProps(thread_, nameIndex, name.GetTaggedValue()); 562} 563 564void Builtins::SetFunctionLength(const JSHandle<JSFunction> &ctor, int length) const 565{ 566 JSTaggedValue taggedLength(length); 567 auto lengthIndex = JSFunction::LENGTH_INLINE_PROPERTY_INDEX; 568 ctor->SetPropertyInlinedProps(thread_, lengthIndex, taggedLength); 569} 570 571void Builtins::SetFunctionPrototype(const JSHandle<JSFunction> &ctor, const JSTaggedValue &prototype) const 572{ 573 auto prototypeIndex = JSFunction::PROTOTYPE_INLINE_PROPERTY_INDEX; 574 ctor->SetPropertyInlinedProps(thread_, prototypeIndex, prototype); 575} 576 577void Builtins::SetInlineFunction(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &obj, std::string_view key, 578 EcmaEntrypoint func, uint32_t index, int length, 579 kungfu::BuiltinsStubCSigns::ID builtinId) const 580{ 581 JSHandle<JSTaggedValue> keyString(factory_->NewFromUtf8ReadOnly(key)); 582 SetInlineFunction(env, obj, keyString, func, index, length, builtinId); 583} 584 585void Builtins::SetInlineFunction(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &obj, 586 const JSHandle<JSTaggedValue> &key, EcmaEntrypoint func, uint32_t index, int length, 587 kungfu::BuiltinsStubCSigns::ID builtinId) const 588{ 589 JSHandle<JSFunction> function(NewFunction(env, key, func, length, builtinId)); 590 obj->SetPropertyInlinedProps(thread_, index, function.GetTaggedValue()); 591} 592 593JSHandle<JSFunction> Builtins::SetInlineFunctionAndRetJSFunction(const JSHandle<GlobalEnv> &env, 594 const JSHandle<JSObject> &obj, std::string_view key, 595 EcmaEntrypoint func, uint32_t index, int length, 596 kungfu::BuiltinsStubCSigns::ID builtinId) const 597{ 598 JSHandle<JSTaggedValue> keyString(factory_->NewFromUtf8ReadOnly(key)); 599 JSHandle<JSFunction> function(NewFunction(env, keyString, func, length, builtinId)); 600 obj->SetPropertyInlinedProps(thread_, index, function.GetTaggedValue()); 601 return function; 602} 603 604void Builtins::SetInlineAccessor(const JSHandle<JSObject> &obj, uint32_t index, 605 const JSHandle<JSTaggedValue> &getter, const JSHandle<JSTaggedValue> &setter) const 606{ 607 JSHandle<AccessorData> accessor = factory_->NewAccessorData(); 608 accessor->SetGetter(thread_, getter); 609 accessor->SetSetter(thread_, setter); 610 obj->SetPropertyInlinedProps(thread_, index, accessor.GetTaggedValue()); 611} 612 613void Builtins::StrictModeForbiddenAccessCallerArguments(const JSHandle<GlobalEnv> &env, uint32_t &index, 614 const JSHandle<JSObject> &prototype) const 615{ 616 auto function = JSHandle<JSTaggedValue>::Cast( 617 factory_->NewJSFunction(env, reinterpret_cast<void *>(JSFunction::AccessCallerArgumentsThrowTypeError))); 618 // "caller" 619 SetInlineAccessor(prototype, index++, function, function); 620 // "arguments" 621 SetInlineAccessor(prototype, index++, function, function); 622} 623 624void Builtins::InitializeFunctionPrototype(const JSHandle<GlobalEnv> &env, JSHandle<JSFunction> &funcFuncPrototype, 625 JSHandle<JSFunction> &funcFunc) const 626{ 627 auto funcFuncPrototypeObj = JSHandle<JSObject>(funcFuncPrototype); 628 SetFunctionLength(funcFuncPrototype, FunctionLength::ZERO); 629 SetFunctionName(funcFuncPrototype, thread_->GlobalConstants()->GetHandledEmptyString()); 630 uint32_t fieldIndex = 2; // 2: length and name 631 funcFuncPrototype->SetPropertyInlinedProps(thread_, fieldIndex++, funcFunc.GetTaggedValue()); // constructor 632 StrictModeForbiddenAccessCallerArguments(env, fieldIndex, funcFuncPrototypeObj); 633 // Function.prototype method 634 for (const base::BuiltinFunctionEntry &entry: Function::GetFunctionPrototypeFunctions()) { 635 SetInlineFunction(env, funcFuncPrototypeObj, entry.GetName(), entry.GetEntrypoint(), fieldIndex++, 636 entry.GetLength(), entry.GetBuiltinStubId()); 637 } 638 // 19.2.3.5 Function.prototype.toString ( ) 639 SetInlineFunction(env, funcFuncPrototypeObj, thread_->GlobalConstants()->GetHandledToStringString(), 640 Function::FunctionPrototypeToString, fieldIndex++, FunctionLength::ZERO); 641 JSHandle<JSFunction> function = SetInlineFunctionAndRetJSFunction( 642 env, funcFuncPrototypeObj, "[Symbol.hasInstance]", 643 Function::FunctionPrototypeHasInstance, fieldIndex++, FunctionLength::ONE, 644 BUILTINS_STUB_ID(FunctionPrototypeHasInstance)); 645 env->SetHasInstanceFunction(thread_, function); 646} 647 648void Builtins::InitializeFunction(const JSHandle<GlobalEnv> &env, JSHandle<JSTaggedValue> &objFuncPrototypeVal) const 649{ 650 [[maybe_unused]] EcmaHandleScope scope(thread_); 651 // Function.prototype.hclass 652 JSHandle<JSHClass> funcPrototypeHClass = CreateFunctionPrototypeHClass(env, objFuncPrototypeVal); 653 // Function.prototype 654 JSHandle<JSFunction> funcFuncPrototype = factory_->NewJSFunctionByHClassWithoutAccessor( 655 reinterpret_cast<void *>(Function::FunctionPrototypeInvokeSelf), funcPrototypeHClass); 656 657 JSHandle<JSTaggedValue> funcFuncPrototypeValue(funcFuncPrototype); 658 // Function.prototype_or_hclass 659 JSHandle<JSHClass> funcFuncIntanceHClass = 660 factory_->NewEcmaHClass(JSFunction::SIZE, JSType::JS_FUNCTION, funcFuncPrototypeValue); 661 funcFuncIntanceHClass->SetConstructor(true); 662 // Function.hclass 663 JSHandle<JSHClass> funcHClass = CreateFunctionHClass(funcFuncPrototype); 664 // Function = new Function() (forbidden use NewBuiltinConstructor) 665 JSHandle<JSFunction> funcFunc = 666 factory_->NewJSFunctionByHClassWithoutAccessor(reinterpret_cast<void *>(Function::FunctionConstructor), 667 funcHClass, FunctionKind::BUILTIN_CONSTRUCTOR); 668 669 // set properties for Function 670 SetFunctionLength(funcFunc, FunctionLength::ONE); 671 SetFunctionName(funcFunc, "Function"); 672 SetFunctionPrototype(funcFunc, funcFuncPrototypeValue.GetTaggedValue()); 673 674 JSHandle<JSTaggedValue> nameString(factory_->NewFromUtf8ReadOnly("Function")); 675 JSHandle<JSObject> globalObject(thread_, env->GetGlobalObject()); 676 PropertyDescriptor descriptor2(thread_, JSHandle<JSTaggedValue>::Cast(funcFunc), true, false, true); 677 JSObject::DefineOwnProperty(thread_, globalObject, nameString, descriptor2); 678 679 JSFunction::SetFunctionPrototypeOrInstanceHClass(thread_, funcFunc, funcFuncIntanceHClass.GetTaggedValue()); 680 env->SetFunctionFunction(thread_, funcFunc); 681 env->SetFunctionPrototype(thread_, funcFuncPrototype); 682 683 JSHandle<JSHClass> normalFuncClass = 684 factory_->NewEcmaHClass(JSFunction::SIZE, JSType::JS_FUNCTION, env->GetFunctionPrototype()); 685 env->SetNormalFunctionClass(thread_, normalFuncClass); 686 687 JSHandle<JSHClass> jSIntlBoundFunctionClass = 688 factory_->CreateFunctionClass(FunctionKind::NORMAL_FUNCTION, JSIntlBoundFunction::SIZE, 689 JSType::JS_INTL_BOUND_FUNCTION, env->GetFunctionPrototype()); 690 env->SetJSIntlBoundFunctionClass(thread_, jSIntlBoundFunctionClass); 691 692 JSHandle<JSHClass> constructorFunctionClass = 693 factory_->NewEcmaHClass(JSFunction::SIZE, JSType::JS_FUNCTION, env->GetFunctionPrototype()); 694 constructorFunctionClass->SetConstructor(true); 695 env->SetConstructorFunctionClass(thread_, constructorFunctionClass); 696 InitializeFunctionPrototype(env, funcFuncPrototype, funcFunc); 697} 698 699void Builtins::InitializeObject(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &objFuncPrototype, 700 const JSHandle<JSObject> &objFunc) 701{ 702 [[maybe_unused]] EcmaHandleScope scope(thread_); 703 // Object method. 704 for (const base::BuiltinFunctionEntry &entry: Object::GetObjectFunctions()) { 705 SetFunction(env, objFunc, entry.GetName(), entry.GetEntrypoint(), 706 entry.GetLength(), entry.GetBuiltinStubId()); 707 } 708 // Object.prototype method 709 for (const base::BuiltinFunctionEntry &entry: Object::GetObjectPrototypeFunctions()) { 710 SetFunction(env, objFuncPrototype, entry.GetName(), entry.GetEntrypoint(), 711 entry.GetLength(), entry.GetBuiltinStubId()); 712 } 713 714 // B.2.2.1 Object.prototype.__proto__ 715 JSHandle<JSTaggedValue> protoKey(factory_->NewFromASCIIReadOnly("__proto__")); 716 JSHandle<JSTaggedValue> protoGetter = CreateGetter(env, Object::ProtoGetter, "__proto__", FunctionLength::ZERO); 717 JSHandle<JSTaggedValue> protoSetter = CreateSetter(env, Object::ProtoSetter, "__proto__", FunctionLength::ONE); 718 SetAccessor(objFuncPrototype, protoKey, protoGetter, protoSetter); 719 720 GlobalEnvConstants *globalConst = const_cast<GlobalEnvConstants *>(thread_->GlobalConstants()); 721 globalConst->SetConstant(ConstantIndex::OBJECT_GET_PROTO_INDEX, protoGetter); 722 723 GlobalIndex globalIndex; 724 globalIndex.UpdateGlobalEnvId(static_cast<size_t>(GlobalEnvField::OBJECT_FUNCTION_INDEX)); 725 thread_->SetInitialBuiltinGlobalHClass(objFunc->GetJSHClass(), globalIndex); 726} 727 728void Builtins::InitializeSymbol(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncClass) const 729{ 730 [[maybe_unused]] EcmaHandleScope scope(thread_); 731 const GlobalEnvConstants *globalConst = thread_->GlobalConstants(); 732 // Symbol.prototype 733 JSHandle<JSObject> symbolFuncPrototype = factory_->NewJSObjectWithInit(objFuncClass); 734 JSHandle<JSTaggedValue> symbolFuncPrototypeValue(symbolFuncPrototype); 735 736 // Symbol.prototype_or_hclass 737 JSHandle<JSHClass> symbolFuncInstanceHClass = 738 factory_->NewEcmaHClass(JSPrimitiveRef::SIZE, JSType::JS_PRIMITIVE_REF, symbolFuncPrototypeValue); 739 740 // Symbol = new Function() 741 JSHandle<JSObject> symbolFunction( 742 NewBuiltinConstructor(env, symbolFuncPrototype, Symbol::SymbolConstructor, "Symbol", FunctionLength::ZERO)); 743 JSFunction::SetFunctionPrototypeOrInstanceHClass(thread_, 744 JSHandle<JSFunction>(symbolFunction), symbolFuncInstanceHClass.GetTaggedValue()); 745 746 // "constructor" property on the prototype 747 JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString(); 748 PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>::Cast(symbolFunction), true, false, true); 749 JSObject::DefineOwnProperty(thread_, symbolFuncPrototype, constructorKey, descriptor); 750 751 for (const base::BuiltinFunctionEntry &entry: Symbol::GetSymbolFunctions()) { 752 SetFunction(env, symbolFunction, entry.GetName(), entry.GetEntrypoint(), 753 entry.GetLength(), entry.GetBuiltinStubId()); 754 } 755 756 // Symbol attributes 757#define REGISTER_SYMBOL(name, Name) \ 758 SetNoneAttributeProperty(symbolFunction, #name, env->Get##Name##Symbol()); 759 760BUILTIN_ALL_SYMBOLS(REGISTER_SYMBOL) 761 env->SetSymbolFunction(thread_, symbolFunction); 762 763 // symbol.prototype.description 764 PropertyDescriptor descriptionDesc(thread_); 765 JSHandle<JSTaggedValue> getterKey(factory_->NewFromASCIIReadOnly("description")); 766 JSHandle<JSTaggedValue> getter(factory_->NewJSFunction(env, reinterpret_cast<void *>(Symbol::DescriptionGetter))); 767 SetGetter(symbolFuncPrototype, getterKey, getter); 768 769 // Setup symbol.prototype[@@toPrimitive] 770 SetFunctionAtSymbol<JSSymbol::SYMBOL_TO_PRIMITIVE_TYPE>( 771 env, symbolFuncPrototype, env->GetToPrimitiveSymbol(), "[Symbol.toPrimitive]", 772 Symbol::ToPrimitive, FunctionLength::ONE); 773 // install the Symbol.prototype methods 774 SetFunction(env, symbolFuncPrototype, thread_->GlobalConstants()->GetHandledToStringString(), Symbol::ToString, 775 FunctionLength::ZERO); 776 SetFunction(env, symbolFuncPrototype, thread_->GlobalConstants()->GetHandledValueOfString(), Symbol::ValueOf, 777 FunctionLength::ZERO); 778 779 // Setup %SymbolPrototype% 780 SetStringTagSymbol(env, symbolFuncPrototype, "Symbol"); 781 782 JSHandle<JSTaggedValue> holeySymbol(factory_->NewPrivateNameSymbolWithChar("holey")); 783 env->SetHoleySymbol(thread_, holeySymbol.GetTaggedValue()); 784 JSHandle<JSTaggedValue> elementIcSymbol(factory_->NewPrivateNameSymbolWithChar("element-ic")); 785 env->SetElementICSymbol(thread_, elementIcSymbol.GetTaggedValue()); 786} 787 788void Builtins::InitializeSymbolWithRealm(const JSHandle<GlobalEnv> &realm, 789 const JSHandle<JSHClass> &objFuncInstanceHClass) 790{ 791 [[maybe_unused]] EcmaHandleScope scope(thread_); 792 JSHandle<GlobalEnv> env = vm_->GetGlobalEnv(); 793 // Symbol.prototype 794 JSHandle<JSObject> symbolFuncPrototype = factory_->NewJSObjectWithInit(objFuncInstanceHClass); 795 JSHandle<JSTaggedValue> symbolFuncPrototypeValue(symbolFuncPrototype); 796 797 // Symbol.prototype_or_hclass 798 JSHandle<JSHClass> symbolFuncInstanceHClass = 799 factory_->NewEcmaHClass(JSPrimitiveRef::SIZE, JSType::JS_PRIMITIVE_REF, symbolFuncPrototypeValue); 800 801 // Symbol = new Function() 802 JSHandle<JSObject> symbolFunction( 803 NewBuiltinConstructor(realm, symbolFuncPrototype, Symbol::SymbolConstructor, "Symbol", FunctionLength::ZERO)); 804 JSFunction::SetFunctionPrototypeOrInstanceHClass(thread_, 805 JSHandle<JSFunction>(symbolFunction), 806 symbolFuncInstanceHClass.GetTaggedValue()); 807 808 // "constructor" property on the prototype 809 JSHandle<JSTaggedValue> constructorKey = thread_->GlobalConstants()->GetHandledConstructorString(); 810 PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>::Cast(symbolFunction), true, false, true); 811 JSObject::DefineOwnProperty(thread_, symbolFuncPrototype, constructorKey, descriptor); 812 813 for (const base::BuiltinFunctionEntry &entry: Symbol::GetSymbolFunctions()) { 814 SetFunction(realm, symbolFunction, entry.GetName(), entry.GetEntrypoint(), 815 entry.GetLength(), entry.GetBuiltinStubId()); 816 } 817 818#define BUILTIN_SYMBOL_CREATE_WITH_REALM(name, Name) \ 819 SetNoneAttributeProperty(symbolFunction, #name, env->Get##Name##Symbol()); \ 820 realm->Set##Name##Symbol(thread_, env->Get##Name##Symbol()); 821 822 realm->SetSymbolFunction(thread_, symbolFunction); 823 // Symbol attribute 824 BUILTIN_ALL_SYMBOLS(BUILTIN_SYMBOL_CREATE_WITH_REALM) 825 826 // symbol.prototype.description 827 PropertyDescriptor descriptionDesc(thread_); 828 JSHandle<JSTaggedValue> getterKey(factory_->NewFromASCIIReadOnly("description")); 829 JSHandle<JSTaggedValue> getter(factory_->NewJSFunction(realm, reinterpret_cast<void *>(Symbol::DescriptionGetter))); 830 SetGetter(symbolFuncPrototype, getterKey, getter); 831 832 // Setup symbol.prototype[@@toPrimitive] 833 SetFunctionAtSymbol<JSSymbol::SYMBOL_TO_PRIMITIVE_TYPE>(realm, symbolFuncPrototype, env->GetToPrimitiveSymbol(), 834 "[Symbol.toPrimitive]", Symbol::ToPrimitive, 835 FunctionLength::ONE); 836 // install the Symbol.prototype methods 837 for (const base::BuiltinFunctionEntry &entry: Symbol::GetSymbolPrototypeFunctions()) { 838 SetFunction(realm, symbolFuncPrototype, entry.GetName(), entry.GetEntrypoint(), 839 entry.GetLength(), entry.GetBuiltinStubId()); 840 } 841 // Setup %SymbolPrototype% 842 SetStringTagSymbol(realm, symbolFuncPrototype, "Symbol"); 843 844 JSHandle<JSTaggedValue> holeySymbol(factory_->NewPrivateNameSymbolWithChar("holey")); 845 realm->SetHoleySymbol(thread_, holeySymbol.GetTaggedValue()); 846 JSHandle<JSTaggedValue> elementIcSymbol(factory_->NewPrivateNameSymbolWithChar("element-ic")); 847 realm->SetElementICSymbol(thread_, elementIcSymbol.GetTaggedValue()); 848} 849#undef BUILTIN_SYMBOL_CREATE_WITH_REALM 850 851void Builtins::InitializeNumber(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &globalObject, 852 const JSHandle<JSHClass> &primRefObjHClass) 853{ 854 [[maybe_unused]] EcmaHandleScope scope(thread_); 855 // Number.prototype 856 JSHandle<JSTaggedValue> toObject(thread_, JSTaggedValue(FunctionLength::ZERO)); 857 JSHandle<JSObject> numFuncPrototype = 858 JSHandle<JSObject>::Cast(factory_->NewJSPrimitiveRef(primRefObjHClass, toObject)); 859 JSHandle<JSTaggedValue> numFuncPrototypeValue(numFuncPrototype); 860 861 // Number.prototype_or_hclass 862 JSHandle<JSHClass> numFuncInstanceHClass = 863 factory_->NewEcmaHClass(JSPrimitiveRef::SIZE, JSType::JS_PRIMITIVE_REF, numFuncPrototypeValue); 864 865 // Number = new Function() 866 JSHandle<JSObject> numFunction( 867 NewBuiltinConstructor(env, numFuncPrototype, Number::NumberConstructor, "Number", FunctionLength::ONE, 868 BUILTINS_STUB_ID(NumberConstructor))); 869 JSFunction::SetFunctionPrototypeOrInstanceHClass(thread_, 870 JSHandle<JSFunction>(numFunction), 871 numFuncInstanceHClass.GetTaggedValue()); 872 873 // Number.prototype method 874 for (const base::BuiltinFunctionEntry &entry: Number::GetNumberPrototypeFunctions()) { 875 SetFunction(env, numFuncPrototype, entry.GetName(), entry.GetEntrypoint(), 876 entry.GetLength(), entry.GetBuiltinStubId()); 877 } 878 // Number method 879 for (const base::BuiltinFunctionEntry &entry: Number::GetNumberNonGlobalFunctions()) { 880 SetFunction(env, numFunction, entry.GetName(), entry.GetEntrypoint(), 881 entry.GetLength(), entry.GetBuiltinStubId()); 882 } 883 for (const base::BuiltinFunctionEntry &entry: Number::GetNumberGlobalFunctions()) { 884 SetFuncToObjAndGlobal(env, globalObject, numFunction, 885 entry.GetName(), entry.GetEntrypoint(), entry.GetLength(), entry.GetBuiltinStubId()); 886 } 887 // Number constant 888 for (const base::BuiltinConstantEntry &entry: Number::GetNumberConstants()) { 889 SetConstant(numFunction, entry.GetName(), entry.GetTaggedValue()); 890 } 891 892 env->SetNumberFunction(thread_, numFunction); 893 env->SetNumberPrototype(thread_, numFuncPrototype); 894} 895void Builtins::InitializeBigIntWithRealm(const JSHandle<GlobalEnv> &realm) const 896{ 897 [[maybe_unused]] EcmaHandleScope scope(thread_); 898 JSHandle<GlobalEnv> env = vm_->GetGlobalEnv(); 899 realm->SetBigIntFunction(thread_, env->GetBigIntFunction()); 900 901 JSHandle<JSTaggedValue> nameString(factory_->NewFromASCIIReadOnly("BigInt")); 902 JSHandle<JSObject> globalObject(thread_, realm->GetGlobalObject()); 903 PropertyDescriptor descriptor(thread_, env->GetBigIntFunction(), true, false, true); 904 JSObject::DefineOwnProperty(thread_, globalObject, nameString, descriptor); 905} 906 907void Builtins::InitializeBigInt(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &primRefObjHClass) const 908{ 909 [[maybe_unused]] EcmaHandleScope scope(thread_); 910 // BigInt.prototype 911 JSHandle<JSObject> bigIntFuncPrototype = factory_->NewJSObjectWithInit(primRefObjHClass); 912 JSHandle<JSTaggedValue> bigIntFuncPrototypeValue(bigIntFuncPrototype); 913 914 // BigInt.prototype_or_hclass 915 JSHandle<JSHClass> bigIntFuncInstanceHClass = 916 factory_->NewEcmaHClass(JSPrimitiveRef::SIZE, JSType::JS_PRIMITIVE_REF, bigIntFuncPrototypeValue); 917 // BigInt = new Function() 918 JSHandle<JSObject> bigIntFunction( 919 NewBuiltinConstructor(env, bigIntFuncPrototype, BuiltinsBigInt::BigIntConstructor, "BigInt", 920 FunctionLength::ONE, kungfu::BuiltinsStubCSigns::BigIntConstructor)); 921 JSFunction::SetFunctionPrototypeOrInstanceHClass(thread_, 922 JSHandle<JSFunction>(bigIntFunction), 923 bigIntFuncInstanceHClass.GetTaggedValue()); 924 925 // BigInt.prototype method 926 for (const auto &entry : BuiltinsBigInt::GetBigIntPrototypeFunctions()) { 927 SetFunction(env, bigIntFuncPrototype, entry.GetName(), entry.GetEntrypoint(), 928 entry.GetLength(), entry.GetBuiltinStubId()); 929 } 930 931 // BigInt method 932 for (const auto &entry : BuiltinsBigInt::GetBigIntFunctions()) { 933 SetFunction(env, bigIntFunction, entry.GetName(), entry.GetEntrypoint(), 934 entry.GetLength(), entry.GetBuiltinStubId()); 935 } 936 937 // @@ToStringTag 938 SetStringTagSymbol(env, bigIntFuncPrototype, "BigInt"); 939 env->SetBigIntFunction(thread_, bigIntFunction); 940} 941 942void Builtins::InitializeDate(const JSHandle<GlobalEnv> &env, JSHandle<JSTaggedValue> objFuncPrototypeVal) const 943{ 944 [[maybe_unused]] EcmaHandleScope scope(thread_); 945 // Date.prototype 946 JSHandle<JSHClass> dateFuncPrototypeHClass = factory_->NewEcmaHClass( 947 JSObject::SIZE, Date::GetNumPrototypeInlinedProperties(), JSType::JS_OBJECT, objFuncPrototypeVal); 948 JSHandle<JSObject> dateFuncPrototype = factory_->NewJSObjectWithInit(dateFuncPrototypeHClass); 949 JSHandle<JSTaggedValue> dateFuncPrototypeValue(dateFuncPrototype); 950 951 // Date.prototype_or_hclass 952 JSHandle<JSHClass> dateFuncInstanceHClass = 953 factory_->NewEcmaHClass(JSDate::SIZE, JSType::JS_DATE, dateFuncPrototypeValue); 954 955 // Date = new Function() 956 JSHandle<JSObject> dateFunction( 957 NewBuiltinConstructor(env, dateFuncPrototype, Date::DateConstructor, "Date", FunctionLength::ONE, 958 BUILTINS_STUB_ID(DateConstructor))); 959 JSFunction::SetFunctionPrototypeOrInstanceHClass(thread_, 960 JSHandle<JSFunction>(dateFunction), 961 dateFuncInstanceHClass.GetTaggedValue()); 962 963 // Date.prototype method 964 for (const base::BuiltinFunctionEntry &entry: Date::GetDatePrototypeFunctions()) { 965 SetFunction(env, dateFuncPrototype, entry.GetName(), entry.GetEntrypoint(), 966 entry.GetLength(), entry.GetBuiltinStubId()); 967 } 968 SetFunctionAtSymbol(env, dateFuncPrototype, env->GetToPrimitiveSymbol(), "[Symbol.toPrimitive]", Date::ToPrimitive, 969 FunctionLength::ONE); 970 971 // Date method 972 for (const base::BuiltinFunctionEntry &entry: Date::GetDateFunctions()) { 973 SetFunction(env, dateFunction, entry.GetName(), entry.GetEntrypoint(), 974 entry.GetLength(), entry.GetBuiltinStubId()); 975 } 976 // Date.length 977 SetConstant(dateFunction, "length", JSTaggedValue(Date::UTC_LENGTH)); 978 979 env->SetDateFunction(thread_, dateFunction); 980 env->SetDatePrototype(thread_, dateFuncPrototype); 981 thread_->SetInitialBuiltinHClass(BuiltinTypeId::DATE, 982 dateFunction->GetJSHClass(), 983 *dateFuncInstanceHClass, 984 dateFuncPrototype->GetJSHClass()); 985} 986 987void Builtins::LazyInitializeDate(const JSHandle<GlobalEnv> &env) const 988{ 989 [[maybe_unused]] EcmaHandleScope scope(thread_); 990 JSHandle<JSObject> globalObject(thread_, env->GetGlobalObject()); 991 JSHandle<JSTaggedValue> key(factory_->NewFromUtf8ReadOnly("Date")); 992 auto accessor = factory_->NewInternalAccessor(nullptr, reinterpret_cast<void *>(BuiltinsLazyCallback::Date)); 993 SetLazyAccessor(globalObject, key, accessor); 994 env->SetDateFunction(thread_, accessor); 995 env->SetDatePrototype(thread_, accessor); 996} 997 998void Builtins::InitializeBoolean(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &primRefObjHClass) const 999{ 1000 [[maybe_unused]] EcmaHandleScope scope(thread_); 1001 // Boolean.prototype 1002 JSHandle<JSTaggedValue> toObject(thread_, JSTaggedValue::False()); 1003 JSHandle<JSObject> booleanFuncPrototype = 1004 JSHandle<JSObject>::Cast(factory_->NewJSPrimitiveRef(primRefObjHClass, toObject)); 1005 JSHandle<JSTaggedValue> booleanFuncPrototypeValue(booleanFuncPrototype); 1006 1007 // Boolean.prototype_or_hclass 1008 JSHandle<JSHClass> booleanFuncInstanceHClass = 1009 factory_->NewEcmaHClass(JSPrimitiveRef::SIZE, JSType::JS_PRIMITIVE_REF, booleanFuncPrototypeValue); 1010 1011 // new Boolean Function() 1012 JSHandle<JSFunction> booleanFunction = NewBuiltinConstructor(env, booleanFuncPrototype, Boolean::BooleanConstructor, 1013 "Boolean", FunctionLength::ONE, BUILTINS_STUB_ID(BooleanConstructor)); 1014 JSFunction::SetFunctionPrototypeOrInstanceHClass(thread_, booleanFunction, 1015 booleanFuncInstanceHClass.GetTaggedValue()); 1016 1017 // Boolean.prototype method 1018 SetFunction(env, booleanFuncPrototype, thread_->GlobalConstants()->GetHandledToStringString(), 1019 Boolean::BooleanPrototypeToString, FunctionLength::ZERO); 1020 SetFunction(env, booleanFuncPrototype, thread_->GlobalConstants()->GetHandledValueOfString(), 1021 Boolean::BooleanPrototypeValueOf, FunctionLength::ZERO); 1022 1023 env->SetBooleanFunction(thread_, booleanFunction); 1024 env->SetBooleanPrototype(thread_, booleanFuncPrototype); 1025} 1026 1027void Builtins::InitializeProxy(const JSHandle<GlobalEnv> &env) 1028{ 1029 // 2: The number of parameters is 2 1030 JSHandle<JSObject> proxyFunction(InitializeExoticConstructor(env, Proxy::ProxyConstructor, "Proxy", 2)); 1031 1032 // Proxy method 1033 SetFunction(env, proxyFunction, "revocable", Proxy::Revocable, FunctionLength::TWO); 1034 env->SetProxyFunction(thread_, proxyFunction); 1035} 1036 1037JSHandle<JSFunction> Builtins::InitializeExoticConstructor(const JSHandle<GlobalEnv> &env, EcmaEntrypoint ctorFunc, 1038 std::string_view name, int length) 1039{ 1040 JSHandle<JSFunction> ctor = 1041 factory_->NewJSFunction(env, reinterpret_cast<void *>(ctorFunc), FunctionKind::BUILTIN_PROXY_CONSTRUCTOR, 1042 BUILTINS_STUB_ID(ProxyConstructor)); 1043 1044 JSFunction::SetFunctionLength(thread_, ctor, JSTaggedValue(length)); 1045 JSHandle<JSTaggedValue> nameString(factory_->NewFromUtf8ReadOnly(name)); 1046 JSFunction::SetFunctionName(thread_, JSHandle<JSFunctionBase>(ctor), nameString, 1047 JSHandle<JSTaggedValue>(thread_, JSTaggedValue::Undefined())); 1048 1049 JSHandle<JSObject> globalObject(thread_, env->GetGlobalObject()); 1050 PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>(ctor), true, false, true); 1051 JSObject::DefineOwnProperty(thread_, globalObject, nameString, descriptor); 1052 return ctor; 1053} 1054 1055void Builtins::InitializeAsyncFunction(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncClass) const 1056{ 1057 [[maybe_unused]] EcmaHandleScope scope(thread_); 1058 const GlobalEnvConstants *globalConst = thread_->GlobalConstants(); 1059 // AsyncFunction.prototype 1060 JSHandle<JSObject> asyncFuncPrototype = factory_->NewJSObjectWithInit(objFuncClass); 1061 JSObject::SetPrototype(thread_, asyncFuncPrototype, env->GetFunctionPrototype()); 1062 JSHandle<JSTaggedValue> asyncFuncPrototypeValue(asyncFuncPrototype); 1063 1064 // AsyncFunction.prototype_or_hclass 1065 JSHandle<JSHClass> asyncFuncInstanceHClass = 1066 factory_->NewEcmaHClass(JSAsyncFunction::SIZE, JSType::JS_ASYNC_FUNCTION, asyncFuncPrototypeValue); 1067 1068 // AsyncFunction = new Function() 1069 JSHandle<JSFunction> asyncFunction = NewBuiltinConstructor( 1070 env, asyncFuncPrototype, AsyncFunction::AsyncFunctionConstructor, "AsyncFunction", FunctionLength::ONE); 1071 JSObject::SetPrototype(thread_, JSHandle<JSObject>::Cast(asyncFunction), env->GetFunctionFunction()); 1072 JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString(); 1073 PropertyDescriptor asyncDesc(thread_, JSHandle<JSTaggedValue>::Cast(asyncFunction), false, false, true); 1074 JSObject::DefineOwnProperty(thread_, asyncFuncPrototype, constructorKey, asyncDesc); 1075 asyncFunction->SetProtoOrHClass(thread_, asyncFuncInstanceHClass.GetTaggedValue()); 1076 1077 // AsyncFunction.prototype property 1078 SetStringTagSymbol(env, asyncFuncPrototype, "AsyncFunction"); 1079 env->SetAsyncFunction(thread_, asyncFunction); 1080 env->SetAsyncFunctionPrototype(thread_, asyncFuncPrototype); 1081} 1082 1083void Builtins::InitializeAllTypeError(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncClass) const 1084{ 1085 // Error.prototype 1086 JSHandle<JSObject> errorFuncPrototype = factory_->NewJSObjectWithInit(objFuncClass); 1087 JSHandle<JSTaggedValue> errorFuncPrototypeValue(errorFuncPrototype); 1088 // Error.prototype_or_hclass 1089 JSHandle<JSHClass> errorFuncInstanceHClass = 1090 factory_->NewEcmaHClass(JSObject::SIZE, JSType::JS_ERROR, errorFuncPrototypeValue); 1091 // Error() = new Function() 1092 JSHandle<JSFunction> errorFunction( 1093 NewBuiltinConstructor(env, errorFuncPrototype, Error::ErrorConstructor, "Error", FunctionLength::ONE)); 1094 JSFunction::SetFunctionPrototypeOrInstanceHClass(thread_, errorFunction, errorFuncInstanceHClass.GetTaggedValue()); 1095 1096 // Error.prototype method 1097 SetFunction(env, errorFuncPrototype, thread_->GlobalConstants()->GetHandledToStringString(), Error::ToString, 1098 FunctionLength::ZERO); 1099 1100 // Error.prototype Attribute 1101 SetAttribute(errorFuncPrototype, "name", "Error"); 1102 SetAttribute(errorFuncPrototype, "message", ""); 1103 env->SetErrorFunction(thread_, errorFunction); 1104 1105 JSHandle<JSHClass> nativeErrorFuncClass = 1106 factory_->NewEcmaHClass(JSFunction::SIZE, JSType::JS_FUNCTION, env->GetErrorFunction()); 1107 nativeErrorFuncClass->SetConstructor(true); 1108 env->SetNativeErrorFunctionClass(thread_, nativeErrorFuncClass); 1109 1110 JSHandle<JSHClass> errorNativeFuncInstanceHClass = 1111 factory_->NewEcmaHClass(JSObject::SIZE, JSType::JS_OBJECT, errorFuncPrototypeValue); 1112 InitializeError(env, errorNativeFuncInstanceHClass, JSType::JS_RANGE_ERROR); 1113 InitializeError(env, errorNativeFuncInstanceHClass, JSType::JS_REFERENCE_ERROR); 1114 InitializeError(env, errorNativeFuncInstanceHClass, JSType::JS_TYPE_ERROR); 1115 InitializeError(env, errorNativeFuncInstanceHClass, JSType::JS_AGGREGATE_ERROR); 1116 InitializeError(env, errorNativeFuncInstanceHClass, JSType::JS_URI_ERROR); 1117 InitializeError(env, errorNativeFuncInstanceHClass, JSType::JS_SYNTAX_ERROR); 1118 InitializeError(env, errorNativeFuncInstanceHClass, JSType::JS_EVAL_ERROR); 1119 InitializeError(env, errorNativeFuncInstanceHClass, JSType::JS_OOM_ERROR); 1120 InitializeError(env, errorNativeFuncInstanceHClass, JSType::JS_TERMINATION_ERROR); 1121 1122 JSHandle<EcmaString> handleMsg = factory_->NewFromUtf8ReadOnly("Default oom error"); 1123 JSHandle<JSObject> oomError = factory_->NewJSError(ErrorType::OOM_ERROR, handleMsg, StackCheck::YES); 1124 env->SetOOMErrorObject(thread_, oomError); 1125} 1126 1127void Builtins::InitializeAllTypeErrorWithRealm(const JSHandle<GlobalEnv> &realm) const 1128{ 1129 JSHandle<GlobalEnv> env = vm_->GetGlobalEnv(); 1130 1131 realm->SetErrorFunction(thread_, env->GetErrorFunction()); 1132 realm->SetNativeErrorFunctionClass(thread_, env->GetNativeErrorFunctionClass()); 1133 1134 SetErrorWithRealm(realm, JSType::JS_RANGE_ERROR); 1135 SetErrorWithRealm(realm, JSType::JS_REFERENCE_ERROR); 1136 SetErrorWithRealm(realm, JSType::JS_TYPE_ERROR); 1137 SetErrorWithRealm(realm, JSType::JS_AGGREGATE_ERROR); 1138 SetErrorWithRealm(realm, JSType::JS_URI_ERROR); 1139 SetErrorWithRealm(realm, JSType::JS_SYNTAX_ERROR); 1140 SetErrorWithRealm(realm, JSType::JS_EVAL_ERROR); 1141 SetErrorWithRealm(realm, JSType::JS_OOM_ERROR); 1142 SetErrorWithRealm(realm, JSType::JS_TERMINATION_ERROR); 1143} 1144 1145void Builtins::SetErrorWithRealm(const JSHandle<GlobalEnv> &realm, const JSType &errorTag) const 1146{ 1147 JSHandle<GlobalEnv> env = vm_->GetGlobalEnv(); 1148 JSHandle<JSObject> globalObject(thread_, realm->GetGlobalObject()); 1149 JSHandle<JSTaggedValue> nameString; 1150 JSHandle<JSTaggedValue> nativeErrorFunction; 1151 switch (errorTag) { 1152 case JSType::JS_RANGE_ERROR: 1153 nativeErrorFunction = env->GetRangeErrorFunction(); 1154 nameString = JSHandle<JSTaggedValue>(thread_->GlobalConstants()->GetHandledRangeErrorString()); 1155 realm->SetRangeErrorFunction(thread_, nativeErrorFunction); 1156 break; 1157 case JSType::JS_EVAL_ERROR: 1158 nativeErrorFunction = env->GetEvalErrorFunction(); 1159 nameString = JSHandle<JSTaggedValue>(thread_->GlobalConstants()->GetHandledEvalErrorString()); 1160 realm->SetEvalErrorFunction(thread_, nativeErrorFunction); 1161 break; 1162 case JSType::JS_REFERENCE_ERROR: 1163 nativeErrorFunction = env->GetReferenceErrorFunction(); 1164 nameString = JSHandle<JSTaggedValue>(thread_->GlobalConstants()->GetHandledReferenceErrorString()); 1165 realm->SetReferenceErrorFunction(thread_, nativeErrorFunction); 1166 break; 1167 case JSType::JS_TYPE_ERROR: 1168 nativeErrorFunction = env->GetTypeErrorFunction(); 1169 nameString = JSHandle<JSTaggedValue>(thread_->GlobalConstants()->GetHandledTypeErrorString()); 1170 realm->SetTypeErrorFunction(thread_, nativeErrorFunction); 1171 realm->SetThrowTypeError(thread_, env->GetThrowTypeError()); 1172 break; 1173 case JSType::JS_AGGREGATE_ERROR: 1174 nativeErrorFunction = env->GetAggregateErrorFunction(); 1175 nameString = JSHandle<JSTaggedValue>(thread_->GlobalConstants()->GetHandledAggregateErrorString()); 1176 realm->SetAggregateErrorFunction(thread_, nativeErrorFunction); 1177 break; 1178 case JSType::JS_URI_ERROR: 1179 nativeErrorFunction = env->GetURIErrorFunction(); 1180 nameString = JSHandle<JSTaggedValue>(thread_->GlobalConstants()->GetHandledURIErrorString()); 1181 realm->SetURIErrorFunction(thread_, nativeErrorFunction); 1182 break; 1183 case JSType::JS_SYNTAX_ERROR: 1184 nativeErrorFunction = env->GetSyntaxErrorFunction(); 1185 nameString = JSHandle<JSTaggedValue>(thread_->GlobalConstants()->GetHandledSyntaxErrorString()); 1186 realm->SetSyntaxErrorFunction(thread_, nativeErrorFunction); 1187 break; 1188 case JSType::JS_OOM_ERROR: 1189 nativeErrorFunction = env->GetOOMErrorFunction(); 1190 nameString = JSHandle<JSTaggedValue>(thread_->GlobalConstants()->GetHandledOOMErrorString()); 1191 realm->SetOOMErrorFunction(thread_, nativeErrorFunction); 1192 break; 1193 case JSType::JS_TERMINATION_ERROR: 1194 nativeErrorFunction = env->GetTerminationErrorFunction(); 1195 nameString = JSHandle<JSTaggedValue>(thread_->GlobalConstants()->GetHandledTerminationErrorString()); 1196 realm->SetTerminationErrorFunction(thread_, nativeErrorFunction); 1197 break; 1198 default: 1199 break; 1200 } 1201 PropertyDescriptor descriptor(thread_, nativeErrorFunction, true, false, true); 1202 JSObject::DefineOwnProperty(thread_, globalObject, nameString, descriptor); 1203} 1204 1205void Builtins::GeneralUpdateError(ErrorParameter *error, EcmaEntrypoint constructor, EcmaEntrypoint method, 1206 std::string_view name, JSType type) const 1207{ 1208 error->nativeConstructor = constructor; 1209 error->nativeMethod = method; 1210 error->nativePropertyName = name; 1211 error->nativeJstype = type; 1212} 1213 1214void Builtins::InitializeError(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncClass, 1215 const JSType &errorTag) const 1216{ 1217 // NativeError.prototype 1218 JSHandle<JSObject> nativeErrorFuncPrototype = factory_->NewJSObjectWithInit(objFuncClass); 1219 JSHandle<JSTaggedValue> nativeErrorFuncPrototypeValue(nativeErrorFuncPrototype); 1220 1221 ErrorParameter errorParameter{RangeError::RangeErrorConstructor, RangeError::ToString, "RangeError", 1222 JSType::JS_RANGE_ERROR}; 1223 switch (errorTag) { 1224 case JSType::JS_RANGE_ERROR: 1225 GeneralUpdateError(&errorParameter, RangeError::RangeErrorConstructor, RangeError::ToString, "RangeError", 1226 JSType::JS_RANGE_ERROR); 1227 break; 1228 case JSType::JS_EVAL_ERROR: 1229 GeneralUpdateError(&errorParameter, EvalError::EvalErrorConstructor, EvalError::ToString, "EvalError", 1230 JSType::JS_EVAL_ERROR); 1231 break; 1232 case JSType::JS_REFERENCE_ERROR: 1233 GeneralUpdateError(&errorParameter, ReferenceError::ReferenceErrorConstructor, ReferenceError::ToString, 1234 "ReferenceError", JSType::JS_REFERENCE_ERROR); 1235 break; 1236 case JSType::JS_TYPE_ERROR: 1237 GeneralUpdateError(&errorParameter, TypeError::TypeErrorConstructor, TypeError::ToString, "TypeError", 1238 JSType::JS_TYPE_ERROR); 1239 break; 1240 case JSType::JS_AGGREGATE_ERROR: 1241 GeneralUpdateError(&errorParameter, AggregateError::AggregateErrorConstructor, AggregateError::ToString, 1242 "AggregateError", JSType::JS_AGGREGATE_ERROR); 1243 break; 1244 case JSType::JS_URI_ERROR: 1245 GeneralUpdateError(&errorParameter, URIError::URIErrorConstructor, URIError::ToString, "URIError", 1246 JSType::JS_URI_ERROR); 1247 break; 1248 case JSType::JS_SYNTAX_ERROR: 1249 GeneralUpdateError(&errorParameter, SyntaxError::SyntaxErrorConstructor, SyntaxError::ToString, 1250 "SyntaxError", JSType::JS_SYNTAX_ERROR); 1251 break; 1252 case JSType::JS_OOM_ERROR: 1253 GeneralUpdateError(&errorParameter, OOMError::OOMErrorConstructor, OOMError::ToString, 1254 "OutOfMemoryError", JSType::JS_OOM_ERROR); 1255 break; 1256 case JSType::JS_TERMINATION_ERROR: 1257 GeneralUpdateError(&errorParameter, TerminationError::TerminationErrorConstructor, 1258 TerminationError::ToString, "TerminationError", JSType::JS_TERMINATION_ERROR); 1259 break; 1260 default: 1261 break; 1262 } 1263 1264 // NativeError.prototype_or_hclass 1265 JSHandle<JSHClass> nativeErrorFuncInstanceHClass = 1266 factory_->NewEcmaHClass(JSObject::SIZE, errorParameter.nativeJstype, nativeErrorFuncPrototypeValue); 1267 1268 // NativeError() = new Error() 1269 FunctionLength functionLength = FunctionLength::ONE; 1270 if (errorTag == JSType::JS_AGGREGATE_ERROR) { 1271 functionLength = FunctionLength::TWO; 1272 } 1273 JSHandle<JSFunction> nativeErrorFunction = 1274 factory_->NewJSNativeErrorFunction(env, reinterpret_cast<void *>(errorParameter.nativeConstructor)); 1275 InitializeCtor(env, nativeErrorFuncPrototype, nativeErrorFunction, errorParameter.nativePropertyName, 1276 functionLength); 1277 1278 JSFunction::SetFunctionPrototypeOrInstanceHClass(thread_, nativeErrorFunction, 1279 nativeErrorFuncInstanceHClass.GetTaggedValue()); 1280 1281 // NativeError.prototype method 1282 SetFunction(env, nativeErrorFuncPrototype, thread_->GlobalConstants()->GetHandledToStringString(), 1283 errorParameter.nativeMethod, FunctionLength::ZERO); 1284 1285 // Error.prototype Attribute 1286 SetAttribute(nativeErrorFuncPrototype, "name", errorParameter.nativePropertyName); 1287 SetAttribute(nativeErrorFuncPrototype, "message", ""); 1288 1289 if (errorTag == JSType::JS_RANGE_ERROR) { 1290 env->SetRangeErrorFunction(thread_, nativeErrorFunction); 1291 } else if (errorTag == JSType::JS_REFERENCE_ERROR) { 1292 env->SetReferenceErrorFunction(thread_, nativeErrorFunction); 1293 } else if (errorTag == JSType::JS_TYPE_ERROR) { 1294 env->SetTypeErrorFunction(thread_, nativeErrorFunction); 1295 JSHandle<JSFunction> throwTypeErrorFunction = 1296 factory_->NewJSFunction(env, reinterpret_cast<void *>(TypeError::ThrowTypeError)); 1297 JSFunction::SetFunctionLength(thread_, throwTypeErrorFunction, JSTaggedValue(1), false); 1298 JSObject::PreventExtensions(thread_, JSHandle<JSObject>::Cast(throwTypeErrorFunction)); 1299 env->SetThrowTypeError(thread_, throwTypeErrorFunction); 1300 } else if (errorTag == JSType::JS_AGGREGATE_ERROR) { 1301 env->SetAggregateErrorFunction(thread_, nativeErrorFunction); 1302 } else if (errorTag == JSType::JS_URI_ERROR) { 1303 env->SetURIErrorFunction(thread_, nativeErrorFunction); 1304 } else if (errorTag == JSType::JS_SYNTAX_ERROR) { 1305 env->SetSyntaxErrorFunction(thread_, nativeErrorFunction); 1306 } else if (errorTag == JSType::JS_EVAL_ERROR) { 1307 env->SetEvalErrorFunction(thread_, nativeErrorFunction); 1308 } else if (errorTag == JSType::JS_OOM_ERROR) { 1309 env->SetOOMErrorFunction(thread_, nativeErrorFunction); 1310 } else { 1311 env->SetTerminationErrorFunction(thread_, nativeErrorFunction); 1312 } 1313} // namespace panda::ecmascript 1314 1315void Builtins::InitializeCtor(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &prototype, 1316 const JSHandle<JSFunction> &ctor, std::string_view name, int length) const 1317{ 1318 const GlobalEnvConstants *globalConst = thread_->GlobalConstants(); 1319 JSFunction::SetFunctionLength(thread_, ctor, JSTaggedValue(length)); 1320 JSHandle<JSTaggedValue> nameString(factory_->NewFromUtf8ReadOnly(name)); 1321 JSFunction::SetFunctionName(thread_, JSHandle<JSFunctionBase>(ctor), nameString, 1322 JSHandle<JSTaggedValue>(thread_, JSTaggedValue::Undefined())); 1323 JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString(); 1324 PropertyDescriptor descriptor1(thread_, JSHandle<JSTaggedValue>::Cast(ctor), true, false, true); 1325 JSObject::DefineOwnProperty(thread_, prototype, constructorKey, descriptor1); 1326 1327 /* set "prototype" in constructor */ 1328 JSFunction::SetFunctionPrototypeOrInstanceHClass(thread_, ctor, prototype.GetTaggedValue()); 1329 1330 if (!JSTaggedValue::SameValue(nameString, thread_->GlobalConstants()->GetHandledAsyncFunctionString())) { 1331 JSHandle<JSObject> globalObject(thread_, env->GetGlobalObject()); 1332 PropertyDescriptor descriptor2(thread_, JSHandle<JSTaggedValue>::Cast(ctor), true, false, true); 1333 JSObject::DefineOwnProperty(thread_, globalObject, nameString, descriptor2); 1334 } 1335} 1336 1337void Builtins::InitializeSet(const JSHandle<GlobalEnv> &env, JSHandle<JSTaggedValue> objFuncPrototypeVal) const 1338{ 1339 [[maybe_unused]] EcmaHandleScope scope(thread_); 1340 const GlobalEnvConstants *globalConst = thread_->GlobalConstants(); 1341 // Set.prototype 1342 JSHandle<JSHClass> setFuncPrototypeHClass = factory_->NewEcmaHClass( 1343 JSObject::SIZE, BuiltinsSet::GetNumPrototypeInlinedProperties(), JSType::JS_OBJECT, objFuncPrototypeVal); 1344 JSHandle<JSObject> setFuncPrototype = factory_->NewJSObjectWithInit(setFuncPrototypeHClass); 1345 JSHandle<JSTaggedValue> setFuncPrototypeValue(setFuncPrototype); 1346 // Set.prototype_or_hclass 1347 JSHandle<JSHClass> setFuncInstanceHClass = 1348 factory_->NewEcmaHClass(JSSet::SIZE, JSType::JS_SET, setFuncPrototypeValue); 1349 // Set() = new Function() 1350 JSHandle<JSTaggedValue> setFunction( 1351 NewBuiltinConstructor(env, setFuncPrototype, BuiltinsSet::SetConstructor, "Set", FunctionLength::ZERO, 1352 BUILTINS_STUB_ID(SetConstructor))); 1353 JSFunction::SetFunctionPrototypeOrInstanceHClass(thread_, 1354 JSHandle<JSFunction>(setFunction), 1355 setFuncInstanceHClass.GetTaggedValue()); 1356 1357 // "constructor" property on the prototype 1358 JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString(); 1359 JSObject::SetProperty(thread_, JSHandle<JSTaggedValue>(setFuncPrototype), constructorKey, setFunction); 1360 RETURN_IF_ABRUPT_COMPLETION(thread_); 1361 // Set.prototype functions, excluding keys() 1362 for (const base::BuiltinFunctionEntry &entry: BuiltinsSet::GetSetPrototypeFunctions()) { 1363 SetFunction(env, setFuncPrototype, entry.GetName(), entry.GetEntrypoint(), 1364 entry.GetLength(), entry.GetBuiltinStubId()); 1365 } 1366 // Set.prototype.keys, which is strictly equal to Set.prototype.values 1367 JSHandle<JSTaggedValue> keys(factory_->NewFromASCIIReadOnly("keys")); 1368 JSHandle<JSTaggedValue> values(factory_->NewFromASCIIReadOnly("values")); 1369 JSHandle<JSTaggedValue> valuesFunc = 1370 JSObject::GetMethod(thread_, JSHandle<JSTaggedValue>::Cast(setFuncPrototype), values); 1371 RETURN_IF_ABRUPT_COMPLETION(thread_); 1372 PropertyDescriptor descriptor(thread_, valuesFunc, true, false, true); 1373 JSObject::DefineOwnProperty(thread_, setFuncPrototype, keys, descriptor); 1374 1375 // @@ToStringTag 1376 SetStringTagSymbol(env, setFuncPrototype, "Set"); 1377 1378 // 23.1.3.10get Set.prototype.size 1379 JSHandle<JSTaggedValue> sizeGetter = CreateGetter(env, BuiltinsSet::GetSize, "size", FunctionLength::ZERO); 1380 JSHandle<JSTaggedValue> sizeKey(factory_->NewFromASCIIReadOnly("size")); 1381 SetGetter(setFuncPrototype, sizeKey, sizeGetter); 1382 1383 // 23.1.2.2get Set [ @@species ] 1384 JSHandle<JSTaggedValue> speciesSymbol = env->GetSpeciesSymbol(); 1385 JSHandle<JSTaggedValue> speciesGetter = 1386 CreateGetter(env, BuiltinsSet::Species, "[Symbol.species]", FunctionLength::ZERO); 1387 SetGetter(JSHandle<JSObject>(setFunction), speciesSymbol, speciesGetter); 1388 1389 // %SetPrototype% [ @@iterator ] 1390 JSHandle<JSTaggedValue> iteratorSymbol = env->GetIteratorSymbol(); 1391 JSObject::DefineOwnProperty(thread_, setFuncPrototype, iteratorSymbol, descriptor); 1392 1393 env->SetBuiltinsSetFunction(thread_, setFunction); 1394 env->SetSetPrototype(thread_, setFuncPrototype); 1395 env->SetSetProtoValuesFunction(thread_, valuesFunc); 1396 thread_->SetInitialBuiltinHClass(BuiltinTypeId::SET, 1397 setFunction->GetTaggedObject()->GetClass(), 1398 *setFuncInstanceHClass, 1399 setFuncPrototype->GetJSHClass()); 1400} 1401 1402void Builtins::LazyInitializeSet(const JSHandle<GlobalEnv> &env) 1403{ 1404 [[maybe_unused]] EcmaHandleScope scope(thread_); 1405 JSHandle<JSObject> globalObject(thread_, env->GetGlobalObject()); 1406 JSHandle<JSTaggedValue> key(factory_->NewFromUtf8ReadOnly("Set")); 1407 auto accessor = factory_->NewInternalAccessor(nullptr, reinterpret_cast<void *>(BuiltinsLazyCallback::Set)); 1408 SetLazyAccessor(globalObject, key, accessor); 1409 env->SetBuiltinsSetFunction(thread_, accessor); 1410 env->SetSetPrototype(thread_, accessor); 1411 env->SetSetProtoValuesFunction(thread_, accessor); 1412} 1413 1414void Builtins::InitializeMap(const JSHandle<GlobalEnv> &env, JSHandle<JSTaggedValue> objFuncPrototypeVal) const 1415{ 1416 [[maybe_unused]] EcmaHandleScope scope(thread_); 1417 const GlobalEnvConstants *globalConst = thread_->GlobalConstants(); 1418 // Map.prototype 1419 JSHandle<JSHClass> mapFuncPrototypeHClass = factory_->NewEcmaHClass( 1420 JSObject::SIZE, BuiltinsMap::GetNumPrototypeInlinedProperties(), JSType::JS_OBJECT, objFuncPrototypeVal); 1421 JSHandle<JSObject> mapFuncPrototype = factory_->NewJSObjectWithInit(mapFuncPrototypeHClass); 1422 JSHandle<JSTaggedValue> mapFuncPrototypeValue(mapFuncPrototype); 1423 // Map.prototype_or_hclass 1424 JSHandle<JSHClass> mapFuncInstanceHClass = 1425 factory_->NewEcmaHClass(JSMap::SIZE, JSType::JS_MAP, mapFuncPrototypeValue); 1426 env->SetMapClass(thread_, mapFuncInstanceHClass); 1427 1428 // Map() = new Function() 1429 JSHandle<JSTaggedValue> mapFunction( 1430 NewBuiltinConstructor(env, mapFuncPrototype, BuiltinsMap::MapConstructor, "Map", FunctionLength::ZERO, 1431 BUILTINS_STUB_ID(MapConstructor))); 1432 // Map().prototype = Map.Prototype & Map.prototype.constructor = Map() 1433 JSFunction::SetFunctionPrototypeOrInstanceHClass(thread_, 1434 JSHandle<JSFunction>(mapFunction), 1435 mapFuncInstanceHClass.GetTaggedValue()); 1436 1437 // "constructor" property on the prototype 1438 JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString(); 1439 JSObject::SetProperty(thread_, JSHandle<JSTaggedValue>(mapFuncPrototype), constructorKey, mapFunction); 1440 RETURN_IF_ABRUPT_COMPLETION(thread_); 1441 // Map.prototype functions 1442 for (const base::BuiltinFunctionEntry &entry: BuiltinsMap::GetMapPrototypeFunctions()) { 1443 SetFunction(env, mapFuncPrototype, entry.GetName(), entry.GetEntrypoint(), 1444 entry.GetLength(), entry.GetBuiltinStubId()); 1445 } 1446 // @@ToStringTag 1447 SetStringTagSymbol(env, mapFuncPrototype, "Map"); 1448 1449 // 23.1.3.10get Map.prototype.size 1450 JSHandle<JSTaggedValue> sizeGetter = CreateGetter(env, BuiltinsMap::GetSize, "size", FunctionLength::ZERO); 1451 JSHandle<JSTaggedValue> sizeKey(factory_->NewFromASCIIReadOnly("size")); 1452 SetGetter(mapFuncPrototype, sizeKey, sizeGetter); 1453 1454 // 23.1.2.2get Map [ @@species ] 1455 JSHandle<JSTaggedValue> speciesSymbol = env->GetSpeciesSymbol(); 1456 JSHandle<JSTaggedValue> speciesGetter = 1457 CreateGetter(env, BuiltinsMap::Species, "[Symbol.species]", FunctionLength::ZERO); 1458 SetGetter(JSHandle<JSObject>(mapFunction), speciesSymbol, speciesGetter); 1459 1460 // %MapPrototype% [ @@iterator ] 1461 JSHandle<JSTaggedValue> iteratorSymbol = env->GetIteratorSymbol(); 1462 JSHandle<JSTaggedValue> entries(factory_->NewFromASCIIReadOnly("entries")); 1463 JSHandle<JSTaggedValue> entriesFunc = 1464 JSObject::GetMethod(thread_, JSHandle<JSTaggedValue>::Cast(mapFuncPrototype), entries); 1465 RETURN_IF_ABRUPT_COMPLETION(thread_); 1466 PropertyDescriptor descriptor(thread_, entriesFunc, true, false, true); 1467 JSObject::DefineOwnProperty(thread_, mapFuncPrototype, iteratorSymbol, descriptor); 1468 1469 env->SetBuiltinsMapFunction(thread_, mapFunction); 1470 env->SetMapPrototype(thread_, mapFuncPrototype); 1471 env->SetMapProtoEntriesFunction(thread_, entriesFunc); 1472 thread_->SetInitialBuiltinHClass(BuiltinTypeId::MAP, 1473 mapFunction->GetTaggedObject()->GetClass(), 1474 *mapFuncInstanceHClass, 1475 mapFuncPrototype->GetJSHClass()); 1476} 1477 1478void Builtins::LazyInitializeMap(const JSHandle<GlobalEnv> &env) const 1479{ 1480 [[maybe_unused]] EcmaHandleScope scope(thread_); 1481 JSHandle<JSObject> globalObject(thread_, env->GetGlobalObject()); 1482 JSHandle<JSTaggedValue> key(factory_->NewFromUtf8ReadOnly("Map")); 1483 auto accessor = factory_->NewInternalAccessor(nullptr, reinterpret_cast<void *>(BuiltinsLazyCallback::Map)); 1484 SetLazyAccessor(globalObject, key, accessor); 1485 env->SetBuiltinsMapFunction(thread_, accessor); 1486 env->SetMapPrototype(thread_, accessor); 1487 env->SetMapProtoEntriesFunction(thread_, accessor); 1488 env->SetMapClass(thread_, accessor); 1489} 1490 1491void Builtins::InitializeWeakMap(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncClass) const 1492{ 1493 [[maybe_unused]] EcmaHandleScope scope(thread_); 1494 const GlobalEnvConstants *globalConst = thread_->GlobalConstants(); 1495 // WeakMap.prototype 1496 JSHandle<JSObject> weakMapFuncPrototype = factory_->NewJSObjectWithInit(objFuncClass); 1497 JSHandle<JSTaggedValue> weakMapFuncPrototypeValue(weakMapFuncPrototype); 1498 // WeakMap.prototype_or_hclass 1499 JSHandle<JSHClass> weakMapFuncInstanceHClass = 1500 factory_->NewEcmaHClass(JSWeakMap::SIZE, JSType::JS_WEAK_MAP, weakMapFuncPrototypeValue); 1501 // WeakMap() = new Function() 1502 JSHandle<JSTaggedValue> weakMapFunction(NewBuiltinConstructor( 1503 env, weakMapFuncPrototype, BuiltinsWeakMap::WeakMapConstructor, "WeakMap", FunctionLength::ZERO)); 1504 // WeakMap().prototype = WeakMap.Prototype & WeakMap.prototype.constructor = WeakMap() 1505 JSFunction::Cast(weakMapFunction->GetTaggedObject()) 1506 ->SetProtoOrHClass(thread_, weakMapFuncInstanceHClass.GetTaggedValue()); 1507 1508 // "constructor" property on the prototype 1509 JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString(); 1510 JSObject::SetProperty(thread_, JSHandle<JSTaggedValue>(weakMapFuncPrototype), constructorKey, weakMapFunction); 1511 RETURN_IF_ABRUPT_COMPLETION(thread_); 1512 // weakmap.prototype.set() 1513 SetFunction(env, weakMapFuncPrototype, globalConst->GetHandledSetString(), BuiltinsWeakMap::Set, 1514 FunctionLength::TWO); 1515 // weakmap.prototype.delete() 1516 SetFunction(env, weakMapFuncPrototype, "delete", BuiltinsWeakMap::Delete, FunctionLength::ONE); 1517 // weakmap.prototype.has() 1518 SetFunction(env, weakMapFuncPrototype, "has", BuiltinsWeakMap::Has, FunctionLength::ONE); 1519 // weakmap.prototype.get() 1520 SetFunction(env, weakMapFuncPrototype, thread_->GlobalConstants()->GetHandledGetString(), BuiltinsWeakMap::Get, 1521 FunctionLength::ONE); 1522 // @@ToStringTag 1523 SetStringTagSymbol(env, weakMapFuncPrototype, "WeakMap"); 1524 1525 env->SetBuiltinsWeakMapFunction(thread_, weakMapFunction); 1526} 1527 1528void Builtins::LazyInitializeWeakMap(const JSHandle<GlobalEnv> &env) const 1529{ 1530 [[maybe_unused]] EcmaHandleScope scope(thread_); 1531 JSHandle<JSObject> globalObject(thread_, env->GetGlobalObject()); 1532 JSHandle<JSTaggedValue> key(factory_->NewFromUtf8ReadOnly("WeakMap")); 1533 auto accessor = factory_->NewInternalAccessor(nullptr, reinterpret_cast<void *>(BuiltinsLazyCallback::WeakMap)); 1534 SetLazyAccessor(globalObject, key, accessor); 1535 env->SetBuiltinsWeakMapFunction(thread_, accessor); 1536} 1537 1538void Builtins::InitializeWeakSet(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncClass) const 1539{ 1540 [[maybe_unused]] EcmaHandleScope scope(thread_); 1541 const GlobalEnvConstants *globalConst = thread_->GlobalConstants(); 1542 // Set.prototype 1543 JSHandle<JSObject> weakSetFuncPrototype = factory_->NewJSObjectWithInit(objFuncClass); 1544 JSHandle<JSTaggedValue> weakSetFuncPrototypeValue(weakSetFuncPrototype); 1545 // Set.prototype_or_hclass 1546 JSHandle<JSHClass> weakSetFuncInstanceHClass = 1547 factory_->NewEcmaHClass(JSWeakSet::SIZE, JSType::JS_WEAK_SET, weakSetFuncPrototypeValue); 1548 // Set() = new Function() 1549 JSHandle<JSTaggedValue> weakSetFunction(NewBuiltinConstructor( 1550 env, weakSetFuncPrototype, BuiltinsWeakSet::WeakSetConstructor, "WeakSet", FunctionLength::ZERO)); 1551 JSHandle<JSFunction>(weakSetFunction)->SetProtoOrHClass(thread_, weakSetFuncInstanceHClass.GetTaggedValue()); 1552 1553 // "constructor" property on the prototype 1554 JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString(); 1555 JSObject::SetProperty(thread_, JSHandle<JSTaggedValue>(weakSetFuncPrototype), constructorKey, weakSetFunction); 1556 RETURN_IF_ABRUPT_COMPLETION(thread_); 1557 // set.prototype.add() 1558 SetFunction(env, weakSetFuncPrototype, "add", BuiltinsWeakSet::Add, FunctionLength::ONE); 1559 // set.prototype.delete() 1560 SetFunction(env, weakSetFuncPrototype, "delete", BuiltinsWeakSet::Delete, FunctionLength::ONE); 1561 // set.prototype.has() 1562 SetFunction(env, weakSetFuncPrototype, "has", BuiltinsWeakSet::Has, FunctionLength::ONE); 1563 1564 // @@ToStringTag 1565 SetStringTagSymbol(env, weakSetFuncPrototype, "WeakSet"); 1566 1567 env->SetBuiltinsWeakSetFunction(thread_, weakSetFunction); 1568} 1569 1570void Builtins::LazyInitializeWeakSet(const JSHandle<GlobalEnv> &env) const 1571{ 1572 [[maybe_unused]] EcmaHandleScope scope(thread_); 1573 JSHandle<JSObject> globalObject(thread_, env->GetGlobalObject()); 1574 JSHandle<JSTaggedValue> key(factory_->NewFromUtf8ReadOnly("WeakSet")); 1575 auto accessor = factory_->NewInternalAccessor(nullptr, reinterpret_cast<void *>(BuiltinsLazyCallback::WeakSet)); 1576 SetLazyAccessor(globalObject, key, accessor); 1577 env->SetBuiltinsWeakSetFunction(thread_, accessor); 1578} 1579 1580void Builtins::InitializeAtomics(const JSHandle<GlobalEnv> &env, 1581 const JSHandle<JSTaggedValue> &objFuncPrototypeVal) const 1582{ 1583 [[maybe_unused]] EcmaHandleScope scope(thread_); 1584 JSHandle<JSHClass> atomicsHClass = factory_->NewEcmaHClass(JSObject::SIZE, JSType::JS_OBJECT, 1585 objFuncPrototypeVal); 1586 JSHandle<JSObject> atomicsObject = factory_->NewJSObject(atomicsHClass); 1587 // Atomics functions 1588 for (const base::BuiltinFunctionEntry &entry: Atomics::GetAtomicsFunctions()) { 1589 SetFunction(env, atomicsObject, entry.GetName(), entry.GetEntrypoint(), 1590 entry.GetLength(), entry.GetBuiltinStubId()); 1591 } 1592 JSHandle<JSTaggedValue> atomicsString(factory_->NewFromASCIIReadOnly("Atomics")); 1593 JSHandle<JSObject> globalObject(thread_, env->GetGlobalObject()); 1594 PropertyDescriptor atomicsDesc(thread_, JSHandle<JSTaggedValue>::Cast(atomicsObject), true, false, true); 1595 JSObject::DefineOwnProperty(thread_, globalObject, atomicsString, atomicsDesc); 1596 // @@ToStringTag 1597 SetStringTagSymbol(env, atomicsObject, "Atomics"); 1598 env->SetAtomicsFunction(thread_, atomicsObject); 1599} 1600 1601void Builtins::InitializeWeakRef(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncClass) const 1602{ 1603 [[maybe_unused]] EcmaHandleScope scope(thread_); 1604 const GlobalEnvConstants *globalConst = thread_->GlobalConstants(); 1605 // WeakRef.prototype 1606 JSHandle<JSObject> weakRefFuncPrototype = factory_->NewJSObject(objFuncClass); 1607 JSHandle<JSTaggedValue> weakRefFuncPrototypeValue(weakRefFuncPrototype); 1608 // WeakRef.prototype_or_hclass 1609 JSHandle<JSHClass> weakRefFuncInstanceHClass = 1610 factory_->NewEcmaHClass(JSWeakRef::SIZE, JSType::JS_WEAK_REF, weakRefFuncPrototypeValue); 1611 // WeakRef() = new Function() 1612 JSHandle<JSTaggedValue> weakRefFunction(NewBuiltinConstructor( 1613 env, weakRefFuncPrototype, BuiltinsWeakRef::WeakRefConstructor, "WeakRef", FunctionLength::ONE)); 1614 JSHandle<JSFunction>(weakRefFunction)->SetProtoOrHClass(thread_, weakRefFuncInstanceHClass.GetTaggedValue()); 1615 1616 // "constructor" property on the prototype 1617 JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString(); 1618 JSObject::SetProperty(thread_, JSHandle<JSTaggedValue>(weakRefFuncPrototype), constructorKey, weakRefFunction); 1619 RETURN_IF_ABRUPT_COMPLETION(thread_); 1620 // WeakRef.prototype.deref() 1621 SetFunction(env, weakRefFuncPrototype, "deref", BuiltinsWeakRef::Deref, FunctionLength::ZERO); 1622 1623 // @@ToStringTag 1624 SetStringTagSymbol(env, weakRefFuncPrototype, "WeakRef"); 1625 1626 env->SetBuiltinsWeakRefFunction(thread_, weakRefFunction); 1627} 1628 1629void Builtins::LazyInitializeWeakRef(const JSHandle<GlobalEnv> &env) const 1630{ 1631 [[maybe_unused]] EcmaHandleScope scope(thread_); 1632 JSHandle<JSObject> globalObject(thread_, env->GetGlobalObject()); 1633 JSHandle<JSTaggedValue> key(factory_->NewFromUtf8ReadOnly("WeakRef")); 1634 auto accessor = factory_->NewInternalAccessor(nullptr, reinterpret_cast<void *>(BuiltinsLazyCallback::WeakRef)); 1635 SetLazyAccessor(globalObject, key, accessor); 1636 env->SetBuiltinsWeakRefFunction(thread_, accessor); 1637} 1638 1639void Builtins::InitializeFinalizationRegistry(const JSHandle<GlobalEnv> &env, 1640 const JSHandle<JSHClass> &objFuncClass) const 1641{ 1642 [[maybe_unused]] EcmaHandleScope scope(thread_); 1643 const GlobalEnvConstants *globalConst = thread_->GlobalConstants(); 1644 // FinalizationRegistry.prototype 1645 JSHandle<JSObject> finalizationRegistryFuncPrototype = factory_->NewJSObject(objFuncClass); 1646 JSHandle<JSTaggedValue> finalizationRegistryFuncPrototypeValue(finalizationRegistryFuncPrototype); 1647 // FinalizationRegistry.prototype_or_hclass 1648 JSHandle<JSHClass> finalizationRegistryFuncInstanceHClass = 1649 factory_->NewEcmaHClass(JSFinalizationRegistry::SIZE, JSType::JS_FINALIZATION_REGISTRY, 1650 finalizationRegistryFuncPrototypeValue); 1651 // FinalizationRegistry() = new Function() 1652 JSHandle<JSTaggedValue> finalizationRegistryFunction(NewBuiltinConstructor( 1653 env, finalizationRegistryFuncPrototype, BuiltinsFinalizationRegistry::FinalizationRegistryConstructor, 1654 "FinalizationRegistry", FunctionLength::ONE)); 1655 JSHandle<JSFunction>(finalizationRegistryFunction)->SetProtoOrHClass( 1656 thread_, finalizationRegistryFuncInstanceHClass.GetTaggedValue()); 1657 1658 // "constructor" property on the prototype 1659 JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString(); 1660 JSObject::SetProperty(thread_, JSHandle<JSTaggedValue>(finalizationRegistryFuncPrototype), 1661 constructorKey, finalizationRegistryFunction); 1662 RETURN_IF_ABRUPT_COMPLETION(thread_); 1663 // FinalizationRegistry.prototype.deref() 1664 SetFunction(env, finalizationRegistryFuncPrototype, "register", 1665 BuiltinsFinalizationRegistry::Register, FunctionLength::TWO); 1666 SetFunction(env, finalizationRegistryFuncPrototype, "unregister", 1667 BuiltinsFinalizationRegistry::Unregister, FunctionLength::ONE); 1668 // @@ToStringTag 1669 SetStringTagSymbol(env, finalizationRegistryFuncPrototype, "FinalizationRegistry"); 1670 1671 env->SetBuiltinsFinalizationRegistryFunction(thread_, finalizationRegistryFunction); 1672} 1673 1674void Builtins::LazyInitializeFinalizationRegistry(const JSHandle<GlobalEnv> &env) const 1675{ 1676 [[maybe_unused]] EcmaHandleScope scope(thread_); 1677 JSHandle<JSObject> globalObject(thread_, env->GetGlobalObject()); 1678 JSHandle<JSTaggedValue> key(factory_->NewFromUtf8ReadOnly("FinalizationRegistry")); 1679 auto accessor = factory_->NewInternalAccessor(nullptr, 1680 reinterpret_cast<void *>(BuiltinsLazyCallback::FinalizationRegistry)); 1681 SetLazyAccessor(globalObject, key, accessor); 1682 env->SetBuiltinsFinalizationRegistryFunction(thread_, accessor); 1683} 1684 1685void Builtins::InitializeMath(const JSHandle<GlobalEnv> &env, const JSHandle<JSTaggedValue> &objFuncPrototypeVal) const 1686{ 1687 [[maybe_unused]] EcmaHandleScope scope(thread_); 1688 JSHandle<JSHClass> mathClass = factory_->NewEcmaHClass(JSObject::SIZE, JSType::JS_OBJECT, objFuncPrototypeVal); 1689 JSHandle<JSObject> mathObject = factory_->NewJSObjectWithInit(mathClass); 1690 RandomGenerator::InitRandom(thread_); 1691 1692 for (const base::BuiltinFunctionEntry &entry: Math::GetMathFunctions()) { 1693 SetFunction(env, mathObject, entry.GetName(), entry.GetEntrypoint(), 1694 entry.GetLength(), entry.GetBuiltinStubId()); 1695 } 1696 for (const base::BuiltinConstantEntry &entry: Math::GetMathConstants()) { 1697 SetConstant(mathObject, entry.GetName(), entry.GetTaggedValue()); 1698 } 1699 JSHandle<JSTaggedValue> mathString(factory_->NewFromASCIIReadOnly("Math")); 1700 JSHandle<JSObject> globalObject(thread_, env->GetGlobalObject()); 1701 PropertyDescriptor mathDesc(thread_, JSHandle<JSTaggedValue>::Cast(mathObject), true, false, true); 1702 JSObject::DefineOwnProperty(thread_, globalObject, mathString, mathDesc); 1703 // @@ToStringTag 1704 SetStringTagSymbol(env, mathObject, "Math"); 1705 env->SetMathFunction(thread_, mathObject); 1706 env->SetMathFunctionClass(thread_, JSHandle<JSHClass>(thread_, mathObject->GetJSHClass())); 1707} 1708 1709void Builtins::InitializeJson(const JSHandle<GlobalEnv> &env, const JSHandle<JSTaggedValue> &objFuncPrototypeVal) const 1710{ 1711 [[maybe_unused]] EcmaHandleScope scope(thread_); 1712 JSHandle<JSHClass> jsonHClass = factory_->NewEcmaHClass(JSObject::SIZE, JSType::JS_OBJECT, objFuncPrototypeVal); 1713 JSHandle<JSObject> jsonObject = factory_->NewJSObjectWithInit(jsonHClass); 1714 1715 SetFunction(env, jsonObject, "parse", Json::Parse, FunctionLength::TWO); 1716 SetFunction(env, jsonObject, "parseSendable", SendableJson::Parse, FunctionLength::THREE); 1717 SetFunction(env, jsonObject, "parseBigInt", BigIntJson::Parse, FunctionLength::THREE); 1718 SetFunction(env, jsonObject, "stringify", Json::Stringify, FunctionLength::THREE, BUILTINS_STUB_ID(JsonStringify)); 1719 SetFunction(env, jsonObject, "stringifySendable", SendableJson::Stringify, FunctionLength::THREE); 1720 SetFunction(env, jsonObject, "stringifyBigInt", BigIntJson::Stringify, FunctionLength::THREE); 1721 1722 PropertyDescriptor jsonDesc(thread_, JSHandle<JSTaggedValue>::Cast(jsonObject), true, false, true); 1723 JSHandle<JSTaggedValue> jsonString(factory_->NewFromASCIIReadOnly("JSON")); 1724 JSHandle<JSObject> globalObject(thread_, env->GetGlobalObject()); 1725 JSObject::DefineOwnProperty(thread_, globalObject, jsonString, jsonDesc); 1726 // @@ToStringTag 1727 SetStringTagSymbol(env, jsonObject, "JSON"); 1728 env->SetJsonFunction(thread_, jsonObject); 1729} 1730 1731void Builtins::InitializeString(const JSHandle<GlobalEnv> &env, JSHandle<JSTaggedValue> objFuncPrototypeVal) const 1732{ 1733 [[maybe_unused]] EcmaHandleScope scope(thread_); 1734 // String.prototype 1735 JSHandle<JSTaggedValue> toObject(factory_->GetEmptyString()); 1736 JSHandle<JSHClass> primRefObjHClass = 1737 factory_->NewEcmaHClass(JSPrimitiveRef::SIZE, BuiltinsString::GetNumPrototypeInlinedProperties(), 1738 JSType::JS_PRIMITIVE_REF, objFuncPrototypeVal); 1739 JSHandle<JSObject> stringFuncPrototype = 1740 JSHandle<JSObject>::Cast(factory_->NewJSPrimitiveRef(primRefObjHClass, toObject)); 1741 JSHandle<JSTaggedValue> stringFuncPrototypeValue(stringFuncPrototype); 1742 1743 // String.prototype_or_hclass 1744 JSHandle<JSHClass> stringFuncInstanceHClass = 1745 factory_->NewEcmaHClass(JSPrimitiveRef::SIZE, JSType::JS_PRIMITIVE_REF, stringFuncPrototypeValue); 1746 1747 // String = new Function() 1748 JSHandle<JSObject> stringFunction(NewBuiltinConstructor(env, stringFuncPrototype, BuiltinsString::StringConstructor, 1749 "String", FunctionLength::ONE)); 1750 JSFunction::SetFunctionPrototypeOrInstanceHClass(thread_, 1751 JSHandle<JSFunction>(stringFunction), 1752 stringFuncInstanceHClass.GetTaggedValue()); 1753 1754 // String.prototype method 1755 for (const base::BuiltinFunctionEntry &entry: BuiltinsString::GetStringPrototypeFunctions()) { 1756 SetFunction(env, stringFuncPrototype, entry.GetName(), entry.GetEntrypoint(), 1757 entry.GetLength(), entry.GetBuiltinStubId()); 1758 } 1759 JSHandle<JSTaggedValue> stringIter = SetAndReturnFunctionAtSymbol(env, stringFuncPrototype, 1760 env->GetIteratorSymbol(), "[Symbol.iterator]", BuiltinsString::GetStringIterator, FunctionLength::ZERO, 1761 BUILTINS_STUB_ID(StringGetStringIterator)); 1762 1763 // String method 1764 for (const base::BuiltinFunctionEntry &entry: BuiltinsString::GetStringFunctions()) { 1765 SetFunction(env, stringFunction, entry.GetName(), entry.GetEntrypoint(), 1766 entry.GetLength(), entry.GetBuiltinStubId()); 1767 } 1768 1769 // String.prototype.length 1770 JSHandle<JSTaggedValue> lengthGetter = CreateGetter(env, BuiltinsString::GetLength, "length", FunctionLength::ZERO); 1771 JSHandle<JSTaggedValue> lengthKey(factory_->NewFromASCIIReadOnly("length")); 1772 SetGetter(stringFuncPrototype, lengthKey, lengthGetter); 1773 1774 env->SetStringFunction(thread_, stringFunction); 1775 env->SetStringPrototype(thread_, stringFuncPrototype); 1776 env->SetStringProtoIterFunction(thread_, stringIter); 1777 thread_->SetInitialBuiltinHClass(BuiltinTypeId::STRING, nullptr, nullptr, stringFuncPrototype->GetJSHClass()); 1778} 1779 1780void Builtins::InitializeStringIterator(const JSHandle<GlobalEnv> &env, 1781 const JSHandle<JSHClass> &iteratorFuncClass) const 1782{ 1783 // StringIterator.prototype 1784 JSHandle<JSObject> strIterPrototype(factory_->NewJSObjectWithInit(iteratorFuncClass)); 1785 1786 // StringIterator.prototype_or_hclass 1787 JSHandle<JSHClass> strIterFuncInstanceHClass = factory_->NewEcmaHClass( 1788 JSStringIterator::SIZE, JSType::JS_STRING_ITERATOR, JSHandle<JSTaggedValue>(strIterPrototype)); 1789 1790 JSHandle<JSFunction> strIterFunction( 1791 factory_->NewJSFunction(env, static_cast<void *>(nullptr), FunctionKind::BASE_CONSTRUCTOR)); 1792 JSFunction::SetFunctionPrototypeOrInstanceHClass(thread_, strIterFunction, 1793 strIterFuncInstanceHClass.GetTaggedValue()); 1794 1795 SetFunction(env, strIterPrototype, "next", StringIterator::Next, FunctionLength::ZERO, 1796 BUILTINS_STUB_ID(StringIteratorProtoNext)); 1797 SetStringTagSymbol(env, strIterPrototype, "String Iterator"); 1798 1799 env->SetStringIterator(thread_, strIterFunction); 1800 env->SetStringIteratorClass(thread_, strIterFuncInstanceHClass); 1801 env->SetStringIteratorPrototype(thread_, strIterPrototype); 1802} 1803 1804void Builtins::InitializeAsyncFromSyncIterator(const JSHandle<GlobalEnv> &env, 1805 const JSHandle<JSHClass> &iteratorFuncClass) const 1806{ 1807 [[maybe_unused]] EcmaHandleScope scope(thread_); 1808 1809 JSHandle<JSObject> asyncItPrototype = factory_->NewJSObjectWithInit(iteratorFuncClass); 1810 SetFunction(env, asyncItPrototype, "next", BuiltinsAsyncFromSyncIterator::Next, FunctionLength::ONE); 1811 SetFunction(env, asyncItPrototype, "return", BuiltinsAsyncFromSyncIterator::Return, FunctionLength::ONE); 1812 SetFunction(env, asyncItPrototype, "throw", BuiltinsAsyncFromSyncIterator::Throw, FunctionLength::ONE); 1813 JSHandle<JSHClass> hclass = factory_->NewEcmaHClass(JSAsyncFromSyncIterator::SIZE, 1814 JSType::JS_ASYNC_FROM_SYNC_ITERATOR, 1815 JSHandle<JSTaggedValue>(asyncItPrototype)); 1816 JSHandle<JSFunction> iterFunction( 1817 factory_->NewJSFunction(env, static_cast<void *>(nullptr), FunctionKind::BASE_CONSTRUCTOR)); 1818 JSFunction::SetFunctionPrototypeOrInstanceHClass(thread_, iterFunction, hclass.GetTaggedValue()); 1819 env->SetAsyncFromSyncIterator(thread_, iterFunction); 1820 env->SetAsyncFromSyncIteratorPrototype(thread_, asyncItPrototype); 1821 1822 JSHandle<JSHClass> asyncFromSyncIterUnwarpClass = 1823 factory_->NewEcmaHClass(JSAsyncFromSyncIterUnwarpFunction::SIZE, 1824 JSType::JS_ASYNC_FROM_SYNC_ITER_UNWARP_FUNCTION, 1825 env->GetFunctionPrototype()); 1826 asyncFromSyncIterUnwarpClass->SetCallable(true); 1827 asyncFromSyncIterUnwarpClass->SetExtensible(true); 1828 env->SetAsyncFromSyncIterUnwarpClass(thread_, asyncFromSyncIterUnwarpClass); 1829} 1830 1831void Builtins::InitializeIterator(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncClass) const 1832{ 1833 [[maybe_unused]] EcmaHandleScope scope(thread_); 1834 // Iterator.prototype 1835 JSHandle<JSObject> iteratorPrototype = factory_->NewJSObjectWithInit(objFuncClass); 1836 // Iterator.prototype.next() 1837 SetFunction(env, iteratorPrototype, "next", BuiltinsIterator::Next, FunctionLength::ONE); 1838 // Iterator.prototype.return() 1839 SetFunction(env, iteratorPrototype, "return", BuiltinsIterator::Return, FunctionLength::ONE, 1840 BUILTINS_STUB_ID(IteratorProtoReturn)); 1841 // Iterator.prototype.throw() 1842 SetFunction(env, iteratorPrototype, "throw", BuiltinsIterator::Throw, FunctionLength::ONE); 1843 // %IteratorPrototype% [ @@iterator ] 1844 SetFunctionAtSymbol(env, iteratorPrototype, env->GetIteratorSymbol(), "[Symbol.iterator]", 1845 BuiltinsIterator::GetIteratorObj, FunctionLength::ZERO); 1846 env->SetIteratorPrototype(thread_, iteratorPrototype); 1847 1848 // Iterator.hclass 1849 JSHandle<JSHClass> iteratorFuncClass = 1850 factory_->NewEcmaHClass(JSObject::SIZE, JSType::JS_ITERATOR, JSHandle<JSTaggedValue>(iteratorPrototype)); 1851 1852 auto globalConst = const_cast<GlobalEnvConstants *>(thread_->GlobalConstants()); 1853 globalConst->SetConstant(ConstantIndex::JS_API_ITERATOR_FUNC_CLASS_INDEX, iteratorFuncClass); 1854 1855 // Iterator result hclass 1856 JSHandle<JSHClass> iterResultHClass = factory_->CreateIteratorResultInstanceClass(env); 1857 globalConst->SetConstant(ConstantIndex::ITERATOR_RESULT_CLASS, iterResultHClass); 1858 1859 // use for CloseIterator 1860 JSHandle<CompletionRecord> record = 1861 factory_->NewCompletionRecord(CompletionRecordType::NORMAL, globalConst->GetHandledUndefined()); 1862 globalConst->SetConstant(ConstantIndex::UNDEFINED_COMPLRTION_RECORD_INDEX, record); 1863 1864 thread_->SetInitialBuiltinHClass(BuiltinTypeId::ITERATOR, nullptr, 1865 *iteratorFuncClass, iteratorPrototype->GetJSHClass()); 1866 1867 // iteratorPrototype hclass 1868 JSHandle<JSHClass> iteratorPrototypeHClass(thread_, iteratorPrototype->GetJSHClass()); 1869 1870 InitializeForinIterator(env, iteratorFuncClass); 1871 InitializeSetIterator(env, iteratorFuncClass); 1872 InitializeSSetIterator(env, iteratorFuncClass); 1873 InitializeMapIterator(env, iteratorFuncClass); 1874 InitializeSMapIterator(env, iteratorFuncClass); 1875 InitializeArrayIterator(env, iteratorFuncClass, iteratorPrototypeHClass); 1876 InitializeSArrayIterator(env, iteratorFuncClass); 1877 InitializeStringIterator(env, iteratorFuncClass); 1878 InitializeRegexpIterator(env, iteratorFuncClass); 1879#ifdef ARK_SUPPORT_INTL 1880 InitializeSegmentIterator(env, iteratorFuncClass); 1881#endif 1882} 1883 1884void Builtins::InitializeAsyncIterator(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncDynclass) const 1885{ 1886 [[maybe_unused]] EcmaHandleScope scope(thread_); 1887 // AsyncIterator.prototype 1888 JSHandle<JSObject> asyncIteratorPrototype = factory_->NewJSObjectWithInit(objFuncDynclass); 1889 // AsyncIterator.prototype.next() 1890 SetFunction(env, asyncIteratorPrototype, "next", BuiltinsAsyncIterator::Next, FunctionLength::ONE); 1891 // AsyncIterator.prototype.return() 1892 SetFunction(env, asyncIteratorPrototype, "return", BuiltinsAsyncIterator::Return, FunctionLength::ONE); 1893 // AsyncIterator.prototype.throw() 1894 SetFunction(env, asyncIteratorPrototype, "throw", BuiltinsAsyncIterator::Throw, FunctionLength::ONE); 1895 // %AsyncIteratorPrototype% [ @@AsyncIterator ] 1896 SetFunctionAtSymbol(env, asyncIteratorPrototype, env->GetAsyncIteratorSymbol(), "[Symbol.asyncIterator]", 1897 BuiltinsAsyncIterator::GetAsyncIteratorObj, FunctionLength::ZERO); 1898 env->SetAsyncIteratorPrototype(thread_, asyncIteratorPrototype); 1899 1900 // AsyncIterator.dynclass 1901 JSHandle<JSHClass> asyncIteratorFuncDynclass = 1902 factory_->NewEcmaHClass(JSObject::SIZE, 1903 JSType::JS_ASYNCITERATOR, JSHandle<JSTaggedValue>(asyncIteratorPrototype)); 1904 1905 auto globalConst = const_cast<GlobalEnvConstants *>(thread_->GlobalConstants()); 1906 globalConst->SetConstant(ConstantIndex::JS_API_ASYNCITERATOR_FUNC_CLASS_INDEX, asyncIteratorFuncDynclass); 1907} 1908 1909void Builtins::InitializeForinIterator(const JSHandle<GlobalEnv> &env, 1910 const JSHandle<JSHClass> &iteratorFuncClass) const 1911{ 1912 [[maybe_unused]] EcmaHandleScope scope(thread_); 1913 // Iterator.prototype 1914 JSHandle<JSObject> forinIteratorPrototype = factory_->NewJSObjectWithInit(iteratorFuncClass); 1915 JSHandle<JSHClass> hclass = factory_->NewEcmaHClass(JSForInIterator::SIZE, JSType::JS_FORIN_ITERATOR, 1916 JSHandle<JSTaggedValue>(forinIteratorPrototype)); 1917 1918 // Iterator.prototype.next() 1919 SetFunction(env, forinIteratorPrototype, "next", JSForInIterator::Next, FunctionLength::ONE); 1920 env->SetForinIteratorPrototype(thread_, forinIteratorPrototype); 1921 env->SetForinIteratorClass(thread_, hclass); 1922} 1923 1924void Builtins::InitializeSetIterator(const JSHandle<GlobalEnv> &env, 1925 const JSHandle<JSHClass> &iteratorFuncClass) const 1926{ 1927 // SetIterator.prototype 1928 JSHandle<JSObject> setIteratorPrototype(factory_->NewJSObjectWithInit(iteratorFuncClass)); 1929 // Iterator.prototype.next() 1930 SetFunction(env, setIteratorPrototype, "next", JSSetIterator::Next, FunctionLength::ZERO, 1931 BUILTINS_STUB_ID(SetIteratorProtoNext)); 1932 SetStringTagSymbol(env, setIteratorPrototype, "Set Iterator"); 1933 env->SetSetIteratorPrototype(thread_, setIteratorPrototype); 1934 JSHandle<JSTaggedValue> protoValue = env->GetSetIteratorPrototype(); 1935 const GlobalEnvConstants *globalConst = thread_->GlobalConstants(); 1936 JSHandle<JSHClass> hclassHandle(globalConst->GetHandledJSSetIteratorClass()); 1937 hclassHandle->SetPrototype(thread_, protoValue); 1938 hclassHandle->SetExtensible(true); 1939} 1940 1941void Builtins::InitializeSSetIterator(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &iteratorFuncClass) const 1942{ 1943 // SetIterator.prototype 1944 JSHandle<JSObject> setIteratorPrototype(factory_->NewJSObjectWithInit(iteratorFuncClass)); 1945 // Iterator.prototype.next() 1946 SetFunction(env, setIteratorPrototype, "next", JSSharedSetIterator::Next, FunctionLength::ZERO); 1947 SetStringTagSymbol(env, setIteratorPrototype, "SharedSet Iterator"); 1948 env->SetSharedSetIteratorPrototype(thread_, setIteratorPrototype); 1949 JSHandle<JSTaggedValue> protoValue = env->GetSharedSetIteratorPrototype(); 1950 const GlobalEnvConstants *globalConst = thread_->GlobalConstants(); 1951 JSHandle<JSHClass> hclassHandle(globalConst->GetHandledJSSharedSetIteratorClass()); 1952 hclassHandle->SetPrototype(thread_, protoValue); 1953 hclassHandle->SetExtensible(true); 1954} 1955 1956void Builtins::InitializeMapIterator(const JSHandle<GlobalEnv> &env, 1957 const JSHandle<JSHClass> &iteratorFuncClass) const 1958{ 1959 // MapIterator.prototype 1960 JSHandle<JSObject> mapIteratorPrototype(factory_->NewJSObjectWithInit(iteratorFuncClass)); 1961 // Iterator.prototype.next() 1962 SetFunction(env, mapIteratorPrototype, "next", JSMapIterator::Next, FunctionLength::ZERO, 1963 BUILTINS_STUB_ID(MapIteratorProtoNext)); 1964 SetStringTagSymbol(env, mapIteratorPrototype, "Map Iterator"); 1965 env->SetMapIteratorPrototype(thread_, mapIteratorPrototype); 1966 JSHandle<JSTaggedValue> protoValue = env->GetMapIteratorPrototype(); 1967 const GlobalEnvConstants *globalConst = thread_->GlobalConstants(); 1968 JSHandle<JSHClass> hclassHandle(globalConst->GetHandledJSMapIteratorClass()); 1969 hclassHandle->SetPrototype(thread_, protoValue); 1970 hclassHandle->SetExtensible(true); 1971} 1972 1973void Builtins::InitializeSMapIterator(const JSHandle<GlobalEnv> &env, 1974 const JSHandle<JSHClass> &iteratorFuncClass) const 1975{ 1976 // MapIterator.prototype 1977 JSHandle<JSObject> mapIteratorPrototype(factory_->NewJSObjectWithInit(iteratorFuncClass)); 1978 // Iterator.prototype.next() 1979 SetFunction(env, mapIteratorPrototype, "next", JSSharedMapIterator::Next, FunctionLength::ZERO); 1980 SetStringTagSymbol(env, mapIteratorPrototype, "SharedMap Iterator"); 1981 env->SetSharedMapIteratorPrototype(thread_, mapIteratorPrototype); 1982 JSHandle<JSTaggedValue> protoValue = env->GetSharedMapIteratorPrototype(); 1983 const GlobalEnvConstants *globalConst = thread_->GlobalConstants(); 1984 JSHandle<JSHClass> hclassHandle(globalConst->GetHandledJSSharedMapIteratorClass()); 1985 hclassHandle->SetPrototype(thread_, protoValue); 1986 hclassHandle->SetExtensible(true); 1987} 1988 1989void Builtins::InitializeArrayIterator(const JSHandle<GlobalEnv> &env, 1990 const JSHandle<JSHClass> &iteratorFuncClass, 1991 const JSHandle<JSHClass> &iteratorPrototypeClass) const 1992{ 1993 // ArrayIterator.prototype 1994 JSHandle<JSObject> arrayIteratorPrototype(factory_->NewJSObjectWithInit(iteratorFuncClass)); 1995 JSHandle<JSTaggedValue> arrayIteratorPrototypeValue(arrayIteratorPrototype); 1996 auto globalConst = const_cast<GlobalEnvConstants *>(thread_->GlobalConstants()); 1997 JSHandle<JSHClass> arrayIteratorInstanceHClass(globalConst->GetHandledJSArrayIteratorClass()); 1998 // Iterator.prototype.next() 1999 SetFunction(env, arrayIteratorPrototype, "next", JSArrayIterator::Next, FunctionLength::ZERO, 2000 BUILTINS_STUB_ID(ArrayIteratorProtoNext)); 2001 arrayIteratorInstanceHClass->SetPrototype(thread_, arrayIteratorPrototypeValue); 2002 SetStringTagSymbol(env, arrayIteratorPrototype, "Array Iterator"); 2003 thread_->SetInitialBuiltinHClass(BuiltinTypeId::ARRAY_ITERATOR, nullptr, 2004 *arrayIteratorInstanceHClass, arrayIteratorPrototype->GetJSHClass(), *iteratorPrototypeClass); 2005 env->SetArrayIteratorPrototype(thread_, arrayIteratorPrototype); 2006} 2007 2008void Builtins::InitializeSArrayIterator(const JSHandle<GlobalEnv> &env, 2009 const JSHandle<JSHClass> &iteratorFuncClass) const 2010{ 2011 // ArrayIterator.prototype 2012 JSHandle<JSObject> arrayIteratorPrototype(factory_->NewJSObjectWithInit(iteratorFuncClass)); 2013 // Iterator.prototype.next() 2014 SetFunction(env, arrayIteratorPrototype, "next", JSSharedArrayIterator::Next, FunctionLength::ZERO); 2015 SetStringTagSymbol(env, arrayIteratorPrototype, "SharedArray Iterator"); 2016 env->SetSharedArrayIteratorPrototype(thread_, arrayIteratorPrototype); 2017} 2018 2019void Builtins::InitializeRegexpIterator(const JSHandle<GlobalEnv> &env, 2020 const JSHandle<JSHClass> &iteratorFuncClass) const 2021{ 2022 // RegExpIterator.prototype 2023 JSHandle<JSObject> regExpIteratorPrototype(factory_->NewJSObject(iteratorFuncClass)); 2024 // Iterator.prototype.next() 2025 SetFunction(env, regExpIteratorPrototype, "next", JSRegExpIterator::Next, FunctionLength::ZERO); 2026 SetStringTagSymbol(env, regExpIteratorPrototype, "RegExp String Iterator"); 2027 env->SetRegExpIteratorPrototype(thread_, regExpIteratorPrototype); 2028} 2029 2030void Builtins::InitializeRegExp(const JSHandle<GlobalEnv> &env) 2031{ 2032 [[maybe_unused]] EcmaHandleScope scope(thread_); 2033 // RegExp.prototype 2034 JSHandle<JSFunction> objFun(env->GetObjectFunction()); 2035 JSHandle<JSObject> regPrototype = factory_->NewJSObjectByConstructor(env, objFun, REGEXP_INLINE_PROPS); 2036 JSHandle<JSTaggedValue> regPrototypeValue(regPrototype); 2037 2038 // RegExp.prototype_or_hclass 2039 JSHandle<JSHClass> regexpFuncInstanceHClass = factory_->CreateJSRegExpInstanceClass(regPrototypeValue); 2040 2041 // RegExp = new Function() 2042 JSHandle<JSObject> regexpFunction( 2043 NewBuiltinConstructor(env, regPrototype, RegExp::RegExpConstructor, "RegExp", FunctionLength::TWO)); 2044 2045 // initialize RegExp.$1 .. $9 static and read-only attributes 2046 InitializeGlobalRegExp(regexpFunction); 2047 2048 JSFunction::SetFunctionPrototypeOrInstanceHClass(thread_, 2049 JSHandle<JSFunction>(regexpFunction), 2050 regexpFuncInstanceHClass.GetTaggedValue()); 2051 2052 const GlobalEnvConstants *globalConstants = thread_->GlobalConstants(); 2053 // RegExp.prototype method 2054 JSHandle<JSFunction> execFunc = SetAndReturnFunction(env, regPrototype, "exec", RegExp::Exec, FunctionLength::ONE); 2055 SetFunction(env, regPrototype, "test", RegExp::Test, FunctionLength::ONE); 2056 SetFunction(env, regPrototype, globalConstants->GetHandledToStringString(), RegExp::ToString, 2057 FunctionLength::ZERO); 2058 JSHandle<JSFunction>(execFunc)->SetLexicalEnv(thread_, env); 2059 2060 JSHandle<JSTaggedValue> flagsGetter = CreateGetter(env, RegExp::GetFlags, "flags", FunctionLength::ZERO, 2061 kungfu::BuiltinsStubCSigns::RegExpGetFlags); 2062 JSHandle<JSTaggedValue> flagsKey(globalConstants->GetHandledFlagsString()); 2063 SetGetter(regPrototype, flagsKey, flagsGetter); 2064 JSHandle<JSFunction>(flagsGetter)->SetLexicalEnv(thread_, env); 2065 2066 JSHandle<JSTaggedValue> sourceGetter = CreateGetter(env, RegExp::GetSource, "source", FunctionLength::ZERO); 2067 JSHandle<JSTaggedValue> sourceKey(globalConstants->GetHandledSourceString()); 2068 SetGetter(regPrototype, sourceKey, sourceGetter); 2069 JSHandle<JSFunction>(sourceGetter)->SetLexicalEnv(thread_, env); 2070 2071 JSHandle<JSTaggedValue> globalGetter = CreateGetter(env, RegExp::GetGlobal, "global", FunctionLength::ZERO); 2072 JSHandle<JSTaggedValue> globalKey(globalConstants->GetHandledGlobalString()); 2073 SetGetter(regPrototype, globalKey, globalGetter); 2074 JSHandle<JSFunction>(globalGetter)->SetLexicalEnv(thread_, env); 2075 2076 JSHandle<JSTaggedValue> hasIndicesGetter = 2077 CreateGetter(env, RegExp::GetHasIndices, "hasIndices", FunctionLength::ZERO); 2078 JSHandle<JSTaggedValue> hasIndicesKey(factory_->NewFromASCIIReadOnly("hasIndices")); 2079 SetGetter(regPrototype, hasIndicesKey, hasIndicesGetter); 2080 JSHandle<JSFunction>(hasIndicesGetter)->SetLexicalEnv(thread_, env); 2081 2082 JSHandle<JSTaggedValue> ignoreCaseGetter = 2083 CreateGetter(env, RegExp::GetIgnoreCase, "ignoreCase", FunctionLength::ZERO); 2084 JSHandle<JSTaggedValue> ignoreCaseKey(factory_->NewFromASCIIReadOnly("ignoreCase")); 2085 SetGetter(regPrototype, ignoreCaseKey, ignoreCaseGetter); 2086 JSHandle<JSFunction>(ignoreCaseGetter)->SetLexicalEnv(thread_, env); 2087 2088 JSHandle<JSTaggedValue> multilineGetter = 2089 CreateGetter(env, RegExp::GetMultiline, "multiline", FunctionLength::ZERO); 2090 JSHandle<JSTaggedValue> multilineKey(factory_->NewFromASCIIReadOnly("multiline")); 2091 SetGetter(regPrototype, multilineKey, multilineGetter); 2092 JSHandle<JSFunction>(multilineGetter)->SetLexicalEnv(thread_, env); 2093 2094 JSHandle<JSTaggedValue> dotAllGetter = CreateGetter(env, RegExp::GetDotAll, "dotAll", FunctionLength::ZERO); 2095 JSHandle<JSTaggedValue> dotAllKey(factory_->NewFromASCIIReadOnly("dotAll")); 2096 SetGetter(regPrototype, dotAllKey, dotAllGetter); 2097 JSHandle<JSFunction>(dotAllGetter)->SetLexicalEnv(thread_, env); 2098 2099 JSHandle<JSTaggedValue> stickyGetter = CreateGetter(env, RegExp::GetSticky, "sticky", FunctionLength::ZERO); 2100 JSHandle<JSTaggedValue> stickyKey(globalConstants->GetHandledStickyString()); 2101 SetGetter(regPrototype, stickyKey, stickyGetter); 2102 JSHandle<JSFunction>(stickyGetter)->SetLexicalEnv(thread_, env); 2103 2104 JSHandle<JSTaggedValue> unicodeGetter = CreateGetter(env, RegExp::GetUnicode, "unicode", FunctionLength::ZERO); 2105 JSHandle<JSTaggedValue> unicodeKey(globalConstants->GetHandledUnicodeString()); 2106 SetGetter(regPrototype, unicodeKey, unicodeGetter); 2107 JSHandle<JSFunction>(unicodeGetter)->SetLexicalEnv(thread_, env); 2108 2109 // Set RegExp [ @@species ] 2110 JSHandle<JSTaggedValue> speciesSymbol = env->GetSpeciesSymbol(); 2111 JSHandle<JSTaggedValue> speciesGetter = 2112 CreateGetter(env, BuiltinsMap::Species, "[Symbol.species]", FunctionLength::ZERO); 2113 SetGetter(JSHandle<JSObject>(regexpFunction), speciesSymbol, speciesGetter); 2114 JSHandle<JSFunction>(speciesGetter)->SetLexicalEnv(thread_, env); 2115 2116 // Set RegExp.prototype[@@split] 2117 JSHandle<JSTaggedValue> splitFunc = SetAndReturnFunctionAtSymbol( 2118 env, regPrototype, env->GetSplitSymbol(), "[Symbol.split]", RegExp::Split, FunctionLength::TWO); 2119 // Set RegExp.prototype[@@search] 2120 JSHandle<JSTaggedValue> searchFunc = SetAndReturnFunctionAtSymbol( 2121 env, regPrototype, env->GetSearchSymbol(), "[Symbol.search]", RegExp::Search, FunctionLength::ONE); 2122 // Set RegExp.prototype[@@match] 2123 JSHandle<JSTaggedValue> matchFunc = SetAndReturnFunctionAtSymbol( 2124 env, regPrototype, env->GetMatchSymbol(), "[Symbol.match]", RegExp::Match, FunctionLength::ONE); 2125 // Set RegExp.prototype[@@matchAll] 2126 JSHandle<JSTaggedValue> matchAllFunc = SetAndReturnFunctionAtSymbol( 2127 env, regPrototype, env->GetMatchAllSymbol(), "[Symbol.matchAll]", RegExp::MatchAll, FunctionLength::ONE); 2128 // Set RegExp.prototype[@@replace] 2129 JSHandle<JSTaggedValue> replaceFunc = SetAndReturnFunctionAtSymbol( 2130 env, regPrototype, env->GetReplaceSymbol(), "[Symbol.replace]", RegExp::Replace, FunctionLength::TWO); 2131 2132 env->SetRegExpFunction(thread_, regexpFunction); 2133 env->SetRegExpPrototype(thread_, regPrototype); 2134 env->SetRegExpExecFunction(thread_, execFunc); 2135 env->SetRegExpSplitFunction(thread_, splitFunc); 2136 env->SetRegExpSearchFunction(thread_, searchFunc); 2137 env->SetRegExpMatchFunction(thread_, matchFunc); 2138 env->SetRegExpMatchAllFunction(thread_, matchAllFunc); 2139 env->SetRegExpReplaceFunction(thread_, replaceFunc); 2140 // Set RegExp.prototype hclass 2141 JSHandle<JSHClass> regPrototypeClass(thread_, regPrototype->GetJSHClass()); 2142 env->SetRegExpPrototypeClass(thread_, regPrototypeClass.GetTaggedValue()); 2143 2144 auto globalConst = const_cast<GlobalEnvConstants *>(thread_->GlobalConstants()); 2145 globalConst->SetConstant(ConstantIndex::JS_REGEXP_CLASS_INDEX, regexpFuncInstanceHClass.GetTaggedValue()); 2146} 2147 2148void Builtins::InitializeArray(const JSHandle<GlobalEnv> &env, const JSHandle<JSTaggedValue> &objFuncPrototypeVal) const 2149{ 2150 [[maybe_unused]] EcmaHandleScope scope(thread_); 2151 // Arraybase.prototype 2152 JSHandle<JSHClass> arrBaseFuncInstanceHClass = factory_->CreateJSArrayInstanceClass( 2153 objFuncPrototypeVal, BuiltinsArray::GetNumPrototypeInlinedProperties()); 2154 2155 // Array.prototype 2156 JSHandle<JSObject> arrFuncPrototype = factory_->NewJSObjectWithInit(arrBaseFuncInstanceHClass); 2157 JSHandle<JSArray>::Cast(arrFuncPrototype)->SetLength(FunctionLength::ZERO); 2158 auto accessor = thread_->GlobalConstants()->GetArrayLengthAccessor(); 2159 JSArray::Cast(*arrFuncPrototype)->SetPropertyInlinedProps(thread_, JSArray::LENGTH_INLINE_PROPERTY_INDEX, accessor); 2160 JSHandle<JSTaggedValue> arrFuncPrototypeValue(arrFuncPrototype); 2161 2162 // Array.prototype_or_hclass 2163 JSMutableHandle<JSHClass> arrFuncInstanceHClass(thread_, JSTaggedValue::Undefined()); 2164 arrFuncInstanceHClass.Update(factory_->CreateJSArrayInstanceClass(arrFuncPrototypeValue)); 2165 auto globalConstant = const_cast<GlobalEnvConstants *>(thread_->GlobalConstants()); 2166 globalConstant->InitElementKindHClass(thread_, arrFuncInstanceHClass); 2167 if (thread_->GetEcmaVM()->IsEnableElementsKind()) { 2168 // for all JSArray, the initial ElementsKind should be NONE 2169 // For PGO, currently we do not support elementsKind for builtins 2170 #if ECMASCRIPT_ENABLE_ELEMENTSKIND_ALWAY_GENERIC 2171 auto index = static_cast<size_t>(ConstantIndex::ELEMENT_HOLE_TAGGED_HCLASS_INDEX); 2172 #else 2173 auto index = static_cast<size_t>(ConstantIndex::ELEMENT_NONE_HCLASS_INDEX); 2174 #endif 2175 auto hclassVal = globalConstant->GetGlobalConstantObject(index); 2176 arrFuncInstanceHClass.Update(hclassVal); 2177 } 2178 2179 // Array = new Function() 2180 JSHandle<JSObject> arrayFunction( 2181 NewBuiltinConstructor(env, arrFuncPrototype, BuiltinsArray::ArrayConstructor, "Array", FunctionLength::ONE, 2182 BUILTINS_STUB_ID(ArrayConstructor))); 2183 JSHandle<JSFunction> arrayFuncFunction(arrayFunction); 2184 2185 // Set the [[Realm]] internal slot of F to the running execution context's Realm 2186 JSHandle<LexicalEnv> lexicalEnv = factory_->NewLexicalEnv(0); 2187 lexicalEnv->SetParentEnv(thread_, env.GetTaggedValue()); 2188 arrayFuncFunction->SetLexicalEnv(thread_, lexicalEnv.GetTaggedValue()); 2189 2190 JSFunction::SetFunctionPrototypeOrInstanceHClass(thread_, arrayFuncFunction, 2191 arrFuncInstanceHClass.GetTaggedValue()); 2192 2193 // Array.prototype methods (excluding constructor and '@@' internal properties) 2194 for (const base::BuiltinFunctionEntry &entry: BuiltinsArray::GetArrayPrototypeFunctions()) { 2195 SetFunction(env, arrFuncPrototype, entry.GetName(), entry.GetEntrypoint(), 2196 entry.GetLength(), entry.GetBuiltinStubId()); 2197 } 2198 2199 // %ArrayPrototype% [ @@iterator ] 2200 JSHandle<JSTaggedValue> values(factory_->NewFromASCIIReadOnly("values")); 2201 JSHandle<JSTaggedValue> iteratorSymbol = env->GetIteratorSymbol(); 2202 JSHandle<JSTaggedValue> valuesFunc = 2203 JSObject::GetMethod(thread_, JSHandle<JSTaggedValue>::Cast(arrFuncPrototype), values); 2204 RETURN_IF_ABRUPT_COMPLETION(thread_); 2205 PropertyDescriptor iteartorDesc(thread_, valuesFunc, true, false, true); 2206 JSObject::DefineOwnProperty(thread_, arrFuncPrototype, iteratorSymbol, iteartorDesc); 2207 2208 // Array methods (excluding '@@' internal properties) 2209 for (const base::BuiltinFunctionEntry &entry: BuiltinsArray::GetArrayFunctions()) { 2210 SetFunction(env, arrayFunction, entry.GetName(), entry.GetEntrypoint(), 2211 entry.GetLength(), entry.GetBuiltinStubId()); 2212 } 2213 2214 // 22.1.2.5 get %Array% [ @@species ] 2215 JSHandle<JSTaggedValue> speciesSymbol = env->GetSpeciesSymbol(); 2216 JSHandle<JSTaggedValue> speciesGetter = 2217 CreateGetter(env, BuiltinsArray::Species, "[Symbol.species]", FunctionLength::ZERO); 2218 SetGetter(JSHandle<JSObject>(arrayFunction), speciesSymbol, speciesGetter); 2219 2220 constexpr int arrProtoLen = 0; 2221 JSHandle<JSTaggedValue> keyString = thread_->GlobalConstants()->GetHandledLengthString(); 2222 PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>(thread_, JSTaggedValue(arrProtoLen)), true, false, 2223 false); 2224 JSObject::DefineOwnProperty(thread_, arrFuncPrototype, keyString, descriptor); 2225 2226 JSHandle<JSTaggedValue> valuesKey(factory_->NewFromASCIIReadOnly("values")); 2227 PropertyDescriptor desc(thread_); 2228 JSObject::GetOwnProperty(thread_, arrFuncPrototype, valuesKey, desc); 2229 2230 // Array.prototype [ @@unscopables ] 2231 JSHandle<JSTaggedValue> unscopablesSymbol = env->GetUnscopablesSymbol(); 2232 JSHandle<JSTaggedValue> unscopables = CreateArrayUnscopables(thread_); 2233 PropertyDescriptor unscopablesDesc(thread_, unscopables, false, false, true); 2234 JSObject::DefineOwnProperty(thread_, arrFuncPrototype, unscopablesSymbol, unscopablesDesc); 2235 2236 env->SetArrayProtoValuesFunction(thread_, desc.GetValue()); 2237 env->SetArrayFunction(thread_, arrayFunction); 2238 env->SetArrayPrototype(thread_, arrFuncPrototype); 2239 2240 thread_->SetInitialBuiltinHClass(BuiltinTypeId::ARRAY, arrayFunction->GetJSHClass(), 2241 *arrFuncInstanceHClass, arrFuncPrototype->GetJSHClass()); 2242} 2243 2244void Builtins::InitializeTypedArray(const JSHandle<GlobalEnv> &env, JSHandle<JSTaggedValue> objFuncPrototypeVal) const 2245{ 2246 [[maybe_unused]] EcmaHandleScope scope(thread_); 2247 // TypedArray.prototype 2248 JSHandle<JSHClass> typedArrFuncPrototypeHClass = factory_->NewEcmaHClass( 2249 JSObject::SIZE, BuiltinsTypedArray::GetNumPrototypeInlinedProperties(), 2250 JSType::JS_OBJECT, objFuncPrototypeVal); 2251 JSHandle<JSObject> typedArrFuncPrototype = factory_->NewJSObjectWithInit(typedArrFuncPrototypeHClass); 2252 JSHandle<JSTaggedValue> typedArrFuncPrototypeValue(typedArrFuncPrototype); 2253 2254 // TypedArray.prototype_or_hclass 2255 JSHandle<JSHClass> typedArrFuncInstanceHClass = factory_->NewEcmaHClass( 2256 JSTypedArray::SIZE, JSType::JS_TYPED_ARRAY, typedArrFuncPrototypeValue); 2257 2258 // TypedArray = new Function() 2259 JSHandle<JSObject> typedArrayFunction(NewBuiltinConstructor( 2260 env, typedArrFuncPrototype, BuiltinsTypedArray::TypedArrayBaseConstructor, "TypedArray", FunctionLength::ZERO)); 2261 2262 JSHandle<JSFunction>(typedArrayFunction) 2263 ->SetProtoOrHClass(thread_, typedArrFuncInstanceHClass.GetTaggedValue()); 2264 2265 // TypedArray.prototype method 2266 for (const base::BuiltinFunctionEntry &entry: BuiltinsTypedArray::GetTypedArrayPrototypeFunctions()) { 2267 SetFunction(env, typedArrFuncPrototype, entry.GetName(), entry.GetEntrypoint(), 2268 entry.GetLength(), entry.GetBuiltinStubId()); 2269 } 2270 // TypedArray.prototype get accessor 2271 for (const base::BuiltinFunctionEntry &entry: BuiltinsTypedArray::GetTypedArrayPrototypeAccessors()) { 2272 JSHandle<JSTaggedValue> getter = 2273 CreateGetter(env, entry.GetEntrypoint(), entry.GetName(), entry.GetLength()); 2274 JSHandle<JSTaggedValue> key(factory_->NewFromASCIIReadOnly(entry.GetName())); 2275 SetGetter(typedArrFuncPrototype, key, getter); 2276 } 2277 2278 // %TypedArray%.prototype.toString(), which is strictly equal to Array.prototype.toString 2279 JSHandle<JSTaggedValue> arrFuncPrototype = env->GetArrayPrototype(); 2280 JSHandle<JSTaggedValue> toStringFunc = 2281 JSObject::GetMethod(thread_, arrFuncPrototype, thread_->GlobalConstants()->GetHandledToStringString()); 2282 RETURN_IF_ABRUPT_COMPLETION(thread_); 2283 PropertyDescriptor toStringDesc(thread_, toStringFunc, true, false, true); 2284 JSObject::DefineOwnProperty(thread_, typedArrFuncPrototype, thread_->GlobalConstants()->GetHandledToStringString(), 2285 toStringDesc); 2286 2287 // %TypedArray%.prototype [ @@iterator ] ( ) 2288 JSHandle<JSTaggedValue> values(factory_->NewFromASCIIReadOnly("values")); 2289 JSHandle<JSTaggedValue> iteratorSymbol = env->GetIteratorSymbol(); 2290 JSHandle<JSTaggedValue> valuesFunc = 2291 JSObject::GetMethod(thread_, JSHandle<JSTaggedValue>::Cast(typedArrFuncPrototype), values); 2292 RETURN_IF_ABRUPT_COMPLETION(thread_); 2293 PropertyDescriptor iteartorDesc(thread_, valuesFunc, true, false, true); 2294 JSObject::DefineOwnProperty(thread_, typedArrFuncPrototype, iteratorSymbol, iteartorDesc); 2295 2296 // 22.2.3.31 get %TypedArray%.prototype [ @@toStringTag ] 2297 JSHandle<JSTaggedValue> toStringTagSymbol = env->GetToStringTagSymbol(); 2298 JSHandle<JSTaggedValue> toStringTagGetter = 2299 CreateGetter(env, BuiltinsTypedArray::ToStringTag, "[Symbol.toStringTag]", FunctionLength::ZERO); 2300 SetGetter(typedArrFuncPrototype, toStringTagSymbol, toStringTagGetter); 2301 2302 // TypedArray method 2303 for (const base::BuiltinFunctionEntry &entry: BuiltinsTypedArray::GetTypedArrayFunctions()) { 2304 SetFunction(env, typedArrayFunction, entry.GetName(), entry.GetEntrypoint(), 2305 entry.GetLength(), entry.GetBuiltinStubId()); 2306 } 2307 2308 // 22.2.2.4 get %TypedArray% [ @@species ] 2309 JSHandle<JSTaggedValue> speciesSymbol = env->GetSpeciesSymbol(); 2310 JSHandle<JSTaggedValue> speciesGetter = 2311 CreateGetter(env, BuiltinsTypedArray::Species, "[Symbol.species]", FunctionLength::ZERO); 2312 SetGetter(JSHandle<JSObject>(typedArrayFunction), speciesSymbol, speciesGetter); 2313 2314 env->SetTypedArrayFunction(thread_, typedArrayFunction.GetTaggedValue()); 2315 env->SetTypedArrayPrototype(thread_, typedArrFuncPrototype); 2316 env->SetTypedArrayProtoValuesFunction(thread_, valuesFunc); 2317 thread_->SetInitialBuiltinHClass(BuiltinTypeId::TYPED_ARRAY, 2318 typedArrayFunction->GetJSHClass(), 2319 *typedArrFuncInstanceHClass, 2320 typedArrFuncPrototype->GetJSHClass()); 2321 2322 JSHandle<JSHClass> specificTypedArrayFuncClass = 2323 factory_->NewEcmaHClass(JSFunction::SIZE, JSType::JS_FUNCTION, env->GetTypedArrayFunction()); 2324 specificTypedArrayFuncClass->SetConstructor(true); 2325 env->SetSpecificTypedArrayFunctionClass(thread_, specificTypedArrayFuncClass); 2326 2327#define BUILTIN_TYPED_ARRAY_CALL_INITIALIZE(Type, TYPE, bytesPerElement) \ 2328 Initialize##Type(env, typedArrFuncInstanceHClass); 2329 BUILTIN_TYPED_ARRAY_TYPES(BUILTIN_TYPED_ARRAY_CALL_INITIALIZE) 2330#undef BUILTIN_TYPED_ARRAY_CALL_INITIALIZE 2331} 2332 2333void Builtins::LazyInitializeTypedArray(const JSHandle<GlobalEnv> &env) const 2334{ 2335 [[maybe_unused]] EcmaHandleScope scope(thread_); 2336 JSHandle<JSObject> globalObject(thread_, env->GetGlobalObject()); 2337 JSHandle<JSTaggedValue> key(factory_->NewFromUtf8ReadOnly("TypedArray")); 2338 auto accessor = factory_->NewInternalAccessor(nullptr, 2339 reinterpret_cast<void *>(BuiltinsLazyCallback::TypedArray)); 2340 SetLazyAccessor(globalObject, key, accessor); 2341 env->SetTypedArrayFunction(thread_, accessor); 2342 env->SetTypedArrayPrototype(thread_, accessor); 2343 env->SetSpecificTypedArrayFunctionClass(thread_, accessor); 2344 2345#define BUILTIN_TYPED_ARRAY_CALL_LAZY_INITIALIZE(Type, TYPE, bytesPerElement) \ 2346 LazyInitialize##Type(env); 2347 BUILTIN_TYPED_ARRAY_TYPES(BUILTIN_TYPED_ARRAY_CALL_LAZY_INITIALIZE) 2348#undef BUILTIN_TYPED_ARRAY_CALL_LAZY_INITIALIZE 2349} 2350 2351#define BUILTIN_TYPED_ARRAY_DEFINE_INITIALIZE(Type, TYPE, bytesPerElement) \ 2352void Builtins::Initialize##Type(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &arrFuncClass) const \ 2353{ \ 2354 [[maybe_unused]] EcmaHandleScope scope(thread_); \ 2355 /* %TypedArray%.prototype (where %TypedArray% is one of Int8Array, Uint8Array, etc.) */ \ 2356 JSHandle<JSObject> arrFuncPrototype = factory_->NewJSObjectWithInit(arrFuncClass); \ 2357 JSHandle<JSTaggedValue> arrFuncPrototypeValue(arrFuncPrototype); \ 2358 /* %TypedArray%.prototype_or_hclass */ \ 2359 JSHandle<JSHClass> arrFuncInstanceHClass = factory_->NewEcmaHClass( \ 2360 panda::ecmascript::JSTypedArray::SIZE, JSType::JS_##TYPE, arrFuncPrototypeValue); \ 2361 JSHandle<JSHClass> arrFuncInstanceHClassOnHeap = factory_->NewEcmaHClass( \ 2362 panda::ecmascript::JSTypedArray::SIZE, JSType::JS_##TYPE, arrFuncPrototypeValue); \ 2363 arrFuncInstanceHClassOnHeap->SetIsOnHeap(true); \ 2364 arrFuncInstanceHClass->SetHasConstructor(false); \ 2365 /* %TypedArray% = new Function() */ \ 2366 JSHandle<JSFunction> arrayFunction = factory_->NewSpecificTypedArrayFunction( \ 2367 env, reinterpret_cast<void *>(BuiltinsTypedArray::Type##Constructor), \ 2368 kungfu::BuiltinsStubCSigns::Type##Constructor); \ 2369 InitializeCtor(env, arrFuncPrototype, arrayFunction, #Type, FunctionLength::THREE); \ 2370 \ 2371 arrayFunction->SetProtoOrHClass(thread_, arrFuncInstanceHClass.GetTaggedValue()); \ 2372 SetConstant(arrFuncPrototype, "BYTES_PER_ELEMENT", JSTaggedValue(bytesPerElement)); \ 2373 SetConstant(JSHandle<JSObject>(arrayFunction), "BYTES_PER_ELEMENT", JSTaggedValue(bytesPerElement)); \ 2374 /* %TypedArray%.protoofprototype (where %TypedArray% is one of Int8Array, Uint8Array, etc.) */ \ 2375 JSTaggedValue protoOfPrototypeValue = arrFuncPrototype->GetJSHClass()->GetPrototype(); \ 2376 env->Set##Type##Function(thread_, arrayFunction); \ 2377 env->Set##Type##FunctionPrototype(thread_, arrFuncPrototypeValue); \ 2378 env->Set##Type##RootHclass(thread_, arrFuncInstanceHClass); \ 2379 env->Set##Type##RootHclassOnHeap(thread_, arrFuncInstanceHClassOnHeap); \ 2380 /* Initializes HClass record of %TypedArray% */ \ 2381 thread_->SetInitialBuiltinHClass(BuiltinTypeId::TYPE, \ 2382 arrayFunction->GetJSHClass(), \ 2383 *arrFuncInstanceHClass, \ 2384 arrFuncPrototype->GetJSHClass(), \ 2385 protoOfPrototypeValue.IsHeapObject() ? protoOfPrototypeValue.GetTaggedObject()->GetClass() : nullptr, \ 2386 *arrFuncInstanceHClassOnHeap); \ 2387} 2388 2389BUILTIN_TYPED_ARRAY_TYPES(BUILTIN_TYPED_ARRAY_DEFINE_INITIALIZE) 2390#undef BUILTIN_TYPED_ARRAY_DEFINE_INITIALIZE 2391 2392#define BUILTIN_TYPED_ARRAY_DEFINE_LAZY_INITIALIZE(Type, TYPE, bytesPerElement) \ 2393void Builtins::LazyInitialize##Type(const JSHandle<GlobalEnv> &env) const \ 2394{ \ 2395 [[maybe_unused]] EcmaHandleScope scope(thread_); \ 2396 JSHandle<JSObject> globalObject(thread_, env->GetGlobalObject()); \ 2397 JSHandle<JSTaggedValue> key(factory_->NewFromUtf8ReadOnly(#Type)); \ 2398 auto accessor = factory_->NewInternalAccessor(nullptr, reinterpret_cast<void *>(BuiltinsLazyCallback::Type)); \ 2399 SetLazyAccessor(globalObject, key, accessor); \ 2400 env->Set##Type##Function(thread_, accessor); \ 2401 env->Set##Type##FunctionPrototype(thread_, accessor); \ 2402 env->Set##Type##RootHclass(thread_, accessor); \ 2403 env->Set##Type##RootHclassOnHeap(thread_, accessor); \ 2404} 2405 2406BUILTIN_TYPED_ARRAY_TYPES(BUILTIN_TYPED_ARRAY_DEFINE_LAZY_INITIALIZE) 2407#undef BUILTIN_TYPED_ARRAY_DEFINE_LAZY_INITIALIZE 2408 2409void Builtins::InitializeArrayBuffer(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncClass) const 2410{ 2411 [[maybe_unused]] EcmaHandleScope scope(thread_); 2412 // ArrayBuffer.prototype 2413 JSHandle<JSObject> arrayBufferFuncPrototype = factory_->NewJSObjectWithInit(objFuncClass); 2414 JSHandle<JSTaggedValue> arrayBufferFuncPrototypeValue(arrayBufferFuncPrototype); 2415 2416 // ArrayBuffer.prototype_or_hclass 2417 JSHandle<JSHClass> arrayBufferFuncInstanceHClass = 2418 factory_->NewEcmaHClass(JSArrayBuffer::SIZE, JSType::JS_ARRAY_BUFFER, arrayBufferFuncPrototypeValue); 2419 2420 // ArrayBuffer = new Function() 2421 JSHandle<JSObject> arrayBufferFunction(NewBuiltinConstructor( 2422 env, arrayBufferFuncPrototype, ArrayBuffer::ArrayBufferConstructor, "ArrayBuffer", FunctionLength::ONE)); 2423 2424 JSFunction::SetFunctionPrototypeOrInstanceHClass(thread_, 2425 JSHandle<JSFunction>(arrayBufferFunction), 2426 arrayBufferFuncInstanceHClass.GetTaggedValue()); 2427 2428 // ArrayBuffer prototype method 2429 SetFunction(env, arrayBufferFuncPrototype, "slice", ArrayBuffer::Slice, FunctionLength::TWO); 2430 2431 // ArrayBuffer method 2432 for (const base::BuiltinFunctionEntry& entry: ArrayBuffer::GetArrayBufferFunctions()) { 2433 SetFunction(env, 2434 arrayBufferFunction, 2435 entry.GetName(), 2436 entry.GetEntrypoint(), 2437 entry.GetLength(), 2438 entry.GetBuiltinStubId()); 2439 } 2440 2441 // 24.1.3.3 get ArrayBuffer[@@species] 2442 JSHandle<JSTaggedValue> speciesSymbol = env->GetSpeciesSymbol(); 2443 JSHandle<JSTaggedValue> speciesGetter = 2444 CreateGetter(env, ArrayBuffer::Species, "[Symbol.species]", FunctionLength::ZERO); 2445 SetGetter(JSHandle<JSObject>(arrayBufferFunction), speciesSymbol, speciesGetter); 2446 2447 // 24.1.4.1 get ArrayBuffer.prototype.byteLength 2448 JSHandle<JSTaggedValue> lengthGetter = 2449 CreateGetter(env, ArrayBuffer::GetByteLength, "byteLength", FunctionLength::ZERO); 2450 JSHandle<JSTaggedValue> lengthKey(factory_->NewFromASCIIReadOnly("byteLength")); 2451 SetGetter(arrayBufferFuncPrototype, lengthKey, lengthGetter); 2452 2453 // 24.1.4.4 ArrayBuffer.prototype[@@toStringTag] 2454 SetStringTagSymbol(env, arrayBufferFuncPrototype, "ArrayBuffer"); 2455 2456 env->SetArrayBufferFunction(thread_, arrayBufferFunction.GetTaggedValue()); 2457} 2458 2459void Builtins::LazyInitializeArrayBuffer(const JSHandle<GlobalEnv> &env) const 2460{ 2461 [[maybe_unused]] EcmaHandleScope scope(thread_); 2462 JSHandle<JSObject> globalObject(thread_, env->GetGlobalObject()); 2463 JSHandle<JSTaggedValue> key(factory_->NewFromUtf8ReadOnly("ArrayBuffer")); 2464 auto accessor = factory_->NewInternalAccessor(nullptr, 2465 reinterpret_cast<void *>(BuiltinsLazyCallback::ArrayBuffer)); 2466 SetLazyAccessor(globalObject, key, accessor); 2467 env->SetArrayBufferFunction(thread_, accessor); 2468} 2469 2470void Builtins::InitializeReflect(const JSHandle<GlobalEnv> &env, 2471 const JSHandle<JSTaggedValue> &objFuncPrototypeVal) const 2472{ 2473 [[maybe_unused]] EcmaHandleScope scope(thread_); 2474 JSHandle<JSHClass> reflectHClass = 2475 factory_->NewEcmaHClass(JSObject::SIZE, JSType::JS_OBJECT, objFuncPrototypeVal); 2476 JSHandle<JSObject> reflectObject = factory_->NewJSObjectWithInit(reflectHClass); 2477 2478 // Reflect functions 2479 for (const base::BuiltinFunctionEntry &entry: Reflect::GetReflectFunctions()) { 2480 SetFunction(env, reflectObject, entry.GetName(), entry.GetEntrypoint(), 2481 entry.GetLength(), entry.GetBuiltinStubId()); 2482 } 2483 2484 JSHandle<JSTaggedValue> reflectString(factory_->NewFromASCIIReadOnly("Reflect")); 2485 JSHandle<JSObject> globalObject(thread_, env->GetGlobalObject()); 2486 PropertyDescriptor reflectDesc(thread_, JSHandle<JSTaggedValue>::Cast(reflectObject), true, false, true); 2487 JSObject::DefineOwnProperty(thread_, globalObject, reflectString, reflectDesc); 2488 2489 // @@ToStringTag 2490 SetStringTagSymbol(env, reflectObject, "Reflect"); 2491 2492 env->SetReflectFunction(thread_, reflectObject.GetTaggedValue()); 2493} 2494 2495void Builtins::InitializeSharedArrayBuffer(const JSHandle<GlobalEnv> &env, 2496 const JSHandle<JSHClass> &objFuncClass) const 2497{ 2498 [[maybe_unused]] EcmaHandleScope scope(thread_); 2499 // SharedArrayBuffer.prototype 2500 JSHandle<JSObject> sharedArrayBufferFuncPrototype = factory_->NewJSObjectWithInit(objFuncClass); 2501 JSHandle<JSTaggedValue> sharedArrayBufferFuncPrototypeValue(sharedArrayBufferFuncPrototype); 2502 2503 // SharedArrayBuffer.prototype_or_hclass 2504 JSHandle<JSHClass> sharedArrayBufferFuncInstanceHClass = 2505 factory_->NewEcmaHClass( 2506 JSArrayBuffer::SIZE, JSType::JS_SHARED_ARRAY_BUFFER, sharedArrayBufferFuncPrototypeValue); 2507 2508 // SharedArrayBuffer = new Function() 2509 JSHandle<JSObject> SharedArrayBufferFunction(NewBuiltinConstructor(env, sharedArrayBufferFuncPrototype, 2510 SharedArrayBuffer::SharedArrayBufferConstructor, "SharedArrayBuffer", FunctionLength::ONE)); 2511 2512 JSFunction::SetFunctionPrototypeOrInstanceHClass(thread_, 2513 JSHandle<JSFunction>(SharedArrayBufferFunction), 2514 sharedArrayBufferFuncInstanceHClass.GetTaggedValue()); 2515 2516 // SharedArrayBuffer prototype method 2517 SetFunction(env, sharedArrayBufferFuncPrototype, "slice", SharedArrayBuffer::Slice, FunctionLength::TWO); 2518 2519 // SharedArrayBuffer method 2520 SetFunction(env, SharedArrayBufferFunction, 2521 "IsSharedArrayBuffer", SharedArrayBuffer::IsSharedArrayBuffer, FunctionLength::ONE); 2522 2523 // 25.2.3.2 get SharedArrayBuffer [ @@species ] 2524 JSHandle<JSTaggedValue> speciesSymbol = env->GetSpeciesSymbol(); 2525 JSHandle<JSTaggedValue> speciesGetter = 2526 CreateGetter(env, SharedArrayBuffer::Species, "[Symbol.species]", FunctionLength::ZERO); 2527 SetGetter(JSHandle<JSObject>(SharedArrayBufferFunction), speciesSymbol, speciesGetter); 2528 2529 // 25.2.4.1 get SharedArrayBuffer.prototype.byteLength 2530 JSHandle<JSTaggedValue> lengthGetter = 2531 CreateGetter(env, SharedArrayBuffer::GetByteLength, "byteLength", FunctionLength::ZERO); 2532 JSHandle<JSTaggedValue> lengthKey(factory_->NewFromASCIIReadOnly("byteLength")); 2533 SetGetter(sharedArrayBufferFuncPrototype, lengthKey, lengthGetter); 2534 2535 // 25.2.4.4 SharedArrayBuffer.prototype [ @@toStringTag ] 2536 SetStringTagSymbol(env, sharedArrayBufferFuncPrototype, "SharedArrayBuffer"); 2537 2538 env->SetSharedArrayBufferFunction(thread_, SharedArrayBufferFunction.GetTaggedValue()); 2539} 2540 2541void Builtins::LazyInitializeSharedArrayBuffer(const JSHandle<GlobalEnv> &env) const 2542{ 2543 [[maybe_unused]] EcmaHandleScope scope(thread_); 2544 JSHandle<JSObject> globalObject(thread_, env->GetGlobalObject()); 2545 JSHandle<JSTaggedValue> key(factory_->NewFromUtf8ReadOnly("SharedArrayBuffer")); 2546 auto accessor = 2547 factory_->NewInternalAccessor(nullptr, reinterpret_cast<void *>(BuiltinsLazyCallback::SharedArrayBuffer)); 2548 SetLazyAccessor(globalObject, key, accessor); 2549 env->SetSharedArrayBufferFunction(thread_, accessor); 2550} 2551 2552void Builtins::InitializePromise(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &promiseFuncClass) 2553{ 2554 [[maybe_unused]] EcmaHandleScope scope(thread_); 2555 // Promise.prototype 2556 JSHandle<JSObject> promiseFuncPrototype = factory_->NewJSObjectWithInit(promiseFuncClass); 2557 JSHandle<JSTaggedValue> promiseFuncPrototypeValue(promiseFuncPrototype); 2558 // Promise.prototype_or_hclass 2559 JSHandle<JSHClass> promiseFuncInstanceHClass = 2560 factory_->NewEcmaHClass(JSPromise::SIZE, JSType::JS_PROMISE, promiseFuncPrototypeValue); 2561 // Promise() = new Function() 2562 JSHandle<JSObject> promiseFunction( 2563 NewBuiltinConstructor(env, promiseFuncPrototype, Promise::PromiseConstructor, "Promise", FunctionLength::ONE)); 2564 JSFunction::SetFunctionPrototypeOrInstanceHClass(thread_, 2565 JSHandle<JSFunction>(promiseFunction), 2566 promiseFuncInstanceHClass.GetTaggedValue()); 2567 2568 // Promise method 2569 for (const base::BuiltinFunctionEntry &entry: Promise::GetPromiseFunctions()) { 2570 SetFunction(env, promiseFunction, entry.GetName(), entry.GetEntrypoint(), 2571 entry.GetLength(), entry.GetBuiltinStubId()); 2572 } 2573 // promise.prototype method 2574 for (const base::BuiltinFunctionEntry &entry: Promise::GetPromisePrototypeFunctions()) { 2575 SetFunction(env, promiseFuncPrototype, entry.GetName(), entry.GetEntrypoint(), 2576 entry.GetLength(), entry.GetBuiltinStubId()); 2577 } 2578 // Promise.prototype [ @@toStringTag ] 2579 SetStringTagSymbol(env, promiseFuncPrototype, "Promise"); 2580 2581 // Set Promise [@@species] 2582 JSHandle<JSTaggedValue> speciesSymbol(env->GetSpeciesSymbol()); 2583 JSHandle<JSTaggedValue> speciesGetter = 2584 CreateGetter(env, Promise::GetSpecies, "[Symbol.species]", FunctionLength::ZERO); 2585 SetGetter(promiseFunction, speciesSymbol, speciesGetter); 2586 2587 env->SetPromiseFunction(thread_, promiseFunction); 2588 InitializeForPromiseFuncClass(env); 2589} 2590 2591 2592void Builtins::InitializeForPromiseFuncClass(const JSHandle<GlobalEnv> &env) 2593{ 2594 vm_ = thread_->GetEcmaVM(); 2595 factory_ = vm_->GetFactory(); 2596 2597 JSHandle<JSHClass> promiseReactionFuncClass = factory_->NewEcmaHClass( 2598 JSPromiseReactionsFunction::SIZE, JSType::JS_PROMISE_REACTIONS_FUNCTION, env->GetFunctionPrototype()); 2599 promiseReactionFuncClass->SetCallable(true); 2600 promiseReactionFuncClass->SetExtensible(true); 2601 env->SetPromiseReactionFunctionClass(thread_, promiseReactionFuncClass); 2602 2603 JSHandle<JSHClass> promiseExecutorFuncClass = factory_->NewEcmaHClass( 2604 JSPromiseExecutorFunction::SIZE, JSType::JS_PROMISE_EXECUTOR_FUNCTION, env->GetFunctionPrototype()); 2605 promiseExecutorFuncClass->SetCallable(true); 2606 promiseExecutorFuncClass->SetExtensible(true); 2607 env->SetPromiseExecutorFunctionClass(thread_, promiseExecutorFuncClass); 2608 2609 JSHandle<JSHClass> asyncModuleFulfilledFuncClass = factory_->NewEcmaHClass( 2610 JSAsyncModuleFulfilledFunction::SIZE, JSType::JS_ASYNC_MODULE_FULFILLED_FUNCTION, env->GetFunctionPrototype()); 2611 asyncModuleFulfilledFuncClass->SetCallable(true); 2612 asyncModuleFulfilledFuncClass->SetExtensible(true); 2613 env->SetAsyncModuleFulfilledFunctionClass(thread_, asyncModuleFulfilledFuncClass); 2614 2615 JSHandle<JSHClass> asyncModuleRejectedFuncClass = factory_->NewEcmaHClass( 2616 JSAsyncModuleRejectedFunction::SIZE, JSType::JS_ASYNC_MODULE_REJECTED_FUNCTION, env->GetFunctionPrototype()); 2617 asyncModuleRejectedFuncClass->SetCallable(true); 2618 asyncModuleRejectedFuncClass->SetExtensible(true); 2619 env->SetAsyncModuleRejectedFunctionClass(thread_, asyncModuleRejectedFuncClass); 2620 2621 JSHandle<JSHClass> promiseAllResolveElementFunctionClass = 2622 factory_->NewEcmaHClass(JSPromiseAllResolveElementFunction::SIZE, 2623 JSType::JS_PROMISE_ALL_RESOLVE_ELEMENT_FUNCTION, env->GetFunctionPrototype()); 2624 promiseAllResolveElementFunctionClass->SetCallable(true); 2625 promiseAllResolveElementFunctionClass->SetExtensible(true); 2626 env->SetPromiseAllResolveElementFunctionClass(thread_, promiseAllResolveElementFunctionClass); 2627 2628 JSHandle<JSHClass> promiseAnyRejectElementFunctionClass = 2629 factory_->NewEcmaHClass(JSPromiseAnyRejectElementFunction::SIZE, 2630 JSType::JS_PROMISE_ANY_REJECT_ELEMENT_FUNCTION, env->GetFunctionPrototype()); 2631 promiseAnyRejectElementFunctionClass->SetCallable(true); 2632 promiseAnyRejectElementFunctionClass->SetExtensible(true); 2633 env->SetPromiseAnyRejectElementFunctionClass(thread_, promiseAnyRejectElementFunctionClass); 2634 2635 JSHandle<JSHClass> promiseAllSettledElementFunctionClass = 2636 factory_->NewEcmaHClass(JSPromiseAllSettledElementFunction::SIZE, 2637 JSType::JS_PROMISE_ALL_SETTLED_ELEMENT_FUNCTION, env->GetFunctionPrototype()); 2638 promiseAllSettledElementFunctionClass->SetCallable(true); 2639 promiseAllSettledElementFunctionClass->SetExtensible(true); 2640 env->SetPromiseAllSettledElementFunctionClass(thread_, promiseAllSettledElementFunctionClass); 2641 2642 JSHandle<JSHClass> promiseFinallyFunctionClass = 2643 factory_->NewEcmaHClass(JSPromiseFinallyFunction::SIZE, 2644 JSType::JS_PROMISE_FINALLY_FUNCTION, env->GetFunctionPrototype()); 2645 promiseFinallyFunctionClass->SetCallable(true); 2646 promiseFinallyFunctionClass->SetExtensible(true); 2647 env->SetPromiseFinallyFunctionClass(thread_, promiseFinallyFunctionClass); 2648 2649 JSHandle<JSHClass> promiseValueThunkOrThrowerFunctionClass = 2650 factory_->NewEcmaHClass(JSPromiseValueThunkOrThrowerFunction::SIZE, 2651 JSType::JS_PROMISE_VALUE_THUNK_OR_THROWER_FUNCTION, env->GetFunctionPrototype()); 2652 promiseValueThunkOrThrowerFunctionClass->SetCallable(true); 2653 promiseValueThunkOrThrowerFunctionClass->SetExtensible(true); 2654 env->SetPromiseValueThunkOrThrowerFunctionClass(thread_, promiseValueThunkOrThrowerFunctionClass); 2655} 2656 2657void Builtins::InitializePromiseJob(const JSHandle<GlobalEnv> &env) 2658{ 2659 JSHandle<JSTaggedValue> keyString(thread_->GlobalConstants()->GetHandledEmptyString()); 2660 auto func = NewFunction(env, keyString, BuiltinsPromiseJob::PromiseReactionJob, FunctionLength::TWO); 2661 env->SetPromiseReactionJob(thread_, func); 2662 func = NewFunction(env, keyString, BuiltinsPromiseJob::PromiseResolveThenableJob, FunctionLength::THREE); 2663 env->SetPromiseResolveThenableJob(thread_, func); 2664 func = NewFunction(env, keyString, BuiltinsPromiseJob::DynamicImportJob, FunctionLength::FOUR); 2665 env->SetDynamicImportJob(thread_, func); 2666} 2667 2668void Builtins::InitializeDataView(const JSHandle<GlobalEnv> &env, JSHandle<JSTaggedValue> objFuncPrototypeVal) const 2669{ 2670 [[maybe_unused]] EcmaHandleScope scope(thread_); 2671 // ArrayBuffer.prototype 2672 JSHandle<JSHClass> dataViewFuncPrototypeHClass = factory_->NewEcmaHClass( 2673 JSObject::SIZE, DataView::GetNumPrototypeInlinedProperties(), JSType::JS_OBJECT, objFuncPrototypeVal); 2674 JSHandle<JSObject> dataViewFuncPrototype = factory_->NewJSObjectWithInit(dataViewFuncPrototypeHClass); 2675 JSHandle<JSTaggedValue> dataViewFuncPrototypeValue(dataViewFuncPrototype); 2676 2677 // ArrayBuffer.prototype_or_hclass 2678 JSHandle<JSHClass> dataViewFuncInstanceHClass = 2679 factory_->NewEcmaHClass(JSDataView::SIZE, JSType::JS_DATA_VIEW, dataViewFuncPrototypeValue); 2680 2681 // ArrayBuffer = new Function() 2682 JSHandle<JSObject> dataViewFunction(NewBuiltinConstructor(env, dataViewFuncPrototype, DataView::DataViewConstructor, 2683 "DataView", FunctionLength::ONE)); 2684 2685 JSHandle<JSFunction>(dataViewFunction)->SetProtoOrHClass(thread_, dataViewFuncInstanceHClass.GetTaggedValue()); 2686 // DataView.prototype method 2687 for (const base::BuiltinFunctionEntry &entry: DataView::GetDataViewPrototypeFunctions()) { 2688 SetFunction(env, dataViewFuncPrototype, entry.GetName(), entry.GetEntrypoint(), 2689 entry.GetLength(), entry.GetBuiltinStubId()); 2690 } 2691 2692 // 24.2.4.1 get DataView.prototype.buffer 2693 JSHandle<JSTaggedValue> bufferGetter = CreateGetter(env, DataView::GetBuffer, "buffer", FunctionLength::ZERO); 2694 JSHandle<JSTaggedValue> bufferKey(factory_->NewFromASCIIReadOnly("buffer")); 2695 SetGetter(dataViewFuncPrototype, bufferKey, bufferGetter); 2696 2697 // 24.2.4.2 get DataView.prototype.byteLength 2698 JSHandle<JSTaggedValue> lengthGetter = 2699 CreateGetter(env, DataView::GetByteLength, "byteLength", FunctionLength::ZERO); 2700 JSHandle<JSTaggedValue> lengthKey(factory_->NewFromASCIIReadOnly("byteLength")); 2701 SetGetter(dataViewFuncPrototype, lengthKey, lengthGetter); 2702 2703 // 24.2.4.3 get DataView.prototype.byteOffset 2704 JSHandle<JSTaggedValue> offsetGetter = CreateGetter(env, DataView::GetOffset, "byteOffset", FunctionLength::ZERO); 2705 JSHandle<JSTaggedValue> offsetKey(factory_->NewFromASCIIReadOnly("byteOffset")); 2706 SetGetter(dataViewFuncPrototype, offsetKey, offsetGetter); 2707 2708 // 24.2.4.21 DataView.prototype[ @@toStringTag ] 2709 SetStringTagSymbol(env, dataViewFuncPrototype, "DataView"); 2710 2711 env->SetDataViewFunction(thread_, dataViewFunction.GetTaggedValue()); 2712 env->SetDataViewPrototype(thread_, dataViewFuncPrototype.GetTaggedValue()); 2713 thread_->SetInitialBuiltinHClass(BuiltinTypeId::DATA_VIEW, 2714 dataViewFunction->GetJSHClass(), 2715 *dataViewFuncInstanceHClass, 2716 dataViewFuncPrototype->GetJSHClass()); 2717} 2718 2719void Builtins::LazyInitializeDataView(const JSHandle<GlobalEnv> &env) const 2720{ 2721 [[maybe_unused]] EcmaHandleScope scope(thread_); 2722 JSHandle<JSObject> globalObject(thread_, env->GetGlobalObject()); 2723 JSHandle<JSTaggedValue> key(factory_->NewFromUtf8ReadOnly("DataView")); 2724 auto accessor = factory_->NewInternalAccessor(nullptr, reinterpret_cast<void *>(BuiltinsLazyCallback::DataView)); 2725 SetLazyAccessor(globalObject, key, accessor); 2726 env->SetDataViewFunction(thread_, accessor); 2727 env->SetDataViewPrototype(thread_, accessor); 2728} 2729 2730JSHandle<JSFunction> Builtins::NewBuiltinConstructor(const JSHandle<GlobalEnv> &env, 2731 const JSHandle<JSObject> &prototype, EcmaEntrypoint ctorFunc, 2732 std::string_view name, int length, 2733 kungfu::BuiltinsStubCSigns::ID builtinId) const 2734{ 2735 JSHandle<JSFunction> ctor = 2736 factory_->NewJSFunction(env, reinterpret_cast<void *>(ctorFunc), FunctionKind::BUILTIN_CONSTRUCTOR, builtinId); 2737 InitializeCtor(env, prototype, ctor, name, length); 2738 return ctor; 2739} 2740 2741JSHandle<JSFunction> Builtins::NewBuiltinCjsCtor(const JSHandle<GlobalEnv> &env, 2742 const JSHandle<JSObject> &prototype, EcmaEntrypoint ctorFunc, 2743 std::string_view name, int length) const 2744{ 2745 JSHandle<JSFunction> ctor = 2746 factory_->NewJSFunction(env, reinterpret_cast<void *>(ctorFunc), FunctionKind::BUILTIN_CONSTRUCTOR); 2747 2748 const GlobalEnvConstants *globalConst = thread_->GlobalConstants(); 2749 JSFunction::SetFunctionLength(thread_, ctor, JSTaggedValue(length)); 2750 JSHandle<JSTaggedValue> nameString(factory_->NewFromUtf8ReadOnly(name)); 2751 JSFunction::SetFunctionName(thread_, JSHandle<JSFunctionBase>(ctor), nameString, 2752 JSHandle<JSTaggedValue>(thread_, JSTaggedValue::Undefined())); 2753 JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString(); 2754 PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>::Cast(ctor), true, false, true); 2755 JSObject::DefineOwnProperty(thread_, prototype, constructorKey, descriptor); 2756 2757 return ctor; 2758} 2759 2760JSHandle<JSFunction> Builtins::NewFunction(const JSHandle<GlobalEnv> &env, const JSHandle<JSTaggedValue> &key, 2761 EcmaEntrypoint func, int length, 2762 kungfu::BuiltinsStubCSigns::ID builtinId) const 2763{ 2764 MemSpaceType methodSpaceType = MemSpaceType::SHARED_NON_MOVABLE; 2765 JSHandle<JSFunction> function = factory_->NewJSFunction(env, reinterpret_cast<void *>(func), 2766 FunctionKind::NORMAL_FUNCTION, builtinId, methodSpaceType); 2767 JSFunction::SetFunctionLength(thread_, function, JSTaggedValue(length)); 2768 JSHandle<JSFunctionBase> baseFunction(function); 2769 auto globalConst = const_cast<GlobalEnvConstants *>(thread_->GlobalConstants()); 2770 JSFunction::SetFunctionName(thread_, baseFunction, key, globalConst->GetHandledUndefined()); 2771 if (IS_TYPED_BUILTINS_ID(builtinId) || IS_TYPED_INLINE_BUILTINS_ID(builtinId)) { 2772 globalConst->SetConstant(GET_TYPED_CONSTANT_INDEX(builtinId), function); 2773 } 2774 return function; 2775} 2776 2777void Builtins::SetFunction(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &obj, std::string_view key, 2778 EcmaEntrypoint func, int length, kungfu::BuiltinsStubCSigns::ID builtinId) const 2779{ 2780 JSHandle<JSTaggedValue> keyString(factory_->NewFromUtf8ReadOnly(key)); 2781 SetFunction(env, obj, keyString, func, length, builtinId); 2782} 2783 2784void Builtins::SetFunction(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &obj, 2785 const JSHandle<JSTaggedValue> &key, EcmaEntrypoint func, int length, 2786 kungfu::BuiltinsStubCSigns::ID builtinId) const 2787{ 2788 JSHandle<JSFunction> function(NewFunction(env, key, func, length, builtinId)); 2789 PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>(function), true, false, true); 2790 JSObject::DefineOwnProperty(thread_, obj, key, descriptor); 2791} 2792 2793JSHandle<JSFunction> Builtins::SetAndReturnFunction(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &obj, 2794 const char *key, EcmaEntrypoint func, int length, 2795 kungfu::BuiltinsStubCSigns::ID builtinId) const 2796{ 2797 JSHandle<JSTaggedValue> keyString(factory_->NewFromUtf8ReadOnly(key)); 2798 return SetAndReturnFunction(env, obj, keyString, func, length, builtinId); 2799} 2800 2801JSHandle<JSFunction> Builtins::SetAndReturnFunction(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &obj, 2802 const JSHandle<JSTaggedValue> &key, EcmaEntrypoint func, int length, 2803 kungfu::BuiltinsStubCSigns::ID builtinId) const 2804{ 2805 JSHandle<JSFunction> function(NewFunction(env, key, func, length, builtinId)); 2806 PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>(function), true, false, true); 2807 JSObject::DefineOwnProperty(thread_, obj, key, descriptor); 2808 return function; 2809} 2810 2811void Builtins::SetFrozenFunction(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &obj, std::string_view key, 2812 EcmaEntrypoint func, int length) const 2813{ 2814 JSHandle<JSTaggedValue> keyString(factory_->NewFromUtf8ReadOnly(key)); 2815 JSHandle<JSFunction> function = NewFunction(env, keyString, func, length); 2816 PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>(function), false, false, false); 2817 JSObject::DefineOwnProperty(thread_, obj, keyString, descriptor); 2818} 2819 2820template<int flag> 2821void Builtins::SetFunctionAtSymbol(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &obj, 2822 const JSHandle<JSTaggedValue> &symbol, std::string_view name, 2823 EcmaEntrypoint func, int length) const 2824{ 2825 JSHandle<JSFunction> function = factory_->NewJSFunction(env, reinterpret_cast<void *>(func)); 2826 JSFunction::SetFunctionLength(thread_, function, JSTaggedValue(length)); 2827 JSHandle<JSTaggedValue> nameString(factory_->NewFromUtf8ReadOnly(name)); 2828 JSHandle<JSFunctionBase> baseFunction(function); 2829 JSHandle<JSTaggedValue> handleUndefine(thread_, JSTaggedValue::Undefined()); 2830 JSFunction::SetFunctionName(thread_, baseFunction, nameString, handleUndefine); 2831 // NOLINTNEXTLINE(readability-braces-around-statements, bugprone-suspicious-semicolon) 2832 if constexpr (flag == JSSymbol::SYMBOL_TO_PRIMITIVE_TYPE) { 2833 PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>::Cast(function), false, false, true); 2834 JSObject::DefineOwnProperty(thread_, obj, symbol, descriptor); 2835 return; 2836 } else if constexpr (flag == JSSymbol::SYMBOL_HAS_INSTANCE_TYPE) { // NOLINTE(readability-braces-around-statements) 2837 // ecma 19.2.3.6 Function.prototype[@@hasInstance] has the attributes 2838 // { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }. 2839 PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>::Cast(function), false, false, false); 2840 JSObject::DefineOwnProperty(thread_, obj, symbol, descriptor); 2841 env->SetHasInstanceFunction(thread_, function); 2842 return; 2843 } 2844 PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>::Cast(function), true, false, true); 2845 JSObject::DefineOwnProperty(thread_, obj, symbol, descriptor); 2846} 2847 2848template<int flag> 2849JSHandle<JSTaggedValue> Builtins::SetAndReturnFunctionAtSymbol(const JSHandle<GlobalEnv> &env, 2850 const JSHandle<JSObject> &obj, 2851 const JSHandle<JSTaggedValue> &symbol, 2852 std::string_view name, 2853 EcmaEntrypoint func, 2854 int length, 2855 kungfu::BuiltinsStubCSigns::ID builtinId) const 2856{ 2857 JSHandle<JSFunction> function = factory_->NewJSFunction(env, reinterpret_cast<void *>(func), 2858 FunctionKind::NORMAL_FUNCTION, builtinId); 2859 JSFunction::SetFunctionLength(thread_, function, JSTaggedValue(length)); 2860 JSHandle<JSTaggedValue> nameString(factory_->NewFromUtf8ReadOnly(name)); 2861 JSHandle<JSFunctionBase> baseFunction(function); 2862 JSHandle<JSTaggedValue> handleUndefine(thread_, JSTaggedValue::Undefined()); 2863 JSFunction::SetFunctionName(thread_, baseFunction, nameString, handleUndefine); 2864 // NOLINTNEXTLINE(readability-braces-around-statements, bugprone-suspicious-semicolon) 2865 if constexpr (flag == JSSymbol::SYMBOL_TO_PRIMITIVE_TYPE) { 2866 PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>::Cast(function), false, false, true); 2867 JSObject::DefineOwnProperty(thread_, obj, symbol, descriptor); 2868 return JSHandle<JSTaggedValue>(function); 2869 } else if constexpr (flag == JSSymbol::SYMBOL_HAS_INSTANCE_TYPE) { // NOLINTE(readability-braces-around-statements) 2870 // ecma 19.2.3.6 Function.prototype[@@hasInstance] has the attributes 2871 // { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }. 2872 PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>::Cast(function), false, false, false); 2873 JSObject::DefineOwnProperty(thread_, obj, symbol, descriptor); 2874 env->SetHasInstanceFunction(thread_, function); 2875 return JSHandle<JSTaggedValue>(function); 2876 } 2877 PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>::Cast(function), true, false, true); 2878 JSObject::DefineOwnProperty(thread_, obj, symbol, descriptor); 2879 return JSHandle<JSTaggedValue>(function); 2880} 2881 2882void Builtins::SetStringTagSymbol(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &obj, 2883 std::string_view key) const 2884{ 2885 JSHandle<JSTaggedValue> tag(factory_->NewFromUtf8ReadOnly(key)); 2886 JSHandle<JSTaggedValue> symbol = env->GetToStringTagSymbol(); 2887 PropertyDescriptor desc(thread_, tag, false, false, true); 2888 JSObject::DefineOwnProperty(thread_, obj, symbol, desc); 2889} 2890 2891JSHandle<JSTaggedValue> Builtins::CreateGetter(const JSHandle<GlobalEnv> &env, EcmaEntrypoint func, 2892 std::string_view name, int length, 2893 kungfu::BuiltinsStubCSigns::ID builtinId) const 2894{ 2895 JSHandle<JSTaggedValue> funcName(factory_->NewFromUtf8ReadOnly(name)); 2896 return CreateGetter(env, func, funcName, length, builtinId); 2897} 2898 2899JSHandle<JSTaggedValue> Builtins::CreateGetter(const JSHandle<GlobalEnv> &env, EcmaEntrypoint func, 2900 JSHandle<JSTaggedValue> key, int length, 2901 kungfu::BuiltinsStubCSigns::ID builtinId) const 2902{ 2903 JSHandle<JSFunction> function = factory_->NewJSFunction(env, reinterpret_cast<void *>(func), 2904 FunctionKind::GETTER_FUNCTION, builtinId); 2905 JSFunction::SetFunctionLength(thread_, function, JSTaggedValue(length)); 2906 JSHandle<JSTaggedValue> prefix = thread_->GlobalConstants()->GetHandledGetString(); 2907 JSFunction::SetFunctionName(thread_, JSHandle<JSFunctionBase>(function), key, prefix); 2908 return JSHandle<JSTaggedValue>(function); 2909} 2910 2911JSHandle<JSTaggedValue> Builtins::CreateSetter(const JSHandle<GlobalEnv> &env, EcmaEntrypoint func, 2912 std::string_view name, int length) const 2913{ 2914 JSHandle<JSTaggedValue> funcName(factory_->NewFromUtf8ReadOnly(name)); 2915 return CreateSetter(env, func, funcName, length); 2916} 2917 2918JSHandle<JSTaggedValue> Builtins::CreateSetter(const JSHandle<GlobalEnv> &env, EcmaEntrypoint func, 2919 JSHandle<JSTaggedValue> key, int length) const 2920{ 2921 JSHandle<JSFunction> function = factory_->NewJSFunction(env, reinterpret_cast<void *>(func)); 2922 JSFunction::SetFunctionLength(thread_, function, JSTaggedValue(length)); 2923 JSHandle<JSTaggedValue> prefix = thread_->GlobalConstants()->GetHandledSetString(); 2924 JSFunction::SetFunctionName(thread_, JSHandle<JSFunctionBase>(function), key, prefix); 2925 return JSHandle<JSTaggedValue>(function); 2926} 2927 2928void Builtins::SetConstant(const JSHandle<JSObject> &obj, std::string_view key, JSTaggedValue value) const 2929{ 2930 JSHandle<JSTaggedValue> keyString(factory_->NewFromUtf8ReadOnly(key)); 2931 PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>(thread_, value), false, false, false); 2932 JSObject::DefineOwnProperty(thread_, obj, keyString, descriptor); 2933} 2934 2935void Builtins::SetConstantObject(const JSHandle<JSObject> &obj, std::string_view key, 2936 JSHandle<JSTaggedValue> &value) const 2937{ 2938 JSHandle<JSTaggedValue> keyString(factory_->NewFromUtf8ReadOnly(key)); 2939 PropertyDescriptor descriptor(thread_, value, false, false, false); 2940 JSObject::DefineOwnProperty(thread_, obj, keyString, descriptor); 2941} 2942 2943void Builtins::SetNonConstantObject(const JSHandle<JSObject> &obj, std::string_view key, 2944 JSHandle<JSTaggedValue> &value) const 2945{ 2946 JSHandle<JSTaggedValue> keyString(factory_->NewFromUtf8ReadOnly(key)); 2947 PropertyDescriptor descriptor(thread_, value, true, true, true); 2948 JSObject::DefineOwnProperty(thread_, obj, keyString, descriptor); 2949} 2950 2951void Builtins::SetGlobalThis(const JSHandle<JSObject> &obj, std::string_view key, 2952 const JSHandle<JSTaggedValue> &globalValue) 2953{ 2954 JSHandle<JSTaggedValue> keyString(factory_->NewFromUtf8ReadOnly(key)); 2955 PropertyDescriptor descriptor(thread_, globalValue, true, false, true); 2956 JSObject::DefineOwnProperty(thread_, obj, keyString, descriptor); 2957} 2958 2959void Builtins::SetAttribute(const JSHandle<JSObject> &obj, std::string_view key, std::string_view value) const 2960{ 2961 JSHandle<JSTaggedValue> keyString(factory_->NewFromUtf8ReadOnly(key)); 2962 PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>(factory_->NewFromUtf8ReadOnly(value)), 2963 true, false, true); 2964 JSObject::DefineOwnProperty(thread_, obj, keyString, descriptor); 2965} 2966 2967void Builtins::SetNoneAttributeProperty(const JSHandle<JSObject> &obj, std::string_view key, 2968 const JSHandle<JSTaggedValue> &value) const 2969{ 2970 JSHandle<JSTaggedValue> keyString(factory_->NewFromUtf8ReadOnly(key)); 2971 PropertyDescriptor des(thread_, value, false, false, false); 2972 JSObject::DefineOwnProperty(thread_, obj, keyString, des); 2973} 2974 2975void Builtins::SetFuncToObjAndGlobal(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &globalObject, 2976 const JSHandle<JSObject> &obj, std::string_view key, 2977 EcmaEntrypoint func, int length, kungfu::BuiltinsStubCSigns::ID builtinId) 2978{ 2979 JSHandle<JSFunction> function = factory_->NewJSFunction(env, reinterpret_cast<void *>(func), 2980 FunctionKind::NORMAL_FUNCTION, builtinId); 2981 JSFunction::SetFunctionLength(thread_, function, JSTaggedValue(length)); 2982 JSHandle<JSTaggedValue> keyString(factory_->NewFromUtf8ReadOnly(key)); 2983 JSHandle<JSFunctionBase> baseFunction(function); 2984 JSHandle<JSTaggedValue> handleUndefine(thread_, JSTaggedValue::Undefined()); 2985 JSFunction::SetFunctionName(thread_, baseFunction, keyString, handleUndefine); 2986 if (IS_TYPED_BUILTINS_ID(builtinId) || IS_TYPED_INLINE_BUILTINS_ID(builtinId)) { 2987 auto globalConst = const_cast<GlobalEnvConstants *>(thread_->GlobalConstants()); 2988 globalConst->SetConstant(GET_TYPED_CONSTANT_INDEX(builtinId), function); 2989 } 2990 PropertyDescriptor descriptor(thread_, JSHandle<JSTaggedValue>::Cast(function), true, false, true); 2991 JSObject::DefineOwnProperty(thread_, obj, keyString, descriptor); 2992 JSObject::DefineOwnProperty(thread_, globalObject, keyString, descriptor); 2993} 2994 2995void Builtins::InitializeGeneratorFunction(const JSHandle<GlobalEnv> &env, 2996 const JSHandle<JSHClass> &objFuncClass) const 2997{ 2998 [[maybe_unused]] EcmaHandleScope scope(thread_); 2999 const GlobalEnvConstants *globalConst = thread_->GlobalConstants(); 3000 JSHandle<JSObject> generatorFuncPrototype = factory_->NewJSObjectWithInit(objFuncClass); 3001 JSHandle<JSTaggedValue> generatorFuncPrototypeValue(generatorFuncPrototype); 3002 3003 // 26.3.3.1 GeneratorFunction.prototype.constructor 3004 // GeneratorFunction.prototype_or_hclass 3005 JSHandle<JSHClass> generatorFuncInstanceHClass = 3006 factory_->NewEcmaHClass(JSFunction::SIZE, JSType::JS_GENERATOR_FUNCTION, generatorFuncPrototypeValue); 3007 generatorFuncInstanceHClass->SetCallable(true); 3008 generatorFuncInstanceHClass->SetExtensible(true); 3009 // GeneratorFunction = new GeneratorFunction() 3010 JSHandle<JSFunction> generatorFunction = 3011 NewBuiltinConstructor(env, generatorFuncPrototype, GeneratorObject::GeneratorFunctionConstructor, 3012 "GeneratorFunction", FunctionLength::ONE); 3013 JSObject::SetPrototype(thread_, JSHandle<JSObject>::Cast(generatorFunction), env->GetFunctionFunction()); 3014 JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString(); 3015 PropertyDescriptor generatorDesc(thread_, JSHandle<JSTaggedValue>::Cast(generatorFunction), false, false, true); 3016 JSObject::DefineOwnProperty(thread_, generatorFuncPrototype, constructorKey, generatorDesc); 3017 generatorFunction->SetProtoOrHClass(thread_, generatorFuncInstanceHClass.GetTaggedValue()); 3018 env->SetGeneratorFunctionFunction(thread_, generatorFunction); 3019 3020 // 26.3.3.2 GeneratorFunction.prototype.prototype -> Generator prototype object. 3021 PropertyDescriptor descriptor(thread_, env->GetGeneratorPrototype(), false, false, true); 3022 JSObject::DefineOwnProperty(thread_, generatorFuncPrototype, globalConst->GetHandledPrototypeString(), descriptor); 3023 3024 // 26.3.3.3 GeneratorFunction.prototype[@@toStringTag] 3025 SetStringTagSymbol(env, generatorFuncPrototype, "GeneratorFunction"); 3026 3027 // GeneratorFunction prototype __proto__ -> Function. 3028 JSObject::SetPrototype(thread_, generatorFuncPrototype, env->GetFunctionPrototype()); 3029 3030 // 26.5.1.1 Generator.prototype.constructor -> %GeneratorFunction.prototype%. 3031 PropertyDescriptor generatorObjDesc(thread_, generatorFuncPrototypeValue, false, false, true); 3032 JSObject::DefineOwnProperty(thread_, JSHandle<JSObject>(env->GetGeneratorPrototype()), 3033 globalConst->GetHandledConstructorString(), generatorObjDesc); 3034 3035 // Generator instances prototype -> GeneratorFunction.prototype.prototype 3036 PropertyDescriptor generatorObjProtoDesc(thread_, generatorFuncPrototypeValue, true, false, false); 3037 JSObject::DefineOwnProperty(thread_, JSHandle<JSObject>(env->GetInitialGenerator()), 3038 globalConst->GetHandledPrototypeString(), generatorObjProtoDesc); 3039 3040 env->SetGeneratorFunctionPrototype(thread_, generatorFuncPrototype); 3041} 3042 3043void Builtins::InitializeAsyncGeneratorFunction(const JSHandle<GlobalEnv> &env, 3044 const JSHandle<JSHClass> &objFuncClass) const 3045{ 3046 [[maybe_unused]] EcmaHandleScope scope(thread_); 3047 const GlobalEnvConstants *globalConst = thread_->GlobalConstants(); 3048 JSHandle<JSObject> asyncGeneratorFuncPrototype = factory_->NewJSObjectWithInit(objFuncClass); 3049 JSHandle<JSTaggedValue> asyncGeneratorFuncPrototypeValue(asyncGeneratorFuncPrototype); 3050 // 27.4.3.1 AsyncGeneratorFunction.prototype.constructor 3051 JSHandle<JSHClass> asyncGeneratorFuncInstanceHClass = 3052 factory_->NewEcmaHClass(JSFunction::SIZE, JSType::JS_ASYNC_GENERATOR_FUNCTION, 3053 asyncGeneratorFuncPrototypeValue); 3054 asyncGeneratorFuncInstanceHClass->SetCallable(true); 3055 asyncGeneratorFuncInstanceHClass->SetExtensible(true); 3056 JSHandle<JSFunction> asyncGeneratorFunction = 3057 NewBuiltinConstructor(env, asyncGeneratorFuncPrototype, 3058 AsyncGeneratorObject::AsyncGeneratorFunctionConstructor, "AsyncGeneratorFunction", 3059 FunctionLength::ONE); 3060 JSObject::SetPrototype(thread_, JSHandle<JSObject>::Cast(asyncGeneratorFunction), env->GetFunctionFunction()); 3061 JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString(); 3062 PropertyDescriptor asyncGeneratorDesc(thread_, JSHandle<JSTaggedValue>::Cast(asyncGeneratorFunction), 3063 false, false, true); 3064 JSObject::DefineOwnProperty(thread_, asyncGeneratorFuncPrototype, constructorKey, asyncGeneratorDesc); 3065 asyncGeneratorFunction->SetProtoOrHClass(thread_, asyncGeneratorFuncInstanceHClass.GetTaggedValue()); 3066 env->SetAsyncGeneratorFunctionFunction(thread_, asyncGeneratorFunction); 3067 3068 // 27.4.3.2 AsyncGeneratorFunction.prototype.prototype 3069 PropertyDescriptor descriptor(thread_, env->GetAsyncGeneratorPrototype(), false, false, true); 3070 JSObject::DefineOwnProperty(thread_, asyncGeneratorFuncPrototype, globalConst->GetHandledPrototypeString(), 3071 descriptor); 3072 3073 // 27.4.3.3 AsyncGeneratorFunction.prototype [ @@toStringTag ] 3074 SetStringTagSymbol(env, asyncGeneratorFuncPrototype, "AsyncGeneratorFunction"); 3075 // AsyncGeneratorFunction prototype __proto__ -> Function. 3076 JSObject::SetPrototype(thread_, asyncGeneratorFuncPrototype, env->GetFunctionPrototype()); 3077 3078 PropertyDescriptor asyncGeneratorObjDesc(thread_, asyncGeneratorFuncPrototypeValue, false, false, true); 3079 JSObject::DefineOwnProperty(thread_, JSHandle<JSObject>(env->GetInitialAsyncGenerator()), 3080 globalConst->GetHandledConstructorString(), asyncGeneratorObjDesc); 3081 3082 PropertyDescriptor asyncGeneratorObjProtoDesc(thread_, asyncGeneratorFuncPrototypeValue, true, false, false); 3083 JSObject::DefineOwnProperty(thread_, JSHandle<JSObject>(env->GetInitialAsyncGenerator()), 3084 globalConst->GetHandledPrototypeString(), asyncGeneratorObjProtoDesc); 3085 3086 env->SetAsyncGeneratorFunctionPrototype(thread_, asyncGeneratorFuncPrototype); 3087} 3088 3089void Builtins::InitializeGenerator(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &objFuncClass) const 3090{ 3091 [[maybe_unused]] EcmaHandleScope scope(thread_); 3092 JSHandle<JSObject> generatorPrototype = factory_->NewJSObjectWithInit(objFuncClass); 3093 3094 // GeneratorObject.prototype method 3095 // Generator.prototype.constructor(value) 3096 SetFunction(env, generatorPrototype, "constructor", 3097 GeneratorObject::GeneratorFunctionConstructor, FunctionLength::ONE); 3098 // 26.5.1.2 Generator.prototype.next(value) 3099 SetFunction(env, generatorPrototype, "next", GeneratorObject::GeneratorPrototypeNext, FunctionLength::ONE); 3100 // 26.5.1.3 Generator.prototype.return(value) 3101 SetFunction(env, generatorPrototype, "return", GeneratorObject::GeneratorPrototypeReturn, FunctionLength::ONE); 3102 // 26.5.1.4 Generator.prototype.throw(exception) 3103 SetFunction(env, generatorPrototype, "throw", GeneratorObject::GeneratorPrototypeThrow, FunctionLength::ONE); 3104 3105 // 26.5.1.5 Generator.prototype[@@toStringTag] 3106 SetStringTagSymbol(env, generatorPrototype, "Generator"); 3107 3108 env->SetGeneratorPrototype(thread_, generatorPrototype); 3109 JSObject::SetPrototype(thread_, generatorPrototype, env->GetIteratorPrototype()); 3110 3111 // Generator {} 3112 JSHandle<JSObject> initialGeneratorFuncPrototype = factory_->NewJSObjectWithInit(objFuncClass); 3113 JSObject::SetPrototype(thread_, initialGeneratorFuncPrototype, JSHandle<JSTaggedValue>(generatorPrototype)); 3114 env->SetInitialGenerator(thread_, initialGeneratorFuncPrototype); 3115} 3116 3117void Builtins::InitializeAsyncGenerator(const JSHandle<GlobalEnv> &env, 3118 const JSHandle<JSHClass> &objFuncClass) const 3119{ 3120 [[maybe_unused]] EcmaHandleScope scope(thread_); 3121 3122 const GlobalEnvConstants *globalConst = thread_->GlobalConstants(); 3123 JSHandle<JSObject> asyncGeneratorFuncPrototype = factory_->NewJSObjectWithInit(objFuncClass); 3124 3125 // GeneratorObject.prototype method 3126 // 27.6.1.2 AsyncGenerator.prototype.next ( value ) 3127 SetFunction(env, asyncGeneratorFuncPrototype, "next", AsyncGeneratorObject::AsyncGeneratorPrototypeNext, 3128 FunctionLength::ONE); 3129 // 27.6.1.3 AsyncGenerator.prototype.return ( value ) 3130 SetFunction(env, asyncGeneratorFuncPrototype, "return", AsyncGeneratorObject::AsyncGeneratorPrototypeReturn, 3131 FunctionLength::ONE); 3132 // 27.6.1.4 AsyncGenerator.prototype.throw ( exception ) 3133 SetFunction(env, asyncGeneratorFuncPrototype, "throw", AsyncGeneratorObject::AsyncGeneratorPrototypeThrow, 3134 FunctionLength::ONE); 3135 3136 // 27.6.1.5 AsyncGenerator.prototype [ @@toStringTag ] 3137 SetStringTagSymbol(env, asyncGeneratorFuncPrototype, "AsyncGenerator"); 3138 3139 PropertyDescriptor descriptor(thread_, env->GetAsyncIteratorPrototype(), true, false, false); 3140 JSObject::DefineOwnProperty(thread_, asyncGeneratorFuncPrototype, 3141 globalConst->GetHandledPrototypeString(), descriptor); 3142 env->SetAsyncGeneratorPrototype(thread_, asyncGeneratorFuncPrototype); 3143 JSObject::SetPrototype(thread_, asyncGeneratorFuncPrototype, env->GetAsyncIteratorPrototype()); 3144 3145 JSHandle<JSObject> initialAsyncGeneratorFuncPrototype = factory_->NewJSObjectWithInit(objFuncClass); 3146 JSObject::SetPrototype(thread_, initialAsyncGeneratorFuncPrototype, 3147 JSHandle<JSTaggedValue>(asyncGeneratorFuncPrototype)); 3148 env->SetInitialAsyncGenerator(thread_, initialAsyncGeneratorFuncPrototype); 3149} 3150 3151void Builtins::SetArgumentsSharedAccessor(const JSHandle<GlobalEnv> &env) 3152{ 3153 JSHandle<JSTaggedValue> throwFunction = env->GetThrowTypeError(); 3154 3155 JSHandle<AccessorData> accessor = factory_->NewAccessorData(); 3156 accessor->SetGetter(thread_, throwFunction); 3157 accessor->SetSetter(thread_, throwFunction); 3158 env->SetArgumentsCallerAccessor(thread_, accessor); 3159 3160 accessor = factory_->NewAccessorData(); 3161 accessor->SetGetter(thread_, throwFunction); 3162 accessor->SetSetter(thread_, throwFunction); 3163 env->SetArgumentsCalleeAccessor(thread_, accessor); 3164} 3165 3166void Builtins::SetAccessor(const JSHandle<JSObject> &obj, const JSHandle<JSTaggedValue> &key, 3167 const JSHandle<JSTaggedValue> &getter, const JSHandle<JSTaggedValue> &setter) const 3168{ 3169 JSHandle<AccessorData> accessor = factory_->NewAccessorData(); 3170 accessor->SetGetter(thread_, getter); 3171 accessor->SetSetter(thread_, setter); 3172 PropertyAttributes attr = PropertyAttributes::DefaultAccessor(false, false, true); 3173 JSObject::AddAccessor(thread_, JSHandle<JSTaggedValue>::Cast(obj), key, accessor, attr); 3174} 3175 3176void Builtins::SetGetter(const JSHandle<JSObject> &obj, const JSHandle<JSTaggedValue> &key, 3177 const JSHandle<JSTaggedValue> &getter) const 3178{ 3179 JSHandle<AccessorData> accessor = factory_->NewAccessorData(); 3180 accessor->SetGetter(thread_, getter); 3181 PropertyAttributes attr = PropertyAttributes::DefaultAccessor(false, false, true); 3182 JSObject::AddAccessor(thread_, JSHandle<JSTaggedValue>::Cast(obj), key, accessor, attr); 3183} 3184 3185#ifdef ARK_SUPPORT_INTL 3186JSHandle<JSFunction> Builtins::NewIntlConstructor(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &prototype, 3187 EcmaEntrypoint ctorFunc, std::string_view name, int length) 3188{ 3189 JSHandle<JSFunction> ctor = 3190 factory_->NewJSFunction(env, reinterpret_cast<void *>(ctorFunc), FunctionKind::BUILTIN_CONSTRUCTOR); 3191 InitializeIntlCtor(env, prototype, ctor, name, length); 3192 return ctor; 3193} 3194 3195#define INTL_LAZY_INITIALIZE(type) \ 3196 void Builtins::LazyInitialize##type(const JSHandle<GlobalEnv> &env) const \ 3197 { \ 3198 [[maybe_unused]] EcmaHandleScope scope(thread_); \ 3199 JSHandle<JSObject> intlObject(env->GetIntlFunction()); \ 3200 JSHandle<JSTaggedValue> key(factory_->NewFromUtf8ReadOnly(#type)); \ 3201 auto accessor = factory_->NewInternalAccessor(nullptr, \ 3202 reinterpret_cast<void *>(BuiltinsLazyCallback::type)); \ 3203 SetLazyAccessor(intlObject, key, accessor); \ 3204 env->Set##type##Function(thread_, accessor); \ 3205 } 3206 3207ITERATE_INTL(INTL_LAZY_INITIALIZE) 3208#undef INTL_LAZY_INITIALIZE 3209 3210void Builtins::InitializeIntlCtor(const JSHandle<GlobalEnv> &env, const JSHandle<JSObject> &prototype, 3211 const JSHandle<JSFunction> &ctor, std::string_view name, int length) 3212{ 3213 const GlobalEnvConstants *globalConst = thread_->GlobalConstants(); 3214 JSFunction::SetFunctionLength(thread_, ctor, JSTaggedValue(length)); 3215 JSHandle<JSTaggedValue> nameString(factory_->NewFromUtf8ReadOnly(name)); 3216 JSFunction::SetFunctionName(thread_, JSHandle<JSFunctionBase>(ctor), nameString, 3217 JSHandle<JSTaggedValue>(thread_, JSTaggedValue::Undefined())); 3218 JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString(); 3219 PropertyDescriptor descriptor1(thread_, JSHandle<JSTaggedValue>::Cast(ctor), true, false, true); 3220 JSObject::DefineOwnProperty(thread_, prototype, constructorKey, descriptor1); 3221 3222 // set "prototype" in constructor. 3223 JSFunction::SetFunctionPrototypeOrInstanceHClass(thread_, ctor, prototype.GetTaggedValue()); 3224 3225 if (!JSTaggedValue::SameValue(nameString, thread_->GlobalConstants()->GetHandledAsyncFunctionString())) { 3226 JSHandle<JSObject> intlObject(thread_, env->GetIntlFunction().GetTaggedValue()); 3227 PropertyDescriptor descriptor2(thread_, JSHandle<JSTaggedValue>::Cast(ctor), true, false, true); 3228 JSObject::DefineOwnProperty(thread_, intlObject, nameString, descriptor2); 3229 } 3230} 3231 3232void Builtins::InitializeIntl(const JSHandle<GlobalEnv> &env, const JSHandle<JSTaggedValue> &objFuncPrototypeValue) 3233{ 3234 [[maybe_unused]] EcmaHandleScope scope(thread_); 3235 JSHandle<JSHClass> intlHClass = factory_->NewEcmaHClass(JSIntl::SIZE, JSType::JS_INTL, objFuncPrototypeValue); 3236 JSHandle<JSObject> intlObject = factory_->NewJSObjectWithInit(intlHClass); 3237 3238 JSHandle<JSTaggedValue> initIntlSymbol(factory_->NewPublicSymbolWithChar("Symbol.IntlLegacyConstructedSymbol")); 3239 SetNoneAttributeProperty(intlObject, "fallbackSymbol", initIntlSymbol); 3240 3241 SetFunction(env, intlObject, "getCanonicalLocales", Intl::GetCanonicalLocales, FunctionLength::ONE); 3242 3243 // initial value of the "Intl" property of the global object. 3244 JSHandle<JSTaggedValue> intlString(factory_->NewFromASCIIReadOnly("Intl")); 3245 JSHandle<JSObject> globalObject(thread_, env->GetGlobalObject()); 3246 PropertyDescriptor intlDesc(thread_, JSHandle<JSTaggedValue>::Cast(intlObject), true, false, true); 3247 JSObject::DefineOwnProperty(thread_, globalObject, intlString, intlDesc); 3248 3249 SetStringTagSymbol(env, intlObject, "Intl"); 3250 3251 env->SetIntlFunction(thread_, intlObject); 3252} 3253 3254void Builtins::InitializeDateTimeFormat(const JSHandle<GlobalEnv> &env) 3255{ 3256 [[maybe_unused]] EcmaHandleScope scope(thread_); 3257 // DateTimeFormat.prototype 3258 JSHandle<JSFunction> objFun(env->GetObjectFunction()); 3259 JSHandle<JSObject> dtfPrototype = factory_->NewJSObjectByConstructor(objFun); 3260 JSHandle<JSTaggedValue> dtfPrototypeValue(dtfPrototype); 3261 3262 // DateTimeFormat.prototype_or_hclass 3263 JSHandle<JSHClass> dtfFuncInstanceHClass = 3264 factory_->NewEcmaHClass(JSDateTimeFormat::SIZE, JSType::JS_DATE_TIME_FORMAT, dtfPrototypeValue); 3265 3266 // DateTimeFormat = new Function() 3267 // 13.4.1 Intl.DateTimeFormat.prototype.constructor 3268 JSHandle<JSObject> dtfFunction(NewIntlConstructor(env, dtfPrototype, DateTimeFormat::DateTimeFormatConstructor, 3269 "DateTimeFormat", FunctionLength::ZERO)); 3270 JSFunction::SetFunctionPrototypeOrInstanceHClass(thread_, 3271 JSHandle<JSFunction>(dtfFunction), 3272 JSTaggedValue(*dtfFuncInstanceHClass)); 3273 3274 // 13.3.2 Intl.DateTimeFormat.supportedLocalesOf ( locales [ , options ] ) 3275 SetFunction(env, dtfFunction, "supportedLocalesOf", DateTimeFormat::SupportedLocalesOf, FunctionLength::ONE); 3276 3277 // DateTimeFormat.prototype method 3278 // 13.4.2 Intl.DateTimeFormat.prototype [ @@toStringTag ] 3279 SetStringTagSymbol(env, dtfPrototype, "Intl.DateTimeFormat"); 3280 env->SetDateTimeFormatFunction(thread_, dtfFunction); 3281 3282 // 13.4.3 get Intl.DateTimeFormat.prototype.format 3283 JSHandle<JSTaggedValue> formatGetter = CreateGetter(env, DateTimeFormat::Format, "format", FunctionLength::ZERO); 3284 JSHandle<JSTaggedValue> formatSetter(thread_, JSTaggedValue::Undefined()); 3285 SetAccessor(dtfPrototype, thread_->GlobalConstants()->GetHandledFormatString(), formatGetter, formatSetter); 3286 3287 // 13.4.4 Intl.DateTimeFormat.prototype.formatToParts ( date ) 3288 SetFunction(env, dtfPrototype, "formatToParts", DateTimeFormat::FormatToParts, FunctionLength::ONE); 3289 3290 // 13.4.5 Intl.DateTimeFormat.prototype.resolvedOptions () 3291 SetFunction(env, dtfPrototype, "resolvedOptions", DateTimeFormat::ResolvedOptions, FunctionLength::ZERO); 3292 3293 SetFunction(env, dtfPrototype, "formatRange", DateTimeFormat::FormatRange, FunctionLength::TWO); 3294 3295 SetFunction(env, dtfPrototype, "formatRangeToParts", DateTimeFormat::FormatRangeToParts, FunctionLength::TWO); 3296} 3297 3298void Builtins::InitializeRelativeTimeFormat(const JSHandle<GlobalEnv> &env) 3299{ 3300 [[maybe_unused]] EcmaHandleScope scope(thread_); 3301 // RelativeTimeFormat.prototype 3302 JSHandle<JSFunction> objFun(env->GetObjectFunction()); 3303 JSHandle<JSObject> rtfPrototype = factory_->NewJSObjectByConstructor(objFun); 3304 JSHandle<JSTaggedValue> rtfPrototypeValue(rtfPrototype); 3305 3306 // RelativeTimeFormat.prototype_or_hclass 3307 JSHandle<JSHClass> rtfFuncInstanceHClass = 3308 factory_->NewEcmaHClass(JSRelativeTimeFormat::SIZE, JSType::JS_RELATIVE_TIME_FORMAT, rtfPrototypeValue); 3309 3310 // RelativeTimeFormat = new Function() 3311 // 14.2.1 Intl.RelativeTimeFormat.prototype.constructor 3312 JSHandle<JSObject> rtfFunction(NewIntlConstructor(env, rtfPrototype, 3313 RelativeTimeFormat::RelativeTimeFormatConstructor, 3314 "RelativeTimeFormat", FunctionLength::ZERO)); 3315 JSFunction::SetFunctionPrototypeOrInstanceHClass(thread_, 3316 JSHandle<JSFunction>(rtfFunction), 3317 JSTaggedValue(*rtfFuncInstanceHClass)); 3318 3319 // 14.3.2 Intl.RelativeTimeFormat.supportedLocalesOf ( locales [ , options ] ) 3320 SetFunction(env, rtfFunction, "supportedLocalesOf", RelativeTimeFormat::SupportedLocalesOf, FunctionLength::ONE); 3321 3322 // RelativeTimeFormat.prototype method 3323 // 14.4.2 Intl.RelativeTimeFormat.prototype [ @@toStringTag ] 3324 SetStringTagSymbol(env, rtfPrototype, "Intl.RelativeTimeFormat"); 3325 env->SetRelativeTimeFormatFunction(thread_, rtfFunction); 3326 3327 // 14.4.3 get Intl.RelativeTimeFormat.prototype.format 3328 SetFunction(env, rtfPrototype, "format", RelativeTimeFormat::Format, FunctionLength::TWO); 3329 3330 // 14.4.4 Intl.RelativeTimeFormat.prototype.formatToParts( value, unit ) 3331 SetFunction(env, rtfPrototype, "formatToParts", RelativeTimeFormat::FormatToParts, FunctionLength::TWO); 3332 3333 // 14.4.5 Intl.RelativeTimeFormat.prototype.resolvedOptions () 3334 SetFunction(env, rtfPrototype, "resolvedOptions", RelativeTimeFormat::ResolvedOptions, FunctionLength::ZERO); 3335} 3336 3337void Builtins::InitializeNumberFormat(const JSHandle<GlobalEnv> &env) 3338{ 3339 [[maybe_unused]] EcmaHandleScope scope(thread_); 3340 // NumberFormat.prototype 3341 JSHandle<JSFunction> objFun(env->GetObjectFunction()); 3342 JSHandle<JSObject> nfPrototype = factory_->NewJSObjectByConstructor(objFun); 3343 JSHandle<JSTaggedValue> nfPrototypeValue(nfPrototype); 3344 3345 // NumberFormat.prototype_or_hclass 3346 JSHandle<JSHClass> nfFuncInstanceHClass = 3347 factory_->NewEcmaHClass(JSNumberFormat::SIZE, JSType::JS_NUMBER_FORMAT, nfPrototypeValue); 3348 3349 // NumberFormat = new Function() 3350 // 12.4.1 Intl.NumberFormat.prototype.constructor 3351 JSHandle<JSObject> nfFunction(NewIntlConstructor(env, nfPrototype, NumberFormat::NumberFormatConstructor, 3352 "NumberFormat", FunctionLength::ZERO)); 3353 JSFunction::SetFunctionPrototypeOrInstanceHClass(thread_, 3354 JSHandle<JSFunction>(nfFunction), 3355 JSTaggedValue(*nfFuncInstanceHClass)); 3356 3357 // 12.3.2 Intl.NumberFormat.supportedLocalesOf ( locales [ , options ] ) 3358 SetFunction(env, nfFunction, "supportedLocalesOf", NumberFormat::SupportedLocalesOf, FunctionLength::ONE); 3359 3360 // NumberFormat.prototype method 3361 // 12.4.2 Intl.NumberFormat.prototype [ @@toStringTag ] 3362 SetStringTagSymbol(env, nfPrototype, "Intl.NumberFormat"); 3363 env->SetNumberFormatFunction(thread_, nfFunction); 3364 3365 // 12.4.3 get Intl.NumberFormat.prototype.format 3366 JSHandle<JSTaggedValue> formatGetter = CreateGetter(env, NumberFormat::Format, "format", FunctionLength::ZERO); 3367 JSHandle<JSTaggedValue> formatSetter(thread_, JSTaggedValue::Undefined()); 3368 SetAccessor(nfPrototype, thread_->GlobalConstants()->GetHandledFormatString(), formatGetter, formatSetter); 3369 3370 // 12.4.4 Intl.NumberFormat.prototype.formatToParts ( date ) 3371 SetFunction(env, nfPrototype, "formatToParts", NumberFormat::FormatToParts, FunctionLength::ONE); 3372 3373 // 12.4.5 Intl.NumberFormat.prototype.resolvedOptions () 3374 SetFunction(env, nfPrototype, "resolvedOptions", NumberFormat::ResolvedOptions, FunctionLength::ZERO); 3375} 3376 3377void Builtins::InitializeLocale(const JSHandle<GlobalEnv> &env) 3378{ 3379 [[maybe_unused]] EcmaHandleScope scope(thread_); 3380 // Locale.prototype 3381 JSHandle<JSFunction> objFun(env->GetObjectFunction()); 3382 JSHandle<JSObject> localePrototype = factory_->NewJSObjectByConstructor(objFun); 3383 JSHandle<JSTaggedValue> localePrototypeValue(localePrototype); 3384 3385 // Locale.prototype_or_hclass 3386 JSHandle<JSHClass> localeFuncInstanceHClass = 3387 factory_->NewEcmaHClass(JSLocale::SIZE, JSType::JS_LOCALE, localePrototypeValue); 3388 3389 // Locale = new Function() 3390 JSHandle<JSObject> localeFunction( 3391 NewIntlConstructor(env, localePrototype, Locale::LocaleConstructor, "Locale", FunctionLength::ONE)); 3392 JSFunction::SetFunctionPrototypeOrInstanceHClass(thread_, 3393 JSHandle<JSFunction>(localeFunction), 3394 JSTaggedValue(*localeFuncInstanceHClass)); 3395 3396 // Locale.prototype method 3397 SetFunction(env, localePrototype, "maximize", Locale::Maximize, FunctionLength::ZERO); 3398 SetFunction(env, localePrototype, "minimize", Locale::Minimize, FunctionLength::ZERO); 3399 SetFunction(env, localePrototype, "toString", Locale::ToString, FunctionLength::ZERO); 3400 3401 JSHandle<JSTaggedValue> baseNameGetter = CreateGetter(env, Locale::GetBaseName, "baseName", FunctionLength::ZERO); 3402 SetGetter(localePrototype, thread_->GlobalConstants()->GetHandledBaseNameString(), baseNameGetter); 3403 3404 JSHandle<JSTaggedValue> calendarGetter = CreateGetter(env, Locale::GetCalendar, "calendar", FunctionLength::ZERO); 3405 SetGetter(localePrototype, thread_->GlobalConstants()->GetHandledCalendarString(), calendarGetter); 3406 3407 JSHandle<JSTaggedValue> caseFirstGetter = 3408 CreateGetter(env, Locale::GetCaseFirst, "caseFirst", FunctionLength::ZERO); 3409 SetGetter(localePrototype, thread_->GlobalConstants()->GetHandledCaseFirstString(), caseFirstGetter); 3410 3411 JSHandle<JSTaggedValue> collationGetter = 3412 CreateGetter(env, Locale::GetCollation, "collation", FunctionLength::ZERO); 3413 SetGetter(localePrototype, thread_->GlobalConstants()->GetHandledCollationString(), collationGetter); 3414 3415 JSHandle<JSTaggedValue> hourCycleGetter = 3416 CreateGetter(env, Locale::GetHourCycle, "hourCycle", FunctionLength::ZERO); 3417 SetGetter(localePrototype, thread_->GlobalConstants()->GetHandledHourCycleString(), hourCycleGetter); 3418 3419 JSHandle<JSTaggedValue> numericGetter = CreateGetter(env, Locale::GetNumeric, "numeric", FunctionLength::ZERO); 3420 SetGetter(localePrototype, thread_->GlobalConstants()->GetHandledNumericString(), numericGetter); 3421 3422 JSHandle<JSTaggedValue> numberingSystemGetter = 3423 CreateGetter(env, Locale::GetNumberingSystem, "numberingSystem", FunctionLength::ZERO); 3424 SetGetter(localePrototype, thread_->GlobalConstants()->GetHandledNumberingSystemString(), numberingSystemGetter); 3425 3426 JSHandle<JSTaggedValue> languageGetter = CreateGetter(env, Locale::GetLanguage, "language", FunctionLength::ZERO); 3427 SetGetter(localePrototype, thread_->GlobalConstants()->GetHandledLanguageString(), languageGetter); 3428 3429 JSHandle<JSTaggedValue> scriptGetter = CreateGetter(env, Locale::GetScript, "script", FunctionLength::ZERO); 3430 SetGetter(localePrototype, thread_->GlobalConstants()->GetHandledScriptString(), scriptGetter); 3431 3432 JSHandle<JSTaggedValue> regionGetter = CreateGetter(env, Locale::GetRegion, "region", FunctionLength::ZERO); 3433 SetGetter(localePrototype, thread_->GlobalConstants()->GetHandledRegionString(), regionGetter); 3434 3435 // 10.3.2 Intl.Locale.prototype[ @@toStringTag ] 3436 SetStringTagSymbol(env, localePrototype, "Intl.Locale"); 3437 env->SetLocaleFunction(thread_, localeFunction); 3438} 3439 3440void Builtins::InitializeCollator(const JSHandle<GlobalEnv> &env) 3441{ 3442 [[maybe_unused]] EcmaHandleScope scope(thread_); 3443 // Collator.prototype 3444 JSHandle<JSFunction> objFun(env->GetObjectFunction()); 3445 JSHandle<JSObject> collatorPrototype = factory_->NewJSObjectByConstructor(objFun); 3446 JSHandle<JSTaggedValue> collatorPrototypeValue(collatorPrototype); 3447 3448 // Collator.prototype_or_hclass 3449 JSHandle<JSHClass> collatorFuncInstanceHClass = 3450 factory_->NewEcmaHClass(JSCollator::SIZE, JSType::JS_COLLATOR, collatorPrototypeValue); 3451 3452 // Collator = new Function() 3453 // 11.1.2 Intl.Collator.prototype.constructor 3454 JSHandle<JSObject> collatorFunction( 3455 NewIntlConstructor(env, collatorPrototype, Collator::CollatorConstructor, "Collator", FunctionLength::ZERO)); 3456 JSFunction::SetFunctionPrototypeOrInstanceHClass(thread_, 3457 JSHandle<JSFunction>(collatorFunction), 3458 JSTaggedValue(*collatorFuncInstanceHClass)); 3459 3460 // 11.2.2 Intl.Collator.supportedLocalesOf ( locales [ , options ] ) 3461 SetFunction(env, collatorFunction, "supportedLocalesOf", Collator::SupportedLocalesOf, FunctionLength::ONE); 3462 3463 // Collator.prototype method 3464 // 11.3.2 Intl.Collator.prototype [ @@toStringTag ] 3465 SetStringTagSymbol(env, collatorPrototype, "Intl.Collator"); 3466 env->SetCollatorFunction(thread_, collatorFunction); 3467 3468 // 11.3.3 get Intl.Collator.prototype.compare 3469 JSHandle<JSTaggedValue> compareGetter = CreateGetter(env, Collator::Compare, "compare", FunctionLength::ZERO); 3470 JSHandle<JSTaggedValue> compareSetter(thread_, JSTaggedValue::Undefined()); 3471 SetAccessor(collatorPrototype, thread_->GlobalConstants()->GetHandledCompareString(), compareGetter, compareSetter); 3472 3473 // 11.3.4 Intl.Collator.prototype.resolvedOptions () 3474 SetFunction(env, collatorPrototype, "resolvedOptions", Collator::ResolvedOptions, FunctionLength::ZERO, 3475 kungfu::BuiltinsStubCSigns::CollatorResolvedOptions); 3476} 3477 3478void Builtins::InitializePluralRules(const JSHandle<GlobalEnv> &env) 3479{ 3480 [[maybe_unused]] EcmaHandleScope scope(thread_); 3481 // PluralRules.prototype 3482 JSHandle<JSFunction> objFun(env->GetObjectFunction()); 3483 JSHandle<JSObject> prPrototype = factory_->NewJSObjectByConstructor(objFun); 3484 JSHandle<JSTaggedValue> prPrototypeValue(prPrototype); 3485 3486 // PluralRules.prototype_or_hclass 3487 JSHandle<JSHClass> prFuncInstanceHClass = 3488 factory_->NewEcmaHClass(JSPluralRules::SIZE, JSType::JS_PLURAL_RULES, prPrototypeValue); 3489 3490 // PluralRules = new Function() 3491 // 15.2.1 Intl.PluralRules.prototype.constructor 3492 JSHandle<JSObject> prFunction( 3493 NewIntlConstructor(env, prPrototype, PluralRules::PluralRulesConstructor, "PluralRules", FunctionLength::ZERO)); 3494 JSFunction::SetFunctionPrototypeOrInstanceHClass(thread_, 3495 JSHandle<JSFunction>(prFunction), 3496 JSTaggedValue(*prFuncInstanceHClass)); 3497 3498 // 15.3.2 Intl.PluralRules.supportedLocalesOf ( locales [ , options ] ) 3499 SetFunction(env, prFunction, "supportedLocalesOf", PluralRules::SupportedLocalesOf, FunctionLength::ONE); 3500 3501 // PluralRules.prototype method 3502 // 15.4.2 Intl.PluralRules.prototype [ @@toStringTag ] 3503 SetStringTagSymbol(env, prPrototype, "Intl.PluralRules"); 3504 env->SetPluralRulesFunction(thread_, prFunction); 3505 3506 // 15.4.3 get Intl.PluralRules.prototype.select 3507 SetFunction(env, prPrototype, "select", PluralRules::Select, FunctionLength::ONE); 3508 3509 // 15.4.5 Intl.PluralRules.prototype.resolvedOptions () 3510 SetFunction(env, prPrototype, "resolvedOptions", PluralRules::ResolvedOptions, FunctionLength::ZERO); 3511} 3512 3513void Builtins::InitializeDisplayNames(const JSHandle<GlobalEnv> &env) 3514{ 3515 [[maybe_unused]] EcmaHandleScope scope(thread_); 3516 // DisplayNames.prototype 3517 JSHandle<JSFunction> objFun(env->GetObjectFunction()); 3518 JSHandle<JSObject> dnPrototype = factory_->NewJSObjectByConstructor(objFun); 3519 JSHandle<JSTaggedValue> dnPrototypeValue(dnPrototype); 3520 3521 // DisplayNames.prototype_or_hclass 3522 JSHandle<JSHClass> dnFuncInstanceHClass = 3523 factory_->NewEcmaHClass(JSDisplayNames::SIZE, JSType::JS_DISPLAYNAMES, dnPrototypeValue); 3524 3525 // DisplayNames = new Function() 3526 // 12.4.1 Intl.DisplayNames.prototype.constructor 3527 JSHandle<JSObject> dnFunction(NewIntlConstructor(env, dnPrototype, DisplayNames::DisplayNamesConstructor, 3528 "DisplayNames", FunctionLength::TWO)); 3529 JSFunction::SetFunctionPrototypeOrInstanceHClass(thread_, 3530 JSHandle<JSFunction>(dnFunction), 3531 JSTaggedValue(*dnFuncInstanceHClass)); 3532 3533 // 12.3.2 Intl.DisplayNames.supportedLocalesOf ( locales [ , options ] ) 3534 SetFunction(env, dnFunction, "supportedLocalesOf", DisplayNames::SupportedLocalesOf, FunctionLength::ONE); 3535 3536 // DisplayNames.prototype method 3537 // 12.4.2 Intl.DisplayNames.prototype[ @@toStringTag ] 3538 SetStringTagSymbol(env, dnPrototype, "Intl.DisplayNames"); 3539 env->SetDisplayNamesFunction(thread_, dnFunction); 3540 3541 // 12.4.3 get Intl.DisplayNames.prototype.of 3542 SetFunction(env, dnPrototype, "of", DisplayNames::Of, FunctionLength::ONE); 3543 3544 // 12.4.4 Intl.DisplayNames.prototype.resolvedOptions () 3545 SetFunction(env, dnPrototype, "resolvedOptions", DisplayNames::ResolvedOptions, FunctionLength::ZERO); 3546} 3547 3548void Builtins::InitializeListFormat(const JSHandle<GlobalEnv> &env) 3549{ 3550 [[maybe_unused]] EcmaHandleScope scope(thread_); 3551 // JSListFormat.prototype 3552 JSHandle<JSFunction> objFun(env->GetObjectFunction()); 3553 JSHandle<JSObject> lfPrototype = factory_->NewJSObjectByConstructor(objFun); 3554 JSHandle<JSTaggedValue> lfPrototypeValue(lfPrototype); 3555 3556 // JSListFormat.prototype_or_hclass 3557 JSHandle<JSHClass> lfFuncInstanceHClass = 3558 factory_->NewEcmaHClass(JSListFormat::SIZE, JSType::JS_LIST_FORMAT, lfPrototypeValue); 3559 3560 // JSListFormat = new Function() 3561 // 13.4.1 Intl.ListFormat.prototype.constructor 3562 JSHandle<JSObject> lfFunction(NewIntlConstructor(env, lfPrototype, ListFormat::ListFormatConstructor, 3563 "ListFormat", FunctionLength::ZERO)); 3564 JSFunction::SetFunctionPrototypeOrInstanceHClass(thread_, 3565 JSHandle<JSFunction>(lfFunction), 3566 JSTaggedValue(*lfFuncInstanceHClass)); 3567 3568 // 13.3.2 Intl.ListFormat.supportedLocalesOf ( locales [ , options ] ) 3569 SetFunction(env, lfFunction, "supportedLocalesOf", ListFormat::SupportedLocalesOf, FunctionLength::ONE); 3570 3571 // ListFormat.prototype method 3572 // 13.4.2 Intl.ListFormat.prototype [ @@toStringTag ] 3573 SetStringTagSymbol(env, lfPrototype, "Intl.ListFormat"); 3574 env->SetListFormatFunction(thread_, lfFunction); 3575 3576 // 13.4.3 get Intl.ListFormat.prototype.format( list ) 3577 SetFunction(env, lfPrototype, "format", ListFormat::Format, FunctionLength::ONE); 3578 3579 // 13.4.4 Intl.ListFormat.prototype.formatToParts( list ) 3580 SetFunction(env, lfPrototype, "formatToParts", ListFormat::FormatToParts, FunctionLength::ONE); 3581 3582 // 13.4.5 Intl.ListFormat.prototype.resolvedOptions() 3583 SetFunction(env, lfPrototype, "resolvedOptions", ListFormat::ResolvedOptions, FunctionLength::ZERO); 3584} 3585 3586void Builtins::InitializeSegmenter(const JSHandle<GlobalEnv> &env) 3587{ 3588 [[maybe_unused]] EcmaHandleScope scope(thread_); 3589 // Segmenter.prototype 3590 JSHandle<JSFunction> objFun(env->GetObjectFunction()); 3591 JSHandle<JSObject> sgPrototype = factory_->NewJSObjectByConstructor(objFun); 3592 JSHandle<JSTaggedValue> sgPrototypeValue(sgPrototype); 3593 3594 // Segmenter.prototype_or_hclass 3595 JSHandle<JSHClass> sgFuncInstanceHClass = 3596 factory_->NewEcmaHClass(JSSegmenter::SIZE, JSType::JS_SEGMENTER, sgPrototypeValue); 3597 3598 // Segmenter = new Function() 3599 // 18.3.1 Intl.Segmenter.prototype.constructor 3600 JSHandle<JSObject> sgFunction(NewIntlConstructor(env, sgPrototype, Segmenter::SegmenterConstructor, 3601 "Segmenter", FunctionLength::ZERO)); 3602 JSFunction::SetFunctionPrototypeOrInstanceHClass(thread_, 3603 JSHandle<JSFunction>(sgFunction), 3604 sgFuncInstanceHClass.GetTaggedValue()); 3605 3606 // 18.2.2 Intl.Segmenter.supportedLocalesOf ( locales [ , options ] ) 3607 SetFunction(env, sgFunction, "supportedLocalesOf", Segmenter::SupportedLocalesOf, FunctionLength::ONE); 3608 3609 // Segmenter.prototype method 3610 // 18.3.2 Intl.Segmenter.prototype [ @@toStringTag ] 3611 SetStringTagSymbol(env, sgPrototype, "Intl.Segmenter"); 3612 env->SetSegmenterFunction(thread_, sgFunction); 3613 3614 // 18.3.4 Intl.Segmenter.prototype.resolvedOptions ( ) 3615 SetFunction(env, sgPrototype, "resolvedOptions", Segmenter::ResolvedOptions, FunctionLength::ZERO); 3616 3617 // 18.3.3 Intl.Segmenter.prototype.segment ( string ) 3618 SetFunction(env, sgPrototype, "segment", Segmenter::Segment, FunctionLength::ONE); 3619} 3620 3621void Builtins::InitializeSegments(const JSHandle<GlobalEnv> &env) 3622{ 3623 [[maybe_unused]] EcmaHandleScope scope(thread_); 3624 // Segments.prototype 3625 JSHandle<JSFunction> objFun(env->GetObjectFunction()); 3626 JSHandle<JSObject> segmentsPrototype = factory_->NewJSObjectByConstructor(objFun); 3627 JSHandle<JSTaggedValue> segmentsPrototypeValue(segmentsPrototype); 3628 3629 // Segments.prototype_or_hclass 3630 JSHandle<JSHClass> segmentsFuncInstanceHClass = 3631 factory_->NewEcmaHClass(JSSegments::SIZE, JSType::JS_SEGMENTS, segmentsPrototypeValue); 3632 3633 JSHandle<JSFunction> segmentsFunction( 3634 factory_->NewJSFunction(env, static_cast<void *>(nullptr), FunctionKind::BASE_CONSTRUCTOR)); 3635 JSFunction::SetFunctionPrototypeOrInstanceHClass(thread_, 3636 JSHandle<JSFunction>(segmentsFunction), 3637 segmentsFuncInstanceHClass.GetTaggedValue()); 3638 3639 env->SetSegmentsFunction(thread_, segmentsFunction); 3640 3641 // %SegmentsPrototype%.containing ( index ) 3642 SetFunction(env, segmentsPrototype, "containing", Segments::Containing, FunctionLength::ONE); 3643 SetAndReturnFunctionAtSymbol(env, segmentsPrototype, 3644 env->GetIteratorSymbol(), "[Symbol.iterator]", Segments::GetSegmentIterator, FunctionLength::ZERO); 3645} 3646 3647void Builtins::InitializeSegmentIterator(const JSHandle<GlobalEnv> &env, 3648 const JSHandle<JSHClass> &iteratorFuncClass) const 3649{ 3650 // SegmentIterator.prototype 3651 JSHandle<JSObject> segIterPrototype(factory_->NewJSObjectWithInit(iteratorFuncClass)); 3652 3653 // SegmentIterator.prototype_or_hclass 3654 JSHandle<JSHClass> segIterFuncInstanceHClass = factory_->NewEcmaHClass( 3655 JSSegmentIterator::SIZE, JSType::JS_SEGMENT_ITERATOR, JSHandle<JSTaggedValue>(segIterPrototype)); 3656 3657 JSHandle<JSFunction> segIterFunction( 3658 factory_->NewJSFunction(env, static_cast<void *>(nullptr), FunctionKind::BASE_CONSTRUCTOR)); 3659 JSFunction::SetFunctionPrototypeOrInstanceHClass(thread_, 3660 JSHandle<JSFunction>(segIterFunction), 3661 segIterFuncInstanceHClass.GetTaggedValue()); 3662 3663 SetFunction(env, segIterPrototype, "next", SegmentIterator::Next, FunctionLength::ZERO); 3664 SetStringTagSymbol(env, segIterPrototype, "Segmenter String Iterator"); 3665 3666 env->SetSegmentIterator(thread_, segIterFunction); 3667 env->SetSegmentIteratorPrototype(thread_, segIterPrototype); 3668} 3669#endif // #ifdef ARK_SUPPORT_INTL 3670 3671JSHandle<JSObject> Builtins::InitializeArkTools(const JSHandle<GlobalEnv> &env) const 3672{ 3673 JSHandle<JSObject> tools = factory_->NewEmptyJSObject(); 3674 for (const base::BuiltinFunctionEntry &entry: builtins::BuiltinsArkTools::GetArkToolsFunctions()) { 3675 SetFunction(env, tools, entry.GetName(), entry.GetEntrypoint(), 3676 entry.GetLength(), entry.GetBuiltinStubId()); 3677 } 3678 JSHandle<JSTaggedValue> gcBuiltins(InitializeGcBuiltins(env)); 3679 SetConstantObject(tools, "GC", gcBuiltins); 3680 return tools; 3681} 3682 3683JSHandle<JSObject> Builtins::InitializeGcBuiltins(const JSHandle<GlobalEnv> &env) const 3684{ 3685 JSHandle<JSObject> builtins = factory_->NewEmptyJSObject(); 3686 for (const base::BuiltinFunctionEntry &entry: builtins::BuiltinsGc::GetGcFunctions()) { 3687 SetFunction(env, builtins, entry.GetName(), entry.GetEntrypoint(), 3688 entry.GetLength(), entry.GetBuiltinStubId()); 3689 } 3690 return builtins; 3691} 3692 3693void Builtins::InitializeGlobalRegExp(JSHandle<JSObject> &obj) const 3694{ 3695 // $1 3696 auto accessor1 = factory_->NewInternalAccessor(reinterpret_cast<void *>(RegExp::SetCapture1), 3697 reinterpret_cast<void *>(RegExp::GetCapture1)); 3698 PropertyDescriptor descriptor1(thread_, JSHandle<JSTaggedValue>::Cast(accessor1), true, false, true); 3699 JSHandle<JSTaggedValue> dollar1Key = thread_->GlobalConstants()->GetHandledDollarStringOne(); 3700 JSObject::DefineOwnProperty(thread_, obj, dollar1Key, descriptor1); 3701 // $2 3702 auto accessor2 = factory_->NewInternalAccessor(reinterpret_cast<void *>(RegExp::SetCapture2), 3703 reinterpret_cast<void *>(RegExp::GetCapture2)); 3704 PropertyDescriptor descriptor2(thread_, JSHandle<JSTaggedValue>::Cast(accessor2), true, false, true); 3705 JSHandle<JSTaggedValue> dollar2Key = thread_->GlobalConstants()->GetHandledDollarStringTwo(); 3706 JSObject::DefineOwnProperty(thread_, obj, dollar2Key, descriptor2); 3707 // $3 3708 auto accessor3 = factory_->NewInternalAccessor(reinterpret_cast<void *>(RegExp::SetCapture3), 3709 reinterpret_cast<void *>(RegExp::GetCapture3)); 3710 PropertyDescriptor descriptor3(thread_, JSHandle<JSTaggedValue>::Cast(accessor3), true, false, true); 3711 JSHandle<JSTaggedValue> dollar3Key = thread_->GlobalConstants()->GetHandledDollarStringThree(); 3712 JSObject::DefineOwnProperty(thread_, obj, dollar3Key, descriptor3); 3713 // $4 3714 auto accessor4 = factory_->NewInternalAccessor(reinterpret_cast<void *>(RegExp::SetCapture4), 3715 reinterpret_cast<void *>(RegExp::GetCapture4)); 3716 PropertyDescriptor descriptor4(thread_, JSHandle<JSTaggedValue>::Cast(accessor4), true, false, true); 3717 JSHandle<JSTaggedValue> dollar4Key = thread_->GlobalConstants()->GetHandledDollarStringFour(); 3718 JSObject::DefineOwnProperty(thread_, obj, dollar4Key, descriptor4); 3719 // $5 3720 auto accessor5 = factory_->NewInternalAccessor(reinterpret_cast<void *>(RegExp::SetCapture5), 3721 reinterpret_cast<void *>(RegExp::GetCapture5)); 3722 PropertyDescriptor descriptor5(thread_, JSHandle<JSTaggedValue>::Cast(accessor5), true, false, true); 3723 JSHandle<JSTaggedValue> dollar5Key = thread_->GlobalConstants()->GetHandledDollarStringFive(); 3724 JSObject::DefineOwnProperty(thread_, obj, dollar5Key, descriptor5); 3725 // $6 3726 auto accessor6 = factory_->NewInternalAccessor(reinterpret_cast<void *>(RegExp::SetCapture6), 3727 reinterpret_cast<void *>(RegExp::GetCapture6)); 3728 PropertyDescriptor descriptor6(thread_, JSHandle<JSTaggedValue>::Cast(accessor6), true, false, true); 3729 JSHandle<JSTaggedValue> dollar6Key = thread_->GlobalConstants()->GetHandledDollarStringSix(); 3730 JSObject::DefineOwnProperty(thread_, obj, dollar6Key, descriptor6); 3731 // $7 3732 auto accessor7 = factory_->NewInternalAccessor(reinterpret_cast<void *>(RegExp::SetCapture7), 3733 reinterpret_cast<void *>(RegExp::GetCapture7)); 3734 PropertyDescriptor descriptor7(thread_, JSHandle<JSTaggedValue>::Cast(accessor7), true, false, true); 3735 JSHandle<JSTaggedValue> dollar7Key = thread_->GlobalConstants()->GetHandledDollarStringSeven(); 3736 JSObject::DefineOwnProperty(thread_, obj, dollar7Key, descriptor7); 3737 // $8 3738 auto accessor8 = factory_->NewInternalAccessor(reinterpret_cast<void *>(RegExp::SetCapture8), 3739 reinterpret_cast<void *>(RegExp::GetCapture8)); 3740 PropertyDescriptor descriptor8(thread_, JSHandle<JSTaggedValue>::Cast(accessor8), true, false, true); 3741 JSHandle<JSTaggedValue> dollar8Key = thread_->GlobalConstants()->GetHandledDollarStringEight(); 3742 JSObject::DefineOwnProperty(thread_, obj, dollar8Key, descriptor8); 3743 // $9 3744 auto accessor9 = factory_->NewInternalAccessor(reinterpret_cast<void *>(RegExp::SetCapture9), 3745 reinterpret_cast<void *>(RegExp::GetCapture9)); 3746 PropertyDescriptor descriptor9(thread_, JSHandle<JSTaggedValue>::Cast(accessor9), true, false, true); 3747 JSHandle<JSTaggedValue> dollar9Key = thread_->GlobalConstants()->GetHandledDollarStringNine(); 3748 JSObject::DefineOwnProperty(thread_, obj, dollar9Key, descriptor9); 3749} 3750 3751JSHandle<JSObject> Builtins::InitializeArkPrivate(const JSHandle<GlobalEnv> &env) const 3752{ 3753 JSHandle<JSObject> arkPrivate = factory_->NewEmptyJSObject(); 3754 SetFrozenFunction(env, arkPrivate, "Load", ContainersPrivate::Load, FunctionLength::ZERO); 3755 3756 // It is used to provide non ECMA standard jsapi containers. 3757 SetConstant(arkPrivate, "ArrayList", JSTaggedValue(static_cast<int>(containers::ContainerTag::ArrayList))); 3758 SetConstant(arkPrivate, "Queue", JSTaggedValue(static_cast<int>(containers::ContainerTag::Queue))); 3759 SetConstant(arkPrivate, "Deque", JSTaggedValue(static_cast<int>(containers::ContainerTag::Deque))); 3760 SetConstant(arkPrivate, "Stack", JSTaggedValue(static_cast<int>(containers::ContainerTag::Stack))); 3761 SetConstant(arkPrivate, "Vector", JSTaggedValue(static_cast<int>(containers::ContainerTag::Vector))); 3762 SetConstant(arkPrivate, "BitVector", JSTaggedValue(static_cast<int>(containers::ContainerTag::BitVector))); 3763 SetConstant(arkPrivate, "List", JSTaggedValue(static_cast<int>(containers::ContainerTag::List))); 3764 SetConstant(arkPrivate, "LinkedList", JSTaggedValue(static_cast<int>(containers::ContainerTag::LinkedList))); 3765 SetConstant(arkPrivate, "TreeMap", JSTaggedValue(static_cast<int>(containers::ContainerTag::TreeMap))); 3766 SetConstant(arkPrivate, "TreeSet", JSTaggedValue(static_cast<int>(containers::ContainerTag::TreeSet))); 3767 SetConstant(arkPrivate, "HashMap", JSTaggedValue(static_cast<int>(containers::ContainerTag::HashMap))); 3768 SetConstant(arkPrivate, "HashSet", JSTaggedValue(static_cast<int>(containers::ContainerTag::HashSet))); 3769 SetConstant(arkPrivate, "LightWeightMap", 3770 JSTaggedValue(static_cast<int>(containers::ContainerTag::LightWeightMap))); 3771 SetConstant(arkPrivate, "LightWeightSet", 3772 JSTaggedValue(static_cast<int>(containers::ContainerTag::LightWeightSet))); 3773 SetConstant(arkPrivate, "PlainArray", JSTaggedValue(static_cast<int>(containers::ContainerTag::PlainArray))); 3774 return arkPrivate; 3775} 3776 3777void Builtins::InitializeModuleNamespace(const JSHandle<GlobalEnv> &env, 3778 const JSHandle<JSHClass> &objFuncClass) const 3779{ 3780 [[maybe_unused]] EcmaHandleScope scope(thread_); 3781 // ModuleNamespace.prototype 3782 JSHandle<JSObject> moduleNamespacePrototype = factory_->NewJSObjectWithInit(objFuncClass); 3783 JSHandle<JSTaggedValue> moduleNamespacePrototypeValue(moduleNamespacePrototype); 3784 3785 // ModuleNamespace.prototype_or_hclass 3786 JSHandle<JSHClass> moduleNamespaceHClass = 3787 factory_->NewEcmaHClass(ModuleNamespace::SIZE, JSType::JS_MODULE_NAMESPACE, moduleNamespacePrototypeValue); 3788 moduleNamespaceHClass->SetPrototype(thread_, JSTaggedValue::Null()); 3789 env->SetModuleNamespaceClass(thread_, moduleNamespaceHClass.GetTaggedValue()); 3790 3791 // moduleNamespace.prototype [ @@toStringTag ] 3792 SetStringTagSymbol(env, moduleNamespacePrototype, "Module"); 3793} 3794 3795void Builtins::InitializeNativeModuleFailureInfo(const JSHandle<GlobalEnv> &env, 3796 const JSHandle<JSHClass> &objFuncClass) const 3797{ 3798 [[maybe_unused]] EcmaHandleScope scope(thread_); 3799 // NativeModuleFailureInfo.prototype 3800 JSHandle<JSObject> nativeModuleFailureInfoPrototype = factory_->NewJSObjectWithInit(objFuncClass); 3801 JSHandle<JSTaggedValue> nativeModuleFailureInfoPrototypeValue(nativeModuleFailureInfoPrototype); 3802 3803 // NativeModuleFailureInfo.prototype_or_hclass 3804 JSHandle<JSHClass> nativeModuleFailureInfoHClass = 3805 factory_->NewEcmaHClass(NativeModuleFailureInfo::SIZE, JSType::NATIVE_MODULE_FAILURE_INFO, 3806 nativeModuleFailureInfoPrototypeValue); 3807 nativeModuleFailureInfoHClass->SetPrototype(thread_, JSTaggedValue::Null()); 3808 env->SetNativeModuleFailureInfoClass(thread_, nativeModuleFailureInfoHClass.GetTaggedValue()); 3809} 3810 3811void Builtins::InitializeCjsModule(const JSHandle<GlobalEnv> &env) const 3812{ 3813 [[maybe_unused]] EcmaHandleScope scope(thread_); 3814 // CjsModule.prototype 3815 JSHandle<JSFunction> objFun(env->GetObjectFunction()); 3816 JSHandle<JSObject> cjsModulePrototype = factory_->NewJSObjectByConstructor(objFun); 3817 JSHandle<JSTaggedValue> cjsModulePrototypeValue(cjsModulePrototype); 3818 3819 // CjsModule.prototype_or_hclass 3820 JSHandle<JSHClass> cjsModuleHClass = 3821 factory_->NewEcmaHClass(CjsModule::SIZE, JSType::JS_CJS_MODULE, cjsModulePrototypeValue); 3822 3823 // CjsModule.prototype.Constructor 3824 JSHandle<JSObject> cjsModuleFunction( 3825 NewBuiltinCjsCtor(env, cjsModulePrototype, BuiltinsCjsModule::CjsModuleConstructor, "Module", 3826 FunctionLength::TWO)); 3827 3828 JSFunction::SetFunctionPrototypeOrInstanceHClass(thread_, 3829 JSHandle<JSFunction>(cjsModuleFunction), 3830 cjsModuleHClass.GetTaggedValue()); 3831 3832 // CjsModule method 3833 SetFunction(env, cjsModuleFunction, "_load", BuiltinsCjsModule::Load, FunctionLength::ONE); 3834 SetFunction(env, cjsModuleFunction, "_resolveFilename", BuiltinsCjsModule::ResolveFilename, FunctionLength::ONE); 3835 3836 // CjsModule.prototype method 3837 SetFunction(env, cjsModulePrototype, "require", BuiltinsCjsModule::Require, FunctionLength::ONE); 3838 SetFunction(env, cjsModulePrototype, "getExportsForCircularRequire", 3839 BuiltinsCjsModule::GetExportsForCircularRequire, FunctionLength::ONE); 3840 SetFunction(env, cjsModulePrototype, "updateChildren", BuiltinsCjsModule::UpdateChildren, FunctionLength::ONE); 3841 3842 JSHandle<JSTaggedValue> id(thread_->GlobalConstants()->GetHandledEmptyString()); 3843 JSHandle<JSTaggedValue> path(thread_->GlobalConstants()->GetHandledEmptyString()); 3844 JSHandle<JSTaggedValue> exports(factory_->NewEmptyJSObject()); 3845 JSHandle<JSTaggedValue> parent(factory_->NewEmptyJSObject()); 3846 JSHandle<JSTaggedValue> filename(thread_->GlobalConstants()->GetHandledEmptyString()); 3847 JSHandle<JSTaggedValue> loaded(factory_->NewEmptyJSObject()); 3848 JSHandle<JSTaggedValue> children(factory_->NewEmptyJSObject()); 3849 JSHandle<JSTaggedValue> cache = JSHandle<JSTaggedValue>::Cast(CjsModuleCache::Create(thread_, 3850 CjsModuleCache::DEAULT_DICTIONART_CAPACITY)); 3851 3852 // CjsModule.prototype members 3853 SetNonConstantObject(cjsModulePrototype, "id", id); 3854 SetNonConstantObject(cjsModulePrototype, "path", path); 3855 SetNonConstantObject(cjsModulePrototype, "exports", exports); 3856 SetNonConstantObject(cjsModulePrototype, "parent", parent); 3857 SetNonConstantObject(cjsModulePrototype, "filename", filename); 3858 SetNonConstantObject(cjsModulePrototype, "loaded", loaded); 3859 SetNonConstantObject(cjsModulePrototype, "children", children); 3860 3861 // CjsModule members 3862 SetNonConstantObject(cjsModuleFunction, "_cache", cache); 3863 3864 env->SetCjsModuleFunction(thread_, cjsModuleFunction); 3865} 3866 3867void Builtins::InitializeCjsExports(const JSHandle<GlobalEnv> &env) const 3868{ 3869 [[maybe_unused]] EcmaHandleScope scope(thread_); 3870 3871 // CjsExports.prototype 3872 JSHandle<JSFunction> objFun(env->GetObjectFunction()); 3873 JSHandle<JSObject> cjsExportsPrototype = factory_->NewJSObjectByConstructor(objFun); 3874 JSHandle<JSTaggedValue> cjsExportsPrototypeValue(cjsExportsPrototype); 3875 3876 // CjsExports.prototype_or_hclass 3877 JSHandle<JSHClass> cjsExportsHClass = 3878 factory_->NewEcmaHClass(CjsExports::SIZE, JSType::JS_CJS_EXPORTS, cjsExportsPrototypeValue); 3879 3880 // CjsExports.prototype.Constructor 3881 JSHandle<JSObject> cjsExportsFunction( 3882 NewBuiltinCjsCtor(env, cjsExportsPrototype, BuiltinsCjsExports::CjsExportsConstructor, "Exports", 3883 FunctionLength::TWO)); 3884 3885 JSFunction::SetFunctionPrototypeOrInstanceHClass(thread_, 3886 JSHandle<JSFunction>(cjsExportsFunction), 3887 cjsExportsHClass.GetTaggedValue()); 3888 3889 env->SetCjsExportsFunction(thread_, cjsExportsFunction); 3890} 3891 3892void Builtins::InitializeCjsRequire(const JSHandle<GlobalEnv> &env) const 3893{ 3894 [[maybe_unused]] EcmaHandleScope scope(thread_); 3895 // CjsRequire.prototype 3896 JSHandle<JSFunction> objFun(env->GetObjectFunction()); 3897 JSHandle<JSObject> cjsRequirePrototype = factory_->NewJSObjectByConstructor(objFun); 3898 JSHandle<JSTaggedValue> cjsRequirePrototypeValue(cjsRequirePrototype); 3899 3900 // CjsExports.prototype_or_hclass 3901 JSHandle<JSHClass> cjsRequireHClass = 3902 factory_->NewEcmaHClass(CjsRequire::SIZE, JSType::JS_CJS_REQUIRE, cjsRequirePrototypeValue); 3903 3904 // CjsExports.prototype.Constructor 3905 JSHandle<JSFunction> cjsRequireFunction = 3906 NewBuiltinCjsCtor(env, cjsRequirePrototype, BuiltinsCjsRequire::CjsRequireConstructor, "require", 3907 FunctionLength::ONE); 3908 JSFunction::SetFunctionPrototypeOrInstanceHClass(thread_, 3909 JSHandle<JSFunction>(cjsRequireFunction), 3910 cjsRequireHClass.GetTaggedValue()); 3911 3912 // CjsModule.prototype method 3913 SetFunction(env, cjsRequirePrototype, "Main", BuiltinsCjsRequire::Main, FunctionLength::ONE); 3914 3915 env->SetCjsRequireFunction(thread_, cjsRequireFunction); 3916} 3917 3918void Builtins::InitializeDefaultExportOfScript(const JSHandle<GlobalEnv> &env) const 3919{ 3920 JSHandle<JSFunction> builtinObj(env->GetObjectFunction()); 3921 JSHandle<JSTaggedValue> emptyObj(factory_->NewJSObjectByConstructor(builtinObj)); 3922 JSHandle<JSTaggedValue> defaultKey(factory_->NewFromUtf8ReadOnly("default")); 3923 3924 JSHandle<TaggedArray> props(factory_->NewTaggedArray(2)); // 2 : two propertise 3925 props->Set(thread_, 0, defaultKey); 3926 props->Set(thread_, 1, emptyObj); 3927 JSHandle<JSHClass> hclass = factory_->CreateObjectClass(props, 1); 3928 JSHandle<JSObject> obj = factory_->NewJSObject(hclass); 3929 obj->SetPropertyInlinedProps(thread_, 0, props->Get(1)); 3930 env->SetExportOfScript(thread_, obj); 3931 return; 3932} 3933 3934JSHandle<JSTaggedValue> Builtins::CreateArrayUnscopables(JSThread *thread) const 3935{ 3936 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 3937 const GlobalEnvConstants *globalConst = thread->GlobalConstants(); 3938 3939 JSHandle<JSObject> unscopableList = factory->CreateNullJSObject(); 3940 3941 JSHandle<JSTaggedValue> trueVal(thread, JSTaggedValue::True()); 3942 3943 JSHandle<JSTaggedValue> atKey((factory->NewFromASCIIReadOnly("at"))); 3944 JSObject::CreateDataProperty(thread, unscopableList, atKey, trueVal); 3945 3946 JSHandle<JSTaggedValue> copyWithKey = globalConst->GetHandledCopyWithinString(); 3947 JSObject::CreateDataProperty(thread, unscopableList, copyWithKey, trueVal); 3948 3949 JSHandle<JSTaggedValue> entriesKey = globalConst->GetHandledEntriesString(); 3950 JSObject::CreateDataProperty(thread, unscopableList, entriesKey, trueVal); 3951 3952 JSHandle<JSTaggedValue> fillKey = globalConst->GetHandledFillString(); 3953 JSObject::CreateDataProperty(thread, unscopableList, fillKey, trueVal); 3954 3955 JSHandle<JSTaggedValue> findKey = globalConst->GetHandledFindString(); 3956 JSObject::CreateDataProperty(thread, unscopableList, findKey, trueVal); 3957 3958 JSHandle<JSTaggedValue> findIndexKey = globalConst->GetHandledFindIndexString(); 3959 JSObject::CreateDataProperty(thread, unscopableList, findIndexKey, trueVal); 3960 3961 JSHandle<JSTaggedValue> findLastKey((factory->NewFromASCIIReadOnly("findLast"))); 3962 JSObject::CreateDataProperty(thread, unscopableList, findLastKey, trueVal); 3963 3964 JSHandle<JSTaggedValue> findLastIndexKey((factory->NewFromASCIIReadOnly("findLastIndex"))); 3965 JSObject::CreateDataProperty(thread, unscopableList, findLastIndexKey, trueVal); 3966 3967 JSHandle<JSTaggedValue> flatKey = globalConst->GetHandledFlatString(); 3968 JSObject::CreateDataProperty(thread, unscopableList, flatKey, trueVal); 3969 3970 JSHandle<JSTaggedValue> flatMapKey = globalConst->GetHandledFlatMapString(); 3971 JSObject::CreateDataProperty(thread, unscopableList, flatMapKey, trueVal); 3972 3973 JSHandle<JSTaggedValue> includesKey = globalConst->GetHandledIncludesString(); 3974 JSObject::CreateDataProperty(thread, unscopableList, includesKey, trueVal); 3975 3976 JSHandle<JSTaggedValue> keysKey = globalConst->GetHandledKeysString(); 3977 JSObject::CreateDataProperty(thread, unscopableList, keysKey, trueVal); 3978 3979 JSHandle<JSTaggedValue> valuesKey = globalConst->GetHandledValuesString(); 3980 JSObject::CreateDataProperty(thread, unscopableList, valuesKey, trueVal); 3981 3982 JSHandle<JSTaggedValue> toReversedKey((factory->NewFromASCIIReadOnly("toReversed"))); 3983 JSObject::CreateDataProperty(thread, unscopableList, toReversedKey, trueVal); 3984 3985 JSHandle<JSTaggedValue> toSortedKey((factory->NewFromASCIIReadOnly("toSorted"))); 3986 JSObject::CreateDataProperty(thread, unscopableList, toSortedKey, trueVal); 3987 3988 JSHandle<JSTaggedValue> toSplicedKey((factory->NewFromASCIIReadOnly("toSpliced"))); 3989 JSObject::CreateDataProperty(thread, unscopableList, toSplicedKey, trueVal); 3990 return JSHandle<JSTaggedValue>::Cast(unscopableList); 3991} 3992 3993void Builtins::RegisterSendableContainers(const JSHandle<GlobalEnv> &env) const 3994{ 3995 auto globalObject = JSHandle<JSObject>::Cast(env->GetJSGlobalObject()); 3996 { 3997 JSHandle<JSTaggedValue> nameString(factory_->NewFromUtf8ReadOnly("SendableMap")); 3998 PropertyDescriptor desc(thread_, env->GetSBuiltininMapFunction(), true, false, true); 3999 JSObject::DefineOwnProperty(thread_, globalObject, nameString, desc); 4000 } 4001 { 4002 JSHandle<JSTaggedValue> nameString(factory_->NewFromUtf8ReadOnly("SendableSet")); 4003 PropertyDescriptor desc(thread_, env->GetSBuiltininSetFunction(), true, false, true); 4004 JSObject::DefineOwnProperty(thread_, globalObject, nameString, desc); 4005 } 4006 { 4007 JSHandle<JSTaggedValue> nameString(factory_->NewFromUtf8ReadOnly("SendableArray")); 4008 PropertyDescriptor desc(thread_, env->GetSharedArrayFunction(), true, false, true); 4009 JSObject::DefineOwnProperty(thread_, globalObject, nameString, desc); 4010 } 4011 { 4012 JSHandle<JSTaggedValue> nameString(factory_->NewFromUtf8ReadOnly("SendableArrayBuffer")); 4013 PropertyDescriptor desc(thread_, env->GetSBuiltininArrayBufferFunction(), true, false, true); 4014 JSObject::DefineOwnProperty(thread_, globalObject, nameString, desc); 4015 } 4016// todo: remove sendableName when refactor 4017#define REGISTER_BUILTIN_SHARED_TYPED_ARRAY(Type, ctorName, TYPE, bytesPerElement, sendableName) \ 4018 { \ 4019 JSHandle<JSTaggedValue> nameString(factory_->NewFromUtf8ReadOnly(#sendableName)); \ 4020 PropertyDescriptor desc(thread_, env->Get##ctorName##Function(), true, false, true); \ 4021 JSObject::DefineOwnProperty(thread_, globalObject, nameString, desc); \ 4022 } 4023 BUILTIN_SHARED_TYPED_ARRAY_TYPES(REGISTER_BUILTIN_SHARED_TYPED_ARRAY) 4024#undef REGISTER_BUILTIN_SHARED_TYPED_ARRAY 4025} 4026} // namespace panda::ecmascript 4027