{"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":"566e55c12c1e760d0030c572","__v":0,"updates":[],"next":{"pages":[],"description":""},"createdAt":"2014-10-19T05:00:56.213Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"auth":"never","params":[],"url":""},"isReference":false,"order":4,"body":"# Introduction\nJulia is strongly typed while JavaScript is loosely typed, and so care must be taken to best define rules for moving data between the two languages. There are two considerations.  \n\n1. How to identify the most efficient type for a new variable and mechanism for its creation.\n2. How to avoid unnecessary object creation.\n\n# From JavaScript to Julia\nSince Julia derives a good deal of its efficiency by being able to pass typing information to LLVM, as much typing information as possible is passed from JavaScript.  Not only is the source type taken into consideration, but the value as well.\n\n## From JavaScript Primitive Types\n[block:parameters]\n{\n  \"data\": {\n    \"h-0\": \"JavaScript Type\",\n    \"h-1\": \"JavaScript Value\",\n    \"h-2\": \"Julia Type\",\n    \"0-0\": \"null\",\n    \"0-1\": \"null\",\n    \"0-2\": \"Void\",\n    \"1-0\": \"Boolean\",\n    \"1-1\": \"true or false\",\n    \"1-2\": \"Bool\",\n    \"2-0\": \"Number\",\n    \"2-1\": \"integer\",\n    \"2-2\": \"Int64\",\n    \"3-0\": \"Number\",\n    \"3-1\": \"not an integer\",\n    \"3-2\": \"Float64\",\n    \"4-0\": \"String\",\n    \"4-1\": \"ASCII or UTF8\",\n    \"4-2\": \"UTF8String\",\n    \"5-0\": \"Date\",\n    \"5-1\": \"Date\",\n    \"5-2\": \"DateTime*\",\n    \"6-0\": \"RegExp\",\n    \"6-1\": \"Javascipt regular expression\",\n    \"6-2\": \"Regex\"\n  },\n  \"cols\": 3,\n  \"rows\": 7\n}\n[/block]\n### Date to DateTime*\nSupport for automatic conversion from Javascript's *Date* to Julia's *DateTime* has been added, however, the type *DateTime* was only added to Julia base in version 0.4, and so this feature is currently only supported when node-julia is installed against that version of Julia.\n\n### RegExp to Regex\nJavascript regular expressions are converted to Julia regular expressions.  The format of Javascript regex and Julia regex are similar but not exactly the same, though they are both based on [PCRE](http://www.pcre.org/).  But although the format is different, the type of the constructing argument pattern is *String* which is passed exactly as specified.\n\n## From JavaScript Arrays\nPassing JavaScript arrays to Julia presents a special problem.  Because JavaScript does not differentiate arrays based on the type of the elements they contain, any sort of typing that Julia needs for optimizing would have to be inferred.  Also, JavaScript multidimensional arrays are structured as arrays of arrays unlike Julia which again makes JavaScript arrays inefficient by comparison and conversion to equivalent Julia arrays tedious. And finally, if anything, JavaScript arrays are structured row-major while Julia arrays are column major.  \n\n### Arrays passed to Julia are assumed to be regular.\nCurrently it is an error if the source array is irregular. To enforce this, the source array is examined, and if it is multidimensional, then all sibling arrays must have the same dimensions.  For example this is a valid way to take the transpose of a 2x3 array\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"res = julia.exec('transpose',[[ 1, 2, 3 ], [ 4, 5, 6 ]]);\\nconsole.log('transpose is ',res);\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nwhich produces the following output\n\n    transpose is  [ { '0': 1, '1': 4 }, { '0': 2, '1': 5 }, { '0': 3, '1': 6 } ]\n\nhowever, this is an error\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"console.log('transpose is ',julia.exec('transpose',[[ 1, 2, 3 ], [ 4, 5 ]]));\\n\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nand produces the following exception\n\n    console.log('transpose is ',julia.exec('transpose',[[ 1, 2, 3 ], [ 4, 5 ]]));\n                                      ^\n    Error: Malformed input array\n        at Error (native)\n        \nsince [ 1, 2, 3 ] has different length (3) than [ 4, 5 ] (2).\n\n### Arrays are assumed to be homogeneous.\nJulia requires that the type of an array includes the element type and since Julia uses this information to select the method, and assuming the narrowest type will likely give rise to the most efficient method, the most narrow type is selected from all the elements as the Julia element type where **Boolean** is narrower than **Integer** is narrower than **Number**.  In the case of **Boolean** appearing in array with **Number**, **false** is mapped to 0 and **true** is mapped to 1.  It is currently an error for **String** to appear with anything other than **String**.  \n\n### Type *Any* currently not supported.\nJulia does all allow for heterogeneous Arrays by defining the umbrella type **Any** which is implicitly slow as the type must accompany the value.  Thus this option is currently not available, but will be in an upcoming release.\n\n### Arrays of null\nArrays of null values are also allowed but share the same property as **String**; they can only exist alongside other null elements.  Each element is mapped to the Julia value **nothing**.\n\n### Arrays of Native Arrays\nTo support speedy multidimensional arrays, mixed use of arrays and NativeArrays are allowed.  The ultimate type will of this combination will be determined using homogeneity rules above see [Buffer and Native Arrays](doc:buffer-and-native-arrays) for more information.\n\n# From Julia to JavaScript\nThis direction is easier to define since Julia types are more specific than those of JavaScript.\n\n## From Julia\nThe table below shows how the following Julia types are mapped to their corresponding JavaScript types.\n[block:parameters]\n{\n  \"data\": {\n    \"h-0\": \"Julia Type\",\n    \"h-1\": \"JavaScript Type\",\n    \"0-0\": \"Void\",\n    \"0-1\": \"null\",\n    \"1-0\": \"Bool\",\n    \"1-1\": \"Boolean\",\n    \"2-0\": \"Int8, Uint8, Int16, UInt16, Int32, UInt32\",\n    \"2-1\": \"Number\",\n    \"3-0\": \"Float32, Float64\",\n    \"3-1\": \"Number\",\n    \"4-0\": \"Int64, UInt64\",\n    \"4-1\": \"Number**\",\n    \"5-0\": \"ASCIIString,UTF8String\",\n    \"5-1\": \"String\",\n    \"6-0\": \"SubString\",\n    \"6-1\": \"String\",\n    \"7-0\": \"DateTime*\",\n    \"7-1\": \"Date\",\n    \"8-0\": \"Regex\",\n    \"8-1\": \"RegExp\",\n    \"9-0\": \"Datatype\",\n    \"9-1\": \"JRef***\"\n  },\n  \"cols\": 2,\n  \"rows\": 10\n}\n[/block]\n### Julia Int64 and UInt64 will be mapped to Number **\nJulia **Int64** and **Uint64** will be mapped to **Number** but for numbers larger than 9007199254740992, there will be loss of precision since JavaScript Numbers are internally represented using double.\n\n### Other Julia Datatypes Mapped to JRef ***\nBeyond the basic mapping, Julia composites are retuned as JRefs which are an opaque type that can be passed back to Julia as arguments to **eval**, **exec**, and **Script.exec**, see [JRef](doc:jref) ] for more information.\n\n## From Julia Arrays\nJavaScript NativeArrays are used whenever possible.  Thus when the Julia return type is an array of Integer or Float, even if multidimensional, the result will contain NativeArrays as the most derived children.  This support is paired with allowing arrays of NativeArrays as input.","excerpt":"","slug":"datatype-mapping","type":"basic","title":"Datatype Mapping"}
# Introduction Julia is strongly typed while JavaScript is loosely typed, and so care must be taken to best define rules for moving data between the two languages. There are two considerations. 1. How to identify the most efficient type for a new variable and mechanism for its creation. 2. How to avoid unnecessary object creation. # From JavaScript to Julia Since Julia derives a good deal of its efficiency by being able to pass typing information to LLVM, as much typing information as possible is passed from JavaScript. Not only is the source type taken into consideration, but the value as well. ## From JavaScript Primitive Types [block:parameters] { "data": { "h-0": "JavaScript Type", "h-1": "JavaScript Value", "h-2": "Julia Type", "0-0": "null", "0-1": "null", "0-2": "Void", "1-0": "Boolean", "1-1": "true or false", "1-2": "Bool", "2-0": "Number", "2-1": "integer", "2-2": "Int64", "3-0": "Number", "3-1": "not an integer", "3-2": "Float64", "4-0": "String", "4-1": "ASCII or UTF8", "4-2": "UTF8String", "5-0": "Date", "5-1": "Date", "5-2": "DateTime*", "6-0": "RegExp", "6-1": "Javascipt regular expression", "6-2": "Regex" }, "cols": 3, "rows": 7 } [/block] ### Date to DateTime* Support for automatic conversion from Javascript's *Date* to Julia's *DateTime* has been added, however, the type *DateTime* was only added to Julia base in version 0.4, and so this feature is currently only supported when node-julia is installed against that version of Julia. ### RegExp to Regex Javascript regular expressions are converted to Julia regular expressions. The format of Javascript regex and Julia regex are similar but not exactly the same, though they are both based on [PCRE](http://www.pcre.org/). But although the format is different, the type of the constructing argument pattern is *String* which is passed exactly as specified. ## From JavaScript Arrays Passing JavaScript arrays to Julia presents a special problem. Because JavaScript does not differentiate arrays based on the type of the elements they contain, any sort of typing that Julia needs for optimizing would have to be inferred. Also, JavaScript multidimensional arrays are structured as arrays of arrays unlike Julia which again makes JavaScript arrays inefficient by comparison and conversion to equivalent Julia arrays tedious. And finally, if anything, JavaScript arrays are structured row-major while Julia arrays are column major. ### Arrays passed to Julia are assumed to be regular. Currently it is an error if the source array is irregular. To enforce this, the source array is examined, and if it is multidimensional, then all sibling arrays must have the same dimensions. For example this is a valid way to take the transpose of a 2x3 array [block:code] { "codes": [ { "code": "res = julia.exec('transpose',[[ 1, 2, 3 ], [ 4, 5, 6 ]]);\nconsole.log('transpose is ',res);", "language": "javascript" } ] } [/block] which produces the following output transpose is [ { '0': 1, '1': 4 }, { '0': 2, '1': 5 }, { '0': 3, '1': 6 } ] however, this is an error [block:code] { "codes": [ { "code": "console.log('transpose is ',julia.exec('transpose',[[ 1, 2, 3 ], [ 4, 5 ]]));\n", "language": "javascript" } ] } [/block] and produces the following exception console.log('transpose is ',julia.exec('transpose',[[ 1, 2, 3 ], [ 4, 5 ]])); ^ Error: Malformed input array at Error (native) since [ 1, 2, 3 ] has different length (3) than [ 4, 5 ] (2). ### Arrays are assumed to be homogeneous. Julia requires that the type of an array includes the element type and since Julia uses this information to select the method, and assuming the narrowest type will likely give rise to the most efficient method, the most narrow type is selected from all the elements as the Julia element type where **Boolean** is narrower than **Integer** is narrower than **Number**. In the case of **Boolean** appearing in array with **Number**, **false** is mapped to 0 and **true** is mapped to 1. It is currently an error for **String** to appear with anything other than **String**. ### Type *Any* currently not supported. Julia does all allow for heterogeneous Arrays by defining the umbrella type **Any** which is implicitly slow as the type must accompany the value. Thus this option is currently not available, but will be in an upcoming release. ### Arrays of null Arrays of null values are also allowed but share the same property as **String**; they can only exist alongside other null elements. Each element is mapped to the Julia value **nothing**. ### Arrays of Native Arrays To support speedy multidimensional arrays, mixed use of arrays and NativeArrays are allowed. The ultimate type will of this combination will be determined using homogeneity rules above see [Buffer and Native Arrays](doc:buffer-and-native-arrays) for more information. # From Julia to JavaScript This direction is easier to define since Julia types are more specific than those of JavaScript. ## From Julia The table below shows how the following Julia types are mapped to their corresponding JavaScript types. [block:parameters] { "data": { "h-0": "Julia Type", "h-1": "JavaScript Type", "0-0": "Void", "0-1": "null", "1-0": "Bool", "1-1": "Boolean", "2-0": "Int8, Uint8, Int16, UInt16, Int32, UInt32", "2-1": "Number", "3-0": "Float32, Float64", "3-1": "Number", "4-0": "Int64, UInt64", "4-1": "Number**", "5-0": "ASCIIString,UTF8String", "5-1": "String", "6-0": "SubString", "6-1": "String", "7-0": "DateTime*", "7-1": "Date", "8-0": "Regex", "8-1": "RegExp", "9-0": "Datatype", "9-1": "JRef***" }, "cols": 2, "rows": 10 } [/block] ### Julia Int64 and UInt64 will be mapped to Number ** Julia **Int64** and **Uint64** will be mapped to **Number** but for numbers larger than 9007199254740992, there will be loss of precision since JavaScript Numbers are internally represented using double. ### Other Julia Datatypes Mapped to JRef *** Beyond the basic mapping, Julia composites are retuned as JRefs which are an opaque type that can be passed back to Julia as arguments to **eval**, **exec**, and **Script.exec**, see [JRef](doc:jref) ] for more information. ## From Julia Arrays JavaScript NativeArrays are used whenever possible. Thus when the Julia return type is an array of Integer or Float, even if multidimensional, the result will contain NativeArrays as the most derived children. This support is paired with allowing arrays of NativeArrays as input.