import SwiftSyntax

/// A `@JS func` collected for **direct JSI binding**. Instead of describing the function with a
/// `Function(...)` / `AsyncFunction(...)` DSL entry that the runtime interprets per call, the enclosing
/// macro synthesizes a decorator (`_decorateModule(object:)` / `_decorateSharedObject(prototype:)`) that binds each such
/// function into the JS object via the closure-taking `JavaScriptObject.setProperty(_:)`, with the
/// decode-call-encode body inlined into the closure. This omits the dynamic-call path entirely:
/// every argument is decoded individually by its static type.
///
/// The receiver (see `Receiver`) is the module's `self` for a module binding, or the per-call `_self`
/// unwrapped from the JS `this` for a shared-object binding. An `async` `@JS func` binds through the
/// two-phase async `setProperty(_:)` overload: the closure is the synchronous decode phase (receiver
/// unwrap, arity guard, argument decode, all while `this` and the arguments are still valid)
/// and returns the async body (call and result encode), so nothing JSI-owned crosses the asynchronous
/// boundary and JS gets a promise.
///
/// A synchronous function marked `@JS(.withAsync("name"))` also binds a promise-returning variant
/// under that name (see `asyncVariant`). It decodes and encodes like an `async` function, and calls
/// the function on the concurrent pool.
///
/// A synchronous function can take the JavaScript runtime as its last parameter,
/// `in runtime: borrowing JavaScriptRuntime`. JavaScript doesn't see it: the binding passes the runtime
/// of the call.
///
/// A function marked `@JS(symbol: ...)` binds under a symbol instead of a string name (see
/// `JSMemberKey`).
internal struct JSFunction {
  let swiftName: String
  /// The key that the function binds under.
  private(set) var key: JSMemberKey
  /// The parameters that take JavaScript arguments, without the runtime parameter.
  let parameters: [FunctionParameterSyntax]
  /// The label of the parameter that takes the JavaScript runtime, or `nil` when the function has none.
  private let runtimeLabel: String?
  /// The declared return type as written, or `nil` when the function returns `Void`/nothing.
  let returnType: String?
  /// How the return value encodes when its type mentions `Any`, or `nil` for any other type.
  let returnFreeForm: FreeFormConversion?
  let isThrowing: Bool
  private(set) var isAsync: Bool
  /// The function's name in the source, where a JavaScript name collision is reported.
  private(set) var nameToken: TokenSyntax
  /// The name from `.withAsync("name")` and its token, or `nil` when the attribute doesn't list it.
  private var asyncVariantName: (name: String, token: TokenSyntax)?
  /// True for the binding `asyncVariant` returns: it calls the synchronous function on the concurrent
  /// pool instead of on the JavaScript thread.
  private(set) var isAsyncVariant = false

  init(funcDecl: FunctionDeclSyntax, attribute: AttributeSyntax) {
    self.swiftName = funcDecl.name.text
    self.key = JSMemberKey(attribute: attribute, defaultName: withoutBackticks(funcDecl.name.text))
    self.nameToken = funcDecl.name
    let declaredParameters = funcDecl.signature.parameterClause.parameters
    self.parameters = declaredParameters.filter { !isRuntimeParameter($0) }
    self.runtimeLabel = declaredParameters.first(where: isRuntimeParameter)?.firstName.text

    let declaredReturnType = funcDecl.signature.returnClause?.type
    self.returnType = isVoidType(declaredReturnType) ? nil : declaredReturnType?.trimmedDescription
    self.returnFreeForm = declaredReturnType.flatMap(freeFormConversion(of:))

    let effectSpecifiers = funcDecl.signature.effectSpecifiers
    self.isThrowing = effectSpecifiers?.throwsClause?.throwsSpecifier != nil
    self.isAsync = effectSpecifiers?.asyncSpecifier != nil
    self.asyncVariantName = asyncVariantOption(of: attribute).flatMap(asyncVariantName(of:))
  }

  /// The promise-returning binding that `.withAsync("name")` adds next to a synchronous function, or
  /// `nil` when the attribute doesn't list it. An `async` function gets none: `@JS` reports the option
  /// there.
  var asyncVariant: JSFunction? {
    guard let asyncVariantName, !isAsync else {
      return nil
    }
    var variant = self
    variant.key = .name(asyncVariantName.name)
    variant.nameToken = asyncVariantName.token
    variant.isAsync = true
    variant.isAsyncVariant = true
    variant.asyncVariantName = nil
    return variant
  }

