java scripting

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

java scripting

Lee Breisacher-2
Hi. I want to know if this is a crazy idea or not:

I want to use amber Smalltalk as a scripting language for java.
Currently, I work on a java program that uses Rhino for javascript
scripting, including Rhino's nice java integration. What I would like
to try to do is use amber.js (and associated other js files) to allow
for Smalltalk scripting and java integration (via amber's javascript
integration).

I don't know that much about the details of amber implementation,
although I've been studying it for a few hours. I do know Smalltalk
and I'm ok with javascript (and willing to learn lots more).

So, is this completely crazy, or does it seem do-able? One question I
have is how dependent amber is on node.js?  From the little studying
I've done, it looks like amber itself (compiler and runtime) is
independent of node.js, but the IDE and the REPL do use node.js. Is
that accurate?

Thanks,

Lee
Reply | Threaded
Open this post in threaded view
|

Re: java scripting

Stefan Krecher

Lee,
Sounds great - it's exactly what i did with my Ambrhino project:
https://github.com/StefanKrecher/Ambrhino
It's a server and a runtime based on Java/ Rhino
Regarding your question on node.js : Ambrhinos server-part can replace node.js as server.
Please see my blog-entry on this at krecher.com
I'm very interested in what you think about this.
Regards,
Stefan

--
sent from my android-phone

Am 08.01.2012 21:42 schrieb "Lee" <[hidden email]>:
Hi. I want to know if this is a crazy idea or not:

I want to use amber Smalltalk as a scripting language for java.
Currently, I work on a java program that uses Rhino for javascript
scripting, including Rhino's nice java integration. What I would like
to try to do is use amber.js (and associated other js files) to allow
for Smalltalk scripting and java integration (via amber's javascript
integration).

I don't know that much about the details of amber implementation,
although I've been studying it for a few hours. I do know Smalltalk
and I'm ok with javascript (and willing to learn lots more).

So, is this completely crazy, or does it seem do-able? One question I
have is how dependent amber is on node.js?  From the little studying
I've done, it looks like amber itself (compiler and runtime) is
independent of node.js, but the IDE and the REPL do use node.js. Is
that accurate?

Thanks,

Lee
Reply | Threaded
Open this post in threaded view
|

Re: java scripting

Lee Breisacher-2
Nice. That gives me inspiration to give it a try in my environment (don't want to copy any of your code directly due to possible licensing issues). Thanks!

One nit: Your blog mentions JSR-223, but you are not actually using it. The latest versions of Rhino do not actually support JSR-223, but rather have their own java integration API (which is similar to JSR-223).

Lee
Reply | Threaded
Open this post in threaded view
|

Re: java scripting

Stefan Krecher

Oh, i thought that it would be a Jsr 223 implementation. Rhino can be discovered as available scripting engine - but that does not mean that it's jsr 223?
Btw, you can use/ modify any code i've written. I have not wasted any thought on licensing. It's just free

--
sent from my android-phone

Am 08.01.2012 23:06 schrieb "Lee Breisacher" <[hidden email]>:
Nice. That gives me inspiration to give it a try in my environment (don't want to copy any of your code directly due to possible licensing issues). Thanks!

One nit: Your blog mentions JSR-223, but you are not actually using it. The latest versions of Rhino do not actually support JSR-223, but rather have their own java integration API (which is similar to JSR-223).

Lee
Reply | Threaded
Open this post in threaded view
|

Re: java scripting

Lee Breisacher-2
JSR 223 is about these 'generic' scripting classes:

import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.script.SimpleScriptContext;

whereas these are Rhino-specific:

import org.mozilla.javascript.Context;
import org.mozilla.javascript.ContextFactory;
import org.mozilla.javascript.ImporterTopLevel;
import org.mozilla.javascript.JavaScriptException;
import org.mozilla.javascript.ScriptableObject;


Reply | Threaded
Open this post in threaded view
|

Re: java scripting

gokr
In reply to this post by Stefan Krecher

"I have not wasted any thought on licensing. It's just free"

Then I would suggest you slap the X11 (MIT) license on it.

Btw, regarding Amber and node - we do not have a dependency on node except if you want to compile Amber code to js outside of the browser - amberc is a bash script that basically runs the Compiler (written in Amber) in a server side js runtime, and I picked node because I had a hard time getting d8 to work for me. Node is also clearly the best server side js vm that I know of today.

Furthermore - the server part of the IDE is just a trivial webDAV impl that Nicolas wrote, it works perfectly fine with Apache.

regards, Göran

Reply | Threaded
Open this post in threaded view
|

Re: java scripting

Lee Breisacher-2
hm...compiling outside the browser is exactly what I want to do. But I don't quite see why that requires node or any server. I'm going to be running amber within a different js engine (Rhino). As long as that engine can execute javascript, isn't that enough to run the compiler? I basically want to do this: take some Smalltalk source, compile it to javascript, then execute that resulting js. 

Thanks,

Lee
Reply | Threaded
Open this post in threaded view
|

Re: java scripting

gokr
On 01/09/2012 05:26 PM, Lee Breisacher wrote:
> hm...compiling outside the browser is exactly what I want to do. But I
> don't quite see why that requires node or any server.

Node is just a js runtime, and easily the best packaged one thatI know
of (for all platforms etc). And since the compiler is written in Amber
we need a js runtime :).

> I'm going to be
> running amber within a different js engine (Rhino). As long as that
> engine can execute javascript, isn't that enough to run the compiler? I

It is enough.

> basically want to do this: take some Smalltalk source, compile it to
> javascript, then execute that resulting js.

Sure, simplest is to just look at the code in the IDE. The class
Compiler etc. amberc runs nodecompile.js and it just basically uses the
Exporter and Importer class in Amber.

regards, Göran
Reply | Threaded
Open this post in threaded view
|

Re: java scripting

Stefan Krecher
In reply to this post by Lee Breisacher-2
Lee,
you really should look at the Ambrhino-project since it is all about your questions. Maybe you'd like to contribute, so we don't need to reinvent the wheel.

Ambrhino handles PUT-Requests to act exactly like node.js (saves files to the filesystem):

it handles GET-Requests to serve pages (similar to node.js)

And it handles POST-Request - POST-requests are checkt to see if there is a special command, e.g.:
...
else if (uri.startsWith("createProgram")) {
String[] params = uri.split("\\?");
String main = params[1];
String targetFilename = params[2];

ArrayList<String> files = new ArrayList<String>();

String[] kernelFiles = new String[] { "boot", "Kernel-Objects", "Kernel-Classes", "Kernel-Methods",
"Kernel-Collections", "Kernel-Exceptions", "Kernel-Transcript", "parser", "Compiler" };

for (String filename : kernelFiles) {
files.add(filename);
}

for (String entry : stringdata.split("&")) {
files.add(entry.split("=")[1]);
}

files.add("init");

FileOutputStream fos = new FileOutputStream(targetFilename + ".js");

for (String filename : files) {
writeProgram(fos, filename);
}

fos.write(("smalltalk." + main + "._main()").getBytes());
fos.close();
...

This snippet is called via an ajax-call to create an Amber-Program that later can be executed from the commandline.
Core-categories and a list of user-defined categories (that previously were compiled and saved via commit) are concatenated. After that, a call to a main-method is appended.
The so created .js-file can be executed like this:

java -cp ambrhino.jar com.ambrhino.server.FileServer yourfile.js

the snippet which is reponsible for script-execution:
public static void runScript(String filename) throws IOException {
ContextFactory.initGlobal(new AmberContextFactory());
Context ctx = Context.enter();
ctx.setOptimizationLevel(-1);
String script = readFile(filename);
ScriptableObject scope = new ImporterTopLevel(ctx);
Object o = ctx.evaluateString(scope, script, "<cmd>", 1, null);
}

HTH,
Stefan
Reply | Threaded
Open this post in threaded view
|

Re: java scripting

Lee Breisacher-2
Yes, I had looked at that code before. Helpful indeed. One thing I do not yet understand: amber.js depends on the existence of globals such as window and document, which obviously exist in a browser, but do not exist in "vanilla" Rhino. How did you make those objects exist so the amber js code will run? 

Thanks,

Lee
Reply | Threaded
Open this post in threaded view
|

Re: java scripting

Lee Breisacher-2
Ah, I see now -- you don't actually use amber.js at all.

Lee
Reply | Threaded
Open this post in threaded view
|

Re: java scripting

Hannes Hirzel
Nice work Stephan,

A question: Is FileServer.java basically all the code done in addition
to the existing Amber files?

https://github.com/StefanKrecher/Ambrhino/blob/master/Ambrhino/src/com/ambrhino/server/FileServer.java

Can you use it to replace
node.js and keep working with a web browser?

I would appreciate some comments in that file.

workspace.js is a 18000 lines file
https://github.com/StefanKrecher/Ambrhino/blob/master/Ambrhino/workspace.js

It contains Smalltalk translated to JavaScript. Mainly Amber
Smalltalk. Does it contain amber.js  1:1? Seemingly there are some of
your own additions; where is the source code of it?


How much effort would it be to have a Swing environment with a Amber
class browser and Transcript and Workspace?

Kind regards
Hannes

On 1/11/12, Lee Breisacher <[hidden email]> wrote:
> Ah, I see now -- you don't actually use amber.js at all.
>
> Lee
>
Reply | Threaded
Open this post in threaded view
|

Re: java scripting

Hannes Hirzel
p.s.

a screenshot

HH

On 1/12/12, H. Hirzel <[hidden email]> wrote:

> Nice work Stephan,
>
> A question: Is FileServer.java basically all the code done in addition
> to the existing Amber files?
>
> https://github.com/StefanKrecher/Ambrhino/blob/master/Ambrhino/src/com/ambrhino/server/FileServer.java
>
> Can you use it to replace
> node.js and keep working with a web browser?
>
> I would appreciate some comments in that file.
>
> workspace.js is a 18000 lines file
> https://github.com/StefanKrecher/Ambrhino/blob/master/Ambrhino/workspace.js
>
> It contains Smalltalk translated to JavaScript. Mainly Amber
> Smalltalk. Does it contain amber.js  1:1? Seemingly there are some of
> your own additions; where is the source code of it?
>
>
> How much effort would it be to have a Swing environment with a Amber
> class browser and Transcript and Workspace?
>
> Kind regards
> Hannes
>
> On 1/11/12, Lee Breisacher <[hidden email]> wrote:
>> Ah, I see now -- you don't actually use amber.js at all.
>>
>> Lee
>>
>

amber-workspace-in-Java-JTextArea.PNG (16K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: java scripting

Stefan Krecher
In reply to this post by Hannes Hirzel
Hi,

Am 12. Januar 2012 04:26 schrieb H. Hirzel <[hidden email]>:
> A question: Is FileServer.java basically all the code done in addition
> to the existing Amber files?

yes, that's all.

> https://github.com/StefanKrecher/Ambrhino/blob/master/Ambrhino/src/com/ambrhino/server/FileServer.java
>
> Can you use it to replace
> node.js and keep working with a web browser?

yes - just start it with the server.bat file and navigate to
localhost:8080/index.html

> I would appreciate some comments in that file.

i will do that. The code needs some clean up too - i just committed
everything i got to share it early

> workspace.js is a 18000 lines file
> https://github.com/StefanKrecher/Ambrhino/blob/master/Ambrhino/workspace.js
>
> It contains Smalltalk translated to JavaScript. Mainly Amber
> Smalltalk. Does it contain amber.js  1:1? Seemingly there are some of
> your own additions; where is the source code of it?

workspace.js can be created with the "AmberClient createExample" (from
within the browser). It's does a "special" POST-Request to create a js
file on the server-side. Beside some core-categories a list of
user-categories will be concatenated and a call to the main-method of
a desired class. amber.js itself isn't included.
Concatenation is done in FileServer.handlePOST. This files are concatenated:
String[] kernelFiles = new String[] { "boot", "Kernel-Objects",
"Kernel-Classes", "Kernel-Methods",
                                        "Kernel-Collections", "Kernel-Exceptions", "Kernel-Transcript",
"parser", "Compiler" };

plus a list of user-provided categories plus the call to the
main-method: fos.write(("smalltalk." + main + "._main()").getBytes());

> How much effort would it be to have a Swing environment with a Amber
> class browser and Transcript and Workspace?

it depends - you can code everything in pure Amber like i did in the
workspace-example.
An easyer approach would be to prepare UI classes in Java, inject the
instances into the javascript Context and call them via Amber.
Would you be interested in an Java/ Swing IDE for Amber?

regards,
Stefan

p.s.: thanks for the screenshot