mirror of
https://github.com/Ylianst/MeshAgent
synced 2025-12-21 02:33:24 +00:00
Added ability to execute zip packages
This commit is contained in:
@@ -4609,6 +4609,10 @@ void MeshAgent_ScriptMode_ZipSink_Run(duk_context *ctx, void ** args, int argsLe
|
||||
|
||||
duk_ret_t MeshAgent_ScriptMode_ZipSink2(duk_context *ctx)
|
||||
{
|
||||
duk_size_t tmpLen;
|
||||
char *tmp;
|
||||
|
||||
duk_idx_t top;
|
||||
if (duk_get_length(ctx, 0) == 1)
|
||||
{
|
||||
// Only one file is in here
|
||||
@@ -4620,6 +4624,87 @@ duk_ret_t MeshAgent_ScriptMode_ZipSink2(duk_context *ctx)
|
||||
duk_put_prop_string(ctx, -2, "_scriptName"); // [stash][obj]
|
||||
ILibDuktape_Immediate(ctx, NULL, 0, MeshAgent_ScriptMode_ZipSink_Run);
|
||||
}
|
||||
else
|
||||
{
|
||||
char *run = NULL;
|
||||
int runidx = 0;
|
||||
int found = 0;
|
||||
duk_eval_string(ctx, "process.argv"); // [argarray]
|
||||
duk_array_partialIncludes(ctx, -1, "--run="); // [argarray][int]
|
||||
if (duk_is_number(ctx, -1) && duk_get_int(ctx, -1) >= 0)
|
||||
{
|
||||
runidx = duk_get_int(ctx, -1);
|
||||
duk_get_prop_index(ctx, -2, duk_get_int(ctx, -1)); // [argarray][int][string]
|
||||
duk_string_split(ctx, -1, "="); // [argarray][int][string][tokens]
|
||||
duk_array_pop(ctx, -1); // [argarray][int][string][tokens][string]
|
||||
run = (char*)duk_get_string(ctx, -1);
|
||||
}
|
||||
duk_dup(ctx, 0); // [array]
|
||||
top = duk_get_top(ctx);
|
||||
while (duk_get_length(ctx, -1) > 0)
|
||||
{
|
||||
duk_array_pop(ctx, -1); // [array][obj]
|
||||
duk_get_prop_string(ctx, -1, "name"); // [array][obj][name]
|
||||
#ifdef WIN32
|
||||
duk_string_split(ctx, -1, "\\"); // [array][obj][name][tokens]
|
||||
#else
|
||||
duk_string_split(ctx, -1, "/");
|
||||
#endif
|
||||
duk_array_pop(ctx, -1); // [array][obj][name][tokens][filename]
|
||||
duk_string_endsWith(ctx, -1, ".js"); // [array][obj][name][tokens][filename][boolean]
|
||||
if (duk_get_boolean(ctx, -1))
|
||||
{
|
||||
// This is a JS module
|
||||
if (run != NULL && found == 0)
|
||||
{
|
||||
duk_push_string(ctx, run); // [array][obj][name][tokens][filename][boolean][run]
|
||||
if (duk_equals(ctx, -3, -1) == 1)
|
||||
{
|
||||
// This is the script to run
|
||||
duk_push_heap_stash(ctx); // [array][obj][name][tokens][filename][boolean][run][stash]
|
||||
duk_get_prop_string(ctx, -7, "buffer"); // [array][obj][name][tokens][filename][boolean][run][stash][buffer]
|
||||
duk_put_prop_string(ctx, -2, "_script"); // [array][obj][name][tokens][filename][boolean][run][stash]
|
||||
duk_swap_top(ctx, -2); // [array][obj][name][tokens][filename][boolean][stash][run]
|
||||
duk_put_prop_string(ctx, -2, "_scriptName");// [array][obj][name][tokens][filename][boolean][stash]
|
||||
found = 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Load as a module // [array][obj][name][tokens][filename][boolean]
|
||||
duk_string_split(ctx, -2, "\\"); // [toks]
|
||||
duk_array_pop(ctx, -1); // [toks][string]
|
||||
duk_remove(ctx, -2); // [string]
|
||||
duk_string_split(ctx, -1, "/"); // [string][toks]
|
||||
duk_array_pop(ctx, -1); // [string][toks][string]
|
||||
duk_remove(ctx, -2); // [toks][string]
|
||||
duk_remove(ctx, -2); // [string]
|
||||
|
||||
duk_string_split(ctx, -1, ".js"); // [array][obj][name][tokens][filename][boolean][string][tokens]
|
||||
duk_array_shift(ctx, -1); // [array][obj][name][tokens][filename][boolean][string][tokens][name]
|
||||
duk_get_prop_string(ctx, -8, "buffer"); // [array][obj][name][tokens][filename][boolean][string][tokens][name][buffer]
|
||||
tmp = (char*)duk_to_lstring(ctx, -1, &tmpLen);
|
||||
ILibDuktape_ModSearch_AddModule(ctx, (char*)duk_get_string(ctx, -2), tmp, (int)tmpLen);
|
||||
}
|
||||
}
|
||||
duk_set_top(ctx, top);
|
||||
}
|
||||
if (run != NULL && found != 0)
|
||||
{
|
||||
duk_push_heapptr(ctx, ILibDuktape_GetProcessObject(ctx));
|
||||
duk_get_prop_string(ctx, -1, "\xFF_argArray"); // [process][array]
|
||||
duk_prepare_method_call(ctx, -1, "splice"); // [process][array][splice][this]
|
||||
duk_push_int(ctx, runidx); // [process][array][splice][this][start]
|
||||
duk_push_int(ctx, 1); // [process][array][splice][this][start][deleteCount]
|
||||
duk_pcall_method(ctx, 2);
|
||||
ILibDuktape_Immediate(ctx, NULL, 0, MeshAgent_ScriptMode_ZipSink_Run);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Unable to initialize
|
||||
duk_eval_string_noresult(ctx, "console.log('Error Initializing script from Zip file');process._exit();");
|
||||
}
|
||||
}
|
||||
return(0);
|
||||
}
|
||||
duk_ret_t MeshAgent_ScriptMode_ZipSink(duk_context *ctx)
|
||||
|
||||
@@ -103,6 +103,7 @@ extern duk_ret_t ILibDuktape_EventEmitter_DefaultNewListenerHandler(duk_context
|
||||
#define duk_array_push(ctx, i) duk_dup(ctx, i);duk_get_prop_string(ctx, -1, "push");duk_swap_top(ctx, -2);duk_dup(ctx,-3);duk_pcall_method(ctx, 1);duk_pop_2(ctx);
|
||||
#define duk_array_join(ctx, i, str) duk_dup(ctx, i);duk_get_prop_string(ctx, -1, "join");duk_swap_top(ctx, -2);duk_push_string(ctx, str);duk_pcall_method(ctx, 1);
|
||||
#define duk_array_unshift(ctx, i) duk_dup(ctx, i);duk_get_prop_string(ctx, -1, "unshift");duk_swap_top(ctx, -2);duk_dup(ctx, -3);duk_remove(ctx, -4);duk_pcall_method(ctx, 1);duk_pop(ctx);
|
||||
#define duk_array_partialIncludes(ctx, i, str) duk_prepare_method_call(ctx, i, "partialIncludes");duk_push_string(ctx, str);duk_pcall_method(ctx, 1);
|
||||
|
||||
#define duk_events_setup_on(ctx, i, name, func) duk_prepare_method_call(ctx, i, "on");duk_push_string(ctx, name);duk_push_c_function(ctx, func, DUK_VARARGS);
|
||||
#define duk_events_newListener(ctx, i, name, func) duk_events_setup_on(ctx, i, "newListener", ILibDuktape_EventEmitter_DefaultNewListenerHandler);duk_push_string(ctx, name);duk_put_prop_string(ctx, -2, "event_name");duk_push_c_function(ctx, func, DUK_VARARGS);duk_put_prop_string(ctx, -2, "event_callback");if(duk_pcall_method(ctx, 2)!=0){printf("oops\n");ILibDuktape_Process_UncaughtExceptionEx(ctx, "duk_events_newListener (%s,%d)", __FILE__, __LINE__);}duk_pop(ctx);
|
||||
|
||||
@@ -461,12 +461,34 @@ duk_ret_t ILibDuktape_Polyfills_Array_includes(duk_context *ctx)
|
||||
duk_push_false(ctx);
|
||||
return(1);
|
||||
}
|
||||
duk_ret_t ILibDuktape_Polyfills_Array_partialIncludes(duk_context *ctx)
|
||||
{
|
||||
duk_size_t inLen;
|
||||
char *inStr = (char*)duk_get_lstring(ctx, 0, &inLen);
|
||||
duk_push_this(ctx); // [array]
|
||||
uint32_t count = (uint32_t)duk_get_length(ctx, -1);
|
||||
uint32_t i;
|
||||
duk_size_t tmpLen;
|
||||
char *tmp;
|
||||
for (i = 0; i < count; ++i)
|
||||
{
|
||||
tmp = Duktape_GetStringPropertyIndexValueEx(ctx, -1, i, "", &tmpLen);
|
||||
if (inLen > 0 && inLen <= tmpLen && strncmp(inStr, tmp, inLen) == 0)
|
||||
{
|
||||
duk_push_int(ctx, i);
|
||||
return(1);
|
||||
}
|
||||
}
|
||||
duk_push_int(ctx, -1);
|
||||
return(1);
|
||||
}
|
||||
void ILibDuktape_Polyfills_Array(duk_context *ctx)
|
||||
{
|
||||
// Polyfill 'Array.includes'
|
||||
duk_get_prop_string(ctx, -1, "Array"); // [Array]
|
||||
duk_get_prop_string(ctx, -1, "prototype"); // [Array][proto]
|
||||
ILibDuktape_CreateProperty_InstanceMethod(ctx, "includes", ILibDuktape_Polyfills_Array_includes, 1);
|
||||
ILibDuktape_CreateProperty_InstanceMethod(ctx, "partialIncludes", ILibDuktape_Polyfills_Array_partialIncludes, 1);
|
||||
duk_pop_2(ctx); // ...
|
||||
}
|
||||
void ILibDuktape_Polyfills_String(duk_context *ctx)
|
||||
|
||||
Reference in New Issue
Block a user