Repository navigation
BridgeJS: Escape reserved words in exported function parameters #822
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1758,7 +1758,7 @@ public struct BridgeJSLink { | |
| loweringFragment.parameters.count == 1, | ||
| "Lowering fragment should have exactly one parameter to lower" | ||
| ) | ||
| let paramName = scope.variable(param.name) | ||
| let paramName = scope.variable(param.escapedJSIdentifier) | ||
| let loweredValues = try loweringFragment.printCode([paramName], context) | ||
| parameterForwardings.append(contentsOf: loweredValues) | ||
| } | ||
|
|
@@ -1950,7 +1950,7 @@ public struct BridgeJSLink { | |
| } | ||
| let parameterSignatures = parameters.map { param in | ||
| let optional = param.hasDefault ? "?" : "" | ||
| return "\(param.name)\(optional): \(resolveTypeScriptType(param.type))" | ||
| return "\(param.escapedJSIdentifier)\(optional): \(resolveTypeScriptType(param.type))" | ||
| } | ||
| return "(\(parameterSignatures.joined(separator: ", "))): \(returnTypeWithEffect)" | ||
| } | ||
|
|
@@ -2669,7 +2669,7 @@ extension BridgeJSLink { | |
| if let constructor = klass.constructor { | ||
| let paramSignatures = constructor.parameters.map { param in | ||
| let optional = param.hasDefault ? "?" : "" | ||
| return "\(param.name)\(optional): \(resolveTypeScriptType(param.type))" | ||
| return "\(param.escapedJSIdentifier)\(optional): \(resolveTypeScriptType(param.type))" | ||
| } | ||
| printer.write( | ||
| lines: renderJSDoc(documentation: constructor.documentation, parameters: constructor.parameters) | ||
|
|
@@ -4321,9 +4321,9 @@ enum DefaultValueUtils { | |
| return parameters.map { param in | ||
| if let defaultValue = param.defaultValue { | ||
| let defaultJs = format(defaultValue, as: .javascript, resolveTypeName: resolveTypeName) | ||
| return "\(param.name) = \(defaultJs)" | ||
| return "\(param.escapedJSIdentifier) = \(defaultJs)" | ||
| } | ||
| return param.name | ||
| return param.escapedJSIdentifier | ||
| }.joined(separator: ", ") | ||
| } | ||
| } | ||
|
|
@@ -4348,11 +4348,11 @@ extension BridgeJSLink { | |
| } | ||
| switch (docText, defaultValue) { | ||
| case let (.some(text), .some(value)): | ||
| tagLines.append("@param \(parameter.name) \(text) (default: \(value))") | ||
| tagLines.append("@param \(parameter.escapedJSIdentifier) \(text) (default: \(value))") | ||
| case let (.some(text), .none): | ||
| tagLines.append("@param \(parameter.name) \(text)") | ||
| tagLines.append("@param \(parameter.escapedJSIdentifier) \(text)") | ||
| case let (.none, .some(value)): | ||
| tagLines.append("@param \(parameter.name) - Optional parameter (default: \(value))") | ||
| tagLines.append("@param \(parameter.escapedJSIdentifier) - Optional parameter (default: \(value))") | ||
| case (.none, .none): | ||
| continue | ||
| } | ||
|
|
@@ -4477,6 +4477,21 @@ struct BridgeJSLinkError: Error { | |
| let message: String | ||
| } | ||
|
|
||
| extension Parameter { | ||
| // From: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#reserved_words | ||
| private static let javaScriptReservedWords: Set<String> = [ | ||
| "break", "case", "catch", "class", "const", "continue", "debugger", "default", "delete", "do", "else", | ||
| "export", "extends", "false", "finally", "for", "function", "if", "import", "in", "instanceof", "new", | ||
| "null", "return", "super", "switch", "this", "throw", "true", "try", "typeof", "var", "void", "while", | ||
| "with", "let", "static", "yield", "await", "enum", "implements", "interface", "package", "private", | ||
| "protected", "public", "arguments", "eval", | ||
| ] | ||
|
|
||
| fileprivate var escapedJSIdentifier: String { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For TestModule["bjs_Delegate_handle"] = function bjs_Delegate_handle(self, in) {func handle(in: Int) -> Void {
let inValue = in.bridgeJSLowerParameter()
} else if liftingFragment.parameters.count == 1 {
let name = scope.variable(param.escapedJSIdentifier)
parameterNames.append(name)
valuesToLift = [name]
}The Swift side needs |
||
| Self.javaScriptReservedWords.contains(name) ? "_\(name)" : name | ||
| } | ||
| } | ||
|
|
||
| extension ClosureSignature { | ||
| fileprivate func renderTSFunctionType(renderType: (BridgeType) -> String) -> String { | ||
| let renderedParameters = parameters.enumerated().map { index, parameter in | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| @JS func check(extension: Int, break: Double, function: Bool, in: String) {} | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This PR also changes the constructor and method paths. They generate correctly today, and these cases would keep it that way: @JS class ReservedWordBox {
@JS init(in: Int) {}
@JS func run(for: Int) {}
@JS static func make(default: Int) -> Int { 0 }
} |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| { | ||
| "exported" : { | ||
| "aliases" : [ | ||
|
|
||
| ], | ||
| "classes" : [ | ||
|
|
||
| ], | ||
| "enums" : [ | ||
|
|
||
| ], | ||
| "exposeToGlobal" : false, | ||
| "functions" : [ | ||
| { | ||
| "abiName" : "bjs_check", | ||
| "effects" : { | ||
| "isAsync" : false, | ||
| "isStatic" : false, | ||
| "isThrows" : false | ||
| }, | ||
| "name" : "check", | ||
| "parameters" : [ | ||
| { | ||
| "label" : "extension", | ||
| "name" : "extension", | ||
| "type" : { | ||
| "integer" : { | ||
| "_0" : { | ||
| "isSigned" : true, | ||
| "width" : "word" | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| { | ||
| "label" : "break", | ||
| "name" : "break", | ||
| "type" : { | ||
| "double" : { | ||
|
|
||
| } | ||
| } | ||
| }, | ||
| { | ||
| "label" : "function", | ||
| "name" : "function", | ||
| "type" : { | ||
| "bool" : { | ||
|
|
||
| } | ||
| } | ||
| }, | ||
| { | ||
| "label" : "in", | ||
| "name" : "in", | ||
| "type" : { | ||
| "string" : { | ||
|
|
||
| } | ||
| } | ||
| } | ||
| ], | ||
| "returnType" : { | ||
| "void" : { | ||
|
|
||
| } | ||
| } | ||
| } | ||
| ], | ||
| "protocols" : [ | ||
|
|
||
| ], | ||
| "structs" : [ | ||
|
|
||
| ] | ||
| }, | ||
| "moduleName" : "TestModule", | ||
| "usedExternalModules" : [ | ||
|
|
||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| @_expose(wasm, "bjs_check") | ||
| @_cdecl("bjs_check") | ||
| public func _bjs_check(_ `extension`: Int32, _ `break`: Float64, _ function: Int32, _ inBytes: Int32, _ inLength: Int32) -> Void { | ||
| #if arch(wasm32) | ||
| let `in` = String.bridgeJSLiftParameter(inBytes, inLength) | ||
| let function = Bool.bridgeJSLiftParameter(function) | ||
| let `break` = Double.bridgeJSLiftParameter(`break`) | ||
| let `extension` = Int.bridgeJSLiftParameter(`extension`) | ||
| check(extension: `extension`, break: `break`, function: function, in: `in`) | ||
| #else | ||
| fatalError("Only available on WebAssembly") | ||
| #endif | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| // NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit, | ||
| // DO NOT EDIT. | ||
| // | ||
| // To update this file, just rebuild your project or run | ||
| // `swift package bridge-js`. | ||
|
|
||
| export type Exports = { | ||
| check(extension: number, _break: number, _function: boolean, _in: string): void; | ||
| } | ||
| export type Imports = { | ||
| } | ||
| export function createInstantiator(options: { | ||
| imports: Imports; | ||
| }, swift: any): Promise<{ | ||
| addImports: (importObject: WebAssembly.Imports) => void; | ||
| setInstance: (instance: WebAssembly.Instance) => void; | ||
| createExports: (instance: WebAssembly.Instance) => Exports; | ||
| }>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JS func f(_in: String)still generates invalid Swift and JS, becauseparseParameters(SwiftToSkeleton.swift:1590) keeps the backticks inparam.name:The import path already strips them with
normalizeIdentifier. If this path does the same, the escaping in this PR covers both spellings: