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
);
40 if (hop
) mem_free(hop
);
43 /* @action_fn_class.call */
45 smjs_action_fn_callback(JSContext
*ctx
, JSObject
*obj
, uintN argc
, jsval
*argv
,
48 struct smjs_action_fn_callback_hop
*hop
;
52 if_assert_failed
return JS_FALSE
;
56 if (JS_TRUE
!= JS_ValueToObject(ctx
, argv
[-2], &fn_obj
))
58 assert(JS_InstanceOf(ctx
, fn_obj
, (JSClass
*) &action_fn_class
, NULL
));
59 if_assert_failed
return JS_FALSE
;
61 hop
= JS_GetInstancePrivate(ctx
, fn_obj
,
62 (JSClass
*) &action_fn_class
, NULL
);
63 if (!hop
) return JS_TRUE
;
65 if (!would_window_receive_keypresses(hop
->ses
->tab
)) {
66 /* The user cannot run actions in this tab by pressing
67 * keys, and some actions could crash if run in this
68 * situation; so we don't let user scripts run actions
71 * In particular, this check should fix bug 943, where
72 * ::ACT_MAIN_TAB_CLOSE called destroy_session(),
73 * which freed struct type_query while BFU dialogs had
74 * pointers to it. That crash could be prevented in
75 * various ways but it seems other similar crashes are
76 * possible, e.g. if the link menu is open and has a
77 * pointer to a session that is then destroyed.
78 * Instead of thoroughly auditing the use of pointers
79 * to sessions and related structures, I'll just
80 * disable the feature, to bring the ELinks 0.12
83 * The "%s" prevents interpretation of any percent
84 * signs in translations. */
85 JS_ReportError(ctx
, "%s",
86 _("Cannot run actions in a tab that doesn't "
87 "have the focus", hop
->ses
->tab
->term
));
88 return JS_FALSE
; /* make JS propagate the exception */
94 if (JS_TRUE
== JS_ValueToInt32(smjs_ctx
, argv
[0], &val
)) {
95 hop
->ses
->kbdprefix
.repeat_count
= val
;
99 do_action(hop
->ses
, hop
->action_id
, 1);
106 static const JSClass action_fn_class
= {
108 JSCLASS_HAS_PRIVATE
, /* struct smjs_action_fn_callback_hop * */
109 JS_PropertyStub
, JS_PropertyStub
,
110 JS_PropertyStub
, JS_PropertyStub
,
111 JS_EnumerateStub
, JS_ResolveStub
, JS_ConvertStub
,
112 smjs_action_fn_finalize
,
114 smjs_action_fn_callback
,
118 smjs_get_action_fn_object(unsigned char *action_str
)
121 struct smjs_action_fn_callback_hop
*hop
;
124 if (!smjs_ses
) return NULL
;
126 obj
= JS_NewObject(smjs_ctx
, (JSClass
*) &action_fn_class
, NULL
, NULL
);
127 if (!obj
) return NULL
;
129 hop
= mem_alloc(sizeof(*hop
));
130 if (!hop
) return NULL
;
134 /* ECMAScript methods cannot have hyphens in the name, so one should
135 * use underscores instead; here, we must convert them back to hyphens
136 * for the action code. */
137 for (c
= action_str
; *c
; ++c
) if (*c
== '_') *c
= '-';
139 hop
->action_id
= get_action_from_string(KEYMAP_MAIN
, action_str
);
141 if (-1 != hop
->action_id
142 && JS_TRUE
== JS_SetPrivate(smjs_ctx
, obj
, hop
)) { /* to @action_fn_class */
151 /*** elinks.action object ***/
153 /* @action_class.getProperty */
155 action_get_property(JSContext
*ctx
, JSObject
*obj
, jsval id
, jsval
*vp
)
158 unsigned char *action_str
;
162 action_str
= JS_GetStringBytes(JS_ValueToString(ctx
, id
));
163 if (!action_str
) return JS_TRUE
;
165 action_fn
= smjs_get_action_fn_object(action_str
);
166 if (!action_fn
) return JS_TRUE
;
168 *vp
= OBJECT_TO_JSVAL(action_fn
);
173 static const JSClass action_class
= {
176 JS_PropertyStub
, JS_PropertyStub
,
177 action_get_property
, JS_PropertyStub
,
178 JS_EnumerateStub
, JS_ResolveStub
, JS_ConvertStub
, JS_FinalizeStub
,
182 smjs_get_action_object(void)
188 obj
= JS_NewObject(smjs_ctx
, (JSClass
*) &action_class
, NULL
, NULL
);
194 smjs_init_action_interface(void)
197 struct JSObject
*action_object
;
199 if (!smjs_ctx
|| !smjs_elinks_object
)
202 action_object
= smjs_get_action_object();
203 if (!action_object
) return;
205 val
= OBJECT_TO_JSVAL(action_object
);
207 JS_SetProperty(smjs_ctx
, smjs_elinks_object
, "action", &val
);