diff --git a/meshcore/agentcore.c b/meshcore/agentcore.c index d9dc429..49545ca 100644 --- a/meshcore/agentcore.c +++ b/meshcore/agentcore.c @@ -4627,7 +4627,7 @@ duk_ret_t MeshAgent_ScriptMode_ZipSink2(duk_context *ctx) else { char *run = NULL; - int runidx = 0; + int runidx = -1; int found = 0; duk_eval_string(ctx, "process.argv"); // [argarray] duk_array_partialIncludes(ctx, -1, "--run="); // [argarray][int] @@ -4639,6 +4639,27 @@ duk_ret_t MeshAgent_ScriptMode_ZipSink2(duk_context *ctx) duk_array_pop(ctx, -1); // [argarray][int][string][tokens][string] run = (char*)duk_get_string(ctx, -1); } + + if (run == NULL) + { + // --run="" was not specified, so we'll default to the name of the binary + duk_eval_string(ctx, "process.argv[0]"); // [path] +#ifdef WIN32 + duk_string_split(ctx, -1, "\\"); // [path][array] +#else + duk_string_split(ctx, -1, "/"); +#endif + duk_array_pop(ctx, -1); // [path][array][string] +#ifdef WIN32 + int tlen = (int)duk_get_length(ctx, -1); + duk_string_substring(ctx, -1, 0, tlen - 4); // [path][array][string][string] +#endif + duk_push_string(ctx, ".js"); // [path][array][string][string][.js] + duk_string_concat(ctx, -2); // [path][array][string][string][string] + run = (char*)duk_to_string(ctx, -1); + } + + duk_dup(ctx, 0); // [array] top = duk_get_top(ctx); while (duk_get_length(ctx, -1) > 0) @@ -4691,12 +4712,15 @@ duk_ret_t MeshAgent_ScriptMode_ZipSink2(duk_context *ctx) } 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); + if (runidx != -1) + { + 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 @@ -4854,7 +4878,13 @@ void MeshAgent_ScriptMode(MeshAgentHostContainer *agentHost, int argc, char **ar duk_peval_string_noresult(agentHost->meshCoreCtx, "require('linux-pathfix')();"); #endif - if (jsPath == NULL || ILibString_EndsWith(jsPath, -1, ".zip", 4) == 0) + int embeddedZIP = 0; + if (jsFileLen > 30 && jsFile[0] == 0x50 && jsFile[1] == 0x4B && jsFile[2] == 0x03 && jsFile[3] == 0x04) + { + embeddedZIP = 1; + } + + if ((embeddedZIP == 0 && jsPath == NULL) || (embeddedZIP == 0 && jsPath != NULL && ILibString_EndsWith(jsPath, -1, ".zip", 4) == 0)) { if (ILibDuktape_ScriptContainer_CompileJavaScriptEx(agentHost->meshCoreCtx, jsFile, jsFileLen, agentHost->meshCoreCtx_embeddedScript == NULL ? scriptArgs[0] : "[embedded].js", 0) != 0 || ILibDuktape_ScriptContainer_ExecuteByteCode(agentHost->meshCoreCtx) != 0) { @@ -4868,16 +4898,33 @@ void MeshAgent_ScriptMode(MeshAgentHostContainer *agentHost, int argc, char **ar } else { - // Trying to run a zip file - duk_push_sprintf(agentHost->meshCoreCtx, "require('zip-reader').read('%s');", jsPath); // [string] + int r; + if (embeddedZIP != 0 && jsFile != NULL) + { + // Trying to run an embedded zip file + duk_eval_string(agentHost->meshCoreCtx, "require('zip-reader')"); // [zip-reader] + duk_get_prop_string(agentHost->meshCoreCtx, -1, "read"); // [zip-reader][read] + duk_swap_top(agentHost->meshCoreCtx, -2); // [read][this] + char *tmp=(char*)duk_push_fixed_buffer(agentHost->meshCoreCtx, jsFileLen); // [read][this][buffer] + memcpy_s(tmp, jsFileLen, jsFile, jsFileLen); + duk_push_buffer_object(agentHost->meshCoreCtx, -1, 0, jsFileLen, DUK_BUFOBJ_NODEJS_BUFFER); //..][this][buffer][njsBuffer] + duk_remove(agentHost->meshCoreCtx, -2); // [read][this][buffer] + r = duk_pcall_method(agentHost->meshCoreCtx, 1); // [promise] + } + else + { + // Trying to run a zip file + duk_push_sprintf(agentHost->meshCoreCtx, "require('zip-reader').read('%s');", jsPath); // [string] #ifdef WIN32 - duk_string_split(agentHost->meshCoreCtx, -1, "\\"); // [string][array] - duk_array_join(agentHost->meshCoreCtx, -1, "\\\\"); // [string][array][string] - duk_remove(agentHost->meshCoreCtx, -2); // [string][string] - duk_remove(agentHost->meshCoreCtx, -2); // [string] + duk_string_split(agentHost->meshCoreCtx, -1, "\\"); // [string][array] + duk_array_join(agentHost->meshCoreCtx, -1, "\\\\"); // [string][array][string] + duk_remove(agentHost->meshCoreCtx, -2); // [string][string] + duk_remove(agentHost->meshCoreCtx, -2); // [string] #endif + r = duk_peval(agentHost->meshCoreCtx); // [promise] + } - if (duk_peval(agentHost->meshCoreCtx) != 0) // [zip-reader] + if (r != 0) // [zip-reader] { duk_peval_string_noresult(agentHost->meshCoreCtx, "console.log('Error decoding zip file');process._exit();"); duk_pop(agentHost->meshCoreCtx); // ... diff --git a/microscript/ILibDuktape_Helpers.h b/microscript/ILibDuktape_Helpers.h index 2942717..2051a45 100644 --- a/microscript/ILibDuktape_Helpers.h +++ b/microscript/ILibDuktape_Helpers.h @@ -127,6 +127,7 @@ extern duk_ret_t ILibDuktape_EventEmitter_DefaultNewListenerHandler(duk_context #define duk_string_concat(ctx, i) duk_dup(ctx, i);duk_get_prop_string(ctx, -1, "concat");duk_swap_top(ctx, -2);duk_dup(ctx, -3);duk_pcall_method(ctx, 1);duk_remove(ctx, -2); #define duk_string_split(ctx, i, delim) duk_dup(ctx, i);duk_get_prop_string(ctx, -1, "split");duk_swap_top(ctx, -2);duk_push_string(ctx, delim);duk_call_method(ctx, 1); #define duk_string_endsWith(ctx, i, val) duk_prepare_method_call(ctx, i, "endsWith");duk_push_string(ctx,val);duk_pcall_method(ctx, 1); +#define duk_string_substring(ctx, i, start, end) duk_prepare_method_call(ctx, i, "substring");duk_push_int(ctx, start);duk_push_int(ctx, end);duk_pcall_method(ctx, 2); int Duktape_GetBooleanProperty(duk_context *ctx, duk_idx_t i, char *propertyName, int defaultValue); struct sockaddr_in6* Duktape_IPAddress4_FromString(char* address, unsigned short port); diff --git a/microscript/ILibDuktape_fs.c b/microscript/ILibDuktape_fs.c index 9088bc6..ebceeb3 100644 --- a/microscript/ILibDuktape_fs.c +++ b/microscript/ILibDuktape_fs.c @@ -819,7 +819,7 @@ duk_ret_t ILibDuktape_fs_read(duk_context *ctx) char *srbuf = (char*)Duktape_GetBufferPropertyEx(ctx, 0, "buffer", &srbufLen); memcpy_s(wrbuf + offset, bytesRead, srbuf + dpos, bytesRead); - duk_push_int(ctx, bytesRead); // [bufferDescriptor][buffer][position] + duk_push_int(ctx, bytesRead + dpos); // [bufferDescriptor][buffer][position] duk_put_prop_string(ctx, -3, "position"); // [bufferDescriptor][buffer] duk_push_this(ctx); // [bufferDescriptor][buffer][fs] duk_get_prop_string(ctx, -1, FS_BUFFER_DESCRIPTOR_PENDING); // [bufferDescriptor][buffer][fs][array] diff --git a/modules/exe.js b/modules/exe.js index 2531520..8a9dc67 100644 --- a/modules/exe.js +++ b/modules/exe.js @@ -54,7 +54,7 @@ for (i = 1; i < process.argv.length; ++i) { if (process.argv[i].startsWith('-o')) { outputFileName = process.argv[i].substring(2); } // Output file if (process.argv[i].startsWith('-x')) { execPath = process.argv[i].substring(2); } // Input executable if (process.argv[i].startsWith('-d')) { depPath = process.argv[i].substring(2); } // Dependencies path - if (!process.argv[i].startsWith('-') && process.argv[i].endsWith('.js')) { sourcejs = process.argv[i]; } // JavaScript + if (!process.argv[i].startsWith('-') && (process.argv[i].endsWith('.js') || process.argv[i].endsWith('.zip'))) { sourcejs = process.argv[i]; } // JavaScript } console.log('Output Filename: ' + outputFileName);