@@ -16,25 +16,22 @@ Napi::FunctionReference Backup::constructor;
1616Napi::Object Backup::Init (Napi::Env env, Napi::Object exports) {
1717 Napi::HandleScope scope (env);
1818
19- Napi::FunctionReference t = Napi::Function::New (env, New);
20-
21-
22- t->SetClassName (Napi::String::New (env, " Backup" ));
23-
24- InstanceMethod (" step" , &Step),
25- InstanceMethod (" finish" , &Finish),
26-
27- NODE_SET_GETTER (t, " idle" , IdleGetter);
28- NODE_SET_GETTER (t, " completed" , CompletedGetter);
29- NODE_SET_GETTER (t, " failed" , FailedGetter);
30- NODE_SET_GETTER (t, " remaining" , RemainingGetter);
31- NODE_SET_GETTER (t, " pageCount" , PageCountGetter);
32-
33- NODE_SET_SETTER (t, " retryErrors" , RetryErrorGetter, RetryErrorSetter);
34-
35- constructor.Reset (t);
36- (target).Set (Napi::String::New (env, " Backup" ),
37- Napi::GetFunction (t));
19+ Napi::Function t = DefineClass (env, " Backup" , {
20+ InstanceMethod (" step" , &Backup::Step),
21+ InstanceMethod (" finish" , &Backup::Finish),
22+ InstanceAccessor (" idle" , &Backup::IdleGetter, nullptr ),
23+ InstanceAccessor (" completed" , &Backup::CompletedGetter, nullptr ),
24+ InstanceAccessor (" failed" , &Backup::FailedGetter, nullptr ),
25+ InstanceAccessor (" remaining" , &Backup::RemainingGetter, nullptr ),
26+ InstanceAccessor (" pageCount" , &Backup::PageCountGetter, nullptr ),
27+ InstanceAccessor (" retryErrors" , &Backup::RetryErrorGetter, &Backup::RetryErrorSetter),
28+ });
29+
30+ constructor = Napi::Persistent (t);
31+ constructor.SuppressDestruct ();
32+
33+ exports.Set (" Backup" , t);
34+ return exports;
3835}
3936
4037void Backup::Process () {
@@ -65,32 +62,34 @@ void Backup::Schedule(Work_Callback callback, Baton* baton) {
6562}
6663
6764template <class T > void Backup::Error (T* baton) {
65+ Napi::Env env = baton->backup ->Env ();
6866 Napi::HandleScope scope (env);
6967
7068 Backup* backup = baton->backup ;
7169 // Fail hard on logic errors.
7270 assert (backup->status != 0 );
73- EXCEPTION (backup->message , backup->status , exception);
71+ EXCEPTION (Napi::String::New (env, backup->message ) , backup->status , exception);
7472
75- Napi::Function cb = Napi::New (env, baton->callback );
73+ Napi::Function cb = baton->callback . Value ( );
7674
77- if (!cb.IsEmpty () && cb-> IsFunction ()) {
75+ if (!cb.IsEmpty () && cb. IsFunction ()) {
7876 Napi::Value argv[] = { exception };
79- TRY_CATCH_CALL (backup->handle (), cb, 1 , argv);
77+ TRY_CATCH_CALL (backup->Value (), cb, 1 , argv);
8078 }
8179 else {
8280 Napi::Value argv[] = { Napi::String::New (env, " error" ), exception };
83- EMIT_EVENT (backup->handle (), 2 , argv);
81+ EMIT_EVENT (backup->Value (), 2 , argv);
8482 }
8583}
8684
8785void Backup::CleanQueue () {
86+ Napi::Env env = this ->Env ();
8887 Napi::HandleScope scope (env);
8988
9089 if (inited && !queue.empty ()) {
9190 // This backup has already been initialized and is now finished.
9291 // Fire error for all remaining items in the queue.
93- EXCEPTION (" Backup is already finished" , SQLITE_MISUSE , exception);
92+ EXCEPTION (Napi::String::New (env, " Backup is already finished" ) , SQLITE_MISUSE , exception);
9493 Napi::Value argv[] = { exception };
9594 bool called = false ;
9695
@@ -99,11 +98,11 @@ void Backup::CleanQueue() {
9998 Call* call = queue.front ();
10099 queue.pop ();
101100
102- Napi::Function cb = Napi::New (env, call->baton ->callback );
101+ Napi::Function cb = call->baton ->callback . Value ( );
103102
104103 if (inited && !cb.IsEmpty () &&
105- cb-> IsFunction ()) {
106- TRY_CATCH_CALL (handle (), cb, 1 , argv);
104+ cb. IsFunction ()) {
105+ TRY_CATCH_CALL (Value (), cb, 1 , argv);
107106 called = true ;
108107 }
109108
@@ -117,7 +116,7 @@ void Backup::CleanQueue() {
117116 // Backup object.
118117 if (!called) {
119118 Napi::Value info[] = { Napi::String::New (env, " error" ), exception };
120- EMIT_EVENT (handle (), 2 , info);
119+ EMIT_EVENT (Value (), 2 , info);
121120 }
122121 }
123122 else while (!queue.empty ()) {
@@ -133,61 +132,59 @@ void Backup::CleanQueue() {
133132 }
134133}
135134
136- Napi::Value Backup::New (const Napi::CallbackInfo& info) {
135+ Backup::Backup (const Napi::CallbackInfo& info) : Napi::ObjectWrap<Backup>(info) {
136+ Napi::Env env = info.Env ();
137137 if (!info.IsConstructCall ()) {
138138 Napi::TypeError::New (env, " Use the new operator to create new Backup objects" ).ThrowAsJavaScriptException ();
139- return env. Null () ;
139+ return ;
140140 }
141141
142142 int length = info.Length ();
143143
144144 if (length <= 0 || !Database::HasInstance (info[0 ])) {
145145 Napi::TypeError::New (env, " Database object expected" ).ThrowAsJavaScriptException ();
146- return env. Null () ;
146+ return ;
147147 }
148148 else if (length <= 1 || !info[1 ].IsString ()) {
149149 Napi::TypeError::New (env, " Filename expected" ).ThrowAsJavaScriptException ();
150- return env. Null () ;
150+ return ;
151151 }
152152 else if (length <= 2 || !info[2 ].IsString ()) {
153153 Napi::TypeError::New (env, " Source database name expected" ).ThrowAsJavaScriptException ();
154- return env. Null () ;
154+ return ;
155155 }
156156 else if (length <= 3 || !info[3 ].IsString ()) {
157157 Napi::TypeError::New (env, " Destination database name expected" ).ThrowAsJavaScriptException ();
158- return env. Null () ;
158+ return ;
159159 }
160160 else if (length <= 4 || !info[4 ].IsBoolean ()) {
161161 Napi::TypeError::New (env, " Direction flag expected" ).ThrowAsJavaScriptException ();
162- return env. Null () ;
162+ return ;
163163 }
164164 else if (length > 5 && !info[5 ].IsUndefined () && !info[5 ].IsFunction ()) {
165165 Napi::TypeError::New (env, " Callback expected" ).ThrowAsJavaScriptException ();
166- return env. Null () ;
166+ return ;
167167 }
168168
169- Database* db = info[0 ].As <Napi::Object>(). Unwrap <Database>( );
169+ Database* db = Napi::ObjectWrap<Database>:: Unwrap ( info[0 ].As <Napi::Object>());
170170 Napi::String filename = info[1 ].As <Napi::String>();
171171 Napi::String sourceName = info[2 ].As <Napi::String>();
172172 Napi::String destName = info[3 ].As <Napi::String>();
173173 Napi::Boolean filenameIsDest = info[4 ].As <Napi::Boolean>();
174174
175- info.This ().DefineProperty (Napi::String::New (env, " filename" ) , filename, ReadOnly );
176- info.This ().DefineProperty (Napi::String::New (env, " sourceName" ) , sourceName, ReadOnly );
177- info.This ().DefineProperty (Napi::String::New (env, " destName" ) , destName, ReadOnly );
178- info.This ().DefineProperty (Napi::String::New (env, " filenameIsDest" ) , filenameIsDest, ReadOnly );
175+ info.This ().As <Napi::Object>(). DefineProperty (Napi::PropertyDescriptor::Value ( " filename" , filename) );
176+ info.This ().As <Napi::Object>(). DefineProperty (Napi::PropertyDescriptor::Value ( " sourceName" , sourceName) );
177+ info.This ().As <Napi::Object>(). DefineProperty (Napi::PropertyDescriptor::Value ( " destName" , destName) );
178+ info.This ().As <Napi::Object>(). DefineProperty (Napi::PropertyDescriptor::Value ( " filenameIsDest" , filenameIsDest) );
179179
180- Backup* backup = new Backup (db);
181- backup->Wrap (info.This ());
180+ init (db);
182181
183- InitializeBaton* baton = new InitializeBaton (db, info[5 ].As <Napi::Function>(), backup );
184- baton->filename = std::string ( filename-> As <Napi::String>() .Utf8Value (). c_str () );
185- baton->sourceName = std::string ( sourceName-> As <Napi::String>() .Utf8Value (). c_str () );
186- baton->destName = std::string ( destName-> As <Napi::String>() .Utf8Value (). c_str () );
187- baton->filenameIsDest = filenameIsDest.As <Napi::Boolean>(). Value ();
182+ InitializeBaton* baton = new InitializeBaton (db, info[5 ].As <Napi::Function>(), this );
183+ baton->filename = filename.Utf8Value ();
184+ baton->sourceName = sourceName.Utf8Value ();
185+ baton->destName = destName.Utf8Value ();
186+ baton->filenameIsDest = filenameIsDest.Value ();
188187 db->Schedule (Work_BeginInitialize, baton);
189-
190- return info.This ();
191188}
192189
193190void Backup::Work_BeginInitialize (Database::Baton* baton) {
@@ -228,27 +225,29 @@ void Backup::Work_Initialize(uv_work_t* req) {
228225}
229226
230227void Backup::Work_AfterInitialize (uv_work_t * req) {
231- Napi::HandleScope scope (env);
232-
233228 BACKUP_INIT (InitializeBaton);
234229
230+ Napi::Env env = backup->Env ();
231+ Napi::HandleScope scope (env);
232+
235233 if (backup->status != SQLITE_OK ) {
236234 Error (baton);
237235 backup->FinishAll ();
238236 }
239237 else {
240238 backup->inited = true ;
241- Napi::Function cb = Napi::New (env, baton->callback );
242- if (!cb.IsEmpty () && cb-> IsFunction ()) {
239+ Napi::Function cb = baton->callback . Value ( );
240+ if (!cb.IsEmpty () && cb. IsFunction ()) {
243241 Napi::Value argv[] = { env.Null () };
244- TRY_CATCH_CALL (backup->handle (), cb, 1 , argv);
242+ TRY_CATCH_CALL (backup->Value (), cb, 1 , argv);
245243 }
246244 }
247245 BACKUP_END ();
248246}
249247
250248Napi::Value Backup::Step (const Napi::CallbackInfo& info) {
251249 Backup* backup = this ;
250+ Napi::Env env = backup->Env ();
252251
253252 REQUIRE_ARGUMENT_INTEGER (0 , pages);
254253 OPTIONAL_ARGUMENT_FUNCTION (1 , callback);
@@ -288,10 +287,11 @@ void Backup::Work_Step(uv_work_t* req) {
288287}
289288
290289void Backup::Work_AfterStep (uv_work_t * req) {
291- Napi::HandleScope scope (env);
292-
293290 BACKUP_INIT (StepBaton);
294291
292+ Napi::Env env = backup->Env ();
293+ Napi::HandleScope scope (env);
294+
295295 if (backup->status == SQLITE_DONE ) {
296296 backup->completed = true ;
297297 } else if (!backup->_handle ) {
@@ -303,10 +303,10 @@ void Backup::Work_AfterStep(uv_work_t* req) {
303303 }
304304 else {
305305 // Fire callbacks.
306- Napi::Function cb = Napi::New (env, baton->callback );
307- if (!cb.IsEmpty () && cb-> IsFunction ()) {
308- Napi::Value argv[] = { env.Null (), Napi::New (env, backup->status == SQLITE_DONE ) };
309- TRY_CATCH_CALL (backup->handle (), cb, 2 , argv);
306+ Napi::Function cb = baton->callback . Value ( );
307+ if (!cb.IsEmpty () && cb. IsFunction ()) {
308+ Napi::Value argv[] = { env.Null (), Napi::Boolean:: New (env, backup->status == SQLITE_DONE ) };
309+ TRY_CATCH_CALL (backup->Value (), cb, 2 , argv);
310310 }
311311 }
312312
@@ -315,6 +315,7 @@ void Backup::Work_AfterStep(uv_work_t* req) {
315315
316316Napi::Value Backup::Finish (const Napi::CallbackInfo& info) {
317317 Backup* backup = this ;
318+ Napi::Env env = backup->Env ();
318319
319320 OPTIONAL_ARGUMENT_FUNCTION (0 , callback);
320321
@@ -333,15 +334,17 @@ void Backup::Work_Finish(uv_work_t* req) {
333334}
334335
335336void Backup::Work_AfterFinish (uv_work_t * req) {
337+ BACKUP_INIT (Baton);
338+
339+ Napi::Env env = backup->Env ();
336340 Napi::HandleScope scope (env);
337341
338- BACKUP_INIT (Baton);
339342 backup->FinishAll ();
340343
341344 // Fire callback in case there was one.
342- Napi::Function cb = Napi::New (env, baton->callback );
343- if (!cb.IsEmpty () && cb-> IsFunction ()) {
344- TRY_CATCH_CALL (backup->handle (), cb, 0 , NULL );
345+ Napi::Function cb = baton->callback . Value ( );
346+ if (!cb.IsEmpty () && cb. IsFunction ()) {
347+ TRY_CATCH_CALL (backup->Value (), cb, 0 , NULL );
345348 }
346349
347350 BACKUP_END ();
@@ -373,48 +376,49 @@ void Backup::FinishSqlite() {
373376Napi::Value Backup::IdleGetter (const Napi::CallbackInfo& info) {
374377 Backup* backup = this ;
375378 bool idle = backup->inited && !backup->locked && backup->queue .empty ();
376- return idle;
379+ return Napi::Boolean::New ( this -> Env (), idle) ;
377380}
378381
379382Napi::Value Backup::CompletedGetter (const Napi::CallbackInfo& info) {
380383 Backup* backup = this ;
381- return backup->completed ;
384+ return Napi::Boolean::New ( this -> Env (), backup->completed ) ;
382385}
383386
384387Napi::Value Backup::FailedGetter (const Napi::CallbackInfo& info) {
385388 Backup* backup = this ;
386- return backup->failed ;
389+ return Napi::Boolean::New ( this -> Env (), backup->failed ) ;
387390}
388391
389392Napi::Value Backup::RemainingGetter (const Napi::CallbackInfo& info) {
390393 Backup* backup = this ;
391- return backup->remaining ;
394+ return Napi::Number::New ( this -> Env (), backup->remaining ) ;
392395}
393396
394397Napi::Value Backup::PageCountGetter (const Napi::CallbackInfo& info) {
395398 Backup* backup = this ;
396- return backup->pageCount ;
399+ return Napi::Number::New ( this -> Env (), backup->pageCount ) ;
397400}
398401
399402Napi::Value Backup::RetryErrorGetter (const Napi::CallbackInfo& info) {
400403 Backup* backup = this ;
401- return Napi::New (env, backup->retryErrors );
404+ return backup->retryErrors . Value ( );
402405}
403406
404407void Backup::RetryErrorSetter (const Napi::CallbackInfo& info, const Napi::Value& value) {
405408 Backup* backup = this ;
406- if (!value->IsArray ()) {
409+ Napi::Env env = backup->Env ();
410+ if (!value.IsArray ()) {
407411 Napi::Error::New (env, " retryErrors must be an array" ).ThrowAsJavaScriptException ();
408- return env. Null () ;
412+ return ;
409413 }
410414 Napi::Array array = value.As <Napi::Array>();
411415 backup->retryErrors .Reset (array);
412416}
413417
414418void Backup::GetRetryErrors (std::set<int >& retryErrorsSet) {
415419 retryErrorsSet.clear ();
416- Napi::Array array = Napi::New (env, retryErrors);
417- int length = array-> Length ();
420+ Napi::Array array = retryErrors. Value ( );
421+ int length = array. Length ();
418422 for (int i = 0 ; i < length; i++) {
419423 Napi::Value code = (array).Get (i);
420424 if (code.IsNumber ()) {
0 commit comments