Skip to content

Commit

Permalink
[unity]升级p-api
Browse files Browse the repository at this point in the history
  • Loading branch information
chexiongsheng committed Oct 11, 2024
1 parent 46e477f commit 247726e
Show file tree
Hide file tree
Showing 10 changed files with 230 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 6 additions & 0 deletions unity/native_src_il2cpp/Inc/CppObjectMapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ class FCppObjectMapper final : public ICppObjectMapper

virtual void BindCppObject(v8::Isolate* Isolate, JSClassDefinition* ClassDefinition, void* Ptr, v8::Local<v8::Object> JSObject,
bool PassByPointer) override;

virtual void* GetPrivateData(v8::Local<v8::Context> Context, v8::Local<v8::Object> JSObject) override;

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

void UnInitialize(v8::Isolate* InIsolate);

Expand Down Expand Up @@ -86,6 +90,8 @@ class FCppObjectMapper final : public ICppObjectMapper
std::unordered_map<const void*, v8::UniquePersistent<v8::FunctionTemplate>> TypeIdToTemplateMap;

v8::UniquePersistent<v8::FunctionTemplate> PointerTemplate;

v8::Global<v8::Symbol> PrivateKey;

std::unordered_map<void*, FinalizeFunc> CDataFinalizeMap;

Expand Down
4 changes: 4 additions & 0 deletions unity/native_src_il2cpp/Inc/ObjectMapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class ICppObjectMapper
v8::Isolate* Isolate, v8::Local<v8::Context>& Context, const void* TypeId, void* Ptr, bool PassByPointer) = 0;

virtual bool IsInstanceOfCppObject(const void* TypeId, v8::Local<v8::Object> JsObject) = 0;

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

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

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

Expand Down
12 changes: 11 additions & 1 deletion unity/native_src_il2cpp/Inc/pesapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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(
Expand Down
24 changes: 24 additions & 0 deletions unity/native_src_il2cpp/Src/CppObjectMapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ void FCppObjectMapper::Initialize(v8::Isolate* InIsolate, v8::Local<v8::Context>
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<v8::Value> FCppObjectMapper::FindOrAddCppObject(
Expand Down Expand Up @@ -366,6 +367,28 @@ void FCppObjectMapper::BindCppObject(
}
}

void* FCppObjectMapper::GetPrivateData(v8::Local<v8::Context> Context, v8::Local<v8::Object> JSObject)
{
v8::MaybeLocal<v8::Value> maybeValue = JSObject->Get(Context, PrivateKey.Get(Context->GetIsolate()));
if (maybeValue.IsEmpty())
{
return nullptr;
}

v8::Local<v8::Value> maybeExternal = maybeValue.ToLocalChecked();
if (!maybeExternal->IsExternal())
{
return nullptr;
}

return v8::Local<v8::External>::Cast(maybeExternal)->Value();
}

void FCppObjectMapper::SetPrivateData(v8::Local<v8::Context> Context, v8::Local<v8::Object> 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);
Expand Down Expand Up @@ -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();
Expand Down
Loading

0 comments on commit 247726e

Please sign in to comment.