1 /* The "elinks" object */
11 #include "bfu/msgbox.h"
12 #include "config/home.h"
13 #include "ecmascript/spidermonkey-shared.h"
14 #include "ecmascript/spidermonkey/util.h"
15 #include "intl/gettext/libintl.h"
16 #include "protocol/uri.h"
17 #include "scripting/scripting.h"
18 #include "scripting/smjs/action_object.h"
19 #include "scripting/smjs/bookmarks.h"
20 #include "scripting/smjs/core.h"
21 #include "scripting/smjs/elinks_object.h"
22 #include "scripting/smjs/global_object.h"
23 #include "scripting/smjs/globhist.h"
24 #include "scripting/smjs/keybinding.h"
25 #include "scripting/smjs/load_uri.h"
26 #include "scripting/smjs/session_object.h"
27 #include "scripting/smjs/view_state_object.h"
28 #include "scripting/smjs/terminal_object.h"
29 #include "session/location.h"
30 #include "session/session.h"
31 #include "session/task.h"
34 /* @elinks_funcs{"alert"} */
36 elinks_alert(JSContext
*ctx
, uintN argc
, jsval
*rval
)
39 jsval
*argv
= JS_ARGV(ctx
, rval
);
40 unsigned char *string
;
41 struct terminal
*term
;
46 string
= jsval_to_string(ctx
, &argv
[0]);
51 term
= smjs_ses
->tab
->term
;
53 term
= get_default_terminal();
57 info_box(term
, MSGBOX_NO_TEXT_INTL
,
58 N_("User script alert"), ALIGN_LEFT
, string
);
60 usrerror("%s", string
);
64 undef_to_jsval(ctx
, &val
);
65 JS_SET_RVAL(ctx
, rval
, val
);
70 /* @elinks_funcs{"execute"} */
72 elinks_execute(JSContext
*ctx
, uintN argc
, jsval
*rval
)
75 jsval
*argv
= JS_ARGV(ctx
, rval
);
76 unsigned char *string
;
81 string
= jsval_to_string(ctx
, &argv
[0]);
85 exec_on_terminal(smjs_ses
->tab
->term
, string
, "", TERM_EXEC_BG
);
87 undef_to_jsval(ctx
, &val
);
88 JS_SET_RVAL(ctx
, rval
, val
);
98 static const JSPropertySpec elinks_props
[] = {
99 { "home", ELINKS_HOME
, JSPROP_ENUMERATE
| JSPROP_PERMANENT
| JSPROP_READONLY
},
100 { "location", ELINKS_LOCATION
, JSPROP_ENUMERATE
| JSPROP_PERMANENT
},
101 { "session", ELINKS_SESSION
, JSPROP_ENUMERATE
| JSPROP_PERMANENT
| JSPROP_READONLY
},
105 static const JSClass elinks_class
;
107 /* @elinks_class.getProperty */
109 elinks_get_property(JSContext
*ctx
, JSObject
*obj
, jsid id
, jsval
*vp
)
111 /* This can be called if @obj if not itself an instance of the
112 * appropriate class but has one in its prototype chain. Fail
114 if (!JS_InstanceOf(ctx
, obj
, (JSClass
*) &elinks_class
, NULL
))
117 if (!JSID_IS_INT(id
)) {
118 /* Note: If we return JS_FALSE here, the object's methods and
119 * user-added properties do not work. */
123 undef_to_jsval(ctx
, vp
);
125 switch (JSID_TO_INT(id
)) {
127 *vp
= STRING_TO_JSVAL(JS_NewStringCopyZ(smjs_ctx
, elinks_home
));
130 case ELINKS_LOCATION
: {
133 if (!smjs_ses
) return JS_FALSE
;
135 uri
= have_location(smjs_ses
) ? cur_loc(smjs_ses
)->vs
.uri
136 : smjs_ses
->loading_uri
;
138 *vp
= STRING_TO_JSVAL(JS_NewStringCopyZ(smjs_ctx
,
139 uri
? (const char *) struri(uri
) : ""));
143 case ELINKS_SESSION
: {
146 if (!smjs_ses
) return JS_FALSE
;
148 jsobj
= smjs_get_session_object(smjs_ses
);
149 if (!jsobj
) return JS_FALSE
;
151 object_to_jsval(ctx
, vp
, jsobj
);
156 INTERNAL("Invalid ID %d in elinks_get_property().",
164 elinks_set_property(JSContext
*ctx
, JSObject
*obj
, jsid id
, JSBool strict
, jsval
*vp
)
166 /* This can be called if @obj if not itself an instance of the
167 * appropriate class but has one in its prototype chain. Fail
169 if (!JS_InstanceOf(ctx
, obj
, (JSClass
*) &elinks_class
, NULL
))
172 if (!JSID_IS_INT(id
)) {
173 /* Note: If we return JS_FALSE here, the object's methods and
174 * user-added properties do not work. */
178 switch (JSID_TO_INT(id
)) {
179 case ELINKS_LOCATION
: {
183 if (!smjs_ses
) return JS_FALSE
;
185 jsstr
= JS_ValueToString(smjs_ctx
, *vp
);
186 if (!jsstr
) return JS_FALSE
;
188 url
= JS_EncodeString(smjs_ctx
, jsstr
);
189 if (!url
) return JS_FALSE
;
191 goto_url(smjs_ses
, url
);
196 INTERNAL("Invalid ID %d in elinks_set_property().",
203 static const JSClass elinks_class
= {
206 JS_PropertyStub
, JS_PropertyStub
,
207 elinks_get_property
, elinks_set_property
,
208 JS_EnumerateStub
, JS_ResolveStub
, JS_ConvertStub
, JS_FinalizeStub
211 static const spidermonkeyFunctionSpec elinks_funcs
[] = {
212 { "alert", elinks_alert
, 1 },
213 { "execute", elinks_execute
, 1 },
218 smjs_get_elinks_object(void)
221 assert(smjs_global_object
);
223 return spidermonkey_InitClass(smjs_ctx
, smjs_global_object
, NULL
,
224 (JSClass
*) &elinks_class
, NULL
, 0,
225 (JSPropertySpec
*) elinks_props
,
226 elinks_funcs
, NULL
, NULL
);
230 smjs_init_elinks_object(void)
232 smjs_elinks_object
= smjs_get_elinks_object(); /* TODO: check NULL */
234 smjs_init_action_interface();
235 smjs_init_bookmarks_interface();
236 smjs_init_globhist_interface();
237 smjs_init_keybinding_interface();
238 smjs_init_load_uri_interface();
239 smjs_init_view_state_interface();
240 smjs_init_session_interface();
241 smjs_init_terminal_interface();
244 /* If elinks.<method> is defined, call it with the given arguments,
245 * store the return value in rval, and return JS_TRUE. Else return JS_FALSE. */
247 smjs_invoke_elinks_object_method(unsigned char *method
, jsval argv
[], int argc
,
251 assert(smjs_elinks_object
);
255 if (JS_FALSE
== JS_GetProperty(smjs_ctx
, smjs_elinks_object
,
259 if (JSVAL_IS_VOID(*rval
))
262 return JS_CallFunctionValue(smjs_ctx
, smjs_elinks_object
,
263 *rval
, argc
, argv
, rval
);