4

Is there a way to execute a java script from within a java program in a step-by-step manner?

I know how to call javascript from java. I know about Rhino or Beanshell. No web server involved.

What I'm trying to achieve: Have multiple scripts execute semi concurrently scheduled by a scheduler where each script gets a 'tick' every now and then in which it executes one atomic command just like in a debugger. This should also 'step into' functions and for/while loops.

I need to call the script 'tick' at a specific time (and return immediately) so real threads are no use.

So I guess i want a way to debug a script from within a java program without a debugger (and its overhead) involved. Just stepwise execution is needed.

Any ideas?

5
  • U mean calling Javascript from Java? Use Rhino if this is the case Commented Feb 16, 2012 at 8:44
  • You mean call JavaScript on the web site or just execute Javascript script from java? Commented Feb 16, 2012 at 8:44
  • Execute them from Java. This is what u want right? Commented Feb 16, 2012 at 8:47
  • What do you mean by "This should also 'step into' functions and for/while loops." ? Commented Feb 16, 2012 at 8:48
  • To be more precisely: I know how to call javascript from java. I know about Rhino or Beanshell. The point of interest is the tick of execution. Commented Feb 16, 2012 at 8:58

2 Answers 2

3

Yes, it is possible by using an interpreter. More specifically, you need an JavaScript interpreter that can be executed on the JVM. Rhino seems to be the most popular one.

http://www.mozilla.org/rhino/

If you are running scripts concurrently then you will need an interpreter for each concurrent script.

Sign up to request clarification or add additional context in comments.

4 Comments

I edited my post to reflect that i already know about Rhino. My question is a little more specific. Any ideas on the tick?
Ok, that is going to be a bit tricky and pretty sure that functionality isn't provided in its API. You will need to edit the source code of the JavaScript interpreter by adding some volatile flag to instruct to interpret the next JavaScript command. Your tick will need to set the flag to true and then you will need the interpreter to set the flag to false after reading interpreting a statement and then it needs to wait for the flag to be set to true.
I hoped for a simpler way but expected something like that :-( I will look into this when I find the time. Thanks anyway.
I don't think it will be that difficult. Otherwise you could look at the implementation of its debugger (mozilla.org/rhino/debugger.html) and replicate so its operations can be invoked from code rather than from GUI events.
0

Check out these links:

https://www.mozilla.org/rhino/tutorial.html https://blogs.oracle.com/blue/entry/scripting_with_servlets_rhino_part

They will help u get started.

2 Comments

Sorry I don't see how that helps. Besides: no web server involved.
I thought u did not know how to use Rhino. I mean you edited your question after my post if I am not wrong. Anw no problem man.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.