JS++ 0.5.2: BSD License, Interfaces, Abstract Classes, Virtual Methods, Non-generic Standard Library Classes

You’ve asked and we’ve listened. JS++ is now licensed under the 3-clause BSD License.

When we first announced 0.5.1 back in March, we introduced bare minimum classes. Specifically, I noted the following class features were unavailable:

  • Generic classes
  • Abstract classes
  • Inner classes
  • Interfaces
  • Virtual methods
  • Custom Conversion Rules as defined in my book, “The JS++ Type System”, Chapter 6.2

Every feature in the above list that isn’t crossed out is now available except the last feature (custom conversion rules) which will be arriving next. In addition, today’s release marks the introduction of the Standard Library. I’m going to briefly introduce the new features.

Update to Hello World

The JS++ Hello World program is now written as:

import System;

Console.log("Hello World");

Notice we no longer have to declare console as external. external is used for importing JavaScript libraries, and since we didn’t have a JS++ console implementation yet, we resorted to using the JavaScript console. However, now that we have a Console class in the Standard Library, it’s no longer a problem.

It is always recommended that you use the Standard Library’s Console class over the external JavaScript console. JS++ will detect if a console is available and will not crash if you try to log to an unavailable console. This can be a problem for web browsers like older versions of Internet Explorer, which are still used heavily in enterprise web applications.

Standard Library

The following Standard Library classes are now available:

  • System.Boolean
  • System.Character
  • System.Console
  • System.Date
  • System.Double
  • System.Exception
  • System.Exceptions
  • System.Integer8
  • System.UInteger8
  • System.Integer16
  • System.UInteger16
  • System.Integer32
  • System.UInteger32
  • System.Math
  • System.Object
  • System.RegExp
  • System.String

Many of the above classes, such as System.String and System.Integer32, are wrapper classes for auto-boxing. Currently, these wrapper classes simply provide a type-safe (and sometimes optimized) version of their JavaScript-equivalent methods. For example:

import System;

string s = "my string";
Console.log(s.replace(/^[a-z]/, string(string match){ return match.toUpperCase(); })); // Prints "My string"

The above example provides the exact same functionality as JavaScript’s String.prototype.replace. However, you get safety guarantees that you wouldn’t get with JavaScript. For example, if you try to call System.String.replace using the wrong arguments:

Console.log(s.replace(1));
[  ERROR  ] JSPPE5023: No overload for `System.String.replace' that takes `1' argument(s) at line 4 char 12 at replace.jspp

Optimizations

Always favor using the JS++ Standard Library over “rolling your own” functions. Consider the following code (which you can run yourself with the latest JS++):

import System;

double t = (new Date).getTime();
string z;
for (int i = 0; i < 5000000; ++i) {
	z += Double.POSITIVE_INFINITY.toString();
}
Console.log((new Date).getTime() - t);

And the nearly equivalent JavaScript code:

var t = (new Date).getTime();
var z = "";
for (var i = 0; i < 5000000; ++i) {
	z += Number.POSITIVE_INFINITY.toString();
}
console.log((new Date).getTime() - t);

JS++ average time: 124.4ms
JavaScript average time: 211ms

In this case, JS++ is roughly 70% faster... for nearly identical code.

You may think JS++ adds overhead (based on perceptions of what fast code may look like), but well-written JS++ will be faster than JavaScript. See my other article on optimization for more details.

Typed Exceptions and Multiple Catch Clauses

JS++ 0.5.2 introduces the System.Exception class and enables you to create your own custom exceptions.

Here's an example:

import System;

class CustomException : System.Exception
{
    CustomException() {
        super();
    }
    CustomException(string message) {
        super(message);
    }
}

try {
    throw new CustomException("This is a custom exception object.");
}
catch(CustomException e) {
    Console.log("Caught CustomException");
}
catch(System.Exception e) {
    Console.log("Caught System.Exception");
}

Variadic Parameters

The latest version of JS++ also introduces variadic parameters, which allow you to supply an arbitrary number of arguments to a function:

import System;

void log(Date date, ...string messages, bool silent) {
    if (silent) return;

    foreach(string message in messages) {
        Console.log(date.toString() + ": " + message);
    }
}

log(new Date(), "1", "2", "3", false);

Interfaces

An interface creates a contract. Methods defined in an interface must be implemented by all inheriting classes. Classes can inherit more than one interface.

According to JS++ naming conventions, interfaces should be prefixed with "I" and should be UpperCamelCase.

import System;

interface IWalkable {
	void walk();
}
interface ITalkable {
	void talk();
}

