1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-12-04 14:46:47 -05:00

1031: Add spidermonkey-shared.c used for both web and user scripts

Rename src/ecmascript/spidermonkey/util.c to
src/ecmascript/spidermonkey-shared.c and compile it also when
CONFIG_SCRIPTING_SMJS is enabled but CONFIG_ECMASCRIPT_SPIDERMONKEY is
not.  Then use its functions from src/scripting/smjs/ too.  Move the
corresponding declarations, as well as the inline functions needed by
src/scripting/smjs/, from src/ecmascript/spidermonkey/util.h to
src/ecmascript/spidermonkey-shared.h.

ELinks is nowadays using two JSRuntimes and SpiderMonkey has bugs that
make it crash in such use.  To work around them, ELinks will need to
be changed to use only one JSRuntime.  I am planning to define and
initialize that JSRuntime in src/ecmascript/spidermonkey-shared.c,
now that it's compiled whenever either of the modules is enabled.
This commit is contained in:
Kalle Olavi Niemitalo 2008-07-16 12:32:24 +03:00 committed by Kalle Olavi Niemitalo
parent f479d6e82a
commit 32889bf908
21 changed files with 117 additions and 99 deletions

View File

@ -8,6 +8,16 @@ SUBDIRS-$(CONFIG_ECMASCRIPT_SMJS) += spidermonkey
OBJS-$(CONFIG_ECMASCRIPT_SEE) += see.o
OBJS-$(CONFIG_ECMASCRIPT_SMJS) += spidermonkey.o
ifeq ($(CONFIG_ECMASCRIPT_SMJS), yes)
CONFIG_ANY_SPIDERMONKEY = yes
else ifeq ($(CONFIG_SCRIPTING_SPIDERMONKEY), yes)
CONFIG_ANY_SPIDERMONKEY = yes
else
CONFIG_ANY_SPIDERMONKEY = no
endif
OBJS-$(CONFIG_ANY_SPIDERMONKEY) += spidermonkey-shared.o
OBJS = ecmascript.o
include $(top_srcdir)/Makefile.lib

View File

@ -1,4 +1,5 @@
/* Better compatibility across versions of SpiderMonkey. */
/** SpiderMonkey support for both user scripts and web scripts.
* @file */
#ifdef HAVE_CONFIG_H
#include "config.h"
@ -6,7 +7,7 @@
#include "elinks.h"
#include "ecmascript/spidermonkey/util.h"
#include "ecmascript/spidermonkey-shared.h"
/** An ELinks-specific replacement for JS_DefineFunctions().
*

View File

@ -0,0 +1,75 @@
#ifndef EL__ECMASCRIPT_SPIDERMONKEY_SHARED_H
#define EL__ECMASCRIPT_SPIDERMONKEY_SHARED_H
/* Inconsistently applied conventions for prefixes in identifiers:
* - "smjs" for user scripts (scripting/smjs/).
* - "js" for web scripts (ecmascript/spidermonkey/).
* - "spidermonkey" for common stuff, especially any that replace
* similarly named things defined in SpiderMonkey itself. */
/* For wild SpiderMonkey installations. */
#ifdef CONFIG_OS_BEOS
#define XP_BEOS
#elif CONFIG_OS_OS2
#define XP_OS2
#elif CONFIG_OS_RISCOS
#error Out of luck, buddy!
#elif CONFIG_OS_UNIX
#define XP_UNIX
#elif CONFIG_OS_WIN32
#define XP_WIN
#endif
#include <jsapi.h>
#include "util/string.h"
/** An ELinks-specific replacement for JSFunctionSpec.
*
* Bug 1016: In SpiderMonkey 1.7 bundled with XULRunner 1.8, jsapi.h
* defines JSFunctionSpec in different ways depending on whether
* MOZILLA_1_8_BRANCH is defined, and there is no obvious way for
* ELinks to check whether MOZILLA_1_8_BRANCH was defined when the
* library was built. Avoid the unstable JSFunctionSpec definitions
* and use this ELinks-specific structure instead. */
typedef struct spidermonkeyFunctionSpec {
const char *name;
JSNative call;
uint8 nargs;
/* ELinks does not use "flags" and "extra" so omit them here. */
} spidermonkeyFunctionSpec;
JSBool spidermonkey_DefineFunctions(JSContext *cx, JSObject *obj,
const spidermonkeyFunctionSpec *fs);
JSObject *spidermonkey_InitClass(JSContext *cx, JSObject *obj,
JSObject *parent_proto, JSClass *clasp,
JSNative constructor, uintN nargs,
JSPropertySpec *ps,
const spidermonkeyFunctionSpec *fs,
JSPropertySpec *static_ps,
const spidermonkeyFunctionSpec *static_fs);
static void undef_to_jsval(JSContext *ctx, jsval *vp);
static unsigned char *jsval_to_string(JSContext *ctx, jsval *vp);
/* Inline functions */
static inline void
undef_to_jsval(JSContext *ctx, jsval *vp)
{
*vp = JSVAL_NULL;
}
static inline unsigned char *
jsval_to_string(JSContext *ctx, jsval *vp)
{
jsval val;
if (JS_ConvertValue(ctx, *vp, JSTYPE_STRING, &val) == JS_FALSE) {
return "";
}
return empty_string_or_(JS_GetStringBytes(JS_ValueToString(ctx, val)));
}
#endif

