Repository navigation
Commit 507dae6
committed
BridgeJS: Support generic functions on exported Swift APIs
Export half of generic function support (swiftwasm#398); the import half landed
in swiftwasm#799 and this reuses its ABI: values cross on each type's existing
stack layout, and a trailing i32 type ID per generic parameter selects
the concrete type, so the per-function glue stays type-agnostic.
@js public func identity<T: BridgedSwiftGenericBridgeable>(_ value: T) -> T { value }
const n = exports.identity(42, BridgeTypes.Int); // TS erases generics,
const p = exports.identity(pt, BridgeTypes.Point); // so callers pass a token
How it works:
- The wasm entry point is a concrete @_expose thunk taking the trailing
type IDs. Because a type ID is the address of the type's
BridgeJSTypeHandle, the thunk recovers the concrete type directly via
Unmanaged.fromOpaque and reifies T through an opened existential
(nested opening chain for multiple parameters); the body then runs the
same pop-call-push sequence a concrete export would.
- Generic thunks always hoist stack-using arguments: the JS wrapper
lowers self and every argument in declaration order, so the pops must
run in reverse declaration order ahead of any self pop embedded in the
callee expression.
- The JS wrapper resolves the caller's BridgeTypes token through the map
built during type-handle registration and throws a catchable TypeError
for unknown tokens before anything is lowered, so the shared value
stack stays balanced. A raw wasm caller passing a garbage type ID is
undefined behavior; the generated wrapper is the only supported
caller.
- Tokens are unqualified type names, so linking two modules that define
same-named @js types fails the build while generics are in use.
- Generics work on top-level functions and on instance and static
methods of @js classes, structs, and enums, with T bare or wrapped as
[T], T?, or [String: T].
- Type recovery needs the handle's metatype storage (an existential), so
exported generic thunks are fatalError stubs under Embedded Swift;
imports remain Embedded-compatible.
Unsupported forms (async or throws generics, where clauses, unused
generic parameters, concrete non-Void returns, generic initializers,
default parameter values, nested wrappings) are rejected with build-time
diagnostics.1 parent 777848d commit 507dae6
11 files changed
Lines changed: 1096 additions & 120 deletions
File tree
- Plugins/BridgeJS
- Sources
- BridgeJSCore
- BridgeJSLink
- BridgeJSSkeleton
- Tests/BridgeJSToolTests
- Sources/JavaScriptKit/Documentation.docc/Articles/BridgeJS
- BridgeJS-Internals
- Exporting-Swift
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
98 | 98 | | |
99 | 99 | | |
100 | 100 | | |
101 | | - | |
| 101 | + | |
102 | 102 | | |
103 | 103 | | |
104 | 104 | | |
| |||
Lines changed: 206 additions & 12 deletions
Large diffs are not rendered by default.
Lines changed: 149 additions & 12 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1555 | 1555 | | |
1556 | 1556 | | |
1557 | 1557 | | |
| 1558 | + | |
| 1559 | + | |
| 1560 | + | |
| 1561 | + | |
| 1562 | + | |
| 1563 | + | |
| 1564 | + | |
| 1565 | + | |
| 1566 | + | |
| 1567 | + | |
| 1568 | + | |
| 1569 | + | |
| 1570 | + | |
| 1571 | + | |
| 1572 | + | |
| 1573 | + | |
| 1574 | + | |
| 1575 | + | |
| 1576 | + | |
| 1577 | + | |
1558 | 1578 | | |
1559 | 1579 | | |
1560 | 1580 | | |
1561 | | - | |
| 1581 | + | |
| 1582 | + | |
1562 | 1583 | | |
1563 | 1584 | | |
1564 | 1585 | | |
1565 | 1586 | | |
1566 | | - | |
| 1587 | + | |
| 1588 | + | |
| 1589 | + | |
| 1590 | + | |
| 1591 | + | |
| 1592 | + | |
| 1593 | + | |
1567 | 1594 | | |
1568 | 1595 | | |
1569 | 1596 | | |
| |||
1655 | 1682 | | |
1656 | 1683 | | |
1657 | 1684 | | |
1658 | | - | |
1659 | | - | |
1660 | | - | |
1661 | | - | |
1662 | | - | |
1663 | | - | |
1664 | | - | |
| 1685 | + | |
| 1686 | + | |
| 1687 | + | |
| 1688 | + | |
| 1689 | + | |
| 1690 | + | |
| 1691 | + | |
| 1692 | + | |
| 1693 | + | |
| 1694 | + | |
| 1695 | + | |
| 1696 | + | |
| 1697 | + | |
| 1698 | + | |
| 1699 | + | |
| 1700 | + | |
| 1701 | + | |
| 1702 | + | |
| 1703 | + | |
| 1704 | + | |
| 1705 | + | |
| 1706 | + | |
| 1707 | + | |
| 1708 | + | |
| 1709 | + | |
| 1710 | + | |
| 1711 | + | |
| 1712 | + | |
| 1713 | + | |
| 1714 | + | |
| 1715 | + | |
| 1716 | + | |
| 1717 | + | |
| 1718 | + | |
| 1719 | + | |
1665 | 1720 | | |
1666 | 1721 | | |
1667 | 1722 | | |
| |||
1694 | 1749 | | |
1695 | 1750 | | |
1696 | 1751 | | |
1697 | | - | |
| 1752 | + | |
| 1753 | + | |
| 1754 | + | |
| 1755 | + | |
| 1756 | + | |
1698 | 1757 | | |
1699 | 1758 | | |
1700 | | - | |
| 1759 | + | |
| 1760 | + | |
| 1761 | + | |
| 1762 | + | |
| 1763 | + | |
| 1764 | + | |
| 1765 | + | |
1701 | 1766 | | |
1702 | 1767 | | |
1703 | 1768 | | |
| |||
1710 | 1775 | | |
1711 | 1776 | | |
1712 | 1777 | | |
| 1778 | + | |
| 1779 | + | |
| 1780 | + | |
| 1781 | + | |
| 1782 | + | |
| 1783 | + | |
| 1784 | + | |
| 1785 | + | |
| 1786 | + | |
| 1787 | + | |
| 1788 | + | |
| 1789 | + | |
| 1790 | + | |
| 1791 | + | |
| 1792 | + | |
| 1793 | + | |
| 1794 | + | |
| 1795 | + | |
| 1796 | + | |
| 1797 | + | |
| 1798 | + | |
| 1799 | + | |
| 1800 | + | |
| 1801 | + | |
| 1802 | + | |
| 1803 | + | |
| 1804 | + | |
| 1805 | + | |
| 1806 | + | |
| 1807 | + | |
| 1808 | + | |
| 1809 | + | |
| 1810 | + | |
| 1811 | + | |
| 1812 | + | |
| 1813 | + | |
| 1814 | + | |
| 1815 | + | |
| 1816 | + | |
| 1817 | + | |
| 1818 | + | |
| 1819 | + | |
| 1820 | + | |
| 1821 | + | |
| 1822 | + | |
1713 | 1823 | | |
1714 | 1824 | | |
1715 | 1825 | | |
| |||
1772 | 1882 | | |
1773 | 1883 | | |
1774 | 1884 | | |
1775 | | - | |
| 1885 | + | |
| 1886 | + | |
1776 | 1887 | | |
1777 | 1888 | | |
1778 | 1889 | | |
| |||
1932 | 2043 | | |
1933 | 2044 | | |
1934 | 2045 | | |
| 2046 | + | |
| 2047 | + | |
| 2048 | + | |
| 2049 | + | |
| 2050 | + | |
| 2051 | + | |
| 2052 | + | |
| 2053 | + | |
| 2054 | + | |
| 2055 | + | |
| 2056 | + | |
| 2057 | + | |
| 2058 | + | |
1935 | 2059 | | |
1936 | 2060 | | |
1937 | 2061 | | |
| |||
2630 | 2754 | | |
2631 | 2755 | | |
2632 | 2756 | | |
| 2757 | + | |
| 2758 | + | |
| 2759 | + | |
| 2760 | + | |
| 2761 | + | |
| 2762 | + | |
| 2763 | + | |
| 2764 | + | |
| 2765 | + | |
| 2766 | + | |
| 2767 | + | |
| 2768 | + | |
| 2769 | + | |
2633 | 2770 | | |
2634 | 2771 | | |
2635 | 2772 | | |
| |||
0 commit comments