1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-12-04 14:46:47 -05:00

ecmascript: Added clearTimeout.

This commit is contained in:
Witold Filipczyk 2007-04-26 22:36:02 +02:00 committed by Witold Filipczyk
parent 05d40f6a63
commit 6d4828440e
5 changed files with 81 additions and 2 deletions

View File

@ -95,6 +95,7 @@ struct SEE_string *s_button;
struct SEE_string *s_hidden; struct SEE_string *s_hidden;
struct SEE_string *s_timeout; struct SEE_string *s_timeout;
struct SEE_string *s_clearTimeout;
struct SEE_string *s_setTimeout; struct SEE_string *s_setTimeout;
struct SEE_string *s_status; struct SEE_string *s_status;
@ -195,6 +196,7 @@ init_intern_strings(void)
s_hidden = SEE_intern_global("hidden"); s_hidden = SEE_intern_global("hidden");
s_timeout = SEE_intern_global("timeout"); s_timeout = SEE_intern_global("timeout");
s_clearTimeout = SEE_intern_global("clearTimeout");
s_setTimeout = SEE_intern_global("setTimeout"); s_setTimeout = SEE_intern_global("setTimeout");
s_status = SEE_intern_global("status"); s_status = SEE_intern_global("status");
} }

View File

@ -100,6 +100,7 @@ extern struct SEE_string *s_button;
extern struct SEE_string *s_hidden; extern struct SEE_string *s_hidden;
extern struct SEE_string *s_timeout; extern struct SEE_string *s_timeout;
extern struct SEE_string *s_clearTimeout;
extern struct SEE_string *s_setTimeout; extern struct SEE_string *s_setTimeout;
extern struct SEE_string *s_status; extern struct SEE_string *s_status;
#endif #endif

View File