  /// The number of leading parameters that must always be supplied: the total minus the maximal
  /// trailing run of *omittable* parameters (each having a default value or an optional type). A
  /// non-omittable parameter part-way through stops the run, since arguments are positional — a
  /// required parameter after an omittable one forces the earlier one to be supplied too.
  private var requiredArgumentCount: Int {
    var required = parameters.count
    for parameter in parameters.reversed() {
      guard isOmittable(parameter) else {
        break
      }
      required -= 1
    }
    return required
  }

  /// The decode-call-encode statements that form the host-function body, indented with the given
  /// prefix. Receiver unwrap (shared objects only), then an arity guard (an exact check when every
  /// parameter is required, otherwise a range check) throwing `Exceptions.ArgumentsRangeMismatch`;
  /// then the decode of the always-present required prefix via `JavaScriptDecodable.decode` on a
  /// zero-copy `arguments.unownedValue(at:)`; then the call and result encode via
  /// `JavaScriptEncodable.encode`. When a trailing run of parameters is omittable the call branches
  /// on `arguments.count`, decoding only the slots that branch actually has — `unownedValue(at:)` is
  /// unchecked, so a slot the caller didn't pass is never indexed.
  private func bodyStatements(receiver: Receiver, indent: String) -> String {
    let required = requiredArgumentCount
    let maximum = parameters.count
    var lines: [String] = []

    if let unwrap = receiver.unwrapStatement {
      lines.append(unwrap)
    }

    if required == maximum {
      lines.append(
        """
        guard arguments.count == \(maximum) else {
          throw Exceptions.ArgumentsRangeMismatch((functionName: \(key.functionNameLiteral), received: arguments.count, required: \(required), maximum: \(maximum)))
        }
        """
      )
    } else {
      lines.append(
        """
        guard arguments.count >= \(required) && arguments.count <= \(maximum) else {
          throw Exceptions.ArgumentsRangeMismatch((functionName: \(key.functionNameLiteral), received: arguments.count, required: \(required), maximum: \(maximum)))
        }
        """
      )
    }

    // Decode the required prefix once — these slots are present in every accepted arity, so the
    // decode is shared rather than repeated per branch.
    for index in 0..<required {
      lines.append(contentsOf: decodeLines(at: index))
    }

    if required == maximum {
      // No omittable trailing run: a single flat call with every argument decoded. An async
      // function decodes any remaining slots here (still the synchronous phase) and returns its
      // body; a sync one calls and encodes directly.
      if isAsync {
        for index in required..<maximum {
          lines.append(contentsOf: decodeLines(at: index))
        }
        lines.append(contentsOf: asyncBodyLines(receiver: receiver, arity: maximum))
      } else {
        lines.append(contentsOf: callAndEncodeLines(receiver: receiver, arity: maximum, decodingFrom: required))
      }
    } else if isAsync {
      // One body per accepted arity, branching on `arguments.count`. A branch decodes its trailing
      // slots synchronously and returns the async body for that call shape, so each `return` ends
      // the decode phase for its arity.
      lines.append("switch arguments.count {")
      for arity in required...maximum {
        let label = arity == maximum ? "default:" : "case \(arity):"
        lines.append(label)
        for index in required..<arity {
          lines.append(contentsOf: decodeLines(at: index).map { "  " + $0 })
        }
        lines.append(contentsOf: asyncBodyLines(receiver: receiver, arity: arity).map { "  " + $0 })
      }
      lines.append("}")
    } else {
      // One call shape per accepted arity, branching on `arguments.count`. A branch decodes a
      // trailing slot before the call, so this is a `switch` statement (not an expression): a
      // value-returning function declares `value` up front and each branch assigns it.
      if let returnType {
        lines.append("let value: \(expressionType(returnType))")
      }
      lines.append("switch arguments.count {")
      for arity in required...maximum {
        let label = arity == maximum ? "default:" : "case \(arity):"
        lines.append(label)
        for index in required..<arity {
          lines.append(contentsOf: decodeLines(at: index).map { "  " + $0 })
        }
        let assignment = returnType != nil ? "value = " : ""
        lines.append("  \(assignment)\(callExpression(receiver: receiver, arity: arity))")
      }
      lines.append("}")
      lines.append(contentsOf: encodeResultLines())
    }

    return
      lines
      .flatMap { $0.split(separator: "\n", omittingEmptySubsequences: false) }
      .map { indent + $0 }
      .joined(separator: "\n")
  }

