mozilla-spidermonkey / spidermonkey-embedding-examples

Documentation and examples for embedding the SpiderMonkey JavaScript / WebAssembly engine in their applications.
Other
197 stars 34 forks source link

memory crash with conversions #86

Closed DragonSlayer62 closed 3 months ago

DragonSlayer62 commented 3 months ago

so my issue at the moment is two fold Validating that I can find the functions in a script - I think I have this Converting a JSScript * to a std::string. It works, but when the conversion goes out of scope (it's a UniquePtr) I get a memory crash

std::string ConvertJSScriptToString(JSContext cx, JSScript script) { // Ensure the script and context are valid if (!script || !cx) { std::cerr << "Invalid script or context!" << std::endl; return ""; }

// Get the source code as a JSString
JS::Rooted<JSString*> scriptSource(cx, JS_DecompileScript(cx, script, "script", 0));
if (!scriptSource) {
    std::cerr << "Failed to decompile script!" << std::endl;
    return "";
}

// Convert the JSString to a UTF-8 encoded C string
JS::UniqueChars utf8chars = JS_EncodeStringToUTF8(cx, scriptSource);
if (!utf8chars) {
    std::cerr << "Failed to encode string to UTF-8!" << std::endl;
    return "";
}

// Log the content of the UTF-8 string
std::cout << "Script source: " << utf8chars.get() << std::endl;

// Convert the C string to std::string
return std::string(utf8chars.get());

}

void ExampleUsage(JSContext cx, JSScript script) { std::string scriptContents = ConvertJSScriptToString(cx, script); std::cout << "Converted script content: " << scriptContents << std::endl; }

i tryed this still get memory crash or null erorr cant find it.

https://github.com/UOX3DevTeam/UOX3/tree/feature/spidermonkey-esr115

here is the project we are working on upgrading from spidermoneky 1.8 to 115

here some of the errors for the stirng console outp[uts

Loading JS Scripts ERROR: JS script failure: Script Number (65535) Message (GetServerSetting is not defined) ERROR: Filename: D:/Local/media/Code/repos/UOX3/UOX3/data/js/server/global.js Line Number: 114 script result: null/undefined (D:/Local/media/Code/repos/UOX3/UOX3/data/js/server/global.js) script result: null/undefined (D:/Local/media/Code/repos/UOX3/UOX3/data/js/server/misc/helpgump.js) script result: null/undefined (D:/Local/media/Code/repos/UOX3/UOX3/data/js/server/timer/long_term_timers.js) WARNING: JS script failure: Message (unreachable code after return statement) ERROR: Compiling D:/Local/media/Code/repos/UOX3/UOX3/data/js/server/resource/pitchers.js caused a construction failure (Details: Compilation failed) script result: null/undefined (D:/Local/media/Code/repos/UOX3/UOX3/data/js/server/misc/facetRuleset.js) ERROR: JS script failure: Script Number (65535) Message (GetServerSetting is not defined) ERROR: Filename: D:/Local/media/Code/repos/UOX3/UOX3/data/js/npc/pets/static_horse.js Line Number: 4 script result: null/undefined (D:/Local/media/Code/repos/UOX3/UOX3/data/js/npc/pets/static_horse.js) ERROR: JS script failure: Script Number (65535) Message (GetServerSetting is not defined) ERROR: Filename: D:/Local/media/Code/repos/UOX3/UOX3/data/js/npc/pets/static_ostard.js Line Number: 4

GetServerSetting is the stirng its tyring to read but seems cant

DragonSlayer62 commented 3 months ago

Any help on this project or ideas how to solve this isssue would be greatful because we been working for months on this.

arai-a commented 3 months ago

I'd suggest the following:

Also, this is not an appropriate place to get support for each project. If you need more help, please use https://discourse.mozilla.org/c/spidermonkey/551 , or ask in https://chat.mozilla.org/#/room/#spidermonkey:mozilla.org

ptomato commented 3 months ago

From the error message you posted, it looks like the conversion to a string is not the problem; the constructor of std::string copies its argument so it shouldn't matter if the UniqueChars goes out of scope. Anyway, happy to chat elsewhere about this.