JavaScript in Mustang

David Flanagan has this look at using the new javax.script package in Mustang and it's associated JavaScript interpreter...
Sorry about the spacing. [Ed note: - modified story to fix spacing] It is a byproduct of the way the penguin publishes.

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = Manager.getEngineByExtension("js");

// Use our own name/value pairs as JavaScript variables
Bindings bindings = new SimpleBindings(defaults);

// Create a context for evaluating scripts in
ScriptContext context = new SimpleScriptContext();

// Set those Bindings in the Context so that they are readable
// by the scripts, but that variables defined by the scripts do
// not get placed into our Map object.
context.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);

Comments

RE: JavaScript in Mustang

JavaScript in a real programming language, arghhhh! Ok, somewhat kidding as I have grown in terms of my contempt for ECMAScript over the years. Even so, what the hell is this for? Please explain.

You can already just go get Rhino if you want to embed Javascript in your Java (which is done for example in testing stuff like JWebUnit), why should it be included with the JDK?

Sure whats "in" and whats "out" is debatable when it comes to many packages, why are some things external, then why are some things included - but JavaScript really needs to be there?

And I call BS on the "this is how the penguin publishes". It makes newlines into break tags, thats it. I will edit the story to fix that, since the author who writes entire programming languages in his spare time - cant seem to figure it out.

RE: JavaScript in Mustang

Ok smartass. The problem is turning newlines into br's double spaces any pre tags because they already treat newlines as newlines. :P

RE: JavaScript in Mustang

Which is hence why I removed the pre tags, and it looks just fine.

RE: JavaScript in Mustang

Its of course not all programmy "pre" looking, but thats better than the spacing thing.

No penguin is not smart enough to detect pre tags and then non nl2br those.

RE: JavaScript in Mustang

I added back a font tag, to sort of emulate the monospaced pre thing. I know this is not nearly the same as pre itself which preserves formatting and all, and its a few hoops, but its the closest still pretty simple thing I could come up with (dont feel like touching the old penguins php code for this one).

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.