forked from cory/tildefriends
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:
parent
5fbe9c42bc
commit
05b55c849a
58
src/task.c
58
src/task.c
@ -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);
|
||||
}
|
||||
|
@ -12,8 +12,8 @@ typedef int taskid_t;
|
||||
typedef int promiseid_t;
|
||||
typedef int exportid_t;
|
||||
typedef struct _tf_taskstub_t tf_taskstub_t;
|
||||
|
||||
typedef struct _tf_task_t tf_task_t;
|
||||
typedef struct _tf_trace_t tf_trace_t;
|
||||
|
||||
static const taskid_t k_task_parent_id = 0;
|
||||
|
||||
@ -51,6 +51,7 @@ exportid_t tf_task_export_function(tf_task_t* task, tf_taskstub_t* to, JSValue f
|
||||
JSValue tf_task_add_import(tf_task_t* task, taskid_t stub_id, exportid_t export_id);
|
||||
uv_loop_t* tf_task_get_loop(tf_task_t* task);
|
||||
tf_task_t* tf_task_get(JSContext* context);
|
||||
tf_trace_t* tf_task_get_trace(tf_task_t* task);
|
||||
void tf_task_run_jobs(tf_task_t* task);
|
||||
const char* tf_task_get_name(tf_task_t* task);
|
||||
|
||||
|
@ -1,9 +1,14 @@
|
||||
#include "util.js.h"
|
||||
|
||||
#include "task.h"
|
||||
#include "trace.h"
|
||||
|
||||
#include "quickjs-libc.h"
|
||||
|
||||
#include <uv.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
static JSValue _util_utf8_decode(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
||||
{
|
||||
JSValue result = JS_NULL;
|
||||
@ -121,10 +126,62 @@ void tf_util_report_error(JSContext* context, JSValue value)
|
||||
}
|
||||
}
|
||||
|
||||
typedef struct _timeout_t {
|
||||
tf_task_t* _task;
|
||||
JSValue _callback;
|
||||
} timeout_t;
|
||||
|
||||
static void _handle_closed(uv_handle_t* handle)
|
||||
{
|
||||
free(handle);
|
||||
}
|
||||
|
||||
static void _util_timeoutCallback(uv_timer_t* handle)
|
||||
{
|
||||
timeout_t* timeout = handle->data;
|
||||
tf_trace_begin(tf_task_get_trace(timeout->_task), "_util_timeoutCallback");
|
||||
JSContext* context = tf_task_get_context(timeout->_task);
|
||||
JSValue result = JS_Call(
|
||||
context,
|
||||
timeout->_callback,
|
||||
JS_NULL,
|
||||
0,
|
||||
NULL);
|
||||
tf_util_report_error(context, result);
|
||||
JS_FreeValue(context, result);
|
||||
tf_task_run_jobs(timeout->_task);
|
||||
tf_trace_end(tf_task_get_trace(timeout->_task));
|
||||
free(timeout);
|
||||
uv_close((uv_handle_t*)handle, _handle_closed);
|
||||
}
|
||||
|
||||
static JSValue _util_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(tf_task_get_loop(task), timer);
|
||||
timer->data = timeout;
|
||||
|
||||
int64_t duration;
|
||||
JS_ToInt64(context, &duration, argv[1]);
|
||||
uv_timer_start(timer, _util_timeoutCallback, duration, 0);
|
||||
return JS_NULL;
|
||||
}
|
||||
|
||||
void tf_util_register(JSContext* context)
|
||||
{
|
||||
JSValue global = JS_GetGlobalObject(context);
|
||||
JS_SetPropertyStr(context, global, "utf8Decode", JS_NewCFunction(context, _util_utf8_decode, "utf8Decode", 1));
|
||||
JS_SetPropertyStr(context, global, "print", JS_NewCFunction(context, _util_print, "print", 1));
|
||||
JS_SetPropertyStr(context, global, "setTimeout", JS_NewCFunction(context, _util_setTimeout, "setTimeout", 2));
|
||||
JS_FreeValue(context, global);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user