| 
6 | 6 | static void Finalize(napi_env env, void* data, void* hint) {  | 
7 | 7 |   napi_value global, set_timeout;  | 
8 | 8 |   napi_ref* ref = data;  | 
 | 9 | + | 
 | 10 | +  NODE_API_NOGC_ASSERT_RETURN_VOID(  | 
 | 11 | +      napi_delete_reference(env, *ref) == napi_ok,  | 
 | 12 | +      "deleting reference in finalizer should succeed");  | 
 | 13 | +  NODE_API_NOGC_ASSERT_RETURN_VOID(  | 
 | 14 | +      napi_get_global(env, &global) == napi_ok,  | 
 | 15 | +      "getting global reference in finalizer should succeed");  | 
 | 16 | +  napi_status result =  | 
 | 17 | +      napi_get_named_property(env, global, "setTimeout", &set_timeout);  | 
 | 18 | + | 
 | 19 | +  // The finalizer could be invoked either from check callbacks (as native  | 
 | 20 | +  // immediates) if the event loop is still running (where napi_ok is returned)  | 
 | 21 | +  // or during environment shutdown (where napi_cannot_run_js or  | 
 | 22 | +  // napi_pending_exception is returned). This is not deterministic from  | 
 | 23 | +  // the point of view of the addon.  | 
 | 24 | +  NODE_API_NOGC_ASSERT_RETURN_VOID(  | 
9 | 25 | #ifdef NAPI_EXPERIMENTAL  | 
10 |  | -  napi_status expected_status = napi_cannot_run_js;  | 
 | 26 | +      result == napi_cannot_run_js || result == napi_ok,  | 
 | 27 | +      "getting named property from global in finalizer should succeed "  | 
 | 28 | +      "or return napi_cannot_run_js");  | 
11 | 29 | #else  | 
12 |  | -  napi_status expected_status = napi_pending_exception;  | 
 | 30 | +      result == napi_pending_exception || result == napi_ok,  | 
 | 31 | +      "getting named property from global in finalizer should succeed "  | 
 | 32 | +      "or return napi_pending_exception");  | 
13 | 33 | #endif  // NAPI_EXPERIMENTAL  | 
14 |  | - | 
15 |  | -  if (napi_delete_reference(env, *ref) != napi_ok) abort();  | 
16 |  | -  if (napi_get_global(env, &global) != napi_ok) abort();  | 
17 |  | -  if (napi_get_named_property(env, global, "setTimeout", &set_timeout) !=  | 
18 |  | -      expected_status)  | 
19 |  | -    abort();  | 
20 | 34 |   free(ref);  | 
21 | 35 | }  | 
22 | 36 | 
 
  | 
 | 
0 commit comments