Skip to main content
GameDev.net gamedev.net
🔒 Locked

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

Started by swan1981 Sep 8, 2004 at 12:28 PM 19 replies 3.9k views
Original Post
swan1981
swan1981
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 #include #include /* 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!!!
swan1981
swan1981
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>
Leffe
Leffe
Are you linking against spidermonkey?
swan1981
swan1981
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.
Washu
Washu
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.
evolutional
evolutional
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.
swan1981
swan1981
hi Oli,

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

Thx
TrueTom
TrueTom
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)
evolutional
evolutional
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]
TrueTom
TrueTom
Thanks, I'm still evaluating but I think I will go with JS (maybe just because of the syntax)...
evolutional
evolutional
I love SpiderMonkey and will be writing more articles on it's use in a gaming context. I'm planning on setting up a wiki on my site to cover the use of SpiderMonkey in games, would that be useful to anyone?
Kippesoep
Kippesoep
Quote:
Original post by evolutional
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.


That's not a bug. It is by design. JS_CompileScript does exactly what the name implies. JS being an interpreted language, the actual function and variable declaration doesn't happen until that part of the script is actually executed (through JS_ExecuteScript and related functions).

I also found the &#106avascript C Engine Embedder's Guide</a> and <a href="http://www.mozilla.org/js/spidermonkey/apidoc/sparse-frameset.html">reference</a> on Mozilla.org to be quite informative, if slightly outdated in places.
Kippesoep
evolutional
evolutional
Quote:
Original post by Kippesoep
That's not a bug. It is by design. JS_CompileScript does exactly what the name implies. JS being an interpreted language, the actual function and variable declaration doesn't happen until that part of the script is actually executed (through JS_ExecuteScript and related functions)


Thanks for the info, I was thinking it must have been some design characteristic as the rest of the runtime is quite bug-free. However, I was unable to find a clear explanation of it on the SpiderMonkey site. The API docs are quite useful, yes but there does unfortunately seem to be a lack of practical tutorials aside from the few linked on the site. It's a shame because it's a nice language.
Kippesoep
Kippesoep
Quote:
Original post by Anonymous Poster
Absolutely. Even though i followed the embeding guide from mozilla, i still cannot run the program.

moreover, I have define XP_PC / XP_WIN in the main program, but the compiler still request the definition of them, does anyone can help to solve the problem?? THX


Have you actually #defined them before including JSAPI.H. You could just add them to the "preprocessor definitions" in the project settings (or whatever it's called in your IDE).
Kippesoep
Kippesoep
Kippesoep
That's a rather odd exception. Are you using managed C++?
Kippesoep
yodaman
yodaman
Evolutional: Im still waiting for the article and for JSinvaders man. Can't wait! I have been thinking of going to lua however, but your xml article will be nice!
www.jinx.com www.thebroken.org www.suprnova.org www.mozilla.org
Kippesoep
Kippesoep
Quote:
Original post by Anonymous Poster
it appears so by the topic VC++.net


VC++.net is simply the version of the VC compiler (i.e. VC2k2, 2k3 or 2k5). VC++.net allows you to write normal (i.e. unmanaged) code as well as managed, so the subject does not give any information about that.

To swan1981:
Try doing all this in an unmanaged app (you can change the compiler options and remove the "/clr" option).

It sounds like a linking error between your managed app and the unmanaged DLL. (And people ask me why I'm so suspicious of managed C++).
Kippesoep

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.