Remove require. There is only import+export.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3905 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2022-06-18 17:50:22 +00:00
parent 2d8a956c14
commit b42b5d11fa
2 changed files with 24 additions and 206 deletions

View File

@ -47,15 +47,6 @@ typedef struct _task_child_node_t
task_child_node_t* next;
} task_child_node_t;
typedef struct _script_export_t script_export_t;
typedef struct _script_export_t
{
const char* name;
JSValue value;
script_export_t* next;
} script_export_t;
typedef struct _promise_t promise_t;
typedef struct _promise_t
{
@ -73,8 +64,6 @@ typedef struct _tf_task_t
tf_taskstub_t* _parent;
const char* _path;
script_export_t* _scriptExports;
bool _activated;
bool _trusted;
bool _killed;
@ -110,7 +99,6 @@ typedef struct _tf_task_t
import_record_t** _imports;
int _import_count;
JSValue _requires;
JSValue _loadedFiles;
int _ssb_port;
@ -155,7 +143,6 @@ static bool _export_record_release(tf_task_t* task, export_record_t** export)
static JSValue _tf_task_version(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_exit(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_getStats(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
static JSValue _tf_task_getFile(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
@ -869,10 +856,6 @@ void tf_task_on_receive_packet(int packetType, const char* begin, size_t length,
}
}
break;
case kSetRequires:
JS_FreeValue(to->_context, to->_requires);
to->_requires = tf_serialize_load(to, from, begin, length);
break;
case kLoadFile:
{
JSValue args = tf_serialize_load(to, from, begin, length);
@ -981,108 +964,6 @@ void tf_task_on_receive_packet(int packetType, const char* begin, size_t length,
tf_trace_end(to->_trace);
}
static const char* _tf_task_resolveRequire(tf_task_t* task, const char* require)
{
printf("Looking in %s for %s\n", task->_path, require);
if (strstr(require, "..") || strstr(require, "/") || strstr(require, "\\"))
{
return NULL;
}
char test[1024];
snprintf(test, sizeof(test), "%s/%s%s", task->_path, require, strstr(require, ".js") ? "" : ".js");
printf("Testing %s\n", test);
uv_fs_t request;
if (uv_fs_access(&task->_loop, &request, test, R_OK, 0) == 0)
{
return tf_strdup(test);
}
return NULL;
}
static script_export_t* _task_find_script_export(tf_task_t* task, const char* path)
{
for (script_export_t* it = task->_scriptExports; it; it = it->next)
{
if (strcmp(it->name, path) == 0)
{
return it;
}
}
return NULL;
}
JSValue _tf_task_require(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{
JSValue result = JS_NULL;
tf_task_t* task = JS_GetContextOpaque(context);
tf_trace_begin(task->_trace, "_tf_task_require");
const char* in_path = JS_ToCString(context, argv[0]);
if (in_path)
{
const char* path = _tf_task_resolveRequire(task, in_path);
if (!path)
{
result = JS_ThrowReferenceError(task->_context, "require(): Unable to resolve module: %s.", in_path);
}
else
{
script_export_t* it = _task_find_script_export(task, path);
if (it)
{
result = JS_DupValue(task->_context, it->value);
tf_free((void*)path);
}
else
{
JSValue exports = JS_NewObject(task->_context);
script_export_t* export = tf_malloc(sizeof(script_export_t));
*export = (script_export_t)
{
.name = path,
.value = JS_DupValue(task->_context, exports),
.next = task->_scriptExports,
};
task->_scriptExports = export;
const char* source = _task_loadFile(path);
printf("Requiring script %sn", path);
if (source)
{
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, JS_EVAL_TYPE_MODULE);
tf_util_report_error(task->_context, eval);
if (JS_IsError(task->_context, eval) ||
JS_IsException(eval))
{
result = JS_DupValue(task->_context, eval);
}
else
{
result = JS_DupValue(task->_context, exports);
}
JS_FreeValue(task->_context, eval);
JS_SetPropertyStr(task->_context, global, "exports", oldExports);
JS_FreeValue(task->_context, global);
tf_free((void*)source);
}
else
{
printf("Failed to load %s.\n", path);
}
JS_FreeValue(task->_context, exports);
}
}
}
else
{
result = JS_ThrowReferenceError(task->_context, "require(): No module specified.");
}
JS_FreeCString(context, in_path);
tf_trace_end(task->_trace);
return result;
}
static JSValue _tf_task_executeSource(tf_task_t* task, const char* source, const char* name)
{
tf_trace_begin(task->_trace, "_tf_task_executeSource");
@ -1095,63 +976,6 @@ static JSValue _tf_task_executeSource(tf_task_t* task, const char* source, const
return result;
}
JSValue _tf_task_sandbox_require(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{
tf_task_t* task = JS_GetContextOpaque(context);
if (JS_IsObject(task->_loadedFiles))
{
const char* name = JS_ToCString(context, argv[0]);
script_export_t* it = _task_find_script_export(task, name);
if (it)
{
return it->value;
}
else
{
JSValue exports = JS_NewObject(context);
const char* name = JS_ToCString(context, argv[0]);
JSValue value = JS_GetPropertyStr(context, task->_loadedFiles, name);
size_t length;
uint8_t* array = tf_util_try_get_array_buffer(context, &length, value);
if (array)
{
char* source = tf_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, JS_EVAL_TYPE_MODULE);
tf_util_report_error(context, result);
JS_SetPropertyStr(context, global, "exports", oldExports);
JS_FreeValue(context, global);
tf_free(source);
return exports;
}
else if (JS_IsString(value))
{
size_t length;
const char* source = JS_ToCStringLen(context, &length, value);
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, JS_EVAL_TYPE_MODULE);
tf_util_report_error(context, result);
JS_SetPropertyStr(context, global, "exports", oldExports);
JS_FreeValue(context, global);
return exports;
}
else
{
return JS_ThrowInternalError(context, "Failed to load %s %d %d %d.", name, JS_IsNull(value), JS_IsUndefined(value), JS_IsException(value));
}
}
}
return JS_UNDEFINED;
}
uv_loop_t* tf_task_get_loop(tf_task_t* task)
{
return &task->_loop;
@ -1555,7 +1379,6 @@ void tf_task_activate(tf_task_t* task)
{
sqlite3_open(*task->_db_path ? task->_db_path : "db.sqlite", &task->_db);
JS_SetPropertyStr(context, global, "require", JS_NewCFunction(context, _tf_task_require, "require", 1));
JS_SetPropertyStr(context, global, "Task", tf_taskstub_register(context));
JS_SetPropertyStr(context, global, "Socket", tf_socket_register(context));
JS_SetPropertyStr(context, global, "TlsContext", tf_tls_context_register(context));
@ -1576,7 +1399,6 @@ void tf_task_activate(tf_task_t* task)
}
else
{
JS_SetPropertyStr(context, global, "require", JS_NewCFunction(context, _tf_task_sandbox_require, "sandboxRequire", 0));
tf_trace_set_write_callback(task->_trace, _tf_task_trace_to_parent, task);
}
@ -1653,16 +1475,7 @@ void tf_task_destroy(tf_task_t* task)
}
tf_free(task->_promises);
task->_promises = NULL;
JS_FreeValue(task->_context, task->_requires);
JS_FreeValue(task->_context, task->_loadedFiles);
while (task->_scriptExports)
{
script_export_t* export = task->_scriptExports;
JS_FreeValue(task->_context, export->value);
task->_scriptExports = export->next;
tf_free((void*)export->name);
tf_free(export);
}
if (task->_ssb)
{

View File

@ -299,32 +299,29 @@ static void _test_await(const tf_test_options_t* options)
unlink("out/test.js");
}
static void _test_require(const tf_test_options_t* options)
static void _test_import(const tf_test_options_t* options)
{
FILE* file = fopen("out/test.js", "w");
fprintf(file,
"if (require('required').foo() != 12345) {\n"
"import * as req from './required.js';\n"
"if (req.foo() != 12345) {\n"
" exit(1);\n"
"}\n"
"var gotError = false;\n"
"try {\n"
" require('missing');\n"
"} catch (error) {\n"
" print('nope');\n"
" gotError = true;\n"
"}\n"
"if (!gotError) {\n"
" exit(2);\n"
"}\n"
"exit(0);\n");
"}\n");
fclose(file);
file = fopen("out/required.js", "w");
fprintf(file,
"function foo() {\n"
"export function foo() {\n"
" return 12345;\n"
"}\n"
"exports.foo = foo;\n");
"}\n");
fclose(file);
file = fopen("out/bad.js", "w");
fprintf(file,
"import * as req from './missing.js';\n"
"if (req.foo() != 12345) {\n"
" exit(1);\n"
"}\n");
fclose(file);
char command[256];
@ -335,14 +332,22 @@ static void _test_require(const tf_test_options_t* options)
assert(WIFEXITED(result));
assert(WEXITSTATUS(result) == 0);
snprintf(command, sizeof(command), "%s run --ssb-port=0 -s out/bad.js", options->exe_path);
printf("%s\n", command);
result = system(command);
printf("returned %d\n", WEXITSTATUS(result));
assert(WIFEXITED(result));
assert(WEXITSTATUS(result) == 0);
unlink("out/test.js");
unlink("out/required.js");
unlink("out/missing.js");
}
static void _test_exit(const tf_test_options_t* options)
{
FILE* file = fopen("out/test.js", "w");
fprintf(file, "require('blah');");
fprintf(file, "import * as blah from './blah.js';\n");
fclose(file);
file = fopen("out/blah.js", "w");
@ -595,7 +600,7 @@ void tf_tests(const tf_test_options_t* options)
_tf_test_run(options, "database", _test_database);
_tf_test_run(options, "this", _test_this);
_tf_test_run(options, "await", _test_await);
_tf_test_run(options, "require", _test_require);
_tf_test_run(options, "import", _test_import);
_tf_test_run(options, "exit", _test_exit);
_tf_test_run(options, "icu", _test_icu);
_tf_test_run(options, "uint8array", _test_uint8array);