Skip to content

Commit 2a70ee5

Browse files
committed
familyof for unmanaged classes. suggestion AssemblyScript#2097 (comment)
1 parent a962528 commit 2a70ee5

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

src/builtins.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ export namespace BuiltinNames {
188188
export const unchecked = "~lib/builtins/unchecked";
189189
export const instantiate = "~lib/builtins/instantiate";
190190
export const idof = "~lib/builtins/idof";
191+
export const familyof = "~lib/builtins/familyof";
191192

192193
export const i8 = "~lib/builtins/i8";
193194
export const i16 = "~lib/builtins/i16";
@@ -1085,6 +1086,36 @@ function builtin_idof(ctx: BuiltinContext): ExpressionRef {
10851086
}
10861087
builtins.set(BuiltinNames.idof, builtin_idof);
10871088

1089+
1090+
function builtin_familyof(ctx: BuiltinContext): ExpressionRef {
1091+
var compiler = ctx.compiler;
1092+
var module = compiler.module;
1093+
var program = compiler.program;
1094+
var type = evaluateConstantType(ctx);
1095+
compiler.currentType = Type.u32;
1096+
if (!type) return module.unreachable();
1097+
let signatureReference = type.getSignature();
1098+
if (signatureReference) {
1099+
return module.i32(signatureReference.id);
1100+
}
1101+
let classReference = type.getClassOrWrapper(compiler.program);
1102+
1103+
if(classReference !== null){
1104+
return module.i32(program.unmanagedClassNames.get(classReference.internalName) as i32);
1105+
}
1106+
1107+
compiler.error(
1108+
DiagnosticCode.Operation_0_cannot_be_applied_to_type_1,
1109+
ctx.reportNode.typeArgumentsRange,
1110+
"familyof",
1111+
type.toString()
1112+
);
1113+
return module.unreachable();
1114+
}
1115+
1116+
// in Transform
1117+
builtins.set(BuiltinNames.familyof, builtin_familyof);
1118+
10881119
// === Math ===================================================================================
10891120

10901121
// clz<T?>(value: T) -> T

src/program.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,8 @@ export class Program extends DiagnosticEmitter {
455455
nativeFile!: File;
456456
/** Next class id. */
457457
nextClassId: u32 = 0;
458+
/** Next family id. */
459+
nextFamilyId: u32 = 0;
458460
/** Next signature id. */
459461
nextSignatureId: i32 = 0;
460462
/** An indicator if the program has been initialized. */
@@ -474,6 +476,8 @@ export class Program extends DiagnosticEmitter {
474476
wrapperClasses: Map<Type,Class> = new Map();
475477
/** Managed classes contained in the program, by id. */
476478
managedClasses: Map<i32,Class> = new Map();
479+
/** unmanaged classes contained in the program, by id. */
480+
unmanagedClassNames: Map<string,i32> = new Map();
477481
/** A set of unique function signatures contained in the program, by id. */
478482
uniqueSignatures: Signature[] = new Array<Signature>(0);
479483

@@ -4211,6 +4215,10 @@ export class Class extends TypedElement {
42114215
this._id = id;
42124216
program.managedClasses.set(id, this);
42134217
}
4218+
else{
4219+
let id = program.nextFamilyId++;
4220+
program.unmanagedClassNames.set(this.internalName, id);
4221+
}
42144222

42154223
// apply pre-checked instance-specific contextual type arguments
42164224
var typeParameters = prototype.typeParameterNodes;

std/assembly/builtins.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ export declare function offsetof<T>(fieldName?: string): usize; // | u32 / u64
160160
@builtin
161161
export declare function idof<T>(): u32;
162162

163+
// @ts-ignore: decorator
164+
@builtin
165+
export declare function familyof<T>(): u32;
166+
163167
// @ts-ignore
164168
@builtin
165169
export declare function nameof<T>(): string;

std/assembly/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ declare function offsetof<T>(fieldName?: string): usize;
172172
declare function nameof<T>(value?: T): string;
173173
/** Determines the unique runtime id of a class type. Compiles to a constant. */
174174
declare function idof<T>(): u32;
175+
/** Determines the unique runtime id of a unmanaged class type. Compiles to a constant. */
176+
declare function familyof<T>(): u32;
175177
/** 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.*/
176178
declare function changetype<T>(value: any): T;
177179
/** Explicitly requests no bounds checks on the provided expression. Useful for array accesses. */

0 commit comments

Comments
 (0)