2021-01-02 13:10:00 -05:00
|
|
|
#include "task.h"
|
|
|
|
|
2021-10-24 11:46:30 -04:00
|
|
|
#include "bcrypt.js.h"
|
|
|
|
#include "database.js.h"
|
|
|
|
#include "file.js.h"
|
2021-01-02 13:10:00 -05:00
|
|
|
#include "packetstream.h"
|
|
|
|
#include "serialize.h"
|
2021-10-24 11:46:30 -04:00
|
|
|
#include "socket.js.h"
|
2021-01-02 13:10:00 -05:00
|
|
|
#include "ssb.h"
|
2021-10-24 11:46:30 -04:00
|
|
|
#include "ssb.js.h"
|
|
|
|
#include "taskstub.js.h"
|
|
|
|
#include "tlscontext.js.h"
|
2021-01-02 13:10:00 -05:00
|
|
|
#include "trace.h"
|
2021-11-03 18:15:46 -04:00
|
|
|
#include "util.js.h"
|
2021-01-02 13:10:00 -05:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <uv.h>
|
|
|
|
|
|
|
|
#include <sqlite3.h>
|
|
|
|
|
|
|
|
#include "quickjs.h"
|
|
|
|
#include "quickjs-libc.h"
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
static const int STDIN_FILENO = 0;
|
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static const char* k_version = "1.0";
|
|
|
|
|
|
|
|
static JSClassID _import_class_id;
|
|
|
|
static int _count;
|
|
|
|
|
|
|
|
typedef struct _export_record_t export_record_t;
|
|
|
|
typedef struct _import_record_t import_record_t;
|
|
|
|
|
|
|
|
typedef struct _task_child_node_t task_child_node_t;
|
|
|
|
|
|
|
|
typedef struct _task_child_node_t {
|
|
|
|
taskid_t id;
|
|
|
|
tf_taskstub_t* stub;
|
|
|
|
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 {
|
|
|
|
promiseid_t id;
|
|
|
|
JSValue values[3];
|
|
|
|
} promise_t;
|
|
|
|
|
|
|
|
typedef struct _tf_task_t {
|
|
|
|
taskid_t _nextTask;
|
|
|
|
task_child_node_t* _children;
|
|
|
|
int _child_count;
|
|
|
|
|
|
|
|
tf_taskstub_t* _stub;
|
|
|
|
tf_taskstub_t* _parent;
|
|
|
|
const char* _path;
|
|
|
|
|
|
|
|
script_export_t* _scriptExports;
|
|
|
|
|
2022-01-02 13:17:58 -05:00
|
|
|
bool _activated;
|
2021-01-02 13:10:00 -05:00
|
|
|
bool _trusted;
|
|
|
|
bool _killed;
|
|
|
|
int32_t _exitCode;
|
|
|
|
char _scriptName[256];
|
|
|
|
|
|
|
|
JSRuntime* _runtime;
|
|
|
|
JSContext* _context;
|
|
|
|
|
|
|
|
sqlite3* _db;
|
|
|
|
|
|
|
|
tf_ssb_t* _ssb;
|
|
|
|
|
|
|
|
tf_trace_t* _trace;
|
|
|
|
|
|
|
|
promise_t* _promises;
|
|
|
|
int _promise_count;
|
|
|
|
promiseid_t _nextPromise;
|
2021-11-14 17:55:21 -05:00
|
|
|
uv_loop_t _loop;
|
2021-01-02 13:10:00 -05:00
|
|
|
|
2022-01-09 20:50:58 -05:00
|
|
|
export_record_t** _exports;
|
2021-01-02 13:10:00 -05:00
|
|
|
int _export_count;
|
|
|
|
exportid_t _nextExport;
|
|
|
|
|
2022-01-08 16:56:57 -05:00
|
|
|
import_record_t** _imports;
|
2021-01-02 13:10:00 -05:00
|
|
|
int _import_count;
|
|
|
|
JSValue _requires;
|
|
|
|
JSValue _loadedFiles;
|
|
|
|
|
|
|
|
int _ssb_port;
|
|
|
|
int _http_port;
|
|
|
|
int _https_port;
|
2021-11-14 17:55:21 -05:00
|
|
|
char _db_path[256];
|
|
|
|
char _secrets_path[256];
|
2021-12-01 18:29:53 -05:00
|
|
|
const char* _args;
|
2021-01-02 13:10:00 -05:00
|
|
|
} tf_task_t;
|
|
|
|
|
|
|
|
typedef struct _export_record_t {
|
|
|
|
taskid_t _taskid;
|
2022-01-09 20:50:58 -05:00
|
|
|
exportid_t _export_id;
|
2021-01-02 13:10:00 -05:00
|
|
|
JSValue _function;
|
|
|
|
int _useCount;
|
|
|
|
} export_record_t;
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static void _export_record_ref(export_record_t* export)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
export->_useCount++;
|
|
|
|
}
|
|
|
|
|
2022-01-09 20:50:58 -05:00
|
|
|
static bool _export_record_release(tf_task_t* task, export_record_t** export)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2022-01-09 20:50:58 -05:00
|
|
|
if (--(*export)->_useCount == 0)
|
|
|
|
{
|
|
|
|
JS_FreeValue(task->_context, (*export)->_function);
|
|
|
|
(*export)->_function = JS_UNDEFINED;
|
|
|
|
free(*export);
|
|
|
|
int index = export - task->_exports;
|
|
|
|
if (task->_export_count - index)
|
|
|
|
{
|
|
|
|
memmove(export, export + 1, sizeof(export_record_t*) * (task->_export_count - index - 1));
|
|
|
|
}
|
|
|
|
task->_export_count--;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2021-01-10 21:09:19 -05:00
|
|
|
static JSValue _tf_task_sandbox_require(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
2021-01-02 13:10:00 -05:00
|
|
|
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 void _tf_task_sendPromiseMessage(tf_task_t* from, tf_taskstub_t* to, tf_task_message_t messageType, promiseid_t promise, JSValue result);
|
|
|
|
static void _tf_task_sendPromiseResolve(tf_task_t* from, tf_taskstub_t* to, promiseid_t promise, JSValue result);
|
|
|
|
static void _tf_task_sendPromiseReject(tf_task_t* from, tf_taskstub_t* to, promiseid_t promise, JSValue result);
|
|
|
|
|
|
|
|
static void _tf_task_sendPromiseExportMessage(tf_task_t* from, tf_taskstub_t* to, tf_task_message_t messageType, promiseid_t promiseId, exportid_t exportId, JSValue result);
|
|
|
|
static JSValue _tf_task_executeSource(tf_task_t* task, const char* source, const char* name);
|
|
|
|
static tf_taskstub_t* _tf_task_get_stub(tf_task_t* task, taskid_t id);
|
|
|
|
static void _tf_task_release_export(tf_taskstub_t* stub, exportid_t exportId);
|
|
|
|
|
|
|
|
typedef struct _import_record_t {
|
2022-01-08 16:56:57 -05:00
|
|
|
tf_task_t* _owner;
|
2021-01-02 13:10:00 -05:00
|
|
|
JSValue _function;
|
|
|
|
exportid_t _export;
|
|
|
|
taskid_t _task;
|
|
|
|
int _useCount;
|
|
|
|
} import_record_t;
|
|
|
|
|
2022-01-08 16:56:57 -05:00
|
|
|
static int _import_compare(const void* a, const void* b)
|
|
|
|
{
|
|
|
|
const import_record_t* ia = a;
|
|
|
|
const import_record_t* const* ib = b;
|
|
|
|
if (ia->_task != (*ib)->_task)
|
|
|
|
{
|
|
|
|
return ia->_task < (*ib)->_task ? -1 : 1;
|
|
|
|
}
|
|
|
|
else if (ia->_export != (*ib)->_export)
|
|
|
|
{
|
|
|
|
return ia->_export < (*ib)->_export ? -1 : 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static import_record_t** _tf_task_find_import(tf_task_t* task, taskid_t task_id, exportid_t export_id)
|
|
|
|
{
|
|
|
|
if (!task->_imports)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
import_record_t search =
|
|
|
|
{
|
|
|
|
._task = task_id,
|
|
|
|
._export = export_id,
|
|
|
|
};
|
|
|
|
import_record_t** it = bsearch(&search, task->_imports, task->_import_count, sizeof(import_record_t*), _import_compare);
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
static void _tf_task_trace_imports(tf_task_t* task)
|
|
|
|
{
|
|
|
|
tf_trace_counter(task->_trace, "imports", 1, (const char*[]) { "count" }, (int64_t[]) { task->_import_count });
|
|
|
|
}
|
|
|
|
|
2022-01-08 16:56:57 -05:00
|
|
|
static void _import_record_release(import_record_t** import)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2022-01-08 16:56:57 -05:00
|
|
|
JSContext* context = (*import)->_owner->_context;
|
|
|
|
tf_task_t* task = (*import)->_owner;
|
|
|
|
import_record_t* record = *import;
|
|
|
|
|
|
|
|
if (--record->_useCount > 0)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-08 16:56:57 -05:00
|
|
|
JS_SetOpaque(record->_function, NULL);
|
|
|
|
JS_FreeValue(context, record->_function);
|
|
|
|
record->_function = JS_UNDEFINED;
|
|
|
|
_tf_task_release_export(_tf_task_get_stub(task, record->_task), record->_export);
|
2021-01-02 13:10:00 -05:00
|
|
|
|
2022-01-08 16:56:57 -05:00
|
|
|
int index = import - task->_imports;
|
|
|
|
memmove(import, import + 1, sizeof(import_record_t*) * (task->_import_count - index - 1));
|
|
|
|
task->_import_count--;
|
|
|
|
_tf_task_trace_imports(task);
|
2021-01-02 13:10:00 -05:00
|
|
|
|
2022-01-08 16:56:57 -05:00
|
|
|
free(record);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void _import_record_release_for_task(tf_task_t* task, taskid_t task_id)
|
|
|
|
{
|
|
|
|
bool any = false;
|
2022-01-08 16:56:57 -05:00
|
|
|
for (int i = 0; i < task->_import_count; i++)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2022-01-08 16:56:57 -05:00
|
|
|
if (task->_imports[i]->_task == task_id)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2022-01-08 16:56:57 -05:00
|
|
|
_import_record_release(&task->_imports[i]);
|
2021-01-02 13:10:00 -05:00
|
|
|
any = true;
|
2022-01-08 16:56:57 -05:00
|
|
|
--i;
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
if (any)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
_tf_task_trace_imports(task);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _tf_task_trace_exports(tf_task_t* task)
|
|
|
|
{
|
|
|
|
tf_trace_counter(task->_trace, "exports", 1, (const char*[]) { "count" }, (int64_t[]) { task->_export_count });
|
|
|
|
}
|
|
|
|
|
2022-01-09 20:50:58 -05:00
|
|
|
static int _export_compare(const void* a, const void* b)
|
|
|
|
{
|
|
|
|
exportid_t ia = *(const exportid_t*)a;
|
|
|
|
const export_record_t* const* ib = b;
|
|
|
|
if (ia != (*ib)->_export_id)
|
|
|
|
{
|
|
|
|
return ia < (*ib)->_export_id ? -1 : 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
static void _export_record_release_for_task(tf_task_t* task, taskid_t task_id)
|
|
|
|
{
|
|
|
|
bool any = false;
|
2022-01-09 20:50:58 -05:00
|
|
|
for (int i = 0; i < task->_export_count; i++)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2022-01-09 20:50:58 -05:00
|
|
|
if (task->_exports[i]->_taskid == task_id)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2022-01-09 20:50:58 -05:00
|
|
|
if (_export_record_release(task, &task->_exports[i]))
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
any = true;
|
2022-01-09 20:50:58 -05:00
|
|
|
i--;
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
if (any)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
_tf_task_trace_exports(task);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-03 18:28:25 -04:00
|
|
|
void tf_task_send_error_to_parent(tf_task_t* task, JSValue error)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2021-11-03 18:28:25 -04:00
|
|
|
if (task && task->_parent)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
void* buffer = NULL;
|
|
|
|
size_t size = 0;
|
|
|
|
tf_serialize_store(task, task->_parent, &buffer, &size, error);
|
|
|
|
tf_packetstream_send(tf_taskstub_get_stream(task->_parent), kTaskError, buffer, size);
|
|
|
|
free(buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static const char* _task_loadFile(const char* fileName)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
char* result = NULL;
|
|
|
|
FILE* file = fopen(fileName, "rb");
|
2021-10-10 17:51:38 -04:00
|
|
|
if (file)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
fseek(file, 0, SEEK_END);
|
|
|
|
long fileSize = ftell(file);
|
|
|
|
fseek(file, 0, SEEK_SET);
|
|
|
|
result = malloc(fileSize + 1);
|
|
|
|
fread(result, 1, fileSize, file);
|
|
|
|
result[fileSize] = '\0';
|
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
JSValue _tf_task_exit(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_task_t* task = JS_GetContextOpaque(context);
|
|
|
|
int exitCode = 0;
|
|
|
|
JS_ToInt32(task->_context, &exitCode, argv[0]);
|
|
|
|
exit(exitCode);
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
int tf_task_execute(tf_task_t* task, const char* fileName)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
bool executed = false;
|
|
|
|
|
|
|
|
tf_trace_begin(task->_trace, "tf_task_execute");
|
|
|
|
const char* source = _task_loadFile(fileName);
|
|
|
|
printf("Running script %s\n", fileName);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (!*task->_scriptName)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
strncpy(task->_scriptName, fileName, sizeof(task->_scriptName) - 1);
|
|
|
|
}
|
2021-10-10 17:51:38 -04:00
|
|
|
if (!task->_path)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
char* path = strdup(fileName);
|
|
|
|
char* slash = strrchr(path, '/');
|
2021-10-10 17:51:38 -04:00
|
|
|
if (slash)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
*slash = '\0';
|
|
|
|
task->_path = strdup(path);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
task->_path = strdup("./");
|
|
|
|
}
|
|
|
|
free(path);
|
|
|
|
}
|
2021-10-10 17:51:38 -04:00
|
|
|
if (source)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JSValue result = JS_Eval(task->_context, source, strlen(source), fileName, 0);
|
2021-11-03 18:28:25 -04:00
|
|
|
tf_util_report_error(task->_context, result);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (!JS_IsError(task->_context, result) && !JS_IsException(result))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
executed = true;
|
|
|
|
}
|
|
|
|
JS_FreeValue(task->_context, result);
|
|
|
|
tf_task_run_jobs(task);
|
|
|
|
free((void*)source);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
printf("Failed to load file: %s.\n", fileName);
|
|
|
|
}
|
|
|
|
tf_trace_end(task->_trace);
|
|
|
|
return executed;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static tf_taskstub_t* _tf_task_get_stub(tf_task_t* task, taskid_t id)
|
|
|
|
{
|
|
|
|
if (id == k_task_parent_id)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
return task->_parent;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
for (task_child_node_t* node = task->_children; node; node = node->next)
|
|
|
|
{
|
|
|
|
if (node->id == id)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
return node->stub;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static JSValue _import_call(JSContext* context, JSValueConst func_obj, JSValueConst this_val, int argc, JSValueConst *argv, int flags)
|
|
|
|
{
|
|
|
|
import_record_t* import = JS_GetOpaque(func_obj, _import_class_id);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (!import)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
return JS_ThrowInternalError(context, "Invoking a function that has been released.");
|
|
|
|
}
|
|
|
|
import->_useCount++;
|
|
|
|
|
|
|
|
JSValue array = JS_NewArray(context);
|
|
|
|
JS_SetPropertyUint32(context, array, 0, JS_DupValue(context, this_val));
|
2021-10-10 17:51:38 -04:00
|
|
|
for (int i = 0; i < argc; ++i)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JS_SetPropertyUint32(context, array, i + 1, JS_DupValue(context, argv[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
tf_task_t* sender = JS_GetContextOpaque(context);
|
|
|
|
JSValue result;
|
|
|
|
tf_taskstub_t* recipient = _tf_task_get_stub(sender, import->_task);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (recipient)
|
|
|
|
{
|
2022-01-13 22:05:37 -05:00
|
|
|
promiseid_t promise = -1;
|
|
|
|
result = tf_task_allocate_promise(sender, &promise);
|
2021-01-02 13:10:00 -05:00
|
|
|
_tf_task_sendPromiseExportMessage(sender, recipient, kInvokeExport, promise, import->_export, array);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
result = JS_ThrowInternalError(context, "Invoking a function on a nonexistent task.");
|
|
|
|
}
|
|
|
|
JS_FreeValue(context, array);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-01-09 20:50:58 -05:00
|
|
|
static export_record_t** _task_get_export(tf_task_t* task, exportid_t export_id)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2022-01-09 20:50:58 -05:00
|
|
|
export_record_t** it = bsearch(&export_id, task->_exports, task->_export_count, sizeof(export_record_t*), _export_compare);
|
|
|
|
return it;
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
JSValue _task_invokeExport_internal(tf_taskstub_t* from, tf_task_t* to, exportid_t exportId, const char* buffer, size_t size)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JSValue result = JS_NULL;
|
2022-01-09 20:50:58 -05:00
|
|
|
export_record_t** it = _task_get_export(to, exportId);
|
|
|
|
export_record_t* export = it ? *it : NULL;
|
2021-10-10 17:51:38 -04:00
|
|
|
if (export)
|
|
|
|
{
|
2022-01-02 13:17:58 -05:00
|
|
|
JSValue arguments = tf_serialize_load(to, from, buffer, size);
|
2021-01-02 13:10:00 -05:00
|
|
|
JSValue* argument_array = NULL;
|
|
|
|
JSValue length_val = JS_GetPropertyStr(to->_context, arguments, "length");
|
|
|
|
int length;
|
|
|
|
JSValue this_val = JS_NULL;
|
2021-10-10 17:51:38 -04:00
|
|
|
if (JS_ToInt32(to->_context, &length, length_val) == 0)
|
|
|
|
{
|
|
|
|
if (length > 0)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
this_val = JS_GetPropertyUint32(to->_context, arguments, 0);
|
|
|
|
argument_array = alloca(sizeof(JSValue) * (length - 1));
|
|
|
|
}
|
2021-10-10 17:51:38 -04:00
|
|
|
for (int i = 1; i < length; ++i)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
argument_array[i - 1] = JS_GetPropertyUint32(to->_context, arguments, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
JSValue function = export->_function;
|
|
|
|
JSPropertyDescriptor desc;
|
|
|
|
const char* name = NULL;
|
|
|
|
JSAtom atom = JS_NewAtom(to->_context, "name");
|
2021-10-10 17:51:38 -04:00
|
|
|
if (JS_GetOwnProperty(to->_context, &desc, function, atom) == 1)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
name = JS_ToCString(to->_context, desc.value);
|
|
|
|
JS_FreeValue(to->_context, desc.value);
|
|
|
|
}
|
|
|
|
JS_FreeAtom(to->_context, atom);
|
|
|
|
tf_trace_begin(to->_trace, name ? name : "_task_invokeExport_internal");
|
2021-10-10 17:51:38 -04:00
|
|
|
if (name)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JS_FreeCString(to->_context, name);
|
|
|
|
}
|
|
|
|
result = JS_Call(to->_context, function, this_val, length - 1, argument_array);
|
|
|
|
tf_trace_end(to->_trace);
|
2021-11-03 18:28:25 -04:00
|
|
|
tf_util_report_error(to->_context, result);
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_task_run_jobs(to);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
printf("%s: That's not an export we have (exportId=%d, exports=%d)\n", to->_scriptName, exportId, to->_export_count);
|
|
|
|
}
|
|
|
|
tf_packetstream_send(tf_taskstub_get_stream(from), kReleaseImport, (void*)&exportId, sizeof(exportId));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static JSValue _invokeThen(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv, int magic, JSValue* func_data)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
taskid_t taskid = 0;
|
|
|
|
JS_ToInt32(context, &taskid, func_data[0]);
|
|
|
|
tf_task_t* from = JS_GetContextOpaque(context);
|
|
|
|
tf_taskstub_t* to = _tf_task_get_stub(from, taskid);
|
|
|
|
promiseid_t promiseid = 0;
|
|
|
|
JS_ToInt32(context, &promiseid, func_data[1]);
|
|
|
|
_tf_task_sendPromiseMessage(from, to, kResolvePromise, promiseid, argv[0]);
|
|
|
|
return JS_DupValue(context, this_val);
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static JSValue _invokeCatch(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv, int magic, JSValue* func_data)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
taskid_t taskid = 0;
|
|
|
|
JS_ToInt32(context, &taskid, func_data[0]);
|
|
|
|
tf_task_t* from = JS_GetContextOpaque(context);
|
|
|
|
tf_taskstub_t* to = _tf_task_get_stub(from, taskid);
|
|
|
|
promiseid_t promiseid = 0;
|
|
|
|
JS_ToInt32(context, &promiseid, func_data[1]);
|
|
|
|
_tf_task_sendPromiseMessage(from, to, kRejectPromise, promiseid, argv[0]);
|
|
|
|
return JS_DupValue(context, this_val);
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static void _forward_promise(tf_task_t* from, tf_taskstub_t* to, promiseid_t promise, JSValue result)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
// We're not going to serialize/deserialize a promise...
|
|
|
|
JSValue data[] = {
|
|
|
|
JS_NewInt32(from->_context, tf_taskstub_get_id(to)),
|
|
|
|
JS_NewInt32(from->_context, promise),
|
|
|
|
};
|
|
|
|
JSValue promise_then = JS_GetPropertyStr(from->_context, result, "then");
|
|
|
|
JSValue promise_catch = JS_GetPropertyStr(from->_context, result, "catch");
|
|
|
|
|
|
|
|
JSValue then_handler = JS_NewCFunctionData(from->_context, _invokeThen, 0, 0, 2, data);
|
|
|
|
JSValue catch_handler = JS_NewCFunctionData(from->_context, _invokeCatch, 0, 0, 2, data);
|
|
|
|
|
|
|
|
JSValue error = JS_Call(from->_context, promise_then, result, 1, &then_handler);
|
2021-11-03 18:28:25 -04:00
|
|
|
tf_util_report_error(from->_context, error);
|
2021-09-06 16:54:44 -04:00
|
|
|
JS_FreeValue(from->_context, error);
|
2021-01-02 13:10:00 -05:00
|
|
|
error = JS_Call(from->_context, promise_catch, result, 1, &catch_handler);
|
2021-11-03 18:28:25 -04:00
|
|
|
tf_util_report_error(from->_context, error);
|
2021-09-06 16:54:44 -04:00
|
|
|
JS_FreeValue(from->_context, error);
|
2021-01-02 13:10:00 -05:00
|
|
|
|
|
|
|
tf_task_run_jobs(from);
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static void _tf_task_sendPromiseResolve(tf_task_t* from, tf_taskstub_t* to, promiseid_t promise, JSValue result)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JSValue global = JS_GetGlobalObject(from->_context);
|
|
|
|
JSValue promiseType = JS_GetPropertyStr(from->_context, global, "Promise");
|
|
|
|
JS_FreeValue(from->_context, global);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (JS_IsInstanceOf(from->_context, result, promiseType))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
_forward_promise(from, to, promise, result);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
_tf_task_sendPromiseMessage(from, to, kResolvePromise, promise, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static void _tf_task_sendPromiseReject(tf_task_t* from, tf_taskstub_t* to, promiseid_t promise, JSValue result)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JSValue global = JS_GetGlobalObject(from->_context);
|
|
|
|
JSValue promiseType = JS_GetPropertyStr(from->_context, global, "Promise");
|
|
|
|
JS_FreeValue(from->_context, global);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (JS_IsInstanceOf(from->_context, result, promiseType))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
_forward_promise(from, to, promise, result);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
_tf_task_sendPromiseMessage(from, to, kRejectPromise, promise, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static void _tf_task_sendPromiseMessage(tf_task_t* from, tf_taskstub_t* to, tf_task_message_t messageType, promiseid_t promise, JSValue result)
|
|
|
|
{
|
|
|
|
if (to)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
void* buffer;
|
|
|
|
size_t size;
|
|
|
|
tf_serialize_store(from, to, &buffer, &size, result);
|
|
|
|
|
|
|
|
char* copy = (char*)malloc(sizeof(promise) + size);
|
|
|
|
memcpy(copy, &promise, sizeof(promise));
|
|
|
|
memcpy(copy + sizeof(promise), buffer, size);
|
|
|
|
tf_packetstream_send(tf_taskstub_get_stream(to), messageType, copy, size + sizeof(promise));
|
|
|
|
free(buffer);
|
|
|
|
free(copy);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
printf("Sending to a NULL task.\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static void _tf_task_sendPromiseExportMessage(tf_task_t* from, tf_taskstub_t* to, tf_task_message_t messageType, promiseid_t promise, exportid_t exportId, JSValue result)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
void* buffer;
|
|
|
|
size_t size;
|
|
|
|
tf_serialize_store(from, to, &buffer, &size, result);
|
|
|
|
char* copy = (char*)malloc(sizeof(promise) + sizeof(exportId) + size);
|
|
|
|
memcpy(copy, &promise, sizeof(promise));
|
|
|
|
memcpy(copy + sizeof(promise), &exportId, sizeof(exportId));
|
|
|
|
memcpy(copy + sizeof(promise) + sizeof(exportId), buffer, size);
|
|
|
|
tf_packetstream_send(tf_taskstub_get_stream(to), messageType, copy, sizeof(promise) + sizeof(exportId) + size);
|
|
|
|
free(buffer);
|
|
|
|
free(copy);
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
JSValue _tf_task_get_parent(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_task_t* task = JS_GetContextOpaque(context);
|
|
|
|
return task->_parent ? tf_taskstub_get_task_object(task->_parent) : JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static JSValue _tf_task_version(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_task_t* task = JS_GetContextOpaque(context);
|
|
|
|
return JS_NewString(task->_context, k_version);
|
|
|
|
}
|
|
|
|
|
2022-01-09 20:50:58 -05:00
|
|
|
static int _insert_index(const void* key, const void* base, size_t count, size_t size, int (*compare)(const void*, const void*))
|
|
|
|
{
|
|
|
|
int lower = 0;
|
|
|
|
int upper = count;
|
|
|
|
while (lower < upper && lower < (int)count)
|
|
|
|
{
|
|
|
|
int guess = (lower + upper) / 2;
|
|
|
|
int result = compare(key, ((char*)base) + size * guess);
|
|
|
|
if (result < 0)
|
|
|
|
{
|
|
|
|
upper = guess;
|
|
|
|
}
|
|
|
|
else if (result > 0)
|
|
|
|
{
|
|
|
|
lower = guess + 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return guess;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return lower;
|
|
|
|
}
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
exportid_t tf_task_export_function(tf_task_t* task, tf_taskstub_t* to, JSValue function)
|
|
|
|
{
|
|
|
|
export_record_t* export = NULL;
|
2022-01-09 20:50:58 -05:00
|
|
|
/* TODO: _exports_by_function */
|
|
|
|
for (int i = 0; i < task->_export_count; i++)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2022-01-09 20:50:58 -05:00
|
|
|
if (JS_VALUE_GET_PTR(task->_exports[i]->_function) == JS_VALUE_GET_PTR(function))
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2022-01-09 20:50:58 -05:00
|
|
|
export = task->_exports[i];
|
2021-01-02 13:10:00 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
if (!export)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
int id = -1;
|
|
|
|
do {
|
|
|
|
id = task->_nextExport++;
|
|
|
|
} while (_task_get_export(task, id));
|
|
|
|
|
|
|
|
export = malloc(sizeof(export_record_t));
|
2021-10-10 17:51:38 -04:00
|
|
|
*export = (export_record_t)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
._export_id = id,
|
|
|
|
._taskid = tf_taskstub_get_id(to),
|
|
|
|
._function = JS_DupValue(task->_context, function),
|
|
|
|
};
|
2022-01-09 20:50:58 -05:00
|
|
|
|
|
|
|
int index = _insert_index(&id, task->_exports, task->_export_count, sizeof(export_record_t*), _export_compare);
|
|
|
|
task->_exports = realloc(task->_exports, sizeof(export_record_t*) * (task->_export_count + 1));
|
|
|
|
if (task->_export_count - index)
|
|
|
|
{
|
|
|
|
memmove(task->_exports + index + 1, task->_exports + index, sizeof(export_record_t*) * (task->_export_count - index));
|
|
|
|
}
|
|
|
|
task->_exports[index] = export;
|
2021-01-02 13:10:00 -05:00
|
|
|
task->_export_count++;
|
|
|
|
_tf_task_trace_exports(task);
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
if (export)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
_export_record_ref(export);
|
|
|
|
}
|
|
|
|
|
|
|
|
return export ? export->_export_id : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _tf_task_release_export(tf_taskstub_t* stub, exportid_t exportId)
|
|
|
|
{
|
2021-10-10 17:51:38 -04:00
|
|
|
if (stub)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_packetstream_t* stream = tf_taskstub_get_stream(stub);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (stream)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_packetstream_send(stream, kReleaseExport, (void*)&exportId, sizeof(exportId));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSValue _tf_task_trace(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv){
|
|
|
|
tf_task_t* task = JS_GetContextOpaque(context);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (!task->_trace)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* trace = tf_trace_export(task->_trace);
|
|
|
|
JSValue result = JS_NewString(context, trace);
|
|
|
|
free(trace);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSValue _tf_task_getFile(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
tf_task_t* task = JS_GetContextOpaque(context);
|
|
|
|
const char* name = JS_ToCString(context, argv[0]);
|
|
|
|
JSValue result = JS_GetPropertyStr(context, task->_loadedFiles, name);
|
|
|
|
JS_FreeCString(context, name);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-01-08 16:56:57 -05:00
|
|
|
const char* _tf_task_get_message_type(tf_task_message_t type)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case kResolvePromise: return "kResolvePromise";
|
|
|
|
case kRejectPromise: return "kRejectPromise";
|
|
|
|
case kInvokeExport: return "kInvokeExport";
|
|
|
|
case kReleaseExport: return "kReleaseExport";
|
|
|
|
case kReleaseImport: return "kReleaseImport";
|
|
|
|
case kSetRequires: return "kSetRequires";
|
|
|
|
case kActivate: return "kActivate";
|
|
|
|
case kExecute: return "kExecute";
|
|
|
|
case kKill: return "kKill";
|
|
|
|
case kSetImports: return "kSetImports";
|
|
|
|
case kGetExports: return "kGetExports";
|
|
|
|
case kLoadFile: return "kLoadFile";
|
|
|
|
case kTaskError: return "kTaskError";
|
|
|
|
case kTaskTrace: return "kTaskTrace";
|
|
|
|
}
|
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
void tf_task_on_receive_packet(int packetType, const char* begin, size_t length, void* userData)
|
|
|
|
{
|
|
|
|
tf_taskstub_t* stub = userData;
|
|
|
|
tf_taskstub_t* from = stub;
|
|
|
|
tf_task_t* to = tf_taskstub_get_owner(stub);
|
2022-01-08 16:56:57 -05:00
|
|
|
tf_trace_begin(to->_trace, _tf_task_get_message_type(packetType));
|
2021-01-02 13:10:00 -05:00
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
switch (packetType)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
case kInvokeExport:
|
|
|
|
{
|
|
|
|
promiseid_t promise;
|
|
|
|
exportid_t exportId;
|
|
|
|
memcpy(&promise, begin, sizeof(promise));
|
|
|
|
memcpy(&exportId, begin + sizeof(promise), sizeof(exportId));
|
|
|
|
|
|
|
|
JSValue result = _task_invokeExport_internal(from, to, exportId, begin + sizeof(promiseid_t) + sizeof(exportid_t), length);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (JS_IsException(result))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
_tf_task_sendPromiseReject(to, from, promise, result);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
_tf_task_sendPromiseResolve(to, from, promise, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kResolvePromise:
|
|
|
|
case kRejectPromise:
|
|
|
|
{
|
|
|
|
JSValue arg = JS_UNDEFINED;
|
|
|
|
promiseid_t promise;
|
|
|
|
memcpy(&promise, begin, sizeof(promiseid_t));
|
2021-10-10 17:51:38 -04:00
|
|
|
if (length > sizeof(promiseid_t))
|
|
|
|
{
|
2022-01-02 13:17:58 -05:00
|
|
|
arg = tf_serialize_load(to, from, begin + sizeof(promiseid_t), length - sizeof(promiseid_t));
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
2021-10-10 17:51:38 -04:00
|
|
|
if (packetType == kResolvePromise)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_task_resolve_promise(to, promise, arg);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_task_reject_promise(to, promise, arg);
|
|
|
|
}
|
|
|
|
JS_FreeValue(to->_context, arg);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kReleaseExport:
|
|
|
|
{
|
|
|
|
assert(length == sizeof(exportid_t));
|
|
|
|
exportid_t exportId;
|
|
|
|
memcpy(&exportId, begin, sizeof(exportId));
|
2022-01-09 20:50:58 -05:00
|
|
|
export_record_t** it = _task_get_export(to, exportId);
|
|
|
|
if (it)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2022-01-09 20:50:58 -05:00
|
|
|
_export_record_release(to, it);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kReleaseImport:
|
|
|
|
{
|
|
|
|
assert(length == sizeof(exportid_t));
|
|
|
|
exportid_t exportId;
|
|
|
|
memcpy(&exportId, begin, sizeof(exportId));
|
2022-01-08 16:56:57 -05:00
|
|
|
import_record_t** it = _tf_task_find_import(to, tf_taskstub_get_id(from), exportId);
|
|
|
|
if (it)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2022-01-08 16:56:57 -05:00
|
|
|
_import_record_release(it);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kSetRequires:
|
2022-01-02 13:17:58 -05:00
|
|
|
to->_requires = tf_serialize_load(to, from, begin, length);
|
2021-01-02 13:10:00 -05:00
|
|
|
break;
|
|
|
|
case kLoadFile:
|
|
|
|
{
|
2022-01-02 13:17:58 -05:00
|
|
|
JSValue args = tf_serialize_load(to, from, begin, length);
|
2021-01-02 13:10:00 -05:00
|
|
|
JSValue key = JS_GetPropertyUint32(to->_context, args, 0);
|
|
|
|
JSValue content = JS_GetPropertyUint32(to->_context, args, 1);
|
|
|
|
const char* name = JS_ToCString(to->_context, key);
|
|
|
|
JS_SetPropertyStr(to->_context, to->_loadedFiles, name, JS_DupValue(to->_context, content));
|
|
|
|
JS_FreeCString(to->_context, name);
|
|
|
|
JS_FreeValue(to->_context, key);
|
|
|
|
JS_FreeValue(to->_context, content);
|
|
|
|
JS_FreeValue(to->_context, args);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kActivate:
|
|
|
|
tf_task_activate(to);
|
|
|
|
break;
|
|
|
|
case kExecute:
|
|
|
|
{
|
|
|
|
assert(length >= sizeof(promiseid_t));
|
|
|
|
promiseid_t promise;
|
|
|
|
memcpy(&promise, begin, sizeof(promiseid_t));
|
2022-01-02 13:17:58 -05:00
|
|
|
JSValue value = tf_serialize_load(to, from, begin + sizeof(promiseid_t), length - sizeof(promiseid_t));
|
2021-01-02 13:10:00 -05:00
|
|
|
JSValue source = JS_GetPropertyStr(to->_context, value, "source");
|
|
|
|
JSValue name_val = JS_GetPropertyStr(to->_context, value, "name");
|
|
|
|
const char* name = JS_ToCString(to->_context, name_val);
|
|
|
|
JS_FreeValue(to->_context, name_val);
|
|
|
|
JS_FreeValue(to->_context, value);
|
|
|
|
|
2021-11-03 18:15:46 -04:00
|
|
|
JSValue utf8 = tf_util_utf8_decode(to->_context, source);
|
2021-01-02 13:10:00 -05:00
|
|
|
const char* source_str = JS_ToCString(to->_context, utf8);
|
|
|
|
JS_FreeValue(to->_context, utf8);
|
2022-01-02 21:25:11 -05:00
|
|
|
JSValue result = _tf_task_executeSource(to, source_str, name);
|
|
|
|
if (JS_IsException(result))
|
|
|
|
{
|
|
|
|
_tf_task_sendPromiseReject(to, from, promise, JS_GetException(to->_context));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_tf_task_sendPromiseResolve(to, from, promise, result);
|
|
|
|
}
|
2021-01-02 13:10:00 -05:00
|
|
|
JS_FreeCString(to->_context, source_str);
|
|
|
|
JS_FreeCString(to->_context, name);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kKill:
|
|
|
|
exit(1);
|
|
|
|
break;
|
|
|
|
case kSetImports:
|
|
|
|
{
|
|
|
|
JSValue global = JS_GetGlobalObject(to->_context);
|
2022-01-02 13:17:58 -05:00
|
|
|
JSValue imports = tf_serialize_load(to, from, begin, length);
|
2021-01-02 13:10:00 -05:00
|
|
|
|
|
|
|
JSPropertyEnum* ptab;
|
|
|
|
uint32_t plen;
|
|
|
|
JS_GetOwnPropertyNames(to->_context, &ptab, &plen, imports, JS_GPN_STRING_MASK);
|
2021-10-10 17:51:38 -04:00
|
|
|
for (uint32_t i = 0; i < plen; ++i)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JSPropertyDescriptor desc;
|
2021-10-10 17:51:38 -04:00
|
|
|
if (JS_GetOwnProperty(to->_context, &desc, imports, ptab[i].atom) == 1)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JS_SetProperty(to->_context, global, ptab[i].atom, desc.value);
|
|
|
|
}
|
|
|
|
JS_FreeAtom(to->_context, ptab[i].atom);
|
|
|
|
}
|
|
|
|
js_free(to->_context, ptab);
|
|
|
|
|
|
|
|
JS_SetPropertyStr(to->_context, global, "imports", imports);
|
|
|
|
JS_FreeValue(to->_context, global);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kGetExports:
|
|
|
|
{
|
|
|
|
promiseid_t promise;
|
|
|
|
assert(length >= sizeof(promise));
|
|
|
|
memcpy(&promise, begin, sizeof(promiseid_t));
|
|
|
|
JSValue global = JS_GetGlobalObject(to->_context);
|
|
|
|
JSValue exportObject = JS_GetPropertyStr(to->_context, global, "exports");
|
|
|
|
_tf_task_sendPromiseResolve(to, from, promise, exportObject);
|
|
|
|
JS_FreeValue(to->_context, exportObject);
|
|
|
|
JS_FreeValue(to->_context, global);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kTaskError:
|
|
|
|
{
|
2022-01-02 13:17:58 -05:00
|
|
|
JSValue error = tf_serialize_load(to, from, begin, length);
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_taskstub_on_error(from, error);
|
|
|
|
JS_FreeValue(to->_context, error);
|
|
|
|
}
|
|
|
|
break;
|
2022-01-02 13:17:58 -05:00
|
|
|
case kTaskTrace:
|
|
|
|
tf_trace_raw(to->_trace, begin, length);
|
|
|
|
break;
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
tf_trace_end(to->_trace);
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static const char* _tf_task_resolveRequire(tf_task_t* task, const char* require)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
printf("Looking in %s for %s\n", task->_path, require);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (strstr(require, "..") || strstr(require, "/") || strstr(require, "\\"))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
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;
|
2021-11-14 17:55:21 -05:00
|
|
|
if (uv_fs_access(&task->_loop, &request, test, R_OK, 0) == 0)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
return strdup(test);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
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)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
return it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
JSValue _tf_task_require(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
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]);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (in_path)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
const char* path = _tf_task_resolveRequire(task, in_path);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (!path)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
result = JS_ThrowReferenceError(task->_context, "require(): Unable to resolve module: %s.", in_path);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
script_export_t* it = _task_find_script_export(task, path);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (it)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
result = JS_DupValue(task->_context, it->value);
|
|
|
|
free((void*)path);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JSValue exports = JS_NewObject(task->_context);
|
|
|
|
script_export_t* export = malloc(sizeof(script_export_t));
|
2021-10-10 17:51:38 -04:00
|
|
|
*export = (script_export_t)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
.name = path,
|
|
|
|
.value = JS_DupValue(task->_context, exports),
|
|
|
|
.next = task->_scriptExports,
|
|
|
|
};
|
|
|
|
task->_scriptExports = export;
|
|
|
|
const char* source = _task_loadFile(path);
|
|
|
|
printf("Requiring script %s\n", path);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (source)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
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);
|
2021-11-03 18:28:25 -04:00
|
|
|
tf_util_report_error(task->_context, eval);
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_task_run_jobs(task);
|
|
|
|
if (JS_IsError(task->_context, eval) ||
|
2021-10-10 17:51:38 -04:00
|
|
|
JS_IsException(eval))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
result = JS_DupValue(task->_context, eval);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
result = JS_DupValue(task->_context, exports);
|
|
|
|
}
|
|
|
|
JS_FreeValue(task->_context, eval);
|
|
|
|
JS_SetPropertyStr(task->_context, global, "exports", oldExports);
|
|
|
|
JS_FreeValue(task->_context, global);
|
|
|
|
free((void*)source);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
printf("Failed to load %s.\n", path);
|
|
|
|
}
|
|
|
|
JS_FreeValue(task->_context, exports);
|
|
|
|
}
|
|
|
|
}
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
result = JS_ThrowReferenceError(task->_context, "require(): No module specified.");
|
|
|
|
}
|
|
|
|
JS_FreeCString(context, in_path);
|
|
|
|
tf_trace_end(task->_trace);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static JSValue _tf_task_executeSource(tf_task_t* task, const char* source, const char* name)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_trace_begin(task->_trace, "_tf_task_executeSource");
|
|
|
|
JSValue result = JS_Eval(task->_context, source, strlen(source), name, 0);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (!*task->_scriptName)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
snprintf(task->_scriptName, sizeof(task->_scriptName), "%s", name);
|
|
|
|
}
|
2022-01-02 21:25:11 -05:00
|
|
|
if (!JS_IsException(result))
|
|
|
|
{
|
|
|
|
tf_task_run_jobs(task);
|
|
|
|
}
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_trace_end(task->_trace);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
JSValue _tf_task_sandbox_require(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_task_t* task = JS_GetContextOpaque(context);
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
if (JS_IsObject(task->_loadedFiles))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
const char* name = JS_ToCString(context, argv[0]);
|
|
|
|
script_export_t* it = _task_find_script_export(task, name);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (it)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
return it->value;
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JSValue exports = JS_NewObject(context);
|
|
|
|
const char* name = JS_ToCString(context, argv[0]);
|
2021-01-10 21:09:19 -05:00
|
|
|
JSValue value = JS_GetPropertyStr(context, task->_loadedFiles, name);
|
|
|
|
size_t length;
|
2021-11-03 18:15:46 -04:00
|
|
|
uint8_t* array = tf_util_try_get_array_buffer(context, &length, value);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (array)
|
|
|
|
{
|
2021-01-10 21:09:19 -05:00
|
|
|
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);
|
2021-11-03 18:28:25 -04:00
|
|
|
tf_util_report_error(context, result);
|
2021-01-10 21:09:19 -05:00
|
|
|
JS_SetPropertyStr(context, global, "exports", oldExports);
|
|
|
|
JS_FreeValue(context, global);
|
|
|
|
tf_task_run_jobs(task);
|
|
|
|
free(source);
|
|
|
|
return exports;
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else if (JS_IsString(value))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
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, 0);
|
2021-11-03 18:28:25 -04:00
|
|
|
tf_util_report_error(context, result);
|
2021-01-02 13:10:00 -05:00
|
|
|
JS_SetPropertyStr(context, global, "exports", oldExports);
|
|
|
|
JS_FreeValue(context, global);
|
|
|
|
tf_task_run_jobs(task);
|
|
|
|
return exports;
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-10 21:09:19 -05:00
|
|
|
return JS_ThrowInternalError(context, "Failed to load %s %d %d %d.", name, JS_IsNull(value), JS_IsUndefined(value), JS_IsException(value));
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
uv_loop_t* tf_task_get_loop(tf_task_t* task)
|
|
|
|
{
|
2021-11-14 17:55:21 -05:00
|
|
|
return &task->_loop;
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2021-12-27 15:49:07 -05:00
|
|
|
tf_trace_t* tf_task_get_trace(tf_task_t* task)
|
|
|
|
{
|
|
|
|
return task->_trace;
|
|
|
|
}
|
|
|
|
|
2021-12-28 12:44:48 -05:00
|
|
|
static int _promise_compare(const void* a, const void* b)
|
|
|
|
{
|
|
|
|
promiseid_t ida = (promiseid_t)(intptr_t)a;
|
|
|
|
const promise_t* pb = b;
|
|
|
|
return ida < pb->id ? -1 : pb->id < ida ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static promise_t* _tf_task_find_promise(tf_task_t* task, promiseid_t id)
|
|
|
|
{
|
|
|
|
if (!task->_promises)
|
|
|
|
{
|
|
|
|
return NULL;
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
2021-12-28 12:44:48 -05:00
|
|
|
promise_t* it = bsearch((void*)(intptr_t)id, task->_promises, task->_promise_count, sizeof(promise_t), _promise_compare);
|
|
|
|
return it ? it : NULL;
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void _tf_task_trace_promises(tf_task_t* task)
|
|
|
|
{
|
|
|
|
tf_trace_counter(task->_trace, "promises", 1, (const char*[]) { "count" }, (int64_t[]) { task->_promise_count });
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static void _tf_task_free_promise(tf_task_t* task, promiseid_t id)
|
|
|
|
{
|
2021-12-28 12:44:48 -05:00
|
|
|
promise_t* it = bsearch((void*)(intptr_t)id, task->_promises, task->_promise_count, sizeof(promise_t), _promise_compare);
|
|
|
|
if (it)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2021-12-28 12:44:48 -05:00
|
|
|
int index = it - task->_promises;
|
|
|
|
memmove(it, it + 1, sizeof(promise_t) * (task->_promise_count - index - 1));
|
|
|
|
task->_promise_count--;
|
|
|
|
_tf_task_trace_promises(task);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-13 22:05:37 -05:00
|
|
|
JSValue tf_task_allocate_promise(tf_task_t* task, promiseid_t* out_promise)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
promiseid_t promiseId;
|
|
|
|
do {
|
|
|
|
promiseId = task->_nextPromise++;
|
2021-12-28 14:47:32 -05:00
|
|
|
} while (_tf_task_find_promise(task, promiseId) || !promiseId);
|
2021-01-02 13:10:00 -05:00
|
|
|
|
2021-12-28 12:44:48 -05:00
|
|
|
promise_t promise =
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
.id = promiseId,
|
|
|
|
.values = { JS_NULL, JS_NULL, JS_NULL },
|
|
|
|
};
|
2021-12-28 12:44:48 -05:00
|
|
|
promise.values[0] = JS_NewPromiseCapability(task->_context, &promise.values[1]);
|
|
|
|
int index = _insert_index((void*)(intptr_t)promiseId, task->_promises, task->_promise_count, sizeof(promise_t), _promise_compare);
|
|
|
|
task->_promises = realloc(task->_promises, sizeof(promise_t) * (task->_promise_count + 1));
|
2022-01-08 16:56:57 -05:00
|
|
|
if (task->_promise_count - index)
|
|
|
|
{
|
|
|
|
memmove(task->_promises + index + 1, task->_promises + index, sizeof(promise_t) * (task->_promise_count - index));
|
|
|
|
}
|
2021-12-28 12:44:48 -05:00
|
|
|
task->_promises[index] = promise;
|
2021-01-02 13:10:00 -05:00
|
|
|
task->_promise_count++;
|
|
|
|
_tf_task_trace_promises(task);
|
2022-01-13 22:05:37 -05:00
|
|
|
*out_promise = promiseId;
|
|
|
|
return promise.values[0];
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void tf_task_resolve_promise(tf_task_t* task, promiseid_t promise, JSValue value)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
promise_t* it = _tf_task_find_promise(task, promise);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (it)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JSValue result = JS_Call(task->_context, it->values[1], JS_UNDEFINED, 1, &value);
|
2021-11-03 18:28:25 -04:00
|
|
|
tf_util_report_error(task->_context, result);
|
2021-01-02 13:10:00 -05:00
|
|
|
JS_FreeValue(task->_context, it->values[1]);
|
|
|
|
JS_FreeValue(task->_context, it->values[2]);
|
2021-09-06 16:54:44 -04:00
|
|
|
JS_FreeValue(task->_context, result);
|
2021-01-02 13:10:00 -05:00
|
|
|
_tf_task_free_promise(task, promise);
|
|
|
|
tf_task_run_jobs(task);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
printf("Didn't find promise %d to resolve.\n", promise);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void tf_task_reject_promise(tf_task_t* task, promiseid_t promise, JSValue value)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
promise_t* it = _tf_task_find_promise(task, promise);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (it)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JSValue result = JS_Call(task->_context, it->values[2], JS_UNDEFINED, 1, &value);
|
2021-11-03 18:28:25 -04:00
|
|
|
tf_util_report_error(task->_context, result);
|
2021-01-02 13:10:00 -05:00
|
|
|
JS_FreeValue(task->_context, it->values[1]);
|
|
|
|
JS_FreeValue(task->_context, it->values[2]);
|
2021-09-06 16:54:44 -04:00
|
|
|
JS_FreeValue(task->_context, result);
|
2021-01-02 13:10:00 -05:00
|
|
|
_tf_task_free_promise(task, promise);
|
|
|
|
tf_task_run_jobs(task);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
JSValue tf_task_get_promise(tf_task_t* task, promiseid_t promise)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
promise_t* it = _tf_task_find_promise(task, promise);
|
|
|
|
return it ? it->values[0] : JS_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _tf_task_trace_children(tf_task_t* task)
|
|
|
|
{
|
|
|
|
tf_trace_counter(task->_trace, "child_tasks", 1, (const char*[]) { "count" }, (int64_t[]) { task->_child_count });
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
taskid_t tf_task_allocate_task_id(tf_task_t* task, tf_taskstub_t* stub)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
taskid_t id = 0;
|
|
|
|
do {
|
|
|
|
id = task->_nextTask++;
|
|
|
|
} while (id == k_task_parent_id || _tf_task_get_stub(task, id));
|
|
|
|
|
|
|
|
task_child_node_t* node = malloc(sizeof(task_child_node_t));
|
2021-10-10 17:51:38 -04:00
|
|
|
*node = (task_child_node_t)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
.id = id,
|
|
|
|
.stub = stub,
|
|
|
|
.next = task->_children,
|
|
|
|
};
|
|
|
|
task->_children = node;
|
|
|
|
task->_child_count++;
|
|
|
|
_tf_task_trace_children(task);
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tf_task_remove_child(tf_task_t* task, tf_taskstub_t* child)
|
|
|
|
{
|
|
|
|
_import_record_release_for_task(task, tf_taskstub_get_id(child));
|
|
|
|
_export_record_release_for_task(task, tf_taskstub_get_id(child));
|
|
|
|
for (task_child_node_t** it = &task->_children;
|
|
|
|
*it;
|
2021-10-10 17:51:38 -04:00
|
|
|
it = &(*it)->next)
|
|
|
|
{
|
|
|
|
if ((*it)->stub == child)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
task_child_node_t* node = *it;
|
|
|
|
*it = node->next;
|
|
|
|
free(node);
|
|
|
|
task->_child_count--;
|
|
|
|
_tf_task_trace_children(task);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _import_finalizer(JSRuntime* runtime, JSValue value)
|
|
|
|
{
|
|
|
|
import_record_t* import = JS_GetOpaque(value, _import_class_id);
|
2022-01-08 16:56:57 -05:00
|
|
|
if (import)
|
|
|
|
{
|
|
|
|
import_record_t** it = _tf_task_find_import(import->_owner, import->_task, import->_export);
|
|
|
|
_import_record_release(it);
|
|
|
|
}
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void _import_mark_func(JSRuntime* runtime, JSValueConst value, JS_MarkFunc mark_func)
|
|
|
|
{
|
|
|
|
import_record_t* import = JS_GetOpaque(value, _import_class_id);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (import && import->_useCount > 0)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JS_MarkValue(runtime, import->_function, mark_func);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _tf_task_promise_rejection_tracker(JSContext* context, JSValueConst promise, JSValueConst reason, JS_BOOL is_handled, void* user_data)
|
|
|
|
{
|
2021-10-10 17:51:38 -04:00
|
|
|
if (!is_handled)
|
|
|
|
{
|
2021-11-03 18:28:25 -04:00
|
|
|
tf_util_report_error(context, reason);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
tf_task_t* tf_task_create()
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_task_t* task = malloc(sizeof(tf_task_t));
|
|
|
|
*task = (tf_task_t) { 0 };
|
|
|
|
++_count;
|
|
|
|
task->_runtime = JS_NewRuntime();
|
|
|
|
task->_context = JS_NewContext(task->_runtime);
|
|
|
|
JS_SetContextOpaque(task->_context, task);
|
|
|
|
|
|
|
|
JS_SetHostPromiseRejectionTracker(task->_runtime, _tf_task_promise_rejection_tracker, task);
|
|
|
|
|
|
|
|
JS_NewClassID(&_import_class_id);
|
2021-11-14 17:55:21 -05:00
|
|
|
JSClassDef def =
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
.class_name = "imported_function",
|
|
|
|
.finalizer = _import_finalizer,
|
|
|
|
.gc_mark = _import_mark_func,
|
|
|
|
.call = _import_call,
|
|
|
|
};
|
|
|
|
JS_NewClass(task->_runtime, _import_class_id, &def);
|
|
|
|
task->_loadedFiles = JS_NewObject(task->_context);
|
2021-11-14 17:55:21 -05:00
|
|
|
uv_loop_init(&task->_loop);
|
|
|
|
task->_loop.data = task;
|
2021-01-02 13:10:00 -05:00
|
|
|
return task;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void tf_task_configure_from_stdin(tf_task_t* task)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
task->_parent = tf_taskstub_create_parent(task, STDIN_FILENO);
|
|
|
|
}
|
|
|
|
|
2022-01-02 13:17:58 -05:00
|
|
|
static void _tf_task_trace_to_parent(tf_trace_t* trace, const char* buffer, size_t size, void* user_data)
|
|
|
|
{
|
|
|
|
tf_task_t* task = user_data;
|
|
|
|
tf_packetstream_send(tf_taskstub_get_stream(task->_parent), kTaskTrace, buffer, size);
|
|
|
|
}
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
void tf_task_activate(tf_task_t* task)
|
|
|
|
{
|
2022-01-02 13:17:58 -05:00
|
|
|
assert(!task->_activated);
|
|
|
|
task->_activated = true;
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
JSContext* context = task->_context;
|
|
|
|
JSValue global = JS_GetGlobalObject(context);
|
|
|
|
JS_SetPropertyStr(context, global, "exports", JS_NewObject(context));
|
|
|
|
|
|
|
|
JSAtom atom = JS_NewAtom(context, "parent");
|
|
|
|
JS_DefinePropertyGetSet(context, global, atom, JS_NewCFunction(context, _tf_task_get_parent, "parent", 0), JS_UNDEFINED, 0);
|
|
|
|
JS_FreeAtom(context, atom);
|
|
|
|
|
|
|
|
JSValue tildefriends = JS_NewObject(context);
|
|
|
|
JS_SetPropertyStr(context, tildefriends, "ssb_port", JS_NewInt32(context, task->_ssb_port));
|
|
|
|
JS_SetPropertyStr(context, tildefriends, "http_port", JS_NewInt32(context, task->_http_port));
|
|
|
|
JS_SetPropertyStr(context, tildefriends, "https_port", JS_NewInt32(context, task->_https_port));
|
2021-12-01 18:29:53 -05:00
|
|
|
JSValue args = JS_NewObject(context);
|
|
|
|
JS_SetPropertyStr(context, tildefriends, "args", args);
|
|
|
|
if (task->_args)
|
|
|
|
{
|
|
|
|
char* saveptr = NULL;
|
|
|
|
char* copy = strdup(task->_args);
|
|
|
|
char* start = copy;
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
char* assignment = strtok_r(start, ",", &saveptr);
|
|
|
|
start = NULL;
|
|
|
|
if (!assignment)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
char* equals = strchr(assignment, '=');
|
|
|
|
if (equals)
|
|
|
|
{
|
|
|
|
*equals = '\0';
|
|
|
|
JS_SetPropertyStr(context, args, assignment, JS_NewString(context, equals + 1));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("Assignment missing '=': %s.\n", assignment);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(copy);
|
|
|
|
}
|
2021-01-02 13:10:00 -05:00
|
|
|
JS_SetPropertyStr(context, global, "tildefriends", tildefriends);
|
|
|
|
|
2022-01-02 13:17:58 -05:00
|
|
|
task->_trace = tf_trace_create();
|
2021-10-10 17:51:38 -04:00
|
|
|
if (task->_trusted)
|
|
|
|
{
|
2021-11-14 17:55:21 -05:00
|
|
|
sqlite3_open(*task->_db_path ? task->_db_path : "db.sqlite", &task->_db);
|
2021-01-02 13:10:00 -05:00
|
|
|
|
|
|
|
JS_SetPropertyStr(context, global, "require", JS_NewCFunction(context, _tf_task_require, "require", 1));
|
2021-10-24 11:46:30 -04:00
|
|
|
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));
|
|
|
|
tf_file_register(context);
|
2021-01-02 13:10:00 -05:00
|
|
|
|
2021-11-14 17:55:21 -05:00
|
|
|
task->_ssb = tf_ssb_create(&task->_loop, task->_context, task->_db, *task->_secrets_path ? task->_secrets_path : NULL);
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_ssb_set_trace(task->_ssb, task->_trace);
|
2021-10-24 11:46:30 -04:00
|
|
|
tf_ssb_register(context, task->_ssb);
|
2021-10-27 20:20:17 -04:00
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
if (task->_ssb_port)
|
|
|
|
{
|
2021-10-27 20:20:17 -04:00
|
|
|
tf_ssb_broadcast_listener_start(task->_ssb, false);
|
2021-08-19 15:29:37 -04:00
|
|
|
tf_ssb_server_open(task->_ssb, task->_ssb_port);
|
|
|
|
}
|
2021-01-02 13:10:00 -05:00
|
|
|
|
|
|
|
JS_SetPropertyStr(context, global, "trace", JS_NewCFunction(context, _tf_task_trace, "trace", 1));
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-10 21:09:19 -05:00
|
|
|
JS_SetPropertyStr(context, global, "require", JS_NewCFunction(context, _tf_task_sandbox_require, "sandboxRequire", 0));
|
2022-01-02 13:17:58 -05:00
|
|
|
tf_trace_set_write_callback(task->_trace, _tf_task_trace_to_parent, task);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2021-10-24 11:46:30 -04:00
|
|
|
tf_bcrypt_register(context);
|
2021-11-03 18:15:46 -04:00
|
|
|
tf_util_register(context);
|
2021-01-02 13:10:00 -05:00
|
|
|
JS_SetPropertyStr(context, global, "exit", JS_NewCFunction(context, _tf_task_exit, "exit", 1));
|
|
|
|
JS_SetPropertyStr(context, global, "version", JS_NewCFunction(context, _tf_task_version, "version", 0));
|
|
|
|
JS_SetPropertyStr(context, global, "getFile", JS_NewCFunction(context, _tf_task_getFile, "getFile", 1));
|
|
|
|
JS_FreeValue(context, global);
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void tf_task_run(tf_task_t* task)
|
|
|
|
{
|
2021-11-14 17:55:21 -05:00
|
|
|
uv_run(&task->_loop, UV_RUN_DEFAULT);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void tf_task_set_trusted(tf_task_t* task, bool trusted)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
task->_trusted = trusted;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
JSContext* tf_task_get_context(tf_task_t* task)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
return task->_context;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void tf_task_destroy(tf_task_t* task)
|
|
|
|
{
|
2022-01-08 16:56:57 -05:00
|
|
|
for (int i = 0; i < task->_import_count; i++)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2022-01-08 16:56:57 -05:00
|
|
|
JS_FreeValue(task->_context, task->_imports[i]->_function);
|
|
|
|
free(task->_imports[i]);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
2022-01-08 16:56:57 -05:00
|
|
|
free(task->_imports);
|
2021-01-02 13:10:00 -05:00
|
|
|
task->_imports = NULL;
|
2022-01-08 16:56:57 -05:00
|
|
|
task->_import_count = 0;
|
2021-01-02 13:10:00 -05:00
|
|
|
|
2022-01-09 20:50:58 -05:00
|
|
|
for (int i = 0; i < task->_export_count; i++)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2022-01-09 20:50:58 -05:00
|
|
|
JS_FreeValue(task->_context, task->_exports[i]->_function);
|
|
|
|
free(task->_exports[i]);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
2022-01-09 20:50:58 -05:00
|
|
|
free(task->_exports);
|
|
|
|
task->_exports = NULL;
|
|
|
|
task->_export_count = 0;
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
while (task->_children)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
task_child_node_t* node = task->_children;
|
|
|
|
tf_taskstub_destroy(node->stub);
|
|
|
|
task->_children = node->next;
|
|
|
|
free(node);
|
|
|
|
}
|
2021-10-10 17:51:38 -04:00
|
|
|
if (task->_parent)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JS_FreeValue(task->_context, tf_taskstub_get_task_object(task->_parent));
|
|
|
|
}
|
2021-12-28 12:44:48 -05:00
|
|
|
while (task->_promise_count)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2021-12-28 12:44:48 -05:00
|
|
|
tf_task_reject_promise(task, task->_promises[task->_promise_count - 1].id, JS_NULL);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
2021-12-28 12:44:48 -05:00
|
|
|
free(task->_promises);
|
|
|
|
task->_promises = NULL;
|
2021-01-02 13:10:00 -05:00
|
|
|
JS_FreeValue(task->_context, task->_requires);
|
|
|
|
JS_FreeValue(task->_context, task->_loadedFiles);
|
2021-10-10 17:51:38 -04:00
|
|
|
while (task->_scriptExports)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
script_export_t* export = task->_scriptExports;
|
|
|
|
JS_FreeValue(task->_context, export->value);
|
|
|
|
task->_scriptExports = export->next;
|
|
|
|
free((void*)export->name);
|
|
|
|
free(export);
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
if (task->_trace)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_trace_destroy(task->_trace);
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
if (task->_ssb)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_ssb_destroy(task->_ssb);
|
|
|
|
}
|
|
|
|
JS_FreeContext(task->_context);
|
|
|
|
JS_FreeRuntime(task->_runtime);
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
if (task->_db)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
sqlite3_close(task->_db);
|
|
|
|
}
|
|
|
|
|
2021-12-21 17:43:41 -05:00
|
|
|
if (uv_loop_close(&task->_loop) != 0)
|
|
|
|
{
|
|
|
|
uv_print_all_handles(&task->_loop, stdout);
|
|
|
|
}
|
2021-01-02 13:10:00 -05:00
|
|
|
--_count;
|
|
|
|
free((void*)task->_path);
|
|
|
|
free(task);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSValue tf_task_add_import(tf_task_t* task, taskid_t stub_id, exportid_t export_id)
|
|
|
|
{
|
|
|
|
JSValue function = JS_NewObjectClass(task->_context, _import_class_id);
|
|
|
|
import_record_t* import = malloc(sizeof(import_record_t));
|
|
|
|
JS_SetOpaque(function, import);
|
2021-10-10 17:51:38 -04:00
|
|
|
*import = (import_record_t)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
._function = JS_DupValue(task->_context, function),
|
|
|
|
._export = export_id,
|
|
|
|
._owner = task,
|
|
|
|
._task = stub_id,
|
|
|
|
._useCount = 1,
|
|
|
|
};
|
|
|
|
|
2022-01-08 16:56:57 -05:00
|
|
|
int index = _insert_index(import, task->_imports, task->_import_count, sizeof(import_record_t*), _import_compare);
|
|
|
|
task->_imports = realloc(task->_imports, sizeof(import_record_t*) * (task->_import_count + 1));
|
|
|
|
if (task->_import_count - index)
|
|
|
|
{
|
|
|
|
memmove(task->_imports + index + 1, task->_imports + index, sizeof(import_record_t*) * (task->_import_count - index));
|
|
|
|
}
|
|
|
|
task->_imports[index] = import;
|
2021-01-02 13:10:00 -05:00
|
|
|
task->_import_count++;
|
|
|
|
_tf_task_trace_imports(task);
|
|
|
|
return function;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
tf_task_t* tf_task_get(JSContext* context)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
return JS_GetContextOpaque(context);
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void tf_task_run_jobs(tf_task_t* task)
|
|
|
|
{
|
|
|
|
while (JS_IsJobPending(task->_runtime))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JSContext* context = NULL;
|
|
|
|
int r = JS_ExecutePendingJob(task->_runtime, &context);
|
|
|
|
JSValue result = JS_GetException(context);
|
2021-11-03 18:28:25 -04:00
|
|
|
if (context)
|
|
|
|
{
|
|
|
|
tf_util_report_error(context, result);
|
|
|
|
}
|
2021-10-10 17:51:38 -04:00
|
|
|
if (r < 0)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
js_std_dump_error(context);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else if (r == 0)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void tf_task_send_promise_message(tf_task_t* from, tf_taskstub_t* to, tf_task_message_t type, promiseid_t promise, JSValue payload)
|
|
|
|
{
|
|
|
|
_tf_task_sendPromiseMessage(from, to, type, promise, payload);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tf_task_set_ssb_port(tf_task_t* task, int port)
|
|
|
|
{
|
|
|
|
task->_ssb_port = port;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tf_task_set_http_port(tf_task_t* task, int port)
|
|
|
|
{
|
|
|
|
task->_http_port = port;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tf_task_set_https_port(tf_task_t* task, int port)
|
|
|
|
{
|
|
|
|
task->_https_port = port;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tf_task_set_db_path(tf_task_t* task, const char* db_path)
|
|
|
|
{
|
2021-11-14 17:55:21 -05:00
|
|
|
snprintf(task->_db_path, sizeof(task->_db_path), "%s", db_path);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void tf_task_set_secrets_path(tf_task_t* task, const char* secrets_path)
|
|
|
|
{
|
2021-11-14 17:55:21 -05:00
|
|
|
snprintf(task->_secrets_path, sizeof(task->_secrets_path), "%s", secrets_path);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
2021-11-03 18:15:46 -04:00
|
|
|
|
2021-12-01 18:29:53 -05:00
|
|
|
void tf_task_set_args(tf_task_t* task, const char* args)
|
|
|
|
{
|
|
|
|
task->_args = args;
|
|
|
|
}
|
|
|
|
|
2021-11-03 18:15:46 -04:00
|
|
|
const char* tf_task_get_name(tf_task_t* task)
|
|
|
|
{
|
|
|
|
return task->_scriptName;
|
|
|
|
}
|