Repository navigation
Commit bf8d01e
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 53e8cc1 commit bf8d01e
11 files changed
Lines changed: 1093 additions & 119 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 | | |
| |||
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1460 | 1460 | | |
1461 | 1461 | | |
1462 | 1462 | | |
| 1463 | + | |
| 1464 | + | |
| 1465 | + | |
| 1466 | + | |
| 1467 | + | |
| 1468 | + | |
| 1469 | + | |
| 1470 | + | |
| 1471 | + | |
| 1472 | + | |
| 1473 | + | |
| 1474 | + | |
| 1475 | + | |
| 1476 | + | |
| 1477 | + | |
| 1478 | + | |
| 1479 | + | |
| 1480 | + | |
| 1481 | + | |
| 1482 | + | |
1463 | 1483 | | |
1464 | 1484 | | |
1465 | 1485 | | |
1466 | | - | |
| 1486 | + | |
| 1487 | + | |
1467 | 1488 | | |
1468 | 1489 | | |
1469 | 1490 | | |
1470 | 1491 | | |
1471 | | - | |
| 1492 | + | |
| 1493 | + | |
| 1494 | + | |
| 1495 | + | |
| 1496 | + | |
| 1497 | + | |
| 1498 | + | |
1472 | 1499 | | |
1473 | 1500 | | |
1474 | 1501 | | |
| |||
1560 | 1587 | | |
1561 | 1588 | | |
1562 | 1589 | | |
1563 | | - | |
1564 | | - | |
1565 | | - | |
1566 | | - | |
1567 | | - | |
1568 | | - | |
1569 | | - | |
| 1590 | + | |
| 1591 | + | |
| 1592 | + | |
| 1593 | + | |
| 1594 | + | |
| 1595 | + | |
| 1596 | + | |
| 1597 | + | |
| 1598 | + | |
| 1599 | + | |
| 1600 | + | |
| 1601 | + | |
| 1602 | + | |
| 1603 | + | |
| 1604 | + | |
| 1605 | + | |
| 1606 | + | |
| 1607 | + | |
| 1608 | + | |
| 1609 | + | |
| 1610 | + | |
| 1611 | + | |
| 1612 | + | |
| 1613 | + | |
| 1614 | + | |
| 1615 | + | |
| 1616 | + | |
| 1617 | + | |
| 1618 | + | |
| 1619 | + | |
| 1620 | + | |
| 1621 | + | |
| 1622 | + | |
| 1623 | + | |
| 1624 | + | |
1570 | 1625 | | |
1571 | 1626 | | |
1572 | 1627 | | |
| |||
1599 | 1654 | | |
1600 | 1655 | | |
1601 | 1656 | | |
1602 | | - | |
| 1657 | + | |
| 1658 | + | |
| 1659 | + | |
| 1660 | + | |
| 1661 | + | |
1603 | 1662 | | |
1604 | 1663 | | |
1605 | | - | |
| 1664 | + | |
| 1665 | + | |
| 1666 | + | |
| 1667 | + | |
| 1668 | + | |
| 1669 | + | |
| 1670 | + | |
1606 | 1671 | | |
1607 | 1672 | | |
1608 | 1673 | | |
| |||
1615 | 1680 | | |
1616 | 1681 | | |
1617 | 1682 | | |
| 1683 | + | |
| 1684 | + | |
| 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 | + | |
| 1720 | + | |
| 1721 | + | |
| 1722 | + | |
| 1723 | + | |
| 1724 | + | |
| 1725 | + | |
| 1726 | + | |
| 1727 | + | |
1618 | 1728 | | |
1619 | 1729 | | |
1620 | 1730 | | |
| |||
1677 | 1787 | | |
1678 | 1788 | | |
1679 | 1789 | | |
1680 | | - | |
| 1790 | + | |
| 1791 | + | |
1681 | 1792 | | |
1682 | 1793 | | |
1683 | 1794 | | |
| |||
1837 | 1948 | | |
1838 | 1949 | | |
1839 | 1950 | | |
| 1951 | + | |
| 1952 | + | |
| 1953 | + | |
| 1954 | + | |
| 1955 | + | |
| 1956 | + | |
| 1957 | + | |
| 1958 | + | |
| 1959 | + | |
| 1960 | + | |
| 1961 | + | |
| 1962 | + | |
| 1963 | + | |
1840 | 1964 | | |
1841 | 1965 | | |
1842 | 1966 | | |
| |||
2514 | 2638 | | |
2515 | 2639 | | |
2516 | 2640 | | |
| 2641 | + | |
| 2642 | + | |
| 2643 | + | |
| 2644 | + | |
| 2645 | + | |
| 2646 | + | |
| 2647 | + | |
| 2648 | + | |
| 2649 | + | |
| 2650 | + | |
| 2651 | + | |
| 2652 | + | |
| 2653 | + | |
2517 | 2654 | | |
2518 | 2655 | | |
2519 | 2656 | | |
| |||
0 commit comments