Skip to content

Commit 8293c3f

Browse files
avpmeta-codesync[bot]
authored andcommitted
FlowLib: Add typed Set wrapper
Summary: Add a generic `Set<T>` to FlowLib that wraps the native Set with typed add/has/delete/clear/forEach methods, allowing typed code to use Set methods directly. Differential Revision: D96520014
1 parent 8349087 commit 8293c3f

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

lib/FlowLib/04-set.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
class Set<T> {
9+
/// Native implementation of this Set.
10+
#impl: any;
11+
12+
constructor() {
13+
"inline";
14+
this.#impl = new globalThis.Set();
15+
}
16+
17+
@Hermes.final
18+
add(v: T): Set<T> {
19+
"inline";
20+
this.#impl.add(v);
21+
return this;
22+
}
23+
24+
@Hermes.final
25+
has(v: T): bool {
26+
"inline";
27+
return this.#impl.has(v) as bool;
28+
}
29+
30+
@Hermes.final
31+
delete(v: T): bool {
32+
"inline";
33+
return this.#impl.delete(v) as bool;
34+
}
35+
36+
@Hermes.final
37+
clear(): void {
38+
"inline";
39+
this.#impl.clear();
40+
}
41+
42+
@Hermes.final
43+
forEach(cb: (v: T, k?: T, s?: Set<T>) => any): void {
44+
"inline";
45+
this.#impl.forEach((v, k, s) => {
46+
return cb(v, k, this);
47+
});
48+
}
49+
}

lib/FlowLib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ set(flowlib_sources
1616
"01-array.js"
1717
"02-string.js"
1818
"03-map.js"
19+
"04-set.js"
1920
)
2021
list(SORT flowlib_sources)
2122

test/hermes/flow/set.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
*/
9+
10+
// RUN: %hermes -O0 -typed %s | %FileCheck --match-full-lines %s
11+
// RUN: %hermes -typed %s | %FileCheck --match-full-lines %s
12+
// RUN: %shermes -typed -exec %s | %FileCheck --match-full-lines %s
13+
14+
// Basic add/has.
15+
let s = new Set<string>();
16+
s.add('a');
17+
s.add('b');
18+
print(s.has('a'));
19+
// CHECK: true
20+
print(s.has('b'));
21+
// CHECK-NEXT: true
22+
print(s.has('c'));
23+
// CHECK-NEXT: false
24+
25+
// Chained add.
26+
let s2 = new Set<number>();
27+
s2.add(1).add(2).add(3);
28+
print(s2.has(1));
29+
// CHECK-NEXT: true
30+
print(s2.has(3));
31+
// CHECK-NEXT: true
32+
33+
// delete().
34+
print(s.delete('a'));
35+
// CHECK-NEXT: true
36+
print(s.has('a'));
37+
// CHECK-NEXT: false
38+
print(s.delete('a'));
39+
// CHECK-NEXT: false
40+
41+
// clear().
42+
s.add('x');
43+
s.add('y');
44+
print(s.has('x'));
45+
// CHECK-NEXT: true
46+
s.clear();
47+
print(s.has('x'));
48+
// CHECK-NEXT: false
49+
print(s.has('b'));
50+
// CHECK-NEXT: false
51+
52+
// forEach().
53+
let s3 = new Set<number>();
54+
s3.add(10).add(20).add(30);
55+
s3.forEach(v => { print(v); });
56+
// CHECK-NEXT: 10
57+
// CHECK-NEXT: 20
58+
// CHECK-NEXT: 30

0 commit comments

Comments
 (0)