diff --git a/src/ecmascript/libdom/spidermonkey/Makefile b/src/ecmascript/libdom/spidermonkey/Makefile index 1cea76c86..655796fed 100644 --- a/src/ecmascript/libdom/spidermonkey/Makefile +++ b/src/ecmascript/libdom/spidermonkey/Makefile @@ -2,6 +2,6 @@ top_builddir=../../.. include $(top_builddir)/Makefile.config INCLUDES += $(SPIDERMONKEY_CFLAGS) -OBJS = attr.obj attributes.obj collection.obj console.obj heartbeat.obj history.obj keyboard.obj localstorage.obj location.obj message.obj navigator.obj screen.obj unibar.obj window.obj xhr.obj +OBJS = attr.obj attributes.obj collection.obj console.obj heartbeat.obj history.obj implementation.obj keyboard.obj localstorage.obj location.obj message.obj navigator.obj screen.obj unibar.obj window.obj xhr.obj include $(top_srcdir)/Makefile.lib diff --git a/src/ecmascript/libdom/spidermonkey/implementation.cpp b/src/ecmascript/libdom/spidermonkey/implementation.cpp new file mode 100644 index 000000000..30092a256 --- /dev/null +++ b/src/ecmascript/libdom/spidermonkey/implementation.cpp @@ -0,0 +1,118 @@ +/* The SpiderMonkey domimplementation object. */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include + +#include "elinks.h" + +#include "ecmascript/ecmascript.h" +#include "ecmascript/libdom/parse.h" +#include "ecmascript/spidermonkey/document.h" +#include "ecmascript/spidermonkey/implementation.h" +#include "ecmascript/spidermonkey/util.h" +#include "util/conv.h" +#include + +static JSClassOps implementation_ops = { + nullptr, // addProperty + nullptr, // deleteProperty + nullptr, // enumerate + nullptr, // newEnumerate + nullptr, // resolve + nullptr, // mayResolve + nullptr, // finalize + nullptr, // call + nullptr, // construct + JS_GlobalObjectTraceHook +}; + +static JSClass implementation_class = { + "implementation", + JSCLASS_HAS_RESERVED_SLOTS(1), + &implementation_ops +}; + +static bool +implementation_createHTMLDocument(JSContext *ctx, unsigned int argc, JS::Value *rval) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + JS::RootedObject parent_doc(ctx); /* instance of @document_class */ + JS::CallArgs args = JS::CallArgsFromVp(argc, rval); + JS::RootedObject hobj(ctx, &args.thisv().toObject()); + + JS::Realm *comp = js::GetContextRealm(ctx); + + if (!comp || argc != 1) { +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s %d\n", __FILE__, __FUNCTION__, __LINE__); +#endif + return false; + } + + if (!JS_InstanceOf(ctx, hobj, &implementation_class, &args)) { +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s %d\n", __FILE__, __FUNCTION__, __LINE__); +#endif + return false; + } + + char *title = jsval_to_string(ctx, args[0]); + + if (title) { + struct string str; + if (!init_string(&str)) { + mem_free(title); + args.rval().setNull(); + return true; + } + + add_to_string(&str, "\n"); + add_html_to_string(&str, title, strlen(title)); + add_to_string(&str, ""); + + void *docu = document_parse_text(str.source, str.length); + done_string(&str); + mem_free(title); + + JSObject *obj = getDocument(ctx, docu); + + if (obj) { + args.rval().setObject(*obj); + return true; + } + } + + args.rval().setNull(); + return true; +} + +static const spidermonkeyFunctionSpec implementation_funcs[] = { + { "createHTMLDocument", implementation_createHTMLDocument, 1 }, + { NULL } +}; + + +JSObject * +getImplementation(JSContext *ctx) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + JSObject *el = JS_NewObject(ctx, &implementation_class); + + if (!el) { + return NULL; + } + + JS::RootedObject r_el(ctx, el); + spidermonkey_DefineFunctions(ctx, el, implementation_funcs); + + return el; +} diff --git a/src/ecmascript/libdom/spidermonkey/meson.build b/src/ecmascript/libdom/spidermonkey/meson.build index 62d6c5d0f..a6fdb143c 100644 --- a/src/ecmascript/libdom/spidermonkey/meson.build +++ b/src/ecmascript/libdom/spidermonkey/meson.build @@ -1,2 +1,2 @@ -srcs += files('attr.cpp', 'attributes.cpp', 'collection.cpp', 'console.cpp', 'heartbeat.cpp', 'history.cpp', 'keyboard.cpp', +srcs += files('attr.cpp', 'attributes.cpp', 'collection.cpp', 'console.cpp', 'heartbeat.cpp', 'history.cpp', 'implementation.cpp', 'keyboard.cpp', 'localstorage.cpp', 'location.cpp', 'message.cpp', 'navigator.cpp', 'screen.cpp', 'unibar.cpp', 'window.cpp', 'xhr.cpp') diff --git a/src/ecmascript/spidermonkey/implementation.cpp b/src/ecmascript/spidermonkey/implementation.cpp index 5a8682bd0..b776b8484 100644 --- a/src/ecmascript/spidermonkey/implementation.cpp +++ b/src/ecmascript/spidermonkey/implementation.cpp @@ -20,6 +20,8 @@ #include #include +#ifndef CONFIG_LIBDOM + static JSClassOps implementation_ops = { nullptr, // addProperty nullptr, // deleteProperty @@ -122,3 +124,4 @@ getImplementation(JSContext *ctx) return el; } +#endif