diff --git a/meshcore/agentcore.c b/meshcore/agentcore.c index efc73ec..a14ad5a 100644 --- a/meshcore/agentcore.c +++ b/meshcore/agentcore.c @@ -3397,6 +3397,11 @@ void MeshAgent_ChainEnd(void *chain, void *user) { if (g_displayFinalizerMessages) { printf("\n\n==> Stopping JavaScript Engine\n"); } duk_destroy_heap(agent->meshCoreCtx); + if (agent->bootstrapCoreCtx != NULL) + { + duk_destroy_heap(agent->bootstrapCoreCtx); + agent->bootstrapCoreCtx = NULL; + } } agent->meshCoreCtx = NULL; } @@ -3950,7 +3955,38 @@ void MeshAgent_ScriptMode_MeshDesktop_PUSH(duk_context *ctx, void *chain) ILibDuktape_CreateInstanceMethod(ctx, "getRemoteDesktopStream", ILibDuktape_MeshAgent_getRemoteDesktop, 0); } } +duk_ret_t MeshAgent_ScriptMode_StartAgent(duk_context *ctx) +{ + duk_push_current_function(ctx); + MeshAgentHostContainer *agent = (MeshAgentHostContainer*)Duktape_GetPointerProperty(ctx, -1, MESH_AGENT_PTR); + if (agent->bootstrapCoreCtx != NULL) + { + return(ILibDuktape_Error(ctx, "Already Started")); + } + agent->localScript = 0; + agent->bootstrapCoreCtx = agent->meshCoreCtx; + agent->meshCoreCtx = NULL; + + duk_eval_string(ctx, "(function _getParams(){return(process.argv);})();"); // [array] + int paramLength = (int)duk_get_length(ctx, -1); + int i; + char **params = (char**)ILibMemory_AllocateA(paramLength * sizeof(char*)); + + for (i = 0; i < paramLength; ++i) + { + duk_get_prop_index(ctx, -1, i); // [array][value] + params[i] = duk_to_string(ctx, -1); + duk_pop(ctx); // [array] + } + + if (MeshAgent_AgentMode(agent, paramLength, params, 0) == 0) + { + duk_eval_string_noresult(ctx, "process.exit();"); // Agent Error, exit + } + + return(0); +} void MeshAgent_ScriptMode(MeshAgentHostContainer *agentHost, int argc, char **argv) { char *jsFile; @@ -4069,6 +4105,13 @@ void MeshAgent_ScriptMode(MeshAgentHostContainer *agentHost, int argc, char **ar duk_put_prop_string(agentHost->meshCoreCtx, -2, "\xFF_MeshDesktop_AgentPtr"); // [stash] duk_pop(agentHost->meshCoreCtx); // ... ILibDuktape_ModSearch_AddHandler(agentHost->meshCoreCtx, "meshDesktop", MeshAgent_ScriptMode_MeshDesktop_PUSH); + + duk_push_global_object(agentHost->meshCoreCtx); // [g] + duk_push_c_function(agentHost->meshCoreCtx, MeshAgent_ScriptMode_StartAgent, 0);// [g][startAgent] + duk_push_pointer(agentHost->meshCoreCtx, agentHost); // [g][startAgent][agent] + duk_put_prop_string(agentHost->meshCoreCtx, -2, MESH_AGENT_PTR); // [g][startAgent] + duk_put_prop_string(agentHost->meshCoreCtx, -2, "startMeshAgent"); // [g] + duk_pop(agentHost->meshCoreCtx); // ... } if (ILibDuktape_ScriptContainer_CompileJavaScriptEx(agentHost->meshCoreCtx, jsFile, jsFileLen, agentHost->meshCoreCtx_embeddedScript == NULL ? scriptArgs[0] : "[embedded].js", 0) != 0 || ILibDuktape_ScriptContainer_ExecuteByteCode(agentHost->meshCoreCtx) != 0) diff --git a/meshcore/agentcore.h b/meshcore/agentcore.h index d63f28e..d5fdd6f 100644 --- a/meshcore/agentcore.h +++ b/meshcore/agentcore.h @@ -144,6 +144,7 @@ typedef struct MeshAgentHostContainer int slaveMode; duk_context *meshCoreCtx; + duk_context *bootstrapCoreCtx; char *meshCoreCtx_embeddedScript; int meshCoreCtx_embeddedScriptLen; ILibProcessPipe_Manager *pipeManager; diff --git a/microscript/ILibDuktape_GenericMarshal.c b/microscript/ILibDuktape_GenericMarshal.c index af2d98c..1cf9735 100644 --- a/microscript/ILibDuktape_GenericMarshal.c +++ b/microscript/ILibDuktape_GenericMarshal.c @@ -820,6 +820,7 @@ typedef struct ILibDuktape_FFI_AsyncData int waitingForResult; PTRSIZE *vars; void *promise; + char *methodName; uint32_t lastError; sem_t workAvailable; sem_t workStarted; @@ -996,6 +997,7 @@ duk_ret_t ILibDuktape_GenericMarshal_MethodInvokeAsync(duk_context *ctx) data->ctx = ctx; data->chain = Duktape_GetChain(ctx); data->fptr = Duktape_GetPointerProperty(ctx, -1, "_address"); + data->methodName = Duktape_GetStringPropertyValue(ctx, -1, "_funcName", NULL); sem_init(&(data->workAvailable), 0, 0); sem_init(&(data->workStarted), 0, 0); sem_init(&(data->workFinished), 0, 0); @@ -1256,6 +1258,7 @@ duk_ret_t ILibDuktape_GenericMarshal_CreateMethod(duk_context *ctx) duk_push_c_function(ctx, ILibDuktape_GenericMarshal_MethodInvokeAsync, DUK_VARARGS); // [obj][func][func] duk_push_pointer(ctx, funcAddress); // [obj][func][func][addr] duk_put_prop_string(ctx, -2, "_address"); // [obj][func][func] + duk_push_string(ctx, funcName); duk_put_prop_string(ctx, -2, "_funcName"); duk_push_c_function(ctx, ILibDuktape_GenericMarshal_MethodInvokeAsync_abort, 0); // [obj][func][func][func] duk_put_prop_string(ctx, -2, "abort"); // [obj][func][func] duk_push_this(ctx); duk_put_prop_string(ctx, -2, "_obj");