2013-08-03 00:58:41 +04:00
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
2017-04-30 22:18:48 +03:00
|
|
|
// Licensed under the MIT license.
|
2013-05-21 16:53:16 +04:00
|
|
|
|
|
|
|
#include "ClearScriptV8Native.h"
|
|
|
|
|
Version 5.4.1: Implemented a V8 debug agent to compensate for removed V8 API, V8Update now fetches V8 source and dependencies from Git repositories, fixed V8 assembly unloading and patched V8 to tolerate redundant initialization (Issue #60), added ScriptEngine.EnableAutoHostVariables, fixed by-reference arguments to VBScript functions (Issue #58), removed support for Visual Studio 2012 (V8 build now requires at least Visual Studio 2013), added explicit loading of primary interop assemblies to fix Issue #68, added host exception marshaling for V8, fixed V8ScriptEngine crash when script code calls the HostObject constructor, fixed host item caching for host variables, added non-generic overloads of newArr() and func() to HostFunctions, added ScriptEngine.Current, HostFunctions instances can now be exposed in multiple script engines, added a GetDynamicMemberNames() override to MetaScriptItem (Issue #64), fixed indexed property binding ambiguity for inherited interfaces (Issue #69), added tests for bug fixes and new APIs. Tested with V8 3.30.33.16.
2015-02-06 07:35:44 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// V8Platform
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
class V8Platform final: public v8::Platform
|
Version 5.4.1: Implemented a V8 debug agent to compensate for removed V8 API, V8Update now fetches V8 source and dependencies from Git repositories, fixed V8 assembly unloading and patched V8 to tolerate redundant initialization (Issue #60), added ScriptEngine.EnableAutoHostVariables, fixed by-reference arguments to VBScript functions (Issue #58), removed support for Visual Studio 2012 (V8 build now requires at least Visual Studio 2013), added explicit loading of primary interop assemblies to fix Issue #68, added host exception marshaling for V8, fixed V8ScriptEngine crash when script code calls the HostObject constructor, fixed host item caching for host variables, added non-generic overloads of newArr() and func() to HostFunctions, added ScriptEngine.Current, HostFunctions instances can now be exposed in multiple script engines, added a GetDynamicMemberNames() override to MetaScriptItem (Issue #64), fixed indexed property binding ambiguity for inherited interfaces (Issue #69), added tests for bug fixes and new APIs. Tested with V8 3.30.33.16.
2015-02-06 07:35:44 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2017-02-28 15:16:13 +03:00
|
|
|
static V8Platform& GetInstance();
|
2022-03-05 21:04:28 +03:00
|
|
|
void EnsureInitialized();
|
|
|
|
V8GlobalFlags GetGlobalFlags() const;
|
Version 5.4.1: Implemented a V8 debug agent to compensate for removed V8 API, V8Update now fetches V8 source and dependencies from Git repositories, fixed V8 assembly unloading and patched V8 to tolerate redundant initialization (Issue #60), added ScriptEngine.EnableAutoHostVariables, fixed by-reference arguments to VBScript functions (Issue #58), removed support for Visual Studio 2012 (V8 build now requires at least Visual Studio 2013), added explicit loading of primary interop assemblies to fix Issue #68, added host exception marshaling for V8, fixed V8ScriptEngine crash when script code calls the HostObject constructor, fixed host item caching for host variables, added non-generic overloads of newArr() and func() to HostFunctions, added ScriptEngine.Current, HostFunctions instances can now be exposed in multiple script engines, added a GetDynamicMemberNames() override to MetaScriptItem (Issue #64), fixed indexed property binding ambiguity for inherited interfaces (Issue #69), added tests for bug fixes and new APIs. Tested with V8 3.30.33.16.
2015-02-06 07:35:44 +03:00
|
|
|
|
2022-09-28 04:12:03 +03:00
|
|
|
virtual v8::PageAllocator* GetPageAllocator() override;
|
2018-08-20 20:45:15 +03:00
|
|
|
virtual int NumberOfWorkerThreads() override;
|
2024-03-25 03:48:28 +03:00
|
|
|
virtual std::shared_ptr<v8::TaskRunner> GetForegroundTaskRunner(v8::Isolate* pIsolate, v8::TaskPriority priority) override;
|
Version 5.4.1: Implemented a V8 debug agent to compensate for removed V8 API, V8Update now fetches V8 source and dependencies from Git repositories, fixed V8 assembly unloading and patched V8 to tolerate redundant initialization (Issue #60), added ScriptEngine.EnableAutoHostVariables, fixed by-reference arguments to VBScript functions (Issue #58), removed support for Visual Studio 2012 (V8 build now requires at least Visual Studio 2013), added explicit loading of primary interop assemblies to fix Issue #68, added host exception marshaling for V8, fixed V8ScriptEngine crash when script code calls the HostObject constructor, fixed host item caching for host variables, added non-generic overloads of newArr() and func() to HostFunctions, added ScriptEngine.Current, HostFunctions instances can now be exposed in multiple script engines, added a GetDynamicMemberNames() override to MetaScriptItem (Issue #64), fixed indexed property binding ambiguity for inherited interfaces (Issue #69), added tests for bug fixes and new APIs. Tested with V8 3.30.33.16.
2015-02-06 07:35:44 +03:00
|
|
|
virtual double MonotonicallyIncreasingTime() override;
|
2017-12-26 23:49:38 +03:00
|
|
|
virtual double CurrentClockTimeMillis() override;
|
2017-11-07 22:32:41 +03:00
|
|
|
virtual v8::TracingController* GetTracingController() override;
|
Version 5.4.1: Implemented a V8 debug agent to compensate for removed V8 API, V8Update now fetches V8 source and dependencies from Git repositories, fixed V8 assembly unloading and patched V8 to tolerate redundant initialization (Issue #60), added ScriptEngine.EnableAutoHostVariables, fixed by-reference arguments to VBScript functions (Issue #58), removed support for Visual Studio 2012 (V8 build now requires at least Visual Studio 2013), added explicit loading of primary interop assemblies to fix Issue #68, added host exception marshaling for V8, fixed V8ScriptEngine crash when script code calls the HostObject constructor, fixed host item caching for host variables, added non-generic overloads of newArr() and func() to HostFunctions, added ScriptEngine.Current, HostFunctions instances can now be exposed in multiple script engines, added a GetDynamicMemberNames() override to MetaScriptItem (Issue #64), fixed indexed property binding ambiguity for inherited interfaces (Issue #69), added tests for bug fixes and new APIs. Tested with V8 3.30.33.16.
2015-02-06 07:35:44 +03:00
|
|
|
|
2024-03-25 03:48:28 +03:00
|
|
|
protected:
|
|
|
|
|
|
|
|
virtual std::unique_ptr<v8::JobHandle> CreateJobImpl(v8::TaskPriority priority, std::unique_ptr<v8::JobTask> upJobTask, const v8::SourceLocation& location) override;
|
|
|
|
virtual void PostTaskOnWorkerThreadImpl(v8::TaskPriority priority, std::unique_ptr<v8::Task> upTask, const v8::SourceLocation& location) override;
|
|
|
|
virtual void PostDelayedTaskOnWorkerThreadImpl(v8::TaskPriority priority, std::unique_ptr<v8::Task> upTask, double delayInSeconds, const v8::SourceLocation& location) override;
|
|
|
|
|
Version 5.4.1: Implemented a V8 debug agent to compensate for removed V8 API, V8Update now fetches V8 source and dependencies from Git repositories, fixed V8 assembly unloading and patched V8 to tolerate redundant initialization (Issue #60), added ScriptEngine.EnableAutoHostVariables, fixed by-reference arguments to VBScript functions (Issue #58), removed support for Visual Studio 2012 (V8 build now requires at least Visual Studio 2013), added explicit loading of primary interop assemblies to fix Issue #68, added host exception marshaling for V8, fixed V8ScriptEngine crash when script code calls the HostObject constructor, fixed host item caching for host variables, added non-generic overloads of newArr() and func() to HostFunctions, added ScriptEngine.Current, HostFunctions instances can now be exposed in multiple script engines, added a GetDynamicMemberNames() override to MetaScriptItem (Issue #64), fixed indexed property binding ambiguity for inherited interfaces (Issue #69), added tests for bug fixes and new APIs. Tested with V8 3.30.33.16.
2015-02-06 07:35:44 +03:00
|
|
|
private:
|
|
|
|
|
|
|
|
V8Platform();
|
|
|
|
|
|
|
|
static V8Platform ms_Instance;
|
2022-11-12 18:57:46 +03:00
|
|
|
std::unique_ptr<v8::PageAllocator> m_upPageAllocator;
|
2022-03-05 21:04:28 +03:00
|
|
|
OnceFlag m_InitializationFlag;
|
|
|
|
V8GlobalFlags m_GlobalFlags;
|
2017-11-07 22:32:41 +03:00
|
|
|
v8::TracingController m_TracingController;
|
Version 5.4.1: Implemented a V8 debug agent to compensate for removed V8 API, V8Update now fetches V8 source and dependencies from Git repositories, fixed V8 assembly unloading and patched V8 to tolerate redundant initialization (Issue #60), added ScriptEngine.EnableAutoHostVariables, fixed by-reference arguments to VBScript functions (Issue #58), removed support for Visual Studio 2012 (V8 build now requires at least Visual Studio 2013), added explicit loading of primary interop assemblies to fix Issue #68, added host exception marshaling for V8, fixed V8ScriptEngine crash when script code calls the HostObject constructor, fixed host item caching for host variables, added non-generic overloads of newArr() and func() to HostFunctions, added ScriptEngine.Current, HostFunctions instances can now be exposed in multiple script engines, added a GetDynamicMemberNames() override to MetaScriptItem (Issue #64), fixed indexed property binding ambiguity for inherited interfaces (Issue #69), added tests for bug fixes and new APIs. Tested with V8 3.30.33.16.
2015-02-06 07:35:44 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2017-02-28 15:16:13 +03:00
|
|
|
V8Platform& V8Platform::GetInstance()
|
|
|
|
{
|
|
|
|
return ms_Instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2022-03-05 21:04:28 +03:00
|
|
|
V8GlobalFlags V8Platform::GetGlobalFlags() const
|
Version 5.4.1: Implemented a V8 debug agent to compensate for removed V8 API, V8Update now fetches V8 source and dependencies from Git repositories, fixed V8 assembly unloading and patched V8 to tolerate redundant initialization (Issue #60), added ScriptEngine.EnableAutoHostVariables, fixed by-reference arguments to VBScript functions (Issue #58), removed support for Visual Studio 2012 (V8 build now requires at least Visual Studio 2013), added explicit loading of primary interop assemblies to fix Issue #68, added host exception marshaling for V8, fixed V8ScriptEngine crash when script code calls the HostObject constructor, fixed host item caching for host variables, added non-generic overloads of newArr() and func() to HostFunctions, added ScriptEngine.Current, HostFunctions instances can now be exposed in multiple script engines, added a GetDynamicMemberNames() override to MetaScriptItem (Issue #64), fixed indexed property binding ambiguity for inherited interfaces (Issue #69), added tests for bug fixes and new APIs. Tested with V8 3.30.33.16.
2015-02-06 07:35:44 +03:00
|
|
|
{
|
2022-03-05 21:04:28 +03:00
|
|
|
return m_GlobalFlags;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void V8Platform::EnsureInitialized()
|
|
|
|
{
|
|
|
|
m_InitializationFlag.CallOnce([this]
|
Version 5.4.1: Implemented a V8 debug agent to compensate for removed V8 API, V8Update now fetches V8 source and dependencies from Git repositories, fixed V8 assembly unloading and patched V8 to tolerate redundant initialization (Issue #60), added ScriptEngine.EnableAutoHostVariables, fixed by-reference arguments to VBScript functions (Issue #58), removed support for Visual Studio 2012 (V8 build now requires at least Visual Studio 2013), added explicit loading of primary interop assemblies to fix Issue #68, added host exception marshaling for V8, fixed V8ScriptEngine crash when script code calls the HostObject constructor, fixed host item caching for host variables, added non-generic overloads of newArr() and func() to HostFunctions, added ScriptEngine.Current, HostFunctions instances can now be exposed in multiple script engines, added a GetDynamicMemberNames() override to MetaScriptItem (Issue #64), fixed indexed property binding ambiguity for inherited interfaces (Issue #69), added tests for bug fixes and new APIs. Tested with V8 3.30.33.16.
2015-02-06 07:35:44 +03:00
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
v8::V8::InitializePlatform(&ms_Instance);
|
2021-05-26 17:39:08 +03:00
|
|
|
|
2022-03-05 21:04:28 +03:00
|
|
|
m_GlobalFlags = V8_SPLIT_PROXY_MANAGED_INVOKE_NOTHROW(V8GlobalFlags, GetGlobalFlags);
|
2022-02-06 18:53:15 +03:00
|
|
|
std::vector<std::string> flagStrings;
|
2023-06-01 04:42:43 +03:00
|
|
|
flagStrings.push_back("--expose_gc");
|
2022-02-06 18:53:15 +03:00
|
|
|
|
|
|
|
#ifdef CLEARSCRIPT_TOP_LEVEL_AWAIT_CONTROL
|
|
|
|
|
|
|
|
if (!HasFlag(globalFlags, V8GlobalFlags::EnableTopLevelAwait))
|
|
|
|
{
|
|
|
|
flagStrings.push_back("--no_harmony_top_level_await");
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // CLEARSCRIPT_TOP_LEVEL_AWAIT_CONTROL
|
2022-09-15 15:04:51 +03:00
|
|
|
|
2022-03-05 21:04:28 +03:00
|
|
|
if (HasFlag(m_GlobalFlags, V8GlobalFlags::DisableJITCompilation))
|
2021-05-26 17:39:08 +03:00
|
|
|
{
|
2022-02-06 18:53:15 +03:00
|
|
|
flagStrings.push_back("--jitless");
|
|
|
|
}
|
|
|
|
|
2022-03-05 21:04:28 +03:00
|
|
|
if (HasFlag(m_GlobalFlags, V8GlobalFlags::DisableBackgroundWork))
|
|
|
|
{
|
|
|
|
flagStrings.push_back("--single_threaded");
|
|
|
|
}
|
|
|
|
|
2022-02-06 18:53:15 +03:00
|
|
|
if (!flagStrings.empty())
|
|
|
|
{
|
|
|
|
std::string flagsString(flagStrings[0]);
|
2022-07-07 15:47:33 +03:00
|
|
|
for (size_t index = 1; index < flagStrings.size(); index++)
|
2022-02-06 18:53:15 +03:00
|
|
|
{
|
|
|
|
flagsString += " ";
|
|
|
|
flagsString += flagStrings[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
v8::V8::SetFlagsFromString(flagsString.c_str(), flagsString.length());
|
2021-05-26 17:39:08 +03:00
|
|
|
}
|
2022-09-15 15:04:51 +03:00
|
|
|
|
|
|
|
ASSERT_EVAL(v8::V8::Initialize());
|
Version 5.4.1: Implemented a V8 debug agent to compensate for removed V8 API, V8Update now fetches V8 source and dependencies from Git repositories, fixed V8 assembly unloading and patched V8 to tolerate redundant initialization (Issue #60), added ScriptEngine.EnableAutoHostVariables, fixed by-reference arguments to VBScript functions (Issue #58), removed support for Visual Studio 2012 (V8 build now requires at least Visual Studio 2013), added explicit loading of primary interop assemblies to fix Issue #68, added host exception marshaling for V8, fixed V8ScriptEngine crash when script code calls the HostObject constructor, fixed host item caching for host variables, added non-generic overloads of newArr() and func() to HostFunctions, added ScriptEngine.Current, HostFunctions instances can now be exposed in multiple script engines, added a GetDynamicMemberNames() override to MetaScriptItem (Issue #64), fixed indexed property binding ambiguity for inherited interfaces (Issue #69), added tests for bug fixes and new APIs. Tested with V8 3.30.33.16.
2015-02-06 07:35:44 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2022-09-28 04:12:03 +03:00
|
|
|
v8::PageAllocator* V8Platform::GetPageAllocator()
|
|
|
|
{
|
2022-11-12 18:57:46 +03:00
|
|
|
return m_upPageAllocator.get();
|
2022-09-28 04:12:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2018-08-20 20:45:15 +03:00
|
|
|
int V8Platform::NumberOfWorkerThreads()
|
2016-09-13 22:34:07 +03:00
|
|
|
{
|
2018-08-20 20:45:15 +03:00
|
|
|
return static_cast<int>(HighResolutionClock::GetHardwareConcurrency());
|
2016-09-13 22:34:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2024-03-25 03:48:28 +03:00
|
|
|
std::shared_ptr<v8::TaskRunner> V8Platform::GetForegroundTaskRunner(v8::Isolate* pIsolate, v8::TaskPriority /*priority*/)
|
2018-08-20 20:45:15 +03:00
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
return V8IsolateImpl::GetInstanceFromIsolate(pIsolate)->GetForegroundTaskRunner();
|
2018-08-20 20:45:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2024-03-25 03:48:28 +03:00
|
|
|
std::unique_ptr<v8::JobHandle> V8Platform::CreateJobImpl(v8::TaskPriority priority, std::unique_ptr<v8::JobTask> upJobTask, const v8::SourceLocation& /*location*/)
|
|
|
|
{
|
|
|
|
return v8::platform::NewDefaultJobHandle(this, priority, std::move(upJobTask), NumberOfWorkerThreads());
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void V8Platform::PostTaskOnWorkerThreadImpl(v8::TaskPriority /*priority*/, std::unique_ptr<v8::Task> upTask, const v8::SourceLocation& /*location*/)
|
Version 5.4.1: Implemented a V8 debug agent to compensate for removed V8 API, V8Update now fetches V8 source and dependencies from Git repositories, fixed V8 assembly unloading and patched V8 to tolerate redundant initialization (Issue #60), added ScriptEngine.EnableAutoHostVariables, fixed by-reference arguments to VBScript functions (Issue #58), removed support for Visual Studio 2012 (V8 build now requires at least Visual Studio 2013), added explicit loading of primary interop assemblies to fix Issue #68, added host exception marshaling for V8, fixed V8ScriptEngine crash when script code calls the HostObject constructor, fixed host item caching for host variables, added non-generic overloads of newArr() and func() to HostFunctions, added ScriptEngine.Current, HostFunctions instances can now be exposed in multiple script engines, added a GetDynamicMemberNames() override to MetaScriptItem (Issue #64), fixed indexed property binding ambiguity for inherited interfaces (Issue #69), added tests for bug fixes and new APIs. Tested with V8 3.30.33.16.
2015-02-06 07:35:44 +03:00
|
|
|
{
|
2017-04-19 00:28:29 +03:00
|
|
|
auto pIsolate = v8::Isolate::GetCurrent();
|
|
|
|
if (pIsolate == nullptr)
|
2016-09-13 22:34:07 +03:00
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
upTask->Run();
|
2017-04-19 00:28:29 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
V8IsolateImpl::GetInstanceFromIsolate(pIsolate)->RunTaskAsync(std::move(upTask));
|
2017-04-19 00:28:29 +03:00
|
|
|
}
|
2015-12-07 14:57:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2024-03-25 03:48:28 +03:00
|
|
|
void V8Platform::PostDelayedTaskOnWorkerThreadImpl(v8::TaskPriority /*priority*/, std::unique_ptr<v8::Task> upTask, double delayInSeconds, const v8::SourceLocation& /*location*/)
|
2015-12-07 14:57:35 +03:00
|
|
|
{
|
2018-08-20 20:45:15 +03:00
|
|
|
auto pIsolate = v8::Isolate::GetCurrent();
|
|
|
|
if (pIsolate != nullptr)
|
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
V8IsolateImpl::GetInstanceFromIsolate(pIsolate)->RunTaskDelayed(std::move(upTask), delayInSeconds);
|
2018-08-20 20:45:15 +03:00
|
|
|
}
|
2015-12-07 14:57:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
Version 5.4.1: Implemented a V8 debug agent to compensate for removed V8 API, V8Update now fetches V8 source and dependencies from Git repositories, fixed V8 assembly unloading and patched V8 to tolerate redundant initialization (Issue #60), added ScriptEngine.EnableAutoHostVariables, fixed by-reference arguments to VBScript functions (Issue #58), removed support for Visual Studio 2012 (V8 build now requires at least Visual Studio 2013), added explicit loading of primary interop assemblies to fix Issue #68, added host exception marshaling for V8, fixed V8ScriptEngine crash when script code calls the HostObject constructor, fixed host item caching for host variables, added non-generic overloads of newArr() and func() to HostFunctions, added ScriptEngine.Current, HostFunctions instances can now be exposed in multiple script engines, added a GetDynamicMemberNames() override to MetaScriptItem (Issue #64), fixed indexed property binding ambiguity for inherited interfaces (Issue #69), added tests for bug fixes and new APIs. Tested with V8 3.30.33.16.
2015-02-06 07:35:44 +03:00
|
|
|
double V8Platform::MonotonicallyIncreasingTime()
|
|
|
|
{
|
|
|
|
return HighResolutionClock::GetRelativeSeconds();
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2017-12-26 23:49:38 +03:00
|
|
|
double V8Platform::CurrentClockTimeMillis()
|
|
|
|
{
|
2018-01-03 01:51:11 +03:00
|
|
|
return std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(std::chrono::system_clock::now().time_since_epoch()).count();
|
2017-12-26 23:49:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2017-11-07 22:32:41 +03:00
|
|
|
v8::TracingController* V8Platform::GetTracingController()
|
|
|
|
{
|
|
|
|
return &m_TracingController;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2022-03-05 21:04:28 +03:00
|
|
|
V8Platform::V8Platform():
|
2022-11-12 18:57:46 +03:00
|
|
|
m_upPageAllocator(v8::platform::NewDefaultPageAllocator()),
|
2022-03-05 21:04:28 +03:00
|
|
|
m_GlobalFlags(V8GlobalFlags::None)
|
Version 5.4.1: Implemented a V8 debug agent to compensate for removed V8 API, V8Update now fetches V8 source and dependencies from Git repositories, fixed V8 assembly unloading and patched V8 to tolerate redundant initialization (Issue #60), added ScriptEngine.EnableAutoHostVariables, fixed by-reference arguments to VBScript functions (Issue #58), removed support for Visual Studio 2012 (V8 build now requires at least Visual Studio 2013), added explicit loading of primary interop assemblies to fix Issue #68, added host exception marshaling for V8, fixed V8ScriptEngine crash when script code calls the HostObject constructor, fixed host item caching for host variables, added non-generic overloads of newArr() and func() to HostFunctions, added ScriptEngine.Current, HostFunctions instances can now be exposed in multiple script engines, added a GetDynamicMemberNames() override to MetaScriptItem (Issue #64), fixed indexed property binding ambiguity for inherited interfaces (Issue #69), added tests for bug fixes and new APIs. Tested with V8 3.30.33.16.
2015-02-06 07:35:44 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
V8Platform V8Platform::ms_Instance;
|
|
|
|
|
2018-08-20 20:45:15 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// V8ForegroundTaskRunner
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2019-05-02 06:05:22 +03:00
|
|
|
class V8ForegroundTaskRunner final: public v8::TaskRunner
|
2018-08-20 20:45:15 +03:00
|
|
|
{
|
|
|
|
PROHIBIT_COPY(V8ForegroundTaskRunner)
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2022-05-01 21:36:34 +03:00
|
|
|
V8ForegroundTaskRunner(V8IsolateImpl& isolateImpl);
|
2018-08-20 20:45:15 +03:00
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
virtual void PostTask(std::unique_ptr<v8::Task> upTask) override;
|
|
|
|
virtual void PostNonNestableTask(std::unique_ptr<v8::Task> upTask) override;
|
|
|
|
virtual void PostDelayedTask(std::unique_ptr<v8::Task> upTask, double delayInSeconds) override;
|
|
|
|
virtual void PostNonNestableDelayedTask(std::unique_ptr<v8::Task> upTask, double delayInSeconds) override;
|
|
|
|
virtual void PostIdleTask(std::unique_ptr<v8::IdleTask> upTask) override;
|
2018-08-20 20:45:15 +03:00
|
|
|
virtual bool IdleTasksEnabled() override;
|
2020-04-13 18:29:36 +03:00
|
|
|
virtual bool NonNestableTasksEnabled() const override;
|
|
|
|
virtual bool NonNestableDelayedTasksEnabled() const override;
|
2018-08-20 20:45:15 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2022-05-01 21:36:34 +03:00
|
|
|
V8IsolateImpl& m_IsolateImpl;
|
2018-08-20 20:45:15 +03:00
|
|
|
WeakRef<V8Isolate> m_wrIsolate;
|
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2022-05-01 21:36:34 +03:00
|
|
|
V8ForegroundTaskRunner::V8ForegroundTaskRunner(V8IsolateImpl& isolateImpl):
|
|
|
|
m_IsolateImpl(isolateImpl),
|
|
|
|
m_wrIsolate(isolateImpl.CreateWeakRef())
|
2018-08-20 20:45:15 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
void V8ForegroundTaskRunner::PostTask(std::unique_ptr<v8::Task> upTask)
|
2018-08-20 20:45:15 +03:00
|
|
|
{
|
|
|
|
auto spIsolate = m_wrIsolate.GetTarget();
|
|
|
|
if (spIsolate.IsEmpty())
|
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
upTask->Run();
|
2018-08-20 20:45:15 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-05-01 21:36:34 +03:00
|
|
|
m_IsolateImpl.RunTaskWithLockAsync(true /*allowNesting*/, std::move(upTask));
|
2020-04-13 18:29:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void V8ForegroundTaskRunner::PostNonNestableTask(std::unique_ptr<v8::Task> upTask)
|
|
|
|
{
|
|
|
|
auto spIsolate = m_wrIsolate.GetTarget();
|
|
|
|
if (!spIsolate.IsEmpty())
|
|
|
|
{
|
2022-05-01 21:36:34 +03:00
|
|
|
m_IsolateImpl.RunTaskWithLockAsync(false /*allowNesting*/, std::move(upTask));
|
2018-08-20 20:45:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
void V8ForegroundTaskRunner::PostDelayedTask(std::unique_ptr<v8::Task> upTask, double delayInSeconds)
|
2018-08-20 20:45:15 +03:00
|
|
|
{
|
|
|
|
auto spIsolate = m_wrIsolate.GetTarget();
|
|
|
|
if (!spIsolate.IsEmpty())
|
|
|
|
{
|
2022-05-01 21:36:34 +03:00
|
|
|
m_IsolateImpl.RunTaskWithLockDelayed(true /*allowNesting*/, std::move(upTask), delayInSeconds);
|
2018-08-20 20:45:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
void V8ForegroundTaskRunner::PostNonNestableDelayedTask(std::unique_ptr<v8::Task> upTask, double delayInSeconds)
|
|
|
|
{
|
|
|
|
auto spIsolate = m_wrIsolate.GetTarget();
|
|
|
|
if (!spIsolate.IsEmpty())
|
|
|
|
{
|
2022-05-01 21:36:34 +03:00
|
|
|
m_IsolateImpl.RunTaskWithLockDelayed(false /*allowNesting*/, std::move(upTask), delayInSeconds);
|
2020-04-13 18:29:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2021-03-04 16:22:48 +03:00
|
|
|
void V8ForegroundTaskRunner::PostIdleTask(std::unique_ptr<v8::IdleTask> /*upTask*/)
|
2018-08-20 20:45:15 +03:00
|
|
|
{
|
|
|
|
// unexpected call to unsupported method
|
|
|
|
std::terminate();
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
bool V8ForegroundTaskRunner::IdleTasksEnabled()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
bool V8ForegroundTaskRunner::NonNestableTasksEnabled() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
bool V8ForegroundTaskRunner::NonNestableDelayedTasksEnabled() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-17 01:06:51 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// V8ArrayBufferAllocator
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2019-05-02 06:05:22 +03:00
|
|
|
class V8ArrayBufferAllocator final: public v8::ArrayBuffer::Allocator
|
2014-05-17 01:06:51 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2022-05-01 21:36:34 +03:00
|
|
|
V8ArrayBufferAllocator(V8IsolateImpl& isolateImpl);
|
2014-05-17 01:06:51 +04:00
|
|
|
|
Version 5.4.1: Implemented a V8 debug agent to compensate for removed V8 API, V8Update now fetches V8 source and dependencies from Git repositories, fixed V8 assembly unloading and patched V8 to tolerate redundant initialization (Issue #60), added ScriptEngine.EnableAutoHostVariables, fixed by-reference arguments to VBScript functions (Issue #58), removed support for Visual Studio 2012 (V8 build now requires at least Visual Studio 2013), added explicit loading of primary interop assemblies to fix Issue #68, added host exception marshaling for V8, fixed V8ScriptEngine crash when script code calls the HostObject constructor, fixed host item caching for host variables, added non-generic overloads of newArr() and func() to HostFunctions, added ScriptEngine.Current, HostFunctions instances can now be exposed in multiple script engines, added a GetDynamicMemberNames() override to MetaScriptItem (Issue #64), fixed indexed property binding ambiguity for inherited interfaces (Issue #69), added tests for bug fixes and new APIs. Tested with V8 3.30.33.16.
2015-02-06 07:35:44 +03:00
|
|
|
virtual void* Allocate(size_t size) override;
|
|
|
|
virtual void* AllocateUninitialized(size_t size) override;
|
|
|
|
virtual void Free(void* pvData, size_t size) override;
|
2014-05-17 01:06:51 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2022-05-01 21:36:34 +03:00
|
|
|
V8IsolateImpl& m_IsolateImpl;
|
|
|
|
WeakRef<V8Isolate> m_wrIsolate;
|
2014-05-17 01:06:51 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2022-05-01 21:36:34 +03:00
|
|
|
V8ArrayBufferAllocator::V8ArrayBufferAllocator(V8IsolateImpl& isolateImpl):
|
|
|
|
m_IsolateImpl(isolateImpl),
|
|
|
|
m_wrIsolate(isolateImpl.CreateWeakRef())
|
2014-05-17 01:06:51 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void* V8ArrayBufferAllocator::Allocate(size_t size)
|
|
|
|
{
|
2022-05-01 21:36:34 +03:00
|
|
|
auto spIsolate = m_wrIsolate.GetTarget();
|
|
|
|
if (!spIsolate.IsEmpty())
|
2021-07-23 06:47:37 +03:00
|
|
|
{
|
2022-05-01 21:36:34 +03:00
|
|
|
return m_IsolateImpl.AllocateArrayBuffer(size);
|
2021-07-23 06:47:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
2014-05-17 01:06:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void* V8ArrayBufferAllocator::AllocateUninitialized(size_t size)
|
|
|
|
{
|
2022-05-01 21:36:34 +03:00
|
|
|
auto spIsolate = m_wrIsolate.GetTarget();
|
|
|
|
if (!spIsolate.IsEmpty())
|
2021-07-23 06:47:37 +03:00
|
|
|
{
|
2022-05-01 21:36:34 +03:00
|
|
|
return m_IsolateImpl.AllocateUninitializedArrayBuffer(size);
|
2021-07-23 06:47:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
2014-05-17 01:06:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2021-07-23 06:47:37 +03:00
|
|
|
void V8ArrayBufferAllocator::Free(void* pvData, size_t size)
|
2014-05-17 01:06:51 +04:00
|
|
|
{
|
2022-05-01 21:36:34 +03:00
|
|
|
auto spIsolate = m_wrIsolate.GetTarget();
|
|
|
|
if (!spIsolate.IsEmpty())
|
2021-07-23 06:47:37 +03:00
|
|
|
{
|
2022-05-01 21:36:34 +03:00
|
|
|
m_IsolateImpl.FreeArrayBuffer(pvData, size);
|
|
|
|
}
|
|
|
|
else if (pvData)
|
|
|
|
{
|
|
|
|
::free(pvData);
|
2021-07-23 06:47:37 +03:00
|
|
|
}
|
2014-05-17 01:06:51 +04:00
|
|
|
}
|
|
|
|
|
2021-01-22 17:44:18 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// V8OutputStream
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class V8OutputStream final: public v8::OutputStream
|
|
|
|
{
|
|
|
|
PROHIBIT_COPY(V8OutputStream)
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
explicit V8OutputStream(void* pvStream):
|
|
|
|
m_pvStream(pvStream)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual int GetChunkSize() override;
|
|
|
|
virtual WriteResult WriteAsciiChunk(char* pData, int size) override;
|
|
|
|
virtual void EndOfStream() override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
void* m_pvStream;
|
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
int V8OutputStream::GetChunkSize()
|
|
|
|
{
|
|
|
|
return 64 * 1024;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
V8OutputStream::WriteResult V8OutputStream::WriteAsciiChunk(char* pData, int size)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
V8_SPLIT_PROXY_MANAGED_INVOKE_VOID(WriteBytesToStream, m_pvStream, reinterpret_cast<uint8_t*>(pData), size);
|
|
|
|
return kContinue;
|
|
|
|
}
|
|
|
|
catch (const HostException& exception)
|
|
|
|
{
|
|
|
|
V8_SPLIT_PROXY_MANAGED_INVOKE_VOID(ScheduleForwardingException, exception.GetException());
|
|
|
|
return kAbort;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void V8OutputStream::EndOfStream()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-05-21 16:53:16 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// V8IsolateImpl implementation
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2015-08-16 19:42:51 +03:00
|
|
|
#define BEGIN_ISOLATE_NATIVE_SCOPE \
|
|
|
|
{ \
|
2020-10-17 07:26:01 +03:00
|
|
|
DISABLE_WARNING(4456) /* declaration hides previous local declaration */ \
|
2020-04-13 18:29:36 +03:00
|
|
|
NativeScope t_IsolateNativeScope(*this); \
|
2020-10-17 07:26:01 +03:00
|
|
|
DEFAULT_WARNING(4456)
|
2015-08-16 19:42:51 +03:00
|
|
|
|
|
|
|
#define END_ISOLATE_NATIVE_SCOPE \
|
|
|
|
IGNORE_UNUSED(t_IsolateNativeScope); \
|
|
|
|
}
|
|
|
|
|
2013-05-21 16:53:16 +04:00
|
|
|
#define BEGIN_ISOLATE_SCOPE \
|
2013-11-28 20:15:47 +04:00
|
|
|
{ \
|
2020-10-17 07:26:01 +03:00
|
|
|
DISABLE_WARNING(4456) /* declaration hides previous local declaration */ \
|
2020-04-13 18:29:36 +03:00
|
|
|
Scope t_IsolateScope(*this); \
|
2020-10-17 07:26:01 +03:00
|
|
|
DEFAULT_WARNING(4456)
|
2013-05-21 16:53:16 +04:00
|
|
|
|
|
|
|
#define END_ISOLATE_SCOPE \
|
2013-11-28 20:15:47 +04:00
|
|
|
IGNORE_UNUSED(t_IsolateScope); \
|
|
|
|
}
|
2013-05-21 16:53:16 +04:00
|
|
|
|
2022-03-05 21:04:28 +03:00
|
|
|
#define BEGIN_PROMISE_HOOK_SCOPE \
|
|
|
|
{ \
|
|
|
|
DISABLE_WARNING(4456) /* declaration hides previous local declaration */ \
|
|
|
|
PromiseHookScope t_PromiseHookScope(*this); \
|
|
|
|
DEFAULT_WARNING(4456)
|
|
|
|
|
|
|
|
#define END_PROMISE_HOOK_SCOPE \
|
|
|
|
IGNORE_UNUSED(t_PromiseHookScope); \
|
|
|
|
}
|
|
|
|
|
2013-05-21 16:53:16 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2019-07-31 20:03:53 +03:00
|
|
|
static std::atomic<size_t> s_InstanceCount(0);
|
2017-11-07 22:32:41 +03:00
|
|
|
static const int s_ContextGroupId = 1;
|
2013-11-28 20:15:47 +04:00
|
|
|
static const size_t s_StackBreathingRoom = static_cast<size_t>(16 * 1024);
|
|
|
|
static size_t* const s_pMinStackLimit = reinterpret_cast<size_t*>(sizeof(size_t));
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2019-05-02 06:05:22 +03:00
|
|
|
V8IsolateImpl::V8IsolateImpl(const StdString& name, const v8::ResourceConstraints* pConstraints, const Options& options):
|
2014-06-01 15:58:05 +04:00
|
|
|
m_Name(name),
|
2020-04-13 18:29:36 +03:00
|
|
|
m_CallWithLockLevel(0),
|
2013-05-21 16:53:16 +04:00
|
|
|
m_DebuggingEnabled(false),
|
2021-07-23 06:47:37 +03:00
|
|
|
m_MaxArrayBufferAllocation(options.MaxArrayBufferAllocation),
|
|
|
|
m_ArrayBufferAllocation(0),
|
2014-10-22 17:05:01 +04:00
|
|
|
m_MaxHeapSize(0),
|
2020-10-17 07:26:01 +03:00
|
|
|
m_HeapSizeSampleInterval(0.0),
|
2014-10-22 17:05:01 +04:00
|
|
|
m_HeapWatchLevel(0),
|
2021-03-04 16:22:48 +03:00
|
|
|
m_HeapExpansionMultiplier(options.HeapExpansionMultiplier),
|
2013-11-28 20:15:47 +04:00
|
|
|
m_MaxStackUsage(0),
|
2022-01-11 18:42:57 +03:00
|
|
|
m_EnableInterruptPropagation(false),
|
2022-05-01 21:36:34 +03:00
|
|
|
m_DisableHeapSizeViolationInterrupt(false),
|
2019-05-02 06:05:22 +03:00
|
|
|
m_CpuProfileSampleInterval(1000U),
|
2014-10-22 17:05:01 +04:00
|
|
|
m_StackWatchLevel(0),
|
2013-11-28 20:15:47 +04:00
|
|
|
m_pStackLimit(nullptr),
|
2024-03-25 03:48:28 +03:00
|
|
|
m_IsExecutionTerminating(false),
|
2016-05-29 14:03:05 +03:00
|
|
|
m_pExecutionScope(nullptr),
|
2019-07-31 20:03:53 +03:00
|
|
|
m_pDocumentInfo(nullptr),
|
2015-08-16 19:42:51 +03:00
|
|
|
m_IsOutOfMemory(false),
|
2017-04-19 00:28:29 +03:00
|
|
|
m_Released(false)
|
2013-05-21 16:53:16 +04:00
|
|
|
{
|
2022-03-05 21:04:28 +03:00
|
|
|
V8Platform::GetInstance().EnsureInitialized();
|
2014-05-17 01:06:51 +04:00
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
m_upIsolate.reset(v8::Isolate::Allocate());
|
|
|
|
m_upIsolate->SetData(0, this);
|
Version 5.4.1: Implemented a V8 debug agent to compensate for removed V8 API, V8Update now fetches V8 source and dependencies from Git repositories, fixed V8 assembly unloading and patched V8 to tolerate redundant initialization (Issue #60), added ScriptEngine.EnableAutoHostVariables, fixed by-reference arguments to VBScript functions (Issue #58), removed support for Visual Studio 2012 (V8 build now requires at least Visual Studio 2013), added explicit loading of primary interop assemblies to fix Issue #68, added host exception marshaling for V8, fixed V8ScriptEngine crash when script code calls the HostObject constructor, fixed host item caching for host variables, added non-generic overloads of newArr() and func() to HostFunctions, added ScriptEngine.Current, HostFunctions instances can now be exposed in multiple script engines, added a GetDynamicMemberNames() override to MetaScriptItem (Issue #64), fixed indexed property binding ambiguity for inherited interfaces (Issue #69), added tests for bug fixes and new APIs. Tested with V8 3.30.33.16.
2015-02-06 07:35:44 +03:00
|
|
|
|
2013-05-21 16:53:16 +04:00
|
|
|
BEGIN_ADDREF_SCOPE
|
|
|
|
|
2022-05-01 21:36:34 +03:00
|
|
|
v8::Isolate::CreateParams params;
|
|
|
|
params.array_buffer_allocator_shared = std::make_shared<V8ArrayBufferAllocator>(*this);
|
|
|
|
if (pConstraints != nullptr)
|
|
|
|
{
|
|
|
|
params.constraints.set_max_young_generation_size_in_bytes(pConstraints->max_young_generation_size_in_bytes());
|
|
|
|
params.constraints.set_max_old_generation_size_in_bytes(pConstraints->max_old_generation_size_in_bytes());
|
|
|
|
}
|
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
v8::Isolate::Initialize(m_upIsolate.get(), params);
|
2014-10-22 17:05:01 +04:00
|
|
|
|
2021-03-04 16:22:48 +03:00
|
|
|
m_upIsolate->AddNearHeapLimitCallback(HeapExpansionCallback, this);
|
2020-04-13 18:29:36 +03:00
|
|
|
m_upIsolate->AddBeforeCallEnteredCallback(OnBeforeCallEntered);
|
2013-05-21 16:53:16 +04:00
|
|
|
|
2019-12-17 19:50:07 +03:00
|
|
|
BEGIN_ISOLATE_SCOPE
|
2013-05-21 16:53:16 +04:00
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
m_upIsolate->SetCaptureStackTraceForUncaughtExceptions(true, 64, v8::StackTrace::kDetailed);
|
2019-12-17 19:50:07 +03:00
|
|
|
|
|
|
|
m_hHostObjectHolderKey = CreatePersistent(CreatePrivate());
|
|
|
|
|
2022-09-15 15:04:51 +03:00
|
|
|
if (HasFlag(options.Flags, Flags::EnableDebugging))
|
2019-12-17 19:50:07 +03:00
|
|
|
{
|
2022-09-15 15:04:51 +03:00
|
|
|
EnableDebugging(options.DebugPort, HasFlag(options.Flags, Flags::EnableRemoteDebugging));
|
2019-12-17 19:50:07 +03:00
|
|
|
}
|
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
m_upIsolate->SetHostInitializeImportMetaObjectCallback(ImportMetaInitializeCallback);
|
2022-09-15 15:04:51 +03:00
|
|
|
if (HasFlag(options.Flags, Flags::EnableDynamicModuleImports))
|
2019-12-17 19:50:07 +03:00
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
m_upIsolate->SetHostImportModuleDynamicallyCallback(ModuleImportCallback);
|
2019-12-17 19:50:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
END_ISOLATE_SCOPE
|
2019-07-31 20:03:53 +03:00
|
|
|
|
2013-05-21 16:53:16 +04:00
|
|
|
END_ADDREF_SCOPE
|
2015-08-16 19:42:51 +03:00
|
|
|
|
|
|
|
++s_InstanceCount;
|
2013-05-21 16:53:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2018-11-19 22:49:38 +03:00
|
|
|
V8IsolateImpl* V8IsolateImpl::GetInstanceFromIsolate(v8::Isolate* pIsolate)
|
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
_ASSERTE(pIsolate);
|
2019-07-31 20:03:53 +03:00
|
|
|
return static_cast<V8IsolateImpl*>(pIsolate->GetData(0));
|
2018-11-19 22:49:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2015-08-16 19:42:51 +03:00
|
|
|
size_t V8IsolateImpl::GetInstanceCount()
|
2013-05-21 16:53:16 +04:00
|
|
|
{
|
2015-08-16 19:42:51 +03:00
|
|
|
return s_InstanceCount;
|
|
|
|
}
|
2013-05-21 16:53:16 +04:00
|
|
|
|
2015-08-16 19:42:51 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2018-03-30 17:32:55 +03:00
|
|
|
void V8IsolateImpl::AddContext(V8ContextImpl* pContextImpl, const V8Context::Options& options)
|
2015-08-16 19:42:51 +03:00
|
|
|
{
|
|
|
|
_ASSERTE(IsCurrent() && IsLocked());
|
2017-11-07 22:32:41 +03:00
|
|
|
|
2022-09-15 15:04:51 +03:00
|
|
|
if (!HasFlag(options.Flags, V8Context::Flags::EnableDebugging))
|
2013-05-21 16:53:16 +04:00
|
|
|
{
|
2019-05-02 06:05:22 +03:00
|
|
|
m_ContextEntries.emplace_back(pContextImpl);
|
2013-05-21 16:53:16 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-05-02 06:05:22 +03:00
|
|
|
m_ContextEntries.emplace_front(pContextImpl);
|
2022-09-15 15:04:51 +03:00
|
|
|
EnableDebugging(options.DebugPort, HasFlag(options.Flags, V8Context::Flags::EnableRemoteDebugging));
|
2017-11-07 22:32:41 +03:00
|
|
|
}
|
|
|
|
|
2022-09-15 15:04:51 +03:00
|
|
|
if (HasFlag(options.Flags, V8Context::Flags::EnableDynamicModuleImports))
|
2019-07-31 20:03:53 +03:00
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
m_upIsolate->SetHostImportModuleDynamicallyCallback(ModuleImportCallback);
|
2019-07-31 20:03:53 +03:00
|
|
|
}
|
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
if (m_upInspector)
|
2017-11-07 22:32:41 +03:00
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
m_upInspector->contextCreated(v8_inspector::V8ContextInfo(pContextImpl->GetContext(), s_ContextGroupId, pContextImpl->GetName().GetStringView()));
|
2013-05-21 16:53:16 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void V8IsolateImpl::RemoveContext(V8ContextImpl* pContextImpl)
|
|
|
|
{
|
2015-08-16 19:42:51 +03:00
|
|
|
_ASSERTE(IsCurrent() && IsLocked());
|
2017-11-07 22:32:41 +03:00
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
if (m_upInspector)
|
2017-11-07 22:32:41 +03:00
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
m_upInspector->contextDestroyed(pContextImpl->GetContext());
|
2017-11-07 22:32:41 +03:00
|
|
|
}
|
|
|
|
|
2019-05-02 06:05:22 +03:00
|
|
|
m_ContextEntries.remove_if([pContextImpl] (const ContextEntry& contextEntry)
|
|
|
|
{
|
|
|
|
return contextEntry.pContextImpl == pContextImpl;
|
|
|
|
});
|
2013-05-21 16:53:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2019-07-31 20:03:53 +03:00
|
|
|
V8ContextImpl* V8IsolateImpl::FindContext(v8::Local<v8::Context> hContext)
|
|
|
|
{
|
|
|
|
_ASSERTE(IsCurrent() && IsLocked());
|
|
|
|
|
|
|
|
for (const auto& entry : m_ContextEntries)
|
|
|
|
{
|
|
|
|
if (entry.pContextImpl->GetContext() == hContext)
|
|
|
|
{
|
|
|
|
return entry.pContextImpl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2017-11-07 22:32:41 +03:00
|
|
|
void V8IsolateImpl::EnableDebugging(int port, bool remote)
|
2013-05-21 16:53:16 +04:00
|
|
|
{
|
2015-08-16 19:42:51 +03:00
|
|
|
_ASSERTE(IsCurrent() && IsLocked());
|
2017-11-07 22:32:41 +03:00
|
|
|
|
2013-05-21 16:53:16 +04:00
|
|
|
if (!m_DebuggingEnabled)
|
|
|
|
{
|
2017-11-07 22:32:41 +03:00
|
|
|
const char* pVersion = v8::V8::GetVersion();
|
|
|
|
StdString version(v8_inspector::StringView(reinterpret_cast<const uint8_t*>(pVersion), strlen(pVersion)));
|
|
|
|
|
|
|
|
if (port < 1)
|
2013-05-21 16:53:16 +04:00
|
|
|
{
|
2017-11-07 22:32:41 +03:00
|
|
|
port = 9222;
|
2013-05-21 16:53:16 +04:00
|
|
|
}
|
|
|
|
|
2014-10-22 17:05:01 +04:00
|
|
|
auto wrIsolate = CreateWeakRef();
|
2020-10-17 07:26:01 +03:00
|
|
|
m_pvDebugAgent = HostObjectUtil::GetInstance().CreateDebugAgent(m_Name, version, port, remote, [this, wrIsolate] (IHostObjectUtil::DebugDirective directive, const StdString* pCommand)
|
2013-05-21 16:53:16 +04:00
|
|
|
{
|
Version 5.4.1: Implemented a V8 debug agent to compensate for removed V8 API, V8Update now fetches V8 source and dependencies from Git repositories, fixed V8 assembly unloading and patched V8 to tolerate redundant initialization (Issue #60), added ScriptEngine.EnableAutoHostVariables, fixed by-reference arguments to VBScript functions (Issue #58), removed support for Visual Studio 2012 (V8 build now requires at least Visual Studio 2013), added explicit loading of primary interop assemblies to fix Issue #68, added host exception marshaling for V8, fixed V8ScriptEngine crash when script code calls the HostObject constructor, fixed host item caching for host variables, added non-generic overloads of newArr() and func() to HostFunctions, added ScriptEngine.Current, HostFunctions instances can now be exposed in multiple script engines, added a GetDynamicMemberNames() override to MetaScriptItem (Issue #64), fixed indexed property binding ambiguity for inherited interfaces (Issue #69), added tests for bug fixes and new APIs. Tested with V8 3.30.33.16.
2015-02-06 07:35:44 +03:00
|
|
|
auto spIsolate = wrIsolate.GetTarget();
|
|
|
|
if (!spIsolate.IsEmpty())
|
2013-05-21 16:53:16 +04:00
|
|
|
{
|
2020-10-17 07:26:01 +03:00
|
|
|
if (directive == IHostObjectUtil::DebugDirective::ConnectClient)
|
2017-11-07 22:32:41 +03:00
|
|
|
{
|
|
|
|
ConnectDebugClient();
|
|
|
|
}
|
2020-10-17 07:26:01 +03:00
|
|
|
else if ((directive == IHostObjectUtil::DebugDirective::SendCommand) && pCommand)
|
2015-05-09 15:00:44 +03:00
|
|
|
{
|
2015-12-07 14:57:35 +03:00
|
|
|
SendDebugCommand(*pCommand);
|
2015-05-09 15:00:44 +03:00
|
|
|
}
|
2020-10-17 07:26:01 +03:00
|
|
|
else if (directive == IHostObjectUtil::DebugDirective::DisconnectClient)
|
2015-05-09 15:00:44 +03:00
|
|
|
{
|
2017-11-07 22:32:41 +03:00
|
|
|
DisconnectDebugClient();
|
2015-05-09 15:00:44 +03:00
|
|
|
}
|
Version 5.4.1: Implemented a V8 debug agent to compensate for removed V8 API, V8Update now fetches V8 source and dependencies from Git repositories, fixed V8 assembly unloading and patched V8 to tolerate redundant initialization (Issue #60), added ScriptEngine.EnableAutoHostVariables, fixed by-reference arguments to VBScript functions (Issue #58), removed support for Visual Studio 2012 (V8 build now requires at least Visual Studio 2013), added explicit loading of primary interop assemblies to fix Issue #68, added host exception marshaling for V8, fixed V8ScriptEngine crash when script code calls the HostObject constructor, fixed host item caching for host variables, added non-generic overloads of newArr() and func() to HostFunctions, added ScriptEngine.Current, HostFunctions instances can now be exposed in multiple script engines, added a GetDynamicMemberNames() override to MetaScriptItem (Issue #64), fixed indexed property binding ambiguity for inherited interfaces (Issue #69), added tests for bug fixes and new APIs. Tested with V8 3.30.33.16.
2015-02-06 07:35:44 +03:00
|
|
|
}
|
2013-05-21 16:53:16 +04:00
|
|
|
});
|
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
m_upInspector = v8_inspector::V8Inspector::create(m_upIsolate.get(), this);
|
2013-05-21 16:53:16 +04:00
|
|
|
|
|
|
|
m_DebuggingEnabled = true;
|
2017-11-07 22:32:41 +03:00
|
|
|
m_DebugPort = port;
|
2013-05-21 16:53:16 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void V8IsolateImpl::DisableDebugging()
|
|
|
|
{
|
2015-08-16 19:42:51 +03:00
|
|
|
_ASSERTE(IsCurrent() && IsLocked());
|
2017-11-07 22:32:41 +03:00
|
|
|
|
2013-05-21 16:53:16 +04:00
|
|
|
if (m_DebuggingEnabled)
|
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
m_upInspectorSession.reset();
|
|
|
|
m_upInspector.reset();
|
2013-05-21 16:53:16 +04:00
|
|
|
|
2020-10-17 07:26:01 +03:00
|
|
|
HostObjectUtil::GetInstance().DestroyDebugAgent(m_pvDebugAgent);
|
2013-05-21 16:53:16 +04:00
|
|
|
m_DebuggingEnabled = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-10-22 17:05:01 +04:00
|
|
|
size_t V8IsolateImpl::GetMaxHeapSize()
|
|
|
|
{
|
|
|
|
return m_MaxHeapSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void V8IsolateImpl::SetMaxHeapSize(size_t value)
|
|
|
|
{
|
|
|
|
m_MaxHeapSize = value;
|
|
|
|
m_IsOutOfMemory = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
double V8IsolateImpl::GetHeapSizeSampleInterval()
|
|
|
|
{
|
|
|
|
return m_HeapSizeSampleInterval;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void V8IsolateImpl::SetHeapSizeSampleInterval(double value)
|
|
|
|
{
|
|
|
|
m_HeapSizeSampleInterval = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2013-11-28 20:15:47 +04:00
|
|
|
size_t V8IsolateImpl::GetMaxStackUsage()
|
|
|
|
{
|
|
|
|
return m_MaxStackUsage;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void V8IsolateImpl::SetMaxStackUsage(size_t value)
|
|
|
|
{
|
|
|
|
m_MaxStackUsage = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2017-12-26 23:49:38 +03:00
|
|
|
void V8IsolateImpl::AwaitDebuggerAndPause()
|
|
|
|
{
|
|
|
|
BEGIN_ISOLATE_SCOPE
|
|
|
|
|
2022-12-20 05:50:58 +03:00
|
|
|
if (m_DebuggingEnabled && !m_upInspectorSession)
|
2017-12-26 23:49:38 +03:00
|
|
|
{
|
2022-12-20 05:50:58 +03:00
|
|
|
auto exitReason = RunMessageLoop(RunMessageLoopReason::AwaitingDebugger);
|
|
|
|
switch (exitReason)
|
2017-12-26 23:49:38 +03:00
|
|
|
{
|
2022-12-20 05:50:58 +03:00
|
|
|
case ExitMessageLoopReason::TerminatedExecution:
|
|
|
|
throw V8Exception(V8Exception::Type::Interrupt, m_Name, StdString(SL("Script execution interrupted by host while awaiting debugger connection")), false);
|
2021-11-22 18:45:53 +03:00
|
|
|
|
2022-12-20 05:50:58 +03:00
|
|
|
case ExitMessageLoopReason::CanceledAwaitDebugger:
|
|
|
|
return;
|
2021-11-22 18:45:53 +03:00
|
|
|
|
2022-12-20 05:50:58 +03:00
|
|
|
default:
|
|
|
|
_ASSERTE(exitReason == ExitMessageLoopReason::ResumedExecution);
|
2017-12-26 23:49:38 +03:00
|
|
|
}
|
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
_ASSERTE(m_upInspectorSession);
|
|
|
|
if (m_upInspectorSession)
|
2017-12-26 23:49:38 +03:00
|
|
|
{
|
2020-10-17 07:26:01 +03:00
|
|
|
StdString breakReason(SL("Break on debugger connection"));
|
2020-04-13 18:29:36 +03:00
|
|
|
m_upInspectorSession->schedulePauseOnNextStatement(breakReason.GetStringView(), breakReason.GetStringView());
|
2017-12-26 23:49:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
END_ISOLATE_SCOPE
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2021-11-22 18:45:53 +03:00
|
|
|
void V8IsolateImpl::CancelAwaitDebugger()
|
|
|
|
{
|
|
|
|
BEGIN_MUTEX_SCOPE(m_DataMutex)
|
|
|
|
|
|
|
|
if (m_optRunMessageLoopReason == RunMessageLoopReason::AwaitingDebugger)
|
|
|
|
{
|
|
|
|
m_optExitMessageLoopReason = ExitMessageLoopReason::CanceledAwaitDebugger;
|
|
|
|
m_CallWithLockQueueChanged.notify_one();
|
|
|
|
}
|
|
|
|
|
|
|
|
END_MUTEX_SCOPE
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2019-07-31 20:03:53 +03:00
|
|
|
V8ScriptHolder* V8IsolateImpl::Compile(const V8DocumentInfo& documentInfo, StdString&& code)
|
2013-05-21 16:53:16 +04:00
|
|
|
{
|
|
|
|
BEGIN_ISOLATE_SCOPE
|
|
|
|
|
2023-08-19 17:29:45 +03:00
|
|
|
SharedPtr<V8ContextImpl> spContextImpl(!m_ContextEntries.empty() ? m_ContextEntries.front().pContextImpl : new V8ContextImpl(this, m_Name));
|
2019-07-31 20:03:53 +03:00
|
|
|
return spContextImpl->Compile(documentInfo, std::move(code));
|
2013-05-21 16:53:16 +04:00
|
|
|
|
|
|
|
END_ISOLATE_SCOPE
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2023-08-19 17:29:45 +03:00
|
|
|
V8ScriptHolder* V8IsolateImpl::Compile(const V8DocumentInfo& documentInfo, StdString&& code, V8CacheKind cacheKind, std::vector<uint8_t>& cacheBytes)
|
2016-11-02 15:07:05 +03:00
|
|
|
{
|
|
|
|
BEGIN_ISOLATE_SCOPE
|
|
|
|
|
2023-08-19 17:29:45 +03:00
|
|
|
SharedPtr<V8ContextImpl> spContextImpl(!m_ContextEntries.empty() ? m_ContextEntries.front().pContextImpl : new V8ContextImpl(this, m_Name));
|
|
|
|
return spContextImpl->Compile(documentInfo, std::move(code), cacheKind, cacheBytes);
|
2016-11-02 15:07:05 +03:00
|
|
|
|
|
|
|
END_ISOLATE_SCOPE
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2023-08-19 17:29:45 +03:00
|
|
|
V8ScriptHolder* V8IsolateImpl::Compile(const V8DocumentInfo& documentInfo, StdString&& code, V8CacheKind cacheKind, const std::vector<uint8_t>& cacheBytes, bool& cacheAccepted)
|
2016-11-02 15:07:05 +03:00
|
|
|
{
|
|
|
|
BEGIN_ISOLATE_SCOPE
|
|
|
|
|
2023-08-19 17:29:45 +03:00
|
|
|
SharedPtr<V8ContextImpl> spContextImpl(!m_ContextEntries.empty() ? m_ContextEntries.front().pContextImpl : new V8ContextImpl(this, m_Name));
|
|
|
|
return spContextImpl->Compile(documentInfo, std::move(code), cacheKind, cacheBytes, cacheAccepted);
|
|
|
|
|
|
|
|
END_ISOLATE_SCOPE
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
V8ScriptHolder* V8IsolateImpl::Compile(const V8DocumentInfo& documentInfo, StdString&& code, V8CacheKind cacheKind, std::vector<uint8_t>& cacheBytes, V8CacheResult& cacheResult)
|
|
|
|
{
|
|
|
|
BEGIN_ISOLATE_SCOPE
|
|
|
|
|
|
|
|
SharedPtr<V8ContextImpl> spContextImpl(!m_ContextEntries.empty() ? m_ContextEntries.front().pContextImpl : new V8ContextImpl(this, m_Name));
|
|
|
|
return spContextImpl->Compile(documentInfo, std::move(code), cacheKind, cacheBytes, cacheResult);
|
2016-11-02 15:07:05 +03:00
|
|
|
|
|
|
|
END_ISOLATE_SCOPE
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2022-01-11 18:42:57 +03:00
|
|
|
bool V8IsolateImpl::GetEnableInterruptPropagation()
|
|
|
|
{
|
|
|
|
return m_EnableInterruptPropagation;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void V8IsolateImpl::SetEnableInterruptPropagation(bool value)
|
|
|
|
{
|
|
|
|
m_EnableInterruptPropagation = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2022-05-01 21:36:34 +03:00
|
|
|
bool V8IsolateImpl::GetDisableHeapSizeViolationInterrupt()
|
|
|
|
{
|
|
|
|
return m_DisableHeapSizeViolationInterrupt;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void V8IsolateImpl::SetDisableHeapSizeViolationInterrupt(bool value)
|
|
|
|
{
|
|
|
|
m_DisableHeapSizeViolationInterrupt = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2019-05-02 06:05:22 +03:00
|
|
|
void V8IsolateImpl::GetHeapStatistics(v8::HeapStatistics& heapStatistics)
|
2013-05-21 16:53:16 +04:00
|
|
|
{
|
|
|
|
BEGIN_ISOLATE_SCOPE
|
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
m_upIsolate->GetHeapStatistics(&heapStatistics);
|
2013-05-21 16:53:16 +04:00
|
|
|
|
|
|
|
END_ISOLATE_SCOPE
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2019-07-31 20:03:53 +03:00
|
|
|
V8Isolate::Statistics V8IsolateImpl::GetStatistics()
|
|
|
|
{
|
|
|
|
BEGIN_ISOLATE_SCOPE
|
2020-04-13 18:29:36 +03:00
|
|
|
BEGIN_MUTEX_SCOPE(m_DataMutex)
|
2019-07-31 20:03:53 +03:00
|
|
|
|
|
|
|
return m_Statistics;
|
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
END_MUTEX_SCOPE
|
2019-07-31 20:03:53 +03:00
|
|
|
END_ISOLATE_SCOPE
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2013-05-21 16:53:16 +04:00
|
|
|
void V8IsolateImpl::CollectGarbage(bool exhaustive)
|
|
|
|
{
|
|
|
|
BEGIN_ISOLATE_SCOPE
|
|
|
|
|
2019-05-02 06:05:22 +03:00
|
|
|
if (exhaustive)
|
|
|
|
{
|
2019-07-31 20:03:53 +03:00
|
|
|
ClearScriptCache();
|
2023-06-01 04:42:43 +03:00
|
|
|
ClearCachesForTesting();
|
|
|
|
RequestGarbageCollectionForTesting(v8::Isolate::kFullGarbageCollection);
|
2019-05-02 06:05:22 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-06-01 04:42:43 +03:00
|
|
|
RequestGarbageCollectionForTesting(v8::Isolate::kMinorGarbageCollection);
|
2019-05-02 06:05:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
END_ISOLATE_SCOPE
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
bool V8IsolateImpl::BeginCpuProfile(const StdString& name, v8::CpuProfilingMode mode, bool recordSamples)
|
|
|
|
{
|
|
|
|
BEGIN_ISOLATE_SCOPE
|
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
if (!m_upCpuProfiler)
|
2019-05-02 06:05:22 +03:00
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
m_upCpuProfiler.reset(v8::CpuProfiler::New(m_upIsolate.get()));
|
2019-05-02 06:05:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
v8::Local<v8::String> hName;
|
|
|
|
if (!CreateString(name).ToLocal(&hName))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
m_upCpuProfiler->StartProfiling(hName, mode, recordSamples);
|
2019-05-02 06:05:22 +03:00
|
|
|
return true;
|
|
|
|
|
|
|
|
END_ISOLATE_SCOPE
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2020-10-17 07:26:01 +03:00
|
|
|
bool V8IsolateImpl::EndCpuProfile(const StdString& name, CpuProfileCallback* pCallback, void* pvArg)
|
2019-05-02 06:05:22 +03:00
|
|
|
{
|
|
|
|
BEGIN_ISOLATE_SCOPE
|
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
if (!m_upCpuProfiler)
|
2019-05-02 06:05:22 +03:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
v8::Local<v8::String> hName;
|
|
|
|
if (!CreateString(name).ToLocal(&hName))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
UniqueDeletePtr<v8::CpuProfile> upProfile(m_upCpuProfiler->StopProfiling(hName));
|
|
|
|
if (!upProfile)
|
2019-05-02 06:05:22 +03:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pCallback)
|
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
pCallback(*upProfile, pvArg);
|
2019-05-02 06:05:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
END_ISOLATE_SCOPE
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void V8IsolateImpl::CollectCpuProfileSample()
|
|
|
|
{
|
|
|
|
BEGIN_ISOLATE_SCOPE
|
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
v8::CpuProfiler::CollectSample(m_upIsolate.get());
|
2019-05-02 06:05:22 +03:00
|
|
|
|
|
|
|
END_ISOLATE_SCOPE
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
uint32_t V8IsolateImpl::GetCpuProfileSampleInterval()
|
|
|
|
{
|
|
|
|
return m_CpuProfileSampleInterval;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void V8IsolateImpl::SetCpuProfileSampleInterval(uint32_t value)
|
|
|
|
{
|
|
|
|
BEGIN_ISOLATE_SCOPE
|
|
|
|
|
|
|
|
if (value != m_CpuProfileSampleInterval)
|
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
m_CpuProfileSampleInterval = std::min(std::max(value, 125U), static_cast<uint32_t>(INT_MAX));
|
2019-05-02 06:05:22 +03:00
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
if (!m_upCpuProfiler)
|
2019-05-02 06:05:22 +03:00
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
m_upCpuProfiler.reset(v8::CpuProfiler::New(m_upIsolate.get()));
|
2019-05-02 06:05:22 +03:00
|
|
|
}
|
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
m_upCpuProfiler->SetSamplingInterval(static_cast<int>(m_CpuProfileSampleInterval));
|
2019-05-02 06:05:22 +03:00
|
|
|
}
|
2013-05-21 16:53:16 +04:00
|
|
|
|
|
|
|
END_ISOLATE_SCOPE
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2021-01-22 17:44:18 +03:00
|
|
|
void V8IsolateImpl::WriteHeapSnapshot(void* pvStream)
|
|
|
|
{
|
|
|
|
BEGIN_ISOLATE_SCOPE
|
|
|
|
|
|
|
|
auto pSnapshot = m_upIsolate->GetHeapProfiler()->TakeHeapSnapshot();
|
|
|
|
|
|
|
|
V8OutputStream stream(pvStream);
|
|
|
|
pSnapshot->Serialize(&stream);
|
|
|
|
|
|
|
|
const_cast<v8::HeapSnapshot*>(pSnapshot)->Delete();
|
|
|
|
|
|
|
|
END_ISOLATE_SCOPE
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2017-11-07 22:32:41 +03:00
|
|
|
void V8IsolateImpl::runMessageLoopOnPause(int /*contextGroupId*/)
|
|
|
|
{
|
2021-11-22 18:45:53 +03:00
|
|
|
RunMessageLoop(RunMessageLoopReason::PausedInDebugger);
|
2017-11-07 22:32:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void V8IsolateImpl::quitMessageLoopOnPause()
|
|
|
|
{
|
|
|
|
_ASSERTE(IsCurrent() && IsLocked());
|
|
|
|
|
|
|
|
BEGIN_MUTEX_SCOPE(m_DataMutex)
|
2021-11-22 18:45:53 +03:00
|
|
|
m_optExitMessageLoopReason = ExitMessageLoopReason::ResumedExecution;
|
2017-11-07 22:32:41 +03:00
|
|
|
END_MUTEX_SCOPE
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2017-12-26 23:49:38 +03:00
|
|
|
void V8IsolateImpl::runIfWaitingForDebugger(int /*contextGroupId*/)
|
|
|
|
{
|
|
|
|
quitMessageLoopOnPause();
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2017-11-07 22:32:41 +03:00
|
|
|
v8::Local<v8::Context> V8IsolateImpl::ensureDefaultContextInGroup(int contextGroupId)
|
|
|
|
{
|
|
|
|
_ASSERTE(IsCurrent() && IsLocked());
|
|
|
|
|
2023-08-19 17:29:45 +03:00
|
|
|
if (!m_ContextEntries.empty())
|
2017-11-07 22:32:41 +03:00
|
|
|
{
|
2019-05-02 06:05:22 +03:00
|
|
|
return m_ContextEntries.front().pContextImpl->GetContext();
|
2017-11-07 22:32:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return v8_inspector::V8InspectorClient::ensureDefaultContextInGroup(contextGroupId);
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
double V8IsolateImpl::currentTimeMS()
|
|
|
|
{
|
|
|
|
return HighResolutionClock::GetRelativeSeconds() * 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
void V8IsolateImpl::sendResponse(int /*callId*/, std::unique_ptr<v8_inspector::StringBuffer> upMessage)
|
2017-11-07 22:32:41 +03:00
|
|
|
{
|
|
|
|
_ASSERTE(IsCurrent() && IsLocked());
|
|
|
|
|
|
|
|
if (m_pvDebugAgent)
|
|
|
|
{
|
2020-10-17 07:26:01 +03:00
|
|
|
HostObjectUtil::GetInstance().SendDebugMessage(m_pvDebugAgent, StdString(upMessage->string()));
|
2017-11-07 22:32:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
void V8IsolateImpl::sendNotification(std::unique_ptr<v8_inspector::StringBuffer> upMessage)
|
2017-11-07 22:32:41 +03:00
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
sendResponse(0, std::move(upMessage));
|
2017-11-07 22:32:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void V8IsolateImpl::flushProtocolNotifications()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-04-25 05:25:58 +04:00
|
|
|
void* V8IsolateImpl::AddRefV8Object(void* pvObject)
|
|
|
|
{
|
|
|
|
BEGIN_ISOLATE_SCOPE
|
|
|
|
|
2017-02-28 15:16:13 +03:00
|
|
|
return ::PtrFromHandle(CreatePersistent(::HandleFromPtr<v8::Object>(pvObject)));
|
2014-04-25 05:25:58 +04:00
|
|
|
|
|
|
|
END_ISOLATE_SCOPE
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void V8IsolateImpl::ReleaseV8Object(void* pvObject)
|
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
CallWithLockNoWait(true /*allowNesting*/, [pvObject] (V8IsolateImpl* pIsolateImpl)
|
2015-08-16 19:42:51 +03:00
|
|
|
{
|
2017-02-28 15:16:13 +03:00
|
|
|
pIsolateImpl->Dispose(::HandleFromPtr<v8::Object>(pvObject));
|
2015-08-16 19:42:51 +03:00
|
|
|
});
|
2014-04-25 05:25:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2013-05-21 16:53:16 +04:00
|
|
|
void* V8IsolateImpl::AddRefV8Script(void* pvScript)
|
|
|
|
{
|
|
|
|
BEGIN_ISOLATE_SCOPE
|
|
|
|
|
2017-02-28 15:16:13 +03:00
|
|
|
return ::PtrFromHandle(CreatePersistent(::HandleFromPtr<v8::UnboundScript>(pvScript)));
|
2013-05-21 16:53:16 +04:00
|
|
|
|
|
|
|
END_ISOLATE_SCOPE
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void V8IsolateImpl::ReleaseV8Script(void* pvScript)
|
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
CallWithLockNoWait(true /*allowNesting*/, [pvScript] (V8IsolateImpl* pIsolateImpl)
|
2015-08-16 19:42:51 +03:00
|
|
|
{
|
2017-02-28 15:16:13 +03:00
|
|
|
pIsolateImpl->Dispose(::HandleFromPtr<v8::Script>(pvScript));
|
2015-08-16 19:42:51 +03:00
|
|
|
});
|
|
|
|
}
|
2013-05-21 16:53:16 +04:00
|
|
|
|
2015-08-16 19:42:51 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
2013-05-21 16:53:16 +04:00
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
void V8IsolateImpl::RunTaskAsync(std::unique_ptr<v8::Task> upTask)
|
2017-04-19 00:28:29 +03:00
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
if (upTask)
|
2017-04-19 00:28:29 +03:00
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
if (m_Released)
|
|
|
|
{
|
|
|
|
upTask->Run();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::shared_ptr<v8::Task> spTask(std::move(upTask));
|
|
|
|
std::weak_ptr<v8::Task> wpTask(spTask);
|
2017-04-19 00:28:29 +03:00
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
BEGIN_MUTEX_SCOPE(m_DataMutex)
|
|
|
|
m_AsyncTasks.push_back(std::move(spTask));
|
|
|
|
m_Statistics.BumpPostedTaskCount(TaskKind::Worker);
|
|
|
|
END_MUTEX_SCOPE
|
2017-04-19 00:28:29 +03:00
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
auto wrIsolate = CreateWeakRef();
|
2020-10-17 07:26:01 +03:00
|
|
|
HostObjectUtil::GetInstance().QueueNativeCallback([this, wrIsolate, wpTask] ()
|
2017-04-19 00:28:29 +03:00
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
auto spIsolate = wrIsolate.GetTarget();
|
|
|
|
if (!spIsolate.IsEmpty())
|
2017-04-19 00:28:29 +03:00
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
auto spTask = wpTask.lock();
|
|
|
|
if (spTask)
|
|
|
|
{
|
|
|
|
spTask->Run();
|
2017-04-19 00:28:29 +03:00
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
BEGIN_MUTEX_SCOPE(m_DataMutex)
|
|
|
|
auto it = std::remove(m_AsyncTasks.begin(), m_AsyncTasks.end(), spTask);
|
|
|
|
m_AsyncTasks.erase(it, m_AsyncTasks.end());
|
|
|
|
m_Statistics.BumpInvokedTaskCount(TaskKind::Worker);
|
|
|
|
END_MUTEX_SCOPE
|
|
|
|
}
|
2017-04-19 00:28:29 +03:00
|
|
|
}
|
2020-04-13 18:29:36 +03:00
|
|
|
});
|
|
|
|
}
|
2017-04-19 00:28:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
void V8IsolateImpl::RunTaskDelayed(std::unique_ptr<v8::Task> upTask, double delayInSeconds)
|
2018-08-20 20:45:15 +03:00
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
if (upTask && !m_Released)
|
2018-08-20 20:45:15 +03:00
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
std::shared_ptr<v8::Task> spTask(std::move(upTask));
|
2018-08-20 20:45:15 +03:00
|
|
|
|
|
|
|
auto wrIsolate = CreateWeakRef();
|
|
|
|
SharedPtr<Timer> spTimer(new Timer(static_cast<int>(delayInSeconds * 1000), -1, [this, wrIsolate, spTask] (Timer* pTimer) mutable
|
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
auto spIsolate = wrIsolate.GetTarget();
|
|
|
|
if (!spIsolate.IsEmpty())
|
2018-08-20 20:45:15 +03:00
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
spTask->Run();
|
2018-08-20 20:45:15 +03:00
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
// Release the timer's strong task reference. Doing so avoids a deadlock when
|
|
|
|
// spIsolate's implicit destruction below triggers immediate isolate teardown.
|
2018-08-20 20:45:15 +03:00
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
spTask.reset();
|
2018-08-20 20:45:15 +03:00
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
// the timer has fired; discard it
|
2018-08-20 20:45:15 +03:00
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
BEGIN_MUTEX_SCOPE(m_DataMutex)
|
|
|
|
auto it = std::remove(m_TaskTimers.begin(), m_TaskTimers.end(), SharedPtr<Timer>(pTimer));
|
|
|
|
m_TaskTimers.erase(it, m_TaskTimers.end());
|
|
|
|
m_Statistics.BumpInvokedTaskCount(TaskKind::DelayedWorker);
|
|
|
|
END_MUTEX_SCOPE
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Release the timer's strong task reference. Doing so avoids a deadlock if the
|
|
|
|
// isolate is awaiting task completion on the managed finalization thread.
|
2018-08-20 20:45:15 +03:00
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
spTask.reset();
|
2018-08-20 20:45:15 +03:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
// hold on to the timer to ensure callback execution
|
|
|
|
|
|
|
|
BEGIN_MUTEX_SCOPE(m_DataMutex)
|
|
|
|
m_TaskTimers.push_back(spTimer);
|
2020-04-13 18:29:36 +03:00
|
|
|
m_Statistics.BumpPostedTaskCount(TaskKind::DelayedWorker);
|
2018-08-20 20:45:15 +03:00
|
|
|
END_MUTEX_SCOPE
|
|
|
|
|
|
|
|
// Release the local task reference explicitly. Doing so avoids a deadlock if the callback is
|
|
|
|
// executed synchronously. That shouldn't happen given the current timer implementation.
|
|
|
|
|
|
|
|
spTask.reset();
|
|
|
|
|
|
|
|
// now it's safe to start the timer
|
|
|
|
|
|
|
|
spTimer->Start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
void V8IsolateImpl::RunTaskWithLockAsync(bool allowNesting, std::unique_ptr<v8::Task> upTask)
|
2016-06-20 21:23:21 +03:00
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
if (upTask)
|
2016-06-20 21:23:21 +03:00
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
if (m_Released)
|
2017-04-19 00:28:29 +03:00
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
if (allowNesting)
|
|
|
|
{
|
|
|
|
upTask->Run();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::shared_ptr<v8::Task> spTask(std::move(upTask));
|
|
|
|
CallWithLockAsync(allowNesting, [allowNesting, spTask] (V8IsolateImpl* pIsolateImpl)
|
|
|
|
{
|
|
|
|
spTask->Run();
|
|
|
|
|
|
|
|
BEGIN_MUTEX_SCOPE(pIsolateImpl->m_DataMutex)
|
|
|
|
pIsolateImpl->m_Statistics.BumpInvokedTaskCount(allowNesting ? TaskKind::Foreground : TaskKind::NonNestableForeground);
|
|
|
|
END_MUTEX_SCOPE
|
|
|
|
});
|
|
|
|
|
|
|
|
BEGIN_MUTEX_SCOPE(m_DataMutex)
|
|
|
|
m_Statistics.BumpPostedTaskCount(allowNesting ? TaskKind::Foreground : TaskKind::NonNestableForeground);
|
|
|
|
END_MUTEX_SCOPE
|
|
|
|
}
|
2017-04-19 00:28:29 +03:00
|
|
|
}
|
2016-06-20 21:23:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
void V8IsolateImpl::RunTaskWithLockDelayed(bool allowNesting, std::unique_ptr<v8::Task> upTask, double delayInSeconds)
|
2015-12-07 14:57:35 +03:00
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
if (upTask && !m_Released)
|
2015-12-07 14:57:35 +03:00
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
std::shared_ptr<v8::Task> spTask(std::move(upTask));
|
2017-04-19 00:28:29 +03:00
|
|
|
|
|
|
|
auto wrIsolate = CreateWeakRef();
|
2020-04-13 18:29:36 +03:00
|
|
|
SharedPtr<Timer> spTimer(new Timer(static_cast<int>(delayInSeconds * 1000), -1, [this, wrIsolate, allowNesting, spTask] (Timer* pTimer) mutable
|
2015-12-07 14:57:35 +03:00
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
auto spIsolate = wrIsolate.GetTarget();
|
|
|
|
if (!spIsolate.IsEmpty())
|
2015-12-07 14:57:35 +03:00
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
CallWithLockNoWait(allowNesting, [allowNesting, spTask] (V8IsolateImpl* pIsolateImpl)
|
2016-11-02 15:07:05 +03:00
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
spTask->Run();
|
2017-04-19 00:28:29 +03:00
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
BEGIN_MUTEX_SCOPE(pIsolateImpl->m_DataMutex)
|
|
|
|
pIsolateImpl->m_Statistics.BumpInvokedTaskCount(allowNesting ? TaskKind::DelayedForeground : TaskKind::NonNestableDelayedForeground);
|
|
|
|
END_MUTEX_SCOPE
|
|
|
|
});
|
2015-12-07 14:57:35 +03:00
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
// Release the timer's strong task reference. Doing so avoids a deadlock when
|
|
|
|
// spIsolate's implicit destruction below triggers immediate isolate teardown.
|
2015-12-07 14:57:35 +03:00
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
spTask.reset();
|
2016-11-02 15:07:05 +03:00
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
// the timer has fired; discard it
|
2015-12-07 14:57:35 +03:00
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
BEGIN_MUTEX_SCOPE(m_DataMutex)
|
|
|
|
auto it = std::remove(m_TaskTimers.begin(), m_TaskTimers.end(), pTimer);
|
|
|
|
m_TaskTimers.erase(it, m_TaskTimers.end());
|
|
|
|
END_MUTEX_SCOPE
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Release the timer's strong task reference. Doing so avoids a deadlock if the
|
|
|
|
// isolate is awaiting task completion on the managed finalization thread.
|
|
|
|
|
|
|
|
spTask.reset();
|
2016-11-02 15:07:05 +03:00
|
|
|
}
|
2017-04-19 00:28:29 +03:00
|
|
|
}));
|
2015-12-07 14:57:35 +03:00
|
|
|
|
2017-04-19 00:28:29 +03:00
|
|
|
// hold on to the timer to ensure callback execution
|
2016-11-02 15:07:05 +03:00
|
|
|
|
2017-04-19 00:28:29 +03:00
|
|
|
BEGIN_MUTEX_SCOPE(m_DataMutex)
|
|
|
|
m_TaskTimers.push_back(spTimer);
|
2020-04-13 18:29:36 +03:00
|
|
|
m_Statistics.BumpPostedTaskCount(allowNesting ? TaskKind::DelayedForeground : TaskKind::NonNestableDelayedForeground);
|
2017-04-19 00:28:29 +03:00
|
|
|
END_MUTEX_SCOPE
|
2015-12-07 14:57:35 +03:00
|
|
|
|
2017-04-19 00:28:29 +03:00
|
|
|
// Release the local task reference explicitly. Doing so avoids a deadlock if the callback is
|
|
|
|
// executed synchronously. That shouldn't happen given the current timer implementation.
|
2016-11-02 15:07:05 +03:00
|
|
|
|
2017-04-19 00:28:29 +03:00
|
|
|
spTask.reset();
|
2016-11-02 15:07:05 +03:00
|
|
|
|
2017-04-19 00:28:29 +03:00
|
|
|
// now it's safe to start the timer
|
2016-11-02 15:07:05 +03:00
|
|
|
|
2017-04-19 00:28:29 +03:00
|
|
|
spTimer->Start();
|
|
|
|
}
|
2015-12-07 14:57:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
std::shared_ptr<v8::TaskRunner> V8IsolateImpl::GetForegroundTaskRunner()
|
2018-08-20 20:45:15 +03:00
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
BEGIN_MUTEX_SCOPE(m_DataMutex)
|
|
|
|
|
|
|
|
if (!m_spForegroundTaskRunner)
|
|
|
|
{
|
2022-05-01 21:36:34 +03:00
|
|
|
m_spForegroundTaskRunner = std::make_shared<V8ForegroundTaskRunner>(*this);
|
2020-04-13 18:29:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return m_spForegroundTaskRunner;
|
|
|
|
|
|
|
|
END_MUTEX_SCOPE
|
2018-08-20 20:45:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2021-07-23 06:47:37 +03:00
|
|
|
void* V8IsolateImpl::AllocateArrayBuffer(size_t size)
|
|
|
|
{
|
2022-05-01 21:36:34 +03:00
|
|
|
BEGIN_MUTEX_SCOPE(m_DataMutex)
|
|
|
|
|
|
|
|
auto newArrayBufferAllocation = m_ArrayBufferAllocation + size;
|
|
|
|
if ((newArrayBufferAllocation >= m_ArrayBufferAllocation) && (newArrayBufferAllocation <= m_MaxArrayBufferAllocation))
|
2021-07-23 06:47:37 +03:00
|
|
|
{
|
2022-05-01 21:36:34 +03:00
|
|
|
auto pvData = ::calloc(1, size);
|
|
|
|
if (pvData)
|
|
|
|
{
|
|
|
|
m_ArrayBufferAllocation = newArrayBufferAllocation;
|
|
|
|
return pvData;
|
|
|
|
}
|
2021-07-23 06:47:37 +03:00
|
|
|
}
|
|
|
|
|
2022-05-01 21:36:34 +03:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
END_MUTEX_SCOPE
|
2021-07-23 06:47:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void* V8IsolateImpl::AllocateUninitializedArrayBuffer(size_t size)
|
|
|
|
{
|
2022-05-01 21:36:34 +03:00
|
|
|
BEGIN_MUTEX_SCOPE(m_DataMutex)
|
|
|
|
|
|
|
|
auto newArrayBufferAllocation = m_ArrayBufferAllocation + size;
|
|
|
|
if ((newArrayBufferAllocation >= m_ArrayBufferAllocation) && (newArrayBufferAllocation <= m_MaxArrayBufferAllocation))
|
2021-07-23 06:47:37 +03:00
|
|
|
{
|
2022-05-01 21:36:34 +03:00
|
|
|
auto pvData = ::malloc(size);
|
|
|
|
if (pvData)
|
|
|
|
{
|
|
|
|
m_ArrayBufferAllocation = newArrayBufferAllocation;
|
|
|
|
return pvData;
|
|
|
|
}
|
2021-07-23 06:47:37 +03:00
|
|
|
}
|
|
|
|
|
2022-05-01 21:36:34 +03:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
END_MUTEX_SCOPE
|
2021-07-23 06:47:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void V8IsolateImpl::FreeArrayBuffer(void* pvData, size_t size)
|
|
|
|
{
|
2022-05-01 21:36:34 +03:00
|
|
|
BEGIN_MUTEX_SCOPE(m_DataMutex)
|
|
|
|
|
|
|
|
if (pvData)
|
2021-07-23 06:47:37 +03:00
|
|
|
{
|
2022-05-01 21:36:34 +03:00
|
|
|
::free(pvData);
|
|
|
|
if (m_ArrayBufferAllocation >= size)
|
|
|
|
{
|
|
|
|
m_ArrayBufferAllocation -= size;
|
|
|
|
}
|
2021-07-23 06:47:37 +03:00
|
|
|
}
|
2022-05-01 21:36:34 +03:00
|
|
|
|
|
|
|
END_MUTEX_SCOPE
|
2021-07-23 06:47:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
void V8IsolateImpl::CallWithLockNoWait(bool allowNesting, CallWithLockCallback&& callback)
|
2015-08-16 19:42:51 +03:00
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
if (callback)
|
2015-08-16 19:42:51 +03:00
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
if (m_Mutex.TryLock())
|
|
|
|
{
|
|
|
|
// the callback may release this instance; hold it for destruction outside isolate scope
|
|
|
|
SharedPtr<V8IsolateImpl> spThis(this);
|
2015-08-16 19:42:51 +03:00
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
MutexLock<RecursiveMutex> lock(m_Mutex, false);
|
|
|
|
if (allowNesting || (m_CallWithLockLevel < 1))
|
|
|
|
{
|
|
|
|
BEGIN_ISOLATE_NATIVE_SCOPE
|
|
|
|
BEGIN_PULSE_VALUE_SCOPE(&m_CallWithLockLevel, m_CallWithLockLevel + 1)
|
|
|
|
|
|
|
|
callback(this);
|
|
|
|
return;
|
|
|
|
|
|
|
|
END_PULSE_VALUE_SCOPE
|
|
|
|
END_ISOLATE_NATIVE_SCOPE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CallWithLockAsync(allowNesting, std::move(callback));
|
2015-08-16 19:42:51 +03:00
|
|
|
}
|
2013-05-21 16:53:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2020-10-17 07:26:01 +03:00
|
|
|
void NORETURN V8IsolateImpl::ThrowOutOfMemoryException()
|
2013-05-21 16:53:16 +04:00
|
|
|
{
|
|
|
|
m_IsOutOfMemory = true;
|
2024-03-25 03:48:28 +03:00
|
|
|
throw V8Exception(V8Exception::Type::Fatal, m_Name, StdString(SL("The V8 runtime has exceeded its memory limit")), ExecutionStarted());
|
2013-05-21 16:53:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2019-07-31 20:03:53 +03:00
|
|
|
void V8IsolateImpl::ImportMetaInitializeCallback(v8::Local<v8::Context> hContext, v8::Local<v8::Module> hModule, v8::Local<v8::Object> hMeta)
|
|
|
|
{
|
|
|
|
GetInstanceFromIsolate(hContext->GetIsolate())->InitializeImportMeta(hContext, hModule, hMeta);
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2022-02-06 18:53:15 +03:00
|
|
|
v8::MaybeLocal<v8::Promise> V8IsolateImpl::ModuleImportCallback(v8::Local<v8::Context> hContext, v8::Local<v8::Data> hHostDefinedOptions, v8::Local<v8::Value> hResourceName, v8::Local<v8::String> hSpecifier, v8::Local<v8::FixedArray> hImportAssertions)
|
2019-07-31 20:03:53 +03:00
|
|
|
{
|
2022-02-06 18:53:15 +03:00
|
|
|
return GetInstanceFromIsolate(hContext->GetIsolate())->ImportModule(hContext, hHostDefinedOptions, hResourceName, hSpecifier, hImportAssertions);
|
2019-07-31 20:03:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2021-03-04 16:22:48 +03:00
|
|
|
v8::MaybeLocal<v8::Module> V8IsolateImpl::ModuleResolveCallback(v8::Local<v8::Context> hContext, v8::Local<v8::String> hSpecifier, v8::Local<v8::FixedArray> /*importAssertions*/, v8::Local<v8::Module> hReferrer)
|
2019-07-31 20:03:53 +03:00
|
|
|
{
|
|
|
|
return GetInstanceFromIsolate(hContext->GetIsolate())->ResolveModule(hContext, hSpecifier, hReferrer);
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void V8IsolateImpl::InitializeImportMeta(v8::Local<v8::Context> hContext, v8::Local<v8::Module> hModule, v8::Local<v8::Object> hMeta)
|
|
|
|
{
|
|
|
|
_ASSERTE(IsCurrent() && IsLocked());
|
|
|
|
|
|
|
|
auto pContextImpl = FindContext(hContext);
|
|
|
|
if (pContextImpl)
|
|
|
|
{
|
|
|
|
return pContextImpl->InitializeImportMeta(hContext, hModule, hMeta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2022-02-06 18:53:15 +03:00
|
|
|
v8::MaybeLocal<v8::Promise> V8IsolateImpl::ImportModule(v8::Local<v8::Context> hContext, v8::Local<v8::Data> hHostDefinedOptions, v8::Local<v8::Value> hResourceName, v8::Local<v8::String> hSpecifier, v8::Local<v8::FixedArray> hImportAssertions)
|
2019-07-31 20:03:53 +03:00
|
|
|
{
|
|
|
|
_ASSERTE(IsCurrent() && IsLocked());
|
|
|
|
|
|
|
|
auto pContextImpl = FindContext(hContext);
|
|
|
|
if (pContextImpl)
|
|
|
|
{
|
2022-02-06 18:53:15 +03:00
|
|
|
return pContextImpl->ImportModule(hHostDefinedOptions, hResourceName, hSpecifier, hImportAssertions);
|
2019-07-31 20:03:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return v8::MaybeLocal<v8::Promise>();
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
v8::MaybeLocal<v8::Module> V8IsolateImpl::ResolveModule(v8::Local<v8::Context> hContext, v8::Local<v8::String> hSpecifier, v8::Local<v8::Module> hReferrer)
|
|
|
|
{
|
|
|
|
_ASSERTE(IsCurrent() && IsLocked());
|
|
|
|
|
|
|
|
auto pContextImpl = FindContext(hContext);
|
|
|
|
if (pContextImpl)
|
|
|
|
{
|
|
|
|
return pContextImpl->ResolveModule(hSpecifier, hReferrer);
|
|
|
|
}
|
|
|
|
|
|
|
|
return v8::MaybeLocal<v8::Module>();
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2020-05-28 21:40:04 +03:00
|
|
|
bool V8IsolateImpl::TryGetCachedScriptInfo(uint64_t uniqueId, V8DocumentInfo& documentInfo)
|
2019-07-31 20:03:53 +03:00
|
|
|
{
|
|
|
|
_ASSERTE(IsCurrent() && IsLocked());
|
|
|
|
|
2023-08-19 17:29:45 +03:00
|
|
|
for (auto it = m_ScriptCache.cbegin(); it != m_ScriptCache.cend(); it++)
|
2019-07-31 20:03:53 +03:00
|
|
|
{
|
2020-05-28 21:40:04 +03:00
|
|
|
if (it->DocumentInfo.GetUniqueId() == uniqueId)
|
|
|
|
{
|
|
|
|
m_ScriptCache.splice(m_ScriptCache.begin(), m_ScriptCache, it);
|
|
|
|
documentInfo = it->DocumentInfo;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
v8::Local<v8::UnboundScript> V8IsolateImpl::GetCachedScript(uint64_t uniqueId, size_t codeDigest)
|
|
|
|
{
|
|
|
|
_ASSERTE(IsCurrent() && IsLocked());
|
|
|
|
|
2023-08-19 17:29:45 +03:00
|
|
|
for (auto it = m_ScriptCache.cbegin(); it != m_ScriptCache.cend(); it++)
|
|
|
|
{
|
|
|
|
if ((it->DocumentInfo.GetUniqueId() == uniqueId) && (it->CodeDigest == codeDigest))
|
|
|
|
{
|
|
|
|
m_ScriptCache.splice(m_ScriptCache.begin(), m_ScriptCache, it);
|
|
|
|
return it->hScript;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return v8::Local<v8::UnboundScript>();
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
v8::Local<v8::UnboundScript> V8IsolateImpl::GetCachedScript(uint64_t uniqueId, size_t codeDigest, std::vector<uint8_t>& cacheBytes)
|
|
|
|
{
|
|
|
|
_ASSERTE(IsCurrent() && IsLocked());
|
|
|
|
|
|
|
|
for (auto it = m_ScriptCache.cbegin(); it != m_ScriptCache.cend(); it++)
|
2020-05-28 21:40:04 +03:00
|
|
|
{
|
|
|
|
if ((it->DocumentInfo.GetUniqueId() == uniqueId) && (it->CodeDigest == codeDigest))
|
2019-07-31 20:03:53 +03:00
|
|
|
{
|
|
|
|
m_ScriptCache.splice(m_ScriptCache.begin(), m_ScriptCache, it);
|
2023-08-19 17:29:45 +03:00
|
|
|
cacheBytes = it->CacheBytes;
|
2019-07-31 20:03:53 +03:00
|
|
|
return it->hScript;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-19 17:29:45 +03:00
|
|
|
cacheBytes.clear();
|
2019-07-31 20:03:53 +03:00
|
|
|
return v8::Local<v8::UnboundScript>();
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void V8IsolateImpl::CacheScript(const V8DocumentInfo& documentInfo, size_t codeDigest, v8::Local<v8::UnboundScript> hScript)
|
2023-08-19 17:29:45 +03:00
|
|
|
{
|
|
|
|
CacheScript(documentInfo, codeDigest, hScript, std::vector<uint8_t>());
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void V8IsolateImpl::CacheScript(const V8DocumentInfo& documentInfo, size_t codeDigest, v8::Local<v8::UnboundScript> hScript, const std::vector<uint8_t>& cacheBytes)
|
2019-07-31 20:03:53 +03:00
|
|
|
{
|
|
|
|
_ASSERTE(IsCurrent() && IsLocked());
|
|
|
|
|
2020-10-17 07:26:01 +03:00
|
|
|
auto maxScriptCacheSize = HostObjectUtil::GetInstance().GetMaxScriptCacheSize();
|
2020-05-28 21:40:04 +03:00
|
|
|
while (m_ScriptCache.size() >= maxScriptCacheSize)
|
2019-07-31 20:03:53 +03:00
|
|
|
{
|
|
|
|
Dispose(m_ScriptCache.back().hScript);
|
|
|
|
m_ScriptCache.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
_ASSERTE(std::none_of(m_ScriptCache.begin(), m_ScriptCache.end(),
|
|
|
|
[&documentInfo, codeDigest] (const ScriptCacheEntry& entry)
|
|
|
|
{
|
|
|
|
return (entry.DocumentInfo.GetUniqueId() == documentInfo.GetUniqueId()) && (entry.CodeDigest == codeDigest);
|
|
|
|
}));
|
|
|
|
|
2023-08-19 17:29:45 +03:00
|
|
|
ScriptCacheEntry entry { documentInfo, codeDigest, CreatePersistent(hScript), cacheBytes };
|
2019-07-31 20:03:53 +03:00
|
|
|
m_ScriptCache.push_front(std::move(entry));
|
|
|
|
|
|
|
|
m_Statistics.ScriptCacheSize = m_ScriptCache.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2023-08-19 17:29:45 +03:00
|
|
|
void V8IsolateImpl::SetCachedScriptCacheBytes(uint64_t uniqueId, size_t codeDigest, const std::vector<uint8_t>& cacheBytes)
|
|
|
|
{
|
|
|
|
_ASSERTE(IsCurrent() && IsLocked());
|
|
|
|
|
|
|
|
for (auto it = m_ScriptCache.begin(); it != m_ScriptCache.end(); it++)
|
|
|
|
{
|
|
|
|
if ((it->DocumentInfo.GetUniqueId() == uniqueId) && (it->CodeDigest == codeDigest))
|
|
|
|
{
|
|
|
|
m_ScriptCache.splice(m_ScriptCache.begin(), m_ScriptCache, it);
|
|
|
|
it->CacheBytes = cacheBytes;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2019-07-31 20:03:53 +03:00
|
|
|
void V8IsolateImpl::ClearScriptCache()
|
|
|
|
{
|
|
|
|
_ASSERTE(IsCurrent() && IsLocked());
|
|
|
|
|
2023-08-19 17:29:45 +03:00
|
|
|
while (!m_ScriptCache.empty())
|
2019-07-31 20:03:53 +03:00
|
|
|
{
|
|
|
|
Dispose(m_ScriptCache.front().hScript);
|
|
|
|
m_ScriptCache.pop_front();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_Statistics.ScriptCacheSize = m_ScriptCache.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2024-03-25 03:48:28 +03:00
|
|
|
void V8IsolateImpl::TerminateExecutionInternal()
|
|
|
|
{
|
|
|
|
if (!m_IsExecutionTerminating)
|
|
|
|
{
|
|
|
|
m_upIsolate->TerminateExecution();
|
|
|
|
m_IsExecutionTerminating = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void V8IsolateImpl::CancelTerminateExecutionInternal()
|
|
|
|
{
|
|
|
|
if (m_IsExecutionTerminating)
|
|
|
|
{
|
|
|
|
m_upIsolate->CancelTerminateExecution();
|
|
|
|
m_IsExecutionTerminating = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2013-05-21 16:53:16 +04:00
|
|
|
V8IsolateImpl::~V8IsolateImpl()
|
|
|
|
{
|
2015-08-16 19:42:51 +03:00
|
|
|
--s_InstanceCount;
|
2017-04-19 00:28:29 +03:00
|
|
|
m_Released = true;
|
|
|
|
|
|
|
|
// Entering the isolate scope triggers call-with-lock queue processing. It should always be
|
|
|
|
// done here, if for no other reason than that it may prevent deadlocks in V8 isolate disposal.
|
2013-05-21 16:53:16 +04:00
|
|
|
|
2015-08-16 19:42:51 +03:00
|
|
|
BEGIN_ISOLATE_SCOPE
|
2013-05-21 16:53:16 +04:00
|
|
|
DisableDebugging();
|
2019-07-31 20:03:53 +03:00
|
|
|
ClearScriptCache();
|
2013-05-21 16:53:16 +04:00
|
|
|
END_ISOLATE_SCOPE
|
|
|
|
|
2016-11-02 15:07:05 +03:00
|
|
|
{
|
2017-04-19 00:28:29 +03:00
|
|
|
std::vector<std::shared_ptr<v8::Task>> asyncTasks;
|
2016-11-02 15:07:05 +03:00
|
|
|
std::vector<SharedPtr<Timer>> taskTimers;
|
2017-04-19 00:28:29 +03:00
|
|
|
|
2016-11-02 15:07:05 +03:00
|
|
|
BEGIN_MUTEX_SCOPE(m_DataMutex)
|
2017-04-19 00:28:29 +03:00
|
|
|
std::swap(asyncTasks, m_AsyncTasks);
|
2016-11-02 15:07:05 +03:00
|
|
|
std::swap(taskTimers, m_TaskTimers);
|
|
|
|
END_MUTEX_SCOPE
|
2017-04-19 00:28:29 +03:00
|
|
|
|
|
|
|
for (const auto& spTask : asyncTasks)
|
|
|
|
{
|
|
|
|
spTask->Run();
|
|
|
|
}
|
2016-11-02 15:07:05 +03:00
|
|
|
}
|
|
|
|
|
2017-11-07 22:32:41 +03:00
|
|
|
Dispose(m_hHostObjectHolderKey);
|
2022-05-01 21:36:34 +03:00
|
|
|
|
2022-02-06 18:53:15 +03:00
|
|
|
m_upIsolate->SetHostImportModuleDynamicallyCallback(static_cast<v8::HostImportModuleDynamicallyCallback>(nullptr));
|
2020-04-13 18:29:36 +03:00
|
|
|
m_upIsolate->SetHostInitializeImportMetaObjectCallback(nullptr);
|
2022-03-05 21:04:28 +03:00
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
m_upIsolate->RemoveBeforeCallEnteredCallback(OnBeforeCallEntered);
|
2021-03-04 16:22:48 +03:00
|
|
|
m_upIsolate->RemoveNearHeapLimitCallback(HeapExpansionCallback, 0);
|
2013-05-21 16:53:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2021-11-22 18:45:53 +03:00
|
|
|
V8IsolateImpl::ExitMessageLoopReason V8IsolateImpl::RunMessageLoop(RunMessageLoopReason reason)
|
2017-12-26 23:49:38 +03:00
|
|
|
{
|
|
|
|
_ASSERTE(IsCurrent() && IsLocked());
|
|
|
|
|
|
|
|
std::unique_lock<std::mutex> lock(m_DataMutex.GetImpl());
|
|
|
|
|
2021-11-22 18:45:53 +03:00
|
|
|
if (!m_optRunMessageLoopReason)
|
2017-12-26 23:49:38 +03:00
|
|
|
{
|
2021-11-22 18:45:53 +03:00
|
|
|
m_optExitMessageLoopReason.reset();
|
2017-12-26 23:49:38 +03:00
|
|
|
|
2021-11-22 18:45:53 +03:00
|
|
|
BEGIN_PULSE_VALUE_SCOPE(&m_optRunMessageLoopReason, reason)
|
2017-12-26 23:49:38 +03:00
|
|
|
|
|
|
|
ProcessCallWithLockQueue(lock);
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
m_CallWithLockQueueChanged.wait(lock);
|
|
|
|
ProcessCallWithLockQueue(lock);
|
|
|
|
|
2021-11-22 18:45:53 +03:00
|
|
|
if (m_optExitMessageLoopReason)
|
2017-12-26 23:49:38 +03:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
END_PULSE_VALUE_SCOPE
|
|
|
|
|
|
|
|
ProcessCallWithLockQueue(lock);
|
2021-11-22 18:45:53 +03:00
|
|
|
return *m_optExitMessageLoopReason;
|
2017-12-26 23:49:38 +03:00
|
|
|
}
|
|
|
|
|
2021-11-22 18:45:53 +03:00
|
|
|
return ExitMessageLoopReason::NestedInvocation;
|
2017-12-26 23:49:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
void V8IsolateImpl::CallWithLockAsync(bool allowNesting, CallWithLockCallback&& callback)
|
Version 5.4.1: Implemented a V8 debug agent to compensate for removed V8 API, V8Update now fetches V8 source and dependencies from Git repositories, fixed V8 assembly unloading and patched V8 to tolerate redundant initialization (Issue #60), added ScriptEngine.EnableAutoHostVariables, fixed by-reference arguments to VBScript functions (Issue #58), removed support for Visual Studio 2012 (V8 build now requires at least Visual Studio 2013), added explicit loading of primary interop assemblies to fix Issue #68, added host exception marshaling for V8, fixed V8ScriptEngine crash when script code calls the HostObject constructor, fixed host item caching for host variables, added non-generic overloads of newArr() and func() to HostFunctions, added ScriptEngine.Current, HostFunctions instances can now be exposed in multiple script engines, added a GetDynamicMemberNames() override to MetaScriptItem (Issue #64), fixed indexed property binding ambiguity for inherited interfaces (Issue #69), added tests for bug fixes and new APIs. Tested with V8 3.30.33.16.
2015-02-06 07:35:44 +03:00
|
|
|
{
|
2015-08-16 19:42:51 +03:00
|
|
|
if (callback)
|
|
|
|
{
|
2015-12-07 14:57:35 +03:00
|
|
|
BEGIN_MUTEX_SCOPE(m_DataMutex)
|
2017-11-07 22:32:41 +03:00
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
m_CallWithLockQueue.push(std::make_pair(allowNesting, std::move(callback)));
|
2017-11-07 22:32:41 +03:00
|
|
|
|
2021-11-22 18:45:53 +03:00
|
|
|
if (m_optRunMessageLoopReason)
|
2017-11-07 22:32:41 +03:00
|
|
|
{
|
|
|
|
m_CallWithLockQueueChanged.notify_one();
|
2019-05-02 06:05:22 +03:00
|
|
|
return;
|
2017-11-07 22:32:41 +03:00
|
|
|
}
|
2019-05-02 06:05:22 +03:00
|
|
|
|
|
|
|
if (m_CallWithLockQueue.size() > 1)
|
2017-11-07 22:32:41 +03:00
|
|
|
{
|
2019-05-02 06:05:22 +03:00
|
|
|
return;
|
2017-11-07 22:32:41 +03:00
|
|
|
}
|
|
|
|
|
2015-08-16 19:42:51 +03:00
|
|
|
END_MUTEX_SCOPE
|
|
|
|
|
2019-05-02 06:05:22 +03:00
|
|
|
// trigger asynchronous queue processing
|
|
|
|
|
|
|
|
auto wrIsolate = CreateWeakRef();
|
2020-10-17 07:26:01 +03:00
|
|
|
HostObjectUtil::GetInstance().QueueNativeCallback([this, wrIsolate] ()
|
2015-08-16 19:42:51 +03:00
|
|
|
{
|
2019-05-02 06:05:22 +03:00
|
|
|
auto spIsolate = wrIsolate.GetTarget();
|
|
|
|
if (!spIsolate.IsEmpty())
|
|
|
|
{
|
|
|
|
if (m_Mutex.TryLock())
|
|
|
|
{
|
|
|
|
MutexLock<RecursiveMutex> lock(m_Mutex, false);
|
|
|
|
BEGIN_ISOLATE_NATIVE_SCOPE
|
|
|
|
// do nothing; scope entry triggers automatic queue processing
|
|
|
|
END_ISOLATE_NATIVE_SCOPE
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// The isolate is active on another thread, and the queue will be processed automatically
|
|
|
|
// at scope exit, but an interrupt ensures relatively timely processing.
|
|
|
|
|
|
|
|
RequestInterrupt(ProcessCallWithLockQueue, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2015-08-16 19:42:51 +03:00
|
|
|
}
|
Version 5.4.1: Implemented a V8 debug agent to compensate for removed V8 API, V8Update now fetches V8 source and dependencies from Git repositories, fixed V8 assembly unloading and patched V8 to tolerate redundant initialization (Issue #60), added ScriptEngine.EnableAutoHostVariables, fixed by-reference arguments to VBScript functions (Issue #58), removed support for Visual Studio 2012 (V8 build now requires at least Visual Studio 2013), added explicit loading of primary interop assemblies to fix Issue #68, added host exception marshaling for V8, fixed V8ScriptEngine crash when script code calls the HostObject constructor, fixed host item caching for host variables, added non-generic overloads of newArr() and func() to HostFunctions, added ScriptEngine.Current, HostFunctions instances can now be exposed in multiple script engines, added a GetDynamicMemberNames() override to MetaScriptItem (Issue #64), fixed indexed property binding ambiguity for inherited interfaces (Issue #69), added tests for bug fixes and new APIs. Tested with V8 3.30.33.16.
2015-02-06 07:35:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2015-08-16 19:42:51 +03:00
|
|
|
void V8IsolateImpl::ProcessCallWithLockQueue(v8::Isolate* /*pIsolate*/, void* pvIsolateImpl)
|
2014-10-22 17:05:01 +04:00
|
|
|
{
|
2015-08-16 19:42:51 +03:00
|
|
|
static_cast<V8IsolateImpl*>(pvIsolateImpl)->ProcessCallWithLockQueue();
|
|
|
|
}
|
2014-10-22 17:05:01 +04:00
|
|
|
|
2015-08-16 19:42:51 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
2014-10-22 17:05:01 +04:00
|
|
|
|
2015-08-16 19:42:51 +03:00
|
|
|
void V8IsolateImpl::ProcessCallWithLockQueue()
|
|
|
|
{
|
2022-03-05 21:04:28 +03:00
|
|
|
BEGIN_PROMISE_HOOK_SCOPE
|
|
|
|
|
|
|
|
std::unique_lock<std::mutex> lock(m_DataMutex.GetImpl());
|
|
|
|
ProcessCallWithLockQueue(lock);
|
|
|
|
|
|
|
|
END_PROMISE_HOOK_SCOPE
|
2017-11-07 22:32:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void V8IsolateImpl::ProcessCallWithLockQueue(std::unique_lock<std::mutex>& lock)
|
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
_ASSERTE(lock.mutex() == &m_DataMutex.GetImpl());
|
2017-11-07 22:32:41 +03:00
|
|
|
_ASSERTE(lock.owns_lock());
|
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
CallWithLockQueue callWithLockQueue(PopCallWithLockQueue(lock));
|
2023-08-19 17:29:45 +03:00
|
|
|
while (!callWithLockQueue.empty())
|
2014-10-22 17:05:01 +04:00
|
|
|
{
|
2017-11-07 22:32:41 +03:00
|
|
|
lock.unlock();
|
|
|
|
ProcessCallWithLockQueue(callWithLockQueue);
|
|
|
|
lock.lock();
|
2020-04-13 18:29:36 +03:00
|
|
|
callWithLockQueue = PopCallWithLockQueue(lock);
|
2014-10-22 17:05:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
void V8IsolateImpl::ProcessCallWithLockQueue(CallWithLockQueue& callWithLockQueue)
|
Version 5.4.1: Implemented a V8 debug agent to compensate for removed V8 API, V8Update now fetches V8 source and dependencies from Git repositories, fixed V8 assembly unloading and patched V8 to tolerate redundant initialization (Issue #60), added ScriptEngine.EnableAutoHostVariables, fixed by-reference arguments to VBScript functions (Issue #58), removed support for Visual Studio 2012 (V8 build now requires at least Visual Studio 2013), added explicit loading of primary interop assemblies to fix Issue #68, added host exception marshaling for V8, fixed V8ScriptEngine crash when script code calls the HostObject constructor, fixed host item caching for host variables, added non-generic overloads of newArr() and func() to HostFunctions, added ScriptEngine.Current, HostFunctions instances can now be exposed in multiple script engines, added a GetDynamicMemberNames() override to MetaScriptItem (Issue #64), fixed indexed property binding ambiguity for inherited interfaces (Issue #69), added tests for bug fixes and new APIs. Tested with V8 3.30.33.16.
2015-02-06 07:35:44 +03:00
|
|
|
{
|
2019-05-02 06:05:22 +03:00
|
|
|
_ASSERTE(IsCurrent() && IsLocked());
|
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
BEGIN_PULSE_VALUE_SCOPE(&m_CallWithLockLevel, m_CallWithLockLevel + 1)
|
|
|
|
|
2023-08-19 17:29:45 +03:00
|
|
|
while (!callWithLockQueue.empty())
|
2017-11-07 22:32:41 +03:00
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
try
|
|
|
|
{
|
|
|
|
callWithLockQueue.front().second(this);
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
callWithLockQueue.pop();
|
2017-11-07 22:32:41 +03:00
|
|
|
}
|
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
END_PULSE_VALUE_SCOPE
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
V8IsolateImpl::CallWithLockQueue V8IsolateImpl::PopCallWithLockQueue(const std::unique_lock<std::mutex>& lock)
|
|
|
|
{
|
|
|
|
_ASSERTE(IsCurrent() && IsLocked());
|
|
|
|
_ASSERTE(lock.mutex() == &m_DataMutex.GetImpl());
|
|
|
|
_ASSERTE(lock.owns_lock());
|
|
|
|
IGNORE_UNUSED(lock);
|
|
|
|
|
|
|
|
if (m_CallWithLockLevel < 1)
|
|
|
|
{
|
|
|
|
return std::move(m_CallWithLockQueue);
|
2017-11-07 22:32:41 +03:00
|
|
|
}
|
2020-04-13 18:29:36 +03:00
|
|
|
|
|
|
|
CallWithLockQueue nestableCallWithLockQueue;
|
|
|
|
CallWithLockQueue nonNestableCallWithLockQueue;
|
|
|
|
|
2023-08-19 17:29:45 +03:00
|
|
|
while (!m_CallWithLockQueue.empty())
|
2020-04-13 18:29:36 +03:00
|
|
|
{
|
|
|
|
auto& callWithLockEntry = m_CallWithLockQueue.front();
|
|
|
|
auto& callWithLockQueue = callWithLockEntry.first ? nestableCallWithLockQueue : nonNestableCallWithLockQueue;
|
|
|
|
callWithLockQueue.push(std::move(callWithLockEntry));
|
|
|
|
m_CallWithLockQueue.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_CallWithLockQueue = std::move(nonNestableCallWithLockQueue);
|
|
|
|
return nestableCallWithLockQueue;
|
Version 5.4.1: Implemented a V8 debug agent to compensate for removed V8 API, V8Update now fetches V8 source and dependencies from Git repositories, fixed V8 assembly unloading and patched V8 to tolerate redundant initialization (Issue #60), added ScriptEngine.EnableAutoHostVariables, fixed by-reference arguments to VBScript functions (Issue #58), removed support for Visual Studio 2012 (V8 build now requires at least Visual Studio 2013), added explicit loading of primary interop assemblies to fix Issue #68, added host exception marshaling for V8, fixed V8ScriptEngine crash when script code calls the HostObject constructor, fixed host item caching for host variables, added non-generic overloads of newArr() and func() to HostFunctions, added ScriptEngine.Current, HostFunctions instances can now be exposed in multiple script engines, added a GetDynamicMemberNames() override to MetaScriptItem (Issue #64), fixed indexed property binding ambiguity for inherited interfaces (Issue #69), added tests for bug fixes and new APIs. Tested with V8 3.30.33.16.
2015-02-06 07:35:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2017-11-07 22:32:41 +03:00
|
|
|
void V8IsolateImpl::ConnectDebugClient()
|
Version 5.4.1: Implemented a V8 debug agent to compensate for removed V8 API, V8Update now fetches V8 source and dependencies from Git repositories, fixed V8 assembly unloading and patched V8 to tolerate redundant initialization (Issue #60), added ScriptEngine.EnableAutoHostVariables, fixed by-reference arguments to VBScript functions (Issue #58), removed support for Visual Studio 2012 (V8 build now requires at least Visual Studio 2013), added explicit loading of primary interop assemblies to fix Issue #68, added host exception marshaling for V8, fixed V8ScriptEngine crash when script code calls the HostObject constructor, fixed host item caching for host variables, added non-generic overloads of newArr() and func() to HostFunctions, added ScriptEngine.Current, HostFunctions instances can now be exposed in multiple script engines, added a GetDynamicMemberNames() override to MetaScriptItem (Issue #64), fixed indexed property binding ambiguity for inherited interfaces (Issue #69), added tests for bug fixes and new APIs. Tested with V8 3.30.33.16.
2015-02-06 07:35:44 +03:00
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
CallWithLockNoWait(true /*allowNesting*/, [] (V8IsolateImpl* pIsolateImpl)
|
2017-11-07 22:32:41 +03:00
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
if (pIsolateImpl->m_upInspector && !pIsolateImpl->m_upInspectorSession)
|
2017-11-07 22:32:41 +03:00
|
|
|
{
|
2022-06-29 07:28:42 +03:00
|
|
|
pIsolateImpl->m_upInspectorSession = pIsolateImpl->m_upInspector->connect(s_ContextGroupId, pIsolateImpl, v8_inspector::StringView(), v8_inspector::V8Inspector::ClientTrustLevel::kFullyTrusted);
|
2017-11-07 22:32:41 +03:00
|
|
|
}
|
|
|
|
});
|
Version 5.4.1: Implemented a V8 debug agent to compensate for removed V8 API, V8Update now fetches V8 source and dependencies from Git repositories, fixed V8 assembly unloading and patched V8 to tolerate redundant initialization (Issue #60), added ScriptEngine.EnableAutoHostVariables, fixed by-reference arguments to VBScript functions (Issue #58), removed support for Visual Studio 2012 (V8 build now requires at least Visual Studio 2013), added explicit loading of primary interop assemblies to fix Issue #68, added host exception marshaling for V8, fixed V8ScriptEngine crash when script code calls the HostObject constructor, fixed host item caching for host variables, added non-generic overloads of newArr() and func() to HostFunctions, added ScriptEngine.Current, HostFunctions instances can now be exposed in multiple script engines, added a GetDynamicMemberNames() override to MetaScriptItem (Issue #64), fixed indexed property binding ambiguity for inherited interfaces (Issue #69), added tests for bug fixes and new APIs. Tested with V8 3.30.33.16.
2015-02-06 07:35:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2017-11-07 22:32:41 +03:00
|
|
|
void V8IsolateImpl::SendDebugCommand(const StdString& command)
|
Version 5.4.1: Implemented a V8 debug agent to compensate for removed V8 API, V8Update now fetches V8 source and dependencies from Git repositories, fixed V8 assembly unloading and patched V8 to tolerate redundant initialization (Issue #60), added ScriptEngine.EnableAutoHostVariables, fixed by-reference arguments to VBScript functions (Issue #58), removed support for Visual Studio 2012 (V8 build now requires at least Visual Studio 2013), added explicit loading of primary interop assemblies to fix Issue #68, added host exception marshaling for V8, fixed V8ScriptEngine crash when script code calls the HostObject constructor, fixed host item caching for host variables, added non-generic overloads of newArr() and func() to HostFunctions, added ScriptEngine.Current, HostFunctions instances can now be exposed in multiple script engines, added a GetDynamicMemberNames() override to MetaScriptItem (Issue #64), fixed indexed property binding ambiguity for inherited interfaces (Issue #69), added tests for bug fixes and new APIs. Tested with V8 3.30.33.16.
2015-02-06 07:35:44 +03:00
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
CallWithLockNoWait(true /*allowNesting*/, [command] (V8IsolateImpl* pIsolateImpl)
|
Version 5.4.1: Implemented a V8 debug agent to compensate for removed V8 API, V8Update now fetches V8 source and dependencies from Git repositories, fixed V8 assembly unloading and patched V8 to tolerate redundant initialization (Issue #60), added ScriptEngine.EnableAutoHostVariables, fixed by-reference arguments to VBScript functions (Issue #58), removed support for Visual Studio 2012 (V8 build now requires at least Visual Studio 2013), added explicit loading of primary interop assemblies to fix Issue #68, added host exception marshaling for V8, fixed V8ScriptEngine crash when script code calls the HostObject constructor, fixed host item caching for host variables, added non-generic overloads of newArr() and func() to HostFunctions, added ScriptEngine.Current, HostFunctions instances can now be exposed in multiple script engines, added a GetDynamicMemberNames() override to MetaScriptItem (Issue #64), fixed indexed property binding ambiguity for inherited interfaces (Issue #69), added tests for bug fixes and new APIs. Tested with V8 3.30.33.16.
2015-02-06 07:35:44 +03:00
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
if (pIsolateImpl->m_upInspectorSession)
|
2017-11-07 22:32:41 +03:00
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
pIsolateImpl->m_upInspectorSession->dispatchProtocolMessage(command.GetStringView());
|
2017-11-07 22:32:41 +03:00
|
|
|
}
|
|
|
|
});
|
Version 5.4.1: Implemented a V8 debug agent to compensate for removed V8 API, V8Update now fetches V8 source and dependencies from Git repositories, fixed V8 assembly unloading and patched V8 to tolerate redundant initialization (Issue #60), added ScriptEngine.EnableAutoHostVariables, fixed by-reference arguments to VBScript functions (Issue #58), removed support for Visual Studio 2012 (V8 build now requires at least Visual Studio 2013), added explicit loading of primary interop assemblies to fix Issue #68, added host exception marshaling for V8, fixed V8ScriptEngine crash when script code calls the HostObject constructor, fixed host item caching for host variables, added non-generic overloads of newArr() and func() to HostFunctions, added ScriptEngine.Current, HostFunctions instances can now be exposed in multiple script engines, added a GetDynamicMemberNames() override to MetaScriptItem (Issue #64), fixed indexed property binding ambiguity for inherited interfaces (Issue #69), added tests for bug fixes and new APIs. Tested with V8 3.30.33.16.
2015-02-06 07:35:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2017-11-07 22:32:41 +03:00
|
|
|
void V8IsolateImpl::DisconnectDebugClient()
|
2013-05-21 16:53:16 +04:00
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
CallWithLockNoWait(true /*allowNesting*/, [] (V8IsolateImpl* pIsolateImpl)
|
2013-05-21 16:53:16 +04:00
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
pIsolateImpl->m_upInspectorSession.reset();
|
2017-11-07 22:32:41 +03:00
|
|
|
});
|
2013-05-21 16:53:16 +04:00
|
|
|
}
|
2013-11-28 20:15:47 +04:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2016-05-29 14:03:05 +03:00
|
|
|
V8IsolateImpl::ExecutionScope* V8IsolateImpl::EnterExecutionScope(ExecutionScope* pExecutionScope, size_t* pStackMarker)
|
2013-11-28 20:15:47 +04:00
|
|
|
{
|
2015-08-16 19:42:51 +03:00
|
|
|
_ASSERTE(IsCurrent() && IsLocked());
|
2013-11-28 20:15:47 +04:00
|
|
|
|
2014-10-22 17:05:01 +04:00
|
|
|
// is heap size monitoring in progress?
|
|
|
|
if (m_HeapWatchLevel == 0)
|
2013-11-28 20:15:47 +04:00
|
|
|
{
|
2014-10-22 17:05:01 +04:00
|
|
|
// no; there should be no heap watch timer
|
|
|
|
_ASSERTE(m_spHeapWatchTimer.IsEmpty());
|
|
|
|
|
|
|
|
// is a heap size limit specified?
|
|
|
|
size_t maxHeapSize = m_MaxHeapSize;
|
|
|
|
if (maxHeapSize > 0)
|
|
|
|
{
|
|
|
|
// yes; perform initial check and set up heap watch timer
|
2023-01-23 17:44:25 +03:00
|
|
|
CheckHeapSize(maxHeapSize, false /*timerTriggered*/);
|
2014-10-22 17:05:01 +04:00
|
|
|
|
|
|
|
// enter outermost heap size monitoring scope
|
|
|
|
m_HeapWatchLevel = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// heap size monitoring in progress; enter nested scope
|
|
|
|
m_HeapWatchLevel++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// is stack usage monitoring in progress?
|
|
|
|
if (m_StackWatchLevel == 0)
|
|
|
|
{
|
|
|
|
// no; there should be no stack address limit
|
2013-11-28 20:15:47 +04:00
|
|
|
_ASSERTE(m_pStackLimit == nullptr);
|
|
|
|
|
|
|
|
// is a stack usage limit specified?
|
|
|
|
size_t maxStackUsage = m_MaxStackUsage;
|
|
|
|
if (maxStackUsage > 0)
|
|
|
|
{
|
|
|
|
// yes; ensure minimum breathing room
|
2014-04-10 15:51:19 +04:00
|
|
|
maxStackUsage = std::max(maxStackUsage, s_StackBreathingRoom);
|
2013-11-28 20:15:47 +04:00
|
|
|
|
|
|
|
// calculate stack address limit
|
|
|
|
size_t* pStackLimit = pStackMarker - (maxStackUsage / sizeof(size_t));
|
|
|
|
if ((pStackLimit < s_pMinStackLimit) || (pStackLimit > pStackMarker))
|
|
|
|
{
|
|
|
|
// underflow; use minimum non-null stack address
|
|
|
|
pStackLimit = s_pMinStackLimit;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// check stack address limit sanity
|
2021-03-04 16:22:48 +03:00
|
|
|
_ASSERTE(static_cast<size_t>(pStackMarker - pStackLimit) >= (s_StackBreathingRoom / sizeof(size_t)));
|
2013-11-28 20:15:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// set and record stack address limit
|
2020-04-13 18:29:36 +03:00
|
|
|
m_upIsolate->SetStackLimit(reinterpret_cast<uintptr_t>(pStackLimit));
|
2013-11-28 20:15:47 +04:00
|
|
|
m_pStackLimit = pStackLimit;
|
2014-10-22 17:05:01 +04:00
|
|
|
|
|
|
|
// enter outermost stack usage monitoring scope
|
|
|
|
m_StackWatchLevel = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// stack usage monitoring in progress
|
|
|
|
if ((m_pStackLimit != nullptr) && (pStackMarker < m_pStackLimit))
|
|
|
|
{
|
|
|
|
// stack usage limit exceeded (host-side detection)
|
2020-10-17 07:26:01 +03:00
|
|
|
throw V8Exception(V8Exception::Type::General, m_Name, StdString(SL("The V8 runtime has exceeded its stack usage limit")), false /*executionStarted*/);
|
2013-11-28 20:15:47 +04:00
|
|
|
}
|
2014-10-22 17:05:01 +04:00
|
|
|
|
|
|
|
// enter nested stack usage monitoring scope
|
|
|
|
m_StackWatchLevel++;
|
2013-11-28 20:15:47 +04:00
|
|
|
}
|
2015-08-16 19:42:51 +03:00
|
|
|
|
2016-05-29 14:03:05 +03:00
|
|
|
// mark execution scope
|
2024-03-25 03:48:28 +03:00
|
|
|
return SetExecutionScope(pExecutionScope);
|
2014-10-22 17:05:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2016-05-29 14:03:05 +03:00
|
|
|
void V8IsolateImpl::ExitExecutionScope(ExecutionScope* pPreviousExecutionScope)
|
2014-10-22 17:05:01 +04:00
|
|
|
{
|
2015-08-16 19:42:51 +03:00
|
|
|
_ASSERTE(IsCurrent() && IsLocked());
|
2014-10-22 17:05:01 +04:00
|
|
|
|
2016-05-29 14:03:05 +03:00
|
|
|
// reset execution scope
|
2024-03-25 03:48:28 +03:00
|
|
|
SetExecutionScope(pPreviousExecutionScope);
|
2016-05-29 14:03:05 +03:00
|
|
|
|
2022-01-11 18:42:57 +03:00
|
|
|
// is interrupt propagation enabled?
|
|
|
|
if (!m_EnableInterruptPropagation)
|
|
|
|
{
|
|
|
|
// no; cancel termination to allow remaining script frames to execute
|
|
|
|
CancelTerminateExecution();
|
|
|
|
}
|
2015-12-07 14:57:35 +03:00
|
|
|
|
2014-10-22 17:05:01 +04:00
|
|
|
// is stack usage monitoring in progress?
|
|
|
|
if (m_StackWatchLevel > 0)
|
|
|
|
{
|
|
|
|
// yes; exit stack usage monitoring scope
|
|
|
|
if (--m_StackWatchLevel == 0)
|
|
|
|
{
|
|
|
|
// exited outermost scope; remove stack address limit
|
|
|
|
if (m_pStackLimit != nullptr)
|
|
|
|
{
|
|
|
|
// V8 has no API for removing a stack address limit
|
2020-04-13 18:29:36 +03:00
|
|
|
m_upIsolate->SetStackLimit(reinterpret_cast<uintptr_t>(s_pMinStackLimit));
|
2014-10-22 17:05:01 +04:00
|
|
|
m_pStackLimit = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// is heap size monitoring in progress?
|
|
|
|
if (m_HeapWatchLevel > 0)
|
2013-11-28 20:15:47 +04:00
|
|
|
{
|
2014-10-22 17:05:01 +04:00
|
|
|
// yes; exit heap size monitoring scope
|
|
|
|
if (--m_HeapWatchLevel == 0)
|
|
|
|
{
|
|
|
|
// exited outermost scope; destroy heap watch timer
|
|
|
|
m_spHeapWatchTimer.Empty();
|
|
|
|
}
|
2013-11-28 20:15:47 +04:00
|
|
|
}
|
2014-10-22 17:05:01 +04:00
|
|
|
}
|
|
|
|
|
2024-03-25 03:48:28 +03:00
|
|
|
|
2014-10-22 17:05:01 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2024-03-25 03:48:28 +03:00
|
|
|
V8IsolateImpl::ExecutionScope* V8IsolateImpl::SetExecutionScope(ExecutionScope* pExecutionScope)
|
|
|
|
{
|
|
|
|
BEGIN_MUTEX_SCOPE(m_TerminateExecutionMutex)
|
|
|
|
|
|
|
|
auto pPrevExecutionScope = std::exchange(m_pExecutionScope, pExecutionScope);
|
|
|
|
|
|
|
|
if (pExecutionScope == nullptr)
|
|
|
|
{
|
|
|
|
CancelTerminateExecutionInternal();
|
|
|
|
}
|
|
|
|
|
|
|
|
return pPrevExecutionScope;
|
|
|
|
|
|
|
|
END_MUTEX_SCOPE
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
bool V8IsolateImpl::InExecutionScope()
|
|
|
|
{
|
|
|
|
BEGIN_MUTEX_SCOPE(m_TerminateExecutionMutex)
|
|
|
|
return m_pExecutionScope != nullptr;
|
|
|
|
END_MUTEX_SCOPE
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void V8IsolateImpl::OnExecutionStarted()
|
|
|
|
{
|
|
|
|
BEGIN_MUTEX_SCOPE(m_TerminateExecutionMutex)
|
|
|
|
|
|
|
|
if (m_pExecutionScope != nullptr)
|
|
|
|
{
|
|
|
|
m_pExecutionScope->OnExecutionStarted();
|
|
|
|
}
|
|
|
|
|
|
|
|
END_MUTEX_SCOPE
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
bool V8IsolateImpl::ExecutionStarted()
|
|
|
|
{
|
|
|
|
BEGIN_MUTEX_SCOPE(m_TerminateExecutionMutex)
|
|
|
|
return (m_pExecutionScope != nullptr) ? m_pExecutionScope->ExecutionStarted() : false;
|
|
|
|
END_MUTEX_SCOPE
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void V8IsolateImpl::SetUpHeapWatchTimer(bool forceMinInterval)
|
2014-10-22 17:05:01 +04:00
|
|
|
{
|
2015-08-16 19:42:51 +03:00
|
|
|
_ASSERTE(IsCurrent() && IsLocked());
|
2013-11-28 20:15:47 +04:00
|
|
|
|
2024-03-25 03:48:28 +03:00
|
|
|
const auto minInterval = 50.0;
|
|
|
|
auto interval = forceMinInterval ? minInterval : std::max(GetHeapSizeSampleInterval(), minInterval);
|
|
|
|
|
2014-10-22 17:05:01 +04:00
|
|
|
// create heap watch timer
|
|
|
|
auto wrIsolate = CreateWeakRef();
|
2024-03-25 03:48:28 +03:00
|
|
|
m_spHeapWatchTimer = new Timer(static_cast<int>(interval), -1, [this, wrIsolate] (Timer* pTimer)
|
2014-10-22 17:05:01 +04:00
|
|
|
{
|
|
|
|
// heap watch callback; is the isolate still alive?
|
|
|
|
auto spIsolate = wrIsolate.GetTarget();
|
|
|
|
if (!spIsolate.IsEmpty())
|
|
|
|
{
|
|
|
|
// yes; request callback on execution thread
|
|
|
|
auto wrTimer = pTimer->CreateWeakRef();
|
2022-05-01 21:36:34 +03:00
|
|
|
CallWithLockAsync(true /*allowNesting*/, [wrTimer] (V8IsolateImpl* pIsolateImpl)
|
2014-10-22 17:05:01 +04:00
|
|
|
{
|
|
|
|
// execution thread callback; is the timer still alive?
|
|
|
|
auto spTimer = wrTimer.GetTarget();
|
|
|
|
if (!spTimer.IsEmpty())
|
|
|
|
{
|
|
|
|
// yes; check heap size
|
2023-01-23 17:44:25 +03:00
|
|
|
pIsolateImpl->CheckHeapSize(std::nullopt, true /*timerTriggered*/);
|
2014-10-22 17:05:01 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// start heap watch timer
|
|
|
|
m_spHeapWatchTimer->Start();
|
2013-11-28 20:15:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2023-01-23 17:44:25 +03:00
|
|
|
void V8IsolateImpl::CheckHeapSize(const std::optional<size_t>& optMaxHeapSize, bool timerTriggered)
|
2013-11-28 20:15:47 +04:00
|
|
|
{
|
2015-08-16 19:42:51 +03:00
|
|
|
_ASSERTE(IsCurrent() && IsLocked());
|
2013-11-28 20:15:47 +04:00
|
|
|
|
2022-05-01 21:36:34 +03:00
|
|
|
// do we have a heap size limit?
|
|
|
|
auto maxHeapSize = optMaxHeapSize.has_value() ? optMaxHeapSize.value() : m_MaxHeapSize.load();
|
|
|
|
if (maxHeapSize > 0)
|
2013-11-28 20:15:47 +04:00
|
|
|
{
|
2024-03-25 03:48:28 +03:00
|
|
|
// yes; use normal heap watch timer interval by default
|
|
|
|
auto forceMinInterval = false;
|
|
|
|
|
|
|
|
// is the total heap size over the limit?
|
2022-05-01 21:36:34 +03:00
|
|
|
v8::HeapStatistics heapStatistics;
|
2019-05-02 06:05:22 +03:00
|
|
|
GetHeapStatistics(heapStatistics);
|
|
|
|
if (heapStatistics.total_heap_size() > maxHeapSize)
|
2013-11-28 20:15:47 +04:00
|
|
|
{
|
2022-05-01 21:36:34 +03:00
|
|
|
// yes; collect garbage
|
2023-06-01 04:42:43 +03:00
|
|
|
ClearCachesForTesting();
|
|
|
|
RequestGarbageCollectionForTesting(v8::Isolate::kFullGarbageCollection);
|
2022-05-01 21:36:34 +03:00
|
|
|
|
|
|
|
// is the total heap size still over the limit?
|
|
|
|
GetHeapStatistics(heapStatistics);
|
|
|
|
if (heapStatistics.total_heap_size() > maxHeapSize)
|
|
|
|
{
|
|
|
|
// yes; the isolate is out of memory; act based on policy
|
|
|
|
if (m_DisableHeapSizeViolationInterrupt)
|
|
|
|
{
|
2024-03-25 03:48:28 +03:00
|
|
|
if (InExecutionScope())
|
|
|
|
{
|
|
|
|
m_MaxHeapSize = 0;
|
|
|
|
m_upIsolate->ThrowError("The V8 runtime has exceeded its memory limit");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// defer exception until code execution is in progress
|
|
|
|
forceMinInterval = true;
|
2022-05-01 21:36:34 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_IsOutOfMemory = true;
|
|
|
|
TerminateExecution();
|
2024-03-25 03:48:28 +03:00
|
|
|
return;
|
2022-05-01 21:36:34 +03:00
|
|
|
}
|
|
|
|
}
|
2013-11-28 20:15:47 +04:00
|
|
|
}
|
2014-10-22 17:05:01 +04:00
|
|
|
|
2023-01-23 17:44:25 +03:00
|
|
|
// the isolate is not out of memory; is heap size monitoring in progress?
|
|
|
|
if (!timerTriggered || (m_HeapWatchLevel > 0))
|
|
|
|
{
|
|
|
|
// yes; restart heap watch timer
|
2024-03-25 03:48:28 +03:00
|
|
|
SetUpHeapWatchTimer(forceMinInterval);
|
2023-01-23 17:44:25 +03:00
|
|
|
}
|
2022-05-01 21:36:34 +03:00
|
|
|
}
|
2013-11-28 20:15:47 +04:00
|
|
|
}
|
2016-05-29 14:03:05 +03:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void V8IsolateImpl::OnBeforeCallEntered(v8::Isolate* pIsolate)
|
|
|
|
{
|
2018-11-19 22:49:38 +03:00
|
|
|
GetInstanceFromIsolate(pIsolate)->OnBeforeCallEntered();
|
2016-05-29 14:03:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void V8IsolateImpl::OnBeforeCallEntered()
|
|
|
|
{
|
|
|
|
_ASSERTE(IsCurrent() && IsLocked());
|
2024-03-25 03:48:28 +03:00
|
|
|
OnExecutionStarted();
|
2016-05-29 14:03:05 +03:00
|
|
|
}
|
2019-05-02 06:05:22 +03:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void V8IsolateImpl::PromiseHook(v8::PromiseHookType type, v8::Local<v8::Promise> hPromise, v8::Local<v8::Value> /*hParent*/)
|
|
|
|
{
|
|
|
|
if ((type == v8::PromiseHookType::kResolve) && !hPromise.IsEmpty())
|
|
|
|
{
|
2021-04-17 21:09:56 +03:00
|
|
|
auto hContext = hPromise->GetCreationContext().FromMaybe(v8::Local<v8::Context>());
|
2019-05-02 06:05:22 +03:00
|
|
|
if (!hContext.IsEmpty())
|
|
|
|
{
|
|
|
|
GetInstanceFromIsolate(hContext->GetIsolate())->FlushContextAsync(hContext);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void V8IsolateImpl::FlushContextAsync(v8::Local<v8::Context> hContext)
|
|
|
|
{
|
|
|
|
_ASSERTE(IsCurrent() && IsLocked());
|
|
|
|
|
|
|
|
for (auto& contextEntry : m_ContextEntries)
|
|
|
|
{
|
|
|
|
if (contextEntry.pContextImpl->GetContext() == hContext)
|
|
|
|
{
|
|
|
|
FlushContextAsync(contextEntry);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void V8IsolateImpl::FlushContextAsync(ContextEntry& contextEntry)
|
|
|
|
{
|
|
|
|
auto expected = false;
|
|
|
|
if (contextEntry.FlushPending.compare_exchange_strong(expected, true))
|
|
|
|
{
|
|
|
|
auto wrContext = contextEntry.pContextImpl->CreateWeakRef();
|
2020-04-13 18:29:36 +03:00
|
|
|
CallWithLockAsync(true /*allowNesting*/, [wrContext] (V8IsolateImpl* pIsolateImpl)
|
2019-05-02 06:05:22 +03:00
|
|
|
{
|
|
|
|
auto spContext = wrContext.GetTarget();
|
|
|
|
if (!spContext.IsEmpty())
|
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
pIsolateImpl->FlushContext(spContext.DerefAs<V8ContextImpl>());
|
2019-05-02 06:05:22 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
void V8IsolateImpl::FlushContext(V8ContextImpl& contextImpl)
|
2019-05-02 06:05:22 +03:00
|
|
|
{
|
|
|
|
_ASSERTE(IsCurrent() && IsLocked());
|
|
|
|
|
|
|
|
for (auto& contextEntry : m_ContextEntries)
|
|
|
|
{
|
2020-04-13 18:29:36 +03:00
|
|
|
if (contextEntry.pContextImpl == &contextImpl)
|
2019-05-02 06:05:22 +03:00
|
|
|
{
|
|
|
|
contextEntry.FlushPending = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-13 18:29:36 +03:00
|
|
|
contextImpl.Flush();
|
2019-05-02 06:05:22 +03:00
|
|
|
}
|
2021-03-04 16:22:48 +03:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
size_t V8IsolateImpl::HeapExpansionCallback(void* pvData, size_t currentLimit, size_t /*initialLimit*/)
|
|
|
|
{
|
2023-04-08 21:24:38 +03:00
|
|
|
const size_t minBump = 1024 * 1024;
|
|
|
|
|
2021-03-04 16:22:48 +03:00
|
|
|
if (pvData)
|
|
|
|
{
|
|
|
|
auto multiplier = static_cast<const V8IsolateImpl*>(pvData)->m_HeapExpansionMultiplier;
|
|
|
|
if (multiplier > 1.0)
|
|
|
|
{
|
2023-04-08 21:24:38 +03:00
|
|
|
auto newLimit = static_cast<size_t>(static_cast<double>(currentLimit) * multiplier);
|
|
|
|
return std::max(newLimit, currentLimit + minBump);
|
2021-03-04 16:22:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return currentLimit;
|
|
|
|
}
|