Introduction

Julia scripts are supported using the node-julia functions createScript and Script. Currently there is little difference between the 2 other than the internal implementation. Script is a constructor of a Script object which is a C++ object implemented by the node-julia module wrapped within a Javascript object named Script using the C++ wrapper support provided by node.js. The function createScript is a factory of Script. Both the constructor method as well as the factory method take 1 argument which is the file name of the Julia script to compile. Currently all three of these calls are equivalent.

var julia = require('node-julia');

var script1 = julia.Script('a_script.jl');
var script2 = new julia.Script('a_script.jl');
var script3 = julia.createScript('a_script.jl');

However, even though the above refer to the same file "a_script.jl" 3 different objects will be created.

Script functions

The Script object has 3 functions

  1. getPath
  2. getModuleName
  3. exec

getPath

This function will return the path of the file containing the script. It's essentially the constructor argument.

getModuleName

This function will return the module name associated with the script object. Each script is isolated within its own module, so that include directives will not pollute the global namespace. The module name will have the form njIsoModX, where X will be an integer serial starting from 0.

exec

Like the node-julia function exec, this function will execute the statements in the script. Any number of arguments can be passed to the script, and like node-julia exec, the result is passed back to a function callback. However, this function does not require a function name argument since it is determined at script creation time and stored with the object.

In action

For example, if the contents of the script file are the following:

sqrt(args[1])

then it can be compiled into a script object using the following:

var julia = require('node-julia');
var script = julia.Script('example_script.jl');

console.log('The script path: ' + script.getPath());
console.log('The script module name: ' + script.getModuleName());

script.exec(12345,function(err,res) {
   console.log('The sqrt of 12345 is ' + res);
});

and will produce the following output

The script path: example_script.jl
The script module name: njIsoMod0
The sqrt of 12345 is 111.1080555135405

Script.exec also supports both styles

Just like eval and exec, Script.exec can be called with or without a callback, thus the following is equivalent (keeping in mind that one is synchronous and the other is asynchronous:

console.log('The sqrt of 12345 is ' + script.exec(12345));