Repository navigation
Commit c6055e7
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 ebc501f commit c6055e7
11 files changed
Lines changed: 1090 additions & 112 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 | |
|---|---|---|---|
| |||
1452 | 1452 | | |
1453 | 1453 | | |
1454 | 1454 | | |
| 1455 | + | |
| 1456 | + | |
| 1457 | + | |
| 1458 | + | |
| 1459 | + | |
| 1460 | + | |
| 1461 | + | |
| 1462 | + | |
| 1463 | + | |
| 1464 | + | |
| 1465 | + | |
| 1466 | + | |
| 1467 | + | |
| 1468 | + | |
| 1469 | + | |
| 1470 | + | |
| 1471 | + | |
| 1472 | + | |
| 1473 | + | |
| 1474 | + | |
1455 | 1475 | | |
1456 | 1476 | | |
1457 | 1477 | | |
1458 | | - | |
| 1478 | + | |
| 1479 | + | |
1459 | 1480 | | |
1460 | 1481 | | |
1461 | 1482 | | |
1462 | 1483 | | |
1463 | | - | |
| 1484 | + | |
| 1485 | + | |
| 1486 | + | |
| 1487 | + | |
| 1488 | + | |
| 1489 | + | |
| 1490 | + | |
1464 | 1491 | | |
1465 | 1492 | | |
1466 | 1493 | | |
| |||
1552 | 1579 | | |
1553 | 1580 | | |
1554 | 1581 | | |
1555 | | - | |
1556 | | - | |
1557 | | - | |
1558 | | - | |
1559 | | - | |
1560 | | - | |
1561 | | - | |
| 1582 | + | |
| 1583 | + | |
| 1584 | + | |
| 1585 | + | |
| 1586 | + | |
| 1587 | + | |
| 1588 | + | |
| 1589 | + | |
| 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 | + | |
1562 | 1617 | | |
1563 | 1618 | | |
1564 | 1619 | | |
| |||
1591 | 1646 | | |
1592 | 1647 | | |
1593 | 1648 | | |
1594 | | - | |
| 1649 | + | |
| 1650 | + | |
| 1651 | + | |
| 1652 | + | |
| 1653 | + | |
1595 | 1654 | | |
1596 | 1655 | | |
1597 | | - | |
| 1656 | + | |
| 1657 | + | |
| 1658 | + | |
| 1659 | + | |
| 1660 | + | |
| 1661 | + | |
| 1662 | + | |
1598 | 1663 | | |
1599 | 1664 | | |
1600 | 1665 | | |
| |||
1607 | 1672 | | |
1608 | 1673 | | |
1609 | 1674 | | |
| 1675 | + | |
| 1676 | + | |
| 1677 | + | |
| 1678 | + | |
| 1679 | + | |
| 1680 | + | |
| 1681 | + | |
| 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 | + | |
1610 | 1720 | | |
1611 | 1721 | | |
1612 | 1722 | | |
| |||
1669 | 1779 | | |
1670 | 1780 | | |
1671 | 1781 | | |
1672 | | - | |
| 1782 | + | |
| 1783 | + | |
1673 | 1784 | | |
1674 | 1785 | | |
1675 | 1786 | | |
| |||
1829 | 1940 | | |
1830 | 1941 | | |
1831 | 1942 | | |
| 1943 | + | |
| 1944 | + | |
| 1945 | + | |
| 1946 | + | |
| 1947 | + | |
| 1948 | + | |
| 1949 | + | |
| 1950 | + | |
| 1951 | + | |
| 1952 | + | |
| 1953 | + | |
| 1954 | + | |
| 1955 | + | |
1832 | 1956 | | |
1833 | 1957 | | |
1834 | 1958 | | |
| |||
2503 | 2627 | | |
2504 | 2628 | | |
2505 | 2629 | | |
| 2630 | + | |
| 2631 | + | |
| 2632 | + | |
| 2633 | + | |
| 2634 | + | |
| 2635 | + | |
| 2636 | + | |
| 2637 | + | |
| 2638 | + | |
| 2639 | + | |
| 2640 | + | |
| 2641 | + | |
| 2642 | + | |
2506 | 2643 | | |
2507 | 2644 | | |
2508 | 2645 | | |
| |||
0 commit comments