Skip to content

Commit

Permalink
[p-api]添加placement版本的scope api
Browse files Browse the repository at this point in the history
  • Loading branch information
chexiongsheng committed Oct 17, 2024
1 parent 0be2f75 commit 65f25d0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
20 changes: 10 additions & 10 deletions unreal/Puerts/Source/JsEnv/Private/PesapiAddonLoad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
24 changes: 24 additions & 0 deletions unreal/Puerts/Source/JsEnv/Private/PesapiV8Impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<v8::Value>) == sizeof(pesapi_value), "Cannot convert between v8::Local<v8::Value> and pesapi_value");
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions unreal/Puerts/Source/JsEnv/Public/pesapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 65f25d0

Please sign in to comment.