class Person : IWalkable, ITalkable
{
	void talk() {
		Console.log("Talking...");
	}
	void walk() {
		Console.log("Walking...");
	}
}

Person person = new Person();
person.talk();
person.walk();

Callback Type Parameter Names

Callback types can have parameters. Previously, you could only specify the parameter types for a callback/function type. However, you can now add names for these parameters. While these names cannot be used and have no meaningful effect on the executed code, they improve the readability of the code.

import System;

class Bird
{
	void fly() {
		Console.log("Flying...");
	}
}

void(Bird bird) fly = void(Bird bird) {
	bird.fly();
};
Bird bird = new Bird();
fly(bird);

Removal of 'Convert' Module

We have removed from the Convert module from the latest release. It was always used as a stopgap until we implemented the Standard Library wrapper classes, which provide toString() and other methods.

Bug fix: 'typeof' for internal types

For non-external types, typeof will always return the string "internal".

import System;

int x;
Console.log(typeof x); // "internal"

Virtual Methods

JS++ 0.5.2 introduces the virtual keyword and the override keyword to enable virtual methods on classes.

Virtual methods enable late binding and runtime polymorphism.

class Shape
{
    public virtual double area() {
        return 0;
    }
}
 
class Rectangle : Shape
{
    private int length, width;
 
    public Rectangle(int length, int width) {
        this.length = length;
        this.width = width;
    }
 
    public override double area() {
        return length * width;
    }
}
 
class Triangle : Shape
{
    private int base, height;
 
    public Triangle(int base, int height) {
        this.base = base;
        this.height = height;
    }
 
    public override double area() {
        return (base * height) / 2;
    }
}

Abstract Classes

Use the abstract modifier to create abstract classes and methods.

abstract class Shape
{
    public abstract int area();
}
class Rectangle : Shape
{
    private int length, width;
 
    public override int area() {
        return length * width;
    }
}

Enumerations

Enumerations (enums) can be used to restrict values and write type-safe code:

enum Importance { None, Regular, Critical }
 
Importance errorLevel = Importance.Critical;

The one missing feature...

Sadly, there is still one major missing feature from JS++. The Standard Library does not support System.Array yet because it is a generic class, and generics have not yet been implemented. In the meantime, you can resort to declaring your arrays as var:

var arr = [ 1, 2, 3 ];

BSD License

Last, but not least, JS++ 0.5.2 is the first version of JS++ licensed under the 3-clause BSD License.

The download for JS++ 0.5.2 is available from our home page.

Enjoy JS++!

JS++ 0.5.1: ‘foreach’

We just released JS++ 0.5.1, which features the foreach keyword.

Basic usage is as follows:

external console;

int[] arr = [ 50, 40, 30 ];

foreach (int value in arr) {
    console.log(value);
}

// Output:
// 50
// 40
// 30

More complex usage, such as arrays of objects, are also possible:

external console;

class Foo
{
	string bar() {
		return "bar";
	}
}

Foo[] foo = [ new Foo(), new Foo(), new Foo() ];

foreach (Foo value in foo) {
    console.log(value.bar());
}

// Output:
// "bar"
// "bar"
// "bar"

Additionally, the for-in keyword has been implemented. It is a counterpart to the foreach keyword in the sense that it enumerates keys rather than values.

JS++ 0.5.0: Basic Classes

JS++ now supports the ‘class’ keyword. As promised, we would have classes available by Q1 2017. Notably, we are providing “basic” classes, so the following features are supported:

  • ‘class’ keyword
  • Inheritance
  • Constructors (including private/protected constructors that limit instantiation/inheritance)
  • Static Constructors
  • Instantiation
  • Fields
  • Methods
  • ‘this’ keyword
  • ‘super’ keyword
  • Method Overloading
  • Constructor Overloading
  • Getters and Setters (via ‘property’ keyword)
  • Type system support
  • Debugger support for classes (via source maps)

What did we consider to be outside the scope of basic classes? The following features are currently not available yet:

As an example of what you can do with JS++ classes, we included an example with the Tiled Map Editor:

Tiled Map Editor

The included sample for Tiled can be found in the ‘Samples/Classes/tiled-loader’ directory with JS++ 0.5.0. Currently, it will load all maps exported to JSON with orthogonal tiles. It’s just a small but powerful example of what you can start doing with JS++ for early adopters.

I am also happy to inform that the backend for the JS++ website is written completely in JS++, and it has now run stable for one week without a single crash or error (other than 404 errors).

