Skip to content

Commit

Permalink
Merge pull request #23 from ejt1/feat-libuv
Browse files Browse the repository at this point in the history
Small fixes
  • Loading branch information
ejt1 authored Feb 11, 2024
2 parents f74a8dd + 8ebf3d6 commit 124fd80
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
23 changes: 13 additions & 10 deletions src/Spidermonkey Engine/Engine/Script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ bool Script::Start() {
return false;
}

void Script::Stop(bool force) {
void Script::Stop() {
// make sure the script is actually running
if (m_scriptState != kScriptStateRunning)
return;
Expand All @@ -96,12 +96,10 @@ void Script::Stop(bool force) {
// trigger call back so script ends
SetEvent(m_eventSignal);

// normal wait: 500ms, forced wait: 300ms, really forced wait: 100ms
int maxCount = (force ? 10 : 30);
if (GetCurrentThreadId() != GetThreadId()) {
for (int i = 0; m_scriptState != kScriptStateStopped; i++) {
// if we pass the time frame, just ignore the wait because the thread will end forcefully anyway
if (i >= maxCount)
if (i >= 30)
break;
Sleep(10);
}
Expand All @@ -127,13 +125,13 @@ void Script::Join() {
}

void Script::Pause(void) {
if (!(m_scriptState == kScriptStateRunning) && !m_isPaused)
if (m_scriptState == kScriptStateRunning && !m_isPaused)
m_isPaused = true;
SetEvent(m_eventSignal);
}

void Script::Resume(void) {
if (!(m_scriptState == kScriptStateRunning) && m_isPaused)
if (m_scriptState == kScriptStateRunning && m_isPaused)
m_isPaused = false;
SetEvent(m_eventSignal);
}
Expand All @@ -143,7 +141,7 @@ bool Script::IsUninitialized() {
}

bool Script::IsRunning(void) {
return /*m_context && */ !(m_scriptState != kScriptStateRunning || m_isPaused);
return m_scriptState == kScriptStateRunning && !m_isPaused;
}

bool Script::IsAborted() {
Expand Down Expand Up @@ -358,7 +356,6 @@ bool Script::Initialize() {
// JS_SetPropertyStr(m_context, console, "error", JS_NewCFunction(m_context, my_print, "error", 1));
// JS_SetPropertyStr(m_context, m_globalObject, "console", console);

// TODO(ejt): this is a solution to minimize changes during migration to quickjs, refactor this in the future
JS_SetPropertyFunctionList(m_context, m_globalObject, global_funcs, _countof(global_funcs));

RegisterBuiltinBindings(m_context);
Expand Down Expand Up @@ -399,16 +396,15 @@ bool Script::Initialize() {
return false;
}

// TODO(ejt): revisit these
m_scriptState = kScriptStateRunning;
m_threadState.lastSpinTime = std::chrono::steady_clock::now();

return true;
}

// TODO(ejt): this is kinda hacky so change this in the future
static void __walk_loop(uv_handle_t* handle, void* /*arg*/) {
// NOTE(ejt): as of writing this, the only uv timers are TimerWrap*
// if this changes in the future fix this
std::vector<TimerWrap*> wraps;
if (handle->type == uv_handle_type::UV_TIMER) {
TimerWrap* wrap = static_cast<TimerWrap*>(handle->data);
Expand Down Expand Up @@ -491,6 +487,7 @@ bool Script::RunEventLoop() {
if (pause)
SetPauseState(false);

m_threadState.lastSpinTime = std::chrono::steady_clock::now();
uv_run(&m_loop, UV_RUN_NOWAIT);
JSContext* ctx;
for (;;) {
Expand Down Expand Up @@ -838,6 +835,12 @@ int Script::InterruptHandler(JSRuntime* rt, void* /*opaque*/) {
ThreadState* ts = (ThreadState*)JS_GetRuntimeOpaque(rt);
Script* script = ts->script;

//auto t = std::chrono::steady_clock::now() - ts->lastSpinTime;
//auto tms = std::chrono::duration_cast<std::chrono::milliseconds>(t);
//if (tms.count() > 5000) {
// // interrupt the script if it stalls for more than 5 seconds without yielding to event loop
// return 1;
//}
if (!script->RunEventLoop()) {
return 1;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Spidermonkey Engine/Engine/Script.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ struct ThreadState {
uv_loop_t* loop;
DWORD id;
HANDLE handle;
std::chrono::steady_clock::time_point lastSpinTime;
};

class Script {
Expand All @@ -43,7 +44,7 @@ class Script {
Script& operator=(const Script&) = delete;

bool Start();
void Stop(bool force = false);
void Stop();
void Run();

void Join(void);
Expand Down Expand Up @@ -125,7 +126,7 @@ class Script {
HANDLE m_eventSignal;
std::list<std::shared_ptr<Event>> m_EventList;

bool m_isPaused, m_isReallyPaused;
std::atomic_bool m_isPaused, m_isReallyPaused;

IncludeList m_includes, m_inProgress;

Expand Down
8 changes: 4 additions & 4 deletions src/Spidermonkey Engine/Engine/ScriptEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ bool __fastcall DisposeScript(Script* script, void*) {
return true;
}

bool __fastcall StopScript(Script* script, void* argv) {
bool __fastcall StopScript(Script* script, void* /*argv*/) {
if (script->GetMode() != kScriptModeCommand)
script->Stop(*(bool*)(argv));
script->Stop();
return true;
}

Expand Down Expand Up @@ -53,7 +53,7 @@ void ScriptEngine::Shutdown(void) {
// bring the engine down properly
LockScriptList("Shutdown");
StopAll(true);
m_console->Stop(true);
m_console->Stop(/*true*/);

// clear all scripts now that they're stopped
ForEachScript(::DisposeScript, NULL);
Expand Down Expand Up @@ -189,6 +189,6 @@ void ScriptEngine::UpdateConsole() {

bool __fastcall StopIngameScript(Script* script, void*) {
if (script->GetMode() == kScriptModeGame)
script->Stop(true);
script->Stop(/*true*/);
return true;
}

0 comments on commit 124fd80

Please sign in to comment.