GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — BridgeJS: Export types using a separate JS representation by wfltaylor · Pull Request #750 · swiftwasm/JavaScriptKit Conversation
Contributor
Note that this PR is much smaller than it looks! The vast majority of the lines added are tests/generated code. Adds MotivationBridgeJS provides sensible defaults when exporting Swift code to JavaScript. For example, structs are exported using copy semantics: each field in the Swift struct is copied when crossing the boundary. BridgeJS also only supports exporting a subset of Swift features, which makes sense given there are many things in the Swift language which would be difficult to expose to JavaScript. While this approach makes sense, sometimes the defaults used by BridgeJS won’t work well in a specific situation. For example, a Swift At first, the “duplicated types” approach sounds reasonable. Unfortunately, it starts to break down as the project scales. For example, a large existing codebase could use SolutionAllow types to provide an alternative JS representation. This works as follows: @JS(as: JSPolygon.self) public struct Polygon { public var vertices: [Point] public consuming func bridgeToJS() -> JSPolygon { JSPolygon(underlying: self) } public static func bridgeFromJS(_ value: consuming JSPolygon) -> Polygon { value.underlying } } @JS public final class JSPolygon { var underlying: Polygon @JS public init(underlying: Polygon) { self.underlying = underlying } @JS var boundingBox: Rect { underlying.boundingBox } } Now, the existing Swift code can continue to use the Swift-idiomatic DesignThis is implemented by inserting calls to Alternatives
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
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. Thank you for trying to tackle this and showing your idea with actual code! This is very interesting to me, and I just realized the ability to have different transferring representations solves some other problems like #496
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page.
krodak
approved these changes
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. LGTM, just minor requests
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactionsUh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page.
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.
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
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.
if parent.extractNamespace(from: jsAttribute) != nil { errors.append(DiagnosticError( node: node, message: "`namespace` is ignored on `@JS(as:)` types", hint: "Remove the `namespace:` argument; the alias adopts its target's representation" )) } (adjust to whatever namespace-extraction helper is in scope here)
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
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. If guard let asArg = arguments.first(where: { $0.label?.text == "as" }) else { return nil // genuinely not an alias } guard let memberAccess = asArg.expression.as(MemberAccessExprSyntax.self), memberAccess.declName.baseName.text == "self", let base = memberAccess.base else { // `as:` present but malformed - report instead of silently ignoring return nil } return TypeSyntax(stringLiteral: base.trimmedDescription) Surfacing a diagnostic from here means threading an
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
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. Aliases mangle on name only ( case .alias(let name, _): // `name` is the namespace-qualified swiftCallName (unique); the underlying // representation isn't folded in because aliases bridge via their JS type's ABI. return "Al\(name.count)\(name)"
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
kateinoigakukun
approved these changes
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. LGTM as long as @krodak's feedbacks are resolved.
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactionsFollow-up to the review on swiftwasm#750: - ImportTS: give the unreachable `.alias` cases in loweringParameterInfo / liftingReturnInfo a message stating the `.unaliased` invariant, so a future change that breaks it fails loudly instead of trapping silently. - JSGlueGen: in `optionalConvention` and `wasmParams`, delegate `.alias` to its underlying type rather than `preconditionFailure()`. These switch on `self` (not `.unaliased`), so this removes a latent crash for alias-wrapped values and matches how abiReturnType / mangleTypeName already handle `.alias`. - SwiftToSkeleton: diagnose `@JS(as:)` combined with `namespace:` instead of silently dropping the namespace; an alias adopts its representation's placement. Adds a diagnostics test. - BridgeJSSkeleton: note why alias mangling uses the (unique) swiftCallName only. - Docs: add an "Exporting a Type With a Custom JS Representation" article and link it from the exporting topics.
Member
All reactions
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page.
krodak
enabled auto-merge
Uh oh!
There was an error while loading. Please reload this page.
MaxDesiatov
reviewed
Uh oh!
There was an error while loading. Please reload this page.
Contributor
Author
All reactions
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ReviewersUh oh!
There was an error while loading. Please reload this page. AssigneesNo one assignedLabels
None yet
ProjectsMilestoneNo milestoneDevelopmentSuccessfully merging this pull request may close these issues. Uh oh!
There was an error while loading. Please reload this page. Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |