project64/Source/Project64/UserInterface/Debugger/ScriptInstance.cpp

349 lines
7.9 KiB
C++
Raw Normal View History

2017-08-18 05:08:22 +00:00
#include <stdafx.h>
#include "ScriptTypes.h"
2017-08-18 05:08:22 +00:00
#include "ScriptInstance.h"
#include "ScriptAPI/ScriptAPI.h"
#include <sys/stat.h>
2017-08-18 05:08:22 +00:00
extern "C" {
int DukTimeoutCheck(void* udata)
{
CScriptInstance* inst = (CScriptInstance*)udata;
return (int)inst->IsTimedOut();
}
2017-08-18 05:08:22 +00:00
}
CScriptInstance::CScriptInstance(CScriptSystem* sys, const char* name) :
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),
m_bStopping(false),
m_bAborting(false)
2017-08-18 05:08:22 +00:00
{
}
CScriptInstance::~CScriptInstance()
{
Cleanup();
2017-08-18 05:08:22 +00:00
}
std::string& CScriptInstance::Name()
2017-08-18 05:08:22 +00:00
{
return m_InstanceName;
2017-08-18 05:08:22 +00:00
}
CScriptSystem* CScriptInstance::System()
2017-08-18 05:08:22 +00:00
{
return m_System;
2017-08-18 05:08:22 +00:00
}
CDebuggerUI* CScriptInstance::Debugger()
2017-08-18 05:08:22 +00:00
{
return m_System->Debugger();
2017-08-18 05:08:22 +00:00
}
JSAppCallbackID CScriptInstance::CallbackId()
2017-08-18 05:08:22 +00:00
{
return m_CurExecCallbackId;
2017-08-18 05:08:22 +00:00
}
bool CScriptInstance::Run(const char* path)
2017-08-18 05:08:22 +00:00
{
if(m_Ctx != nullptr)
{
return false;
}
m_Ctx = duk_create_heap(nullptr, nullptr, nullptr, this, nullptr);
if(m_Ctx == nullptr)
{
goto error_cleanup;
}
struct stat statBuf;
if(stat(path, &statBuf) != 0)
{
m_System->ConsoleLog("[SCRIPTSYS]: error: could not stat '%s'", path);
goto error_cleanup;
}
m_SourceCode = new char[statBuf.st_size + 1];
m_SourceCode[statBuf.st_size] = '\0';
2017-08-18 05:08:22 +00:00
m_SourceFile.open(path, std::ios::in | std::ios::binary);
if(!m_SourceFile.is_open())
{
m_System->ConsoleLog("[SCRIPTSYS]: error: could not open '%s'", path);
goto error_cleanup;
}
2017-08-18 05:08:22 +00:00
m_SourceFile.read(m_SourceCode, statBuf.st_size);
2017-08-18 05:08:22 +00:00
if((size_t)m_SourceFile.tellg() != (size_t)statBuf.st_size)
{
m_System->ConsoleLog("[SCRIPTSYS]: error: could not read '%s'", path);
goto error_cleanup;
}
2017-08-18 05:08:22 +00:00
m_ExecStartTime = Timestamp();
2017-08-18 05:08:22 +00:00
ScriptAPI::InitEnvironment(m_Ctx, this);
2017-08-18 05:08:22 +00:00
try
{
duk_push_string(m_Ctx, m_InstanceName.c_str());
if (duk_pcompile_string_filename(m_Ctx, DUK_COMPILE_STRICT, m_SourceCode) != 0 ||
duk_pcall(m_Ctx, 0) == DUK_EXEC_ERROR)
{
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);
}
catch (std::runtime_error& exc)
{
FatalHandler(exc);
}
2017-08-18 05:08:22 +00:00
return true;
2017-08-18 05:08:22 +00:00
error_cleanup:
Cleanup();
return false;
2017-08-18 05:08:22 +00:00
}
void CScriptInstance::IncRefCount()
2017-08-18 05:08:22 +00:00
{
m_RefCount++;
2017-08-18 05:08:22 +00:00
}
void CScriptInstance::DecRefCount()
2017-08-18 05:08:22 +00:00
{
if(m_RefCount > 0)
{
m_RefCount--;
}
2017-08-18 05:08:22 +00:00
}
void CScriptInstance::SetStopping(bool bStopping)
2017-08-18 05:08:22 +00:00
{
m_bStopping = bStopping;
2017-08-18 05:08:22 +00:00
}
bool CScriptInstance::PrepareAbort()
2017-08-18 05:08:22 +00:00
{
if (!m_bAborting)
{
m_bAborting = true;
m_RefCount = 0;
return true;
}
return false;
}
2017-08-18 05:08:22 +00:00
void CScriptInstance::RawCMethodCall(void* dukThisHeapPtr, duk_c_function func, JSDukArgSetupFunc argSetupFunc, void *argSetupParam)
{
try
2019-12-25 00:41:20 +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);
}
duk_pop(m_Ctx);
2019-12-25 00:41:20 +00:00
}
catch (std::runtime_error& exc)
{
FatalHandler(exc);
}
2017-08-18 05:08:22 +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
{
m_System->PostCMethodCall(m_InstanceName.c_str(), dukThisHeapPtr, func, argSetupFunc, argSetupParam, argSetupParamSize);
2017-08-18 05:08:22 +00:00
}
void CScriptInstance::RawConsoleInput(const char* code)
{
m_System->ConsoleLog("> %s", code);
try
{
duk_get_global_string(m_Ctx, HS_gInputListener);
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);
m_ExecStartTime = Timestamp();
duk_push_string(m_Ctx, stdstr_f("<input:%s>", m_InstanceName.c_str()).c_str());
if (duk_pcompile_string_filename(m_Ctx, DUK_COMPILE_STRICT, code) != 0 ||
duk_pcall(m_Ctx, 0) == 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);
}
else
{
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);
const char* str = duk_safe_to_string(m_Ctx, -2);
const char* res = duk_get_string(m_Ctx, -1);
m_System->ConsoleLog("%s %s", str, res);
duk_pop(m_Ctx);
}
else
{
m_System->ConsoleLog("%s", duk_safe_to_string(m_Ctx, -1));
}
}
duk_pop(m_Ctx);
}
catch (std::runtime_error& exc)
{
FatalHandler(exc);
}
}
void CScriptInstance::SetExecTimeout(uint64_t timeout)
2017-08-18 05:08:22 +00:00
{
m_ExecTimeout = timeout;
2017-08-18 05:08:22 +00:00
}
bool CScriptInstance::IsTimedOut()
2017-08-18 05:08:22 +00:00
{
if(m_ExecStartTime == 0 || m_ExecTimeout == 0)
{
return false;
}
uint64_t timeElapsed = Timestamp() - m_ExecStartTime;
return (timeElapsed >= m_ExecTimeout);
2017-08-18 05:08:22 +00:00
}
uint64_t CScriptInstance::Timestamp()
{
FILETIME ft;
GetSystemTimeAsFileTime(&ft);
ULARGE_INTEGER li;
li.LowPart = ft.dwLowDateTime;
li.HighPart = ft.dwHighDateTime;
return li.QuadPart / 10000;
}
void CScriptInstance::Cleanup()
2017-08-18 05:08:22 +00:00
{
if(m_Ctx != nullptr)
{
duk_destroy_heap(m_Ctx);
m_Ctx = nullptr;
}
if(m_SourceCode != nullptr)
{
delete[] m_SourceCode;
m_SourceCode = nullptr;
}
if(m_SourceFile.is_open())
{
m_SourceFile.close();
2019-12-25 00:41:20 +00:00
}
2017-08-18 05:08:22 +00:00
}
bool CScriptInstance::RegisterWorker(CScriptWorker* worker)
2017-08-18 05:08:22 +00:00
{
if (IsStopping())
{
return false;
}
2017-08-18 05:08:22 +00:00
m_Workers.push_back(worker);
return true;
2017-08-18 05:08:22 +00:00
}
void CScriptInstance::UnregisterWorker(CScriptWorker* worker)
2017-08-18 05:08:22 +00:00
{
std::vector<CScriptWorker*>::iterator it;
for (it = m_Workers.begin(); it != m_Workers.end(); it++)
{
if (*it == worker)
{
m_Workers.erase(it);
return;
}
}
2017-08-18 05:08:22 +00:00
}
void CScriptInstance::StopRegisteredWorkers()
2017-08-18 05:08:22 +00:00
{
std::vector<CScriptWorker*>::iterator it;
for (it = m_Workers.begin(); it != m_Workers.end(); it++)
{
CScriptWorker* worker = *it;
worker->StopWorkerProc();
}
}
void CScriptInstance::FatalHandler(std::runtime_error& exc)
{
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__);
}
}