Răsfoiți Sursa

Update base binary reader

Lutz Roeder 3 ani în urmă
părinte
comite
62af655123
10 a modificat fișierele cu 41 adăugiri și 19 ștergeri
  1. 2 2
      source/base.js
  2. 1 1
      source/dl4j.js
  3. 2 1
      source/electron.js
  4. 22 3
      source/index.js
  5. 4 2
      source/kmodel.js
  6. 5 5
      source/mlnet.js
  7. 1 1
      source/msgpack.js
  8. 1 1
      source/onnx.js
  9. 2 2
      source/tf.js
  10. 1 1
      source/weka.js

+ 2 - 2
source/base.js

@@ -620,7 +620,6 @@ base.BinaryReader = class {
         this._position = 0;
         this._length = this._buffer.length;
         this._view = new DataView(this._buffer.buffer, this._buffer.byteOffset, this._buffer.byteLength);
-        this._utf8 = new TextDecoder('utf-8');
     }
 
     get length() {
@@ -720,7 +719,8 @@ base.BinaryReader = class {
         const position = this._position;
         this.skip(length);
         const data = this._buffer.subarray(position, this._position);
-        return this._utf8.decode(data);
+        this._decoder = this._decoder || new TextDecoder('utf-8');
+        return this._decoder.decode(data);
     }
 };
 

+ 1 - 1
source/dl4j.js

