{"_id":"566e55c12c1e760d0030c570","category":{"_id":"566e55c12c1e760d0030c56d","__v":2,"pages":["566e55c12c1e760d0030c56e","566e55c12c1e760d0030c56f","566e55c12c1e760d0030c570","566e55c12c1e760d0030c571","566e55c12c1e760d0030c572","566e55c12c1e760d0030c573","566e55c12c1e760d0030c574","566e55c12c1e760d0030c575","566e55c12c1e760d0030c576","566e55c12c1e760d0030c577","566e55c12c1e760d0030c578","566e55c12c1e760d0030c579","566e55c12c1e760d0030c57a","566e55c12c1e760d0030c57b","566e55c12c1e760d0030c57c","566e55c12c1e760d0030c57d","566e55c12c1e760d0030c57e","566e55c12c1e760d0030c57f","566e55c12c1e760d0030c580","566e55c12c1e760d0030c581","566e55c12c1e760d0030c582","566e55c12c1e760d0030c583","566e55c12c1e760d0030c584","566e55c12c1e760d0030c585","566e55c12c1e760d0030c586","566e55c12c1e760d0030c587","566e55c12c1e760d0030c588","566e5640c15c8f0d000ee863"],"project":"54408e54309354080070a896","version":"566e55c02c1e760d0030c56c","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2014-10-17T03:34:44.886Z","from_sync":false,"order":0,"slug":"documentation","title":"Documentation"},"project":"54408e54309354080070a896","version":{"_id":"566e55c02c1e760d0030c56c","project":"54408e54309354080070a896","__v":1,"createdAt":"2015-12-14T05:38:08.400Z","releaseDate":"2015-12-14T05:38:08.400Z","categories":["566e55c12c1e760d0030c56d"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.2.3","version":"1.2.3"},"__v":0,"user":"544083bee239230800071bef","updates":[],"next":{"pages":[],"description":""},"createdAt":"2014-10-19T00:12:43.905Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"auth":"never","params":[],"url":""},"isReference":false,"order":2,"body":"#Introduction\nJulia 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.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"var julia = require('node-julia');\\n\\nvar script1 = julia.Script('a_script.jl');\\nvar script2 = new julia.Script('a_script.jl');\\nvar script3 = julia.createScript('a_script.jl');\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nHowever, even though the above refer to the same file \"a_script.jl\" 3 different objects will be created.\n\n# Script functions\n\nThe Script object has 3 functions\n1. getPath\n2. getModuleName\n3. exec\n\n## getPath\nThis function will return the path of the file containing the script.  It's essentially the constructor argument.\n\n## getModuleName\nThis 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 **njIsoMod*X***, where ***X*** will be an integer serial starting from 0.\n\n## exec\nLike 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.\n\n## In action\nFor example, if the contents of the script file are the following:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"sqrt(args[1])\",\n      \"language\": \"julia\",\n      \"name\": \"example_script.jl\"\n    }\n  ]\n}\n[/block]\nthen it can be compiled into a script object using the following:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"var julia = require('node-julia');\\nvar script = julia.Script('example_script.jl');\\n\\nconsole.log('The script path: ' + script.getPath());\\nconsole.log('The script module name: ' + script.getModuleName());\\n\\nscript.exec(12345,function(err,res) {\\n   console.log('The sqrt of 12345 is ' + res);\\n});\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nand will produce the following output\n\n    The script path: example_script.jl\n    The script module name: njIsoMod0\n    The sqrt of 12345 is 111.1080555135405\n    \n## Script.exec also supports both styles\nJust 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](doc:synchronous-vs-asynchronous):\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"console.log('The sqrt of 12345 is ' + script.exec(12345));\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]","excerpt":"","slug":"scripts","type":"basic","title":"Scripts"}
#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. [block:code] { "codes": [ { "code": "var julia = require('node-julia');\n\nvar script1 = julia.Script('a_script.jl');\nvar script2 = new julia.Script('a_script.jl');\nvar script3 = julia.createScript('a_script.jl');", "language": "javascript" } ] } [/block] 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 **njIsoMod*X***, 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: [block:code] { "codes": [ { "code": "sqrt(args[1])", "language": "julia", "name": "example_script.jl" } ] } [/block] then it can be compiled into a script object using the following: [block:code] { "codes": [ { "code": "var julia = require('node-julia');\nvar script = julia.Script('example_script.jl');\n\nconsole.log('The script path: ' + script.getPath());\nconsole.log('The script module name: ' + script.getModuleName());\n\nscript.exec(12345,function(err,res) {\n console.log('The sqrt of 12345 is ' + res);\n});", "language": "javascript" } ] } [/block] 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](doc:synchronous-vs-asynchronous): [block:code] { "codes": [ { "code": "console.log('The sqrt of 12345 is ' + script.exec(12345));", "language": "javascript" } ] } [/block]