node-addon-examples icon indicating copy to clipboard operation
node-addon-examples copied to clipboard

[Question] Why are the return values ​​of the two functions different?

Open sdg9670 opened this issue 1 year ago • 1 comments

https://github.com/nodejs/node-addon-examples/blob/ed36ba2d8512d73c7171b9537c96a63bc34e9831/src/1-getting-started/7_factory_wrap/node-addon-api/myobject.cc#L24-L28

https://github.com/nodejs/node-addon-examples/blob/ed36ba2d8512d73c7171b9537c96a63bc34e9831/src/1-getting-started/6_object_wrap/node-addon-api/myobject.cc#L46-L58

I'm Newby, who just got into NAPI. In the above two codes, methods are create and return the object. I wonder why only the return value in NewInstance uses escape scope.

sdg9670 avatar Mar 21 '24 13:03 sdg9670

The different is that in the first case the creation of the object was done in the context of a scope

Napi::EscapableHandleScope scope(env);

You can read more about scopes in -> https://github.com/nodejs/node-addon-api/blob/main/doc/handle_scope.md#handlescope

mhdawson avatar Apr 04 '24 21:04 mhdawson