2022-09-21 05:16:07 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
#include "ScriptAPI/ScriptAPI.h"
|
2022-09-21 05:16:07 +00:00
|
|
|
#include "ScriptInstance.h"
|
|
|
|
#include "ScriptTypes.h"
|
2021-09-03 05:11:04 +00:00
|
|
|
#include <sys/stat.h>
|
2017-08-18 05:08:22 +00:00
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
int DukTimeoutCheck(void * udata)
|
2017-09-13 12:00:53 +00:00
|
|
|
{
|
2022-09-21 05:16:07 +00:00
|
|
|
CScriptInstance * inst = (CScriptInstance *)udata;
|
2021-09-03 05:11:04 +00:00
|
|
|
return (int)inst->IsTimedOut();
|
2017-09-13 12:00:53 +00:00
|
|
|
}
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
CScriptInstance::CScriptInstance(CScriptSystem * sys, const char * name) :
|
2021-09-03 05:11:04 +00:00
|
|
|
m_System(sys),
|
|
|
|
m_InstanceName(name),
|
|
|
|
m_Ctx(nullptr),
|
|
|
|
m_RefCount(0),
|
|
|
|
m_ExecTimeout(JS_EXEC_TIMEOUT),
|
|
|
|
m_ExecStartTime(0),
|
|
|
|
m_SourceCode(nullptr),
|
|
|
|
m_CurExecCallbackId(JS_INVALID_CALLBACK),
|
2022-01-13 20:39:21 +00:00
|
|
|
m_bStopping(false),
|
|
|
|
m_bAborting(false)
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CScriptInstance::~CScriptInstance()
|
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
Cleanup();
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
std::string & CScriptInstance::Name()
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
return m_InstanceName;
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
CScriptSystem * CScriptInstance::System()
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
return m_System;
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
CDebuggerUI * CScriptInstance::Debugger()
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
return m_System->Debugger();
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
JSAppCallbackID CScriptInstance::CallbackId()
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
return m_CurExecCallbackId;
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
bool CScriptInstance::Run(const char * path)
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2022-09-21 05:16:07 +00:00
|
|
|
if (m_Ctx != nullptr)
|
2017-09-13 12:00:53 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
return false;
|
2017-09-13 12:00:53 +00:00
|
|
|
}
|
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
m_Ctx = duk_create_heap(nullptr, nullptr, nullptr, this, nullptr);
|
2017-09-13 12:00:53 +00:00
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
if (m_Ctx == nullptr)
|
2017-09-13 12:00:53 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
goto error_cleanup;
|
2017-09-13 12:00:53 +00:00
|
|
|
}
|
2021-09-03 05:11:04 +00:00
|
|
|
|
|
|
|
struct stat statBuf;
|
2022-09-21 05:16:07 +00:00
|
|
|
if (stat(path, &statBuf) != 0)
|
2017-09-13 12:00:53 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
m_System->ConsoleLog("[SCRIPTSYS]: error: could not stat '%s'", path);
|
|
|
|
goto error_cleanup;
|
2017-09-13 12:00:53 +00:00
|
|
|
}
|
2018-02-18 21:37:03 +00:00
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
m_SourceCode = new char[statBuf.st_size + 1];
|
|
|
|
m_SourceCode[statBuf.st_size] = '\0';
|
2017-08-18 05:08:22 +00:00
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
m_SourceFile.open(path, std::ios::in | std::ios::binary);
|
2022-09-21 05:16:07 +00:00
|
|
|
if (!m_SourceFile.is_open())
|
2017-09-13 12:00:53 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
m_System->ConsoleLog("[SCRIPTSYS]: error: could not open '%s'", path);
|
|
|
|
goto error_cleanup;
|
|
|
|
}
|
2017-08-18 05:08:22 +00:00
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
m_SourceFile.read(m_SourceCode, statBuf.st_size);
|
2017-08-18 05:08:22 +00:00
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
if ((size_t)m_SourceFile.tellg() != (size_t)statBuf.st_size)
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
|
|
|
m_System->ConsoleLog("[SCRIPTSYS]: error: could not read '%s'", path);
|
|
|
|
goto error_cleanup;
|
2017-09-13 12:00:53 +00:00
|
|
|
}
|
2017-08-18 05:08:22 +00:00
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
m_ExecStartTime = Timestamp();
|
2017-08-18 05:08:22 +00:00
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
ScriptAPI::InitEnvironment(m_Ctx, this);
|
2017-08-18 05:08:22 +00:00
|
|
|
|
2022-01-13 20:39:21 +00:00
|
|
|
try
|
2017-09-13 12:00:53 +00:00
|
|
|
{
|
2022-01-13 20:39:21 +00:00
|
|
|
duk_push_string(m_Ctx, m_InstanceName.c_str());
|
2022-09-26 02:31:54 +00:00
|
|
|
if (duk_pcompile_string_filename(m_Ctx, DUK_COMPILE_STRICT, m_SourceCode) != 0 || duk_pcall(m_Ctx, 0) == DUK_EXEC_ERROR)
|
2022-01-13 20:39:21 +00:00
|
|
|
{
|
|
|
|
duk_get_prop_string(m_Ctx, -1, "stack");
|
|
|
|
m_System->ConsoleLog("%s", duk_safe_to_string(m_Ctx, -1));
|
|
|
|
duk_pop_n(m_Ctx, 2);
|
|
|
|
goto error_cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
duk_pop(m_Ctx);
|
|
|
|
}
|
2022-09-21 05:16:07 +00:00
|
|
|
catch (std::runtime_error & exc)
|
2022-01-13 20:39:21 +00:00
|
|
|
{
|
|
|
|
FatalHandler(exc);
|
2017-09-13 12:00:53 +00:00
|
|
|
}
|
2017-08-18 05:08:22 +00:00
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
return true;
|
2017-08-18 05:08:22 +00:00
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
error_cleanup:
|
|
|
|
Cleanup();
|
|
|
|
return false;
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
void CScriptInstance::IncRefCount()
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
m_RefCount++;
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
void CScriptInstance::DecRefCount()
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2022-09-21 05:16:07 +00:00
|
|
|
if (m_RefCount > 0)
|
2017-09-13 12:00:53 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
m_RefCount--;
|
2017-09-13 12:00:53 +00:00
|
|
|
}
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
void CScriptInstance::SetStopping(bool bStopping)
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
m_bStopping = bStopping;
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 20:39:21 +00:00
|
|
|
bool CScriptInstance::PrepareAbort()
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2022-01-13 20:39:21 +00:00
|
|
|
if (!m_bAborting)
|
|
|
|
{
|
|
|
|
m_bAborting = true;
|
|
|
|
m_RefCount = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-08-18 05:08:22 +00:00
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
void CScriptInstance::RawCMethodCall(void * dukThisHeapPtr, duk_c_function func, JSDukArgSetupFunc argSetupFunc, void * argSetupParam)
|
2022-01-13 20:39:21 +00:00
|
|
|
{
|
|
|
|
try
|
2019-12-25 00:41:20 +00:00
|
|
|
{
|
2022-01-13 20:39:21 +00:00
|
|
|
m_ExecStartTime = Timestamp();
|
|
|
|
duk_push_c_function(m_Ctx, func, DUK_VARARGS);
|
|
|
|
duk_push_heapptr(m_Ctx, dukThisHeapPtr);
|
|
|
|
duk_idx_t nargs = argSetupFunc ? argSetupFunc(m_Ctx, argSetupParam) : 0;
|
|
|
|
|
|
|
|
if (duk_pcall_method(m_Ctx, nargs) == DUK_EXEC_ERROR)
|
|
|
|
{
|
|
|
|
duk_get_prop_string(m_Ctx, -1, "stack");
|
|
|
|
m_System->ConsoleLog("%s", duk_safe_to_string(m_Ctx, -1));
|
|
|
|
duk_pop(m_Ctx);
|
|
|
|
}
|
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
duk_pop(m_Ctx);
|
2019-12-25 00:41:20 +00:00
|
|
|
}
|
2022-09-21 05:16:07 +00:00
|
|
|
catch (std::runtime_error & exc)
|
2022-01-13 20:39:21 +00:00
|
|
|
{
|
|
|
|
FatalHandler(exc);
|
|
|
|
}
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
void CScriptInstance::PostCMethodCall(void * dukThisHeapPtr, duk_c_function func, JSDukArgSetupFunc argSetupFunc,
|
|
|
|
void * argSetupParam, size_t argSetupParamSize)
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
m_System->PostCMethodCall(m_InstanceName.c_str(), dukThisHeapPtr, func, argSetupFunc, argSetupParam, argSetupParamSize);
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
void CScriptInstance::RawConsoleInput(const char * code)
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
|
|
|
m_System->ConsoleLog("> %s", code);
|
2022-09-21 05:16:07 +00:00
|
|
|
|
2022-01-13 20:39:21 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
duk_get_global_string(m_Ctx, HS_gInputListener);
|
2017-09-13 12:00:53 +00:00
|
|
|
|
2022-01-13 20:39:21 +00:00
|
|
|
if (duk_is_function(m_Ctx, -1))
|
|
|
|
{
|
|
|
|
m_ExecStartTime = Timestamp();
|
|
|
|
duk_push_string(m_Ctx, code);
|
|
|
|
if (duk_pcall(m_Ctx, 1) != DUK_EXEC_SUCCESS)
|
|
|
|
{
|
|
|
|
duk_get_prop_string(m_Ctx, -1, "stack");
|
|
|
|
m_System->ConsoleLog("%s", duk_safe_to_string(m_Ctx, -1));
|
|
|
|
duk_pop_n(m_Ctx, 2);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
duk_pop(m_Ctx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
duk_pop(m_Ctx);
|
2017-09-13 12:00:53 +00:00
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
m_ExecStartTime = Timestamp();
|
2022-01-13 20:39:21 +00:00
|
|
|
|
|
|
|
duk_push_string(m_Ctx, stdstr_f("<input:%s>", m_InstanceName.c_str()).c_str());
|
2022-09-26 02:31:54 +00:00
|
|
|
if (duk_pcompile_string_filename(m_Ctx, DUK_COMPILE_STRICT, code) != 0 || duk_pcall(m_Ctx, 0) == DUK_EXEC_ERROR)
|
2017-09-13 12:00:53 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
duk_get_prop_string(m_Ctx, -1, "stack");
|
|
|
|
m_System->ConsoleLog("%s", duk_safe_to_string(m_Ctx, -1));
|
2022-01-13 20:39:21 +00:00
|
|
|
duk_pop(m_Ctx);
|
2017-09-13 12:00:53 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-01-13 20:39:21 +00:00
|
|
|
if (duk_is_string(m_Ctx, -1))
|
|
|
|
{
|
|
|
|
m_System->ConsoleLog("\"%s\"", duk_get_string(m_Ctx, -1));
|
|
|
|
}
|
|
|
|
else if (duk_is_object(m_Ctx, -1))
|
|
|
|
{
|
|
|
|
duk_dup(m_Ctx, -1);
|
|
|
|
duk_get_global_string(m_Ctx, "JSON");
|
|
|
|
duk_get_prop_string(m_Ctx, -1, "stringify");
|
|
|
|
duk_remove(m_Ctx, -2);
|
|
|
|
duk_pull(m_Ctx, -3);
|
|
|
|
duk_push_null(m_Ctx);
|
|
|
|
duk_push_int(m_Ctx, 2);
|
|
|
|
duk_pcall(m_Ctx, 3);
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
const char * str = duk_safe_to_string(m_Ctx, -2);
|
|
|
|
const char * res = duk_get_string(m_Ctx, -1);
|
2022-01-13 20:39:21 +00:00
|
|
|
|
|
|
|
m_System->ConsoleLog("%s %s", str, res);
|
|
|
|
duk_pop(m_Ctx);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_System->ConsoleLog("%s", duk_safe_to_string(m_Ctx, -1));
|
|
|
|
}
|
2017-09-13 12:00:53 +00:00
|
|
|
}
|
2021-09-03 05:11:04 +00:00
|
|
|
duk_pop(m_Ctx);
|
2017-09-13 12:00:53 +00:00
|
|
|
}
|
2022-09-21 05:16:07 +00:00
|
|
|
catch (std::runtime_error & exc)
|
2017-09-13 12:00:53 +00:00
|
|
|
{
|
2022-01-13 20:39:21 +00:00
|
|
|
FatalHandler(exc);
|
2018-02-18 21:37:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
void CScriptInstance::SetExecTimeout(uint64_t timeout)
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
m_ExecTimeout = timeout;
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
bool CScriptInstance::IsTimedOut()
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2022-09-21 05:16:07 +00:00
|
|
|
if (m_ExecStartTime == 0 || m_ExecTimeout == 0)
|
2017-09-13 12:00:53 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
return false;
|
2017-09-13 12:00:53 +00:00
|
|
|
}
|
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
uint64_t timeElapsed = Timestamp() - m_ExecStartTime;
|
|
|
|
return (timeElapsed >= m_ExecTimeout);
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
uint64_t CScriptInstance::Timestamp()
|
2018-12-09 21:24:11 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
FILETIME ft;
|
|
|
|
GetSystemTimeAsFileTime(&ft);
|
2018-12-09 21:24:11 +00:00
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
ULARGE_INTEGER li;
|
|
|
|
li.LowPart = ft.dwLowDateTime;
|
|
|
|
li.HighPart = ft.dwHighDateTime;
|
2018-12-09 21:24:11 +00:00
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
return li.QuadPart / 10000;
|
2018-12-09 21:24:11 +00:00
|
|
|
}
|
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
void CScriptInstance::Cleanup()
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2022-09-21 05:16:07 +00:00
|
|
|
if (m_Ctx != nullptr)
|
2017-09-13 12:00:53 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
duk_destroy_heap(m_Ctx);
|
|
|
|
m_Ctx = nullptr;
|
2017-09-13 12:00:53 +00:00
|
|
|
}
|
2022-09-21 05:16:07 +00:00
|
|
|
if (m_SourceCode != nullptr)
|
2017-09-13 12:00:53 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
delete[] m_SourceCode;
|
|
|
|
m_SourceCode = nullptr;
|
2017-09-13 12:00:53 +00:00
|
|
|
}
|
2022-09-21 05:16:07 +00:00
|
|
|
if (m_SourceFile.is_open())
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
|
|
|
m_SourceFile.close();
|
2019-12-25 00:41:20 +00:00
|
|
|
}
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
bool CScriptInstance::RegisterWorker(CScriptWorker * worker)
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
if (IsStopping())
|
2017-09-13 12:00:53 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
return false;
|
2017-09-13 12:00:53 +00:00
|
|
|
}
|
2017-08-18 05:08:22 +00:00
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
m_Workers.push_back(worker);
|
|
|
|
return true;
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
void CScriptInstance::UnregisterWorker(CScriptWorker * worker)
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2022-09-21 05:16:07 +00:00
|
|
|
std::vector<CScriptWorker *>::iterator it;
|
2022-01-12 20:36:30 +00:00
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
for (it = m_Workers.begin(); it != m_Workers.end(); it++)
|
2017-09-13 12:00:53 +00:00
|
|
|
{
|
2021-09-03 05:11:04 +00:00
|
|
|
if (*it == worker)
|
|
|
|
{
|
|
|
|
m_Workers.erase(it);
|
|
|
|
return;
|
|
|
|
}
|
2017-09-13 12:00:53 +00:00
|
|
|
}
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
void CScriptInstance::StopRegisteredWorkers()
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2022-09-21 05:16:07 +00:00
|
|
|
std::vector<CScriptWorker *>::iterator it;
|
2021-09-03 05:11:04 +00:00
|
|
|
for (it = m_Workers.begin(); it != m_Workers.end(); it++)
|
2017-09-13 12:00:53 +00:00
|
|
|
{
|
2022-09-21 05:16:07 +00:00
|
|
|
CScriptWorker * worker = *it;
|
2021-09-03 05:11:04 +00:00
|
|
|
worker->StopWorkerProc();
|
2017-09-13 12:00:53 +00:00
|
|
|
}
|
2021-03-18 03:54:59 +00:00
|
|
|
}
|
2022-01-13 20:39:21 +00:00
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
void CScriptInstance::FatalHandler(std::runtime_error & exc)
|
2022-01-13 20:39:21 +00:00
|
|
|
{
|
|
|
|
if (m_bAborting)
|
|
|
|
{
|
|
|
|
m_System->ConsoleLog("[SCRIPTSYS]: '%s' aborted", m_InstanceName.c_str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_System->ConsoleLog("%s", exc.what());
|
|
|
|
g_Notify->BreakPoint(__FILE__, __LINE__);
|
|
|
|
}
|
|
|
|
}
|