Using the Java Scripting API w/ JavaScript and/or Lua - java

So, I just realized that Java has a built in API system that works with JavaScript and apparently other scripting languages that are JSR-223 compliant. Now, I'm designing a game engine and I'm wondering if I should just use the integrated JavaScript support or figure out how to use Lua.
QUESTION: Can the Java Scripting API easily support Lua? If so, how?
If you feel like it: Would it be unknown to do the scripting side of a game engine in JavaScript?
EDIT: I need scripting capabilities for my engine to allow the creation of AIs, special voxels (Like blocks in minecraft) and other add-ons to the game. I'm not set on doing all this creation in a scripting language because the difficulty of doing so but a scripting language appears as a good alternative to making JARs for all game content. I'm simply exploring what I can do with Java.

The Java Scripting framework relies on the code implementing JSR-223 being written in Java. As such lua won't work.
However, you can use an implementation of lua written in Java such as luaj which has JSR-223 support included.
Note: luaj is not a complete clone of lua but it's pretty functional and allows you access to java classes etc.
On the subject of choosing a scripting language, as you are using Java as your system programming language, some of the benefits of using lua as a scripting language don't apply e.g. fast, small footprint, excellent C integration.
So really it becomes a personal preference. Javascript would be probably be fine, as would Jypthon, JRuby or luaj.

It is not a wise idea to make a game engine in JS. Despite possible(see Node.js / Three.js), JS is rather slow because is interpreted. Node.js has JIT compilation, but it's still 3-4x(best case) slower than C++ equivalent.
I never used Lua or Java Scripting API, I cannot give you an answer on this, but I think the speed of JS will change your mind.

Related

Using Nashorn in Java?

I'm reading about new Java 8 features and came to know it comes with Nashorn (Javascript engine). Can someone explain me why would/should I use a Javascript engine inside Java. What programming problems it would solve ? any examples
Thanks,
Ravi
Simply, it would allow you to create standalone JavaScript programs and allow embedding JavaScript in Java. Java must be compiled into what is known as a "machine language" before it can be run. JavaScript is text-based and interpreted, and is interpreted by browsers. So being able to use this technology inside Java is a pretty cool thing. JRuby is another example of a scripting language that can run inside Java, as well as it's Python equivalent Jython. They are all just wrappers for languages to use inside Java. It is already possible to run dynamic languages on top of the JVM, but the goal is to ease new dynamic language implementations and increase their performance.
One advantage I can think of is using JavaScript for the scripting of a Java game.
Hope it helps :)

A platform for Java, Scala, Groovy and jRuby would be implemented in what language?

As an experiment I am working on a small platform for Java applications (client server, P2P etc.) which I would like to be able to use from both Java and Scala. And possibly also from Groovy, jRuby etc.
I know that Scala can call easily into Java, but that Java cannot as easily call back into Scala. I don't know how this is working in jRuby and Groovy.
My initial thought was to implement the platform in Java, and then see if I could make it available to other languages. However, the platform may include starting threads which call components that implement certain Java interfaces. Would it be possible to pass e.g. a factory implemented in Scala, but which implements a Java interface, to a Java component and have that Java component call the Scala factory?
What are your recommendations in general for implementing such a cross language platform?
I am by no means an expert and have never done such a cross-language project (just minor Java/Scala).
But I would suggest implementing it in the language:
you know best
makes you most productive
ensures the most maintainability
In my case it would be Scala.
So what I would do is define the API in the form of Java interfaces, which I would then implement in Scala (probably I would need to convert Scala/Java collections back-and-forth, but that would be an implementation detail).
That way you make sure that other JVM languages can interact with your API, and you implement it in whatever language you are best.
Java can call Scala as easy at the other way around,
With eclipse you need to install 'Scala IDE' from the 'eclipse market place'
and configure the compiler to 'Scala Then Java' option in Properties->'Scala Compiler'-> 'Build Manager'-> compileorder.
P.s. you also need to add Scala nature to the project.
Would it be possible to pass e.g. a factory implemented in Scala, but which implements a Java interface, to a Java component and have that Java component call the Scala factory?
Even if it were so, switching to Scala would not solve the problem, as then jRuby would have even more problems to access that component.
Actually, all listed languages declare interoperability with Java, but not with other languages. So first design goal is, your platform should be Java-compatible in all its API, though some components could be written in other languages - but so that it is cannot be seen by API users. The easiest way to make it so is to write all in Java. Besides, I recommend Java because features of "more advanced" languages has their reverse side - one innocent-looking line of code can consume enormous amounts of CPU time or memory.

