Files
2026-05-22 11:14:29 +02:00

107 lines
3.8 KiB
Plaintext

<snippet>
<subclass>afryca.ase.Snippet</subclass>
<file></file>
<category>ASE-API</category>
<name>Scripting examples</name>
<description></description>
<code>
var js = 'function product(x, y) {\n'
js += ' return x * y;\n';
js += '}';
var groovy = 'def product(Double x, Double y) {\n'
groovy += ' return x * y\n';
groovy += '}';
var r = 'product <- function(x ,y) {\n';
r += ' return (x * y)\n';
r += '}';
var python = 'def product(x , y):\n';
python += ' return x * y';
var ruby = 'def product(x, y)\n';
ruby += ' return x * y\n';
ruby += 'end';
var lua = 'function product(x, y)\n';
lua += ' return x * y\n';
lua += 'end';
var scala = 'def product(x:Double, y:Double) : Double = {\n';
scala += ' return x * y\n';
scala += '}';
if (typeof Groovy !== 'undefined') Groovy.eval(groovy);
if (typeof R !== 'undefined') R.eval(r);
if (typeof Jython !== 'undefined') Jython.eval(python);
if (typeof JRuby !== 'undefined') JRuby.eval(ruby);
if (typeof LuaJ !== 'undefined') LuaJ.eval(lua);
if (typeof Scala !== 'undefined') Scala.eval(scala);
eval(js);
var x = 3;
var y = 4;
var code = 'product(' + x + ',' + y + ')';
var notLoaded = 'is not loaded';
var nashorn = eval(code);
var groovyResult = (typeof Groovy !== 'undefined') ? Groovy.eval(code) : notLoaded;
var rengine = (typeof R !== 'undefined') ? R.eval(code) : notLoaded;
var jython = (typeof Jython !== 'undefined') ? Jython.eval(code) : notLoaded;
var jruby = (typeof JRuby !== 'undefined') ? JRuby.eval(code) : notLoaded;
if (typeof LuaJ !== 'undefined') LuaJ.eval('result = ' + code); // LuaJ does not return eval result
var luaj = (typeof LuaJ !== 'undefined') ? LuaJ.get('result') : notLoaded;
var scalaResult = (typeof Scala !== 'undefined') ? Scala.eval(code) : notLoaded;
var mode1 = 'Nashorn: ' + nashorn + '\n' +
'Groovy: ' + groovyResult + '\n' +
'R: ' + rengine + '\n' +
'Jython: ' + jython + '\n' +
'JRuby: ' + jruby + '\n' +
'LuaJ: ' +luaj + '\n'+
'Scala: ' + scalaResult
groovyResult = (typeof Groovy !== 'undefined') ? ase.evalCode(Groovy, code) : notLoaded;
rengine = (typeof R !== 'undefined') ? ase.evalCode(R, code) : notLoaded;
jython = (typeof Jython !== 'undefined') ? ase.evalCode(Jython, code) : notLoaded;
jruby = (typeof JRuby !== 'undefined') ? ase.evalCode(JRuby, code) : notLoaded;
luaj = (typeof LuaJ !== 'undefined') ? ase.evalCode(LuaJ, 'result = ' + code + ' -- ase get result') : notLoaded; // ASE shortcut
scalaResult = (typeof Scala !== 'undefined') ? ase.evalCode(Scala, code) : notLoaded;
var mode2 = 'Groovy: ' + groovyResult + '\n' +
'R: ' + rengine + '\n' +
'Jython: ' + jython + '\n' +
'JRuby: ' + jruby + '\n' +
'LuaJ: ' +luaj + '\n'+
'Scala: ' + scalaResult
groovyResult = ase.evalCode('Groovy', code);
rengine = ase.evalCode('R', code);
jython = ase.evalCode('Jython', code);
jruby = ase.evalCode('JRuby', code);
luaj = ase.evalCode('LuaJ', 'result = ' + code + ' -- ase get result'); // ASE shortcut
scalaResult = ase.evalCode('Scala', code);
var mode3 = 'Groovy: ' + groovyResult + '\n' +
'R: ' + rengine + '\n' +
'Jython: ' + jython + '\n' +
'JRuby: ' + jruby + '\n' +
'LuaJ: ' +luaj + '\n'+
'Scala: ' + scalaResult
'Mode 1\n' + mode1 + '\n\nMode 2\n' + mode2 + '\n\nMode 3\n' + mode3
//
// IMPORTANT INFORMATION ABOUT MODES
//
// - Mode 1 is slightly faster than mode 2
// - Mode 2 is slightly faster than mode 3
// - Mode 1 can only be used in ASE
// - Modes 2 and 3 can be used in ASE or in AFRYCA plug-ins source code
// - Modes 1 and 2 fail if the engine is not loaded
// - Mode 3 return null if the engine is not loaded
</code>
</snippet>