Rocksolid Light

Welcome to Rocksolid Light

mail  files  register  newsreader  groups  login

Message-ID:  

"Ada is PL/I trying to be Smalltalk. -- Codoso diBlini


devel / comp.lang.java.programmer / JSR 223 API

SubjectAuthor
o JSR 223 APIArne_Vajhøj

1
JSR 223 API

<u1u7c0$2pjnb$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=239&group=comp.lang.java.programmer#239

  copy link   Newsgroups: comp.lang.java.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: arne@vajhoej.dk (Arne Vajhøj)
Newsgroups: comp.lang.java.programmer
Subject: JSR 223 API
Date: Fri, 21 Apr 2023 10:42:05 -0400
Organization: A noiseless patient Spider
Lines: 332
Message-ID: <u1u7c0$2pjnb$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 21 Apr 2023 14:42:09 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e996e2ba642637259c424cf34d12715f";
logging-data="2936555"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX188P5et47/xmKM5Oin74j94xLZkAc9mxLE="
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.10.0
Cancel-Lock: sha1:CHFfIbfchXBfXv6tvPqFeZ36/jo=
Content-Language: en-US
 by: Arne Vajhøj - Fri, 21 Apr 2023 14:42 UTC

I have posted some examples of usage of JSR 223 API
(scripting engines) in comp.os.vms and I will
post them here as well just in case someone ever need
to find such examples (there is not that much
available on the net).

Required libs to run all: abcl, bsh, groovy, jacl, jruby, jython,
luaj, perlito5 and quercus (but in most practical cases
you would only need lib for one language).

Arne

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

public class EmbeddedScript {
public static void test(String language, String source) throws
ScriptException {
System.out.println(language + ":");
System.out.print(source);
ScriptEngineManager sem = new ScriptEngineManager();
ScriptEngine se = sem.getEngineByName(language);
se.eval(source);
}
public static void main(String[] args) throws ScriptException {
test("javascript", "for(i = 0; i < 3; i++) {\r\n" +
" print(\"Hi from JavaScript!\")\r\n" +
"}\r\n");
test("python", "for i in range(3):\r\n" +
" print(\"Hi from Python!\")\r\n");
test("ruby", "for i in 1..3 do\r\n" +
" puts 'Hi from Ruby!'\r\n" +
"end\r\n");
test("php", "<?php\r\n" +
"for($i = 0; $i < 3; $i++) {\r\n" +
" echo \"Hi from PHP!\\r\\n\";\r\n" +
"}\r\n" +
"?>\r\n");
test("groovy", "for(i in 1..3) {\r\n" +
" println \"Hi from Groovy!\"\r\n" +
"}\r\n");
test("beanshell", "for(int i = 0; i < 3; i++) {\r\n" +
" System.out.println(\"Hi from
BeanShell!\");\r\n"+
"}\r\n");
test("perl", "for(1..3) {\r\n" +
" print(\"Hi from Perl!\\n\");\r\n" +
"}\r\n");
test("lua", "for i = 1,3\r\n" +
"do\r\n" +
" print(\"Hi from Lua!\")\r\n" +
"end\r\n");
test("Lisp", "(dotimes (n 3)\r\n" +
" (format t \"Hi from Lisp!~%\"))\r\n");
test("tcl", "for {set i 0} {$i < 3} {incr i} {\r\n" +
" puts \"Hi from Tcl!\"\r\n" +
"}\r\n");
}
}

Output:

javascript:
for(i = 0; i < 3; i++) {
print("Hi from JavaScript!")
} Hi from JavaScript!
Hi from JavaScript!
Hi from JavaScript!
python:
for i in range(3):
print("Hi from Python!")
Hi from Python!
Hi from Python!
Hi from Python!
ruby:
for i in 1..3 do
puts 'Hi from Ruby!'
end
Hi from Ruby!
Hi from Ruby!
Hi from Ruby!
php:
<?php
for($i = 0; $i < 3; $i++) {
echo "Hi from PHP!\r\n";
} ?>
Hi from PHP!
Hi from PHP!
Hi from PHP!
groovy:
for(i in 1..3) {
println "Hi from Groovy!"
} Hi from Groovy!
Hi from Groovy!
Hi from Groovy!
beanshell:
for(int i = 0; i < 3; i++) {
System.out.println("Hi from BeanShell!");
} Hi from BeanShell!
Hi from BeanShell!
Hi from BeanShell!
perl:
for(1..3) {
print("Hi from Perl!\n");
} Hi from Perl!
Hi from Perl!
Hi from Perl!
lua:
for i = 1,3
do
print("Hi from Lua!")
end
Hi from Lua!
Hi from Lua!
Hi from Lua!
Lisp:
(dotimes (n 3)
(format t "Hi from Lisp!~%"))
Hi from Lisp!
Hi from Lisp!
Hi from Lisp!
tcl:
for {set i 0} {$i < 3} {incr i} {
puts "Hi from Tcl!"
} Hi from Tcl!
Hi from Tcl!
Hi from Tcl!

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

