2022-09-21 05:16:07 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
#include "ScriptAPI.h"
|
2022-09-21 05:16:07 +00:00
|
|
|
#include <windows.h>
|
2021-09-03 05:11:04 +00:00
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
static void ConcatArgs(duk_context * ctx, stdstr & out);
|
2021-09-03 05:11:04 +00:00
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
void ScriptAPI::Define_console(duk_context * ctx)
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
|
|
|
const DukPropListEntry props[] = {
|
2022-09-21 05:16:07 +00:00
|
|
|
{"print", DukCFunction(js_console_print)},
|
|
|
|
{"log", DukCFunction(js_console_log)},
|
|
|
|
{"error", DukCFunction(js_console_error)},
|
|
|
|
{"clear", DukCFunction(js_console_clear)},
|
|
|
|
{"listen", DukCFunction(js_console_listen)},
|
2022-09-26 02:31:54 +00:00
|
|
|
{nullptr},
|
2021-09-03 05:11:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
DefineGlobalInterface(ctx, "console", props);
|
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
duk_ret_t ScriptAPI::js_console_print(duk_context * ctx)
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
|
|
|
stdstr out;
|
|
|
|
ConcatArgs(ctx, out);
|
|
|
|
GetInstance(ctx)->System()->ConsolePrint("%s", out.c_str());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
duk_ret_t ScriptAPI::js_console_log(duk_context * ctx)
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
|
|
|
stdstr out;
|
|
|
|
ConcatArgs(ctx, out);
|
|
|
|
GetInstance(ctx)->System()->ConsoleLog("%s", out.c_str());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
duk_ret_t ScriptAPI::js_console_error(duk_context * ctx)
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
2022-09-21 05:16:07 +00:00
|
|
|
CheckArgs(ctx, {Arg_OptAny});
|
2021-09-03 05:11:04 +00:00
|
|
|
|
|
|
|
if (duk_is_error(ctx, 0))
|
|
|
|
{
|
|
|
|
duk_get_prop_string(ctx, 0, "stack");
|
2022-09-21 05:16:07 +00:00
|
|
|
const char * message = duk_get_string(ctx, -1);
|
2021-09-03 05:11:04 +00:00
|
|
|
GetInstance(ctx)->System()->ConsoleLog("%s", message);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
GetInstance(ctx)->System()->ConsoleLog("%s", duk_safe_to_string(ctx, 0));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
duk_ret_t ScriptAPI::js_console_clear(duk_context * ctx)
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
|
|
|
CheckArgs(ctx, {});
|
|
|
|
GetInstance(ctx)->System()->ConsoleClear();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
duk_ret_t ScriptAPI::js_console_listen(duk_context * ctx)
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
2022-09-21 05:16:07 +00:00
|
|
|
CScriptInstance * inst = GetInstance(ctx);
|
2021-09-03 05:11:04 +00:00
|
|
|
|
|
|
|
duk_push_global_object(ctx);
|
|
|
|
duk_bool_t haveListener = duk_has_prop_string(ctx, -1, HS_gInputListener);
|
|
|
|
|
|
|
|
if (duk_is_function(ctx, 0))
|
|
|
|
{
|
|
|
|
duk_pull(ctx, 0);
|
|
|
|
duk_put_prop_string(ctx, -2, HS_gInputListener);
|
|
|
|
if (!haveListener)
|
|
|
|
{
|
|
|
|
inst->IncRefCount();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (duk_is_null(ctx, 0))
|
|
|
|
{
|
|
|
|
if (haveListener)
|
|
|
|
{
|
|
|
|
duk_del_prop_string(ctx, -1, HS_gInputListener);
|
|
|
|
inst->DecRefCount();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ThrowInvalidArgsError(ctx);
|
|
|
|
}
|
|
|
|
|
2022-09-21 05:16:07 +00:00
|
|
|
void ConcatArgs(duk_context * ctx, stdstr & out)
|
2021-09-03 05:11:04 +00:00
|
|
|
{
|
|
|
|
out = "";
|
|
|
|
duk_idx_t nargs = duk_get_top(ctx);
|
2022-09-21 05:16:07 +00:00
|
|
|
|
2021-09-03 05:11:04 +00:00
|
|
|
// note: global JSON.stringify must be intact
|
|
|
|
|
|
|
|
duk_get_global_string(ctx, "JSON");
|
|
|
|
duk_get_prop_string(ctx, -1, "stringify");
|
|
|
|
duk_remove(ctx, -2);
|
|
|
|
|
|
|
|
for (duk_idx_t n = 0; n < nargs; n++)
|
|
|
|
{
|
|
|
|
if (n != 0)
|
|
|
|
{
|
|
|
|
out += " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (duk_is_object(ctx, n))
|
|
|
|
{
|
|
|
|
duk_dup(ctx, n);
|
|
|
|
out += duk_safe_to_string(ctx, -1);
|
|
|
|
out += " ";
|
|
|
|
duk_pop(ctx);
|
|
|
|
duk_dup(ctx, -1);
|
|
|
|
duk_dup(ctx, n);
|
|
|
|
duk_push_null(ctx);
|
|
|
|
duk_push_int(ctx, 2);
|
|
|
|
duk_pcall(ctx, 3);
|
|
|
|
out += duk_safe_to_string(ctx, -1);
|
|
|
|
duk_pop(ctx);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
out += duk_safe_to_string(ctx, n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|