Repository: expo/expo
File: packages/expo-modules-core/ios/Core/Modules/CoreModule.swift (Swift)

Code before:
    Function("installOnUIRuntime") {
      guard let appContext else {
        throw Exceptions.AppContextLost()
      }

      // Check if was already installed
      if appContext._uiRuntime != nil {
        return
      }

      let runtime = try appContext.runtime
      if !runtime.global().hasProperty(WORKLET_RUNTIME_KEY) {
        throw WorkletUIRuntimeException()
      }

      let pointerHolder = runtime.global().getProperty(WORKLET_RUNTIME_KEY)
      if !pointerHolder.isObject() {
        throw WorkletUIRuntimeException()
      }

      let uiRuntimePointer = WorkletRuntimeFactory.extractRuntimePointer(pointerHolder, runtime: runtime)
      if uiRuntimePointer == nil {
        throw WorkletUIRuntimePointerExtractionException()
      }

      DispatchQueue.main.sync {
        let workletRuntime = WorkletRuntimeFactory.createWorkletRuntime(appContext, fromPointer: uiRuntimePointer)
        appContext._uiRuntime = workletRuntime
      }
    }

    // Expose some common classes and maybe even the `modules` host object in the future.
    Function("uuidv4") { () -> String in
      return UUID().uuidString.lowercased()
    }

    Function("uuidv5") { (name: String, namespace: String) -> String in
      guard let namespaceUuid = UUID(uuidString: namespace) else {
        throw InvalidNamespaceException(namespace)
      }

      return uuidv5(name: name, namespace: namespaceUuid).uuidString.lowercased()
    }

    // swiftlint:disable:next unused_closure_parameter
    Function("getViewConfig") { (moduleName: String, viewName: String?) -> [String: Any]? in
      var validAttributes: [String: Any] = [:]
      var directEventTypes: [String: Any] = [:]
      let moduleHolder = appContext?.moduleRegistry.get(moduleHolderForName: getHolderName(moduleName))

      guard let viewDefinition = moduleHolder?.definition.views[viewName ?? DEFAULT_MODULE_VIEW] else {

Review comment (bug):
What happens if I call `installOnUIRuntime` from the UI runtime? 🤓 I know, it will exit earlier, but in theory this call could deadlock if called from the main queue. We could use our utility function `performSynchronouslyOnMainActor` instead.

Code after:
    Function("installOnUIRuntime") {
      guard let appContext else {
        throw Exceptions.AppContextLost()
      }

      // Check if was already installed
      if appContext._uiRuntime != nil {
        return
      }

      let runtime = try appContext.runtime
      if !runtime.global().hasProperty(WORKLET_RUNTIME_KEY) {
        throw WorkletUIRuntimeException()
      }

      let pointerHolder = runtime.global().getProperty(WORKLET_RUNTIME_KEY)
      if !pointerHolder.isObject() {
        throw WorkletUIRuntimeException()
      }

      let uiRuntimePointer = WorkletRuntimeFactory.extractRuntimePointer(pointerHolder, runtime: runtime)
      if uiRuntimePointer == nil {
        throw WorkletUIRuntimePointerExtractionException()
      }

      let block = {
        let workletRuntime = WorkletRuntimeFactory.createWorkletRuntime(appContext, fromPointer: uiRuntimePointer)
        appContext._uiRuntime = workletRuntime
      }

      if Thread.isMainThread {
        block()
      } else {
        DispatchQueue.main.sync {
          block()
        }
      }
    }

    // Expose some common classes and maybe even the `modules` host object in the future.
    Function("uuidv4") { () -> String in
      return UUID().uuidString.lowercased()
    }

    Function("uuidv5") { (name: String, namespace: String) -> String in
      guard let namespaceUuid = UUID(uuidString: namespace) else {
        throw InvalidNamespaceException(namespace)
      }

      return uuidv5(name: name, namespace: namespaceUuid).uuidString.lowercased()
    }