2008-07-16 05:32:24 -04:00
|
|
|
/** SpiderMonkey support for both user scripts and web scripts.
|
|
|
|
* @file */
|
2008-06-16 17:25:59 -04:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "elinks.h"
|
|
|
|
|
2008-07-16 05:32:24 -04:00
|
|
|
#include "ecmascript/spidermonkey-shared.h"
|
2020-10-23 16:34:58 -04:00
|
|
|
#include <js/Initialization.h>
|
2008-06-16 17:25:59 -04:00
|
|
|
|
2008-07-07 08:57:47 -04:00
|
|
|
/** A JSContext that can be used in JS_SetPrivate and JS_GetPrivate
|
2020-10-27 09:53:24 -04:00
|
|
|
- * when no better one is available. This context has no global
|
|
|
|
- * object, so scripts cannot be evaluated in it.
|
|
|
|
- */
|
2008-07-07 08:57:47 -04:00
|
|
|
|
2020-10-28 17:32:20 -04:00
|
|
|
JSContext *main_ctx;
|
|
|
|
|
2008-07-16 07:23:19 -04:00
|
|
|
/** A reference count for ::spidermonkey_runtime so that modules using
|
|
|
|
* it can be initialized and shut down in arbitrary order. */
|
|
|
|
static int spidermonkey_runtime_refcount;
|
|
|
|
|
2008-07-07 08:57:47 -04:00
|
|
|
/** Initialize ::spidermonkey_runtime and ::spidermonkey_empty_context.
|
|
|
|
* If already initialized, just increment the reference count.
|
|
|
|
*
|
2008-07-16 07:23:19 -04:00
|
|
|
* @return 1 if successful or 0 on error. If this succeeds, the
|
|
|
|
* caller must eventually call spidermonkey_runtime_release(). */
|
|
|
|
int
|
|
|
|
spidermonkey_runtime_addref(void)
|
|
|
|
{
|
2008-07-07 08:57:47 -04:00
|
|
|
if (spidermonkey_runtime_refcount == 0) {
|
2008-07-16 07:50:41 -04:00
|
|
|
|
2020-10-11 09:41:27 -04:00
|
|
|
if (!JS_Init()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-09-25 15:43:50 -04:00
|
|
|
main_ctx = JS_NewContext(64 * 1024 * 1024);
|
2020-10-28 17:32:20 -04:00
|
|
|
|
|
|
|
if (!main_ctx) {
|
|
|
|
JS_ShutDown();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!JS::InitSelfHostedCode(main_ctx)) {
|
|
|
|
JS_DestroyContext(main_ctx);
|
2008-07-07 08:57:47 -04:00
|
|
|
JS_ShutDown();
|
2008-09-07 11:04:18 -04:00
|
|
|
return 0;
|
2008-07-07 08:57:47 -04:00
|
|
|
}
|
2008-07-16 07:23:19 -04:00
|
|
|
}
|
2008-07-16 07:50:41 -04:00
|
|
|
|
2008-07-16 07:23:19 -04:00
|
|
|
spidermonkey_runtime_refcount++;
|
|
|
|
assert(spidermonkey_runtime_refcount > 0);
|
|
|
|
if_assert_failed { spidermonkey_runtime_refcount--; return 0; }
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Release a reference to ::spidermonkey_runtime, and destroy it if
|
|
|
|
* that was the last reference. If spidermonkey_runtime_addref()
|
|
|
|
* failed, then this must not be called. */
|
|
|
|
void
|
|
|
|
spidermonkey_runtime_release(void)
|
|
|
|
{
|
|
|
|
assert(spidermonkey_runtime_refcount > 0);
|
|
|
|
|
|
|
|
--spidermonkey_runtime_refcount;
|
|
|
|
if (spidermonkey_runtime_refcount == 0) {
|
2020-10-28 17:32:20 -04:00
|
|
|
JS_DestroyContext(main_ctx);
|
2008-07-16 07:50:41 -04:00
|
|
|
JS_ShutDown();
|
2008-07-16 07:23:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-16 17:25:59 -04:00
|
|
|
/** An ELinks-specific replacement for JS_DefineFunctions().
|
|
|
|
*
|
|
|
|
* @relates spidermonkeyFunctionSpec */
|
2020-10-11 09:41:27 -04:00
|
|
|
bool
|
2008-06-16 17:25:59 -04:00
|
|
|
spidermonkey_DefineFunctions(JSContext *cx, JSObject *obj,
|
|
|
|
const spidermonkeyFunctionSpec *fs)
|
|
|
|
{
|
2020-10-11 09:41:27 -04:00
|
|
|
JS::RootedObject hobj(cx, obj);
|
2008-06-16 17:25:59 -04:00
|
|
|
for (; fs->name; fs++) {
|
2020-10-11 09:41:27 -04:00
|
|
|
if (!JS_DefineFunction(cx, hobj, fs->name, fs->call,
|
2008-06-16 17:25:59 -04:00
|
|
|
fs->nargs, 0))
|
2020-10-11 09:41:27 -04:00
|
|
|
return false;
|
2008-06-16 17:25:59 -04:00
|
|
|
}
|
2020-10-11 09:41:27 -04:00
|
|
|
return true;
|
2008-06-16 17:25:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** An ELinks-specific replacement for JS_InitClass().
|
|
|
|
*
|
|
|
|
* @relates spidermonkeyFunctionSpec */
|
|
|
|
JSObject *
|
|
|
|
spidermonkey_InitClass(JSContext *cx, JSObject *obj,
|
2022-02-25 14:38:38 -05:00
|
|
|
JSObject *parent_proto, const JSClass *clasp,
|
2019-02-10 15:00:37 -05:00
|
|
|
JSNative constructor, unsigned int nargs,
|
2008-06-16 17:25:59 -04:00
|
|
|
JSPropertySpec *ps,
|
|
|
|
const spidermonkeyFunctionSpec *fs,
|
|
|
|
JSPropertySpec *static_ps,
|
|
|
|
const spidermonkeyFunctionSpec *static_fs)
|
|
|
|
{
|
2020-10-11 09:41:27 -04:00
|
|
|
JS::RootedObject hobj(cx, obj);
|
|
|
|
JS::RootedObject r_parent_proto(cx, parent_proto);
|
|
|
|
JSObject *proto = JS_InitClass(cx, hobj, r_parent_proto, clasp,
|
2008-06-16 17:25:59 -04:00
|
|
|
constructor, nargs,
|
|
|
|
ps, NULL, static_ps, NULL);
|
|
|
|
|
|
|
|
if (proto == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (fs) {
|
|
|
|
if (!spidermonkey_DefineFunctions(cx, proto, fs))
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (static_fs) {
|
2020-10-11 09:41:27 -04:00
|
|
|
JS::RootedObject r_proto(cx, proto);
|
|
|
|
JSObject *cons_obj = JS_GetConstructor(cx, r_proto);
|
2008-06-16 17:25:59 -04:00
|
|
|
|
|
|
|
if (cons_obj == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (!spidermonkey_DefineFunctions(cx, cons_obj, static_fs))
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return proto;
|
|
|
|
}
|
2021-08-21 05:30:09 -04:00
|
|
|
|
|
|
|
bool
|
|
|
|
spidermonkey_check_if_function_name(const spidermonkeyFunctionSpec funcs[], const char *string)
|
|
|
|
{
|
|
|
|
for (int i = 0; funcs[i].name; i++) {
|
|
|
|
if (!strcmp(funcs[i].name, string)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|