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

116 lines
3.1 KiB
C++
Raw Normal View History

2022-09-21 05:16:07 +00:00
#include "stdafx.h"
#include "JSServerWorker.h"
2022-09-21 05:16:07 +00:00
#include "ScriptAPI.h"
2022-09-21 05:16:07 +00:00
static CJSServerWorker * GetThisServer(duk_context * ctx);
2022-09-21 05:16:07 +00:00
void ScriptAPI::Define_Server(duk_context * ctx)
{
const DukPropListEntry prototype[] = {
2022-09-21 05:16:07 +00:00
{"listen", DukCFunction(js_Server_listen)},
{"close", DukCFunction(js_Server_close)},
{"on", DukCFunction(js__Emitter_on)},
{"off", DukCFunction(js__Emitter_off)},
{"port", DukGetter(js_Server__get_port)},
{"address", DukGetter(js_Server__get_address)},
{"addressFamily", DukGetter(js_Server__get_addressFamily)},
2022-09-26 02:31:54 +00:00
{nullptr},
};
2022-09-26 02:31:54 +00:00
DefineGlobalClass(ctx, "Server", js_Server__constructor, prototype);
}
2022-09-21 05:16:07 +00:00
duk_ret_t ScriptAPI::js_Server__constructor(duk_context * ctx)
{
CheckArgs(ctx, {});
if (!duk_is_constructor_call(ctx))
{
return DUK_RET_ERROR;
}
2022-09-21 05:16:07 +00:00
CScriptInstance * inst = GetInstance(ctx);
duk_push_this(ctx);
2022-09-21 05:16:07 +00:00
void * objectHeapPtr = duk_get_heapptr(ctx, -1);
InitEmitter(ctx, -1, {"close", "connection", "error", "listening"});
duk_push_c_function(ctx, js_Server__finalizer, 1);
duk_set_finalizer(ctx, -2);
2022-09-21 05:16:07 +00:00
CJSServerWorker * serverWorker = new CJSServerWorker(inst, objectHeapPtr);
duk_push_pointer(ctx, serverWorker);
duk_put_prop_string(ctx, -2, HS_serverWorkerPtr);
return 0;
}
2022-09-21 05:16:07 +00:00
duk_ret_t ScriptAPI::js_Server__finalizer(duk_context * ctx)
{
UnrefObject(ctx, 0);
duk_get_prop_string(ctx, 0, HS_serverWorkerPtr);
2022-09-21 05:16:07 +00:00
CJSServerWorker * serverWorker = (CJSServerWorker *)duk_get_pointer(ctx, -1);
delete serverWorker;
return 0;
}
2022-09-21 05:16:07 +00:00
duk_ret_t ScriptAPI::js_Server_listen(duk_context * ctx)
{
2022-09-21 05:16:07 +00:00
CheckArgs(ctx, {Arg_Number, Arg_OptString, Arg_OptFunction});
CJSServerWorker * serverWorker = GetThisServer(ctx);
unsigned short port = (unsigned short)duk_get_int(ctx, 0);
2022-09-21 05:16:07 +00:00
const char * address = duk_get_string_default(ctx, 1, "0.0.0.0");
// todo callback
duk_push_this(ctx);
RefObject(ctx, -1);
serverWorker->Init(address, port);
serverWorker->StartWorkerProc();
return 0;
}
2022-09-21 05:16:07 +00:00
duk_ret_t ScriptAPI::js_Server_close(duk_context * ctx)
{
CheckArgs(ctx, {});
2022-09-21 05:16:07 +00:00
CJSServerWorker * serverWorker = GetThisServer(ctx);
serverWorker->StopWorkerProc();
return 0;
}
2022-09-21 05:16:07 +00:00
duk_ret_t ScriptAPI::js_Server__get_port(duk_context * ctx)
{
2022-09-21 05:16:07 +00:00
CJSServerWorker * serverWorker = GetThisServer(ctx);
duk_push_uint(ctx, serverWorker->GetPort());
return 1;
}
2022-09-21 05:16:07 +00:00
duk_ret_t ScriptAPI::js_Server__get_address(duk_context * ctx)
{
2022-09-21 05:16:07 +00:00
CJSServerWorker * serverWorker = GetThisServer(ctx);
duk_push_string(ctx, serverWorker->GetAddress().c_str());
return 1;
}
2022-09-21 05:16:07 +00:00
duk_ret_t ScriptAPI::js_Server__get_addressFamily(duk_context * ctx)
{
2022-09-21 05:16:07 +00:00
CJSServerWorker * serverWorker = GetThisServer(ctx);
duk_push_string(ctx, serverWorker->GetFamily());
return 1;
}
2022-09-21 05:16:07 +00:00
CJSServerWorker * GetThisServer(duk_context * ctx)
{
duk_push_this(ctx);
duk_get_prop_string(ctx, -1, HS_serverWorkerPtr);
2022-09-21 05:16:07 +00:00
CJSServerWorker * serverWorker = (CJSServerWorker *)duk_get_pointer(ctx, -1);
duk_pop_n(ctx, 2);
return serverWorker;
}