@ -73,6 +73,25 @@ struct SEE_objectclass js_window_object_class = {
NULL NULL
}; };
struct SEE_objectclass js_timeout_object_class = {
"timeout",
SEE_no_get,
SEE_no_put,
SEE_no_canput,
SEE_no_hasproperty,
SEE_no_delete,
SEE_no_defaultvalue,
NULL,
NULL,
NULL,
NULL
};
struct timeout_class {
struct SEE_object object;
struct timeout_data *td;
};
static struct js_window_object * static struct js_window_object *
js_get_global_object(void *data) js_get_global_object(void *data)
{ {
@ -138,6 +157,8 @@ window_get(struct SEE_interpreter *interp, struct SEE_object *o,
SEE_SET_OBJECT(res, win->open); SEE_SET_OBJECT(res, win->open);
} else if (p == s_setTimeout) { } else if (p == s_setTimeout) {
SEE_SET_OBJECT(res, win->setTimeout); SEE_SET_OBJECT(res, win->setTimeout);
} else if (p == s_clearTimeout) {
SEE_SET_OBJECT(res, win->clearTimeout);
} else if (p == s_location) { } else if (p == s_location) {
SEE_OBJECT_GET(interp, interp->Global, s_location, res); SEE_OBJECT_GET(interp, interp->Global, s_location, res);
} else if (p == s_navigator) { } else if (p == s_navigator) {
@ -329,6 +350,20 @@ end:
done_uri(uri); done_uri(uri);
} }
static void
js_clearTimeout(struct SEE_interpreter *interp, struct SEE_object *self,
struct SEE_object *thisobj, int argc, struct SEE_value **argv,
struct SEE_value *res)
{
struct timeout_class *timer;
if (argc != 1) return;
timer = (struct timeout_class *)argv[0]->u.object;
see_check_class(interp, (struct SEE_object *)timer, &js_timeout_object_class);
ecmascript_clear_timeout(timer->td);
}
static void static void
js_setTimeout(struct SEE_interpreter *interp, struct SEE_object *self, js_setTimeout(struct SEE_interpreter *interp, struct SEE_object *self,
struct SEE_object *thisobj, int argc, struct SEE_value **argv, struct SEE_object *thisobj, int argc, struct SEE_value **argv,
@ -336,6 +371,8 @@ js_setTimeout(struct SEE_interpreter *interp, struct SEE_object *self,
{ {
struct ecmascript_interpreter *ei; struct ecmascript_interpreter *ei;
unsigned char *code; unsigned char *code;
struct timeout_data *td;
struct timeout_class *timer;
int timeout; int timeout;
if (thisobj != interp->Global) if (thisobj != interp->Global)
@ -345,7 +382,12 @@ js_setTimeout(struct SEE_interpreter *interp, struct SEE_object *self,
ei = ((struct global_object *)interp)->interpreter; ei = ((struct global_object *)interp)->interpreter;
code = see_value_to_unsigned_char(interp, argv[0]); code = see_value_to_unsigned_char(interp, argv[0]);
timeout = SEE_ToInt32(interp, argv[1]); timeout = SEE_ToInt32(interp, argv[1]);
ecmascript_set_timeout(ei, code, timeout); td = ecmascript_set_timeout(ei, code, timeout);
timer = SEE_NEW(interp, struct timeout_class);
timer->object.objectclass = &js_timeout_object_class;
timer->object.Prototype = NULL;
timer->td = td;
SEE_SET_OBJECT(res, (struct SEE_object *)timer);
} }
void void
@ -365,6 +407,7 @@ init_js_window_object(struct ecmascript_interpreter *interpreter)
SEE_OBJECT_PUT(interp, interp->Global, s_window, &v, 0); SEE_OBJECT_PUT(interp, interp->Global, s_window, &v, 0);
g->win->alert = SEE_cfunction_make(interp, js_window_alert, s_alert, 1); g->win->alert = SEE_cfunction_make(interp, js_window_alert, s_alert, 1);
g->win->clearTimeout = SEE_cfunction_make(interp, js_clearTimeout, s_clearTimeout, 1);
g->win->open = SEE_cfunction_make(interp, js_window_open, s_open, 3); g->win->open = SEE_cfunction_make(interp, js_window_open, s_open, 3);
g->win->setTimeout = SEE_cfunction_make(interp, js_setTimeout, s_setTimeout, 2); g->win->setTimeout = SEE_cfunction_make(interp, js_setTimeout, s_setTimeout, 2);
@ -380,6 +423,8 @@ init_js_window_object(struct ecmascript_interpreter *interpreter)
SEE_OBJECT_PUT(interp, interp->Global, s_open, &v, 0); SEE_OBJECT_PUT(interp, interp->Global, s_open, &v, 0);
SEE_OBJECT_GET(interp, (struct SEE_object *)g->win, s_setTimeout, &v); SEE_OBJECT_GET(interp, (struct SEE_object *)g->win, s_setTimeout, &v);
SEE_OBJECT_PUT(interp, interp->Global, s_setTimeout, &v, 0); SEE_OBJECT_PUT(interp, interp->Global, s_setTimeout, &v, 0);
SEE_OBJECT_GET(interp, (struct SEE_object *)g->win, s_clearTimeout, &v);
SEE_OBJECT_PUT(interp, interp->Global, s_clearTimeout, &v, 0);
} }
void void

View File

@ -13,6 +13,7 @@ struct js_window_object {
struct view_state *vs; struct view_state *vs;
struct SEE_object *alert; struct SEE_object *alert;
struct SEE_object *open; struct SEE_object *open;
struct SEE_object *clearTimeout;
struct SEE_object *setTimeout; struct SEE_object *setTimeout;
}; };

View File

@ -55,6 +55,13 @@ const JSClass window_class = {
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub
}; };
const JSClass timeout_class = {
"timeout",
JSCLASS_HAS_PRIVATE,
JS_PropertyStub, JS_PropertyStub,
JS_PropertyStub, JS_PropertyStub,
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub
};
/* Tinyids of properties. Use negative values to distinguish these /* Tinyids of properties. Use negative values to distinguish these
* from array indexes (even though this object has no array elements). * from array indexes (even though this object has no array elements).
@ -300,10 +307,12 @@ window_set_property(JSContext *ctx, JSObject *obj, jsval id, jsval *vp)
static JSBool window_alert(JSContext *ctx, JSObject *obj, uintN argc, jsval *argv, jsval *rval); static JSBool window_alert(JSContext *ctx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
static JSBool window_open(JSContext *ctx, JSObject *obj, uintN argc, jsval *argv, jsval *rval); static JSBool window_open(JSContext *ctx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
static JSBool window_clearTimeout(JSContext *ctx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
static JSBool window_setTimeout(JSContext *ctx, JSObject *obj, uintN argc, jsval *argv, jsval *rval); static JSBool window_setTimeout(JSContext *ctx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
const JSFunctionSpec window_funcs[] = { const JSFunctionSpec window_funcs[] = {
{ "alert", window_alert, 1 }, { "alert", window_alert, 1 },
{ "clearTimeout",window_clearTimeout, 1 },
{ "open", window_open, 3 }, { "open", window_open, 3 },
{ "setTimeout", window_setTimeout, 2 }, { "setTimeout", window_setTimeout, 2 },
{ NULL } { NULL }
@ -435,12 +444,30 @@ end:
return JS_TRUE; return JS_TRUE;
} }
/* @window_funcs{"clearTimeout"} */
static JSBool
window_clearTimeout(JSContext *ctx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
JSObject *timeout;
struct timeout_data *td;
if (argc != 1)
return JS_TRUE;
timeout = JSVAL_TO_OBJECT(argv[0]);
if (!JS_InstanceOf(ctx, timeout, (JSClass *) &timeout_class, NULL)) return JS_FALSE;
td = JS_GetPrivate(ctx, timeout);
ecmascript_clear_timeout(td);
return JS_TRUE;
}
/* @window_funcs{"setTimeout"} */ /* @window_funcs{"setTimeout"} */
static JSBool static JSBool
window_setTimeout(JSContext *ctx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) window_setTimeout(JSContext *ctx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{ {
struct ecmascript_interpreter *interpreter = JS_GetContextPrivate(ctx); struct ecmascript_interpreter *interpreter = JS_GetContextPrivate(ctx);
unsigned char *code; unsigned char *code;
struct timeout_data *td;
JSObject *timer;
int timeout; int timeout;
if (argc != 2) if (argc != 2)
@ -458,6 +485,9 @@ window_setTimeout(JSContext *ctx, JSObject *obj, uintN argc, jsval *argv, jsval
mem_free(code); mem_free(code);
return JS_TRUE; return JS_TRUE;
} }
ecmascript_set_timeout(interpreter, code, timeout); td = ecmascript_set_timeout(interpreter, code, timeout);
timer = JS_NewObject(ctx, (JSClass *)&timeout_class, NULL, NULL);
JS_SetPrivate(ctx, timer, td);
object_to_jsval(ctx, rval, timer);
return JS_TRUE; return JS_TRUE;
} }