Ver Fonte

Add state functions to synchronous buffers

Fabian há 3 anos atrás
pai
commit
bb529ccfec
3 ficheiros alterados com 33 adições e 6 exclusões
  1. 19 3
      src/browser/lib.js
  2. 12 0
      src/lib.js
  3. 2 3
      tests/api/state.js

+ 19 - 3
src/browser/lib.js

@@ -442,21 +442,23 @@ var ASYNC_SAFE = false;
     AsyncXHRBuffer.prototype.get_state = function()
     {
         const state = [];
-
         const loaded_blocks = [];
-        for(let [index, block] of Object.values(this.loaded_blocks))
+
+        for(let [index, block] of Object.entries(this.loaded_blocks))
         {
             dbg_assert(isFinite(+index));
             loaded_blocks.push([+index, block]);
         }
-        state[0] = loaded_blocks;
 
+        state[0] = loaded_blocks;
         return state;
     };
+
     AsyncXHRBuffer.prototype.set_state = function(state)
     {
         const loaded_blocks = state[0];
         this.loaded_blocks = Object.create(null);
+
         for(let [index, block] of Object.values(loaded_blocks))
         {
             this.loaded_blocks[index] = block;
@@ -557,6 +559,20 @@ var ASYNC_SAFE = false;
         fn(this.buffer);
     };
 
+    SyncFileBuffer.prototype.get_state = function()
+    {
+        const state = [];
+        state[0] = this.byteLength;
+        state[1] = new Uint8Array(this.buffer);
+        return state;
+    };
+
+    SyncFileBuffer.prototype.set_state = function(state)
+    {
+        this.byteLength = state[0];
+        this.buffer = state[1].slice().buffer;
+    };
+
     /**
      * Asynchronous access to File, loading blocks from the input type=file
      *

+ 12 - 0
src/lib.js

@@ -155,7 +155,19 @@ SyncBuffer.prototype.get_buffer = function(fn)
     fn(this.buffer);
 };
 
+SyncBuffer.prototype.get_state = function()
+{
+    const state = [];
+    state[0] = this.byteLength;
+    state[1] = new Uint8Array(this.buffer);
+    return state;
+};
 
+SyncBuffer.prototype.set_state = function(state)
+{
+    this.byteLength = state[0];
+    this.buffer = state[1].slice().buffer;
+};
 
 (function()
 {

+ 2 - 3
tests/api/state.js

@@ -53,7 +53,7 @@ function run_test(name, config, done)
                                 {
                                     console.log("Done: %s", name);
                                     emulator.stop();
-                                    done();
+                                    done && done();
                                 }, 1000);
                         }, 1000);
                 });
@@ -62,6 +62,5 @@ function run_test(name, config, done)
 
 run_test("async cdrom", config_async_cdrom, function()
     {
-        // XXX: Fails currently
-        //run_test("sync cdrom", config_sync_cdrom, function() {});
+        run_test("sync cdrom", config_sync_cdrom);
     });