I have been studying this example in an effort to develop a basic JavaScript engine that can execute scripts like the zxcvbn library. I thought I had it all figured out, but there are certain parts of the code that still puzzle me. Particularly, I am struggling with extracting values from a JavaScript function without causing any memory issues.
Here is my implementation: My GitHub repo
While it runs smoothly in isolation, integrating it into a real project often leads to crashes similar to the access violation exception described below.
I have found a solution for the problem, but I am having trouble grasping that part of the sample. When I run the script, it simply returns double what the original script returned. The callback mechanism with the runScriptDelegate also confuses me.
Thank you for raising this question. Let me explain further.
The issue with most of the access violation errors stems from installed callbacks not being retained in the C# application and getting garbage collected before invocation. For instance, when creating a function like the one below,
//JsNativeFunction callback
private static JavaScriptValue nativeFunction(JavaScriptValue callee, bool isConstructCall, JavaScriptValue[] arguments, ushort argumentCount, IntPtr callbackData)
{
//do something here
}
...
JsCreateFunction(nativeFunction, callbackData, func);