{"category":{"__v":2,"_id":"566e55c12c1e760d0030c56d","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","user":"544083bee239230800071bef","version":{"__v":1,"_id":"566e55c02c1e760d0030c56c","project":"54408e54309354080070a896","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"},"_id":"566e55c12c1e760d0030c579","__v":0,"updates":[],"next":{"pages":[],"description":""},"createdAt":"2014-10-19T00:51:43.122Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"auth":"never","params":[],"url":""},"isReference":false,"order":11,"body":"# Functionalizing \nNote that in the previous script example, **sqrt** is called with a variable *args*.  When the script is compiled, it is not only placed inside its own module as described previously, but is also wrapped inside its own function.  This wrapping function is declared to take a single argument *args...* which is a tuple of arbitrary size, or more precisely\n\n    function _(args...)\n    \nAnd if the module name is for example **njIsoMod0**, then the complete function would be\ninvoked as\n\n    njIsoMod0._(args...)\n    \nwhich is exactly what **script.exec** does.\n\n# Handling include\nThe use of include is allowed inside scripts, but because of how the script is isolated and functionalized, special treatment of included files is performed.  The default Julia include behavior is supported with 2 modifications.\n1. The include is evaluated local to the isolating module rather than the global main module.\n2. The containing script is transformed and evaluated as if all of the include invocations are hoisted to the top of the file and outside of the functionalized body.\n\n# Essentially the following\n\nConsider for example 3 files: *f.jl*, *g.jl*, and *fg.jl*.  Where\n    \n### f.jl\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"f(x)=x\",\n      \"language\": \"julia\"\n    }\n  ]\n}\n[/block]\n### g.fl\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"g(x)=f(x)^2\",\n      \"language\": \"julia\"\n    }\n  ]\n}\n[/block]\n### fg.jl\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"x = args[1];\\ny = args[2];\\nz = f(x)*g(y);\\ninclude(\\\"f.jl\\\");\\ninclude(\\\"g.jl\\\");\\n\\\"f*g = $z\\\"\",\n      \"language\": \"julia\"\n    }\n  ]\n}\n[/block]\nthe file *f.jl* defines the function **f** and the file *g.jl* defines the function **g**, and *fg.jl* will be included as a script.  Assuming the name of the isolating module is named **njIsoMod0**, the above is essentially the same as\n\n### fg.jl transformed\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"module njIsoMod0\\n\\nf(x)=x\\ng(x)=f(x)^2\\n\\nfunction _(args...)\\n   x = args[1];\\n   y = args[2];\\n   z = f(x)*g(y);\\n   \\\"f*g = $z\\\"\\nend\\n\\nend\",\n      \"language\": \"julia\"\n    }\n  ]\n}\n[/block]\nAnd if fg.jl is scriptified, using the following\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"var julia = require(\\\"node-julia\\\");\\nvar script = julia.Script(\\\"fg.jl\\\");\\n\\nconsole.log(script.exec(2,3));\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nThe following output is produced\n    \n    f*g = 18\n    \nNote that *g.jl* depends on *f.jl* being evaluated first, which is ok as hoisting maintains include order, and that even though the expression **z = f(x)** ***** **g(x)** depends on **f(x)** and **g(x)** aready being defined and that the includes of *f.jl* and *g.jl* occur after the expression, the include directives are hoisted outside *_* and evaluated first.","excerpt":"","slug":"script-internals","type":"basic","title":"Script Internals"}
# Functionalizing Note that in the previous script example, **sqrt** is called with a variable *args*. When the script is compiled, it is not only placed inside its own module as described previously, but is also wrapped inside its own function. This wrapping function is declared to take a single argument *args...* which is a tuple of arbitrary size, or more precisely function _(args...) And if the module name is for example **njIsoMod0**, then the complete function would be invoked as njIsoMod0._(args...) which is exactly what **script.exec** does. # Handling include The use of include is allowed inside scripts, but because of how the script is isolated and functionalized, special treatment of included files is performed. The default Julia include behavior is supported with 2 modifications. 1. The include is evaluated local to the isolating module rather than the global main module. 2. The containing script is transformed and evaluated as if all of the include invocations are hoisted to the top of the file and outside of the functionalized body. # Essentially the following Consider for example 3 files: *f.jl*, *g.jl*, and *fg.jl*. Where ### f.jl [block:code] { "codes": [ { "code": "f(x)=x", "language": "julia" } ] } [/block] ### g.fl [block:code] { "codes": [ { "code": "g(x)=f(x)^2", "language": "julia" } ] } [/block] ### fg.jl [block:code] { "codes": [ { "code": "x = args[1];\ny = args[2];\nz = f(x)*g(y);\ninclude(\"f.jl\");\ninclude(\"g.jl\");\n\"f*g = $z\"", "language": "julia" } ] } [/block] the file *f.jl* defines the function **f** and the file *g.jl* defines the function **g**, and *fg.jl* will be included as a script. Assuming the name of the isolating module is named **njIsoMod0**, the above is essentially the same as ### fg.jl transformed [block:code] { "codes": [ { "code": "module njIsoMod0\n\nf(x)=x\ng(x)=f(x)^2\n\nfunction _(args...)\n x = args[1];\n y = args[2];\n z = f(x)*g(y);\n \"f*g = $z\"\nend\n\nend", "language": "julia" } ] } [/block] And if fg.jl is scriptified, using the following [block:code] { "codes": [ { "code": "var julia = require(\"node-julia\");\nvar script = julia.Script(\"fg.jl\");\n\nconsole.log(script.exec(2,3));", "language": "javascript" } ] } [/block] The following output is produced f*g = 18 Note that *g.jl* depends on *f.jl* being evaluated first, which is ok as hoisting maintains include order, and that even though the expression **z = f(x)** ***** **g(x)** depends on **f(x)** and **g(x)** aready being defined and that the includes of *f.jl* and *g.jl* occur after the expression, the include directives are hoisted outside *_* and evaluated first.