1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-07-01 02:05:33 +00:00

[mozjs31] scripting smjs

It was not heavily tested.
This commit is contained in:
Witold Filipczyk 2020-10-12 18:43:56 +02:00
parent dd704b900a
commit ca24054cc6
14 changed files with 1492 additions and 777 deletions

View File

@ -24,7 +24,7 @@ struct smjs_action_fn_callback_hop {
};
static void smjs_action_fn_finalize(JSFreeOp *op, JSObject *obj);
static JSBool smjs_action_fn_callback(JSContext *ctx, unsigned int argc, jsval *rval);
static bool smjs_action_fn_callback(JSContext *ctx, unsigned int argc, jsval *rval);
static const JSClass action_fn_class = {
"action_fn",
@ -57,31 +57,31 @@ smjs_action_fn_finalize(JSFreeOp *op, JSObject *obj)
}
/* @action_fn_class.call */
static JSBool
static bool
smjs_action_fn_callback(JSContext *ctx, unsigned int argc, jsval *rval)
{
jsval value;
jsval *argv = JS_ARGV(ctx, rval);
JS::CallArgs args = CallArgsFromVp(argc, rval);
struct smjs_action_fn_callback_hop *hop;
JSObject *fn_obj;
JS::RootedObject fn_obj(ctx);
assert(smjs_ctx);
if_assert_failed return JS_FALSE;
if_assert_failed return false;
value = JSVAL_FALSE;
if (JS_TRUE != JS_ValueToObject(ctx, JS_CALLEE(ctx, rval), &fn_obj)) {
JS_SET_RVAL(ctx, rval, value);
return JS_TRUE;
// if (true != JS_ValueToObject(ctx, JS_CALLEE(ctx, rval), &fn_obj)) {
if (true != JS_ValueToObject(ctx, args[0], &fn_obj)) {
args.rval().setBoolean(false);
return true;
}
assert(JS_InstanceOf(ctx, fn_obj, (JSClass *) &action_fn_class, NULL));
if_assert_failed return JS_FALSE;
if_assert_failed return false;
hop = JS_GetInstancePrivate(ctx, fn_obj,
(JSClass *) &action_fn_class, NULL);
if (!hop) {
JS_SET_RVAL(ctx, rval, value);
return JS_TRUE;
args.rval().setBoolean(false);
return true;
}
if (!would_window_receive_keypresses(hop->ses->tab)) {
@ -107,23 +107,21 @@ smjs_action_fn_callback(JSContext *ctx, unsigned int argc, jsval *rval)
JS_ReportError(ctx, "%s",
_("Cannot run actions in a tab that doesn't "
"have the focus", hop->ses->tab->term));
return JS_FALSE; /* make JS propagate the exception */
return false; /* make JS propagate the exception */
}
if (argc >= 1) {
int32_t val;
if (JS_TRUE == JS_ValueToInt32(smjs_ctx, argv[0], &val)) {
if (true == JS::ToInt32(smjs_ctx, args[0], &val)) {
set_kbd_repeat_count(hop->ses, val);
}
}
do_action(hop->ses, hop->action_id, 1);
args.rval().setBoolean(true);
value = JSVAL_TRUE;
JS_SET_RVAL(ctx, rval, value);
return JS_TRUE;
return true;
}
@ -136,7 +134,7 @@ smjs_get_action_fn_object(unsigned char *action_str)
if (!smjs_ses) return NULL;
obj = JS_NewObject(smjs_ctx, (JSClass *) &action_fn_class, NULL, NULL);
obj = JS_NewObject(smjs_ctx, (JSClass *) &action_fn_class, JS::NullPtr(), JS::NullPtr());
if (!obj) return NULL;
hop = mem_alloc(sizeof(*hop));
@ -164,29 +162,28 @@ smjs_get_action_fn_object(unsigned char *action_str)
/*** elinks.action object ***/
/* @action_class.getProperty */
static JSBool
static bool
action_get_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp)
{
ELINKS_CAST_PROP_PARAMS
jsid id = hid.get();
(void)obj;
jsval val;
JS::RootedValue rval(ctx, val);
JSObject *action_fn;
unsigned char *action_str;
*vp = JSVAL_NULL;
hvp.setNull();
JS_IdToValue(ctx, id, &val);
action_str = JS_EncodeString(ctx, JS_ValueToString(ctx, val));
if (!action_str) return JS_TRUE;
JS_IdToValue(ctx, id, &rval);
action_str = JS_EncodeString(ctx, JS::ToString(ctx, rval));
if (!action_str) return true;
action_fn = smjs_get_action_fn_object(action_str);
if (!action_fn) return JS_TRUE;
if (!action_fn) return true;
*vp = OBJECT_TO_JSVAL(action_fn);
hvp.setObject(*action_fn);
return JS_TRUE;
return true;
}
static const JSClass action_class = {
@ -204,7 +201,7 @@ smjs_get_action_object(void)
assert(smjs_ctx);
obj = JS_NewObject(smjs_ctx, (JSClass *) &action_class, NULL, NULL);
obj = JS_NewObject(smjs_ctx, (JSClass *) &action_class, JS::NullPtr(), JS::NullPtr());
return obj;
}
@ -221,7 +218,10 @@ smjs_init_action_interface(void)
action_object = smjs_get_action_object();
if (!action_object) return;
val = OBJECT_TO_JSVAL(action_object);
JS::RootedObject r_smjs_elinks_object(smjs_ctx, smjs_elinks_object);
JS_SetProperty(smjs_ctx, smjs_elinks_object, "action", &val);
JS::RootedValue r_val(smjs_ctx, val);
r_val.setObject(*r_smjs_elinks_object);
JS_SetProperty(smjs_ctx, r_smjs_elinks_object, "action", r_val);
}

View File

@ -18,7 +18,7 @@
/*** common code ***/
static void bookmark_finalize(JSFreeOp *op, JSObject *obj);
static JSBool bookmark_folder_get_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp);
static bool bookmark_folder_get_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp);
static const JSClass bookmark_class = {
"bookmark",
@ -44,7 +44,7 @@ smjs_get_bookmark_generic_object(struct bookmark *bookmark, JSClass *clasp)
assert(clasp == &bookmark_class || clasp == &bookmark_folder_class);
if_assert_failed return NULL;
jsobj = JS_NewObject(smjs_ctx, clasp, NULL, NULL);
jsobj = JS_NewObject(smjs_ctx, clasp, JS::NullPtr(), JS::NullPtr());
if (!jsobj) return NULL;
if (!bookmark) return jsobj;
@ -84,16 +84,16 @@ enum bookmark_prop {
BOOKMARK_CHILDREN = -3,
};
static JSBool bookmark_get_property_title(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp);
static JSBool bookmark_set_property_title(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JSBool strict, JS::MutableHandleValue hvp);
static JSBool bookmark_get_property_url(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp);
static JSBool bookmark_set_property_url(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JSBool strict, JS::MutableHandleValue hvp);
static JSBool bookmark_get_property_children(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp);
static bool bookmark_get_property_title(JSContext *ctx, unsigned int argc, jsval *vp);
static bool bookmark_set_property_title(JSContext *ctx, unsigned int argc, jsval *vp);
static bool bookmark_get_property_url(JSContext *ctx, unsigned int argc, jsval *vp);
static bool bookmark_set_property_url(JSContext *ctx, unsigned int argc, jsval *vp);
static bool bookmark_get_property_children(JSContext *ctx, unsigned int argc, jsval *vp);
static const JSPropertySpec bookmark_props[] = {
{ "title", 0, JSPROP_ENUMERATE, JSOP_WRAPPER(bookmark_get_property_title), JSOP_WRAPPER(bookmark_set_property_title) },
{ "url", 0, JSPROP_ENUMERATE, JSOP_WRAPPER(bookmark_get_property_url), JSOP_WRAPPER(bookmark_set_property_url) },
{ "children", 0, JSPROP_ENUMERATE | JSPROP_READONLY, JSOP_WRAPPER(bookmark_get_property_children), JSOP_NULLWRAPPER },
JS_PSGS("title", bookmark_get_property_title, bookmark_set_property_title, JSPROP_ENUMERATE),
JS_PSGS( "url", bookmark_get_property_url, bookmark_set_property_url, JSPROP_ENUMERATE),
JS_PSG("children", bookmark_get_property_children, JSPROP_ENUMERATE),
{ NULL }
};
@ -101,17 +101,17 @@ static JSObject *smjs_get_bookmark_folder_object(struct bookmark *bookmark);
/** Convert a string retrieved from struct bookmark to a jsval.
*
* @return JS_TRUE if successful. On error, report the error and
* return JS_FALSE. */
static JSBool
* @return true if successful. On error, report the error and
* return false. */
static bool
bookmark_string_to_jsval(JSContext *ctx, const unsigned char *str, jsval *vp)
{
JSString *jsstr = utf8_to_jsstring(ctx, str, -1);
if (jsstr == NULL)
return JS_FALSE;
return false;
*vp = STRING_TO_JSVAL(jsstr);
return JS_TRUE;
return true;
}
/** Convert a jsval to a string and store it in struct bookmark.
@ -124,72 +124,61 @@ bookmark_string_to_jsval(JSContext *ctx, const unsigned char *str, jsval *vp)
* A string allocated with mem_alloc().
* On success, this function frees the original string, if any.
*
* @return JS_TRUE if successful. On error, report the error to
* SpiderMonkey and return JS_FALSE. */
static JSBool
jsval_to_bookmark_string(JSContext *ctx, jsval val, unsigned char **result)
* @return true if successful. On error, report the error to
* SpiderMonkey and return false. */
static bool
jsval_to_bookmark_string(JSContext *ctx, JS::HandleValue val, unsigned char **result)
{
JSString *jsstr = NULL;
unsigned char *str;
/* JS_ValueToString constructs a new string if val is not
* already a string. Protect the new string from the garbage
* collector, which jsstring_to_utf8() may trigger.
*
* Actually, SpiderMonkey 1.8.5 does not require this
* JS_AddNamedStringRoot call because it conservatively scans
* the C stack for GC roots. Do the call anyway, because:
* 1. Omitting the call would require somehow ensuring that the
* C compiler won't reuse the stack location too early.
* (See template class js::Anchor in <jsapi.h>.)
* 2. Later versions of SpiderMonkey are switching back to
* precise GC rooting, with a C++-only API.
* 3. jsval_to_bookmark_string() does not seem speed-critical. */
if (!JS_AddNamedStringRoot(ctx, &jsstr, "jsval_to_bookmark_string"))
return JS_FALSE;
JSString *jsstr = JS::ToString(ctx, val);
jsstr = JS_ValueToString(ctx, val);
if (jsstr == NULL) {
JS_RemoveStringRoot(ctx, &jsstr);
return JS_FALSE;
return false;
}
str = jsstring_to_utf8(ctx, jsstr, NULL);
if (str == NULL) {
JS_RemoveStringRoot(ctx, &jsstr);
return JS_FALSE;
return false;
}
JS_RemoveStringRoot(ctx, &jsstr);
mem_free_set(result, str);
return JS_TRUE;
return true;
}
static JSBool
bookmark_get_property_title(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp)
static bool
bookmark_get_property_title(JSContext *ctx, unsigned int argc, jsval *vp)
{
ELINKS_CAST_PROP_PARAMS
JS::CallArgs args = CallArgsFromVp(argc, vp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
struct bookmark *bookmark;
/* This can be called if @obj if not itself an instance of the
* appropriate class but has one in its prototype chain. Fail
* such calls. */
if (!JS_InstanceOf(ctx, obj, (JSClass *) &bookmark_class, NULL))
return JS_FALSE;
if (!JS_InstanceOf(ctx, hobj, (JSClass *) &bookmark_class, NULL))
return false;
bookmark = JS_GetInstancePrivate(ctx, obj,
bookmark = JS_GetInstancePrivate(ctx, hobj,
(JSClass *) &bookmark_class, NULL);
if (!bookmark) return JS_FALSE;
if (!bookmark) return false;
return bookmark_string_to_jsval(ctx, bookmark->title, vp);
JSString *jsstr = utf8_to_jsstring(ctx, bookmark->title, -1);
if (!jsstr) {
return false;
}
args.rval().setString(jsstr);
return true;
}
static JSBool
bookmark_set_property_title(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JSBool strict, JS::MutableHandleValue hvp)
static bool
bookmark_set_property_title(JSContext *ctx, unsigned int argc, jsval *vp)
{
ELINKS_CAST_PROP_PARAMS
JS::CallArgs args = CallArgsFromVp(argc, vp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
struct bookmark *bookmark;
unsigned char *title = NULL;
@ -199,48 +188,56 @@ bookmark_set_property_title(JSContext *ctx, JS::HandleObject hobj, JS::HandleId
/* This can be called if @obj if not itself an instance of the
* appropriate class but has one in its prototype chain. Fail
* such calls. */
if (!JS_InstanceOf(ctx, obj, (JSClass *) &bookmark_class, NULL))
return JS_FALSE;
if (!JS_InstanceOf(ctx, hobj, (JSClass *) &bookmark_class, NULL))
return false;
bookmark = JS_GetInstancePrivate(ctx, obj,
bookmark = JS_GetInstancePrivate(ctx, hobj,
(JSClass *) &bookmark_class, NULL);
if (!bookmark) return JS_FALSE;
if (!bookmark) return false;
if (!jsval_to_bookmark_string(ctx, *vp, &title))
return JS_FALSE;
if (!jsval_to_bookmark_string(ctx, args[0], &title))
return false;
ok = update_bookmark(bookmark, get_cp_index("UTF-8"), title, url);
mem_free_if(title);
mem_free_if(url);
return ok ? JS_TRUE : JS_FALSE;
return ok ? true : false;
}
static JSBool
bookmark_get_property_url(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp)
static bool
bookmark_get_property_url(JSContext *ctx, unsigned int argc, jsval *vp)
{
ELINKS_CAST_PROP_PARAMS
JS::CallArgs args = CallArgsFromVp(argc, vp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
struct bookmark *bookmark;
/* This can be called if @obj if not itself an instance of the
* appropriate class but has one in its prototype chain. Fail
* such calls. */
if (!JS_InstanceOf(ctx, obj, (JSClass *) &bookmark_class, NULL))
return JS_FALSE;
if (!JS_InstanceOf(ctx, hobj, (JSClass *) &bookmark_class, NULL))
return false;
bookmark = JS_GetInstancePrivate(ctx, obj,
bookmark = JS_GetInstancePrivate(ctx, hobj,
(JSClass *) &bookmark_class, NULL);
if (!bookmark) return JS_FALSE;
if (!bookmark) return false;
return bookmark_string_to_jsval(ctx, bookmark->url, vp);
JSString *jsstr = utf8_to_jsstring(ctx, bookmark->url, -1);
if (!jsstr) {
return false;
}
args.rval().setString(jsstr);
return true;
}
static JSBool
bookmark_set_property_url(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JSBool strict, JS::MutableHandleValue hvp)
static bool
bookmark_set_property_url(JSContext *ctx, unsigned int argc, jsval *vp)
{
ELINKS_CAST_PROP_PARAMS
JS::CallArgs args = CallArgsFromVp(argc, vp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
struct bookmark *bookmark;
unsigned char *title = NULL;
@ -250,44 +247,45 @@ bookmark_set_property_url(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hi
/* This can be called if @obj if not itself an instance of the
* appropriate class but has one in its prototype chain. Fail
* such calls. */
if (!JS_InstanceOf(ctx, obj, (JSClass *) &bookmark_class, NULL))
return JS_FALSE;
if (!JS_InstanceOf(ctx, hobj, (JSClass *) &bookmark_class, NULL))
return false;
bookmark = JS_GetInstancePrivate(ctx, obj,
bookmark = JS_GetInstancePrivate(ctx, hobj,
(JSClass *) &bookmark_class, NULL);
if (!bookmark) return JS_FALSE;
if (!bookmark) return false;
if (!jsval_to_bookmark_string(ctx, *vp, &url))
return JS_FALSE;
if (!jsval_to_bookmark_string(ctx, args[0], &url))
return false;
ok = update_bookmark(bookmark, get_cp_index("UTF-8"), title, url);
mem_free_if(title);
mem_free_if(url);
return ok ? JS_TRUE : JS_FALSE;
return ok ? true : false;
}
static JSBool
bookmark_get_property_children(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp)
static bool
bookmark_get_property_children(JSContext *ctx, unsigned int argc, jsval *vp)
{
ELINKS_CAST_PROP_PARAMS
JS::CallArgs args = CallArgsFromVp(argc, vp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
struct bookmark *bookmark;
/* This can be called if @obj if not itself an instance of the
* appropriate class but has one in its prototype chain. Fail
* such calls. */
if (!JS_InstanceOf(ctx, obj, (JSClass *) &bookmark_class, NULL))
return JS_FALSE;
if (!JS_InstanceOf(ctx, hobj, (JSClass *) &bookmark_class, NULL))
return false;
bookmark = JS_GetInstancePrivate(ctx, obj,
bookmark = JS_GetInstancePrivate(ctx, hobj,
(JSClass *) &bookmark_class, NULL);
if (!bookmark) return JS_FALSE;
if (!bookmark) return false;
*vp = OBJECT_TO_JSVAL(smjs_get_bookmark_folder_object(bookmark));
args.rval().setObject(*smjs_get_bookmark_folder_object(bookmark));
return JS_TRUE;
return true;
}
@ -299,8 +297,9 @@ smjs_get_bookmark_object(struct bookmark *bookmark)
jsobj = smjs_get_bookmark_generic_object(bookmark,
(JSClass *) &bookmark_class);
JS::RootedObject r_jsobj(smjs_ctx, jsobj);
if (jsobj
&& JS_TRUE == JS_DefineProperties(smjs_ctx, jsobj,
&& true == JS_DefineProperties(smjs_ctx, r_jsobj,
(JSPropertySpec *) bookmark_props))
return jsobj;
@ -311,41 +310,41 @@ smjs_get_bookmark_object(struct bookmark *bookmark)
/*** bookmark folder object ***/
/* @bookmark_folder_class.getProperty */
static JSBool
static bool
bookmark_folder_get_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp)
{
ELINKS_CAST_PROP_PARAMS
jsid id = hid.get();
struct bookmark *bookmark;
struct bookmark *folder;
jsval title_jsval = JSVAL_VOID;
jsval val;
JS::RootedValue title_jsval(ctx, val);
unsigned char *title = NULL;
/* This can be called if @obj if not itself an instance of the
* appropriate class but has one in its prototype chain. Fail
* such calls. */
if (!JS_InstanceOf(ctx, obj, (JSClass *) &bookmark_folder_class, NULL))
return JS_FALSE;
if (!JS_InstanceOf(ctx, hobj, (JSClass *) &bookmark_folder_class, NULL))
return false;
folder = JS_GetInstancePrivate(ctx, obj,
folder = JS_GetInstancePrivate(ctx, hobj,
(JSClass *) &bookmark_folder_class, NULL);
*vp = JSVAL_NULL;
hvp.setNull();
if (!JS_IdToValue(ctx, id, &title_jsval))
return JS_FALSE;
return false;
if (!jsval_to_bookmark_string(ctx, title_jsval, &title))
return JS_FALSE;
return false;
bookmark = get_bookmark_by_name(folder, title);
if (bookmark) {
*vp = OBJECT_TO_JSVAL(smjs_get_bookmark_object(bookmark));
hvp.setObject(*smjs_get_bookmark_object(bookmark));
}
mem_free(title);
return JS_TRUE;
return true;
}
@ -368,7 +367,9 @@ smjs_init_bookmarks_interface(void)
bookmarks_object = smjs_get_bookmark_folder_object(NULL);
if (!bookmarks_object) return;
val = OBJECT_TO_JSVAL(bookmarks_object);
JS::RootedValue rval(smjs_ctx, val);
rval.setObject(*bookmarks_object);
JS::RootedObject r_smjs_elinks_object(smjs_ctx, smjs_elinks_object);
JS_SetProperty(smjs_ctx, smjs_elinks_object, "bookmarks", &val);
JS_SetProperty(smjs_ctx, r_smjs_elinks_object, "bookmarks", rval);
}

View File

@ -37,45 +37,46 @@ enum cache_entry_prop {
CACHE_ENTRY_URI = -5,
};
static JSBool cache_entry_get_property_content(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp);
static JSBool cache_entry_set_property_content(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JSBool strict, JS::MutableHandleValue hvp);
static JSBool cache_entry_get_property_type(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp);
static JSBool cache_entry_set_property_type(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JSBool strict, JS::MutableHandleValue hvp);
static JSBool cache_entry_get_property_length(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp);
static JSBool cache_entry_get_property_head(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp);
static JSBool cache_entry_set_property_head(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JSBool strict, JS::MutableHandleValue hvp);
static JSBool cache_entry_get_property_uri(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp);
static bool cache_entry_get_property_content(JSContext *ctx, unsigned int argc, jsval *vp);
static bool cache_entry_set_property_content(JSContext *ctx, unsigned int argc, jsval *vp);
static bool cache_entry_get_property_type(JSContext *ctx, unsigned int argc, jsval *vp);
static bool cache_entry_set_property_type(JSContext *ctx, unsigned int argc, jsval *vp);
static bool cache_entry_get_property_length(JSContext *ctx, unsigned int argc, jsval *vp);
static bool cache_entry_get_property_head(JSContext *ctx, unsigned int argc, jsval *vp);
static bool cache_entry_set_property_head(JSContext *ctx, unsigned int argc, jsval *vp);
static bool cache_entry_get_property_uri(JSContext *ctx, unsigned int argc, jsval *vp);
static const JSPropertySpec cache_entry_props[] = {
{ "content", 0, JSPROP_ENUMERATE, JSOP_WRAPPER(cache_entry_get_property_content), JSOP_WRAPPER(cache_entry_set_property_content) },
{ "type", 0, JSPROP_ENUMERATE, JSOP_WRAPPER(cache_entry_get_property_type), JSOP_WRAPPER(cache_entry_set_property_type)},
{ "length", 0, JSPROP_ENUMERATE | JSPROP_READONLY, JSOP_WRAPPER(cache_entry_get_property_length), JSOP_NULLWRAPPER },
{ "head", 0, JSPROP_ENUMERATE, JSOP_WRAPPER(cache_entry_get_property_head), JSOP_WRAPPER(cache_entry_set_property_head) },
{ "uri", 0, JSPROP_ENUMERATE | JSPROP_READONLY, JSOP_WRAPPER(cache_entry_get_property_uri), JSOP_NULLWRAPPER },
JS_PSGS("content", cache_entry_get_property_content, cache_entry_set_property_content, JSPROP_ENUMERATE),
JS_PSGS("type", cache_entry_get_property_type, cache_entry_set_property_type, JSPROP_ENUMERATE),
JS_PSG("length", cache_entry_get_property_length, JSPROP_ENUMERATE),
JS_PSGS("head", cache_entry_get_property_head, cache_entry_set_property_head, JSPROP_ENUMERATE),
JS_PSG("uri", cache_entry_get_property_uri, JSPROP_ENUMERATE),
{ NULL }
};
static JSBool
cache_entry_get_property_content(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp)
static bool
cache_entry_get_property_content(JSContext *ctx, unsigned int argc, jsval *vp)
{
ELINKS_CAST_PROP_PARAMS
JS::CallArgs args = CallArgsFromVp(argc, vp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
struct cache_entry *cached;
struct fragment *fragment;
JSBool ret;
bool ret;
/* This can be called if @obj if not itself an instance of the
* appropriate class but has one in its prototype chain. Fail
* such calls. */
if (!JS_InstanceOf(ctx, obj, (JSClass *) &cache_entry_class, NULL))
return JS_FALSE;
if (!JS_InstanceOf(ctx, hobj, (JSClass *) &cache_entry_class, NULL))
return false;
cached = JS_GetInstancePrivate(ctx, obj,
cached = JS_GetInstancePrivate(ctx, hobj,
(JSClass *) &cache_entry_class, NULL);
if (!cached) return JS_FALSE; /* already detached */
if (!cached) return false; /* already detached */
assert(cache_entry_is_valid(cached));
if_assert_failed return JS_FALSE;
if_assert_failed return false;
/* Get a strong reference to the cache entry to prevent it
* from being deleted if some function called below decides to
@ -83,24 +84,26 @@ cache_entry_get_property_content(JSContext *ctx, JS::HandleObject hobj, JS::Hand
* eventually unlock the object. */
object_lock(cached);
undef_to_jsval(ctx, vp);
args.rval().setUndefined();
fragment = get_cache_fragment(cached);
if (!fragment) {
ret = JS_FALSE;
ret = false;
} else {
*vp = STRING_TO_JSVAL(JS_NewStringCopyN(smjs_ctx, fragment->data, fragment->length));
ret = JS_TRUE;
args.rval().setString(JS_NewStringCopyN(smjs_ctx, fragment->data, fragment->length));
ret = true;
}
object_unlock(cached);
return ret;
}
static JSBool
cache_entry_set_property_content(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JSBool strict, JS::MutableHandleValue hvp)
static bool
cache_entry_set_property_content(JSContext *ctx, unsigned int argc, jsval *vp)
{
ELINKS_CAST_PROP_PARAMS
JS::CallArgs args = CallArgsFromVp(argc, vp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
struct cache_entry *cached;
JSString *jsstr;
@ -110,15 +113,15 @@ cache_entry_set_property_content(JSContext *ctx, JS::HandleObject hobj, JS::Hand
/* This can be called if @obj if not itself an instance of the
* appropriate class but has one in its prototype chain. Fail
* such calls. */
if (!JS_InstanceOf(ctx, obj, (JSClass *) &cache_entry_class, NULL))
return JS_FALSE;
if (!JS_InstanceOf(ctx, hobj, (JSClass *) &cache_entry_class, NULL))
return false;
cached = JS_GetInstancePrivate(ctx, obj,
cached = JS_GetInstancePrivate(ctx, hobj,
(JSClass *) &cache_entry_class, NULL);
if (!cached) return JS_FALSE; /* already detached */
if (!cached) return false; /* already detached */
assert(cache_entry_is_valid(cached));
if_assert_failed return JS_FALSE;
if_assert_failed return false;
/* Get a strong reference to the cache entry to prevent it
* from being deleted if some function called below decides to
@ -126,51 +129,53 @@ cache_entry_set_property_content(JSContext *ctx, JS::HandleObject hobj, JS::Hand
* eventually unlock the object. */
object_lock(cached);
jsstr = JS_ValueToString(smjs_ctx, *vp);
jsstr = JS::ToString(smjs_ctx, args[0]);
str = JS_EncodeString(smjs_ctx, jsstr);
len = JS_GetStringLength(jsstr);
add_fragment(cached, 0, str, len);
normalize_cache_entry(cached, len);
object_unlock(cached);
return JS_TRUE;
return true;
}
static JSBool
cache_entry_get_property_type(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp)
static bool
cache_entry_get_property_type(JSContext *ctx, unsigned int argc, jsval *vp)
{
ELINKS_CAST_PROP_PARAMS
JS::CallArgs args = CallArgsFromVp(argc, vp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
struct cache_entry *cached;
/* This can be called if @obj if not itself an instance of the
* appropriate class but has one in its prototype chain. Fail
* such calls. */
if (!JS_InstanceOf(ctx, obj, (JSClass *) &cache_entry_class, NULL))
return JS_FALSE;
if (!JS_InstanceOf(ctx, hobj, (JSClass *) &cache_entry_class, NULL))
return false;
cached = JS_GetInstancePrivate(ctx, obj,
cached = JS_GetInstancePrivate(ctx, hobj,
(JSClass *) &cache_entry_class, NULL);
if (!cached) return JS_FALSE; /* already detached */
if (!cached) return false; /* already detached */
assert(cache_entry_is_valid(cached));
if_assert_failed return JS_FALSE;
if_assert_failed return false;
/* Get a strong reference to the cache entry to prevent it
* from being deleted if some function called below decides to
* collect garbage. After this, all code paths must
* eventually unlock the object. */
object_lock(cached);
*vp = STRING_TO_JSVAL(JS_NewStringCopyZ(smjs_ctx, cached->content_type));
args.rval().setString(JS_NewStringCopyZ(smjs_ctx, cached->content_type));
object_unlock(cached);
return JS_TRUE;
return true;
}
static JSBool
cache_entry_set_property_type(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JSBool strict, JS::MutableHandleValue hvp)
static bool
cache_entry_set_property_type(JSContext *ctx, unsigned int argc, jsval *vp)
{
ELINKS_CAST_PROP_PARAMS
JS::CallArgs args = CallArgsFromVp(argc, vp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
struct cache_entry *cached;
JSString *jsstr;
@ -179,15 +184,15 @@ cache_entry_set_property_type(JSContext *ctx, JS::HandleObject hobj, JS::HandleI
/* This can be called if @obj if not itself an instance of the
* appropriate class but has one in its prototype chain. Fail
* such calls. */
if (!JS_InstanceOf(ctx, obj, (JSClass *) &cache_entry_class, NULL))
return JS_FALSE;
if (!JS_InstanceOf(ctx, hobj, (JSClass *) &cache_entry_class, NULL))
return false;
cached = JS_GetInstancePrivate(ctx, obj,
cached = JS_GetInstancePrivate(ctx, hobj,
(JSClass *) &cache_entry_class, NULL);
if (!cached) return JS_FALSE; /* already detached */
if (!cached) return false; /* already detached */
assert(cache_entry_is_valid(cached));
if_assert_failed return JS_FALSE;
if_assert_failed return false;
/* Get a strong reference to the cache entry to prevent it
* from being deleted if some function called below decides to
@ -195,13 +200,13 @@ cache_entry_set_property_type(JSContext *ctx, JS::HandleObject hobj, JS::HandleI
* eventually unlock the object. */
object_lock(cached);
jsstr = JS_ValueToString(smjs_ctx, *vp);
jsstr = JS::ToString(smjs_ctx, args[0]);
str = JS_EncodeString(smjs_ctx, jsstr);
mem_free_set(&cached->content_type, stracpy(str));
object_unlock(cached);
return JS_TRUE;
return true;
}
@ -249,11 +254,13 @@ smjs_get_cache_entry_object(struct cache_entry *cached)
cache_entry_object = JS_NewObject(smjs_ctx,
(JSClass *) &cache_entry_class,
NULL, NULL);
JS::NullPtr(), JS::NullPtr());
if (!cache_entry_object) return NULL;
if (JS_FALSE == JS_DefineProperties(smjs_ctx, cache_entry_object,
JS::RootedObject r_cache_entry_object(smjs_ctx, cache_entry_object);
if (false == JS_DefineProperties(smjs_ctx, r_cache_entry_object,
(JSPropertySpec *) cache_entry_props))
return NULL;
@ -279,7 +286,9 @@ smjs_detach_cache_entry_object(struct cache_entry *cached)
if (!cached->jsobject) return;
assert(JS_GetInstancePrivate(smjs_ctx, cached->jsobject,
JS::RootedObject r_jsobject(smjs_ctx, cached->jsobject);
assert(JS_GetInstancePrivate(smjs_ctx, r_jsobject,
(JSClass *) &cache_entry_class, NULL)
== cached);
if_assert_failed {}
@ -288,72 +297,75 @@ smjs_detach_cache_entry_object(struct cache_entry *cached)
cached->jsobject = NULL;
}
static JSBool
cache_entry_get_property_length(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp)
static bool
cache_entry_get_property_length(JSContext *ctx, unsigned int argc, jsval *vp)
{
ELINKS_CAST_PROP_PARAMS
JS::CallArgs args = CallArgsFromVp(argc, vp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
struct cache_entry *cached;
/* This can be called if @obj if not itself an instance of the
* appropriate class but has one in its prototype chain. Fail
* such calls. */
if (!JS_InstanceOf(ctx, obj, (JSClass *) &cache_entry_class, NULL))
return JS_FALSE;
if (!JS_InstanceOf(ctx, hobj, (JSClass *) &cache_entry_class, NULL))
return false;
cached = JS_GetInstancePrivate(ctx, obj,
cached = JS_GetInstancePrivate(ctx, hobj,
(JSClass *) &cache_entry_class, NULL);
if (!cached) return JS_FALSE; /* already detached */
if (!cached) return false; /* already detached */
assert(cache_entry_is_valid(cached));
if_assert_failed return JS_FALSE;
if_assert_failed return false;
/* Get a strong reference to the cache entry to prevent it
* from being deleted if some function called below decides to
* collect garbage. After this, all code paths must
* eventually unlock the object. */
object_lock(cached);
*vp = INT_TO_JSVAL(cached->length);
args.rval().setInt32(cached->length);
object_unlock(cached);
return JS_TRUE;
return true;
}
static JSBool
cache_entry_get_property_head(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp)
static bool
cache_entry_get_property_head(JSContext *ctx, unsigned int argc, jsval *vp)
{
ELINKS_CAST_PROP_PARAMS
JS::CallArgs args = CallArgsFromVp(argc, vp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
struct cache_entry *cached;
/* This can be called if @obj if not itself an instance of the
* appropriate class but has one in its prototype chain. Fail
* such calls. */
if (!JS_InstanceOf(ctx, obj, (JSClass *) &cache_entry_class, NULL))
return JS_FALSE;
if (!JS_InstanceOf(ctx, hobj, (JSClass *) &cache_entry_class, NULL))
return false;
cached = JS_GetInstancePrivate(ctx, obj,
cached = JS_GetInstancePrivate(ctx, hobj,
(JSClass *) &cache_entry_class, NULL);
if (!cached) return JS_FALSE; /* already detached */
if (!cached) return false; /* already detached */
assert(cache_entry_is_valid(cached));
if_assert_failed return JS_FALSE;
if_assert_failed return false;
/* Get a strong reference to the cache entry to prevent it
* from being deleted if some function called below decides to
* collect garbage. After this, all code paths must
* eventually unlock the object. */
object_lock(cached);
*vp = STRING_TO_JSVAL(JS_NewStringCopyZ(smjs_ctx, cached->head));
args.rval().setString(JS_NewStringCopyZ(smjs_ctx, cached->head));
object_unlock(cached);
return JS_TRUE;
return true;
}
static JSBool
cache_entry_set_property_head(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JSBool strict, JS::MutableHandleValue hvp)
static bool
cache_entry_set_property_head(JSContext *ctx, unsigned int argc, jsval *vp)
{
ELINKS_CAST_PROP_PARAMS
JS::CallArgs args = CallArgsFromVp(argc, vp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
struct cache_entry *cached;
JSString *jsstr;
@ -362,15 +374,15 @@ cache_entry_set_property_head(JSContext *ctx, JS::HandleObject hobj, JS::HandleI
/* This can be called if @obj if not itself an instance of the
* appropriate class but has one in its prototype chain. Fail
* such calls. */
if (!JS_InstanceOf(ctx, obj, (JSClass *) &cache_entry_class, NULL))
return JS_FALSE;
if (!JS_InstanceOf(ctx, hobj, (JSClass *) &cache_entry_class, NULL))
return false;
cached = JS_GetInstancePrivate(ctx, obj,
cached = JS_GetInstancePrivate(ctx, hobj,
(JSClass *) &cache_entry_class, NULL);
if (!cached) return JS_FALSE; /* already detached */
if (!cached) return false; /* already detached */
assert(cache_entry_is_valid(cached));
if_assert_failed return JS_FALSE;
if_assert_failed return false;
/* Get a strong reference to the cache entry to prevent it
* from being deleted if some function called below decides to
@ -378,42 +390,43 @@ cache_entry_set_property_head(JSContext *ctx, JS::HandleObject hobj, JS::HandleI
* eventually unlock the object. */
object_lock(cached);
jsstr = JS_ValueToString(smjs_ctx, *vp);
jsstr = JS::ToString(smjs_ctx, args[0]);
str = JS_EncodeString(smjs_ctx, jsstr);
mem_free_set(&cached->head, stracpy(str));
object_unlock(cached);
return JS_TRUE;
return true;
}
static JSBool
cache_entry_get_property_uri(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp)
static bool
cache_entry_get_property_uri(JSContext *ctx, unsigned int argc, jsval *vp)
{
ELINKS_CAST_PROP_PARAMS
JS::CallArgs args = CallArgsFromVp(argc, vp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
struct cache_entry *cached;
/* This can be called if @obj if not itself an instance of the
* appropriate class but has one in its prototype chain. Fail
* such calls. */
if (!JS_InstanceOf(ctx, obj, (JSClass *) &cache_entry_class, NULL))
return JS_FALSE;
if (!JS_InstanceOf(ctx, hobj, (JSClass *) &cache_entry_class, NULL))
return false;
cached = JS_GetInstancePrivate(ctx, obj,
cached = JS_GetInstancePrivate(ctx, hobj,
(JSClass *) &cache_entry_class, NULL);
if (!cached) return JS_FALSE; /* already detached */
if (!cached) return false; /* already detached */
assert(cache_entry_is_valid(cached));
if_assert_failed return JS_FALSE;
if_assert_failed return false;
/* Get a strong reference to the cache entry to prevent it
* from being deleted if some function called below decides to
* collect garbage. After this, all code paths must
* eventually unlock the object. */
object_lock(cached);
*vp = STRING_TO_JSVAL(JS_NewStringCopyZ(smjs_ctx, struri(cached->uri)));
args.rval().setString(JS_NewStringCopyZ(smjs_ctx, struri(cached->uri)));
object_unlock(cached);
return JS_TRUE;
return true;
}

View File

@ -75,14 +75,16 @@ static int
smjs_do_file(unsigned char *path)
{
int ret = 1;
jsval rval;
struct string script;
if (!init_string(&script)) return 0;
jsval val;
JS::RootedValue rval(smjs_ctx, val);
JS::RootedObject cg(smjs_ctx, JS::CurrentGlobalOrNull(smjs_ctx));
if (!add_file_to_string(&script, path)
|| JS_FALSE == JS_EvaluateScript(smjs_ctx,
JS_GetGlobalForScopeChain(smjs_ctx),
|| false == JS_EvaluateScript(smjs_ctx, cg,
script.source, script.length, path, 1, &rval)) {
alert_smjs_error("error loading script file");
ret = 0;
@ -93,17 +95,18 @@ smjs_do_file(unsigned char *path)
return ret;
}
static JSBool
static bool
smjs_do_file_wrapper(JSContext *ctx, unsigned int argc, jsval *rval)
{
jsval *argv = JS_ARGV(ctx, rval);
JSString *jsstr = JS_ValueToString(smjs_ctx, *argv);
JS::CallArgs args = CallArgsFromVp(argc, rval);
JSString *jsstr = JS::ToString(smjs_ctx, args[0]);
unsigned char *path = JS_EncodeString(smjs_ctx, jsstr);
if (smjs_do_file(path))
return JS_TRUE;
return true;
return JS_FALSE;
return false;
}
static void
@ -136,15 +139,15 @@ init_smjs(struct module *module)
return;
}
JS_SetOptions(smjs_ctx, JSOPTION_VAROBJFIX | JS_METHODJIT);
JS_SetErrorReporter(smjs_ctx, error_reporter);
smjs_init_global_object();
smjs_init_elinks_object();
JS_DefineFunction(smjs_ctx, smjs_global_object, "do_file",
JS::RootedObject r_smjs_global_object(smjs_ctx, smjs_global_object);
JS_DefineFunction(smjs_ctx, r_smjs_global_object, "do_file",
&smjs_do_file_wrapper, 1, 0);
smjs_load_hooks();

View File

@ -32,20 +32,21 @@
/* @elinks_funcs{"alert"} */
static JSBool
static bool
elinks_alert(JSContext *ctx, unsigned int argc, jsval *rval)
{
jsval val;
jsval *argv = JS_ARGV(ctx, rval);
JS::CallArgs args = CallArgsFromVp(argc, rval);
unsigned char *string;
struct terminal *term;
if (argc != 1)
return JS_TRUE;
return true;
string = JS_EncodeString(ctx, args[0].toString());
string = jsval_to_string(ctx, &argv[0]);
if (!*string)
return JS_TRUE;
return true;
if (smjs_ses) {
term = smjs_ses->tab->term;
@ -61,32 +62,30 @@ elinks_alert(JSContext *ctx, unsigned int argc, jsval *rval)
sleep(3);
}
undef_to_jsval(ctx, &val);
JS_SET_RVAL(ctx, rval, val);
args.rval().setUndefined();
return JS_TRUE;
return true;
}
/* @elinks_funcs{"execute"} */
static JSBool
static bool
elinks_execute(JSContext *ctx, unsigned int argc, jsval *rval)
{
jsval val;
jsval *argv = JS_ARGV(ctx, rval);
JS::CallArgs args = CallArgsFromVp(argc, rval);
unsigned char *string;
if (argc != 1)
return JS_TRUE;
return true;
string = jsval_to_string(ctx, &argv[0]);
string = JS_EncodeString(ctx, args[0].toString());
if (!*string)
return JS_TRUE;
return true;
exec_on_terminal(smjs_ses->tab->term, string, "", TERM_EXEC_BG);
undef_to_jsval(ctx, &val);
JS_SET_RVAL(ctx, rval, val);
return JS_TRUE;
args.rval().setUndefined();
return true;
}
enum elinks_prop {
@ -95,20 +94,19 @@ enum elinks_prop {
ELINKS_SESSION,
};
static JSBool elinks_get_property_home(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp);
static JSBool elinks_get_property_location(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp);
static JSBool elinks_set_property_location(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JSBool strict, JS::MutableHandleValue hvp);
static JSBool elinks_get_property_session(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp);
static bool elinks_get_property_home(JSContext *ctx, unsigned int argc, jsval *vp);
static bool elinks_get_property_location(JSContext *ctx, unsigned int argc, jsval *vp);
static bool elinks_set_property_location(JSContext *ctx, unsigned int argc, jsval *vp);
static bool elinks_get_property_session(JSContext *ctx, unsigned int argc, jsval *vp);
static const JSPropertySpec elinks_props[] = {
{ "home", 0, JSPROP_ENUMERATE | JSPROP_PERMANENT | JSPROP_READONLY, JSOP_WRAPPER(elinks_get_property_home), JSOP_NULLWRAPPER },
{ "location", 0, JSPROP_ENUMERATE | JSPROP_PERMANENT, JSOP_WRAPPER(elinks_get_property_location), JSOP_WRAPPER(elinks_set_property_location) },
{ "session", 0, JSPROP_ENUMERATE | JSPROP_PERMANENT | JSPROP_READONLY, JSOP_WRAPPER(elinks_get_property_session), JSOP_NULLWRAPPER},
JS_PSG("home", elinks_get_property_home, JSPROP_ENUMERATE),
JS_PSGS("location", elinks_get_property_location, elinks_set_property_location, JSPROP_ENUMERATE),
JS_PSG("session", elinks_get_property_session, JSPROP_ENUMERATE),
{ NULL }
};
static JSBool elinks_get_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp);
static JSBool elinks_set_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JSBool strict, JS::MutableHandleValue hvp);
static bool elinks_get_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp);
static bool elinks_set_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, bool strict, JS::MutableHandleValue hvp);
static const JSClass elinks_class = {
"elinks",
@ -120,80 +118,78 @@ static const JSClass elinks_class = {
/* @elinks_class.getProperty */
static JSBool
static bool
elinks_get_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp)
{
ELINKS_CAST_PROP_PARAMS
jsid id = hid.get();
/* This can be called if @obj if not itself an instance of the
* appropriate class but has one in its prototype chain. Fail
* such calls. */
if (!JS_InstanceOf(ctx, obj, (JSClass *) &elinks_class, NULL))
return JS_FALSE;
if (!JS_InstanceOf(ctx, hobj, (JSClass *) &elinks_class, NULL))
return false;
if (!JSID_IS_INT(id)) {
/* Note: If we return JS_FALSE here, the object's methods and
/* Note: If we return false here, the object's methods and
* user-added properties do not work. */
return JS_TRUE;
return true;
}
undef_to_jsval(ctx, vp);
hvp.setUndefined();
switch (JSID_TO_INT(id)) {
case ELINKS_HOME:
*vp = STRING_TO_JSVAL(JS_NewStringCopyZ(smjs_ctx, elinks_home));
hvp.setString(JS_NewStringCopyZ(smjs_ctx, elinks_home));
return JS_TRUE;
return true;
case ELINKS_LOCATION: {
struct uri *uri;
if (!smjs_ses) return JS_FALSE;
if (!smjs_ses) return false;
uri = have_location(smjs_ses) ? cur_loc(smjs_ses)->vs.uri
: smjs_ses->loading_uri;
*vp = STRING_TO_JSVAL(JS_NewStringCopyZ(smjs_ctx,
hvp.setString(JS_NewStringCopyZ(smjs_ctx,
uri ? (const char *) struri(uri) : ""));
return JS_TRUE;
return true;
}
case ELINKS_SESSION: {
JSObject *jsobj;
if (!smjs_ses) return JS_FALSE;
if (!smjs_ses) return false;
jsobj = smjs_get_session_object(smjs_ses);
if (!jsobj) return JS_FALSE;
if (!jsobj) return false;
object_to_jsval(ctx, vp, jsobj);
hvp.setObject(*jsobj);
return JS_TRUE;
return true;
}
default:
INTERNAL("Invalid ID %d in elinks_get_property().",
JSID_TO_INT(id));
}
return JS_FALSE;
return false;
}
static JSBool
elinks_set_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JSBool strict, JS::MutableHandleValue hvp)
static bool
elinks_set_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, bool strict, JS::MutableHandleValue hvp)
{
ELINKS_CAST_PROP_PARAMS
jsid id = hid.get();
/* This can be called if @obj if not itself an instance of the
* appropriate class but has one in its prototype chain. Fail
* such calls. */
if (!JS_InstanceOf(ctx, obj, (JSClass *) &elinks_class, NULL))
return JS_FALSE;
if (!JS_InstanceOf(ctx, hobj, (JSClass *) &elinks_class, NULL))
return false;
if (!JSID_IS_INT(id)) {
/* Note: If we return JS_FALSE here, the object's methods and
/* Note: If we return false here, the object's methods and
* user-added properties do not work. */
return JS_TRUE;
return true;
}
switch (JSID_TO_INT(id)) {
@ -201,24 +197,24 @@ elinks_set_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JSB
JSString *jsstr;
unsigned char *url;
if (!smjs_ses) return JS_FALSE;
if (!smjs_ses) return false;
jsstr = JS_ValueToString(smjs_ctx, *vp);
if (!jsstr) return JS_FALSE;
jsstr = JS::ToString(smjs_ctx, hvp);
if (!jsstr) return false;
url = JS_EncodeString(smjs_ctx, jsstr);
if (!url) return JS_FALSE;
if (!url) return false;
goto_url(smjs_ses, url);
return JS_TRUE;
return true;
}
default:
INTERNAL("Invalid ID %d in elinks_set_property().",
JSID_TO_INT(id));
}
return JS_FALSE;
return false;
}
@ -256,107 +252,117 @@ smjs_init_elinks_object(void)
}
/* If elinks.<method> is defined, call it with the given arguments,
* store the return value in rval, and return JS_TRUE. Else return JS_FALSE. */
JSBool
smjs_invoke_elinks_object_method(unsigned char *method, jsval argv[], int argc,
jsval *rval)
* store the return value in rval, and return true. Else return false. */
bool
smjs_invoke_elinks_object_method(unsigned char *method, int argc, jsval *argv, JS::MutableHandleValue rval)
{
JS::CallArgs args = CallArgsFromVp(argc, argv);
assert(smjs_ctx);
assert(smjs_elinks_object);
assert(rval);
assert(argv);
if (JS_FALSE == JS_GetProperty(smjs_ctx, smjs_elinks_object,
method, rval))
return JS_FALSE;
JS::RootedObject r_smjs_elinks_object(smjs_ctx, smjs_elinks_object);
jsval val;
JS::RootedValue fun(smjs_ctx, val);
if (JSVAL_IS_VOID(*rval))
return JS_FALSE;
if (false == JS_GetProperty(smjs_ctx, r_smjs_elinks_object,
method, &fun))
return false;
return JS_CallFunctionValue(smjs_ctx, smjs_elinks_object,
*rval, argc, argv, rval);
if (rval.isUndefined())
return false;
return JS_CallFunctionValue(smjs_ctx, r_smjs_elinks_object, fun, args, rval);
}
static JSBool
elinks_get_property_home(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp)
static bool
elinks_get_property_home(JSContext *ctx, unsigned int argc, jsval *vp)
{
ELINKS_CAST_PROP_PARAMS
JS::CallArgs args = CallArgsFromVp(argc, vp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
/* This can be called if @obj if not itself an instance of the
* appropriate class but has one in its prototype chain. Fail
* such calls. */
if (!JS_InstanceOf(ctx, obj, (JSClass *) &elinks_class, NULL))
return JS_FALSE;
if (!JS_InstanceOf(ctx, hobj, (JSClass *) &elinks_class, NULL))
return false;
*vp = STRING_TO_JSVAL(JS_NewStringCopyZ(smjs_ctx, elinks_home));
args.rval().setString(JS_NewStringCopyZ(smjs_ctx, elinks_home));
return JS_TRUE;
return true;
}
static JSBool
elinks_get_property_location(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp)
static bool
elinks_get_property_location(JSContext *ctx, unsigned int argc, jsval *vp)
{
ELINKS_CAST_PROP_PARAMS
JS::CallArgs args = CallArgsFromVp(argc, vp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
struct uri *uri;
/* This can be called if @obj if not itself an instance of the
* appropriate class but has one in its prototype chain. Fail
* such calls. */
if (!JS_InstanceOf(ctx, obj, (JSClass *) &elinks_class, NULL))
return JS_FALSE;
if (!JS_InstanceOf(ctx, hobj, (JSClass *) &elinks_class, NULL))
return false;
if (!smjs_ses) return JS_FALSE;
if (!smjs_ses) return false;
uri = have_location(smjs_ses) ? cur_loc(smjs_ses)->vs.uri : smjs_ses->loading_uri;
*vp = STRING_TO_JSVAL(JS_NewStringCopyZ(smjs_ctx, uri ? (const char *) struri(uri) : ""));
args.rval().setString(JS_NewStringCopyZ(smjs_ctx, uri ? (const char *) struri(uri) : ""));
return JS_TRUE;
return true;
}
static JSBool
elinks_set_property_location(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JSBool strict, JS::MutableHandleValue hvp)
static bool
elinks_set_property_location(JSContext *ctx, unsigned int argc, jsval *vp)
{
ELINKS_CAST_PROP_PARAMS
JS::CallArgs args = CallArgsFromVp(argc, vp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
JSString *jsstr;
unsigned char *url;
/* This can be called if @obj if not itself an instance of the
* appropriate class but has one in its prototype chain. Fail
* such calls. */
if (!JS_InstanceOf(ctx, obj, (JSClass *) &elinks_class, NULL))
return JS_FALSE;
if (!JS_InstanceOf(ctx, hobj, (JSClass *) &elinks_class, NULL))
return false;
if (!smjs_ses) return JS_FALSE;
if (!smjs_ses) return false;
jsstr = JS_ValueToString(smjs_ctx, *vp);
if (!jsstr) return JS_FALSE;
jsstr = JS::ToString(smjs_ctx, args[0]);
if (!jsstr) return false;
url = JS_EncodeString(smjs_ctx, jsstr);
if (!url) return JS_FALSE;
if (!url) return false;
goto_url(smjs_ses, url);
return JS_TRUE;
return true;
}
static JSBool
elinks_get_property_session(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp)
static bool
elinks_get_property_session(JSContext *ctx, unsigned int argc, jsval *vp)
{
ELINKS_CAST_PROP_PARAMS
JS::CallArgs args = CallArgsFromVp(argc, vp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
JSObject *jsobj;
/* This can be called if @obj if not itself an instance of the
* appropriate class but has one in its prototype chain. Fail
* such calls. */
if (!JS_InstanceOf(ctx, obj, (JSClass *) &elinks_class, NULL))
return JS_FALSE;
if (!JS_InstanceOf(ctx, hobj, (JSClass *) &elinks_class, NULL))
return false;
if (!smjs_ses) return JS_FALSE;
if (!smjs_ses) return false;
jsobj = smjs_get_session_object(smjs_ses);
if (!jsobj) return JS_FALSE;
if (!jsobj) return false;
object_to_jsval(ctx, vp, jsobj);
args.rval().setObject(*jsobj);;
return JS_TRUE;
return true;
}

View File

@ -12,7 +12,6 @@ void smjs_init_elinks_object(void);
/* Invoke elinks.<method> with the given arguments and put the return value
* into *rval. */
JSBool smjs_invoke_elinks_object_method(unsigned char *method,
jsval argv[], int argc, jsval *rval);
bool smjs_invoke_elinks_object_method(unsigned char *method, int argc, jsval *argv, JS::MutableHandleValue rval);
#endif

View File

@ -32,7 +32,7 @@ smjs_get_global_object(void)
JSAutoRequest ar(smjs_ctx);
RootedObject jsobj(smjs_ctx);
jsobj = JS_NewGlobalObject(smjs_ctx, (JSClass *) &global_class, NULL);
jsobj = JS_NewGlobalObject(smjs_ctx, (JSClass *) &global_class, NULL, JS::DontFireOnNewGlobalHook);
if (!jsobj) return NULL;

View File

@ -12,8 +12,8 @@
#include "scripting/smjs/elinks_object.h"
#include "util/memory.h"
static JSBool smjs_globhist_item_get_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp);
static JSBool smjs_globhist_item_set_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JSBool strict, JS::MutableHandleValue hvp);
static bool smjs_globhist_item_get_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp);
static bool smjs_globhist_item_set_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, bool strict, JS::MutableHandleValue hvp);
static void smjs_globhist_item_finalize(JSFreeOp *op, JSObject *obj);
@ -51,26 +51,25 @@ enum smjs_globhist_item_prop {
GLOBHIST_LAST_VISIT = -3,
};
static JSBool smjs_globhist_item_get_property_title(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp);
static JSBool smjs_globhist_item_set_property_title(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JSBool strict, JS::MutableHandleValue hvp);
static JSBool smjs_globhist_item_get_property_url(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp);
static JSBool smjs_globhist_item_set_property_url(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JSBool strict, JS::MutableHandleValue hvp);
static JSBool smjs_globhist_item_get_property_last_visit(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp);
static JSBool smjs_globhist_item_set_property_last_visit(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JSBool strict, JS::MutableHandleValue hvp);
static bool smjs_globhist_item_get_property_title(JSContext *ctx, unsigned int argc, jsval *vp);
static bool smjs_globhist_item_set_property_title(JSContext *ctx, unsigned int argc, jsval *vp);
static bool smjs_globhist_item_get_property_url(JSContext *ctx, unsigned int argc, jsval *vp);
static bool smjs_globhist_item_set_property_url(JSContext *ctx, unsigned int argc, jsval *vp);
static bool smjs_globhist_item_get_property_last_visit(JSContext *ctx, unsigned int argc, jsval *vp);
static bool smjs_globhist_item_set_property_last_visit(JSContext *ctx, unsigned int argc, jsval *vp);
static const JSPropertySpec smjs_globhist_item_props[] = {
{ "title", 0, JSPROP_ENUMERATE, JSOP_WRAPPER(smjs_globhist_item_get_property_title), JSOP_WRAPPER(smjs_globhist_item_set_property_title) },
{ "url", 0, JSPROP_ENUMERATE, JSOP_WRAPPER(smjs_globhist_item_get_property_url), JSOP_WRAPPER(smjs_globhist_item_set_property_url) },
{ "last_visit", 0, JSPROP_ENUMERATE, JSOP_WRAPPER(smjs_globhist_item_get_property_last_visit), JSOP_WRAPPER(smjs_globhist_item_set_property_last_visit) },
JS_PSGS("title", smjs_globhist_item_get_property_title, smjs_globhist_item_set_property_title, JSPROP_ENUMERATE),
JS_PSGS("url", smjs_globhist_item_get_property_url, smjs_globhist_item_set_property_url, JSPROP_ENUMERATE),
JS_PSGS("last_visit", smjs_globhist_item_get_property_last_visit, smjs_globhist_item_set_property_last_visit, JSPROP_ENUMERATE),
{ NULL }
};
/* @smjs_globhist_item_class.getProperty */
static JSBool
static bool
smjs_globhist_item_get_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid,
JS::MutableHandleValue hvp)
{
ELINKS_CAST_PROP_PARAMS
jsid id = hid.get();
struct global_history_item *history_item;
@ -78,31 +77,29 @@ smjs_globhist_item_get_property(JSContext *ctx, JS::HandleObject hobj, JS::Handl
/* This can be called if @obj if not itself an instance of the
* appropriate class but has one in its prototype chain. Fail
* such calls. */
if (!JS_InstanceOf(ctx, obj, (JSClass *) &smjs_globhist_item_class, NULL))
return JS_FALSE;
if (!JS_InstanceOf(ctx, hobj, (JSClass *) &smjs_globhist_item_class, NULL))
return false;
history_item = JS_GetInstancePrivate(ctx, obj,
history_item = JS_GetInstancePrivate(ctx, hobj,
(JSClass *) &smjs_globhist_item_class,
NULL);
if (!history_item) return JS_FALSE;
if (!history_item) return false;
undef_to_jsval(ctx, vp);
hvp.setUndefined();
if (!JSID_IS_INT(id))
return JS_FALSE;
return false;
switch (JSID_TO_INT(id)) {
case GLOBHIST_TITLE:
*vp = STRING_TO_JSVAL(JS_NewStringCopyZ(smjs_ctx,
history_item->title));
hvp.setString(JS_NewStringCopyZ(smjs_ctx, history_item->title));
return JS_TRUE;
return true;
case GLOBHIST_URL:
*vp = STRING_TO_JSVAL(JS_NewStringCopyZ(smjs_ctx,
history_item->url));
hvp.setString(JS_NewStringCopyZ(smjs_ctx, history_item->url));
return JS_TRUE;
return true;
case GLOBHIST_LAST_VISIT:
/* TODO: I'd rather return a date object, but that introduces
* synchronisation issues:
@ -120,25 +117,24 @@ smjs_globhist_item_get_property(JSContext *ctx, JS::HandleObject hobj, JS::Handl
* Since the Date object uses milliseconds since the epoch,
* I'd rather export that, but SpiderMonkey doesn't provide
* a suitable type. -- Miciah */
*vp = JS_NumberValue(history_item->last_visit);
hvp.setInt32(history_item->last_visit);
return JS_TRUE;
return true;
default:
/* Unrecognized integer property ID; someone is using
* the object as an array. SMJS builtin classes (e.g.
* js_RegExpClass) just return JS_TRUE in this case
* js_RegExpClass) just return true in this case
* and leave *@vp unchanged. Do the same here.
* (Actually not quite the same, as we already used
* @undef_to_jsval.) */
return JS_TRUE;
return true;
}
}
/* @smjs_globhist_item_class.setProperty */
static JSBool
smjs_globhist_item_set_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JSBool strict, JS::MutableHandleValue hvp)
static bool
smjs_globhist_item_set_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, bool strict, JS::MutableHandleValue hvp)
{
ELINKS_CAST_PROP_PARAMS
jsid id = hid.get();
struct global_history_item *history_item;
@ -146,50 +142,50 @@ smjs_globhist_item_set_property(JSContext *ctx, JS::HandleObject hobj, JS::Handl
/* This can be called if @obj if not itself an instance of the
* appropriate class but has one in its prototype chain. Fail
* such calls. */
if (!JS_InstanceOf(ctx, obj, (JSClass *) &smjs_globhist_item_class, NULL))
return JS_FALSE;
if (!JS_InstanceOf(ctx, hobj, (JSClass *) &smjs_globhist_item_class, NULL))
return false;
history_item = JS_GetInstancePrivate(ctx, obj,
history_item = JS_GetInstancePrivate(ctx, hobj,
(JSClass *) &smjs_globhist_item_class,
NULL);
if (!history_item) return JS_FALSE;
if (!history_item) return false;
if (!JSID_IS_INT(id))
return JS_FALSE;
return false;
switch (JSID_TO_INT(id)) {
case GLOBHIST_TITLE: {
JSString *jsstr = JS_ValueToString(smjs_ctx, *vp);
JSString *jsstr = JS::ToString(smjs_ctx, hvp);
unsigned char *str = JS_EncodeString(smjs_ctx, jsstr);
mem_free_set(&history_item->title, stracpy(str));
return JS_TRUE;
return true;
}
case GLOBHIST_URL: {
JSString *jsstr = JS_ValueToString(smjs_ctx, *vp);
JSString *jsstr = JS::ToString(smjs_ctx, hvp);
unsigned char *str = JS_EncodeString(smjs_ctx, jsstr);
mem_free_set(&history_item->url, stracpy(str));
return JS_TRUE;
return true;
}
case GLOBHIST_LAST_VISIT: {
uint32_t seconds;
/* Bug 923: Assumes time_t values fit in uint32. */
JS_ValueToECMAUint32(smjs_ctx, *vp, &seconds);
JS::ToInt32(smjs_ctx, hvp, &seconds);
history_item->last_visit = seconds;
return JS_TRUE;
return true;
}
default:
/* Unrecognized integer property ID; someone is using
* the object as an array. SMJS builtin classes (e.g.
* js_RegExpClass) just return JS_TRUE in this case.
* js_RegExpClass) just return true in this case.
* Do the same here. */
return JS_TRUE;
return true;
}
}
@ -200,9 +196,12 @@ smjs_get_globhist_item_object(struct global_history_item *history_item)
JSObject *jsobj;
jsobj = JS_NewObject(smjs_ctx, (JSClass *) &smjs_globhist_item_class,
NULL, NULL);
JS::NullPtr(), JS::NullPtr());
JS::RootedObject r_jsobj(smjs_ctx, jsobj);
if (!jsobj
|| JS_TRUE != JS_DefineProperties(smjs_ctx, jsobj,
|| true != JS_DefineProperties(smjs_ctx, r_jsobj,
(JSPropertySpec *) smjs_globhist_item_props)) {
return NULL;
}
@ -214,22 +213,22 @@ smjs_get_globhist_item_object(struct global_history_item *history_item)
/* @smjs_globhist_class.getProperty */
static JSBool
static bool
smjs_globhist_get_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp)
{
ELINKS_CAST_PROP_PARAMS
jsid id = hid.get();
(void)obj;
JSObject *jsobj;
unsigned char *uri_string;
struct global_history_item *history_item;
jsval tmp;
JS::RootedValue r_tmp(ctx, tmp);
if (!JS_IdToValue(ctx, id, &tmp))
if (!JS_IdToValue(ctx, id, &r_tmp))
goto ret_null;
uri_string = JS_EncodeString(ctx, JS_ValueToString(ctx, tmp));
uri_string = JS_EncodeString(ctx, JS::ToString(ctx, r_tmp));
if (!uri_string) goto ret_null;
history_item = get_global_history_item(uri_string);
@ -238,14 +237,14 @@ smjs_globhist_get_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId h
jsobj = smjs_get_globhist_item_object(history_item);
if (!jsobj) goto ret_null;
*vp = OBJECT_TO_JSVAL(jsobj);
hvp.setObject(*jsobj);
return JS_TRUE;
return true;
ret_null:
*vp = JSVAL_NULL;
hvp.setNull();
return JS_TRUE;
return true;
}
static const JSClass smjs_globhist_class = {
@ -261,7 +260,7 @@ smjs_get_globhist_object(void)
JSObject *globhist;
globhist = JS_NewObject(smjs_ctx, (JSClass *) &smjs_globhist_class,
NULL, NULL);
JS::NullPtr(), JS::NullPtr());
if (!globhist) return NULL;
return globhist;
@ -279,40 +278,43 @@ smjs_init_globhist_interface(void)
globhist = smjs_get_globhist_object();
if (!globhist) return;
val = OBJECT_TO_JSVAL(globhist);
JS::RootedObject r_smjs_elinks_object(smjs_ctx, smjs_elinks_object);
JS::RootedValue r_val(smjs_ctx, val);
r_val.setObject(*globhist);
JS_SetProperty(smjs_ctx, smjs_elinks_object, "globhist", &val);
JS_SetProperty(smjs_ctx, r_smjs_elinks_object, "globhist", r_val);
}
static JSBool
smjs_globhist_item_get_property_title(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid,
JS::MutableHandleValue hvp)
static bool
smjs_globhist_item_get_property_title(JSContext *ctx, unsigned int argc, jsval *vp)
{
ELINKS_CAST_PROP_PARAMS
JS::CallArgs args = CallArgsFromVp(argc, vp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
struct global_history_item *history_item;
/* This can be called if @obj if not itself an instance of the
* appropriate class but has one in its prototype chain. Fail
* such calls. */
if (!JS_InstanceOf(ctx, obj, (JSClass *) &smjs_globhist_item_class, NULL))
return JS_FALSE;
if (!JS_InstanceOf(ctx, hobj, (JSClass *) &smjs_globhist_item_class, NULL))
return false;
history_item = JS_GetInstancePrivate(ctx, obj,
history_item = JS_GetInstancePrivate(ctx, hobj,
(JSClass *) &smjs_globhist_item_class,
NULL);
if (!history_item) return JS_FALSE;
if (!history_item) return false;
*vp = STRING_TO_JSVAL(JS_NewStringCopyZ(smjs_ctx, history_item->title));
args.rval().setString(JS_NewStringCopyZ(smjs_ctx, history_item->title));
return JS_TRUE;
return true;
}
static JSBool
smjs_globhist_item_set_property_title(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JSBool strict, JS::MutableHandleValue hvp)
static bool
smjs_globhist_item_set_property_title(JSContext *ctx, unsigned int argc, jsval *vp)
{
ELINKS_CAST_PROP_PARAMS
JS::CallArgs args = CallArgsFromVp(argc, vp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
struct global_history_item *history_item;
JSString *jsstr;
@ -321,51 +323,52 @@ smjs_globhist_item_set_property_title(JSContext *ctx, JS::HandleObject hobj, JS:
/* This can be called if @obj if not itself an instance of the
* appropriate class but has one in its prototype chain. Fail
* such calls. */
if (!JS_InstanceOf(ctx, obj, (JSClass *) &smjs_globhist_item_class, NULL))
return JS_FALSE;
if (!JS_InstanceOf(ctx, hobj, (JSClass *) &smjs_globhist_item_class, NULL))
return false;
history_item = JS_GetInstancePrivate(ctx, obj,
history_item = JS_GetInstancePrivate(ctx, hobj,
(JSClass *) &smjs_globhist_item_class,
NULL);
if (!history_item) return JS_FALSE;
if (!history_item) return false;
jsstr = JS_ValueToString(smjs_ctx, *vp);
jsstr = JS::ToString(smjs_ctx, args[0]);
str = JS_EncodeString(smjs_ctx, jsstr);
mem_free_set(&history_item->title, stracpy(str));
return JS_TRUE;
return true;
}
static JSBool
smjs_globhist_item_get_property_url(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid,
JS::MutableHandleValue hvp)
static bool
smjs_globhist_item_get_property_url(JSContext *ctx, unsigned int argc, jsval *vp)
{
ELINKS_CAST_PROP_PARAMS
JS::CallArgs args = CallArgsFromVp(argc, vp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
struct global_history_item *history_item;
/* This can be called if @obj if not itself an instance of the
* appropriate class but has one in its prototype chain. Fail
* such calls. */
if (!JS_InstanceOf(ctx, obj, (JSClass *) &smjs_globhist_item_class, NULL))
return JS_FALSE;
if (!JS_InstanceOf(ctx, hobj, (JSClass *) &smjs_globhist_item_class, NULL))
return false;
history_item = JS_GetInstancePrivate(ctx, obj,
history_item = JS_GetInstancePrivate(ctx, hobj,
(JSClass *) &smjs_globhist_item_class,
NULL);
if (!history_item) return JS_FALSE;
if (!history_item) return false;
*vp = STRING_TO_JSVAL(JS_NewStringCopyZ(smjs_ctx, history_item->url));
args.rval().setString(JS_NewStringCopyZ(smjs_ctx, history_item->url));
return JS_TRUE;
return true;
}
static JSBool
smjs_globhist_item_set_property_url(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JSBool strict, JS::MutableHandleValue hvp)
static bool
smjs_globhist_item_set_property_url(JSContext *ctx, unsigned int argc, jsval *vp)
{
ELINKS_CAST_PROP_PARAMS
JS::CallArgs args = CallArgsFromVp(argc, vp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
struct global_history_item *history_item;
JSString *jsstr;
@ -374,41 +377,41 @@ smjs_globhist_item_set_property_url(JSContext *ctx, JS::HandleObject hobj, JS::H
/* This can be called if @obj if not itself an instance of the
* appropriate class but has one in its prototype chain. Fail
* such calls. */
if (!JS_InstanceOf(ctx, obj, (JSClass *) &smjs_globhist_item_class, NULL))
return JS_FALSE;
if (!JS_InstanceOf(ctx, hobj, (JSClass *) &smjs_globhist_item_class, NULL))
return false;
history_item = JS_GetInstancePrivate(ctx, obj,
history_item = JS_GetInstancePrivate(ctx, hobj,
(JSClass *) &smjs_globhist_item_class,
NULL);
if (!history_item) return JS_FALSE;
if (!history_item) return false;
jsstr = JS_ValueToString(smjs_ctx, *vp);
jsstr = JS::ToString(smjs_ctx, args[0]);
str = JS_EncodeString(smjs_ctx, jsstr);
mem_free_set(&history_item->url, stracpy(str));
return JS_TRUE;
return true;
}
static JSBool
smjs_globhist_item_get_property_last_visit(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid,
JS::MutableHandleValue hvp)
static bool
smjs_globhist_item_get_property_last_visit(JSContext *ctx, unsigned int argc, jsval *vp)
{
ELINKS_CAST_PROP_PARAMS
JS::CallArgs args = CallArgsFromVp(argc, vp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
struct global_history_item *history_item;
/* This can be called if @obj if not itself an instance of the
* appropriate class but has one in its prototype chain. Fail
* such calls. */
if (!JS_InstanceOf(ctx, obj, (JSClass *) &smjs_globhist_item_class, NULL))
return JS_FALSE;
if (!JS_InstanceOf(ctx, hobj, (JSClass *) &smjs_globhist_item_class, NULL))
return false;
history_item = JS_GetInstancePrivate(ctx, obj,
history_item = JS_GetInstancePrivate(ctx, hobj,
(JSClass *) &smjs_globhist_item_class,
NULL);
if (!history_item) return JS_FALSE;
if (!history_item) return false;
/* TODO: I'd rather return a date object, but that introduces
* synchronisation issues:
@ -426,16 +429,17 @@ smjs_globhist_item_get_property_last_visit(JSContext *ctx, JS::HandleObject hobj
* Since the Date object uses milliseconds since the epoch,
* I'd rather export that, but SpiderMonkey doesn't provide
* a suitable type. -- Miciah */
*vp = JS_NumberValue(history_item->last_visit);
args.rval().setInt32(history_item->last_visit);
return JS_TRUE;
return true;
}
/* @smjs_globhist_item_class.setProperty */
static JSBool
smjs_globhist_item_set_property_last_visit(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JSBool strict, JS::MutableHandleValue hvp)
static bool
smjs_globhist_item_set_property_last_visit(JSContext *ctx, unsigned int argc, jsval *vp)
{
ELINKS_CAST_PROP_PARAMS
JS::CallArgs args = CallArgsFromVp(argc, vp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
struct global_history_item *history_item;
uint32_t seconds;
@ -443,18 +447,18 @@ smjs_globhist_item_set_property_last_visit(JSContext *ctx, JS::HandleObject hobj
/* This can be called if @obj if not itself an instance of the
* appropriate class but has one in its prototype chain. Fail
* such calls. */
if (!JS_InstanceOf(ctx, obj, (JSClass *) &smjs_globhist_item_class, NULL))
return JS_FALSE;
if (!JS_InstanceOf(ctx, hobj, (JSClass *) &smjs_globhist_item_class, NULL))
return false;
history_item = JS_GetInstancePrivate(ctx, obj,
history_item = JS_GetInstancePrivate(ctx, hobj,
(JSClass *) &smjs_globhist_item_class,
NULL);
if (!history_item) return JS_FALSE;
if (!history_item) return false;
/* Bug 923: Assumes time_t values fit in uint32. */
JS_ValueToECMAUint32(smjs_ctx, *vp, &seconds);
JS::ToInt32(smjs_ctx, args[0], &seconds);
history_item->last_visit = seconds;
return JS_TRUE;
return true;
}

View File

@ -27,20 +27,20 @@ script_hook_url(va_list ap, void *data)
unsigned char **url = va_arg(ap, unsigned char **);
struct session *ses = va_arg(ap, struct session *);
enum evhook_status ret = EVENT_HOOK_STATUS_NEXT;
jsval args[1], rval;
jsval args[3], rval;
JS::RootedValue r_rval(smjs_ctx, rval);
if (*url == NULL) return EVENT_HOOK_STATUS_NEXT;
smjs_ses = ses;
args[2].setString(JS_NewStringCopyZ(smjs_ctx, *url));
args[0] = STRING_TO_JSVAL(JS_NewStringCopyZ(smjs_ctx, *url));
if (JS_TRUE == smjs_invoke_elinks_object_method(data, args, 1, &rval)) {
if (JSVAL_IS_BOOLEAN(rval)) {
if (JS_FALSE == JSVAL_TO_BOOLEAN(rval))
if (true == smjs_invoke_elinks_object_method(data, 1, args, &r_rval)) {
if (r_rval.isBoolean()) {
if (false == (r_rval.toBoolean()))
ret = EVENT_HOOK_STATUS_LAST;
} else {
JSString *jsstr = JS_ValueToString(smjs_ctx, rval);
JSString *jsstr = JS::ToString(smjs_ctx, r_rval);
unsigned char *str = JS_EncodeString(smjs_ctx, jsstr);
mem_free_set(url, stracpy(str));
@ -59,7 +59,8 @@ script_hook_pre_format_html(va_list ap, void *data)
struct cache_entry *cached = va_arg(ap, struct cache_entry *);
enum evhook_status ret = EVENT_HOOK_STATUS_NEXT;
JSObject *cache_entry_object, *view_state_object = NULL;
jsval args[2], rval;
jsval args[4], rval;
JS::RootedValue r_rval(smjs_ctx, rval);
evhook_use_params(ses && cached);
@ -76,12 +77,12 @@ script_hook_pre_format_html(va_list ap, void *data)
cache_entry_object = smjs_get_cache_entry_object(cached);
if (!cache_entry_object) goto end;
args[0] = OBJECT_TO_JSVAL(cache_entry_object);
args[1] = OBJECT_TO_JSVAL(view_state_object);
args[2].setObject(*cache_entry_object);
args[3].setObject(*view_state_object);
if (JS_TRUE == smjs_invoke_elinks_object_method("preformat_html",
args, 2, &rval))
if (JS_FALSE == JSVAL_TO_BOOLEAN(rval))
if (true == smjs_invoke_elinks_object_method("preformat_html",
2, args, &r_rval))
if (false == JSVAL_TO_BOOLEAN(rval))
ret = EVENT_HOOK_STATUS_LAST;
end:

View File

@ -13,8 +13,8 @@
#include "scripting/smjs/elinks_object.h"
#include "util/memory.h"
static JSBool keymap_get_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp);
static JSBool keymap_set_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JSBool strict, JS::MutableHandleValue hvp);
static bool keymap_get_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp);
static bool keymap_set_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, bool strict, JS::MutableHandleValue hvp);
static void keymap_finalize(JSFreeOp *op, JSObject *obj);
static const JSClass keymap_class = {
@ -26,50 +26,53 @@ static const JSClass keymap_class = {
};
/* @keymap_class.getProperty */
static JSBool
static bool
keymap_get_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp)
{
ELINKS_CAST_PROP_PARAMS
jsid id = hid.get();
unsigned char *action_str;
const unsigned char *keystroke_str;
int *data;
jsval tmp;
JS::RootedValue r_tmp(ctx, tmp);
/* This can be called if @obj if not itself an instance of the
* appropriate class but has one in its prototype chain. Fail
* such calls. */
if (!JS_InstanceOf(ctx, obj, (JSClass *) &keymap_class, NULL))
return JS_FALSE;
if (!JS_InstanceOf(ctx, hobj, (JSClass *) &keymap_class, NULL))
return false;
data = JS_GetInstancePrivate(ctx, obj,
data = JS_GetInstancePrivate(ctx, hobj,
(JSClass *) &keymap_class, NULL);
if (!JS_IdToValue(ctx, id, &tmp))
if (!JS_IdToValue(ctx, id, &r_tmp))
goto ret_null;
keystroke_str = JS_EncodeString(ctx, JS_ValueToString(ctx, tmp));
keystroke_str = JS_EncodeString(ctx, JS::ToString(ctx, r_tmp));
if (!keystroke_str) goto ret_null;
action_str = get_action_name_from_keystroke((enum keymap_id) *data,
keystroke_str);
if (!action_str || !strcmp(action_str, "none")) goto ret_null;
*vp = STRING_TO_JSVAL(JS_NewStringCopyZ(ctx, action_str));
hvp.setString(JS_NewStringCopyZ(ctx, action_str));
return JS_TRUE;
return true;
ret_null:
*vp = JSVAL_NULL;
hvp.setNull();
return JS_TRUE;
return true;
}
static enum evhook_status
smjs_keybinding_action_callback(va_list ap, void *data)
{
JS::CallArgs args;
jsval rval;
JS::RootedValue r_rval(smjs_ctx, rval);
struct session *ses = va_arg(ap, struct session *);
JSObject *jsobj = data;
@ -77,8 +80,12 @@ smjs_keybinding_action_callback(va_list ap, void *data)
smjs_ses = ses;
JS_CallFunctionValue(smjs_ctx, NULL, OBJECT_TO_JSVAL(jsobj),
0, NULL, &rval);
jsval r2;
JS::RootedValue r_jsobject(smjs_ctx, r2);
r_jsobject.setObject(*jsobj);
JS_CallFunctionValue(smjs_ctx, JS::NullPtr(), r_jsobject,
args, &r_rval);
smjs_ses = NULL;
@ -86,10 +93,9 @@ smjs_keybinding_action_callback(va_list ap, void *data)
}
/* @keymap_class.setProperty */
static JSBool
keymap_set_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JSBool strict, JS::MutableHandleValue hvp)
static bool
keymap_set_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, bool strict, JS::MutableHandleValue hvp)
{
ELINKS_CAST_PROP_PARAMS
jsid id = hid.get();
int *data;
@ -100,48 +106,49 @@ keymap_set_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JSB
/* This can be called if @obj if not itself an instance of the
* appropriate class but has one in its prototype chain. Fail
* such calls. */
if (!JS_InstanceOf(ctx, obj, (JSClass *) &keymap_class, NULL))
return JS_FALSE;
if (!JS_InstanceOf(ctx, hobj, (JSClass *) &keymap_class, NULL))
return false;
data = JS_GetInstancePrivate(ctx, obj,
data = JS_GetInstancePrivate(ctx, hobj,
(JSClass *) &keymap_class, NULL);
/* Ugly fact: we need to get the string from the id to give to bind_do,
* which will of course then convert the string back to an id... */
keymap_str = get_keymap_name((enum keymap_id) *data);
if (!keymap_str) return JS_FALSE;
if (!keymap_str) return false;
JS_IdToValue(ctx, id, &val);
keystroke_str = JS_EncodeString(ctx, JS_ValueToString(ctx, val));
if (!keystroke_str) return JS_FALSE;
JS::RootedValue rval(ctx, val);
JS_IdToValue(ctx, id, &rval);
keystroke_str = JS_EncodeString(ctx, JS::ToString(ctx, rval));
if (!keystroke_str) return false;
if (JSVAL_IS_STRING(*vp)) {
if (hvp.isString()) {
unsigned char *action_str;
action_str = JS_EncodeString(ctx, JS_ValueToString(ctx, *vp));
if (!action_str) return JS_FALSE;
action_str = JS_EncodeString(ctx, JS::ToString(ctx, hvp));
if (!action_str) return false;
if (bind_do(keymap_str, keystroke_str, action_str, 0))
return JS_FALSE;
return false;
return JS_TRUE;
return true;
} else if (JSVAL_IS_NULL(*vp)) { /* before JSVAL_IS_OBJECT */
} else if (hvp.isNull()) { /* before JSVAL_IS_OBJECT */
if (bind_do(keymap_str, keystroke_str, "none", 0))
return JS_FALSE;
return false;
return JS_TRUE;
return true;
} else if (!JSVAL_IS_PRIMITIVE(*vp) || JSVAL_IS_NULL(*vp)) {
} else if (hvp.isObject() || hvp.isNull()) {
unsigned char *err = NULL;
int event_id;
struct string event_name = NULL_STRING;
JSObject *jsobj = JSVAL_TO_OBJECT(*vp);
JSObject *jsobj = &hvp.toObject();
if (JS_FALSE == JS_ObjectIsFunction(ctx, jsobj))
return JS_FALSE;
if (false == JS_ObjectIsFunction(ctx, jsobj))
return false;
if (!init_string(&event_name)) return JS_FALSE;
if (!init_string(&event_name)) return false;
add_format_to_string(&event_name, "smjs-run-func %p", jsobj);
@ -154,7 +161,7 @@ keymap_set_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JSB
if (err) {
alert_smjs_error(err);
return JS_FALSE;
return false;
}
event_id = register_event_hook(event_id,
@ -165,13 +172,13 @@ keymap_set_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JSB
alert_smjs_error("error registering event hook"
" for keybinding");
return JS_FALSE;
return false;
}
return JS_TRUE;
return true;
}
return JS_FALSE;
return false;
}
/* @keymap_class.finalize */
@ -198,7 +205,7 @@ smjs_get_keymap_object(enum keymap_id keymap_id)
assert(smjs_ctx);
keymap_object = JS_NewObject(smjs_ctx, (JSClass *) &keymap_class,
NULL, NULL);
JS::NullPtr(),JS::NullPtr());
if (!keymap_object) return NULL;
@ -225,9 +232,12 @@ smjs_get_keymap_hash_object(void)
JSObject *keymaps_hash;
keymaps_hash = JS_NewObject(smjs_ctx, (JSClass *) &keymaps_hash_class,
NULL, NULL);
JS::NullPtr(), JS::NullPtr());
if (!keymaps_hash) return NULL;
JS::RootedObject r_keymaps_hash(smjs_ctx, keymaps_hash);
JS::RootedValue r_val(smjs_ctx, val);
for (keymap_id = 0; keymap_id < KEYMAP_MAX; ++keymap_id) {
unsigned char *keymap_str = get_keymap_name(keymap_id);
JSObject *map = smjs_get_keymap_object(keymap_id);
@ -236,9 +246,9 @@ smjs_get_keymap_hash_object(void)
if (!map) return NULL;
val = OBJECT_TO_JSVAL(map);
r_val.setObject(*map);
JS_SetProperty(smjs_ctx, keymaps_hash, keymap_str, &val);
JS_SetProperty(smjs_ctx, r_keymaps_hash, keymap_str, r_val);
}
return keymaps_hash;
@ -256,7 +266,9 @@ smjs_init_keybinding_interface(void)
keymaps_hash = smjs_get_keymap_hash_object();
if (!keymaps_hash) return;
val = OBJECT_TO_JSVAL(keymaps_hash);
JS::RootedValue r_val(smjs_ctx, val);
r_val.setObject(*keymaps_hash);
JS::RootedObject r_smjs_elinks_object(smjs_ctx, smjs_elinks_object);
JS_SetProperty(smjs_ctx, smjs_elinks_object, "keymaps", &val);
JS_SetProperty(smjs_ctx, r_smjs_elinks_object, "keymaps", r_val);
}

View File

@ -24,7 +24,7 @@ struct smjs_load_uri_hop {
* jsval that points to the corresponding JSObject. Besides,
* JS_AddNamedRoot is not documented to support JSFunction
* pointers. */
jsval callback;
JS::MutableHandleValue callback;
};
static void
@ -32,11 +32,15 @@ smjs_loading_callback(struct download *download, void *data)
{
struct session *saved_smjs_ses = smjs_ses;
struct smjs_load_uri_hop *hop = data;
jsval args[1], rval;
JSObject *cache_entry_object;
if (is_in_progress_state(download->state)) return;
JS::CallArgs argv;
JS::RootedValue r_rval(smjs_ctx, rval);
if (!download->cached) goto end;
/* download->cached->object.refcount is typically 0 here
@ -52,22 +56,24 @@ smjs_loading_callback(struct download *download, void *data)
if (!cache_entry_object) goto end;
args[0] = OBJECT_TO_JSVAL(cache_entry_object);
JS_CallFunctionValue(smjs_ctx, NULL, hop->callback, 1, args, &rval);
argv = CallArgsFromVp(1, args);
JS_CallFunctionValue(smjs_ctx, JS::NullPtr(), hop->callback, argv, &r_rval);
end:
if (download->cached)
object_unlock(download->cached);
JS_RemoveValueRoot(smjs_ctx, &hop->callback);
mem_free(download->data);
mem_free(download);
smjs_ses = saved_smjs_ses;
}
static JSBool
static bool
smjs_load_uri(JSContext *ctx, unsigned int argc, jsval *rval)
{
jsval *argv = JS_ARGV(ctx, rval);
JS::CallArgs args = CallArgsFromVp(argc, rval);
struct smjs_load_uri_hop *hop;
struct download *download;
JSString *jsstr;
@ -75,45 +81,38 @@ smjs_load_uri(JSContext *ctx, unsigned int argc, jsval *rval)
unsigned char *uri_string;
struct uri *uri;
if (argc < 2) return JS_FALSE;
if (argc < 2) return false;
jsstr = JS_ValueToString(smjs_ctx, argv[0]);
jsstr = JS::ToString(smjs_ctx, args[0]);
uri_string = JS_EncodeString(smjs_ctx, jsstr);
if (!uri_string || !*uri_string) return JS_FALSE;
if (!uri_string || !*uri_string) return false;
uri = get_uri(uri_string, 0);
if (!uri) return JS_FALSE;
if (!uri) return false;
external_handler = get_protocol_external_handler(NULL, uri);
if (external_handler) {
/* Because smjs_load_uri is carrying out an asynchronous
* operation, it is inappropriate to call an external
* handler here, so just return. */
return JS_FALSE;
return false;
}
download = mem_alloc(sizeof(*download));
if (!download) {
done_uri(uri);
return JS_FALSE;
return false;
}
hop = mem_alloc(sizeof(*hop));
if (!hop) {
mem_free(download);
done_uri(uri);
return JS_FALSE;
return false;
}
hop->callback = argv[1];
hop->callback.set(args[1]);
hop->ses = smjs_ses;
if (!JS_AddNamedValueRoot(smjs_ctx, &hop->callback,
"smjs_load_uri_hop.callback")) {
mem_free(hop);
mem_free(download);
done_uri(uri);
return JS_FALSE;
}
download->data = hop;
download->callback = (download_callback_T *) smjs_loading_callback;
@ -122,7 +121,7 @@ smjs_load_uri(JSContext *ctx, unsigned int argc, jsval *rval)
done_uri(uri);
return JS_TRUE;
return true;
}
void
@ -131,6 +130,8 @@ smjs_init_load_uri_interface(void)
if (!smjs_ctx || !smjs_elinks_object)
return;
JS_DefineFunction(smjs_ctx, smjs_elinks_object, "load_uri",
JS::RootedObject r_smjs_elinks_object(smjs_ctx, smjs_elinks_object);
JS_DefineFunction(smjs_ctx, r_smjs_elinks_object, "load_uri",
&smjs_load_uri, 2, 0);
}

File diff suppressed because it is too large Load Diff

View File

@ -17,7 +17,7 @@
#include "util/memory.h"
#include "viewer/text/vs.h"
static JSBool terminal_get_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp);
static bool terminal_get_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp);
static void terminal_finalize(JSFreeOp *op, JSObject *obj);
static const JSClass terminal_class = {
@ -38,10 +38,9 @@ static const JSPropertySpec terminal_props[] = {
};
/* @terminal_class.getProperty */
static JSBool
static bool
terminal_get_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp)
{
ELINKS_CAST_PROP_PARAMS
jsid id = hid.get();
struct terminal *term;
@ -49,31 +48,33 @@ terminal_get_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, J
/* This can be called if @obj if not itself an instance of the
* appropriate class but has one in its prototype chain. Fail
* such calls. */
if (!JS_InstanceOf(ctx, obj, (JSClass *) &terminal_class, NULL))
return JS_FALSE;
if (!JS_InstanceOf(ctx, hobj, (JSClass *) &terminal_class, NULL))
return false;
term = JS_GetInstancePrivate(ctx, obj,
term = JS_GetInstancePrivate(ctx, hobj,
(JSClass *) &terminal_class, NULL);
if (!term) return JS_FALSE; /* already detached */
if (!term) return false; /* already detached */
undef_to_jsval(ctx, vp);
hvp.setUndefined();
if (!JSID_IS_INT(id)) return JS_FALSE;
if (!JSID_IS_INT(id)) return false;
switch (JSID_TO_INT(id)) {
case TERMINAL_TAB: {
JSObject *obj = smjs_get_session_array_object(term);
if (obj) object_to_jsval(ctx, vp, obj);
if (obj) {
hvp.setObject(*obj);
}
return JS_TRUE;
return true;
}
default:
INTERNAL("Invalid ID %d in terminal_get_property().",
JSID_TO_INT(id));
}
return JS_FALSE;
return false;
}
/** Pointed to by terminal_class.finalize. SpiderMonkey automatically
@ -111,11 +112,12 @@ smjs_get_terminal_object(struct terminal *term)
assert(smjs_ctx);
if_assert_failed return NULL;
obj = JS_NewObject(smjs_ctx, (JSClass *) &terminal_class, NULL, NULL);
obj = JS_NewObject(smjs_ctx, (JSClass *) &terminal_class, JS::NullPtr(), JS::NullPtr());
if (!obj) return NULL;
if (JS_FALSE == JS_DefineProperties(smjs_ctx, obj,
JS::RootedObject robj(smjs_ctx, obj);
if (false == JS_DefineProperties(smjs_ctx, robj,
(JSPropertySpec *) terminal_props))
return NULL;
@ -143,7 +145,9 @@ smjs_detach_terminal_object(struct terminal *term)
if (!term->jsobject) return;
assert(JS_GetInstancePrivate(smjs_ctx, term->jsobject,
JS::RootedObject r_jsobject(smjs_ctx, term->jsobject);
assert(JS_GetInstancePrivate(smjs_ctx, r_jsobject,
(JSClass *) &terminal_class, NULL)
== term);
if_assert_failed {}
@ -154,31 +158,32 @@ smjs_detach_terminal_object(struct terminal *term)
/* @terminal_array_class.getProperty */
static JSBool
static bool
terminal_array_get_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp)
{
ELINKS_CAST_PROP_PARAMS
jsid id = hid.get();
int index;
struct terminal *term;
undef_to_jsval(ctx, vp);
hvp.setUndefined();
if (!JSID_IS_INT(id))
return JS_FALSE;
return false;
index = JSID_TO_INT(id);
foreach (term, terminals) {
if (!index) break;
--index;
}
if ((void *) term == (void *) &terminals) return JS_FALSE;
if ((void *) term == (void *) &terminals) return false;
obj = smjs_get_terminal_object(term);
if (obj) object_to_jsval(ctx, vp, obj);
JSObject *obj = smjs_get_terminal_object(term);
if (obj) {
hvp.setObject(*obj);
}
return JS_TRUE;
return true;
;
}
@ -199,7 +204,7 @@ smjs_get_terminal_array_object(void)
if_assert_failed return NULL;
return JS_NewObject(smjs_ctx, (JSClass *) &terminal_array_class,
NULL, NULL);
JS::NullPtr(), JS::NullPtr());
}
void
@ -214,7 +219,9 @@ smjs_init_terminal_interface(void)
obj = smjs_get_terminal_array_object();
if (!obj) return;
val = OBJECT_TO_JSVAL(obj);
JS::RootedValue rval(smjs_ctx, val);
rval.setObject(*obj);
JS::RootedObject r_smjs_elinks_object(smjs_ctx, smjs_elinks_object);
JS_SetProperty(smjs_ctx, smjs_elinks_object, "terminal", &val);
JS_SetProperty(smjs_ctx, r_smjs_elinks_object, "terminal", rval);
}

View File

@ -20,8 +20,8 @@
#include "util/memory.h"
#include "viewer/text/vs.h"
static JSBool view_state_get_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp);
static JSBool view_state_set_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JSBool strict, JS::MutableHandleValue hvp);
static bool view_state_get_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp);
static bool view_state_set_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, bool strict, JS::MutableHandleValue hvp);
static void view_state_finalize(JSFreeOp *op, JSObject *obj);
static const JSClass view_state_class = {
@ -42,16 +42,15 @@ enum view_state_prop {
};
static const JSPropertySpec view_state_props[] = {
{ "plain", VIEW_STATE_PLAIN, JSPROP_ENUMERATE },
{ "uri", VIEW_STATE_URI, JSPROP_ENUMERATE | JSPROP_READONLY },
{ "plain", (unsigned char)VIEW_STATE_PLAIN, JSPROP_ENUMERATE },
{ "uri", (unsigned char)VIEW_STATE_URI, JSPROP_ENUMERATE | JSPROP_READONLY },
{ NULL }
};
/* @view_state_class.getProperty */
static JSBool
static bool
view_state_get_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp)
{
ELINKS_CAST_PROP_PARAMS
jsid id = hid.get();
struct view_state *vs;
@ -59,44 +58,42 @@ view_state_get_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid,
/* This can be called if @obj if not itself an instance of the
* appropriate class but has one in its prototype chain. Fail
* such calls. */
if (!JS_InstanceOf(ctx, obj, (JSClass *) &view_state_class, NULL))
return JS_FALSE;
if (!JS_InstanceOf(ctx, hobj, (JSClass *) &view_state_class, NULL))
return false;
vs = JS_GetInstancePrivate(ctx, obj,
vs = JS_GetInstancePrivate(ctx, hobj,
(JSClass *) &view_state_class, NULL);
if (!vs) return JS_FALSE;
if (!vs) return false;
undef_to_jsval(ctx, vp);
hvp.setUndefined();
if (!JSID_IS_INT(id))
return JS_FALSE;
return false;
switch (JSID_TO_INT(id)) {
case VIEW_STATE_PLAIN:
*vp = INT_TO_JSVAL(vs->plain);
hvp.setInt32(vs->plain);
return JS_TRUE;
return true;
case VIEW_STATE_URI:
*vp = STRING_TO_JSVAL(JS_NewStringCopyZ(smjs_ctx,
struri(vs->uri)));
hvp.setString(JS_NewStringCopyZ(smjs_ctx, struri(vs->uri)));
return JS_TRUE;
return true;
default:
/* Unrecognized integer property ID; someone is using
* the object as an array. SMJS builtin classes (e.g.
* js_RegExpClass) just return JS_TRUE in this case
* js_RegExpClass) just return true in this case
* and leave *@vp unchanged. Do the same here.
* (Actually not quite the same, as we already used
* @undef_to_jsval.) */
return JS_TRUE;
return true;
}
}
/* @view_state_class.setProperty */
static JSBool
view_state_set_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JSBool strict, JS::MutableHandleValue hvp)
static bool
view_state_set_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, bool strict, JS::MutableHandleValue hvp)
{
ELINKS_CAST_PROP_PARAMS
jsid id = hid.get();
struct view_state *vs;
@ -104,28 +101,28 @@ view_state_set_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid,
/* This can be called if @obj if not itself an instance of the
* appropriate class but has one in its prototype chain. Fail
* such calls. */
if (!JS_InstanceOf(ctx, obj, (JSClass *) &view_state_class, NULL))
return JS_FALSE;
if (!JS_InstanceOf(ctx, hobj, (JSClass *) &view_state_class, NULL))
return false;
vs = JS_GetInstancePrivate(ctx, obj,
vs = JS_GetInstancePrivate(ctx, hobj,
(JSClass *) &view_state_class, NULL);
if (!vs) return JS_FALSE;
if (!vs) return false;
if (!JSID_IS_INT(id))
return JS_FALSE;
return false;
switch (JSID_TO_INT(id)) {
case VIEW_STATE_PLAIN: {
vs->plain = atol(jsval_to_string(ctx, vp));
vs->plain = hvp.toInt32();
return JS_TRUE;
return true;
}
default:
/* Unrecognized integer property ID; someone is using
* the object as an array. SMJS builtin classes (e.g.
* js_RegExpClass) just return JS_TRUE in this case.
* js_RegExpClass) just return true in this case.
* Do the same here. */
return JS_TRUE;
return true;
}
}
@ -169,11 +166,13 @@ smjs_get_view_state_object(struct view_state *vs)
view_state_object = JS_NewObject(smjs_ctx,
(JSClass *) &view_state_class,
NULL, NULL);
JS::NullPtr(), JS::NullPtr());
if (!view_state_object) return NULL;
if (JS_FALSE == JS_DefineProperties(smjs_ctx, view_state_object,
JS::RootedObject r_view_state_object(smjs_ctx, view_state_object);
if (false == JS_DefineProperties(smjs_ctx, r_view_state_object,
(JSPropertySpec *) view_state_props))
return NULL;
@ -186,28 +185,25 @@ smjs_get_view_state_object(struct view_state *vs)
return view_state_object;
}
static JSBool
static bool
smjs_elinks_get_view_state(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp)
{
ELINKS_CAST_PROP_PARAMS
(void)obj;
JSObject *vs_obj;
struct view_state *vs;
*vp = JSVAL_NULL;
hvp.setNull();
if (!smjs_ses || !have_location(smjs_ses)) return JS_TRUE;
if (!smjs_ses || !have_location(smjs_ses)) return true;
vs = &cur_loc(smjs_ses)->vs;
if (!vs) return JS_TRUE;
if (!vs) return true;
vs_obj = smjs_get_view_state_object(vs);
if (!vs_obj) return JS_TRUE;
if (!vs_obj) return true;
*vp = OBJECT_TO_JSVAL(vs_obj);
hvp.setObject(*vs_obj);
return JS_TRUE;
return true;
}
/** Ensure that no JSObject contains the pointer @a vs. This is called from
@ -223,7 +219,9 @@ smjs_detach_view_state_object(struct view_state *vs)
if (!vs->jsobject) return;
assert(JS_GetInstancePrivate(smjs_ctx, vs->jsobject,
JS::RootedObject r_vs_jsobject(smjs_ctx, vs->jsobject);
assert(JS_GetInstancePrivate(smjs_ctx, r_vs_jsobject,
(JSClass *) &view_state_class, NULL)
== vs);
if_assert_failed {}
@ -238,7 +236,9 @@ smjs_init_view_state_interface(void)
if (!smjs_ctx || !smjs_elinks_object)
return;
JS_DefineProperty(smjs_ctx, smjs_elinks_object, "vs", JSVAL_NULL,
smjs_elinks_get_view_state, JS_StrictPropertyStub,
JSPROP_ENUMERATE | JSPROP_PERMANENT | JSPROP_READONLY);
JS::RootedObject r_smjs_elinks_object(smjs_ctx, smjs_elinks_object);
JS_DefineProperty(smjs_ctx, r_smjs_elinks_object, "vs", (int32_t)0,
(unsigned int)(JSPROP_ENUMERATE | JSPROP_PERMANENT | JSPROP_READONLY), smjs_elinks_get_view_state, JS_StrictPropertyStub
);
}