JS++
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. HistoryJS++ 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. FeaturesSound gradual type systemSince 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: int x = 1;
var y = 2;
bool z = true;
Notably, this differs from TypeScript and ActionScript, which use a more verbose style: var x : number = 1;
var y : any = 2;
var z : boolean = true;
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.[9][10] This differs from other JavaScript supersets where types are optional and discarded at runtime via type erasure, such as in TypeScript.[11][12] Importing JavaScript librariesJS++ can use JavaScript libraries using the one-line import System;
// Import JavaScript libraries in one line of code
external jQuery, $;
class Example {
public Example() {
// Nearly NO learning curve
// You can keep writing regular JavaScript
var a = 0, random = Math.random();
// Integer types and other primitives
// ... enable fast (optimized) and clear code
byte[] rgbColors = [ 0xFF, 0xFA, 0xFF ];
}
public void showMessage(int id, string text) {
// 100% compatible with regular JavaScript
jQuery("#msgbox").show();
$("#msgbox").text(id.toString() + text);
}
}
Object-oriented programmingWhile classes in JavaScript (ECMAScript 6) are syntactic sugar for prototypes under the hood,[13] 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.[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. Example: object-oriented sortingThe following source code illustrates object-oriented sorting in JS++ using the IComparable<T> interface and Comparison enumeration for type-safe and readable comparisons.[15] The custom sorting logic is one line of code in the overridden import System;
class Employee : IComparable<Employee>
{
private string firstName;
private string lastName;
public Employee(string firstName, string lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public override Comparison compare(Employee that) {
// Sort by employee surname
return this.lastName.compare(that.lastName);
}
public override string toString() {
return this.firstName + " " + this.lastName;
}
}
Employee zig = new Employee("Zig", "Ziglar");
Employee john = new Employee("John", "Smith");
Employee abe = new Employee("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: return this.lastName.compare(that.lastName);
Likewise, to call the sort: employees.sort();
For printing the sorted results: Console.log(employees.join(", "));
Example: encapsulation by defaultJS++ provides encapsulation by default. In the following example, the fields class Point
{
int x, y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
int getX() { return this.x; }
int getY() { return this.y; }
}
Out-of-bounds analysisAn 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: int[] arr = [ 1, 2, 3 ];
Console.log(arr[100]); // out-of-bounds access, 'arr' does not have an element at index 100
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.[17][18] JS++ can efficiently analyze and prevent out-of-bounds errors at compile time.[19][20][21] JavaScript has the notion of Consider the following code, with a nullable 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");
}
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."[21] Therefore, the previous example can be amended. The existent type 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 in path explosion and exponential compile times, existent types have essentially the same compile-time analysis cost as In JS++, Also, in comparison to Java and early object-oriented languages such as Eiffel,[24][25] JS++ does not default initialize objects to class Car {}
Car car;
[ 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] DatabasesThe concept of existent types can be extended outside of containers. For example, in MySQL, columns can be nullable.[27][28] If the row does not exist for a specified condition (e.g. WHERE clause), the Integer typesJS++ provides 8-bit, 16-bit, 32-bit, and 64-bit integer types as well as floating point types: byte b1 = 0xFF; // unsigned 8-bit
signed byte b2 = -1; // signed 8-bit
short s1 = 1; // signed 16-bit
unsigned short s2 = 1; // unsigned 16-bit
int x1 = 0; // signed 32-bit
unsigned int x2 = 0; // unsigned 32-bit
long z1 = 1; // signed 64-bit
unsigned long z2 = 1; // unsigned 64-bit
float f = 0.5; // single-precision floating point
double d = 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):
var offset = 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.
unsigned int pageHeight = $(document).height();
Block scopingAll variables in JS++ are block-scoped, including the JavaScript Development toolsCompilerThe 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.[31][32] Editor integrationJS++ integrates with various code editors including Visual Studio Code, Atom, and Sublime Text.[33][34][35] Build toolsJS++ can be integrated with third-party build tools like Webpack.[36] Release history
See alsoReferences
|
Portal di Ensiklopedia Dunia