<commit_before>/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

#include "SurfaceRegistryBinding.h"
#include <react/renderer/debug/SystraceSection.h>
#include <react/renderer/uimanager/bindingUtils.h>
#include <react/renderer/uimanager/primitives.h>
#include "bindingUtils.h"

namespace facebook::react {

void SurfaceRegistryBinding::startSurface(
    jsi::Runtime &runtime,
    SurfaceId surfaceId,
    std::string const &moduleName,
    folly::dynamic const &initalProps,
    DisplayMode displayMode) {
  SystraceSection s("SurfaceRegistryBinding::startSurface");
  folly::dynamic parameters = folly::dynamic::object();
  parameters["rootTag"] = surfaceId;
  parameters["initialProps"] = initalProps;
  parameters["fabric"] = true;

  auto global = runtime.global();
  auto isBridgeless = global.hasProperty(runtime, "RN$Bridgeless") &&
      global.getProperty(runtime, "RN$Bridgeless").asBool();

  if (isBridgeless) {
    if (!global.hasProperty(runtime, "RN$SurfaceRegistry")) {
      throw std::runtime_error(
          "SurfaceRegistryBinding::startSurface: Failed to start Surface \"" +
          moduleName + "\". global.RN$SurfaceRegistry was not installed.");
    }

    auto registry = global.getPropertyAsObject(runtime, "RN$SurfaceRegistry");
    auto method = registry.getPropertyAsFunction(runtime, "renderSurface");
    method.call(
        runtime,
        {jsi::String::createFromUtf8(runtime, moduleName),
         jsi::valueFromDynamic(runtime, parameters),
         jsi::Value(runtime, displayModeToInt(displayMode))});
  } else {
    callMethodOfModule(
        runtime,
        "AppRegistry",
        "runApplication",
        {jsi::String::createFromUtf8(runtime, moduleName),
         jsi::valueFromDynamic(runtime, parameters),
         jsi::Value(runtime, displayModeToInt(displayMode))});
  }
}

void SurfaceRegistryBinding::setSurfaceProps(
    jsi::Runtime &runtime,
    SurfaceId surfaceId,
    std::string const &moduleName,
    folly::dynamic const &initalProps,
    DisplayMode displayMode) {
  SystraceSection s("UIManagerBinding::setSurfaceProps");
  folly::dynamic parameters = folly::dynamic::object();
  parameters["rootTag"] = surfaceId;
  parameters["initialProps"] = initalProps;
  parameters["fabric"] = true;

  auto global = runtime.global();
  auto isBridgeless = global.hasProperty(runtime, "RN$Bridgeless") &&
      global.getProperty(runtime, "RN$Bridgeless").asBool();

  if (isBridgeless) {
    if (!global.hasProperty(runtime, "RN$SurfaceRegistry")) {
      throw std::runtime_error(
          "SurfaceRegistryBinding::setSurfaceProps: Failed to set Surface props for \"" +
          moduleName + "\". global.RN$SurfaceRegistry was not installed.");
    }

    auto registry = global.getPropertyAsObject(runtime, "RN$SurfaceRegistry");
    auto method = registry.getPropertyAsFunction(runtime, "setSurfaceProps");

    method.call(
        runtime,
        {jsi::String::createFromUtf8(runtime, moduleName),
         jsi::valueFromDynamic(runtime, parameters),
         jsi::Value(runtime, displayModeToInt(displayMode))});
  } else {
    callMethodOfModule(
        runtime,
        "AppRegistry",
        "setSurfaceProps",
        {jsi::String::createFromUtf8(runtime, moduleName),
         jsi::valueFromDynamic(runtime, parameters),
         jsi::Value(runtime, displayModeToInt(displayMode))});
  }
}
<commit_msg>Unbreak SurfaceRegistry users in bridge mode<commit_after>/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

#include "SurfaceRegistryBinding.h"
#include <react/renderer/debug/SystraceSection.h>
#include <react/renderer/uimanager/bindingUtils.h>
#include <react/renderer/uimanager/primitives.h>
#include "bindingUtils.h"

namespace facebook::react {

void SurfaceRegistryBinding::startSurface(
    jsi::Runtime &runtime,
    SurfaceId surfaceId,
    std::string const &moduleName,
    folly::dynamic const &initalProps,
    DisplayMode displayMode) {
  SystraceSection s("SurfaceRegistryBinding::startSurface");
  folly::dynamic parameters = folly::dynamic::object();
  parameters["rootTag"] = surfaceId;
  parameters["initialProps"] = initalProps;
  parameters["fabric"] = true;

  auto global = runtime.global();
  auto isBridgeless = global.hasProperty(runtime, "RN$Bridgeless") &&
      global.getProperty(runtime, "RN$Bridgeless").asBool();

  if (isBridgeless) {
    if (!global.hasProperty(runtime, "RN$SurfaceRegistry")) {
      throw std::runtime_error(
          "SurfaceRegistryBinding::startSurface: Failed to start Surface \"" +
          moduleName + "\". global.RN$SurfaceRegistry was not installed.");
    }

    auto registry = global.getPropertyAsObject(runtime, "RN$SurfaceRegistry");
    auto method = registry.getPropertyAsFunction(runtime, "renderSurface");
    method.call(
        runtime,
        {jsi::String::createFromUtf8(runtime, moduleName),
         jsi::valueFromDynamic(runtime, parameters),
         jsi::Value(runtime, displayModeToInt(displayMode))});
  } else {
    if (moduleName != "LogBox" &&
        global.hasProperty(runtime, "RN$SurfaceRegistry")) {
      auto registry = global.getPropertyAsObject(runtime, "RN$SurfaceRegistry");
      auto method = registry.getPropertyAsFunction(runtime, "renderSurface");

      method.call(
          runtime,
          {jsi::String::createFromUtf8(runtime, moduleName),
           jsi::valueFromDynamic(runtime, parameters),
           jsi::Value(runtime, displayModeToInt(displayMode))});
    } else {
      callMethodOfModule(
          runtime,
          "AppRegistry",
          "runApplication",
          {jsi::String::createFromUtf8(runtime, moduleName),
           jsi::valueFromDynamic(runtime, parameters),
           jsi::Value(runtime, displayModeToInt(displayMode))});
    }
  }
}

void SurfaceRegistryBinding::setSurfaceProps(
    jsi::Runtime &runtime,
    SurfaceId surfaceId,
    std::string const &moduleName,
    folly::dynamic const &initalProps,
    DisplayMode displayMode) {
  SystraceSection s("UIManagerBinding::setSurfaceProps");
  folly::dynamic parameters = folly::dynamic::object();
  parameters["rootTag"] = surfaceId;
  parameters["initialProps"] = initalProps;
  parameters["fabric"] = true;

  auto global = runtime.global();
  auto isBridgeless = global.hasProperty(runtime, "RN$Bridgeless") &&
      global.getProperty(runtime, "RN$Bridgeless").asBool();

  if (isBridgeless) {
    if (!global.hasProperty(runtime, "RN$SurfaceRegistry")) {
      throw std::runtime_error(
          "SurfaceRegistryBinding::setSurfaceProps: Failed to set Surface props for \"" +
          moduleName + "\". global.RN$SurfaceRegistry was not installed.");
    }

    auto registry = global.getPropertyAsObject(runtime, "RN$SurfaceRegistry");
    auto method = registry.getPropertyAsFunction(runtime, "setSurfaceProps");

    method.call(
        runtime,
        {jsi::String::createFromUtf8(runtime, moduleName),
         jsi::valueFromDynamic(runtime, parameters),
         jsi::Value(runtime, displayModeToInt(displayMode))});
  } else {
    if (moduleName != "LogBox" &&
        global.hasProperty(runtime, "RN$SurfaceRegistry")) {
      auto registry = global.getPropertyAsObject(runtime, "RN$SurfaceRegistry");
      auto method = registry.getPropertyAsFunction(runtime, "setSurfaceProps");

      method.call(
          runtime,
          {jsi::String::createFromUtf8(runtime, moduleName),
           jsi::valueFromDynamic(runtime, parameters),
           jsi::Value(runtime, displayModeToInt(displayMode))});
    } else {
      callMethodOfModule(
          runtime,
          "AppRegistry",
          "setSurfaceProps",
          {jsi::String::createFromUtf8(runtime, moduleName),
           jsi::valueFromDynamic(runtime, parameters),
           jsi::Value(runtime, displayModeToInt(displayMode))});
    }
  }
}

{'fraction_non_alphanumeric': 0.12600569377398194, 'fraction_numerical': 0.0012377769525931427, 'mean_word_length': 4.043695380774032, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6271254', 'n_tokens_mistral': 2326, 'n_tokens_neox': 2280, 'n_words': 449}