Support requiring adjacent files from the same app.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3636 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2021-01-11 02:09:19 +00:00
parent 2b5a56abfe
commit 7012418b13

View File

@ -129,7 +129,7 @@ static JSValue _tf_task_utf8Decode(JSContext* context, JSValueConst this_val, in
static JSValue _tf_task_get_parent(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv); static JSValue _tf_task_get_parent(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
static JSValue _tf_task_print(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv); static JSValue _tf_task_print(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
static JSValue _tf_task_exit(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv); static JSValue _tf_task_exit(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
static JSValue _tf_task_childRequire(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv); static JSValue _tf_task_sandbox_require(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
static JSValue _tf_task_trace(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv); static JSValue _tf_task_trace(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
static JSValue _tf_task_getFile(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv); static JSValue _tf_task_getFile(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
@ -909,10 +909,10 @@ static JSValue _tf_task_executeSource(tf_task_t* task, const char* source, const
return result; return result;
} }
JSValue _tf_task_childRequire(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { JSValue _tf_task_sandbox_require(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) {
tf_task_t* task = JS_GetContextOpaque(context); tf_task_t* task = JS_GetContextOpaque(context);
if (JS_IsObject(task->_requires)) { if (JS_IsObject(task->_loadedFiles)) {
const char* name = JS_ToCString(context, argv[0]); const char* name = JS_ToCString(context, argv[0]);
script_export_t* it = _task_find_script_export(task, name); script_export_t* it = _task_find_script_export(task, name);
if (it) { if (it) {
@ -920,8 +920,24 @@ JSValue _tf_task_childRequire(JSContext* context, JSValueConst this_val, int arg
} else { } else {
JSValue exports = JS_NewObject(context); JSValue exports = JS_NewObject(context);
const char* name = JS_ToCString(context, argv[0]); const char* name = JS_ToCString(context, argv[0]);
JSValue value = JS_GetProperty(context, task->_requires, JS_ValueToAtom(context, argv[0])); JSValue value = JS_GetPropertyStr(context, task->_loadedFiles, name);
if (JS_IsString(value)) { size_t length;
uint8_t* array = tf_try_get_array_buffer(context, &length, value);
if (array) {
char* source = malloc(length + 1);
memcpy(source, array, length);
source[length] = '\0';
JSValue global = JS_GetGlobalObject(context);
JSValue oldExports = JS_GetPropertyStr(context, global, "exports");
JS_SetPropertyStr(context, global, "exports", JS_DupValue(context, exports));
JSValue result = JS_Eval(context, source, length, name, 0);
tf_task_report_error(task, result);
JS_SetPropertyStr(context, global, "exports", oldExports);
JS_FreeValue(context, global);
tf_task_run_jobs(task);
free(source);
return exports;
} else if (JS_IsString(value)) {
size_t length; size_t length;
const char* source = JS_ToCStringLen(context, &length, value); const char* source = JS_ToCStringLen(context, &length, value);
JSValue global = JS_GetGlobalObject(context); JSValue global = JS_GetGlobalObject(context);
@ -934,7 +950,7 @@ JSValue _tf_task_childRequire(JSContext* context, JSValueConst this_val, int arg
tf_task_run_jobs(task); tf_task_run_jobs(task);
return exports; return exports;
} else { } else {
return JS_ThrowInternalError(context, "Failed to load %s.", name); return JS_ThrowInternalError(context, "Failed to load %s %d %d %d.", name, JS_IsNull(value), JS_IsUndefined(value), JS_IsException(value));
} }
} }
} }
@ -1180,7 +1196,7 @@ void tf_task_activate(tf_task_t* task)
JS_SetPropertyStr(context, global, "trace", JS_NewCFunction(context, _tf_task_trace, "trace", 1)); JS_SetPropertyStr(context, global, "trace", JS_NewCFunction(context, _tf_task_trace, "trace", 1));
} else { } else {
JS_SetPropertyStr(context, global, "require", JS_NewCFunction(context, _tf_task_childRequire, "childRequire", 0)); JS_SetPropertyStr(context, global, "require", JS_NewCFunction(context, _tf_task_sandbox_require, "sandboxRequire", 0));
} }
tf_bcrypt_init(context); tf_bcrypt_init(context);