diff --git a/unity/Assets/core/upm/Editor/Resources/puerts/xil2cpp/Puerts_il2cpp.cpp.txt b/unity/Assets/core/upm/Editor/Resources/puerts/xil2cpp/Puerts_il2cpp.cpp.txt index a15fc1814a..8f7ac37b28 100644 --- a/unity/Assets/core/upm/Editor/Resources/puerts/xil2cpp/Puerts_il2cpp.cpp.txt +++ b/unity/Assets/core/upm/Editor/Resources/puerts/xil2cpp/Puerts_il2cpp.cpp.txt @@ -487,7 +487,7 @@ static void* CtorCallback(pesapi_callback_info info) static void SetPObjectRefInfoValue(pesapi_env env, PObjectRefInfo* objectInfo, pesapi_value jsval) { objectInfo->EnvRef = pesapi_create_env_ref(env); - objectInfo->ValueRef = pesapi_create_value_ref(env, jsval); + objectInfo->ValueRef = pesapi_create_value_ref(env, jsval, 0); g_unityExports.SetExtraData(env, objectInfo); } diff --git a/unity/Assets/core/upm/Editor/Resources/puerts/xil2cpp/pesapi.h.txt b/unity/Assets/core/upm/Editor/Resources/puerts/xil2cpp/pesapi.h.txt index 9ec81ac52d..def0c2ce18 100644 --- a/unity/Assets/core/upm/Editor/Resources/puerts/xil2cpp/pesapi.h.txt +++ b/unity/Assets/core/upm/Editor/Resources/puerts/xil2cpp/pesapi.h.txt @@ -188,6 +188,7 @@ PESAPI_EXTERN void pesapi_add_return(pesapi_callback_info info, pesapi_value val PESAPI_EXTERN void pesapi_throw_by_string(pesapi_callback_info pinfo, const char* msg); PESAPI_EXTERN pesapi_env_ref pesapi_create_env_ref(pesapi_env env); +PESAPI_EXTERN bool pesapi_env_ref_is_valid(pesapi_env_ref env); PESAPI_EXTERN pesapi_env pesapi_get_env_from_ref(pesapi_env_ref env_ref); 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); @@ -197,16 +198,21 @@ 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 pesapi_value_ref pesapi_create_value_ref(pesapi_env env, pesapi_value value); +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); PESAPI_EXTERN void pesapi_release_value_ref(pesapi_value_ref value_ref); PESAPI_EXTERN pesapi_value pesapi_get_value_from_ref(pesapi_env env, pesapi_value_ref value_ref); PESAPI_EXTERN void pesapi_set_ref_weak(pesapi_env env, pesapi_value_ref value_ref); // Optional api: return false if can not fulfill PESAPI_EXTERN bool pesapi_set_owner(pesapi_env env, pesapi_value value, pesapi_value owner); +// suggestion: struct pesapi_value_ref : pesapi_env_ref {...}; +PESAPI_EXTERN pesapi_env_ref pesapi_get_ref_associated_env(pesapi_value_ref value_ref); +PESAPI_EXTERN void** pesapi_get_ref_internal_fields(pesapi_value_ref value_ref, uint32_t* pinternal_field_count); PESAPI_EXTERN pesapi_value pesapi_get_property(pesapi_env env, pesapi_value object, const char* key); PESAPI_EXTERN void pesapi_set_property(pesapi_env env, pesapi_value object, const char* key, pesapi_value value); +PESAPI_EXTERN const void* pesapi_get_private(pesapi_env env, pesapi_value object); +PESAPI_EXTERN void pesapi_set_private(pesapi_env env, pesapi_value object, const void* ptr); PESAPI_EXTERN pesapi_value pesapi_get_property_uint32(pesapi_env env, pesapi_value object, uint32_t key); PESAPI_EXTERN void pesapi_set_property_uint32(pesapi_env env, pesapi_value object, uint32_t key, pesapi_value value); @@ -216,6 +222,10 @@ PESAPI_EXTERN pesapi_value pesapi_call_function( PESAPI_EXTERN pesapi_value pesapi_eval(pesapi_env env, const uint8_t* code, size_t code_size, const char* path); +PESAPI_EXTERN pesapi_value pesapi_global(pesapi_env env); +PESAPI_EXTERN const void* pesapi_get_env_private(pesapi_env env); +PESAPI_EXTERN void pesapi_set_env_private(pesapi_env env, const void* ptr); + PESAPI_EXTERN pesapi_type_info pesapi_alloc_type_infos(size_t count); PESAPI_EXTERN void pesapi_set_type_info( diff --git a/unity/Assets/core/upm/Editor/Resources/puerts/xil2cpp/pesapi_adpt.c.txt b/unity/Assets/core/upm/Editor/Resources/puerts/xil2cpp/pesapi_adpt.c.txt index 9536de98b1..463568ae40 100644 --- a/unity/Assets/core/upm/Editor/Resources/puerts/xil2cpp/pesapi_adpt.c.txt +++ b/unity/Assets/core/upm/Editor/Resources/puerts/xil2cpp/pesapi_adpt.c.txt @@ -328,6 +328,12 @@ pesapi_env_ref pesapi_create_env_ref (pesapi_env env) { return pesapi_create_env_ref_ptr(env); } +typedef bool (*pesapi_env_ref_is_validType)(pesapi_env_ref env); +static pesapi_env_ref_is_validType pesapi_env_ref_is_valid_ptr; +bool pesapi_env_ref_is_valid (pesapi_env_ref env) { + return pesapi_env_ref_is_valid_ptr(env); +} + typedef pesapi_env (*pesapi_get_env_from_refType)(pesapi_env_ref env_ref); static pesapi_get_env_from_refType pesapi_get_env_from_ref_ptr; pesapi_env pesapi_get_env_from_ref (pesapi_env_ref env_ref) { @@ -370,10 +376,10 @@ void pesapi_close_scope (pesapi_scope scope) { pesapi_close_scope_ptr(scope); } -typedef pesapi_value_ref (*pesapi_create_value_refType)(pesapi_env env, pesapi_value value); +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) { - return pesapi_create_value_ref_ptr(env, value); +pesapi_value_ref pesapi_create_value_ref (pesapi_env env, pesapi_value value, uint32_t internal_field_count) { + return pesapi_create_value_ref_ptr(env, value, internal_field_count); } typedef pesapi_value_ref (*pesapi_duplicate_value_refType)(pesapi_value_ref value_ref); @@ -406,6 +412,18 @@ bool pesapi_set_owner (pesapi_env env, pesapi_value value, pesapi_value owner) { return pesapi_set_owner_ptr(env, value, owner); } +typedef pesapi_env_ref (*pesapi_get_ref_associated_envType)(pesapi_value_ref value_ref); +static pesapi_get_ref_associated_envType pesapi_get_ref_associated_env_ptr; +pesapi_env_ref pesapi_get_ref_associated_env (pesapi_value_ref value_ref) { + return pesapi_get_ref_associated_env_ptr(value_ref); +} + +typedef void** (*pesapi_get_ref_internal_fieldsType)(pesapi_value_ref value_ref, uint32_t* pinternal_field_count); +static pesapi_get_ref_internal_fieldsType pesapi_get_ref_internal_fields_ptr; +void** pesapi_get_ref_internal_fields (pesapi_value_ref value_ref, uint32_t* pinternal_field_count) { + return pesapi_get_ref_internal_fields_ptr(value_ref, pinternal_field_count); +} + typedef pesapi_value (*pesapi_get_propertyType)(pesapi_env env, pesapi_value object, const char* key); static pesapi_get_propertyType pesapi_get_property_ptr; pesapi_value pesapi_get_property (pesapi_env env, pesapi_value object, const char* key) { @@ -418,6 +436,18 @@ void pesapi_set_property (pesapi_env env, pesapi_value object, const char* key, pesapi_set_property_ptr(env, object, key, value); } +typedef const void* (*pesapi_get_privateType)(pesapi_env env, pesapi_value object); +static pesapi_get_privateType pesapi_get_private_ptr; +const void* pesapi_get_private (pesapi_env env, pesapi_value object) { + return pesapi_get_private_ptr(env, object); +} + +typedef void (*pesapi_set_privateType)(pesapi_env env, pesapi_value object, const void* ptr); +static pesapi_set_privateType pesapi_set_private_ptr; +void pesapi_set_private (pesapi_env env, pesapi_value object, const void* ptr) { + pesapi_set_private_ptr(env, object, ptr); +} + typedef pesapi_value (*pesapi_get_property_uint32Type)(pesapi_env env, pesapi_value object, uint32_t key); static pesapi_get_property_uint32Type pesapi_get_property_uint32_ptr; pesapi_value pesapi_get_property_uint32 (pesapi_env env, pesapi_value object, uint32_t key) { @@ -442,6 +472,24 @@ pesapi_value pesapi_eval (pesapi_env env, const uint8_t* code, size_t code_size, return pesapi_eval_ptr(env, code, code_size, path); } +typedef pesapi_value (*pesapi_globalType)(pesapi_env env); +static pesapi_globalType pesapi_global_ptr; +pesapi_value pesapi_global (pesapi_env env) { + return pesapi_global_ptr(env); +} + +typedef const void* (*pesapi_get_env_privateType)(pesapi_env env); +static pesapi_get_env_privateType pesapi_get_env_private_ptr; +const void* pesapi_get_env_private (pesapi_env env) { + return pesapi_get_env_private_ptr(env); +} + +typedef void (*pesapi_set_env_privateType)(pesapi_env env, const void* ptr); +static pesapi_set_env_privateType pesapi_set_env_private_ptr; +void pesapi_set_env_private (pesapi_env env, const void* ptr) { + pesapi_set_env_private_ptr(env, ptr); +} + typedef pesapi_type_info (*pesapi_alloc_type_infosType)(size_t count); static pesapi_alloc_type_infosType pesapi_alloc_type_infos_ptr; pesapi_type_info pesapi_alloc_type_infos (size_t count) { @@ -553,34 +601,42 @@ void pesapi_init(pesapi_func_ptr* func_array){ pesapi_add_return_ptr = (pesapi_add_returnType)func_array[49]; pesapi_throw_by_string_ptr = (pesapi_throw_by_stringType)func_array[50]; pesapi_create_env_ref_ptr = (pesapi_create_env_refType)func_array[51]; - pesapi_get_env_from_ref_ptr = (pesapi_get_env_from_refType)func_array[52]; - pesapi_duplicate_env_ref_ptr = (pesapi_duplicate_env_refType)func_array[53]; - pesapi_release_env_ref_ptr = (pesapi_release_env_refType)func_array[54]; - pesapi_open_scope_ptr = (pesapi_open_scopeType)func_array[55]; - pesapi_has_caught_ptr = (pesapi_has_caughtType)func_array[56]; - pesapi_get_exception_as_string_ptr = (pesapi_get_exception_as_stringType)func_array[57]; - pesapi_close_scope_ptr = (pesapi_close_scopeType)func_array[58]; - pesapi_create_value_ref_ptr = (pesapi_create_value_refType)func_array[59]; - pesapi_duplicate_value_ref_ptr = (pesapi_duplicate_value_refType)func_array[60]; - pesapi_release_value_ref_ptr = (pesapi_release_value_refType)func_array[61]; - pesapi_get_value_from_ref_ptr = (pesapi_get_value_from_refType)func_array[62]; - pesapi_set_ref_weak_ptr = (pesapi_set_ref_weakType)func_array[63]; - pesapi_set_owner_ptr = (pesapi_set_ownerType)func_array[64]; - pesapi_get_property_ptr = (pesapi_get_propertyType)func_array[65]; - pesapi_set_property_ptr = (pesapi_set_propertyType)func_array[66]; - pesapi_get_property_uint32_ptr = (pesapi_get_property_uint32Type)func_array[67]; - pesapi_set_property_uint32_ptr = (pesapi_set_property_uint32Type)func_array[68]; - pesapi_call_function_ptr = (pesapi_call_functionType)func_array[69]; - pesapi_eval_ptr = (pesapi_evalType)func_array[70]; - pesapi_alloc_type_infos_ptr = (pesapi_alloc_type_infosType)func_array[71]; - pesapi_set_type_info_ptr = (pesapi_set_type_infoType)func_array[72]; - pesapi_create_signature_info_ptr = (pesapi_create_signature_infoType)func_array[73]; - pesapi_alloc_property_descriptors_ptr = (pesapi_alloc_property_descriptorsType)func_array[74]; - pesapi_set_method_info_ptr = (pesapi_set_method_infoType)func_array[75]; - pesapi_set_property_info_ptr = (pesapi_set_property_infoType)func_array[76]; - pesapi_define_class_ptr = (pesapi_define_classType)func_array[77]; - pesapi_class_type_info_ptr = (pesapi_class_type_infoType)func_array[78]; - pesapi_find_type_id_ptr = (pesapi_find_type_idType)func_array[79]; + pesapi_env_ref_is_valid_ptr = (pesapi_env_ref_is_validType)func_array[52]; + pesapi_get_env_from_ref_ptr = (pesapi_get_env_from_refType)func_array[53]; + pesapi_duplicate_env_ref_ptr = (pesapi_duplicate_env_refType)func_array[54]; + pesapi_release_env_ref_ptr = (pesapi_release_env_refType)func_array[55]; + pesapi_open_scope_ptr = (pesapi_open_scopeType)func_array[56]; + pesapi_has_caught_ptr = (pesapi_has_caughtType)func_array[57]; + pesapi_get_exception_as_string_ptr = (pesapi_get_exception_as_stringType)func_array[58]; + pesapi_close_scope_ptr = (pesapi_close_scopeType)func_array[59]; + pesapi_create_value_ref_ptr = (pesapi_create_value_refType)func_array[60]; + pesapi_duplicate_value_ref_ptr = (pesapi_duplicate_value_refType)func_array[61]; + pesapi_release_value_ref_ptr = (pesapi_release_value_refType)func_array[62]; + pesapi_get_value_from_ref_ptr = (pesapi_get_value_from_refType)func_array[63]; + pesapi_set_ref_weak_ptr = (pesapi_set_ref_weakType)func_array[64]; + pesapi_set_owner_ptr = (pesapi_set_ownerType)func_array[65]; + pesapi_get_ref_associated_env_ptr = (pesapi_get_ref_associated_envType)func_array[66]; + pesapi_get_ref_internal_fields_ptr = (pesapi_get_ref_internal_fieldsType)func_array[67]; + pesapi_get_property_ptr = (pesapi_get_propertyType)func_array[68]; + pesapi_set_property_ptr = (pesapi_set_propertyType)func_array[69]; + pesapi_get_private_ptr = (pesapi_get_privateType)func_array[70]; + pesapi_set_private_ptr = (pesapi_set_privateType)func_array[71]; + pesapi_get_property_uint32_ptr = (pesapi_get_property_uint32Type)func_array[72]; + pesapi_set_property_uint32_ptr = (pesapi_set_property_uint32Type)func_array[73]; + pesapi_call_function_ptr = (pesapi_call_functionType)func_array[74]; + pesapi_eval_ptr = (pesapi_evalType)func_array[75]; + pesapi_global_ptr = (pesapi_globalType)func_array[76]; + pesapi_get_env_private_ptr = (pesapi_get_env_privateType)func_array[77]; + pesapi_set_env_private_ptr = (pesapi_set_env_privateType)func_array[78]; + pesapi_alloc_type_infos_ptr = (pesapi_alloc_type_infosType)func_array[79]; + pesapi_set_type_info_ptr = (pesapi_set_type_infoType)func_array[80]; + pesapi_create_signature_info_ptr = (pesapi_create_signature_infoType)func_array[81]; + pesapi_alloc_property_descriptors_ptr = (pesapi_alloc_property_descriptorsType)func_array[82]; + pesapi_set_method_info_ptr = (pesapi_set_method_infoType)func_array[83]; + pesapi_set_property_info_ptr = (pesapi_set_property_infoType)func_array[84]; + pesapi_define_class_ptr = (pesapi_define_classType)func_array[85]; + pesapi_class_type_info_ptr = (pesapi_class_type_infoType)func_array[86]; + pesapi_find_type_id_ptr = (pesapi_find_type_idType)func_array[87]; #endif } diff --git a/unity/native_src_il2cpp/Inc/CppObjectMapper.h b/unity/native_src_il2cpp/Inc/CppObjectMapper.h index d9b921e80d..00a7866d41 100644 --- a/unity/native_src_il2cpp/Inc/CppObjectMapper.h +++ b/unity/native_src_il2cpp/Inc/CppObjectMapper.h @@ -59,6 +59,10 @@ class FCppObjectMapper final : public ICppObjectMapper virtual void BindCppObject(v8::Isolate* Isolate, JSClassDefinition* ClassDefinition, void* Ptr, v8::Local JSObject, bool PassByPointer) override; + + virtual void* GetPrivateData(v8::Local Context, v8::Local JSObject) override; + + virtual void SetPrivateData(v8::Local Context, v8::Local JSObject, void* Ptr) override; void UnInitialize(v8::Isolate* InIsolate); @@ -86,6 +90,8 @@ class FCppObjectMapper final : public ICppObjectMapper std::unordered_map> TypeIdToTemplateMap; v8::UniquePersistent PointerTemplate; + + v8::Global PrivateKey; std::unordered_map CDataFinalizeMap; diff --git a/unity/native_src_il2cpp/Inc/ObjectMapper.h b/unity/native_src_il2cpp/Inc/ObjectMapper.h index 00e4250f7d..6dd68dab8c 100644 --- a/unity/native_src_il2cpp/Inc/ObjectMapper.h +++ b/unity/native_src_il2cpp/Inc/ObjectMapper.h @@ -35,6 +35,10 @@ class ICppObjectMapper v8::Isolate* Isolate, v8::Local& Context, const void* TypeId, void* Ptr, bool PassByPointer) = 0; virtual bool IsInstanceOfCppObject(const void* TypeId, v8::Local JsObject) = 0; + + virtual void* GetPrivateData(v8::Local Context, v8::Local JSObject) = 0; + + virtual void SetPrivateData(v8::Local Context, v8::Local JSObject, void* Ptr) = 0; virtual std::weak_ptr GetJsEnvLifeCycleTracker() = 0; diff --git a/unity/native_src_il2cpp/Inc/pesapi.h b/unity/native_src_il2cpp/Inc/pesapi.h index 9ec81ac52d..def0c2ce18 100644 --- a/unity/native_src_il2cpp/Inc/pesapi.h +++ b/unity/native_src_il2cpp/Inc/pesapi.h @@ -188,6 +188,7 @@ PESAPI_EXTERN void pesapi_add_return(pesapi_callback_info info, pesapi_value val PESAPI_EXTERN void pesapi_throw_by_string(pesapi_callback_info pinfo, const char* msg); PESAPI_EXTERN pesapi_env_ref pesapi_create_env_ref(pesapi_env env); +PESAPI_EXTERN bool pesapi_env_ref_is_valid(pesapi_env_ref env); PESAPI_EXTERN pesapi_env pesapi_get_env_from_ref(pesapi_env_ref env_ref); 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); @@ -197,16 +198,21 @@ 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 pesapi_value_ref pesapi_create_value_ref(pesapi_env env, pesapi_value value); +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); PESAPI_EXTERN void pesapi_release_value_ref(pesapi_value_ref value_ref); PESAPI_EXTERN pesapi_value pesapi_get_value_from_ref(pesapi_env env, pesapi_value_ref value_ref); PESAPI_EXTERN void pesapi_set_ref_weak(pesapi_env env, pesapi_value_ref value_ref); // Optional api: return false if can not fulfill PESAPI_EXTERN bool pesapi_set_owner(pesapi_env env, pesapi_value value, pesapi_value owner); +// suggestion: struct pesapi_value_ref : pesapi_env_ref {...}; +PESAPI_EXTERN pesapi_env_ref pesapi_get_ref_associated_env(pesapi_value_ref value_ref); +PESAPI_EXTERN void** pesapi_get_ref_internal_fields(pesapi_value_ref value_ref, uint32_t* pinternal_field_count); PESAPI_EXTERN pesapi_value pesapi_get_property(pesapi_env env, pesapi_value object, const char* key); PESAPI_EXTERN void pesapi_set_property(pesapi_env env, pesapi_value object, const char* key, pesapi_value value); +PESAPI_EXTERN const void* pesapi_get_private(pesapi_env env, pesapi_value object); +PESAPI_EXTERN void pesapi_set_private(pesapi_env env, pesapi_value object, const void* ptr); PESAPI_EXTERN pesapi_value pesapi_get_property_uint32(pesapi_env env, pesapi_value object, uint32_t key); PESAPI_EXTERN void pesapi_set_property_uint32(pesapi_env env, pesapi_value object, uint32_t key, pesapi_value value); @@ -216,6 +222,10 @@ PESAPI_EXTERN pesapi_value pesapi_call_function( PESAPI_EXTERN pesapi_value pesapi_eval(pesapi_env env, const uint8_t* code, size_t code_size, const char* path); +PESAPI_EXTERN pesapi_value pesapi_global(pesapi_env env); +PESAPI_EXTERN const void* pesapi_get_env_private(pesapi_env env); +PESAPI_EXTERN void pesapi_set_env_private(pesapi_env env, const void* ptr); + PESAPI_EXTERN pesapi_type_info pesapi_alloc_type_infos(size_t count); PESAPI_EXTERN void pesapi_set_type_info( diff --git a/unity/native_src_il2cpp/Src/CppObjectMapper.cpp b/unity/native_src_il2cpp/Src/CppObjectMapper.cpp index 5c27bf3e61..ece2de8269 100644 --- a/unity/native_src_il2cpp/Src/CppObjectMapper.cpp +++ b/unity/native_src_il2cpp/Src/CppObjectMapper.cpp @@ -91,6 +91,7 @@ void FCppObjectMapper::Initialize(v8::Isolate* InIsolate, v8::Local PersistentObjectEnvInfo.Isolate = InIsolate; PersistentObjectEnvInfo.Context.Reset(InIsolate, InContext); PersistentObjectEnvInfo.SymbolCSPtr.Reset(InIsolate, v8::Symbol::New(InIsolate)); + PrivateKey.Reset(InIsolate, v8::Symbol::New(InIsolate)); } v8::Local FCppObjectMapper::FindOrAddCppObject( @@ -366,6 +367,28 @@ void FCppObjectMapper::BindCppObject( } } +void* FCppObjectMapper::GetPrivateData(v8::Local Context, v8::Local JSObject) +{ + v8::MaybeLocal maybeValue = JSObject->Get(Context, PrivateKey.Get(Context->GetIsolate())); + if (maybeValue.IsEmpty()) + { + return nullptr; + } + + v8::Local maybeExternal = maybeValue.ToLocalChecked(); + if (!maybeExternal->IsExternal()) + { + return nullptr; + } + + return v8::Local::Cast(maybeExternal)->Value(); +} + +void FCppObjectMapper::SetPrivateData(v8::Local Context, v8::Local JSObject, void* Ptr) +{ + (void) (JSObject->Set(Context, PrivateKey.Get(Context->GetIsolate()), v8::External::New(Context->GetIsolate(), Ptr))); +} + void FCppObjectMapper::UnBindCppObject(JSClassDefinition* ClassDefinition, void* Ptr) { CDataFinalizeMap.erase(Ptr); @@ -399,6 +422,7 @@ void FCppObjectMapper::UnInitialize(v8::Isolate* InIsolate) CDataCache.clear(); CDataFinalizeMap.clear(); TypeIdToTemplateMap.clear(); + PrivateKey.Reset(); PointerTemplate.Reset(); PersistentObjectEnvInfo.Context.Reset(); PersistentObjectEnvInfo.SymbolCSPtr.Reset(); diff --git a/unity/native_src_il2cpp/Src/PesapiAddonLoad.cpp b/unity/native_src_il2cpp/Src/PesapiAddonLoad.cpp index a4ad06bd3b..00995567d6 100644 --- a/unity/native_src_il2cpp/Src/PesapiAddonLoad.cpp +++ b/unity/native_src_il2cpp/Src/PesapiAddonLoad.cpp @@ -55,14 +55,18 @@ static pesapi_func_ptr funcs[] = {(pesapi_func_ptr) &pesapi_create_null, (pesapi (pesapi_func_ptr) &pesapi_get_arg, (pesapi_func_ptr) &pesapi_get_env, (pesapi_func_ptr) &pesapi_get_this, (pesapi_func_ptr) &pesapi_get_holder, (pesapi_func_ptr) &pesapi_get_userdata, (pesapi_func_ptr) &pesapi_get_constructor_userdata, (pesapi_func_ptr) &pesapi_add_return, - (pesapi_func_ptr) &pesapi_throw_by_string, (pesapi_func_ptr) &pesapi_create_env_ref, (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_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_property, - (pesapi_func_ptr) &pesapi_set_property, (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_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/unity/native_src_il2cpp/Src/PesapiV8Impl.cpp b/unity/native_src_il2cpp/Src/PesapiV8Impl.cpp index 70f28a881d..e24ec9aef1 100644 --- a/unity/native_src_il2cpp/Src/PesapiV8Impl.cpp +++ b/unity/native_src_il2cpp/Src/PesapiV8Impl.cpp @@ -11,6 +11,7 @@ #include "pesapi.h" #include "DataTransfer.h" #include "JSClassRegister.h" +#include "ObjectMapper.h" #include #include @@ -33,20 +34,16 @@ struct pesapi_env_ref__ std::weak_ptr env_life_cycle_tracker; }; -struct pesapi_value_ref__ +struct pesapi_value_ref__ : pesapi_env_ref__ { - explicit pesapi_value_ref__(v8::Local context, v8::Local value) - : value_persistent(context->GetIsolate(), value) - , isolate(context->GetIsolate()) - , ref_count(1) - , env_life_cycle_tracker(puerts::DataTransfer::GetJsEnvLifeCycleTracker(context->GetIsolate())) + explicit pesapi_value_ref__(v8::Local context, v8::Local value, uint32_t field_count) + : pesapi_env_ref__(context), value_persistent(context->GetIsolate(), value), internal_field_count(field_count) { } v8::Persistent value_persistent; - v8::Isolate* const isolate; - int ref_count; - std::weak_ptr env_life_cycle_tracker; + uint32_t internal_field_count; + void* internal_fields[0]; }; struct pesapi_scope__ @@ -471,6 +468,11 @@ pesapi_env_ref pesapi_create_env_ref(pesapi_env env) return new pesapi_env_ref__(context); } +bool pesapi_env_ref_is_valid(pesapi_env_ref env_ref) +{ + return !env_ref->env_life_cycle_tracker.expired(); +} + pesapi_env pesapi_get_env_from_ref(pesapi_env_ref env_ref) { if (env_ref->env_life_cycle_tracker.expired()) @@ -564,11 +566,13 @@ void pesapi_close_scope(pesapi_scope scope) isolate->Exit(); } -pesapi_value_ref pesapi_create_value_ref(pesapi_env env, pesapi_value pvalue) +pesapi_value_ref pesapi_create_value_ref(pesapi_env env, pesapi_value pvalue, uint32_t internal_field_count) { auto context = v8impl::V8LocalContextFromPesapiEnv(env); auto value = v8impl::V8LocalValueFromPesapiValue(pvalue); - return new pesapi_value_ref__(context, value); + size_t totalSize = sizeof(pesapi_value_ref__) + sizeof(void*) * internal_field_count; + void* buffer = ::operator new(totalSize); + return new (buffer) pesapi_value_ref__(context, value, internal_field_count); } pesapi_value_ref pesapi_duplicate_value_ref(pesapi_value_ref value_ref) @@ -585,15 +589,14 @@ void pesapi_release_value_ref(pesapi_value_ref value_ref) { #if V8_MAJOR_VERSION < 11 value_ref->value_persistent.Empty(); - delete value_ref; -#else - ::operator delete(static_cast(value_ref)); + value_ref->~pesapi_value_ref__(); #endif } else { - delete value_ref; + value_ref->~pesapi_value_ref__(); } + ::operator delete(static_cast(value_ref)); } } @@ -627,6 +630,17 @@ bool pesapi_set_owner(pesapi_env env, pesapi_value pvalue, pesapi_value powner) return false; } +pesapi_env_ref pesapi_get_ref_associated_env(pesapi_value_ref value_ref) +{ + return value_ref; +} + +void** pesapi_get_ref_internal_fields(pesapi_value_ref value_ref, uint32_t* pinternal_field_count) +{ + *pinternal_field_count = value_ref->internal_field_count; + return &value_ref->internal_fields[0]; +} + pesapi_value pesapi_get_property(pesapi_env env, pesapi_value pobject, const char* key) { auto context = v8impl::V8LocalContextFromPesapiEnv(env); @@ -657,6 +671,22 @@ void pesapi_set_property(pesapi_env env, pesapi_value pobject, const char* key, } } +const void* pesapi_get_private(pesapi_env env, pesapi_value pobject) +{ + auto context = v8impl::V8LocalContextFromPesapiEnv(env); + auto object = v8impl::V8LocalValueFromPesapiValue(pobject); + return puerts::DataTransfer::IsolateData(context->GetIsolate()) + ->GetPrivateData(context, object.As()); +} + +void pesapi_set_private(pesapi_env env, pesapi_value pobject, const void* ptr) +{ + auto context = v8impl::V8LocalContextFromPesapiEnv(env); + auto object = v8impl::V8LocalValueFromPesapiValue(pobject); + puerts::DataTransfer::IsolateData(context->GetIsolate()) + ->SetPrivateData(context, object.As(), const_cast(ptr)); +} + pesapi_value pesapi_get_property_uint32(pesapi_env env, pesapi_value pobject, uint32_t key) { auto context = v8impl::V8LocalContextFromPesapiEnv(env); @@ -733,6 +763,25 @@ pesapi_value pesapi_eval(pesapi_env env, const uint8_t* code, size_t code_size, return v8impl::PesapiValueFromV8LocalValue(maybe_ret.ToLocalChecked()); } +pesapi_value pesapi_global(pesapi_env env) +{ + auto context = v8impl::V8LocalContextFromPesapiEnv(env); + auto global = context->Global(); + return v8impl::PesapiValueFromV8LocalValue(global); +} + +const void* pesapi_get_env_private(pesapi_env env) +{ + auto context = v8impl::V8LocalContextFromPesapiEnv(env); + return context->GetIsolate()->GetData(MAPPER_ISOLATE_DATA_POS + 1); +} + +void pesapi_set_env_private(pesapi_env env, const void* ptr) +{ + auto context = v8impl::V8LocalContextFromPesapiEnv(env); + context->GetIsolate()->SetData(MAPPER_ISOLATE_DATA_POS + 1, const_cast(ptr)); +} + struct pesapi_type_info__ { const char* name; diff --git a/unity/native_src_il2cpp/Src/Puerts.cpp b/unity/native_src_il2cpp/Src/Puerts.cpp index b2aace7261..0938d22ce2 100644 --- a/unity/native_src_il2cpp/Src/Puerts.cpp +++ b/unity/native_src_il2cpp/Src/Puerts.cpp @@ -164,6 +164,16 @@ static void SetExtraData(pesapi_env env, struct PObjectRefInfo* objectInfo) objectInfo->EnvLifeCycleTracker = DataTransfer::GetJsEnvLifeCycleTracker(Isolate); } +// 临时兼容新papi +struct pesapi_value_ref_def +{ + v8::Persistent context_persistent; + v8::Isolate* const isolate; + int ref_count; + std::weak_ptr env_life_cycle_tracker; + v8::Global value_persistent; +}; + static void UnrefJsObject(PObjectRefInfo* objectInfo) { // gc线程不能访问v8虚拟机,访问就会崩溃 /// @@ -171,9 +181,8 @@ static void UnrefJsObject(PObjectRefInfo* objectInfo) { auto envInfo = static_cast(objectInfo->ExtraData); std::lock_guard guard(envInfo->Mutex); - - v8::Global *obj = reinterpret_cast *>(objectInfo->ValueRef); // TODO: 和实现绑定了,需优化 - envInfo->PendingReleaseObjects.push_back(std::move(*obj)); + pesapi_value_ref_def* vr = (pesapi_value_ref_def*)objectInfo->ValueRef; + envInfo->PendingReleaseObjects.push_back(std::move(vr->value_persistent)); } objectInfo->ExtraData = nullptr; // 两个delete,可以通过直接用PObjectRefInfo placement new的方式优化,但需要p-api新增api