Skip to content

Commit fc8859c

Browse files
committed
feat: support generic function overload
Signed-off-by: moruiyi <[email protected]>
1 parent 5761201 commit fc8859c

18 files changed

+511
-13244
lines changed

assembly/entity.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ export class Entity {
88
return left.id == right.id && left.version == right.version;
99
}
1010

11-
@inline
11+
// @ts-ignore: decorator
12+
@inline()
1213
constructor(offset: usize = heap.alloc(sizeof<Entity>())) {
1314
return changetype<Entity>(offset);
1415
}

assembly/index.ts

+6-38
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
// The entry file of your WebAssembly module.
22
import "wasi"
33
import { Entity } from "./entity";
4+
import './system'
45

5-
@uniqueid @unmanaged
6-
class Foo {
6+
@family @unmanaged
7+
export class Foo {
78
index: i32;
89
version: i32;
910
@inline static get offset(): usize {
1011
return offsetof<Foo>();
1112
}
1213
}
1314

14-
@uniqueid
15-
class Coo {
15+
@family
16+
export class Coo {
1617

1718
}
1819

@@ -60,38 +61,5 @@ class Vector<T> {
6061

6162

6263
export function add(a: i32, b: i32): i32 {
63-
// let ent = new Entity();
64-
// let ent1 = new Entity();
65-
// ent.id = 123;
66-
// ent1.id = 456;
67-
// ent1.dispose();
68-
// return uniqueidof<Coo>();
69-
const arr = new StaticArray<u32>(1000000)
70-
const arrResult = new StaticArray<bool>(1000000)
71-
const arrResult1 = new StaticArray<bool>(1000000)
72-
for (let i = 0; i < 1000000; i++) {
73-
arr[i] = <u32>(Math.random() * <f64>u32.MAX_VALUE)
74-
}
75-
76-
let now: i64 = 0;
77-
now = Date.now()
78-
const mask = <u32>parseInt("111011", 2)
79-
for (let i = 0; i < 1000000; i++) {
80-
arrResult[i] = (arr[i] ^ mask) > 0
81-
}
82-
console.log(`${Date.now() - now}`)
83-
84-
// now = Date.now()
85-
86-
// const arrPtr = changetype<usize>(arr)
87-
// for (let i = 0; i < 1000000 / sizeof<u32>(); i++) {
88-
// const i = 0;
89-
// var r = i32x4.all_true(v128.xor(v128.load32_zero(arrPtr + i * sizeof<u32>()), i32x4.splat(59)))
90-
// arrResult1[i] = i32x4.extract_lane(r, 0) > 0
91-
// arrResult1[i + 1] = (i32x4.extract_lane(r, 1) > 0)
92-
// arrResult1[i + 2] = (i32x4.extract_lane(r, 2) > 0)
93-
// arrResult1[i + 3] = (i32x4.extract_lane(r, 3) > 0)
94-
// }
95-
// console.log(`${Date.now() - now}`)
96-
return 1;
64+
return familyof<Foo>();
9765
}

assembly/system.ts

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class EntityManager {
2+
@overloadgeneric("foreach")
3+
protected foreach1<T1>(cb: (t1: T1) => void): void {}
4+
@overloadgeneric("foreach")
5+
protected foreach2<T1, T2>(cb: (t1: T1, t2: T2) => void): void {}
6+
@overloadgeneric("foreach")
7+
foreach3<T1, T2, T3>(cb: (t1: T1, t2: T2, t3: T3) => void): void {}
8+
@overloadgeneric("foreach")
9+
protected foreach4<T1, T2, T3, T4>(
10+
cb: (t1: T1, t2: T2, t3: T3, t4: T4) => void
11+
): void {}
12+
13+
abc<T>(a: T): T {
14+
return a;
15+
this.foreach(a => {
16+
17+
})
18+
}
19+
}
20+
21+
class Temp {
22+
readonly a: i32;
23+
}
24+
25+
const Entities = new EntityManager();
26+
const a = new Temp();
27+
Entities.foreach((a: Temp, b: i32) => {
28+
// a.index = 123\
29+
});
30+
31+
Entities.abc(110);

assembly/transform.d.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
1-
declare function uniqueid(constructor: Function): void;
2-
declare function uniqueidof<T>(): u32
1+
declare function family(constructor: Function): void;
2+
declare function familyof<T>(): u32;
3+
declare type Readonly<T> = {
4+
readonly [P in keyof T]: T[P];
5+
};
6+
declare function overloadgeneric(funcName: string): any;
7+
declare interface EntityManager {
8+
foreach<T extends any[]>(cb: (...args: T) => void): void
9+
}

build/.gitignore

-3
This file was deleted.

0 commit comments

Comments
 (0)