2021-10-24 11:46:30 -04:00
|
|
|
#include "taskstub.js.h"
|
2021-01-02 13:10:00 -05:00
|
|
|
|
|
|
|
#include "packetstream.h"
|
|
|
|
#include "serialize.h"
|
|
|
|
#include "task.h"
|
2021-11-03 18:28:25 -04:00
|
|
|
#include "util.js.h"
|
2021-01-02 13:10:00 -05:00
|
|
|
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "quickjs-libc.h"
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <io.h>
|
|
|
|
#include <windows.h>
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static JSClassID _classId;
|
|
|
|
static char _executable[1024];
|
|
|
|
|
|
|
|
typedef struct _tf_taskstub_t {
|
|
|
|
taskid_t _id;
|
|
|
|
JSValue _object;
|
|
|
|
JSValue _taskObject;
|
|
|
|
|
|
|
|
JSValue _on_exit;
|
|
|
|
JSValue _on_error;
|
2022-02-13 17:39:22 -05:00
|
|
|
JSValue _on_print;
|
2021-01-02 13:10:00 -05:00
|
|
|
|
|
|
|
tf_task_t* _owner;
|
|
|
|
tf_packetstream_t* _stream;
|
|
|
|
uv_process_t _process;
|
|
|
|
bool _finalized;
|
|
|
|
} tf_taskstub_t;
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void tf_taskstub_startup()
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
static bool initialized;
|
2021-10-10 17:51:38 -04:00
|
|
|
if (!initialized)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JS_NewClassID(&_classId);
|
|
|
|
size_t size = sizeof(_executable);
|
|
|
|
uv_exepath(_executable, &size);
|
|
|
|
initialized = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSValue _taskstub_activate(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
|
|
|
static JSValue _taskstub_execute(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
|
|
|
static JSValue _taskstub_setImports(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
|
|
|
static JSValue _taskstub_getExports(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
|
|
|
static JSValue _taskstub_setRequires(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
|
|
|
static JSValue _taskstub_kill(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
|
|
|
static JSValue _taskstub_get_on_exit(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
|
|
|
static JSValue _taskstub_set_on_exit(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
|
|
|
static JSValue _taskstub_get_on_error(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
|
|
|
static JSValue _taskstub_set_on_error(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
2022-02-13 17:39:22 -05:00
|
|
|
static JSValue _taskstub_get_on_print(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
|
|
|
static JSValue _taskstub_set_on_print(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
2021-01-02 13:10:00 -05:00
|
|
|
static JSValue _taskstub_loadFile(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
|
|
|
static void _taskstub_on_process_exit(uv_process_t* process, int64_t status, int terminationSignal);
|
|
|
|
static void _taskstub_finalizer(JSRuntime *runtime, JSValue value);
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static JSValue _taskstub_create(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_task_t* parent = tf_task_get(context);
|
|
|
|
tf_taskstub_t* stub = malloc(sizeof(tf_taskstub_t));
|
|
|
|
memset(stub, 0, sizeof(*stub));
|
|
|
|
stub->_stream = tf_packetstream_create();
|
|
|
|
|
|
|
|
JSValue taskObject = JS_NewObjectClass(context, _classId);
|
|
|
|
JS_SetOpaque(taskObject, stub);
|
|
|
|
stub->_owner = parent;
|
|
|
|
stub->_on_exit = JS_UNDEFINED;
|
|
|
|
stub->_on_error = JS_UNDEFINED;
|
2022-02-13 17:39:22 -05:00
|
|
|
stub->_on_print = JS_UNDEFINED;
|
2021-01-02 13:10:00 -05:00
|
|
|
stub->_object = JS_DupValue(context, taskObject);
|
|
|
|
|
|
|
|
JSAtom atom = JS_NewAtom(context, "onExit");
|
|
|
|
JS_DefinePropertyGetSet(
|
|
|
|
context,
|
|
|
|
taskObject,
|
|
|
|
atom,
|
|
|
|
JS_NewCFunction(context, _taskstub_get_on_exit, "getOnExit", 0),
|
|
|
|
JS_NewCFunction(context, _taskstub_set_on_exit, "setOnExit", 0),
|
|
|
|
0);
|
|
|
|
JS_FreeAtom(context, atom);
|
|
|
|
|
|
|
|
atom = JS_NewAtom(context, "onError");
|
|
|
|
JS_DefinePropertyGetSet(
|
|
|
|
context,
|
|
|
|
taskObject,
|
|
|
|
atom,
|
|
|
|
JS_NewCFunction(context, _taskstub_get_on_error, "getOnError", 0),
|
|
|
|
JS_NewCFunction(context, _taskstub_set_on_error, "setOnError", 0),
|
|
|
|
0);
|
|
|
|
JS_FreeAtom(context, atom);
|
|
|
|
|
2022-02-13 17:39:22 -05:00
|
|
|
atom = JS_NewAtom(context, "onPrint");
|
|
|
|
JS_DefinePropertyGetSet(
|
|
|
|
context,
|
|
|
|
taskObject,
|
|
|
|
atom,
|
|
|
|
JS_NewCFunction(context, _taskstub_get_on_print, "getOnPrint", 0),
|
|
|
|
JS_NewCFunction(context, _taskstub_set_on_print, "setOnPrint", 0),
|
|
|
|
0);
|
|
|
|
JS_FreeAtom(context, atom);
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
JS_SetPropertyStr(context, taskObject, "activate", JS_NewCFunction(context, _taskstub_activate, "activate", 0));
|
|
|
|
JS_SetPropertyStr(context, taskObject, "execute", JS_NewCFunction(context, _taskstub_execute, "execute", 1));
|
|
|
|
JS_SetPropertyStr(context, taskObject, "setImports", JS_NewCFunction(context, _taskstub_setImports, "setImports", 1));
|
|
|
|
JS_SetPropertyStr(context, taskObject, "getExports", JS_NewCFunction(context, _taskstub_getExports, "getExports", 0));
|
|
|
|
JS_SetPropertyStr(context, taskObject, "setRequires", JS_NewCFunction(context, _taskstub_setRequires, "setRequires", 1));
|
|
|
|
JS_SetPropertyStr(context, taskObject, "kill", JS_NewCFunction(context, _taskstub_kill, "kill", 0));
|
|
|
|
JS_SetPropertyStr(context, taskObject, "loadFile", JS_NewCFunction(context, _taskstub_loadFile, "loadFile", 1));
|
|
|
|
|
|
|
|
taskid_t id = k_task_parent_id;
|
2021-10-10 17:51:38 -04:00
|
|
|
if (parent)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
id = tf_task_allocate_task_id(parent, (tf_taskstub_t*)stub);
|
|
|
|
}
|
|
|
|
stub->_id = id;
|
|
|
|
|
|
|
|
char arg1[] = "sandbox";
|
|
|
|
char* command_argv[] = { _executable, arg1, 0 };
|
|
|
|
|
|
|
|
uv_pipe_t* pipe = tf_packetstream_get_pipe(stub->_stream);
|
|
|
|
memset(pipe, 0, sizeof(*pipe));
|
2021-10-10 17:51:38 -04:00
|
|
|
if (uv_pipe_init(tf_task_get_loop(parent), pipe, 1) != 0)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
fprintf(stderr, "uv_pipe_init failed\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
uv_stdio_container_t io[3];
|
|
|
|
io[0].flags = UV_CREATE_PIPE | UV_READABLE_PIPE | UV_WRITABLE_PIPE;
|
|
|
|
io[0].data.stream = (uv_stream_t*)pipe;
|
|
|
|
io[1].flags = UV_INHERIT_FD;
|
|
|
|
io[1].data.fd = STDOUT_FILENO;
|
|
|
|
io[2].flags = UV_INHERIT_FD;
|
|
|
|
io[2].data.fd = STDERR_FILENO;
|
|
|
|
|
|
|
|
uv_process_options_t options = {0};
|
|
|
|
options.args = command_argv;
|
|
|
|
options.exit_cb = _taskstub_on_process_exit;
|
|
|
|
options.stdio = io;
|
|
|
|
options.stdio_count = sizeof(io) / sizeof(*io);
|
|
|
|
options.file = command_argv[0];
|
|
|
|
|
|
|
|
JSValue result = JS_NULL;
|
|
|
|
stub->_process.data = stub;
|
|
|
|
int spawn_result = uv_spawn(tf_task_get_loop(parent), &stub->_process, &options);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (spawn_result == 0)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_packetstream_set_on_receive(stub->_stream, tf_task_on_receive_packet, stub);
|
|
|
|
tf_packetstream_start(stub->_stream);
|
|
|
|
|
|
|
|
result = taskObject;
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
fprintf(stderr, "uv_spawn failed: %s\n", uv_strerror(spawn_result));
|
|
|
|
JS_FreeValue(context, taskObject);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void _taskstub_gc_mark(JSRuntime* rt, JSValueConst value, JS_MarkFunc mark_func)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_taskstub_t* stub = JS_GetOpaque(value, _classId);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (stub)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JS_MarkValue(rt, stub->_on_exit, mark_func);
|
|
|
|
JS_MarkValue(rt, stub->_on_error, mark_func);
|
2022-02-13 17:39:22 -05:00
|
|
|
JS_MarkValue(rt, stub->_on_print, mark_func);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-24 11:46:30 -04:00
|
|
|
JSValue tf_taskstub_register(JSContext* context)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JSClassDef def = {
|
|
|
|
.class_name = "TaskStub",
|
|
|
|
.finalizer = &_taskstub_finalizer,
|
|
|
|
.gc_mark = _taskstub_gc_mark,
|
|
|
|
};
|
2021-10-10 17:51:38 -04:00
|
|
|
if (JS_NewClass(JS_GetRuntime(context), _classId, &def) != 0)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
fprintf(stderr, "Failed to register TaskStub class.\n");
|
|
|
|
}
|
|
|
|
return JS_NewCFunction2(context, _taskstub_create, "TaskStub", 0, JS_CFUNC_constructor, 0);
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
taskid_t tf_taskstub_get_id(const tf_taskstub_t* stub)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
return stub->_id;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
JSValue tf_taskstub_get_task_object(const tf_taskstub_t* stub)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
return stub->_taskObject;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
tf_packetstream_t* tf_taskstub_get_stream(const tf_taskstub_t* stub)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
return stub->_stream;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
tf_task_t* tf_taskstub_get_owner(const tf_taskstub_t* stub)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
return stub->_owner;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
tf_taskstub_t* tf_taskstub_create_parent(tf_task_t* task, uv_file file)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JSValue parentObject = JS_NewObject(tf_task_get_context(task));
|
|
|
|
tf_taskstub_t* parentStub = malloc(sizeof(tf_taskstub_t));
|
|
|
|
memset(parentStub, 0, sizeof(tf_taskstub_t));
|
|
|
|
parentStub->_stream = tf_packetstream_create();
|
|
|
|
parentStub->_on_exit = JS_UNDEFINED;
|
|
|
|
parentStub->_on_error = JS_UNDEFINED;
|
2022-02-13 17:39:22 -05:00
|
|
|
parentStub->_on_print = JS_UNDEFINED;
|
2021-01-02 13:10:00 -05:00
|
|
|
|
|
|
|
parentStub->_taskObject = parentObject;
|
|
|
|
JS_SetOpaque(parentObject, parentStub);
|
|
|
|
parentStub->_owner = task;
|
|
|
|
parentStub->_id = k_task_parent_id;
|
|
|
|
parentStub->_object = JS_DupValue(tf_task_get_context(task), parentObject);
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
if (uv_pipe_init(tf_task_get_loop(task), tf_packetstream_get_pipe(parentStub->_stream), 1) != 0)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
fprintf(stderr, "uv_pipe_init failed\n");
|
|
|
|
}
|
|
|
|
tf_packetstream_set_on_receive(parentStub->_stream, tf_task_on_receive_packet, parentStub);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (uv_pipe_open(tf_packetstream_get_pipe(parentStub->_stream), file) != 0)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
fprintf(stderr, "uv_pipe_open failed\n");
|
|
|
|
}
|
|
|
|
tf_packetstream_start(parentStub->_stream);
|
|
|
|
|
|
|
|
return parentStub;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _taskstub_cleanup(tf_taskstub_t* stub)
|
|
|
|
{
|
|
|
|
if (!stub->_process.data &&
|
|
|
|
JS_IsUndefined(stub->_object) &&
|
2021-10-10 17:51:38 -04:00
|
|
|
stub->_finalized)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
free(stub);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static void _taskstub_finalizer(JSRuntime* runtime, JSValue value)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_taskstub_t* stub = JS_GetOpaque(value, _classId);
|
2022-05-20 20:06:01 -04:00
|
|
|
JSContext* context = tf_task_get_context(stub->_owner);
|
|
|
|
if (!JS_IsUndefined(stub->_on_exit))
|
|
|
|
{
|
|
|
|
JS_FreeValue(context, stub->_on_exit);
|
|
|
|
stub->_on_exit = JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
if (!JS_IsUndefined(stub->_on_error))
|
|
|
|
{
|
|
|
|
JS_FreeValue(context, stub->_on_error);
|
|
|
|
stub->_on_error = JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
if (!JS_IsUndefined(stub->_on_print))
|
|
|
|
{
|
|
|
|
JS_FreeValue(context, stub->_on_print);
|
|
|
|
stub->_on_print = JS_UNDEFINED;
|
|
|
|
}
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_packetstream_destroy(stub->_stream);
|
|
|
|
stub->_stream = NULL;
|
|
|
|
stub->_finalized = true;
|
|
|
|
tf_task_remove_child(stub->_owner, stub);
|
|
|
|
_taskstub_cleanup(stub);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _taskstub_on_handle_close(uv_handle_t* handle)
|
|
|
|
{
|
|
|
|
tf_taskstub_t* stub = handle->data;
|
|
|
|
handle->data = NULL;
|
|
|
|
tf_task_remove_child(stub->_owner, stub);
|
|
|
|
_taskstub_cleanup(stub);
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static void _taskstub_on_process_exit(uv_process_t* process, int64_t status, int terminationSignal)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_taskstub_t* stub = process->data;
|
|
|
|
JSContext* context = tf_task_get_context(stub->_owner);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (!JS_IsUndefined(stub->_on_exit))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JSValue argv[] = { JS_NewInt32(context, status), JS_NewInt32(context, terminationSignal) };
|
|
|
|
JSValue result = JS_Call(context, stub->_on_exit, JS_NULL, 2, argv);
|
2021-11-03 18:28:25 -04:00
|
|
|
tf_util_report_error(context, result);
|
2021-01-02 13:10:00 -05:00
|
|
|
JS_FreeValue(context, result);
|
|
|
|
JS_FreeValue(context, argv[0]);
|
|
|
|
JS_FreeValue(context, argv[1]);
|
|
|
|
}
|
|
|
|
tf_packetstream_close(stub->_stream);
|
|
|
|
uv_close((uv_handle_t*)process, _taskstub_on_handle_close);
|
|
|
|
tf_taskstub_destroy(stub);
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static JSValue _taskstub_getExports(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_taskstub_t* stub = JS_GetOpaque(this_val, _classId);
|
2022-01-13 22:05:37 -05:00
|
|
|
promiseid_t promise = -1;
|
|
|
|
JSValue result = tf_task_allocate_promise(stub->_owner, &promise);
|
2022-01-20 19:49:03 -05:00
|
|
|
tf_task_send_promise_message(stub->_owner, stub, kGetExports, promise, JS_UNDEFINED);
|
2022-01-13 22:05:37 -05:00
|
|
|
return result;
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static JSValue _taskstub_setImports(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_taskstub_t* stub = JS_GetOpaque(this_val, _classId);
|
|
|
|
void* buffer;
|
|
|
|
size_t size;
|
|
|
|
tf_serialize_store(tf_task_get(context), stub, &buffer, &size, argv[0]);
|
|
|
|
tf_packetstream_send(stub->_stream, kSetImports, (char*)buffer, size);
|
2022-06-03 23:36:36 -04:00
|
|
|
free(buffer);
|
2021-01-02 13:10:00 -05:00
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static JSValue _taskstub_setRequires(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_taskstub_t* stub = JS_GetOpaque(this_val, _classId);
|
|
|
|
void* buffer;
|
|
|
|
size_t size;
|
|
|
|
tf_serialize_store(tf_task_get(context), stub, &buffer, &size, argv[0]);
|
|
|
|
tf_packetstream_send(stub->_stream, kSetRequires, (char*)buffer, size);
|
2022-06-03 23:36:36 -04:00
|
|
|
free(buffer);
|
2021-01-02 13:10:00 -05:00
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static JSValue _taskstub_loadFile(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_taskstub_t* stub = JS_GetOpaque(this_val, _classId);
|
|
|
|
void* buffer;
|
|
|
|
size_t size;
|
|
|
|
tf_serialize_store(tf_task_get(context), stub, &buffer, &size, argv[0]);
|
|
|
|
tf_packetstream_send(stub->_stream, kLoadFile, (char*)buffer, size);
|
2022-03-07 21:33:32 -05:00
|
|
|
free(buffer);
|
2021-01-02 13:10:00 -05:00
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static JSValue _taskstub_get_on_exit(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_taskstub_t* stub = JS_GetOpaque(this_val, _classId);
|
|
|
|
return JS_DupValue(context, stub->_on_exit);
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static JSValue _taskstub_set_on_exit(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_taskstub_t* stub = JS_GetOpaque(this_val, _classId);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (!JS_IsUndefined(stub->_on_exit))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JS_FreeValue(context, stub->_on_exit);
|
|
|
|
}
|
|
|
|
stub->_on_exit = JS_DupValue(context, argv[0]);
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static JSValue _taskstub_get_on_error(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_taskstub_t* stub = JS_GetOpaque(this_val, _classId);
|
|
|
|
return JS_DupValue(context, stub->_on_error);
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static JSValue _taskstub_set_on_error(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_taskstub_t* stub = JS_GetOpaque(this_val, _classId);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (!JS_IsUndefined(stub->_on_error))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JS_FreeValue(context, stub->_on_error);
|
|
|
|
}
|
|
|
|
stub->_on_error = JS_DupValue(context, argv[0]);
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
2022-02-13 17:39:22 -05:00
|
|
|
static JSValue _taskstub_get_on_print(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
tf_taskstub_t* stub = JS_GetOpaque(this_val, _classId);
|
|
|
|
return JS_DupValue(context, stub->_on_print);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSValue _taskstub_set_on_print(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
tf_taskstub_t* stub = JS_GetOpaque(this_val, _classId);
|
|
|
|
if (!JS_IsUndefined(stub->_on_print))
|
|
|
|
{
|
|
|
|
JS_FreeValue(context, stub->_on_print);
|
|
|
|
}
|
|
|
|
stub->_on_print = JS_DupValue(context, argv[0]);
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static JSValue _taskstub_activate(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_taskstub_t* stub = JS_GetOpaque(this_val, _classId);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (stub)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_packetstream_send(stub->_stream, kActivate, 0, 0);
|
|
|
|
}
|
|
|
|
return JS_NULL;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static JSValue _taskstub_execute(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_taskstub_t* stub = JS_GetOpaque(this_val, _classId);
|
2022-01-13 22:05:37 -05:00
|
|
|
promiseid_t promise = -1;
|
|
|
|
JSValue result = tf_task_allocate_promise(stub->_owner, &promise);
|
2022-01-20 19:49:03 -05:00
|
|
|
tf_task_send_promise_message(stub->_owner, stub, kExecute, promise, argv[0]);
|
2022-01-13 22:05:37 -05:00
|
|
|
return result;
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
static JSValue _taskstub_kill(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_taskstub_t* stub = JS_GetOpaque(this_val, _classId);
|
|
|
|
uv_process_kill(&stub->_process, SIGTERM);
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:51:38 -04:00
|
|
|
void tf_taskstub_destroy(tf_taskstub_t* stub)
|
|
|
|
{
|
|
|
|
if (!JS_IsUndefined(stub->_object))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JSValue object = stub->_object;
|
|
|
|
stub->_object = JS_UNDEFINED;
|
|
|
|
JS_FreeValue(tf_task_get_context(stub->_owner), object);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void tf_taskstub_on_error(tf_taskstub_t* stub, JSValue error)
|
|
|
|
{
|
|
|
|
JSContext* context = tf_task_get_context(stub->_owner);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (!JS_IsUndefined(stub->_on_error))
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
JSValue result = JS_Call(context, stub->_on_error, JS_NULL, 1, &error);
|
2021-11-03 18:28:25 -04:00
|
|
|
tf_util_report_error(context, result);
|
2021-01-02 13:10:00 -05:00
|
|
|
JS_FreeValue(context, result);
|
|
|
|
}
|
|
|
|
}
|
2022-02-13 17:39:22 -05:00
|
|
|
|
|
|
|
void tf_taskstub_on_print(tf_taskstub_t* stub, JSValue arguments)
|
|
|
|
{
|
|
|
|
JSContext* context = tf_task_get_context(stub->_owner);
|
|
|
|
if (!JS_IsUndefined(stub->_on_print))
|
|
|
|
{
|
|
|
|
JSValue result = JS_Call(context, stub->_on_print, JS_NULL, 1, &arguments);
|
|
|
|
tf_util_report_error(context, result);
|
|
|
|
JS_FreeValue(context, result);
|
|
|
|
}
|
|
|
|
}
|