View File

@ -2,6 +2,6 @@ top_builddir=../../..
include $(top_builddir)/Makefile.config
INCLUDES += $(SPIDERMONKEY_CFLAGS)
OBJS = document.o form.o location.o navigator.o unibar.o util.o window.o
OBJS = document.o form.o location.o navigator.o unibar.o window.o
include $(top_srcdir)/Makefile.lib

View File

@ -2,32 +2,16 @@
#ifndef EL__ECMASCRIPT_SPIDERMONKEY_UTIL_H
#define EL__ECMASCRIPT_SPIDERMONKEY_UTIL_H
/* For wild SpiderMonkey installations. */
#ifdef CONFIG_OS_BEOS
#define XP_BEOS
#elif CONFIG_OS_OS2
#define XP_OS2
#elif CONFIG_OS_RISCOS
#error Out of luck, buddy!
#elif CONFIG_OS_UNIX
#define XP_UNIX
#elif CONFIG_OS_WIN32
#define XP_WIN
#endif
#include <jsapi.h>
#include "ecmascript/spidermonkey-shared.h"
#include "util/memory.h"
#include "util/string.h"
static void string_to_jsval(JSContext *ctx, jsval *vp, unsigned char *string);
static void astring_to_jsval(JSContext *ctx, jsval *vp, unsigned char *string);
static void int_to_jsval(JSContext *ctx, jsval *vp, int number);
static void object_to_jsval(JSContext *ctx, jsval *vp, JSObject *object);
static void boolean_to_jsval(JSContext *ctx, jsval *vp, int boolean);
static void undef_to_jsval(JSContext *ctx, jsval *vp);
static int jsval_to_boolean(JSContext *ctx, jsval *vp);
static unsigned char *jsval_to_string(JSContext *ctx, jsval *vp);
@ -68,12 +52,6 @@ boolean_to_jsval(JSContext *ctx, jsval *vp, int boolean)
*vp = BOOLEAN_TO_JSVAL(boolean);
}
static inline void
undef_to_jsval(JSContext *ctx, jsval *vp)
{
*vp = JSVAL_NULL;
}
static inline int
jsval_to_boolean(JSContext *ctx, jsval *vp)
@ -87,41 +65,4 @@ jsval_to_boolean(JSContext *ctx, jsval *vp)
return JSVAL_TO_BOOLEAN(val);
}
static inline unsigned char *
jsval_to_string(JSContext *ctx, jsval *vp)
{
jsval val;
if (JS_ConvertValue(ctx, *vp, JSTYPE_STRING, &val) == JS_FALSE) {
return "";
}
return empty_string_or_(JS_GetStringBytes(JS_ValueToString(ctx, val)));
}
/** An ELinks-specific replacement for JSFunctionSpec.
*
* Bug 1016: In SpiderMonkey 1.7 bundled with XULRunner 1.8, jsapi.h
* defines JSFunctionSpec in different ways depending on whether
* MOZILLA_1_8_BRANCH is defined, and there is no obvious way for
* ELinks to check whether MOZILLA_1_8_BRANCH was defined when the
* library was built. Avoid the unstable JSFunctionSpec definitions
* and use this ELinks-specific structure instead. */
typedef struct spidermonkeyFunctionSpec {
const char *name;
JSNative call;
uint8 nargs;
/* ELinks does not use "flags" and "extra" so omit them here. */
} spidermonkeyFunctionSpec;
JSBool spidermonkey_DefineFunctions(JSContext *cx, JSObject *obj,
const spidermonkeyFunctionSpec *fs);
JSObject *spidermonkey_InitClass(JSContext *cx, JSObject *obj,
JSObject *parent_proto, JSClass *clasp,
JSNative constructor, uintN nargs,
JSPropertySpec *ps,
const spidermonkeyFunctionSpec *fs,
JSPropertySpec *static_ps,
const spidermonkeyFunctionSpec *static_fs);
#endif

