@aptre/v86 0.6.0 → 0.6.2

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.
@@ -19336,26 +19336,24 @@ var V86Starter = (() => {
19336
19336
  );
19337
19337
  }
19338
19338
  resize_memory(new_size) {
19339
- const mem8_offset = this.mem8.byteOffset;
19340
- const needed_total = mem8_offset + new_size;
19341
- const current_buffer = this.wasm_memory.buffer.byteLength;
19342
- if (needed_total > current_buffer) {
19343
- const grow_pages = Math.ceil(
19344
- (needed_total - current_buffer) / WASM_PAGE_SIZE
19345
- );
19346
- this.wasm_memory.grow(grow_pages);
19347
- }
19348
- this.mem8 = view(Uint8Array, this.wasm_memory, mem8_offset, new_size);
19349
- this.mem32s = view(
19350
- Int32Array,
19351
- this.wasm_memory,
19352
- mem8_offset,
19353
- new_size >> 2
19354
- );
19339
+ const offset = this.mem8.byteOffset;
19340
+ const needed = offset + new_size;
19341
+ const current = this.wasm_memory.buffer.byteLength;
19342
+ if (needed > current) {
19343
+ const pages = Math.ceil((needed - current) / WASM_PAGE_SIZE);
19344
+ this.wasm_memory.grow(pages);
19345
+ }
19346
+ this.mem8 = view(Uint8Array, this.wasm_memory, offset, new_size);
19347
+ this.mem32s = view(Uint32Array, this.wasm_memory, offset, new_size >> 2);
19355
19348
  this.memory_size[0] = new_size;
19356
19349
  }
19357
19350
  set_state(state) {
19358
- this.memory_size[0] = state[0];
19351
+ const saved_memory_size = state[0];
19352
+ if (saved_memory_size > this.memory_size[0]) {
19353
+ this.resize_memory(saved_memory_size);
19354
+ } else {
19355
+ this.memory_size[0] = saved_memory_size;
19356
+ }
19359
19357
  if (this.mem8.length !== this.memory_size[0]) {
19360
19358
  console.warn(
19361
19359
  "Note: Memory size mismatch. we=" + this.mem8.length + " state=" + this.memory_size[0]
@@ -26211,12 +26209,24 @@ ${e.stack || e.message}`
26211
26209
  this.bus = bus[0];
26212
26210
  this.emulator_bus = bus[1];
26213
26211
  let cpu;
26214
- let wasm_memory;
26212
+ const memory_size = options.memory_size || 64 * 1024 * 1024;
26213
+ const vga_memory_size = options.vga_memory_size || 8 * 1024 * 1024;
26214
+ const memory_max = options.memory_max || (memory_size + vga_memory_size) * 4;
26215
+ const wasm_initial_pages = 256;
26216
+ const wasm_max_pages = Math.max(
26217
+ wasm_initial_pages,
26218
+ Math.min(Math.ceil(memory_max / WASM_PAGE_SIZE), 65536)
26219
+ );
26220
+ const wasm_memory = new WebAssembly.Memory({
26221
+ initial: wasm_initial_pages,
26222
+ maximum: wasm_max_pages
26223
+ });
26215
26224
  const wasm_table = new WebAssembly.Table({
26216
26225
  element: "anyfunc",
26217
26226
  initial: WASM_TABLE_SIZE + WASM_TABLE_OFFSET
26218
26227
  });
26219
26228
  const wasm_shared_funcs = {
26229
+ memory: wasm_memory,
26220
26230
  cpu_exception_hook: (n) => this.cpu_exception_hook(n),
26221
26231
  run_hardware_timers: function(a, t) {
26222
26232
  return cpu.run_hardware_timers(a, t);
@@ -26351,7 +26361,6 @@ ${e.stack || e.message}`
26351
26361
  };
26352
26362
  }
26353
26363
  wasm_fn({ env: wasm_shared_funcs }).then((exports) => {
26354
- wasm_memory = exports.memory;
26355
26364
  exports["rust_init"]();
26356
26365
  const emulator = this.v86 = new v86(this.emulator_bus, {
26357
26366
  exports,
@@ -26878,6 +26887,26 @@ ${e.stack || e.message}`
26878
26887
  async run() {
26879
26888
  this.v86.run();
26880
26889
  }
26890
+ /**
26891
+ * Grow guest memory to the specified size in bytes. Stops the VM,
26892
+ * grows WASM linear memory, updates memory_size, clears the TLB,
26893
+ * and resumes execution.
26894
+ */
26895
+ async growMemory(newSizeBytes) {
26896
+ const cpu = this.v86.cpu;
26897
+ if (newSizeBytes <= cpu.memory_size[0]) {
26898
+ return;
26899
+ }
26900
+ const wasRunning = this.cpu_is_running;
26901
+ if (wasRunning) {
26902
+ await this.stop();
26903
+ }
26904
+ cpu.resize_memory(newSizeBytes);
26905
+ cpu.full_clear_tlb();
26906
+ if (wasRunning) {
26907
+ await this.run();
26908
+ }
26909
+ }
26881
26910
  /**
26882
26911
  * Stop emulation. Do nothing if emulator is not running. Can be asynchronous.
26883
26912
  */