  /// The `return { … }` statement closing an async binding's decode phase: the returned closure is
  /// the function's async body (`AsyncFunctionBody`), awaiting the call and encoding the result. It
  /// captures only what the call needs — the decoded `arg<i>` locals, the receiver, and `runtime`
  /// for the encode — never `this` or the arguments buffer, which are only valid for the duration
  /// of the host call. The `asyncVariant` binding awaits its call on the concurrent pool instead.
  private func asyncBodyLines(receiver: Receiver, arity: Int) -> [String] {
    var lines: [String] = ["return {"]
    let call =
      isAsyncVariant
      ? concurrentCallExpression(receiver: receiver, arity: arity)
      : callExpression(receiver: receiver, arity: arity)
    // The concurrent call spans several lines, so each of its lines is indented into the body.
    let indented = { (statement: String) in
      statement.split(separator: "\n", omittingEmptySubsequences: false).map { "  " + $0 }
    }
    if returnType != nil {
      lines.append(contentsOf: indented("let result = \(call)"))
      lines.append(contentsOf: encodeResultLines().map { "  " + $0 })
    } else {
      lines.append(contentsOf: indented(call))
      lines.append("  return .undefined()")
    }
    lines.append("}")
    return lines
  }

  /// The lines binding `arg<index>`: a decode of the slot at `index` by its static type through
  /// `JavaScriptDecodable.decode` on the borrowed `JavaScriptUnownedValue` — no owning value, no
  /// `jsi::Value` copy, no `Any` boxing, no force-cast; it returns the concrete type directly. A
  /// primitive's `decode` is `@inlinable` and lowers to the same direct accessor a hand-rolled fast
  /// path would use. A closure parameter takes several lines instead, wrapping the JS function.
  private func decodeLines(at index: Int) -> [String] {
    return argumentDecodeStatements(
      type: parameters[index].type,
      into: "arg\(index)",
      from: "arguments.unownedValue(at: \(index))"
    )
  }

  /// The `<callee>.<name>(...)` call for the given arity, with the arguments from `callArguments`.
  private func callExpression(receiver: Receiver, arity: Int) -> String {
    // `try` only for a throwing function. An `async` call that doesn't throw must not get one, or the
    // expansion warns that no throwing call occurs within the `try` expression.
    let tryKeyword = isThrowing ? "try " : ""
    let awaitKeyword = isAsync ? "await " : ""
    return "\(tryKeyword)\(awaitKeyword)\(receiver.callee).\(swiftName)(\(callArguments(arity: arity)))"
  }

  /// The call of the `asyncVariant` binding for the given arity: the synchronous function runs in a
  /// `@concurrent` closure, so it leaves the JavaScript thread, and the body continues once it returns.
  private func concurrentCallExpression(receiver: Receiver, arity: Int) -> String {
    // The closure captures the receiver and the decoded arguments, so Swift 6 requires them to be
    // `Sendable`, as it does for the receiver and arguments of a `.concurrent` call. The signature is
    // spelled out because `@concurrent` needs an `async` closure, and a body without `await` would
    // infer a synchronous one.
    let tryKeyword = isThrowing ? "try " : ""
    let effects = isThrowing ? "async throws" : "async"
    let returnClause = returnType.map { " -> \(expressionType($0))" } ?? ""
    return """
      \(tryKeyword)await { @concurrent () \(effects)\(returnClause) in
        \(tryKeyword)\(receiver.callee).\(swiftName)(\(callArguments(arity: arity)))
      }()
      """
  }

  /// The argument list of a call for the given arity. Slots `0..<arity` are passed their decoded
  /// `arg<i>`; a trailing optional-without-default slot that this arity omits is passed `nil`; a
  /// trailing defaulted slot that this arity omits is dropped entirely so Swift applies its default.
  private func callArguments(arity: Int) -> String {
    var callArguments: [String] = []
    for (index, parameter) in parameters.enumerated() {
      let label = parameter.firstName.text
      let value: String?
      if index < arity {
        value = "arg\(index)"
      } else if hasDefaultValue(parameter) {
        // Omitted defaulted slot: drop it from the call so Swift fills in the default.
        value = nil
      } else {
        // Omitted optional-without-default slot: pass `nil`.
        value = "nil"
      }
      guard let value else {
        continue
      }
      callArguments.append(label == "_" ? value : "\(label): \(value)")
    }
    if let runtimeLabel {
      callArguments.append(runtimeLabel == "_" ? "runtime" : "\(runtimeLabel): runtime")
    }
    return callArguments.joined(separator: ", ")
  }

