• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 
17 #include <cstdint>
18 #include <fcntl.h>
19 #include <sys/types.h>
20 #include <unistd.h>
21 
22 #include "ecmascript/base/json_parser.h"
23 #include "ecmascript/base/json_stringifier.h"
24 #include "ecmascript/base/typed_array_helper-inl.h"
25 #include "ecmascript/builtins/builtins_object.h"
26 #if defined(ECMASCRIPT_SUPPORT_CPUPROFILER)
27 #include "ecmascript/dfx/cpu_profiler/cpu_profiler.h"
28 #endif
29 #include "ecmascript/checkpoint/thread_state_transition.h"
30 #include "ecmascript/interpreter/fast_runtime_stub-inl.h"
31 #include "ecmascript/js_generator_object.h"
32 #include "ecmascript/js_regexp.h"
33 #include "ecmascript/module/module_path_helper.h"
34 #include "ecmascript/regexp/regexp_parser.h"
35 #include "ecmascript/js_weak_container.h"
36 #ifdef ARK_SUPPORT_INTL
37 #include "ecmascript/js_bigint.h"
38 #include "ecmascript/js_collator.h"
39 #include "ecmascript/js_date_time_format.h"
40 #include "ecmascript/js_number_format.h"
41 #endif
42 
43 #if defined(ECMASCRIPT_SUPPORT_DEBUGGER) && defined(PANDA_TARGET_IOS)
44 namespace OHOS::ArkCompiler::Toolchain {
45 using DebuggerPostTask = std::function<void(std::function<void()> &&)>;
46 extern "C" {
47     bool StartDebug(const std::string& componentName, void* vm, bool isDebugMode, int32_t instanceId,
48         const DebuggerPostTask& debuggerPostTask, int port);
49     void StopDebug(void* vm);
50     void WaitForDebugger(void* vm);
51 }
52 } // namespace OHOS::ArkCompiler::Toolchain
53 const std::string DEBUGGER_NAME = "PandaDebugger";
54 #endif
55 
56 namespace panda {
57 using ecmascript::ECMAObject;
58 using ecmascript::EcmaString;
59 using ecmascript::EcmaStringAccessor;
60 using ecmascript::ErrorType;
61 using ecmascript::FastRuntimeStub;
62 using ecmascript::GlobalEnv;
63 using ecmascript::GlobalEnvConstants;
64 using ecmascript::EcmaRuntimeCallInfo;
65 using ecmascript::JSArray;
66 using ecmascript::JSArrayBuffer;
67 using ecmascript::JSDataView;
68 using ecmascript::ByteArray;
69 using ecmascript::JSDate;
70 using ecmascript::JSFunction;
71 using ecmascript::JSFunctionBase;
72 using ecmascript::JSHClass;
73 using ecmascript::JSMap;
74 using ecmascript::Method;
75 using ecmascript::JSNativePointer;
76 using ecmascript::JSObject;
77 using ecmascript::JSPandaFile;
78 using ecmascript::JSPandaFileManager;
79 using ecmascript::JSPrimitiveRef;
80 using ecmascript::JSPromise;
81 using ecmascript::JSRegExp;
82 using ecmascript::JSSet;
83 using ecmascript::JSSymbol;
84 using ecmascript::JSTaggedNumber;
85 using ecmascript::JSTaggedType;
86 using ecmascript::JSTaggedValue;
87 using ecmascript::JSThread;
88 using ecmascript::LinkedHashMap;
89 using ecmascript::LinkedHashSet;
90 using ecmascript::ObjectFactory;
91 using ecmascript::PromiseCapability;
92 using ecmascript::PropertyDescriptor;
93 using ecmascript::OperationResult;
94 using ecmascript::Region;
95 using ecmascript::TaggedArray;
96 using ecmascript::JSTypedArray;
97 using ecmascript::base::BuiltinsBase;
98 using ecmascript::builtins::BuiltinsObject;
99 using ecmascript::base::Utf8JsonParser;
100 using ecmascript::base::Utf16JsonParser;
101 using ecmascript::base::JsonStringifier;
102 using ecmascript::base::StringHelper;
103 using ecmascript::base::TypedArrayHelper;
104 using ecmascript::job::MicroJobQueue;
105 using ecmascript::job::QueueType;
106 using ecmascript::JSRuntimeOptions;
107 using ecmascript::BigInt;
108 using ecmascript::MemMapAllocator;
109 using ecmascript::Mutex;
110 using ecmascript::LockHolder;
111 using ecmascript::JSMapIterator;
112 using ecmascript::JSSetIterator;
113 using ecmascript::IterationKind;
114 using ecmascript::JSGeneratorState;
115 using ecmascript::JSIterator;
116 using ecmascript::JSGeneratorFunction;
117 using ecmascript::JSGeneratorObject;
118 using ecmascript::GeneratorContext;
119 using ecmascript::JSProxy;
120 #ifdef ARK_SUPPORT_INTL
121 using ecmascript::JSCollator;
122 using ecmascript::JSDateTimeFormat;
123 using ecmascript::JSNumberFormat;
124 #endif
125 using ecmascript::RegExpParser;
126 using ecmascript::DebugInfoExtractor;
127 using ecmascript::PatchErrorCode;
128 using ecmascript::base::NumberHelper;
129 using ecmascript::Log;
130 using ecmascript::EcmaContext;
131 using ecmascript::JSWeakMap;
132 using ecmascript::JSWeakSet;
133 using ecmascript::JSSendableArrayBuffer;
134 template<typename T>
135 using JSHandle = ecmascript::JSHandle<T>;
136 
137 template<typename T>
138 using JSMutableHandle = ecmascript::JSMutableHandle<T>;
139 
140 using PathHelper = ecmascript::base::PathHelper;
141 using ModulePathHelper = ecmascript::ModulePathHelper;
142 using TransformType = ecmascript::base::JsonHelper::TransformType;
143 
144 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
145 #define TYPED_ARRAY_NEW(Type)                                                                             \
146     Local<Type##Ref> Type##Ref::New(                                                                      \
147         const EcmaVM *vm, Local<ArrayBufferRef> buffer, int32_t byteOffset, int32_t length)               \
148     {                                                                                                     \
149         CROSS_THREAD_AND_EXCEPTION_CHECK_WITH_RETURN(vm, JSValueRef::Undefined(vm));                      \
150         ecmascript::ThreadManagedScope managedScope(vm->GetJSThread());                                   \
151         JSHandle<GlobalEnv> env = vm->GetGlobalEnv();                                                     \
152                                                                                                           \
153         JSHandle<JSTaggedValue> func = env->Get##Type##Function();                                        \
154         JSHandle<JSArrayBuffer> arrayBuffer(JSNApiHelper::ToJSHandle(buffer));                            \
155         JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined();             \
156         const uint32_t argsLength = 3;                                                                    \
157         EcmaRuntimeCallInfo *info =                                                                       \
158             ecmascript::EcmaInterpreter::NewRuntimeCallInfo(thread, func, undefined, func, argsLength);   \
159         RETURN_VALUE_IF_ABRUPT(thread, JSValueRef::Undefined(vm));                                        \
160         info->SetCallArg(arrayBuffer.GetTaggedValue(), JSTaggedValue(byteOffset), JSTaggedValue(length)); \
161         JSTaggedValue result = JSFunction::Construct(info);                                               \
162         RETURN_VALUE_IF_ABRUPT(thread, JSValueRef::Undefined(vm));                                        \
163         JSHandle<JSTaggedValue> resultHandle(thread, result);                                             \
164         return JSNApiHelper::ToLocal<Type##Ref>(resultHandle);                                            \
165     }
166 
167 TYPED_ARRAY_ALL(TYPED_ARRAY_NEW)
168 
169 #undef TYPED_ARRAY_NEW
170 
171 #define SENDABLE_TYPED_ARRAY_NEW(Type)                                                                    \
172     Local<Type##Ref> Type##Ref::New(                                                                      \
173         const EcmaVM *vm, Local<SendableArrayBufferRef> buffer, int32_t byteOffset, int32_t length)       \
174     {                                                                                                     \
175         CROSS_THREAD_AND_EXCEPTION_CHECK_WITH_RETURN(vm, JSValueRef::Undefined(vm));                      \
176         ecmascript::ThreadManagedScope managedScope(vm->GetJSThread());                                   \
177         JSHandle<GlobalEnv> env = vm->GetGlobalEnv();                                                     \
178                                                                                                           \
179         JSHandle<JSTaggedValue> func = env->Get##Type##Function();                                        \
180         JSHandle<JSSendableArrayBuffer> arrayBuffer(JSNApiHelper::ToJSHandle(buffer));                    \
181         JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined();             \
182         const uint32_t argsLength = 3;                                                                    \
183         EcmaRuntimeCallInfo *info =                                                                       \
184             ecmascript::EcmaInterpreter::NewRuntimeCallInfo(thread, func, undefined, func, argsLength);   \
185         RETURN_VALUE_IF_ABRUPT(thread, JSValueRef::Undefined(vm));                                        \
186         info->SetCallArg(arrayBuffer.GetTaggedValue(), JSTaggedValue(byteOffset), JSTaggedValue(length)); \
187         JSTaggedValue result = JSFunction::Construct(info);                                               \
188         RETURN_VALUE_IF_ABRUPT(thread, JSValueRef::Undefined(vm));                                        \
189         JSHandle<JSTaggedValue> resultHandle(thread, result);                                             \
190         return JSNApiHelper::ToLocal<Type##Ref>(resultHandle);                                            \
191     }
192 
SENDABLE_TYPED_ARRAY_ALL(SENDABLE_TYPED_ARRAY_NEW)193 SENDABLE_TYPED_ARRAY_ALL(SENDABLE_TYPED_ARRAY_NEW)
194 
195 #undef SENDABLE_TYPED_ARRAY_NEW
196 
197 // ---------------------------------- JSON ------------------------------------------
198 Local<JSValueRef> JSON::Parse(const EcmaVM *vm, Local<StringRef> string)
199 {
200     CROSS_THREAD_AND_EXCEPTION_CHECK_WITH_RETURN(vm, JSValueRef::Undefined(vm));
201     ecmascript::ThreadManagedScope managedScope(vm->GetJSThread());
202     auto ecmaStr = EcmaString::Cast(JSNApiHelper::ToJSTaggedValue(*string).GetTaggedObject());
203     JSHandle<JSTaggedValue> result;
204     if (EcmaStringAccessor(ecmaStr).IsUtf8()) {
205         Utf8JsonParser parser(thread, TransformType::NORMAL);
206         JSHandle<EcmaString> str(thread, JSNApiHelper::ToJSTaggedValue(*string));
207         result = parser.Parse(str);
208     } else {
209         Utf16JsonParser parser(thread, TransformType::NORMAL);
210         result = parser.Parse(EcmaString::Cast(JSNApiHelper::ToJSTaggedValue(*string).GetTaggedObject()));
211     }
212     RETURN_VALUE_IF_ABRUPT(thread, JSValueRef::Undefined(vm));
213     return JSNApiHelper::ToLocal<JSValueRef>(result);
214 }
215 
Stringify(const EcmaVM * vm,Local<JSValueRef> json)216 Local<JSValueRef> JSON::Stringify(const EcmaVM *vm, Local<JSValueRef> json)
217 {
218     CROSS_THREAD_AND_EXCEPTION_CHECK_WITH_RETURN(vm, JSValueRef::Undefined(vm));
219     ecmascript::ThreadManagedScope managedScope(vm->GetJSThread());
220     auto constants = thread->GlobalConstants();
221     JsonStringifier stringifier(thread);
222     JSHandle<JSTaggedValue> str = stringifier.Stringify(
223         JSNApiHelper::ToJSHandle(json), constants->GetHandledUndefined(), constants->GetHandledUndefined());
224     RETURN_VALUE_IF_ABRUPT(thread, JSValueRef::Undefined(vm));
225     return JSNApiHelper::ToLocal<JSValueRef>(str);
226 }
227 
GetOriginalSource(const EcmaVM * vm)228 Local<StringRef> RegExpRef::GetOriginalSource(const EcmaVM *vm)
229 {
230     CROSS_THREAD_AND_EXCEPTION_CHECK_WITH_RETURN(vm, JSValueRef::Undefined(vm));
231     ecmascript::ThreadManagedScope managedScope(thread);
232     JSHandle<JSRegExp> regExp(JSNApiHelper::ToJSHandle(this));
233     LOG_IF_SPECIAL(regExp, FATAL);
234     JSTaggedValue source = regExp->GetOriginalSource();
235     if (!source.IsString()) {
236         auto constants = thread->GlobalConstants();
237         return JSNApiHelper::ToLocal<StringRef>(constants->GetHandledEmptyString());
238     }
239     JSHandle<JSTaggedValue> sourceHandle(thread, source);
240     return JSNApiHelper::ToLocal<StringRef>(sourceHandle);
241 }
242 
GetOriginalFlags(const EcmaVM * vm)243 std::string RegExpRef::GetOriginalFlags([[maybe_unused]] const EcmaVM *vm)
244 {
245     DCHECK_SPECIAL_VALUE_WITH_RETURN(this, "");
246     ecmascript::ThreadManagedScope managedScope(vm->GetJSThread());
247     JSHandle<JSRegExp> regExp(JSNApiHelper::ToJSHandle(this));
248     JSTaggedValue regExpFlags = regExp->GetOriginalFlags();
249     uint32_t regExpFlagsInt = static_cast<uint32_t>(regExpFlags.GetInt());
250     std::string strFlags = "";
251     if (regExpFlagsInt & RegExpParser::FLAG_GLOBAL) {
252         strFlags += "g";
253     }
254     if (regExpFlagsInt & RegExpParser::FLAG_IGNORECASE) {
255         strFlags += "i";
256     }
257     if (regExpFlagsInt & RegExpParser::FLAG_MULTILINE) {
258         strFlags += "m";
259     }
260     if (regExpFlagsInt & RegExpParser::FLAG_DOTALL) {
261         strFlags += "s";
262     }
263     if (regExpFlagsInt & RegExpParser::FLAG_UTF16) {
264         strFlags += "u";
265     }
266     if (regExpFlagsInt & RegExpParser::FLAG_STICKY) {
267         strFlags += "y";
268     }
269     std::sort(strFlags.begin(), strFlags.end());
270     return strFlags;
271 }
272 
IsGlobal(const EcmaVM * vm)273 Local<JSValueRef> RegExpRef::IsGlobal(const EcmaVM *vm)
274 {
275     CROSS_THREAD_AND_EXCEPTION_CHECK_WITH_RETURN(vm, JSValueRef::Undefined(vm));
276     ecmascript::ThreadManagedScope managedScope(vm->GetJSThread());
277     JSHandle<JSRegExp> regExp(JSNApiHelper::ToJSHandle(this));
278     LOG_IF_SPECIAL(regExp, FATAL);
279     JSTaggedValue flags = regExp->GetOriginalFlags();
280     bool result = flags.GetInt() & RegExpParser::FLAG_GLOBAL;
281     Local<JSValueRef> jsValue = BooleanRef::New(vm, result);
282     return jsValue;
283 }
284 
IsIgnoreCase(const EcmaVM * vm)285 Local<JSValueRef> RegExpRef::IsIgnoreCase(const EcmaVM *vm)
286 {
287     CROSS_THREAD_AND_EXCEPTION_CHECK_WITH_RETURN(vm, JSValueRef::Undefined(vm));
288     ecmascript::ThreadManagedScope managedScope(vm->GetJSThread());
289     JSHandle<JSRegExp> regExp(JSNApiHelper::ToJSHandle(this));
290     LOG_IF_SPECIAL(regExp, FATAL);
291     JSTaggedValue flags = regExp->GetOriginalFlags();
292     bool result = flags.GetInt() & RegExpParser::FLAG_IGNORECASE;
293     Local<JSValueRef> jsValue = BooleanRef::New(vm, result);
294     return jsValue;
295 }
296 
IsMultiline(const EcmaVM * vm)297 Local<JSValueRef> RegExpRef::IsMultiline(const EcmaVM *vm)
298 {
299     CROSS_THREAD_AND_EXCEPTION_CHECK_WITH_RETURN(vm, JSValueRef::Undefined(vm));
300     ecmascript::ThreadManagedScope managedScope(vm->GetJSThread());
301     JSHandle<JSRegExp> regExp(JSNApiHelper::ToJSHandle(this));
302     LOG_IF_SPECIAL(regExp, FATAL);
303     JSTaggedValue flags = regExp->GetOriginalFlags();
304     bool result = flags.GetInt() & RegExpParser::FLAG_MULTILINE;
305     Local<JSValueRef> jsValue = BooleanRef::New(vm, result);
306     return jsValue;
307 }
308 
IsDotAll(const EcmaVM * vm)309 Local<JSValueRef> RegExpRef::IsDotAll(const EcmaVM *vm)
310 {
311     CROSS_THREAD_AND_EXCEPTION_CHECK_WITH_RETURN(vm, JSValueRef::Undefined(vm));
312     ecmascript::ThreadManagedScope managedScope(vm->GetJSThread());
313     JSHandle<JSRegExp> regExp(JSNApiHelper::ToJSHandle(this));
314     LOG_IF_SPECIAL(regExp, FATAL);
315     JSTaggedValue flags = regExp->GetOriginalFlags();
316     bool result = flags.GetInt() & RegExpParser::FLAG_DOTALL;
317     Local<JSValueRef> jsValue = BooleanRef::New(vm, result);
318     return jsValue;
319 }
320 
IsUtf16(const EcmaVM * vm)321 Local<JSValueRef> RegExpRef::IsUtf16(const EcmaVM *vm)
322 {
323     CROSS_THREAD_AND_EXCEPTION_CHECK_WITH_RETURN(vm, JSValueRef::Undefined(vm));
324     ecmascript::ThreadManagedScope managedScope(vm->GetJSThread());
325     JSHandle<JSRegExp> regExp(JSNApiHelper::ToJSHandle(this));
326     LOG_IF_SPECIAL(regExp, FATAL);
327     JSTaggedValue flags = regExp->GetOriginalFlags();
328     bool result = flags.GetInt() & RegExpParser::FLAG_UTF16;
329     Local<JSValueRef> jsValue = BooleanRef::New(vm, result);
330     return jsValue;
331 }
332 
IsStick(const EcmaVM * vm)333 Local<JSValueRef> RegExpRef::IsStick(const EcmaVM *vm)
334 {
335     CROSS_THREAD_AND_EXCEPTION_CHECK_WITH_RETURN(vm, JSValueRef::Undefined(vm));
336     ecmascript::ThreadManagedScope managedScope(vm->GetJSThread());
337     JSHandle<JSRegExp> regExp(JSNApiHelper::ToJSHandle(this));
338     LOG_IF_SPECIAL(regExp, FATAL);
339     JSTaggedValue flags = regExp->GetOriginalFlags();
340     bool result = flags.GetInt() & RegExpParser::FLAG_STICKY;
341     Local<JSValueRef> jsValue = BooleanRef::New(vm, result);
342     return jsValue;
343 }
344 
IsGenerator(const EcmaVM * vm)345 bool GeneratorFunctionRef::IsGenerator(const EcmaVM *vm)
346 {
347     ecmascript::ThreadManagedScope managedScope(vm->GetJSThread());
348     // Omit exception check because ark calls here may not
349     // cause side effect even pending exception exists.
350     return IsGeneratorFunction(vm);
351 }
352 
GetGeneratorState(const EcmaVM * vm)353 Local<JSValueRef> GeneratorObjectRef::GetGeneratorState(const EcmaVM *vm)
354 {
355     CROSS_THREAD_AND_EXCEPTION_CHECK_WITH_RETURN(vm, JSValueRef::Undefined(vm));
356     ecmascript::ThreadManagedScope managedScope(vm->GetJSThread());
357     JSHandle<JSGeneratorObject> jsGenerator(JSNApiHelper::ToJSHandle(this));
358     LOG_IF_SPECIAL(jsGenerator, FATAL);
359     if (jsGenerator->GetGeneratorState() == JSGeneratorState::COMPLETED) {
360         return StringRef::NewFromUtf8(vm, "closed");
361     }
362     return StringRef::NewFromUtf8(vm, "suspended");
363 }
364 
GetGeneratorFunction(const EcmaVM * vm)365 Local<JSValueRef> GeneratorObjectRef::GetGeneratorFunction(const EcmaVM *vm)
366 {
367     CROSS_THREAD_AND_EXCEPTION_CHECK_WITH_RETURN(vm, JSValueRef::Undefined(vm));
368     ecmascript::ThreadManagedScope managedScope(vm->GetJSThread());
369     JSHandle<JSGeneratorObject> jsGenerator(JSNApiHelper::ToJSHandle(this));
370     LOG_IF_SPECIAL(jsGenerator, FATAL);
371     JSHandle<GeneratorContext> generatorContext(thread, jsGenerator->GetGeneratorContext());
372     JSTaggedValue jsTagValue = generatorContext->GetMethod();
373     return JSNApiHelper::ToLocal<GeneratorFunctionRef>(JSHandle<JSTaggedValue>(thread, jsTagValue));
374 }
375 
GetGeneratorReceiver(const EcmaVM * vm)376 Local<JSValueRef> GeneratorObjectRef::GetGeneratorReceiver(const EcmaVM *vm)
377 {
378     CROSS_THREAD_AND_EXCEPTION_CHECK_WITH_RETURN(vm, JSValueRef::Undefined(vm));
379     ecmascript::ThreadManagedScope managedScope(vm->GetJSThread());
380     JSHandle<JSGeneratorObject> jsGenerator(JSNApiHelper::ToJSHandle(this));
381     LOG_IF_SPECIAL(jsGenerator, FATAL);
382     JSHandle<GeneratorContext> generatorContext(thread, jsGenerator->GetGeneratorContext());
383     JSTaggedValue jsTagValue = generatorContext->GetAcc();
384     return JSNApiHelper::ToLocal<GeneratorObjectRef>(JSHandle<JSTaggedValue>(thread, jsTagValue));
385 }
386 
GetCompareFunction(const EcmaVM * vm)387 Local<JSValueRef> CollatorRef::GetCompareFunction(const EcmaVM *vm)
388 {
389     CROSS_THREAD_AND_EXCEPTION_CHECK_WITH_RETURN(vm, JSValueRef::Undefined(vm));
390     ecmascript::ThreadManagedScope managedScope(vm->GetJSThread());
391 #ifdef ARK_SUPPORT_INTL
392     JSHandle<JSCollator> jsCollator(JSNApiHelper::ToJSHandle(this));
393     LOG_IF_SPECIAL(jsCollator, FATAL);
394     JSTaggedValue jsTagValue = jsCollator->GetBoundCompare();
395     return JSNApiHelper::ToLocal<CollatorRef>(JSHandle<JSTaggedValue>(thread, jsTagValue));
396 #else
397     LOG_ECMA(ERROR) << "Not support arkcompiler intl";
398     return JSNApiHelper::ToLocal<CollatorRef>(JSHandle<JSTaggedValue>(thread, JSTaggedValue::Undefined()));
399 #endif
400 }
401 
GetFormatFunction(const EcmaVM * vm)402 Local<JSValueRef> DataTimeFormatRef::GetFormatFunction(const EcmaVM *vm)
403 {
404     CROSS_THREAD_AND_EXCEPTION_CHECK_WITH_RETURN(vm, JSValueRef::Undefined(vm));
405     ecmascript::ThreadManagedScope managedScope(vm->GetJSThread());
406 #ifdef ARK_SUPPORT_INTL
407     JSHandle<JSDateTimeFormat> jsDateTimeFormat(JSNApiHelper::ToJSHandle(this));
408     LOG_IF_SPECIAL(jsDateTimeFormat, FATAL);
409     JSTaggedValue jsTagValue = jsDateTimeFormat->GetBoundFormat();
410     return JSNApiHelper::ToLocal<DataTimeFormatRef>(JSHandle<JSTaggedValue>(thread, jsTagValue));
411 #else
412     LOG_ECMA(ERROR) << "Not support arkcompiler intl";
413     return JSNApiHelper::ToLocal<DataTimeFormatRef>(JSHandle<JSTaggedValue>(thread, JSTaggedValue::Undefined()));
414 #endif
415 }
416 
GetFormatFunction(const EcmaVM * vm)417 Local<JSValueRef> NumberFormatRef::GetFormatFunction(const EcmaVM *vm)
418 {
419     CROSS_THREAD_AND_EXCEPTION_CHECK_WITH_RETURN(vm, JSValueRef::Undefined(vm));
420     ecmascript::ThreadManagedScope managedScope(vm->GetJSThread());
421 #ifdef ARK_SUPPORT_INTL
422     JSHandle<JSNumberFormat> jsNumberFormat(JSNApiHelper::ToJSHandle(this));
423     LOG_IF_SPECIAL(jsNumberFormat, FATAL);
424     JSTaggedValue jsTagValue = jsNumberFormat->GetBoundFormat();
425     return JSNApiHelper::ToLocal<NumberFormatRef>(JSHandle<JSTaggedValue>(thread, jsTagValue));
426 #else
427     LOG_ECMA(ERROR) << "Not support arkcompiler intl";
428     return JSNApiHelper::ToLocal<NumberFormatRef>(JSHandle<JSTaggedValue>(thread, JSTaggedValue::Undefined()));
429 #endif
430 }
431 
432 // ----------------------------------- FunctionCallback ---------------------------------
RegisterCallback(ecmascript::EcmaRuntimeCallInfo * ecmaRuntimeCallInfo)433 JSTaggedValue Callback::RegisterCallback(ecmascript::EcmaRuntimeCallInfo *ecmaRuntimeCallInfo)
434 {
435     // Constructor
436     JSThread *thread = ecmaRuntimeCallInfo->GetThread();
437     ecmascript::ThreadManagedScope managedScope(thread);
438     JSHandle<JSTaggedValue> constructor = BuiltinsBase::GetConstructor(ecmaRuntimeCallInfo);
439     if (!constructor->IsJSFunction()) {
440         return JSTaggedValue::False();
441     }
442     [[maybe_unused]] LocalScope scope(thread->GetEcmaVM());
443     JSHandle<JSFunctionBase> function(constructor);
444     JSTaggedValue extraInfoValue = function->GetFunctionExtraInfo();
445     if (!extraInfoValue.IsJSNativePointer()) {
446         return JSTaggedValue::False();
447     }
448     JSHandle<JSNativePointer> extraInfo(thread, extraInfoValue);
449     // callBack
450     FunctionCallback nativeFunc = reinterpret_cast<FunctionCallback>(extraInfo->GetExternalPointer());
451 
452     JsiRuntimeCallInfo *jsiRuntimeCallInfo = reinterpret_cast<JsiRuntimeCallInfo *>(ecmaRuntimeCallInfo);
453 #if defined(ECMASCRIPT_SUPPORT_CPUPROFILER)
454     bool getStackBeforeCallNapiSuccess = false;
455     if (thread->GetIsProfiling() && function->IsCallNapi()) {
456         getStackBeforeCallNapiSuccess = thread->GetEcmaVM()->GetProfiler()->GetStackBeforeCallNapi(thread);
457     }
458 #endif
459     Local<JSValueRef> result;
460     {
461         ecmascript::ThreadNativeScope nativeScope(thread);
462 #if ECMASCRIPT_ENABLE_INTERPRETER_ARKUINAITVE_TRACE
463         ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "ArkUI::NativeCallBack");
464 #endif
465         result = nativeFunc(jsiRuntimeCallInfo);
466     }
467 #if defined(ECMASCRIPT_SUPPORT_CPUPROFILER)
468     if (thread->GetIsProfiling() && function->IsCallNapi() && getStackBeforeCallNapiSuccess) {
469         thread->GetEcmaVM()->GetProfiler()->GetStackAfterCallNapi(thread);
470     }
471 #endif
472     return JSNApiHelper::ToJSHandle(result).GetTaggedValue();
473 }
474 }  // namespace panda
475