wasm-webp 0.0.2 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1349,128 +1349,884 @@ function dbg(...args) {
1349
1349
  };
1350
1350
 
1351
1351
 
1352
- var emval_freelist = [];
1353
1352
 
1354
- var emval_handles = [];
1355
- var __emval_decref = (handle) => {
1356
- if (handle > 9 && 0 === --emval_handles[handle + 1]) {
1357
- assert(emval_handles[handle] !== undefined, `Decref for unallocated handle.`);
1358
- emval_handles[handle] = undefined;
1359
- emval_freelist.push(handle);
1353
+ var shallowCopyInternalPointer = (o) => {
1354
+ return {
1355
+ count: o.count,
1356
+ deleteScheduled: o.deleteScheduled,
1357
+ preservePointerOnDelete: o.preservePointerOnDelete,
1358
+ ptr: o.ptr,
1359
+ ptrType: o.ptrType,
1360
+ smartPtr: o.smartPtr,
1361
+ smartPtrType: o.smartPtrType,
1362
+ };
1363
+ };
1364
+
1365
+ var throwInstanceAlreadyDeleted = (obj) => {
1366
+ function getInstanceTypeName(handle) {
1367
+ return handle.$$.ptrType.registeredClass.name;
1360
1368
  }
1369
+ throwBindingError(getInstanceTypeName(obj) + ' instance already deleted');
1361
1370
  };
1362
1371
 
1372
+ var finalizationRegistry = false;
1363
1373
 
1374
+ var detachFinalizer = (handle) => {};
1364
1375
 
1376
+ var runDestructor = ($$) => {
1377
+ if ($$.smartPtr) {
1378
+ $$.smartPtrType.rawDestructor($$.smartPtr);
1379
+ } else {
1380
+ $$.ptrType.registeredClass.rawDestructor($$.ptr);
1381
+ }
1382
+ };
1383
+ var releaseClassHandle = ($$) => {
1384
+ $$.count.value -= 1;
1385
+ var toDelete = 0 === $$.count.value;
1386
+ if (toDelete) {
1387
+ runDestructor($$);
1388
+ }
1389
+ };
1365
1390
 
1391
+ var downcastPointer = (ptr, ptrClass, desiredClass) => {
1392
+ if (ptrClass === desiredClass) {
1393
+ return ptr;
1394
+ }
1395
+ if (undefined === desiredClass.baseClass) {
1396
+ return null; // no conversion
1397
+ }
1366
1398
 
1367
- var count_emval_handles = () => {
1368
- return emval_handles.length / 2 - 5 - emval_freelist.length;
1399
+ var rv = downcastPointer(ptr, ptrClass, desiredClass.baseClass);
1400
+ if (rv === null) {
1401
+ return null;
1402
+ }
1403
+ return desiredClass.downcast(rv);
1369
1404
  };
1370
1405
 
1371
- var init_emval = () => {
1372
- // reserve 0 and some special values. These never get de-allocated.
1373
- emval_handles.push(
1374
- 0, 1,
1375
- undefined, 1,
1376
- null, 1,
1377
- true, 1,
1378
- false, 1,
1379
- );
1380
- assert(emval_handles.length === 5 * 2);
1381
- Module['count_emval_handles'] = count_emval_handles;
1406
+ var registeredPointers = {
1407
+ };
1408
+
1409
+ var getInheritedInstanceCount = () => Object.keys(registeredInstances).length;
1410
+
1411
+ var getLiveInheritedInstances = () => {
1412
+ var rv = [];
1413
+ for (var k in registeredInstances) {
1414
+ if (registeredInstances.hasOwnProperty(k)) {
1415
+ rv.push(registeredInstances[k]);
1416
+ }
1417
+ }
1418
+ return rv;
1382
1419
  };
1383
- var Emval = {
1384
- toValue:(handle) => {
1385
- if (!handle) {
1386
- throwBindingError('Cannot use deleted val. handle = ' + handle);
1420
+
1421
+ var deletionQueue = [];
1422
+ var flushPendingDeletes = () => {
1423
+ while (deletionQueue.length) {
1424
+ var obj = deletionQueue.pop();
1425
+ obj.$$.deleteScheduled = false;
1426
+ obj['delete']();
1427
+ }
1428
+ };
1429
+
1430
+ var delayFunction;
1431
+
1432
+
1433
+ var setDelayFunction = (fn) => {
1434
+ delayFunction = fn;
1435
+ if (deletionQueue.length && delayFunction) {
1436
+ delayFunction(flushPendingDeletes);
1437
+ }
1438
+ };
1439
+ var init_embind = () => {
1440
+ Module['getInheritedInstanceCount'] = getInheritedInstanceCount;
1441
+ Module['getLiveInheritedInstances'] = getLiveInheritedInstances;
1442
+ Module['flushPendingDeletes'] = flushPendingDeletes;
1443
+ Module['setDelayFunction'] = setDelayFunction;
1444
+ };
1445
+ var registeredInstances = {
1446
+ };
1447
+
1448
+ var getBasestPointer = (class_, ptr) => {
1449
+ if (ptr === undefined) {
1450
+ throwBindingError('ptr should not be undefined');
1451
+ }
1452
+ while (class_.baseClass) {
1453
+ ptr = class_.upcast(ptr);
1454
+ class_ = class_.baseClass;
1455
+ }
1456
+ return ptr;
1457
+ };
1458
+ var getInheritedInstance = (class_, ptr) => {
1459
+ ptr = getBasestPointer(class_, ptr);
1460
+ return registeredInstances[ptr];
1461
+ };
1462
+
1463
+
1464
+ var makeClassHandle = (prototype, record) => {
1465
+ if (!record.ptrType || !record.ptr) {
1466
+ throwInternalError('makeClassHandle requires ptr and ptrType');
1467
+ }
1468
+ var hasSmartPtrType = !!record.smartPtrType;
1469
+ var hasSmartPtr = !!record.smartPtr;
1470
+ if (hasSmartPtrType !== hasSmartPtr) {
1471
+ throwInternalError('Both smartPtrType and smartPtr must be specified');
1472
+ }
1473
+ record.count = { value: 1 };
1474
+ return attachFinalizer(Object.create(prototype, {
1475
+ $$: {
1476
+ value: record,
1477
+ writable: true,
1478
+ },
1479
+ }));
1480
+ };
1481
+ /** @suppress {globalThis} */
1482
+ function RegisteredPointer_fromWireType(ptr) {
1483
+ // ptr is a raw pointer (or a raw smartpointer)
1484
+
1485
+ // rawPointer is a maybe-null raw pointer
1486
+ var rawPointer = this.getPointee(ptr);
1487
+ if (!rawPointer) {
1488
+ this.destructor(ptr);
1489
+ return null;
1490
+ }
1491
+
1492
+ var registeredInstance = getInheritedInstance(this.registeredClass, rawPointer);
1493
+ if (undefined !== registeredInstance) {
1494
+ // JS object has been neutered, time to repopulate it
1495
+ if (0 === registeredInstance.$$.count.value) {
1496
+ registeredInstance.$$.ptr = rawPointer;
1497
+ registeredInstance.$$.smartPtr = ptr;
1498
+ return registeredInstance['clone']();
1499
+ } else {
1500
+ // else, just increment reference count on existing object
1501
+ // it already has a reference to the smart pointer
1502
+ var rv = registeredInstance['clone']();
1503
+ this.destructor(ptr);
1504
+ return rv;
1387
1505
  }
1388
- // handle 2 is supposed to be `undefined`.
1389
- assert(handle === 2 || emval_handles[handle] !== undefined && handle % 2 === 0, `invalid handle: ${handle}`);
1390
- return emval_handles[handle];
1391
- },
1392
- toHandle:(value) => {
1393
- switch (value) {
1394
- case undefined: return 2;
1395
- case null: return 4;
1396
- case true: return 6;
1397
- case false: return 8;
1398
- default:{
1399
- const handle = emval_freelist.pop() || emval_handles.length;
1400
- emval_handles[handle] = value;
1401
- emval_handles[handle + 1] = 1;
1402
- return handle;
1506
+ }
1507
+
1508
+ function makeDefaultHandle() {
1509
+ if (this.isSmartPointer) {
1510
+ return makeClassHandle(this.registeredClass.instancePrototype, {
1511
+ ptrType: this.pointeeType,
1512
+ ptr: rawPointer,
1513
+ smartPtrType: this,
1514
+ smartPtr: ptr,
1515
+ });
1516
+ } else {
1517
+ return makeClassHandle(this.registeredClass.instancePrototype, {
1518
+ ptrType: this,
1519
+ ptr,
1520
+ });
1521
+ }
1522
+ }
1523
+
1524
+ var actualType = this.registeredClass.getActualType(rawPointer);
1525
+ var registeredPointerRecord = registeredPointers[actualType];
1526
+ if (!registeredPointerRecord) {
1527
+ return makeDefaultHandle.call(this);
1528
+ }
1529
+
1530
+ var toType;
1531
+ if (this.isConst) {
1532
+ toType = registeredPointerRecord.constPointerType;
1533
+ } else {
1534
+ toType = registeredPointerRecord.pointerType;
1535
+ }
1536
+ var dp = downcastPointer(
1537
+ rawPointer,
1538
+ this.registeredClass,
1539
+ toType.registeredClass);
1540
+ if (dp === null) {
1541
+ return makeDefaultHandle.call(this);
1542
+ }
1543
+ if (this.isSmartPointer) {
1544
+ return makeClassHandle(toType.registeredClass.instancePrototype, {
1545
+ ptrType: toType,
1546
+ ptr: dp,
1547
+ smartPtrType: this,
1548
+ smartPtr: ptr,
1549
+ });
1550
+ } else {
1551
+ return makeClassHandle(toType.registeredClass.instancePrototype, {
1552
+ ptrType: toType,
1553
+ ptr: dp,
1554
+ });
1555
+ }
1556
+ }
1557
+ var attachFinalizer = (handle) => {
1558
+ if ('undefined' === typeof FinalizationRegistry) {
1559
+ attachFinalizer = (handle) => handle;
1560
+ return handle;
1561
+ }
1562
+ // If the running environment has a FinalizationRegistry (see
1563
+ // https://github.com/tc39/proposal-weakrefs), then attach finalizers
1564
+ // for class handles. We check for the presence of FinalizationRegistry
1565
+ // at run-time, not build-time.
1566
+ finalizationRegistry = new FinalizationRegistry((info) => {
1567
+ console.warn(info.leakWarning.stack.replace(/^Error: /, ''));
1568
+ releaseClassHandle(info.$$);
1569
+ });
1570
+ attachFinalizer = (handle) => {
1571
+ var $$ = handle.$$;
1572
+ var hasSmartPtr = !!$$.smartPtr;
1573
+ if (hasSmartPtr) {
1574
+ // We should not call the destructor on raw pointers in case other code expects the pointee to live
1575
+ var info = { $$: $$ };
1576
+ // Create a warning as an Error instance in advance so that we can store
1577
+ // the current stacktrace and point to it when / if a leak is detected.
1578
+ // This is more useful than the empty stacktrace of `FinalizationRegistry`
1579
+ // callback.
1580
+ var cls = $$.ptrType.registeredClass;
1581
+ info.leakWarning = new Error(`Embind found a leaked C++ instance ${cls.name} <${ptrToString($$.ptr)}>.\n` +
1582
+ "We'll free it automatically in this case, but this functionality is not reliable across various environments.\n" +
1583
+ "Make sure to invoke .delete() manually once you're done with the instance instead.\n" +
1584
+ "Originally allocated"); // `.stack` will add "at ..." after this sentence
1585
+ if ('captureStackTrace' in Error) {
1586
+ Error.captureStackTrace(info.leakWarning, RegisteredPointer_fromWireType);
1587
+ }
1588
+ finalizationRegistry.register(handle, info, handle);
1589
+ }
1590
+ return handle;
1591
+ };
1592
+ detachFinalizer = (handle) => finalizationRegistry.unregister(handle);
1593
+ return attachFinalizer(handle);
1594
+ };
1595
+
1596
+
1597
+
1598
+ var init_ClassHandle = () => {
1599
+ Object.assign(ClassHandle.prototype, {
1600
+ "isAliasOf"(other) {
1601
+ if (!(this instanceof ClassHandle)) {
1602
+ return false;
1603
+ }
1604
+ if (!(other instanceof ClassHandle)) {
1605
+ return false;
1606
+ }
1607
+
1608
+ var leftClass = this.$$.ptrType.registeredClass;
1609
+ var left = this.$$.ptr;
1610
+ other.$$ = /** @type {Object} */ (other.$$);
1611
+ var rightClass = other.$$.ptrType.registeredClass;
1612
+ var right = other.$$.ptr;
1613
+
1614
+ while (leftClass.baseClass) {
1615
+ left = leftClass.upcast(left);
1616
+ leftClass = leftClass.baseClass;
1617
+ }
1618
+
1619
+ while (rightClass.baseClass) {
1620
+ right = rightClass.upcast(right);
1621
+ rightClass = rightClass.baseClass;
1622
+ }
1623
+
1624
+ return leftClass === rightClass && left === right;
1625
+ },
1626
+
1627
+ "clone"() {
1628
+ if (!this.$$.ptr) {
1629
+ throwInstanceAlreadyDeleted(this);
1630
+ }
1631
+
1632
+ if (this.$$.preservePointerOnDelete) {
1633
+ this.$$.count.value += 1;
1634
+ return this;
1635
+ } else {
1636
+ var clone = attachFinalizer(Object.create(Object.getPrototypeOf(this), {
1637
+ $$: {
1638
+ value: shallowCopyInternalPointer(this.$$),
1639
+ }
1640
+ }));
1641
+
1642
+ clone.$$.count.value += 1;
1643
+ clone.$$.deleteScheduled = false;
1644
+ return clone;
1645
+ }
1646
+ },
1647
+
1648
+ "delete"() {
1649
+ if (!this.$$.ptr) {
1650
+ throwInstanceAlreadyDeleted(this);
1651
+ }
1652
+
1653
+ if (this.$$.deleteScheduled && !this.$$.preservePointerOnDelete) {
1654
+ throwBindingError('Object already scheduled for deletion');
1655
+ }
1656
+
1657
+ detachFinalizer(this);
1658
+ releaseClassHandle(this.$$);
1659
+
1660
+ if (!this.$$.preservePointerOnDelete) {
1661
+ this.$$.smartPtr = undefined;
1662
+ this.$$.ptr = undefined;
1663
+ }
1664
+ },
1665
+
1666
+ "isDeleted"() {
1667
+ return !this.$$.ptr;
1668
+ },
1669
+
1670
+ "deleteLater"() {
1671
+ if (!this.$$.ptr) {
1672
+ throwInstanceAlreadyDeleted(this);
1673
+ }
1674
+ if (this.$$.deleteScheduled && !this.$$.preservePointerOnDelete) {
1675
+ throwBindingError('Object already scheduled for deletion');
1676
+ }
1677
+ deletionQueue.push(this);
1678
+ if (deletionQueue.length === 1 && delayFunction) {
1679
+ delayFunction(flushPendingDeletes);
1403
1680
  }
1681
+ this.$$.deleteScheduled = true;
1682
+ return this;
1683
+ },
1684
+ });
1685
+ };
1686
+ /** @constructor */
1687
+ function ClassHandle() {
1688
+ }
1689
+
1690
+ var createNamedFunction = (name, body) => Object.defineProperty(body, 'name', {
1691
+ value: name
1692
+ });
1693
+
1694
+
1695
+ var ensureOverloadTable = (proto, methodName, humanName) => {
1696
+ if (undefined === proto[methodName].overloadTable) {
1697
+ var prevFunc = proto[methodName];
1698
+ // Inject an overload resolver function that routes to the appropriate overload based on the number of arguments.
1699
+ proto[methodName] = function(...args) {
1700
+ // TODO This check can be removed in -O3 level "unsafe" optimizations.
1701
+ if (!proto[methodName].overloadTable.hasOwnProperty(args.length)) {
1702
+ throwBindingError(`Function '${humanName}' called with an invalid number of arguments (${args.length}) - expects one of (${proto[methodName].overloadTable})!`);
1703
+ }
1704
+ return proto[methodName].overloadTable[args.length].apply(this, args);
1705
+ };
1706
+ // Move the previous function into the overload table.
1707
+ proto[methodName].overloadTable = [];
1708
+ proto[methodName].overloadTable[prevFunc.argCount] = prevFunc;
1709
+ }
1710
+ };
1711
+
1712
+ /** @param {number=} numArguments */
1713
+ var exposePublicSymbol = (name, value, numArguments) => {
1714
+ if (Module.hasOwnProperty(name)) {
1715
+ if (undefined === numArguments || (undefined !== Module[name].overloadTable && undefined !== Module[name].overloadTable[numArguments])) {
1716
+ throwBindingError(`Cannot register public name '${name}' twice`);
1717
+ }
1718
+
1719
+ // We are exposing a function with the same name as an existing function. Create an overload table and a function selector
1720
+ // that routes between the two.
1721
+ ensureOverloadTable(Module, name, name);
1722
+ if (Module.hasOwnProperty(numArguments)) {
1723
+ throwBindingError(`Cannot register multiple overloads of a function with the same number of arguments (${numArguments})!`);
1724
+ }
1725
+ // Add the new function into the overload table.
1726
+ Module[name].overloadTable[numArguments] = value;
1727
+ }
1728
+ else {
1729
+ Module[name] = value;
1730
+ if (undefined !== numArguments) {
1731
+ Module[name].numArguments = numArguments;
1732
+ }
1733
+ }
1734
+ };
1735
+
1736
+ var char_0 = 48;
1737
+
1738
+ var char_9 = 57;
1739
+ var makeLegalFunctionName = (name) => {
1740
+ if (undefined === name) {
1741
+ return '_unknown';
1742
+ }
1743
+ name = name.replace(/[^a-zA-Z0-9_]/g, '$');
1744
+ var f = name.charCodeAt(0);
1745
+ if (f >= char_0 && f <= char_9) {
1746
+ return `_${name}`;
1747
+ }
1748
+ return name;
1749
+ };
1750
+
1751
+
1752
+ /** @constructor */
1753
+ function RegisteredClass(name,
1754
+ constructor,
1755
+ instancePrototype,
1756
+ rawDestructor,
1757
+ baseClass,
1758
+ getActualType,
1759
+ upcast,
1760
+ downcast) {
1761
+ this.name = name;
1762
+ this.constructor = constructor;
1763
+ this.instancePrototype = instancePrototype;
1764
+ this.rawDestructor = rawDestructor;
1765
+ this.baseClass = baseClass;
1766
+ this.getActualType = getActualType;
1767
+ this.upcast = upcast;
1768
+ this.downcast = downcast;
1769
+ this.pureVirtualFunctions = [];
1770
+ }
1771
+
1772
+
1773
+ var upcastPointer = (ptr, ptrClass, desiredClass) => {
1774
+ while (ptrClass !== desiredClass) {
1775
+ if (!ptrClass.upcast) {
1776
+ throwBindingError(`Expected null or instance of ${desiredClass.name}, got an instance of ${ptrClass.name}`);
1777
+ }
1778
+ ptr = ptrClass.upcast(ptr);
1779
+ ptrClass = ptrClass.baseClass;
1780
+ }
1781
+ return ptr;
1782
+ };
1783
+ /** @suppress {globalThis} */
1784
+ function constNoSmartPtrRawPointerToWireType(destructors, handle) {
1785
+ if (handle === null) {
1786
+ if (this.isReference) {
1787
+ throwBindingError(`null is not a valid ${this.name}`);
1788
+ }
1789
+ return 0;
1790
+ }
1791
+
1792
+ if (!handle.$$) {
1793
+ throwBindingError(`Cannot pass "${embindRepr(handle)}" as a ${this.name}`);
1794
+ }
1795
+ if (!handle.$$.ptr) {
1796
+ throwBindingError(`Cannot pass deleted object as a pointer of type ${this.name}`);
1797
+ }
1798
+ var handleClass = handle.$$.ptrType.registeredClass;
1799
+ var ptr = upcastPointer(handle.$$.ptr, handleClass, this.registeredClass);
1800
+ return ptr;
1801
+ }
1802
+
1803
+
1804
+ /** @suppress {globalThis} */
1805
+ function genericPointerToWireType(destructors, handle) {
1806
+ var ptr;
1807
+ if (handle === null) {
1808
+ if (this.isReference) {
1809
+ throwBindingError(`null is not a valid ${this.name}`);
1810
+ }
1811
+
1812
+ if (this.isSmartPointer) {
1813
+ ptr = this.rawConstructor();
1814
+ if (destructors !== null) {
1815
+ destructors.push(this.rawDestructor, ptr);
1816
+ }
1817
+ return ptr;
1818
+ } else {
1819
+ return 0;
1820
+ }
1821
+ }
1822
+
1823
+ if (!handle || !handle.$$) {
1824
+ throwBindingError(`Cannot pass "${embindRepr(handle)}" as a ${this.name}`);
1825
+ }
1826
+ if (!handle.$$.ptr) {
1827
+ throwBindingError(`Cannot pass deleted object as a pointer of type ${this.name}`);
1828
+ }
1829
+ if (!this.isConst && handle.$$.ptrType.isConst) {
1830
+ throwBindingError(`Cannot convert argument of type ${(handle.$$.smartPtrType ? handle.$$.smartPtrType.name : handle.$$.ptrType.name)} to parameter type ${this.name}`);
1831
+ }
1832
+ var handleClass = handle.$$.ptrType.registeredClass;
1833
+ ptr = upcastPointer(handle.$$.ptr, handleClass, this.registeredClass);
1834
+
1835
+ if (this.isSmartPointer) {
1836
+ // TODO: this is not strictly true
1837
+ // We could support BY_EMVAL conversions from raw pointers to smart pointers
1838
+ // because the smart pointer can hold a reference to the handle
1839
+ if (undefined === handle.$$.smartPtr) {
1840
+ throwBindingError('Passing raw pointer to smart pointer is illegal');
1841
+ }
1842
+
1843
+ switch (this.sharingPolicy) {
1844
+ case 0: // NONE
1845
+ // no upcasting
1846
+ if (handle.$$.smartPtrType === this) {
1847
+ ptr = handle.$$.smartPtr;
1848
+ } else {
1849
+ throwBindingError(`Cannot convert argument of type ${(handle.$$.smartPtrType ? handle.$$.smartPtrType.name : handle.$$.ptrType.name)} to parameter type ${this.name}`);
1850
+ }
1851
+ break;
1852
+
1853
+ case 1: // INTRUSIVE
1854
+ ptr = handle.$$.smartPtr;
1855
+ break;
1856
+
1857
+ case 2: // BY_EMVAL
1858
+ if (handle.$$.smartPtrType === this) {
1859
+ ptr = handle.$$.smartPtr;
1860
+ } else {
1861
+ var clonedHandle = handle['clone']();
1862
+ ptr = this.rawShare(
1863
+ ptr,
1864
+ Emval.toHandle(() => clonedHandle['delete']())
1865
+ );
1866
+ if (destructors !== null) {
1867
+ destructors.push(this.rawDestructor, ptr);
1868
+ }
1869
+ }
1870
+ break;
1871
+
1872
+ default:
1873
+ throwBindingError('Unsupporting sharing policy');
1874
+ }
1875
+ }
1876
+ return ptr;
1877
+ }
1878
+
1879
+
1880
+ /** @suppress {globalThis} */
1881
+ function nonConstNoSmartPtrRawPointerToWireType(destructors, handle) {
1882
+ if (handle === null) {
1883
+ if (this.isReference) {
1884
+ throwBindingError(`null is not a valid ${this.name}`);
1885
+ }
1886
+ return 0;
1887
+ }
1888
+
1889
+ if (!handle.$$) {
1890
+ throwBindingError(`Cannot pass "${embindRepr(handle)}" as a ${this.name}`);
1891
+ }
1892
+ if (!handle.$$.ptr) {
1893
+ throwBindingError(`Cannot pass deleted object as a pointer of type ${this.name}`);
1894
+ }
1895
+ if (handle.$$.ptrType.isConst) {
1896
+ throwBindingError(`Cannot convert argument of type ${handle.$$.ptrType.name} to parameter type ${this.name}`);
1897
+ }
1898
+ var handleClass = handle.$$.ptrType.registeredClass;
1899
+ var ptr = upcastPointer(handle.$$.ptr, handleClass, this.registeredClass);
1900
+ return ptr;
1901
+ }
1902
+
1903
+
1904
+
1905
+
1906
+ var init_RegisteredPointer = () => {
1907
+ Object.assign(RegisteredPointer.prototype, {
1908
+ getPointee(ptr) {
1909
+ if (this.rawGetPointee) {
1910
+ ptr = this.rawGetPointee(ptr);
1911
+ }
1912
+ return ptr;
1913
+ },
1914
+ destructor(ptr) {
1915
+ this.rawDestructor?.(ptr);
1916
+ },
1917
+ 'argPackAdvance': GenericWireTypeSize,
1918
+ 'readValueFromPointer': readPointer,
1919
+ 'fromWireType': RegisteredPointer_fromWireType,
1920
+ });
1921
+ };
1922
+ /** @constructor
1923
+ @param {*=} pointeeType,
1924
+ @param {*=} sharingPolicy,
1925
+ @param {*=} rawGetPointee,
1926
+ @param {*=} rawConstructor,
1927
+ @param {*=} rawShare,
1928
+ @param {*=} rawDestructor,
1929
+ */
1930
+ function RegisteredPointer(
1931
+ name,
1932
+ registeredClass,
1933
+ isReference,
1934
+ isConst,
1935
+
1936
+ // smart pointer properties
1937
+ isSmartPointer,
1938
+ pointeeType,
1939
+ sharingPolicy,
1940
+ rawGetPointee,
1941
+ rawConstructor,
1942
+ rawShare,
1943
+ rawDestructor
1944
+ ) {
1945
+ this.name = name;
1946
+ this.registeredClass = registeredClass;
1947
+ this.isReference = isReference;
1948
+ this.isConst = isConst;
1949
+
1950
+ // smart pointer properties
1951
+ this.isSmartPointer = isSmartPointer;
1952
+ this.pointeeType = pointeeType;
1953
+ this.sharingPolicy = sharingPolicy;
1954
+ this.rawGetPointee = rawGetPointee;
1955
+ this.rawConstructor = rawConstructor;
1956
+ this.rawShare = rawShare;
1957
+ this.rawDestructor = rawDestructor;
1958
+
1959
+ if (!isSmartPointer && registeredClass.baseClass === undefined) {
1960
+ if (isConst) {
1961
+ this['toWireType'] = constNoSmartPtrRawPointerToWireType;
1962
+ this.destructorFunction = null;
1963
+ } else {
1964
+ this['toWireType'] = nonConstNoSmartPtrRawPointerToWireType;
1965
+ this.destructorFunction = null;
1966
+ }
1967
+ } else {
1968
+ this['toWireType'] = genericPointerToWireType;
1969
+ // Here we must leave this.destructorFunction undefined, since whether genericPointerToWireType returns
1970
+ // a pointer that needs to be freed up is runtime-dependent, and cannot be evaluated at registration time.
1971
+ // TODO: Create an alternative mechanism that allows removing the use of var destructors = []; array in
1972
+ // craftInvokerFunction altogether.
1973
+ }
1974
+ }
1975
+
1976
+ /** @param {number=} numArguments */
1977
+ var replacePublicSymbol = (name, value, numArguments) => {
1978
+ if (!Module.hasOwnProperty(name)) {
1979
+ throwInternalError('Replacing nonexistent public symbol');
1980
+ }
1981
+ // If there's an overload table for this symbol, replace the symbol in the overload table instead.
1982
+ if (undefined !== Module[name].overloadTable && undefined !== numArguments) {
1983
+ Module[name].overloadTable[numArguments] = value;
1984
+ }
1985
+ else {
1986
+ Module[name] = value;
1987
+ Module[name].argCount = numArguments;
1988
+ }
1989
+ };
1990
+
1991
+
1992
+
1993
+ var dynCallLegacy = (sig, ptr, args) => {
1994
+ assert(('dynCall_' + sig) in Module, `bad function pointer type - dynCall function not found for sig '${sig}'`);
1995
+ if (args?.length) {
1996
+ // j (64-bit integer) must be passed in as two numbers [low 32, high 32].
1997
+ assert(args.length === sig.substring(1).replace(/j/g, '--').length);
1998
+ } else {
1999
+ assert(sig.length == 1);
2000
+ }
2001
+ var f = Module['dynCall_' + sig];
2002
+ return f(ptr, ...args);
2003
+ };
2004
+
2005
+ var wasmTableMirror = [];
2006
+
2007
+ var wasmTable;
2008
+ var getWasmTableEntry = (funcPtr) => {
2009
+ var func = wasmTableMirror[funcPtr];
2010
+ if (!func) {
2011
+ if (funcPtr >= wasmTableMirror.length) wasmTableMirror.length = funcPtr + 1;
2012
+ wasmTableMirror[funcPtr] = func = wasmTable.get(funcPtr);
2013
+ }
2014
+ assert(wasmTable.get(funcPtr) == func, 'JavaScript-side Wasm function table mirror is out of date!');
2015
+ return func;
2016
+ };
2017
+
2018
+ var dynCall = (sig, ptr, args = []) => {
2019
+ // Without WASM_BIGINT support we cannot directly call function with i64 as
2020
+ // part of their signature, so we rely on the dynCall functions generated by
2021
+ // wasm-emscripten-finalize
2022
+ if (sig.includes('j')) {
2023
+ return dynCallLegacy(sig, ptr, args);
2024
+ }
2025
+ assert(getWasmTableEntry(ptr), `missing table entry in dynCall: ${ptr}`);
2026
+ var rtn = getWasmTableEntry(ptr)(...args);
2027
+ return rtn;
2028
+ };
2029
+ var getDynCaller = (sig, ptr) => {
2030
+ assert(sig.includes('j') || sig.includes('p'), 'getDynCaller should only be called with i64 sigs')
2031
+ return (...args) => dynCall(sig, ptr, args);
2032
+ };
2033
+
2034
+
2035
+ var embind__requireFunction = (signature, rawFunction) => {
2036
+ signature = readLatin1String(signature);
2037
+
2038
+ function makeDynCaller() {
2039
+ if (signature.includes('j')) {
2040
+ return getDynCaller(signature, rawFunction);
2041
+ }
2042
+ return getWasmTableEntry(rawFunction);
2043
+ }
2044
+
2045
+ var fp = makeDynCaller();
2046
+ if (typeof fp != "function") {
2047
+ throwBindingError(`unknown function pointer with signature ${signature}: ${rawFunction}`);
2048
+ }
2049
+ return fp;
2050
+ };
2051
+
2052
+
2053
+
2054
+ var extendError = (baseErrorType, errorName) => {
2055
+ var errorClass = createNamedFunction(errorName, function(message) {
2056
+ this.name = errorName;
2057
+ this.message = message;
2058
+
2059
+ var stack = (new Error(message)).stack;
2060
+ if (stack !== undefined) {
2061
+ this.stack = this.toString() + '\n' +
2062
+ stack.replace(/^Error(:[^\n]*)?\n/, '');
2063
+ }
2064
+ });
2065
+ errorClass.prototype = Object.create(baseErrorType.prototype);
2066
+ errorClass.prototype.constructor = errorClass;
2067
+ errorClass.prototype.toString = function() {
2068
+ if (this.message === undefined) {
2069
+ return this.name;
2070
+ } else {
2071
+ return `${this.name}: ${this.message}`;
2072
+ }
2073
+ };
2074
+
2075
+ return errorClass;
2076
+ };
2077
+ var UnboundTypeError;
2078
+
2079
+
2080
+
2081
+ var getTypeName = (type) => {
2082
+ var ptr = ___getTypeName(type);
2083
+ var rv = readLatin1String(ptr);
2084
+ _free(ptr);
2085
+ return rv;
2086
+ };
2087
+ var throwUnboundTypeError = (message, types) => {
2088
+ var unboundTypes = [];
2089
+ var seen = {};
2090
+ function visit(type) {
2091
+ if (seen[type]) {
2092
+ return;
2093
+ }
2094
+ if (registeredTypes[type]) {
2095
+ return;
2096
+ }
2097
+ if (typeDependencies[type]) {
2098
+ typeDependencies[type].forEach(visit);
2099
+ return;
1404
2100
  }
1405
- },
1406
- };
2101
+ unboundTypes.push(type);
2102
+ seen[type] = true;
2103
+ }
2104
+ types.forEach(visit);
1407
2105
 
2106
+ throw new UnboundTypeError(`${message}: ` + unboundTypes.map(getTypeName).join([', ']));
2107
+ };
1408
2108
 
1409
- var EmValType = {
1410
- name: 'emscripten::val',
1411
- 'fromWireType': (handle) => {
1412
- var rv = Emval.toValue(handle);
1413
- __emval_decref(handle);
1414
- return rv;
1415
- },
1416
- 'toWireType': (destructors, value) => Emval.toHandle(value),
1417
- 'argPackAdvance': GenericWireTypeSize,
1418
- 'readValueFromPointer': readPointer,
1419
- destructorFunction: null, // This type does not need a destructor
2109
+ var __embind_register_class = (rawType,
2110
+ rawPointerType,
2111
+ rawConstPointerType,
2112
+ baseClassRawType,
2113
+ getActualTypeSignature,
2114
+ getActualType,
2115
+ upcastSignature,
2116
+ upcast,
2117
+ downcastSignature,
2118
+ downcast,
2119
+ name,
2120
+ destructorSignature,
2121
+ rawDestructor) => {
2122
+ name = readLatin1String(name);
2123
+ getActualType = embind__requireFunction(getActualTypeSignature, getActualType);
2124
+ upcast &&= embind__requireFunction(upcastSignature, upcast);
2125
+ downcast &&= embind__requireFunction(downcastSignature, downcast);
2126
+ rawDestructor = embind__requireFunction(destructorSignature, rawDestructor);
2127
+ var legalFunctionName = makeLegalFunctionName(name);
2128
+
2129
+ exposePublicSymbol(legalFunctionName, function() {
2130
+ // this code cannot run if baseClassRawType is zero
2131
+ throwUnboundTypeError(`Cannot construct ${name} due to unbound types`, [baseClassRawType]);
2132
+ });
1420
2133
 
1421
- // TODO: do we need a deleteObject here? write a test where
1422
- // emval is passed into JS via an interface
2134
+ whenDependentTypesAreResolved(
2135
+ [rawType, rawPointerType, rawConstPointerType],
2136
+ baseClassRawType ? [baseClassRawType] : [],
2137
+ (base) => {
2138
+ base = base[0];
2139
+
2140
+ var baseClass;
2141
+ var basePrototype;
2142
+ if (baseClassRawType) {
2143
+ baseClass = base.registeredClass;
2144
+ basePrototype = baseClass.instancePrototype;
2145
+ } else {
2146
+ basePrototype = ClassHandle.prototype;
2147
+ }
2148
+
2149
+ var constructor = createNamedFunction(name, function(...args) {
2150
+ if (Object.getPrototypeOf(this) !== instancePrototype) {
2151
+ throw new BindingError("Use 'new' to construct " + name);
2152
+ }
2153
+ if (undefined === registeredClass.constructor_body) {
2154
+ throw new BindingError(name + " has no accessible constructor");
2155
+ }
2156
+ var body = registeredClass.constructor_body[args.length];
2157
+ if (undefined === body) {
2158
+ throw new BindingError(`Tried to invoke ctor of ${name} with invalid number of parameters (${args.length}) - expected (${Object.keys(registeredClass.constructor_body).toString()}) parameters instead!`);
2159
+ }
2160
+ return body.apply(this, args);
2161
+ });
2162
+
2163
+ var instancePrototype = Object.create(basePrototype, {
2164
+ constructor: { value: constructor },
2165
+ });
2166
+
2167
+ constructor.prototype = instancePrototype;
2168
+
2169
+ var registeredClass = new RegisteredClass(name,
2170
+ constructor,
2171
+ instancePrototype,
2172
+ rawDestructor,
2173
+ baseClass,
2174
+ getActualType,
2175
+ upcast,
2176
+ downcast);
2177
+
2178
+ if (registeredClass.baseClass) {
2179
+ // Keep track of class hierarchy. Used to allow sub-classes to inherit class functions.
2180
+ registeredClass.baseClass.__derivedClasses ??= [];
2181
+
2182
+ registeredClass.baseClass.__derivedClasses.push(registeredClass);
2183
+ }
2184
+
2185
+ var referenceConverter = new RegisteredPointer(name,
2186
+ registeredClass,
2187
+ true,
2188
+ false,
2189
+ false);
2190
+
2191
+ var pointerConverter = new RegisteredPointer(name + '*',
2192
+ registeredClass,
2193
+ false,
2194
+ false,
2195
+ false);
2196
+
2197
+ var constPointerConverter = new RegisteredPointer(name + ' const*',
2198
+ registeredClass,
2199
+ false,
2200
+ true,
2201
+ false);
2202
+
2203
+ registeredPointers[rawType] = {
2204
+ pointerType: pointerConverter,
2205
+ constPointerType: constPointerConverter
2206
+ };
2207
+
2208
+ replacePublicSymbol(legalFunctionName, constructor);
2209
+
2210
+ return [referenceConverter, pointerConverter, constPointerConverter];
2211
+ }
2212
+ );
1423
2213
  };
1424
- var __embind_register_emval = (rawType) => registerType(rawType, EmValType);
1425
2214
 
1426
- var embindRepr = (v) => {
1427
- if (v === null) {
1428
- return 'null';
1429
- }
1430
- var t = typeof v;
1431
- if (t === 'object' || t === 'array' || t === 'function') {
1432
- return v.toString();
1433
- } else {
1434
- return '' + v;
2215
+ var heap32VectorToArray = (count, firstElement) => {
2216
+ var array = [];
2217
+ for (var i = 0; i < count; i++) {
2218
+ // TODO(https://github.com/emscripten-core/emscripten/issues/17310):
2219
+ // Find a way to hoist the `>> 2` or `>> 3` out of this loop.
2220
+ array.push(HEAPU32[(((firstElement)+(i * 4))>>2)]);
1435
2221
  }
2222
+ return array;
1436
2223
  };
1437
2224
 
1438
- var floatReadValueFromPointer = (name, width) => {
1439
- switch (width) {
1440
- case 4: return function(pointer) {
1441
- return this['fromWireType'](HEAPF32[((pointer)>>2)]);
1442
- };
1443
- case 8: return function(pointer) {
1444
- return this['fromWireType'](HEAPF64[((pointer)>>3)]);
1445
- };
1446
- default:
1447
- throw new TypeError(`invalid float width (${width}): ${name}`);
1448
- }
1449
- };
1450
2225
 
1451
2226
 
1452
- var __embind_register_float = (rawType, name, size) => {
1453
- name = readLatin1String(name);
1454
- registerType(rawType, {
1455
- name,
1456
- 'fromWireType': (value) => value,
1457
- 'toWireType': (destructors, value) => {
1458
- if (typeof value != "number" && typeof value != "boolean") {
1459
- throw new TypeError(`Cannot convert ${embindRepr(value)} to ${this.name}`);
1460
- }
1461
- // The VM will perform JS to Wasm value conversion, according to the spec:
1462
- // https://www.w3.org/TR/wasm-js-api-1/#towebassemblyvalue
1463
- return value;
1464
- },
1465
- 'argPackAdvance': GenericWireTypeSize,
1466
- 'readValueFromPointer': floatReadValueFromPointer(name, size),
1467
- destructorFunction: null, // This type does not need a destructor
1468
- });
1469
- };
1470
-
1471
- var createNamedFunction = (name, body) => Object.defineProperty(body, 'name', {
1472
- value: name
1473
- });
2227
+
2228
+
2229
+
1474
2230
 
1475
2231
 
1476
2232
 
@@ -1622,203 +2378,254 @@ function dbg(...args) {
1622
2378
  var invokerFn = newFunc(Function, args)(...closureArgs);
1623
2379
  return createNamedFunction(humanName, invokerFn);
1624
2380
  }
2381
+ var __embind_register_class_constructor = (
2382
+ rawClassType,
2383
+ argCount,
2384
+ rawArgTypesAddr,
2385
+ invokerSignature,
2386
+ invoker,
2387
+ rawConstructor
2388
+ ) => {
2389
+ assert(argCount > 0);
2390
+ var rawArgTypes = heap32VectorToArray(argCount, rawArgTypesAddr);
2391
+ invoker = embind__requireFunction(invokerSignature, invoker);
2392
+ var args = [rawConstructor];
2393
+ var destructors = [];
1625
2394
 
1626
- var ensureOverloadTable = (proto, methodName, humanName) => {
1627
- if (undefined === proto[methodName].overloadTable) {
1628
- var prevFunc = proto[methodName];
1629
- // Inject an overload resolver function that routes to the appropriate overload based on the number of arguments.
1630
- proto[methodName] = function(...args) {
1631
- // TODO This check can be removed in -O3 level "unsafe" optimizations.
1632
- if (!proto[methodName].overloadTable.hasOwnProperty(args.length)) {
1633
- throwBindingError(`Function '${humanName}' called with an invalid number of arguments (${args.length}) - expects one of (${proto[methodName].overloadTable})!`);
1634
- }
1635
- return proto[methodName].overloadTable[args.length].apply(this, args);
2395
+ whenDependentTypesAreResolved([], [rawClassType], (classType) => {
2396
+ classType = classType[0];
2397
+ var humanName = `constructor ${classType.name}`;
2398
+
2399
+ if (undefined === classType.registeredClass.constructor_body) {
2400
+ classType.registeredClass.constructor_body = [];
2401
+ }
2402
+ if (undefined !== classType.registeredClass.constructor_body[argCount - 1]) {
2403
+ throw new BindingError(`Cannot register multiple constructors with identical number of parameters (${argCount-1}) for class '${classType.name}'! Overload resolution is currently only performed using the parameter count, not actual type info!`);
2404
+ }
2405
+ classType.registeredClass.constructor_body[argCount - 1] = () => {
2406
+ throwUnboundTypeError(`Cannot construct ${classType.name} due to unbound types`, rawArgTypes);
1636
2407
  };
1637
- // Move the previous function into the overload table.
1638
- proto[methodName].overloadTable = [];
1639
- proto[methodName].overloadTable[prevFunc.argCount] = prevFunc;
2408
+
2409
+ whenDependentTypesAreResolved([], rawArgTypes, (argTypes) => {
2410
+ // Insert empty slot for context type (argTypes[1]).
2411
+ argTypes.splice(1, 0, null);
2412
+ classType.registeredClass.constructor_body[argCount - 1] = craftInvokerFunction(humanName, argTypes, null, invoker, rawConstructor);
2413
+ return [];
2414
+ });
2415
+ return [];
2416
+ });
2417
+ };
2418
+
2419
+
2420
+
2421
+
2422
+
2423
+
2424
+
2425
+ var getFunctionName = (signature) => {
2426
+ signature = signature.trim();
2427
+ const argsIndex = signature.indexOf("(");
2428
+ if (argsIndex !== -1) {
2429
+ assert(signature[signature.length - 1] == ")", "Parentheses for argument names should match.");
2430
+ return signature.substr(0, argsIndex);
2431
+ } else {
2432
+ return signature;
1640
2433
  }
1641
2434
  };
2435
+ var __embind_register_class_function = (rawClassType,
2436
+ methodName,
2437
+ argCount,
2438
+ rawArgTypesAddr, // [ReturnType, ThisType, Args...]
2439
+ invokerSignature,
2440
+ rawInvoker,
2441
+ context,
2442
+ isPureVirtual,
2443
+ isAsync) => {
2444
+ var rawArgTypes = heap32VectorToArray(argCount, rawArgTypesAddr);
2445
+ methodName = readLatin1String(methodName);
2446
+ methodName = getFunctionName(methodName);
2447
+ rawInvoker = embind__requireFunction(invokerSignature, rawInvoker);
2448
+
2449
+ whenDependentTypesAreResolved([], [rawClassType], (classType) => {
2450
+ classType = classType[0];
2451
+ var humanName = `${classType.name}.${methodName}`;
2452
+
2453
+ if (methodName.startsWith("@@")) {
2454
+ methodName = Symbol[methodName.substring(2)];
2455
+ }
1642
2456
 
1643
- /** @param {number=} numArguments */
1644
- var exposePublicSymbol = (name, value, numArguments) => {
1645
- if (Module.hasOwnProperty(name)) {
1646
- if (undefined === numArguments || (undefined !== Module[name].overloadTable && undefined !== Module[name].overloadTable[numArguments])) {
1647
- throwBindingError(`Cannot register public name '${name}' twice`);
2457
+ if (isPureVirtual) {
2458
+ classType.registeredClass.pureVirtualFunctions.push(methodName);
1648
2459
  }
1649
2460
 
1650
- // We are exposing a function with the same name as an existing function. Create an overload table and a function selector
1651
- // that routes between the two.
1652
- ensureOverloadTable(Module, name, name);
1653
- if (Module.hasOwnProperty(numArguments)) {
1654
- throwBindingError(`Cannot register multiple overloads of a function with the same number of arguments (${numArguments})!`);
2461
+ function unboundTypesHandler() {
2462
+ throwUnboundTypeError(`Cannot call ${humanName} due to unbound types`, rawArgTypes);
1655
2463
  }
1656
- // Add the new function into the overload table.
1657
- Module[name].overloadTable[numArguments] = value;
1658
- }
1659
- else {
1660
- Module[name] = value;
1661
- if (undefined !== numArguments) {
1662
- Module[name].numArguments = numArguments;
2464
+
2465
+ var proto = classType.registeredClass.instancePrototype;
2466
+ var method = proto[methodName];
2467
+ if (undefined === method || (undefined === method.overloadTable && method.className !== classType.name && method.argCount === argCount - 2)) {
2468
+ // This is the first overload to be registered, OR we are replacing a
2469
+ // function in the base class with a function in the derived class.
2470
+ unboundTypesHandler.argCount = argCount - 2;
2471
+ unboundTypesHandler.className = classType.name;
2472
+ proto[methodName] = unboundTypesHandler;
2473
+ } else {
2474
+ // There was an existing function with the same name registered. Set up
2475
+ // a function overload routing table.
2476
+ ensureOverloadTable(proto, methodName, humanName);
2477
+ proto[methodName].overloadTable[argCount - 2] = unboundTypesHandler;
1663
2478
  }
1664
- }
2479
+
2480
+ whenDependentTypesAreResolved([], rawArgTypes, (argTypes) => {
2481
+ var memberFunction = craftInvokerFunction(humanName, argTypes, classType, rawInvoker, context, isAsync);
2482
+
2483
+ // Replace the initial unbound-handler-stub function with the
2484
+ // appropriate member function, now that all types are resolved. If
2485
+ // multiple overloads are registered for this function, the function
2486
+ // goes into an overload table.
2487
+ if (undefined === proto[methodName].overloadTable) {
2488
+ // Set argCount in case an overload is registered later
2489
+ memberFunction.argCount = argCount - 2;
2490
+ proto[methodName] = memberFunction;
2491
+ } else {
2492
+ proto[methodName].overloadTable[argCount - 2] = memberFunction;
2493
+ }
2494
+
2495
+ return [];
2496
+ });
2497
+ return [];
2498
+ });
1665
2499
  };
2500
+
1666
2501
 
1667
- var heap32VectorToArray = (count, firstElement) => {
1668
- var array = [];
1669
- for (var i = 0; i < count; i++) {
1670
- // TODO(https://github.com/emscripten-core/emscripten/issues/17310):
1671
- // Find a way to hoist the `>> 2` or `>> 3` out of this loop.
1672
- array.push(HEAPU32[(((firstElement)+(i * 4))>>2)]);
2502
+ var emval_freelist = [];
2503
+
2504
+ var emval_handles = [];
2505
+ var __emval_decref = (handle) => {
2506
+ if (handle > 9 && 0 === --emval_handles[handle + 1]) {
2507
+ assert(emval_handles[handle] !== undefined, `Decref for unallocated handle.`);
2508
+ emval_handles[handle] = undefined;
2509
+ emval_freelist.push(handle);
1673
2510
  }
1674
- return array;
1675
2511
  };
1676
2512
 
1677
2513
 
1678
- /** @param {number=} numArguments */
1679
- var replacePublicSymbol = (name, value, numArguments) => {
1680
- if (!Module.hasOwnProperty(name)) {
1681
- throwInternalError('Replacing nonexistent public symbol');
1682
- }
1683
- // If there's an overload table for this symbol, replace the symbol in the overload table instead.
1684
- if (undefined !== Module[name].overloadTable && undefined !== numArguments) {
1685
- Module[name].overloadTable[numArguments] = value;
1686
- }
1687
- else {
1688
- Module[name] = value;
1689
- Module[name].argCount = numArguments;
2514
+
2515
+
2516
+
2517
+ var count_emval_handles = () => {
2518
+ return emval_handles.length / 2 - 5 - emval_freelist.length;
2519
+ };
2520
+
2521
+ var init_emval = () => {
2522
+ // reserve 0 and some special values. These never get de-allocated.
2523
+ emval_handles.push(
2524
+ 0, 1,
2525
+ undefined, 1,
2526
+ null, 1,
2527
+ true, 1,
2528
+ false, 1,
2529
+ );
2530
+ assert(emval_handles.length === 5 * 2);
2531
+ Module['count_emval_handles'] = count_emval_handles;
2532
+ };
2533
+ var Emval = {
2534
+ toValue:(handle) => {
2535
+ if (!handle) {
2536
+ throwBindingError('Cannot use deleted val. handle = ' + handle);
2537
+ }
2538
+ // handle 2 is supposed to be `undefined`.
2539
+ assert(handle === 2 || emval_handles[handle] !== undefined && handle % 2 === 0, `invalid handle: ${handle}`);
2540
+ return emval_handles[handle];
2541
+ },
2542
+ toHandle:(value) => {
2543
+ switch (value) {
2544
+ case undefined: return 2;
2545
+ case null: return 4;
2546
+ case true: return 6;
2547
+ case false: return 8;
2548
+ default:{
2549
+ const handle = emval_freelist.pop() || emval_handles.length;
2550
+ emval_handles[handle] = value;
2551
+ emval_handles[handle + 1] = 1;
2552
+ return handle;
2553
+ }
2554
+ }
2555
+ },
2556
+ };
2557
+
2558
+
2559
+ var EmValType = {
2560
+ name: 'emscripten::val',
2561
+ 'fromWireType': (handle) => {
2562
+ var rv = Emval.toValue(handle);
2563
+ __emval_decref(handle);
2564
+ return rv;
2565
+ },
2566
+ 'toWireType': (destructors, value) => Emval.toHandle(value),
2567
+ 'argPackAdvance': GenericWireTypeSize,
2568
+ 'readValueFromPointer': readPointer,
2569
+ destructorFunction: null, // This type does not need a destructor
2570
+
2571
+ // TODO: do we need a deleteObject here? write a test where
2572
+ // emval is passed into JS via an interface
2573
+ };
2574
+ var __embind_register_emval = (rawType) => registerType(rawType, EmValType);
2575
+
2576
+ var embindRepr = (v) => {
2577
+ if (v === null) {
2578
+ return 'null';
1690
2579
  }
1691
- };
1692
-
1693
-
1694
-
1695
- var dynCallLegacy = (sig, ptr, args) => {
1696
- assert(('dynCall_' + sig) in Module, `bad function pointer type - dynCall function not found for sig '${sig}'`);
1697
- if (args?.length) {
1698
- // j (64-bit integer) must be passed in as two numbers [low 32, high 32].
1699
- assert(args.length === sig.substring(1).replace(/j/g, '--').length);
2580
+ var t = typeof v;
2581
+ if (t === 'object' || t === 'array' || t === 'function') {
2582
+ return v.toString();
1700
2583
  } else {
1701
- assert(sig.length == 1);
1702
- }
1703
- var f = Module['dynCall_' + sig];
1704
- return f(ptr, ...args);
1705
- };
1706
-
1707
- var wasmTableMirror = [];
1708
-
1709
- var wasmTable;
1710
- var getWasmTableEntry = (funcPtr) => {
1711
- var func = wasmTableMirror[funcPtr];
1712
- if (!func) {
1713
- if (funcPtr >= wasmTableMirror.length) wasmTableMirror.length = funcPtr + 1;
1714
- wasmTableMirror[funcPtr] = func = wasmTable.get(funcPtr);
2584
+ return '' + v;
1715
2585
  }
1716
- assert(wasmTable.get(funcPtr) == func, 'JavaScript-side Wasm function table mirror is out of date!');
1717
- return func;
1718
2586
  };
1719
2587
 
1720
- var dynCall = (sig, ptr, args = []) => {
1721
- // Without WASM_BIGINT support we cannot directly call function with i64 as
1722
- // part of their signature, so we rely on the dynCall functions generated by
1723
- // wasm-emscripten-finalize
1724
- if (sig.includes('j')) {
1725
- return dynCallLegacy(sig, ptr, args);
2588
+ var floatReadValueFromPointer = (name, width) => {
2589
+ switch (width) {
2590
+ case 4: return function(pointer) {
2591
+ return this['fromWireType'](HEAPF32[((pointer)>>2)]);
2592
+ };
2593
+ case 8: return function(pointer) {
2594
+ return this['fromWireType'](HEAPF64[((pointer)>>3)]);
2595
+ };
2596
+ default:
2597
+ throw new TypeError(`invalid float width (${width}): ${name}`);
1726
2598
  }
1727
- assert(getWasmTableEntry(ptr), `missing table entry in dynCall: ${ptr}`);
1728
- var rtn = getWasmTableEntry(ptr)(...args);
1729
- return rtn;
1730
- };
1731
- var getDynCaller = (sig, ptr) => {
1732
- assert(sig.includes('j') || sig.includes('p'), 'getDynCaller should only be called with i64 sigs')
1733
- return (...args) => dynCall(sig, ptr, args);
1734
2599
  };
1735
2600
 
1736
2601
 
1737
- var embind__requireFunction = (signature, rawFunction) => {
1738
- signature = readLatin1String(signature);
1739
-
1740
- function makeDynCaller() {
1741
- if (signature.includes('j')) {
1742
- return getDynCaller(signature, rawFunction);
1743
- }
1744
- return getWasmTableEntry(rawFunction);
1745
- }
1746
-
1747
- var fp = makeDynCaller();
1748
- if (typeof fp != "function") {
1749
- throwBindingError(`unknown function pointer with signature ${signature}: ${rawFunction}`);
1750
- }
1751
- return fp;
2602
+ var __embind_register_float = (rawType, name, size) => {
2603
+ name = readLatin1String(name);
2604
+ registerType(rawType, {
2605
+ name,
2606
+ 'fromWireType': (value) => value,
2607
+ 'toWireType': (destructors, value) => {
2608
+ if (typeof value != "number" && typeof value != "boolean") {
2609
+ throw new TypeError(`Cannot convert ${embindRepr(value)} to ${this.name}`);
2610
+ }
2611
+ // The VM will perform JS to Wasm value conversion, according to the spec:
2612
+ // https://www.w3.org/TR/wasm-js-api-1/#towebassemblyvalue
2613
+ return value;
2614
+ },
2615
+ 'argPackAdvance': GenericWireTypeSize,
2616
+ 'readValueFromPointer': floatReadValueFromPointer(name, size),
2617
+ destructorFunction: null, // This type does not need a destructor
2618
+ });
1752
2619
  };
2620
+
1753
2621
 
1754
2622
 
1755
2623
 
1756
- var extendError = (baseErrorType, errorName) => {
1757
- var errorClass = createNamedFunction(errorName, function(message) {
1758
- this.name = errorName;
1759
- this.message = message;
1760
-
1761
- var stack = (new Error(message)).stack;
1762
- if (stack !== undefined) {
1763
- this.stack = this.toString() + '\n' +
1764
- stack.replace(/^Error(:[^\n]*)?\n/, '');
1765
- }
1766
- });
1767
- errorClass.prototype = Object.create(baseErrorType.prototype);
1768
- errorClass.prototype.constructor = errorClass;
1769
- errorClass.prototype.toString = function() {
1770
- if (this.message === undefined) {
1771
- return this.name;
1772
- } else {
1773
- return `${this.name}: ${this.message}`;
1774
- }
1775
- };
1776
-
1777
- return errorClass;
1778
- };
1779
- var UnboundTypeError;
1780
-
1781
2624
 
1782
2625
 
1783
- var getTypeName = (type) => {
1784
- var ptr = ___getTypeName(type);
1785
- var rv = readLatin1String(ptr);
1786
- _free(ptr);
1787
- return rv;
1788
- };
1789
- var throwUnboundTypeError = (message, types) => {
1790
- var unboundTypes = [];
1791
- var seen = {};
1792
- function visit(type) {
1793
- if (seen[type]) {
1794
- return;
1795
- }
1796
- if (registeredTypes[type]) {
1797
- return;
1798
- }
1799
- if (typeDependencies[type]) {
1800
- typeDependencies[type].forEach(visit);
1801
- return;
1802
- }
1803
- unboundTypes.push(type);
1804
- seen[type] = true;
1805
- }
1806
- types.forEach(visit);
1807
2626
 
1808
- throw new UnboundTypeError(`${message}: ` + unboundTypes.map(getTypeName).join([', ']));
1809
- };
1810
2627
 
1811
2628
 
1812
- var getFunctionName = (signature) => {
1813
- signature = signature.trim();
1814
- const argsIndex = signature.indexOf("(");
1815
- if (argsIndex !== -1) {
1816
- assert(signature[signature.length - 1] == ")", "Parentheses for argument names should match.");
1817
- return signature.substr(0, argsIndex);
1818
- } else {
1819
- return signature;
1820
- }
1821
- };
1822
2629
  var __embind_register_function = (name, argCount, rawArgTypesAddr, signature, rawInvoker, fn, isAsync) => {
1823
2630
  var argTypes = heap32VectorToArray(argCount, rawArgTypesAddr);
1824
2631
  name = readLatin1String(name);
@@ -1937,6 +2744,10 @@ function dbg(...args) {
1937
2744
  });
1938
2745
  };
1939
2746
 
2747
+ var __embind_register_optional = (rawOptionalType, rawType) => {
2748
+ __embind_register_emval(rawOptionalType);
2749
+ };
2750
+
1940
2751
 
1941
2752
 
1942
2753
 
@@ -2431,32 +3242,6 @@ function dbg(...args) {
2431
3242
  });
2432
3243
  };
2433
3244
 
2434
-
2435
-
2436
-
2437
- var requireRegisteredType = (rawType, humanName) => {
2438
- var impl = registeredTypes[rawType];
2439
- if (undefined === impl) {
2440
- throwBindingError(`${humanName} has unknown type ${getTypeName(rawType)}`);
2441
- }
2442
- return impl;
2443
- };
2444
-
2445
- var emval_returnValue = (returnType, destructorsRef, handle) => {
2446
- var destructors = [];
2447
- var result = returnType['toWireType'](destructors, handle);
2448
- if (destructors.length) {
2449
- // void, primitives and any other types w/o destructors don't need to allocate a handle
2450
- HEAPU32[((destructorsRef)>>2)] = Emval.toHandle(destructors);
2451
- }
2452
- return result;
2453
- };
2454
- var __emval_as = (handle, returnType, destructorsRef) => {
2455
- handle = Emval.toValue(handle);
2456
- returnType = requireRegisteredType(returnType, 'emval::as');
2457
- return emval_returnValue(returnType, destructorsRef, handle);
2458
- };
2459
-
2460
3245
  var emval_methodCallers = [];
2461
3246
 
2462
3247
  var __emval_call = (caller, handle, destructorsRef, args) => {
@@ -2501,6 +3286,15 @@ function dbg(...args) {
2501
3286
  return id;
2502
3287
  };
2503
3288
 
3289
+
3290
+
3291
+ var requireRegisteredType = (rawType, humanName) => {
3292
+ var impl = registeredTypes[rawType];
3293
+ if (undefined === impl) {
3294
+ throwBindingError(`${humanName} has unknown type ${getTypeName(rawType)}`);
3295
+ }
3296
+ return impl;
3297
+ };
2504
3298
  var emval_lookupTypes = (argCount, argTypes) => {
2505
3299
  var a = new Array(argCount);
2506
3300
  for (var i = 0; i < argCount; ++i) {
@@ -2513,6 +3307,15 @@ function dbg(...args) {
2513
3307
 
2514
3308
  var reflectConstruct = Reflect.construct;
2515
3309
 
3310
+ var emval_returnValue = (returnType, destructorsRef, handle) => {
3311
+ var destructors = [];
3312
+ var result = returnType['toWireType'](destructors, handle);
3313
+ if (destructors.length) {
3314
+ // void, primitives and any other types w/o destructors don't need to allocate a handle
3315
+ HEAPU32[((destructorsRef)>>2)] = Emval.toHandle(destructors);
3316
+ }
3317
+ return result;
3318
+ };
2516
3319
 
2517
3320
  var __emval_get_method_caller = (argCount, argTypes, kind) => {
2518
3321
  var types = emval_lookupTypes(argCount, argTypes);
@@ -2555,11 +3358,7 @@ function dbg(...args) {
2555
3358
  return emval_addMethodCaller(createNamedFunction(functionName, invokerFunction));
2556
3359
  };
2557
3360
 
2558
- var __emval_get_property = (handle, key) => {
2559
- handle = Emval.toValue(handle);
2560
- key = Emval.toValue(key);
2561
- return Emval.toHandle(handle[key]);
2562
- };
3361
+ var __emval_new_array = () => Emval.toHandle([]);
2563
3362
 
2564
3363
 
2565
3364
  var __emval_new_cstring = (v) => Emval.toHandle(getStringOrSymbol(v));
@@ -2744,8 +3543,11 @@ function dbg(...args) {
2744
3543
  InternalError = Module['InternalError'] = class InternalError extends Error { constructor(message) { super(message); this.name = 'InternalError'; }};
2745
3544
  embind_init_charCodes();
2746
3545
  BindingError = Module['BindingError'] = class BindingError extends Error { constructor(message) { super(message); this.name = 'BindingError'; }};
2747
- init_emval();;
3546
+ init_ClassHandle();
3547
+ init_embind();;
3548
+ init_RegisteredPointer();
2748
3549
  UnboundTypeError = Module['UnboundTypeError'] = extendError(Error, 'UnboundTypeError');;
3550
+ init_emval();;
2749
3551
  function checkIncomingModuleAPI() {
2750
3552
  ignoredModuleProp('fetchSettings');
2751
3553
  }
@@ -2759,6 +3561,12 @@ var wasmImports = {
2759
3561
  /** @export */
2760
3562
  _embind_register_bool: __embind_register_bool,
2761
3563
  /** @export */
3564
+ _embind_register_class: __embind_register_class,
3565
+ /** @export */
3566
+ _embind_register_class_constructor: __embind_register_class_constructor,
3567
+ /** @export */
3568
+ _embind_register_class_function: __embind_register_class_function,
3569
+ /** @export */
2762
3570
  _embind_register_emval: __embind_register_emval,
2763
3571
  /** @export */
2764
3572
  _embind_register_float: __embind_register_float,
@@ -2769,6 +3577,8 @@ var wasmImports = {
2769
3577
  /** @export */
2770
3578
  _embind_register_memory_view: __embind_register_memory_view,
2771
3579
  /** @export */
3580
+ _embind_register_optional: __embind_register_optional,
3581
+ /** @export */
2772
3582
  _embind_register_std_string: __embind_register_std_string,
2773
3583
  /** @export */
2774
3584
  _embind_register_std_wstring: __embind_register_std_wstring,
@@ -2779,8 +3589,6 @@ var wasmImports = {
2779
3589
  /** @export */
2780
3590
  _embind_register_void: __embind_register_void,
2781
3591
  /** @export */
2782
- _emval_as: __emval_as,
2783
- /** @export */
2784
3592
  _emval_call: __emval_call,
2785
3593
  /** @export */
2786
3594
  _emval_decref: __emval_decref,
@@ -2789,7 +3597,7 @@ var wasmImports = {
2789
3597
  /** @export */
2790
3598
  _emval_get_method_caller: __emval_get_method_caller,
2791
3599
  /** @export */
2792
- _emval_get_property: __emval_get_property,
3600
+ _emval_new_array: __emval_new_array,
2793
3601
  /** @export */
2794
3602
  _emval_new_cstring: __emval_new_cstring,
2795
3603
  /** @export */
@@ -3006,38 +3814,10 @@ var missingLibrarySymbols = [
3006
3814
  'demangle',
3007
3815
  'getFunctionArgsName',
3008
3816
  'createJsInvokerSignature',
3009
- 'init_embind',
3010
- 'getBasestPointer',
3011
3817
  'registerInheritedInstance',
3012
3818
  'unregisterInheritedInstance',
3013
- 'getInheritedInstance',
3014
- 'getInheritedInstanceCount',
3015
- 'getLiveInheritedInstances',
3016
3819
  'enumReadValueFromPointer',
3017
- 'genericPointerToWireType',
3018
- 'constNoSmartPtrRawPointerToWireType',
3019
- 'nonConstNoSmartPtrRawPointerToWireType',
3020
- 'init_RegisteredPointer',
3021
- 'RegisteredPointer',
3022
- 'RegisteredPointer_fromWireType',
3023
- 'runDestructor',
3024
- 'releaseClassHandle',
3025
- 'detachFinalizer',
3026
- 'attachFinalizer',
3027
- 'makeClassHandle',
3028
- 'init_ClassHandle',
3029
- 'ClassHandle',
3030
- 'throwInstanceAlreadyDeleted',
3031
- 'flushPendingDeletes',
3032
- 'setDelayFunction',
3033
- 'RegisteredClass',
3034
- 'shallowCopyInternalPointer',
3035
- 'downcastPointer',
3036
- 'upcastPointer',
3037
3820
  'validateThis',
3038
- 'char_0',
3039
- 'char_9',
3040
- 'makeLegalFunctionName',
3041
3821
  ];
3042
3822
  missingLibrarySymbols.forEach(missingLibrarySymbol)
3043
3823
 
@@ -3172,6 +3952,7 @@ var unexportedSymbols = [
3172
3952
  'PureVirtualError',
3173
3953
  'GenericWireTypeSize',
3174
3954
  'EmValType',
3955
+ 'init_embind',
3175
3956
  'throwUnboundTypeError',
3176
3957
  'ensureOverloadTable',
3177
3958
  'exposePublicSymbol',
@@ -3180,6 +3961,10 @@ var unexportedSymbols = [
3180
3961
  'createNamedFunction',
3181
3962
  'embindRepr',
3182
3963
  'registeredInstances',
3964
+ 'getBasestPointer',
3965
+ 'getInheritedInstance',
3966
+ 'getInheritedInstanceCount',
3967
+ 'getLiveInheritedInstances',
3183
3968
  'registeredPointers',
3184
3969
  'registerType',
3185
3970
  'integerReadValueFromPointer',
@@ -3189,10 +3974,33 @@ var unexportedSymbols = [
3189
3974
  'newFunc',
3190
3975
  'craftInvokerFunction',
3191
3976
  'embind__requireFunction',
3977
+ 'genericPointerToWireType',
3978
+ 'constNoSmartPtrRawPointerToWireType',
3979
+ 'nonConstNoSmartPtrRawPointerToWireType',
3980
+ 'init_RegisteredPointer',
3981
+ 'RegisteredPointer',
3982
+ 'RegisteredPointer_fromWireType',
3983
+ 'runDestructor',
3984
+ 'releaseClassHandle',
3192
3985
  'finalizationRegistry',
3193
3986
  'detachFinalizer_deps',
3987
+ 'detachFinalizer',
3988
+ 'attachFinalizer',
3989
+ 'makeClassHandle',
3990
+ 'init_ClassHandle',
3991
+ 'ClassHandle',
3992
+ 'throwInstanceAlreadyDeleted',
3194
3993
  'deletionQueue',
3994
+ 'flushPendingDeletes',
3195
3995
  'delayFunction',
3996
+ 'setDelayFunction',
3997
+ 'RegisteredClass',
3998
+ 'shallowCopyInternalPointer',
3999
+ 'downcastPointer',
4000
+ 'upcastPointer',
4001
+ 'char_0',
4002
+ 'char_9',
4003
+ 'makeLegalFunctionName',
3196
4004
  'emval_freelist',
3197
4005
  'emval_handles',
3198
4006
  'emval_symbols',