diff --git a/unreal/Puerts/Source/JsEnv/Private/PesapiAddonLoad.cpp b/unreal/Puerts/Source/JsEnv/Private/PesapiAddonLoad.cpp index 447b163df2..c89d449efa 100644 --- a/unreal/Puerts/Source/JsEnv/Private/PesapiAddonLoad.cpp +++ b/unreal/Puerts/Source/JsEnv/Private/PesapiAddonLoad.cpp @@ -48,16 +48,16 @@ static pesapi_func_ptr funcs[] = {(pesapi_func_ptr) &pesapi_create_null, (pesapi (pesapi_func_ptr) &pesapi_get_class_data_in_constructor, (pesapi_func_ptr) &pesapi_add_return, (pesapi_func_ptr) &pesapi_throw_by_string, (pesapi_func_ptr) &pesapi_create_env_ref, (pesapi_func_ptr) &pesapi_env_ref_is_valid, (pesapi_func_ptr) &pesapi_get_env_from_ref, (pesapi_func_ptr) &pesapi_duplicate_env_ref, - (pesapi_func_ptr) &pesapi_release_env_ref, (pesapi_func_ptr) &pesapi_open_scope, (pesapi_func_ptr) &pesapi_has_caught, - (pesapi_func_ptr) &pesapi_get_exception_as_string, (pesapi_func_ptr) &pesapi_close_scope, - (pesapi_func_ptr) &pesapi_create_value_ref, (pesapi_func_ptr) &pesapi_duplicate_value_ref, - (pesapi_func_ptr) &pesapi_release_value_ref, (pesapi_func_ptr) &pesapi_get_value_from_ref, - (pesapi_func_ptr) &pesapi_set_ref_weak, (pesapi_func_ptr) &pesapi_set_owner, (pesapi_func_ptr) &pesapi_get_ref_associated_env, - (pesapi_func_ptr) &pesapi_get_ref_internal_fields, (pesapi_func_ptr) &pesapi_get_property, - (pesapi_func_ptr) &pesapi_set_property, (pesapi_func_ptr) &pesapi_get_private, (pesapi_func_ptr) &pesapi_set_private, - (pesapi_func_ptr) &pesapi_get_property_uint32, (pesapi_func_ptr) &pesapi_set_property_uint32, - (pesapi_func_ptr) &pesapi_call_function, (pesapi_func_ptr) &pesapi_eval, (pesapi_func_ptr) &pesapi_global, - (pesapi_func_ptr) &pesapi_get_env_private, (pesapi_func_ptr) &pesapi_set_env_private, + (pesapi_func_ptr) &pesapi_release_env_ref, (pesapi_func_ptr) &pesapi_open_scope, (pesapi_func_ptr) &pesapi_open_scope_placement, + (pesapi_func_ptr) &pesapi_has_caught, (pesapi_func_ptr) &pesapi_get_exception_as_string, (pesapi_func_ptr) &pesapi_close_scope, + (pesapi_func_ptr) &pesapi_close_scope_placement, (pesapi_func_ptr) &pesapi_create_value_ref, + (pesapi_func_ptr) &pesapi_duplicate_value_ref, (pesapi_func_ptr) &pesapi_release_value_ref, + (pesapi_func_ptr) &pesapi_get_value_from_ref, (pesapi_func_ptr) &pesapi_set_ref_weak, (pesapi_func_ptr) &pesapi_set_owner, + (pesapi_func_ptr) &pesapi_get_ref_associated_env, (pesapi_func_ptr) &pesapi_get_ref_internal_fields, + (pesapi_func_ptr) &pesapi_get_property, (pesapi_func_ptr) &pesapi_set_property, (pesapi_func_ptr) &pesapi_get_private, + (pesapi_func_ptr) &pesapi_set_private, (pesapi_func_ptr) &pesapi_get_property_uint32, + (pesapi_func_ptr) &pesapi_set_property_uint32, (pesapi_func_ptr) &pesapi_call_function, (pesapi_func_ptr) &pesapi_eval, + (pesapi_func_ptr) &pesapi_global, (pesapi_func_ptr) &pesapi_get_env_private, (pesapi_func_ptr) &pesapi_set_env_private, (pesapi_func_ptr) &pesapi_alloc_type_infos, (pesapi_func_ptr) &pesapi_set_type_info, (pesapi_func_ptr) &pesapi_create_signature_info, (pesapi_func_ptr) &pesapi_alloc_property_descriptors, (pesapi_func_ptr) &pesapi_set_method_info, (pesapi_func_ptr) &pesapi_set_property_info, (pesapi_func_ptr) &pesapi_define_class, diff --git a/unreal/Puerts/Source/JsEnv/Private/PesapiV8Impl.cpp b/unreal/Puerts/Source/JsEnv/Private/PesapiV8Impl.cpp index 1711a84057..2b66ecef8f 100644 --- a/unreal/Puerts/Source/JsEnv/Private/PesapiV8Impl.cpp +++ b/unreal/Puerts/Source/JsEnv/Private/PesapiV8Impl.cpp @@ -64,6 +64,8 @@ struct pesapi_scope__ std::string errinfo; }; +static_assert(sizeof(pesapi_scope_memory) >= sizeof(pesapi_scope__), "sizeof(pesapi_scope__) > sizeof(pesapi_scope_memory__)"); + namespace v8impl { static_assert(sizeof(v8::Local) == sizeof(pesapi_value), "Cannot convert between v8::Local and pesapi_value"); @@ -552,6 +554,18 @@ pesapi_scope pesapi_open_scope(pesapi_env_ref env_ref) return scope; } +pesapi_scope pesapi_open_scope_placement(pesapi_env_ref env_ref, pesapi_scope_memory* memory) +{ + if (env_ref->env_life_cycle_tracker.expired()) + { + return nullptr; + } + env_ref->isolate->Enter(); + auto scope = new (memory) pesapi_scope__(env_ref->isolate); + env_ref->context_persistent.Get(env_ref->isolate)->Enter(); + return scope; +} + bool pesapi_has_caught(pesapi_scope scope) { return scope && scope->trycatch.HasCaught(); @@ -598,6 +612,16 @@ void pesapi_close_scope(pesapi_scope scope) isolate->Exit(); } +void pesapi_close_scope_placement(pesapi_scope scope) +{ + if (!scope) + return; + auto isolate = scope->scope.GetIsolate(); + isolate->GetCurrentContext()->Exit(); + scope->~pesapi_scope__(); + isolate->Exit(); +} + pesapi_value_ref pesapi_create_value_ref(pesapi_env env, pesapi_value pvalue, uint32_t internal_field_count) { auto context = v8impl::V8LocalContextFromPesapiEnv(env); diff --git a/unreal/Puerts/Source/JsEnv/Public/pesapi.h b/unreal/Puerts/Source/JsEnv/Public/pesapi.h index 184c38d7d0..241278531d 100644 --- a/unreal/Puerts/Source/JsEnv/Public/pesapi.h +++ b/unreal/Puerts/Source/JsEnv/Public/pesapi.h @@ -108,6 +108,12 @@ EXTERN_C_START +// alloc on stack +struct pesapi_scope_memory +{ + int padding__[32]; +}; + typedef struct pesapi_env__* pesapi_env; typedef struct pesapi_env_ref__* pesapi_env_ref; typedef struct pesapi_value__* pesapi_value; @@ -197,9 +203,11 @@ PESAPI_EXTERN pesapi_env_ref pesapi_duplicate_env_ref(pesapi_env_ref env_ref); PESAPI_EXTERN void pesapi_release_env_ref(pesapi_env_ref env_ref); PESAPI_EXTERN pesapi_scope pesapi_open_scope(pesapi_env_ref env_ref); +PESAPI_EXTERN pesapi_scope pesapi_open_scope_placement(pesapi_env_ref env_ref, pesapi_scope_memory* memory); PESAPI_EXTERN bool pesapi_has_caught(pesapi_scope scope); PESAPI_EXTERN const char* pesapi_get_exception_as_string(pesapi_scope scope, bool with_stack); PESAPI_EXTERN void pesapi_close_scope(pesapi_scope scope); +PESAPI_EXTERN void pesapi_close_scope_placement(pesapi_scope scope); PESAPI_EXTERN pesapi_value_ref pesapi_create_value_ref(pesapi_env env, pesapi_value value, uint32_t internal_field_count); PESAPI_EXTERN pesapi_value_ref pesapi_duplicate_value_ref(pesapi_value_ref value_ref);