9 #include "config/kbdbind.h"
10 #include "ecmascript/spidermonkey-shared.h"
11 #include "intl/gettext/libintl.h"
12 #include "scripting/smjs/core.h"
13 #include "scripting/smjs/elinks_object.h"
14 #include "session/session.h"
15 #include "terminal/window.h"
16 #include "util/memory.h"
17 #include "viewer/action.h"
19 /*** action method object ***/
21 struct smjs_action_fn_callback_hop
{
23 action_id_T action_id
;
26 static const JSClass action_fn_class
; /* defined below */
28 /* @action_fn_class.finalize */
30 smjs_action_fn_finalize(JSContext
*ctx
, JSObject
*obj
)
32 struct smjs_action_fn_callback_hop
*hop
;
34 assert(JS_InstanceOf(ctx
, obj
, (JSClass
*) &action_fn_class
, NULL
));
35 if_assert_failed
return;
37 hop
= JS_GetInstancePrivate(ctx
, obj
,
38 (JSClass
*) &action_fn_class
, NULL
);
43 /* @action_fn_class.call */
45 smjs_action_fn_callback(JSContext
*ctx
, uintN argc
, jsval
*rval
)
48 jsval
*argv
= JS_ARGV(ctx
, rval
);
49 struct smjs_action_fn_callback_hop
*hop
;
53 if_assert_failed
return JS_FALSE
;
57 if (JS_TRUE
!= JS_ValueToObject(ctx
, JS_CALLEE(ctx
, rval
), &fn_obj
)) {
58 JS_SET_RVAL(ctx
, rval
, value
);
61 assert(JS_InstanceOf(ctx
, fn_obj
, (JSClass
*) &action_fn_class
, NULL
));
62 if_assert_failed
return JS_FALSE
;
64 hop
= JS_GetInstancePrivate(ctx
, fn_obj
,
65 (JSClass
*) &action_fn_class
, NULL
);
67 JS_SET_RVAL(ctx
, rval
, value
);
71 if (!would_window_receive_keypresses(hop
->ses
->tab
)) {
72 /* The user cannot run actions in this tab by pressing
73 * keys, and some actions could crash if run in this
74 * situation; so we don't let user scripts run actions
77 * In particular, this check should fix bug 943, where
78 * ::ACT_MAIN_TAB_CLOSE called destroy_session(),
79 * which freed struct type_query while BFU dialogs had
80 * pointers to it. That crash could be prevented in
81 * various ways but it seems other similar crashes are
82 * possible, e.g. if the link menu is open and has a
83 * pointer to a session that is then destroyed.
84 * Instead of thoroughly auditing the use of pointers
85 * to sessions and related structures, I'll just
86 * disable the feature, to bring the ELinks 0.12
89 * The "%s" prevents interpretation of any percent
90 * signs in translations. */
91 JS_ReportError(ctx
, "%s",
92 _("Cannot run actions in a tab that doesn't "
93 "have the focus", hop
->ses
->tab
->term
));
94 return JS_FALSE
; /* make JS propagate the exception */
100 if (JS_TRUE
== JS_ValueToInt32(smjs_ctx
, argv
[0], &val
)) {
101 set_kbd_repeat_count(hop
->ses
, val
);
105 do_action(hop
->ses
, hop
->action_id
, 1);
108 JS_SET_RVAL(ctx
, rval
, value
);
113 static const JSClass action_fn_class
= {
115 JSCLASS_HAS_PRIVATE
, /* struct smjs_action_fn_callback_hop * */
116 JS_PropertyStub
, JS_PropertyStub
,
117 JS_PropertyStub
, JS_StrictPropertyStub
,
118 JS_EnumerateStub
, JS_ResolveStub
, JS_ConvertStub
,
119 smjs_action_fn_finalize
,
121 smjs_action_fn_callback
,
125 smjs_get_action_fn_object(unsigned char *action_str
)
128 struct smjs_action_fn_callback_hop
*hop
;
131 if (!smjs_ses
) return NULL
;
133 obj
= JS_NewObject(smjs_ctx
, (JSClass
*) &action_fn_class
, NULL
, NULL
);
134 if (!obj
) return NULL
;
136 hop
= mem_alloc(sizeof(*hop
));
137 if (!hop
) return NULL
;
141 /* ECMAScript methods cannot have hyphens in the name, so one should
142 * use underscores instead; here, we must convert them back to hyphens
143 * for the action code. */
144 for (c
= action_str
; *c
; ++c
) if (*c
== '_') *c
= '-';
146 hop
->action_id
= get_action_from_string(KEYMAP_MAIN
, action_str
);
148 if (-1 != hop
->action_id
149 && JS_TRUE
== JS_SetPrivate(smjs_ctx
, obj
, hop
)) { /* to @action_fn_class */
158 /*** elinks.action object ***/
160 /* @action_class.getProperty */
162 action_get_property(JSContext
*ctx
, JSObject
*obj
, jsid id
, jsval
*vp
)
166 unsigned char *action_str
;
170 JS_IdToValue(ctx
, id
, &val
);
171 action_str
= JS_EncodeString(ctx
, JS_ValueToString(ctx
, val
));
172 if (!action_str
) return JS_TRUE
;
174 action_fn
= smjs_get_action_fn_object(action_str
);
175 if (!action_fn
) return JS_TRUE
;
177 *vp
= OBJECT_TO_JSVAL(action_fn
);
182 static const JSClass action_class
= {
185 JS_PropertyStub
, JS_PropertyStub
,
186 action_get_property
, JS_StrictPropertyStub
,
187 JS_EnumerateStub
, JS_ResolveStub
, JS_ConvertStub
, JS_FinalizeStub
,
191 smjs_get_action_object(void)
197 obj
= JS_NewObject(smjs_ctx
, (JSClass
*) &action_class
, NULL
, NULL
);
203 smjs_init_action_interface(void)
206 struct JSObject
*action_object
;
208 if (!smjs_ctx
|| !smjs_elinks_object
)
211 action_object
= smjs_get_action_object();
212 if (!action_object
) return;
214 val
= OBJECT_TO_JSVAL(action_object
);
216 JS_SetProperty(smjs_ctx
, smjs_elinks_object
, "action", &val
);