🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Embed mozilla's javascript engine into applicatoin using VC++.net

Started by
19 comments, last by Kippesoep 19 years, 10 months ago
Hi all, I am now trying to embed the engine(written in C) into my game(written in C++), I would like to use the script to change the AI of the game. When i compile the following code (copied from http://www.mozilla.org/js/spidermonkey/apidoc/jsguide.html): #include <stdio.h> #include <stdlib.h> #include <string.h> /* include the JS engine API header */ #define XP_PC #include "jsapi.h" /* main function sets up global JS variables, including run time, * a context, and a global object, then initializes the JS run time, * and creates a context. */ int main(int argc, char **argv) { /*set up global JS variables, including global and custom objects */ JSVersion version; JSRuntime *rt; JSContext *cx; JSObject *glob, *it; JSBool builtins; /* initialize the JS run time, and return result in rt */ rt = JS_NewRuntime(8L * 1024L * 1024L); /* if rt does not have a value, end the program here */ if (!rt) return 1; return 0; } A linking error (LNK2001) occurs. And i have tried many mehtods to solve the problems, but all are failed. Does any body can know the problem. Actually, i am the new comer of C++. Moreover, Is &#106avascript suitable in game development? As i want to reduce the development time. Thank you!!!
Advertisement
The error is generated from
rt = JS_NewRuntime(8L * 1024L * 1024L);

and i would like to reduce time on studying new programming language, so i will use &#106avascript. is it good?<br><br>
Are you linking against spidermonkey?
I have downloaded and unzipped the source code of mozilla.
And add directory in "include files" & "library files" of VS.NET. There is no problem in compiling but the problem is in linking.
Did you tell the linker to use the libraries? Telling it where libraries are doesn't mean it knows which ones to use., Project/Properties/Linker/Input/Additional Dependancies.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Quote: Original post by swan1981
Moreover, Is &#106avascript suitable in game development? As i want to reduce the development time. Thank you!!!


I think so, yes. Others will probably disagree and tell you to stick with Lua, Python or anoher library but I like SpiderMonkey for scripting (as you'll probably find out when the article goes up, ho-hum).

Please be aware that SpiderMonkey does have it's drawbacks that may hinge it away from complex game application. When SM was created, it was for embedding in applications which typically have one thread of execution. Many scripting engines in games these days seem to run parallel virtual machine threads, SpiderMonkey doesn't have this luxury (at least not that I can figure out - more later) so you'll have to split up your executions into event-like calls. This means no infinite script loops - SpiderMonkey is best as an event driven scriptig language - so you call back Level_onUpdate() type functions each frame. I don't find this a problem myself as it coincides with my programming style anyway - but some people may find it hard to deal with.

Porting a class over to SpiderMonkey is simple, yet it can be time consuming. I've been creating my own templated binding tools (not like LuaBind, sorry people) that simplify the process and cut down the work involved. Please, contact me if you need any help or code.

The worst thing about SpiderMonkey is the lack of documentation and examples for the embedding side of it. You will find millions of web pages that concern web-based &#106avascript - whilst this is useful for understanding the language syntax it doesn't go very far with helping you embed the library. Most of my work with SpiderMonkey has involved trawling the source, writing notes, figuring out exactly how and why things do what they do. I have a list of 'sticky' areas that could be classed as bugs, but I tend to think of them more as poor documentation &#111;n the part of the developers.<br><br>I'm going to be writing articles for my website about some technical details of SpiderMonkey and how to embed it in games - there will be a highlevel version of this series &#111;n GameDev soon, I hope. Please, PM me or email me if you have any suggestions or ideas for such articles or just need any help with SpiderMonkey in general. I'm no expert, but I'm beginning to understand how it all falls together after hacking around with it for a few months.
hi Oli,

can you send some examples to me about the embeding of spidermonkey in C++(VS.net).
my email: swan1981@gmail.com

Thx
Is it possible with SpiderMonkey to load several scripts at once (aka split the script to several files)? From what I have read until now, it doesn't seem possible. (Not that it would be a problem to temporarly concat all files)
Yes it is. If you open the script and compile it into an existing context all the functions and variables within that script are merged into it and become available for use.

EDIT: To add... bear in mind there's a bug(?) in SpiderMonkey that means you have to compile the script and then partially execute the script. The first time, use JS_CompileScript() and then call JS_ExecuteScriptPart() with JSEXEC_PROLOG. If you don't do this second part, you won't able to access the functions and variables in the script from C++. Don't ask me why, but after hours of tinkering this was the only workaround I found.

[Edited by - evolutional on September 13, 2004 4:18:28 AM]
Thanks, I'm still evaluating but I think I will go with JS (maybe just because of the syntax)...

This topic is closed to new replies.

Advertisement