Skip to content

Implement isSet<T>() and isMap<T>() #1106

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

Closed
wants to merge 2 commits into from
Closed
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
26 changes: 26 additions & 0 deletions src/builtins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ export namespace BuiltinNames {
export const isReference = "~lib/builtins/isReference";
export const isString = "~lib/builtins/isString";
export const isArray = "~lib/builtins/isArray";
export const isMap = "~lib/builtins/isMap";
export const isSet = "~lib/builtins/isSet";
export const isArrayLike = "~lib/builtins/isArrayLike";
export const isFunction = "~lib/builtins/isFunction";
export const isNullable = "~lib/builtins/isNullable";
Expand Down Expand Up @@ -674,6 +676,30 @@ export function compileCall(
if (!type) return module.unreachable();
return module.i32(type.signatureReference ? 1 : 0);
}
case BuiltinNames.isMap: { // isArray<T!>() / isArray<T?>(value: T) -> bool
let type = evaluateConstantType(compiler, typeArguments, operands, reportNode);
compiler.currentType = Type.bool;
if (!type) return module.unreachable();
if (type.is(TypeFlags.REFERENCE)) {
let classReference = type.classReference;
if (classReference) {
return module.i32(classReference.prototype.extends(compiler.program.mapPrototype) ? 1 : 0);
}
}
return module.i32(0);
}
case BuiltinNames.isSet: { // isArray<T!>() / isArray<T?>(value: T) -> bool
let type = evaluateConstantType(compiler, typeArguments, operands, reportNode);
compiler.currentType = Type.bool;
if (!type) return module.unreachable();
if (type.is(TypeFlags.REFERENCE)) {
let classReference = type.classReference;
if (classReference) {
return module.i32(classReference.prototype.extends(compiler.program.setPrototype) ? 1 : 0);
}
}
return module.i32(0);
}
case BuiltinNames.isNullable: { // isNullable<T!> / isNullable<T?>(value: T) -> bool
let type = evaluateConstantType(compiler, typeArguments, operands, reportNode);
compiler.currentType = Type.bool;
Expand Down
8 changes: 8 additions & 0 deletions std/assembly/builtins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ export declare function isString<T>(value?: T): bool;
@builtin
export declare function isArray<T>(value?: T): bool;

// @ts-ignore: decorator
@builtin
export declare function isMap<T>(value?: T): bool;

// @ts-ignore: decorator
@builtin
export declare function isSet<T>(value?: T): bool;

// @ts-ignore: decorator
@builtin
export declare function isArrayLike<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 @@ -156,6 +156,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 can be used as a Map. Compiles to a constant. */
declare function isMap<T>(value?: any): value is Map<any, any>;
/** Tests if the specified type *or* expression can be used as a Set. Compiles to a constant. */
declare function isSet<T>(value?: any): value is Set<any>;
/** Tests if the specified type *or* expression can be used as an array like object. Compiles to a constant. */
declare function isArrayLike<T>(value?: any): value is ArrayLike<any>;
/** Tests if the specified type *or* expression is of a function type. Compiles to a constant. */
Expand Down
Loading