2021-09-04 08:06:49 +00:00
|
|
|
/*
|
2022-04-16 07:57:40 +00:00
|
|
|
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
|
2021-09-04 08:06:49 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ecmascript/ecma_vm.h"
|
|
|
|
|
|
|
|
#include "ecmascript/base/string_helper.h"
|
2022-08-25 12:47:18 +00:00
|
|
|
#include "ecmascript/builtins/builtins.h"
|
2023-01-29 03:40:13 +00:00
|
|
|
#include "ecmascript/builtins/builtins_ark_tools.h"
|
2023-03-03 02:59:00 +00:00
|
|
|
#ifdef ARK_SUPPORT_INTL
|
2022-08-16 13:05:02 +00:00
|
|
|
#include "ecmascript/builtins/builtins_collator.h"
|
|
|
|
#include "ecmascript/builtins/builtins_date_time_format.h"
|
|
|
|
#include "ecmascript/builtins/builtins_number_format.h"
|
2023-03-03 02:59:00 +00:00
|
|
|
#endif
|
|
|
|
#include "ecmascript/builtins/builtins_global.h"
|
2022-08-16 13:05:02 +00:00
|
|
|
#include "ecmascript/builtins/builtins_object.h"
|
|
|
|
#include "ecmascript/builtins/builtins_promise.h"
|
|
|
|
#include "ecmascript/builtins/builtins_promise_handler.h"
|
|
|
|
#include "ecmascript/builtins/builtins_proxy.h"
|
2021-09-04 08:06:49 +00:00
|
|
|
#include "ecmascript/builtins/builtins_regexp.h"
|
2022-08-04 06:58:57 +00:00
|
|
|
#include "ecmascript/compiler/builtins/builtins_call_signature.h"
|
2022-05-16 11:38:24 +00:00
|
|
|
#include "ecmascript/compiler/call_signature.h"
|
2022-03-19 01:17:21 +00:00
|
|
|
#include "ecmascript/compiler/common_stubs.h"
|
2022-03-16 10:12:15 +00:00
|
|
|
#include "ecmascript/compiler/interpreter_stub.h"
|
|
|
|
#include "ecmascript/compiler/rt_call_signature.h"
|
2022-02-26 03:08:37 +00:00
|
|
|
#if defined(ECMASCRIPT_SUPPORT_CPUPROFILER)
|
2022-03-03 14:22:54 +00:00
|
|
|
#include "ecmascript/dfx/cpu_profiler/cpu_profiler.h"
|
2022-02-26 03:08:37 +00:00
|
|
|
#endif
|
2022-10-10 02:08:43 +00:00
|
|
|
#if !WIN_OR_MAC_OR_IOS_PLATFORM
|
2022-08-02 08:07:48 +00:00
|
|
|
#include "ecmascript/dfx/hprof/heap_profiler.h"
|
|
|
|
#include "ecmascript/dfx/hprof/heap_profiler_interface.h"
|
|
|
|
#endif
|
2023-04-20 08:03:36 +00:00
|
|
|
#include "ecmascript/compiler/aot_file/an_file_data_manager.h"
|
|
|
|
#include "ecmascript/compiler/aot_file/aot_file_manager.h"
|
2022-09-20 11:25:15 +00:00
|
|
|
#include "ecmascript/debugger/js_debugger_manager.h"
|
2023-04-27 12:03:12 +00:00
|
|
|
#include "ecmascript/dfx/stackinfo/js_stackinfo.h"
|
2023-06-04 03:04:08 +00:00
|
|
|
#include "ecmascript/dfx/vmstat/function_call_timer.h"
|
2022-12-14 09:07:53 +00:00
|
|
|
#include "ecmascript/dfx/vmstat/opt_code_profiler.h"
|
2022-03-03 14:22:54 +00:00
|
|
|
#include "ecmascript/dfx/vmstat/runtime_stat.h"
|
2023-04-15 08:10:54 +00:00
|
|
|
#include "ecmascript/ecma_context.h"
|
2021-09-04 08:06:49 +00:00
|
|
|
#include "ecmascript/ecma_string_table.h"
|
|
|
|
#include "ecmascript/global_env.h"
|
|
|
|
#include "ecmascript/global_env_constants-inl.h"
|
|
|
|
#include "ecmascript/global_env_constants.h"
|
2022-03-24 07:04:57 +00:00
|
|
|
#include "ecmascript/interpreter/interpreter-inl.h"
|
2021-09-04 08:06:49 +00:00
|
|
|
#include "ecmascript/jobs/micro_job_queue.h"
|
2023-04-20 08:03:36 +00:00
|
|
|
#include "ecmascript/js_arraybuffer.h"
|
|
|
|
#include "ecmascript/js_for_in_iterator.h"
|
|
|
|
#include "ecmascript/js_native_pointer.h"
|
|
|
|
#include "ecmascript/js_thread.h"
|
2022-05-06 08:34:15 +00:00
|
|
|
#include "ecmascript/jspandafile/constpool_value.h"
|
2022-02-06 15:16:08 +00:00
|
|
|
#include "ecmascript/jspandafile/js_pandafile.h"
|
|
|
|
#include "ecmascript/jspandafile/js_pandafile_manager.h"
|
2022-01-30 08:01:33 +00:00
|
|
|
#include "ecmascript/jspandafile/panda_file_translator.h"
|
2022-04-05 13:28:18 +00:00
|
|
|
#include "ecmascript/jspandafile/program_object.h"
|
2021-12-17 09:18:10 +00:00
|
|
|
#include "ecmascript/mem/concurrent_marker.h"
|
2022-05-11 09:36:28 +00:00
|
|
|
#include "ecmascript/mem/gc_stats.h"
|
2023-04-20 08:03:36 +00:00
|
|
|
#include "ecmascript/mem/heap.h"
|
2022-05-11 09:36:28 +00:00
|
|
|
#include "ecmascript/mem/mem.h"
|
|
|
|
#include "ecmascript/mem/space.h"
|
|
|
|
#include "ecmascript/mem/visitor.h"
|
2022-01-30 08:01:33 +00:00
|
|
|
#include "ecmascript/module/js_module_manager.h"
|
2023-03-05 10:05:15 +00:00
|
|
|
#include "ecmascript/module/module_data_extractor.h"
|
2023-04-20 08:03:36 +00:00
|
|
|
#include "ecmascript/napi/include/dfx_jsnapi.h"
|
2021-09-04 08:06:49 +00:00
|
|
|
#include "ecmascript/object_factory.h"
|
2022-11-29 06:28:34 +00:00
|
|
|
#include "ecmascript/patch/quick_fix_manager.h"
|
2023-02-09 13:47:15 +00:00
|
|
|
#include "ecmascript/pgo_profiler/pgo_profiler_manager.h"
|
2021-09-04 08:06:49 +00:00
|
|
|
#include "ecmascript/regexp/regexp_parser_cache.h"
|
|
|
|
#include "ecmascript/runtime_call_id.h"
|
2022-05-11 09:36:28 +00:00
|
|
|
#include "ecmascript/snapshot/mem/snapshot.h"
|
2023-04-20 08:03:36 +00:00
|
|
|
#include "ecmascript/snapshot/mem/snapshot_env.h"
|
2022-03-16 10:12:15 +00:00
|
|
|
#include "ecmascript/stubs/runtime_stubs.h"
|
2021-09-04 08:06:49 +00:00
|
|
|
#include "ecmascript/tagged_array-inl.h"
|
2021-12-17 09:18:10 +00:00
|
|
|
#include "ecmascript/tagged_dictionary.h"
|
2022-04-05 13:28:18 +00:00
|
|
|
#include "ecmascript/tagged_queue.h"
|
2021-09-04 08:06:49 +00:00
|
|
|
#include "ecmascript/tagged_queue.h"
|
2023-04-20 08:03:36 +00:00
|
|
|
#include "ecmascript/taskpool/task.h"
|
|
|
|
#include "ecmascript/taskpool/taskpool.h"
|
2022-07-21 17:37:55 +00:00
|
|
|
#include "ecmascript/ts_types/ts_manager.h"
|
2021-09-04 08:06:49 +00:00
|
|
|
|
|
|
|
namespace panda::ecmascript {
|
2023-02-21 06:20:10 +00:00
|
|
|
using RandomGenerator = base::RandomGenerator;
|
2023-04-27 12:03:12 +00:00
|
|
|
AOTFileManager *JsStackInfo::loader = nullptr;
|
2021-09-04 08:06:49 +00:00
|
|
|
/* static */
|
2022-06-15 03:23:16 +00:00
|
|
|
EcmaVM *EcmaVM::Create(const JSRuntimeOptions &options, EcmaParamConfiguration &config)
|
2021-09-04 08:06:49 +00:00
|
|
|
{
|
2022-06-20 11:04:49 +00:00
|
|
|
JSRuntimeOptions newOptions = options;
|
2022-07-05 09:18:30 +00:00
|
|
|
// only define SUPPORT_ENABLE_ASM_INTERP can enable asm-interpreter
|
|
|
|
#if !defined(SUPPORT_ENABLE_ASM_INTERP)
|
2022-06-20 11:04:49 +00:00
|
|
|
newOptions.SetEnableAsmInterpreter(false);
|
|
|
|
#endif
|
|
|
|
auto vm = new EcmaVM(newOptions, config);
|
2021-09-04 08:06:49 +00:00
|
|
|
if (UNLIKELY(vm == nullptr)) {
|
|
|
|
LOG_ECMA(ERROR) << "Failed to create jsvm";
|
|
|
|
return nullptr;
|
|
|
|
}
|
2022-04-20 08:27:09 +00:00
|
|
|
auto jsThread = JSThread::Create(vm);
|
2021-09-04 08:06:49 +00:00
|
|
|
vm->thread_ = jsThread;
|
|
|
|
vm->Initialize();
|
2023-05-23 01:34:51 +00:00
|
|
|
JsStackInfo::loader = vm->GetJSThread()->GetCurrentEcmaContext()->GetAOTFileManager();
|
2023-05-08 01:39:58 +00:00
|
|
|
#if defined(__aarch64__) && !defined(PANDA_TARGET_MACOS) && !defined(PANDA_TARGET_IOS)
|
2023-04-27 12:03:12 +00:00
|
|
|
if (SetThreadInfoCallback != nullptr) {
|
|
|
|
SetThreadInfoCallback(CrashCallback);
|
|
|
|
}
|
|
|
|
#endif
|
2021-09-04 08:06:49 +00:00
|
|
|
return vm;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2022-04-20 08:27:09 +00:00
|
|
|
bool EcmaVM::Destroy(EcmaVM *vm)
|
2021-09-04 08:06:49 +00:00
|
|
|
{
|
|
|
|
if (vm != nullptr) {
|
2022-04-20 08:27:09 +00:00
|
|
|
delete vm;
|
|
|
|
vm = nullptr;
|
2021-09-04 08:06:49 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-11-17 08:32:20 +00:00
|
|
|
void EcmaVM::PreFork()
|
2022-07-05 06:15:02 +00:00
|
|
|
{
|
|
|
|
heap_->CompactHeapBeforeFork();
|
2022-08-08 02:31:03 +00:00
|
|
|
heap_->AdjustSpaceSizeForAppSpawn();
|
2022-07-05 06:15:02 +00:00
|
|
|
heap_->GetReadOnlySpace()->SetReadOnly();
|
|
|
|
heap_->DisableParallelGC();
|
|
|
|
}
|
|
|
|
|
2022-11-17 08:32:20 +00:00
|
|
|
void EcmaVM::PostFork()
|
2022-07-05 06:15:02 +00:00
|
|
|
{
|
2023-02-21 06:20:10 +00:00
|
|
|
RandomGenerator::InitRandom();
|
2023-01-10 07:42:27 +00:00
|
|
|
heap_->SetHeapMode(HeapMode::SHARE);
|
2022-07-05 06:15:02 +00:00
|
|
|
GetAssociatedJSThread()->SetThreadId();
|
2022-12-22 07:16:35 +00:00
|
|
|
heap_->EnableParallelGC();
|
2022-07-05 06:15:02 +00:00
|
|
|
}
|
|
|
|
|
2022-06-15 03:23:16 +00:00
|
|
|
EcmaVM::EcmaVM(JSRuntimeOptions options, EcmaParamConfiguration config)
|
2023-05-17 06:37:01 +00:00
|
|
|
: nativeAreaAllocator_(std::make_unique<NativeAreaAllocator>()),
|
2022-02-17 07:11:23 +00:00
|
|
|
heapRegionAllocator_(std::make_unique<HeapRegionAllocator>()),
|
|
|
|
chunk_(nativeAreaAllocator_.get()),
|
2022-06-15 03:23:16 +00:00
|
|
|
ecmaParamConfiguration_(std::move(config))
|
2021-09-04 08:06:49 +00:00
|
|
|
{
|
2021-10-19 03:55:00 +00:00
|
|
|
options_ = std::move(options);
|
2022-04-30 12:02:19 +00:00
|
|
|
icEnabled_ = options_.EnableIC();
|
|
|
|
optionalLogEnabled_ = options_.EnableOptionalLog();
|
2022-03-18 07:17:30 +00:00
|
|
|
options_.ParseAsmInterOption();
|
2021-09-04 08:06:49 +00:00
|
|
|
}
|
|
|
|
|
2023-03-08 07:05:35 +00:00
|
|
|
void EcmaVM::InitializePGOProfiler()
|
2023-01-30 13:03:02 +00:00
|
|
|
{
|
2023-03-24 10:29:10 +00:00
|
|
|
bool isEnablePGOProfiler = IsEnablePGOProfiler();
|
|
|
|
if (pgoProfiler_ == nullptr) {
|
|
|
|
pgoProfiler_ = PGOProfilerManager::GetInstance()->Build(this, isEnablePGOProfiler);
|
2023-03-22 03:00:58 +00:00
|
|
|
}
|
2023-01-30 13:03:02 +00:00
|
|
|
thread_->SetPGOProfilerEnable(isEnablePGOProfiler);
|
|
|
|
}
|
|
|
|
|
2023-03-08 07:05:35 +00:00
|
|
|
void EcmaVM::ResetPGOProfiler()
|
|
|
|
{
|
|
|
|
if (pgoProfiler_ != nullptr) {
|
|
|
|
bool isEnablePGOProfiler = IsEnablePGOProfiler();
|
|
|
|
PGOProfilerManager::GetInstance()->Reset(pgoProfiler_, isEnablePGOProfiler);
|
|
|
|
thread_->SetPGOProfilerEnable(isEnablePGOProfiler);
|
|
|
|
thread_->CheckOrSwitchPGOStubs();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-30 13:03:02 +00:00
|
|
|
bool EcmaVM::IsEnablePGOProfiler() const
|
|
|
|
{
|
2023-03-08 07:05:35 +00:00
|
|
|
if (options_.IsWorker()) {
|
|
|
|
return PGOProfilerManager::GetInstance()->IsEnable();
|
|
|
|
}
|
2023-01-30 13:03:02 +00:00
|
|
|
return options_.GetEnableAsmInterpreter() && options_.IsEnablePGOProfiler();
|
|
|
|
}
|
|
|
|
|
2021-09-04 08:06:49 +00:00
|
|
|
bool EcmaVM::Initialize()
|
|
|
|
{
|
2022-05-11 08:51:55 +00:00
|
|
|
ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "EcmaVM::Initialize");
|
2023-03-08 07:05:35 +00:00
|
|
|
InitializePGOProfiler();
|
2022-03-23 07:43:38 +00:00
|
|
|
Taskpool::GetCurrentTaskpool()->Initialize();
|
2022-02-26 03:08:37 +00:00
|
|
|
#ifndef PANDA_TARGET_WINDOWS
|
2022-03-16 10:12:15 +00:00
|
|
|
RuntimeStubs::Initialize(thread_);
|
2022-02-26 03:08:37 +00:00
|
|
|
#endif
|
2021-09-04 08:06:49 +00:00
|
|
|
heap_ = new Heap(this);
|
2021-09-07 14:24:16 +00:00
|
|
|
heap_->Initialize();
|
2022-05-27 09:56:07 +00:00
|
|
|
gcStats_ = chunk_.New<GCStats>(heap_, options_.GetLongPauseTime());
|
2022-08-17 02:09:16 +00:00
|
|
|
factory_ = chunk_.New<ObjectFactory>(thread_, heap_);
|
2021-09-04 08:06:49 +00:00
|
|
|
if (UNLIKELY(factory_ == nullptr)) {
|
2022-07-06 06:12:54 +00:00
|
|
|
LOG_FULL(FATAL) << "alloc factory_ failed";
|
2021-09-04 08:06:49 +00:00
|
|
|
UNREACHABLE();
|
|
|
|
}
|
2022-10-18 03:55:00 +00:00
|
|
|
debuggerManager_ = chunk_.New<tooling::JsDebuggerManager>(this);
|
2023-05-16 01:27:59 +00:00
|
|
|
auto context = new EcmaContext(thread_);
|
|
|
|
thread_->PushContext(context);
|
2021-12-17 09:18:10 +00:00
|
|
|
[[maybe_unused]] EcmaHandleScope scope(thread_);
|
2023-05-16 01:27:59 +00:00
|
|
|
context->Initialize();
|
2022-08-16 13:05:02 +00:00
|
|
|
GenerateInternalNativeMethods();
|
2022-02-19 02:09:52 +00:00
|
|
|
thread_->SetGlobalObject(GetGlobalEnv()->GetGlobalObject());
|
2022-09-08 03:46:46 +00:00
|
|
|
quickFixManager_ = new QuickFixManager();
|
2022-05-12 02:23:24 +00:00
|
|
|
snapshotEnv_ = new SnapshotEnv(this);
|
2022-10-10 02:08:43 +00:00
|
|
|
if (!WIN_OR_MAC_OR_IOS_PLATFORM) {
|
2022-06-14 11:18:29 +00:00
|
|
|
snapshotEnv_->Initialize();
|
|
|
|
}
|
2022-06-08 06:43:48 +00:00
|
|
|
if (options_.GetEnableAsmInterpreter()) {
|
2023-05-23 01:34:51 +00:00
|
|
|
thread_->GetCurrentEcmaContext()->LoadStubFile();
|
2022-05-12 02:23:24 +00:00
|
|
|
}
|
2022-12-14 09:07:53 +00:00
|
|
|
|
2023-06-04 03:04:08 +00:00
|
|
|
callTimer_ = new FunctionCallTimer();
|
2022-12-14 09:07:53 +00:00
|
|
|
|
2022-07-27 06:09:34 +00:00
|
|
|
initialized_ = true;
|
2021-12-17 09:18:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-09-04 08:06:49 +00:00
|
|
|
EcmaVM::~EcmaVM()
|
|
|
|
{
|
2022-07-27 06:09:34 +00:00
|
|
|
initialized_ = false;
|
2023-01-29 03:40:13 +00:00
|
|
|
#if defined(ECMASCRIPT_SUPPORT_CPUPROFILER)
|
|
|
|
DFXJSNApi::StopCpuProfilerForFile(this);
|
|
|
|
#endif
|
2022-07-18 11:06:58 +00:00
|
|
|
heap_->WaitAllTasksFinished();
|
2022-11-18 03:58:51 +00:00
|
|
|
Taskpool::GetCurrentTaskpool()->Destroy(thread_->GetThreadId());
|
2021-09-04 08:06:49 +00:00
|
|
|
|
2023-05-15 08:48:14 +00:00
|
|
|
if (pgoProfiler_ != nullptr) {
|
|
|
|
PGOProfilerManager::GetInstance()->Destroy(pgoProfiler_);
|
|
|
|
pgoProfiler_ = nullptr;
|
|
|
|
}
|
|
|
|
|
2023-06-08 06:13:41 +00:00
|
|
|
#if ECMASCRIPT_ENABLE_FUNCTION_CALL_TIMER
|
|
|
|
DumpCallTimeInfo();
|
|
|
|
#endif
|
2023-05-25 13:43:05 +00:00
|
|
|
|
2021-09-04 08:06:49 +00:00
|
|
|
// clear c_address: c++ pointer delete
|
|
|
|
ClearBufferData();
|
2022-11-15 03:05:29 +00:00
|
|
|
if (!isBundlePack_) {
|
2023-03-15 08:25:25 +00:00
|
|
|
std::shared_ptr<JSPandaFile> jsPandaFile = JSPandaFileManager::GetInstance()->FindJSPandaFile(assetPath_);
|
2022-11-15 03:05:29 +00:00
|
|
|
if (jsPandaFile != nullptr) {
|
2023-03-15 08:25:25 +00:00
|
|
|
jsPandaFile->DeleteParsedConstpoolVM(this);
|
2022-11-15 03:05:29 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-04 08:06:49 +00:00
|
|
|
|
2021-09-07 14:24:16 +00:00
|
|
|
if (gcStats_ != nullptr) {
|
2022-04-30 12:02:19 +00:00
|
|
|
if (options_.EnableGCStatsPrint()) {
|
2023-04-23 01:52:14 +00:00
|
|
|
gcStats_->PrintStatisticResult();
|
2022-01-07 09:36:04 +00:00
|
|
|
}
|
2021-09-07 14:24:16 +00:00
|
|
|
chunk_.Delete(gcStats_);
|
|
|
|
gcStats_ = nullptr;
|
|
|
|
}
|
|
|
|
|
2021-09-04 08:06:49 +00:00
|
|
|
if (heap_ != nullptr) {
|
2021-09-07 14:24:16 +00:00
|
|
|
heap_->Destroy();
|
2021-09-04 08:06:49 +00:00
|
|
|
delete heap_;
|
|
|
|
heap_ = nullptr;
|
|
|
|
}
|
|
|
|
|
2022-04-22 07:19:24 +00:00
|
|
|
if (debuggerManager_ != nullptr) {
|
|
|
|
chunk_.Delete(debuggerManager_);
|
|
|
|
debuggerManager_ = nullptr;
|
2021-10-18 08:54:11 +00:00
|
|
|
}
|
|
|
|
|
2021-09-04 08:06:49 +00:00
|
|
|
if (factory_ != nullptr) {
|
|
|
|
chunk_.Delete(factory_);
|
|
|
|
factory_ = nullptr;
|
|
|
|
}
|
|
|
|
|
2022-09-08 03:46:46 +00:00
|
|
|
if (quickFixManager_ != nullptr) {
|
|
|
|
delete quickFixManager_;
|
|
|
|
quickFixManager_ = nullptr;
|
2022-08-27 03:52:15 +00:00
|
|
|
}
|
|
|
|
|
2022-05-17 06:52:04 +00:00
|
|
|
if (snapshotEnv_ != nullptr) {
|
|
|
|
delete snapshotEnv_;
|
|
|
|
snapshotEnv_ = nullptr;
|
|
|
|
}
|
|
|
|
|
2023-06-04 03:04:08 +00:00
|
|
|
if (callTimer_ != nullptr) {
|
|
|
|
delete callTimer_;
|
|
|
|
callTimer_ = nullptr;
|
|
|
|
}
|
|
|
|
|
2021-09-04 08:06:49 +00:00
|
|
|
if (thread_ != nullptr) {
|
|
|
|
delete thread_;
|
|
|
|
thread_ = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
JSHandle<GlobalEnv> EcmaVM::GetGlobalEnv() const
|
|
|
|
{
|
2023-05-16 01:27:59 +00:00
|
|
|
return thread_->GetCurrentEcmaContext()->GetGlobalEnv();
|
2021-09-04 08:06:49 +00:00
|
|
|
}
|
2023-05-18 11:13:51 +00:00
|
|
|
|
|
|
|
JSTaggedValue EcmaVM::FastCallAot(size_t actualNumArgs, JSTaggedType *args, const JSTaggedType *prevFp)
|
|
|
|
{
|
2023-06-08 06:13:41 +00:00
|
|
|
INTERPRETER_TRACE(thread_, ExecuteAot);
|
2023-05-18 11:13:51 +00:00
|
|
|
auto entry = thread_->GetRTInterface(kungfu::RuntimeStubCSigns::ID_OptimizedFastCallEntry);
|
2023-01-31 13:15:39 +00:00
|
|
|
// do not modify this log to INFO, this will call many times
|
2023-05-18 11:13:51 +00:00
|
|
|
LOG_ECMA(DEBUG) << "start to execute aot entry: " << (void*)entry;
|
|
|
|
auto res = reinterpret_cast<FastCallAotEntryType>(entry)(thread_->GetGlueAddr(),
|
|
|
|
actualNumArgs,
|
|
|
|
args,
|
|
|
|
reinterpret_cast<uintptr_t>(prevFp));
|
2022-10-18 08:16:20 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2023-01-29 03:40:13 +00:00
|
|
|
void EcmaVM::CheckStartCpuProfiler()
|
|
|
|
{
|
|
|
|
#if defined(ECMASCRIPT_SUPPORT_CPUPROFILER)
|
|
|
|
if (profiler_ == nullptr && !options_.IsWorker() &&
|
|
|
|
options_.EnableCpuProfiler() && options_.GetArkBundleName().compare(bundleName_) == 0) {
|
|
|
|
std::string fileName = options_.GetArkBundleName() + ".cpuprofile";
|
|
|
|
if (!builtins::BuiltinsArkTools::CreateFile(fileName)) {
|
|
|
|
LOG_ECMA(ERROR) << "createFile failed " << fileName;
|
|
|
|
} else {
|
2023-05-19 01:26:18 +00:00
|
|
|
DFXJSNApi::StartCpuProfilerForFile(this, fileName, 100); // 100:Sampling interval 100 microseconds
|
2023-01-29 03:40:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-01-27 07:54:49 +00:00
|
|
|
JSHandle<JSTaggedValue> EcmaVM::GetAndClearEcmaUncaughtException() const
|
|
|
|
{
|
|
|
|
JSHandle<JSTaggedValue> exceptionHandle = GetEcmaUncaughtException();
|
|
|
|
thread_->ClearException(); // clear for ohos app
|
|
|
|
return exceptionHandle;
|
|
|
|
}
|
|
|
|
|
2021-09-04 08:06:49 +00:00
|
|
|
JSHandle<JSTaggedValue> EcmaVM::GetEcmaUncaughtException() const
|
|
|
|
{
|
2022-08-22 06:44:19 +00:00
|
|
|
if (!thread_->HasPendingException()) {
|
2021-09-04 08:06:49 +00:00
|
|
|
return JSHandle<JSTaggedValue>();
|
|
|
|
}
|
|
|
|
JSHandle<JSTaggedValue> exceptionHandle(thread_, thread_->GetException());
|
|
|
|
return exceptionHandle;
|
|
|
|
}
|
|
|
|
|
2023-06-06 06:49:12 +00:00
|
|
|
void EcmaVM::PrintJSErrorInfo(const JSHandle<JSTaggedValue> &exceptionInfo) const
|
2021-09-04 08:06:49 +00:00
|
|
|
{
|
|
|
|
JSHandle<JSTaggedValue> nameKey = thread_->GlobalConstants()->GetHandledNameString();
|
|
|
|
JSHandle<EcmaString> name(JSObject::GetProperty(thread_, exceptionInfo, nameKey).GetValue());
|
|
|
|
JSHandle<JSTaggedValue> msgKey = thread_->GlobalConstants()->GetHandledMessageString();
|
|
|
|
JSHandle<EcmaString> msg(JSObject::GetProperty(thread_, exceptionInfo, msgKey).GetValue());
|
|
|
|
JSHandle<JSTaggedValue> stackKey = thread_->GlobalConstants()->GetHandledStackString();
|
|
|
|
JSHandle<EcmaString> stack(JSObject::GetProperty(thread_, exceptionInfo, stackKey).GetValue());
|
|
|
|
|
|
|
|
CString nameBuffer = ConvertToString(*name);
|
|
|
|
CString msgBuffer = ConvertToString(*msg);
|
|
|
|
CString stackBuffer = ConvertToString(*stack);
|
2022-09-22 13:37:23 +00:00
|
|
|
LOG_NO_TAG(ERROR) << nameBuffer << ": " << msgBuffer << "\n" << stackBuffer;
|
2021-09-04 08:06:49 +00:00
|
|
|
}
|
|
|
|
|
2022-07-30 08:29:50 +00:00
|
|
|
void EcmaVM::ProcessNativeDelete(const WeakRootVisitor &visitor)
|
2022-03-07 03:58:44 +00:00
|
|
|
{
|
|
|
|
auto iter = nativePointerList_.begin();
|
|
|
|
while (iter != nativePointerList_.end()) {
|
|
|
|
JSNativePointer *object = *iter;
|
2022-07-30 08:29:50 +00:00
|
|
|
auto fwd = visitor(reinterpret_cast<TaggedObject *>(object));
|
2022-03-07 03:58:44 +00:00
|
|
|
if (fwd == nullptr) {
|
|
|
|
object->Destroy();
|
|
|
|
iter = nativePointerList_.erase(iter);
|
|
|
|
} else {
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
}
|
2023-05-23 07:51:10 +00:00
|
|
|
thread_->GetCurrentEcmaContext()->ProcessNativeDelete(visitor);
|
2022-03-07 03:58:44 +00:00
|
|
|
}
|
2023-06-05 06:56:01 +00:00
|
|
|
|
2022-07-30 08:29:50 +00:00
|
|
|
void EcmaVM::ProcessReferences(const WeakRootVisitor &visitor)
|
2021-09-04 08:06:49 +00:00
|
|
|
{
|
2023-05-16 01:27:59 +00:00
|
|
|
if (thread_->GetCurrentEcmaContext()->GetRegExpParserCache() != nullptr) {
|
|
|
|
thread_->GetCurrentEcmaContext()->GetRegExpParserCache()->Clear();
|
2021-09-04 08:06:49 +00:00
|
|
|
}
|
2022-09-09 08:58:03 +00:00
|
|
|
heap_->ResetNativeBindingSize();
|
2021-09-04 08:06:49 +00:00
|
|
|
// array buffer
|
2022-10-15 10:47:04 +00:00
|
|
|
auto iter = nativePointerList_.begin();
|
|
|
|
while (iter != nativePointerList_.end()) {
|
2021-09-04 08:06:49 +00:00
|
|
|
JSNativePointer *object = *iter;
|
2022-07-30 08:29:50 +00:00
|
|
|
auto fwd = visitor(reinterpret_cast<TaggedObject *>(object));
|
2021-09-04 08:06:49 +00:00
|
|
|
if (fwd == nullptr) {
|
|
|
|
object->Destroy();
|
2022-03-04 07:23:09 +00:00
|
|
|
iter = nativePointerList_.erase(iter);
|
2022-03-07 03:58:44 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-09-21 03:30:58 +00:00
|
|
|
heap_->IncreaseNativeBindingSize(JSNativePointer::Cast(fwd));
|
2022-03-07 03:58:44 +00:00
|
|
|
if (fwd != reinterpret_cast<TaggedObject *>(object)) {
|
2021-09-04 08:06:49 +00:00
|
|
|
*iter = JSNativePointer::Cast(fwd);
|
|
|
|
}
|
2022-03-07 03:58:44 +00:00
|
|
|
++iter;
|
2021-09-04 08:06:49 +00:00
|
|
|
}
|
2023-05-23 07:51:10 +00:00
|
|
|
thread_->GetCurrentEcmaContext()->ProcessReferences(visitor);
|
2021-09-04 08:06:49 +00:00
|
|
|
}
|
|
|
|
|
2022-03-04 07:23:09 +00:00
|
|
|
void EcmaVM::PushToNativePointerList(JSNativePointer *array)
|
2021-09-04 08:06:49 +00:00
|
|
|
{
|
2022-03-04 07:23:09 +00:00
|
|
|
nativePointerList_.emplace_back(array);
|
2021-09-04 08:06:49 +00:00
|
|
|
}
|
|
|
|
|
2022-03-04 07:23:09 +00:00
|
|
|
void EcmaVM::RemoveFromNativePointerList(JSNativePointer *array)
|
2021-09-04 08:06:49 +00:00
|
|
|
{
|
2022-03-04 07:23:09 +00:00
|
|
|
auto iter = std::find(nativePointerList_.begin(), nativePointerList_.end(), array);
|
|
|
|
if (iter != nativePointerList_.end()) {
|
2022-05-07 09:21:10 +00:00
|
|
|
JSNativePointer *object = *iter;
|
|
|
|
object->Destroy();
|
2022-03-04 07:23:09 +00:00
|
|
|
nativePointerList_.erase(iter);
|
2021-09-04 08:06:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EcmaVM::ClearBufferData()
|
|
|
|
{
|
2022-03-04 07:23:09 +00:00
|
|
|
for (auto iter : nativePointerList_) {
|
2021-09-04 08:06:49 +00:00
|
|
|
iter->Destroy();
|
|
|
|
}
|
2022-03-04 07:23:09 +00:00
|
|
|
nativePointerList_.clear();
|
2023-05-23 07:51:10 +00:00
|
|
|
thread_->GetCurrentEcmaContext()->ClearBufferData();
|
2022-08-16 13:05:02 +00:00
|
|
|
internalNativeMethods_.clear();
|
2022-11-29 02:22:20 +00:00
|
|
|
workerList_.clear();
|
2021-09-04 08:06:49 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:52:14 +00:00
|
|
|
void EcmaVM::CollectGarbage(TriggerGCType gcType, GCReason reason) const
|
2021-09-04 08:06:49 +00:00
|
|
|
{
|
2023-04-23 01:52:14 +00:00
|
|
|
heap_->CollectGarbage(gcType, reason);
|
2021-09-04 08:06:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EcmaVM::StartHeapTracking(HeapTracker *tracker)
|
|
|
|
{
|
|
|
|
heap_->StartHeapTracking(tracker);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EcmaVM::StopHeapTracking()
|
|
|
|
{
|
|
|
|
heap_->StopHeapTracking();
|
|
|
|
}
|
|
|
|
|
2022-08-16 13:05:02 +00:00
|
|
|
void EcmaVM::Iterate(const RootVisitor &v, const RootRangeVisitor &rv)
|
2021-09-04 08:06:49 +00:00
|
|
|
{
|
2022-11-28 01:56:43 +00:00
|
|
|
rv(Root::ROOT_VM, ObjectSlot(ToUintPtr(&internalNativeMethods_.front())),
|
|
|
|
ObjectSlot(ToUintPtr(&internalNativeMethods_.back()) + JSTaggedValue::TaggedTypeSize()));
|
2022-10-10 02:08:43 +00:00
|
|
|
if (!WIN_OR_MAC_OR_IOS_PLATFORM) {
|
2022-06-07 08:44:17 +00:00
|
|
|
snapshotEnv_->Iterate(v);
|
|
|
|
}
|
2021-09-04 08:06:49 +00:00
|
|
|
}
|
|
|
|
|
2023-02-15 09:17:18 +00:00
|
|
|
#if defined(ECMASCRIPT_SUPPORT_HEAPPROFILER)
|
2022-08-02 08:07:48 +00:00
|
|
|
void EcmaVM::DeleteHeapProfile()
|
|
|
|
{
|
|
|
|
if (heapProfile_ == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const_cast<NativeAreaAllocator *>(GetNativeAreaAllocator())->Delete(heapProfile_);
|
|
|
|
heapProfile_ = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
HeapProfilerInterface *EcmaVM::GetOrNewHeapProfile()
|
|
|
|
{
|
|
|
|
if (heapProfile_ != nullptr) {
|
|
|
|
return heapProfile_;
|
|
|
|
}
|
|
|
|
heapProfile_ = const_cast<NativeAreaAllocator *>(GetNativeAreaAllocator())->New<HeapProfiler>(this);
|
|
|
|
ASSERT(heapProfile_ != nullptr);
|
|
|
|
return heapProfile_;
|
|
|
|
}
|
|
|
|
#endif
|
2022-08-16 13:05:02 +00:00
|
|
|
|
|
|
|
// NOLINTNEXTLINE(modernize-avoid-c-arrays)
|
|
|
|
void *EcmaVM::InternalMethodTable[] = {
|
|
|
|
reinterpret_cast<void *>(builtins::BuiltinsGlobal::CallJsBoundFunction),
|
|
|
|
reinterpret_cast<void *>(builtins::BuiltinsGlobal::CallJsProxy),
|
|
|
|
reinterpret_cast<void *>(builtins::BuiltinsObject::CreateDataPropertyOnObjectFunctions),
|
2023-03-03 02:59:00 +00:00
|
|
|
#ifdef ARK_SUPPORT_INTL
|
2022-08-16 13:05:02 +00:00
|
|
|
reinterpret_cast<void *>(builtins::BuiltinsCollator::AnonymousCollator),
|
|
|
|
reinterpret_cast<void *>(builtins::BuiltinsDateTimeFormat::AnonymousDateTimeFormat),
|
|
|
|
reinterpret_cast<void *>(builtins::BuiltinsNumberFormat::NumberFormatInternalFormatNumber),
|
2023-03-03 02:59:00 +00:00
|
|
|
#endif
|
2022-08-16 13:05:02 +00:00
|
|
|
reinterpret_cast<void *>(builtins::BuiltinsProxy::InvalidateProxyFunction),
|
|
|
|
reinterpret_cast<void *>(builtins::BuiltinsPromiseHandler::AsyncAwaitFulfilled),
|
|
|
|
reinterpret_cast<void *>(builtins::BuiltinsPromiseHandler::AsyncAwaitRejected),
|
|
|
|
reinterpret_cast<void *>(builtins::BuiltinsPromiseHandler::ResolveElementFunction),
|
|
|
|
reinterpret_cast<void *>(builtins::BuiltinsPromiseHandler::Resolve),
|
|
|
|
reinterpret_cast<void *>(builtins::BuiltinsPromiseHandler::Reject),
|
|
|
|
reinterpret_cast<void *>(builtins::BuiltinsPromiseHandler::Executor),
|
|
|
|
reinterpret_cast<void *>(builtins::BuiltinsPromiseHandler::AnyRejectElementFunction),
|
|
|
|
reinterpret_cast<void *>(builtins::BuiltinsPromiseHandler::AllSettledResolveElementFunction),
|
|
|
|
reinterpret_cast<void *>(builtins::BuiltinsPromiseHandler::AllSettledRejectElementFunction),
|
|
|
|
reinterpret_cast<void *>(builtins::BuiltinsPromiseHandler::ThenFinally),
|
|
|
|
reinterpret_cast<void *>(builtins::BuiltinsPromiseHandler::CatchFinally),
|
|
|
|
reinterpret_cast<void *>(builtins::BuiltinsPromiseHandler::valueThunkFunction),
|
|
|
|
reinterpret_cast<void *>(builtins::BuiltinsPromiseHandler::throwerFunction),
|
|
|
|
reinterpret_cast<void *>(JSAsyncGeneratorObject::ProcessorFulfilledFunc),
|
2023-01-12 10:49:18 +00:00
|
|
|
reinterpret_cast<void *>(JSAsyncGeneratorObject::ProcessorRejectedFunc),
|
|
|
|
reinterpret_cast<void *>(JSAsyncFromSyncIterator::AsyncFromSyncIterUnwarpFunction)
|
2022-08-16 13:05:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void EcmaVM::GenerateInternalNativeMethods()
|
|
|
|
{
|
|
|
|
size_t length = static_cast<size_t>(MethodIndex::METHOD_END);
|
|
|
|
for (size_t i = 0; i < length; i++) {
|
|
|
|
uint32_t numArgs = 2; // function object and this
|
2023-04-12 07:26:21 +00:00
|
|
|
auto method = factory_->NewMethod(nullptr, MemSpaceType::NON_MOVABLE);
|
2022-08-16 13:05:02 +00:00
|
|
|
method->SetNativePointer(InternalMethodTable[i]);
|
|
|
|
method->SetNativeBit(true);
|
|
|
|
method->SetNumArgsWithCallField(numArgs);
|
2022-09-08 15:22:35 +00:00
|
|
|
method->SetFunctionKind(FunctionKind::NORMAL_FUNCTION);
|
2022-08-16 13:05:02 +00:00
|
|
|
internalNativeMethods_.emplace_back(method.GetTaggedValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
JSTaggedValue EcmaVM::GetMethodByIndex(MethodIndex idx)
|
|
|
|
{
|
|
|
|
auto index = static_cast<uint8_t>(idx);
|
|
|
|
ASSERT(index < internalNativeMethods_.size());
|
|
|
|
return internalNativeMethods_[index];
|
|
|
|
}
|
2023-02-07 10:36:08 +00:00
|
|
|
|
|
|
|
void EcmaVM::TriggerConcurrentCallback(JSTaggedValue result, JSTaggedValue hint)
|
|
|
|
{
|
|
|
|
if (concurrentCallback_ == nullptr) {
|
2023-05-25 08:09:04 +00:00
|
|
|
LOG_ECMA(DEBUG) << "Only trigger concurrent callback in taskpool thread";
|
2023-02-07 10:36:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-07 01:52:05 +00:00
|
|
|
bool success = true;
|
2023-02-07 10:36:08 +00:00
|
|
|
if (result.IsJSPromise()) {
|
|
|
|
// Async concurrent will return Promise
|
|
|
|
auto promise = JSPromise::Cast(result.GetTaggedObject());
|
2023-03-07 01:52:05 +00:00
|
|
|
auto status = promise->GetPromiseState();
|
|
|
|
if (status == PromiseState::PENDING) {
|
|
|
|
result = JSHandle<JSTaggedValue>::Cast(factory_->GetJSError(
|
|
|
|
ErrorType::ERROR, "Can't return Promise in pending state")).GetTaggedValue();
|
|
|
|
} else {
|
|
|
|
result = promise->GetPromiseResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status != PromiseState::FULFILLED) {
|
|
|
|
success = false;
|
2023-02-07 10:36:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-25 08:09:04 +00:00
|
|
|
JSHandle<JSTaggedValue> functionValue(thread_, hint);
|
|
|
|
if (!functionValue->IsJSFunction()) {
|
|
|
|
LOG_ECMA(ERROR) << "TriggerConcurrentCallback hint is not function";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
JSHandle<JSFunction> functionInfo(functionValue);
|
|
|
|
JSTaggedValue extraInfoValue = functionInfo->GetFunctionExtraInfo();
|
|
|
|
if (!extraInfoValue.IsJSNativePointer()) {
|
|
|
|
LOG_ECMA(ERROR) << "FunctionExtraInfo is not JSNativePointer";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
JSHandle<JSNativePointer> extraInfo(thread_, extraInfoValue);
|
|
|
|
void *taskInfo = extraInfo->GetData();
|
|
|
|
|
2023-03-07 01:52:05 +00:00
|
|
|
concurrentCallback_(JSNApiHelper::ToLocal<JSValueRef>(JSHandle<JSTaggedValue>(thread_, result)), success,
|
2023-05-25 08:09:04 +00:00
|
|
|
taskInfo, concurrentData_);
|
2023-02-07 10:36:08 +00:00
|
|
|
}
|
2023-06-04 03:04:08 +00:00
|
|
|
|
2023-06-08 06:13:41 +00:00
|
|
|
void EcmaVM::PrintOptStat()
|
|
|
|
{
|
|
|
|
if (optCodeProfiler_ != nullptr) {
|
|
|
|
optCodeProfiler_->PrintAndReset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-04 03:04:08 +00:00
|
|
|
void EcmaVM::DumpCallTimeInfo()
|
|
|
|
{
|
2023-06-08 06:13:41 +00:00
|
|
|
if (callTimer_ != nullptr) {
|
|
|
|
callTimer_->PrintAllStats();
|
|
|
|
}
|
2023-06-04 03:04:08 +00:00
|
|
|
}
|
2022-08-17 02:09:16 +00:00
|
|
|
} // namespace panda::ecmascript
|