Sign in to follow this  

SpiderMonkey Question

Recommended Posts

Whelp, after spending the better part of the afternoon fighting with it, I've finally got SpiderMonkey integrated into my 4E4 game. I've got the Context creation and script execution all combined into a very attractive little class. I can call back into my C++ from javascript. The only problem is that I can't figure out how to call INTO the javascript from my C++ code. I've seen the tutorial that says, essentially: JS_CallFunctionName(cx, target, "onClick", 1, argv, &rval); However, I'm not trying to call a method to an object (the object being 'target' above), but rather a top-level function in the script... I've tried passing global ( JS_NewObject(cx, &global_class, NULL, NULL) ) as this parameter, but it doesn't work. Anyone got any experience in this area? ~Pax

Share this post

Link to post
Share on other sites
Whelp, I solved the problem. I guess I'm the only one here who uses SpiderMonkey ;p.

It's a pretty naste kludge, but it works:

BOOL ASpiderMonkeyScript::TriggerEvent(const char *event, const char *args)
char *buf;
BOOL ret;

buf = (char *)malloc(strlen(event) + strlen(args) + 6);
memset(buf, 0, strlen(event) + strlen(args) + 6);
sprintf(buf, "%s(%s)", event, args);

ret = ExecuteScript(buf);

return ret;

That done, I have to say I highly recommend SM. Once you get past the initialization and integration, it becomes really easy to work with.


Share this post

Link to post
Share on other sites
Aaaah a spidermonkey question, and I missed it... sob :(
You're not quite the only one using it.
As long as you have set the global object up in the standard way, ie:

rt = JS_NewRuntime(0x100000);
cx = JS_NewContext(rt, 0x1000);
JSClass global_class = {
global = JS_NewObject(cx, &global_class, NULL, NULL);
JS_InitStandardClasses(cx, global);
I think what you were originally trying to do should work. Interesting kludge though.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this