Introduction

Julia modules may be imported from Javascript using a syntax similar to that used by common.js. Upon import of a Julia package, a Javascript object is created with function objects corresponding to the exported functions declared in the module declared by the Julia package.

Julia Dict to JSON to Javascript

The following example illustrates the concept:

var julia = require('node-julia');
var juliaJSON = julia.import('JSON');

// Create a JRef of a Julia Dict then export that JSON of
// the Dict, but Julia exports this as a string, so then
// use JSON.parse on the output.

var x = julia.eval('Dict("a" => "b")');
var y = juliaJSON.json(x);

console.log(JSON.parse(y));

The functionNames variable

In addition to all of the functions exported from the Julia package, being equated to Javascript functions, the functionNames variable is an array of strings of all of names of those functions and can be seen using the following example:

var julia = require('node-julia');
var JuMP = julia.import('JuMP');

console.log(JSON.stringify(JuMP.functionNames,null,2));

Output

[
  "@LinearConstraint",
  "@LinearConstraints",
  "@QuadConstraint",

...

  "setLower",
  "setName",
  "setObjective",
  "setObjectiveSense",
  "setPrintHook",
  "setSolutionValue!",
  "setSolveHook",
  "setSolver",
  "setUpper",
  "setValue",
  "solve",
  "writeLP",
  "writeMPS"
]

Caveat for Macro Functions

Note how macro names are also included, however there are 2 things to remember here. First, the character @ is illegal as a field identifier in Javascript, so referring to the example above, @LinearConstraint could be invoked using the following JuMP['@LinearConstraint'](...), but currently the effect is not what is expected; a quote block is returned rather than an inline expansion and evaluation. Thus, macros invoked in this way will be seldom used, nevertheless, they are included for completeness.

Example: using JuMP

Extending the above example, and taking from the JuMP simple example, and noting the use of julia.eval(...) when invoking macros, JuMP can be used as follows:

var julia = require('node-julia');
var JuMP = julia.import('JuMP');
var m = julia.eval('m = JuMP.Model()');
var x = julia.eval('JuMP.@defVar(m,0 <= x <= 2)');
var y = julia.eval('JuMP.@defVar(m,0 <= y <= 30)');

julia.eval('JuMP.@setObjective(m,Max, 5x + 3*y)');
julia.eval('JuMP.@addConstraint(m,1x + 5y <= 3.0)');

var status = JuMP.solve(m);

console.log('Objective value: ',JuMP.getObjectiveValue(m));
console.log('X value: ',JuMP.getValue(x));
console.log('Y value: ',JuMP.getValue(y));

Output

Objective value:  10.6
X value:  2
Y value:  0.2