import org.luaj.vm2.script.LuajContext;

public class EmbeddedScript2 {
public static class Data {
public int iv;
public String sv;
public Data(int iv, String sv) {
this.iv = iv;
this.sv = sv;
}
public void m() {
iv++;
sv += "X";
}
@Override
public String toString() {
return String.format("(%d,%s)", iv, sv);
}
}
public static void test(String language, String source) throws
ScriptException {
System.out.println(language + ":");
System.out.print(source);
ScriptEngineManager sem = new ScriptEngineManager();
ScriptEngine se = sem.getEngineByName(language);
ScriptContext ctx;
switch(language) {
case "lua":
ctx = new LuajContext(); // LuaJ insist on getting a
LuajContext which is actually a sub class of SimpleScriptContext
break;
default:
ctx = new SimpleScriptContext();
break;
}
ctx.setAttribute("n", 3, ScriptContext.ENGINE_SCOPE);
ctx.setAttribute("o", new Data(123, "ABC"),
ScriptContext.ENGINE_SCOPE);
se.eval(source, ctx);
Data o = (Data)ctx.getAttribute("o");
System.out.println(o);
}
public static void main(String[] args) throws ScriptException {
test("javascript", "for(i = 0; i < n; i++) {\r\n" +
" print(\"Hi from JavaScript!\")\r\n" +
"}\r\n" +
"o.iv = o.iv + 1;\r\n" +
"o.sv = o.sv + \"X\";\r\n" +
"o.m();\r\n");
test("python", "for i in range(n):\r\n" +
" print(\"Hi from Python!\")\r\n" +
"o.iv = o.iv + 1\r\n" +
"o.sv = o.sv + 'X'\r\n" +
"o.m()\r\n");
test("ruby", "for i in 1..n do\r\n" +
" puts 'Hi from Ruby!'\r\n" +
"end\r\n" +
"o.iv = o.iv + 1\r\n" +
"o.sv = o.sv + 'X'\r\n" +
"o.m()\r\n");
test("php", "<?php\r\n" +
"for($i = 0; $i < $n; $i++) {\r\n" +
" echo \"Hi from PHP!\\r\\n\";\r\n" +
"}\r\n" +
"$o->iv = $o->iv + 1;\r\n" +
"$o->sv = $o->sv . 'X';\r\n" +
"$o->m();\r\n" +
"?>\r\n");
test("groovy", "for(i in 1..n) {\r\n" +
" println \"Hi from Groovy!\"\r\n" +
"}\r\n" +
"o.iv = o.iv + 1\r\n" +
"o.sv = o.sv + \"X\"\r\n" +
"o.m()\r\n");
test("beanshell", "for(int i = 0; i < n; i++) {\r\n" +
" System.out.println(\"Hi from
BeanShell!\");\r\n"+
"}\r\n" +
"o.iv = o.iv + 1;\r\n" +
"o.sv = o.sv + \"X\";\r\n" +
"o.m();\r\n");
// I cannot get perlito5 to work with context
test("lua", "for i = 1,n\r\n" +
"do\r\n" +
" print(\"Hi from Lua!\")\r\n" +
"end\r\n" +
"o.iv = o.iv + 1\r\n" +
"o.sv = o.sv .. \"X\"\r\n" +
"o:m()\r\n");
test("Lisp", "(dotimes (n n)\r\n" +
" (format t \"Hi from Lisp!~%\"))\r\n" +
"(setf (jfield \"iv\" o) (+ (jfield \"iv\" o)
1))\r\n" +
"(setf (jfield \"sv\" o) (concatenate 'string
(jfield \"sv\" o) \"X\"))\r\n" +
"(jcall \"m\" o)\r\n");
// I cannot get jacl to work with context
}
}


Click here to read the complete article
1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor