0

Im trying to write a scritable plugin and I am using mozilla's example below as my guide, as well as looking at firebreath to see how it wraps the code. I am getting stuck on the return value to javascript.

Mozilla scriptable example

When javascript calls my function the Allocate,HasProperty,HasMethod,Invoke all get called. I return back the result in Invoke and the javascript variable is undefined or crashes the browser when modifying the result.

    STRINGZ_TO_NPVARIANT(_strdup("Hello World"), *result);
0

1 Answer 1

1

STRINGZ_TO_NPVARIANT is actually a bit dangerous; when you put a string into an NPVariant object you give ownership of that memory to the browser. However, if you didn't allocate that memory with NPN_MemAlloc things may explode when it tries to release that memory (possibly the source of your crash).

Look at what STRINGZ_TO_NPVARIANT is actually doing and don't use it 'til you understand how it works; until then, you may try performing the steps by hand so you have a better understanding. Allocate memory using NPN_MemAlloc and then strcpy your string to it. I bet this fixes your problem; after you've got it figured out build your own inline functions or whatnot to clean up the code again.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the response, I had already looked into the macro and I dont see anything wrong with how the macro is done problematically. But after reading here: link[/link]and with your comments I didnt use NPN_MemAlloc and copy the data into a temporary string. (temp = NPN_MemAlloc(strlen("Hello World")); memcpy(temp, "Hello World", strlen("Hello World")); STRINGZ_TO_NPVARIANT(temp, *result);)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.