Skip to content

Commit

Permalink
[unity]update p-api
Browse files Browse the repository at this point in the history
  • Loading branch information
chexiongsheng committed Oct 17, 2024
1 parent 9096240 commit 250fc41
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 43 deletions.
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, struct 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
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,12 @@ pesapi_scope pesapi_open_scope (pesapi_env_ref env_ref) {
return pesapi_open_scope_ptr(env_ref);
}

typedef pesapi_scope (*pesapi_open_scope_placementType)(pesapi_env_ref env_ref, struct pesapi_scope_memory* memory);
static pesapi_open_scope_placementType pesapi_open_scope_placement_ptr;
pesapi_scope pesapi_open_scope_placement (pesapi_env_ref env_ref, struct pesapi_scope_memory* memory) {
return pesapi_open_scope_placement_ptr(env_ref, memory);
}

typedef bool (*pesapi_has_caughtType)(pesapi_scope scope);
static pesapi_has_caughtType pesapi_has_caught_ptr;
bool pesapi_has_caught (pesapi_scope scope) {
Expand All @@ -388,6 +394,12 @@ void pesapi_close_scope (pesapi_scope scope) {
pesapi_close_scope_ptr(scope);
}

typedef void (*pesapi_close_scope_placementType)(pesapi_scope scope);
static pesapi_close_scope_placementType pesapi_close_scope_placement_ptr;
void pesapi_close_scope_placement (pesapi_scope scope) {
pesapi_close_scope_placement_ptr(scope);
}

typedef pesapi_value_ref (*pesapi_create_value_refType)(pesapi_env env, pesapi_value value, uint32_t internal_field_count);
static pesapi_create_value_refType pesapi_create_value_ref_ptr;
pesapi_value_ref pesapi_create_value_ref (pesapi_env env, pesapi_value value, uint32_t internal_field_count) {
Expand Down Expand Up @@ -632,39 +644,41 @@ void pesapi_init(pesapi_func_ptr* func_array){
pesapi_duplicate_env_ref_ptr = (pesapi_duplicate_env_refType)func_array[56];
pesapi_release_env_ref_ptr = (pesapi_release_env_refType)func_array[57];
pesapi_open_scope_ptr = (pesapi_open_scopeType)func_array[58];
pesapi_has_caught_ptr = (pesapi_has_caughtType)func_array[59];
pesapi_get_exception_as_string_ptr = (pesapi_get_exception_as_stringType)func_array[60];
pesapi_close_scope_ptr = (pesapi_close_scopeType)func_array[61];
pesapi_create_value_ref_ptr = (pesapi_create_value_refType)func_array[62];
pesapi_duplicate_value_ref_ptr = (pesapi_duplicate_value_refType)func_array[63];
pesapi_release_value_ref_ptr = (pesapi_release_value_refType)func_array[64];
pesapi_get_value_from_ref_ptr = (pesapi_get_value_from_refType)func_array[65];
pesapi_set_ref_weak_ptr = (pesapi_set_ref_weakType)func_array[66];
pesapi_set_owner_ptr = (pesapi_set_ownerType)func_array[67];
pesapi_get_ref_associated_env_ptr = (pesapi_get_ref_associated_envType)func_array[68];
pesapi_get_ref_internal_fields_ptr = (pesapi_get_ref_internal_fieldsType)func_array[69];
pesapi_get_property_ptr = (pesapi_get_propertyType)func_array[70];
pesapi_set_property_ptr = (pesapi_set_propertyType)func_array[71];
pesapi_get_private_ptr = (pesapi_get_privateType)func_array[72];
pesapi_set_private_ptr = (pesapi_set_privateType)func_array[73];
pesapi_get_property_uint32_ptr = (pesapi_get_property_uint32Type)func_array[74];
pesapi_set_property_uint32_ptr = (pesapi_set_property_uint32Type)func_array[75];
pesapi_call_function_ptr = (pesapi_call_functionType)func_array[76];
pesapi_eval_ptr = (pesapi_evalType)func_array[77];
pesapi_global_ptr = (pesapi_globalType)func_array[78];
pesapi_get_env_private_ptr = (pesapi_get_env_privateType)func_array[79];
pesapi_set_env_private_ptr = (pesapi_set_env_privateType)func_array[80];
pesapi_alloc_type_infos_ptr = (pesapi_alloc_type_infosType)func_array[81];
pesapi_set_type_info_ptr = (pesapi_set_type_infoType)func_array[82];
pesapi_create_signature_info_ptr = (pesapi_create_signature_infoType)func_array[83];
pesapi_alloc_property_descriptors_ptr = (pesapi_alloc_property_descriptorsType)func_array[84];
pesapi_set_method_info_ptr = (pesapi_set_method_infoType)func_array[85];
pesapi_set_property_info_ptr = (pesapi_set_property_infoType)func_array[86];
pesapi_define_class_ptr = (pesapi_define_classType)func_array[87];
pesapi_get_class_data_ptr = (pesapi_get_class_dataType)func_array[88];
pesapi_on_class_not_found_ptr = (pesapi_on_class_not_foundType)func_array[89];
pesapi_class_type_info_ptr = (pesapi_class_type_infoType)func_array[90];
pesapi_find_type_id_ptr = (pesapi_find_type_idType)func_array[91];
pesapi_open_scope_placement_ptr = (pesapi_open_scope_placementType)func_array[59];
pesapi_has_caught_ptr = (pesapi_has_caughtType)func_array[60];
pesapi_get_exception_as_string_ptr = (pesapi_get_exception_as_stringType)func_array[61];
pesapi_close_scope_ptr = (pesapi_close_scopeType)func_array[62];
pesapi_close_scope_placement_ptr = (pesapi_close_scope_placementType)func_array[63];
pesapi_create_value_ref_ptr = (pesapi_create_value_refType)func_array[64];
pesapi_duplicate_value_ref_ptr = (pesapi_duplicate_value_refType)func_array[65];
pesapi_release_value_ref_ptr = (pesapi_release_value_refType)func_array[66];
pesapi_get_value_from_ref_ptr = (pesapi_get_value_from_refType)func_array[67];
pesapi_set_ref_weak_ptr = (pesapi_set_ref_weakType)func_array[68];
pesapi_set_owner_ptr = (pesapi_set_ownerType)func_array[69];
pesapi_get_ref_associated_env_ptr = (pesapi_get_ref_associated_envType)func_array[70];
pesapi_get_ref_internal_fields_ptr = (pesapi_get_ref_internal_fieldsType)func_array[71];
pesapi_get_property_ptr = (pesapi_get_propertyType)func_array[72];
pesapi_set_property_ptr = (pesapi_set_propertyType)func_array[73];
pesapi_get_private_ptr = (pesapi_get_privateType)func_array[74];
pesapi_set_private_ptr = (pesapi_set_privateType)func_array[75];
pesapi_get_property_uint32_ptr = (pesapi_get_property_uint32Type)func_array[76];
pesapi_set_property_uint32_ptr = (pesapi_set_property_uint32Type)func_array[77];
pesapi_call_function_ptr = (pesapi_call_functionType)func_array[78];
pesapi_eval_ptr = (pesapi_evalType)func_array[79];
pesapi_global_ptr = (pesapi_globalType)func_array[80];
pesapi_get_env_private_ptr = (pesapi_get_env_privateType)func_array[81];
pesapi_set_env_private_ptr = (pesapi_set_env_privateType)func_array[82];
pesapi_alloc_type_infos_ptr = (pesapi_alloc_type_infosType)func_array[83];
pesapi_set_type_info_ptr = (pesapi_set_type_infoType)func_array[84];
pesapi_create_signature_info_ptr = (pesapi_create_signature_infoType)func_array[85];
pesapi_alloc_property_descriptors_ptr = (pesapi_alloc_property_descriptorsType)func_array[86];
pesapi_set_method_info_ptr = (pesapi_set_method_infoType)func_array[87];
pesapi_set_property_info_ptr = (pesapi_set_property_infoType)func_array[88];
pesapi_define_class_ptr = (pesapi_define_classType)func_array[89];
pesapi_get_class_data_ptr = (pesapi_get_class_dataType)func_array[90];
pesapi_on_class_not_found_ptr = (pesapi_on_class_not_foundType)func_array[91];
pesapi_class_type_info_ptr = (pesapi_class_type_infoType)func_array[92];
pesapi_find_type_id_ptr = (pesapi_find_type_idType)func_array[93];

#endif
}
Expand Down
8 changes: 8 additions & 0 deletions unity/native_src_il2cpp/Inc/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, struct 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
20 changes: 10 additions & 10 deletions unity/native_src_il2cpp/Src/PesapiAddonLoad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,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 unity/native_src_il2cpp/Src/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, struct 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

0 comments on commit 250fc41

Please sign in to comment.