Skip to content

Add isFunction and isNullable builtins #504

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/builtins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ export namespace BuiltinSymbols {
export const isReference = "~lib/builtins/isReference";
export const isString = "~lib/builtins/isString";
export const isArray = "~lib/builtins/isArray";
export const isFunction = "~lib/builtins/isFunction";
export const isNullable = "~lib/builtins/isNullable";
export const isDefined = "~lib/builtins/isDefined";
export const isConstant = "~lib/builtins/isConstant";
export const isManaged = "~lib/builtins/isManaged";
Expand Down Expand Up @@ -348,6 +350,18 @@ export function compileCall(
: 0
);
}
case BuiltinSymbols.isFunction: { // isFunction<T!> / isFunction<T?>(value: T) -> bool
let type = evaluateConstantType(compiler, typeArguments, operands, reportNode);
compiler.currentType = Type.bool;
if (!type) return module.createUnreachable();
return module.createI32(type.signatureReference ? 1 : 0);
}
case BuiltinSymbols.isNullable: { // isNullable<T!> / isNullable<T?>(value: T) -> bool
let type = evaluateConstantType(compiler, typeArguments, operands, reportNode);
compiler.currentType = Type.bool;
if (!type) return module.createUnreachable();
return module.createI32(type.is(TypeFlags.NULLABLE) ? 1 : 0);
}
case BuiltinSymbols.isDefined: { // isDefined(expression) -> bool
compiler.currentType = Type.bool;
if (typeArguments) {
Expand Down
2 changes: 2 additions & 0 deletions std/assembly/builtins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
@builtin export declare function isReference<T>(value?: T): bool;
@builtin export declare function isString<T>(value?: T): bool;
@builtin export declare function isArray<T>(value?: T): bool;
@builtin export declare function isFunction<T>(value?: T): bool;
@builtin export declare function isNullable<T>(value?: T): bool;
@builtin export declare function isDefined(expression: void): bool;
@builtin export declare function isConstant(expression: void): bool;
@builtin export declare function isManaged<T>(value?: T): bool;
Expand Down
4 changes: 4 additions & 0 deletions std/assembly/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ declare function isReference<T>(value?: any): value is object | string;
declare function isString<T>(value?: any): value is string | String;
/** Tests if the specified type *or* expression can be used as an array. Compiles to a constant. */
declare function isArray<T>(value?: any): value is Array<any>;
/** Tests if the specified type *or* expression is of a function type. Compiles to a constant. */
declare function isFunction<T>(value?: any): value is Array<any>;
/** Tests if the specified type *or* expression is of a nullable reference type. Compiles to a constant. */
declare function isNullable<T>(value?: any): value is Array<any>;
/** Tests if the specified expression resolves to a defined element. Compiles to a constant. */
declare function isDefined(expression: any): bool;
/** Tests if the specified expression evaluates to a constant value. Compiles to a constant. */
Expand Down
12 changes: 6 additions & 6 deletions tests/compiler/builtins.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
if
i32.const 0
i32.const 8
i32.const 50
i32.const 59
i32.const 19
call $~lib/env/abort
unreachable
Expand All @@ -56,7 +56,7 @@
if
i32.const 0
i32.const 8
i32.const 51
i32.const 60
i32.const 20
call $~lib/env/abort
unreachable
Expand All @@ -69,7 +69,7 @@
if
i32.const 0
i32.const 8
i32.const 52
i32.const 61
i32.const 20
call $~lib/env/abort
unreachable
Expand All @@ -92,7 +92,7 @@
if
i32.const 0
i32.const 8
i32.const 68
i32.const 77
i32.const 19
call $~lib/env/abort
unreachable
Expand All @@ -105,7 +105,7 @@
if
i32.const 0
i32.const 8
i32.const 69
i32.const 78
i32.const 20
call $~lib/env/abort
unreachable
Expand All @@ -118,7 +118,7 @@
if
i32.const 0
i32.const 8
i32.const 70
i32.const 79
i32.const 20
call $~lib/env/abort
unreachable
Expand Down
9 changes: 9 additions & 0 deletions tests/compiler/builtins.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var b: bool;
class C {}

// type checks

Expand All @@ -10,6 +11,10 @@ assert(isReference<string>());
assert(!isReference<usize>());
assert(isArray<i32[]>());
assert(!isArray<usize>());
assert(isFunction<() => void>());
assert(!isFunction<u32>());
assert(isNullable<C | null>());
assert(!isNullable<C>());

assert(isInteger(<i32>1));
assert(!isInteger(<f32>1));
Expand All @@ -21,6 +26,10 @@ assert(isString("1"));
assert(!isString(1));
assert(isArray(changetype<i32[]>(null)));
assert(!isArray(changetype<usize>(null)));
assert(isFunction(changetype<() => void>(null)));
assert(!isFunction(changetype<u32>(null)));
assert(isNullable(changetype<C | null>(null)));
assert(!isNullable(changetype<C>(null)));

// evaluation

Expand Down
Loading