| JS++ | |
|---|---|
| Paradigm | Multi-paradigm:Imperative,structured,object-oriented,functional,generic |
| Family | ECMAScript |
| Designed by | Roger Poon, Anton Rapetov |
| Developer | Onux |
| First appeared | October 8, 2011; 14 years ago (2011-10-08) |
| Stable release | 0.10.0 / December 10, 2021; 4 years ago (2021-12-10) |
| Typing discipline | Gradual,static,dynamic |
| Scope | lexical |
| License | BSD |
| Filename extensions | .jspp, .js++, .jpp |
| Website | www |
| Influenced by | |
| C,C++,C#,Java,JavaScript | |
JS++ is aprogramming language forweb development that extendsJavaScript with asoundtype system. It includesimperative,object-oriented,functional, andgeneric programming features. It isfree and open-source software released under aBSD license.
JS++ first appeared on October 8, 2011.[1][2][3] The modern implementation was announced at DeveloperWeek 2016[4] and released on May 31, 2016.[5][6][7][8] The language is designed by Roger Poon and Anton Rapetov.
Since JS++ is a superset of JavaScript, declaringdata 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 traditionalC/C++ syntax:
intx=1;vary=2;boolz=true;
Notably, this differs fromTypeScript andActionScript, which use a more verbose style:
varx:number=1;vary:any=2;varz:boolean=true;
The type system in JS++ issound forECMAScript andDOM API corner cases, including host objects, dynamic key-value pairs,Comet,JScript conditional compilation, dynamic return types,ActiveX,ECMAScript for XML,web browsergarbage collector andcyclicreference counting bugs, conditional logic, and otheredge andcorner cases.[9][10] This differs from otherJavaScriptsupersets where types are optional and discarded at runtime viatype erasure, such as inTypeScript.[11][12]
JS++ can use JavaScript libraries using the one-lineexternal statement as in the following example from the homepage of JS++:
importSystem;// Import JavaScript libraries in one line of codeexternaljQuery,$;classExample{publicExample(){// Nearly NO learning curve// You can keep writing regular JavaScriptvara=0,random=Math.random();// Integer types and other primitives// ... enable fast (optimized) and clear codebyte[]rgbColors=[0xFF,0xFA,0xFF];}publicvoidshowMessage(intid,stringtext){// 100% compatible with regular JavaScriptjQuery("#msgbox").show();$("#msgbox").text(id.toString()+text);}}
While classes inJavaScript (ECMAScript 6) aresyntactic sugar forprototypes under the hood,[13] JS++classes resemble the classes found in classicalprogramming languages such asC++,Java, andC# 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 forJavaScript,Smalltalk,Lisp, andTypeScript, which rely on prototypes.[14] 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 theIComparable<T> interface andComparison enumeration for type-safe and readable comparisons.[15] The custom sorting logic is one line of code in theoverriddencompare method below:
importSystem;classEmployee:IComparable<Employee>{privatestringfirstName;privatestringlastName;publicEmployee(stringfirstName,stringlastName){this.firstName=firstName;this.lastName=lastName;}publicoverrideComparisoncompare(Employeethat){// Sort by employee surnamereturnthis.lastName.compare(that.lastName);}publicoverridestringtoString(){returnthis.firstName+" "+this.lastName;}}Employeezig=newEmployee("Zig","Ziglar");Employeejohn=newEmployee("John","Smith");Employeeabe=newEmployee("Abe","Lincoln");Employee[]employees=[zig,john,abe];employees.sort();Console.log(employees.join(", "));// Output:// Abe Lincoln, John Smith, Zig Ziglar
Thus, in the code above, the custom sorting logic provided is:
returnthis.lastName.compare(that.lastName);
Likewise, to call the sort:
employees.sort();
For printing the sorted results:
Console.log(employees.join(", "));
JS++ providesencapsulation by default. In the following example, the fieldsx andy areprivate by default, even if no access modifier is specified. The methodsgetX() andgetY() arepublic by default. This enables a more concise class definition syntax, as illustrated in thePoint class below:[16]
classPoint{intx,y;Point(intx,inty){this.x=x;this.y=y;}intgetX(){returnthis.x;}intgetY(){returnthis.y;}}
An out-of-bounds access usually occurs witharrays and other containers. For example, when we access the 100th element of a 3-element array, we have an out-of-bounds access:
int[]arr=[1,2,3];Console.log(arr[100]);// out-of-bounds access, 'arr' does not have an element at index 100
InJava andC#, this can result in an exception and program termination. InC, this can result inbuffer overflows orsegmentation faults.C++ has varying semantics, such as default initialization, exceptions, segmentation faults, or buffer overflows.[17][18]
JS++ can efficiently analyze and preventout-of-bounds errors atcompile time.[19][20][21]
JavaScript has the notion ofnull andundefined values, wherenull means a value is present but it's an empty value, andundefined means there isn't a value there at all. JS++ extends this intuition further to differentiate between empty values and out-of-bounds accesses.[21]
Consider the following code, with a nullableint type represented withint?:
int[]a=[1,2];int?value1=a[2];if(value1==null){Console.log("Definitely out of bounds");}int?[]b=[1,null];int?value2=b[2];if(value2==null){Console.log("Might be out of bounds, might just be an access of a null element");}
Whilenullable types can represent an out-of-bounds access, it falls apart when the array might contain nullable values as illustrated above. Instead, JS++ introduces anadditional 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."[21]
Therefore, the previous example can be amended. The existent typeint+ means "int or out of bounds" andint?+ means "int,null, or out of bounds":
int[]a=[1,2];int?value1=a[2];if(value1==undefined){Console.log("Definitely out of bounds");}int?[]b=[1,null];int?+value2=b[2];if(value2==undefined){Console.log("Definitely out of bounds");}
Intuitively, this means existent types cannot be used as the underlying type for array elements. JS++ enforces this at compile time:
int+[]arr=[];// ERROR
[ ERROR ] JSPPE5204: Existent type `int+' cannot be used as the element type for arrays
Instead of following every conditional branch or virtual method call path, which would result inpath explosion and exponential compile times, existent types have essentially the same compile-time analysis cost asint,bool, and otherprimitive types. Consequently, compile times have been shown to be unaffected (±1-2ms) by the introduction of existent types.[21] Since existent types are used for all array and container types in JS++ (such ashash maps,Stack<T>, andQueue<T>), JS++ containers are thus guaranteed to not have out-of-bounds errors.
In JS++,undefined is a value that cannot be changed. In JavaScript (ECMAScript 3),undefined is a mutable property of the global object, resulting in circumstances where "undefined" can be "defined."[22] 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++.[21][23]
Also, in comparison toJava and early object-oriented languages such asEiffel,[24][25] JS++ does not default initialize objects tonull.[21] Instead, the compiler enforces initialization by the programmer:
classCar{}Carcar;
[ ERROR ] JSPPE6000: Variable `car' is not initialized at line 3 char 4
Therefore, since existent types are deeply embedded into the language, JS++ can guarantee that out-of-bounds errors never occur.[26][10]
The concept of existent types can be extended outside of containers. For example, inMySQL, columns can be nullable.[27][28] If the row does not exist for a specified condition (e.g. WHERE clause), theundefined value can be returned. However, if the row does exist but the value at the column is empty, anull value can be returned instead. This can simplify the code and interfaces to thedata access layer.
JS++ provides 8-bit, 16-bit, 32-bit, and 64-bit integer types as well asfloating point types:
byteb1=0xFF;// unsigned 8-bitsignedbyteb2=-1;// signed 8-bitshorts1=1;// signed 16-bitunsignedshorts2=1;// unsigned 16-bitintx1=0;// signed 32-bitunsignedintx2=0;// unsigned 32-bitlongz1=1;// signed 64-bitunsignedlongz2=1;// unsigned 64-bitfloatf=0.5;// single-precision floating pointdoubled=0.5;// double-precision floating point
From the project homepage:
// You're not required to declare the type for a variable and you can just// keep writing regular JavaScript (again, NO learning curve):varoffset=0;// But, if you do, this next variable will always remain an 'unsigned int' - even at runtime.// You'll never see NaN ("Not a Number") runtime errors again.// This variable is 'unsigned' so it's also guaranteed to never be negative.unsignedintpageHeight=$(document).height();
All variables in JS++ are block-scoped, including the JavaScriptvar statement.[29][30][31] Thus, there is no need for two different variable declaration keywords with different scoping rules, such asvar andlet co-existing simultaneously inJavaScript (ECMAScript 6).
The JS++compiler is available forWindows,macOS, andLinux. It is asource-to-source compiler which emits JavaScriptsource code as anintermediate representation.
The compiler is developed withC/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.[31][32]
JS++ integrates with various code editors includingVisual Studio Code,Atom, andSublime Text.[33][34][35]
JS++ can be integrated with third-party build tools likeWebpack.[36]
| Version number | Release date | Changes |
|---|---|---|
| 0.01 | 8 October 2011 (2011-10-08) | Alpha version, initial release |
| 0.011 | 10 October 2011 (2011-10-10) | Alpha version |
| 0.012 | 25 October 2011 (2011-10-25) | Alpha version |
| 0.013 | 29 January 2012 (2012-01-29) | Alpha version |
| 0.014.1 | 15 August 2012 (2012-08-15) | Alpha version |
| 0.4.1 | 31 May 2016 (2016-05-31) | Beta version, array and callback types, character literals, integral suffixes, removed ECMAScript ASI |
| 0.4.2 | 18 October 2016 (2016-10-18) | Modules, function overloading, dead code elimination, editor integrations |
| 0.4.2.1 | 24 October 2016 (2016-10-24) | Bug fixes |
| 0.4.2.2 | 17 November 2016 (2016-11-17) | Source map debugging |
| 0.4.2.4 | 25 December 2016 (2016-12-25) | Support for Mac OS X, C-style casts, callback and array conversions |
| 0.5.0 | 13 March 2017 (2017-03-13) | Classes |
| 0.5.1 | 26 March 2017 (2017-03-26) | 'foreach' loops |
| 0.5.2 | 27 July 2017 (2017-07-27) | BSD License, Interfaces, Abstract Classes, Virtual Methods, Auto-boxing |
| 0.7.0 | 27 October 2017 (2017-10-27) | All ECMAScript 3 features via Array<T> and Standard Library |
| 0.8.0 | 15 March 2018 (2018-03-15) | Generic programming, Dictionary<T>, multi-line strings, .js++ file extension |
| 0.8.1 | 27 March 2018 (2018-03-27) | auto, catch-all clauses, standard library modules for handling time, bug fixes |
| 0.8.4 | 23 May 2018 (2018-05-23) | New string functions, advanced generics, bug fixes, standard library expansion |
| 0.8.5 | 2 June 2018 (2018-06-02) | Bug fixes |
| 0.8.10 | 24 November 2018 (2018-11-24) | Faster compile times, stacks, queues,Unicode,Base64, generic default constraint rules |
| 0.9.0 | 11 January 2019 (2019-01-11) | Efficient compile time out-of-bounds error analysis |
| 0.9.1 | 1 July 2019 (2019-07-01) | Bug fixes |
| 0.9.2 | 18 October 2019 (2019-10-18) | Final (immutable) variables and default to64-bit formacOS Catalina |