1
1
#include < napi.h>
2
2
3
+ class TestIter : public Napi ::ObjectWrap<TestIter> {
4
+ public:
5
+ TestIter (const Napi::CallbackInfo& info) : Napi::ObjectWrap<TestIter>(info) {}
6
+
7
+ Napi::Value Next (const Napi::CallbackInfo& info) {
8
+ auto object = Napi::Object::New (info.Env ());
9
+ object.Set (" done" , Napi::Boolean::New (info.Env (), true ));
10
+ return object;
11
+ }
12
+
13
+ static void Initialize (Napi::Env env, Napi::Object exports) {
14
+ Constructor = Napi::Persistent (DefineClass (env, " TestIter" , {
15
+ InstanceMethod (" next" , &TestIter::Next),
16
+ }));
17
+ }
18
+
19
+ static Napi::FunctionReference Constructor;
20
+ };
21
+
22
+ Napi::FunctionReference TestIter::Constructor;
23
+
3
24
class Test : public Napi ::ObjectWrap<Test> {
4
25
public:
5
26
Test (const Napi::CallbackInfo& info) :
@@ -14,10 +35,15 @@ class Test : public Napi::ObjectWrap<Test> {
14
35
return Napi::Number::New (info.Env (), value);
15
36
}
16
37
38
+ Napi::Value Iter (const Napi::CallbackInfo& info) {
39
+ return TestIter::Constructor.New ({});
40
+ }
41
+
17
42
static void Initialize (Napi::Env env, Napi::Object exports) {
18
43
exports.Set (" Test" , DefineClass (env, " Test" , {
19
44
InstanceMethod (" test_set" , &Test::Set),
20
- InstanceMethod (" test_get" , &Test::Get)
45
+ InstanceMethod (" test_get" , &Test::Get),
46
+ InstanceMethod (Napi::Symbol::WellKnown (env, " iterator" ), &Test::Iter)
21
47
}));
22
48
}
23
49
@@ -28,5 +54,6 @@ class Test : public Napi::ObjectWrap<Test> {
28
54
Napi::Object InitObjectWrap (Napi::Env env) {
29
55
Napi::Object exports = Napi::Object::New (env);
30
56
Test::Initialize (env, exports);
57
+ TestIter::Initialize (env, exports);
31
58
return exports;
32
59
}
0 commit comments