2021-01-02 13:10:00 -05:00
|
|
|
#pragma once
|
|
|
|
|
2024-02-20 21:41:37 -05:00
|
|
|
/**
|
|
|
|
** \defgroup task Task
|
|
|
|
** Task is responsible for running JS in C. It exposes just what is needed for
|
|
|
|
** sandboxed or trusted code, helps with communiciation between parent and
|
|
|
|
** child processes, including function calls and async operations across the
|
|
|
|
** boundaries.
|
|
|
|
** @{
|
|
|
|
*/
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
#include "quickjs.h"
|
|
|
|
|
2024-03-01 21:18:12 -05:00
|
|
|
/** An event loop. */
|
2021-01-02 13:10:00 -05:00
|
|
|
typedef struct uv_loop_s uv_loop_t;
|
2024-03-01 21:18:12 -05:00
|
|
|
/** A timer. */
|
2021-01-02 13:10:00 -05:00
|
|
|
typedef struct uv_timer_s uv_timer_t;
|
|
|
|
|
2024-03-01 21:18:12 -05:00
|
|
|
/** A task identifier. */
|
2021-01-02 13:10:00 -05:00
|
|
|
typedef int taskid_t;
|
2024-03-01 21:18:12 -05:00
|
|
|
/** A promise identifier. */
|
2021-01-02 13:10:00 -05:00
|
|
|
typedef int promiseid_t;
|
2024-03-01 21:18:12 -05:00
|
|
|
/** An exported function identifier. */
|
2021-01-02 13:10:00 -05:00
|
|
|
typedef int exportid_t;
|
2024-03-01 21:18:12 -05:00
|
|
|
/** A handle to a task. */
|
2021-01-02 13:10:00 -05:00
|
|
|
typedef struct _tf_taskstub_t tf_taskstub_t;
|
2024-03-01 21:18:12 -05:00
|
|
|
/** A task. */
|
2021-01-02 13:10:00 -05:00
|
|
|
typedef struct _tf_task_t tf_task_t;
|
2024-03-01 21:18:12 -05:00
|
|
|
/** A trace instance. */
|
2021-12-27 15:49:07 -05:00
|
|
|
typedef struct _tf_trace_t tf_trace_t;
|
2024-03-01 21:18:12 -05:00
|
|
|
/** An SSB instance. */
|
2022-01-17 17:00:42 -05:00
|
|
|
typedef struct _tf_ssb_t tf_ssb_t;
|
2021-01-02 13:10:00 -05:00
|
|
|
|
2024-03-01 21:18:12 -05:00
|
|
|
/** The fixed ID of the parent task. */
|
2021-01-02 13:10:00 -05:00
|
|
|
static const taskid_t k_task_parent_id = 0;
|
|
|
|
|
2024-03-01 21:18:12 -05:00
|
|
|
/** A message type that can be sent between tasks. */
|
2024-02-15 18:35:01 -05:00
|
|
|
typedef enum _tf_task_message_t
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
kResolvePromise,
|
|
|
|
kRejectPromise,
|
|
|
|
kInvokeExport,
|
|
|
|
kReleaseExport,
|
|
|
|
kReleaseImport,
|
|
|
|
kActivate,
|
|
|
|
kExecute,
|
|
|
|
kKill,
|
|
|
|
kSetImports,
|
|
|
|
kGetExports,
|
|
|
|
kLoadFile,
|
|
|
|
kTaskError,
|
2022-01-02 13:17:58 -05:00
|
|
|
kTaskTrace,
|
2022-02-13 17:39:22 -05:00
|
|
|
kPrint,
|
2021-01-02 13:10:00 -05:00
|
|
|
} tf_task_message_t;
|
|
|
|
|
2024-03-01 21:18:12 -05:00
|
|
|
/**
|
|
|
|
** Create a task.
|
|
|
|
** @return A new task.
|
|
|
|
*/
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_task_t* tf_task_create();
|
2024-03-01 21:18:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
** Configure a task from a file descriptor. Typically a pipe to the parent
|
|
|
|
** task's process.
|
|
|
|
** @param task The task to configure.
|
|
|
|
** @param fd The file descriptor.
|
|
|
|
*/
|
2023-10-15 13:33:36 -04:00
|
|
|
void tf_task_configure_from_fd(tf_task_t* task, int fd);
|
2024-03-01 21:18:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
** Set the port number on which to run an SSB secure handshake server.
|
|
|
|
** @param task The task.
|
|
|
|
** @param port The port number or 0 to disable.
|
|
|
|
*/
|
2021-01-02 13:10:00 -05:00
|
|
|
void tf_task_set_ssb_port(tf_task_t* task, int port);
|
2024-03-01 21:18:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
** Set the port number on which to run an HTTP server.
|
|
|
|
** @param task The task.
|
|
|
|
** @param port The port number of 0 to disable.
|
|
|
|
*/
|
2021-01-02 13:10:00 -05:00
|
|
|
void tf_task_set_http_port(tf_task_t* task, int port);
|
2024-03-01 21:18:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
** Set the port number on which to run an HTTPS server.
|
|
|
|
** @param task The task.
|
|
|
|
** @param port The port number of 0 to disable.
|
|
|
|
*/
|
2021-01-02 13:10:00 -05:00
|
|
|
void tf_task_set_https_port(tf_task_t* task, int port);
|
2024-03-01 21:18:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
** Set the path to the SQLite database.
|
|
|
|
** @param task The task.
|
|
|
|
** @param path The database path.
|
|
|
|
*/
|
2021-01-02 13:10:00 -05:00
|
|
|
void tf_task_set_db_path(tf_task_t* task, const char* path);
|
2024-03-01 21:18:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
** Set the path to a zip file from which to load all static data.
|
|
|
|
** @param task The task.
|
|
|
|
** @param path The zip file path or NULL.
|
|
|
|
*/
|
2023-03-08 18:59:11 -05:00
|
|
|
void tf_task_set_zip_path(tf_task_t* task, const char* path);
|
2024-03-01 21:18:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
** Get the path to the zipp file being used for static data.
|
|
|
|
** @param task The task.
|
|
|
|
** @return The zip file path or NULL.
|
|
|
|
*/
|
2023-03-08 20:03:35 -05:00
|
|
|
const char* tf_task_get_zip_path(tf_task_t* task);
|
2024-03-01 21:18:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
** Set arbitrary named arguments that will be made available to the task.
|
|
|
|
** @param task The task.
|
|
|
|
** @param args A string of the form "key=value,other_key=other_value,..."
|
|
|
|
*/
|
2021-12-01 18:29:53 -05:00
|
|
|
void tf_task_set_args(tf_task_t* task, const char* args);
|
2024-03-01 21:18:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
** Get whether this instance is configure to run in a single process.
|
|
|
|
** @param task The running task.
|
|
|
|
** @return true if all tasks are running in a single process.
|
|
|
|
*/
|
2023-10-15 13:33:36 -04:00
|
|
|
bool tf_task_get_one_proc(tf_task_t* task);
|
2024-03-01 21:18:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
** Set whether all tasks should run in a single process. Only supported to
|
|
|
|
** appease Apple's limitations.
|
|
|
|
** @param task The running task.
|
|
|
|
** @param one_proc True if subprocesses should not be used.
|
|
|
|
*/
|
2023-10-15 13:33:36 -04:00
|
|
|
void tf_task_set_one_proc(tf_task_t* task, bool one_proc);
|
2024-03-01 21:18:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
** Start a task running its script.
|
|
|
|
** @param task The task.
|
|
|
|
*/
|
2021-01-02 13:10:00 -05:00
|
|
|
void tf_task_activate(tf_task_t* task);
|
2024-03-01 21:18:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
** Update a task until it is done or stopped.
|
|
|
|
** @param task The task.
|
|
|
|
*/
|
2021-01-02 13:10:00 -05:00
|
|
|
void tf_task_run(tf_task_t* task);
|
2024-03-01 21:18:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
** Run a script from file on disk.
|
|
|
|
** @param task The task.
|
|
|
|
** @param file The path to the script file to run.
|
|
|
|
** @return 0 if there was a problem or 1 if the script was started.
|
|
|
|
*/
|
2021-01-02 13:10:00 -05:00
|
|
|
int tf_task_execute(tf_task_t* task, const char* file);
|
2024-03-01 21:18:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
** Set a task as trusted or untrusted. Trusted tasks have more interface exposed to them.
|
|
|
|
** @param task The task.
|
|
|
|
** @param trusted true if the task is trusted.
|
|
|
|
*/
|
2021-01-02 13:10:00 -05:00
|
|
|
void tf_task_set_trusted(tf_task_t* task, bool trusted);
|
2024-03-01 21:18:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
** Get the JS context from a task.
|
|
|
|
** @param task The task.
|
|
|
|
** @return The context.
|
|
|
|
*/
|
2021-01-02 13:10:00 -05:00
|
|
|
JSContext* tf_task_get_context(tf_task_t* task);
|
2024-03-01 21:18:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
** Destroy a task.
|
|
|
|
** @param task The task.
|
|
|
|
*/
|
2021-01-02 13:10:00 -05:00
|
|
|
void tf_task_destroy(tf_task_t* task);
|
2024-03-01 21:18:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
** Convert a function to an integer handle that can be passed across processes.
|
|
|
|
** @param task The running task.
|
|
|
|
** @param to The task stub to which the handle will be passed.
|
|
|
|
** @param function The functoin to export.
|
|
|
|
** @return A handle representing the function.
|
|
|
|
*/
|
2021-01-02 13:10:00 -05:00
|
|
|
exportid_t tf_task_export_function(tf_task_t* task, tf_taskstub_t* to, JSValue function);
|
2024-03-01 21:18:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
** Create a function that can be called from a handle to an exported function
|
|
|
|
** from another task.
|
|
|
|
** @param task The running task.
|
|
|
|
** @param stub_id The task stub from which the function was exported.
|
|
|
|
** @param export_id The handle to the function.
|
|
|
|
** @return A function that, when called, invokes the corresponding function in
|
|
|
|
** the remote task.
|
|
|
|
*/
|
2021-01-02 13:10:00 -05:00
|
|
|
JSValue tf_task_add_import(tf_task_t* task, taskid_t stub_id, exportid_t export_id);
|
2024-03-01 21:18:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
** Get the event loop from a task.
|
|
|
|
** @param task The task.
|
|
|
|
** @return The loop.
|
|
|
|
*/
|
2021-01-02 13:10:00 -05:00
|
|
|
uv_loop_t* tf_task_get_loop(tf_task_t* task);
|
2024-03-01 21:18:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
** Get the task from a JS context.
|
|
|
|
** @param context The context.
|
|
|
|
** @return The task.
|
|
|
|
*/
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_task_t* tf_task_get(JSContext* context);
|
2024-03-01 21:18:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
** Get the trace instance from a task.
|
|
|
|
** @param task The task.
|
|
|
|
** @return The trace instance.
|
|
|
|
*/
|
2021-12-27 15:49:07 -05:00
|
|
|
tf_trace_t* tf_task_get_trace(tf_task_t* task);
|
2024-03-01 21:18:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
** Get the SSB instance from a task.
|
|
|
|
** @param task The task.
|
|
|
|
** @return The SSB instance.
|
|
|
|
*/
|
2022-01-17 17:00:42 -05:00
|
|
|
tf_ssb_t* tf_task_get_ssb(tf_task_t* task);
|
2024-03-01 21:18:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
** Get the name of a task.
|
|
|
|
** @param task The task.
|
|
|
|
** @return The task's name as derived from the script it is running.
|
|
|
|
*/
|
2021-11-03 18:15:46 -04:00
|
|
|
const char* tf_task_get_name(tf_task_t* task);
|
2024-03-01 21:18:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
** Print through a task's parent.
|
|
|
|
** @param task The running task.
|
|
|
|
** @param argc The number of arguments to print.
|
|
|
|
** @param argv The arguments to print.
|
|
|
|
*/
|
2022-02-13 17:39:22 -05:00
|
|
|
void tf_task_print(tf_task_t* task, int argc, JSValueConst* argv);
|
2021-01-02 13:10:00 -05:00
|
|
|
|
2024-03-01 21:18:12 -05:00
|
|
|
/**
|
|
|
|
** Allocate a promise object.
|
|
|
|
** @param task The running task.
|
|
|
|
** @param[out] out_promise The promise that was allocated.
|
|
|
|
** @return The promise JS object.
|
|
|
|
*/
|
2022-01-13 22:05:37 -05:00
|
|
|
JSValue tf_task_allocate_promise(tf_task_t* task, promiseid_t* out_promise);
|
2024-03-01 21:18:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
** Reject a promise.
|
|
|
|
** @param task The running task.
|
|
|
|
** @param promise The promise to reject.
|
|
|
|
** @param error The value with which to reject the promise.
|
|
|
|
*/
|
2021-01-02 13:10:00 -05:00
|
|
|
void tf_task_reject_promise(tf_task_t* task, promiseid_t promise, JSValue error);
|
2024-03-01 21:18:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
** Resolve a promise.
|
|
|
|
** @param task The running task.
|
|
|
|
** @param promise The promise to resolve.
|
|
|
|
** @param result The value with which to resolve the promise.
|
|
|
|
*/
|
2021-01-02 13:10:00 -05:00
|
|
|
void tf_task_resolve_promise(tf_task_t* task, promiseid_t promise, JSValue result);
|
2024-03-01 21:18:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
** Send a message referencing a promise across a packet stream.
|
|
|
|
** @param from The task originating the message.
|
|
|
|
** @param to The task handle receiving the message.
|
|
|
|
** @param type The message type.
|
|
|
|
** @param promise The promise.
|
|
|
|
** @param payload The content of the message.
|
|
|
|
*/
|
2021-01-02 13:10:00 -05:00
|
|
|
void tf_task_send_promise_message(tf_task_t* from, tf_taskstub_t* to, tf_task_message_t type, promiseid_t promise, JSValue payload);
|
|
|
|
|
2024-03-01 21:18:12 -05:00
|
|
|
/**
|
|
|
|
** Have a task handle a message from a packaet stream.
|
|
|
|
** @param packetType The type of the message.
|
|
|
|
** @param begin The data.
|
|
|
|
** @param length The size of the data.
|
|
|
|
** @param userData The task stub from which the packet was received.
|
|
|
|
*/
|
2021-01-02 13:10:00 -05:00
|
|
|
void tf_task_on_receive_packet(int packetType, const char* begin, size_t length, void* userData);
|
|
|
|
|
2024-03-01 21:18:12 -05:00
|
|
|
/**
|
|
|
|
** Generate an unused task identifier representing the task stub from the running task.
|
|
|
|
** @param task The running task.
|
|
|
|
** @param stub A handle to the task requesting an identifier.
|
|
|
|
** @return The new identifier.
|
|
|
|
*/
|
|
|
|
taskid_t tf_task_allocate_task_id(tf_task_t* task, tf_taskstub_t* stub);
|
2021-01-02 13:10:00 -05:00
|
|
|
|
2024-03-01 21:18:12 -05:00
|
|
|
/**
|
|
|
|
** Remove a task stub from a task.
|
|
|
|
** @param task The parent task.
|
|
|
|
** @param child The task handle to remove.
|
|
|
|
*/
|
|
|
|
void tf_task_remove_child(tf_task_t* task, tf_taskstub_t* child);
|
2021-11-03 18:28:25 -04:00
|
|
|
|
2024-03-01 21:18:12 -05:00
|
|
|
/**
|
|
|
|
** Send an error to the parent task.
|
|
|
|
** @param task The current task.
|
|
|
|
** @param error The potential error.
|
|
|
|
** @return true If the object was an error or exception and it was passed to
|
|
|
|
** the parent task.
|
|
|
|
*/
|
2022-07-26 20:27:10 -04:00
|
|
|
bool tf_task_send_error_to_parent(tf_task_t* task, JSValue error);
|
2024-01-02 18:26:42 -05:00
|
|
|
|
2024-03-01 21:18:12 -05:00
|
|
|
/**
|
|
|
|
** Get a report of recent disconnections.
|
|
|
|
** @param task The task.
|
|
|
|
** @return A JSON representation of recent disconnections that must be freed
|
|
|
|
** with tf_free().
|
|
|
|
*/
|
2024-01-02 18:26:42 -05:00
|
|
|
char* tf_task_get_disconnections(tf_task_t* task);
|
2024-03-01 21:18:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
** Get a report of miscellaneous debug information.
|
|
|
|
** @param task The task.
|
|
|
|
** @return A JSON representation of various debug information that must be
|
|
|
|
** freed with tf_free().
|
|
|
|
*/
|
2024-01-02 18:26:42 -05:00
|
|
|
char* tf_task_get_debug(tf_task_t* task);
|
2024-03-01 21:18:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
** Get a report of hitches that occurred.
|
|
|
|
** @param task The task.
|
|
|
|
** @return A JSON report of recent hitches that must be freed with tf_free().
|
|
|
|
*/
|
2024-01-02 18:26:42 -05:00
|
|
|
char* tf_task_get_hitches(tf_task_t* task);
|
2024-02-20 21:41:37 -05:00
|
|
|
|
|
|
|
/** @} */
|