How to use More Safely In JS++

You can use JavaScript more safely from JS++ than you can from JavaScript itself.

Let’s start by modifying our main.jspp code to explicitly use the ‘byte’ data type:

external alert;
external rgbToHex;

byte red = 255;
byte green = 255;
byte blue = 255;
alert(rgbToHex(red, green, blue));

Compile main.jspp and open index.html. The result should be exactly the same: a message box will pop up containing “#ffffff”. However, there’s a difference this time: you’re guaranteed to only be able to send whole number values ranging from 0 to 255.

Remember all those checks in the JavaScript function to make sure we received acceptable RGB input values? If incorrect values were provided, the script will stop application execution by throwing exceptions. All of these potential errors have been wiped out by using JS++ and declaring types. These errors have been wiped out despite the fact that we haven’t modified any of the original JavaScript code. The runtime errors are still present in the JavaScript code, but they will never execute because the input values we provide will always be correct.

Try changing one of the ‘red’, ‘green’, or ‘blue’ variables to a number outside the 0 to 255 range:

external alert;
external rgbToHex;

byte red = -1;
byte green = 255;
byte blue = 255;
alert(rgbToHex(red, green, blue));

Now try to compile the modified main.jspp file. You’ll get an error:

[  ERROR  ] JSPPE5013: Computed value `-1' is out of range for type `byte' at line 4 char 11 at main.jspp

We can build on this and re-write the entire ‘rgbToHex’ function in JS++ instead of JavaScript to improve type safety while removing all the runtime checks and errors.

JS++ | Type System

Similar Reads

RGBtoHex: Using JavaScript from JS++

We’re going to create two files, a JavaScript file and a JS++ file, to introduce how they interact together. We’re going to use the classic ‘rgbToHex’ example to understand JS++ type guarantees....

Type Guarantees

“Type guarantees” are a unique feature of JS++ that mean your types are guaranteed to be correct during compile-time analysis and runtime code execution even when you’re polluting your JS++ code with untyped JavaScript code. It’s the closest thing you’ll find to the type safety featured in safer languages that don’t have to deal with large collections of untyped code....

RGB Values

The RGB color model defines three color values: red, blue, and green. These color values are numeric and must fit within the range 0-255. JS++ actually happens to have a data type that exactly fits the specification of being numeric and guaranteeing numbers to be in the 0-255 range: the ‘byte’ data type....

Examining the Untyped JavaScript

RGB values must be within the 0 to 255 range. Since JavaScript lacks data types, we have to manually perform these checks. Let’s inspect how we handle this in our ‘rgbToHex’ JavaScript function....

Using JavaScript More Safely

You can use JavaScript more safely from JS++ than you can from JavaScript itself....

Move rgbToHex to JS++

First, let’s stop importing the JavaScript ‘rgbToHex’ function by removing the ‘external’ statement that imports it. Our main.jspp file should now just look like this:...

Converting the “Untyped” JavaScript into “Typed” JS++

We’ll start converting our JavaScript ‘rgbToHex’ function into JS++ by removing all the runtime checks and errors. You won’t be needing them anymore....

Add More Types

Our ‘rgbToHex’ function is still declared with the keyword ‘function’. This is the JavaScript way of declaring a function, and it still leaves our function unsafe....

Other Considerations

As we learned in the previous chapter about JavaScript, we intuitively have a sense of the data type to expect....

Final Note

Why are JS++ type guarantees necessary? Because, with unsafe JavaScript, even large websites with resources, like GoDaddy, can have broken checkouts resulting in lost sales from a single TypeError:...