This project has moved. For the latest updates, please go here.

XBox and XNA

Feb 14, 2012 at 6:02 PM
Edited Feb 14, 2012 at 6:03 PM

Is it possible to use Jurassic on the XBox 360 with XNA?

I know you can't use System.Reflection.Emit on the XBox, but I'm thinking you could emit the compiled JS into a .NET Assembly as part of the compile step. You could then ship this Assembly together with a stripped down version of Jurassic on the XBox.

Creating the .NET Assembly from the JS source shouldn't be a problem (I saw another thread about this) and stripping the Compiler from Jurassic shouldn't be that difficult either, since the source quality is excellent. However, I'm not sure if there are any other parts in Jurassic that need Reflection.Emit and I don't know how I can load and execute the compiled Assembly again.

I'm pretty new to .NET, but I'm totally committed to make this work. Before I dive into this: is there anything I'm missing? Any pointers where to start?

Coordinator
Feb 14, 2012 at 9:51 PM
Edited Feb 14, 2012 at 9:56 PM

It seems doable, though I think you underestimate the work required to generate the assembly.  I know I have talked about how to do this in the forum in the past, but this was only ever intended as a hack to help me with debugging the code generation.  Adding this as a proper feature requires the following:

  • A new API, maybe called void CompileToFile(string code, string filePath) or something similar.
  • Code is currently generated when a method is called (i.e. on the fly).  This will obviously not work if you want to generate code for all possible code paths.  Code is needed to enumerate all methods and compile them up front.
  • The code that bridges javascript to .NET method calls also uses code generation - these stubs will need to be saved into the assembly as well.  Come to think of it, multiple stubs can be generated depending on the number of input parameters - this will probably need to be changed to a single generic stub.
  • If you're saving the assembly to an arbitrary location on disk then the assembly will not load unless the Jurassic DLL is in the same directory (or the GAC).
  • eval("") and new Function("") will obviously not work on XBox 360 - maybe use #ifdefs to throw an exception on this platform.

You can generate the assembly as an EXE (with an entry point) so that is no problem, but I don't know if that will work without modification on XBox 360.

I hope I haven't discouraged you too much: it would be very cool if Jurassic could do this and I think it would help a lot of people.

Feb 14, 2012 at 9:58 PM
Also, as a valid point, actually compiling the script to an assembly would of cause take away the the dynamic capability of scripting. You can't have users add their own scripts or stuff like that. And to top it of, XNA would normally require rather fast execution, and compiled JS is not.... fast, in regards to C# or the likes. At least in my experience. Not saying it's slow for normal use. But games are under no circumstances normal use.
Feb 14, 2012 at 11:11 PM
Edited Feb 14, 2012 at 11:44 PM

paulbartrum:

I didn't realize that code is only generated 'on the fly'. But you say enumerating all functions (from the syntax tree I suppose?) could work? Could this handle clousures and all? This suddenly sounds very complicated :/

I think I'd want to compile Jurassic directly into the Xbox App. Would the JS Assembly still search for the DLL file if everything is included in the App anyway?

I don't really care about 'eval()' and 'new Function()'. I don't see a reason to use these anyway, so thats totally ok with me.

 

Alxandr:

Not being able to compile the JS at runtime would be ok for my use case and I don't think speed will be much of an issue. From my experience with JavaScript games in the browser, 99% of the time is spent drawing the game, not computing the logic. And I can make the drawing part fast.

A bit of background, in case you're interested: I'm the author of Impact, a JavaScript Game Engine. I've previously worked on a project to get the engine running on the iPhone - drawing everything with OpenGL instead of in a browser window. Doing this for the XBox seems like the next logical step :)

Coordinator
Feb 14, 2012 at 11:42 PM

> I didn't realize that code is only generated 'on the fly'. But you say enumerating all functions (from the syntax tree I suppose?) could work? Could this handle clousures and all?

Yes, and yes.

> This suddenly sounds very complicated :/

Yes :-)  If it was easy, I would have done it already...

> I think I'd want to compile Jurassic directly into the Xbox App. Would the JS Assembly still search for the DLL file if everything is included in the App anyway?

That depends on how you construct the JS Assembly, but generally it will look for the assembly that built it.  Because this is normally Jurassic.dll, this is what it looks for.  If you included the source within an EXE and generated the JS Assembly, it would look for the EXE.

Apr 28, 2012 at 2:16 PM
Edited Apr 28, 2012 at 2:17 PM

Got it working, but it's not as fast as hoped it would be. More info and the source here:

http://www.phoboslab.org/log/2012/04/javascript-on-the-xbox-360