Best integrated scripting language for Java? (Stacktraces and debug)

I have a need for providing code snippets at runtime.
For this to work well, I basically need to call into the scripting language, and back into Java. For this to be usable in a debug scenario StackTraces must be usable too (so methods and linenumbers go directly to the script source like in modern JSP-pages) and Exceptions must bubble up correctly.
What scripting languages - where the source is read at runtime - can provide this? JSR-223 support is a bonus.
I think JRuby is best choice because:
Its performance as a scripting language is good.
It can execute in two mode (Compiled, Interpreted).
It supports Ruby on Rails and you can use JRuby for Rails applications in production environments.
You can invoke the classes of the Java Platform easily without any restriction.
I think Groovy would be the perfect fit in your case given it's super similarity and ease of integration with Java not to mention that it's pretty much the most mature JVM language out there with excellent support from the likes of SpringSource.
As an example of JSR support, Groovy has it.

What is a good, simple scripting language to embed into a Java game engine?

What I'm looking for is a scripting engine for Java that would allow users to write simple scripts to control the behavior and events for a game. Something that:
is simple - something easy to pick up, especially for people with some basic programming/scripting experience
provides lots of control - I can easily start/stop/pause scripts and control how much execution time each gets, perhaps how much memory space they can use
is separated from the Java environment itself - No access from scripts to any Java objects or classes, only to those functions I explicitly provide
I've considered the Rhino JavaScript engine, and it would suit my purposes, but from what I've read (example), it's designed to integrate with Java so much that sandboxing it securely would be tricky. I'd rather start with an engine that gives scripts no access to anything by default, than have a fully open one that I have to close up. The scripts might not always be trusted, so the environment should not be easy to break out of.
I'd also consider developing my own language with something like ANTLR, but it's probably more effort than I want to put in.
Any suggestions?
Have you considered Lua?
Google docs preview of a pdf on the subject
Lyrio, G.H.S.O; Seixas, R.B.; Using Lua as Script Language in Games Coded in Java, Proceedings of The North American Simulation and AI in Games Conference - GAMEON-NA, EUROSIS, Montreal, Canada, 2008.
You should give a try to Groovy, a scripting language that easily integrates with the Java platform.
Its syntax is 100% compatible with Java, but it also has a simplified syntax that makes it a suitable language for DSLs implementation.
I don't know for sure if you can stop/pause the execution of Groovy code from a Java program, you should read the Groovy API.
When executing Groovy code from within a Java program, you can specify the context passed to the script and you can query the context modified by the script for output variables. The script can be completely isolated from the underlying Java environment by creating a GroovyShell with an appropriate CompilerConfiguration.
JACL is one such language. It is based on TCL. Whatever you do, don't invent another language. There are plenty of good choices out there.
In my opinion not only language, but way of interfacing is important. JSR 223 is most compatible between different languages, but "native" seems the best (i.e. full object integration of groovy)

What the mechanism use to integrate python with other languages (.Net, Java ....)

Somebody talking the python's code can embed into C#'s code. What the mechanism to do that? please explain for me.
Thanks a lot
There are several approaches to this, depending on which languages you want to interoperate with.
.Net/CLR Languages - Iron Python provides an implementation of Python running on the CLR. Allows you to use other CLR assemblies and embed a python scripting engine in your code
Java/JVM Based Languages - Jython provides an implementation on the JVM and allows you to use Java classes and call to call into jython as a scripting language using JSR 223 - Scripting for the Java Platform
C/C++/Perl/etc, etc The Simplified Wrapper and Interface Generator allows you to interop between C based languages and others, including .Net and Java. It's very good for C++, C and COM - other languages are little trickier - but worth checking out if you need to use CPython with .Net or Java
Use IronPython for integration with .net. Likewise, Jython integrates with Java.
And Jython for integration with Java.

Categories

Resources