Make setTimeout callable from ssb.js by moving it into util.js.c.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3705 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2021-12-27 20:49:07 +00:00
parent 5fbe9c42bc
commit 05b55c849a
3 changed files with 64 additions and 54 deletions

View File

@ -35,8 +35,6 @@ static const char* k_version = "1.0";
static JSClassID _import_class_id;
static int _count;
static void _task_timeoutCallback(uv_timer_t* handle);
typedef struct _export_record_t export_record_t;
typedef struct _import_record_t import_record_t;
@ -277,56 +275,6 @@ static const char* _task_loadFile(const char* fileName)
return result;
}
typedef struct _timeout_t {
tf_task_t* _task;
JSValue _callback;
} timeout_t;
static JSValue _task_setTimeout(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{
tf_task_t* task = JS_GetContextOpaque(context);
timeout_t* timeout = malloc(sizeof(timeout_t));
*timeout = (timeout_t)
{
._task = task,
._callback = JS_DupValue(context, argv[0]),
};
uv_timer_t* timer = malloc(sizeof(uv_timer_t));
memset(timer, 0, sizeof(uv_timer_t));
uv_timer_init(&task->_loop, timer);
timer->data = timeout;
int64_t duration;
JS_ToInt64(task->_context, &duration, argv[1]);
uv_timer_start(timer, _task_timeoutCallback, duration, 0);
return JS_NULL;
}
static void _handle_closed(uv_handle_t* handle)
{
free(handle);
}
static void _task_timeoutCallback(uv_timer_t* handle)
{
timeout_t* timeout = handle->data;
tf_trace_begin(timeout->_task->_trace, "_task_timeoutCallback");
JSValue result = JS_Call(
timeout->_task->_context,
timeout->_callback,
JS_NULL,
0,
NULL);
tf_util_report_error(timeout->_task->_context, result);
JS_FreeValue(timeout->_task->_context, result);
tf_task_run_jobs(timeout->_task);
tf_trace_end(timeout->_task->_trace);
free(timeout);
uv_close((uv_handle_t*)handle, _handle_closed);
}
JSValue _tf_task_exit(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{
tf_task_t* task = JS_GetContextOpaque(context);
@ -1053,6 +1001,11 @@ uv_loop_t* tf_task_get_loop(tf_task_t* task)
return &task->_loop;
}
tf_trace_t* tf_task_get_trace(tf_task_t* task)
{
return task->_trace;
}
static promise_t* _tf_task_find_promise(tf_task_t* task, promiseid_t id)
{
for (promise_t* it = task->_promises; it; it = it->next)
@ -1325,7 +1278,6 @@ void tf_task_activate(tf_task_t* task)
tf_util_register(context);
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, "setTimeout", JS_NewCFunction(context, _task_setTimeout, "setTimeout", 2));
JS_SetPropertyStr(context, global, "getFile", JS_NewCFunction(context, _tf_task_getFile, "getFile", 1));
JS_FreeValue(context, global);
}