1
0
mirror of https://github.com/Ylianst/MeshAgent synced 2025-12-15 07:43:50 +00:00

Added ability to version embedded JS files

This commit is contained in:
Bryan Roe
2021-07-22 09:46:36 -07:00
parent aec6f65906
commit 1ebdb4c58d
4 changed files with 207 additions and 15 deletions

View File

@@ -29,13 +29,74 @@ limitations under the License.
#include <crtdbg.h>
#endif
#define ILibDuktape_ModSearch_ModuleFile (void*)0xFF
#define ILibDuktape_ModSearch_ModuleObject (void*)0xFE
#define ILibDuktape_ModSearch_JSInclude "\xFF_ModSearch_JSINCLUDE"
#define ILibDuktape_ModSearch_ModulePath "\xFF_ModSearch_Path"
#define ILibDuktape_ModSearch_ModuleFile (void*)0xFF
#define ILibDuktape_ModSearch_ModuleFileDate (void*)0xFC
#define ILibDuktape_ModSearch_ModuleRequired (void*)0xFB
#define ILibDuktape_ModSearch_ModuleObject (void*)0xFE
#define ILibDuktape_ModSearch_JSInclude "\xFF_ModSearch_JSINCLUDE"
#define ILibDuktape_ModSearch_ModulePath "\xFF_ModSearch_Path"
int ILibDuktape_ModSearch_ShowNames = 0;
uint32_t ILibDuktape_ModSearch_GetJSModuleDate(duk_context *ctx, char *id)
{
uint32_t retVal;
ILibHashtable table = NULL;
int idLen = (int)strnlen_s(id, 1024);
duk_push_heap_stash(ctx); // [stash]
if (duk_has_prop_string(ctx, -1, "ModSearchTable"))
{
duk_get_prop_string(ctx, -1, "ModSearchTable"); // [stash][ptr]
table = (ILibHashtable)duk_to_pointer(ctx, -1);
duk_pop(ctx); // [stash]
}
else
{
table = ILibHashtable_Create();
duk_push_pointer(ctx, table); // [stash][ptr]
duk_put_prop_string(ctx, -2, "ModSearchTable"); // [stash]
}
duk_pop(ctx); // ...
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);
}
duk_push_sprintf(ctx, "(new Date(require('fs').statSync('%s').mtime)).getTime()/1000", fileName); // [str]
if (duk_peval(ctx) == 0)
{
retVal = duk_get_uint(ctx, -1);
}
else
{
retVal = (uint32_t)(UINT_PTR)ILibHashtable_Get(table, ILibDuktape_ModSearch_ModuleFileDate, id, idLen);
}
duk_pop(ctx);
return(retVal);
}
int ILibDuktape_ModSearch_IsRequired(duk_context *ctx, char *id, size_t idLen)
{
duk_push_heap_stash(ctx); // [stash]
duk_get_prop_string(ctx, -1, "ModSearchTable"); // [stash][table]
ILibHashtable table = (ILibHashtable)duk_to_pointer(ctx, -1);
duk_pop_2(ctx); // ...
return((int)(UINT_PTR)ILibHashtable_Get(table, ILibDuktape_ModSearch_ModuleRequired, id, (int)idLen));
}
duk_ret_t ILibDuktape_ModSearch_GetJSModule(duk_context *ctx, char *id)
{
ILibHashtable table = NULL;
@@ -123,7 +184,7 @@ void ILibDuktape_ModSearch_AddModuleObject(duk_context *ctx, char *id, void *hea
}
duk_pop(ctx); // ...
}
int ILibDuktape_ModSearch_AddModule(duk_context *ctx, char *id, char *module, int moduleLen)
int ILibDuktape_ModSearch_AddModuleEx(duk_context *ctx, char *id, char *module, int moduleLen, char *mtime)
{
ILibHashtable table = NULL;
int idLen = (int)strnlen_s(id, 1024);
@@ -148,6 +209,14 @@ int ILibDuktape_ModSearch_AddModule(duk_context *ctx, char *id, char *module, in
newModule[moduleLen] = 0;
ILibHashtable_Put(table, ILibDuktape_ModSearch_ModuleFile, id, idLen, newModule);
if (mtime != NULL)
{
duk_push_sprintf(ctx, "(new Date('%s')).getTime()/1000", mtime);
duk_eval(ctx);
uint32_t t = duk_get_uint(ctx, -1);
ILibHashtable_Put(table, ILibDuktape_ModSearch_ModuleFileDate, id, idLen, (void*)(UINT_PTR)t);
}
return 0;
}
int ILibDuktape_ModSearch_AddHandler(duk_context *ctx, char *id, ILibDuktape_ModSearch_PUSH_Object handler)
@@ -264,6 +333,11 @@ duk_ret_t mod_Search(duk_context *ctx)
// Next check if a handler was added via ILibDuktape_ModSearch_AddModule()
if ((module = (char*)ILibHashtable_Get(table, ILibDuktape_ModSearch_ModuleFile, id, (int)idLen)) != NULL)
{
//
// Let's mark that this was already "require'ed"
//
ILibHashtable_Put(table, ILibDuktape_ModSearch_ModuleRequired, id, (int)idLen, (void*)(UINT_PTR)0x01);
duk_push_string(ctx, module);
return(1);
}

View File

@@ -24,9 +24,12 @@ typedef void (*ILibDuktape_ModSearch_PUSH_Object)(duk_context *ctx, void *chain)
int ILibDuktape_ModSearch_AddHandler(duk_context *ctx, char *id, ILibDuktape_ModSearch_PUSH_Object handler);
void ILibDuktape_ModSearch_AddHandler_AlsoIncludeJS(duk_context *ctx, char *js, size_t jsLen);
int ILibDuktape_ModSearch_AddModule(duk_context *ctx, char *id, char *module, int moduleLen);
int ILibDuktape_ModSearch_AddModuleEx(duk_context *ctx, char *id, char *module, int moduleLen, char *mtime);
#define ILibDuktape_ModSearch_AddModule(ctx, id, module, moduleLen) ILibDuktape_ModSearch_AddModuleEx(ctx, id, module, moduleLen, NULL)
void ILibDuktape_ModSearch_AddModuleObject(duk_context *ctx, char *id, void *heapptr);
duk_ret_t ILibDuktape_ModSearch_GetJSModule(duk_context *ctx, char *id);
uint32_t ILibDuktape_ModSearch_GetJSModuleDate(duk_context *ctx, char *id);
int ILibDuktape_ModSearch_IsRequired(duk_context *ctx, char *id, size_t idLen);
void ILibDuktape_ModSearch_Init(duk_context *ctx, void *chain, ILibSimpleDataStore mDB);
#endif

File diff suppressed because one or more lines are too long

View File

@@ -32,6 +32,26 @@ var xclipTable = {};
function nativeAddCompressedModule(name)
{
var value = getJSModule(name);
var valuex = '';
try
{
valuex = getJSModuleDate(name);
if(valuex>0)
{
valuex = (new Date(valuex*1000)).toString().split(' ').join('T');
valuex = ", '" + valuex + "'";
}
else
{
valuex = '';
}
}
catch(e)
{
valuex = '';
}
var zip = require('compressed-stream').createCompressor();
zip.buffer = null;
zip.on('data', function (c)
@@ -47,7 +67,7 @@ function nativeAddCompressedModule(name)
});
zip.end(value);
var vstring = zip.buffer.toString('base64');
var ret = "duk_peval_string_noresult(ctx, \"addCompressedModule('" + name + "', Buffer.from('" + vstring + "', 'base64'));\");";
var ret = "duk_peval_string_noresult(ctx, \"addCompressedModule('" + name + "', Buffer.from('" + vstring + "', 'base64')" + valuex + ");\");";
if (ret.length > 16300)
{
// MS Visual Studio has a maxsize limitation
@@ -60,7 +80,7 @@ function nativeAddCompressedModule(name)
ret += ('memcpy_s(_' + name.split('-').join('') + ' + ' + i + ', ' + (tmp.length - i) + ', "' + chunk + '", ' + chunk.length + ');\n');
i += chunk.length;
}
ret += ('ILibDuktape_AddCompressedModule(ctx, "' + name + '", _' + name.split('-').join('') + ');\n');
ret += ('ILibDuktape_AddCompressedModule(ctx, "' + name + '", _' + name.split('-').join('') + valuex + ');\n');
ret += ('free(_' + name.split('-').join('') + ');\n');
}
module.exports(ret);