2. In this tutorial...
We learn how to run Javascript code in a Jolie service.
We will use the following Jolie program as an example. It asks an
external service, Calculator, to calculate x to the power of y.
Our objective is to implement Calculator in Javascript!
main
{
request.x = 2;
request.y = 3;
pow@Calculator( request )( result );
println@Console( "The result is: " + result )()
}
3. Define an interface
We first define an interface between the Jolie and Javascript
programs:
type PowRequest:void {
.x:double
.y:double
}
interface CalculatorIface {
RequestResponse:
pow( PowRequest )( double )
}
Save this interface in file calculator.iol
4. Write the Javascript program
We implement operation pow as a Javascript function:
function pow( request )
{
return Math.pow( request.x, request.y );
}
Save this program in file calculator.js
Above, parameter request is the Jolie data tree we will receive
from the Jolie program.
5. Embed the Javascript program
We use embedding to run the Javascript program inside of Jolie.
Save the following Jolie program in file main.ol
include "calculator.iol"
include "console.iol"
outputPort Calculator { Interfaces: CalculatorIface }
embedded {
JavaScript: "calculator.js" in Calculator
}
main {
request.x = 2; request.y = 3;
pow@Calculator( request )( result );
println@Console( "The result is: " + result )()
}
6. Embed the Javascript program
Here is what it all means:
include "calculator.iol"
include "console.iol"
outputPort Calculator { Interfaces: CalculatorIface }
embedded {
JavaScript: "calculator.js" in Calculator
}
main {
request.x = 2; request.y = 3;
pow@Calculator( request )( result );
println@Console( "The result is: " + result )()
}
Include the interface
Create an output port with
our interface.
Bind the output port to our
Javascript program
7. Files overview
You should now have the following files:
main.ol
calculator.iol
calculator.js Our Javascript program
Our Jolie program
Shared interface
9. Extend it
request.x = double( args[0] );
request.y = double( args[1] )
If you like, you can take the numbers as
command line arguments in your Jolie program:
jolie main.ol 3.0 4.0
For example, try running: