is there a way to validate a Javascript using Java?

U

ufmemo

What i'm looking to do is to serve dynamically generated Javascript
from a J2EE application. I'd like to see if there are Javascript
compiler exceptions before i serve the JS to the browser.

Is there a javascript interpreter for Java that I might be able to use
to tell if there are compile time exceptions in the JS? An alternative
or other suggestions are welcome! Thank you.

John Jimenez
 
O

Oliver Wong

What i'm looking to do is to serve dynamically generated Javascript
from a J2EE application. I'd like to see if there are Javascript
compiler exceptions before i serve the JS to the browser.

Is there a javascript interpreter for Java that I might be able to use
to tell if there are compile time exceptions in the JS? An alternative
or other suggestions are welcome! Thank you.

When I google for "javascript interpreter for java" I get
http://www.mozilla.org/rhino/

- Oliver
 
C

Chris Smith

What i'm looking to do is to serve dynamically generated Javascript
from a J2EE application. I'd like to see if there are Javascript
compiler exceptions before i serve the JS to the browser.

The entirety of what you're asking for is absolutely IMPOSSIBLE, for any
sufficiently powerful language (of which JavaScript is considered
sufficiently powerful). Alan Turing and Alonzo Church both gave
independent proofs of this in 1936. There exists NO way to examine an
arbitrary piece of code and determine whether something will happen or
not in general.

That said, you can do as Oliver suggested and give it a few test runs
with Rhino. The problem will be in simulating the user's web browser
accurately enough to reproduce any problems that might come up. Are you
sure you want to re-implement the DOM for HTML, for instance? It will
be a lot of work, and it won't be perfect... but it still might be
better than nothing.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
O

Oliver Wong

Chris Smith said:
The entirety of what you're asking for is absolutely IMPOSSIBLE, for any
sufficiently powerful language (of which JavaScript is considered
sufficiently powerful). Alan Turing and Alonzo Church both gave
independent proofs of this in 1936. There exists NO way to examine an
arbitrary piece of code and determine whether something will happen or
not in general.

I had assumed by "compiler exceptions", the OP meant they wanted to
check for syntax errors (e.g. missing braces or parentheses). The usefulness
of this? Perhaps as unit tests, or because the generated JavaScript depends
on user provided input. If the OP is indeed asking about behaviour of
syntactically legal JavaScript, then Chris is right: it's impossible, and a
summary of why is at http://en.wikipedia.org/wiki/Halting_problem

- Oliver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top