Use proper js modules for apps. Kludge enough things to make things seem to work. Need to apply this to core still.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3862 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2022-03-18 01:24:29 +00:00
parent 9b2d4b393d
commit 00bdf1df4a
4 changed files with 107 additions and 57 deletions

View File

@ -347,7 +347,7 @@ int tf_task_execute(tf_task_t* task, const char* fileName)
}
if (source)
{
JSValue result = JS_Eval(task->_context, source, strlen(source), fileName, 0);
JSValue result = JS_Eval(task->_context, source, strlen(source), fileName, JS_EVAL_TYPE_MODULE);
tf_util_report_error(task->_context, result);
if (!JS_IsError(task->_context, result) && !JS_IsException(result))
{
@ -891,12 +891,15 @@ void tf_task_on_receive_packet(int packetType, const char* begin, size_t length,
JSValue result = _tf_task_executeSource(to, source_str, name);
if (JS_IsException(result))
{
_tf_task_sendPromiseReject(to, from, promise, JS_GetException(to->_context));
JSValue exception = JS_GetException(to->_context);
_tf_task_sendPromiseReject(to, from, promise, exception);
JS_FreeValue(to->_context, exception);
}
else
{
_tf_task_sendPromiseResolve(to, from, promise, result);
}
JS_FreeValue(to->_context, result);
JS_FreeCString(to->_context, source_str);
JS_FreeCString(to->_context, name);
}
@ -1031,7 +1034,7 @@ JSValue _tf_task_require(JSContext* context, JSValueConst this_val, int argc, JS
JSValue global = JS_GetGlobalObject(task->_context);
JSValue oldExports = JS_GetPropertyStr(task->_context, global, "exports");
JS_SetPropertyStr(task->_context, global, "exports", JS_DupValue(task->_context, exports));
JSValue eval = JS_Eval(task->_context, source, strlen(source), path, 0);
JSValue eval = JS_Eval(task->_context, source, strlen(source), path, JS_EVAL_TYPE_MODULE);
tf_util_report_error(task->_context, eval);
if (JS_IsError(task->_context, eval) ||
JS_IsException(eval))
@ -1067,7 +1070,7 @@ JSValue _tf_task_require(JSContext* context, JSValueConst this_val, int argc, JS
static JSValue _tf_task_executeSource(tf_task_t* task, const char* source, const char* name)
{
tf_trace_begin(task->_trace, "_tf_task_executeSource");
JSValue result = JS_Eval(task->_context, source, strlen(source), name, 0);
JSValue result = JS_Eval(task->_context, source, strlen(source), name, JS_EVAL_TYPE_MODULE);
if (!*task->_scriptName)
{
snprintf(task->_scriptName, sizeof(task->_scriptName), "%s", name);
@ -1103,7 +1106,7 @@ JSValue _tf_task_sandbox_require(JSContext* context, JSValueConst this_val, int
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);
JSValue result = JS_Eval(context, source, length, name, JS_EVAL_TYPE_MODULE);
tf_util_report_error(context, result);
JS_SetPropertyStr(context, global, "exports", oldExports);
JS_FreeValue(context, global);
@ -1117,7 +1120,7 @@ JSValue _tf_task_sandbox_require(JSContext* context, JSValueConst this_val, int
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);
JSValue result = JS_Eval(context, source, length, name, JS_EVAL_TYPE_MODULE);
tf_util_report_error(context, result);
JS_SetPropertyStr(context, global, "exports", oldExports);
JS_FreeValue(context, global);
@ -1378,6 +1381,40 @@ static void _tf_task_run_jobs_prepare(uv_prepare_t* prepare)
}
}
JSModuleDef* _tf_task_module_loader(JSContext* context, const char* module_name, void* opaque)
{
tf_task_t* task = opaque;
JSValue source_value = JS_GetPropertyStr(context, task->_loadedFiles, module_name);
char* source = NULL;
size_t length = 0;
uint8_t* array = tf_util_try_get_array_buffer(context, &length, source_value);
if (array)
{
source = malloc(length + 1);
memcpy(source, array, length);
source[length] = '\0';
}
JS_FreeValue(context, source_value);
if (!source)
{
JS_ThrowReferenceError(context, "Could not load '%s'.", module_name);
return NULL;
}
JSValue result = JS_Eval(context, source, length, module_name, JS_EVAL_TYPE_MODULE | JS_EVAL_FLAG_COMPILE_ONLY);
free(source);
if (tf_util_report_error(task->_context, result))
{
return NULL;
}
JSModuleDef* module = JS_VALUE_GET_PTR(result);
JS_FreeValue(context, result);
return module;
}
tf_task_t* tf_task_create()
{
tf_task_t* task = malloc(sizeof(tf_task_t));
@ -1389,6 +1426,8 @@ tf_task_t* tf_task_create()
JS_SetHostPromiseRejectionTracker(task->_runtime, _tf_task_promise_rejection_tracker, task);
JS_SetModuleLoaderFunc(task->_runtime, NULL, _tf_task_module_loader, task);
JS_NewClassID(&_import_class_id);
JSClassDef def =
{