  /// The flat (single-arity) call-and-encode lines used when no trailing parameter is omittable:
  /// `let value = <callee>.f(...)` then the encode (or just `<callee>.f(...)` for a function
  /// that returns nothing).
  private func callAndEncodeLines(receiver: Receiver, arity: Int, decodingFrom: Int) -> [String] {
    var lines: [String] = []
    for index in decodingFrom..<arity {
      lines.append(contentsOf: decodeLines(at: index))
    }
    if returnType != nil {
      lines.append("let value = \(callExpression(receiver: receiver, arity: arity))")
      lines.append(contentsOf: encodeResultLines())
    } else {
      lines.append(callExpression(receiver: receiver, arity: arity))
    }
    return lines
  }

  /// Encode the `result` local back to JS and return it through `JavaScriptEncodable.encode` (or, for a
  /// type that mentions `Any`, the matching `JavaScriptValue` free-form method). Unlike the decode side
  /// there's no primitive fast path: `encode` produces the same value as the primitive's
  /// `toJavaScriptValue(in:)` except for `Int`/`UInt`, where it range-checks
  /// and throws instead of silently encoding an out-of-safe-range value as a lossy number — the
  /// catchable error is the right behavior, and matches how non-primitive integers already encode. A
  /// no-return function has nothing to encode, so its binding closure leaves the result slot empty (see
  /// `decorateStatements(object:receiver:)`). A synchronous function encodes straight into the
  /// engine's result slot, so a number, a boolean or a string never becomes a `JavaScriptValue`.
  ///
  /// For an `async` function the encode must run on the JS thread. An `async` body may suspend and
  /// resume on an arbitrary cooperative-pool thread, and encoding a heap-allocated JS value (a string,
  /// object, array, or typed array) off the JS thread races the engine's garbage collector and
  /// corrupts the heap. `runtime.execute` hops the encode onto the JS thread, running inline when it is
  /// already there (the common case, where the body never truly suspended), so synchronous functions —
  /// which always run on the JS thread — keep encoding directly without the wrapper.
  private func encodeResultLines() -> [String] {
    guard let returnType else {
      return []
    }
    if isAsync {
      let encode = "try \(encodeCall(returnType, freeForm: returnFreeForm, of: "result"))"
      return [
        "return try await runtime.execute {",
        "  return \(encode)",
        "}",
      ]
    }
    return [encodeIntoSlotStatement(returnType, freeForm: returnFreeForm, of: "value")]
  }

  /// The `setProperty` statement that installs this function on the JS object. The decode-call-encode
  /// body is inlined directly into the closure passed to the closure-taking `setProperty` overload
  /// (which creates the host function under the hood) — no separate named binding. For an `async`
  /// function the closure is the synchronous decode phase and returns the async body, which selects
  /// the async `setProperty` overload (so JS receives a promise).
  ///
  /// A module captures its `self` **strong** —
  /// the host-function closure is what keeps the native callable alive for as long as JS can invoke
  /// it; its lifetime is bounded by the JS VM's garbage collection of the object. A shared object
  /// captures nothing of the instance: it recovers the typed receiver from the JS `this` per call.
  ///
  /// A function bound under a symbol gets the symbol from the runtime first, so the statement is
  /// marked `try`.
  /// Whether the body of a synchronous binding uses the runtime: to decode an argument, to unwrap a shared
  /// object from `this`, or to pass it to a function that takes the runtime.
  private func bodyUsesRuntime(receiver: Receiver) -> Bool {
    if runtimeLabel != nil || !parameters.isEmpty {
      return true
    }
    if case .sharedObject = receiver {
      return true
    }
    return false
  }