View File

@ -7,7 +7,7 @@
#include "elinks.h"
#include "config/kbdbind.h"
#include "ecmascript/spidermonkey/util.h"
#include "ecmascript/spidermonkey-shared.h"
#include "scripting/smjs/core.h"
#include "scripting/smjs/elinks_object.h"
#include "session/session.h"

View File

@ -7,7 +7,7 @@
#include "elinks.h"
#include "bookmarks/bookmarks.h"
#include "ecmascript/spidermonkey/util.h"
#include "ecmascript/spidermonkey-shared.h"
#include "main/event.h"
#include "scripting/smjs/core.h"
#include "scripting/smjs/elinks_object.h"

View File

@ -1,7 +1,7 @@
#ifndef EL__SCRIPTING_SMJS_BOOKMARKS_H
#define EL__SCRIPTING_SMJS_BOOKMARKS_H
#include "ecmascript/spidermonkey/util.h"
#include "ecmascript/spidermonkey-shared.h"
void smjs_init_bookmarks_interface(void);

View File

@ -7,7 +7,7 @@
#include "elinks.h"
#include "cache/cache.h"
#include "ecmascript/spidermonkey/util.h"
#include "ecmascript/spidermonkey-shared.h"
#include "protocol/uri.h"
#include "scripting/smjs/cache_object.h"
#include "scripting/smjs/core.h"

View File

@ -7,7 +7,7 @@
#include "elinks.h"
#include "config/home.h"
#include "ecmascript/spidermonkey/util.h"
#include "ecmascript/spidermonkey-shared.h"
#include "main/module.h"
#include "osdep/osdep.h"
#include "scripting/scripting.h"

View File

@ -1,7 +1,7 @@
#ifndef EL__SCRIPTING_SMJS_CORE_H
#define EL__SCRIPTING_SMJS_CORE_H
#include "ecmascript/spidermonkey/util.h"
#include "ecmascript/spidermonkey-shared.h"
struct module;
struct session;

View File

