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

Code before:
    try coreModuleHolder.definition.decorate(object: coreObject, appContext: self)

    // Initialize `global.expo`.
    try runtime.initializeCoreObject(coreObject)

    // Install `global.expo.EventEmitter`.
    EXJavaScriptRuntimeManager.installEventEmitterClass(runtime)

    // Install `global.expo.SharedObject`.
    EXJavaScriptRuntimeManager.installSharedObjectClass(runtime) { [weak sharedObjectRegistry] objectId in
      sharedObjectRegistry?.delete(objectId)
    }

    // Install `global.expo.SharedRef`.
    EXJavaScriptRuntimeManager.installSharedRefClass(runtime)

    // Install `global.expo.NativeModule`.
    EXJavaScriptRuntimeManager.installNativeModuleClass(runtime)

    // Install the modules host object as the `global.expo.modules`.
    EXJavaScriptRuntimeManager.installExpoModulesHostObject(self)
  }

  @MainActor
  internal func prepareUIRuntime() throws {
    guard let runtime = uiRuntime else {
      throw Exceptions.RuntimeLost()
    }

    let coreObject = runtime.createObject()

    // Initialize `global.expo`.
    runtime.global().defineProperty(EXGlobalCoreObjectPropertyName, value: coreObject, options: .enumerable)

    // Install `global.expo.EventEmitter`.
    EXJavaScriptRuntimeManager.installEventEmitterClass(runtime)

    // Install `global.expo.NativeModule`.
    EXJavaScriptRuntimeManager.installNativeModuleClass(runtime)
  }

  /**
   Unsets runtime objects that we hold for each module.
   */
  @JavaScriptActor
  private func releaseRuntimeObjects() {
    sharedObjectRegistry.clear()
    classRegistry.clear()

    for module in moduleRegistry {
      module.javaScriptObject = nil

Review comment (suggestion):
Honestly, I would prefer not renaming it to `runtime`, it might be a bit confusing to which `runtime` the code refers too as both are properties of `self`

Code after:

    coreObject.defineProperty("__expo_app_identifier__", value: appIdentifier, options: [])

    try coreModuleHolder.definition.decorate(object: coreObject, appContext: self)

    // Initialize `global.expo`.
    try runtime.initializeCoreObject(coreObject)

    // Install `global.expo.EventEmitter`.
    EXJavaScriptRuntimeManager.installEventEmitterClass(runtime)

    // Install `global.expo.SharedObject`.
    EXJavaScriptRuntimeManager.installSharedObjectClass(runtime) { [weak sharedObjectRegistry] objectId in
      sharedObjectRegistry?.delete(objectId)
    }

    // Install `global.expo.SharedRef`.
    EXJavaScriptRuntimeManager.installSharedRefClass(runtime)

    // Install `global.expo.NativeModule`.
    EXJavaScriptRuntimeManager.installNativeModuleClass(runtime)

    // Install the modules host object as the `global.expo.modules`.
    EXJavaScriptRuntimeManager.installExpoModulesHostObject(self)
  }

  @MainActor
  internal func prepareUIRuntime() throws {
    let uiRuntime = try uiRuntime
    let coreObject = uiRuntime.createObject()

    // Initialize `global.expo`.
    uiRuntime.global().defineProperty(EXGlobalCoreObjectPropertyName, value: coreObject, options: .enumerable)

    // Install `global.expo.EventEmitter`.
    EXJavaScriptRuntimeManager.installEventEmitterClass(uiRuntime)

    // Install `global.expo.NativeModule`.
    EXJavaScriptRuntimeManager.installNativeModuleClass(uiRuntime)
  }

  /**
   Unsets runtime objects that we hold for each module.
   */
  @JavaScriptActor
  private func releaseRuntimeObjects() {
    sharedObjectRegistry.clear()
    classRegistry.clear()

    for module in moduleRegistry {
      module.javaScriptObject = nil