index.d.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /// <reference lib="esnext.bigint" />
  2. export interface ResultObject {
  3. module: WebAssembly.Module;
  4. instance: WebAssembly.Instance;
  5. }
  6. /** WebAssembly imports with an optional env object and two levels of nesting. */
  7. export type Imports = {
  8. [key: string]: Record<string,unknown>;
  9. env?: {
  10. memory?: WebAssembly.Memory;
  11. table?: WebAssembly.Table;
  12. seed?(): number;
  13. abort?(msg: number, file: number, line: number, column: number): void;
  14. trace?(msg: number, numArgs?: number, ...args: number[]): void;
  15. };
  16. };
  17. /** Utility mixed in by the loader. */
  18. export interface ASUtil {
  19. memory?: WebAssembly.Memory;
  20. table?: WebAssembly.Table;
  21. /** Explicit start function, if requested. */
  22. _start(): void;
  23. /** Allocates a new string in the module's memory and returns a reference (pointer) to it. */
  24. __allocString(str: string): number;
  25. /** Allocates a new array in the module's memory and returns a reference (pointer) to it. */
  26. __allocArray(id: number, values: ArrayLike<number>): number;
  27. /** Copies a string's value from the module's memory. */
  28. __getString(ptr: number): string;
  29. /** Copies an ArrayBuffer's value from the module's memory. */
  30. __getArrayBuffer(ptr: number): ArrayBuffer;
  31. /** Copies an array's values from the module's memory. Infers the array type from RTTI. */
  32. __getArray(ptr: number): number[];
  33. /** Copies an Int8Array's values from the module's memory. */
  34. __getInt8Array(ptr: number): Int8Array;
  35. /** Copies an Uint8Array's values from the module's memory. */
  36. __getUint8Array(ptr: number): Uint8Array;
  37. /** Copies an Uint8ClampedArray's values from the module's memory. */
  38. __getUint8ClampedArray(ptr: number): Uint8ClampedArray;
  39. /** Copies an Int16Array's values from the module's memory. */
  40. __getInt16Array(ptr: number): Int16Array;
  41. /** Copies an Uint16Array's values from the module's memory. */
  42. __getUint16Array(ptr: number): Uint16Array;
  43. /** Copies an Int32Array's values from the module's memory. */
  44. __getInt32Array(ptr: number): Int32Array;
  45. /** Copies an Uint32Array's values from the module's memory. */
  46. __getUint32Array(ptr: number): Uint32Array;
  47. /** Copies an Int32Array's values from the module's memory. */
  48. __getInt64Array?(ptr: number): BigInt64Array;
  49. /** Copies an Uint32Array's values from the module's memory. */
  50. __getUint64Array?(ptr: number): BigUint64Array;
  51. /** Copies a Float32Array's values from the module's memory. */
  52. __getFloat32Array(ptr: number): Float32Array;
  53. /** Copies a Float64Array's values from the module's memory. */
  54. __getFloat64Array(ptr: number): Float64Array;
  55. /** Gets a live view on an array's values in the module's memory. Infers the array type from RTTI. */
  56. __getArrayView(ptr: number): ArrayBufferView;
  57. /** Gets a live view on an Int8Array's values in the module's memory. */
  58. __getInt8ArrayView(ptr: number): Int8Array;
  59. /** Gets a live view on an Uint8Array's values in the module's memory. */
  60. __getUint8ArrayView(ptr: number): Uint8Array;
  61. /** Gets a live view on an Uint8ClampedArray's values in the module's memory. */
  62. __getUint8ClampedArrayView(ptr: number): Uint8ClampedArray;
  63. /** Gets a live view on an Int16Array's values in the module's memory. */
  64. __getInt16ArrayView(ptr: number): Int16Array;
  65. /** Gets a live view on an Uint16Array's values in the module's memory. */
  66. __getUint16ArrayView(ptr: number): Uint16Array;
  67. /** Gets a live view on an Int32Array's values in the module's memory. */
  68. __getInt32ArrayView(ptr: number): Int32Array;
  69. /** Gets a live view on an Uint32Array's values in the module's memory. */
  70. __getUint32ArrayView(ptr: number): Uint32Array;
  71. /** Gets a live view on an Int32Array's values in the module's memory. */
  72. __getInt64ArrayView?(ptr: number): BigInt64Array;
  73. /** Gets a live view on an Uint32Array's values in the module's memory. */
  74. __getUint64ArrayView?(ptr: number): BigUint64Array;
  75. /** Gets a live view on a Float32Array's values in the module's memory. */
  76. __getFloat32ArrayView(ptr: number): Float32Array;
  77. /** Gets a live view on a Float64Array's values in the module's memory. */
  78. __getFloat64ArrayView(ptr: number): Float64Array;
  79. /** Retains a reference to a managed object externally, making sure that it doesn't become collected prematurely. Returns the pointer. */
  80. __retain(ptr: number): number;
  81. /** Releases a previously retained reference to a managed object, allowing the runtime to collect it once its reference count reaches zero. */
  82. __release(ptr: number): void;
  83. /** Forcefully resets the heap to its initial offset, effectively clearing dynamic memory. Stub runtime only. */
  84. __reset?(): void;
  85. /** Allocates an instance of the class represented by the specified id. */
  86. __alloc(size: number, id: number): number;
  87. /** Tests whether a managed object is an instance of the class represented by the specified base id. */
  88. __instanceof(ptr: number, baseId: number): boolean;
  89. /** Forces a cycle collection. Only relevant if objects potentially forming reference cycles are used. */
  90. __collect(): void;
  91. }
  92. /** Asynchronously instantiates an AssemblyScript module from anything that can be instantiated. */
  93. export declare function instantiate<T extends Record<string,unknown>>(
  94. source: WebAssembly.Module | BufferSource | Response | PromiseLike<WebAssembly.Module | BufferSource | Response>,
  95. imports?: Imports
  96. ): Promise<ResultObject & { exports: ASUtil & T }>;
  97. /** Synchronously instantiates an AssemblyScript module from a WebAssembly.Module or binary buffer. */
  98. export declare function instantiateSync<T extends Record<string,unknown>>(
  99. source: WebAssembly.Module | BufferSource,
  100. imports?: Imports
  101. ): ResultObject & { exports: ASUtil & T };
  102. /** Asynchronously instantiates an AssemblyScript module from a response, i.e. as obtained by `fetch`. */
  103. export declare function instantiateStreaming<T extends Record<string,unknown>>(
  104. source: Response | PromiseLike<Response>,
  105. imports?: Imports
  106. ): Promise<ResultObject & { exports: ASUtil & T }>;
  107. /** Demangles an AssemblyScript module's exports to a friendly object structure. */
  108. export declare function demangle<T extends Record<string,unknown>>(
  109. exports: Record<string,unknown>,
  110. extendedExports?: Record<string,unknown>
  111. ): T;