@@ -550,7 +550,6 @@ dl4j.BinaryReader = class {
         this._buffer = buffer;
         this._position = 0;
         this._view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
-        this._decoder = new TextDecoder('ascii');
     }
 
     bytes(size) {
@@ -562,6 +561,7 @@ dl4j.BinaryReader = class {
     string() {
         const size = this._buffer[this._position++] << 8 | this._buffer[this._position++];
         const buffer = this.bytes(size);
+        this._decoder = this._decoder || new TextDecoder('ascii');
         return this._decoder.decode(buffer);
     }
 

+ 2 - 1
source/electron.js

@@ -726,5 +726,6 @@ window.addEventListener('load', () => {
     global.protobuf = require('./protobuf');
     global.flatbuffers = require('./flatbuffers');
     const view = require('./view');
-    window.__view__ = new view.View(new host.ElectronHost());
+    window.__host__ = new host.ElectronHost();
+    window.__view__ = new view.View(window.__host__);
 });

+ 22 - 3
source/index.js

@@ -28,7 +28,6 @@ host.BrowserHost = class {
         this._telemetry = this._version && this._version !== '0.0.0';
         this._environment = new Map();
         this._environment.set('zoom', 'scroll');
-        // this._environment.set('zoom', 'drag');
     }
 
     get window() {
@@ -69,6 +68,9 @@ host.BrowserHost = class {
                             this.window.ga('create', 'UA-54146-13', 'auto');
                             this.window.ga('set', 'anonymizeIp', true);
                         }
+                        if (typeof fetch === 'undefined') {
+                            this.event('Host', 'Browser', 'fetch', 1);
+                        }
                         resolve();
                     };
                     script.onerror = () => {
@@ -841,6 +843,10 @@ host.BrowserHost.BrowserContext = class {
 if (typeof TextDecoder === "undefined") {
     TextDecoder = function TextDecoder(encoding) {
         this._encoding = encoding;
+        if (window.__host__ && !TextEncoder.__event__) {
+            TextEncoder.__event__ = true;
+            window.__host__.event('Host', 'Browser', 'TextDecoder', 1);
+        }
     };
     TextDecoder.prototype.decode = function decode(buffer) {
         let result = '';
@@ -893,6 +899,10 @@ if (typeof TextDecoder === "undefined") {
 
 if (typeof TextEncoder === 'undefined') {
     TextEncoder = function TextEncoder() {
+        if (window.__host__ && !TextEncoder.__event__) {
+            TextEncoder.__event__ = true;
+            window.__host__.event('Host', 'Browser', 'TextEncoder', 1);
+        }
     };
     TextEncoder.prototype.encode = function encode(str) {
         "use strict";
@@ -937,7 +947,7 @@ if (typeof TextEncoder === 'undefined') {
                 resArr[resPos += 1] = (0x2<<6) | (point&0x3f);
             }
         }
-        if (typeof Uint8Array!=="undefined") {
+        if (typeof Uint8Array !== "undefined") {
             return new Uint8Array(resArr.buffer.slice(0, resPos+1));
         }
         return resArr.length === resPos + 1 ? resArr : resArr.slice(0, resPos + 1);
@@ -965,6 +975,10 @@ if (typeof TextEncoder === 'undefined') {
 
 if (typeof URLSearchParams === 'undefined') {
     URLSearchParams = function URLSearchParams(search) {
+        if (window.__host__ && !URLSearchParams.__event__) {
+            URLSearchParams.__event__ = true;
+            window.__host__.event('Host', 'Browser', 'URLSearchParams', 1);
+        }
         const decode = (str) => {
             return str.replace(/[ +]/g, '%20').replace(/(%[a-f0-9]{2})+/ig, (match) => { return decodeURIComponent(match); });
         };
@@ -990,6 +1004,10 @@ if (typeof URLSearchParams === 'undefined') {
 
 if (!HTMLCanvasElement.prototype.toBlob) {
     HTMLCanvasElement.prototype.toBlob = function(callback, type, quality) {
+        if (window.__host__ && !HTMLCanvasElement.__event__) {
+            HTMLCanvasElement.__event__ = true;
+            window.__host__.event('Host', 'Browser', 'HTMLCanvasElement.toBlob', 1);
+        }
         const canvas = this;
         setTimeout(function() {
             const data = atob(canvas.toDataURL(type, quality).split(',')[1]);
@@ -1044,5 +1062,6 @@ if (!('scrollBehavior' in window.document.documentElement.style)) {
 }
 
 window.addEventListener('load', () => {
-    window.__view__ = new view.View(new host.BrowserHost());
+    window.__host__ = new host.BrowserHost();
+    window.__view__ = new view.View(window.__host__);
 });

+ 4 - 2
source/kmodel.js

@@ -1056,7 +1056,8 @@ kmodel.Reader = class {
                     };
                     reader.module_type_t = function() {
                         const buffer = reader.read(16);
-                        return new TextDecoder('ascii').decode(buffer);
+                        const decoder = new TextDecoder('ascii');
+                        return decoder.decode(buffer);
                     };
                     reader.module_header = function() {
                         return {
@@ -1079,8 +1080,9 @@ kmodel.Reader = class {
                         };
                     };
                     reader.section_header = function() {
+                        const decoder = new TextDecoder('ascii');
                         return {
-                            name: new TextDecoder('ascii').decode(reader.read(16)),
+                            name: decoder.decode(reader.read(16)),
                             flags: reader.uint32(),
                             body_start: reader.uint32(),
                             body_size: reader.uint32(),

+ 5 - 5
source/mlnet.js

@@ -544,7 +544,7 @@ mlnet.ModelHeader = class {
         if (data) {
             const reader = new mlnet.Reader(data);
 
-            const textDecoder = new TextDecoder('ascii');
+            const decoder = new TextDecoder('ascii');
             reader.assert('ML\0MODEL');
             this.versionWritten = reader.uint32();
             this.versionReadable = reader.uint32();
@@ -555,11 +555,11 @@ mlnet.ModelHeader = class {
             const stringTableSize = reader.uint64();
             const stringCharsOffset = reader.uint64();
             /* v stringCharsSize = */ reader.uint64();
-            this.modelSignature = textDecoder.decode(reader.bytes(8));
+            this.modelSignature = decoder.decode(reader.bytes(8));
             this.modelVersionWritten = reader.uint32();
             this.modelVersionReadable = reader.uint32();
-            this.loaderSignature = textDecoder.decode(reader.bytes(24).filter((c) => c != 0));
-            this.loaderSignatureAlt = textDecoder.decode(reader.bytes(24).filter((c) => c != 0));
+            this.loaderSignature = decoder.decode(reader.bytes(24).filter((c) => c != 0));
+            this.loaderSignatureAlt = decoder.decode(reader.bytes(24).filter((c) => c != 0));
             const tailOffset = reader.uint64();
             /* let tailLimit = */ reader.uint64();
             const assemblyNameOffset = reader.uint64();
@@ -587,7 +587,7 @@ mlnet.ModelHeader = class {
             }
             if (assemblyNameOffset != 0) {
                 reader.seek(assemblyNameOffset);
-                this.assemblyName = textDecoder.decode(reader.bytes(assemblyNameSize));
+                this.assemblyName = decoder.decode(reader.bytes(assemblyNameSize));
             }
             reader.seek(tailOffset);
             reader.assert('LEDOM\0LM');

+ 1 - 1
source/msgpack.js

@@ -14,7 +14,6 @@ msgpack.BinaryReader = class {
         this._callback = callback;
         this._position = 0;
         this._view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
-        this._decoder = new TextDecoder('utf8');
     }
 
     read() {
@@ -178,6 +177,7 @@ msgpack.BinaryReader = class {
 
     string(size) {
         const buffer = this.bytes(size);
+        this._decoder = this._decoder || new TextDecoder('utf8');
         return this._decoder.decode(buffer);
     }
 };

+ 1 - 1
source/onnx.js

@@ -1539,7 +1539,6 @@ onnx.GraphContext = class {
 
     constructor(context, nodes) {
         this._context = context;
-        this._decoder = new TextDecoder('utf-8');
         this._dataTypes = new Map(Object.entries(onnx.DataType).map((entry) => [ entry[1], entry[0].toLowerCase() ]));
         this._dataTypes.set(onnx.DataType.UNDEFINED, 'UNDEFINED');
         this._dataTypes.set(onnx.DataType.BOOL, 'boolean');
@@ -1707,6 +1706,7 @@ onnx.GraphContext = class {
         if (typeof value === 'string') {
             return value;
         }
+        this._decoder = this._decoder || new TextDecoder('utf-8');
         return this._decoder.decode(value);
     }
 

+ 2 - 2
source/tf.js

@@ -1784,7 +1784,7 @@ tf.TensorBundle.Table.Block = class {
         for (let i = 0; i < numRestarts; i++) {
             restartOffsets.push(reader.int32());
         }
-        const textDecoder = new TextDecoder();
+        const decoder = new TextDecoder();
         for (let i = 0; i < numRestarts; i++) {
             reader.seek(restartOffsets[i]);
             let key = '';
@@ -1796,7 +1796,7 @@ tf.TensorBundle.Table.Block = class {
                     break;
                 }
                 key = key.substring(0, sharedSize);
-                key = key + textDecoder.decode(reader.read(nonSharedSize));
+                key = key + decoder.decode(reader.read(nonSharedSize));
                 const value = reader.read(valueSize);
                 this.entries.set(key, value);
             }

+ 1 - 1
source/weka.js

@@ -205,7 +205,6 @@ java.io.InputObjectStream.BinaryReader = class {
         this._position = 0;
         this._length = buffer.length;
         this._view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
-        this._decoder = new TextDecoder('utf-8');
     }
 
     skip(offset) {
@@ -243,6 +242,7 @@ java.io.InputObjectStream.BinaryReader = class {
         const size = long ? this.uint64().toNumber() : this.uint16();
         const position = this._position;
         this.skip(size);
+        this._decoder = this._decoder || new TextDecoder('utf-8');
         return this._decoder.decode(this._buffer.subarray(position, this._position));
     }
 };