|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +#include "NativeFantom.h" |
| 9 | + |
| 10 | +#include <jsi/JSIDynamic.h> |
| 11 | +#include <react/bridging/Bridging.h> |
| 12 | +#include <react/renderer/components/modal/ModalHostViewShadowNode.h> |
| 13 | +#include <react/renderer/components/scrollview/ScrollViewShadowNode.h> |
| 14 | +#include <react/renderer/uimanager/UIManagerBinding.h> |
| 15 | +#include <iostream> |
| 16 | + |
| 17 | +#include "TesterAppDelegate.h" |
| 18 | + |
| 19 | +#include <jsi/instrumentation.h> |
| 20 | +#include "render/RenderFormatOptions.h" |
| 21 | +#include "render/RenderOutput.h" |
| 22 | + |
| 23 | +namespace facebook::react { |
| 24 | + |
| 25 | +NativeFantom::NativeFantom( |
| 26 | + TesterAppDelegate& appDelegate, |
| 27 | + std::shared_ptr<CallInvoker> jsInvoker) |
| 28 | + : NativeFantomCxxSpec<NativeFantom>(std::move(jsInvoker)), |
| 29 | + appDelegate_(appDelegate) {} |
| 30 | + |
| 31 | +SurfaceId NativeFantom::startSurface( |
| 32 | + jsi::Runtime& runtime, |
| 33 | + double viewportWidth, |
| 34 | + double viewportHeight, |
| 35 | + double devicePixelRatio, |
| 36 | + double viewportOffsetX, |
| 37 | + double viewportOffsetY) { |
| 38 | + SurfaceId surfaceId = nextSurfaceId_; |
| 39 | + nextSurfaceId_ += 10; |
| 40 | + appDelegate_.startSurface( |
| 41 | + runtime, |
| 42 | + static_cast<float>(viewportWidth), |
| 43 | + static_cast<float>(viewportHeight), |
| 44 | + surfaceId, |
| 45 | + static_cast<float>(devicePixelRatio), |
| 46 | + static_cast<float>(viewportOffsetX), |
| 47 | + static_cast<float>(viewportOffsetY)); |
| 48 | + return surfaceId; |
| 49 | +} |
| 50 | + |
| 51 | +void NativeFantom::stopSurface(jsi::Runtime& /*runtime*/, SurfaceId surfaceId) { |
| 52 | + appDelegate_.stopSurface(surfaceId); |
| 53 | +} |
| 54 | + |
| 55 | +void NativeFantom::produceFramesForDuration( |
| 56 | + jsi::Runtime& /*runtime*/, |
| 57 | + double milliseconds) { |
| 58 | + appDelegate_.produceFramesForDuration(milliseconds); |
| 59 | +} |
| 60 | + |
| 61 | +void NativeFantom::flushMessageQueue(jsi::Runtime& /*runtime*/) { |
| 62 | + appDelegate_.flushMessageQueue(); |
| 63 | +} |
| 64 | + |
| 65 | +void NativeFantom::flushEventQueue(jsi::Runtime& /*runtime*/) { |
| 66 | + appDelegate_.onRender(); |
| 67 | +} |
| 68 | + |
| 69 | +void NativeFantom::validateEmptyMessageQueue(jsi::Runtime& /*runtime*/) { |
| 70 | + if (appDelegate_.hasPendingTasksInMessageQueue()) { |
| 71 | + throw std::runtime_error("MessageQueue is not empty"); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +std::vector<std::string> NativeFantom::takeMountingManagerLogs( |
| 76 | + jsi::Runtime& /*runtime*/, |
| 77 | + SurfaceId surfaceId) { |
| 78 | + return appDelegate_.mountingManager_->takeMountingLogs(surfaceId); |
| 79 | +} |
| 80 | + |
| 81 | +std::string NativeFantom::getRenderedOutput( |
| 82 | + jsi::Runtime& /*runtime*/, |
| 83 | + SurfaceId surfaceId, |
| 84 | + NativeFantomGetRenderedOutputRenderFormatOptions options) { |
| 85 | + RenderFormatOptions formatOptions{ |
| 86 | + options.includeRoot, options.includeLayoutMetrics}; |
| 87 | + |
| 88 | + auto viewTree = appDelegate_.mountingManager_->getViewTree(surfaceId); |
| 89 | + return RenderOutput::render(viewTree, formatOptions); |
| 90 | +} |
| 91 | + |
| 92 | +void NativeFantom::reportTestSuiteResultsJSON( |
| 93 | + jsi::Runtime& /*runtime*/, |
| 94 | + std::string testSuiteResultsJSON) { |
| 95 | + std::cout << testSuiteResultsJSON << std::endl; |
| 96 | +} |
| 97 | + |
| 98 | +jsi::Object NativeFantom::getDirectManipulationProps( |
| 99 | + jsi::Runtime& runtime, |
| 100 | + const ShadowNode::Shared& shadowNode) { |
| 101 | + auto props = appDelegate_.mountingManager_->getViewDirectManipulationProps( |
| 102 | + shadowNode->getTag()); |
| 103 | + return facebook::jsi::valueFromDynamic(runtime, props).asObject(runtime); |
| 104 | +} |
| 105 | + |
| 106 | +jsi::Object NativeFantom::getFabricUpdateProps( |
| 107 | + jsi::Runtime& runtime, |
| 108 | + const ShadowNode::Shared& shadowNode) { |
| 109 | + auto props = appDelegate_.mountingManager_->getViewFabricUpdateProps( |
| 110 | + shadowNode->getTag()); |
| 111 | + return facebook::jsi::valueFromDynamic(runtime, props).asObject(runtime); |
| 112 | +} |
| 113 | + |
| 114 | +void NativeFantom::enqueueNativeEvent( |
| 115 | + jsi::Runtime& /*runtime*/, |
| 116 | + ShadowNode::Shared shadowNode, |
| 117 | + std::string type, |
| 118 | + const std::optional<folly::dynamic>& payload, |
| 119 | + std::optional<RawEvent::Category> category, |
| 120 | + std::optional<bool> isUnique) { |
| 121 | + if (isUnique.value_or(false)) { |
| 122 | + shadowNode->getEventEmitter()->dispatchUniqueEvent( |
| 123 | + std::move(type), payload.value_or(folly::dynamic::object())); |
| 124 | + } else { |
| 125 | + shadowNode->getEventEmitter()->dispatchEvent( |
| 126 | + std::move(type), |
| 127 | + payload.value_or(folly::dynamic::object()), |
| 128 | + category.value_or(RawEvent::Category::Unspecified)); |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +void NativeFantom::enqueueScrollEvent( |
| 133 | + jsi::Runtime& /*runtime*/, |
| 134 | + ShadowNode::Shared shadowNode, |
| 135 | + ScrollOptions options) { |
| 136 | + const auto* scrollViewShadowNode = |
| 137 | + dynamic_cast<const ScrollViewShadowNode*>(&*shadowNode); |
| 138 | + |
| 139 | + if (scrollViewShadowNode == nullptr) { |
| 140 | + throw std::runtime_error( |
| 141 | + "enqueueScrollEvent() can only be called on <ScrollView />"); |
| 142 | + } |
| 143 | + |
| 144 | + auto point = Point{ |
| 145 | + .x = options.x, |
| 146 | + .y = options.y, |
| 147 | + }; |
| 148 | + |
| 149 | + auto scrollEvent = ScrollEvent(); |
| 150 | + |
| 151 | + scrollEvent.contentOffset = point; |
| 152 | + scrollEvent.contentSize = |
| 153 | + scrollViewShadowNode->getStateData().getContentSize(); |
| 154 | + scrollEvent.containerSize = |
| 155 | + scrollViewShadowNode->getLayoutMetrics().frame.size; |
| 156 | + scrollEvent.contentInset = |
| 157 | + scrollViewShadowNode->getConcreteProps().contentInset; |
| 158 | + scrollEvent.zoomScale = options.zoomScale.value_or(scrollEvent.zoomScale); |
| 159 | + |
| 160 | + scrollViewShadowNode->getConcreteEventEmitter().onScroll(scrollEvent); |
| 161 | + |
| 162 | + auto state = |
| 163 | + std::static_pointer_cast<const ScrollViewShadowNode::ConcreteState>( |
| 164 | + scrollViewShadowNode->getState()); |
| 165 | + state->updateState( |
| 166 | + [point](const ScrollViewShadowNode::ConcreteState::Data& oldData) |
| 167 | + -> ScrollViewShadowNode::ConcreteState::SharedData { |
| 168 | + auto newData = oldData; |
| 169 | + newData.contentOffset = point; |
| 170 | + return std::make_shared< |
| 171 | + const ScrollViewShadowNode::ConcreteState::Data>(newData); |
| 172 | + }); |
| 173 | +} |
| 174 | + |
| 175 | +void NativeFantom::enqueueModalSizeUpdate( |
| 176 | + jsi::Runtime& /*runtime*/, |
| 177 | + ShadowNode::Shared shadowNode, |
| 178 | + double width, |
| 179 | + double height) { |
| 180 | + const auto* modalHostViewShadowNode = |
| 181 | + dynamic_cast<const ModalHostViewShadowNode*>(&*shadowNode); |
| 182 | + |
| 183 | + if (modalHostViewShadowNode == nullptr) { |
| 184 | + throw std::runtime_error( |
| 185 | + "enqueueModalSizeUpdate() can only be called on <Modal />"); |
| 186 | + } |
| 187 | + |
| 188 | + auto state = |
| 189 | + std::static_pointer_cast<const ModalHostViewShadowNode::ConcreteState>( |
| 190 | + modalHostViewShadowNode->getState()); |
| 191 | + |
| 192 | + state->updateState(ModalHostViewState( |
| 193 | + {.width = static_cast<Float>(width), |
| 194 | + .height = static_cast<Float>(height)})); |
| 195 | +} |
| 196 | + |
| 197 | +jsi::Function NativeFantom::createShadowNodeReferenceCounter( |
| 198 | + jsi::Runtime& runtime, |
| 199 | + ShadowNode::Shared shadowNode) { |
| 200 | + auto weakShadowNode = std::weak_ptr<const ShadowNode>(shadowNode); |
| 201 | + |
| 202 | + return jsi::Function::createFromHostFunction( |
| 203 | + runtime, |
| 204 | + jsi::PropNameID::forAscii(runtime, "getReferenceCount"), |
| 205 | + 0, |
| 206 | + [weakShadowNode]( |
| 207 | + jsi::Runtime&, const jsi::Value&, const jsi::Value*, size_t) |
| 208 | + -> jsi::Value { return {(int)weakShadowNode.use_count()}; }); |
| 209 | +} |
| 210 | + |
| 211 | +jsi::Function NativeFantom::createShadowNodeRevisionGetter( |
| 212 | + jsi::Runtime& runtime, |
| 213 | + ShadowNode::Shared shadowNode) { |
| 214 | +#if RN_DEBUG_STRING_CONVERTIBLE |
| 215 | + auto weakShadowNode = std::weak_ptr<const ShadowNode>(shadowNode); |
| 216 | + |
| 217 | + return jsi::Function::createFromHostFunction( |
| 218 | + runtime, |
| 219 | + jsi::PropNameID::forAscii(runtime, "getRevision"), |
| 220 | + 0, |
| 221 | + [weakShadowNode]( |
| 222 | + jsi::Runtime& runtime, const jsi::Value&, const jsi::Value*, size_t) |
| 223 | + -> jsi::Value { |
| 224 | + if (auto strongShadowNode = weakShadowNode.lock()) { |
| 225 | + const auto& uiManager = |
| 226 | + UIManagerBinding::getBinding(runtime)->getUIManager(); |
| 227 | + |
| 228 | + const auto& currentRevision = |
| 229 | + *uiManager.getNewestCloneOfShadowNode(*strongShadowNode); |
| 230 | + return currentRevision.revision_; |
| 231 | + } else { |
| 232 | + return jsi::Value::null(); |
| 233 | + } |
| 234 | + }); |
| 235 | +#else |
| 236 | + // TODO(T225400348): Remove this when revision_ is available in optimised |
| 237 | + // builds. |
| 238 | + throw std::runtime_error( |
| 239 | + "createShadowNodeRevisionGetter() is only available in debug builds"); |
| 240 | +#endif |
| 241 | +} |
| 242 | + |
| 243 | +void NativeFantom::saveJSMemoryHeapSnapshot( |
| 244 | + jsi::Runtime& runtime, |
| 245 | + std::string filePath) { |
| 246 | + runtime.instrumentation().collectGarbage("heapsnapshot"); |
| 247 | + runtime.instrumentation().createSnapshotToFile(filePath); |
| 248 | +} |
| 249 | + |
| 250 | +} // namespace facebook::react |
0 commit comments