Finally, we have made the decision to not include the Mac/Linux installer… yet.

Be sure to check out the documentation on the ‘class’ keyword to get up to speed on JS++ classes.

Download JS++ 0.5.0 from our home page.

Mac/Linux Installer Coming for JS++ 0.5

The next version of JS++ will feature the ‘class’ keyword and is expected for Q1 2017.

In addition, Mac and Linux users will benefit from a JS++ installer:


The installer is completely optional. However, it will be useful if you don’t want to manually install JS++ and various editor plugins. We auto-detect which editors are installed on your system, and, if a plugin is available, we will attempt to install it for you.

JS++ for Mac OS X Updated to Latest Version

During our Christmas release, we had to release one version down (0.4.2.3) for Mac OS X. I’m happy to announce that – now that our staff are back from their breaks – JS++ for Mac OS X has been updated to the latest version and is in sync with JS++ for Windows and Linux.

Please note that, as of 0.4.2.4 for Mac OS X, JS++ defaults to the 32-bit binary. The 64-bit binary is available as jspp-x64.

You can download JS++ for Mac OS X from our homepage.

A JS++ Christmas: AJAX, js++Unit, Mac compiler, Tutorials

Merry Christmas from JS++!

We have multiple “gifts” and announcements for you this Christmas. We have lots of new libraries written with JS++ and a new version.

Version 0.4.2.4 and Mac Support

We have released version 0.4.2.4 which supports callback conversions, array conversions, and C-style casts such as:

int x = (int) y;

For a long time now, we have not provided Mac support. That’s going to change beginning this Christmas. We’ve released 0.4.2.3 for Mac OS X. It is one version down from the latest release for PC, but this was due to the fact that I let our staff off on holiday, found a bug with auto-boxing, and patched it at the last minute.

If you want access to our new libraries, you will need to download the new versions.

Library: js++Unit

js++Unit is an xUnit testing framework for automated testing in JS++. js++Unit currently works best for Node.js and is inspired by the Mocha test framework. It doesn’t really support the browser yet, but we’ll get there. We’re taking open source contributions for js++Unit too!

js++Unit also comes with a full-featured assertion framework (Vendor.JSPPUnit.Assert) and will do neat things like working with cycles for deepEqual.

You can download js++Unit from GitHub.

Library: ConsoleStyle

If you have a terminal emulator that supports ANSI escape codes, we have a library for you to provide colors to your console output. Download the ConsoleStyle library from GitHub. js++Unit depends on it and you need to link ConsoleStyle to use js++Unit.

Library: AJAX

Are you tired of having to include the entire jQuery library just to send an AJAX request? In JS++, you no longer have to. Since JS++ features dead code elimination, if you import an AJAX library that defines functions for HTTP GET and POST requests and you only use GET requests, the logic for POST requests will not be compiled into your final application.

You can download the AJAX library here. (It also ships with the latest version of JS++.)

Tutorial: What Are “Type Guarantees?”

The Getting Started guide has been completely re-written. We now explain how to use JavaScript libraries from JS++. We go one step further and explain what “type guarantees” are and how you can take advantage of them from JS++ using a new “RGBtoHex” tutorial.

The RGBtoHex tutorial shows an “untyped” JavaScript library. We teach you how to use it from JS++ – without ever adding types to your code. We gradually begin to add types (without compromising the soundness of the type system). Finally, we end with fully typed code and a re-usable function.

RGBtoHex is an ideal tutorial. RGB values are limited to the values 0 to 255. This is the JS++ byte data type. In the RGBtoHex tutorial, we show you how JS++ will guarantee your values will remain within the 0 to 255 range.

Check out the Getting Started tutorial now.

Merry Christmas!

JS++ 0.4.2.2: Debugging with Source Maps

JS++ 0.4.2.2 is being released today with support for in-browser debugging via source maps. Source maps allow you to debug your JS++ programs from a web browser of your choice that supports source maps debugging: Google Chrome, Mozilla Firefox, Microsoft Edge (untested), etc. With source maps, you can set breakpoints, log messages, and more with all locations pointing back to the original source JS++ file—rather than the generated .jspp.js file.

At the time of this writing, the recommended web browser to use for source maps debugging is Google Chrome.

In order to leverage source maps, compile your JS++ files with the --debug (or -d) flag:

Compiling source maps with JS++

This will generate a .map file, which maps the generated code back to the original JS++ source code.

When you run the generated code in your web browser, you will notice that console.log statements point to the original source .jspp file’s location rather than the locations of the generated code:

Source Maps - console.log Original Location

