@@ -427,7 +427,7 @@ static inline napi_status CreateJSValueHandle(napi_env env, JSValue value, struc
427427 CHECK_ARG (env )
428428 CHECK_ARG (result )
429429
430- RETURN_STATUS_IF_FALSE (!LIST_EMPTY (& env - > handleScopeList ), napi_handle_scope_empty )
430+ RETURN_STATUS_IF_FALSE (!LIST_EMPTY (& env - > handleScopeList ), napi_handle_scope_mismatch )
431431
432432 napi_handle_scope handleScope = LIST_FIRST (& env -> handleScopeList );
433433
@@ -483,7 +483,7 @@ napi_status napi_open_handle_scope(napi_env env, napi_handle_scope *result) {
483483 napi_handle_scope__ * handleScope = (napi_handle_scope__ * ) mi_malloc (
484484 sizeof (napi_handle_scope__ ));
485485
486- RETURN_STATUS_IF_FALSE (handleScope , napi_memory_error )
486+ RETURN_STATUS_IF_FALSE (handleScope , napi_handle_scope_mismatch )
487487 handleScope -> type = HANDLE_HEAP_ALLOCATED ;
488488 handleScope -> handleCount = 0 ;
489489 handleScope -> escapeCalled = false;
@@ -529,12 +529,12 @@ napi_status napi_open_escapable_handle_scope(napi_env env, napi_escapable_handle
529529 sizeof (napi_handle_scope__ ));
530530 handleScope -> type = HANDLE_HEAP_ALLOCATED ;
531531 handleScope -> handleCount = 0 ;
532- RETURN_STATUS_IF_FALSE (handleScope , napi_memory_error )
532+ RETURN_STATUS_IF_FALSE (handleScope , napi_handle_scope_mismatch )
533533 SLIST_INIT (& handleScope -> handleList );
534534 handleScope -> escapeCalled = false;
535535 LIST_INSERT_HEAD (& env -> handleScopeList , handleScope , node );
536536
537- * result = handleScope ;
537+ * result = ( napi_escapable_handle_scope ) handleScope ;
538538
539539 return napi_clear_last_error (env );
540540}
@@ -544,23 +544,27 @@ napi_close_escapable_handle_scope(napi_env env, napi_escapable_handle_scope esca
544544 CHECK_ARG (env )
545545 CHECK_ARG (escapableScope )
546546
547- assert (LIST_FIRST (& env -> handleScopeList ) == escapableScope &&
547+ // Escapable scopes share the napi_handle_scope__ representation; the public
548+ // type is opaque and distinct, so cast to operate on the members.
549+ napi_handle_scope__ * scope = (napi_handle_scope__ * ) escapableScope ;
550+
551+ assert (LIST_FIRST (& env -> handleScopeList ) == scope &&
548552 "napi_close_handle_scope() or napi_close_escapable_handle_scope() should follow FILO rule." );
549553
550554 Handle * handle , * tempHandle ;
551- SLIST_FOREACH_SAFE (handle , & escapableScope -> handleList , node , tempHandle ) {
555+ SLIST_FOREACH_SAFE (handle , & scope -> handleList , node , tempHandle ) {
552556 JS_FreeValue (env -> context , handle -> value );
553557 handle -> value = JSUndefined ;
554558
555559 // Instead of freeing, return the handle to the pool for reuse
556- SLIST_REMOVE (& escapableScope -> handleList , handle , Handle , node );
560+ SLIST_REMOVE (& scope -> handleList , handle , Handle , node );
557561 if (handle -> type == HANDLE_HEAP_ALLOCATED ) {
558562 mi_free (handle );
559563 }
560564 }
561565
562- LIST_REMOVE (escapableScope , node );
563- mi_free (escapableScope );
566+ LIST_REMOVE (scope , node );
567+ mi_free (scope );
564568
565569 return napi_clear_last_error (env );
566570}
@@ -573,16 +577,20 @@ napi_status napi_escape_handle(napi_env env, napi_escapable_handle_scope scope,
573577 CHECK_ARG (scope )
574578 CHECK_ARG (escapee )
575579
576- RETURN_STATUS_IF_FALSE (!scope - > escapeCalled , napi_escape_called_twice )
580+ // Escapable scopes share the napi_handle_scope__ representation; the public
581+ // type is opaque and distinct, so cast to operate on the members.
582+ napi_handle_scope__ * hs = (napi_handle_scope__ * ) scope ;
583+
584+ RETURN_STATUS_IF_FALSE (!hs -> escapeCalled , napi_escape_called_twice )
577585 // Get the outer handle scope
578- napi_handle_scope handleScope = LIST_NEXT (scope , node );
579- RETURN_STATUS_IF_FALSE (handleScope , napi_handle_scope_empty )
586+ napi_handle_scope handleScope = LIST_NEXT (hs , node );
587+ RETURN_STATUS_IF_FALSE (handleScope , napi_handle_scope_mismatch )
580588
581589 Handle * handle = (Handle * ) mi_malloc (sizeof (Handle ));
582590
583- RETURN_STATUS_IF_FALSE (handle , napi_memory_error )
591+ RETURN_STATUS_IF_FALSE (handle , napi_handle_scope_mismatch )
584592
585- scope -> escapeCalled = true;
593+ hs -> escapeCalled = true;
586594 handle -> value = JS_DupValue (env -> context , * ((JSValue * ) escapee ));
587595 SLIST_INSERT_HEAD (& handleScope -> handleList , handle , node );
588596
@@ -795,7 +803,7 @@ napi_create_reference(napi_env env, napi_value value, uint32_t initialRefCount,
795803 CHECK_ARG (result )
796804
797805 * result = (napi_ref__ * ) mi_malloc (sizeof (napi_ref__ ));
798- RETURN_STATUS_IF_FALSE (* result , napi_memory_error )
806+ RETURN_STATUS_IF_FALSE (* result , napi_generic_failure )
799807
800808 JSValue jsValue = * ((JSValue * ) value );
801809
@@ -3223,7 +3231,7 @@ napi_create_function(napi_env env, const char *utf8name, size_t length, napi_cal
32233231 CHECK_ARG (result )
32243232
32253233 FunctionInfo * functionInfo = (FunctionInfo * ) mi_malloc (sizeof (FunctionInfo ));
3226- RETURN_STATUS_IF_FALSE (functionInfo , napi_memory_error )
3234+ RETURN_STATUS_IF_FALSE (functionInfo , napi_generic_failure )
32273235 functionInfo -> data = data ;
32283236 functionInfo -> callback = cb ;
32293237
0 commit comments