From 2133aca5a0bbec5e869b5891f9a9fd5ddf48be14 Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Fri, 20 Sep 2024 18:54:49 +0200 Subject: [PATCH] [spidermonkey] JSObject as a key, not std::string There is a small progress, but still repeated requests to python's doc search fail. --- src/ecmascript/spidermonkey.cpp | 8 ++++---- src/ecmascript/spidermonkey/window.cpp | 4 ++++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/ecmascript/spidermonkey.cpp b/src/ecmascript/spidermonkey.cpp index b875f0a39..fb22caa1a 100644 --- a/src/ecmascript/spidermonkey.cpp +++ b/src/ecmascript/spidermonkey.cpp @@ -182,7 +182,7 @@ CompileExampleModule(JSContext* cx, const char* filename, const char* code, size // // NOTE: This example assumes only one JSContext/GlobalObject is used, but in // general the registry needs to be distinct for each GlobalObject. -static std::map moduleRegistry; +std::map moduleRegistry; // Callback for embedding to provide modules for import statements. This example // hardcodes sources, but an embedding would normally load files here. @@ -202,9 +202,10 @@ ExampleResolveHook(JSContext* cx, JS::HandleValue modulePrivate, JS::HandleObjec return nullptr; } std::u16string filename(specChars.get()); + JSObject *global = JS::CurrentGlobalOrNull(cx); // If we already resolved before, return same module. - auto search = moduleRegistry.find(filename); + auto search = moduleRegistry.find(global); if (search != moduleRegistry.end()) { return search->second; @@ -221,7 +222,7 @@ ExampleResolveHook(JSContext* cx, JS::HandleValue modulePrivate, JS::HandleObjec // Register result in table. if (mod) { - moduleRegistry.emplace(filename, JS::PersistentRootedObject(cx, mod)); + moduleRegistry[global] = JS::PersistentRootedObject(cx, mod); return mod; } JS_ReportErrorASCII(cx, "Cannot resolve import specifier"); @@ -466,7 +467,6 @@ spidermonkey_get_interpreter(struct ecmascript_interpreter *interpreter) goto release_and_fail; } - #if 1 // Register a hook in order to provide modules JS::SetModuleResolveHook(JS_GetRuntime(ctx), ExampleResolveHook); diff --git a/src/ecmascript/spidermonkey/window.cpp b/src/ecmascript/spidermonkey/window.cpp index 14b98f0bd..de76d5dc1 100644 --- a/src/ecmascript/spidermonkey/window.cpp +++ b/src/ecmascript/spidermonkey/window.cpp @@ -54,6 +54,7 @@ #include "viewer/text/link.h" #include "viewer/text/vs.h" +#include static bool window_get_property_closed(JSContext *cx, unsigned int argc, JS::Value *vp); static bool window_get_property_event(JSContext *cx, unsigned int argc, JS::Value *vp); @@ -87,6 +88,8 @@ struct el_message { struct el_window *elwin; }; +extern std::map moduleRegistry; + static void window_finalize(JS::GCContext *op, JSObject *obj) { @@ -104,6 +107,7 @@ window_finalize(JS::GCContext *op, JSObject *obj) free_list(elwin->listeners); mem_free(elwin); } + moduleRegistry.erase(obj); } JSClassOps window_ops = {