2021-09-03 05:11:04 +00:00
|
|
|
#include <stdafx.h>
|
2022-09-26 02:31:54 +00:00
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
#include "ScriptAPI.h"
|
|
|
|
|
2022-09-26 02:31:54 +00:00
|
|
|
#pragma warning(disable : 4702) // disable unreachable code warning
|
2022-01-13 20:39:21 +00:00
|
|
|
|
2022-09-26 02:31:54 +00:00
|
|
|
void ScriptAPI::Define_script(duk_context * ctx)
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
|
|
|
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},
|
2021-09-03 05:11:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
DefineGlobalInterface(ctx, "script", props);
|
|
|
|
}
|
|
|
|
|
2022-09-26 02:31:54 +00:00
|
|
|
duk_ret_t ScriptAPI::js_script_timeout(duk_context * ctx)
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
2022-09-26 02:31:54 +00:00
|
|
|
CheckArgs(ctx, {Arg_Number});
|
|
|
|
CScriptInstance * inst = GetInstance(ctx);
|
2021-09-03 05:11:04 +00:00
|
|
|
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)
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
2022-09-26 02:31:54 +00:00
|
|
|
CheckArgs(ctx, {Arg_Boolean});
|
|
|
|
CScriptInstance * inst = GetInstance(ctx);
|
2021-09-03 05:11:04 +00:00
|
|
|
|
|
|
|
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)
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
|
|
|
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)
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
|
|
|
duk_del_prop_string(ctx, -1, HS_gKeepAlive);
|
|
|
|
inst->DecRefCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
duk_pop(ctx);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2022-01-13 20:39:21 +00:00
|
|
|
|
2022-09-26 02:31:54 +00:00
|
|
|
duk_ret_t ScriptAPI::js_script_abort(duk_context * ctx)
|
2022-01-13 20:39:21 +00:00
|
|
|
{
|
|
|
|
CheckArgs(ctx, {});
|
2022-09-26 02:31:54 +00:00
|
|
|
CScriptInstance * inst = GetInstance(ctx);
|
2022-01-13 20:39:21 +00:00
|
|
|
if (inst->PrepareAbort())
|
|
|
|
{
|
|
|
|
return duk_fatal(ctx, "aborted");
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|