Initial compile time type check builtins

This commit is contained in:
dcodeIO
2018-03-17 12:54:37 +01:00
parent faac3c31eb
commit 2ed9fac171
15 changed files with 1158 additions and 657 deletions

10
std/assembly.d.ts vendored
View File

@ -194,6 +194,16 @@ declare function changetype<T>(value: any): T;
declare function isNaN<T = f32 | f64>(value: T): bool;
/** Tests if a 32-bit or 64-bit float is finite, that is not `NaN` or +/-`Infinity`. */
declare function isFinite<T = f32 | f64>(value: T): bool;
/** Tests if the specified expression is of an integer type and not a reference. Compiles to a constant. */
declare function isInteger(value: any): bool;
/** Tests if the specified expression is of a float type. Compiles to a constant. */
declare function isFloat(value: any): bool;
/** Tests if the specified expression is of a reference type. Compiles to a constant. */
declare function isReference(value: any): bool;
/** Tests if the specified expression can be used ass a string. Compiles to a constant. */
declare function isString(value: any): bool;
/** Tests if the specified expression can be used as an array. Compiles to a constant. */
declare function isArray(value: any): bool;
/** Traps if the specified value is not true-ish, otherwise returns the (non-nullable) value. */
declare function assert<T>(isTrueish: T, message?: string): T & object; // any better way to model `: T != null`?
/** Parses an integer string to a 64-bit float. */

View File

@ -1,3 +1,22 @@
// types
@builtin
export declare function isInteger(value: void): bool;
@builtin
export declare function isFloat(value: void): bool;
@builtin
export declare function isReference(value: void): bool;
@builtin
export declare function isString(value: void): bool;
@builtin
export declare function isArray(value: void): bool;
// math
@builtin
export declare const NaN: f64; // | f32

14
std/portable.d.ts vendored
View File

@ -136,6 +136,20 @@ declare function bswap16<T = i16 | u16 | i32 | u32>(value: T): T;
/** Changes the type of any value of `usize` kind to another one of `usize` kind. Useful for casting class instances to their pointer values and vice-versa. Beware that this is unsafe.*/
declare function changetype<T>(value: any): T;
/** Tests if a 32-bit or 64-bit float is `NaN`. */
declare function isNaN<T = f32 | f64>(value: T): bool;
/** Tests if a 32-bit or 64-bit float is finite, that is not `NaN` or +/-`Infinity`. */
declare function isFinite<T = f32 | f64>(value: T): bool;
/** Tests if the specified value is a valid integer. Can't distinguish an integer from an integral float. */
declare function isInteger(value: any): bool;
/** Tests if the specified value is a valid float. Can't distinguish a float from an integer. */
declare function isFloat(value: any): bool;
/** Tests if the specified value is of a reference type. */
declare function isReference(value: any): bool;
/** Tests if the specified value can be used as a string. */
declare function isString(value: any): bool;
/** Tests if the specified value can be used as an array. */
declare function isArray(value: any): bool;
/** Traps if the specified value is not true-ish, otherwise returns the value. */
declare function assert<T>(isTrueish: T | null, message?: string): T;
/** Parses an integer string to a 64-bit float. */

View File

@ -148,3 +148,19 @@ String["fromCharCodes"] = function fromCharCodes(arr) {
String["fromCodePoints"] = function fromCodePoints(arr) {
return String.fromCodePoint.apply(String, arr);
};
globalScope["isInteger"] = Number.isInteger;
globalScope["isFloat"] = function isFloat(arg) {
return typeof arg === "number";
};
globalScope["isReference"] = function isClass(arg) {
return typeof arg === "object" || typeof arg === "string";
};
globalScope["isString"] = function isString(arg) {
return typeof arg === "string" || arg instanceof String;
};
globalScope["isArray"] = Array.isArray;