Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (804)
Games in Android Showcase (239)
games submitted by our members
Games in WIP (868)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Is it worth to use nashorn ?  (Read 3552 times)
0 Members and 1 Guest are viewing this topic.
Offline Azias

Junior Newbie


Exp: 2 years



« Posted 2015-12-29 10:48:47 »

Hello,

I've been experimenting with nashorn lately and I have a few questions about it.

Firstly, I wanted to create scripts for events(onUnitDeath, on...) to let the user customize it without having to recompile the game and I'm not sure if it is a good idea to do it this way.

Secondly, is it possible to disable/prevent Java code from being run within JavaScript code ?
I asked this one, because some people can simply call the "java.io.File.delete()" function and other functions like that and because I don't plan on using it for the client side ?

Thanks in advance for your help.
Offline Riven
Administrator

« JGO Overlord »


Medals: 1371
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2015-12-29 12:08:07 »

An issue with using different languages is that you have to bridge between them. This is rather messy, and getting it secure is very complex. Regardless of the 'scripting language', how do you expect to handle, say, an infinite loop? If you want the least amount of bridging between languages, make Java the scripting language, by using Janino (runtime java->bytecode compilation). You can use Java's bytecode transformation library ASM to validate the generated bytecode (validating plain sourcecode is too complex), by whitelisting method calls (not blacklisting them). After that you simply load the bytecode with a ClassLoader.

Last but not least: this is quite an advanced topic, are you sure you want to even support end-user scripting?

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline nsigma
« Reply #2 - Posted 2015-12-30 11:59:01 »

I agree with @Riven about using Java.  You may potentially find Janino somewhat limiting though - it's missing some useful language support (eg. enums and runtime annotations).  In the Praxis LIVE repositories you can find a forked version of Janino and a refactored javac should that be useful.

https://github.com/praxis-live/

The compiler is Java 7 currently - will be adding Java 8 version in the new year.  Fairly easy to refactor though - it's mostly changing package declarations, some in strings IIRC.

Setting up the classpath is a little more complicated than vanilla Janino though.

... how do you expect to handle, say, an infinite loop? ...You can use Java's bytecode transformation library ASM to validate the generated bytecode (validating plain sourcecode is too complex), by whitelisting method calls (not blacklisting them).

Personally I'm dubious about using ASM to do security management in this way.  If it's for the user's own use, why bother with the complexity.  And if you're expecting users to share scripts then I think it's probably better to assume caveat emptor than set up expectations that you've closed every loophole.

@Riven - I would be interested in your thoughts on handling an infinite loop in this situation though.

Praxis LIVE - hybrid visual IDE for (live) creative coding
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline KaiHH

JGO Kernel


Medals: 796



« Reply #3 - Posted 2015-12-30 12:12:46 »

@Riven - I would be interested in your thoughts on handling an infinite loop in this situation though.
https://en.m.wikipedia.org/wiki/Halting_problem  Wink
Offline nsigma
« Reply #4 - Posted 2015-12-30 12:22:28 »


Thanks for the link!  Bear in mind @Riven said "handling" though.  Handling the possibility is not the same as proving it will occur!  Wink

Praxis LIVE - hybrid visual IDE for (live) creative coding
Offline Phased
« Reply #5 - Posted 2015-12-30 12:27:35 »

Can you really handle the possibility when you don't know if it will occur? Maybe it only needs 1 cycle more to finish.
Offline nsigma
« Reply #6 - Posted 2015-12-30 12:37:14 »

Well, by definition an infinite loop won't finish in 1 more cycle!   Wink  However, I understand your point.  But, who cares?  If we're talking in the context of a game, or any other real-time situation, you want code with a maximum bounded execution time?  In that situation code that exceeds that contract is invalid and should be disabled.  But how to do that without potentially leaving invalid state and/or making the code run substantially slower - eg. one thought I'd had was a monitoring thread and injecting a check on a volatile flag into every loop. 

Praxis LIVE - hybrid visual IDE for (live) creative coding
Offline Riven
Administrator

« JGO Overlord »


Medals: 1371
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #7 - Posted 2015-12-30 12:43:02 »

I agree with @Riven about using Java.  You may potentially find Janino somewhat limiting though - it's missing some useful language support (eg. enums and runtime annotations).
Who needs enums and runtime annotations for handling an
onUnitDeath(...)
event Pointing

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline nsigma
« Reply #8 - Posted 2015-12-30 13:06:39 »

Who needs enums and runtime annotations for handling an
onUnitDeath(...)
event Pointing

I needed runtime annotations for what I wanted to do, which is admittedly more complicated than your example.  The annotations are used to define behaviour and / or inject variables into the "scripts".  It can lead to much simpler user code.  Imagine you've got 10's or 100's of such onXSomething() style hooks, and the user wants to run a fragment of code on a subset of them - you can define annotations to filter the conditions such code is run and help the engine manage scripts more easily.

It depends on the level of flexibility around connections / maintaining state you require.  Simple example of how I'm using annotations is here - https://youtu.be/5R_ZJdYLpow

Praxis LIVE - hybrid visual IDE for (live) creative coding
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (581 views)
2019-09-04 15:33:17

hadezbladez (5510 views)
2018-11-16 13:46:03

hadezbladez (2402 views)
2018-11-16 13:41:33

hadezbladez (5772 views)
2018-11-16 13:35:35

hadezbladez (1223 views)
2018-11-16 13:32:03

EgonOlsen (4661 views)
2018-06-10 19:43:48

EgonOlsen (5682 views)
2018-06-10 19:43:44

EgonOlsen (3198 views)
2018-06-10 19:43:20

DesertCoockie (4095 views)
2018-05-13 18:23:11

nelsongames (5115 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!