|
1 | 1 | using System;
|
| 2 | +using System.Collections.Generic; |
2 | 3 | using System.Runtime.InteropServices;
|
3 | 4 | using System.Text;
|
4 | 5 |
|
@@ -579,6 +580,101 @@ public Instance(Store store, Module module, params object[] imports)
|
579 | 580 | return _store.GetCachedExtern(ext.of.global);
|
580 | 581 | }
|
581 | 582 |
|
| 583 | + /// <summary> |
| 584 | + /// Get all exported functions |
| 585 | + /// </summary> |
| 586 | + /// <returns>An enumerable of functions exported from this instance</returns> |
| 587 | + public IEnumerable<(string Name, Function Function)> GetFunctions() |
| 588 | + { |
| 589 | + for (var i = 0; i < int.MaxValue; i++) |
| 590 | + { |
| 591 | + if (TryGetExtern(i, ExternKind.Func) is not var (name, @extern)) |
| 592 | + { |
| 593 | + break; |
| 594 | + } |
| 595 | + |
| 596 | + yield return (name, _store.GetCachedExtern(@extern.of.func)); |
| 597 | + } |
| 598 | + |
| 599 | + GC.KeepAlive(_store); |
| 600 | + } |
| 601 | + |
| 602 | + /// <summary> |
| 603 | + /// Get all exported tables |
| 604 | + /// </summary> |
| 605 | + /// <returns>An enumerable of tables exported from this instance</returns> |
| 606 | + public IEnumerable<(string Name, Table Table)> GetTables() |
| 607 | + { |
| 608 | + for (var i = 0; i < int.MaxValue; i++) |
| 609 | + { |
| 610 | + if (TryGetExtern(i, ExternKind.Table) is not var (name, @extern)) |
| 611 | + { |
| 612 | + break; |
| 613 | + } |
| 614 | + |
| 615 | + yield return (name, new Table(_store, @extern.of.table)); |
| 616 | + } |
| 617 | + |
| 618 | + GC.KeepAlive(_store); |
| 619 | + } |
| 620 | + |
| 621 | + /// <summary> |
| 622 | + /// Get all exported memories |
| 623 | + /// </summary> |
| 624 | + /// <returns>An enumerable of memories exported from this instance</returns> |
| 625 | + public IEnumerable<(string Name, Memory Memory)> GetMemories() |
| 626 | + { |
| 627 | + for (var i = 0; i < int.MaxValue; i++) |
| 628 | + { |
| 629 | + if (TryGetExtern(i, ExternKind.Memory) is not var (name, @extern)) |
| 630 | + { |
| 631 | + break; |
| 632 | + } |
| 633 | + |
| 634 | + yield return (name, _store.GetCachedExtern(@extern.of.memory)); |
| 635 | + } |
| 636 | + |
| 637 | + GC.KeepAlive(_store); |
| 638 | + } |
| 639 | + |
| 640 | + /// <summary> |
| 641 | + /// Get all exported globals |
| 642 | + /// </summary> |
| 643 | + /// <returns>An enumerable of globals exported from this instance</returns> |
| 644 | + public IEnumerable<(string Name, Global Global)> GetGlobals() |
| 645 | + { |
| 646 | + for (var i = 0; i < int.MaxValue; i++) |
| 647 | + { |
| 648 | + if (TryGetExtern(i, ExternKind.Global) is not var (name, @extern)) |
| 649 | + { |
| 650 | + break; |
| 651 | + } |
| 652 | + |
| 653 | + yield return (name, _store.GetCachedExtern(@extern.of.global)); |
| 654 | + } |
| 655 | + |
| 656 | + GC.KeepAlive(_store); |
| 657 | + } |
| 658 | + |
| 659 | + private (string name, Extern @extern)? TryGetExtern(int index, ExternKind? type = null) |
| 660 | + { |
| 661 | + unsafe |
| 662 | + { |
| 663 | + if (!Native.wasmtime_instance_export_nth(_store.Context.handle, instance, (UIntPtr)index, out var namePtr, out var nameLen, out var @extern)) |
| 664 | + { |
| 665 | + return null; |
| 666 | + } |
| 667 | + |
| 668 | + if (type != null && type.Value != @extern.kind) |
| 669 | + { |
| 670 | + return null; |
| 671 | + } |
| 672 | + |
| 673 | + var name = Encoding.UTF8.GetString(namePtr, checked((int)nameLen)); |
| 674 | + return (name, @extern); |
| 675 | + } |
| 676 | + } |
| 677 | + |
582 | 678 | private bool TryGetExtern(StoreContext context, string name, out Extern ext)
|
583 | 679 | {
|
584 | 680 | using var nameBytes = name.ToUTF8(stackalloc byte[Math.Min(64, name.Length * 2)]);
|
@@ -615,9 +711,6 @@ private static class Native
|
615 | 711 | [DllImport(Engine.LibraryName)]
|
616 | 712 | [return: MarshalAs(UnmanagedType.I1)]
|
617 | 713 | public static extern unsafe bool wasmtime_instance_export_nth(IntPtr context, in ExternInstance instance, UIntPtr index, out byte* name, out UIntPtr len, out Extern ext);
|
618 |
| - |
619 |
| - [DllImport(Engine.LibraryName)] |
620 |
| - public static extern void wasmtime_instancetype_delete(IntPtr handle); |
621 | 714 | }
|
622 | 715 |
|
623 | 716 | private readonly Store _store;
|
|
0 commit comments