From 0557787ceb6e65f41d0e795235dd1f2b2bf6c87f Mon Sep 17 00:00:00 2001 From: Bryan Roe Date: Tue, 29 Jan 2019 00:37:29 -0800 Subject: [PATCH] Updated behavior of getJSModule so that the order of precedence matches the recent changes to require() --- microscript/ILibDuktapeModSearch.c | 55 ++++++++++++++++-------------- 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/microscript/ILibDuktapeModSearch.c b/microscript/ILibDuktapeModSearch.c index cbe677f..82d231c 100644 --- a/microscript/ILibDuktapeModSearch.c +++ b/microscript/ILibDuktapeModSearch.c @@ -55,36 +55,41 @@ duk_ret_t ILibDuktape_ModSearch_GetJSModule(duk_context *ctx, char *id) } duk_pop(ctx); // ... - retVal = ILibHashtable_Get(table, ILibDuktape_ModSearch_ModuleFile, id, idLen); - if (retVal == NULL) - { - duk_push_heap_stash(ctx); - char *mpath; - duk_size_t mpathLen; - mpath = Duktape_GetStringPropertyValueEx(ctx, -1, ILibDuktape_ModSearch_ModulePath, NULL, &mpathLen); - duk_pop(ctx); - char *fileName = ILibMemory_AllocateA(idLen + 4 + mpathLen + 1); - if (mpath == NULL) - { - sprintf_s(fileName, idLen + 4, "%s.js", id); - } - else - { - sprintf_s(fileName, idLen + 5 + mpathLen, "%s/%s.js", mpath, id); - } - int dataLen = ILibReadFileFromDiskEx(&retVal, fileName); - if (dataLen > 0) { duk_push_lstring(ctx, retVal, dataLen); free(retVal); } - else - { - return(0); - } + duk_push_heap_stash(ctx); + char *mpath; + duk_size_t mpathLen; + mpath = Duktape_GetStringPropertyValueEx(ctx, -1, ILibDuktape_ModSearch_ModulePath, NULL, &mpathLen); + duk_pop(ctx); + + char *fileName = ILibMemory_AllocateA(idLen + 4 + mpathLen + 1); + if (mpath == NULL) + { + sprintf_s(fileName, idLen + 4, "%s.js", id); } else { - duk_push_string(ctx, retVal); + sprintf_s(fileName, idLen + 5 + mpathLen, "%s/%s.js", mpath, id); + } + int dataLen = ILibReadFileFromDiskEx(&retVal, fileName); + if (dataLen > 0) + { + duk_push_lstring(ctx, retVal, dataLen); free(retVal); + return(1); + } + else + { + retVal = ILibHashtable_Get(table, ILibDuktape_ModSearch_ModuleFile, id, idLen); + if (retVal == NULL) + { + return(0); + } + else + { + duk_push_string(ctx, retVal); + return(1); + } } - return(1); } void ILibDuktape_ModSearch_AddModuleObject(duk_context *ctx, char *id, void *heapptr) {