JS++ is a programming language for web development that extends JavaScript with a sound type system. It includes imperative, object-oriented, functional, and generic programming features. It is free and open-source software released under a BSD license.
JS++ first appeared on October 8, 2011. The modern implementation was announced at DeveloperWeek 2016 and released on May 31, 2016. The language is designed by Roger Poon and Anton Rapetov.
Since JS++ is a superset of JavaScript, declaring data types for variables is optional. However, when types are declared, the types are enforced at both compile time and runtime.
Type annotations in JS++ use the traditional C/C++ syntax:
Notably, this differs from TypeScript and ActionScript, which use a more verbose style:
The type system in JS++ is sound for ECMAScript and DOM API corner cases, including host objects, dynamic key-value pairs, Comet, JScript conditional compilation, dynamic return types, ActiveX, ECMAScript for XML, web browser garbage collector and cyclic reference counting bugs, conditional logic, and other edge and corner cases. This differs from other JavaScript supersets where types are optional and discarded at runtime via type erasure, such as in TypeScript.
JS++ can use JavaScript libraries using the one-line <code>external</code> statement as in the following example from the homepage of JS++:
While classes in JavaScript (ECMAScript 6) are syntactic sugar for prototypes under the hood, JS++ classes resemble the classes found in classical programming languages such as C++, Java, and C# in terms of memory layout, performance, and semantics. "Classes" are a static concept, and they cannot be altered at runtime (during program execution) as is the case for JavaScript, Smalltalk, Lisp, and TypeScript, which rely on prototypes. For example, private methods are private at both compile time and runtime, and external JavaScript objects cannot access private JS++ fields or methodsâÂÂeven if a reference to a JS++ object is obtained from JavaScript.
The following source code illustrates object-oriented sorting in JS++ using the IComparable<T> interface and Comparison enumeration for type-safe and readable comparisons. The custom sorting logic is one line of code in the overridden <code>compare</code> method below:
Thus, in the code above, the custom sorting logic provided is:
Likewise, to call the sort:
For printing the sorted results:
JS++ provides encapsulation by default. In the following example, the fields <code>x</code> and <code>y</code> are <code>private</code> by default, even if no access modifier is specified. The methods <code>getX()</code> and <code>getY()</code> are <code>public</code> by default. This enables a more concise class definition syntax, as illustrated in the <code>Point</code> class below:
An out-of-bounds access usually occurs with arrays and other containers. For example, when we access the 100th element of a 3-element array, we have an out-of-bounds access:
In Java and C#, this can result in an exception and program termination. In C, this can result in buffer overflows or segmentation faults. C++ has varying semantics, such as default initialization, exceptions, segmentation faults, or buffer overflows.
JS++ can efficiently analyze and prevent out-of-bounds errors at compile time.
JavaScript has the notion of <code>null</code> and <code>undefined</code> values, where <code>null</code> means a value is present but it's an empty value, and <code>undefined</code> means there isn't a value there at all. JS++ extends this intuition further to differentiate between empty values and out-of-bounds accesses.
Consider the following code, with a nullable <code>int</code> type represented with <code>int?</code>:
While nullable types can represent an out-of-bounds access, it falls apart when the array might contain nullable values as illustrated above. Instead, JS++ introduces an additional concept in addition to null values: undefined values. Recall that JS++ extends the JavaScript notion that null means a value is present but is an empty value, while an undefined value means a value does not exist at all. JS++ uses the concept of "a value does not exist at all" to mean an out-of-bounds access has occurred, and this concept is known in JS++ as "existent types."
Therefore, the previous example can be amended. The existent type <code>int+</code> means "<code>int</code> or out of bounds" and <code>int?+</code> means "<code>int</code>, <code>null</code>, or out of bounds":
Intuitively, this means existent types cannot be used as the underlying type for array elements. JS++ enforces this at compile time:
<pre style="color: silver; background: black;">[ ERROR ] JSPPE5204: Existent type `int+' cannot be used as the element type for arrays</pre>
Instead of following every conditional branch or virtual method call path, which would result in path explosion and exponential compile times, existent types have essentially the same compile-time analysis cost as <code>int</code>, <code>bool</code>, and other primitive types. Consequently, compile times have been shown to be unaffected (ñ1-2ms) by the introduction of existent types. Since existent types are used for all array and container types in JS++ (such as hash maps, <code>Stack<T></code>, and <code>Queue<T></code>), JS++ containers are thus guaranteed to not have out-of-bounds errors.
In JS++, <code>undefined</code> is a value that cannot be changed. In JavaScript (ECMAScript 3), <code>undefined</code> is a mutable property of the global object, resulting in circumstances where "undefined" can be "defined." Thus, existent types would not have been possible in pure JavaScript, as arrays can contain elements with the undefined value, undefined can be defined, or other edge and corner cases that are prevented in JS++.
Also, in comparison to Java and early object-oriented languages such as Eiffel, JS++ does not default initialize objects to <code>null</code>. Instead, the compiler enforces initialization by the programmer:
<pre style="color: silver; background: black;">[ ERROR ] JSPPE6000: Variable `car' is not initialized at line 3 char 4</pre>
Therefore, since existent types are deeply embedded into the language, JS++ can guarantee that out-of-bounds errors never occur.
The concept of existent types can be extended outside of containers. For example, in MySQL, columns can be nullable. If the row does not exist for a specified condition (e.g. WHERE clause), the <code>undefined</code> value can be returned. However, if the row does exist but the value at the column is empty, a <code>null</code> value can be returned instead. This can simplify the code and interfaces to the data access layer.
JS++ provides 8-bit, 16-bit, 32-bit, and 64-bit integer types as well as floating point types:
From the project homepage:
All variables in JS++ are block-scoped, including the JavaScript <code>var</code> statement. Thus, there is no need for two different variable declaration keywords with different scoping rules, such as <code>var</code> and <code>let</code> co-existing simultaneously in JavaScript (ECMAScript 6).
The JS++ compiler is available for Windows, macOS, and Linux. It is a source-to-source compiler which emits JavaScript source code as an intermediate representation.
The compiler is developed with C/C++, and the developers claim there are "fewer than 10 open bug reports in the core compiler" after 3.5 years of engineering and 400,000 lines of code.
JS++ integrates with various code editors including Visual Studio Code, Atom, and Sublime Text.
JS++ can be integrated with third-party build tools like Webpack.