project64/Source/Project64/UserInterface/Debugger/ScriptAPI/ScriptAPI_script.cpp

64 lines
1.5 KiB
C++
Raw Normal View History

#include <stdafx.h>
2022-09-26 02:31:54 +00:00
#include "ScriptAPI.h"
2022-09-26 02:31:54 +00:00
#pragma warning(disable : 4702) // disable unreachable code warning
2022-09-26 02:31:54 +00:00
void ScriptAPI::Define_script(duk_context * ctx)
{
const DukPropListEntry props[] = {
2022-09-26 02:31:54 +00:00
{"timeout", DukCFunction(js_script_timeout)},
{"keepalive", DukCFunction(js_script_keepalive)},
{"abort", DukCFunction(js_script_abort)},
{nullptr},
};
DefineGlobalInterface(ctx, "script", props);
}
2022-09-26 02:31:54 +00:00
duk_ret_t ScriptAPI::js_script_timeout(duk_context * ctx)
{
2022-09-26 02:31:54 +00:00
CheckArgs(ctx, {Arg_Number});
CScriptInstance * inst = GetInstance(ctx);
inst->SetExecTimeout((uint64_t)duk_get_number(ctx, 0));
return 0;
}
2022-09-26 02:31:54 +00:00
duk_ret_t ScriptAPI::js_script_keepalive(duk_context * ctx)
{
2022-09-26 02:31:54 +00:00
CheckArgs(ctx, {Arg_Boolean});
CScriptInstance * inst = GetInstance(ctx);
duk_bool_t bKeepAlive = duk_get_boolean(ctx, 0);
duk_push_global_object(ctx);
duk_bool_t bHaveProp = duk_has_prop_string(ctx, -1, HS_gKeepAlive);
2022-09-26 02:31:54 +00:00
if (bKeepAlive && !bHaveProp)
{
duk_push_boolean(ctx, 1);
duk_put_prop_string(ctx, -2, HS_gKeepAlive);
inst->IncRefCount();
}
2022-09-26 02:31:54 +00:00
else if (!bKeepAlive && bHaveProp)
{
duk_del_prop_string(ctx, -1, HS_gKeepAlive);
inst->DecRefCount();
}
duk_pop(ctx);
return 0;
}
2022-09-26 02:31:54 +00:00
duk_ret_t ScriptAPI::js_script_abort(duk_context * ctx)
{
CheckArgs(ctx, {});
2022-09-26 02:31:54 +00:00
CScriptInstance * inst = GetInstance(ctx);
if (inst->PrepareAbort())
{
return duk_fatal(ctx, "aborted");
}
return 0;
}