Skip to content

Commit

Permalink
Fix the issue of JSVM running crash
Browse files Browse the repository at this point in the history
  • Loading branch information
qiuguohua committed Dec 16, 2024
1 parent e4ac5b3 commit 2bfc780
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 26 deletions.
25 changes: 6 additions & 19 deletions native/cocos/bindings/jswrapper/jsvm/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void Object::setPrivateObject(PrivateObjectBase* data) {
auto tmpThis = _objRef.getValue(_env);
JSVM_Ref result = nullptr;
NODE_API_CALL(status, _env,
OH_JSVM_Wrap(_env, tmpThis, this, sendWeakCallback,
OH_JSVM_Wrap(_env, tmpThis, this, weakCallback,
(void*)this /* finalize_hint */, &result));
//_objRef.setWeakref(_env, result);
setProperty("__native_ptr__", se::Value(static_cast<uint64_t>(reinterpret_cast<uintptr_t>(data))));
Expand Down Expand Up @@ -423,19 +423,8 @@ Object* Object::createTypedArrayWithBuffer(TypedArrayType type, const Object *ob
Object* Object::createExternalArrayBufferObject(void* contents, size_t byteLength, BufferContentsFreeFunc freeFunc, void* freeUserData) {
JSVM_Status status;
JSVM_Value result;
if (freeFunc) {
struct ExternalArrayBufferCallbackParams* param = new (struct ExternalArrayBufferCallbackParams);
param->func = freeFunc;
param->contents = contents;
param->byteLength = byteLength;
param->userData = freeUserData;
NODE_API_CALL(status, ScriptEngine::getEnv(), OH_JSVM_CreateArraybuffer(ScriptEngine::getEnv(), byteLength, &contents, &result));
param->func(param->contents, param->byteLength, param->userData);
delete param;
} else {
NODE_API_CALL(status, ScriptEngine::getEnv(), OH_JSVM_CreateArraybuffer(ScriptEngine::getEnv(), byteLength, &contents, &result));
}

// JSVM does not support the napi_create_external_arraybuffer interface like in NAPI.
NODE_API_CALL(status, ScriptEngine::getEnv(), OH_JSVM_CreateArrayBufferFromBackingStoreData(ScriptEngine::getEnv(), contents, byteLength, 0, byteLength, &result));
Object* obj = Object::_createJSObject(ScriptEngine::getEnv(), result, nullptr);
return obj;
}
Expand Down Expand Up @@ -703,16 +692,14 @@ void Object::sendWeakCallback(JSVM_Env env, void* nativeObject, void* finalizeHi
}

void Object::weakCallback(JSVM_Env env, void* nativeObject, void* finalizeHint /*finalize_hint*/) {

if (finalizeHint) {
if (nativeObject == nullptr) {
return;
}
void *rawPtr = reinterpret_cast<Object*>(finalizeHint)->_privateData;
Object* seObj = reinterpret_cast<Object*>(finalizeHint);
Object* rawPtrObj = reinterpret_cast<Object*>(rawPtr);
if(rawPtrObj->getRefCount() == 0) {
return;
}
if (seObj->_onCleaingPrivateData) { //called by cleanPrivateData, not release seObj;
return;
}
Expand All @@ -726,11 +713,11 @@ void Object::weakCallback(JSVM_Env env, void* nativeObject, void* finalizeHint /
}

if (seObj->_finalizeCb != nullptr) {
seObj->_finalizeCb(env, rawPtr, rawPtr);
seObj->_finalizeCb(env, finalizeHint, finalizeHint);
} else {
assert(seObj->_getClass() != nullptr);
if (seObj->_getClass()->_getFinalizeFunction() != nullptr) {
seObj->_getClass()->_getFinalizeFunction()(env, rawPtr, rawPtr);
seObj->_getClass()->_getFinalizeFunction()(env, finalizeHint, finalizeHint);
}
}
seObj->decRef();
Expand Down
8 changes: 4 additions & 4 deletions native/cocos/bindings/jswrapper/jsvm/Object.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,10 @@ class Object : public RefCounter {


/**
* @brief Delete a property of an object.
* @param[in] name A utf-8 string containing the property's name.
* @return true if the property is deleted successfully, otherwise false.
*/
* @brief Delete a property of an object.
* @param[in] name A utf-8 string containing the property's name.
* @return true if the property is deleted successfully, otherwise false.
*/
bool deleteProperty(const char *name);

/**
Expand Down
4 changes: 2 additions & 2 deletions native/cocos/bindings/jswrapper/napi/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -662,11 +662,11 @@ void Object::weakCallback(napi_env env, void* nativeObject, void* finalizeHint /
}

if (seObj->_finalizeCb != nullptr) {
seObj->_finalizeCb(env, rawPtr, rawPtr);
seObj->_finalizeCb(env, finalizeHint, finalizeHint);
} else {
assert(seObj->_getClass() != nullptr);
if (seObj->_getClass()->_getFinalizeFunction() != nullptr) {
seObj->_getClass()->_getFinalizeFunction()(env, rawPtr, rawPtr);
seObj->_getClass()->_getFinalizeFunction()(env, finalizeHint, finalizeHint);
}
}
seObj->decRef();
Expand Down
2 changes: 1 addition & 1 deletion native/cocos/bindings/manual/jsb_global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,7 @@ static bool jsb_createExternalArrayBuffer(se::State &s) { // NOLINT
SE_PRECONDITION2(ok, false, "Error processing arguments");
if (byteLength > 0) {
// NOTE: Currently V8 use shared_ptr which has different abi on win64-debug and win64-release
#if CC_PLATFORM == CC_PLATFORM_WINDOWS && SCRIPT_ENGINE_TYPE == SCRIPT_ENGINE_V8
#if (CC_PLATFORM == CC_PLATFORM_WINDOWS && SCRIPT_ENGINE_TYPE == SCRIPT_ENGINE_V8) || (SCRIPT_ENGINE_TYPE == SCRIPT_ENGINE_JSVM)
se::HandleObject arrayBuffer{se::Object::createArrayBufferObject(nullptr, byteLength)};
#else
void *buffer = malloc(byteLength);
Expand Down

0 comments on commit 2bfc780

Please sign in to comment.