| 6324 | }; |
| 6325 | |
| 6326 | TEST_P(RuntimeFixture, supportsUserCreatedNativeObjects) { |
| 6327 | Ref<MyNativeObject> nativeObject = makeShared<MyNativeObject>(); |
| 6328 | |
| 6329 | auto objectsManager = wrapper.runtime->getJavaScriptRuntime()->createNativeObjectsManager(""); |
| 6330 | |
| 6331 | ASSERT_EQ(1, nativeObject.use_count()); |
| 6332 | |
| 6333 | auto jsResult = |
| 6334 | callFunctionSync(wrapper, objectsManager, "test/src/WrapNativeObject", "wrapper", {Value(nativeObject)}); |
| 6335 | |
| 6336 | ASSERT_TRUE(jsResult) << jsResult.description(); |
| 6337 | |
| 6338 | // Our native object should have been retained |
| 6339 | ASSERT_EQ(2, nativeObject.use_count()); |
| 6340 | |
| 6341 | ASSERT_TRUE(jsResult.value().isFunction()); |
| 6342 | |
| 6343 | auto unwrapObject = [&]() -> Value { |
| 6344 | return jsResult.value().getFunction()->call(Valdi::ValueFunctionFlagsCallSync, {}).value(); |
| 6345 | }; |
| 6346 | |
| 6347 | Value retValue = unwrapObject(); |
| 6348 | |
| 6349 | // We should be able to unwrap our passed object |
| 6350 | ASSERT_TRUE(retValue.isValdiObject()); |
| 6351 | ASSERT_EQ(nativeObject, castOrNull<MyNativeObject>(retValue.getValdiObject())); |
| 6352 | |
| 6353 | // We should have +1 inside the Value |
| 6354 | ASSERT_EQ(3, nativeObject.use_count()); |
| 6355 | |
| 6356 | retValue = Value(); |
| 6357 | |
| 6358 | ASSERT_EQ(2, nativeObject.use_count()); |
| 6359 | |
| 6360 | // Now we dispose the objects manager |
| 6361 | |
| 6362 | wrapper.runtime->getJavaScriptRuntime()->dispatchSynchronouslyOnJsThread([&](auto& jsEntry) { |
| 6363 | wrapper.runtime->getJavaScriptRuntime()->destroyNativeObjectsManager(objectsManager); |
| 6364 | return; |
| 6365 | }); |
| 6366 | |
| 6367 | // The reference should have been released |
| 6368 | ASSERT_EQ(1, nativeObject.use_count()); |
| 6369 | |
| 6370 | // We should not be able to unwrap the object anymore |
| 6371 | retValue = unwrapObject(); |
| 6372 | |
| 6373 | ASSERT_TRUE(retValue.isUndefined()); |
| 6374 | } |
| 6375 | |
| 6376 | TEST_P(RuntimeFixture, destroysNativeRefsWhenUserCreatedContextIsDestroyed) { |
| 6377 | Ref<MyNativeObject> nativeObject = makeShared<MyNativeObject>(); |
nothing calls this directly
no test coverage detected