As usual we are not speaking of mixing JOLIE code with another language, which would result in incomprehensible JOLIE programs. We are instead speaking of the insertion of another language in JOLIE's service-oriented paradigm, by means of the embedding mechanism (which we are detailing in our new, shiny tutorials, to be published really soon).
Embedding a JavaScript service goes like this:
// main.ol
include "console.iol"
// Standard JOLIE service declaration
outputPort Math {
RequestResponse: sum
}
embedded {
// We load our JavaScript file as the JS service
JavaScript:
"mathservice.js" in Math
}
main
{
// Now we can call JS as every other service type supported by JOLIE
with( request ) {
.addend[0] = 2;
.addend[1] = 4;
.addend[2] = 1;
.addend[3] = 3
};
sum@Math( request )( response );
println@Console( response ) // Will print 10
}
The JavaScript API for accessing structured JOLIE data is the same as the one for Java. Let us see how the Math service is implemented:
// mathservice.js
function sum( request )
{
var addends = request.getChildren( "addend" );
var total = 0;
for( var i = 0; i < addends.size(); i++ ) {
total += addends.get( i ).intValue();
}
return total;
}
VoilĂ ! Simple, isn't it?
Support for other scripting programming languages will be introduced in the future, stay tuned! =)

3 comments:
Using Javascript's 'with' is considered a bad idea - something to do with the way in which names are looked up. In general, it's better to assign the parameter of the 'with' to a temporary variable, and use that as a prefix for the statements otherwise enclosed by the 'with'.
Yes, that would be the case if my with block were actually JavaScript. The code in which you see the with thing is JOLIE code, so that's a JOLIE with construct. =)
Interesting blog...I like much the code examples you have, for some blogs do not do a good job with those sorts of things.
---------------------------------
Like programming
http://eschembor.wordpress.com/
Post a Comment