2006-01-27 22:23:17 -05:00
|
|
|
/* Exports struct view_state to the world of ECMAScript */
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "elinks.h"
|
|
|
|
|
2008-07-16 05:32:24 -04:00
|
|
|
#include "ecmascript/spidermonkey-shared.h"
|
2006-01-27 22:23:17 -05:00
|
|
|
#include "protocol/uri.h"
|
2006-06-10 14:02:10 -04:00
|
|
|
#include "scripting/smjs/elinks_object.h"
|
2006-01-27 22:23:17 -05:00
|
|
|
#include "scripting/smjs/view_state_object.h"
|
|
|
|
#include "scripting/smjs/core.h"
|
2006-06-10 14:02:10 -04:00
|
|
|
#include "session/history.h"
|
|
|
|
#include "session/location.h"
|
|
|
|
#include "session/session.h"
|
2006-01-27 22:23:17 -05:00
|
|
|
#include "util/error.h"
|
|
|
|
#include "util/memory.h"
|
|
|
|
#include "viewer/text/vs.h"
|
|
|
|
|
2020-10-12 12:43:56 -04:00
|
|
|
static bool view_state_get_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp);
|
2020-10-23 16:34:58 -04:00
|
|
|
static bool view_state_set_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp);
|
2020-10-05 14:14:55 -04:00
|
|
|
static void view_state_finalize(JSFreeOp *op, JSObject *obj);
|
|
|
|
|
2020-10-27 09:53:24 -04:00
|
|
|
static const JSClassOps view_state_ops = {
|
2021-08-25 14:08:02 -04:00
|
|
|
nullptr, nullptr,
|
2020-10-05 14:14:55 -04:00
|
|
|
view_state_get_property, view_state_set_property,
|
2020-10-16 13:54:02 -04:00
|
|
|
nullptr, nullptr, nullptr, view_state_finalize
|
2020-10-05 14:14:55 -04:00
|
|
|
};
|
2006-11-25 01:54:58 -05:00
|
|
|
|
2020-10-27 09:53:24 -04:00
|
|
|
static const JSClass view_state_class = {
|
|
|
|
"view_state",
|
|
|
|
JSCLASS_HAS_PRIVATE, /* struct view_state * */
|
|
|
|
&view_state_ops
|
|
|
|
};
|
|
|
|
|
2006-12-06 16:09:14 -05:00
|
|
|
/* Tinyids of properties. Use negative values to distinguish these
|
|
|
|
* from array indexes (even though this object has no array elements).
|
|
|
|
* ECMAScript code should not use these directly as in view_state[-1];
|
|
|
|
* future versions of ELinks may change the numbers. */
|
2006-01-27 22:23:17 -05:00
|
|
|
enum view_state_prop {
|
2006-12-06 16:09:14 -05:00
|
|
|
VIEW_STATE_PLAIN = -1,
|
|
|
|
VIEW_STATE_URI = -2,
|
2006-01-27 22:23:17 -05:00
|
|
|
};
|
|
|
|
|
2020-10-16 13:54:02 -04:00
|
|
|
static bool
|
2020-10-23 16:34:58 -04:00
|
|
|
view_state_get_property_plain(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
2020-10-16 13:54:02 -04:00
|
|
|
{
|
|
|
|
JS::CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
JS::RootedObject hobj(ctx, &args.thisv().toObject());
|
|
|
|
|
|
|
|
struct view_state *vs;
|
|
|
|
|
|
|
|
/* 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, hobj, (JSClass *) &view_state_class, NULL))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
vs = JS_GetInstancePrivate(ctx, hobj,
|
|
|
|
(JSClass *) &view_state_class, NULL);
|
|
|
|
if (!vs) return false;
|
|
|
|
|
|
|
|
args.rval().setInt32(vs->plain);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2020-10-23 16:34:58 -04:00
|
|
|
view_state_set_property_plain(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
2020-10-16 13:54:02 -04:00
|
|
|
{
|
|
|
|
JS::CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
JS::RootedObject hobj(ctx, &args.thisv().toObject());
|
|
|
|
|
|
|
|
struct view_state *vs;
|
|
|
|
|
|
|
|
if (argc != 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/* 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, hobj, (JSClass *) &view_state_class, NULL))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
vs = JS_GetInstancePrivate(ctx, hobj,
|
|
|
|
(JSClass *) &view_state_class, NULL);
|
|
|
|
if (!vs) return false;
|
|
|
|
|
|
|
|
vs->plain = args[0].toInt32();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2020-10-23 16:34:58 -04:00
|
|
|
view_state_get_property_uri(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
2020-10-16 13:54:02 -04:00
|
|
|
{
|
|
|
|
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, hobj, (JSClass *) &view_state_class, NULL))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
struct view_state *vs = JS_GetInstancePrivate(ctx, hobj,
|
|
|
|
(JSClass *) &view_state_class, NULL);
|
|
|
|
if (!vs) return false;
|
|
|
|
|
|
|
|
args.rval().setString(JS_NewStringCopyZ(ctx, struri(vs->uri)));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-01-27 22:23:17 -05:00
|
|
|
static const JSPropertySpec view_state_props[] = {
|
2020-10-16 13:54:02 -04:00
|
|
|
JS_PSGS("plain", view_state_get_property_plain, view_state_set_property_plain, JSPROP_ENUMERATE),
|
|
|
|
JS_PSG("uri", view_state_get_property_uri, JSPROP_ENUMERATE),
|
|
|
|
JS_PS_END
|
2006-01-27 22:23:17 -05:00
|
|
|
};
|
|
|
|
|
2006-11-23 16:33:43 -05:00
|
|
|
/* @view_state_class.getProperty */
|
2020-10-12 12:43:56 -04:00
|
|
|
static bool
|
2020-10-05 14:14:55 -04:00
|
|
|
view_state_get_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp)
|
2006-01-27 22:23:17 -05:00
|
|
|
{
|
2020-10-05 14:14:55 -04:00
|
|
|
jsid id = hid.get();
|
2019-02-10 15:00:37 -05:00
|
|
|
|
2006-11-24 01:50:12 -05:00
|
|
|
struct view_state *vs;
|
|
|
|
|
2006-12-03 04:14:22 -05:00
|
|
|
/* 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. */
|
2020-10-12 12:43:56 -04:00
|
|
|
if (!JS_InstanceOf(ctx, hobj, (JSClass *) &view_state_class, NULL))
|
|
|
|
return false;
|
2006-11-25 01:54:58 -05:00
|
|
|
|
2020-10-12 12:43:56 -04:00
|
|
|
vs = JS_GetInstancePrivate(ctx, hobj,
|
2007-05-27 11:32:53 -04:00
|
|
|
(JSClass *) &view_state_class, NULL);
|
2020-10-12 12:43:56 -04:00
|
|
|
if (!vs) return false;
|
2006-01-27 22:23:17 -05:00
|
|
|
|
2020-10-12 12:43:56 -04:00
|
|
|
hvp.setUndefined();
|
2006-01-27 22:23:17 -05:00
|
|
|
|
2011-04-19 16:41:05 -04:00
|
|
|
if (!JSID_IS_INT(id))
|
2020-10-12 12:43:56 -04:00
|
|
|
return false;
|
2006-01-27 22:23:17 -05:00
|
|
|
|
2011-04-19 16:41:05 -04:00
|
|
|
switch (JSID_TO_INT(id)) {
|
2006-01-27 22:23:17 -05:00
|
|
|
case VIEW_STATE_PLAIN:
|
2020-10-12 12:43:56 -04:00
|
|
|
hvp.setInt32(vs->plain);
|
2006-01-27 22:23:17 -05:00
|
|
|
|
2020-10-12 12:43:56 -04:00
|
|
|
return true;
|
2006-01-27 22:23:17 -05:00
|
|
|
case VIEW_STATE_URI:
|
2020-10-12 12:43:56 -04:00
|
|
|
hvp.setString(JS_NewStringCopyZ(smjs_ctx, struri(vs->uri)));
|
2006-01-27 22:23:17 -05:00
|
|
|
|
2020-10-12 12:43:56 -04:00
|
|
|
return true;
|
2006-01-27 22:23:17 -05:00
|
|
|
default:
|
2007-05-27 11:36:31 -04:00
|
|
|
/* Unrecognized integer property ID; someone is using
|
|
|
|
* the object as an array. SMJS builtin classes (e.g.
|
2020-10-12 12:43:56 -04:00
|
|
|
* js_RegExpClass) just return true in this case
|
2006-12-03 05:07:07 -05:00
|
|
|
* and leave *@vp unchanged. Do the same here.
|
|
|
|
* (Actually not quite the same, as we already used
|
|
|
|
* @undef_to_jsval.) */
|
2020-10-12 12:43:56 -04:00
|
|
|
return true;
|
2006-01-27 22:23:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-11-23 16:33:43 -05:00
|
|
|
/* @view_state_class.setProperty */
|
2020-10-12 12:43:56 -04:00
|
|
|
static bool
|
2020-10-23 16:34:58 -04:00
|
|
|
view_state_set_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp)
|
2006-01-27 22:23:17 -05:00
|
|
|
{
|
2020-10-05 14:14:55 -04:00
|
|
|
jsid id = hid.get();
|
2019-02-10 15:00:37 -05:00
|
|
|
|
2006-11-24 01:50:12 -05:00
|
|
|
struct view_state *vs;
|
|
|
|
|
2006-12-03 04:14:22 -05:00
|
|
|
/* 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. */
|
2020-10-12 12:43:56 -04:00
|
|
|
if (!JS_InstanceOf(ctx, hobj, (JSClass *) &view_state_class, NULL))
|
|
|
|
return false;
|
2006-11-25 01:54:58 -05:00
|
|
|
|
2020-10-12 12:43:56 -04:00
|
|
|
vs = JS_GetInstancePrivate(ctx, hobj,
|
2007-05-27 11:32:53 -04:00
|
|
|
(JSClass *) &view_state_class, NULL);
|
2020-10-12 12:43:56 -04:00
|
|
|
if (!vs) return false;
|
2006-01-27 22:23:17 -05:00
|
|
|
|
2011-04-19 16:41:05 -04:00
|
|
|
if (!JSID_IS_INT(id))
|
2020-10-12 12:43:56 -04:00
|
|
|
return false;
|
2006-01-27 22:23:17 -05:00
|
|
|
|
2011-04-19 16:41:05 -04:00
|
|
|
switch (JSID_TO_INT(id)) {
|
2006-01-27 22:23:17 -05:00
|
|
|
case VIEW_STATE_PLAIN: {
|
2020-10-12 12:43:56 -04:00
|
|
|
vs->plain = hvp.toInt32();
|
2006-01-27 22:23:17 -05:00
|
|
|
|
2020-10-12 12:43:56 -04:00
|
|
|
return true;
|
2006-01-27 22:23:17 -05:00
|
|
|
}
|
|
|
|
default:
|
2007-05-27 11:36:31 -04:00
|
|
|
/* Unrecognized integer property ID; someone is using
|
|
|
|
* the object as an array. SMJS builtin classes (e.g.
|
2020-10-12 12:43:56 -04:00
|
|
|
* js_RegExpClass) just return true in this case.
|
2006-12-03 05:07:07 -05:00
|
|
|
* Do the same here. */
|
2020-10-12 12:43:56 -04:00
|
|
|
return true;
|
2006-01-27 22:23:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-13 01:14:01 -05:00
|
|
|
/** Pointed to by view_state_class.finalize. SpiderMonkey automatically
|
|
|
|
* finalizes all objects before it frees the JSRuntime, so view_state.jsobject
|
|
|
|
* won't be left dangling. */
|
|
|
|
static void
|
2019-02-10 15:00:37 -05:00
|
|
|
view_state_finalize(JSFreeOp *op, JSObject *obj)
|
2011-11-13 01:14:01 -05:00
|
|
|
{
|
|
|
|
struct view_state *vs;
|
2019-02-10 15:00:37 -05:00
|
|
|
#if 0
|
2011-11-13 01:14:01 -05:00
|
|
|
assert(JS_InstanceOf(ctx, obj, (JSClass *) &view_state_class, NULL));
|
|
|
|
if_assert_failed return;
|
|
|
|
|
|
|
|
vs = JS_GetInstancePrivate(ctx, obj,
|
|
|
|
(JSClass *) &view_state_class, NULL);
|
2019-02-10 15:00:37 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
vs = JS_GetPrivate(obj);
|
2011-11-13 01:14:01 -05:00
|
|
|
|
|
|
|
if (!vs) return; /* already detached */
|
|
|
|
|
2019-02-10 15:00:37 -05:00
|
|
|
JS_SetPrivate(obj, NULL); /* perhaps not necessary */
|
2011-11-13 01:14:01 -05:00
|
|
|
assert(vs->jsobject == obj);
|
|
|
|
if_assert_failed return;
|
|
|
|
vs->jsobject = NULL;
|
|
|
|
}
|
|
|
|
|
2006-01-27 22:23:17 -05:00
|
|
|
|
2011-11-13 01:14:01 -05:00
|
|
|
/** Return an SMJS object through which scripts can access @a vs. If there
|
|
|
|
* already is such an object, return that; otherwise create a new one. */
|
2006-01-27 22:23:17 -05:00
|
|
|
JSObject *
|
|
|
|
smjs_get_view_state_object(struct view_state *vs)
|
|
|
|
{
|
|
|
|
JSObject *view_state_object;
|
|
|
|
|
2011-11-13 01:14:01 -05:00
|
|
|
if (vs->jsobject) return vs->jsobject;
|
|
|
|
|
2006-01-27 22:23:17 -05:00
|
|
|
assert(smjs_ctx);
|
2011-11-13 01:14:01 -05:00
|
|
|
if_assert_failed return NULL;
|
2006-01-27 22:23:17 -05:00
|
|
|
|
|
|
|
view_state_object = JS_NewObject(smjs_ctx,
|
2020-10-16 13:54:02 -04:00
|
|
|
(JSClass *) &view_state_class);
|
2006-01-27 22:23:17 -05:00
|
|
|
|
|
|
|
if (!view_state_object) return NULL;
|
|
|
|
|
2020-10-12 12:43:56 -04:00
|
|
|
JS::RootedObject r_view_state_object(smjs_ctx, view_state_object);
|
|
|
|
|
|
|
|
if (false == JS_DefineProperties(smjs_ctx, r_view_state_object,
|
2006-01-27 22:23:17 -05:00
|
|
|
(JSPropertySpec *) view_state_props))
|
|
|
|
return NULL;
|
|
|
|
|
2011-11-13 01:14:01 -05:00
|
|
|
/* Do this last, so that if any previous step fails, we can
|
|
|
|
* just forget the object and its finalizer won't attempt to
|
|
|
|
* access @vs. */
|
2019-02-10 15:00:37 -05:00
|
|
|
JS_SetPrivate(view_state_object, vs); /* to @view_state_class */
|
2011-11-13 01:14:01 -05:00
|
|
|
|
|
|
|
vs->jsobject = view_state_object;
|
2006-01-27 22:23:17 -05:00
|
|
|
return view_state_object;
|
|
|
|
}
|
2006-06-10 14:02:10 -04:00
|
|
|
|
2020-10-12 12:43:56 -04:00
|
|
|
static bool
|
2020-10-27 09:53:24 -04:00
|
|
|
smjs_elinks_get_view_state(JSContext *ctx, unsigned int argc, JS::Value *vp)
|
2006-06-10 14:02:10 -04:00
|
|
|
{
|
2020-10-27 09:53:24 -04:00
|
|
|
JS::CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
JS::RootedObject hobj(ctx, &args.thisv().toObject());
|
2006-06-10 14:02:10 -04:00
|
|
|
|
2020-10-27 09:53:24 -04:00
|
|
|
args.rval().setNull();
|
2006-06-10 14:02:10 -04:00
|
|
|
|
2020-10-12 12:43:56 -04:00
|
|
|
if (!smjs_ses || !have_location(smjs_ses)) return true;
|
2006-06-10 14:02:10 -04:00
|
|
|
|
2020-10-27 09:53:24 -04:00
|
|
|
struct view_state *vs = &cur_loc(smjs_ses)->vs;
|
2020-10-12 12:43:56 -04:00
|
|
|
if (!vs) return true;
|
2006-06-10 14:02:10 -04:00
|
|
|
|
2020-10-27 09:53:24 -04:00
|
|
|
JSObject *vs_obj = smjs_get_view_state_object(vs);
|
2020-10-12 12:43:56 -04:00
|
|
|
if (!vs_obj) return true;
|
2006-06-10 14:02:10 -04:00
|
|
|
|
2020-10-27 09:53:24 -04:00
|
|
|
args.rval().setObject(*vs_obj);
|
2006-06-10 14:02:10 -04:00
|
|
|
|
2020-10-12 12:43:56 -04:00
|
|
|
return true;
|
2006-06-10 14:02:10 -04:00
|
|
|
}
|
|
|
|
|
2011-11-13 01:14:01 -05:00
|
|
|
/** Ensure that no JSObject contains the pointer @a vs. This is called from
|
|
|
|
* destroy_vs. If a JSObject was previously attached to the view state, the
|
|
|
|
* object will remain in memory but it will no longer be able to access the
|
|
|
|
* view state. */
|
|
|
|
void
|
|
|
|
smjs_detach_view_state_object(struct view_state *vs)
|
|
|
|
{
|
|
|
|
assert(smjs_ctx);
|
|
|
|
assert(vs);
|
|
|
|
if_assert_failed return;
|
|
|
|
|
|
|
|
if (!vs->jsobject) return;
|
|
|
|
|
2020-10-12 12:43:56 -04:00
|
|
|
JS::RootedObject r_vs_jsobject(smjs_ctx, vs->jsobject);
|
|
|
|
|
|
|
|
assert(JS_GetInstancePrivate(smjs_ctx, r_vs_jsobject,
|
2011-11-13 01:14:01 -05:00
|
|
|
(JSClass *) &view_state_class, NULL)
|
|
|
|
== vs);
|
|
|
|
if_assert_failed {}
|
|
|
|
|
2019-02-10 15:00:37 -05:00
|
|
|
JS_SetPrivate(vs->jsobject, NULL);
|
2011-11-13 01:14:01 -05:00
|
|
|
vs->jsobject = NULL;
|
|
|
|
}
|
|
|
|
|
2006-06-10 14:02:10 -04:00
|
|
|
void
|
|
|
|
smjs_init_view_state_interface(void)
|
|
|
|
{
|
|
|
|
if (!smjs_ctx || !smjs_elinks_object)
|
|
|
|
return;
|
|
|
|
|
2020-10-12 12:43:56 -04:00
|
|
|
JS::RootedObject r_smjs_elinks_object(smjs_ctx, smjs_elinks_object);
|
|
|
|
|
|
|
|
JS_DefineProperty(smjs_ctx, r_smjs_elinks_object, "vs", (int32_t)0,
|
2021-08-26 09:44:32 -04:00
|
|
|
(unsigned int)(JSPROP_ENUMERATE | JSPROP_PERMANENT | JSPROP_READONLY), smjs_elinks_get_view_state
|
2020-10-12 12:43:56 -04:00
|
|
|
);
|
2006-06-10 14:02:10 -04:00
|
|
|
}
|