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);
|
2022-10-01 15:08:03 -04:00
|
|
|
static void view_state_finalize(JS::GCContext *op, JSObject *obj);
|
2020-10-05 14:14:55 -04:00
|
|
|
|
2020-10-27 09:53:24 -04:00
|
|
|
static const JSClassOps view_state_ops = {
|
2021-08-27 13:46:05 -04:00
|
|
|
nullptr, // addProperty
|
|
|
|
nullptr, // deleteProperty
|
|
|
|
nullptr, // enumerate
|
|
|
|
nullptr, // newEnumerate
|
|
|
|
nullptr, // resolve
|
|
|
|
nullptr, // mayResolve
|
2022-06-19 15:39:47 -04:00
|
|
|
view_state_finalize, // finalize
|
2021-08-27 13:46:05 -04:00
|
|
|
nullptr, // call
|
|
|
|
nullptr, // construct
|
|
|
|
nullptr // trace JS_GlobalObjectTraceHook
|
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",
|
2022-10-01 15:08:03 -04:00
|
|
|
JSCLASS_HAS_RESERVED_SLOTS(1), /* struct view_state * */
|
2020-10-27 09:53:24 -04:00
|
|
|
&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;
|
|
|
|
|
2022-10-01 15:08:03 -04:00
|
|
|
vs = JS::GetMaybePtrFromReservedSlot<struct view_state>(hobj, 0);
|
2022-06-19 15:39:47 -04:00
|
|
|
|
2020-10-16 13:54:02 -04:00
|
|
|
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;
|
|
|
|
|
2022-10-01 15:08:03 -04:00
|
|
|
vs = JS::GetMaybePtrFromReservedSlot<struct view_state>(hobj, 0);
|
2022-06-19 15:39:47 -04:00
|
|
|
|
2020-10-16 13:54:02 -04:00
|
|
|
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;
|
|
|
|
|
2022-10-01 15:08:03 -04:00
|
|
|
struct view_state *vs = JS::GetMaybePtrFromReservedSlot<struct view_state>(hobj, 0);
|
2022-06-19 15:39:47 -04:00
|
|
|
|
2020-10-16 13:54:02 -04:00
|
|
|
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
|
|
|
};
|
|
|
|
|
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
|
2022-10-01 15:08:03 -04:00
|
|
|
view_state_finalize(JS::GCContext *op, JSObject *obj)
|
2011-11-13 01:14:01 -05:00
|
|
|
{
|
|
|
|
struct view_state *vs;
|
2019-02-10 15:00:37 -05:00
|
|
|
|
2022-10-01 15:08:03 -04:00
|
|
|
vs = JS::GetMaybePtrFromReservedSlot<struct view_state>(obj, 0);
|
2011-11-13 01:14:01 -05:00
|
|
|
|
|
|
|
if (!vs) return; /* already detached */
|
|
|
|
assert(vs->jsobject == obj);
|
|
|
|
if_assert_failed return;
|
2022-06-19 15:39:47 -04:00
|
|
|
|
2022-10-01 15:08:03 -04:00
|
|
|
JS::SetReservedSlot(obj, 0, JS::UndefinedValue()); /* perhaps not necessary */
|
2011-11-13 01:14:01 -05:00
|
|
|
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. */
|
|
|
|
|
2022-10-01 15:08:03 -04:00
|
|
|
JS::SetReservedSlot(view_state_object, 0, JS::PrivateValue(vs)); /* to @view_state_class */
|
2011-11-13 01:14:01 -05:00
|
|
|
vs->jsobject = view_state_object;
|
2022-06-19 15:39:47 -04:00
|
|
|
|
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;
|
2006-06-10 14:02:10 -04:00
|
|
|
|
2022-06-19 15:39:47 -04:00
|
|
|
if (!vs) return true;
|
2020-10-27 09:53:24 -04:00
|
|
|
JSObject *vs_obj = smjs_get_view_state_object(vs);
|
2006-06-10 14:02:10 -04:00
|
|
|
|
2022-06-19 15:39:47 -04:00
|
|
|
if (!vs_obj) return true;
|
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;
|
|
|
|
|
2022-06-19 15:39:47 -04:00
|
|
|
JS::RootedObject robj(smjs_ctx, vs->jsobject);
|
|
|
|
|
|
|
|
if (!JS_InstanceOf(smjs_ctx, robj, (JSClass *) &view_state_class, NULL))
|
|
|
|
return;
|
2020-10-12 12:43:56 -04:00
|
|
|
|
2022-10-01 15:08:03 -04:00
|
|
|
JS::SetReservedSlot(vs->jsobject, 0, JS::UndefinedValue());
|
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);
|
2022-06-17 09:55:05 -04:00
|
|
|
JS_DefineProperty(smjs_ctx, r_smjs_elinks_object, "vs", smjs_elinks_get_view_state, nullptr,
|
|
|
|
(unsigned int)(JSPROP_ENUMERATE | JSPROP_PERMANENT | JSPROP_READONLY)
|
2020-10-12 12:43:56 -04:00
|
|
|
);
|
2006-06-10 14:02:10 -04:00
|
|
|
}
|