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"
|
|
|
|
|
2019-04-21 06:27:40 -04:00
|
|
|
static const JSClass view_state_class; /* defined below */
|
2006-11-25 01:54:58 -05:00
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
static const JSPropertySpec view_state_props[] = {
|
|
|
|
{ "plain", VIEW_STATE_PLAIN, JSPROP_ENUMERATE },
|
|
|
|
{ "uri", VIEW_STATE_URI, JSPROP_ENUMERATE | JSPROP_READONLY },
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2006-11-23 16:33:43 -05:00
|
|
|
/* @view_state_class.getProperty */
|
2006-01-27 22:23:17 -05:00
|
|
|
static JSBool
|
2019-02-10 15:00:37 -05:00
|
|
|
view_state_get_property(JSContext *ctx, JSHandleObject hobj, JSHandleId hid, JSMutableHandleValue hvp)
|
2006-01-27 22:23:17 -05:00
|
|
|
{
|
2019-02-10 15:00:37 -05:00
|
|
|
ELINKS_CAST_PROP_PARAMS
|
2019-04-21 06:27:40 -04:00
|
|
|
jsid id = *(hid._);
|
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. */
|
|
|
|
if (!JS_InstanceOf(ctx, obj, (JSClass *) &view_state_class, NULL))
|
|
|
|
return JS_FALSE;
|
2006-11-25 01:54:58 -05:00
|
|
|
|
2007-05-27 11:32:53 -04:00
|
|
|
vs = JS_GetInstancePrivate(ctx, obj,
|
|
|
|
(JSClass *) &view_state_class, NULL);
|
2011-11-13 01:14:01 -05:00
|
|
|
if (!vs) return JS_FALSE;
|
2006-01-27 22:23:17 -05:00
|
|
|
|
2019-04-21 06:26:27 -04:00
|
|
|
undef_to_jsval(ctx, vp);
|
2006-01-27 22:23:17 -05:00
|
|
|
|
2011-04-19 16:41:05 -04:00
|
|
|
if (!JSID_IS_INT(id))
|
2006-01-27 22:23:17 -05:00
|
|
|
return JS_FALSE;
|
|
|
|
|
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:
|
2019-04-21 06:26:27 -04:00
|
|
|
*vp = INT_TO_JSVAL(vs->plain);
|
2006-01-27 22:23:17 -05:00
|
|
|
|
|
|
|
return JS_TRUE;
|
|
|
|
case VIEW_STATE_URI:
|
2019-04-21 06:26:27 -04:00
|
|
|
*vp = STRING_TO_JSVAL(JS_NewStringCopyZ(smjs_ctx,
|
2006-01-27 22:23:17 -05:00
|
|
|
struri(vs->uri)));
|
|
|
|
|
|
|
|
return JS_TRUE;
|
|
|
|
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.
|
2006-12-03 05:07:07 -05:00
|
|
|
* js_RegExpClass) just return JS_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;
|
2006-01-27 22:23:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-11-23 16:33:43 -05:00
|
|
|
/* @view_state_class.setProperty */
|
2006-01-27 22:23:17 -05:00
|
|
|
static JSBool
|
2019-02-10 15:00:37 -05:00
|
|
|
view_state_set_property(JSContext *ctx, JSHandleObject hobj, JSHandleId hid, JSBool strict, JSMutableHandleValue hvp)
|
2006-01-27 22:23:17 -05:00
|
|
|
{
|
2019-02-10 15:00:37 -05:00
|
|
|
ELINKS_CAST_PROP_PARAMS
|
2019-04-21 06:27:40 -04:00
|
|
|
jsid id = *(hid._);
|
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. */
|
|
|
|
if (!JS_InstanceOf(ctx, obj, (JSClass *) &view_state_class, NULL))
|
|
|
|
return JS_FALSE;
|
2006-11-25 01:54:58 -05:00
|
|
|
|
2007-05-27 11:32:53 -04:00
|
|
|
vs = JS_GetInstancePrivate(ctx, obj,
|
|
|
|
(JSClass *) &view_state_class, NULL);
|
2011-11-13 01:14:01 -05:00
|
|
|
if (!vs) return JS_FALSE;
|
2006-01-27 22:23:17 -05:00
|
|
|
|
2011-04-19 16:41:05 -04:00
|
|
|
if (!JSID_IS_INT(id))
|
2006-01-27 22:23:17 -05:00
|
|
|
return JS_FALSE;
|
|
|
|
|
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: {
|
2019-04-21 06:26:27 -04:00
|
|
|
vs->plain = atol(jsval_to_string(ctx, vp));
|
2006-01-27 22:23:17 -05:00
|
|
|
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
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.
|
2006-12-03 05:07:07 -05:00
|
|
|
* js_RegExpClass) just return JS_TRUE in this case.
|
|
|
|
* Do the same here. */
|
|
|
|
return JS_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;
|
|
|
|
}
|
|
|
|
|
2019-04-21 06:27:40 -04:00
|
|
|
static const JSClass view_state_class = {
|
|
|
|
"view_state",
|
|
|
|
JSCLASS_HAS_PRIVATE, /* struct view_state * */
|
|
|
|
JS_PropertyStub, JS_PropertyStub,
|
|
|
|
view_state_get_property, view_state_set_property,
|
|
|
|
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, view_state_finalize
|
|
|
|
};
|
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,
|
|
|
|
(JSClass *) &view_state_class,
|
|
|
|
NULL, NULL);
|
|
|
|
|
|
|
|
if (!view_state_object) return NULL;
|
|
|
|
|
|
|
|
if (JS_FALSE == JS_DefineProperties(smjs_ctx, view_state_object,
|
|
|
|
(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
|
|
|
|
|
|
|
static JSBool
|
2019-02-10 15:00:37 -05:00
|
|
|
smjs_elinks_get_view_state(JSContext *ctx, JSHandleObject hobj, JSHandleId hid, JSMutableHandleValue hvp)
|
2006-06-10 14:02:10 -04:00
|
|
|
{
|
2019-02-10 15:00:37 -05:00
|
|
|
ELINKS_CAST_PROP_PARAMS
|
|
|
|
(void)obj;
|
|
|
|
|
2006-06-10 14:02:10 -04:00
|
|
|
JSObject *vs_obj;
|
|
|
|
struct view_state *vs;
|
|
|
|
|
2019-04-21 06:26:27 -04:00
|
|
|
*vp = JSVAL_NULL;
|
2006-06-10 14:02:10 -04:00
|
|
|
|
2006-06-10 22:33:41 -04:00
|
|
|
if (!smjs_ses || !have_location(smjs_ses)) return JS_TRUE;
|
2006-06-10 14:02:10 -04:00
|
|
|
|
|
|
|
vs = &cur_loc(smjs_ses)->vs;
|
|
|
|
if (!vs) return JS_TRUE;
|
|
|
|
|
|
|
|
vs_obj = smjs_get_view_state_object(vs);
|
|
|
|
if (!vs_obj) return JS_TRUE;
|
|
|
|
|
2019-04-21 06:26:27 -04:00
|
|
|
*vp = OBJECT_TO_JSVAL(vs_obj);
|
2006-06-10 14:02:10 -04:00
|
|
|
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
assert(JS_GetInstancePrivate(smjs_ctx, vs->jsobject,
|
|
|
|
(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;
|
|
|
|
|
|
|
|
JS_DefineProperty(smjs_ctx, smjs_elinks_object, "vs", JSVAL_NULL,
|
2011-04-19 16:41:05 -04:00
|
|
|
smjs_elinks_get_view_state, JS_StrictPropertyStub,
|
2006-06-10 14:02:10 -04:00
|
|
|
JSPROP_ENUMERATE | JSPROP_PERMANENT | JSPROP_READONLY);
|
|
|
|
}
|