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 15, 2024
1 parent d443a37 commit 38f4b1d
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ PESAPI_EXTERN pesapi_value pesapi_create_binary(pesapi_env env, void* str, size_
PESAPI_EXTERN pesapi_value pesapi_create_array(pesapi_env env);
PESAPI_EXTERN pesapi_value pesapi_create_object(pesapi_env env);
PESAPI_EXTERN pesapi_value pesapi_create_function(pesapi_env env, pesapi_callback native_impl, void* data);
PESAPI_EXTERN pesapi_value pesapi_create_class(pesapi_env env, const void* type_id);

PESAPI_EXTERN bool pesapi_get_value_bool(pesapi_env env, pesapi_value value);
PESAPI_EXTERN int32_t pesapi_get_value_int32(pesapi_env env, pesapi_value value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ pesapi_value pesapi_create_function (pesapi_env env, pesapi_callback native_impl
return pesapi_create_function_ptr(env, native_impl, data);
}

typedef pesapi_value (*pesapi_create_classType)(pesapi_env env, const void* type_id);
static pesapi_create_classType pesapi_create_class_ptr;
pesapi_value pesapi_create_class (pesapi_env env, const void* type_id) {
return pesapi_create_class_ptr(env, type_id);
}

typedef bool (*pesapi_get_value_boolType)(pesapi_env env, pesapi_value value);
static pesapi_get_value_boolType pesapi_get_value_bool_ptr;
bool pesapi_get_value_bool (pesapi_env env, pesapi_value value) {
Expand Down Expand Up @@ -580,84 +586,85 @@ void pesapi_init(pesapi_func_ptr* func_array){
pesapi_create_array_ptr = (pesapi_create_arrayType)func_array[10];
pesapi_create_object_ptr = (pesapi_create_objectType)func_array[11];
pesapi_create_function_ptr = (pesapi_create_functionType)func_array[12];
pesapi_get_value_bool_ptr = (pesapi_get_value_boolType)func_array[13];
pesapi_get_value_int32_ptr = (pesapi_get_value_int32Type)func_array[14];
pesapi_get_value_uint32_ptr = (pesapi_get_value_uint32Type)func_array[15];
pesapi_get_value_int64_ptr = (pesapi_get_value_int64Type)func_array[16];
pesapi_get_value_uint64_ptr = (pesapi_get_value_uint64Type)func_array[17];
pesapi_get_value_double_ptr = (pesapi_get_value_doubleType)func_array[18];
pesapi_get_value_string_utf8_ptr = (pesapi_get_value_string_utf8Type)func_array[19];
pesapi_get_value_binary_ptr = (pesapi_get_value_binaryType)func_array[20];
pesapi_get_array_length_ptr = (pesapi_get_array_lengthType)func_array[21];
pesapi_is_null_ptr = (pesapi_is_nullType)func_array[22];
pesapi_is_undefined_ptr = (pesapi_is_undefinedType)func_array[23];
pesapi_is_boolean_ptr = (pesapi_is_booleanType)func_array[24];
pesapi_is_int32_ptr = (pesapi_is_int32Type)func_array[25];
pesapi_is_uint32_ptr = (pesapi_is_uint32Type)func_array[26];
pesapi_is_int64_ptr = (pesapi_is_int64Type)func_array[27];
pesapi_is_uint64_ptr = (pesapi_is_uint64Type)func_array[28];
pesapi_is_double_ptr = (pesapi_is_doubleType)func_array[29];
pesapi_is_string_ptr = (pesapi_is_stringType)func_array[30];
pesapi_is_object_ptr = (pesapi_is_objectType)func_array[31];
pesapi_is_function_ptr = (pesapi_is_functionType)func_array[32];
pesapi_is_binary_ptr = (pesapi_is_binaryType)func_array[33];
pesapi_is_array_ptr = (pesapi_is_arrayType)func_array[34];
pesapi_native_object_to_value_ptr = (pesapi_native_object_to_valueType)func_array[35];
pesapi_get_native_object_ptr_ptr = (pesapi_get_native_object_ptrType)func_array[36];
pesapi_get_native_object_typeid_ptr = (pesapi_get_native_object_typeidType)func_array[37];
pesapi_is_instance_of_ptr = (pesapi_is_instance_ofType)func_array[38];
pesapi_boxing_ptr = (pesapi_boxingType)func_array[39];
pesapi_unboxing_ptr = (pesapi_unboxingType)func_array[40];
pesapi_update_boxed_value_ptr = (pesapi_update_boxed_valueType)func_array[41];
pesapi_is_boxed_value_ptr = (pesapi_is_boxed_valueType)func_array[42];
pesapi_get_args_len_ptr = (pesapi_get_args_lenType)func_array[43];
pesapi_get_arg_ptr = (pesapi_get_argType)func_array[44];
pesapi_get_env_ptr = (pesapi_get_envType)func_array[45];
pesapi_get_this_ptr = (pesapi_get_thisType)func_array[46];
pesapi_get_holder_ptr = (pesapi_get_holderType)func_array[47];
pesapi_get_userdata_ptr = (pesapi_get_userdataType)func_array[48];
pesapi_get_class_data_in_constructor_ptr = (pesapi_get_class_data_in_constructorType)func_array[49];
pesapi_add_return_ptr = (pesapi_add_returnType)func_array[50];
pesapi_throw_by_string_ptr = (pesapi_throw_by_stringType)func_array[51];
pesapi_create_env_ref_ptr = (pesapi_create_env_refType)func_array[52];
pesapi_env_ref_is_valid_ptr = (pesapi_env_ref_is_validType)func_array[53];
pesapi_get_env_from_ref_ptr = (pesapi_get_env_from_refType)func_array[54];
pesapi_duplicate_env_ref_ptr = (pesapi_duplicate_env_refType)func_array[55];
pesapi_release_env_ref_ptr = (pesapi_release_env_refType)func_array[56];
pesapi_open_scope_ptr = (pesapi_open_scopeType)func_array[57];
pesapi_has_caught_ptr = (pesapi_has_caughtType)func_array[58];
pesapi_get_exception_as_string_ptr = (pesapi_get_exception_as_stringType)func_array[59];
pesapi_close_scope_ptr = (pesapi_close_scopeType)func_array[60];
pesapi_create_value_ref_ptr = (pesapi_create_value_refType)func_array[61];
pesapi_duplicate_value_ref_ptr = (pesapi_duplicate_value_refType)func_array[62];
pesapi_release_value_ref_ptr = (pesapi_release_value_refType)func_array[63];
pesapi_get_value_from_ref_ptr = (pesapi_get_value_from_refType)func_array[64];
pesapi_set_ref_weak_ptr = (pesapi_set_ref_weakType)func_array[65];
pesapi_set_owner_ptr = (pesapi_set_ownerType)func_array[66];
pesapi_get_ref_associated_env_ptr = (pesapi_get_ref_associated_envType)func_array[67];
pesapi_get_ref_internal_fields_ptr = (pesapi_get_ref_internal_fieldsType)func_array[68];
pesapi_get_property_ptr = (pesapi_get_propertyType)func_array[69];
pesapi_set_property_ptr = (pesapi_set_propertyType)func_array[70];
pesapi_get_private_ptr = (pesapi_get_privateType)func_array[71];
pesapi_set_private_ptr = (pesapi_set_privateType)func_array[72];
pesapi_get_property_uint32_ptr = (pesapi_get_property_uint32Type)func_array[73];
pesapi_set_property_uint32_ptr = (pesapi_set_property_uint32Type)func_array[74];
pesapi_call_function_ptr = (pesapi_call_functionType)func_array[75];
pesapi_eval_ptr = (pesapi_evalType)func_array[76];
pesapi_global_ptr = (pesapi_globalType)func_array[77];
pesapi_get_env_private_ptr = (pesapi_get_env_privateType)func_array[78];
pesapi_set_env_private_ptr = (pesapi_set_env_privateType)func_array[79];
pesapi_alloc_type_infos_ptr = (pesapi_alloc_type_infosType)func_array[80];
pesapi_set_type_info_ptr = (pesapi_set_type_infoType)func_array[81];
pesapi_create_signature_info_ptr = (pesapi_create_signature_infoType)func_array[82];
pesapi_alloc_property_descriptors_ptr = (pesapi_alloc_property_descriptorsType)func_array[83];
pesapi_set_method_info_ptr = (pesapi_set_method_infoType)func_array[84];
pesapi_set_property_info_ptr = (pesapi_set_property_infoType)func_array[85];
pesapi_define_class_ptr = (pesapi_define_classType)func_array[86];
pesapi_find_class_data_ptr = (pesapi_find_class_dataType)func_array[87];
pesapi_on_class_not_found_ptr = (pesapi_on_class_not_foundType)func_array[88];
pesapi_class_type_info_ptr = (pesapi_class_type_infoType)func_array[89];
pesapi_find_type_id_ptr = (pesapi_find_type_idType)func_array[90];
pesapi_create_class_ptr = (pesapi_create_classType)func_array[13];
pesapi_get_value_bool_ptr = (pesapi_get_value_boolType)func_array[14];
pesapi_get_value_int32_ptr = (pesapi_get_value_int32Type)func_array[15];
pesapi_get_value_uint32_ptr = (pesapi_get_value_uint32Type)func_array[16];
pesapi_get_value_int64_ptr = (pesapi_get_value_int64Type)func_array[17];
pesapi_get_value_uint64_ptr = (pesapi_get_value_uint64Type)func_array[18];
pesapi_get_value_double_ptr = (pesapi_get_value_doubleType)func_array[19];
pesapi_get_value_string_utf8_ptr = (pesapi_get_value_string_utf8Type)func_array[20];
pesapi_get_value_binary_ptr = (pesapi_get_value_binaryType)func_array[21];
pesapi_get_array_length_ptr = (pesapi_get_array_lengthType)func_array[22];
pesapi_is_null_ptr = (pesapi_is_nullType)func_array[23];
pesapi_is_undefined_ptr = (pesapi_is_undefinedType)func_array[24];
pesapi_is_boolean_ptr = (pesapi_is_booleanType)func_array[25];
pesapi_is_int32_ptr = (pesapi_is_int32Type)func_array[26];
pesapi_is_uint32_ptr = (pesapi_is_uint32Type)func_array[27];
pesapi_is_int64_ptr = (pesapi_is_int64Type)func_array[28];
pesapi_is_uint64_ptr = (pesapi_is_uint64Type)func_array[29];
pesapi_is_double_ptr = (pesapi_is_doubleType)func_array[30];
pesapi_is_string_ptr = (pesapi_is_stringType)func_array[31];
pesapi_is_object_ptr = (pesapi_is_objectType)func_array[32];
pesapi_is_function_ptr = (pesapi_is_functionType)func_array[33];
pesapi_is_binary_ptr = (pesapi_is_binaryType)func_array[34];
pesapi_is_array_ptr = (pesapi_is_arrayType)func_array[35];
pesapi_native_object_to_value_ptr = (pesapi_native_object_to_valueType)func_array[36];
pesapi_get_native_object_ptr_ptr = (pesapi_get_native_object_ptrType)func_array[37];
pesapi_get_native_object_typeid_ptr = (pesapi_get_native_object_typeidType)func_array[38];
pesapi_is_instance_of_ptr = (pesapi_is_instance_ofType)func_array[39];
pesapi_boxing_ptr = (pesapi_boxingType)func_array[40];
pesapi_unboxing_ptr = (pesapi_unboxingType)func_array[41];
pesapi_update_boxed_value_ptr = (pesapi_update_boxed_valueType)func_array[42];
pesapi_is_boxed_value_ptr = (pesapi_is_boxed_valueType)func_array[43];
pesapi_get_args_len_ptr = (pesapi_get_args_lenType)func_array[44];
pesapi_get_arg_ptr = (pesapi_get_argType)func_array[45];
pesapi_get_env_ptr = (pesapi_get_envType)func_array[46];
pesapi_get_this_ptr = (pesapi_get_thisType)func_array[47];
pesapi_get_holder_ptr = (pesapi_get_holderType)func_array[48];
pesapi_get_userdata_ptr = (pesapi_get_userdataType)func_array[49];
pesapi_get_class_data_in_constructor_ptr = (pesapi_get_class_data_in_constructorType)func_array[50];
pesapi_add_return_ptr = (pesapi_add_returnType)func_array[51];
pesapi_throw_by_string_ptr = (pesapi_throw_by_stringType)func_array[52];
pesapi_create_env_ref_ptr = (pesapi_create_env_refType)func_array[53];
pesapi_env_ref_is_valid_ptr = (pesapi_env_ref_is_validType)func_array[54];
pesapi_get_env_from_ref_ptr = (pesapi_get_env_from_refType)func_array[55];
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_find_class_data_ptr = (pesapi_find_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];

#endif
}
Expand Down
2 changes: 1 addition & 1 deletion unity/native_src_il2cpp/Inc/CppObjectMapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class FCppObjectMapper final : public ICppObjectMapper

v8::Local<v8::Function> LoadTypeByString(v8::Isolate* Isolate, v8::Local<v8::Context> Context, std::string TypeName);

v8::Local<v8::Function> LoadTypeById(v8::Isolate* Isolate, v8::Local<v8::Context> Context, const void* TypeId);
virtual v8::MaybeLocal<v8::Function> LoadTypeById(v8::Local<v8::Context> Context, const void* TypeId) override;

void LoadCppType(const v8::FunctionCallbackInfo<v8::Value>& Info);

Expand Down
2 changes: 2 additions & 0 deletions unity/native_src_il2cpp/Inc/ObjectMapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class ICppObjectMapper

virtual void SetPrivateData(v8::Local<v8::Context> Context, v8::Local<v8::Object> JSObject, void* Ptr) = 0;

virtual v8::MaybeLocal<v8::Function> LoadTypeById(v8::Local<v8::Context> Context, const void* TypeId) = 0;

virtual std::weak_ptr<int> GetJsEnvLifeCycleTracker() = 0;

virtual ~ICppObjectMapper()
Expand Down
1 change: 1 addition & 0 deletions unity/native_src_il2cpp/Inc/pesapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ PESAPI_EXTERN pesapi_value pesapi_create_binary(pesapi_env env, void* str, size_
PESAPI_EXTERN pesapi_value pesapi_create_array(pesapi_env env);
PESAPI_EXTERN pesapi_value pesapi_create_object(pesapi_env env);
PESAPI_EXTERN pesapi_value pesapi_create_function(pesapi_env env, pesapi_callback native_impl, void* data);
PESAPI_EXTERN pesapi_value pesapi_create_class(pesapi_env env, const void* type_id);

PESAPI_EXTERN bool pesapi_get_value_bool(pesapi_env env, pesapi_value value);
PESAPI_EXTERN int32_t pesapi_get_value_int32(pesapi_env env, pesapi_value value);
Expand Down
13 changes: 3 additions & 10 deletions unity/native_src_il2cpp/Src/CppObjectMapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,10 @@ v8::Local<v8::Function> FCppObjectMapper::LoadTypeByString(v8::Isolate* Isolate,
}


v8::Local<v8::Function> FCppObjectMapper::LoadTypeById(v8::Isolate* Isolate, v8::Local<v8::Context> Context, const void* TypeId)
v8::MaybeLocal<v8::Function> FCppObjectMapper::LoadTypeById(v8::Local<v8::Context> Context, const void* TypeId)
{
auto Template = GetTemplateOfClass(Isolate, TypeId);
if (!Template.IsEmpty())
{
return Template->GetFunction(Context).ToLocalChecked();
}
else
{
return v8::Local<v8::Function>();
}
auto Template = GetTemplateOfClass(Context->GetIsolate(), TypeId);
return Template->GetFunction(Context);
}

void FCppObjectMapper::LoadCppType(const v8::FunctionCallbackInfo<v8::Value>& Info)
Expand Down
4 changes: 2 additions & 2 deletions unity/native_src_il2cpp/Src/PesapiAddonLoad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ static pesapi_func_ptr funcs[] = {(pesapi_func_ptr) &pesapi_create_null, (pesapi
(pesapi_func_ptr) &pesapi_create_boolean, (pesapi_func_ptr) &pesapi_create_int32, (pesapi_func_ptr) &pesapi_create_uint32,
(pesapi_func_ptr) &pesapi_create_int64, (pesapi_func_ptr) &pesapi_create_uint64, (pesapi_func_ptr) &pesapi_create_double,
(pesapi_func_ptr) &pesapi_create_string_utf8, (pesapi_func_ptr) &pesapi_create_binary, (pesapi_func_ptr) &pesapi_create_array,
(pesapi_func_ptr) &pesapi_create_object, (pesapi_func_ptr) &pesapi_create_function, (pesapi_func_ptr) &pesapi_get_value_bool,
(pesapi_func_ptr) &pesapi_get_value_int32, (pesapi_func_ptr) &pesapi_get_value_uint32,
(pesapi_func_ptr) &pesapi_create_object, (pesapi_func_ptr) &pesapi_create_function, (pesapi_func_ptr) &pesapi_create_class,
(pesapi_func_ptr) &pesapi_get_value_bool, (pesapi_func_ptr) &pesapi_get_value_int32, (pesapi_func_ptr) &pesapi_get_value_uint32,
(pesapi_func_ptr) &pesapi_get_value_int64, (pesapi_func_ptr) &pesapi_get_value_uint64,
(pesapi_func_ptr) &pesapi_get_value_double, (pesapi_func_ptr) &pesapi_get_value_string_utf8,
(pesapi_func_ptr) &pesapi_get_value_binary, (pesapi_func_ptr) &pesapi_get_array_length, (pesapi_func_ptr) &pesapi_is_null,
Expand Down
9 changes: 9 additions & 0 deletions unity/native_src_il2cpp/Src/PesapiV8Impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,15 @@ pesapi_value pesapi_create_function(pesapi_env env, pesapi_callback native_impl,
}
MSVC_PRAGMA(warning(pop))

pesapi_value pesapi_create_class(pesapi_env env, const void* type_id)
{
auto context = v8impl::V8LocalContextFromPesapiEnv(env);
auto cls = puerts::DataTransfer::IsolateData<puerts::ICppObjectMapper>(context->GetIsolate())->LoadTypeById(context, type_id);
if (cls.IsEmpty())
return nullptr;
return v8impl::PesapiValueFromV8LocalValue(cls.ToLocalChecked());
}

bool pesapi_get_value_bool(pesapi_env env, pesapi_value pvalue)
{
auto context = v8impl::V8LocalContextFromPesapiEnv(env);
Expand Down
4 changes: 2 additions & 2 deletions unity/native_src_il2cpp/Src/Puerts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ struct JSEnv
return;
}

auto Ret = pom->LoadTypeById(Isolate, Context, type);
auto Ret = pom->LoadTypeById(Context, type);

if (!Ret.IsEmpty())
{
Info.GetReturnValue().Set(Ret);
Info.GetReturnValue().Set(Ret.ToLocalChecked());
}

}, v8::External::New(Isolate, &CppObjectMapper))->GetFunction(Context).ToLocalChecked()).Check();
Expand Down

0 comments on commit 38f4b1d

Please sign in to comment.