9 #include "config/kbdbind.h"
10 #include "ecmascript/spidermonkey/util.h"
11 #include "scripting/smjs/core.h"
12 #include "scripting/smjs/elinks_object.h"
13 #include "session/session.h"
14 #include "util/memory.h"
15 #include "viewer/action.h"
17 /*** action method object ***/
19 struct smjs_action_fn_callback_hop
{
21 action_id_T action_id
;
25 smjs_action_fn_finalize(JSContext
*ctx
, JSObject
*obj
)
27 struct smjs_action_fn_callback_hop
*hop
= JS_GetPrivate(ctx
, obj
);
29 if (hop
) mem_free(hop
);
33 smjs_action_fn_callback(JSContext
*ctx
, JSObject
*obj
, uintN argc
, jsval
*argv
,
36 struct smjs_action_fn_callback_hop
*hop
;
43 if (JS_TRUE
!= JS_ValueToObject(ctx
, argv
[-2], &fn_obj
))
46 hop
= JS_GetPrivate(ctx
, fn_obj
);
47 if (!hop
) return JS_TRUE
;
52 if (JS_TRUE
== JS_ValueToInt32(smjs_ctx
, argv
[0], &val
)) {
53 hop
->ses
->kbdprefix
.repeat_count
= val
;
57 do_action(hop
->ses
, hop
->action_id
, 1);
64 static const JSClass action_fn_class
= {
66 JSCLASS_HAS_PRIVATE
, /* struct smjs_action_fn_callback_hop * */
67 JS_PropertyStub
, JS_PropertyStub
,
68 JS_PropertyStub
, JS_PropertyStub
,
69 JS_EnumerateStub
, JS_ResolveStub
, JS_ConvertStub
,
70 smjs_action_fn_finalize
,
72 smjs_action_fn_callback
,
76 smjs_get_action_fn_object(unsigned char *action_str
)
79 struct smjs_action_fn_callback_hop
*hop
;
82 if (!smjs_ses
) return NULL
;
84 obj
= JS_NewObject(smjs_ctx
, (JSClass
*) &action_fn_class
, NULL
, NULL
);
85 if (!obj
) return NULL
;
87 hop
= mem_alloc(sizeof(*hop
));
88 if (!hop
) return NULL
;
92 /* ECMAScript methods cannot have hyphens in the name, so one should
93 * use underscores instead; here, we must convert them back to hyphens
94 * for the action code. */
95 for (c
= action_str
; *c
; ++c
) if (*c
== '_') *c
= '-';
97 hop
->action_id
= get_action_from_string(KEYMAP_MAIN
, action_str
);
99 if (-1 != hop
->action_id
100 && JS_TRUE
== JS_SetPrivate(smjs_ctx
, obj
, hop
)) {
109 /*** elinks.action object ***/
112 action_get_property(JSContext
*ctx
, JSObject
*obj
, jsval id
, jsval
*vp
)
115 unsigned char *action_str
;
119 action_str
= JS_GetStringBytes(JS_ValueToString(ctx
, id
));
120 if (!action_str
) return JS_TRUE
;
122 action_fn
= smjs_get_action_fn_object(action_str
);
123 if (!action_fn
) return JS_TRUE
;
125 *vp
= OBJECT_TO_JSVAL(action_fn
);
130 static const JSClass action_class
= {
133 JS_PropertyStub
, JS_PropertyStub
,
134 action_get_property
, JS_PropertyStub
,
135 JS_EnumerateStub
, JS_ResolveStub
, JS_ConvertStub
, JS_FinalizeStub
,
139 smjs_get_action_object(void)
145 obj
= JS_NewObject(smjs_ctx
, (JSClass
*) &action_class
, NULL
, NULL
);
151 smjs_init_action_interface(void)
154 struct JSObject
*action_object
;
156 if (!smjs_ctx
|| !smjs_elinks_object
)
159 action_object
= smjs_get_action_object();
160 if (!action_object
) return;
162 val
= OBJECT_TO_JSVAL(action_object
);
164 JS_SetProperty(smjs_ctx
, smjs_elinks_object
, "action", &val
);