◐ Shell
clean mode source ↗

BridgeJS: @JS Protocol with methods support by krodak · Pull Request #456 · swiftwasm/JavaScriptKit

Introduction

This PR adds basic support for exporting Swift protocols to JS / TS using BridgeJS plugin.

Overview

BridgeJS now supports exporting Swift protocols as TypeScript interfaces. JavaScript objects implementing these interfaces can be passed to Swift code, where they are automatically wrapped in generated Swift struct that forward method calls and property accesses to the JavaScript implementation.

When you mark a protocol with @JS, BridgeJS generates:

  • A TypeScript interface with the protocol's method signatures and properties
  • A Swift wrapper struct (Any{ProtocolName}) that conforms to the protocol
  • JavaScript bridge code that forwards calls between Swift and JavaScript

Current Limitations

This PR covers support for methods and basic types in parameters and return types, as these types are supported in both Export / Import directions.
Support for properties, Optional and others will be posted in separate PR as changeset is quite large.

Testing

Basic tests with protocol implementation on JS and Swift side used as delegate property of protocol type are added.

Documentation

Added to Sources/JavaScriptKit/Documentation.docc/Articles/BridgeJS/Exporting-Swift/Exporting-Swift-Protocols.md