Skip to content

Commit f318d68

Browse files
authored
Add isFunction and isNullable builtins (AssemblyScript#504)
1 parent 3b5b96f commit f318d68

File tree

6 files changed

+209
-96
lines changed

6 files changed

+209
-96
lines changed

src/builtins.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ export namespace BuiltinSymbols {
7676
export const isReference = "~lib/builtins/isReference";
7777
export const isString = "~lib/builtins/isString";
7878
export const isArray = "~lib/builtins/isArray";
79+
export const isFunction = "~lib/builtins/isFunction";
80+
export const isNullable = "~lib/builtins/isNullable";
7981
export const isDefined = "~lib/builtins/isDefined";
8082
export const isConstant = "~lib/builtins/isConstant";
8183
export const isManaged = "~lib/builtins/isManaged";
@@ -348,6 +350,18 @@ export function compileCall(
348350
: 0
349351
);
350352
}
353+
case BuiltinSymbols.isFunction: { // isFunction<T!> / isFunction<T?>(value: T) -> bool
354+
let type = evaluateConstantType(compiler, typeArguments, operands, reportNode);
355+
compiler.currentType = Type.bool;
356+
if (!type) return module.createUnreachable();
357+
return module.createI32(type.signatureReference ? 1 : 0);
358+
}
359+
case BuiltinSymbols.isNullable: { // isNullable<T!> / isNullable<T?>(value: T) -> bool
360+
let type = evaluateConstantType(compiler, typeArguments, operands, reportNode);
361+
compiler.currentType = Type.bool;
362+
if (!type) return module.createUnreachable();
363+
return module.createI32(type.is(TypeFlags.NULLABLE) ? 1 : 0);
364+
}
351365
case BuiltinSymbols.isDefined: { // isDefined(expression) -> bool
352366
compiler.currentType = Type.bool;
353367
if (typeArguments) {

std/assembly/builtins.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
@builtin export declare function isReference<T>(value?: T): bool;
1010
@builtin export declare function isString<T>(value?: T): bool;
1111
@builtin export declare function isArray<T>(value?: T): bool;
12+
@builtin export declare function isFunction<T>(value?: T): bool;
13+
@builtin export declare function isNullable<T>(value?: T): bool;
1214
@builtin export declare function isDefined(expression: void): bool;
1315
@builtin export declare function isConstant(expression: void): bool;
1416
@builtin export declare function isManaged<T>(value?: T): bool;

std/assembly/index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ declare function isReference<T>(value?: any): value is object | string;
128128
declare function isString<T>(value?: any): value is string | String;
129129
/** Tests if the specified type *or* expression can be used as an array. Compiles to a constant. */
130130
declare function isArray<T>(value?: any): value is Array<any>;
131+
/** Tests if the specified type *or* expression is of a function type. Compiles to a constant. */
132+
declare function isFunction<T>(value?: any): value is Array<any>;
133+
/** Tests if the specified type *or* expression is of a nullable reference type. Compiles to a constant. */
134+
declare function isNullable<T>(value?: any): value is Array<any>;
131135
/** Tests if the specified expression resolves to a defined element. Compiles to a constant. */
132136
declare function isDefined(expression: any): bool;
133137
/** Tests if the specified expression evaluates to a constant value. Compiles to a constant. */

tests/compiler/builtins.optimized.wat

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
if
4444
i32.const 0
4545
i32.const 8
46-
i32.const 50
46+
i32.const 59
4747
i32.const 19
4848
call $~lib/env/abort
4949
unreachable
@@ -56,7 +56,7 @@
5656
if
5757
i32.const 0
5858
i32.const 8
59-
i32.const 51
59+
i32.const 60
6060
i32.const 20
6161
call $~lib/env/abort
6262
unreachable
@@ -69,7 +69,7 @@
6969
if
7070
i32.const 0
7171
i32.const 8
72-
i32.const 52
72+
i32.const 61
7373
i32.const 20
7474
call $~lib/env/abort
7575
unreachable
@@ -92,7 +92,7 @@
9292
if
9393
i32.const 0
9494
i32.const 8
95-
i32.const 68
95+
i32.const 77
9696
i32.const 19
9797
call $~lib/env/abort
9898
unreachable
@@ -105,7 +105,7 @@
105105
if
106106
i32.const 0
107107
i32.const 8
108-
i32.const 69
108+
i32.const 78
109109
i32.const 20
110110
call $~lib/env/abort
111111
unreachable
@@ -118,7 +118,7 @@
118118
if
119119
i32.const 0
120120
i32.const 8
121-
i32.const 70
121+
i32.const 79
122122
i32.const 20
123123
call $~lib/env/abort
124124
unreachable

tests/compiler/builtins.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var b: bool;
2+
class C {}
23

34
// type checks
45

@@ -10,6 +11,10 @@ assert(isReference<string>());
1011
assert(!isReference<usize>());
1112
assert(isArray<i32[]>());
1213
assert(!isArray<usize>());
14+
assert(isFunction<() => void>());
15+
assert(!isFunction<u32>());
16+
assert(isNullable<C | null>());
17+
assert(!isNullable<C>());
1318

1419
assert(isInteger(<i32>1));
1520
assert(!isInteger(<f32>1));
@@ -21,6 +26,10 @@ assert(isString("1"));
2126
assert(!isString(1));
2227
assert(isArray(changetype<i32[]>(null)));
2328
assert(!isArray(changetype<usize>(null)));
29+
assert(isFunction(changetype<() => void>(null)));
30+
assert(!isFunction(changetype<u32>(null)));
31+
assert(isNullable(changetype<C | null>(null)));
32+
assert(!isNullable(changetype<C>(null)));
2433

2534
// evaluation
2635

0 commit comments

Comments
 (0)