@ -8,7 +8,7 @@
#include "bfu/msgbox.h"
#include "config/home.h"
#include "ecmascript/spidermonkey/util.h"
#include "ecmascript/spidermonkey-shared.h"
#include "intl/gettext/libintl.h"
#include "protocol/uri.h"
#include "scripting/scripting.h"
@ -69,7 +69,7 @@ elinks_set_location(JSContext *ctx, JSObject *obj, jsval id, jsval *vp)
return JS_TRUE;
}
/* function "alert" in the object returned by smjs_get_elinks_object() */
/* @elinks_funcs{"alert"} */
static JSBool
elinks_alert(JSContext *ctx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
@ -90,7 +90,7 @@ elinks_alert(JSContext *ctx, JSObject *obj, uintN argc, jsval *argv, jsval *rval
return JS_TRUE;
}
/* function "execute" in the object returned by smjs_get_elinks_object() */
/* @elinks_funcs{"execute"} */
static JSBool
elinks_execute(JSContext *ctx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
@ -117,6 +117,12 @@ static const JSClass elinks_class = {
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub
};
static const spidermonkeyFunctionSpec elinks_funcs[] = {
{ "alert", elinks_alert, 1 },
{ "execute", elinks_execute, 1 },
{ NULL }
};
static JSObject *
smjs_get_elinks_object(void)
{
@ -125,24 +131,9 @@ smjs_get_elinks_object(void)
assert(smjs_ctx);
assert(smjs_global_object);
jsobj = JS_InitClass(smjs_ctx, smjs_global_object, NULL,
(JSClass *) &elinks_class, NULL, 0, NULL,
(JSFunctionSpec *) NULL, NULL, NULL);
/* Bug 1016: In SpiderMonkey 1.7 bundled with XULRunner 1.8,
* jsapi.h defines JSFunctionSpec in different ways depending
* on whether MOZILLA_1_8_BRANCH is defined, and there is no
* obvious way for ELinks to check whether MOZILLA_1_8_BRANCH
* was defined when the library was built. Avoid the unstable
* JSFunctionSpec definitions and instead use JS_DefineFunction
* directly.
*
* In elinks/src/ecmascript/spidermonkey/, there is an
* ELinks-specific replacement for JSFunctionSpec; however, to
* keep the modules independent, elinks/src/scripting/smjs/
* does not use that. */
JS_DefineFunction(smjs_ctx, jsobj, "alert", elinks_alert, 1, 0);
JS_DefineFunction(smjs_ctx, jsobj, "execute", elinks_execute, 1, 0);
jsobj = spidermonkey_InitClass(smjs_ctx, smjs_global_object, NULL,
(JSClass *) &elinks_class, NULL, 0, NULL,
elinks_funcs, NULL, NULL);
JS_DefineProperty(smjs_ctx, jsobj, "location", JSVAL_NULL,
elinks_get_location, elinks_set_location,

View File

@ -1,7 +1,7 @@
#ifndef EL__SCRIPTING_SMJS_ELINKS_OBJECT_H
#define EL__SCRIPTING_SMJS_ELINKS_OBJECT_H
#include "ecmascript/spidermonkey/util.h"
#include "ecmascript/spidermonkey-shared.h"
/* This is the all-powerful elinks object through which all client scripts
* will interface with ELinks. */

View File

@ -6,7 +6,7 @@
#include "elinks.h"
#include "ecmascript/spidermonkey/util.h"
#include "ecmascript/spidermonkey-shared.h"
#include "scripting/scripting.h"
#include "scripting/smjs/core.h"
#include "scripting/smjs/global_object.h"

View File

@ -1,7 +1,7 @@
#ifndef EL__SCRIPTING_SMJS_GLOBAL_OBJECT_H
#define EL__SCRIPTING_SMJS_GLOBAL_OBJECT_H
#include "ecmascript/spidermonkey/util.h"
#include "ecmascript/spidermonkey-shared.h"
/* The root of the object hierarchy. If object 'foo' has this as its parent,
* you can use foo's method and properties with 'foo.<method|property>'. */

View File

@ -7,7 +7,7 @@
#include "elinks.h"
#include "globhist/globhist.h"
#include "ecmascript/spidermonkey/util.h"
#include "ecmascript/spidermonkey-shared.h"
#include "scripting/smjs/core.h"
#include "scripting/smjs/elinks_object.h"
#include "util/memory.h"

View File

@ -7,7 +7,7 @@
#include "elinks.h"
#include "cache/cache.h"
#include "ecmascript/spidermonkey/util.h"
#include "ecmascript/spidermonkey-shared.h"
#include "protocol/uri.h"
#include "main/event.h"
#include "main/module.h"

View File

@ -7,7 +7,7 @@
#include "elinks.h"
#include "config/kbdbind.h"
#include "ecmascript/spidermonkey/util.h"
#include "ecmascript/spidermonkey-shared.h"
#include "main/event.h"
#include "scripting/smjs/core.h"
#include "scripting/smjs/elinks_object.h"

View File

@ -1,7 +1,7 @@
#ifndef EL__SCRIPTING_SMJS_KEYBINDING_H
#define EL__SCRIPTING_SMJS_KEYBINDING_H
#include "ecmascript/spidermonkey/util.h"
#include "ecmascript/spidermonkey-shared.h"
void smjs_init_keybinding_interface(void);

View File

@ -6,7 +6,7 @@
#include "elinks.h"
#include "ecmascript/spidermonkey/util.h"
#include "ecmascript/spidermonkey-shared.h"
#include "network/connection.h"
#include "protocol/uri.h"
#include "scripting/smjs/core.h"

View File

@ -8,7 +8,7 @@
#include "elinks.h"
#include "ecmascript/spidermonkey/util.h"
#include "ecmascript/spidermonkey-shared.h"
#include "protocol/uri.h"
#include "scripting/smjs/elinks_object.h"
#include "scripting/smjs/view_state_object.h"