{"id":384,"date":"2017-10-27T16:37:09","date_gmt":"2017-10-27T16:37:09","guid":{"rendered":"http:\/\/www.onux.com\/jspp\/blog\/?p=384"},"modified":"2017-11-03T03:33:30","modified_gmt":"2017-11-03T03:33:30","slug":"jspp-0-7-0-javascript-fully-implemented","status":"publish","type":"post","link":"https:\/\/www.onux.com\/jspp\/blog\/jspp-0-7-0-javascript-fully-implemented\/","title":{"rendered":"JS++ 0.7.0: JavaScript Fully Implemented"},"content":{"rendered":"<p>JS++ 0.7.0 has arrived. Everything that is possible to do in JavaScript is now possible to do in JS++. JS++ has fully implemented JavaScript and <a href=\"https:\/\/docs.onux.com\/en-US\/Developers\/JavaScript-PP\/Standard-Library\/System\" rel=\"noopener\" target=\"_blank\">the JS++ Standard Library is now fully documented<\/a>.<\/p>\n<p>This means that every JavaScript feature you expect is now available in JS++: arrays (0.7.0 introduces <a href=\"https:\/\/docs.onux.com\/en-US\/Developers\/JavaScript-PP\/Standard-Library\/System\/Array\" rel=\"noopener\" target=\"_blank\">the generic Array&lt;T&gt; class<\/a>), functions, date\/time, regular expressions, loops, bitwise operators, etc. This is in addition to several JS++-only features: <a href=\"https:\/\/docs.onux.com\/en-US\/Developers\/JavaScript-PP\/Language\/Reference\/Statements\/foreach\" rel=\"noopener\" target=\"_blank\">foreach<\/a>, modules, classes, imports, <a href=\"https:\/\/docs.onux.com\/en-US\/Developers\/JavaScript-PP\/Language\/Reference\/Statements\/enum\" rel=\"noopener\" target=\"_blank\">enums<\/a>, virtual functions, integer types, optimized auto-boxing, function overloading, dead code elimination (DCE), and so on.<\/p>\n<p>You can find all the current Standard Library documentation by <a href=\"https:\/\/docs.onux.com\/en-US\/Developers\/JavaScript-PP\/Standard-Library\/System\">clicking here<\/a>. Every documented class, method, and field (with examples) should be available and work in 0.7.0.<\/p>\n<h3>System.Array&lt;T&gt;<\/h3>\n<p>The major missing feature for a while now has been arrays. JS++ 0.7.0 brings the generic System.Array&lt;T&gt; class to JS++. With this addition, everything you can do in JavaScript is possible in JS++.<\/p>\n<p>The full documentation for System.Array&lt;T&gt; is available <a href=\"https:\/\/docs.onux.com\/en-US\/Developers\/JavaScript-PP\/Standard-Library\/System\/Array\" rel=\"noopener\" target=\"_blank\">here<\/a>.<\/p>\n<p>Additionally, JS++ adds more features to the <code>Array<\/code> API that aren&#8217;t available in JavaScript such as <a href=\"https:\/\/docs.onux.com\/en-US\/Developers\/JavaScript-PP\/Standard-Library\/System\/Array\/clear\" rel=\"noopener\" target=\"_blank\">clear<\/a>, <a href=\"https:\/\/docs.onux.com\/en-US\/Developers\/JavaScript-PP\/Standard-Library\/System\/Array\/contains\" rel=\"noopener\" target=\"_blank\">contains<\/a>, <a href=\"https:\/\/docs.onux.com\/en-US\/Developers\/JavaScript-PP\/Standard-Library\/System\/Array\/remove\" rel=\"noopener\" target=\"_blank\">remove<\/a>, <a href=\"https:\/\/docs.onux.com\/en-US\/Developers\/JavaScript-PP\/Standard-Library\/System\/Array\/first\" rel=\"noopener\" target=\"_blank\">first<\/a>, <a href=\"https:\/\/docs.onux.com\/en-US\/Developers\/JavaScript-PP\/Standard-Library\/System\/Array\/last\" rel=\"noopener\" target=\"_blank\">last<\/a>, <a href=\"https:\/\/docs.onux.com\/en-US\/Developers\/JavaScript-PP\/Standard-Library\/System\/Array\/count\" rel=\"noopener\" target=\"_blank\">count<\/a>, and more. These methods are all <em>optimized<\/em>; in other words, there is no performance overhead in using them. In addition, we benchmark each method to make sure we are always providing the fastest implementation for an abstraction you want.<\/p>\n<h3>System.Array.sort and IComparable&lt;T&gt;<\/h3>\n<p>It comes as no surprise that JavaScript has strange behavior. Consider this JavaScript code:<\/p>\n<pre class=\"brush:js\">\r\nvar arr = [ 1, 2, 10, 9 ];\r\narr.sort();\r\nconsole.log(arr); \/\/ [ 1, 10, 2, 9 ]\r\n<\/pre>\n<p>As you can see, JavaScript did not sort the numeric array correctly. The reason for this behavior is because JavaScript performs a string sort regardless of the types in the array. Since JS++ is statically-typed, we can generate code for a correct sort with zero overhead. In JS++, the System.IComparable&lt;T&gt; interface provides exactly this behavior. If a class implements System.IComparable&lt;T&gt;, it can provide custom sorting behavior.<\/p>\n<p>All Standard Library wrapper classes implement System.IComparable&lt;T&gt;. Thus, all JS++ numeric types will be sorted <em>numerically<\/em> when used inside an array &#8211; as you would expect from a modern, well-designed language. Here&#8217;s an example:<\/p>\n<pre class=\"brush:jspp\">\r\nimport System;\r\n\r\nint[] arr = [ 1, 2, 10, 9 ]; \/\/ make sure you use an internal type like 'int[]' and not an external type like 'var'\r\narr.sort();\r\n<\/pre>\n<p>And here&#8217;s the generated code:<\/p>\n<pre class=\"brush:js\">\r\n\/\/ Compiled with JS++ v.0.7.0\r\n! function() {\r\n    ! function() {\r\n        var arr = [1, 2, 10, 9];\r\n        arr.sort(function(a, b) {\r\n            return (a - b);\r\n        });\r\n    }();\r\n}();\r\n<\/pre>\n<p>As you can see, there is <em>zero<\/em> overhead &#8211; despite the complex inheritance hierarchy (<code>int[]<\/code> being auto-boxed by <code>System.Array&lt;int&gt;<\/code>, <code>System.Array&lt;int&gt;<\/code> providing a custom <code>sort<\/code> based on generic type constraints, and <code>System.Integer32<\/code> implementing <code>IComparable&lt;T&gt;<\/code>).<\/p>\n<h3>ECMAScript 5 Array Methods<\/h3>\n<p>ECMAScript 5 (ES5) added several array methods:<\/p>\n<ul>\n<li>indexOf<\/li>\n<li>lastIndexOf<\/li>\n<li>every<\/li>\n<li>some<\/li>\n<li>filter<\/li>\n<li>map<\/li>\n<li>forEach<\/li>\n<li>reduce<\/li>\n<li>reduceRight<\/li>\n<\/ul>\n<p>However, these methods are not supported in older web browsers. JS++ aims for enterprise support and legacy web application support. However, in order to polyfill these methods, it would result in hundreds of lines of code. Thus, in the spirit of JS++ dead code elimination (DCE), the above methods are <em>only<\/em> polyfilled for incompatible web browsers if and only if the individual method is used.<\/p>\n<h3>System.Object<\/h3>\n<p>One peculiar Standard Library class you might notice is <a href=\"https:\/\/docs.onux.com\/en-US\/Developers\/JavaScript-PP\/Standard-Library\/System\/Object\" rel=\"noopener\" target=\"_blank\">System.Object<\/a>. Specifically, it does not implement the JavaScript API at all. This is because JS++ does not use prototypical inheritance like JavaScript does. JS++ uses class-based inheritance; thus, <code>System.Object<\/code> is minimal and does not provide methods that are only useful for a prototype-based language &#8211; such as <code>hasOwnProperty<\/code> and <code>isPrototypeOf<\/code>.<\/p>\n<p>JS++ uses a &#8220;Unified Type System&#8221; (like Java\/C#) with <code>System.Object<\/code> at the top of the inheritance hierarchy. Thus, <code>System.Object<\/code> represents all &#8220;internal types.&#8221; If you don&#8217;t know what that means, please fully read the &#8220;<a href=\"https:\/\/www.onux.com\/jspp\/getting-started\" rel=\"noopener\" target=\"_blank\">Getting Started<\/a>&#8221; guide.<\/p>\n<p>If you want to use JavaScript objects, you still have to declare external types until <code>System.Dictionary&lt;T&gt;<\/code> arrives:<\/p>\n<pre class=\"brush:jspp\">\r\nvar obj = {\r\n    \"a\": 1,\r\n    \"b\": 2\r\n};\r\n<\/pre>\n<p>In addition, if you want the JavaScript <code>Object<\/code> prototype methods, use the <code>Externals.JS<\/code> module:<\/p>\n<pre class=\"brush:jspp\">\r\nimport System;\r\nimport Externals.JS;\r\n\r\nConsole.log(typeof Externals.JS.Object.prototype.hasOwnProperty == \"function\"); \/\/ true\r\n<\/pre>\n<h3>Prefer the Standard Library<\/h3>\n<p>There are 300+ pages of Standard Library documentation (not including all the documentation for individual method overloads). This is in addition to 300+ pages of handwritten documentation, bringing the JS++ documentation to over 600 pages.<\/p>\n<p>As always, I recommend that you prefer the JS++ Standard Library to writing your own JavaScript implementation. Consider <a href=\"https:\/\/docs.onux.com\/en-US\/Developers\/JavaScript-PP\/Standard-Library\/System\/Array\/clear\" rel=\"noopener\" target=\"_blank\">clearing an array<\/a>. In JS++, the code is:<\/p>\n<pre class=\"brush:jspp\">arr.clear();<\/pre>\n<p>You might be tempted to avoid the function call overhead and try to roll your own JavaScript:<\/p>\n<pre class=\"brush:js\">arr = [];<\/pre>\n<p>First of all, this can create a <strong>memory leak<\/strong> (e.g. if other references to the original array are being held). The JS++ Standard Library provides high-quality, fast, and well-tested functions to you. Use it.<\/p>\n<p>Secondly, what <em>looks<\/em> like function call overhead on the surface is actually inlined, <em>correct<\/em> code in the final output:<\/p>\n<pre class=\"brush:js\">arr.length = 0;<\/pre>\n<p>Arrays have incredibly low overhead. Here&#8217;s the full code:<\/p>\n<pre class=\"brush:jspp\">\r\nimport System;\r\n\r\nint[] arr = [1,2,3];\r\narr.clear();\r\n<\/pre>\n<p>Here&#8217;s the generated output:<\/p>\n<pre class=\"brush:js\">\r\n\/\/ Compiled with JS++ v.0.7.0\r\n\r\n!function(){!function(){var arr=[1,2,3];arr.length=0;}();}();\r\n<\/pre>\n<p>As you can see, there is zero overhead. Thus, use the Standard Library. Don&#8217;t try to be fancy and write JavaScript rather than JS++ code. Your users&#8217; garbage collectors will thank you for it in the future when you&#8217;re not leaking 500mb of RAM, and your team will thank you for it because <code>.clear()<\/code> is A LOT more readable than <code>.length = 0<\/code> and A LOT more correct than <code>arr = [];<\/code><\/p>\n<h3>Code Readability<\/h3>\n<p>One of the major driving changes for JS++ is code readability. We invest heavily into this from <a href=\"https:\/\/www.onux.com\/jspp\/blog\/glr-parsing-and-disambiguation\/\" rel=\"noopener\" target=\"_blank\">cutting-edge parsers<\/a> to Standard Library design. The JS++ Standard Library allows you to write high-performance, <em>readable<\/em> code.<\/p>\n<p>Consider this JavaScript code:<\/p>\n<pre class=\"brush:js\">\r\nvar abc = [ \"a\", \"b\", \"c\" ];\r\nabc.splice(1, 1);\r\n<\/pre>\n<p>Without evaluating it, what do you think it does?<\/p>\n<p>Here&#8217;s the equivalent JS++ code:<\/p>\n<pre class=\"brush:jspp\">\r\nstring[] abc = [ \"a\", \"b\", \"c\" ];\r\nabc.remove(1);\r\n<\/pre>\n<p>Same code. Same performance. Many times more readable when written in JS++.<\/p>\n<h3>Looking Ahead<\/h3>\n<p>We&#8217;re going to continue to expand the JS++ Standard Library to provide useful functions and give you a &#8220;batteries included&#8221; experience. Additionally, we&#8217;ll continue to expand the language. The following features are still not implemented:<\/p>\n<ul>\n<li>User-defined Generic Classes<\/li>\n<li>Reflection API<\/li>\n<li>System.Dictionary (hash maps)<\/li>\n<li>Block Scoping<\/li>\n<li>Nullable Types<\/li>\n<li>final variables (but final classes and methods are done)<\/li>\n<\/ul>\n<p>If you have a pressing need for any of these features, you may want to wait. However, everything that is possible to do in JavaScript is now possible to do in JS++. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>JS++ 0.7.0 has arrived. Everything that is possible to do in JavaScript is now possible to do in JS++. JS++ has fully implemented JavaScript and the JS++ Standard Library is now fully documented. This means that every JavaScript feature you expect is now available in JS++: arrays (0.7.0 introduces the generic Array&lt;T&gt; class), functions, date\/time, &hellip; <a href=\"https:\/\/www.onux.com\/jspp\/blog\/jspp-0-7-0-javascript-fully-implemented\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;JS++ 0.7.0: JavaScript Fully Implemented&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/posts\/384"}],"collection":[{"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/comments?post=384"}],"version-history":[{"count":28,"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/posts\/384\/revisions"}],"predecessor-version":[{"id":461,"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/posts\/384\/revisions\/461"}],"wp:attachment":[{"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/media?parent=384"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/categories?post=384"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/tags?post=384"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}