SpiderMonkey problem.

Started by
2 comments, last by GameStudioD 18 years, 8 months ago
I am using SpiderMonkey as my scripting engine and I am running into a problem passing arguments to functions in my script. I created an argument using JS_NewObject() and used JS_AddRoot() to protect it from garbage collection. I am adding properties and methods to the object with JS_DefineProperties() and JS_DefineFunctions(). However, when I call a script function and pass my created argument from C++, the script bombs when I access any methods or properties of the argument. What is the proper way to create argument objects to send to script based functions in SpiderMonkey?
Advertisement
I think something like this should work:

jsval argv[1];
argv[0] = INT_TO_JSVAL(25);
ok = JS_CallFunctionName(cx, obj, "myfunction", 1, argv, &rval);

iirc I found that some of the xx_TO_JSVAL macros didn't work as I expected, but their equivalent JS_NewxxValue did. Eg. use JS_NewDoubleValue instead of DOUBLE_TO_JSVAL

Actually... I was just looking at the source:

JSBool js_NewDoubleValue(JSContext *cx, jsdouble d, jsval *rval)
{
jsdouble *dp;

dp = js_NewDouble(cx, d);
if (!dp)
return JS_FALSE;
*rval = DOUBLE_TO_JSVAL(dp);
return JS_TRUE;
}


Looks like you can't use DOUBLE_TO_JSVAL for an ordinary double, which was probably the problem I was having. Be careful with types. hth
I solved the problem and it was something I did not expect.

I defined a function, in &#106avascript, similar to this:

function Draw(g)
{
g.doSomething();
}

I called this function from C++, and I created the argument with JS_NewObject(). SpiderMonkey kept giving a "g not defined" error every time I used it. I thought it was the C++ code, but I could not find anything wrong with it. I played with the &#106avascript and found out that arguments[0] had the correct object in it. Browsing the internet, I cam across this article: http://www.mozilla.org/js/spidermonkey/release-notes/NOICF.html . Incorrect Visual Studio builds of SpiderMonkey can cause spurious "not defined" errors. Oy. Changing the compiler option, in the linker, solved the problem.<br><br>zppz, sounds like you have had similar struggles learning SpiderMonkey. The conversion functions takes a little while to get used to. SpiderMonkey is a great tool, it just takes some time to learn.

This topic is closed to new replies.

Advertisement