Jump to content
  • Advertisement
Sign in to follow this  
GameStudioD

SpiderMonkey problem.

This topic is 4843 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
I solved the problem and it was something I did not expect.

I defined a function, in javascript, 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 javascript 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.

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!