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