-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
helpers for calling methods on JS objects
- Loading branch information
1 parent
e7c2679
commit b3a8a89
Showing
8 changed files
with
415 additions
and
24 deletions.
There are no files selected for viewing
314 changes: 314 additions & 0 deletions
314
Sources/Napoli/Generated/ObjectReferenceMethods.generated.swift
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
codegen/Sources/codegen/Generators/ObjectReferenceMethods.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import Foundation | ||
|
||
enum ObjectReferenceMethods { | ||
static func generate(maxParams: Int) throws -> Source { | ||
var source = Source() | ||
source.add(""" | ||
// swiftformat:disable opaqueGenericParameters | ||
import Foundation | ||
import NAPIC | ||
""") | ||
source.newline() | ||
|
||
for i in (0 ..< maxParams).reversed() { | ||
try generateExtension(source: &source, paramCount: i) | ||
} | ||
|
||
return source | ||
} | ||
|
||
static func generateExtension(source: inout Source, paramCount: Int) throws { | ||
let resultGeneric = Generic(type: "Result") | ||
let inGenerics = [Generic](prefix: "P", count: paramCount) | ||
let allGenerics = [resultGeneric] + inGenerics | ||
let allWheres = allGenerics.conforming(to: Types.valueConvertible).value | ||
let inWheres = inGenerics.conforming(to: Types.valueConvertible).value | ||
|
||
let commaSeparatedInGenerics = inGenerics.map(\.type).commaSeparated | ||
|
||
let inGenericsAsArgs: String | ||
if paramCount > 0 { | ||
inGenericsAsArgs = ", " + inGenerics.map { "_ \($0.type.lowercased()): \($0.type)" }.commaSeparated | ||
} else { | ||
inGenericsAsArgs = "" | ||
} | ||
|
||
let inGenericsAsCallArgs = inGenerics.map { $0.type.lowercased() }.commaSeparated.prefixCommaIfNotEmpty() | ||
|
||
source.add(""" | ||
// \(paramCount) param methods | ||
public extension ObjectReference { | ||
@available(*, noasync) | ||
func callThis\(allGenerics.bracketedOrNone)(_ env: Environment? = nil, _ name: String\(inGenericsAsArgs)) throws -> Result\(allWheres.backspaceIfNotEmpty()) { | ||
let env = env ?? storedEnvironment | ||
let function: TypedFunction\(paramCount)\(allGenerics.bracketedOrNone) = try get(env, name) | ||
return try function.call(env, this: self\(inGenericsAsCallArgs)) | ||
} | ||
@available(*, noasync) | ||
func call\(allGenerics.bracketedOrNone)(_ env: Environment? = nil, _ name: String\(inGenericsAsArgs)) throws -> Result\(allWheres.backspaceIfNotEmpty()) { | ||
let env = env ?? storedEnvironment | ||
let function: TypedFunction\(paramCount)\(allGenerics.bracketedOrNone) = try get(env, name) | ||
return try function.call(env, this: Undefined.default\(inGenericsAsCallArgs)) | ||
} | ||
@available(*, noasync) | ||
func callThis\(inGenerics.bracketedOrNone)(_ env: Environment? = nil, _ name: String\(inGenericsAsArgs)) throws \(inWheres.backspaceIfNotEmpty()) { | ||
let env = env ?? storedEnvironment | ||
let function: TypedFunction\(paramCount)<Undefined\(commaSeparatedInGenerics.prefixCommaIfNotEmpty())> = try get(env, name) | ||
return try function.call(env, this: self\(inGenericsAsCallArgs)) | ||
} | ||
@available(*, noasync) | ||
func call\(inGenerics.bracketedOrNone)(_ env: Environment? = nil, _ name: String\(inGenericsAsArgs)) throws \(inWheres.backspaceIfNotEmpty()) { | ||
let env = env ?? storedEnvironment | ||
let function: TypedFunction\(paramCount)<Undefined\(commaSeparatedInGenerics.prefixCommaIfNotEmpty())> = try get(env, name) | ||
return try function.call(env, this: Undefined.default\(inGenericsAsCallArgs)) | ||
} | ||
} | ||
""") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters