11#include < napi.h>
22
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+
324class Test : public Napi ::ObjectWrap<Test> {
425public:
526 Test (const Napi::CallbackInfo& info) :
@@ -14,10 +35,15 @@ class Test : public Napi::ObjectWrap<Test> {
1435 return Napi::Number::New (info.Env (), value);
1536 }
1637
38+ Napi::Value Iter (const Napi::CallbackInfo& info) {
39+ return TestIter::Constructor.New ({});
40+ }
41+
1742 static void Initialize (Napi::Env env, Napi::Object exports) {
1843 exports.Set (" Test" , DefineClass (env, " Test" , {
1944 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)
2147 }));
2248 }
2349
@@ -28,5 +54,6 @@ class Test : public Napi::ObjectWrap<Test> {
2854Napi::Object InitObjectWrap (Napi::Env env) {
2955 Napi::Object exports = Napi::Object::New (env);
3056 Test::Initialize (env, exports);
57+ TestIter::Initialize (env, exports);
3158 return exports;
3259}
0 commit comments