In addition, you can set breakpoints, step into, step over, step out and use all the features you’d expect from your debugger:

Source Maps - Set Breakpoint

Finally, we have worked to improve Microsoft Windows integration even more. In JS++ 0.4.2, we gave you Windows context menu integration. However, due to restrictions in Windows, this only allowed compilation of one file. JS++ is a multi-file, modular programming language. Thus, we needed users to be able to select multiple files and compile them. You can now do this in the latest version of JS++:

jspp-windows-multifile

Another problem with Windows integration was that the JS++ CLI compiler would pop up and immediately exit. If you had an error, there was no way to know what to fix. With the latest version of JS++, you will now receive a popup dialog notifying you of the compilation results:

jspp-windows-multifile2

Get the latest download of JS++ by navigating to our home page.

JS++ 0.4.2.1 – Minor Bug Fix Release

JS++ 0.4.2.1 has been released. This is a minor bug fix release. The following issues have been fixed:

1. Checking for permissions. Compiler now checks if the user has permission to read input files and write to output directories.
2. Segmentation faults for built-in modules (e.g. `Externals.DOM`)
3. Dead Code Elimination (DCE) algorithm updates so parent modules do not get eliminated.
4. Error 0119 was incorrectly being raised on function parameters. This has been fixed so assignments to parameters can be done.
5. Parser bug for regular expression literals.

In the next release, 0.4.2.2, we will be including free, open-source libraries for AJAX and real-time streaming.

The latest version can be downloaded from the JS++ homepage.

JS++ 0.4.2 Released

We are proud to announce the release of JS++ 0.4.2 Early Access Preview. The latest release of JS++ introduces modules, function overloading, dead code elimination, better integration with Microsoft Windows, and 16 new editor integrations.

Emacs Syntax Highlighting for JS++

JS++ 0.4.2 introduces the module keyword and enables modular design. The application entry point occurs at the “main file”. See the documentation for the main file for more information.

In addition, at the compiler level, JS++ gives you full access to static linking and dead code elimination. Dead code elimination means that all unused code will not be compiled into the final generated output. One of the biggest pain points in JavaScript is that you need to include the entire jQuery library just to use one function. With npm and the “micro-library” revolution, JavaScript code has grown and grown in size. Web pages take longer to load because they depend on megabytes of JavaScript to be downloaded, and this is especially painful over mobile connections. Dead code elimination solves this: if you didn’t use it, it doesn’t end up in the code you ship.

Please note that dead code elimination is a JS++ feature only. It cannot be retroactively applied to JavaScript code effectively. For example, JS++ is a superset of JavaScript that introduces structure, such as the module keyword. Unlike JavaScript prototypes, these structures cannot be modified at runtime. Therefore, the compiler is fully able to analyze which classes, variables, and functions actually get used and eliminate the ones that don’t get used. If your code is structured with JavaScript prototypes rather than JS++ classes and modules, dead code elimination will be a lot less effective.

JS++ 0.4.2 also introduces function overloading. All unused overloads and unused functions (even if not overloaded) will not be compiled in the final output via dead code elimination.

Modules, function overloading, and dead code elimination (DCE) together enable the creation of more sophisticated applications.

However, we’ve delivered much more than that. In addition to all the new compiler features, we are shipping integrations with 16 new code editors – including some of the most popular editors:

1. Sublime Text
2. Notepad++
3. Visual Studio Code
4. Vim
5. GitHub Atom
6. Adobe Brackets
7. GNU Emacs
8. UltraEdit
9. gedit
10. Kate
11. KWrite
12. Nano
13. Geany
14. Ace (Editor for Cloud9 IDE)
15. CodeMirror
16. Alex Gorbatchev’s SyntaxHighlighter

Sublime Text Build System

Finally, we’ve also improved Windows integration. Installation on Windows is now more seamless (no restarts necessary), and you can now use the GUI to compile JS++ files and avoid the command-line altogether:

JS++ Windows Context Menu

This is an exciting and comprehensive new release for JS++. You can download the latest version from the home page.

JS++ for Ace: Embedding and Tooling

JS++ now supports the popular Ace editor library. The Ace editor is an open-source text editor plugin that is used as the underlying editor for Cloud9 IDE and is the successor to the Mozilla Bespin project.

Ace can be used for embedding JS++ snippets into web pages. It can also be used to develop tooling for JS++ such as text editors and IDEs. Ace is fully web-based so it can be used for developing cloud editing tools.

JS++ for Ace AJAX Cloud9 IDE

You can download the JS++ Ace integration from our GitHub.