Title: BridgeJS: Declarative JS interop · Issue #290 · swiftwasm/JavaScriptKit · GitHub
Open Graph Title: BridgeJS: Declarative JS interop · Issue #290 · swiftwasm/JavaScriptKit
X Title: BridgeJS: Declarative JS interop · Issue #290 · swiftwasm/JavaScriptKit
Description: Motivation Current JS interop system, JavaScriptKit, has two main issues: It's based on dynamic, string-based method calls and properties access. It's not type-safe and can easily lead to runtime errors even if the JS side is written wit...
Open Graph Description: Motivation Current JS interop system, JavaScriptKit, has two main issues: It's based on dynamic, string-based method calls and properties access. It's not type-safe and can easily lead to runtime e...
X Description: Motivation Current JS interop system, JavaScriptKit, has two main issues: It's based on dynamic, string-based method calls and properties access. It's not type-safe and can easily lead to r...
Opengraph URL: https://github.com/swiftwasm/JavaScriptKit/issues/290
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"BridgeJS: Declarative JS interop","articleBody":"## Motivation\n\nCurrent JS interop system, [JavaScriptKit](https://github.com/swiftwasm/JavaScriptKit), has two main issues:\n\n* It's based on dynamic, string-based method calls and properties access.\n * It's not type-safe and can easily lead to runtime errors even if the JS side is written with types (like TypeScript).\n * There are non-trivial performance penalties due to the dynamism.\n* There is no easy way to expose Swift functionalities to JS side.\n * Developer productivity\n * We need to write so many boilerplates to set up closures and type conversions for each exposed function.\n * Has some performance penalties too.\n\n## Current Status\n\nTry on our playground https://swiftwasm.org/JavaScriptKit/PlayBridgeJS/\n\n## High-level API\n\nGiven the following interface:\n\n```\n// bridge.d.ts\nexport interface CanvasContext {\n drawRect(x: number, y: number, width: number, height: number) =\u003e void;\n}\n\n// App.swift\npublic struct App {\n let context: CanvasContext\n @ExposeToJS\n init(context: CanvasContext) {\n self.context = context\n }\n\n struct PointerEvent {\n let x: Int\n let y: Int\n let pointerId: Int\n }\n\n @ExposeToJS\n func feedPointerEvents(_ events: [PointerEvent]) {\n ...\n context.drawRect(...)\n }\n}\n```\n\nThen SwiftPM Build Plugin or standalone CLI tool should generate:\n1. Swift and JS bridging glue code to expose `CanvasContext` to Swift\n2. Swift and JS/TS bridging glue code to expose `App` methods to JS/TS\n\n\n## Breakdown\n\n- [x] Produce ES Module package with some instantiation JS code and .wasm\n - [x] Check if the current SwiftPM Plugin API has enough capability to produce a JS package\n - [x] https://github.com/swiftwasm/JavaScriptKit/pull/288\n- [x] Prototype a tool to expose Swift interface to TS/JS\n - [x] Check if we can process it without swift-syntax dependency (to avoid longer build time)\n - [x] Performance benchmark\n- [x] Prototype a tool to import TS interface to Swift\n - [x] Check if TypeScript Compiler API can handle third-party JS packages\n - [x] Performance benchmark\n\n## Other Languages\n\n* https://learn.microsoft.com/en-us/aspnet/core/client-side/dotnet-interop?view=aspnetcore-9.0\n* https://kotlinlang.org/docs/js-to-kotlin-interop.html\n* https://rescript-lang.org/docs/manual/v11.0.0/interop-cheatsheet\n* https://github.com/rustwasm/wasm-bindgen/\n* https://github.com/siefkenj/tsify\n* https://github.com/ocsigen/ts2ocaml\n\n\n","author":{"url":"https://github.com/kateinoigakukun","@type":"Person","name":"kateinoigakukun"},"datePublished":"2025-03-10T08:23:37.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/290/JavaScriptKit/issues/290"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:b6ab85ee-1aec-0846-1182-8537f21078a2 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CFFE:F5406:1B5DBE6:26C6B69:6978DA0B |
| html-safe-nonce | 547e399a90b631d87b0da9c9a4c76ce1d77e2d49abc25c7c14ece19d862bb200 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRkZFOkY1NDA2OjFCNURCRTY6MjZDNkI2OTo2OTc4REEwQiIsInZpc2l0b3JfaWQiOiI2MzExNzY3MzQ3NTM0NzkxNzkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 7aec4d5c816a3468c9c385de261e4a473ccdc6d91f0244fae9084a01493b0521 |
| hovercard-subject-tag | issue:2906447874 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/swiftwasm/JavaScriptKit/290/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b554cdc5667c8d54e613a1110d685ce3bb4e3ccbebc15de019cf7a4f17f6401f/swiftwasm/JavaScriptKit/issues/290 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b554cdc5667c8d54e613a1110d685ce3bb4e3ccbebc15de019cf7a4f17f6401f/swiftwasm/JavaScriptKit/issues/290 |
| og:image:alt | Motivation Current JS interop system, JavaScriptKit, has two main issues: It's based on dynamic, string-based method calls and properties access. It's not type-safe and can easily lead to runtime e... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | kateinoigakukun |
| hostname | github.com |
| expected-hostname | github.com |
| None | 8a71ca1f7ab5436216e1df86f398ef7a51ed3152c90e3f7332fc70cf97fcb9d6 |
| turbo-cache-control | no-preview |
| go-import | github.com/swiftwasm/JavaScriptKit git https://github.com/swiftwasm/JavaScriptKit.git |
| octolytics-dimension-user_id | 49500752 |
| octolytics-dimension-user_login | swiftwasm |
| octolytics-dimension-repository_id | 244832006 |
| octolytics-dimension-repository_nwo | swiftwasm/JavaScriptKit |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 244832006 |
| octolytics-dimension-repository_network_root_nwo | swiftwasm/JavaScriptKit |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | eaed570eead52a9920afef75dc3eb6d826fdc5ef |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width