  func decorateStatements(object: String, receiver: Receiver) -> String {
    // Every `@JS` binding hands `this` in as a borrowed `JavaScriptUnownedValue` instead of
    // allocating an owning `JavaScriptValue` and forming its `weak`-runtime reference on every call,
    // and consumes the arguments buffer — sync and async closures share one parameter shape and
    // differ only in what they return. A module ignores `this`; a shared object unwraps it (still
    // borrowed) — an async binding does so in its synchronous decode phase, before the borrow ends.
    // The parameter list is parenthesized and fully typed, since Swift rejects a type annotation on
    // a shorthand `{ [capture] name, name in }` parameter.
    // A sync function binds through the result-slot overload (`JavaScriptRuntime.HostFunctionClosure`):
    // it encodes its result into the slot and gets the runtime as a parameter. A sync function that
    // returns nothing binds through the `Void` overload (`JavaScriptRuntime.VoidFunctionClosure`)
    // instead, which leaves JS's result as `undefined`. A call through it is faster than through a
    // result-slot closure that writes nothing. The closure declares `-> Void` because a body of several
    // statements without a `return` is otherwise ambiguous between the overloads. The `Void` overload
    // doesn't get the runtime, so it's only for a body that doesn't use it: a function that decodes
    // arguments, unwraps a shared object or takes the runtime binds through the result-slot overload even
    // when it returns nothing. Capturing the decorator's runtime instead would make the runtime own a
    // closure that retains it, so a runtime that owns its engine would never be freed.
    let captures = receiver.captureClause
    let parameters =
      isAsync
      ? "(this: borrowing JavaScriptUnownedValue, arguments: consuming JavaScriptValuesBuffer)"
      : returnType == nil && !bodyUsesRuntime(receiver: receiver)
        ? "(this: borrowing JavaScriptUnownedValue, arguments: consuming JavaScriptValuesBuffer) -> Void"
        : resultSlotClosureParameters

    return """
        \(key.tryPrefix)\(object).setProperty(\(key.expression)) { \(captures)\(parameters) in
      \(bodyStatements(receiver: receiver, indent: "    "))
        }
      """
  }
}

/// A `@JS var` collected for **direct JSI binding**. Instead of describing the property with a
/// `Property(...)` DSL entry, the enclosing macro synthesizes a get/set accessor into the JS object
/// inside its decorator (`_decorateModule(object:)` / `_decorateSharedObject(prototype:)`): it builds a descriptor object
/// (`enumerable` + `get`, and `set` when the property is settable) and installs it with
/// `object.defineProperty(name, descriptor:)`.
/// The `get`/`set` host functions are installed the same way `@JS func`s are — the closure-taking
/// `setProperty(_:)` overload, with the read/write body inlined into the closure.
///
/// The receiver (see `Receiver`) is the module's `self` for a module binding, or the per-call `_self`
/// unwrapped from the JS `this` for a shared object. The getter reads `<callee>.<name>` and the setter
/// writes `<callee>.<name> = …`. Decode/encode of the value go through `JavaScriptDecodable.decode` /
/// `JavaScriptEncodable.encode` (or the free-form methods for a type that mentions `Any`), the same
/// path as functions.
///
/// A `lazy var` and a getter-only computed `var` marked `@JS(.cached)` bind through
/// `JavaScriptObject.defineLazyProperty` instead (see `lazyPropertyStatements`). On the first read, its
/// getter defines the encoded value as a data property on the JS `this`, so later reads don't call into
/// native code. The data property goes on `this`, not on the object that holds the accessor, so each
/// runtime stores its own value, and so does each shared object instance. Two runtimes on different
/// threads can read the property for the first time at once. For a `lazy var`, that is a data race on
/// its Swift storage. A `.cached` property has no storage, so each runtime only runs the getter.
///
/// A property marked `@JS(symbol: ...)` binds under a symbol instead of a string name (see
/// `JSMemberKey`).
internal struct JSProperty {
  let swiftName: String
  /// The key that the property binds under.
  let key: JSMemberKey
  /// The property's value type as written, or `nil` when it couldn't be inferred (no annotation and
  /// no literal default). When `nil` the getter still works (the encode infers from `self.<name>`)
  /// but the setter uses an untyped closure parameter.
  let valueType: String?
  /// How the value converts when its type mentions `Any`, or `nil` for any other type.
  let freeForm: FreeFormConversion?
  /// Whether the property is settable from JS: `true` for a stored `var` or a computed `var` with an
  /// explicit `set` accessor; `false` for a getter-only computed `var`, a `lazy var`, a `.cached`
  /// property or a `let`.
  let isSettable: Bool
  /// Whether the getter replaces the accessor with the first value: `true` for a `lazy var` and for a
  /// property marked `@JS(.cached)`.
  let cachesValue: Bool
  /// Whether the `get` accessor is marked `throws`, so reading the property needs `try`.
  let getterThrows: Bool
  /// The property's name in the source, where a JavaScript name collision is reported.
  let nameToken: TokenSyntax

  /// The statements that install this property's accessor on the JS object, indented for the
  /// decorator body. Builds a descriptor object (`enumerable` + `get`, and `set` when settable) via
  /// the closure-taking `setProperty(_:)` overload — with the read/write body inlined into each
  /// closure — and installs it with `object.defineProperty(name, descriptor:)`. Capture matches the
  /// function bindings: a module captures `self` strong, a shared object captures nothing of the
  /// instance. Getter and setter are gated independently. A property whose getter caches the value binds
  /// through `lazyPropertyStatements`.
  func decorateStatements(object: String, receiver: Receiver) -> String {
    if cachesValue {
      return lazyPropertyStatements(object: object, receiver: receiver)
    }
    let descriptorName = derivedIdentifier(from: swiftName, suffix: "Descriptor")
    let callee = receiver.callee
    // A shared object's accessors unwrap the JS `this` into `_self` before reading/writing; a module
    // reads `self` directly. The unwrap leads each accessor body. Property accessors are synchronous, so
    // they take the borrowed unowned `this`.
    let unwrap = receiver.unwrapStatement.map { "\($0)\n" } ?? ""
    var lines: [String] = []

    lines.append("let \(descriptorName) = runtime.createObject()")
    lines.append("\(descriptorName).setProperty(\"enumerable\", value: true)")

    // Getter: read `<callee>.<name>` and encode it straight into the engine's result slot. When the
    // value type couldn't be inferred (no annotation and no literal default, rare for a stored var)
    // there's no static type to call `encode` on, so the value's own `toJavaScriptValue(in:)` is the
    // fallback.
    let getBody: String
    if let valueType {
      getBody = encodeIntoSlotStatement(valueType, freeForm: freeForm, of: "\(callee).\(swiftName)")
    } else {
      getBody = "result.emplace(\(callee).\(swiftName).toJavaScriptValue(in: runtime))"
    }
    lines.append(
      accessorClosure(descriptorName, "get", receiver: receiver, body: "\(unwrap)\(getBody)")
    )

    // Setter: decode argument 0 by the static type through `decode` and write `<callee>.<name>`. A
    // typed setter needs a known value type; when the type couldn't be inferred the property is bound
    // getter-only (a settable var with neither an annotation nor a literal default is rare and can't
    // be decoded).
    if isSettable, let valueType {
      let decode = decodeCall(valueType, freeForm: freeForm, from: "arguments.unownedValue(at: 0)")
      let setDecode = "\(callee).\(swiftName) = try \(decode)"
      lines.append(
        accessorClosure(descriptorName, "set", receiver: receiver, body: "\(unwrap)\(setDecode)")
      )
    }

    lines.append("\(key.tryPrefix)\(object).defineProperty(\(key.expression), descriptor: \(descriptorName))")

    return
      lines
      .flatMap { $0.split(separator: "\n", omittingEmptySubsequences: false) }
      .map { "  " + $0 }
      .joined(separator: "\n")
  }

  /// The `defineLazyProperty` statement of a property whose getter caches the value. The factory returns
  /// the value for `defineLazyProperty` to encode. A type that has no `encode` of its own (one that
  /// mentions `Any` or holds a tuple, or a type that couldn't be inferred) is encoded in the factory,
  /// which returns the `JavaScriptValue`, and its `encode` passes it through. A shared object's factory
  /// unwraps the instance from `this` first. A property under a symbol passes a
  /// `JavaScriptSymbol.Shared` (see `JSMemberKey.lazyPropertyArgument`), and `defineLazyProperty` gets the
  /// symbol from the runtime, which can throw.
  ///
  /// The data property is enumerable only, so JavaScript can't change or delete it.
  private func lazyPropertyStatements(object: String, receiver: Receiver) -> String {
    let value = "\(receiver.callee).\(swiftName)"
    let returnType: String
    let result: String
    if let valueType, freeForm == nil, tupleConversion(of: valueType) == nil {
      returnType = expressionType(valueType)
      // The other branches read the value inside a `try` expression already. Only a computed property can
      // have a throwing getter, so the untyped branch, which only a `lazy var` reaches, can't need it.
      result = getterThrows ? "try \(value)" : value
    } else if let valueType {
      returnType = "JavaScriptValue"
      result = "try \(encodeCall(valueType, freeForm: freeForm, of: value))"
    } else {
      returnType = "JavaScriptValue"
      result = "\(value).toJavaScriptValue(in: runtime)"
    }
    let body = receiver.unwrapStatement.map { "\($0)\n    return \(result)" } ?? result
    // The factory gets the runtime as a parameter, which shadows the decorator's `runtime`, so it doesn't
    // capture it (see `resultSlotClosureParameters`).
    let factoryParameters =
      "(this: borrowing JavaScriptUnownedValue, runtime: borrowing JavaScriptRuntime) -> \(returnType)"
    return """
        \(key.tryPrefix)\(object).defineLazyProperty(\(key.lazyPropertyArgument), options: [.enumerable]) { \(receiver.captureClause)\(factoryParameters) in
          \(body)
        }
      """
  }

  /// One `descriptor.setProperty("get"/"set") { … }` accessor entry. The capture list follows the
  /// receiver: a module captures `self` strong; a shared object captures nothing of the instance.
  private func accessorClosure(
    _ descriptorName: String,
    _ key: String,
    receiver: Receiver,
    body: String
  ) -> String {
    let captures = receiver.captureClause
    // Indent each line of a (possibly multi-line) body to sit one level inside the closure; a bare
    // `\(body)` interpolation would only indent the first line.
    let indentedBody =
      body
      .split(separator: "\n", omittingEmptySubsequences: false)
      .map { "  \($0)" }
      .joined(separator: "\n")
    // Property `get`/`set` accessors are always synchronous, so they bind through the result-slot
    // `setProperty` overload like sync functions. The parameter list is parenthesized and fully typed
    // because Swift rejects a type annotation on a shorthand closure parameter. A module ignores `this`;
    // a shared object unwraps it in the body. A setter writes nothing, so JS gets `undefined`.
    return """
      \(descriptorName).setProperty("\(key)") { \(captures)\(resultSlotClosureParameters) in
      \(indentedBody)
      }
      """
  }
}

/// The parameter list of a binding closure that writes its result into the engine's result slot
/// (`JavaScriptRuntime.HostFunctionClosure`). The closure gets the runtime as a parameter, which shadows
/// the decorator's `runtime`, so it doesn't capture it.
private let resultSlotClosureParameters =
  "(this: borrowing JavaScriptUnownedValue, arguments: consuming JavaScriptValuesBuffer, "
  + "result: borrowing JavaScriptValue.Slot, runtime: borrowing JavaScriptRuntime)"

/// The statement that encodes `valueExpression` of a boundary type into the result slot `result`. A type
/// that mentions `Any` has no `encode(_:into:in:)`, so its free-form encode result is moved into the slot,
/// and a type that holds a tuple is encoded into the slot by the tuple methods.
private func encodeIntoSlotStatement(_ type: String, freeForm: FreeFormConversion?, of valueExpression: String)
  -> String
{
  if freeForm != nil {
    return "result.emplace(try \(encodeCall(type, freeForm: freeForm, of: valueExpression)))"
  }
  if let tuple = tupleConversion(of: type) {
    return "try \(tuple.encodeCall(of: valueExpression, into: "result"))"
  }
  return "try \(expressionType(type)).encode(\(valueExpression), into: result, in: runtime)"
}

/// The body shared by every decorator phase: every `@JS func` bound via an inlined `setProperty`
/// closure and every `@JS var` via a `defineProperty` accessor, joined for the function body. `object`
/// is the local the bindings decorate (matching the entry point's argument label); `receiver` selects
/// how each binding reaches its Swift value (module `self`, shared-object instance `_self`, or the
/// metatype for a static member).
///
/// `replacesInherited` first defines the name of each function as a writable data property on the
/// object: an object up its prototype chain may hold an accessor with that name, which an assignment
/// would call instead of adding the function to the object. A function bound under a symbol that an
/// object up the prototype chain holds as a read-only property or as an accessor without a setter is
/// defined first too, since an assignment can't shadow such a property (see
/// `JSMemberKey.isInheritedReadOnly`).
private func decorateBody(
  functions: [JSFunction],
  properties: [JSProperty],
  object: String,
  receiver: Receiver,
  replacesInherited: Bool = false
) -> String {
  let functionBody = functions.map { function in
    let binding = function.decorateStatements(object: object, receiver: receiver)
    guard replacesInherited || function.key.isInheritedReadOnly else {
      return binding
    }
    let redefine =
      "  \(function.key.tryPrefix)\(object).defineProperty(\(function.key.expression), descriptor: .init(configurable: true, enumerable: true, writable: true))"
    return redefine + "\n" + binding
  }
  let propertyBody = properties.map {
    $0.decorateStatements(object: object, receiver: receiver)
  }
  return (functionBody + propertyBody).joined(separator: "\n")
}

/// The module decorator: a `_decorateModule(object:)` that binds every `@JS func` (via an inlined
/// `setProperty` closure) and every `@JS var` (via a `defineProperty` accessor) into the module's own
/// JS object. Core supplies the object; the bindings call into the module `self`. It's an *instance*
/// method (a module is a singleton, satisfying the `AnyModule` requirement of the same name), with a
/// `borrowing` object parameter (it mutates through the reference without reassigning or taking
/// ownership). Only emitted when there's at least
/// one member to bind. Uses the shared body generation with the module `object:` phase and `self`
/// receiver; the shared-object counterpart is `buildDecorateSharedObjectPhase`.
internal func buildDecorateJavaScriptObject(functions: [JSFunction], properties: [JSProperty]) -> DeclSyntax {
  let body = decorateBody(
    functions: functions,
    properties: properties,
    object: Phase.object.rawValue,
    receiver: .module
  )
  return """
    @JavaScriptActor
    public func _decorateModule(object: borrowing JavaScriptObject, in runtime: JavaScriptRuntime) throws {
    \(raw: body)
    }
    """
}

/// A shared-object decorator phase, a label overload of `_decorateSharedObject` overriding the matching
/// `open class func` on the `SharedObject` base so core can dispatch through the concrete type's
/// metatype. The `prototype:` overload binds the type's instance `@JS func`/`var` members onto the class
/// prototype (the original hook, unchanged); the `constructor:` overload binds the `static`/`class` ones
/// onto the constructor function itself (a new, additive overload, so introducing it isn't a breaking
/// core change). Both are *static* (a shared object has no singleton `self`). An instance binding
/// recovers its typed receiver from the JS `this` per call (`SharedObject.native(from:as:)`); a static
/// binding calls the Swift member on the type and ignores `this` (which, on the static side, is the
/// constructor). The constructor *object* the static members decorate is distinct from the `@JS init`,
/// which builds an instance and is emitted separately (see `JSConstructor.buildConstructor`). `access`
/// is the access modifier of the override, `open` in an `open` class and `public` otherwise.
///
/// Each phase binds only the members of its own class and doesn't call `super`. Core builds the
/// JavaScript class of a subclass on the JavaScript class of its superclass (`_jsSuperclassType`), so the
/// prototype and the constructor get the inherited members through their prototype chains, and a
/// member of the subclass shadows an inherited one with the same JavaScript name. The macro emits both
/// phases for every class, also when one binds nothing, so a class never runs the phase of its
/// superclass. With `replacesInherited`, which the macro passes when the superclass may be another
/// `@SharedObject` class, each function first defines its name as a data property (see
/// `decorateBody`).
internal func buildDecorateSharedObjectPhase(
  phase: Phase,
  functions: [JSFunction],
  properties: [JSProperty],
  typeName: String,
  access: String,
  replacesInherited: Bool
) -> DeclSyntax {
  let receiver: Receiver =
    phase == .constructor
    ? .staticMember(typeName: typeName)
    : .sharedObject(typeName: typeName)
  let body = decorateBody(
    functions: functions,
    properties: properties,
    object: phase.rawValue,
    receiver: receiver,
    replacesInherited: replacesInherited
  )
  // An empty phase gets an empty body, without a blank line in it.
  let statements = body.isEmpty ? "" : "\(body)\n"
  return """
    @JavaScriptActor
    \(raw: access) override class func _decorateSharedObject(\(raw: phase.rawValue): borrowing JavaScriptObject, in runtime: JavaScriptRuntime) throws {
    \(raw: statements)}
    """
}

/// True when a return clause is absent or written as `Void` / `()` — i.e. the function returns
/// nothing JS-visible, so the binding closure returns `Void`.
private func isVoidType(_ type: TypeSyntax?) -> Bool {
  guard let type else {
    return true
  }
  let text = type.trimmedDescription
  return text == "Void" || text == "()"
}
