memory.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.ModuleMemory = void 0;
  4. const lifetime_1 = require("./lifetime");
  5. /**
  6. * @private
  7. */
  8. class ModuleMemory {
  9. constructor(module) {
  10. this.module = module;
  11. }
  12. toPointerArray(handleArray) {
  13. const typedArray = new Int32Array(handleArray.map((handle) => handle.value));
  14. const numBytes = typedArray.length * typedArray.BYTES_PER_ELEMENT;
  15. const ptr = this.module._malloc(numBytes);
  16. var heapBytes = new Uint8Array(this.module.HEAPU8.buffer, ptr, numBytes);
  17. heapBytes.set(new Uint8Array(typedArray.buffer));
  18. return new lifetime_1.Lifetime(ptr, undefined, (ptr) => this.module._free(ptr));
  19. }
  20. newMutablePointerArray(length) {
  21. const zeros = new Int32Array(new Array(length).fill(0));
  22. const numBytes = zeros.length * zeros.BYTES_PER_ELEMENT;
  23. const ptr = this.module._malloc(numBytes);
  24. const typedArray = new Int32Array(this.module.HEAPU8.buffer, ptr, length);
  25. typedArray.set(zeros);
  26. return new lifetime_1.Lifetime({ typedArray, ptr }, undefined, (value) => this.module._free(value.ptr));
  27. }
  28. newHeapCharPointer(string) {
  29. const numBytes = this.module.lengthBytesUTF8(string) + 1;
  30. const ptr = this.module._malloc(numBytes);
  31. this.module.stringToUTF8(string, ptr, numBytes);
  32. return new lifetime_1.Lifetime(ptr, undefined, (value) => this.module._free(value));
  33. }
  34. consumeHeapCharPointer(ptr) {
  35. const str = this.module.UTF8ToString(ptr);
  36. this.module._free(ptr);
  37. return str;
  38. }
  39. }
  40. exports.ModuleMemory = ModuleMemory;
  41. //# sourceMappingURL=memory.js.map