From 01e2d114aea1abd006fd12fad8eb1ce626ac0c47 Mon Sep 17 00:00:00 2001 From: Seok NamKoong Date: Tue, 8 Nov 2022 11:08:52 +0900 Subject: [PATCH 01/10] base 1 --- src/CircleEditor/CircleEditorProvider.ts | 122 +++++++---------------- 1 file changed, 37 insertions(+), 85 deletions(-) diff --git a/src/CircleEditor/CircleEditorProvider.ts b/src/CircleEditor/CircleEditorProvider.ts index 7d8bacb6..d883b1d1 100644 --- a/src/CircleEditor/CircleEditorProvider.ts +++ b/src/CircleEditor/CircleEditorProvider.ts @@ -24,38 +24,6 @@ import {getUri} from '../Utils/external/Uri'; import {CircleEditorDocument} from './CircleEditorDocument'; -/** - * Message commands for communicating with webviews - */ -export enum MessageDefs { - // message command - alert = 'alert', - request = 'request', - response = 'response', - pageloaded = 'pageloaded', - loadmodel = 'loadmodel', - finishload = 'finishload', - reload = 'reload', - selection = 'selection', - backendColor = 'backendColor', - error = 'error', - colorTheme = 'colorTheme', - // loadmodel type - modelpath = 'modelpath', - uint8array = 'uint8array', - // selection - names = 'names', - tensors = 'tensors', - // partiton of backends - partition = 'partition', - // commands for custom editor features - edit = 'edit', - loadJson = 'loadJson', - updateJson = 'updateJson', - getCustomOpAttrT = 'getCustomOpAttrT', - requestEncodingData = 'requestEncodingData' -} - /** * Custom Editor Provider necessary for vscode extension API */ @@ -112,34 +80,34 @@ export class CircleEditorProvider implements vscode.CustomEditorProvider { + _token: vscode.CancellationToken): Promise { this.webviews.add(document.uri, webviewPanel); // Setup initial content for the webview webviewPanel.webview.options = { enableScripts: true, }; - webviewPanel.webview.html = this.getHtmlForWebview(webviewPanel.webview); + webviewPanel.webview.html = await this.getHtmlForWebview(webviewPanel.webview); webviewPanel.webview.onDidReceiveMessage((e) => this.onMessage(document, e)); } - saveCustomDocument(document: CircleEditorDocument, cancellation: vscode.CancellationToken): + public saveCustomDocument(document: CircleEditorDocument, cancellation: vscode.CancellationToken): Thenable { return document.save(cancellation); } - saveCustomDocumentAs( + public saveCustomDocumentAs( document: CircleEditorDocument, destination: vscode.Uri, cancellation: vscode.CancellationToken): Thenable { return document.saveAs(destination, cancellation); } - revertCustomDocument(document: CircleEditorDocument, cancellation: vscode.CancellationToken): + public revertCustomDocument(document: CircleEditorDocument, cancellation: vscode.CancellationToken): Thenable { return document.revert(cancellation); } - backupCustomDocument( + public backupCustomDocument( document: CircleEditorDocument, context: vscode.CustomDocumentBackupContext, cancellation: vscode.CancellationToken): Thenable { return document.backup(context.destination, cancellation); @@ -151,39 +119,32 @@ export class CircleEditorProvider implements vscode.CustomEditorProvider { const codiconUri = getUri( webview, this._context.extensionUri, ['node_modules', '@vscode', 'codicons', 'dist', 'codicon.css']); - let html = fs.readFileSync(htmlUrl.fsPath, {encoding: 'utf-8'}); const nonce = getNonce(); + + const htmlUri = vscode.Uri.joinPath(this._context.extensionUri, 'media', 'CircleEditor', 'index.html'); + let html = Buffer.from(await vscode.workspace.fs.readFile(htmlUri)).toString(); html = html.replace(/%nonce%/gi, nonce); html = html.replace('%webview.cspSource%', webview.cspSource); html = html.replace(/\${codiconUri}/g, `${codiconUri}`); @@ -222,9 +183,10 @@ export class CircleEditorProvider implements vscode.CustomEditorProvider Date: Tue, 8 Nov 2022 11:09:57 +0900 Subject: [PATCH 02/10] Base 2 --- media/CircleEditor/external/.FORMATDENY | 0 media/CircleEditor/external/LICENSE | 21 - media/CircleEditor/external/ORIGIN | 3 - media/CircleEditor/external/base.js | 694 --- media/CircleEditor/external/circle-schema.js | 2367 ---------- media/CircleEditor/external/circle.js | 856 ---- media/CircleEditor/external/dagre.js | 2251 ---------- media/CircleEditor/external/flatbuffers.js | 392 -- media/CircleEditor/external/flexbuffers.js | 198 - media/CircleEditor/external/gzip.js | 234 - media/CircleEditor/external/json.js | 574 --- media/CircleEditor/external/protobuf.js | 1357 ------ media/CircleEditor/external/python.js | 4134 ------------------ media/CircleEditor/external/tar.js | 171 - media/CircleEditor/external/text.js | 345 -- media/CircleEditor/external/xml.js | 1799 -------- media/CircleEditor/external/zip.js | 713 --- 17 files changed, 16109 deletions(-) delete mode 100644 media/CircleEditor/external/.FORMATDENY delete mode 100644 media/CircleEditor/external/LICENSE delete mode 100644 media/CircleEditor/external/ORIGIN delete mode 100644 media/CircleEditor/external/base.js delete mode 100644 media/CircleEditor/external/circle-schema.js delete mode 100644 media/CircleEditor/external/circle.js delete mode 100644 media/CircleEditor/external/dagre.js delete mode 100644 media/CircleEditor/external/flatbuffers.js delete mode 100644 media/CircleEditor/external/flexbuffers.js delete mode 100644 media/CircleEditor/external/gzip.js delete mode 100644 media/CircleEditor/external/json.js delete mode 100644 media/CircleEditor/external/protobuf.js delete mode 100644 media/CircleEditor/external/python.js delete mode 100644 media/CircleEditor/external/tar.js delete mode 100644 media/CircleEditor/external/text.js delete mode 100644 media/CircleEditor/external/xml.js delete mode 100644 media/CircleEditor/external/zip.js diff --git a/media/CircleEditor/external/.FORMATDENY b/media/CircleEditor/external/.FORMATDENY deleted file mode 100644 index e69de29b..00000000 diff --git a/media/CircleEditor/external/LICENSE b/media/CircleEditor/external/LICENSE deleted file mode 100644 index d4b64f5a..00000000 --- a/media/CircleEditor/external/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) Lutz Roeder - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/media/CircleEditor/external/ORIGIN b/media/CircleEditor/external/ORIGIN deleted file mode 100644 index 1cd9403b..00000000 --- a/media/CircleEditor/external/ORIGIN +++ /dev/null @@ -1,3 +0,0 @@ -repo: https://github.com/lutzroeder/netron.git -commit: ae449ff55642636e6a1eef092eda34ffcba1c684 -tag: v5.7.4 diff --git a/media/CircleEditor/external/base.js b/media/CircleEditor/external/base.js deleted file mode 100644 index 1a50f521..00000000 --- a/media/CircleEditor/external/base.js +++ /dev/null @@ -1,694 +0,0 @@ - -var base = base || {}; - -base.Int64 = class Int64 { - - constructor(low, high) { - this.low = low | 0; - this.high = high | 0; - } - - static create(value) { - if (isNaN(value)) { - return base.Int64.zero; - } - if (value <= -9223372036854776000) { - return base.Int64.min; - } - if (value + 1 >= 9223372036854776000) { - return base.Int64.max; - } - if (value < 0) { - return base.Int64.create(-value).negate(); - } - return new base.Int64((value % 4294967296) | 0, (value / 4294967296)); - } - - get isZero() { - return this.low === 0 && this.high === 0; - } - - get isNegative() { - return this.high < 0; - } - - negate() { - if (this.equals(base.Int64.min)) { - return base.Int64.min; - } - return this.not().add(base.Int64.one); - } - - not() { - return new Int64(~this.low, ~this.high); - } - - equals(other) { - if (!(other instanceof base.Int64) && (this.high >>> 31) === 1 && (other.high >>> 31) === 1) { - return false; - } - return this.high === other.high && this.low === other.low; - } - - compare(other) { - if (this.equals(other)) { - return 0; - } - const thisNeg = this.isNegative; - const otherNeg = other.isNegative; - if (thisNeg && !otherNeg) { - return -1; - } - if (!thisNeg && otherNeg) { - return 1; - } - return this.subtract(other).isNegative ? -1 : 1; - } - - add(other) { - return base.Utility.add(this, other, false); - } - - subtract(other) { - return base.Utility.subtract(this, other, false); - } - - multiply(other) { - return base.Utility.multiply(this, other, false); - } - - divide(other) { - return base.Utility.divide(this, other, false); - } - - toInteger() { - return this.low; - } - - toNumber() { - if (this.high === 0) { - return this.low >>> 0; - } - if (this.high === -1) { - return this.low; - } - return (this.high * 4294967296) + (this.low >>> 0); - } - - toString(radix) { - const r = radix || 10; - if (r < 2 || r > 16) { - throw new RangeError('radix'); - } - if (this.isZero) { - return '0'; - } - if (this.high < 0) { - if (this.equals(base.Int64.min)) { - const r = new Int64(radix, 0); - const div = this.divide(r); - const remainder = div.multiply(r).subtract(this); - return div.toString(r) + (remainder.low >>> 0).toString(r); - } - return '-' + this.negate().toString(r); - } - if (this.high === 0) { - return this.low.toString(radix); - } - return base.Utility.text(this, false, r); - } -}; - -base.Int64.min = new base.Int64(0, -2147483648); -base.Int64.zero = new base.Int64(0, 0); -base.Int64.one = new base.Int64(1, 0); -base.Int64.power24 = new base.Int64(1 << 24, 0); -base.Int64.max = new base.Int64(0, 2147483647); - -base.Uint64 = class Uint64 { - - constructor(low, high) { - this.low = low | 0; - this.high = high | 0; - } - - static create(value) { - if (isNaN(value)) { - return base.Uint64.zero; - } - if (value < 0) { - return base.Uint64.zero; - } - if (value >= 18446744073709552000) { - return base.Uint64.max; - } - if (value < 0) { - return base.Uint64.create(-value).negate(); - } - return new base.Uint64((value % 4294967296) | 0, (value / 4294967296)); - } - - get isZero() { - return this.low === 0 && this.high === 0; - } - - get isNegative() { - return false; - } - - negate() { - return this.not().add(base.Int64.one); - } - - not() { - return new base.Uint64(~this.low, ~this.high); - } - - equals(other) { - if (!(other instanceof base.Uint64) && (this.high >>> 31) === 1 && (other.high >>> 31) === 1) { - return false; - } - return this.high === other.high && this.low === other.low; - } - - compare(other) { - if (this.equals(other)) { - return 0; - } - const thisNeg = this.isNegative; - const otherNeg = other.isNegative; - if (thisNeg && !otherNeg) { - return -1; - } - if (!thisNeg && otherNeg) { - return 1; - } - return (other.high >>> 0) > (this.high >>> 0) || (other.high === this.high && (other.low >>> 0) > (this.low >>> 0)) ? -1 : 1; - } - - add(other) { - return base.Utility.add(this, other, true); - } - - subtract(other) { - return base.Utility.subtract(this, other, true); - } - - multiply(other) { - return base.Utility.multiply(this, other, true); - } - - divide(other) { - return base.Utility.divide(this, other, true); - } - - toInteger() { - return this.low >>> 0; - } - - toNumber() { - if (this.high === 0) { - return this.low >>> 0; - } - return ((this.high >>> 0) * 4294967296) + (this.low >>> 0); - } - - toString(radix) { - const r = radix || 10; - if (r < 2 || 36 < r) { - throw new RangeError('radix'); - } - if (this.isZero) { - return '0'; - } - if (this.high === 0) { - return this.low.toString(radix); - } - return base.Utility.text(this, true, r); - } -}; - -base.Utility = class { - - static add(a, b, unsigned) { - const a48 = a.high >>> 16; - const a32 = a.high & 0xFFFF; - const a16 = a.low >>> 16; - const a00 = a.low & 0xFFFF; - const b48 = b.high >>> 16; - const b32 = b.high & 0xFFFF; - const b16 = b.low >>> 16; - const b00 = b.low & 0xFFFF; - let c48 = 0; - let c32 = 0; - let c16 = 0; - let c00 = 0; - c00 += a00 + b00; - c16 += c00 >>> 16; - c00 &= 0xFFFF; - c16 += a16 + b16; - c32 += c16 >>> 16; - c16 &= 0xFFFF; - c32 += a32 + b32; - c48 += c32 >>> 16; - c32 &= 0xFFFF; - c48 += a48 + b48; - c48 &= 0xFFFF; - return base.Utility._create((c16 << 16) | c00, (c48 << 16) | c32, unsigned); - } - - static subtract(a, b, unsigned) { - return base.Utility.add(a, b.negate(), unsigned); - } - - static multiply(a, b, unsigned) { - if (a.isZero) { - return base.Int64.zero; - } - if (b.isZero) { - return base.Int64.zero; - } - if (a.equals(base.Int64.min)) { - return b.isOdd() ? base.Int64.min : base.Int64.zero; - } - if (b.equals(base.Int64.min)) { - return b.isOdd() ? base.Int64.min : base.Int64.zero; - } - if (a.isNegative) { - if (b.isNegative) { - return this.negate().multiply(b.negate()); - } - else { - return this.negate().multiply(b).negate(); - } - } - else if (b.isNegative) { - return this.multiply(b.negate()).negate(); - } - if (a.compare(base.Int64.power24) < 0 && b.compare(base.Int64.power24) < 0) { - return unsigned ? base.Uint64.create(a.toNumber() * b.toNumber()) : base.Int64.create(a.toNumber() * b.toNumber()); - } - const a48 = a.high >>> 16; - const a32 = a.high & 0xFFFF; - const a16 = a.low >>> 16; - const a00 = a.low & 0xFFFF; - const b48 = b.high >>> 16; - const b32 = b.high & 0xFFFF; - const b16 = b.low >>> 16; - const b00 = b.low & 0xFFFF; - let c48 = 0; - let c32 = 0; - let c16 = 0; - let c00 = 0; - c00 += a00 * b00; - c16 += c00 >>> 16; - c00 &= 0xFFFF; - c16 += a16 * b00; - c32 += c16 >>> 16; - c16 &= 0xFFFF; - c16 += a00 * b16; - c32 += c16 >>> 16; - c16 &= 0xFFFF; - c32 += a32 * b00; - c48 += c32 >>> 16; - c32 &= 0xFFFF; - c32 += a16 * b16; - c48 += c32 >>> 16; - c32 &= 0xFFFF; - c32 += a00 * b32; - c48 += c32 >>> 16; - c32 &= 0xFFFF; - c48 += a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48; - c48 &= 0xFFFF; - return base.Utility._create((c16 << 16) | c00, (c48 << 16) | c32, unsigned); - } - - static divide(a, b, unsigned) { - if (b.isZero) { - throw new Error('Division by zero.'); - } - if (a.isZero) { - return unsigned ? base.Uint64.zero : base.Int64.zero; - } - let approx; - let remainder; - let result; - if (!unsigned) { - if (a.equals(base.Int64.min)) { - if (b.equals(base.Int64.one) || b.equals(base.Int64.negativeOne)) { - return base.Int64.min; - } - else if (b.equals(base.Int64.min)) { - return base.Int64.one; - } - else { - const half = base.Utility._shiftRight(a, unsigned, 1); - const halfDivide = half.divide(b); - approx = base.Utility._shiftLeft(halfDivide, halfDivide instanceof base.Uint64, 1); - if (approx.eq(base.Int64.zero)) { - return b.isNegative ? base.Int64.one : base.Int64.negativeOne; - } - else { - remainder = a.subtract(b.multiply(approx)); - result = approx.add(remainder.divide(b)); - return result; - } - } - } - else if (b.equals(base.Int64.min)) { - return unsigned ? base.Uint64.zero : base.Int64.zero; - } - if (a.isNegative) { - if (b.isNegative) { - return this.negate().divide(b.negate()); - } - return a.negate().divide(b).negate(); - } - else if (b.isNegative) { - return a.divide(b.negate()).negate(); - } - result = base.Int64.zero; - } - else { - if (!(b instanceof base.Uint64)) { - b = new base.Uint64(b.low, b.high); - } - if (b.compare(a) > 0) { - return base.Int64.zero; - } - if (b.compare(base.Utility._shiftRight(a, unsigned, 1)) > 0) { - return base.Uint64.one; - } - result = base.Uint64.zero; - } - remainder = a; - while (remainder.compare(b) >= 0) { - let approx = Math.max(1, Math.floor(remainder.toNumber() / b.toNumber())); - const log2 = Math.ceil(Math.log(approx) / Math.LN2); - const delta = (log2 <= 48) ? 1 : Math.pow(2, log2 - 48); - let approxResult = base.Int64.create(approx); - let approxRemainder = approxResult.multiply(b); - while (approxRemainder.isNegative || approxRemainder.compare(remainder) > 0) { - approx -= delta; - approxResult = unsigned ? base.Uint64.create(approx) : base.Int64.create(approx); - approxRemainder = approxResult.multiply(b); - } - if (approxResult.isZero) { - approxResult = base.Int64.one; - } - result = result.add(approxResult); - remainder = remainder.subtract(approxRemainder); - } - return result; - } - - static text(value, unsigned, radix) { - const power = unsigned ? base.Uint64.create(Math.pow(radix, 6)) : base.Int64.create(Math.pow(radix, 6)); - let remainder = value; - let result = ''; - for (;;) { - const remainderDiv = remainder.divide(power); - const intval = remainder.subtract(remainderDiv.multiply(power)).toInteger() >>> 0; - let digits = intval.toString(radix); - remainder = remainderDiv; - if (remainder.low === 0 && remainder.high === 0) { - return digits + result; - } - while (digits.length < 6) { - digits = '0' + digits; - } - result = '' + digits + result; - } - } - - static _shiftLeft(value, unsigned, shift) { - return base.Utility._create(value.low << shift, (value.high << shift) | (value.low >>> (32 - shift)), unsigned); - } - - static _shiftRight(value, unsigned, shift) { - return base.Utility._create((value.low >>> shift) | (value.high << (32 - shift)), value.high >> shift, unsigned); - } - - static _create(low, high, unsigned) { - return unsigned ? new base.Uint64(low, high) : new base.Int64(low, high); - } -}; - -base.Uint64.zero = new base.Uint64(0, 0); -base.Uint64.one = new base.Uint64(1, 0); -base.Uint64.max = new base.Uint64(-1, -1); - -if (!DataView.prototype.getFloat16) { - DataView.prototype.getFloat16 = function(byteOffset, littleEndian) { - const value = this.getUint16(byteOffset, littleEndian); - const e = (value & 0x7C00) >> 10; - let f = value & 0x03FF; - if (e == 0) { - f = 0.00006103515625 * (f / 1024); - } - else if (e == 0x1F) { - f = f ? NaN : Infinity; - } - else { - f = DataView.__float16_pow[e] * (1 + (f / 1024)); - } - return value & 0x8000 ? -f : f; - }; - DataView.__float16_pow = { - 1: 1/16384, 2: 1/8192, 3: 1/4096, 4: 1/2048, 5: 1/1024, 6: 1/512, 7: 1/256, 8: 1/128, - 9: 1/64, 10: 1/32, 11: 1/16, 12: 1/8, 13: 1/4, 14: 1/2, 15: 1, 16: 2, - 17: 4, 18: 8, 19: 16, 20: 32, 21: 64, 22: 128, 23: 256, 24: 512, - 25: 1024, 26: 2048, 27: 4096, 28: 8192, 29: 16384, 30: 32768, 31: 65536 - }; -} - -if (!DataView.prototype.setFloat16) { - DataView.prototype.setFloat16 = function(byteOffset, value, littleEndian) { - DataView.__float16_float[0] = value; - value = DataView.__float16_int[0]; - const s = (value >>> 16) & 0x8000; - const e = (value >>> 23) & 0xff; - const f = value & 0x7fffff; - const v = s | DataView.__float16_base[e] | (f >> DataView.__float16_shift[e]); - this.setUint16(byteOffset, v, littleEndian); - }; - DataView.__float16_float = new Float32Array(1); - DataView.__float16_int = new Uint32Array(DataView.__float16_float.buffer, 0, DataView.__float16_float.length); - DataView.__float16_base = new Uint32Array(256); - DataView.__float16_shift = new Uint32Array(256); - for (let i = 0; i < 256; ++i) { - const e = i - 127; - if (e < -27) { - DataView.__float16_base[i] = 0x0000; - DataView.__float16_shift[i] = 24; - } - else if (e < -14) { - DataView.__float16_base[i] = 0x0400 >> -e - 14; - DataView.__float16_shift[i] = -e - 1; - } - else if (e <= 15) { - DataView.__float16_base[i] = e + 15 << 10; - DataView.__float16_shift[i] = 13; - } - else if (e < 128) { - DataView.__float16_base[i] = 0x7c00; - DataView.__float16_shift[i] = 24; - } - else { - DataView.__float16_base[i] = 0x7c00; - DataView.__float16_shift[i] = 13; - } - } -} - -if (!DataView.prototype.getBfloat16) { - DataView.prototype.getBfloat16 = function(byteOffset, littleEndian) { - if (littleEndian) { - DataView.__bfloat16_uint16[0] = 0; - DataView.__bfloat16_uint16[1] = this.getUint16(byteOffset, littleEndian); - } - else { - DataView.__bfloat16_uint16[0] = this.getUint16(byteOffset, littleEndian); - DataView.__bfloat16_uint16[1] = 0; - } - return DataView.__bfloat16_float32[0]; - }; - DataView.__bfloat16_float32 = new Float32Array(1); - DataView.__bfloat16_uint16 = new Uint16Array(DataView.__bfloat16_float32.buffer, DataView.__bfloat16_float32.byteOffset, 2); -} - -DataView.prototype.getInt64 = DataView.prototype.getInt64 || function(byteOffset, littleEndian) { - return littleEndian ? - new base.Int64(this.getUint32(byteOffset, true), this.getUint32(byteOffset + 4, true)) : - new base.Int64(this.getUint32(byteOffset + 4, true), this.getUint32(byteOffset, true)); -}; - -DataView.prototype.setInt64 = DataView.prototype.setInt64 || function(byteOffset, value, littleEndian) { - if (littleEndian) { - this.setUint32(byteOffset, value.low, true); - this.setUint32(byteOffset + 4, value.high, true); - } - else { - this.setUint32(byteOffset + 4, value.low, false); - this.setUint32(byteOffset, value.high, false); - } -}; - -DataView.prototype.getUint64 = DataView.prototype.getUint64 || function(byteOffset, littleEndian) { - return littleEndian ? - new base.Uint64(this.getUint32(byteOffset, true), this.getUint32(byteOffset + 4, true)) : - new base.Uint64(this.getUint32(byteOffset + 4, true), this.getUint32(byteOffset, true)); -}; - -DataView.prototype.setUint64 = DataView.prototype.setUint64 || function(byteOffset, value, littleEndian) { - if (littleEndian) { - this.setUInt32(byteOffset, value.low, true); - this.setUInt32(byteOffset + 4, value.high, true); - } - else { - this.setUInt32(byteOffset + 4, value.low, false); - this.setUInt32(byteOffset, value.high, false); - } -}; - -DataView.prototype.getBits = DataView.prototype.getBits || function(offset, bits /*, signed */) { - offset = offset * bits; - const available = (this.byteLength << 3) - offset; - if (bits > available) { - throw new RangeError(); - } - let value = 0; - let index = 0; - while (index < bits) { - const remainder = offset & 7; - const size = Math.min(bits - index, 8 - remainder); - value <<= size; - value |= (this.getUint8(offset >> 3) >> (8 - size - remainder)) & ~(0xff << size); - offset += size; - index += size; - } - return value; -}; - -base.BinaryReader = class { - - constructor(data) { - this._buffer = data instanceof Uint8Array ? data : data.peek(); - 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() { - return this._length; - } - - get position() { - return this._position; - } - - seek(position) { - this._position = position >= 0 ? position : this._length + position; - if (this._position > this._length || this._position < 0) { - throw new Error('Expected ' + (this._position - this._length) + ' more bytes. The file might be corrupted. Unexpected end of file.'); - } - } - - skip(offset) { - this._position += offset; - if (this._position > this._length) { - throw new Error('Expected ' + (this._position - this._length) + ' more bytes. The file might be corrupted. Unexpected end of file.'); - } - } - - read(length) { - if (this._position === 0 && length === undefined) { - this._position = this._length; - return this._buffer; - } - const position = this._position; - this.skip(length !== undefined ? length : this._length - this._position); - return this._buffer.slice(position, this._position); - } - - byte() { - const position = this._position; - this.skip(1); - return this._buffer[position]; - } - - int8() { - const position = this._position; - this.skip(1); - return this._view.getInt8(position, true); - } - - int16() { - const position = this._position; - this.skip(2); - return this._view.getInt16(position, true); - } - - int32() { - const position = this._position; - this.skip(4); - return this._view.getInt32(position, true); - } - - int64() { - const position = this._position; - this.skip(8); - return this._view.getInt64(position, true).toNumber(); - } - - uint16() { - const position = this._position; - this.skip(2); - return this._view.getUint16(position, true); - } - - uint32() { - const position = this._position; - this.skip(4); - return this._view.getUint32(position, true); - } - - uint64() { - const position = this._position; - this.skip(8); - return this._view.getUint64(position, true).toNumber(); - } - - float32() { - const position = this._position; - this.skip(4); - return this._view.getFloat32(position, true); - } - - float64() { - const position = this._position; - this.skip(8); - return this._view.getFloat64(position, true); - } - - string() { - const length = this.uint32(); - const position = this._position; - this.skip(length); - const data = this._buffer.subarray(position, this._position); - return this._utf8.decode(data); - } -}; - -if (typeof window !== 'undefined' && typeof window.Long != 'undefined') { - window.long = { Long: window.Long }; - window.Int64 = base.Int64; - window.Uint64 = base.Uint64; -} - -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.Int64 = base.Int64; - module.exports.Uint64 = base.Uint64; - module.exports.BinaryReader = base.BinaryReader; -} diff --git a/media/CircleEditor/external/circle-schema.js b/media/CircleEditor/external/circle-schema.js deleted file mode 100644 index 9e890dc0..00000000 --- a/media/CircleEditor/external/circle-schema.js +++ /dev/null @@ -1,2367 +0,0 @@ -var $root = flatbuffers.get('circle'); - -$root.circle = $root.circle || {}; - -$root.circle.TensorType = { - FLOAT32: 0, - FLOAT16: 1, - INT32: 2, - UINT8: 3, - INT64: 4, - STRING: 5, - BOOL: 6, - INT16: 7, - COMPLEX64: 8, - INT8: 9, - FLOAT64: 10 -}; - -$root.circle.CustomQuantization = class CustomQuantization { - - static decode(reader, position) { - const $ = new $root.circle.CustomQuantization(); - $.custom = reader.typedArray(position, 4, Uint8Array); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.CustomQuantization(); - $.custom = reader.typedArray(json.custom, Uint8Array); - return $; - } -}; - -$root.circle.QuantizationDetails = class { - - static decode(reader, position, type) { - switch (type) { - case 1: return $root.circle.CustomQuantization.decode(reader, position); - default: return undefined; - } - } - - static decodeText(reader, json, type) { - switch (type) { - case 'CustomQuantization': return $root.circle.CustomQuantization.decodeText(reader, json); - default: return undefined; - } - } -}; - -$root.circle.QuantizationParameters = class QuantizationParameters { - - static decode(reader, position) { - const $ = new $root.circle.QuantizationParameters(); - $.min = reader.typedArray(position, 4, Float32Array); - $.max = reader.typedArray(position, 6, Float32Array); - $.scale = reader.typedArray(position, 8, Float32Array); - $.zero_point = reader.int64s_(position, 10); - $.details = reader.union(position, 12, $root.circle.QuantizationDetails.decode); - $.quantized_dimension = reader.int32_(position, 16, 0); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.QuantizationParameters(); - $.min = reader.typedArray(json.min, Float32Array); - $.max = reader.typedArray(json.max, Float32Array); - $.scale = reader.typedArray(json.scale, Float32Array); - $.zero_point = reader.array(json.zero_point); - $.details = $root.circle.QuantizationDetails.decodeText(reader, json.details, json.details_type); - $.quantized_dimension = reader.value(json.quantized_dimension, 0); - return $; - } -}; - -$root.circle.DimensionType = { - DENSE: 0, - SPARSE_CSR: 1 -}; - -$root.circle.Int32Vector = class Int32Vector { - - static decode(reader, position) { - const $ = new $root.circle.Int32Vector(); - $.values = reader.typedArray(position, 4, Int32Array); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.Int32Vector(); - $.values = reader.typedArray(json.values, Int32Array); - return $; - } -}; - -$root.circle.Uint16Vector = class Uint16Vector { - - static decode(reader, position) { - const $ = new $root.circle.Uint16Vector(); - $.values = reader.typedArray(position, 4, Uint16Array); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.Uint16Vector(); - $.values = reader.typedArray(json.values, Uint16Array); - return $; - } -}; - -$root.circle.Uint8Vector = class Uint8Vector { - - static decode(reader, position) { - const $ = new $root.circle.Uint8Vector(); - $.values = reader.typedArray(position, 4, Uint8Array); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.Uint8Vector(); - $.values = reader.typedArray(json.values, Uint8Array); - return $; - } -}; - -$root.circle.SparseIndexVector = class { - - static decode(reader, position, type) { - switch (type) { - case 1: return $root.circle.Int32Vector.decode(reader, position); - case 2: return $root.circle.Uint16Vector.decode(reader, position); - case 3: return $root.circle.Uint8Vector.decode(reader, position); - default: return undefined; - } - } - - static decodeText(reader, json, type) { - switch (type) { - case 'Int32Vector': return $root.circle.Int32Vector.decodeText(reader, json); - case 'Uint16Vector': return $root.circle.Uint16Vector.decodeText(reader, json); - case 'Uint8Vector': return $root.circle.Uint8Vector.decodeText(reader, json); - default: return undefined; - } - } -}; - -$root.circle.DimensionMetadata = class DimensionMetadata { - - static decode(reader, position) { - const $ = new $root.circle.DimensionMetadata(); - $.format = reader.int8_(position, 4, 0); - $.dense_size = reader.int32_(position, 6, 0); - $.array_segments = reader.union(position, 8, $root.circle.SparseIndexVector.decode); - $.array_indices = reader.union(position, 12, $root.circle.SparseIndexVector.decode); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.DimensionMetadata(); - $.format = $root.circle.DimensionType[json.format]; - $.dense_size = reader.value(json.dense_size, 0); - $.array_segments = $root.circle.SparseIndexVector.decodeText(reader, json.array_segments, json.array_segments_type); - $.array_indices = $root.circle.SparseIndexVector.decodeText(reader, json.array_indices, json.array_indices_type); - return $; - } -}; - -$root.circle.SparsityParameters = class SparsityParameters { - - static decode(reader, position) { - const $ = new $root.circle.SparsityParameters(); - $.traversal_order = reader.typedArray(position, 4, Int32Array); - $.block_map = reader.typedArray(position, 6, Int32Array); - $.dim_metadata = reader.tableArray(position, 8, $root.circle.DimensionMetadata.decode); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.SparsityParameters(); - $.traversal_order = reader.typedArray(json.traversal_order, Int32Array); - $.block_map = reader.typedArray(json.block_map, Int32Array); - $.dim_metadata = reader.objectArray(json.dim_metadata, $root.circle.DimensionMetadata.decodeText); - return $; - } -}; - -$root.circle.Tensor = class Tensor { - - static decode(reader, position) { - const $ = new $root.circle.Tensor(); - $.shape = reader.typedArray(position, 4, Int32Array); - $.type = reader.int8_(position, 6, 0); - $.buffer = reader.uint32_(position, 8, 0); - $.name = reader.string_(position, 10, null); - $.quantization = reader.table(position, 12, $root.circle.QuantizationParameters.decode); - $.is_variable = reader.bool_(position, 14, false); - $.sparsity = reader.table(position, 16, $root.circle.SparsityParameters.decode); - $.shape_signature = reader.typedArray(position, 18, Int32Array); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.Tensor(); - $.shape = reader.typedArray(json.shape, Int32Array); - $.type = $root.circle.TensorType[json.type]; - $.buffer = reader.value(json.buffer, 0); - $.name = reader.value(json.name, null); - $.quantization = reader.object(json.quantization, $root.circle.QuantizationParameters.decodeText); - $.is_variable = reader.value(json.is_variable, false); - $.sparsity = reader.object(json.sparsity, $root.circle.SparsityParameters.decodeText); - $.shape_signature = reader.typedArray(json.shape_signature, Int32Array); - return $; - } -}; - -$root.circle.BuiltinOperator = { - ADD: 0, - AVERAGE_POOL_2D: 1, - CONCATENATION: 2, - CONV_2D: 3, - DEPTHWISE_CONV_2D: 4, - DEPTH_TO_SPACE: 5, - DEQUANTIZE: 6, - EMBEDDING_LOOKUP: 7, - FLOOR: 8, - FULLY_CONNECTED: 9, - HASHTABLE_LOOKUP: 10, - L2_NORMALIZATION: 11, - L2_POOL_2D: 12, - LOCAL_RESPONSE_NORMALIZATION: 13, - LOGISTIC: 14, - LSH_PROJECTION: 15, - LSTM: 16, - MAX_POOL_2D: 17, - MUL: 18, - RELU: 19, - RELU_N1_TO_1: 20, - RELU6: 21, - RESHAPE: 22, - RESIZE_BILINEAR: 23, - RNN: 24, - SOFTMAX: 25, - SPACE_TO_DEPTH: 26, - SVDF: 27, - TANH: 28, - CONCAT_EMBEDDINGS: 29, - SKIP_GRAM: 30, - CALL: 31, - CUSTOM: 32, - EMBEDDING_LOOKUP_SPARSE: 33, - PAD: 34, - UNIDIRECTIONAL_SEQUENCE_RNN: 35, - GATHER: 36, - BATCH_TO_SPACE_ND: 37, - SPACE_TO_BATCH_ND: 38, - TRANSPOSE: 39, - MEAN: 40, - SUB: 41, - DIV: 42, - SQUEEZE: 43, - UNIDIRECTIONAL_SEQUENCE_LSTM: 44, - STRIDED_SLICE: 45, - BIDIRECTIONAL_SEQUENCE_RNN: 46, - EXP: 47, - TOPK_V2: 48, - SPLIT: 49, - LOG_SOFTMAX: 50, - DELEGATE: 51, - BIDIRECTIONAL_SEQUENCE_LSTM: 52, - CAST: 53, - PRELU: 54, - MAXIMUM: 55, - ARG_MAX: 56, - MINIMUM: 57, - LESS: 58, - NEG: 59, - PADV2: 60, - GREATER: 61, - GREATER_EQUAL: 62, - LESS_EQUAL: 63, - SELECT: 64, - SLICE: 65, - SIN: 66, - TRANSPOSE_CONV: 67, - SPARSE_TO_DENSE: 68, - TILE: 69, - EXPAND_DIMS: 70, - EQUAL: 71, - NOT_EQUAL: 72, - LOG: 73, - SUM: 74, - SQRT: 75, - RSQRT: 76, - SHAPE: 77, - POW: 78, - ARG_MIN: 79, - FAKE_QUANT: 80, - REDUCE_PROD: 81, - REDUCE_MAX: 82, - PACK: 83, - LOGICAL_OR: 84, - ONE_HOT: 85, - LOGICAL_AND: 86, - LOGICAL_NOT: 87, - UNPACK: 88, - REDUCE_MIN: 89, - FLOOR_DIV: 90, - REDUCE_ANY: 91, - SQUARE: 92, - ZEROS_LIKE: 93, - FILL: 94, - FLOOR_MOD: 95, - RANGE: 96, - RESIZE_NEAREST_NEIGHBOR: 97, - LEAKY_RELU: 98, - SQUARED_DIFFERENCE: 99, - MIRROR_PAD: 100, - ABS: 101, - SPLIT_V: 102, - UNIQUE: 103, - CEIL: 104, - REVERSE_V2: 105, - ADD_N: 106, - GATHER_ND: 107, - COS: 108, - WHERE: 109, - RANK: 110, - ELU: 111, - REVERSE_SEQUENCE: 112, - MATRIX_DIAG: 113, - QUANTIZE: 114, - MATRIX_SET_DIAG: 115, - ROUND: 116, - HARD_SWISH: 117, - IF: 118, - WHILE: 119, - NON_MAX_SUPPRESSION_V4: 120, - NON_MAX_SUPPRESSION_V5: 121, - SCATTER_ND: 122, - SELECT_V2: 123, - DENSIFY: 124, - SEGMENT_SUM: 125, - BATCH_MATMUL: 126, - BCQ_GATHER: 252, - BCQ_FULLY_CONNECTED: 253, - INSTANCE_NORM: 254 -}; - -$root.circle.BuiltinOptions = class { - - static decode(reader, position, type) { - switch (type) { - case 1: return $root.circle.Conv2DOptions.decode(reader, position); - case 2: return $root.circle.DepthwiseConv2DOptions.decode(reader, position); - case 3: return $root.circle.ConcatEmbeddingsOptions.decode(reader, position); - case 4: return $root.circle.LSHProjectionOptions.decode(reader, position); - case 5: return $root.circle.Pool2DOptions.decode(reader, position); - case 6: return $root.circle.SVDFOptions.decode(reader, position); - case 7: return $root.circle.RNNOptions.decode(reader, position); - case 8: return $root.circle.FullyConnectedOptions.decode(reader, position); - case 9: return $root.circle.SoftmaxOptions.decode(reader, position); - case 10: return $root.circle.ConcatenationOptions.decode(reader, position); - case 11: return $root.circle.AddOptions.decode(reader, position); - case 12: return $root.circle.L2NormOptions.decode(reader, position); - case 13: return $root.circle.LocalResponseNormalizationOptions.decode(reader, position); - case 14: return $root.circle.LSTMOptions.decode(reader, position); - case 15: return $root.circle.ResizeBilinearOptions.decode(reader, position); - case 16: return $root.circle.CallOptions.decode(reader, position); - case 17: return $root.circle.ReshapeOptions.decode(reader, position); - case 18: return $root.circle.SkipGramOptions.decode(reader, position); - case 19: return $root.circle.SpaceToDepthOptions.decode(reader, position); - case 20: return $root.circle.EmbeddingLookupSparseOptions.decode(reader, position); - case 21: return $root.circle.MulOptions.decode(reader, position); - case 22: return $root.circle.PadOptions.decode(reader, position); - case 23: return $root.circle.GatherOptions.decode(reader, position); - case 24: return $root.circle.BatchToSpaceNDOptions.decode(reader, position); - case 25: return $root.circle.SpaceToBatchNDOptions.decode(reader, position); - case 26: return $root.circle.TransposeOptions.decode(reader, position); - case 27: return $root.circle.ReducerOptions.decode(reader, position); - case 28: return $root.circle.SubOptions.decode(reader, position); - case 29: return $root.circle.DivOptions.decode(reader, position); - case 30: return $root.circle.SqueezeOptions.decode(reader, position); - case 31: return $root.circle.SequenceRNNOptions.decode(reader, position); - case 32: return $root.circle.StridedSliceOptions.decode(reader, position); - case 33: return $root.circle.ExpOptions.decode(reader, position); - case 34: return $root.circle.TopKV2Options.decode(reader, position); - case 35: return $root.circle.SplitOptions.decode(reader, position); - case 36: return $root.circle.LogSoftmaxOptions.decode(reader, position); - case 37: return $root.circle.CastOptions.decode(reader, position); - case 38: return $root.circle.DequantizeOptions.decode(reader, position); - case 39: return $root.circle.MaximumMinimumOptions.decode(reader, position); - case 40: return $root.circle.ArgMaxOptions.decode(reader, position); - case 41: return $root.circle.LessOptions.decode(reader, position); - case 42: return $root.circle.NegOptions.decode(reader, position); - case 43: return $root.circle.PadV2Options.decode(reader, position); - case 44: return $root.circle.GreaterOptions.decode(reader, position); - case 45: return $root.circle.GreaterEqualOptions.decode(reader, position); - case 46: return $root.circle.LessEqualOptions.decode(reader, position); - case 47: return $root.circle.SelectOptions.decode(reader, position); - case 48: return $root.circle.SliceOptions.decode(reader, position); - case 49: return $root.circle.TransposeConvOptions.decode(reader, position); - case 50: return $root.circle.SparseToDenseOptions.decode(reader, position); - case 51: return $root.circle.TileOptions.decode(reader, position); - case 52: return $root.circle.ExpandDimsOptions.decode(reader, position); - case 53: return $root.circle.EqualOptions.decode(reader, position); - case 54: return $root.circle.NotEqualOptions.decode(reader, position); - case 55: return $root.circle.ShapeOptions.decode(reader, position); - case 56: return $root.circle.PowOptions.decode(reader, position); - case 57: return $root.circle.ArgMinOptions.decode(reader, position); - case 58: return $root.circle.FakeQuantOptions.decode(reader, position); - case 59: return $root.circle.PackOptions.decode(reader, position); - case 60: return $root.circle.LogicalOrOptions.decode(reader, position); - case 61: return $root.circle.OneHotOptions.decode(reader, position); - case 62: return $root.circle.LogicalAndOptions.decode(reader, position); - case 63: return $root.circle.LogicalNotOptions.decode(reader, position); - case 64: return $root.circle.UnpackOptions.decode(reader, position); - case 65: return $root.circle.FloorDivOptions.decode(reader, position); - case 66: return $root.circle.SquareOptions.decode(reader, position); - case 67: return $root.circle.ZerosLikeOptions.decode(reader, position); - case 68: return $root.circle.FillOptions.decode(reader, position); - case 69: return $root.circle.BidirectionalSequenceLSTMOptions.decode(reader, position); - case 70: return $root.circle.BidirectionalSequenceRNNOptions.decode(reader, position); - case 71: return $root.circle.UnidirectionalSequenceLSTMOptions.decode(reader, position); - case 72: return $root.circle.FloorModOptions.decode(reader, position); - case 73: return $root.circle.RangeOptions.decode(reader, position); - case 74: return $root.circle.ResizeNearestNeighborOptions.decode(reader, position); - case 75: return $root.circle.LeakyReluOptions.decode(reader, position); - case 76: return $root.circle.SquaredDifferenceOptions.decode(reader, position); - case 77: return $root.circle.MirrorPadOptions.decode(reader, position); - case 78: return $root.circle.AbsOptions.decode(reader, position); - case 79: return $root.circle.SplitVOptions.decode(reader, position); - case 80: return $root.circle.UniqueOptions.decode(reader, position); - case 81: return $root.circle.ReverseV2Options.decode(reader, position); - case 82: return $root.circle.AddNOptions.decode(reader, position); - case 83: return $root.circle.GatherNdOptions.decode(reader, position); - case 84: return $root.circle.CosOptions.decode(reader, position); - case 85: return $root.circle.WhereOptions.decode(reader, position); - case 86: return $root.circle.RankOptions.decode(reader, position); - case 87: return $root.circle.ReverseSequenceOptions.decode(reader, position); - case 88: return $root.circle.MatrixDiagOptions.decode(reader, position); - case 89: return $root.circle.QuantizeOptions.decode(reader, position); - case 90: return $root.circle.MatrixSetDiagOptions.decode(reader, position); - case 91: return $root.circle.HardSwishOptions.decode(reader, position); - case 92: return $root.circle.IfOptions.decode(reader, position); - case 93: return $root.circle.WhileOptions.decode(reader, position); - case 94: return $root.circle.DepthToSpaceOptions.decode(reader, position); - case 95: return $root.circle.NonMaxSuppressionV4Options.decode(reader, position); - case 96: return $root.circle.NonMaxSuppressionV5Options.decode(reader, position); - case 97: return $root.circle.ScatterNdOptions.decode(reader, position); - case 98: return $root.circle.SelectV2Options.decode(reader, position); - case 99: return $root.circle.DensifyOptions.decode(reader, position); - case 100: return $root.circle.SegmentSumOptions.decode(reader, position); - case 101: return $root.circle.BatchMatMulOptions.decode(reader, position); - case 252: return $root.circle.BCQGatherOptions.decode(reader, position); - case 253: return $root.circle.BCQFullyConnectedOptions.decode(reader, position); - case 254: return $root.circle.InstanceNormOptions.decode(reader, position); - default: return undefined; - } - } - - static decodeText(reader, json, type) { - switch (type) { - case 'Conv2DOptions': return $root.circle.Conv2DOptions.decodeText(reader, json); - case 'DepthwiseConv2DOptions': return $root.circle.DepthwiseConv2DOptions.decodeText(reader, json); - case 'ConcatEmbeddingsOptions': return $root.circle.ConcatEmbeddingsOptions.decodeText(reader, json); - case 'LSHProjectionOptions': return $root.circle.LSHProjectionOptions.decodeText(reader, json); - case 'Pool2DOptions': return $root.circle.Pool2DOptions.decodeText(reader, json); - case 'SVDFOptions': return $root.circle.SVDFOptions.decodeText(reader, json); - case 'RNNOptions': return $root.circle.RNNOptions.decodeText(reader, json); - case 'FullyConnectedOptions': return $root.circle.FullyConnectedOptions.decodeText(reader, json); - case 'SoftmaxOptions': return $root.circle.SoftmaxOptions.decodeText(reader, json); - case 'ConcatenationOptions': return $root.circle.ConcatenationOptions.decodeText(reader, json); - case 'AddOptions': return $root.circle.AddOptions.decodeText(reader, json); - case 'L2NormOptions': return $root.circle.L2NormOptions.decodeText(reader, json); - case 'LocalResponseNormalizationOptions': return $root.circle.LocalResponseNormalizationOptions.decodeText(reader, json); - case 'LSTMOptions': return $root.circle.LSTMOptions.decodeText(reader, json); - case 'ResizeBilinearOptions': return $root.circle.ResizeBilinearOptions.decodeText(reader, json); - case 'CallOptions': return $root.circle.CallOptions.decodeText(reader, json); - case 'ReshapeOptions': return $root.circle.ReshapeOptions.decodeText(reader, json); - case 'SkipGramOptions': return $root.circle.SkipGramOptions.decodeText(reader, json); - case 'SpaceToDepthOptions': return $root.circle.SpaceToDepthOptions.decodeText(reader, json); - case 'EmbeddingLookupSparseOptions': return $root.circle.EmbeddingLookupSparseOptions.decodeText(reader, json); - case 'MulOptions': return $root.circle.MulOptions.decodeText(reader, json); - case 'PadOptions': return $root.circle.PadOptions.decodeText(reader, json); - case 'GatherOptions': return $root.circle.GatherOptions.decodeText(reader, json); - case 'BatchToSpaceNDOptions': return $root.circle.BatchToSpaceNDOptions.decodeText(reader, json); - case 'SpaceToBatchNDOptions': return $root.circle.SpaceToBatchNDOptions.decodeText(reader, json); - case 'TransposeOptions': return $root.circle.TransposeOptions.decodeText(reader, json); - case 'ReducerOptions': return $root.circle.ReducerOptions.decodeText(reader, json); - case 'SubOptions': return $root.circle.SubOptions.decodeText(reader, json); - case 'DivOptions': return $root.circle.DivOptions.decodeText(reader, json); - case 'SqueezeOptions': return $root.circle.SqueezeOptions.decodeText(reader, json); - case 'SequenceRNNOptions': return $root.circle.SequenceRNNOptions.decodeText(reader, json); - case 'StridedSliceOptions': return $root.circle.StridedSliceOptions.decodeText(reader, json); - case 'ExpOptions': return $root.circle.ExpOptions.decodeText(reader, json); - case 'TopKV2Options': return $root.circle.TopKV2Options.decodeText(reader, json); - case 'SplitOptions': return $root.circle.SplitOptions.decodeText(reader, json); - case 'LogSoftmaxOptions': return $root.circle.LogSoftmaxOptions.decodeText(reader, json); - case 'CastOptions': return $root.circle.CastOptions.decodeText(reader, json); - case 'DequantizeOptions': return $root.circle.DequantizeOptions.decodeText(reader, json); - case 'MaximumMinimumOptions': return $root.circle.MaximumMinimumOptions.decodeText(reader, json); - case 'ArgMaxOptions': return $root.circle.ArgMaxOptions.decodeText(reader, json); - case 'LessOptions': return $root.circle.LessOptions.decodeText(reader, json); - case 'NegOptions': return $root.circle.NegOptions.decodeText(reader, json); - case 'PadV2Options': return $root.circle.PadV2Options.decodeText(reader, json); - case 'GreaterOptions': return $root.circle.GreaterOptions.decodeText(reader, json); - case 'GreaterEqualOptions': return $root.circle.GreaterEqualOptions.decodeText(reader, json); - case 'LessEqualOptions': return $root.circle.LessEqualOptions.decodeText(reader, json); - case 'SelectOptions': return $root.circle.SelectOptions.decodeText(reader, json); - case 'SliceOptions': return $root.circle.SliceOptions.decodeText(reader, json); - case 'TransposeConvOptions': return $root.circle.TransposeConvOptions.decodeText(reader, json); - case 'SparseToDenseOptions': return $root.circle.SparseToDenseOptions.decodeText(reader, json); - case 'TileOptions': return $root.circle.TileOptions.decodeText(reader, json); - case 'ExpandDimsOptions': return $root.circle.ExpandDimsOptions.decodeText(reader, json); - case 'EqualOptions': return $root.circle.EqualOptions.decodeText(reader, json); - case 'NotEqualOptions': return $root.circle.NotEqualOptions.decodeText(reader, json); - case 'ShapeOptions': return $root.circle.ShapeOptions.decodeText(reader, json); - case 'PowOptions': return $root.circle.PowOptions.decodeText(reader, json); - case 'ArgMinOptions': return $root.circle.ArgMinOptions.decodeText(reader, json); - case 'FakeQuantOptions': return $root.circle.FakeQuantOptions.decodeText(reader, json); - case 'PackOptions': return $root.circle.PackOptions.decodeText(reader, json); - case 'LogicalOrOptions': return $root.circle.LogicalOrOptions.decodeText(reader, json); - case 'OneHotOptions': return $root.circle.OneHotOptions.decodeText(reader, json); - case 'LogicalAndOptions': return $root.circle.LogicalAndOptions.decodeText(reader, json); - case 'LogicalNotOptions': return $root.circle.LogicalNotOptions.decodeText(reader, json); - case 'UnpackOptions': return $root.circle.UnpackOptions.decodeText(reader, json); - case 'FloorDivOptions': return $root.circle.FloorDivOptions.decodeText(reader, json); - case 'SquareOptions': return $root.circle.SquareOptions.decodeText(reader, json); - case 'ZerosLikeOptions': return $root.circle.ZerosLikeOptions.decodeText(reader, json); - case 'FillOptions': return $root.circle.FillOptions.decodeText(reader, json); - case 'BidirectionalSequenceLSTMOptions': return $root.circle.BidirectionalSequenceLSTMOptions.decodeText(reader, json); - case 'BidirectionalSequenceRNNOptions': return $root.circle.BidirectionalSequenceRNNOptions.decodeText(reader, json); - case 'UnidirectionalSequenceLSTMOptions': return $root.circle.UnidirectionalSequenceLSTMOptions.decodeText(reader, json); - case 'FloorModOptions': return $root.circle.FloorModOptions.decodeText(reader, json); - case 'RangeOptions': return $root.circle.RangeOptions.decodeText(reader, json); - case 'ResizeNearestNeighborOptions': return $root.circle.ResizeNearestNeighborOptions.decodeText(reader, json); - case 'LeakyReluOptions': return $root.circle.LeakyReluOptions.decodeText(reader, json); - case 'SquaredDifferenceOptions': return $root.circle.SquaredDifferenceOptions.decodeText(reader, json); - case 'MirrorPadOptions': return $root.circle.MirrorPadOptions.decodeText(reader, json); - case 'AbsOptions': return $root.circle.AbsOptions.decodeText(reader, json); - case 'SplitVOptions': return $root.circle.SplitVOptions.decodeText(reader, json); - case 'UniqueOptions': return $root.circle.UniqueOptions.decodeText(reader, json); - case 'ReverseV2Options': return $root.circle.ReverseV2Options.decodeText(reader, json); - case 'AddNOptions': return $root.circle.AddNOptions.decodeText(reader, json); - case 'GatherNdOptions': return $root.circle.GatherNdOptions.decodeText(reader, json); - case 'CosOptions': return $root.circle.CosOptions.decodeText(reader, json); - case 'WhereOptions': return $root.circle.WhereOptions.decodeText(reader, json); - case 'RankOptions': return $root.circle.RankOptions.decodeText(reader, json); - case 'ReverseSequenceOptions': return $root.circle.ReverseSequenceOptions.decodeText(reader, json); - case 'MatrixDiagOptions': return $root.circle.MatrixDiagOptions.decodeText(reader, json); - case 'QuantizeOptions': return $root.circle.QuantizeOptions.decodeText(reader, json); - case 'MatrixSetDiagOptions': return $root.circle.MatrixSetDiagOptions.decodeText(reader, json); - case 'HardSwishOptions': return $root.circle.HardSwishOptions.decodeText(reader, json); - case 'IfOptions': return $root.circle.IfOptions.decodeText(reader, json); - case 'WhileOptions': return $root.circle.WhileOptions.decodeText(reader, json); - case 'DepthToSpaceOptions': return $root.circle.DepthToSpaceOptions.decodeText(reader, json); - case 'NonMaxSuppressionV4Options': return $root.circle.NonMaxSuppressionV4Options.decodeText(reader, json); - case 'NonMaxSuppressionV5Options': return $root.circle.NonMaxSuppressionV5Options.decodeText(reader, json); - case 'ScatterNdOptions': return $root.circle.ScatterNdOptions.decodeText(reader, json); - case 'SelectV2Options': return $root.circle.SelectV2Options.decodeText(reader, json); - case 'DensifyOptions': return $root.circle.DensifyOptions.decodeText(reader, json); - case 'SegmentSumOptions': return $root.circle.SegmentSumOptions.decodeText(reader, json); - case 'BatchMatMulOptions': return $root.circle.BatchMatMulOptions.decodeText(reader, json); - case 'BCQGatherOptions': return $root.circle.BCQGatherOptions.decodeText(reader, json); - case 'BCQFullyConnectedOptions': return $root.circle.BCQFullyConnectedOptions.decodeText(reader, json); - case 'InstanceNormOptions': return $root.circle.InstanceNormOptions.decodeText(reader, json); - default: return undefined; - } - } -}; - -$root.circle.Padding = { - SAME: 0, - VALID: 1 -}; - -$root.circle.ActivationFunctionType = { - NONE: 0, - RELU: 1, - RELU_N1_TO_1: 2, - RELU6: 3, - TANH: 4, - SIGN_BIT: 5 -}; - -$root.circle.Conv2DOptions = class Conv2DOptions { - - static decode(reader, position) { - const $ = new $root.circle.Conv2DOptions(); - $.padding = reader.int8_(position, 4, 0); - $.stride_w = reader.int32_(position, 6, 0); - $.stride_h = reader.int32_(position, 8, 0); - $.fused_activation_function = reader.int8_(position, 10, 0); - $.dilation_w_factor = reader.int32_(position, 12, 1); - $.dilation_h_factor = reader.int32_(position, 14, 1); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.Conv2DOptions(); - $.padding = $root.circle.Padding[json.padding]; - $.stride_w = reader.value(json.stride_w, 0); - $.stride_h = reader.value(json.stride_h, 0); - $.fused_activation_function = $root.circle.ActivationFunctionType[json.fused_activation_function]; - $.dilation_w_factor = reader.value(json.dilation_w_factor, 1); - $.dilation_h_factor = reader.value(json.dilation_h_factor, 1); - return $; - } -}; - -$root.circle.Pool2DOptions = class Pool2DOptions { - - static decode(reader, position) { - const $ = new $root.circle.Pool2DOptions(); - $.padding = reader.int8_(position, 4, 0); - $.stride_w = reader.int32_(position, 6, 0); - $.stride_h = reader.int32_(position, 8, 0); - $.filter_width = reader.int32_(position, 10, 0); - $.filter_height = reader.int32_(position, 12, 0); - $.fused_activation_function = reader.int8_(position, 14, 0); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.Pool2DOptions(); - $.padding = $root.circle.Padding[json.padding]; - $.stride_w = reader.value(json.stride_w, 0); - $.stride_h = reader.value(json.stride_h, 0); - $.filter_width = reader.value(json.filter_width, 0); - $.filter_height = reader.value(json.filter_height, 0); - $.fused_activation_function = $root.circle.ActivationFunctionType[json.fused_activation_function]; - return $; - } -}; - -$root.circle.DepthwiseConv2DOptions = class DepthwiseConv2DOptions { - - static decode(reader, position) { - const $ = new $root.circle.DepthwiseConv2DOptions(); - $.padding = reader.int8_(position, 4, 0); - $.stride_w = reader.int32_(position, 6, 0); - $.stride_h = reader.int32_(position, 8, 0); - $.depth_multiplier = reader.int32_(position, 10, 0); - $.fused_activation_function = reader.int8_(position, 12, 0); - $.dilation_w_factor = reader.int32_(position, 14, 1); - $.dilation_h_factor = reader.int32_(position, 16, 1); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.DepthwiseConv2DOptions(); - $.padding = $root.circle.Padding[json.padding]; - $.stride_w = reader.value(json.stride_w, 0); - $.stride_h = reader.value(json.stride_h, 0); - $.depth_multiplier = reader.value(json.depth_multiplier, 0); - $.fused_activation_function = $root.circle.ActivationFunctionType[json.fused_activation_function]; - $.dilation_w_factor = reader.value(json.dilation_w_factor, 1); - $.dilation_h_factor = reader.value(json.dilation_h_factor, 1); - return $; - } -}; - -$root.circle.ConcatEmbeddingsOptions = class ConcatEmbeddingsOptions { - - static decode(reader, position) { - const $ = new $root.circle.ConcatEmbeddingsOptions(); - $.num_channels = reader.int32_(position, 4, 0); - $.num_columns_per_channel = reader.typedArray(position, 6, Int32Array); - $.embedding_dim_per_channel = reader.typedArray(position, 8, Int32Array); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.ConcatEmbeddingsOptions(); - $.num_channels = reader.value(json.num_channels, 0); - $.num_columns_per_channel = reader.typedArray(json.num_columns_per_channel, Int32Array); - $.embedding_dim_per_channel = reader.typedArray(json.embedding_dim_per_channel, Int32Array); - return $; - } -}; - -$root.circle.LSHProjectionType = { - UNKNOWN: 0, - SPARSE: 1, - DENSE: 2 -}; - -$root.circle.LSHProjectionOptions = class LSHProjectionOptions { - - static decode(reader, position) { - const $ = new $root.circle.LSHProjectionOptions(); - $.type = reader.int8_(position, 4, 0); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.LSHProjectionOptions(); - $.type = $root.circle.LSHProjectionType[json.type]; - return $; - } -}; - -$root.circle.SVDFOptions = class SVDFOptions { - - static decode(reader, position) { - const $ = new $root.circle.SVDFOptions(); - $.rank = reader.int32_(position, 4, 0); - $.fused_activation_function = reader.int8_(position, 6, 0); - $.asymmetric_quantize_inputs = reader.bool_(position, 8, false); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.SVDFOptions(); - $.rank = reader.value(json.rank, 0); - $.fused_activation_function = $root.circle.ActivationFunctionType[json.fused_activation_function]; - $.asymmetric_quantize_inputs = reader.value(json.asymmetric_quantize_inputs, false); - return $; - } -}; - -$root.circle.RNNOptions = class RNNOptions { - - static decode(reader, position) { - const $ = new $root.circle.RNNOptions(); - $.fused_activation_function = reader.int8_(position, 4, 0); - $.asymmetric_quantize_inputs = reader.bool_(position, 6, false); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.RNNOptions(); - $.fused_activation_function = $root.circle.ActivationFunctionType[json.fused_activation_function]; - $.asymmetric_quantize_inputs = reader.value(json.asymmetric_quantize_inputs, false); - return $; - } -}; - -$root.circle.SequenceRNNOptions = class SequenceRNNOptions { - - static decode(reader, position) { - const $ = new $root.circle.SequenceRNNOptions(); - $.time_major = reader.bool_(position, 4, false); - $.fused_activation_function = reader.int8_(position, 6, 0); - $.asymmetric_quantize_inputs = reader.bool_(position, 8, false); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.SequenceRNNOptions(); - $.time_major = reader.value(json.time_major, false); - $.fused_activation_function = $root.circle.ActivationFunctionType[json.fused_activation_function]; - $.asymmetric_quantize_inputs = reader.value(json.asymmetric_quantize_inputs, false); - return $; - } -}; - -$root.circle.BidirectionalSequenceRNNOptions = class BidirectionalSequenceRNNOptions { - - static decode(reader, position) { - const $ = new $root.circle.BidirectionalSequenceRNNOptions(); - $.time_major = reader.bool_(position, 4, false); - $.fused_activation_function = reader.int8_(position, 6, 0); - $.merge_outputs = reader.bool_(position, 8, false); - $.asymmetric_quantize_inputs = reader.bool_(position, 10, false); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.BidirectionalSequenceRNNOptions(); - $.time_major = reader.value(json.time_major, false); - $.fused_activation_function = $root.circle.ActivationFunctionType[json.fused_activation_function]; - $.merge_outputs = reader.value(json.merge_outputs, false); - $.asymmetric_quantize_inputs = reader.value(json.asymmetric_quantize_inputs, false); - return $; - } -}; - -$root.circle.FullyConnectedOptionsWeightsFormat = { - DEFAULT: 0, - SHUFFLED4x16INT8: 1, - SHUFFLED16x1FLOAT32: 127 -}; - -$root.circle.FullyConnectedOptions = class FullyConnectedOptions { - - static decode(reader, position) { - const $ = new $root.circle.FullyConnectedOptions(); - $.fused_activation_function = reader.int8_(position, 4, 0); - $.weights_format = reader.int8_(position, 6, 0); - $.keep_num_dims = reader.bool_(position, 8, false); - $.asymmetric_quantize_inputs = reader.bool_(position, 10, false); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.FullyConnectedOptions(); - $.fused_activation_function = $root.circle.ActivationFunctionType[json.fused_activation_function]; - $.weights_format = $root.circle.FullyConnectedOptionsWeightsFormat[json.weights_format]; - $.keep_num_dims = reader.value(json.keep_num_dims, false); - $.asymmetric_quantize_inputs = reader.value(json.asymmetric_quantize_inputs, false); - return $; - } -}; - -$root.circle.SoftmaxOptions = class SoftmaxOptions { - - static decode(reader, position) { - const $ = new $root.circle.SoftmaxOptions(); - $.beta = reader.float32_(position, 4, 0); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.SoftmaxOptions(); - $.beta = reader.value(json.beta, 0); - return $; - } -}; - -$root.circle.ConcatenationOptions = class ConcatenationOptions { - - static decode(reader, position) { - const $ = new $root.circle.ConcatenationOptions(); - $.axis = reader.int32_(position, 4, 0); - $.fused_activation_function = reader.int8_(position, 6, 0); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.ConcatenationOptions(); - $.axis = reader.value(json.axis, 0); - $.fused_activation_function = $root.circle.ActivationFunctionType[json.fused_activation_function]; - return $; - } -}; - -$root.circle.AddOptions = class AddOptions { - - static decode(reader, position) { - const $ = new $root.circle.AddOptions(); - $.fused_activation_function = reader.int8_(position, 4, 0); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.AddOptions(); - $.fused_activation_function = $root.circle.ActivationFunctionType[json.fused_activation_function]; - return $; - } -}; - -$root.circle.MulOptions = class MulOptions { - - static decode(reader, position) { - const $ = new $root.circle.MulOptions(); - $.fused_activation_function = reader.int8_(position, 4, 0); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.MulOptions(); - $.fused_activation_function = $root.circle.ActivationFunctionType[json.fused_activation_function]; - return $; - } -}; - -$root.circle.L2NormOptions = class L2NormOptions { - - static decode(reader, position) { - const $ = new $root.circle.L2NormOptions(); - $.fused_activation_function = reader.int8_(position, 4, 0); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.L2NormOptions(); - $.fused_activation_function = $root.circle.ActivationFunctionType[json.fused_activation_function]; - return $; - } -}; - -$root.circle.LocalResponseNormalizationOptions = class LocalResponseNormalizationOptions { - - static decode(reader, position) { - const $ = new $root.circle.LocalResponseNormalizationOptions(); - $.radius = reader.int32_(position, 4, 0); - $.bias = reader.float32_(position, 6, 0); - $.alpha = reader.float32_(position, 8, 0); - $.beta = reader.float32_(position, 10, 0); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.LocalResponseNormalizationOptions(); - $.radius = reader.value(json.radius, 0); - $.bias = reader.value(json.bias, 0); - $.alpha = reader.value(json.alpha, 0); - $.beta = reader.value(json.beta, 0); - return $; - } -}; - -$root.circle.LSTMKernelType = { - FULL: 0, - BASIC: 1 -}; - -$root.circle.LSTMOptions = class LSTMOptions { - - static decode(reader, position) { - const $ = new $root.circle.LSTMOptions(); - $.fused_activation_function = reader.int8_(position, 4, 0); - $.cell_clip = reader.float32_(position, 6, 0); - $.proj_clip = reader.float32_(position, 8, 0); - $.kernel_type = reader.int8_(position, 10, 0); - $.asymmetric_quantize_inputs = reader.bool_(position, 12, false); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.LSTMOptions(); - $.fused_activation_function = $root.circle.ActivationFunctionType[json.fused_activation_function]; - $.cell_clip = reader.value(json.cell_clip, 0); - $.proj_clip = reader.value(json.proj_clip, 0); - $.kernel_type = $root.circle.LSTMKernelType[json.kernel_type]; - $.asymmetric_quantize_inputs = reader.value(json.asymmetric_quantize_inputs, false); - return $; - } -}; - -$root.circle.UnidirectionalSequenceLSTMOptions = class UnidirectionalSequenceLSTMOptions { - - static decode(reader, position) { - const $ = new $root.circle.UnidirectionalSequenceLSTMOptions(); - $.fused_activation_function = reader.int8_(position, 4, 0); - $.cell_clip = reader.float32_(position, 6, 0); - $.proj_clip = reader.float32_(position, 8, 0); - $.time_major = reader.bool_(position, 10, false); - $.asymmetric_quantize_inputs = reader.bool_(position, 12, false); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.UnidirectionalSequenceLSTMOptions(); - $.fused_activation_function = $root.circle.ActivationFunctionType[json.fused_activation_function]; - $.cell_clip = reader.value(json.cell_clip, 0); - $.proj_clip = reader.value(json.proj_clip, 0); - $.time_major = reader.value(json.time_major, false); - $.asymmetric_quantize_inputs = reader.value(json.asymmetric_quantize_inputs, false); - return $; - } -}; - -$root.circle.BidirectionalSequenceLSTMOptions = class BidirectionalSequenceLSTMOptions { - - static decode(reader, position) { - const $ = new $root.circle.BidirectionalSequenceLSTMOptions(); - $.fused_activation_function = reader.int8_(position, 4, 0); - $.cell_clip = reader.float32_(position, 6, 0); - $.proj_clip = reader.float32_(position, 8, 0); - $.merge_outputs = reader.bool_(position, 10, false); - $.time_major = reader.bool_(position, 12, true); - $.asymmetric_quantize_inputs = reader.bool_(position, 14, false); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.BidirectionalSequenceLSTMOptions(); - $.fused_activation_function = $root.circle.ActivationFunctionType[json.fused_activation_function]; - $.cell_clip = reader.value(json.cell_clip, 0); - $.proj_clip = reader.value(json.proj_clip, 0); - $.merge_outputs = reader.value(json.merge_outputs, false); - $.time_major = reader.value(json.time_major, true); - $.asymmetric_quantize_inputs = reader.value(json.asymmetric_quantize_inputs, false); - return $; - } -}; - -$root.circle.ResizeBilinearOptions = class ResizeBilinearOptions { - - static decode(reader, position) { - const $ = new $root.circle.ResizeBilinearOptions(); - $.new_height = reader.int32_(position, 4, 0); - $.new_width = reader.int32_(position, 6, 0); - $.align_corners = reader.bool_(position, 8, false); - $.half_pixel_centers = reader.bool_(position, 10, false); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.ResizeBilinearOptions(); - $.new_height = reader.value(json.new_height, 0); - $.new_width = reader.value(json.new_width, 0); - $.align_corners = reader.value(json.align_corners, false); - $.half_pixel_centers = reader.value(json.half_pixel_centers, false); - return $; - } -}; - -$root.circle.ResizeNearestNeighborOptions = class ResizeNearestNeighborOptions { - - static decode(reader, position) { - const $ = new $root.circle.ResizeNearestNeighborOptions(); - $.align_corners = reader.bool_(position, 4, false); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.ResizeNearestNeighborOptions(); - $.align_corners = reader.value(json.align_corners, false); - return $; - } -}; - -$root.circle.CallOptions = class CallOptions { - - static decode(reader, position) { - const $ = new $root.circle.CallOptions(); - $.subgraph = reader.uint32_(position, 4, 0); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.CallOptions(); - $.subgraph = reader.value(json.subgraph, 0); - return $; - } -}; - -$root.circle.PadOptions = class PadOptions { - - static decode(/* reader, position */) { - const $ = new $root.circle.PadOptions(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.PadOptions(); - return $; - } -}; - -$root.circle.PadV2Options = class PadV2Options { - - static decode(/* reader, position */) { - const $ = new $root.circle.PadV2Options(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.PadV2Options(); - return $; - } -}; - -$root.circle.ReshapeOptions = class ReshapeOptions { - - static decode(reader, position) { - const $ = new $root.circle.ReshapeOptions(); - $.new_shape = reader.typedArray(position, 4, Int32Array); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.ReshapeOptions(); - $.new_shape = reader.typedArray(json.new_shape, Int32Array); - return $; - } -}; - -$root.circle.SpaceToBatchNDOptions = class SpaceToBatchNDOptions { - - static decode(/* reader, position */) { - const $ = new $root.circle.SpaceToBatchNDOptions(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.SpaceToBatchNDOptions(); - return $; - } -}; - -$root.circle.BatchToSpaceNDOptions = class BatchToSpaceNDOptions { - - static decode(/* reader, position */) { - const $ = new $root.circle.BatchToSpaceNDOptions(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.BatchToSpaceNDOptions(); - return $; - } -}; - -$root.circle.SkipGramOptions = class SkipGramOptions { - - static decode(reader, position) { - const $ = new $root.circle.SkipGramOptions(); - $.ngram_size = reader.int32_(position, 4, 0); - $.max_skip_size = reader.int32_(position, 6, 0); - $.include_all_ngrams = reader.bool_(position, 8, false); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.SkipGramOptions(); - $.ngram_size = reader.value(json.ngram_size, 0); - $.max_skip_size = reader.value(json.max_skip_size, 0); - $.include_all_ngrams = reader.value(json.include_all_ngrams, false); - return $; - } -}; - -$root.circle.SpaceToDepthOptions = class SpaceToDepthOptions { - - static decode(reader, position) { - const $ = new $root.circle.SpaceToDepthOptions(); - $.block_size = reader.int32_(position, 4, 0); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.SpaceToDepthOptions(); - $.block_size = reader.value(json.block_size, 0); - return $; - } -}; - -$root.circle.DepthToSpaceOptions = class DepthToSpaceOptions { - - static decode(reader, position) { - const $ = new $root.circle.DepthToSpaceOptions(); - $.block_size = reader.int32_(position, 4, 0); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.DepthToSpaceOptions(); - $.block_size = reader.value(json.block_size, 0); - return $; - } -}; - -$root.circle.SubOptions = class SubOptions { - - static decode(reader, position) { - const $ = new $root.circle.SubOptions(); - $.fused_activation_function = reader.int8_(position, 4, 0); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.SubOptions(); - $.fused_activation_function = $root.circle.ActivationFunctionType[json.fused_activation_function]; - return $; - } -}; - -$root.circle.DivOptions = class DivOptions { - - static decode(reader, position) { - const $ = new $root.circle.DivOptions(); - $.fused_activation_function = reader.int8_(position, 4, 0); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.DivOptions(); - $.fused_activation_function = $root.circle.ActivationFunctionType[json.fused_activation_function]; - return $; - } -}; - -$root.circle.TopKV2Options = class TopKV2Options { - - static decode(/* reader, position */) { - const $ = new $root.circle.TopKV2Options(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.TopKV2Options(); - return $; - } -}; - -$root.circle.CombinerType = { - SUM: 0, - MEAN: 1, - SQRTN: 2 -}; - -$root.circle.EmbeddingLookupSparseOptions = class EmbeddingLookupSparseOptions { - - static decode(reader, position) { - const $ = new $root.circle.EmbeddingLookupSparseOptions(); - $.combiner = reader.int8_(position, 4, 0); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.EmbeddingLookupSparseOptions(); - $.combiner = $root.circle.CombinerType[json.combiner]; - return $; - } -}; - -$root.circle.GatherOptions = class GatherOptions { - - static decode(reader, position) { - const $ = new $root.circle.GatherOptions(); - $.axis = reader.int32_(position, 4, 0); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.GatherOptions(); - $.axis = reader.value(json.axis, 0); - return $; - } -}; - -$root.circle.TransposeOptions = class TransposeOptions { - - static decode(/* reader, position */) { - const $ = new $root.circle.TransposeOptions(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.TransposeOptions(); - return $; - } -}; - -$root.circle.ExpOptions = class ExpOptions { - - static decode(/* reader, position */) { - const $ = new $root.circle.ExpOptions(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.ExpOptions(); - return $; - } -}; - -$root.circle.CosOptions = class CosOptions { - - static decode(/* reader, position */) { - const $ = new $root.circle.CosOptions(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.CosOptions(); - return $; - } -}; - -$root.circle.ReducerOptions = class ReducerOptions { - - static decode(reader, position) { - const $ = new $root.circle.ReducerOptions(); - $.keep_dims = reader.bool_(position, 4, false); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.ReducerOptions(); - $.keep_dims = reader.value(json.keep_dims, false); - return $; - } -}; - -$root.circle.SqueezeOptions = class SqueezeOptions { - - static decode(reader, position) { - const $ = new $root.circle.SqueezeOptions(); - $.squeeze_dims = reader.typedArray(position, 4, Int32Array); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.SqueezeOptions(); - $.squeeze_dims = reader.typedArray(json.squeeze_dims, Int32Array); - return $; - } -}; - -$root.circle.SplitOptions = class SplitOptions { - - static decode(reader, position) { - const $ = new $root.circle.SplitOptions(); - $.num_splits = reader.int32_(position, 4, 0); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.SplitOptions(); - $.num_splits = reader.value(json.num_splits, 0); - return $; - } -}; - -$root.circle.SplitVOptions = class SplitVOptions { - - static decode(reader, position) { - const $ = new $root.circle.SplitVOptions(); - $.num_splits = reader.int32_(position, 4, 0); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.SplitVOptions(); - $.num_splits = reader.value(json.num_splits, 0); - return $; - } -}; - -$root.circle.StridedSliceOptions = class StridedSliceOptions { - - static decode(reader, position) { - const $ = new $root.circle.StridedSliceOptions(); - $.begin_mask = reader.int32_(position, 4, 0); - $.end_mask = reader.int32_(position, 6, 0); - $.ellipsis_mask = reader.int32_(position, 8, 0); - $.new_axis_mask = reader.int32_(position, 10, 0); - $.shrink_axis_mask = reader.int32_(position, 12, 0); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.StridedSliceOptions(); - $.begin_mask = reader.value(json.begin_mask, 0); - $.end_mask = reader.value(json.end_mask, 0); - $.ellipsis_mask = reader.value(json.ellipsis_mask, 0); - $.new_axis_mask = reader.value(json.new_axis_mask, 0); - $.shrink_axis_mask = reader.value(json.shrink_axis_mask, 0); - return $; - } -}; - -$root.circle.LogSoftmaxOptions = class LogSoftmaxOptions { - - static decode(/* reader, position */) { - const $ = new $root.circle.LogSoftmaxOptions(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.LogSoftmaxOptions(); - return $; - } -}; - -$root.circle.CastOptions = class CastOptions { - - static decode(reader, position) { - const $ = new $root.circle.CastOptions(); - $.in_data_type = reader.int8_(position, 4, 0); - $.out_data_type = reader.int8_(position, 6, 0); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.CastOptions(); - $.in_data_type = $root.circle.TensorType[json.in_data_type]; - $.out_data_type = $root.circle.TensorType[json.out_data_type]; - return $; - } -}; - -$root.circle.DequantizeOptions = class DequantizeOptions { - - static decode(/* reader, position */) { - const $ = new $root.circle.DequantizeOptions(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.DequantizeOptions(); - return $; - } -}; - -$root.circle.MaximumMinimumOptions = class MaximumMinimumOptions { - - static decode(/* reader, position */) { - const $ = new $root.circle.MaximumMinimumOptions(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.MaximumMinimumOptions(); - return $; - } -}; - -$root.circle.TileOptions = class TileOptions { - - static decode(/* reader, position */) { - const $ = new $root.circle.TileOptions(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.TileOptions(); - return $; - } -}; - -$root.circle.ArgMaxOptions = class ArgMaxOptions { - - static decode(reader, position) { - const $ = new $root.circle.ArgMaxOptions(); - $.output_type = reader.int8_(position, 4, 0); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.ArgMaxOptions(); - $.output_type = $root.circle.TensorType[json.output_type]; - return $; - } -}; - -$root.circle.ArgMinOptions = class ArgMinOptions { - - static decode(reader, position) { - const $ = new $root.circle.ArgMinOptions(); - $.output_type = reader.int8_(position, 4, 0); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.ArgMinOptions(); - $.output_type = $root.circle.TensorType[json.output_type]; - return $; - } -}; - -$root.circle.GreaterOptions = class GreaterOptions { - - static decode(/* reader, position */) { - const $ = new $root.circle.GreaterOptions(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.GreaterOptions(); - return $; - } -}; - -$root.circle.GreaterEqualOptions = class GreaterEqualOptions { - - static decode(/* reader, position */) { - const $ = new $root.circle.GreaterEqualOptions(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.GreaterEqualOptions(); - return $; - } -}; - -$root.circle.LessOptions = class LessOptions { - - static decode(/* reader, position */) { - const $ = new $root.circle.LessOptions(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.LessOptions(); - return $; - } -}; - -$root.circle.LessEqualOptions = class LessEqualOptions { - - static decode(/* reader, position */) { - const $ = new $root.circle.LessEqualOptions(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.LessEqualOptions(); - return $; - } -}; - -$root.circle.NegOptions = class NegOptions { - - static decode(/* reader, position */) { - const $ = new $root.circle.NegOptions(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.NegOptions(); - return $; - } -}; - -$root.circle.SelectOptions = class SelectOptions { - - static decode(/* reader, position */) { - const $ = new $root.circle.SelectOptions(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.SelectOptions(); - return $; - } -}; - -$root.circle.SliceOptions = class SliceOptions { - - static decode(/* reader, position */) { - const $ = new $root.circle.SliceOptions(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.SliceOptions(); - return $; - } -}; - -$root.circle.TransposeConvOptions = class TransposeConvOptions { - - static decode(reader, position) { - const $ = new $root.circle.TransposeConvOptions(); - $.padding = reader.int8_(position, 4, 0); - $.stride_w = reader.int32_(position, 6, 0); - $.stride_h = reader.int32_(position, 8, 0); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.TransposeConvOptions(); - $.padding = $root.circle.Padding[json.padding]; - $.stride_w = reader.value(json.stride_w, 0); - $.stride_h = reader.value(json.stride_h, 0); - return $; - } -}; - -$root.circle.ExpandDimsOptions = class ExpandDimsOptions { - - static decode(/* reader, position */) { - const $ = new $root.circle.ExpandDimsOptions(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.ExpandDimsOptions(); - return $; - } -}; - -$root.circle.SparseToDenseOptions = class SparseToDenseOptions { - - static decode(reader, position) { - const $ = new $root.circle.SparseToDenseOptions(); - $.validate_indices = reader.bool_(position, 4, false); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.SparseToDenseOptions(); - $.validate_indices = reader.value(json.validate_indices, false); - return $; - } -}; - -$root.circle.EqualOptions = class EqualOptions { - - static decode(/* reader, position */) { - const $ = new $root.circle.EqualOptions(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.EqualOptions(); - return $; - } -}; - -$root.circle.NotEqualOptions = class NotEqualOptions { - - static decode(/* reader, position */) { - const $ = new $root.circle.NotEqualOptions(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.NotEqualOptions(); - return $; - } -}; - -$root.circle.ShapeOptions = class ShapeOptions { - - static decode(reader, position) { - const $ = new $root.circle.ShapeOptions(); - $.out_type = reader.int8_(position, 4, 0); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.ShapeOptions(); - $.out_type = $root.circle.TensorType[json.out_type]; - return $; - } -}; - -$root.circle.RankOptions = class RankOptions { - - static decode(/* reader, position */) { - const $ = new $root.circle.RankOptions(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.RankOptions(); - return $; - } -}; - -$root.circle.PowOptions = class PowOptions { - - static decode(/* reader, position */) { - const $ = new $root.circle.PowOptions(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.PowOptions(); - return $; - } -}; - -$root.circle.FakeQuantOptions = class FakeQuantOptions { - - static decode(reader, position) { - const $ = new $root.circle.FakeQuantOptions(); - $.min = reader.float32_(position, 4, 0); - $.max = reader.float32_(position, 6, 0); - $.num_bits = reader.int32_(position, 8, 0); - $.narrow_range = reader.bool_(position, 10, false); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.FakeQuantOptions(); - $.min = reader.value(json.min, 0); - $.max = reader.value(json.max, 0); - $.num_bits = reader.value(json.num_bits, 0); - $.narrow_range = reader.value(json.narrow_range, false); - return $; - } -}; - -$root.circle.PackOptions = class PackOptions { - - static decode(reader, position) { - const $ = new $root.circle.PackOptions(); - $.values_count = reader.int32_(position, 4, 0); - $.axis = reader.int32_(position, 6, 0); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.PackOptions(); - $.values_count = reader.value(json.values_count, 0); - $.axis = reader.value(json.axis, 0); - return $; - } -}; - -$root.circle.LogicalOrOptions = class LogicalOrOptions { - - static decode(/* reader, position */) { - const $ = new $root.circle.LogicalOrOptions(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.LogicalOrOptions(); - return $; - } -}; - -$root.circle.OneHotOptions = class OneHotOptions { - - static decode(reader, position) { - const $ = new $root.circle.OneHotOptions(); - $.axis = reader.int32_(position, 4, 0); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.OneHotOptions(); - $.axis = reader.value(json.axis, 0); - return $; - } -}; - -$root.circle.AbsOptions = class AbsOptions { - - static decode(/* reader, position */) { - const $ = new $root.circle.AbsOptions(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.AbsOptions(); - return $; - } -}; - -$root.circle.HardSwishOptions = class HardSwishOptions { - - static decode(/* reader, position */) { - const $ = new $root.circle.HardSwishOptions(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.HardSwishOptions(); - return $; - } -}; - -$root.circle.LogicalAndOptions = class LogicalAndOptions { - - static decode(/* reader, position */) { - const $ = new $root.circle.LogicalAndOptions(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.LogicalAndOptions(); - return $; - } -}; - -$root.circle.LogicalNotOptions = class LogicalNotOptions { - - static decode(/* reader, position */) { - const $ = new $root.circle.LogicalNotOptions(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.LogicalNotOptions(); - return $; - } -}; - -$root.circle.UnpackOptions = class UnpackOptions { - - static decode(reader, position) { - const $ = new $root.circle.UnpackOptions(); - $.num = reader.int32_(position, 4, 0); - $.axis = reader.int32_(position, 6, 0); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.UnpackOptions(); - $.num = reader.value(json.num, 0); - $.axis = reader.value(json.axis, 0); - return $; - } -}; - -$root.circle.FloorDivOptions = class FloorDivOptions { - - static decode(/* reader, position */) { - const $ = new $root.circle.FloorDivOptions(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.FloorDivOptions(); - return $; - } -}; - -$root.circle.SquareOptions = class SquareOptions { - - static decode(/* reader, position */) { - const $ = new $root.circle.SquareOptions(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.SquareOptions(); - return $; - } -}; - -$root.circle.ZerosLikeOptions = class ZerosLikeOptions { - - static decode(/* reader, position */) { - const $ = new $root.circle.ZerosLikeOptions(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.ZerosLikeOptions(); - return $; - } -}; - -$root.circle.FillOptions = class FillOptions { - - static decode(/* reader, position */) { - const $ = new $root.circle.FillOptions(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.FillOptions(); - return $; - } -}; - -$root.circle.FloorModOptions = class FloorModOptions { - - static decode(/* reader, position */) { - const $ = new $root.circle.FloorModOptions(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.FloorModOptions(); - return $; - } -}; - -$root.circle.RangeOptions = class RangeOptions { - - static decode(/* reader, position */) { - const $ = new $root.circle.RangeOptions(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.RangeOptions(); - return $; - } -}; - -$root.circle.LeakyReluOptions = class LeakyReluOptions { - - static decode(reader, position) { - const $ = new $root.circle.LeakyReluOptions(); - $.alpha = reader.float32_(position, 4, 0); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.LeakyReluOptions(); - $.alpha = reader.value(json.alpha, 0); - return $; - } -}; - -$root.circle.SquaredDifferenceOptions = class SquaredDifferenceOptions { - - static decode(/* reader, position */) { - const $ = new $root.circle.SquaredDifferenceOptions(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.SquaredDifferenceOptions(); - return $; - } -}; - -$root.circle.MirrorPadMode = { - REFLECT: 0, - SYMMETRIC: 1 -}; - -$root.circle.MirrorPadOptions = class MirrorPadOptions { - - static decode(reader, position) { - const $ = new $root.circle.MirrorPadOptions(); - $.mode = reader.int8_(position, 4, 0); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.MirrorPadOptions(); - $.mode = $root.circle.MirrorPadMode[json.mode]; - return $; - } -}; - -$root.circle.UniqueOptions = class UniqueOptions { - - static decode(reader, position) { - const $ = new $root.circle.UniqueOptions(); - $.idx_out_type = reader.int8_(position, 4, 2); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.UniqueOptions(); - $.idx_out_type = $root.circle.TensorType[json.idx_out_type]; - return $; - } -}; - -$root.circle.ReverseV2Options = class ReverseV2Options { - - static decode(/* reader, position */) { - const $ = new $root.circle.ReverseV2Options(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.ReverseV2Options(); - return $; - } -}; - -$root.circle.AddNOptions = class AddNOptions { - - static decode(/* reader, position */) { - const $ = new $root.circle.AddNOptions(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.AddNOptions(); - return $; - } -}; - -$root.circle.GatherNdOptions = class GatherNdOptions { - - static decode(/* reader, position */) { - const $ = new $root.circle.GatherNdOptions(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.GatherNdOptions(); - return $; - } -}; - -$root.circle.WhereOptions = class WhereOptions { - - static decode(/* reader, position */) { - const $ = new $root.circle.WhereOptions(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.WhereOptions(); - return $; - } -}; - -$root.circle.ReverseSequenceOptions = class ReverseSequenceOptions { - - static decode(reader, position) { - const $ = new $root.circle.ReverseSequenceOptions(); - $.seq_dim = reader.int32_(position, 4, 0); - $.batch_dim = reader.int32_(position, 6, 0); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.ReverseSequenceOptions(); - $.seq_dim = reader.value(json.seq_dim, 0); - $.batch_dim = reader.value(json.batch_dim, 0); - return $; - } -}; - -$root.circle.MatrixDiagOptions = class MatrixDiagOptions { - - static decode(/* reader, position */) { - const $ = new $root.circle.MatrixDiagOptions(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.MatrixDiagOptions(); - return $; - } -}; - -$root.circle.QuantizeOptions = class QuantizeOptions { - - static decode(/* reader, position */) { - const $ = new $root.circle.QuantizeOptions(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.QuantizeOptions(); - return $; - } -}; - -$root.circle.MatrixSetDiagOptions = class MatrixSetDiagOptions { - - static decode(/* reader, position */) { - const $ = new $root.circle.MatrixSetDiagOptions(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.MatrixSetDiagOptions(); - return $; - } -}; - -$root.circle.IfOptions = class IfOptions { - - static decode(reader, position) { - const $ = new $root.circle.IfOptions(); - $.then_subgraph_index = reader.int32_(position, 4, 0); - $.else_subgraph_index = reader.int32_(position, 6, 0); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.IfOptions(); - $.then_subgraph_index = reader.value(json.then_subgraph_index, 0); - $.else_subgraph_index = reader.value(json.else_subgraph_index, 0); - return $; - } -}; - -$root.circle.WhileOptions = class WhileOptions { - - static decode(reader, position) { - const $ = new $root.circle.WhileOptions(); - $.cond_subgraph_index = reader.int32_(position, 4, 0); - $.body_subgraph_index = reader.int32_(position, 6, 0); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.WhileOptions(); - $.cond_subgraph_index = reader.value(json.cond_subgraph_index, 0); - $.body_subgraph_index = reader.value(json.body_subgraph_index, 0); - return $; - } -}; - -$root.circle.NonMaxSuppressionV4Options = class NonMaxSuppressionV4Options { - - static decode(/* reader, position */) { - const $ = new $root.circle.NonMaxSuppressionV4Options(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.NonMaxSuppressionV4Options(); - return $; - } -}; - -$root.circle.NonMaxSuppressionV5Options = class NonMaxSuppressionV5Options { - - static decode(/* reader, position */) { - const $ = new $root.circle.NonMaxSuppressionV5Options(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.NonMaxSuppressionV5Options(); - return $; - } -}; - -$root.circle.ScatterNdOptions = class ScatterNdOptions { - - static decode(/* reader, position */) { - const $ = new $root.circle.ScatterNdOptions(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.ScatterNdOptions(); - return $; - } -}; - -$root.circle.SelectV2Options = class SelectV2Options { - - static decode(/* reader, position */) { - const $ = new $root.circle.SelectV2Options(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.SelectV2Options(); - return $; - } -}; - -$root.circle.DensifyOptions = class DensifyOptions { - - static decode(/* reader, position */) { - const $ = new $root.circle.DensifyOptions(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.DensifyOptions(); - return $; - } -}; - -$root.circle.SegmentSumOptions = class SegmentSumOptions { - - static decode(/* reader, position */) { - const $ = new $root.circle.SegmentSumOptions(); - return $; - } - - static decodeText(/* reader, json */) { - const $ = new $root.circle.SegmentSumOptions(); - return $; - } -}; - -$root.circle.BatchMatMulOptions = class BatchMatMulOptions { - - static decode(reader, position) { - const $ = new $root.circle.BatchMatMulOptions(); - $.adjoint_lhs = reader.bool_(position, 4, false); - $.adjoint_rhs = reader.bool_(position, 6, false); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.BatchMatMulOptions(); - $.adjoint_lhs = reader.value(json.adjoint_lhs, false); - $.adjoint_rhs = reader.value(json.adjoint_rhs, false); - return $; - } -}; - -$root.circle.BCQGatherOptions = class BCQGatherOptions { - - static decode(reader, position) { - const $ = new $root.circle.BCQGatherOptions(); - $.input_hidden_size = reader.int32_(position, 4, 0); - $.axis = reader.int32_(position, 6, 0); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.BCQGatherOptions(); - $.input_hidden_size = reader.value(json.input_hidden_size, 0); - $.axis = reader.value(json.axis, 0); - return $; - } -}; - -$root.circle.BCQFullyConnectedOptions = class BCQFullyConnectedOptions { - - static decode(reader, position) { - const $ = new $root.circle.BCQFullyConnectedOptions(); - $.weights_hidden_size = reader.int32_(position, 4, 0); - $.fused_activation_function = reader.int8_(position, 6, 0); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.BCQFullyConnectedOptions(); - $.weights_hidden_size = reader.value(json.weights_hidden_size, 0); - $.fused_activation_function = $root.circle.ActivationFunctionType[json.fused_activation_function]; - return $; - } -}; - -$root.circle.InstanceNormOptions = class InstanceNormOptions { - - static decode(reader, position) { - const $ = new $root.circle.InstanceNormOptions(); - $.epsilon = reader.float32_(position, 4, 0); - $.fused_activation_function = reader.int8_(position, 6, 0); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.InstanceNormOptions(); - $.epsilon = reader.value(json.epsilon, 0); - $.fused_activation_function = $root.circle.ActivationFunctionType[json.fused_activation_function]; - return $; - } -}; - -$root.circle.OperatorCode = class OperatorCode { - - static decode(reader, position) { - const $ = new $root.circle.OperatorCode(); - $.builtin_code = reader.uint8_(position, 4, 0); - $.custom_code = reader.string_(position, 6, null); - $.version = reader.int32_(position, 8, 1); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.OperatorCode(); - $.builtin_code = $root.circle.BuiltinOperator[json.builtin_code]; - $.custom_code = reader.value(json.custom_code, null); - $.version = reader.value(json.version, 1); - return $; - } -}; - -$root.circle.CustomOptionsFormat = { - FLEXBUFFERS: 0 -}; - -$root.circle.DataFormat = { - CHANNELS_LAST: 0, - CHANNELS_FIRST: 1 -}; - -$root.circle.Operator = class Operator { - - static decode(reader, position) { - const $ = new $root.circle.Operator(); - $.opcode_index = reader.uint32_(position, 4, 0); - $.inputs = reader.typedArray(position, 6, Int32Array); - $.outputs = reader.typedArray(position, 8, Int32Array); - $.builtin_options = reader.union(position, 10, $root.circle.BuiltinOptions.decode); - $.custom_options = reader.typedArray(position, 14, Uint8Array); - $.custom_options_format = reader.int8_(position, 16, 0); - $.mutating_variable_inputs = reader.bools_(position, 18); - $.intermediates = reader.typedArray(position, 20, Int32Array); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.Operator(); - $.opcode_index = reader.value(json.opcode_index, 0); - $.inputs = reader.typedArray(json.inputs, Int32Array); - $.outputs = reader.typedArray(json.outputs, Int32Array); - $.builtin_options = $root.circle.BuiltinOptions.decodeText(reader, json.builtin_options, json.builtin_options_type); - $.custom_options = reader.typedArray(json.custom_options, Uint8Array); - $.custom_options_format = $root.circle.CustomOptionsFormat[json.custom_options_format]; - $.mutating_variable_inputs = reader.array(json.mutating_variable_inputs); - $.intermediates = reader.typedArray(json.intermediates, Int32Array); - return $; - } -}; - -$root.circle.SubGraph = class SubGraph { - - static decode(reader, position) { - const $ = new $root.circle.SubGraph(); - $.tensors = reader.tableArray(position, 4, $root.circle.Tensor.decode); - $.inputs = reader.typedArray(position, 6, Int32Array); - $.outputs = reader.typedArray(position, 8, Int32Array); - $.operators = reader.tableArray(position, 10, $root.circle.Operator.decode); - $.name = reader.string_(position, 12, null); - $.data_format = reader.int8_(position, 14, 0); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.SubGraph(); - $.tensors = reader.objectArray(json.tensors, $root.circle.Tensor.decodeText); - $.inputs = reader.typedArray(json.inputs, Int32Array); - $.outputs = reader.typedArray(json.outputs, Int32Array); - $.operators = reader.objectArray(json.operators, $root.circle.Operator.decodeText); - $.name = reader.value(json.name, null); - $.data_format = $root.circle.DataFormat[json.data_format]; - return $; - } -}; - -$root.circle.Buffer = class Buffer { - - static decode(reader, position) { - const $ = new $root.circle.Buffer(); - $.data = reader.typedArray(position, 4, Uint8Array); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.Buffer(); - $.data = reader.typedArray(json.data, Uint8Array); - return $; - } -}; - -$root.circle.Metadata = class Metadata { - - static decode(reader, position) { - const $ = new $root.circle.Metadata(); - $.name = reader.string_(position, 4, null); - $.buffer = reader.uint32_(position, 6, 0); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.Metadata(); - $.name = reader.value(json.name, null); - $.buffer = reader.value(json.buffer, 0); - return $; - } -}; - -$root.circle.Model = class Model { - - static identifier(reader) { - return reader.identifier === 'CIR0'; - } - - static create(reader) { - return $root.circle.Model.decode(reader, reader.root); - } - - static createText(reader) { - return $root.circle.Model.decodeText(reader, reader.root); - } - - static decode(reader, position) { - const $ = new $root.circle.Model(); - $.version = reader.uint32_(position, 4, 0); - $.operator_codes = reader.tableArray(position, 6, $root.circle.OperatorCode.decode); - $.subgraphs = reader.tableArray(position, 8, $root.circle.SubGraph.decode); - $.description = reader.string_(position, 10, null); - $.buffers = reader.tableArray(position, 12, $root.circle.Buffer.decode); - $.metadata_buffer = reader.typedArray(position, 14, Int32Array); - $.metadata = reader.tableArray(position, 16, $root.circle.Metadata.decode); - return $; - } - - static decodeText(reader, json) { - const $ = new $root.circle.Model(); - $.version = reader.value(json.version, 0); - $.operator_codes = reader.objectArray(json.operator_codes, $root.circle.OperatorCode.decodeText); - $.subgraphs = reader.objectArray(json.subgraphs, $root.circle.SubGraph.decodeText); - $.description = reader.value(json.description, null); - $.buffers = reader.objectArray(json.buffers, $root.circle.Buffer.decodeText); - $.metadata_buffer = reader.typedArray(json.metadata_buffer, Int32Array); - $.metadata = reader.objectArray(json.metadata, $root.circle.Metadata.decodeText); - return $; - } -}; diff --git a/media/CircleEditor/external/circle.js b/media/CircleEditor/external/circle.js deleted file mode 100644 index 76b4e3d2..00000000 --- a/media/CircleEditor/external/circle.js +++ /dev/null @@ -1,856 +0,0 @@ - -var circle = circle || {}; -var flatbuffers = flatbuffers || require('./flatbuffers'); -var flexbuffers = flexbuffers || require('./flexbuffers'); -var zip = zip || require('./zip'); - -circle.ModelFactory = class { - - match(context) { - const tags = context.tags('flatbuffers'); - if (tags.get('file_identifier') === 'CIR0') { - return 'circle.flatbuffers'; - } - const obj = context.open('json'); - if (obj && obj.subgraphs && obj.operator_codes) { - return 'circle.flatbuffers.json'; - } - return undefined; - } - - open(context, match) { - return context.require('./circle-schema').then(() => { - circle.schema = flatbuffers.get('circle').circle; - let model = null; - const attachments = new Map(); - switch (match) { - case 'circle.flatbuffers.json': { - try { - const obj = context.open('json'); - const reader = new flatbuffers.TextReader(obj); - model = circle.schema.Model.createText(reader); - } - catch (error) { - const message = error && error.message ? error.message : error.toString(); - throw new circle.Error('File text format is not circle.Model (' + message.replace(/\.$/, '') + ').'); - } - break; - } - case 'circle.flatbuffers': { - const stream = context.stream; - try { - const reader = flatbuffers.BinaryReader.open(stream); - model = circle.schema.Model.create(reader); - } - catch (error) { - const message = error && error.message ? error.message : error.toString(); - throw new circle.Error('File format is not circle.Model (' + message.replace(/\.$/, '') + ').'); - } - try { - const archive = zip.Archive.open(stream); - if (archive) { - for (const entry of archive.entries) { - attachments.set(entry[0], entry[1]); - } - } - } - catch (error) { - // continue regardless of error - } - break; - } - default: { - throw new circle.Error("Unsupported Circle format '" + match + "'."); - } - } - return circle.Metadata.open(context).then((metadata) => { - return new circle.Model(metadata, model); - }); - }); - } -}; - -circle.Model = class { - - constructor(metadata, model) { - this._graphs = []; - this._format = 'Circle'; - this._format = this._format + ' v' + model.version.toString(); - this._description = model.description || ''; - const builtinOperators = new Map(); - const upperCase = new Set([ '2D', 'LSH', 'SVDF', 'RNN', 'L2', 'LSTM' ]); - for (const key of Object.keys(circle.schema.BuiltinOperator)) { - const value = key === 'BATCH_MATMUL' ? 'BATCH_MAT_MUL' : key; - const name = value.split('_').map((s) => (s.length < 1 || upperCase.has(s)) ? s : s[0] + s.substring(1).toLowerCase()).join(''); - const index = circle.schema.BuiltinOperator[key]; - builtinOperators.set(index, name); - } - const operators = model.operator_codes.map((operator) => { - const code = operator.builtin_code || 0; - const version = operator.version; - const custom = code === circle.schema.BuiltinOperator.CUSTOM; - const name = custom ? operator.custom_code ? operator.custom_code : 'Custom' : builtinOperators.has(code) ? builtinOperators.get(code) : code.toString(); - return custom ? { name: name, version: version, custom: true } : { name: name, version: version }; - }); - let modelMetadata = null; - for (const metadata of model.metadata) { - const buffer = model.buffers[metadata.buffer]; - if (buffer) { - switch (metadata.name) { - case 'min_runtime_version': { - const data = buffer.data || new Uint8Array(0); - this._runtime = data ? new TextDecoder().decode(data) : undefined; - break; - } - case 'TFLITE_METADATA': { - const data = buffer.data || new Uint8Array(0); - const reader = flatbuffers.BinaryReader.open(data); - if (circle.schema.ModelMetadata.identifier(reader)) { - modelMetadata = circle.schema.ModelMetadata.create(reader); - this._name = modelMetadata.name || ''; - this._version = modelMetadata.version || ''; - this._description = modelMetadata.description ? [ this.description, modelMetadata.description].join(' ') : this._description; - this._author = modelMetadata.author || ''; - this._license = modelMetadata.license || ''; - } - break; - } - default: { - break; - } - } - } - } - const subgraphs = model.subgraphs; - const subgraphsMetadata = modelMetadata ? modelMetadata.subgraph_metadata : null; - for (let i = 0; i < subgraphs.length; i++) { - const subgraph = subgraphs[i]; - const name = subgraphs.length > 1 ? i.toString() : ''; - const subgraphMetadata = subgraphsMetadata && i < subgraphsMetadata.length ? subgraphsMetadata[i] : null; - this._graphs.push(new circle.Graph(metadata, subgraph, subgraphMetadata, name, operators, model)); - } - } - - get format() { - return this._format; - } - - get runtime() { - return this._runtime; - } - - get name() { - return this._name; - } - - get version() { - return this._version; - } - - get description() { - return this._description; - } - - get author() { - return this._author; - } - - get license() { - return this._license; - } - - get graphs() { - return this._graphs; - } -}; - -circle.Graph = class { - - constructor(metadata, subgraph, subgraphMetadata, name, operators, model) { - this._nodes = []; - this._inputs = []; - this._outputs = []; - this._name = subgraph.name || name; - const tensors = new Map(); - const args = (index) => { - if (index === -1) { - return null; - } - if (!tensors.has(index)) { - if (index < subgraph.tensors.length) { - const tensor = subgraph.tensors[index]; - const buffer = model.buffers[tensor.buffer]; - const is_variable = tensor.is_variable; - const data = buffer ? buffer.data : null; - const initializer = (data && data.length > 0) || is_variable ? new circle.Tensor(index, tensor, buffer, is_variable) : null; - tensors.set(index, new circle.Argument(index, tensor, initializer)); - } - else { - tensors.set(index, new circle.Argument(index, { name: '' }, null)); - } - } - return tensors.get(index); - }; - for (let i = 0; i < subgraph.operators.length; i++) { - const node = subgraph.operators[i]; - const index = node.opcode_index; - const operator = index < operators.length ? operators[index] : { name: '(' + index.toString() + ')' }; - this._nodes.push(new circle.Node(metadata, node, operator, i.toString(), args)); - } - const applyTensorMetadata = (argument, tensorMetadata) => { - if (tensorMetadata) { - const description = tensorMetadata.description; - if (description) { - argument.description = description; - } - const content = tensorMetadata.content; - if (argument.type && content) { - let denotation = null; - const contentProperties = content.content_properties; - if (contentProperties instanceof circle.schema.FeatureProperties) { - denotation = 'Feature'; - } - else if (contentProperties instanceof circle.schema.ImageProperties) { - denotation = 'Image'; - switch(contentProperties.color_space) { - case 0: denotation += '(Unknown)'; break; - case 1: denotation += '(RGB)'; break; - case 2: denotation += '(Grayscale)'; break; - default: throw circle.Error("Unsupported image color space '" + contentProperties.color_space + "'."); - } - } - else if (contentProperties instanceof circle.schema.BoundingBoxProperties) { - denotation = 'BoundingBox'; - } - else if (contentProperties instanceof circle.schema.AudioProperties) { - denotation = 'Audio(' + contentProperties.sample_rate.toString() + ',' + contentProperties.channels.toString() + ')'; - } - if (denotation) { - argument.type.denotation = denotation; - } - } - } - }; - const inputs = subgraph.inputs; - for (let i = 0; i < inputs.length; i++) { - const input = inputs[i]; - const argument = args(input); - if (subgraphMetadata && i < subgraphMetadata.input_tensor_metadata.length) { - applyTensorMetadata(argument, subgraphMetadata.input_tensor_metadata[i]); - } - this._inputs.push(new circle.Parameter(argument ? argument.name : '?', true, argument ? [ argument ] : [])); - } - const outputs = subgraph.outputs; - for (let i = 0; i < outputs.length; i++) { - const output = outputs[i]; - const argument = args(output); - if (subgraphMetadata && i < subgraphMetadata.output_tensor_metadata.length) { - applyTensorMetadata(argument, subgraphMetadata.output_tensor_metadata[i]); - } - this._outputs.push(new circle.Parameter(argument ? argument.name : '?', true, argument ? [ argument ] : [])); - } - } - - get name() { - return this._name; - } - - get inputs() { - return this._inputs; - } - - get outputs() { - return this._outputs; - } - - get nodes() { - return this._nodes; - } -}; - -circle.Node = class { - - constructor(metadata, node, type, location, args) { - this._location = location; - this._type = type.custom ? { name: type.name, category: 'custom' } : metadata.type(type.name); - this._inputs = []; - this._outputs = []; - this._attributes = []; - if (node) { - let inputs = []; - let outputs = []; - inputs = Array.from(node.inputs || new Int32Array(0)); - outputs = Array.from(node.outputs || new Int32Array(0)); - let inputIndex = 0; - while (inputIndex < inputs.length) { - let count = 1; - let inputName = null; - let inputVisible = true; - const inputArguments = []; - if (this._type && this._type.inputs && inputIndex < this._type.inputs.length) { - const input = this._type.inputs[inputIndex]; - inputName = input.name; - if (input.option == 'variadic') { - count = inputs.length - inputIndex; - } - if (Object.prototype.hasOwnProperty.call(input, 'visible') && !input.visible) { - inputVisible = false; - } - } - const inputArray = inputs.slice(inputIndex, inputIndex + count); - for (const index of inputArray) { - const argument = args(index); - if (argument) { - inputArguments.push(argument); - } - } - inputIndex += count; - inputName = inputName ? inputName : inputIndex.toString(); - this._inputs.push(new circle.Parameter(inputName, inputVisible, inputArguments)); - } - for (let k = 0; k < outputs.length; k++) { - const index = outputs[k]; - const outputArguments = []; - const argument = args(index); - if (argument) { - outputArguments.push(argument); - } - let outputName = k.toString(); - if (this._type && this._type.outputs && k < this._type.outputs.length) { - const output = this._type.outputs[k]; - if (output && output.name) { - outputName = output.name; - } - } - this._outputs.push(new circle.Parameter(outputName, true, outputArguments)); - } - if (type.custom && node.custom_options.length > 0) { - let decoded = false; - if (node.custom_options_format === circle.schema.CustomOptionsFormat.FLEXBUFFERS) { - try { - const reader = flexbuffers.BinaryReader.open(node.custom_options); - if (reader) { - const custom_options = reader.read(); - if (Array.isArray(custom_options)) { - const attribute = new circle.Attribute(null, 'custom_options', custom_options); - this._attributes.push(attribute); - decoded = true; - } - else if (custom_options) { - for (const pair of Object.entries(custom_options)) { - const key = pair[0]; - const value = pair[1]; - const schema = metadata.attribute(type.name, key); - const attribute = new circle.Attribute(schema, key, value); - this._attributes.push(attribute); - } - decoded = true; - } - } - } - catch (err) { - // continue regardless of error - } - } - if (!decoded) { - const schema = metadata.attribute(type.name, 'custom'); - this._attributes.push(new circle.Attribute(schema, 'custom', Array.from(node.custom_options))); - } - } - const options = node.builtin_options; - if (options) { - for (const name of Object.keys(options)) { - const value = options[name]; - if (name === 'fused_activation_function' && value !== 0) { - const activationFunctionMap = { 1: 'Relu', 2: 'ReluN1To1', 3: 'Relu6', 4: 'Tanh', 5: 'SignBit' }; - if (!activationFunctionMap[value]) { - throw new circle.Error("Unsupported activation funtion index '" + JSON.stringify(value) + "'."); - } - const type = activationFunctionMap[value]; - this._chain = [ new circle.Node(metadata, null, { name: type }, null, []) ]; - } - const schema = metadata.attribute(type.name, name); - this._attributes.push(new circle.Attribute(schema, name, value)); - } - } - } - } - - get type() { - return this._type; - } - - get name() { - return ''; - } - - get location() { - return this._location; - } - - get inputs() { - return this._inputs; - } - - get outputs() { - return this._outputs; - } - - get chain() { - return this._chain; - } - - get attributes() { - return this._attributes; - } -}; - -circle.Attribute = class { - - constructor(metadata, name, value) { - this._name = name; - this._value = ArrayBuffer.isView(value) ? Array.from(value) : value; - this._type = metadata && metadata.type ? metadata.type : null; - if (this._name == 'fused_activation_function') { - this._visible = false; - } - if (this._type) { - this._value = circle.Utility.enum(this._type, this._value); - } - if (metadata) { - if (Object.prototype.hasOwnProperty.call(metadata, 'visible') && !metadata.visible) { - this._visible = false; - } - else if (Object.prototype.hasOwnProperty.call(metadata, 'default')) { - value = this._value; - if (typeof value == 'function') { - value = value(); - } - if (value == metadata.default) { - this._visible = false; - } - } - } - } - - get name() { - return this._name; - } - - get type() { - return this._type; - } - - get value() { - return this._value; - } - - get visible() { - return this._visible == false ? false : true; - } -}; - -circle.Parameter = class { - - constructor(name, visible, args) { - this._name = name; - this._visible = visible; - this._arguments = args; - } - - get name() { - return this._name; - } - - get visible() { - return this._visible; - } - - get arguments() { - return this._arguments; - } -}; - -circle.Argument = class { - - constructor(index, tensor, initializer) { - const name = tensor.name || ''; - this._name = name + '\n' + index.toString(); - this._location = index.toString(); - this._type = tensor.type !== undefined && tensor.shape !== undefined ? new circle.TensorType(tensor) : null; - this._initializer = initializer; - const quantization = tensor.quantization; - if (quantization) { - const length = Math.max(quantization.scale.length, quantization.zero_point.length, quantization.min.length, quantization.max.length); - const list = []; - for (let i = 0; i < length; i++) { - let value = 'q'; - const scale = i < quantization.scale.length ? quantization.scale[i] : 0; - const zeroPoint = (i < quantization.zero_point.length ? quantization.zero_point[i] : 0).toString(); - if (scale !== 0 || zeroPoint !== '0') { - value = scale.toString() + ' * ' + (zeroPoint === '0' ? 'q' : ('(q' + (!zeroPoint.startsWith('-') ? ' - ' + zeroPoint : ' + ' + zeroPoint.substring(1)) + ')')); - } - if (i < quantization.min.length) { - value = quantization.min[i].toString() + ' \u2264 ' + value; - } - if (i < quantization.max.length) { - value = value + ' \u2264 ' + quantization.max[i].toString(); - } - list.push(value); - } - if (list.length > 0 && !list.every((value) => value === 'q')) { - this._quantization = list; - } - } - } - - get name() { - return this._name; - } - - get location() { - return this._location; - } - - get type() { - return this._type; - } - - get quantization() { - return this._quantization; - } - - set description(value) { - this._description = value; - } - - get description() { - return this._description; - } - - get initializer() { - return this._initializer; - } -}; - -circle.Tensor = class { - - constructor(index, tensor, buffer, is_variable) { - this._location = index.toString(); - this._type = new circle.TensorType(tensor); - this._is_variable = is_variable; - this._name = tensor.name; - this._data = buffer.data.slice(0); - } - - get kind() { - return this._is_variable ? 'Variable' : ''; - } - - get name() { - return this._name; - } - - get location() { - return this._location; - } - - get type() { - return this._type; - } - - get state() { - return this._context().state; - } - - get value() { - const context = this._context(); - if (context.state) { - return null; - } - context.limit = Number.MAX_SAFE_INTEGER; - return this._decode(context, 0); - } - - toString() { - const context = this._context(); - if (context.state) { - return ''; - } - context.limit = 10000; - const value = this._decode(context, 0); - return JSON.stringify(value, null, 4); - } - - _context() { - const context = {}; - context.state = null; - context.index = 0; - context.count = 0; - - if (this._data == null || this._data.length === 0) { - context.state = 'Tensor data is empty.'; - return context; - } - - const dataType = this.type.dataType; - const shape = this.type.shape.dimensions; - - context.dataType = dataType; - context.shape = shape; - - if (dataType === 'string') { - let offset = 0; - const data = new DataView(this._data.buffer, this._data.byteOffset, this._data.byteLength); - const count = data.getInt32(0, true); - offset += 4; - const offsetTable = []; - for (let j = 0; j < count; j++) { - offsetTable.push(data.getInt32(offset, true)); - offset += 4; - } - offsetTable.push(this._data.length); - const stringTable = []; - const utf8Decoder = new TextDecoder('utf-8'); - for (let k = 0; k < count; k++) { - const textArray = this._data.subarray(offsetTable[k], offsetTable[k + 1]); - stringTable.push(utf8Decoder.decode(textArray)); - } - context.data = stringTable; - } - else { - const itemsize = new Map([ - [ 'boolean' ], - [ 'uint8', 1 ], [ 'uint32', 4], - [ 'int8', 1 ], [ 'int16', 2 ], [ 'int32', 4 ], [ 'int64', 8 ], - [ 'float16', 2 ], [ 'float32', 4 ], [ 'float64', 8 ] - ]); - if (!itemsize.has(dataType)) { - throw new circle.Error("Tensor data type '" + this.type.dataType + "' is not implemented."); - } - const size = shape.reduce((a, b) => a * b, 1); - if (this._data.length < itemsize.get(dataType) * size) { - context.state = "Invalid tensor data size."; - return context; - } - context.data = new DataView(this._data.buffer, this._data.byteOffset, this._data.byteLength); - } - return context; - } - - _decode(context, dimension) { - const shape = (context.shape.length == 0) ? [ 1 ] : context.shape; - const size = shape[dimension]; - const results = []; - if (dimension == shape.length - 1) { - for (let i = 0; i < size; i++) { - if (context.count > context.limit) { - results.push('...'); - return results; - } - switch (context.dataType) { - case 'boolean': - results.push(context.data.getUint8(context.index) === 0 ? false : true); - context.index += 1; - context.count++; - break; - case 'uint8': - results.push(context.data.getUint8(context.index)); - context.index += 1; - context.count++; - break; - case 'uint32': - results.push(context.data.getUint32(context.index)); - context.index += 4; - context.count++; - break; - case 'int8': - results.push(context.data.getInt8(context.index)); - context.index += 1; - context.count++; - break; - case 'int16': - results.push(context.data.getInt16(context.index, true)); - context.index += 2; - context.count++; - break; - case 'int32': - results.push(context.data.getInt32(context.index, true)); - context.index += 4; - context.count++; - break; - case 'int64': - results.push(context.data.getInt64(context.index, true)); - context.index += 8; - context.count++; - break; - case 'float16': - results.push(context.data.getFloat16(context.index, true)); - context.index += 2; - context.count++; - break; - case 'float32': - results.push(context.data.getFloat32(context.index, true)); - context.index += 4; - context.count++; - break; - case 'float64': - results.push(context.data.getFloat64(context.index, true)); - context.index += 8; - context.count++; - break; - case 'string': - results.push(context.data[context.index++]); - context.count++; - break; - default: - break; - } - } - } - else { - for (let j = 0; j < size; j++) { - if (context.count > context.limit) { - results.push('...'); - return results; - } - results.push(this._decode(context, dimension + 1)); - } - } - if (context.shape.length == 0) { - return results[0]; - } - return results; - } -}; - -circle.TensorType = class { - - constructor(tensor) { - this._dataType = circle.Utility.dataType(tensor.type); - this._shape = new circle.TensorShape(Array.from(tensor.shape || [])); - } - - get dataType() { - return this._dataType; - } - - get shape() { - return this._shape; - } - - set denotation(value) { - this._denotation = value; - } - - get denotation() { - return this._denotation; - } - - toString() { - return this.dataType + this._shape.toString(); - } -}; - -circle.TensorShape = class { - - constructor(dimensions) { - this._dimensions = dimensions; - } - - get dimensions() { - return this._dimensions; - } - - toString() { - if (!this._dimensions || this._dimensions.length == 0) { - return ''; - } - return '[' + this._dimensions.map((dimension) => dimension.toString()).join(',') + ']'; - } -}; - -circle.Metadata = class { - - static open(context) { - if (circle.Metadata._metadata) { - return Promise.resolve(circle.Metadata._metadata); - } - return context.request('circle-metadata.json', 'utf-8', null).then((data) => { - circle.Metadata._metadata = new circle.Metadata(data); - return circle.Metadata._metadata; - }).catch(() => { - circle.Metadata._metadata = new circle.Metadata(null); - return circle.Metadata._metadata; - }); - } - - constructor(data) { - this._types = new Map(); - this._attributes = new Map(); - if (data) { - const metadata = JSON.parse(data); - this._types = new Map(metadata.map((item) => [ item.name, item ])); - } - } - - type(name) { - if (!this._types.has(name)) { - this._types.set(name, { name: name }); - } - return this._types.get(name); - } - - attribute(type, name) { - const key = type + ':' + name; - if (!this._attributes.has(key)) { - this._attributes.set(key, null); - const metadata = this.type(type); - if (metadata && Array.isArray(metadata.attributes)) { - for (const attribute of metadata.attributes) { - this._attributes.set(type + ':' + attribute.name, attribute); - } - } - } - return this._attributes.get(key); - } -}; - -circle.Utility = class { - - static dataType(type) { - if (!circle.Utility._tensorTypeMap) { - circle.Utility._tensorTypeMap = new Map(Object.keys(circle.schema.TensorType).map((key) => [ circle.schema.TensorType[key], key.toLowerCase() ])); - circle.Utility._tensorTypeMap.set(6, 'boolean'); - } - return circle.Utility._tensorTypeMap.has(type) ? circle.Utility._tensorTypeMap.get(type) : '?'; - } - - static enum(name, value) { - const type = name && circle.schema ? circle.schema[name] : undefined; - if (type) { - circle.Utility._enums = circle.Utility._enums || new Map(); - if (!circle.Utility._enums.has(name)) { - const map = new Map(Object.keys(type).map((key) => [ type[key], key ])); - circle.Utility._enums.set(name, map); - } - const map = circle.Utility._enums.get(name); - if (map.has(value)) { - return map.get(value); - } - } - return value; - } -}; - -circle.Error = class extends Error { - - constructor(message) { - super(message); - this.name = 'Error loading Circle model.'; - } -}; - -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.ModelFactory = circle.ModelFactory; -} diff --git a/media/CircleEditor/external/dagre.js b/media/CircleEditor/external/dagre.js deleted file mode 100644 index 6a428607..00000000 --- a/media/CircleEditor/external/dagre.js +++ /dev/null @@ -1,2251 +0,0 @@ - -var dagre = dagre || {}; - -// Dagre graph layout -// https://github.com/dagrejs/dagre -// https://github.com/dagrejs/graphlib - -dagre.layout = (graph, options) => { - options = options || {}; - // options.time = true; - const time = (name, callback) => { - const start = Date.now(); - const result = callback(); - const duration = Date.now() - start; - if (options.time) { - /* eslint-disable */ - console.log(name + ': ' + duration + 'ms'); - /* eslint-enable */ - } - return result; - }; - - // Constructs a new graph from the input graph, which can be used for layout. - // This process copies only whitelisted attributes from the input graph to the - // layout graph. Thus this function serves as a good place to determine what - // attributes can influence layout. - const buildLayoutGraph = (graph) => { - const g = new dagre.Graph({ compound: true }); - g.options = Object.assign({}, { ranksep: 50, edgesep: 20, nodesep: 50, rankdir: 'tb' }, graph.options); - for (const node of graph.nodes.values()) { - const v = node.v; - const label = node.label; - g.setNode(v, { - width: label.width || 0, - height: label.height || 0 - }); - g.setParent(v, graph.parent(v)); - } - for (const e of graph.edges.values()) { - const edge = e.label; - g.setEdge(e.v, e.w, { - minlen: edge.minlen || 1, - weight: edge.weight || 1, - width: edge.width || 0, - height: edge.height || 0, - labeloffset: edge.labeloffset || 10, - labelpos: edge.labelpos || 'r' - }); - } - return g; - }; - - const runLayout = (g, time) => { - let uniqueIdCounter = 0; - const uniqueId = (prefix) => { - const id = ++uniqueIdCounter; - return prefix + id; - }; - const flat = (list) => { - if (Array.isArray(list) && list.every((item) => !Array.isArray(item))) { - return list; - } - const target = []; - for (const item of list) { - if (!Array.isArray(item)) { - target.push(item); - continue; - } - for (const entry of item) { - target.push(entry); - } - } - return target; - }; - - // Adds a dummy node to the graph and return v. - const addDummyNode = (g, type, label, name) => { - let v; - do { - v = uniqueId(name); - } while (g.hasNode(v)); - label.dummy = type; - g.setNode(v, label); - return v; - }; - - const asNonCompoundGraph = (g) => { - const graph = new dagre.Graph({}); - graph.options = g.options; - for (const node of g.nodes.values()) { - const v = node.v; - if (g.children(v).length === 0) { - graph.setNode(v, node.label); - } - } - for (const e of g.edges.values()) { - graph.setEdge(e.v, e.w, e.label); - } - return graph; - }; - - const maxRank = (g) => { - let rank = Number.NEGATIVE_INFINITY; - for (const node of g.nodes.values()) { - const x = node.label.rank; - if (x !== undefined && x > rank) { - rank = x; - } - } - return rank === Number.NEGATIVE_INFINITY ? undefined : rank; - }; - - // Given a DAG with each node assigned 'rank' and 'order' properties, this function will produce a matrix with the ids of each node. - const buildLayerMatrix = (g) => { - const rank = maxRank(g); - const length = rank === undefined ? 0 : rank + 1; - const layering = Array.from(new Array(length), () => []); - for (const node of g.nodes.values()) { - const label = node.label; - const rank = label.rank; - if (rank !== undefined) { - layering[rank][label.order] = node.v; - } - } - return layering; - }; - - // This idea comes from the Gansner paper: to account for edge labels in our layout we split each rank in half by doubling minlen and halving ranksep. - // Then we can place labels at these mid-points between nodes. - // We also add some minimal padding to the width to push the label for the edge away from the edge itself a bit. - const makeSpaceForEdgeLabels = (g) => { - const graph = g.options; - graph.ranksep /= 2; - for (const e of g.edges.values()) { - const edge = e.label; - edge.minlen *= 2; - if (edge.labelpos.toLowerCase() !== 'c') { - if (graph.rankdir === 'TB' || graph.rankdir === 'BT') { - edge.width += edge.labeloffset; - } - else { - edge.height += edge.labeloffset; - } - } - } - }; - - const removeSelfEdges = (g) => { - for (const e of g.edges.values()) { - if (e.v === e.w) { - const label = e.vNode.label; - if (!label.selfEdges) { - label.selfEdges = []; - } - label.selfEdges.push({ e: e, label: e.label }); - g.removeEdge(e); - } - } - }; - - const acyclic_run = (g) => { - const fas = []; - const stack = new Set(); - const visited = new Set(); - const dfs = (v) => { - if (!visited.has(v)) { - visited.add(v); - stack.add(v); - for (const e of g.node(v).out) { - if (stack.has(e.w)) { - fas.push(e); - } - else { - dfs(e.w); - } - } - stack.delete(v); - } - }; - for (const v of g.nodes.keys()) { - dfs(v); - } - for (const e of fas) { - const label = e.label; - g.removeEdge(e); - label.forwardName = e.name; - label.reversed = true; - g.setEdge(e.w, e.v, label, uniqueId('rev')); - } - }; - const acyclic_undo = (g) => { - for (const e of g.edges.values()) { - const edge = e.label; - if (edge.reversed) { - edge.points.reverse(); - g.removeEdge(e); - const forwardName = edge.forwardName; - delete edge.reversed; - delete edge.forwardName; - g.setEdge(e.w, e.v, edge, forwardName); - } - } - }; - - // Returns the amount of slack for the given edge. - // The slack is defined as the difference between the length of the edge and its minimum length. - const slack = (g, e) => { - return e.wNode.label.rank - e.vNode.label.rank - e.label.minlen; - }; - - // Assigns a rank to each node in the input graph that respects the 'minlen' constraint specified on edges between nodes. - // This basic structure is derived from Gansner, et al., 'A Technique for Drawing Directed Graphs.' - // - // Pre-conditions: - // 1. Graph must be a connected DAG - // 2. Graph nodes must be objects - // 3. Graph edges must have 'weight' and 'minlen' attributes - // - // Post-conditions: - // 1. Graph nodes will have a 'rank' attribute based on the results of the - // algorithm. Ranks can start at any index (including negative), we'll - // fix them up later. - const rank = (g) => { - // Constructs a spanning tree with tight edges and adjusted the input node's ranks to achieve this. - // A tight edge is one that is has a length that matches its 'minlen' attribute. - // The basic structure for this function is derived from Gansner, et al., 'A Technique for Drawing Directed Graphs.' - // - // Pre-conditions: - // 1. Graph must be a DAG. - // 2. Graph must be connected. - // 3. Graph must have at least one node. - // 5. Graph nodes must have been previously assigned a 'rank' property that respects the 'minlen' property of incident edges. - // 6. Graph edges must have a 'minlen' property. - // - // Post-conditions: - // - Graph nodes will have their rank adjusted to ensure that all edges are tight. - // - // Returns a tree (undirected graph) that is constructed using only 'tight' edges. - const feasibleTree = (g) => { - const t = new dagre.Graph({ directed: false }); - // Choose arbitrary node from which to start our tree - const start = g.nodes.keys().next().value; - const size = g.nodes.size; - t.setNode(start, {}); - // Finds a maximal tree of tight edges and returns the number of nodes in the tree. - const tightTree = (t, g) => { - const stack = Array.from(t.nodes.keys()).reverse(); - while (stack.length > 0) { - const v = stack.pop(); - const node = g.node(v); - for (const e of node.in.concat(node.out)) { - const edgeV = e.v; - const w = (v === edgeV) ? e.w : edgeV; - if (!t.hasNode(w) && !slack(g, e)) { - t.setNode(w, {}); - t.setEdge(v, w, {}); - stack.push(w); - } - } - } - return t.nodes.size; - }; - while (tightTree(t, g) < size) { - // Finds the edge with the smallest slack that is incident on tree and returns it. - let minKey = Number.MAX_SAFE_INTEGER; - let edge = undefined; - for (const e of g.edges.values()) { - if (t.hasNode(e.v) !== t.hasNode(e.w)) { - const key = slack(g, e); - if (key < minKey) { - minKey = key; - edge = e; - } - } - } - const delta = t.hasNode(edge.v) ? slack(g, edge) : -slack(g, edge); - for (const v of t.nodes.keys()) { - g.node(v).label.rank += delta; - } - } - return t; - }; - // Initializes ranks for the input graph using the longest path algorithm. This - // algorithm scales well and is fast in practice, it yields rather poor - // solutions. Nodes are pushed to the lowest layer possible, leaving the bottom - // ranks wide and leaving edges longer than necessary. However, due to its - // speed, this algorithm is good for getting an initial ranking that can be fed - // into other algorithms. - // - // This algorithm does not normalize layers because it will be used by other - // algorithms in most cases. If using this algorithm directly, be sure to - // run normalize at the end. - // - // Pre-conditions: - // 1. Input graph is a DAG. - // 2. Input graph node labels can be assigned properties. - // - // Post-conditions: - // 1. Each node will be assign an (unnormalized) 'rank' property. - const longestPath = (g) => { - const visited = new Set(); - const dfs = (v) => { - const node = g.node(v); - if (visited.has(v)) { - return node.label.rank; - } - visited.add(v); - let rank = Number.MAX_SAFE_INTEGER; - for (const e of node.out) { - rank = Math.min(rank, dfs(e.w) - e.label.minlen); - } - if (rank === Number.MAX_SAFE_INTEGER) { - rank = 0; - } - node.label.rank = rank; - return rank; - }; - for (const node of g.nodes.values()) { - if (node.in.length === 0) { - dfs(node.v); - } - } - }; - // The network simplex algorithm assigns ranks to each node in the input graph - // and iteratively improves the ranking to reduce the length of edges. - // - // Preconditions: - // 1. The input graph must be a DAG. - // 2. All nodes in the graph must have an object value. - // 3. All edges in the graph must have 'minlen' and 'weight' attributes. - // - // Postconditions: - // 1. All nodes in the graph will have an assigned 'rank' attribute that has - // been optimized by the network simplex algorithm. Ranks start at 0. - // - // A rough sketch of the algorithm is as follows: - // 1. Assign initial ranks to each node. We use the longest path algorithm, - // which assigns ranks to the lowest position possible. In general this - // leads to very wide bottom ranks and unnecessarily long edges. - // 2. Construct a feasible tight tree. A tight tree is one such that all - // edges in the tree have no slack (difference between length of edge - // and minlen for the edge). This by itself greatly improves the assigned - // rankings by shorting edges. - // 3. Iteratively find edges that have negative cut values. Generally a - // negative cut value indicates that the edge could be removed and a new - // tree edge could be added to produce a more compact graph. - // - // Much of the algorithms here are derived from Gansner, et al., 'A Technique - // for Drawing Directed Graphs.' The structure of the file roughly follows the - // structure of the overall algorithm. - const networkSimplex = (g) => { - // Returns a new graph with only simple edges. Handles aggregation of data associated with multi-edges. - const simplify = (g) => { - const graph = new dagre.Graph(); - graph.options = g.options; - for (const node of g.nodes.values()) { - graph.setNode(node.v, node.label); - } - for (const e of g.edges.values()) { - const simpleEdge = graph.edge(e.v, e.w); - const simpleLabel = simpleEdge ? simpleEdge.label : { weight: 0, minlen: 1 }; - const label = e.label; - graph.setEdge(e.v, e.w, { - weight: simpleLabel.weight + label.weight, - minlen: Math.max(simpleLabel.minlen, label.minlen) - }); - } - return graph; - }; - const initLowLimValues = (tree, root) => { - const dfs = (tree, visited, nextLim, v, parent) => { - const low = nextLim; - const label = tree.node(v).label; - visited.add(v); - for (const w of tree.neighbors(v)) { - if (!visited.has(w)) { - nextLim = dfs(tree, visited, nextLim, w, v); - } - } - label.low = low; - label.lim = nextLim++; - if (parent) { - label.parent = parent; - } - else { - // TODO should be able to remove this when we incrementally update low lim - delete label.parent; - } - return nextLim; - }; - root = tree.nodes.keys().next().value; - const visited = new Set(); - dfs(tree, visited, 1, root); - }; - // Initializes cut values for all edges in the tree. - const initCutValues = (t, g) => { - const vs = []; - const visited = new Set(); - const stack = [ Array.from(t.nodes.keys()).reverse() ]; - while (stack.length > 0) { - const current = stack[stack.length - 1]; - if (Array.isArray(current)) { - const v = current.pop(); - if (current.length === 0) { - stack.pop(); - } - if (!visited.has(v)) { - visited.add(v); - const children = t.neighbors(v); - if (children.length > 0) { - stack.push(v); - stack.push(children.reverse()); - } - else { - vs.push(v); - } - } - } - else { - vs.push(stack.pop()); - } - } - for (const v of vs.slice(0, vs.length - 1)) { - // Given the tight tree, its graph, and a child in the graph calculate and - // return the cut value for the edge between the child and its parent. - const childLabel = t.node(v).label; - const parent = childLabel.parent; - // The graph's view of the tree edge we're inspecting - const edge = g.edge(v, parent); - // True if the child is on the tail end of the edge in the directed graph - const childIsTail = edge ? true : false; - // The accumulated cut value for the edge between this node and its parent - const graphEdge = edge ? edge.label : g.edge(parent, v).label; - let cutValue = graphEdge.weight; - const node = g.node(v); - for (const e of node.in.concat(node.out)) { - const isOutEdge = e.v === v; - const other = isOutEdge ? e.w : e.v; - if (other !== parent) { - const pointsToHead = isOutEdge === childIsTail; - cutValue += pointsToHead ? e.label.weight : -e.label.weight; - const edge = t.edge(v, other); - if (edge) { - const otherCutValue = edge.label.cutvalue; - cutValue += pointsToHead ? -otherCutValue : otherCutValue; - } - } - } - t.edge(v, parent).label.cutvalue = cutValue; - } - }; - const leaveEdge = (tree) => { - return Array.from(tree.edges.values()).find((e) => e.label.cutvalue < 0); - }; - const enterEdge = (t, g, edge) => { - let v = edge.v; - let w = edge.w; - // For the rest of this function we assume that v is the tail and w is the - // head, so if we don't have this edge in the graph we should flip it to - // match the correct orientation. - if (!g.edge(v, w)) { - v = edge.w; - w = edge.v; - } - const vLabel = t.node(v).label; - const wLabel = t.node(w).label; - let tailLabel = vLabel; - let flip = false; - // If the root is in the tail of the edge then we need to flip the logic that - // checks for the head and tail nodes in the candidates function below. - if (vLabel.lim > wLabel.lim) { - tailLabel = wLabel; - flip = true; - } - // Returns true if the specified node is descendant of the root node per the assigned low and lim attributes in the tree. - const isDescendant = (vLabel, rootLabel) => { - return rootLabel.low <= vLabel.lim && vLabel.lim <= rootLabel.lim; - }; - let minKey = Number.POSITIVE_INFINITY; - let minValue = undefined; - for (const edge of g.edges.values()) { - if (flip === isDescendant(t.node(edge.v).label, tailLabel) && - flip !== isDescendant(t.node(edge.w).label, tailLabel)) { - const key = slack(g, edge); - if (key < minKey) { - minKey = key; - minValue = edge; - } - } - } - return minValue; - }; - const exchangeEdges = (t, g, e, f) => { - t.removeEdge(e); - t.setEdge(f.v, f.w, {}); - initLowLimValues(t); - initCutValues(t, g); - // update ranks - const root = Array.from(t.nodes.keys()).find((v) => !g.node(v).label.parent); - const stack = [ root ]; - const visited = new Set(); - while (stack.length > 0) { - const v = stack.pop(); - if (!visited.has(v)) { - visited.add(v); - for (const w of t.neighbors(v).reverse()) { - stack.push(w); - } - } - } - const vs = Array.from(visited); - for (const v of vs.slice(1)) { - const parent = t.node(v).label.parent; - let edge = g.edge(v, parent); - let flipped = false; - if (!edge) { - edge = g.edge(parent, v); - flipped = true; - } - g.node(v).label.rank = g.node(parent).label.rank + (flipped ? edge.label.minlen : -edge.label.minlen); - } - }; - g = simplify(g); - longestPath(g); - const t = feasibleTree(g); - initLowLimValues(t); - initCutValues(t, g); - let e; - let f; - while ((e = leaveEdge(t))) { - f = enterEdge(t, g, e); - exchangeEdges(t, g, e, f); - } - }; - switch(g.options.ranker) { - case 'tight-tree': - longestPath(g); - feasibleTree(g); - break; - case 'longest-path': - longestPath(g); - break; - default: - networkSimplex(g); - break; - } - }; - - // Creates temporary dummy nodes that capture the rank in which each edge's label is going to, if it has one of non-zero width and height. - // We do this so that we can safely remove empty ranks while preserving balance for the label's position. - const injectEdgeLabelProxies = (g) => { - for (const e of g.edges.values()) { - const edge = e.label; - if (edge.width && edge.height) { - const v = e.vNode.label; - const w = e.wNode.label; - addDummyNode(g, 'edge-proxy', { rank: (w.rank - v.rank) / 2 + v.rank, e: e }, '_ep'); - } - } - }; - - const removeEmptyRanks = (g) => { - // Ranks may not start at 0, so we need to offset them - if (g.nodes.size > 0) { - let minRank = Number.MAX_SAFE_INTEGER; - let maxRank = Number.MIN_SAFE_INTEGER; - const nodes = Array.from(g.nodes.values()); - for (const node of nodes) { - const label = node.label; - if (label.rank !== undefined) { - minRank = Math.min(minRank, label.rank); - maxRank = Math.max(maxRank, label.rank); - } - } - const size = maxRank - minRank; - if (size > 0) { - const layers = new Array(size); - for (const node of nodes) { - const label = node.label; - if (label.rank !== undefined) { - const rank = label.rank - minRank; - if (!layers[rank]) { - layers[rank] = []; - } - layers[rank].push(node.v); - } - } - let delta = 0; - const nodeRankFactor = g.options.nodeRankFactor; - for (let i = 0; i < layers.length; i++) { - const vs = layers[i]; - if (vs === undefined && i % nodeRankFactor !== 0) { - delta--; - } - else if (delta && vs) { - for (const v of vs) { - g.node(v).label.rank += delta; - } - } - } - } - } - }; - - // A nesting graph creates dummy nodes for the tops and bottoms of subgraphs, - // adds appropriate edges to ensure that all cluster nodes are placed between - // these boundries, and ensures that the graph is connected. - // In addition we ensure, through the use of the minlen property, that nodes - // and subgraph border nodes do not end up on the same rank. - // - // Preconditions: - // 1. Input graph is a DAG - // 2. Nodes in the input graph has a minlen attribute - // - // Postconditions: - // 1. Input graph is connected. - // 2. Dummy nodes are added for the tops and bottoms of subgraphs. - // 3. The minlen attribute for nodes is adjusted to ensure nodes do not - // get placed on the same rank as subgraph border nodes. - // - // The nesting graph idea comes from Sander, 'Layout of Compound Directed Graphs.' - const nestingGraph_run = (g) => { - const root = addDummyNode(g, 'root', {}, '_root'); - const treeDepths = (g) => { - const depths = {}; - const dfs = (v, depth) => { - const children = g.children(v); - if (children && children.length > 0) { - for (const child of children) { - dfs(child, depth + 1); - } - } - depths[v] = depth; - }; - for (const v of g.children()) { - dfs(v, 1); - } - return depths; - }; - const dfs = (g, root, nodeSep, weight, height, depths, v) => { - const children = g.children(v); - if (!children.length) { - if (v !== root) { - g.setEdge(root, v, { weight: 0, minlen: nodeSep }); - } - return; - } - const top = addDummyNode(g, 'border', { width: 0, height: 0 }, '_bt'); - const bottom = addDummyNode(g, 'border', { width: 0, height: 0 }, '_bb'); - const label = g.node(v).label; - g.setParent(top, v); - label.borderTop = top; - g.setParent(bottom, v); - label.borderBottom = bottom; - for (const child of children) { - dfs(g, root, nodeSep, weight, height, depths, child); - const childNode = g.node(child).label; - const childTop = childNode.borderTop ? childNode.borderTop : child; - const childBottom = childNode.borderBottom ? childNode.borderBottom : child; - const thisWeight = childNode.borderTop ? weight : 2 * weight; - const minlen = childTop !== childBottom ? 1 : height - depths[v] + 1; - g.setEdge(top, childTop, { weight: thisWeight, minlen: minlen, nestingEdge: true }); - g.setEdge(childBottom, bottom, { weight: thisWeight, minlen: minlen, nestingEdge: true }); - } - if (!g.parent(v)) { - g.setEdge(root, top, { weight: 0, minlen: height + depths[v] }); - } - }; - const depths = treeDepths(g); - const height = Math.max(...Object.values(depths)) - 1; // Note: depths is an Object not an array - const nodeSep = 2 * height + 1; - g.options.nestingRoot = root; - // Multiply minlen by nodeSep to align nodes on non-border ranks. - for (const e of g.edges.values()) { - e.label.minlen *= nodeSep; - } - // Calculate a weight that is sufficient to keep subgraphs vertically compact - const weight = Array.from(g.edges.values()).reduce((acc, e) => acc + e.label.weight, 0) + 1; - // Create border nodes and link them up - for (const child of g.children()) { - dfs(g, root, nodeSep, weight, height, depths, child); - } - // Save the multiplier for node layers for later removal of empty border layers. - g.options.nodeRankFactor = nodeSep; - }; - const nestingGraph_cleanup = (g) => { - const graphLabel = g.options; - g.removeNode(graphLabel.nestingRoot); - delete graphLabel.nestingRoot; - for (const e of g.edges.values()) { - if (e.label.nestingEdge) { - g.removeEdge(e); - } - } - }; - - const assignRankMinMax = (g) => { - // Adjusts the ranks for all nodes in the graph such that all nodes v have rank(v) >= 0 and at least one node w has rank(w) = 0. - let min = Number.POSITIVE_INFINITY; - for (const node of g.nodes.values()) { - const rank = node.label.rank; - if (rank !== undefined && rank < min) { - min = rank; - } - } - for (const node of g.nodes.values()) { - const label = node.label; - if (label.rank !== undefined) { - label.rank -= min; - } - } - let maxRank = 0; - for (const node of g.nodes.values()) { - const label = node.label; - if (label.borderTop) { - label.minRank = g.node(label.borderTop).label.rank; - label.maxRank = g.node(label.borderBottom).label.rank; - maxRank = Math.max(maxRank, label.maxRank); - } - } - g.options.maxRank = maxRank; - }; - - // Breaks any long edges in the graph into short segments that span 1 layer each. - // This operation is undoable with the denormalize function. - // - // Pre-conditions: - // 1. The input graph is a DAG. - // 2. Each node in the graph has a 'rank' property. - // - // Post-condition: - // 1. All edges in the graph have a length of 1. - // 2. Dummy nodes are added where edges have been split into segments. - // 3. The graph is augmented with a 'dummyChains' attribute which contains - // the first dummy in each chain of dummy nodes produced. - const normalize = (g) => { - g.options.dummyChains = []; - for (const e of g.edges.values()) { - let v = e.v; - const w = e.w; - const name = e.name; - const edgeLabel = e.label; - const labelRank = edgeLabel.labelRank; - let vRank = g.node(v).label.rank; - const wRank = g.node(w).label.rank; - if (wRank !== vRank + 1) { - g.removeEdge(e); - let first = true; - vRank++; - while (vRank < wRank) { - edgeLabel.points = []; - delete e.key; - const attrs = { - width: 0, height: 0, - edgeLabel: edgeLabel, - edgeObj: e, - rank: vRank - }; - const dummy = addDummyNode(g, 'edge', attrs, '_d'); - if (vRank === labelRank) { - attrs.width = edgeLabel.width; - attrs.height = edgeLabel.height; - attrs.dummy = 'edge-label'; - attrs.labelpos = edgeLabel.labelpos; - } - g.setEdge(v, dummy, { weight: edgeLabel.weight }, name); - if (first) { - g.options.dummyChains.push(dummy); - first = false; - } - v = dummy; - vRank++; - } - g.setEdge(v, w, { weight: edgeLabel.weight }, name); - } - } - }; - - const denormalize = (g) => { - for (let v of g.options.dummyChains) { - let label = g.node(v).label; - const edgeLabel = label.edgeLabel; - const e = label.edgeObj; - g.setEdge(e.v, e.w, edgeLabel, e.name); - while (label.dummy) { - const w = g.successors(v)[0]; - g.removeNode(v); - edgeLabel.points.push({ x: label.x, y: label.y }); - if (label.dummy === 'edge-label') { - edgeLabel.x = label.x; - edgeLabel.y = label.y; - edgeLabel.width = label.width; - edgeLabel.height = label.height; - } - v = w; - label = g.node(v).label; - } - } - }; - - const removeEdgeLabelProxies = (g) => { - for (const node of g.nodes.values()) { - const label = node.label; - if (label.dummy === 'edge-proxy') { - label.e.label.labelRank = label.rank; - g.removeNode(node.v); - } - } - }; - - const parentDummyChains = (g) => { - // Find a path from v to w through the lowest common ancestor (LCA). Return the full path and the LCA. - const findPath = (g, postorderNums, v, w) => { - const vPath = []; - const wPath = []; - const low = Math.min(postorderNums[v].low, postorderNums[w].low); - const lim = Math.max(postorderNums[v].lim, postorderNums[w].lim); - // Traverse up from v to find the LCA - let parent = v; - do { - parent = g.parent(parent); - vPath.push(parent); - } - while (parent && (postorderNums[parent].low > low || lim > postorderNums[parent].lim)); - const lca = parent; - // Traverse from w to LCA - parent = w; - while ((parent = g.parent(parent)) !== lca) { - wPath.push(parent); - } - return { path: vPath.concat(wPath.reverse()), lca: lca }; - }; - const postorder = (g) => { - const result = {}; - let lim = 0; - const dfs = (v) => { - const low = lim; - for (const u of g.children(v)) { - dfs(u); - } - result[v] = { low: low, lim: lim++ }; - }; - for (const v of g.children()) { - dfs(v); - } - return result; - }; - const postorderNums = postorder(g); - for (let v of g.options.dummyChains || []) { - const node = g.node(v).label; - const edgeObj = node.edgeObj; - const pathData = findPath(g, postorderNums, edgeObj.v, edgeObj.w); - const path = pathData.path; - const lca = pathData.lca; - let pathIdx = 0; - let pathV = path[pathIdx]; - let ascending = true; - while (v !== edgeObj.w) { - const node = g.node(v).label; - if (ascending) { - while ((pathV = path[pathIdx]) !== lca && g.node(pathV).label.maxRank < node.rank) { - pathIdx++; - } - if (pathV === lca) { - ascending = false; - } - } - if (!ascending) { - while (pathIdx < path.length - 1 && g.node(pathV = path[pathIdx + 1]).label.minRank <= node.rank) { - pathIdx++; - } - pathV = path[pathIdx]; - } - g.setParent(v, pathV); - v = g.successors(v)[0]; - } - } - }; - - const addBorderSegments = (g) => { - const addBorderNode = (g, prop, prefix, sg, sgNode, rank) => { - const label = { width: 0, height: 0, rank: rank, borderType: prop }; - const prev = sgNode[prop][rank - 1]; - const curr = addDummyNode(g, 'border', label, prefix); - sgNode[prop][rank] = curr; - g.setParent(curr, sg); - if (prev) { - g.setEdge(prev, curr, { weight: 1 }); - } - }; - const queue = g.children(); - while (queue.length > 0) { - const v = queue.shift(); - const node = g.node(v).label; - if ('minRank' in node) { - node.borderLeft = []; - node.borderRight = []; - const maxRank = node.maxRank + 1; - for (let rank = node.minRank; rank < maxRank; rank++) { - addBorderNode(g, 'borderLeft', '_bl', v, node, rank); - addBorderNode(g, 'borderRight', '_br', v, node, rank); - } - } - const children = g.children(v); - if (children.length) { - for (const v of children) { - queue.push(v); - } - } - } - }; - - // Applies heuristics to minimize edge crossings in the graph and sets the best order solution as an order attribute on each node. - // - // Pre-conditions: - // 1. Graph must be DAG - // 2. Graph nodes must have the 'rank' attribute - // 3. Graph edges must have the 'weight' attribute - // - // Post-conditions: - // 1. Graph nodes will have an 'order' attribute based on the results of the algorithm. - const order = (g) => { - const sortSubgraph = (g, v, cg, biasRight) => { - // Given a list of entries of the form {v, barycenter, weight} and a constraint graph this function will resolve any conflicts between the constraint graph and the barycenters for the entries. - // If the barycenters for an entry would violate a constraint in the constraint graph then we coalesce the nodes in the conflict into a new node that respects the contraint and aggregates barycenter and weight information. - // This implementation is based on the description in Forster, 'A Fast and Simple Hueristic for Constrained Two-Level Crossing Reduction,' thought it differs in some specific details. - // - // Pre-conditions: - // 1. Each entry has the form {v, barycenter, weight}, or if the node has no barycenter, then {v}. - // - // Returns: - // A new list of entries of the form {vs, i, barycenter, weight}. - // The list `vs` may either be a singleton or it may be an aggregation of nodes ordered such that they do not violate constraints from the constraint graph. - // The property `i` is the lowest original index of any of the elements in `vs`. - const resolveConflicts = (entries, cg) => { - const mappedEntries = new Map(); - for (let i = 0; i < entries.length; i++) { - const entry = entries[i]; - const tmp = { indegree: 0, 'in': [], out: [], vs: [ entry.v ], i: i }; - if (entry.barycenter !== undefined) { - tmp.barycenter = entry.barycenter; - tmp.weight = entry.weight; - } - mappedEntries.set(entry.v, tmp); - } - for (const e of cg.edges.values()) { - const entryV = mappedEntries.get(e.v); - const entryW = mappedEntries.get(e.w); - if (entryV && entryW) { - entryW.indegree++; - entryV.out.push(entryW); - } - } - const sourceSet = Array.from(mappedEntries.values()).filter((entry) => !entry.indegree); - const results = []; - function handleIn(vEntry) { - return function(uEntry) { - if (uEntry.merged) { - return; - } - if (uEntry.barycenter === undefined || vEntry.barycenter === undefined || uEntry.barycenter >= vEntry.barycenter) { - let sum = 0; - let weight = 0; - if (vEntry.weight) { - sum += vEntry.barycenter * vEntry.weight; - weight += vEntry.weight; - } - if (uEntry.weight) { - sum += uEntry.barycenter * uEntry.weight; - weight += uEntry.weight; - } - vEntry.vs = uEntry.vs.concat(vEntry.vs); - vEntry.barycenter = sum / weight; - vEntry.weight = weight; - vEntry.i = Math.min(uEntry.i, vEntry.i); - uEntry.merged = true; - } - }; - } - function handleOut(vEntry) { - return function(wEntry) { - wEntry.in.push(vEntry); - if (--wEntry.indegree === 0) { - sourceSet.push(wEntry); - } - }; - } - while (sourceSet.length) { - const entry = sourceSet.pop(); - results.push(entry); - entry.in.reverse().forEach(handleIn(entry)); - entry.out.forEach(handleOut(entry)); - } - return results.filter((entry) => !entry.merged).map((entry) => { - const value = { - vs: entry.vs, - i: entry.i - }; - if (entry.barycenter !== undefined) { - value.barycenter = entry.barycenter; - } - if (entry.weight !== undefined) { - value.weight = entry.weight; - } - return value; - }); - }; - const barycenter = (g, movable) => { - return (movable || []).map((v) => { - const inV = g.node(v).in; - if (!inV.length) { - return { v: v }; - } - else { - const result = inV.reduce((acc, e) => { - const edge = e.label; - const nodeU = e.vNode.label; - return { - sum: acc.sum + (edge.weight * nodeU.order), - weight: acc.weight + edge.weight - }; - }, { sum: 0, weight: 0 }); - return { - v: v, - barycenter: result.sum / result.weight, - weight: result.weight - }; - } - }); - }; - const sort = (entries, biasRight) => { - const consumeUnsortable = (vs, unsortable, index) => { - let last; - while (unsortable.length && (last = unsortable[unsortable.length - 1]).i <= index) { - unsortable.pop(); - vs.push(last.vs); - index++; - } - return index; - }; - const compareWithBias = (bias) => { - return function(entryV, entryW) { - if (entryV.barycenter < entryW.barycenter) { - return -1; - } - else if (entryV.barycenter > entryW.barycenter) { - return 1; - } - return !bias ? entryV.i - entryW.i : entryW.i - entryV.i; - }; - }; - // partition - const parts = { lhs: [], rhs: [] }; - for (const value of entries) { - if ('barycenter' in value) { - parts.lhs.push(value); - } - else { - parts.rhs.push(value); - } - } - const sortable = parts.lhs; - const unsortable = parts.rhs.sort((a, b) => -a.i + b.i); - const vs = []; - let sum = 0; - let weight = 0; - let vsIndex = 0; - sortable.sort(compareWithBias(!!biasRight)); - vsIndex = consumeUnsortable(vs, unsortable, vsIndex); - for (const entry of sortable) { - vsIndex += entry.vs.length; - vs.push(entry.vs); - sum += entry.barycenter * entry.weight; - weight += entry.weight; - vsIndex = consumeUnsortable(vs, unsortable, vsIndex); - } - const result = { vs: flat(vs) }; - if (weight) { - result.barycenter = sum / weight; - result.weight = weight; - } - return result; - }; - const node = g.node(v); - const bl = node && node.label ? node.label.borderLeft : undefined; - const br = node && node.label ? node.label.borderRight: undefined; - const subgraphs = {}; - const movable = bl ? g.children(v).filter((w) => w !== bl && w !== br) : g.children(v); - const barycenters = barycenter(g, movable); - for (const entry of barycenters) { - if (g.children(entry.v).length) { - const result = sortSubgraph(g, entry.v, cg, biasRight); - subgraphs[entry.v] = result; - if ('barycenter' in result) { - if (entry.barycenter !== undefined) { - entry.barycenter = (entry.barycenter * entry.weight + result.barycenter * result.weight) / (entry.weight + result.weight); - entry.weight += result.weight; - } - else { - entry.barycenter = result.barycenter; - entry.weight = result.weight; - } - } - } - } - const entries = resolveConflicts(barycenters, cg); - // expand subgraphs - for (const entry of entries) { - entry.vs = flat(entry.vs.map((v) => subgraphs[v] ? subgraphs[v].vs : v)); - } - const result = sort(entries, biasRight); - if (bl) { - result.vs = flat([bl, result.vs, br]); - if (g.predecessors(bl).length) { - const blPred = g.node(g.predecessors(bl)[0]).label; - const brPred = g.node(g.predecessors(br)[0]).label; - if (!('barycenter' in result)) { - result.barycenter = 0; - result.weight = 0; - } - result.barycenter = (result.barycenter * result.weight + blPred.order + brPred.order) / (result.weight + 2); - result.weight += 2; - } - } - return result; - }; - const sweepLayerGraphs = (layerGraphs, biasRight) => { - const cg = new dagre.Graph(); - for (const lg of layerGraphs) { - const root = lg.options.root; - const sorted = sortSubgraph(lg, root, cg, biasRight); - const vs = sorted.vs; - const length = vs.length; - for (let i = 0; i < length; i++) { - lg.node(vs[i]).label.order = i; - } - // add subgraph constraints - const prev = {}; - let rootPrev; - let exit = false; - for (const v of vs) { - let child = lg.parent(v); - let prevChild; - while (child) { - const parent = lg.parent(child); - if (parent) { - prevChild = prev[parent]; - prev[parent] = child; - } - else { - prevChild = rootPrev; - rootPrev = child; - } - if (prevChild && prevChild !== child) { - cg.setEdge(prevChild, child, null); - exit = true; - break; - } - child = parent; - } - if (exit) { - break; - } - } - } - }; - // A function that takes a layering (an array of layers, each with an array of - // ordererd nodes) and a graph and returns a weighted crossing count. - // - // Pre-conditions: - // 1. Input graph must be simple (not a multigraph), directed, and include - // only simple edges. - // 2. Edges in the input graph must have assigned weights. - // - // Post-conditions: - // 1. The graph and layering matrix are left unchanged. - // - // This algorithm is derived from Barth, et al., 'Bilayer Cross Counting.' - const crossCount = (g, layering) => { - let count = 0; - for (let i = 1; i < layering.length; i++) { - const northLayer = layering[i - 1]; - const southLayer = layering[i]; - // Sort all of the edges between the north and south layers by their position in the north layer and then the south. - // Map these edges to the position of their head in the south layer. - const southPos = {}; - for (let i = 0; i < southLayer.length; i++) { - southPos[southLayer[i]] = i; - } - const southEntries = []; - for (const v of northLayer) { - const entries = []; - for (const e of g.node(v).out) { - entries.push({ - pos: southPos[e.w], - weight: e.label.weight - }); - } - entries.sort((a, b) => a.pos - b.pos); - for (const entry of entries) { - southEntries.push(entry); - } - } - // Build the accumulator tree - let firstIndex = 1; - while (firstIndex < southLayer.length) { - firstIndex <<= 1; - } - const treeSize = 2 * firstIndex - 1; - firstIndex -= 1; - const tree = Array.from(new Array(treeSize), () => 0); - // Calculate the weighted crossings - for (const entry of southEntries) { - let index = entry.pos + firstIndex; - tree[index] += entry.weight; - let weightSum = 0; - while (index > 0) { - if (index % 2) { - weightSum += tree[index + 1]; - } - index = (index - 1) >> 1; - tree[index] += entry.weight; - } - count += entry.weight * weightSum; - } - } - return count; - }; - // Assigns an initial order value for each node by performing a DFS search - // starting from nodes in the first rank. Nodes are assigned an order in their - // rank as they are first visited. - // - // This approach comes from Gansner, et al., 'A Technique for Drawing Directed - // Graphs.' - // - // Returns a layering matrix with an array per layer and each layer sorted by - // the order of its nodes. - const initOrder = (g) => { - const visited = new Set(); - const nodes = Array.from(g.nodes.keys()).filter((v) => !g.children(v).length); - let maxRank = undefined; - for (const v of nodes) { - if (!g.children(v).length > 0) { - const rank = g.node(v).label.rank; - if (maxRank === undefined || (rank !== undefined && rank > maxRank)) { - maxRank = rank; - } - } - } - if (maxRank !== undefined) { - const layers = Array.from(new Array(maxRank + 1), () => []); - for (const v of nodes.map((v) => [ g.node(v).label.rank, v ]).sort((a, b) => a[0] - b[0]).map((item) => item[1])) { - const queue = [ v ]; - while (queue.length > 0) { - const v = queue.shift(); - if (!visited.has(v)) { - visited.add(v); - const rank = g.node(v).label.rank; - layers[rank].push(v); - queue.push(...g.successors(v)); - } - } - } - return layers; - } - return []; - }; - // Constructs a graph that can be used to sort a layer of nodes. - // The graph will contain all base and subgraph nodes from the request layer in their original - // hierarchy and any edges that are incident on these nodes and are of the type requested by the 'relationship' parameter. - // - // Nodes from the requested rank that do not have parents are assigned a root node in the output graph, - // which is set in the root graph attribute. - // This makes it easy to walk the hierarchy of movable nodes during ordering. - // - // Pre-conditions: - // 1. Input graph is a DAG - // 2. Base nodes in the input graph have a rank attribute - // 3. Subgraph nodes in the input graph has minRank and maxRank attributes - // 4. Edges have an assigned weight - // - // Post-conditions: - // 1. Output graph has all nodes in the movable rank with preserved hierarchy. - // 2. Root nodes in the movable layer are made children of the node - // indicated by the root attribute of the graph. - // 3. Non-movable nodes incident on movable nodes, selected by the - // relationship parameter, are included in the graph (without hierarchy). - // 4. Edges incident on movable nodes, selected by the relationship parameter, are added to the output graph. - // 5. The weights for copied edges are aggregated as need, since the output graph is not a multi-graph. - const buildLayerGraph = (g, nodes, rank, relationship) => { - let root; - while (g.hasNode((root = uniqueId('_root')))) { - // continue - } - const graph = new dagre.Graph({ compound: true }); - graph.options = { root: root }; - graph.setDefaultNodeLabel((v) => { const node = g.node(v); return node ? node.label : undefined; }); - const length = nodes.length; - let i = 0; - while (i < length) { - const node = nodes[i++]; - const label = node.label; - if (label.rank === rank || 'minRank' in label && 'maxRank' in label && label.minRank <= rank && rank <= label.maxRank) { - const v = node.v; - graph.setNode(v); - const parent = g.parent(v); - graph.setParent(v, parent || root); - // This assumes we have only short edges! - if (relationship) { - for (const e of node.in) { - graph.setEdge(e.v, v, { weight: e.label.weight }); - } - } - else { - for (const e of node.out) { - graph.setEdge(e.w, v, { weight: e.label.weight }); - } - } - if ('minRank' in label) { - graph.setNode(v, { - borderLeft: label.borderLeft[rank], - borderRight: label.borderRight[rank] - }); - } - } - } - return graph; - }; - let layering = initOrder(g); - const assignOrder = (g, layering) => { - for (const layer of layering) { - for (let i = 0; i < layer.length; i++) { - g.node(layer[i]).label.order = i; - } - } - }; - assignOrder(g, layering); - const rank = maxRank(g) || 0; - const downLayerGraphs = new Array(rank); - const upLayerGraphs = new Array(rank); - const nodes = Array.from(g.nodes.values()); - for (let i = 0; i < rank; i++) { - downLayerGraphs[i] = buildLayerGraph(g, nodes, i + 1, true); - upLayerGraphs[i] = buildLayerGraph(g, nodes, rank - i - 1, false); - } - let bestCC = Number.POSITIVE_INFINITY; - let best; - for (let i = 0, lastBest = 0; lastBest < 4; ++i, ++lastBest) { - sweepLayerGraphs(i % 2 ? downLayerGraphs : upLayerGraphs, i % 4 >= 2); - layering = buildLayerMatrix(g); - const cc = crossCount(g, layering); - if (cc < bestCC) { - lastBest = 0; - const length = layering.length; - best = new Array(length); - for (let j = 0; j < length; j++) { - best[j] = layering[j].slice(); - } - bestCC = cc; - } - } - assignOrder(g, best); - }; - - const insertSelfEdges = (g) => { - const layers = buildLayerMatrix(g); - for (const layer of layers) { - let orderShift = 0; - layer.forEach(function(v, i) { - const label = g.node(v).label; - label.order = i + orderShift; - if (label.selfEdges) { - for (const selfEdge of label.selfEdges) { - addDummyNode(g, 'selfedge', { - width: selfEdge.label.width, - height: selfEdge.label.height, - rank: label.rank, - order: i + (++orderShift), - e: selfEdge.e, - label: selfEdge.label - }, '_se'); - } - delete label.selfEdges; - } - }); - } - }; - - const coordinateSystem_swapWidthHeight = (g) => { - for (const node of g.nodes.values()) { - const label = node.label; - const w = label.width; - label.width = label.height; - label.height = w; - } - for (const e of g.edges.values()) { - const label = e.label; - const w = label.width; - label.width = label.height; - label.height = w; - } - }; - const coordinateSystem_adjust = (g) => { - const rankDir = g.options.rankdir.toLowerCase(); - if (rankDir === 'lr' || rankDir === 'rl') { - coordinateSystem_swapWidthHeight(g); - } - }; - const coordinateSystem_undo = (g) => { - const rankDir = g.options.rankdir.toLowerCase(); - if (rankDir === 'bt' || rankDir === 'rl') { - for (const node of g.nodes.values()) { - node.label.y = -node.label.y; - } - for (const e of g.edges.values()) { - const edge = e.label; - for (const attr of edge.points) { - attr.y = -attr.y; - } - if ('y' in edge) { - edge.y = -edge.y; - } - } - } - if (rankDir === 'lr' || rankDir === 'rl') { - const swapXYOne = (attrs) => { - const x = attrs.x; - attrs.x = attrs.y; - attrs.y = x; - }; - for (const node of g.nodes.values()) { - swapXYOne(node.label); - } - for (const e of g.edges.values()) { - const edge = e.label; - for (const e of edge.points) { - swapXYOne(e); - } - if (edge.x !== undefined) { - swapXYOne(edge); - } - } - coordinateSystem_swapWidthHeight(g); - } - }; - - const position = (g) => { - const addConflict = (conflicts, v, w) => { - if (v > w) { - const tmp = v; - v = w; - w = tmp; - } - let conflictsV = conflicts[v]; - if (!conflictsV) { - conflicts[v] = conflictsV = {}; - } - conflictsV[w] = true; - }; - const hasConflict = (conflicts, v, w) => { - if (v > w) { - const tmp = v; - v = w; - w = tmp; - } - return conflicts[v] && w in conflicts[v]; - }; - const buildBlockGraph = (g, layering, root, reverseSep) => { - const nodeSep = g.options.nodesep; - const edgeSep = g.options.edgesep; - const blockGraph = new dagre.Graph(); - for (const layer of layering) { - let u; - for (const v of layer) { - const vRoot = root[v]; - blockGraph.setNode(vRoot, {}); - if (u) { - const uRoot = root[u]; - const vLabel = g.node(v).label; - const wLabel = g.node(u).label; - let sum = 0; - let delta; - sum += vLabel.width / 2; - if ('labelpos' in vLabel) { - switch (vLabel.labelpos) { - case 'l': delta = -vLabel.width / 2; break; - case 'r': delta = vLabel.width / 2; break; - default: throw new dagre.Error("Unsupported label position '" + vLabel.labelpos + "'."); - } - } - if (delta) { - sum += reverseSep ? delta : -delta; - } - delta = 0; - sum += (vLabel.dummy ? edgeSep : nodeSep) / 2; - sum += (wLabel.dummy ? edgeSep : nodeSep) / 2; - sum += wLabel.width / 2; - if ('labelpos' in wLabel) { - switch (wLabel.labelpos) { - case 'l': delta = wLabel.width / 2; break; - case 'r': delta = -wLabel.width / 2; break; - default: throw new dagre.Error("Unsupported label position '" + wLabel.labelpos + "'."); - } - } - if (delta) { - sum += reverseSep ? delta : -delta; - } - const edge = blockGraph.edge(uRoot, vRoot); - const max = Math.max(sum, edge ? edge.label : 0); - if (edge) { - edge.label = max; - } - else { - blockGraph.setEdge(uRoot, vRoot, max); - } - } - u = v; - } - } - return blockGraph; - }; - // Try to align nodes into vertical 'blocks' where possible. - // This algorithm attempts to align a node with one of its median neighbors. - // If the edge connecting a neighbor is a type-1 conflict then we ignore that possibility. - // If a previous node has already formed a block with a node after the node we're trying to form a block with, - // we also ignore that possibility - our blocks would be split in that scenario. - const verticalAlignment = (layering, conflicts, neighborFn) => { - const root = {}; - const align = {}; - const pos = {}; - // We cache the position here based on the layering because the graph and layering may be out of sync. - // The layering matrix is manipulated to generate different extreme alignments. - for (const layer of layering) { - let order = 0; - for (const v of layer) { - root[v] = v; - align[v] = v; - pos[v] = order; - order++; - } - } - for (const layer of layering) { - let prevIdx = -1; - for (const v of layer) { - let ws = neighborFn(v); - if (ws.length > 0) { - ws = ws.sort((a, b) => pos[a] - pos[b]); - const mp = (ws.length - 1) / 2.0; - const il = Math.ceil(mp); - for (let i = Math.floor(mp); i <= il; i++) { - const w = ws[i]; - if (align[v] === v && prevIdx < pos[w] && !hasConflict(conflicts, v, w)) { - align[w] = v; - align[v] = root[v] = root[w]; - prevIdx = pos[w]; - } - } - } - } - } - return { root: root, align: align }; - }; - const horizontalCompaction = (g, layering, root, align, reverseSep) => { - // This portion of the algorithm differs from BK due to a number of problems. - // Instead of their algorithm we construct a new block graph and do two sweeps. - const xs = {}; - const blockG = buildBlockGraph(g, layering, root, reverseSep); - const borderType = reverseSep ? 'borderLeft' : 'borderRight'; - const iterate = (setXsFunc, nextNodesFunc) => { - let stack = Array.from(blockG.nodes.keys()); - const visited = new Set(); - while (stack.length > 0) { - const v = stack.pop(); - if (visited.has(v)) { - setXsFunc(v); - } - else { - visited.add(v); - stack.push(v); - stack = stack.concat(nextNodesFunc(v)); - } - } - }; - // First pass, places blocks with the smallest possible coordinates. - const pass1 = (v) => { - let max = 0; - for (const e of blockG.node(v).in) { - max = Math.max(max, xs[e.v] + e.label); - } - xs[v] = max; - }; - // Second pass, removes unused space by moving blocks to the greatest coordinates without violating separation. - const pass2 = (v) => { - let min = Number.POSITIVE_INFINITY; - for (const e of blockG.node(v).out) { - min = Math.min(min, xs[e.w] - e.label); - } - const label = g.node(v).label; - if (min !== Number.POSITIVE_INFINITY && label.borderType !== borderType) { - xs[v] = Math.max(xs[v], min); - } - }; - iterate(pass1, blockG.predecessors.bind(blockG)); - iterate(pass2, blockG.successors.bind(blockG)); - // Assign x coordinates to all nodes - for (const v of Object.values(align)) { - xs[v] = xs[root[v]]; - } - return xs; - }; - // Marks all edges in the graph with a type-1 conflict with the 'type1Conflict' property. - // A type-1 conflict is one where a non-inner segment crosses an inner segment. - // An inner segment is an edge with both incident nodes marked with the 'dummy' property. - // - // This algorithm scans layer by layer, starting with the second, for type-1 - // conflicts between the current layer and the previous layer. For each layer - // it scans the nodes from left to right until it reaches one that is incident - // on an inner segment. It then scans predecessors to determine if they have - // edges that cross that inner segment. At the end a final scan is done for all - // nodes on the current rank to see if they cross the last visited inner segment. - // - // This algorithm (safely) assumes that a dummy node will only be incident on a - // single node in the layers being scanned. - const findType1Conflicts = (g, layering) => { - const conflicts = {}; - if (layering.length > 0) { - let prev = layering[0]; - for (let k = 1; k < layering.length; k++) { - const layer = layering[k]; - // last visited node in the previous layer that is incident on an inner segment. - let k0 = 0; - // Tracks the last node in this layer scanned for crossings with a type-1 segment. - let scanPos = 0; - const prevLayerLength = prev.length; - const lastNode = layer[layer.length - 1]; - for (let i = 0; i < layer.length; i++) { - const v = layer[i]; - const w = g.node(v).label.dummy ? g.predecessors(v).find((u) => g.node(u).label.dummy) : null; - if (w || v === lastNode) { - const k1 = w ? g.node(w).label.order : prevLayerLength; - for (const scanNode of layer.slice(scanPos, i + 1)) { - // for (const scanNode of layer.slice(scanPos, scanPos + 1)) { - for (const u of g.predecessors(scanNode)) { - const uLabel = g.node(u).label; - const uPos = uLabel.order; - if ((uPos < k0 || k1 < uPos) && !(uLabel.dummy && g.node(scanNode).label.dummy)) { - addConflict(conflicts, u, scanNode); - } - } - } - // scanPos += 1; - scanPos = i + 1; - k0 = k1; - } - } - prev = layer; - } - } - return conflicts; - }; - const findType2Conflicts = (g, layering) => { - const conflicts = {}; - const scan = (south, southPos, southEnd, prevNorthBorder, nextNorthBorder) => { - let v; - for (let i = southPos; i < southEnd; i++) { - v = south[i]; - if (g.node(v).labeldummy) { - for (const u of g.predecessors(v)) { - const uNode = g.node(u).label; - if (uNode.dummy && (uNode.order < prevNorthBorder || uNode.order > nextNorthBorder)) { - addConflict(conflicts, u, v); - } - } - } - } - }; - if (layering.length > 0) { - let north = layering[0]; - for (let i = 1; i < layering.length; i++) { - const south = layering[i]; - let prevNorthPos = -1; - let nextNorthPos; - let southPos = 0; - south.forEach(function(v, southLookahead) { - if (g.node(v).label.dummy === 'border') { - const predecessors = g.predecessors(v); - if (predecessors.length) { - nextNorthPos = g.node(predecessors[0]).label.order; - scan(south, southPos, southLookahead, prevNorthPos, nextNorthPos); - southPos = southLookahead; - prevNorthPos = nextNorthPos; - } - } - scan(south, southPos, south.length, nextNorthPos, north.length); - }); - north = south; - } - } - return conflicts; - }; - - g = asNonCompoundGraph(g); - const layering = buildLayerMatrix(g); - const ranksep = g.options.ranksep; - // Assign y-coordinate based on rank - let y = 0; - for (const layer of layering) { - const maxHeight = layer.reduce((a, v) => Math.max(a, g.node(v).label.height), 0); - for (const v of layer) { - g.node(v).label.y = y + maxHeight / 2; - } - y += maxHeight + ranksep; - } - // Coordinate assignment based on Brandes and Köpf, 'Fast and Simple Horizontal Coordinate Assignment.' - const conflicts = Object.assign(findType1Conflicts(g, layering), findType2Conflicts(g, layering)); - const xss = {}; - for (const vertical of ['u', 'd']) { - let adjustedLayering = vertical === 'u' ? layering : Object.values(layering).reverse(); - for (const horizontal of ['l', 'r']) { - if (horizontal === 'r') { - adjustedLayering = adjustedLayering.map((layer) => Object.values(layer).reverse()); - } - const neighborFn = (vertical === 'u' ? g.predecessors : g.successors).bind(g); - const align = verticalAlignment(adjustedLayering, conflicts, neighborFn); - const xs = horizontalCompaction(g, adjustedLayering, align.root, align.align, horizontal === 'r'); - if (horizontal === 'r') { - for (const entry of Object.entries(xs)) { - xs[entry[0]] = -entry[1]; - } - } - xss[vertical + horizontal] = xs; - } - } - // Find smallest width alignment: Returns the alignment that has the smallest width of the given alignments. - let minWidth = Number.POSITIVE_INFINITY; - let minValue = undefined; - for (const xs of Object.values(xss)) { - let max = Number.NEGATIVE_INFINITY; - let min = Number.POSITIVE_INFINITY; - for (const entry of Object.entries(xs)) { - const v = entry[0]; - const x = entry[1]; - const halfWidth = g.node(v).label.width / 2; - max = Math.max(x + halfWidth, max); - min = Math.min(x - halfWidth, min); - } - const width = max - min; - if (width < minWidth) { - minWidth = width; - minValue = xs; - } - } - // Align the coordinates of each of the layout alignments such that - // left-biased alignments have their minimum coordinate at the same point as - // the minimum coordinate of the smallest width alignment and right-biased - // alignments have their maximum coordinate at the same point as the maximum - // coordinate of the smallest width alignment. - const alignTo = minValue; - const range = (values) => { - let min = Number.POSITIVE_INFINITY; - let max = Number.NEGATIVE_INFINITY; - for (const value of values) { - if (value < min) { - min = value; - } - if (value > max) { - max = value; - } - } - return [ min, max ]; - }; - const alignToRange = range(Object.values(alignTo)); - for (const vertical of ['u', 'd']) { - for (const horizontal of ['l', 'r']) { - const alignment = vertical + horizontal; - const xs = xss[alignment]; - let delta; - if (xs !== alignTo) { - const vsValsRange = range(Object.values(xs)); - delta = horizontal === 'l' ? alignToRange[0] - vsValsRange[0] : alignToRange[1] - vsValsRange[1]; - if (delta) { - const list = {}; - for (const key of Object.keys(xs)) { - list[key] = xs[key] + delta; - } - xss[alignment] = list; - } - } - } - } - // balance - const align = g.options.align; - if (align) { - const xs = xss[align.toLowerCase()]; - for (const v of Object.keys(xss.ul)) { - g.node(v).label.x = xs[v]; - } - } - else { - for (const v of Object.keys(xss.ul)) { - const xs = [ xss.ul[v], xss.ur[v], xss.dl[v], xss.dr[v] ].sort((a, b) => a - b); - g.node(v).label.x = (xs[1] + xs[2]) / 2; - } - } - }; - - const positionSelfEdges = (g) => { - for (const node of g.nodes.values()) { - const label = node.label; - if (label.dummy === 'selfedge') { - const v = node.v; - const selfNode = g.node(label.e.v).label; - const x = selfNode.x + selfNode.width / 2; - const y = selfNode.y; - const dx = label.x - x; - const dy = selfNode.height / 2; - g.setEdge(label.e.v, label.e.w, label.label); - g.removeNode(v); - label.label.points = [ - { x: x + 2 * dx / 3, y: y - dy }, - { x: x + 5 * dx / 6, y: y - dy }, - { x: x + dx , y: y }, - { x: x + 5 * dx / 6, y: y + dy }, - { x: x + 2 * dx / 3, y: y + dy } - ]; - label.label.x = label.x; - label.label.y = label.y; - } - } - }; - - const removeBorderNodes = (g) => { - for (const node of g.nodes.values()) { - const v = node.v; - if (g.children(v).length) { - const label = node.label; - const t = g.node(label.borderTop).label; - const b = g.node(label.borderBottom).label; - const l = g.node(label.borderLeft[label.borderLeft.length - 1]).label; - const r = g.node(label.borderRight[label.borderRight.length - 1]).label; - label.width = Math.abs(r.x - l.x); - label.height = Math.abs(b.y - t.y); - label.x = l.x + label.width / 2; - label.y = t.y + label.height / 2; - } - } - for (const node of g.nodes.values()) { - if (node.label.dummy === 'border') { - g.removeNode(node.v); - } - } - }; - - const fixupEdgeLabelCoords = (g) => { - for (const e of g.edges.values()) { - const edge = e.label; - if ('x' in edge) { - if (edge.labelpos === 'l' || edge.labelpos === 'r') { - edge.width -= edge.labeloffset; - } - switch (edge.labelpos) { - case 'l': edge.x -= edge.width / 2 + edge.labeloffset; break; - case 'r': edge.x += edge.width / 2 + edge.labeloffset; break; - default: throw new dagre.Error("Unsupported label position '" + edge.labelpos + "'."); - } - } - } - }; - - const translateGraph = (g) => { - let minX = Number.POSITIVE_INFINITY; - let maxX = 0; - let minY = Number.POSITIVE_INFINITY; - let maxY = 0; - const getExtremes = (attrs) => { - const x = attrs.x; - const y = attrs.y; - const w = attrs.width; - const h = attrs.height; - minX = Math.min(minX, x - w / 2); - maxX = Math.max(maxX, x + w / 2); - minY = Math.min(minY, y - h / 2); - maxY = Math.max(maxY, y + h / 2); - }; - for (const node of g.nodes.values()) { - getExtremes(node.label); - } - for (const e of g.edges.values()) { - const edge = e.label; - if ('x' in edge) { - getExtremes(edge); - } - } - for (const node of g.nodes.values()) { - node.label.x -= minX; - node.label.y -= minY; - } - for (const e of g.edges.values()) { - const edge = e.label; - for (const p of edge.points) { - p.x -= minX; - p.y -= minY; - } - if ('x' in edge) { - edge.x -= minX; - } - if ('y' in edge) { - edge.y -= minY; - } - } - const graphLabel = g.options; - graphLabel.width = maxX - minX; - graphLabel.height = maxY - minY; - }; - - const assignNodeIntersects = (g) => { - // Finds where a line starting at point ({x, y}) would intersect a rectangle - // ({x, y, width, height}) if it were pointing at the rectangle's center. - const intersectRect = (rect, point) => { - const x = rect.x; - const y = rect.y; - // Rectangle intersection algorithm from: http://math.stackexchange.com/questions/108113/find-edge-between-two-boxes - const dx = point.x - x; - const dy = point.y - y; - let w = rect.width / 2; - let h = rect.height / 2; - if (!dx && !dy) { - throw new Error('Not possible to find intersection inside of the rectangle'); - } - let sx; - let sy; - if (Math.abs(dy) * w > Math.abs(dx) * h) { - // Intersection is top or bottom of rect. - if (dy < 0) { - h = -h; - } - sx = h * dx / dy; - sy = h; - } - else { - // Intersection is left or right of rect. - if (dx < 0) { - w = -w; - } - sx = w; - sy = w * dy / dx; - } - return { x: x + sx, y: y + sy }; - }; - for (const e of g.edges.values()) { - const edge = e.label; - const vNode = e.vNode.label; - const wNode = e.wNode.label; - let p1; - let p2; - if (!edge.points) { - edge.points = []; - p1 = wNode; - p2 = vNode; - } - else { - p1 = edge.points[0]; - p2 = edge.points[edge.points.length - 1]; - } - edge.points.unshift(intersectRect(vNode, p1)); - edge.points.push(intersectRect(wNode, p2)); - } - }; - - time(' makeSpaceForEdgeLabels', () => { makeSpaceForEdgeLabels(g); }); - time(' removeSelfEdges', () => { removeSelfEdges(g); }); - time(' acyclic_run', () => { acyclic_run(g); }); - time(' nestingGraph_run', () => { nestingGraph_run(g); }); - time(' rank', () => { rank(asNonCompoundGraph(g)); }); - time(' injectEdgeLabelProxies', () => { injectEdgeLabelProxies(g); }); - time(' removeEmptyRanks', () => { removeEmptyRanks(g); }); - time(' nestingGraph_cleanup', () => { nestingGraph_cleanup(g); }); - time(' assignRankMinMax', () => { assignRankMinMax(g); }); - time(' removeEdgeLabelProxies', () => { removeEdgeLabelProxies(g); }); - time(' normalize', () => { normalize(g); }); - time(' parentDummyChains', () => { parentDummyChains(g); }); - time(' addBorderSegments', () => { addBorderSegments(g); }); - time(' order', () => { order(g); }); - time(' insertSelfEdges', () => { insertSelfEdges(g); }); - time(' coordinateSystem_adjust', () => { coordinateSystem_adjust(g); }); - time(' position', () => { position(g); }); - time(' positionSelfEdges', () => { positionSelfEdges(g); }); - time(' removeBorderNodes', () => { removeBorderNodes(g); }); - time(' denormalize', () => { denormalize(g); }); - time(' fixupEdgeLabelCoords', () => { fixupEdgeLabelCoords(g); }); - time(' coordinateSystem_undo', () => { coordinateSystem_undo(g); }); - time(' translateGraph', () => { translateGraph(g); }); - time(' assignNodeIntersects', () => { assignNodeIntersects(g); }); - time(' acyclic_undo', () => { acyclic_undo(g); }); - }; - - // Copies final layout information from the layout graph back to the input graph. - // This process only copies whitelisted attributes from the layout graph to the input graph, - // so it serves as a good place to determine what attributes can influence layout. - const updateSourceGraph = (graph, g) => { - for (const node of graph.nodes.values()) { - const label = node.label; - if (label) { - const v = node.v; - const layoutLabel = g.node(v).label; - label.x = layoutLabel.x; - label.y = layoutLabel.y; - if (g.children(v).length) { - label.width = layoutLabel.width; - label.height = layoutLabel.height; - } - } - } - for (const e of graph.edges.values()) { - const label = g.edge(e.v, e.w).label; - e.label.points = label.points; - if ('x' in label) { - e.label.x = label.x; - e.label.y = label.y; - } - } - graph.options.width = g.options.width; - graph.options.height = g.options.height; - }; - - time('layout', () => { - const layoutGraph = - time(' buildLayoutGraph', () => { return buildLayoutGraph(graph); }); - time(' runLayout', () => { runLayout(layoutGraph, time); }); - time(' updateSourceGraph', () => { updateSourceGraph(graph, layoutGraph); }); - }); -}; - -dagre.Graph = class { - - constructor(options) { - options = options || {}; - this._directed = 'directed' in options ? options.directed : true; - this._compound = 'compound' in options ? options.compound : false; - this._label = undefined; - this._defaultNodeLabelFn = () => { - return undefined; - }; - this.nodes = new Map(); - this.edges = new Map(); - if (this._compound) { - this._parent = {}; - this._children = {}; - this._children['\x00'] = {}; - } - } - - set options(value) { - this._label = value; - } - - get options() { - return this._label; - } - - isDirected() { - return this._directed; - } - - isCompound() { - return this._compound; - } - - setDefaultNodeLabel(newDefault) { - this._defaultNodeLabelFn = newDefault; - } - - setNode(v, label) { - const node = this.nodes.get(v); - if (node) { - if (label) { - node.label = label; - } - } - else { - const node = { label: label ? label : this._defaultNodeLabelFn(v), in: [], out: [], predecessors: {}, successors: {}, v: v }; - this.nodes.set(v, node); - if (this._compound) { - this._parent[v] = '\x00'; - this._children[v] = {}; - this._children['\x00'][v] = true; - } - } - } - - node(v) { - return this.nodes.get(v); - } - - hasNode(v) { - return this.nodes.has(v); - } - - removeNode(v) { - const node = this.nodes.get(v); - if (node) { - if (this._compound) { - delete this._children[this._parent[v]][v]; - delete this._parent[v]; - for (const child of this.children(v)) { - this.setParent(child); - } - delete this._children[v]; - } - for (const edge of node.in) { - this.removeEdge(edge); - } - for (const edge of node.out) { - this.removeEdge(edge); - } - this.nodes.delete(v); - } - } - - setParent(v, parent) { - if (!this._compound) { - throw new Error('Cannot set parent in a non-compound graph'); - } - if (parent) { - for (let ancestor = parent; ancestor !== undefined; ancestor = this.parent(ancestor)) { - if (ancestor === v) { - throw new Error('Setting ' + parent + ' as parent of ' + v + ' would create a cycle.'); - } - } - this.setNode(parent); - } - else { - parent = '\x00'; - } - delete this._children[this._parent[v]][v]; - this._parent[v] = parent; - this._children[parent][v] = true; - } - - parent(v) { - if (this._compound) { - const parent = this._parent[v]; - if (parent !== '\x00') { - return parent; - } - } - return null; - } - - children(v) { - if (this._compound) { - return Object.keys(this._children[v === undefined ? '\x00' : v]); - } - else if (v === undefined) { - return this.nodes.keys(); - } - else if (this.hasNode(v)) { - return []; - } - return null; - } - - predecessors(v) { - return Object.keys(this.nodes.get(v).predecessors); - } - - successors(v) { - return Object.keys(this.nodes.get(v).successors); - } - - neighbors(v) { - return Array.from(new Set(this.predecessors(v).concat(this.successors(v)))); - } - - edge(v, w) { - return this.edges.get(this._edgeKey(this._directed, v, w)); - } - - setEdge(v, w, label, name) { - const key = this._edgeKey(this._directed, v, w, name); - const edge = this.edges.get(key); - if (edge) { - edge.label = label; - } - else { - if (!this._directed && v > w) { - const tmp = v; - v = w; - w = tmp; - } - const edge = { label: label, v: v, w: w, name: name, key: key, vNode: null, wNode: null }; - this.edges.set(key, edge); - this.setNode(v); - this.setNode(w); - const wNode = this.nodes.get(w); - const vNode = this.nodes.get(v); - edge.wNode = wNode; - edge.vNode = vNode; - const incrementOrInitEntry = (map, k) => { - if (map[k]) { - map[k]++; - } - else { - map[k] = 1; - } - }; - incrementOrInitEntry(wNode.predecessors, v); - incrementOrInitEntry(vNode.successors, w); - wNode.in.push(edge); - vNode.out.push(edge); - } - } - - removeEdge(edge) { - const key = edge.key; - const v = edge.v; - const w = edge.w; - const decrementOrRemoveEntry = (map, k) => { - if (!--map[k]) { - delete map[k]; - } - }; - const wNode = edge.wNode; - const vNode = edge.vNode; - decrementOrRemoveEntry(wNode.predecessors, v); - decrementOrRemoveEntry(vNode.successors, w); - wNode.in = wNode.in.filter((edge) => edge.key !== key); - vNode.out = vNode.out.filter((edge) => edge.key !== key); - this.edges.delete(key); - } - - _edgeKey(isDirected, v, w, name) { - if (!isDirected && v > w) { - return name ? w + ':' + v + ':' + name : w + ':' + v + ':'; - } - return name ? v + ':' + w + ':' + name : v + ':' + w + ':'; - } - - toString() { - return [ - '[nodes]', Array.from(this.nodes.values()).map(n => JSON.stringify(n.label)).join('\n'), - '[edges]', Array.from(this.edges.values()).map(e => JSON.stringify(e.label)).join('\n'), - '[parents]', JSON.stringify(this._parent, null, 2), - '[children]', JSON.stringify(this._children, null, 2) - ].join('\n'); - } -}; - -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports = dagre; -} diff --git a/media/CircleEditor/external/flatbuffers.js b/media/CircleEditor/external/flatbuffers.js deleted file mode 100644 index f56031a2..00000000 --- a/media/CircleEditor/external/flatbuffers.js +++ /dev/null @@ -1,392 +0,0 @@ - - -var flatbuffers = {}; -var json = json || require('./json'); - -flatbuffers.get = (name) => { - flatbuffers._map = flatbuffers._map || new Map(); - if (!flatbuffers._map.has(name)) { - flatbuffers._map.set(name, {}); - } - return flatbuffers._map.get(name); -}; - -flatbuffers.BinaryReader = class { - - static open(data) { - return new flatbuffers.BinaryReader(data); - } - - constructor(data) { - const buffer = data instanceof Uint8Array ? data : data.peek(); - this._buffer = buffer; - this._position = 0; - this._dataView = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength); - } - - get root() { - return this.int32(this._position) + this._position; - } - - get identifier() { - if (this._buffer.length >= 8) { - const buffer = this._buffer.slice(4, 8); - if (buffer.every((c) => c >= 32 && c <= 128)) { - return String.fromCharCode(...buffer); - } - } - return ''; - } - - bool(offset) { - return !!this.int8(offset); - } - - bool_(position, offset, defaultValue) { - offset = this._offset(position, offset); - return offset ? this.bool(position + offset) : defaultValue; - } - - int8(offset) { - return this.uint8(offset) << 24 >> 24; - } - - int8_(position, offset, defaultValue) { - offset = this._offset(position, offset); - return offset ? this.int8(position + offset) : defaultValue; - } - - uint8(offset) { - return this._buffer[offset]; - } - - uint8_(position, offset, defaultValue) { - offset = this._offset(position, offset); - return offset ? this.uint8(position + offset) : defaultValue; - } - - int16(offset) { - return this._dataView.getInt16(offset, true); - } - - int16_(position, offset, defaultValue) { - offset = this._offset(position, offset); - return offset ? this.int16(position + offset) : defaultValue; - } - - uint16(offset) { - return this._dataView.getUint16(offset, true); - } - - uint16_(position, offset, defaultValue) { - offset = this._offset(position, offset); - return offset ? this.uint16(position + offset) : defaultValue; - } - - int32(offset) { - return this._dataView.getInt32(offset, true); - } - - int32_(position, offset, defaultValue) { - offset = this._offset(position, offset); - return offset ? this.int32(position + offset) : defaultValue; - } - - uint32(offset) { - return this._dataView.getUint32(offset, true); - } - - uint32_(position, offset, defaultValue) { - offset = this._offset(position, offset); - return offset ? this.int32(position + offset) : defaultValue; - } - - int64(offset) { - return this._dataView.getInt64(offset, true); - } - - int64_(position, offset, defaultValue) { - offset = this._offset(position, offset); - return offset ? this.int64(position + offset) : defaultValue; - } - - uint64(offset) { - return this._dataView.getUint64(offset, true); - } - - uint64_(position, offset, defaultValue) { - offset = this._offset(position, offset); - return offset ? this.uint64(position + offset) : defaultValue; - } - - float32(offset) { - return this._dataView.getFloat32(offset, true); - } - - float32_(position, offset, defaultValue) { - offset = this._offset(position, offset); - return offset ? this.float32(position + offset) : defaultValue; - } - - float64(offset) { - return this._dataView.getFloat64(offset, true); - } - - float64_(position, offset, defaultValue) { - offset = this._offset(position, offset); - return offset ? this.float64(position + offset) : defaultValue; - } - - string(offset, encoding) { - offset += this.int32(offset); - const length = this.int32(offset); - var result = ''; - var i = 0; - offset += 4; - if (encoding === 1) { - return this._buffer.subarray(offset, offset + length); - } - while (i < length) { - var codePoint; - // Decode UTF-8 - const a = this.uint8(offset + i++); - if (a < 0xC0) { - codePoint = a; - } - else { - const b = this.uint8(offset + i++); - if (a < 0xE0) { - codePoint = ((a & 0x1F) << 6) | (b & 0x3F); - } - else { - const c = this.uint8(offset + i++); - if (a < 0xF0) { - codePoint = ((a & 0x0F) << 12) | ((b & 0x3F) << 6) | (c & 0x3F); - } - else { - const d = this.uint8(offset + i++); - codePoint = ((a & 0x07) << 18) | ((b & 0x3F) << 12) | ((c & 0x3F) << 6) | (d & 0x3F); - } - } - } - // Encode UTF-16 - if (codePoint < 0x10000) { - result += String.fromCharCode(codePoint); - } - else { - codePoint -= 0x10000; - result += String.fromCharCode((codePoint >> 10) + 0xD800, (codePoint & ((1 << 10) - 1)) + 0xDC00); - } - } - - return result; - } - - string_(position, offset, defaultValue) { - offset = this._offset(position, offset); - return offset ? this.string(position + offset) : defaultValue; - } - - bools_(position, offset) { - offset = this._offset(position, offset); - if (offset) { - const length = this._length(position + offset); - offset = this._vector(position + offset); - const array = new Array(length); - for (let i = 0; i < length; i++) { - array[i] = this.uint8(offset + i + 4) ? true : false; - } - return array; - } - return []; - } - - int64s_(position, offset) { - offset = this._offset(position, offset); - if (offset) { - const length = this._length(position + offset); - offset = this._vector(position + offset); - const array = new Array(length); - for (let i = 0; i < length; i++) { - array[i] = this.int64(offset + (i << 3)); - } - return array; - } - return []; - } - - uint64s_(position, offset) { - offset = this._offset(position, offset); - if (offset) { - const length = this._length(position + offset); - offset = this._vector(position + offset); - const array = new Array(length); - for (let i = 0; i < length; i++) { - array[i] = this.uint64(offset + (i << 3)); - } - return array; - } - return []; - } - - strings_(position, offset) { - offset = this._offset(position, offset); - if (offset) { - const length = this._length(position + offset); - offset = this._vector(position + offset); - const array = new Array(length); - for (let i = 0; i < length; i++) { - array[i] = this.string(offset + i * 4); - } - return array; - } - return []; - } - - struct(position, offset, decode) { - offset = this._offset(position, offset); - return offset ? decode(this, position + offset) : null; - } - - table(position, offset, decode) { - offset = this._offset(position, offset); - return offset ? decode(this, this._indirect(position + offset)) : null; - } - - union(position, offset, decode) { - const type_offset = this._offset(position, offset); - const type = type_offset ? this.uint8(position + type_offset) : 0; - offset = this._offset(position, offset + 2); - return offset ? decode(this, this._union(position + offset), type) : null; - } - - typedArray(position, offset, type) { - offset = this._offset(position, offset); - return offset ? new type(this._buffer.buffer, this._buffer.byteOffset + this._vector(position + offset), this._length(position + offset)) : new type(0); - } - - unionArray(/* position, offset, decode */) { - throw new flatbuffers.Error('Not implemented.'); - } - - structArray(position, offset, size, decode) { - offset = this._offset(position, offset); - const length = offset ? this._length(position + offset) : 0; - const list = new Array(length); - for (let i = 0; i < length; i++) { - list[i] = decode(this, this._indirect(this._vector(position + offset) + i * 4)); - } - return list; - } - - tableArray(position, offset, decode) { - offset = this._offset(position, offset); - const length = offset ? this._length(position + offset) : 0; - const list = new Array(length); - for (let i = 0; i < length; i++) { - list[i] = decode(this, this._indirect(this._vector(position + offset) + i * 4)); - } - return list; - } - - _offset(bb_pos, vtableOffset) { - var vtable = bb_pos - this.int32(bb_pos); - return vtableOffset < this.int16(vtable) ? this.int16(vtable + vtableOffset) : 0; - } - - _indirect(offset) { - return offset + this.int32(offset); - } - - _vector(offset) { - return offset + this.int32(offset) + 4; - } - - _length(offset) { - return this.int32(offset + this.int32(offset)); - } - - _union(offset) { - return offset + this.int32(offset); - } -}; - -flatbuffers.TextReader = class { - - static open(obj) { - return new flatbuffers.TextReader(obj); - } - - constructor(obj) { - this._root = obj; - } - - get root() { - return this._root; - } - - value(obj, defaultValue) { - return obj !== undefined ? obj : defaultValue; - } - - object(obj, decode) { - return obj !== undefined ? decode(this, obj) : obj; - } - - array(obj) { - if (Array.isArray(obj)) { - const target = new Array(obj.length); - for (let i = 0; i < obj.length; i++) { - target[i] = obj[i]; - } - return target; - } - if (!obj) { - return []; - } - throw new flatbuffers.Error('Inalid value array.'); - } - - typedArray(obj, type) { - if (Array.isArray(obj)) { - const target = new type(obj.length); - for (let i = 0; i < obj.length; i++) { - target[i] = obj[i]; - } - return target; - } - if (!obj) { - return new type(0); - } - throw new flatbuffers.Error('Inalid typed array.'); - } - - objectArray(obj, decode) { - if (Array.isArray(obj)) { - const target = new Array(obj.length); - for (let i = 0; i < obj.length; i++) { - target[i] = decode(this, obj[i]); - } - return target; - } - if (!obj) { - return []; - } - throw new flatbuffers.Error('Inalid object array.'); - } -}; - -flatbuffers.Error = class extends Error { - - constructor(message) { - super(message); - this.name = 'FlatBuffers Error'; - this.message = message; - } -}; - -if (typeof module !== "undefined" && typeof module.exports === "object") { - module.exports.BinaryReader = flatbuffers.BinaryReader; - module.exports.TextReader = flatbuffers.TextReader; - module.exports.get = flatbuffers.get; -} diff --git a/media/CircleEditor/external/flexbuffers.js b/media/CircleEditor/external/flexbuffers.js deleted file mode 100644 index d73514d6..00000000 --- a/media/CircleEditor/external/flexbuffers.js +++ /dev/null @@ -1,198 +0,0 @@ - -var flexbuffers = {}; - -flexbuffers.BinaryReader = class { - - static open(buffer) { - const length = buffer.length; - if (length >= 3) { - const byteWidth = buffer[length - 1]; - if (byteWidth <= 8) { - const packedType = buffer[length - 2]; - return new flexbuffers.BinaryReader(buffer, length - 2 - byteWidth, byteWidth, 1 << (packedType & 3), packedType >> 2); - } - } - return null; - } - - constructor(buffer, offset, parentWidth, byteWidth, type) { - this._buffer = buffer; - this._length = buffer.length; - this._view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength); - this._utf8Decoder = new TextDecoder('utf-8'); - this._root = new flexbuffers.Reference(this, offset, parentWidth, byteWidth, type); - } - - read() { - return this._root.read(); - } - - get length() { - return this._length; - } - - int(offset, size) { - switch (size) { - case 1: return this._view.getInt8(offset); - case 2: return this._view.getInt16(offset, true); - case 4: return this._view.getInt32(offset, true); - case 8: return this._view.getInt64(offset, true); - default: throw new flexbuffers.Error("Invalid int size '" + size + "'."); - } - } - - uint(offset, size) { - switch (size) { - case 1: return this._view.getUint8(offset); - case 2: return this._view.getUint16(offset, true); - case 4: return this._view.getUint32(offset, true); - case 8: return this._view.getUint64(offset, true); - default: throw new flexbuffers.Error("Invalid uint size '" + size + "'."); - } - } - - float(offset, size) { - switch (size) { - case 4: return this._view.getFloat32(offset, true); - case 8: return this._view.getFloat64(offset, true); - default: throw new flexbuffers.Error("Invalid float size '" + size + "'."); - } - } - - string(offset, size) { - let end = size === undefined ? this._buffer.indexOf(0, offset) : offset + size; - end = end === -1 ? this._buffer.length : end; - const bytes = this._buffer.subarray(offset, end); - return this._utf8Decoder.decode(bytes); - } - - bytes(offset, size) { - return this._buffer.slice(offset, offset + size); - } -}; - -flexbuffers.Reference = class { - - constructor(reader, offset, parentWidth, byteWidth, type) { - this._reader = reader; - this._offset = offset; - this._parentWidth = parentWidth; - this._byteWidth = byteWidth; - this._type = type; - } - - read() { - switch (this._type) { - case 0x00: // null - return null; - case 0x01: // int - return this._reader.int(this._offset, this._parentWidth); - case 0x02: // uint - return this._reader.uint(this._offset, this._parentWidth); - case 0x03: // float - return this._reader.float(this._offset, this._parentWidth); - case 0x04: { // key - return this._reader.string(this._indirect()); - } - case 0x05: { // string - const offset = this._indirect(); - const size = this._reader.uint(offset - this._byteWidth, this._byteWidth); - return this._reader.string(offset, size); - } - case 0x06: // indirect int - return this._reader.int(this._indirect(), this._byteWidth); - case 0x07: // indirect uint - return this._reader.uint(this._indirect(), this._byteWidth); - case 0x08: // indirect float - return this._reader.float(this._indirect(), this._byteWidth); - case 0x09: { // map - const offset = this._indirect(); - const keysOffset = offset - (this._byteWidth * 3); - const keysVectorOffset = keysOffset - this._reader.uint(keysOffset, this._byteWidth); - const keysByteWidth = this._reader.uint(keysOffset + this._byteWidth, this._byteWidth); - const keys = this._typedVector(keysVectorOffset, keysByteWidth, 0x04); - const values = this._vector(offset, this._byteWidth); - const map = {}; - for (let i = 0; i < keys.length; i++) { - map[keys[i]] = values[i]; - } - return map; - } - case 0x0a: { // vector - return this._vector(this._indirect(), this._byteWidth); - } - case 0x0b: // vector int - case 0x0c: // vector uint - case 0x0d: // vector float - case 0x0e: // vector key - case 0x0f: // vector string deprecated - case 0x24: { // vector bool - return this._typedVector(this._indirect(), this._byteWidth, this._type - 0x0b + 0x01); - } - case 0x10: // vector int2 - case 0x11: // vector uint2 - case 0x12: // vector float2 - case 0x13: // vector int3 - case 0x14: // vector uint3 - case 0x15: // vector float3 - case 0x16: // vector int4 - case 0x17: // vector uint4 - case 0x18: { // vector float4 - const offset = this._indirect(); - const size = (((this._type - 0x10) / 3) >> 0) + 2; - const type = ((this._type - 0x10) % 3) + 0x01; - return this._typedVector(offset, this._byteWidth, type, size); - } - case 0x19: { // blob - const offset = this._indirect(); - const size = this._reader.uint(offset - this._byteWidth, this._byteWidth); - return this._reader.bytes(offset, size); - } - case 0x1a: { // bool - return this._reader.uint(this._offset, this._parentWidth) !== 0; - } - default: { - throw new flexbuffers.Error("Unsupported reference type '" + this._type); - } - } - } - - _indirect() { - return this._offset - this._reader.uint(this._offset, this._parentWidth); - } - - _vector(offset, byteWidth) { - const size = this._reader.uint(offset - byteWidth, byteWidth); - const packedTypeOffset = offset + (size * byteWidth); - const vector = new Array(size); - for (let i = 0; i < size; i++) { - const packedType = this._reader.uint(packedTypeOffset + i, 1); - const reference = new flexbuffers.Reference(this._reader, offset + (i * byteWidth), byteWidth, 1 << (packedType & 3), packedType >> 2); - vector[i] = reference.read(); - } - return vector; - } - - _typedVector(offset, byteWidth, type, size) { - size = size === undefined ? this._reader.uint(offset - byteWidth, byteWidth) : size; - const vector = new Array(size); - for (let i = 0; i < size; i++) { - const reference = new flexbuffers.Reference(this._reader, offset + (i * byteWidth), byteWidth, 1, type); - vector[i] = reference.read(); - } - return vector; - } -}; - -flexbuffers.Error = class extends Error { - - constructor(message) { - super(message); - this.name = 'FlexBuffers Error'; - this.message = message; - } -}; - -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.BinaryReader = flexbuffers.BinaryReader; -} diff --git a/media/CircleEditor/external/gzip.js b/media/CircleEditor/external/gzip.js deleted file mode 100644 index 4c746696..00000000 --- a/media/CircleEditor/external/gzip.js +++ /dev/null @@ -1,234 +0,0 @@ - -var gzip = gzip || {}; -var zip = zip || require('./zip'); - -gzip.Archive = class { - - static open(data) { - const stream = data instanceof Uint8Array ? new gzip.BinaryReader(data) : data; - const signature = [ 0x1f, 0x8b ]; - if (stream.length > 18 && stream.peek(2).every((value, index) => value === signature[index])) { - return new gzip.Archive(stream); - } - return null; - } - - constructor(stream) { - const position = stream.position; - const entry = new gzip.Entry(stream); - this._entries = new Map([ [ entry.name, entry.stream ] ]); - stream.seek(position); - } - - get entries() { - return this._entries; - } -}; - -gzip.Entry = class { - - constructor(stream) { - const signature = [ 0x1f, 0x8b ]; - if (stream.position + 2 > stream.length || - !stream.read(2).every((value, index) => value === signature[index])) { - throw new gzip.Error('Invalid gzip signature.'); - } - const string = () => { - let content = ''; - while (stream.position < stream.length) { - const value = stream.byte(); - if (value === 0x00) { - break; - } - content += String.fromCharCode(value); - } - return content; - }; - const reader = new gzip.BinaryReader(stream.read(8)); - const compressionMethod = reader.byte(); - if (compressionMethod != 8) { - throw new gzip.Error("Invalid compression method '" + compressionMethod.toString() + "'."); - } - const flags = reader.byte(); - reader.uint32(); // MTIME - reader.byte(); // XFL - reader.byte(); // OS - if ((flags & 4) != 0) { // FEXTRA - const xlen = stream.byte() | (stream.byte() << 8); - stream.skip(xlen); - } - this._name = (flags & 8) != 0 ? string() : ''; // FNAME - if ((flags & 16) != 0) { // FCOMMENT - string(); - } - if ((flags & 1) != 0) { // FHCRC - stream.skip(2); - } - this._stream = new gzip.InflaterStream(stream); - } - - get name() { - return this._name; - } - - get stream() { - return this._stream; - } -}; - -gzip.InflaterStream = class { - - constructor(stream) { - this._stream = stream.stream(stream.length - stream.position - 8); - const reader = new gzip.BinaryReader(stream.read(8)); - reader.uint32(); // CRC32 - this._length = reader.uint32(); // ISIZE - this._position = 0; - } - - get position() { - return this._position; - } - - get length() { - return this._length; - } - - seek(position) { - if (this._buffer === undefined) { - this._inflate(); - } - this._position = position >= 0 ? position : this._length + position; - } - - skip(offset) { - if (this._buffer === undefined) { - this._inflate(); - } - this._position += offset; - } - - stream(length) { - return new gzip.BinaryReader(this.read(length)); - } - - peek(length) { - const position = this._position; - length = length !== undefined ? length : this._length - this._position; - this.skip(length); - const end = this._position; - this.seek(position); - if (position === 0 && length === this._length) { - return this._buffer; - } - return this._buffer.subarray(position, end); - } - - read(length) { - const position = this._position; - length = length !== undefined ? length : this._length - this._position; - this.skip(length); - if (position === 0 && length === this._length) { - return this._buffer; - } - return this._buffer.subarray(position, this._position); - } - - byte() { - const position = this._position; - this.skip(1); - return this._buffer[position]; - } - - _inflate() { - if (this._buffer === undefined) { - const buffer = this._stream.peek(); - this._buffer = new zip.Inflater().inflateRaw(buffer, this._length); - delete this._stream; - } - } -}; - -gzip.BinaryReader = class { - - constructor(buffer) { - this._buffer = buffer; - this._length = buffer.length; - this._position = 0; - this._view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength); - } - - get position() { - return this._position; - } - - get length() { - return this._length; - } - - create(buffer) { - return new gzip.BinaryReader(buffer); - } - - stream(length) { - return this.create(this.read(length)); - } - - seek(position) { - this._position = position >= 0 ? position : this._length + position; - } - - skip(offset) { - this._position += offset; - } - - peek(length) { - if (this._position === 0 && length === undefined) { - return this._buffer; - } - const position = this._position; - this.skip(length !== undefined ? length : this._length - this._position); - const end = this._position; - this.seek(position); - return this._buffer.subarray(position, end); - } - - read(length) { - if (this._position === 0 && length === undefined) { - this._position = this._length; - return this._buffer; - } - const position = this._position; - this.skip(length !== undefined ? length : this._length - this._position); - return this._buffer.subarray(position, this._position); - } - - byte() { - const position = this._position; - this.skip(1); - return this._buffer[position]; - } - - uint16() { - const position = this._position; - this.skip(2); - return this._view.getUint16(position, true); - } - - uint32() { - const position = this._position; - this.skip(4); - return this._view.getUint32(position, true); - } -}; - -gzip.Error = class extends Error { - constructor(message) { - super(message); - this.name = 'Gzip Error'; - } -}; - -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.Archive = gzip.Archive; -} diff --git a/media/CircleEditor/external/json.js b/media/CircleEditor/external/json.js deleted file mode 100644 index 17eb7a93..00000000 --- a/media/CircleEditor/external/json.js +++ /dev/null @@ -1,574 +0,0 @@ - -var json = json || {}; -var text = text || require('./text'); - -json.TextReader = class { - - static open(data) { - const decoder = text.Decoder.open(data); - let state = 'start'; - for (let i = 0; i < 0x1000; i++) { - const c = decoder.decode(); - if (c === undefined || c === '\0') { - if (i === 0) { - return null; - } - break; - } - if (c <= ' ') { - if (c !== ' ' && c !== '\n' && c !== '\r' && c !== '\t') { - return null; - } - continue; - } - switch (state) { - case 'start': - if (c === '#') { - state = 'comment'; - break; - } - if (c === '[') { - state = 'list'; - break; - } - if (c === '{') { - state = 'object'; - break; - } - if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') { - state = ''; - break; - } - return null; - case 'list': - if (c === '"' || c === '-' || c === '+' || c === '{' || c === '[' || (c >= '0' && c <= '9')) { - state = ''; - break; - } - return null; - case 'object': - if (c != '"') { - return null; - } - state = ''; - continue; - default: - break; - } - } - return new json.TextReader(data); - } - - constructor(data) { - this._data = data; - this._escape = { '"': '"', '\\': '\\', '/': '/', b: '\b', f: '\f', n: '\n', r: '\r', t: '\t' }; - } - - read() { - const decoder = text.Decoder.open(this._data); - const stack = []; - this._decoder = decoder; - this._position = 0; - this._char = decoder.decode(); - this._whitespace(); - let obj = undefined; - let first = true; - for (;;) { - if (Array.isArray(obj)) { - this._whitespace(); - let c = this._char; - if (c === ']') { - this._next(); - this._whitespace(); - if (stack.length > 0) { - obj = stack.pop(); - first = false; - continue; - } - if (this._char !== undefined) { - this._unexpected(); - } - return obj; - } - if (!first) { - if (this._char !== ',') { - this._unexpected(); - } - this._next(); - this._whitespace(); - c = this._char; - } - first = false; - switch (c) { - case '{': { - this._next(); - stack.push(obj); - const item = {}; - obj.push(item); - obj = item; - first = true; - break; - } - case '[': { - this._next(); - stack.push(obj); - const item = []; - obj.push(item); - obj = item; - first = true; - break; - } - default: { - obj.push(c === '"' ? this._string() : this._literal()); - break; - } - } - } - else if (obj instanceof Object) { - this._whitespace(); - let c = this._char; - if (c === '}') { - this._next(); - this._whitespace(); - if (stack.length > 0) { - obj = stack.pop(); - first = false; - continue; - } - if (this._char !== undefined) { - this._unexpected(); - } - return obj; - } - if (!first) { - if (this._char !== ',') { - this._unexpected(); - } - this._next(); - this._whitespace(); - c = this._char; - } - first = false; - if (c === '"') { - const key = this._string(); - switch (key) { - case '__proto__': - case 'constructor': - case 'prototype': - throw new json.Error("Invalid key '" + key + "'" + this._location()); - default: - break; - } - this._whitespace(); - if (this._char !== ':') { - this._unexpected(); - } - this._next(); - this._whitespace(); - c = this._char; - switch (c) { - case '{': { - this._next(); - stack.push(obj); - const value = {}; - obj[key] = value; - obj = value; - first = true; - break; - } - case '[': { - this._next(); - stack.push(obj); - const value = []; - obj[key] = value; - obj = value; - first = true; - break; - } - default: { - obj[key] = c === '"' ? this._string() : this._literal(); - break; - } - } - this._whitespace(); - continue; - } - this._unexpected(); - } - else { - const c = this._char; - switch (c) { - case '{': { - this._next(); - this._whitespace(); - obj = {}; - first = true; - break; - } - case '[': { - this._next(); - this._whitespace(); - obj = []; - first = true; - break; - } - default: { - const value = c === '"' ? this._string() : c >= '0' && c <= '9' ? this._number() : this._literal(); - this._whitespace(); - if (this._char !== undefined) { - this._unexpected(); - } - return value; - } - } - } - } - } - - _next() { - if (this._char === undefined) { - this._unexpected(); - } - this._position = this._decoder.position; - this._char = this._decoder.decode(); - } - - _whitespace() { - while (this._char === ' ' || this._char === '\n' || this._char === '\r' || this._char === '\t') { - this._next(); - } - } - - _literal() { - const c = this._char; - if (c >= '0' && c <= '9') { - return this._number(); - } - switch (c) { - case 't': this._expect('true'); return true; - case 'f': this._expect('false'); return false; - case 'n': this._expect('null'); return null; - case 'N': this._expect('NaN'); return NaN; - case 'I': this._expect('Infinity'); return Infinity; - case '-': return this._number(); - default: this._unexpected(); - } - return null; - } - - _number() { - let value = ''; - if (this._char === '-') { - value = '-'; - this._next(); - } - if (this._char === 'I') { - this._expect('Infinity'); - return -Infinity; - } - const c = this._char; - if (c < '0' || c > '9') { - this._unexpected(); - } - value += c; - this._next(); - if (c === '0') { - const n = this._char; - if (n >= '0' && n <= '9') { - this._unexpected(); - } - } - while (this._char >= '0' && this._char <= '9') { - value += this._char; - this._next(); - } - if (this._char === '.') { - value += '.'; - this._next(); - const n = this._char; - if (n < '0' || n > '9') { - this._unexpected(); - } - while (this._char >= '0' && this._char <= '9') { - value += this._char; - this._next(); - } - } - if (this._char === 'e' || this._char === 'E') { - value += this._char; - this._next(); - const s = this._char; - if (s === '-' || s === '+') { - value += this._char; - this._next(); - } - const c = this._char; - if (c < '0' || c > '9') { - this._unexpected(); - } - value += this._char; - this._next(); - while (this._char >= '0' && this._char <= '9') { - value += this._char; - this._next(); - } - } - return +value; - } - - _string() { - let value = ''; - this._next(); - while (this._char != '"') { - if (this._char === '\\') { - this._next(); - if (this._char === 'u') { - this._next(); - let uffff = 0; - for (let i = 0; i < 4; i ++) { - const hex = parseInt(this._char, 16); - if (!isFinite(hex)) { - this._unexpected(); - } - this._next(); - uffff = uffff * 16 + hex; - } - value += String.fromCharCode(uffff); - } - else if (this._escape[this._char]) { - value += this._escape[this._char]; - this._next(); - } - else { - this._unexpected(); - } - } - else if (this._char < ' ') { - this._unexpected(); - } - else { - value += this._char; - this._next(); - } - } - this._next(); - return value; - } - - _expect(value) { - for (let i = 0; i < value.length; i++) { - if (value[i] !== this._char) { - this._unexpected(); - } - this._next(); - } - } - - _unexpected() { - let c = this._char; - if (c === undefined) { - throw new json.Error('Unexpected end of JSON input.'); - } - else if (c === '"') { - c = 'string'; - } - else if ((c >= '0' && c <= '9') || c === '-') { - c = 'number'; - } - else { - if (c < ' ' || c > '\x7F') { - const name = Object.keys(this._escape).filter((key) => this._escape[key] === c); - c = (name.length === 1) ? '\\' + name : '\\u' + ('000' + c.charCodeAt(0).toString(16)).slice(-4); - } - c = "token '" + c + "'"; - } - throw new json.Error('Unexpected ' + c + this._location()); - } - - _location() { - let line = 1; - let column = 1; - this._decoder.position = 0; - let c; - do { - if (this._decoder.position === this._position) { - return ' at ' + line.toString() + ':' + column.toString() + '.'; - } - c = this._decoder.decode(); - if (c === '\n') { - line++; - column = 1; - } - else { - column++; - } - } - while (c !== undefined); - return ' at ' + line.toString() + ':' + column.toString() + '.'; - } -}; - -json.BinaryReader = class { - - static open(data) { - const buffer = data instanceof Uint8Array ? data : data.peek(); - return new json.BinaryReader(buffer); - } - - constructor(buffer) { - this._buffer = buffer; - } - - read() { - const buffer = this._buffer; - const length = buffer.length; - const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength); - const asciiDecoder = new TextDecoder('ascii'); - const utf8Decoder = new TextDecoder('utf-8'); - let position = 0; - const skip = (offset) => { - position += offset; - if (position > length) { - throw new json.Error('Expected ' + (position + length) + ' more bytes. The file might be corrupted. Unexpected end of file.', true); - } - }; - const header = () => { - const start = position; - skip(4); - const size = view.getInt32(start, 4); - if (size < 5 || start + size > length || buffer[start + size - 1] != 0x00) { - throw new json.Error('Invalid file size.', true); - } - }; - header(); - const stack = []; - let obj = {}; - for (;;) { - skip(1); - const type = buffer[position - 1]; - if (type == 0x00) { - if (stack.length === 0) { - break; - } - obj = stack.pop(); - continue; - } - const start = position; - position = buffer.indexOf(0x00, start) + 1; - const key = asciiDecoder.decode(buffer.subarray(start, position - 1)); - let value = null; - switch (type) { - case 0x01: { // float64 - const start = position; - skip(8); - value = view.getFloat64(start, true); - break; - } - case 0x02: { // string - skip(4); - const size = view.getInt32(position - 4, true); - const start = position; - skip(size); - value = utf8Decoder.decode(buffer.subarray(start, position - 1)); - if (buffer[position - 1] != '0x00') { - throw new json.Error('String missing terminal 0.', true); - } - break; - } - case 0x03: { // object - header(); - value = {}; - break; - } - case 0x04: { // array - header(); - value = []; - break; - } - case 0x05: { // bytes - const start = position; - skip(5); - const size = view.getInt32(start, true); - const subtype = buffer[start + 4]; - if (subtype !== 0x00) { - throw new json.Error("Unsupported binary subtype '" + subtype + "'.", true); - } - skip(size); - value = buffer.subarray(start + 5, position); - break; - } - case 0x08: { // boolean - skip(1); - value = buffer[position - 1]; - if (value > 1) { - throw new json.Error("Invalid boolean value '" + value + "'.", true); - } - value = value === 1 ? true : false; - break; - } - case 0x0A: - value = null; - break; - case 0x10: { - const start = position; - skip(4); - value = view.getInt32(start, true); - break; - } - case 0x11: { // uint64 - const start = position; - skip(8); - value = view.getUint64(start, true).toNumber(); - break; - } - case 0x12: { // int64 - const start = position; - skip(8); - value = view.getInt64(start, true).toNumber(); - break; - } - default: { - throw new json.Error("Unsupported value type '" + type + "'.", true); - } - } - if (Array.isArray(obj)) { - if (obj.length !== parseInt(key, 10)) { - throw new json.Error("Invalid array index '" + key + "'.", true); - } - obj.push(value); - } - else { - switch (key) { - case '__proto__': - case 'constructor': - case 'prototype': - throw new json.Error("Invalid key '" + key + "' at " + position.toString() + "'.", true); - default: - break; - } - obj[key] = value; - } - if (type === 0x03 || type === 0x04) { - stack.push(obj); - obj = value; - } - } - if (position !== length) { - throw new json.Error("Unexpected data at '" + position.toString() + "'.", true); - } - return obj; - } -}; - -json.Error = class extends Error { - - constructor(message, binary) { - super(message); - this.name = binary ? 'BSON Error' : 'JSON Error'; - } -}; - -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.TextReader = json.TextReader; - module.exports.BinaryReader = json.BinaryReader; -} diff --git a/media/CircleEditor/external/protobuf.js b/media/CircleEditor/external/protobuf.js deleted file mode 100644 index befaee88..00000000 --- a/media/CircleEditor/external/protobuf.js +++ /dev/null @@ -1,1357 +0,0 @@ - -var protobuf = protobuf || {}; -var base = base || require('./base'); -var text = text || require('./text'); - -protobuf.get = (name) => { - protobuf._map = protobuf._map || new Map(); - if (!protobuf._map.has(name)) { - protobuf._map.set(name, {}); - } - return protobuf._map.get(name); -}; - -protobuf.BinaryReader = class { - - static open(buffer) { - return new protobuf.BinaryReader(buffer); - } - - constructor(data) { - const buffer = data instanceof Uint8Array ? data : data.peek(); - this._buffer = buffer; - this._length = buffer.length; - this._position = 0; - this._view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength); - this._utf8Decoder = new TextDecoder('utf-8'); - } - - signature() { - const tags = new Map(); - this._position = 0; - try { - if (this._length > 0) { - const type = this._buffer[0] & 7; - if (type !== 4 && type !== 6 && type !== 7) { - const length = this.length; - while (this._position < length) { - const tag = this.uint32(); - const field = tag >>> 3; - const type = tag & 7; - if (type > 5 || field === 0) { - tags.clear(); - break; - } - tags.set(field, type); - if (!this._skipType(type)) { - tags.clear(); - break; - } - } - } - } - } - catch (err) { - tags.clear(); - } - this._position = 0; - return tags; - } - - decode() { - let tags = {}; - this._position = 0; - try { - const decodeMessage = (max) => { - const length = this._uint32(); - if (length === undefined) { - return undefined; - } - if (length === 0) { - // return 2; - } - const end = this.position + length; - if (end > max) { - return undefined; - } - try { - const tags = {}; - while (this.position < end) { - const tag = this._uint32(); - if (tag === undefined) { - this.seek(end); - return 2; - } - const field = tag >>> 3; - const type = tag & 7; - if (type > 5 || field === 0) { - this.seek(end); - return 2; - } - if (type === 2) { - const type = tags[field]; - if (type !== 2) { - const inner = decodeMessage(end); - if (this.position > end) { - this.seek(end); - return 2; - } - if (inner === undefined) { - this.seek(end); - return 2; - } - if (inner === 2) { - tags[field] = inner; - } - else if (!type) { - tags[field] = inner; - } - else { - for (const pair of Object.entries(inner)) { - if (type[pair[0]] === 2 && pair[1] !== 2) { - continue; - } - type[pair[0]] = pair[1]; - } - } - continue; - } - } - tags[field] = type; - if (!this._skipType(type)) { - this.seek(end); - return 2; - } - } - if (this.position === end) { - return tags; - } - } - catch (err) { - // continue regardless of error - } - this.seek(end); - return 2; - }; - if (this._length > 0) { - const type = this._buffer[0] & 7; - if (type !== 4 && type !== 6 && type !== 7) { - const length = this.length; - while (this.position < length) { - const tag = this.uint32(); - const field = tag >>> 3; - const type = tag & 7; - if (type > 5 || field === 0) { - tags = {}; - break; - } - if (type === 2) { - const type = tags[field]; - if (type !== 2) { - const inner = decodeMessage(length); - if (inner === undefined) { - tags = {}; - break; - } - if (inner === 2) { - tags[field] = inner; - } - else if (!type) { - tags[field] = inner; - } - else { - for (const pair of Object.entries(inner)) { - if (type[pair[0]] === 2 && pair[1] !== 2) { - continue; - } - type[pair[0]] = pair[1]; - } - } - continue; - } - } - tags[field] = type; - if (!this._skipType(type)) { - tags = {}; - break; - } - } - } - } - } - catch (err) { - tags = {}; - } - this._position = 0; - return tags; - } - - get length() { - return this._length; - } - - get position() { - return this._position; - } - - seek(position) { - this._position = position >= 0 ? position : this._length + position; - } - - string() { - return this._utf8Decoder.decode(this.bytes()); - } - - bool() { - return this.uint32() !== 0; - } - - byte() { - if (this._position < this._length) { - return this._buffer[this._position++]; - } - throw new RangeError('Unexpected end of file.'); - } - - bytes() { - const length = this.uint32(); - const position = this._position; - this.skip(length); - return this._buffer.slice(position, this._position); - } - - uint32() { - let c; - c = this.byte(); - let value = (c & 127) >>> 0; - if (c < 128) { - return value; - } - c = this.byte(); - value = (value | (c & 127) << 7) >>> 0; - if (c < 128) { - return value; - } - c = this.byte(); - value = (value | (c & 127) << 14) >>> 0; - if (c < 128) { - return value; - } - c = this.byte(); - value = (value | (c & 127) << 21) >>> 0; - if (c < 128) { - return value; - } - c = this.byte(); - value = (value | (c & 15) << 28) >>> 0; - if (c < 128) { - return value; - } - if (this.byte() !== 255 || this.byte() !== 255 || this.byte() !== 255 || this.byte() !== 255 || this.byte() !== 1) { - throw new protobuf.Error('Varint is not 32-bit.'); - } - return value; - } - - int32() { - return this.uint32() | 0; - } - - sint32() { - const value = this.uint32(); - return value >>> 1 ^ -(value & 1) | 0; - } - - int64() { - return this._varint().toInt64(); - } - - uint64() { - return this._varint().toInt64(); - } - - sint64() { - return this._varint().zzDecode().toInt64(); - } - - fixed64() { - const position = this._position; - this.skip(8); - return this._view.getUint64(position, true); - } - - sfixed64() { - const position = this._position; - this.skip(8); - return this._view.getInt64(position, true); - } - - fixed32() { - const position = this._position; - this.skip(4); - return this._view.getUint32(position, true); - } - - sfixed32() { - const position = this._position; - this.skip(4); - return this._view.getInt32(position, true); - } - - float() { - const position = this._position; - this.skip(4); - return this._view.getFloat32(position, true); - } - - double() { - const position = this._position; - this.skip(8); - return this._view.getFloat64(position, true); - } - - array(obj, item, tag) { - if ((tag & 7) === 2) { - const end = this.uint32() + this._position; - while (this._position < end) { - obj.push(item()); - } - } - else { - obj.push(item()); - } - return obj; - } - - floats(obj, tag) { - if ((tag & 7) === 2) { - if (obj && obj.length > 0) { - throw new protobuf.Error('Invalid packed float array.'); - } - const size = this.uint32(); - const end = this._position + size; - if (end > this._length) { - this._unexpected(); - } - const length = size >>> 2; - obj = size > 1048576 ? new Float32Array(length) : new Array(length); - let position = this._position; - for (let i = 0; i < length; i++) { - obj[i] = this._view.getFloat32(position, true); - position += 4; - } - this._position = end; - } - else { - if (obj !== undefined && obj.length < 1000000) { - obj.push(this.float()); - } - else { - obj = undefined; - this.float(); - } - } - return obj; - } - - doubles(obj, tag) { - if ((tag & 7) === 2) { - if (obj && obj.length > 0) { - throw new protobuf.Error('Invalid packed float array.'); - } - const size = this.uint32(); - const end = this._position + size; - if (end > this._length) { - this._unexpected(); - } - const length = size >>> 3; - obj = size > 1048576 ? new Float64Array(length) : new Array(length); - let position = this._position; - for (let i = 0; i < length; i++) { - obj[i] = this._view.getFloat64(position, true); - position += 8; - } - this._position = end; - } - else { - if (obj !== undefined && obj.length < 1000000) { - obj.push(this.double()); - } - else { - obj = undefined; - this.double(); - } - } - return obj; - } - - skip(offset) { - this._position += offset; - if (this._position > this._length) { - this._unexpected(); - } - } - - skipVarint() { - do { - if (this._position >= this._length) { - this._unexpected(); - } - } - while (this._buffer[this._position++] & 128); - } - - _uint32() { - let c; - if (this._position < this._length) { - c = this._buffer[this._position++]; - let value = (c & 127) >>> 0; - if (c < 128) { - return value; - } - if (this._position < this._length) { - c = this._buffer[this._position++]; - value = (value | (c & 127) << 7) >>> 0; - if (c < 128) { - return value; - } - if (this._position < this._length) { - c = this._buffer[this._position++]; - value = (value | (c & 127) << 14) >>> 0; - if (c < 128) { - return value; - } - if (this._position < this._length) { - c = this._buffer[this._position++]; - value = (value | (c & 127) << 21) >>> 0; - if (c < 128) { - return value; - } - if (this._position < this._length) { - c = this._buffer[this._position++]; - value = (value | (c & 15) << 28) >>> 0; - if (c < 128) { - return value; - } - if (this.byte() !== 255 || this.byte() !== 255 || this.byte() !== 255 || this.byte() !== 255 || this.byte() !== 1) { - return undefined; - } - return value; - } - } - } - } - } - return undefined; - } - - _skipType(wireType) { - switch (wireType) { - case 0: { - // const max = this._position + 9; - do { - if (this._position >= this._length /* || this._position > max */) { - return false; - } - } - while (this._buffer[this._position++] & 128); - break; - } - case 1: { - if (this._position + 8 >= this._length) { - return false; - } - this._position += 8; - break; - } - case 2: { - const length = this._uint32(); - if (length === undefined) { - return false; - } - if (this._position + length > this._end) { - return false; - } - this._position += length; - break; - } - case 3: { - for (;;) { - const tag = this._uint32(); - if (tag === undefined) { - return false; - } - const wireType = tag & 7; - if (wireType === 4) { - break; - } - if (!this._skipType(wireType)) { - return false; - } - } - break; - } - case 5: { - if (this._position + 4 >= this._length) { - return false; - } - this._position += 4; - break; - } - default: { - return false; - } - } - return true; - } - - skipType(wireType) { - switch (wireType) { - case 0: - this.skipVarint(); - break; - case 1: - this.skip(8); - break; - case 2: - this.skip(this.uint32()); - break; - case 3: - while ((wireType = this.uint32() & 7) !== 4) { - this.skipType(wireType); - } - break; - case 5: - this.skip(4); - break; - default: - throw new protobuf.Error('Invalid type ' + wireType + ' at offset ' + this._position + '.'); - } - } - - entry(obj, key, value) { - this.skipVarint(); - this._position++; - let k = key(); - if (!Number.isInteger(k) && typeof k !== 'string') { - k = k.toNumber(); - } - this._position++; - const v = value(); - obj[k] = v; - } - - _varint() { - const bits = new protobuf.LongBits(0, 0); - let i = 0; - if (this._length - this._position > 4) { // fast route (lo) - for (; i < 4; ++i) { - // 1st..4th - bits.lo = (bits.lo | (this._buffer[this._position] & 127) << i * 7) >>> 0; - if (this._buffer[this._position++] < 128) { - return bits; - } - } - // 5th - bits.lo = (bits.lo | (this._buffer[this._position] & 127) << 28) >>> 0; - bits.hi = (bits.hi | (this._buffer[this._position] & 127) >> 4) >>> 0; - if (this._buffer[this._position++] < 128) { - return bits; - } - i = 0; - } - else { - for (; i < 3; i++) { - if (this._position >= this._length) { - this._unexpected(); - } - bits.lo = (bits.lo | (this._buffer[this._position] & 127) << i * 7) >>> 0; - if (this._buffer[this._position++] < 128) { - return bits; - } - } - bits.lo = (bits.lo | (this._buffer[this._position++] & 127) << i * 7) >>> 0; - return bits; - } - if (this._length - this._position > 4) { - for (; i < 5; ++i) { - bits.hi = (bits.hi | (this._buffer[this._position] & 127) << i * 7 + 3) >>> 0; - if (this._buffer[this._position++] < 128) { - return bits; - } - } - } - else { - for (; i < 5; ++i) { - if (this._position >= this._length) { - this._unexpected(); - } - bits.hi = (bits.hi | (this._buffer[this._position] & 127) << i * 7 + 3) >>> 0; - if (this._buffer[this._position++] < 128) { - return bits; - } - } - } - throw new protobuf.Error('Invalid varint encoding.'); - } - - _unexpected() { - throw new RangeError('Unexpected end of file.'); - } -}; - -protobuf.TextReader = class { - - static open(data) { - const buffer = data instanceof Uint8Array ? data : data.peek(); - const decoder = text.Decoder.open(buffer); - let first = true; - for (let i = 0; i < 0x100; i++) { - const c = decoder.decode(); - if (c === undefined) { - if (i === 0) { - return null; - } - break; - } - if (c === '\0') { - return null; - } - const whitespace = c === ' ' || c === '\n' || c === '\r' || c === '\t'; - if (c < ' ' && !whitespace) { - return null; - } - if (first && !whitespace) { - first = false; - if (c === '#') { - let c; - do { - c = decoder.decode(); - } - while (c !== undefined && c !== '\n'); - if (c === undefined) { - break; - } - continue; - } - if (c === '[') { - continue; - } - if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') { - continue; - } - return null; - } - } - return new protobuf.TextReader(buffer); - } - - constructor(buffer) { - this._decoder = text.Decoder.open(buffer); - this.reset(); - } - - signature() { - const tags = new Map(); - this.reset(); - try { - this.start(false); - while (!this.end()) { - const tag = this.tag(); - if (this.token() === '{') { - this.start(); - tags.set(tag, true); - while (!this.end()) { - const subtag = this.tag(); - tags.set(tag + '.' + subtag, true); - this.skip(); - this.match(','); - } - } - else { - this.skip(); - tags.set(tag, true); - } - } - } - catch (err) { - // continue regardless of error - } - this.reset(); - return tags; - } - - reset() { - this._decoder.position = 0; - this._position = 0; - this._token = undefined; - this._depth = 0; - this._arrayDepth = 0; - this._token = ''; - this.next(); - } - - start() { - if (this._depth > 0) { - this.expect('{'); - } - this._depth++; - } - - end() { - if (this._depth <= 0) { - throw new protobuf.Error('Invalid depth ' + this.location()); - } - if (this._token === '}') { - this.expect('}'); - this.match(';'); - this._depth--; - return true; - } - if (this._token === undefined) { - if (this._depth !== 1) { - throw new protobuf.Error('Unexpected end of input' + this.location()); - } - this._depth--; - return true; - } - return false; - } - - tag() { - const name = this._token; - this.next(); - if (this._token !== '[' && this._token !== '{') { - this.expect(':'); - } - return name; - } - - integer() { - const token = this._token; - const value = Number.parseInt(token, 10); - if (Number.isNaN(token - value)) { - throw new protobuf.Error("Couldn't parse integer '" + token + "'" + this.location()); - } - this.next(); - this.semicolon(); - return value; - } - - double() { - let value = NaN; - let token = this._token; - switch (token) { - case 'nan': value = NaN; break; - case 'inf': value = Infinity; break; - case '-inf': value = -Infinity; break; - default: - if (token.endsWith('f')) { - token = token.substring(0, token.length - 1); - } - value = Number.parseFloat(token); - if (Number.isNaN(token - value)) { - throw new protobuf.Error("Couldn't parse float '" + token + "'" + this.location()); - } - break; - } - this.next(); - this.semicolon(); - return value; - } - - float() { - return this.double(); - } - - uint32() { - return this.integer(); - } - - int32() { - return this.integer(); - } - - sint32() { - return this.integer(); - } - - int64() { - return base.Int64.create(this.integer()); - } - - uint64() { - return base.Uint64.create(this.integer()); - } - - sint64() { - return base.Int64.create(this.integer()); - } - - fixed64() { - return base.Uint64.create(this.integer()); - } - - sfixed64() { - return base.Int64.create(this.integer()); - } - - fixed32() { - return this.integer(); - } - - sfixed32() { - return this.integer(); - } - - string() { - const token = this._token; - if (token.length < 2) { - throw new protobuf.Error('String is too short' + this.location()); - } - const quote = token[0]; - if (quote !== "'" && quote !== '"') { - throw new protobuf.Error('String is not in quotes' + this.location()); - } - if (quote !== token[token.length - 1]) { - throw new protobuf.Error('String quotes do not match' + this.location()); - } - const value = token.substring(1, token.length - 1); - this.next(); - this.semicolon(); - return value; - } - - bool() { - const token = this._token; - switch (token) { - case 'true': - case 'True': - case '1': - this.next(); - this.semicolon(); - return true; - case 'false': - case 'False': - case '0': - this.next(); - this.semicolon(); - return false; - default: - throw new protobuf.Error("Couldn't parse boolean '" + token + "'" + this.location()); - } - } - - bytes() { - const token = this.string(); - const length = token.length; - const array = new Uint8Array(length); - for (let i = 0; i < length; i++) { - array[i] = token.charCodeAt(i); - } - return array; - } - - enum(type) { - const token = this._token; - let value; - if (Object.prototype.hasOwnProperty.call(type, token)) { - value = type[token]; - } - else { - value = Number.parseInt(token, 10); - if (Number.isNaN(token - value)) { - throw new protobuf.Error("Couldn't parse enum '" + (token === undefined ? '' : token) + "'" + this.location()); - } - } - this.next(); - this.semicolon(); - return value; - } - - any(type) { - this.start(); - const message = type(); - if (this._token.startsWith('[') && this._token.endsWith(']')) { - message.type_url = this._token.substring(1, this._token.length - 1).trim(); - this.next(); - this.match(':'); - message.value = this.read(); - this.match(';'); - if (!this.end()) { - this.expect('}'); - } - } - else { - while (!this.end()) { - const tag = this.tag(); - switch (tag) { - case "type_url": - message.type_url = this.string(); - break; - case "value": - message.value = this.bytes(); - break; - default: - this.field(tag, message); - break; - } - } - } - return message; - } - - anyarray(obj, type) { - this.start(); - if (this._token.startsWith('[') && this._token.endsWith(']')) { - while (!this.end()) { - if (this._token.startsWith('[') && this._token.endsWith(']')) { - const message = type(); - message.type_url = this._token.substring(1, this._token.length - 1).trim(); - this.next(); - this.match(':'); - message.value = this.read(); - this.match(';'); - obj.push(message); - continue; - } - this.expect('['); - } - } - else { - const message = type(); - while (!this.end()) { - const tag = this.tag(); - switch (tag) { - case "type_url": - message.type_url = this.string(); - break; - case "value": - message.value = this.bytes(); - break; - default: - this.field(tag, message); - break; - } - } - obj.push(message); - } - } - - entry(obj, key, value) { - this.start(); - let k; - let v; - while (!this.end()) { - const tag = this.tag(); - switch (tag) { - case 'key': - k = key(); - break; - case 'value': - v = value(); - break; - default: - throw new protobuf.Error("Unsupported entry tag '" + tag + "'."); - } - } - obj[k] = v; - } - - array(obj, item) { - if (this.first()) { - while (!this.last()) { - obj.push(item()); - switch (this._token) { - case ',': - this.next(); - break; - case ']': - break; - default: - this.handle(this._token); - break; - } - } - } - else { - obj.push(item()); - } - } - - first() { - if (this.match('[')) { - this._arrayDepth++; - return true; - } - return false; - } - - last() { - if (this.match(']')) { - this._arrayDepth--; - return true; - } - return false; - } - - read() { - const start = this._position; - this.skip(); - const end = this._position; - const position = this._decoder.position; - this._decoder.position = start; - let content = ''; - while (this._decoder.position < end) { - content += this._decoder.decode(); - } - this._decoder.position = position; - return content; - } - - skip() { - switch (this._token) { - case '{': { - const depth = this._depth; - this.start(); - while (!this.end() || depth < this._depth) { - if (this._token === '{') { - this.start(); - } - else if (this._token !== '}') { - this.next(); - this.match(';'); - } - } - break; - } - case '[': { - const depth = this._arrayDepth; - this.first(); - while (!this.last() || depth < this._arrayDepth) { - this.next(); - if (this._token === '[') { - this.first(); - } - else if (this._token === undefined) { - this.handle(this._token); - } - } - break; - } - default: { - this.next(); - this.semicolon(); - break; - } - } - } - - handle(token) { - throw new protobuf.Error("Unexpected token '" + token + "'" + this.location()); - } - - field(token /*, module */) { - throw new protobuf.Error("Unsupported field '" + token + "'" + this.location()); - } - - token() { - return this._token; - } - - next() { - if (this._token === undefined) { - throw new protobuf.Error('Unexpected end of input' + this.location()); - } - this._position = this._decoder.position; - let c = this._decoder.decode(); - for (;;) { - switch (c) { - case ' ': - case '\n': - case '\r': - case '\t': - this._position = this._decoder.position; - c = this._decoder.decode(); - continue; - case '#': - do { - c = this._decoder.decode(); - if (c === undefined) { - this._token = undefined; - return; - } - } - while (c !== '\n'); - this._position = this._decoder.position; - c = this._decoder.decode(); - continue; - default: - break; - } - break; - } - if (c === undefined) { - this._token = undefined; - return; - } - if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c === '_' || c === '$') { - let token = c; - let position = this._decoder.position; - for (;;) { - c = this._decoder.decode(); - if (c === undefined || c === '\n') { - break; - } - if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c === '_' || c === '+' || c === '-') { - token += c; - position = this._decoder.position; - continue; - } - break; - } - this._decoder.position = position; - this._token = token; - return; - } - switch (c) { - case '{': - case '}': - case ':': - case ',': - case ']': - case ';': - this._token = c; - return; - case '[': { - let token = c; - let position = this._decoder.position; - let x = this._decoder.decode(); - if ((x !== undefined) && x >= 'a' && x <= 'z' || x >= 'A' && x <= 'Z') { - token += x; - for (;;) { - x = this._decoder.decode(); - if (x === undefined || x === '\n') { - break; - } - if (x >= 'a' && x <= 'z' || x >= 'A' && x <= 'Z' || x >= '0' && x <= '9' || x === '.' || x === '/') { - token += x; - position = this._decoder.position; - continue; - } - if (x === ']') { - this._token = token + x; - return; - } - } - } - this._decoder.position = position; - this._token = '['; - return; - } - case '"': - case "'": { - const quote = c; - let content = c; - for (;;) { - c = this._decoder.decode(); - if (c === undefined || c === '\n') { - throw new protobuf.Error('Unexpected end of string' + this.location()); - } - if (c == '\\') { - c = this._decoder.decode(); - if (c === undefined || c === '\n') { - throw new protobuf.Error('Unexpected end of string' + this.location()); - } - switch (c) { - case '\\': c = '\\'; break; - case "'": c = "'"; break; - case '"': c = '"'; break; - case 'r': c = '\r'; break; - case 'n': c = '\n'; break; - case 't': c = '\t'; break; - case 'b': c = '\b'; break; - case 'x': - case 'X': { - let value = 0; - for (let xi = 0; xi < 2; xi++) { - let xd = this._decoder.decode(); - if (xd === undefined) { - throw new protobuf.Error('Unexpected end of string' + this.location()); - } - xd = xd.charCodeAt(0); - xd = xd >= 65 && xd <= 70 ? xd - 55 : xd >= 97 && xd <= 102 ? xd - 87 : xd >= 48 && xd <= 57 ? xd - 48 : -1; - if (xd === -1) { - throw new protobuf.Error("Unexpected hex digit '" + xd + "' in bytes string" + this.location()); - } - value = value << 4 | xd; - } - c = String.fromCharCode(value); - break; - } - default: { - if (c < '0' || c > '9') { - throw new protobuf.Error("Unexpected character '" + c + "' in string" + this.location()); - } - let value = 0; - let od = c; - if (od < '0' || od > '9') { - throw new protobuf.Error("Unexpected octal digit '" + od + "' in bytes string" + this.location()); - } - od = od.charCodeAt(0); - value = value << 3 | od - 48; - od = this._decoder.decode(); - if (od === undefined) { - throw new protobuf.Error('Unexpected end of string' + this.location()); - } - if (od < '0' || od > '9') { - throw new protobuf.Error("Unexpected octal digit '" + od + "' in bytes string" + this.location()); - } - od = od.charCodeAt(0); - value = value << 3 | od - 48; - od = this._decoder.decode(); - if (od === undefined) { - throw new protobuf.Error('Unexpected end of string' + this.location()); - } - if (od < '0' || od > '9') { - throw new protobuf.Error("Unexpected octal digit '" + od + "' in bytes string" + this.location()); - } - od = od.charCodeAt(0); - value = value << 3 | od - 48; - c = String.fromCharCode(value); - break; - } - } - content += c; - continue; - } - else { - content += c; - if (c === quote) { - break; - } - } - } - this._token = content; - return; - } - case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': - case '-': case '+': case '.': { - let token = c; - let position = this._decoder.position; - for (;;) { - c = this._decoder.decode(); - if (c === undefined || c === '\n') { - break; - } - if ((c >= '0' && c <= '9') || c === '_' || c === '+' || c === '-' || c === '.' || c === 'e' || c === 'E') { - token += c; - position = this._decoder.position; - continue; - } - break; - } - if (token === '-' && c === 'i' && this._decoder.decode() === 'n' && this._decoder.decode() === 'f') { - token = '-inf'; - position = this._decoder.position; - } - if (token === '-' || token === '+' || token === '.') { - throw new protobuf.Error("Unexpected token '" + token + "'" + this.location()); - } - this._decoder.position = position; - this._token = token; - return; - } - default: { - throw new protobuf.Error("Unexpected token '" + c + "'" + this.location()); - } - } - } - - expect(value) { - if (this._token !== value) { - throw new protobuf.Error("Unexpected '" + this._token + "' instead of '" + value + "'" + this.location()); - } - this.next(); - } - - match(value) { - if (value == this._token) { - this.next(); - return true; - } - return false; - } - - location() { - let line = 1; - let column = 1; - this._decoder.position = 0; - let c; - do { - if (this._decoder.position === this._position) { - return ' at ' + line.toString() + ':' + column.toString() + '.'; - } - c = this._decoder.decode(); - if (c === '\n') { - line++; - column = 1; - } - else { - column++; - } - } - while (c !== undefined); - return ' at ' + line.toString() + ':' + column.toString() + '.'; - } - - semicolon() { - if (this._arrayDepth === 0) { - this.match(';'); - } - } -}; - -protobuf.Int64 = base.Int64; -protobuf.Uint64 = base.Uint64; - -protobuf.LongBits = class { - - constructor(lo, hi) { - this.lo = lo >>> 0; - this.hi = hi >>> 0; - } - - zzDecode() { - const mask = -(this.lo & 1); - this.lo = ((this.lo >>> 1 | this.hi << 31) ^ mask) >>> 0; - this.hi = ( this.hi >>> 1 ^ mask) >>> 0; - return this; - } - - toUint64() { - return new base.Uint64(this.lo, this.hi); - } - - toInt64() { - return new base.Int64(this.lo, this.hi); - } -}; - -protobuf.Error = class extends Error { - - constructor(message) { - super(message); - this.name = 'Protocol Buffer Error'; - this.message = message; - } -}; - -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.BinaryReader = protobuf.BinaryReader; - module.exports.TextReader = protobuf.TextReader; - module.exports.Error = protobuf.Error; - module.exports.Int64 = protobuf.Int64; - module.exports.Uint64 = protobuf.Uint64; - module.exports.get = protobuf.get; -} diff --git a/media/CircleEditor/external/python.js b/media/CircleEditor/external/python.js deleted file mode 100644 index 5ced83ab..00000000 --- a/media/CircleEditor/external/python.js +++ /dev/null @@ -1,4134 +0,0 @@ - -// Experimental Python parser - -var python = python || {}; - -python.Parser = class { - - constructor(text, file, debug) { - this._tokenizer = new python.Tokenizer(text, file); - this._debug = debug; - if (!python.Parser._precedence) { - python.Parser._precedence = { - 'or': 2, 'and': 3, 'not' : 4, - 'in': 5, 'instanceof': 5, 'is': 5, '<': 5, '>': 5, '<=': 5, '>=': 5, '<>': 5, '==': 5, '!=': 5, - '|': 6, '^' : 7, '&' : 8, - '<<': 9, '>>': 9, '+': 10, '-': 10, '*': 11, '@': 11, '/': 11, '//': 11, '%': 11, - // '+': 12, '-': 12, - '~': 13, '**': 14 - }; - } - } - - parse() { - const node = this._node('program'); - node.body = []; - while (!this._tokenizer.match('eof')) { - const statement = this._parseStatement(); - if (statement) { - node.body.push(statement); - continue; - } - if (this._tokenizer.eat('\n') || this._tokenizer.eat(';') || this._tokenizer.peek().type == 'eof') { - continue; - } - if (this._tokenizer.eat('indent') && this._tokenizer.peek().type == 'eof') { - continue; - } - throw new python.Error('Unsupported statement' + this._tokenizer.location()); - } - return node; - } - - _parseSuite() { - const node = this._node('block'); - node.statements = []; - let statement = null; - if (this._tokenizer.eat('\n')) { - if (this._tokenizer.eat('indent')) { - while (!this._tokenizer.eat('eof') && !this._tokenizer.eat('dedent')) { - if (this._tokenizer.eat(';')) { - continue; - } - statement = this._parseStatement(); - if (statement) { - node.statements.push(statement); - continue; - } - if (this._tokenizer.eat('\n')) { - continue; - } - if (this._tokenizer.match('dedent') || this._tokenizer.match('eof')) { - continue; - } - throw new python.Error('Empty statement' + this._tokenizer.location()); - } - } - } - else if (!this._tokenizer.eat('eof')) { - while (!this._tokenizer.match('\n') && !this._tokenizer.match('eof') && !this._tokenizer.match('dedent')) { - if (this._tokenizer.eat(';')) { - continue; - } - statement = this._parseStatement(); - if (statement) { - node.statements.push(statement); - continue; - } - throw new python.Error('Empty statement' + this._tokenizer.location()); - } - this._tokenizer.eat('\n'); - } - - return node; - } - - _parseStatement() { - - let node = this._node(); - - node = this._eat('id', 'break'); - if (node) { - return node; - } - node = this._eat('id', 'continue'); - if (node) { - return node; - } - node = this._eat('id', 'return'); - if (node) { - node.expression = this._parseExpression(-1, [], true); - return node; - } - node = this._eat('id', 'raise'); - if (node) { - node.exception = this._parseExpression(-1, [ 'from' ]); - if (this._tokenizer.eat('id', 'from')) { - node.from = this._parseExpression(); - } - else if (this._tokenizer.eat(',')) { - node.exception = [ node.exception ]; - node.exception.push(this._parseExpression()); - if (this._tokenizer.eat(',')) { - node.exception.push(this._parseExpression()); - } - } - return node; - } - node = this._eat('id', 'assert'); - if (node) { - node.condition = this._parseExpression(); - while (this._tokenizer.eat(',')) { - node.condition = { type: 'list', value: [ node.condition ] }; - node.condition.value.push(this._parseExpression()); - } - return node; - } - node = this._eat('id', 'exec'); - if (node) { - node.variable = this._parseExpression(-1, [ 'in' ]); - if (this._tokenizer.eat('in')) { - do { - node.target = node.target || []; - node.target.push(this._parseExpression(-1, [ 'in' ], false)); - } - while (this._tokenizer.eat(',')); - } - return node; - } - - node = this._eat('id', 'global'); - if (node) { - node.variable = []; - do { - node.variable.push(this._parseName()); - } - while (this._tokenizer.eat(',')); - return node; - } - node = this._eat('id', 'nonlocal'); - if (node) { - node.variable = []; - do { - node.variable.push(this._parseName()); - } - while (this._tokenizer.eat(',')); - return node; - } - node = this._eat('id', 'import'); - if (node) { - node.modules = []; - do { - const module = this._node('module'); - module.name = this._parseExpression(-1, [], false); - if (this._tokenizer.eat('id', 'as')) { - module.as = this._parseExpression(-1, [], false); - } - node.modules.push(module); - } - while (this._tokenizer.eat(',')); - return node; - } - node = this._eat('id', 'from'); - if (node) { - const dots = this._tokenizer.peek(); - if (dots && Array.from(dots.type).every((c) => c == '.')) { - node.from = this._eat(dots.type); - node.from.expression = this._parseExpression(); - } - else { - node.from = this._parseExpression(); - } - this._tokenizer.expect('id', 'import'); - node.import = []; - const close = this._tokenizer.eat('('); - do { - const symbol = this._node(); - symbol.symbol = this._parseExpression(-1, [], false); - if (this._tokenizer.eat('id', 'as')) { - symbol.as = this._parseExpression(-1, [], false); - } - node.import.push(symbol); - } - while (this._tokenizer.eat(',')); - if (close) { - this._tokenizer.expect(')'); - } - return node; - } - node = this._eat('id', 'class'); - if (node) { - node.name = this._parseName().value; - if (this._tokenizer.peek().value === '(') { - node.base = this._parseArguments(); - } - this._tokenizer.expect(':'); - node.body = this._parseSuite(); - return node; - } - - const async = this._eat('id', 'async'); - if (async && - !this._tokenizer.match('id', 'def') && - !this._tokenizer.match('id', 'with') && - !this._tokenizer.match('id', 'for')) { - throw new python.Error("Expected 'def', 'with' or 'for'" + this._tokenizer.location()); - } - - node = this._eat('id', 'def'); - if (node) { - if (async) { - node.async = async; - } - node.name = this._parseName().value; - this._tokenizer.expect('('); - node.parameters = this._parseParameters(')'); - if (this._tokenizer.eat('->')) { - node.returnType = this._parseType(); - } - this._tokenizer.expect(':'); - node.body = this._parseSuite(); - return node; - } - node = this._eat('id', 'del'); - if (node) { - node.expression = this._parseExpression(-1, [], true); - return node; - } - node = this._eat('id', 'print'); - if (node) { - node.expression = this._parseExpression(-1, [], true); - return node; - } - node = this._eat('id', 'if'); - if (node) { - node.condition = this._parseExpression(); - this._tokenizer.expect(':'); - node.then = this._parseSuite(); - let current = node; - this._tokenizer.eat('\n'); - while (this._tokenizer.eat('id', 'elif')) { - current.else = this._node('if'); - current = current.else; - current.condition = this._parseExpression(); - this._tokenizer.expect(':'); - current.then = this._parseSuite(); - this._tokenizer.eat('\n'); - } - if (this._tokenizer.eat('id', 'else')) { - this._tokenizer.expect(':'); - current.else = this._parseSuite(); - } - return node; - } - node = this._eat('id', 'while'); - if (node) { - node.condition = this._parseExpression(); - this._tokenizer.expect(':'); - node.body = this._parseSuite(); - if (this._tokenizer.eat('id', 'else')) { - this._tokenizer.expect(':'); - node.else = this._parseSuite(); - } - return node; - } - node = this._eat('id', 'pass'); - if (node) { - return node; - } - node = this._eat('id', 'for'); - if (node) { - node.variable = []; - node.variable.push(this._parseExpression(-1, [ 'in' ])); - while (this._tokenizer.eat(',')) { - if (this._tokenizer.match('id', 'in')) { - node.variable.push({}); - break; - } - node.variable.push(this._parseExpression(-1, [ 'in' ])); - } - this._tokenizer.expect('id', 'in'); - node.target = []; - node.target.push(this._parseExpression()); - while (this._tokenizer.eat(',')) { - if (this._tokenizer.match(':')) { - node.target.push({}); - break; - } - node.target.push(this._parseExpression(-1, [ 'in' ])); - } - this._tokenizer.expect(':'); - node.body = this._parseSuite(); - if (this._tokenizer.eat('id', 'else')) { - this._tokenizer.expect(':'); - node.else = this._parseSuite(); - } - return node; - } - node = this._eat('id', 'with'); - if (node) { - if (async) { - node.async = async; - } - node.item = []; - do { - const item = this._node(); - item.type = 'with_item'; - item.expression = this._parseExpression(); - if (this._tokenizer.eat('id', 'as')) { - item.variable = this._parseExpression(); - } - node.item.push(item); - } - while (this._tokenizer.eat(',')); - this._tokenizer.expect(':'); - node.body = this._parseSuite(); - return node; - } - node = this._eat('id', 'try'); - if (node) { - this._tokenizer.expect(':'); - node.body = this._parseSuite(); - node.except = []; - while (this._tokenizer.match('id', 'except')) { - const except = this._node('except'); - this._tokenizer.expect('id', 'except'); - except.clause = []; - except.clause.push(this._parseExpression()); - while (this._tokenizer.eat(',')) { - if (this._tokenizer.match(':') || this._tokenizer.match('as')) { - except.clause.push({}); - break; - } - except.clause.push(this._parseExpression()); - } - if (this._tokenizer.eat('id', 'as')) { - except.variable = this._parseExpression(); - } - this._tokenizer.expect(':'); - except.body = this._parseSuite(); - node.except.push(except); - } - if (this._tokenizer.match('id', 'else')) { - node.else = this._node('else'); - this._tokenizer.expect('id', 'else'); - this._tokenizer.expect(':'); - node.else.body = this._parseSuite(); - } - if (this._tokenizer.match('id', 'finally')) { - node.finally = this._node('finally'); - this._tokenizer.expect('id', 'finally'); - this._tokenizer.expect(':'); - node.finally.body = this._parseSuite(); - } - return node; - } - - if (this._tokenizer.match('@')) { - node = this._node('decorator'); - this._tokenizer.expect('@'); - node.value = this._parseExpression(); - if (!node.value || (node.value.type !== 'call' && node.value.type !== 'id' && node.value.type !== '.')) { - throw new python.Error('Invalid decorator' + this._tokenizer.location()); - } - return node; - } - - const expression = this._parseExpression(-1, [], true); - if (expression) { - if (expression.type == 'id' && this._tokenizer.eat(':')) { - node = this._node('var'); - node.name = expression.value; - node.location = expression.location; - node.variableType = this._parseExpression(-1, [ '=' ]); - if (this._tokenizer.eat('=')) { - node.initializer = this._parseExpression(); - } - return node; - } - switch (expression.type) { - case '=': - case ':=': - case '==': - case '!=': - case '+=': - case '-=': - case '*=': - case '@=': - case '/=': - case '//=': - case '**=': - case '&=': - case '|=': - case '%=': - case '>>=': - case '<<=': - case '>>': - case '<<': - case '>=': - case '<=': - case '<': - case '>': - case '%': - case '^=': - case '...': - case 'call': - case 'assert': - case 'raise': - case 'string': - case 'list': - case 'var': - case '.': - case '[]': - case 'yield': - case '+': - case '-': - case '*': - case '**': - case '@': - case '/': - case '//': - case '~': - case '&': - case '^': - case '|': - case 'not': - case 'id': - case 'number': - case 'in': - case 'and': - case 'or': - case 'if': - case 'for': - case 'tuple': - case 'lambda': - case 'await': - return expression; - default: - throw new python.Error("Unhandled expression" + this._tokenizer.location()); - } - } - - return null; - } - - _parseExpression(minPrecedence, terminal, tuple) { - minPrecedence = minPrecedence || -1; - const terminalSet = new Set(terminal); - const stack = []; - for (;;) { - let node = this._node(); - const token = this._tokenizer.peek(); - if (stack.length == 1 && terminalSet.has(token.value)) { - break; - } - const precedence = python.Parser._precedence[token.value]; - if (precedence) { - if (precedence >= minPrecedence) { - this._tokenizer.read(); - node.type = token.value; - if (token.type == 'id' && (token.value === 'in' || token.value === 'not')) { - if (token.value === 'in') { - node.type = 'in'; - } - else if (this._tokenizer.eat('id', 'in')) { - node.type = 'not in'; - } - else { - node.type = 'not'; - node.expression = this._parseExpression(precedence, terminal, tuple === false ? false : true); - stack.push(node); - continue; - } - } - else if (token.value == '~') { - node.type = '~'; - node.expression = this._parseExpression(precedence, terminal, tuple === false ? false : true); - stack.push(node); - continue; - } - else if (token.type == 'id' && token.value == 'is') { - if (this._tokenizer.eat('id', 'not')) { - node.type = 'is not'; - } - } - node.left = stack.pop(); - node.right = this._parseExpression(precedence, terminal, tuple === false ? false : true); - stack.push(node); - continue; - } - } - if (this._tokenizer.eat(':=')) { - node.type = ':='; - node.target = stack.pop(); - node.expression = this._parseExpression(-1, terminal, tuple === false ? false : true); - stack.push(node); - continue; - } - if (this._tokenizer.eat('=')) { - node.type = '='; - node.target = stack.pop(); - node.expression = this._parseExpression(-1, terminal, tuple === false ? false : true); - stack.push(node); - continue; - } - switch (token.type) { - case '-=': - case '**=': - case '*=': - case '//=': - case '/=': - case '&=': - case '%=': - case '^=': - case '+=': - case '<<=': - case '>>=': - case '|=': - case '@=': - node = this._node(token.type); - this._tokenizer.expect(token.type); - node.target = stack.pop(); - node.expression = this._parseExpression(-1, terminal, true); - stack.push(node); - continue; - default: - break; - } - node = this._eat('id', 'if'); - if (node) { - node.then = stack.pop(); - node.condition = this._parseExpression(); - this._tokenizer.expect('id', 'else'); - node.else = this._parseExpression(); - stack.push(node); - continue; - } - while (this._tokenizer.match('id', 'for') || this._tokenizer.match('id', 'async')) { - const async = this._eat('id', 'async'); - if (async && !this._tokenizer.match('id', 'for')) { - throw new python.Error("Expected 'for'" + this._tokenizer.location()); - } - node = this._eat('id', 'for'); - if (node) { - if (async) { - node.async = async; - } - node.expression = stack.pop(); - node.variable = this._parseExpression(-1, [ 'in' ], true); - this._tokenizer.expect('id', 'in'); - node.target = this._parseExpression(-1, [ 'for', 'if' ], true); - while (this._tokenizer.eat('id', 'if')) { - node.condition = node.condition || []; - node.condition.push(this._parseExpression(-1, [ 'for', 'if' ])); - } - stack.push(node); - } - } - node = this._eat('id', 'lambda'); - if (node) { - node.parameters = this._parseParameters(':'); - node.body = this._parseExpression(-1, terminal, false); - stack.push(node); - continue; - } - node = this._eat('id', 'yield'); - if (node) { - if (this._tokenizer.eat('id', 'from')) { - node.from = this._parseExpression(-1, [], true); - } - else { - node.expression = []; - do { - node.expression.push(this._parseExpression(-1, [], false)); - } - while (this._tokenizer.eat(',')); - } - stack.push(node); - continue; - } - node = this._eat('id', 'await'); - if (node) { - node.expression = this._parseExpression(minPrecedence, terminal, tuple); - stack.push(node); - continue; - } - node = this._eat('.'); - if (node) { - this._tokenizer.eat('\n'); - node.target = stack.pop(); - node.member = this._parseName(); - stack.push(node); - continue; - } - if (this._tokenizer.peek().value === '(') { - if (stack.length == 0) { - node = this._node('tuple'); - const args = this._parseArguments(); - if (args.length == 1) { - stack.push(args[0]); - } - else { - node.value = args; - stack.push(node); - } - } - else { - node = this._node('call'); - node.target = stack.pop(); - node.arguments = this._parseArguments(); - stack.push(node); - } - continue; - } - if (this._tokenizer.peek().value === '[') { - if (stack.length == 0) { - stack.push(this._parseExpressions()); - } - else { - node = this._node('[]'); - node.target = stack.pop(); - node.arguments = this._parseSlice(); - stack.push(node); - } - continue; - } - if (this._tokenizer.peek().value == '{') { - stack.push(this._parseDictOrSetMaker()); - continue; - } - node = this._node(); - const literal = this._parseLiteral(); - if (literal) { - if (stack.length > 0 && literal.type == 'number' && - (literal.value.startsWith('-') || literal.value.startsWith('+'))) { - node.type = literal.value.substring(0, 1); - literal.value = literal.value.substring(1); - node.left = stack.pop(); - node.right = literal; - stack.push(node); - } - else if (stack.length == 1 && literal.type == 'string' && stack[0].type == 'string') { - stack[0].value += literal.value; - } - else { - if (literal.type === 'number') { - switch (literal.value) { - case 'inf': literal.value = Infinity; break; - case '-inf': literal.value = -Infinity; break; - default: break; - } - } - stack.push(literal); - } - continue; - } - if (this._tokenizer.peek().keyword) { - break; - } - node = this._eat('...'); - if (node) { - stack.push(node); - continue; - } - const identifier = this._parseName(); - if (identifier) { - stack.push(identifier); - continue; - } - - if (tuple === true && stack.length == 1 && this._tokenizer.eat(',')) { - if (stack[0].type === 'tuple') { - node = stack[0]; - } - else { - node = this._node('tuple'); - node.value = [ stack.pop() ]; - stack.push(node); - } - // for, bar, = - if (this._tokenizer.peek().value === '=') { - continue; - } - if (!this._tokenizer.match('=') && !terminalSet.has(this._tokenizer.peek().value)) { - const nextTerminal = terminal.slice(0).concat([ ',', '=' ]); - const expression = this._parseExpression(minPrecedence, nextTerminal, tuple); - if (expression) { - node.value.push(expression); - continue; - } - } - break; - } - break; - } - - if (stack.length == 1) { - return stack.pop(); - } - if (stack.length != 0) { - throw new python.Error('Unexpected expression' + this._tokenizer.location()); - } - return null; - } - - _parseDictOrSetMaker() { - const list = []; - this._tokenizer.expect('{'); - let dict = true; - while (!this._tokenizer.eat('}')) { - const item = this._parseExpression(-1, [], false); - if (item == null) { - throw new python.Error('Expected expression' + this._tokenizer.location()); - } - if (!this._tokenizer.eat(':')) { - dict = false; - } - if (dict) { - const value = this._parseExpression(-1, [], false); - if (value == null) { - throw new python.Error('Expected expression' + this._tokenizer.location()); - } - list.push({ type: 'pair', key: item, value: value }); - } - else { - list.push(item); - } - this._tokenizer.eat(','); - this._tokenizer.eat('\n'); - if (this._tokenizer.eat('}')) { - break; - } - } - if (dict) { - return { type: 'dict', value: list }; - } - return { type: 'set', value: list }; - } - - _parseExpressions() { - const list = []; - this._tokenizer.expect('['); - while (!this._tokenizer.eat(']')) { - const expression = this._parseExpression(); - if (expression == null) { - throw new python.Error('Expected expression' + this._tokenizer.location()); - } - list.push(expression); - this._tokenizer.eat(','); - while (this._tokenizer.eat('\n')) { - // continue - } - if (this._tokenizer.eat(']')) { - break; - } - } - return { type: 'list', value: list }; - } - - _parseSlice() { - let node = { type: '::' }; - let list = []; - const group = [ 'start', 'stop', 'step' ]; - this._tokenizer.expect('['); - while (!this._tokenizer.eat(']')) { - if (this._tokenizer.eat(':')) { - node[group.shift()] = { type: 'list', value: list }; - list = []; - continue; - } - if (this._tokenizer.eat(',')) { - // list.push({}); - continue; - } - if (this._tokenizer.peek().value != ']') { - const expression = this._parseExpression(); - if (expression == null) { - throw new python.Error('Expected expression' + this._tokenizer.location()); - } - list.push(expression); - } - } - if (list.length > 0) { - node[group.shift()] = { type: 'list', value: list }; - } - if (node.start && !node.stop && !node.step) { - node = node.start; - } - return node; - } - - _parseName() { - const token = this._tokenizer.peek(); - if (token.type == 'id' && !token.keyword) { - this._tokenizer.read(); - return token; - } - return null; - } - - _parseLiteral() { - const token = this._tokenizer.peek(); - if (token.type == 'string' || token.type == 'number' || token.type == 'boolean') { - this._tokenizer.read(); - return token; - } - return null; - } - - _parseTypeArguments() { - const list = []; - this._tokenizer.expect('['); - while (!this._tokenizer.eat(']')) { - const type = this._parseType(); - if (type == null) { - throw new python.Error('Expected type ' + this._tokenizer.location()); - } - list.push(type); - if (!this._tokenizer.eat(',')) { - this._tokenizer.expect(']'); - break; - } - } - return list; - } - - _parseType() { - const type = this._node(); - type.type = 'type'; - type.name = this._parseExpression(-1, [ '[', '=' ]); - if (type.name) { - if (this._tokenizer.peek().value === '[') { - type.arguments = this._parseTypeArguments(); - } - return type; - } - return null; - } - - _parseParameter(terminal) { - const node = this._node('parameter'); - if (this._tokenizer.eat('/')) { - node.name = '/'; - return node; - } - if (this._tokenizer.eat('**')) { - node.parameterType = '**'; - } - if (this._tokenizer.eat('*')) { - node.parameterType = '*'; - } - const identifier = this._parseName(); - if (identifier !== null) { - node.name = identifier.value; - if (terminal !== ':' && this._tokenizer.eat(':')) { - node.parameterType = this._parseType(); - } - if (this._tokenizer.eat('=')) { - node.initializer = this._parseExpression(); - } - return node; - } - return null; - } - - _parseParameters(terminal) { - const list = []; - while (!this._tokenizer.eat(terminal)) { - this._tokenizer.eat('\n'); - if (this._tokenizer.eat('(')) { - list.push(this._parseParameters(')')); - } - else { - list.push(this._parseParameter(terminal)); - } - this._tokenizer.eat('\n'); - if (!this._tokenizer.eat(',')) { - this._tokenizer.expect(terminal); - break; - } - } - return list; - } - - _parseArguments() { - const list = []; - this._tokenizer.expect('('); - while (!this._tokenizer.eat(')')) { - if (this._tokenizer.eat('\n')) { - continue; - } - const expression = this._parseExpression(-1, [], false); - if (expression == null) { - throw new python.Error('Expected expression ' + this._tokenizer.location()); - } - list.push(expression); - if (!this._tokenizer.eat(',')) { - this._tokenizer.eat('\n'); - this._tokenizer.expect(')'); - break; - } - } - return list; - } - - _node(type) { - const node = {}; - node.location = this._tokenizer.location(); - if (type) { - node.type = type; - } - return node; - } - - _eat(type, value) { - if (this._tokenizer.match(type, value)) { - const node = this._node(type === 'id' ? value : type); - this._tokenizer.expect(type, value); - return node; - } - return null; - } -}; - -python.Tokenizer = class { - - constructor(text, file) { - this._text = text; - this._file = file; - this._position = 0; - this._lineStart = 0; - this._line = 0; - this._token = { type: '', value: '' }; - this._brackets = 0; - this._indentation = []; - this._outdent = 0; - if (!python.Tokenizer._whitespace) { - python.Tokenizer._whitespace = new RegExp('[\u1680\u180e\u2000-\u200a\u202f\u205f\u3000\ufeff]'); - const identifierStartChars = '\xaa\xb5\xba\xc0-\xd6\xd8-\xf6\xf8-\u02c1\u02c6-\u02d1\u02e0-\u02e4\u02ec\u02ee\u0370-\u0374\u0376\u0377\u037a-\u037d\u0386\u0388-\u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7-\u0481\u048a-\u0527\u0531-\u0556\u0559\u0561-\u0587\u05d0-\u05ea\u05f0-\u05f2\u0620-\u064a\u066e\u066f\u0671-\u06d3\u06d5\u06e5\u06e6\u06ee\u06ef\u06fa-\u06fc\u06ff\u0710\u0712-\u072f\u074d-\u07a5\u07b1\u07ca-\u07ea\u07f4\u07f5\u07fa\u0800-\u0815\u081a\u0824\u0828\u0840-\u0858\u08a0\u08a2-\u08ac\u0904-\u0939\u093d\u0950\u0958-\u0961\u0971-\u0977\u0979-\u097f\u0985-\u098c\u098f\u0990\u0993-\u09a8\u09aa-\u09b0\u09b2\u09b6-\u09b9\u09bd\u09ce\u09dc\u09dd\u09df-\u09e1\u09f0\u09f1\u0a05-\u0a0a\u0a0f\u0a10\u0a13-\u0a28\u0a2a-\u0a30\u0a32\u0a33\u0a35\u0a36\u0a38\u0a39\u0a59-\u0a5c\u0a5e\u0a72-\u0a74\u0a85-\u0a8d\u0a8f-\u0a91\u0a93-\u0aa8\u0aaa-\u0ab0\u0ab2\u0ab3\u0ab5-\u0ab9\u0abd\u0ad0\u0ae0\u0ae1\u0b05-\u0b0c\u0b0f\u0b10\u0b13-\u0b28\u0b2a-\u0b30\u0b32\u0b33\u0b35-\u0b39\u0b3d\u0b5c\u0b5d\u0b5f-\u0b61\u0b71\u0b83\u0b85-\u0b8a\u0b8e-\u0b90\u0b92-\u0b95\u0b99\u0b9a\u0b9c\u0b9e\u0b9f\u0ba3\u0ba4\u0ba8-\u0baa\u0bae-\u0bb9\u0bd0\u0c05-\u0c0c\u0c0e-\u0c10\u0c12-\u0c28\u0c2a-\u0c33\u0c35-\u0c39\u0c3d\u0c58\u0c59\u0c60\u0c61\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbd\u0cde\u0ce0\u0ce1\u0cf1\u0cf2\u0d05-\u0d0c\u0d0e-\u0d10\u0d12-\u0d3a\u0d3d\u0d4e\u0d60\u0d61\u0d7a-\u0d7f\u0d85-\u0d96\u0d9a-\u0db1\u0db3-\u0dbb\u0dbd\u0dc0-\u0dc6\u0e01-\u0e30\u0e32\u0e33\u0e40-\u0e46\u0e81\u0e82\u0e84\u0e87\u0e88\u0e8a\u0e8d\u0e94-\u0e97\u0e99-\u0e9f\u0ea1-\u0ea3\u0ea5\u0ea7\u0eaa\u0eab\u0ead-\u0eb0\u0eb2\u0eb3\u0ebd\u0ec0-\u0ec4\u0ec6\u0edc-\u0edf\u0f00\u0f40-\u0f47\u0f49-\u0f6c\u0f88-\u0f8c\u1000-\u102a\u103f\u1050-\u1055\u105a-\u105d\u1061\u1065\u1066\u106e-\u1070\u1075-\u1081\u108e\u10a0-\u10c5\u10c7\u10cd\u10d0-\u10fa\u10fc-\u1248\u124a-\u124d\u1250-\u1256\u1258\u125a-\u125d\u1260-\u1288\u128a-\u128d\u1290-\u12b0\u12b2-\u12b5\u12b8-\u12be\u12c0\u12c2-\u12c5\u12c8-\u12d6\u12d8-\u1310\u1312-\u1315\u1318-\u135a\u1380-\u138f\u13a0-\u13f4\u1401-\u166c\u166f-\u167f\u1681-\u169a\u16a0-\u16ea\u16ee-\u16f0\u1700-\u170c\u170e-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176c\u176e-\u1770\u1780-\u17b3\u17d7\u17dc\u1820-\u1877\u1880-\u18a8\u18aa\u18b0-\u18f5\u1900-\u191c\u1950-\u196d\u1970-\u1974\u1980-\u19ab\u19c1-\u19c7\u1a00-\u1a16\u1a20-\u1a54\u1aa7\u1b05-\u1b33\u1b45-\u1b4b\u1b83-\u1ba0\u1bae\u1baf\u1bba-\u1be5\u1c00-\u1c23\u1c4d-\u1c4f\u1c5a-\u1c7d\u1ce9-\u1cec\u1cee-\u1cf1\u1cf5\u1cf6\u1d00-\u1dbf\u1e00-\u1f15\u1f18-\u1f1d\u1f20-\u1f45\u1f48-\u1f4d\u1f50-\u1f57\u1f59\u1f5b\u1f5d\u1f5f-\u1f7d\u1f80-\u1fb4\u1fb6-\u1fbc\u1fbe\u1fc2-\u1fc4\u1fc6-\u1fcc\u1fd0-\u1fd3\u1fd6-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ff4\u1ff6-\u1ffc\u2071\u207f\u2090-\u209c\u2102\u2107\u210a-\u2113\u2115\u2119-\u211d\u2124\u2126\u2128\u212a-\u212d\u212f-\u2139\u213c-\u213f\u2145-\u2149\u214e\u2160-\u2188\u2c00-\u2c2e\u2c30-\u2c5e\u2c60-\u2ce4\u2ceb-\u2cee\u2cf2\u2cf3\u2d00-\u2d25\u2d27\u2d2d\u2d30-\u2d67\u2d6f\u2d80-\u2d96\u2da0-\u2da6\u2da8-\u2dae\u2db0-\u2db6\u2db8-\u2dbe\u2dc0-\u2dc6\u2dc8-\u2dce\u2dd0-\u2dd6\u2dd8-\u2dde\u2e2f\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303c\u3041-\u3096\u309d-\u309f\u30a1-\u30fa\u30fc-\u30ff\u3105-\u312d\u3131-\u318e\u31a0-\u31ba\u31f0-\u31ff\u3400-\u4db5\u4e00-\u9fcc\ua000-\ua48c\ua4d0-\ua4fd\ua500-\ua60c\ua610-\ua61f\ua62a\ua62b\ua640-\ua66e\ua67f-\ua697\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua78e\ua790-\ua793\ua7a0-\ua7aa\ua7f8-\ua801\ua803-\ua805\ua807-\ua80a\ua80c-\ua822\ua840-\ua873\ua882-\ua8b3\ua8f2-\ua8f7\ua8fb\ua90a-\ua925\ua930-\ua946\ua960-\ua97c\ua984-\ua9b2\ua9cf\uaa00-\uaa28\uaa40-\uaa42\uaa44-\uaa4b\uaa60-\uaa76\uaa7a\uaa80-\uaaaf\uaab1\uaab5\uaab6\uaab9-\uaabd\uaac0\uaac2\uaadb-\uaadd\uaae0-\uaaea\uaaf2-\uaaf4\uab01-\uab06\uab09-\uab0e\uab11-\uab16\uab20-\uab26\uab28-\uab2e\uabc0-\uabe2\uac00-\ud7a3\ud7b0-\ud7c6\ud7cb-\ud7fb\uf900-\ufa6d\ufa70-\ufad9\ufb00-\ufb06\ufb13-\ufb17\ufb1d\ufb1f-\ufb28\ufb2a-\ufb36\ufb38-\ufb3c\ufb3e\ufb40\ufb41\ufb43\ufb44\ufb46-\ufbb1\ufbd3-\ufd3d\ufd50-\ufd8f\ufd92-\ufdc7\ufdf0-\ufdfb\ufe70-\ufe74\ufe76-\ufefc\uff21-\uff3a\uff41-\uff5a\uff66-\uffbe\uffc2-\uffc7\uffca-\uffcf\uffd2-\uffd7\uffda-\uffdc'; - const identifierChars = '\u0300-\u036f\u0483-\u0487\u0591-\u05bd\u05bf\u05c1\u05c2\u05c4\u05c5\u05c7\u0610-\u061a\u0620-\u0649\u0672-\u06d3\u06e7-\u06e8\u06fb-\u06fc\u0730-\u074a\u0800-\u0814\u081b-\u0823\u0825-\u0827\u0829-\u082d\u0840-\u0857\u08e4-\u08fe\u0900-\u0903\u093a-\u093c\u093e-\u094f\u0951-\u0957\u0962-\u0963\u0966-\u096f\u0981-\u0983\u09bc\u09be-\u09c4\u09c7\u09c8\u09d7\u09df-\u09e0\u0a01-\u0a03\u0a3c\u0a3e-\u0a42\u0a47\u0a48\u0a4b-\u0a4d\u0a51\u0a66-\u0a71\u0a75\u0a81-\u0a83\u0abc\u0abe-\u0ac5\u0ac7-\u0ac9\u0acb-\u0acd\u0ae2-\u0ae3\u0ae6-\u0aef\u0b01-\u0b03\u0b3c\u0b3e-\u0b44\u0b47\u0b48\u0b4b-\u0b4d\u0b56\u0b57\u0b5f-\u0b60\u0b66-\u0b6f\u0b82\u0bbe-\u0bc2\u0bc6-\u0bc8\u0bca-\u0bcd\u0bd7\u0be6-\u0bef\u0c01-\u0c03\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c62-\u0c63\u0c66-\u0c6f\u0c82\u0c83\u0cbc\u0cbe-\u0cc4\u0cc6-\u0cc8\u0cca-\u0ccd\u0cd5\u0cd6\u0ce2-\u0ce3\u0ce6-\u0cef\u0d02\u0d03\u0d46-\u0d48\u0d57\u0d62-\u0d63\u0d66-\u0d6f\u0d82\u0d83\u0dca\u0dcf-\u0dd4\u0dd6\u0dd8-\u0ddf\u0df2\u0df3\u0e34-\u0e3a\u0e40-\u0e45\u0e50-\u0e59\u0eb4-\u0eb9\u0ec8-\u0ecd\u0ed0-\u0ed9\u0f18\u0f19\u0f20-\u0f29\u0f35\u0f37\u0f39\u0f41-\u0f47\u0f71-\u0f84\u0f86-\u0f87\u0f8d-\u0f97\u0f99-\u0fbc\u0fc6\u1000-\u1029\u1040-\u1049\u1067-\u106d\u1071-\u1074\u1082-\u108d\u108f-\u109d\u135d-\u135f\u170e-\u1710\u1720-\u1730\u1740-\u1750\u1772\u1773\u1780-\u17b2\u17dd\u17e0-\u17e9\u180b-\u180d\u1810-\u1819\u1920-\u192b\u1930-\u193b\u1951-\u196d\u19b0-\u19c0\u19c8-\u19c9\u19d0-\u19d9\u1a00-\u1a15\u1a20-\u1a53\u1a60-\u1a7c\u1a7f-\u1a89\u1a90-\u1a99\u1b46-\u1b4b\u1b50-\u1b59\u1b6b-\u1b73\u1bb0-\u1bb9\u1be6-\u1bf3\u1c00-\u1c22\u1c40-\u1c49\u1c5b-\u1c7d\u1cd0-\u1cd2\u1d00-\u1dbe\u1e01-\u1f15\u200c\u200d\u203f\u2040\u2054\u20d0-\u20dc\u20e1\u20e5-\u20f0\u2d81-\u2d96\u2de0-\u2dff\u3021-\u3028\u3099\u309a\ua640-\ua66d\ua674-\ua67d\ua69f\ua6f0-\ua6f1\ua7f8-\ua800\ua806\ua80b\ua823-\ua827\ua880-\ua881\ua8b4-\ua8c4\ua8d0-\ua8d9\ua8f3-\ua8f7\ua900-\ua909\ua926-\ua92d\ua930-\ua945\ua980-\ua983\ua9b3-\ua9c0\uaa00-\uaa27\uaa40-\uaa41\uaa4c-\uaa4d\uaa50-\uaa59\uaa7b\uaae0-\uaae9\uaaf2-\uaaf3\uabc0-\uabe1\uabec\uabed\uabf0-\uabf9\ufb20-\ufb28\ufe00-\ufe0f\ufe20-\ufe26\ufe33\ufe34\ufe4d-\ufe4f\uff10-\uff19\uff3f'; - python.Tokenizer._identifierStart = new RegExp('[' + identifierStartChars + ']'); - /* eslint-disable */ - python.Tokenizer._identifierChar = new RegExp('[' + identifierStartChars + identifierChars + ']'); - /* eslint-enable */ - } - } - - peek() { - if (!this._cache) { - this._token = this._tokenize(this._token); - this._cache = true; - } - return this._token; - } - - read() { - if (!this._cache) { - this._token = this._tokenize(this._token); - } - const next = this._position + this._token.value.length; - while (this._position < next) { - if (python.Tokenizer._isNewline(this._get(this._position))) { - this._position = this._newLine(this._position); - this._lineStart = this._position; - this._line++; - } - else { - this._position++; - } - } - this._cache = false; - return this._token; - } - - match(type, value) { - const token = this.peek(); - if (token.type === type && (!value || token.value === value)) { - return true; - } - return false; - } - - eat(type, value) { - const token = this.peek(); - if (token.type === type && (!value || token.value === value)) { - this.read(); - return true; - } - return false; - } - - expect(type, value) { - const token = this.peek(); - if (token.type !== type) { - throw new python.Error("Unexpected '" + token.value + "' instead of '" + type + "'" + this.location()); - } - if (value && token.value !== value) { - throw new python.Error("Unexpected '" + token.value + "' instead of '" + value + "'" + this.location()); - } - this.read(); - } - - location() { - return ' at ' + this._file + ':' + (this._line + 1).toString() + ':' + (this._position - this._lineStart + 1).toString(); - } - - static _isSpace(c) { - switch (c) { - case ' ': - case '\t': - case '\v': // 11 - case '\f': // 12 - case '\xA0': // 160 - return true; - default: - if (c.charCodeAt(0) >= 0x1680) { - return python.Tokenizer._whitespace.test(c); - } - return false; - } - } - - static _isNewline(c) { - switch(c) { - case '\n': - case '\r': - case '\u2028': // 8232 - case '\u2029': // 8233 - return true; - default: - return false; - } - } - - static _isIdentifierStartChar(c) { - if (c < 'A') { - return c === '$'; - } - if (c <= 'Z') { - return true; - } - if (c < 'a') { - return c === '_'; - } - if (c <= 'z') { - return true; - } - const code = c.charCodeAt(0); - if (code >= 0xAA) { - return python.Tokenizer._identifierStart.test(c); - } - return false; - } - - static _isIdentifierChar(c) { - if (c < '0') { - return c === '$'; - } - if (c <= '9') { - return true; - } - if (c < 'A') { - return false; - } - if (c <= 'Z') { - return true; - } - if (c < 'a') { - return c === '_'; - } - if (c <= 'z') { - return true; - } - const code = c.charCodeAt(0); - if (code >= 0xAA) { - return python.Tokenizer._identifierChar.test(c); - } - return false; - } - - static _isDecimal(c) { - return c >= '0' && c <= '9' || c === '_'; - } - - static _isHex(c) { - return python.Tokenizer._isDecimal(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') || c === '_'; - } - - static _isOctal(c) { - return c >= '0' && c <= '7' || c === '_'; - } - - static _isBinary(c) { - return c === '0' || c === '1' || c === '_'; - } - - _get(position) { - return position >= this._text.length ? '\0' : this._text[position]; - } - - _skipLine() { - while (this._position < this._text.length) { - if (python.Tokenizer._isNewline(this._get(this._position))) { - break; - } - this._position++; - } - } - - _skipWhitespace() { - while (this._position < this._text.length) { - const c = this._text[this._position]; - if (c == '#') { - this._skipLine(); - } - else if (python.Tokenizer._isSpace(c)) { - this._position++; - } - else if (c == '\\') { - // Explicit Line Continuation - this._position++; - if (python.Tokenizer._isNewline(this._get(this._position))) { - this._position = this._newLine(this._position); - this._lineStart = this._position; - this._line++; - } - else { - throw new python.Error("Unexpected '" + this._text[this._position] + "' after line continuation" + this.location()); - } - } - else if (this._brackets > 0 && python.Tokenizer._isNewline(c)) { - // Implicit Line Continuation - this._position = this._newLine(this._position); - this._lineStart = this._position; - this._line++; - } - else { - break; - } - } - } - - _newLine(position) { - if ((this._get(position) === '\n' && this._get(position + 1) === '\r') || - (this._get(position) === '\r' && this._get(position + 1) === '\n')) { - return position + 2; - } - return position + 1; - } - - _tokenize(token) { - if (this._token.type !== '\n') { - this._skipWhitespace(); - } - if (this._token.type === 'dedent') { - this._indentation.pop(); - this._outdent--; - if (this._outdent > 0) { - return { type: 'dedent', value: '' }; - } - } - if (token.type == '\n') { - let indent = ''; - let i = this._position; - while (i < this._text.length) { - const c = this._text[i]; - if (python.Tokenizer._isSpace(c)) { - indent += c; - i++; - } - else if (python.Tokenizer._isNewline(c)) { - indent = ''; - i = this._newLine(i); - this._position = i; - this._lineStart = i; - this._line++; - } - else if (c == '#') { - indent = ''; - while (i < this._text.length && !python.Tokenizer._isNewline(this._text[i])) { - i++; - } - continue; - } - else { - break; - } - } - let type = null; - if (indent.length > 0) { - const current = this._indentation.length > 0 ? this._indentation[this._indentation.length - 1] : ''; - if (indent.length > current.length) { - type = 'indent'; - this._indentation.push(indent); - } - else if (indent.length > 0 && indent.length < current.length) { - type = 'dedent'; - this._outdent = 0; - for (let j = this._indentation.length - 1; j >= 0 && indent.length < this._indentation[j].length; j--) { - this._outdent++; - } - } - else { - this._position += indent.length; - } - } - else if (i >= this._text.length) { - return { type: 'eof', value: '' }; - } - else if (this._indentation.length > 0) { - type = 'dedent'; - this._outdent = this._indentation.length; - } - if (type === 'indent' || type === 'dedent') { - return { type: type, value: indent }; - } - } - if (this._position >= this._text.length) { - return { type: 'eof', value: '' }; - } - const c = this._get(this._position); - const string = this._string(); - if (string) { - return string; - } - switch (c) { - case '(': - case '[': - case '{': - this._brackets++; - return { type: c, value: c }; - case ')': - case ']': - case '}': - if (this._brackets === 0) { - throw new python.Error("Unexpected '" + c + "'" + this.location); - } - this._brackets--; - return { type: c, value: c }; - case ',': - case ';': - case '?': - return { type: c, value: c }; - default: { - const number = this._number(); - if (number) { - return number; - } - if (c === '.') { - let end = this._position + 1; - while (this._get(end) === '.') { - end++; - } - const text = this._text.substring(this._position, end); - return { type: text, value: text }; - } - const identifier = this._identifier(); - if (identifier) { - return identifier; - } - const operator = this._operator(); - if (operator) { - return operator; - } - break; - } - } - if (c === '.') { - return { type: c, value: c }; - } - if (c === '\\') { - return { type: '\\', value: c }; - } - if (python.Tokenizer._isNewline(c)) { - return { type: '\n', value: this._text.substring(this._position, this._newLine(this._position)) }; - } - throw new python.Error("Unexpected token '" + c + "'" + this.location()); - } - - _number() { - let c = this._get(this._position); - const sign = (c === '-' || c === '+') ? 1 : 0; - let i = this._position + sign; - c = this._get(i); - if (c === '0') { - let radix = 0; - const n = this._get(i + 1); - if ((n === 'x' || n === 'X') && python.Tokenizer._isHex(this._get(i + 2))) { - i += 2; - while (python.Tokenizer._isHex(this._get(i))) { - i += 1; - } - if (this._get(i) === 'l' || this._get(i) === 'L') { - i += 1; - } - radix = 16; - } - else if ((n === 'b' || n === 'B') && python.Tokenizer._isBinary(this._get(i + 2))) { - i += 2; - while (python.Tokenizer._isBinary(this._get(i))) { - i++; - } - radix = 2; - } - else if ((n === 'o' || n === 'O') && python.Tokenizer._isOctal(this._get(i + 2))) { - i += 2; - while (python.Tokenizer._isOctal(this._get(i))) { - i++; - } - radix = 8; - } - else if (n >= '0' && n <= '7') { - i++; - while (python.Tokenizer._isOctal(this._get(i))) { - i += 1; - } - if (this._get(i) === 'l' || this._get(i) === 'L') { - i += 1; - } - radix = 8; - } - if (radix > 0 && this._get(i) !== '.') { - const radixText = this._text.substring(this._position, i); - const radixParseText = radixText.indexOf('_') !== -1 ? radixText.split('_').join('') : radixText; - if (!isNaN(parseInt(radixParseText, radix))) { - return { type: 'number', value: radixText }; - } - } - } - i = this._position + sign; - let decimal = false; - if (this._get(i) >= '1' && this._get(i) <= '9') { - while (python.Tokenizer._isDecimal(this._get(i))) { - i++; - } - c = this._get(i).toLowerCase(); - decimal = c !== '.' && c !== 'e'; - } - if (this._get(i) === '0') { - i++; - c = this._get(i).toLowerCase(); - decimal = !python.Tokenizer._isDecimal(c) && c !== '.' && c !== 'e' && c !== 'j'; - } - if (decimal) { - if (this._get(i) === 'j' || this._get(i) === 'J' || this._get(i) === 'l' || this._get(i) === 'L') { - return { 'type': 'number', value: this._text.substring(this._position, i + 1) }; - } - const intText = this._text.substring(this._position, i); - if (!isNaN(parseInt(intText, 10))) { - return { type: 'number', value: intText }; - } - } - i = this._position + sign; - if ((this._get(i) >= '0' && this._get(i) <= '9') || - (this._get(i) === '.' && this._get(i + 1) >= '0' && this._get(i + 1) <= '9')) { - while (python.Tokenizer._isDecimal(this._get(i))) { - i++; - } - if (this._get(i) === '.') { - i++; - } - while (python.Tokenizer._isDecimal(this._get(i))) { - i++; - } - if (i > (this._position + sign)) { - if (this._get(i) === 'e' || this._get(i) === 'E') { - i++; - if (this._get(i) == '-' || this._get(i) == '+') { - i++; - } - if (!python.Tokenizer._isDecimal(this._get(i))) { - i = this._position; - } - else { - while (python.Tokenizer._isDecimal(this._get(i))) { - i++; - } - } - } - else { - while (python.Tokenizer._isDecimal(this._get(i))) { - i++; - } - } - } - if (i > (this._position + sign)) { - if (this._get(i) === 'j' || this._get(i) === 'J') { - return { type: 'number', value: this._text.substring(this._position, i + 1) }; - } - const floatText = this._text.substring(this._position, i); - const floatParseText = floatText.indexOf('_') != -1 ? floatText.split('_').join('') : floatText; - if (!isNaN(parseFloat(floatParseText))) { - return { type: 'number', value: floatText }; - } - } - } - i = this._position + sign; - if (this._get(i) === 'i' && this._get(i + 1) === 'n' && this._get(i + 2) === 'f' && !python.Tokenizer._isIdentifierChar(this._get(i + 3))) { - return { type: 'number', value: this._text.substring(this._position, i + 3) }; - } - return null; - } - - _identifier() { - let i = this._position; - if (python.Tokenizer._isIdentifierStartChar(this._get(i))) { - i++; - while (python.Tokenizer._isIdentifierChar(this._get(i))) { - i++; - } - } - if (i > this._position) { - const text = this._text.substring(this._position, i); - return { type: 'id', value: text, keyword: python.Tokenizer._isKeyword(text) }; - } - return null; - } - - _operator() { - let length = 0; - const c0 = this._get(this._position); - const c1 = this._get(this._position + 1); - const c2 = this._get(this._position + 2); - switch (c0) { - case '+': - case '&': - case '|': - case '^': - case '=': - case '!': - case '%': - case '~': - length = c1 === '=' ? 2 : 1; - break; - case '-': - length = c1 === '=' || c1 === '>' ? 2 : 1; - break; - case '*': - if (c1 === '*') { - length = c2 === '=' ? 3 : 2; - } - else { - length = c1 === '=' ? 2 : 1; - } - break; - case '/': - if (c1 === '/') { - length = c2 === '=' ? 3 : 2; - } - else { - length = c1 === '=' ? 2 : 1; - } - break; - case '<': - if (c1 === '>') { - length = 2; - } - else if (c1 === '<') { - length = c2 === '=' ? 3 : 2; - } - else { - length = c1 === '=' ? 2 : 1; - } - break; - case '>': - if (c1 === '>') { - length = c2 === '=' ? 3 : 2; - } - else { - length = c1 === '=' ? 2 : 1; - } - break; - case '@': - length = c1 === '=' ? 2 : 1; - break; - case ':': - length = c1 === '=' ? 2 : 1; - break; - default: - return null; - } - const text = this._text.substring(this._position, this._position + length); - return { type: text, value: text }; - } - - _string() { - let i = this._position; - let prefix = -1; - if (this._get(i) === "'" || this._get(i) === '"') { - prefix = ''; - } - else if (this._get(i + 1) === "'" || this._get(i + 1) === '"') { - const c = this._get(i); - switch (c.toLowerCase()) { - case 'b': - case 'f': - case 'r': - case 'u': - prefix = c; - break; - default: - break; - } - } - else if (this._get(i + 2) === "'" || this._get(i + 2) === '"') { - const cc = this._text.substr(this._position, 2); - switch (cc.toLowerCase()) { - case 'br': - case 'fr': - case 'rb': - case 'rf': - case 'ur': - prefix = cc; - break; - default: - break; - } - } - if (prefix.length >= 0) { - i += prefix.length; - let quote = ''; - let count = 0; - const q0 = this._get(i); - const q1 = this._get(i + 1); - const q2 = this._get(i + 2); - switch (q0) { - case "'": - quote = q0; - count = (q1 === "'" && q2 === "'") ? 3 : 1; - break; - case '"': - quote = q0; - count = (q1 === '"' && q2 === '"') ? 3 : 1; - break; - default: - throw new python.Error("Unsupported string quote '" + q0 + "'."); - } - i += count; - if (count == 1) { - while (i < this._text.length) { - if (this._text[i] === quote) { - return { type: 'string', value: this._text.substring(this._position, i + 1) }; - } - else if (this._text[i] === '\\' && - (this._get(i + 1) == quote || this._get(i + 1) == '\n' || this._get(i + 1) == '\\')) { - i += 2; - } - else if (this._text[i] === '\r' || this._text[i] === '\n') { - break; - } - else { - i++; - } - } - } - else if (count == 3) { - while (i < this._text.length) { - if (this._get(i) === quote && this._get(i + 1) === quote && this._get(i + 2) === quote) { - return { type: 'string', value: this._text.substring(this._position, i + 3) }; - } - else if (this._get(i) === '\\' && this._get(i + 1) === quote) { - i += 2; - continue; - } - i++; - } - } - } - i = this._position; - if (this._get(i) === '`') { - i++; - while (i < this._text.length) { - if (this._text[i] === '`') { - return { type: 'string', value: this._text.substring(this._position, i + 1) }; - } - i++; - } - } - return null; - } - - static _isKeyword(value) { - switch (value) { - case 'and': - case 'as': - case 'else': - case 'for': - case 'if': - case 'import': - case 'in': - case 'is': - case 'not': - case 'or': - return true; - default: - return false; - } - } -}; - -python.Execution = class { - - constructor(sources, exceptionCallback) { - const self = this; - this._sources = sources || new Map(); - this._exceptionCallback = exceptionCallback; - this._utf8Decoder = new TextDecoder('utf-8'); - this._packages = new Map(); - this._unknownNameMap = new Set(); - this._context = new python.Execution.Context(); - this._context.scope.builtins = {}; - this._context.scope.builtins.type = { __module__: 'builtins', __name__: 'type' }; - this._context.scope.builtins.type.__class__ = this._context.scope.builtins.type; - this._context.scope.builtins.module = { __module__: 'builtins', __name__: 'module', __class__: this._context.scope.builtins.type }; - this._context.scope.builtins.module.__type__ = this._context.scope.builtins.module; - this.registerModule('__builtin__'); - this.registerModule('_codecs'); - this.registerModule('argparse'); - this.registerModule('collections'); - this.registerModule('copy_reg'); - this.registerModule('cuml'); - this.registerModule('gensim'); - this.registerModule('io'); - this.registerModule('joblib'); - this.registerModule('keras'); - this.registerModule('lightgbm'); - this.registerModule('numpy'); - this.registerModule('nolearn'); - this.registerModule('sklearn'); - this.registerModule('typing'); - this.registerModule('xgboost'); - const builtins = this._context.scope.builtins; - const numpy = this._context.scope.numpy; - const typing = this._context.scope.typing; - this.registerType('builtins.function', class {}); - this.registerType('builtins.method', class {}); - this.registerType('builtins.dict', class {}); - this.registerType('builtins.list', class {}); - this.registerType('builtins.bool', class {}); - this.registerType('builtins.int', class {}); - this.registerType('builtins.float', class {}); - this.registerType('builtins.object', class {}); - this.registerType('builtins.str', class {}); - this.registerType('builtins.tuple', class {}); - this.registerType('typing._Final', class {}); - this.registerType('typing._SpecialForm', class extends typing._Final {}); - this.registerType('typing._BaseGenericAlias', class extends typing._Final {}); - this.registerType('typing._GenericAlias', class extends typing._BaseGenericAlias {}); - this.registerType('typing._SpecialGenericAlias', class extends typing._BaseGenericAlias {}); - this.registerType('typing._TupleType', class extends typing._SpecialGenericAlias {}); - typing.Optional = self.invoke('typing._SpecialForm', []); - typing.List = self.invoke('typing._SpecialGenericAlias', []); - typing.Dict = self.invoke('typing._SpecialGenericAlias', []); - typing.Tuple = self.invoke('typing._TupleType', []); - this.registerType('argparse.Namespace', class { - constructor(args) { - this.args = args; - } - }); - this.registerType('collections.deque', class { - constructor(iterable) { - if (iterable) { - let i = 0; - for (const value of iterable) { - this[i++] = value; - } - this.length = i; - } - } - }); - this.registerType('collections.OrderedDict', class extends Map { - constructor(items) { - super(); - if (items) { - for (const pair of items) { - this.__setitem__(pair[0], pair[1]); - } - } - } - __setitem__(key, value) { - this.set(key, value); - } - }); - this.registerType('cuml.common.array_descriptor.CumlArrayDescriptorMeta', class {}); - this.registerType('cuml.ensemble.randomforestclassifier.RandomForestClassifier', class {}); - this.registerType('cuml.raft.common.handle.Handle', class { - __setstate__(state) { - this._handle = state; - } - }); - this.registerType('haiku._src.data_structures.FlatMapping', class { - constructor(dict) { - for (const key of Object.keys(dict)) { - this[key] = dict[key]; - } - } - }); - this.registerType('io.BytesIO', class { - constructor(buf, mode) { - this.mode = mode || 'r'; - this._buf = this.mode === 'w' ? null : buf; - this._point = 0; - } - seek(offset) { - this._point = offset; - } - read(size) { - const start = this._point; - this._point = size !== undefined ? start + size : this._buf.length; - return this._buf.subarray(start, this._point); - } - write(data) { - const src = this._buf || new Uint8Array(); - this._point = src.length + data.length; - this._buf = new Uint8Array(this._point); - this._buf.set(src, 0); - this._buf.set(data, src.length); - } - }); - this.registerType('numpy.dtype', class { - constructor(obj, align, copy) { - switch (obj) { - case 'b1': case 'bool': this.name = 'bool'; this.itemsize = 1; this.kind = 'b'; break; - case 'i1': case 'int8': this.name = 'int8'; this.itemsize = 1; this.kind = 'i'; break; - case 'i2': case 'int16': this.name = 'int16'; this.itemsize = 2; this.kind = 'i'; break; - case 'i4': case 'int32': this.name = 'int32'; this.itemsize = 4; this.kind = 'i'; break; - case 'i8': case 'int64': case 'int': this.name = 'int64'; this.itemsize = 8; this.kind = 'i'; break; - case 'u1': case 'uint8': this.name = 'uint8'; this.itemsize = 1; this.kind = 'u'; break; - case 'u2': case 'uint16': this.name = 'uint16'; this.itemsize = 2; this.kind = 'u'; break; - case 'u4': case 'uint32': this.name = 'uint32'; this.itemsize = 4; this.kind = 'u'; break; - case 'u8': case 'uint64': case 'uint': this.name = 'uint64'; this.itemsize = 8; this.kind = 'u'; break; - case 'f2': case 'float16': this.name = 'float16'; this.itemsize = 2; this.kind = 'f'; break; - case 'f4': case 'float32': this.name = 'float32'; this.itemsize = 4; this.kind = 'f'; break; - case 'f8': case 'float64': case 'float': this.name = 'float64'; this.itemsize = 8; this.kind = 'f'; break; - case 'c8': case 'complex64': this.name = 'complex64'; this.itemsize = 8; this.kind = 'c'; break; - case 'c16': case 'complex128': case 'complex': this.name = 'complex128'; this.itemsize = 16; this.kind = 'c'; break; - default: - if (obj.startsWith('V')) { - this.itemsize = parseInt(obj.substring(1), 10); - this.kind = 'V'; - this.name = 'void' + (this.itemsize * 8).toString(); - } - else if (obj.startsWith('O')) { - this.itemsize = parseInt(obj.substring(1), 10); - this.kind = 'O'; - this.name = 'object'; - } - else if (obj.startsWith('S')) { - this.itemsize = parseInt(obj.substring(1), 10); - this.kind = 'S'; - this.name = 'string'; - } - else if (obj.startsWith('U')) { // Unicode string - this.itemsize = 4 * parseInt(obj.substring(1), 10); - this.kind = 'U'; - this.name = 'string'; // 'str' + (8 * this.itemsize) - } - else if (obj.startsWith('M')) { // datetime - this.itemsize = parseInt(obj.substring(1), 10); - this.kind = 'M'; - this.name = 'datetime'; - } - else { - throw new python.Error("Unsupported dtype '" + obj.toString() + "'."); - } - break; - } - this.byteorder = '='; - if (align) { - this.align = align; - } - if (copy) { - this.copy = copy; - } - } - get str() { - return (this.byteorder === '=' ? '<' : this.byteorder) + this.kind + this.itemsize.toString(); - } - __setstate__(state) { - switch (state.length) { - case 8: - this.version = state[0]; - this.byteorder = state[1]; - this.subarray = state[2]; - this.names = state[3]; - this.fields = state[4]; - this.elsize = state[5]; - this.alignment = state[6]; - this.int_dtypeflags = state[7]; - break; - case 9: - this.version = state[0]; - this.byteorder = state[1]; - this.subarray = state[2]; - this.names = state[3]; - this.fields = state[4]; - this.elsize = state[5]; - this.alignment = state[6]; - this.int_dtypeflags = state[7]; - this.metadata = state[8]; - break; - default: - throw new python.Error("Unsupported numpy.dtype setstate length '" + state.length.toString() + "'."); - } - } - }); - this.registerType('gensim.models.doc2vec.Doctag', class {}); - this.registerType('gensim.models.doc2vec.Doc2Vec', class {}); - this.registerType('gensim.models.doc2vec.Doc2VecTrainables', class {}); - this.registerType('gensim.models.doc2vec.Doc2VecVocab', class {}); - this.registerType('gensim.models.fasttext.FastText', class {}); - this.registerType('gensim.models.fasttext.FastTextTrainables', class {}); - this.registerType('gensim.models.fasttext.FastTextVocab', class {}); - this.registerType('gensim.models.fasttext.FastTextKeyedVectors', class {}); - this.registerType('gensim.models.keyedvectors.Doc2VecKeyedVectors', class {}); - this.registerType('gensim.models.keyedvectors.FastTextKeyedVectors', class {}); - this.registerType('gensim.models.keyedvectors.KeyedVectors', class {}); - this.registerType('gensim.models.keyedvectors.Vocab', class {}); - this.registerType('gensim.models.keyedvectors.Word2VecKeyedVectors', class {}); - this.registerType('gensim.models.phrases.Phrases', class {}); - this.registerType('gensim.models.tfidfmodel.TfidfModel', class {}); - this.registerType('gensim.models.word2vec.Vocab', class {}); - this.registerType('gensim.models.word2vec.Word2Vec', class {}); - this.registerType('gensim.models.word2vec.Word2VecTrainables', class {}); - this.registerType('gensim.models.word2vec.Word2VecVocab', class {}); - this.registerType('joblib.numpy_pickle.NumpyArrayWrapper', class { - constructor(/* subtype, shape, dtype */) { - } - __setstate__(state) { - this.subclass = state.subclass; - this.dtype = state.dtype; - this.shape = state.shape; - this.order = state.order; - this.allow_mmap = state.allow_mmap; - } - __read__(unpickler) { - if (this.dtype.name == 'object') { - return unpickler.load((name, args) => self.invoke(name, args), null); - } - else { - const size = this.dtype.itemsize * this.shape.reduce((a, b) => a * b, 1); - this.data = unpickler.read(size); - } - return self.invoke(this.subclass, [ this.shape, this.dtype, this.data ]); - } - }); - this.registerType('keras.engine.sequential.Sequential', class {}); - this.registerType('lightgbm.sklearn.LGBMRegressor', class {}); - this.registerType('lightgbm.sklearn.LGBMClassifier', class {}); - this.registerType('lightgbm.basic.Booster', class { - constructor() { - this.average_output = false; - this.models = []; - this.loaded_parameter = ''; - } - __setstate__(state) { - if (typeof state.handle === 'string') { - this.LoadModelFromString(state.handle); - return; - } - Object.assign(this, state); - } - LoadModelFromString(model_str) { - const lines = model_str.split('\n'); - const signature = lines.shift() || '?'; - if (signature.trim() !== 'tree') { - throw new python.Error("Invalid signature '" + signature.trim() + "'."); - } - // GBDT::LoadModelFromString() in https://github.com/microsoft/LightGBM/blob/master/src/boosting/gbdt_model_text.cpp - const key_vals = new Map(); - while (lines.length > 0 && !lines[0].startsWith('Tree=')) { - const cur_line = lines.shift().trim(); - if (cur_line.length > 0) { - const strs = cur_line.split('='); - if (strs.length === 1) { - key_vals.set(strs[0], ''); - } - else if (strs.length === 2) { - key_vals.set(strs[0], strs[1]); - } - else if (strs.length > 2) { - if (strs[0] === "feature_names") { - key_vals.set(strs[0], cur_line.substring("feature_names=".length)); - } - else if (strs[0] == 'monotone_constraints') { - key_vals.set(strs[0], cur_line.substring('monotone_constraints='.length)); - } - else { - throw new python.Error('Wrong line: ' + cur_line.substring(0, Math.min(128, cur_line.length))); - } - } - } - } - const atoi = (key, value) => { - if (key_vals.has(key)) { - return parseInt(key_vals.get(key), 10); - } - if (value !== undefined) { - return value; - } - throw new python.Error('Model file does not specify ' + key + '.'); - }; - const list = (key, size) => { - if (key_vals.has(key)) { - const value = key_vals.get(key).split(' '); - if (value.length !== size) { - throw new python.Error('Wrong size of ' + key + '.'); - } - return value; - } - throw new python.Error('Model file does not contain ' + key + '.'); - }; - this.version = key_vals.get('version') || ''; - this.num_class = atoi('num_class'); - this.num_tree_per_iteration = atoi('num_tree_per_iteration', this.num_class); - this.label_index = atoi('label_index'); - this.max_feature_idx = atoi('max_feature_idx'); - if (key_vals.has('average_output')) { - this.average_output = true; - } - this.feature_names = list('feature_names', this.max_feature_idx + 1); - this.feature_infos = list('feature_infos', this.max_feature_idx + 1); - if (key_vals.has('monotone_constraints')) { - this.monotone_constraints = list('monotone_constraints', this.max_feature_idx + 1, true); - } - if (key_vals.has('objective')) { - this.objective = key_vals.get('objective'); - } - let tree = null; - // let lineNumber = 0; - while (lines.length > 0) { - // lineNumber++; - const text = lines.shift(); - const line = text.trim(); - if (line.length === 0) { - continue; - } - if (line.startsWith('Tree=')) { - tree = { index: parseInt(line.split('=').pop(), 10) }; - this.models.push(tree); - continue; - } - if (line === 'end of trees') { - break; - } - const param = line.split('='); - if (param.length !== 2) { - throw new python.Error("Invalid property '" + line + "'."); - } - const name = param[0].trim(); - const value = param[1].trim(); - tree[name] = value; - } - const ss = []; - let is_inparameter = false; - while (lines.length > 0) { - const text = lines.shift(); - const line = text.trim(); - if (line === 'parameters:') { - is_inparameter = true; - continue; - } - else if (line === 'end of parameters') { - break; - } - else if (is_inparameter) { - ss.push(line); - } - } - if (ss.length > 0) { - this.loaded_parameter = ss.join('\n'); - } - } - }); - this.registerType('nolearn.lasagne.base.BatchIterator', class {}); - this.registerType('nolearn.lasagne.base.Layers', class {}); - this.registerType('nolearn.lasagne.base.NeuralNet', class {}); - this.registerType('nolearn.lasagne.base.TrainSplit', class {}); - this.registerType('nolearn.lasagne.handlers.PrintLayerInfo', class {}); - this.registerType('nolearn.lasagne.handlers.PrintLog', class {}); - this.registerType('numpy.ndarray', class { - constructor(shape, dtype, buffer, offset, strides, order) { - this.shape = shape; - this.dtype = dtype; - this.data = buffer !== undefined ? buffer : null; - this.offset = offset !== undefined ? offset : 0; - this.strides = strides !== undefined ? strides : null; - this.order = offset !== undefined ? order : null; - this.flags = {}; - } - __setstate__(state) { - this.version = state[0]; - this.shape = state[1]; - this.dtype = state[2]; - this.flags.fnc = state[3]; - this.data = state[4]; - } - __read__(unpickler) { - const dims = (this.shape || []).reduce((a, b) => a * b, 1); - const size = this.dtype.itemsize * dims; - if (typeof this.data == 'string') { - this.data = unpickler.unescape(this.data, size); - if (this.data.length != size) { - throw new python.Error('Invalid string array data size.'); - } - } - else { - if (this.data.length != size) { - // throw new pytorch.Error('Invalid array data size.'); - } - } - return this; - } - tobytes() { - return this.data; - } - }); - this.registerType('numpy.ma.core.MaskedArray', class extends numpy.ndarray { - constructor(data /*, mask, dtype, copy, subok, ndmin, fill_value, keep_mask, hard_mask, shrink, order */) { - super(data.shape, data.dtype, data.data); - } - }); - this.registerType('numpy.core.memmap.memmap', class extends numpy.ndarray { - constructor(shape, dtype) { - super(shape, dtype); - } - }); - this.registerType('pathlib.PosixPath', class { - constructor() { - this.path = Array.from(arguments).join('/'); - } - }); - this.registerType('sklearn.calibration._CalibratedClassifier', class {}); - this.registerType('sklearn.calibration._SigmoidCalibration', class {}); - this.registerType('sklearn.calibration.CalibratedClassifierCV', class {}); - this.registerType('sklearn.compose._column_transformer.ColumnTransformer', class {}); - this.registerType('sklearn.compose._target.TransformedTargetRegressor', class {}); - this.registerType('sklearn.cluster._dbscan.DBSCAN', class {}); - this.registerType('sklearn.cluster._kmeans.KMeans', class {}); - this.registerType('sklearn.decomposition._pca.PCA', class {}); - this.registerType('sklearn.decomposition.PCA', class {}); - this.registerType('sklearn.decomposition.pca.PCA', class {}); - this.registerType('sklearn.decomposition._truncated_svd.TruncatedSVD', class {}); - this.registerType('sklearn.decomposition.truncated_svd.TruncatedSVD', class {}); - this.registerType('sklearn.discriminant_analysis.LinearDiscriminantAnalysis', class {}); - this.registerType('sklearn.discriminant_analysis.QuadraticDiscriminantAnalysis', class {}); - this.registerType('sklearn.dummy.DummyClassifier', class {}); - this.registerType('sklearn.dummy.DummyRegressor', class {}); - this.registerType('sklearn.externals.joblib.numpy_pickle.NumpyArrayWrapper', class { - constructor(/* subtype, shape, dtype */) { - } - __setstate__(state) { - this.subclass = state.subclass; - this.dtype = state.dtype; - this.shape = state.shape; - this.order = state.order; - this.allow_mmap = state.allow_mmap; - } - __read__(unpickler) { - if (this.dtype.name == 'object') { - return unpickler.load((name, args) => self.invoke(name, args), null); - } - else { - const size = this.dtype.itemsize * this.shape.reduce((a, b) => a * b, 1); - this.data = unpickler.read(size); - } - return self.invoke(this.subclass, [ this.shape, this.dtype, this.data ]); - } - }); - this.registerType('sklearn.externals.joblib.numpy_pickle.NDArrayWrapper', class { - constructor(/* subtype, shape, dtype */) { - } - __setstate__(state) { - this.subclass = state.subclass; - this.filename = state.state; - this.allow_mmap = state.allow_mmap; - } - __read__(/* unpickler */) { - return this; // return self.invoke(this.subclass, [ this.shape, this.dtype, this.data ]); - } - }); - this.registerType('sklearn.ensemble._bagging.BaggingClassifier', class {}); - this.registerType('sklearn.ensemble._forest.RandomForestRegressor', class {}); - this.registerType('sklearn.ensemble._forest.RandomForestClassifier', class {}); - this.registerType('sklearn.ensemble._forest.ExtraTreesClassifier', class {}); - this.registerType('sklearn.ensemble._gb_losses.BinomialDeviance', class {}); - this.registerType('sklearn.ensemble._gb_losses.LeastSquaresError', class {}); - this.registerType('sklearn.ensemble._gb_losses.MultinomialDeviance', class {}); - this.registerType('sklearn.ensemble._gb.GradientBoostingClassifier', class {}); - this.registerType('sklearn.ensemble._gb.GradientBoostingRegressor', class {}); - this.registerType('sklearn.ensemble._iforest.IsolationForest', class {}); - this.registerType('sklearn.ensemble._stacking.StackingClassifier', class {}); - this.registerType('sklearn.ensemble._voting.VotingClassifier', class {}); - this.registerType('sklearn.ensemble.forest.RandomForestClassifier', class {}); - this.registerType('sklearn.ensemble.forest.RandomForestRegressor', class {}); - this.registerType('sklearn.ensemble.forest.ExtraTreesClassifier', class {}); - this.registerType('sklearn.ensemble.gradient_boosting.BinomialDeviance', class {}); - this.registerType('sklearn.ensemble.gradient_boosting.GradientBoostingClassifier', class {}); - this.registerType('sklearn.ensemble.gradient_boosting.LogOddsEstimator', class {}); - this.registerType('sklearn.ensemble.gradient_boosting.MultinomialDeviance', class {}); - this.registerType('sklearn.ensemble.gradient_boosting.PriorProbabilityEstimator', class {}); - this.registerType('sklearn.ensemble.weight_boosting.AdaBoostClassifier', class {}); - this.registerType('sklearn.feature_extraction._hashing.FeatureHasher', class {}); - this.registerType('sklearn.feature_extraction.text.CountVectorizer', class {}); - this.registerType('sklearn.feature_extraction.text.HashingVectorizer', class {}); - this.registerType('sklearn.feature_extraction.text.TfidfTransformer', class {}); - this.registerType('sklearn.feature_extraction.text.TfidfVectorizer', class {}); - this.registerType('sklearn.feature_selection._from_model.SelectFromModel', class {}); - this.registerType('sklearn.feature_selection._univariate_selection.SelectKBest', class {}); - this.registerType('sklearn.feature_selection._univariate_selection.SelectPercentile', class {}); - this.registerType('sklearn.feature_selection._variance_threshold.VarianceThreshold', class {}); - this.registerType('sklearn.feature_selection.univariate_selection.SelectKBest', class {}); - this.registerType('sklearn.feature_selection.variance_threshold.VarianceThreshold', class {}); - this.registerType('sklearn.gaussian_process.gpc.GaussianProcessClassifier', class {}); - this.registerType('sklearn.gaussian_process.kernels.ConstantKernel', class {}); - this.registerType('sklearn.gaussian_process.kernels.Product', class {}); - this.registerType('sklearn.gaussian_process.kernels.RBF', class {}); - this.registerType('sklearn.grid_search._CVScoreTuple', class {}); - this.registerType('sklearn.grid_search.GridSearchCV', class {}); - this.registerType('sklearn.impute._base.SimpleImputer', class {}); - this.registerType('sklearn.impute.SimpleImputer', class {}); - this.registerType('sklearn.isotonic.IsotonicRegression', class {}); - this.registerType('sklearn.linear_model._base.LinearRegression', class {}); - this.registerType('sklearn.linear_model._bayes.BayesianRidge', class {}); - this.registerType('sklearn.linear_model._coordinate_descent.ElasticNetCV', class {}); - this.registerType('sklearn.linear_model._coordinate_descent.ElasticNet', class {}); - this.registerType('sklearn.linear_model._logistic.LogisticRegression', class {}); - this.registerType('sklearn.linear_model._ridge.Ridge', class {}); - this.registerType('sklearn.linear_model._sgd_fast.Hinge', class {}); - this.registerType('sklearn.linear_model._sgd_fast.Log', class {}); - this.registerType('sklearn.linear_model._sgd_fast.ModifiedHuber', class {}); - this.registerType('sklearn.linear_model._sgd_fast.SquaredHinge', class {}); - this.registerType('sklearn.linear_model._stochastic_gradient.SGDClassifier', class {}); - this.registerType('sklearn.linear_model.base.LinearRegression', class {}); - this.registerType('sklearn.linear_model.sgd_fast.Hinge', class {}); - this.registerType('sklearn.linear_model.LogisticRegression', class {}); - this.registerType('sklearn.linear_model.logistic.LogisticRegression', class {}); - this.registerType('sklearn.linear_model.logistic.LogisticRegressionCV', class {}); - this.registerType('sklearn.linear_model.LassoLars​', class {}); - this.registerType('sklearn.linear_model.ridge.Ridge', class {}); - this.registerType('sklearn.linear_model.sgd_fast.Log', class {}); - this.registerType('sklearn.linear_model.stochastic_gradient.SGDClassifier', class {}); - this.registerType('sklearn.metrics._scorer._PredictScorer', class {}); - this.registerType('sklearn.metrics.scorer._PredictScorer', class {}); - this.registerType('sklearn.metrics._scorer._ThresholdScorer', class {}); - this.registerType('sklearn.mixture._bayesian_mixture.BayesianGaussianMixture', class {}); - this.registerType('sklearn.model_selection._search.GridSearchCV', class {}); - this.registerType('sklearn.model_selection._search.RandomizedSearchCV', class {}); - this.registerType('sklearn.model_selection._split.KFold', class {}); - this.registerType('sklearn.model_selection._split.StratifiedKFold', class {}); - this.registerType('sklearn.multiclass.OneVsRestClassifier', class {}); - this.registerType('sklearn.multioutput.MultiOutputClassifier', class {}); - this.registerType('sklearn.multioutput.MultiOutputRegressor', class {}); - this.registerType('sklearn.naive_bayes.BernoulliNB', class {}); - this.registerType('sklearn.naive_bayes.ComplementNB', class {}); - this.registerType('sklearn.naive_bayes.GaussianNB', class {}); - this.registerType('sklearn.naive_bayes.MultinomialNB', class {}); - this.registerType('sklearn.neighbors._classification.KNeighborsClassifier', class {}); - this.registerType('sklearn.neighbors._dist_metrics.newObj', class {}); - this.registerType('sklearn.neighbors._kd_tree.newObj', class {}); - this.registerType('sklearn.neighbors._regression.KNeighborsRegressor', class {}); - this.registerType('sklearn.neighbors.classification.KNeighborsClassifier', class {}); - this.registerType('sklearn.neighbors.dist_metrics.newObj', class {}); - this.registerType('sklearn.neighbors.kd_tree.newObj', class {}); - this.registerType('sklearn.neighbors.KNeighborsClassifier', class {}); - this.registerType('sklearn.neighbors.KNeighborsRegressor', class {}); - this.registerType('sklearn.neighbors.regression.KNeighborsRegressor', class {}); - this.registerType('sklearn.neighbors.unsupervised.NearestNeighbors', class {}); - this.registerType('sklearn.neural_network._multilayer_perceptron.MLPClassifier', class {}); - this.registerType('sklearn.neural_network._multilayer_perceptron.MLPRegressor', class {}); - this.registerType('sklearn.neural_network._stochastic_optimizers.AdamOptimizer', class {}); - this.registerType('sklearn.neural_network._stochastic_optimizers.SGDOptimizer', class {}); - this.registerType('sklearn.neural_network.rbm.BernoulliRBM', class {}); - this.registerType('sklearn.neural_network.multilayer_perceptron.MLPClassifier', class {}); - this.registerType('sklearn.neural_network.multilayer_perceptron.MLPRegressor', class {}); - this.registerType('sklearn.neural_network.stochastic_gradient.SGDClassifier', class {}); - this.registerType('sklearn.pipeline.Pipeline', class {}); - this.registerType('sklearn.pipeline.FeatureUnion', class {}); - this.registerType('sklearn.preprocessing._data.MinMaxScaler', class {}); - this.registerType('sklearn.preprocessing._data.MaxAbsScaler', class {}); - this.registerType('sklearn.preprocessing._data.Normalizer', class {}); - this.registerType('sklearn.preprocessing._data.PolynomialFeatures', class {}); - this.registerType('sklearn.preprocessing._data.QuantileTransformer', class {}); - this.registerType('sklearn.preprocessing._data.RobustScaler', class {}); - this.registerType('sklearn.preprocessing._data.StandardScaler', class {}); - this.registerType('sklearn.preprocessing._discretization.KBinsDiscretizer', class {}); - this.registerType('sklearn.preprocessing._encoders.OneHotEncoder', class {}); - this.registerType('sklearn.preprocessing._function_transformer.FunctionTransformer', class {}); - this.registerType('sklearn.preprocessing._label.LabelBinarizer', class {}); - this.registerType('sklearn.preprocessing._label.LabelEncoder', class {}); - this.registerType('sklearn.preprocessing.data.Binarizer', class {}); - this.registerType('sklearn.preprocessing.data.MaxAbsScaler', class {}); - this.registerType('sklearn.preprocessing.data.MinMaxScaler', class {}); - this.registerType('sklearn.preprocessing.data.Normalizer', class {}); - this.registerType('sklearn.preprocessing.data.OneHotEncoder', class {}); - this.registerType('sklearn.preprocessing.data.PolynomialFeatures', class {}); - this.registerType('sklearn.preprocessing.data.PowerTransformer', class {}); - this.registerType('sklearn.preprocessing.data.RobustScaler', class {}); - this.registerType('sklearn.preprocessing.data.QuantileTransformer', class {}); - this.registerType('sklearn.preprocessing.data.StandardScaler', class {}); - this.registerType('sklearn.preprocessing.imputation.Imputer', class {}); - this.registerType('sklearn.preprocessing.label.LabelBinarizer', class {}); - this.registerType('sklearn.preprocessing.label.LabelEncoder', class {}); - this.registerType('sklearn.preprocessing.label.MultiLabelBinarizer', class {}); - this.registerType('sklearn.svm._classes.LinearSVC', class {}); - this.registerType('sklearn.svm._classes.SVC', class {}); - this.registerType('sklearn.svm._classes.SVR', class {}); - this.registerType('sklearn.svm.classes.LinearSVC', class {}); - this.registerType('sklearn.svm.classes.OneClassSVM', class {}); - this.registerType('sklearn.svm.classes.SVC', class {}); - this.registerType('sklearn.svm.classes.SVR', class {}); - this.registerType('sklearn.tree._classes.DecisionTreeClassifier', class {}); - this.registerType('sklearn.tree._classes.DecisionTreeRegressor', class {}); - this.registerType('sklearn.tree._classes.ExtraTreeClassifier', class {}); - this.registerType('sklearn.tree._classes.ExtraTreeRegressor', class {}); - this.registerType('sklearn.tree._tree.Tree', class { - constructor(n_features, n_classes, n_outputs) { - this.n_features = n_features; - this.n_classes = n_classes; - this.n_outputs = n_outputs; - } - __setstate__(state) { - this.max_depth = state.max_depth; - this.node_count = state.node_count; - this.nodes = state.nodes; - this.values = state.values; - } - }); - this.registerType('sklearn.tree.tree.DecisionTreeClassifier', class {}); - this.registerType('sklearn.tree.tree.DecisionTreeRegressor', class {}); - this.registerType('sklearn.tree.tree.ExtraTreeClassifier', class {}); - this.registerType('sklearn.utils.Bunch', class {}); - this.registerType('sklearn.utils.deprecation.DeprecationDict', class {}); - this.registerType('re.Pattern', function(pattern, flags) { - this.pattern = pattern; - this.flags = flags; - }); - this.registerType('spacy._ml.PrecomputableAffine', class { - __setstate__(state) { - Object.assign(this, python.Unpickler.open(state).load((name, args) => self.invoke(name, args), null)); - } - }); - this.registerType('spacy.syntax._parser_model.ParserModel', class { - __setstate__(state) { - Object.assign(this, python.Unpickler.open(state).load((name, args) => self.invoke(name, args), null)); - } - }); - this.registerType('thinc.describe.Biases', class { - __setstate__(state) { - Object.assign(this, state); - } - }); - this.registerType('thinc.describe.Dimension', class { - __setstate__(state) { - Object.assign(this, state); - } - }); - this.registerType('thinc.describe.Gradient', class { - __setstate__(state) { - Object.assign(this, state); - } - }); - this.registerType('thinc.describe.Weights', class { - __setstate__(state) { - Object.assign(this, state); - } - }); - this.registerType('thinc.describe.Synapses', class { - __setstate__(state) { - Object.assign(this, state); - } - }); - this.registerType('thinc.neural._classes.affine.Affine', class { - __setstate__(state) { - Object.assign(this, python.Unpickler.open(state).load((name, args) => self.invoke(name, args), null)); - } - }); - this.registerType('thinc.neural._classes.convolution.ExtractWindow', class { - __setstate__(state) { - Object.assign(this, python.Unpickler.open(state).load((name, args) => self.invoke(name, args), null)); - } - }); - this.registerType('thinc.neural._classes.feature_extracter.FeatureExtracter', class { - __setstate__(state) { - Object.assign(this, python.Unpickler.open(state).load((name, args) => self.invoke(name, args), null)); - } - }); - this.registerType('thinc.neural._classes.feed_forward.FeedForward', class { - __setstate__(state) { - Object.assign(this, python.Unpickler.open(state).load((name, args) => self.invoke(name, args), null)); - } - }); - this.registerType('thinc.neural._classes.function_layer.FunctionLayer', class { - __setstate__(state) { - Object.assign(this, python.Unpickler.open(state).load((name, args) => self.invoke(name, args), null)); - } - }); - this.registerType('thinc.neural._classes.hash_embed.HashEmbed', class { - __setstate__(state) { - Object.assign(this, python.Unpickler.open(state).load((name, args) => self.invoke(name, args), null)); - } - }); - this.registerType('thinc.neural._classes.layernorm.LayerNorm', class { - __setstate__(state) { - Object.assign(this, python.Unpickler.open(state).load((name, args) => self.invoke(name, args), null)); - } - }); - this.registerType('thinc.neural._classes.maxout.Maxout', class { - __setstate__(state) { - Object.assign(this, python.Unpickler.open(state).load((name, args) => self.invoke(name, args), null)); - } - }); - this.registerType('thinc.neural._classes.resnet.Residual', class { - __setstate__(state) { - Object.assign(this, python.Unpickler.open(state).load((name, args) => self.invoke(name, args), null)); - } - }); - this.registerType('thinc.neural._classes.softmax.Softmax', class { - __setstate__(state) { - Object.assign(this, python.Unpickler.open(state).load((name, args) => self.invoke(name, args), null)); - } - }); - this.registerType('thinc.neural.mem.Memory', class { - }); - this.registerType('thinc.neural.ops.NumpyOps', class { - }); - this.registerType('types.CodeType', class { - constructor(/* args */) { - } - }); - this.registerType('types.MethodType', class { - constructor(/* args */) { - } - }); - this.registerType('types.ObjectType', builtins.object); - this.registerType('xgboost.compat.XGBoostLabelEncoder', class {}); - this.registerType('xgboost.core.Booster', class {}); - this.registerType('xgboost.sklearn.XGBClassifier', class {}); - this.registerType('xgboost.sklearn.XGBRegressor', class {}); - this.registerFunction('__builtin__.bytearray', function(source, encoding /*, errors */) { - if (source) { - if (encoding === 'latin-1') { - const array = new Uint8Array(source.length); - for (let i = 0; i < source.length; i++) { - array[i] = source.charCodeAt(i); - } - return array; - } - throw new python.Error("Unsupported bytearray encoding '" + JSON.stringify(encoding) + "'."); - } - return []; - }); - this.registerFunction('__builtin__.bytes', function(source, encoding /*, errors */) { - if (source) { - if (encoding === 'latin-1') { - const array = new Uint8Array(source.length); - for (let i = 0; i < source.length; i++) { - array[i] = source.charCodeAt(i); - } - return array; - } - throw new python.Error("Unsupported bytearray encoding '" + JSON.stringify(encoding) + "'."); - } - return []; - }); - this.registerFunction('__builtin__.set', function(iterable) { - return iterable ? iterable : []; - }); - this.registerFunction('__builtin__.frozenset', function(iterable) { - return iterable ? iterable : []; - }); - this.registerFunction('__builtin__.getattr', function(obj, name, defaultValue) { - if (Object.prototype.hasOwnProperty.call(obj, name)) { - return obj[name]; - } - return defaultValue; - }); - this.registerFunction('__builtin__.slice', function(start, stop , step) { - return [ start, stop, step ]; - }); - this.registerFunction('__builtin__.type', function(obj) { - return obj ? obj.__class__ : undefined; - }); - this.registerFunction('_codecs.encode', function(obj /*, econding */) { - return obj; - }); - this.registerFunction('builtins.bytearray', function(data) { - return { data: data }; - }); - this.registerFunction('builtins.getattr', function(obj, name, defaultValue) { - if (Object.prototype.hasOwnProperty.call(obj, name)) { - return obj[name]; - } - return defaultValue; - }); - this.registerFunction('builtins.set', function(iterable) { - return iterable ? iterable : []; - }); - this.registerFunction('builtins.slice', function(start, stop, step) { - return { start: start, stop: stop, step: step }; - }); - this.registerFunction('cloudpickle.cloudpickle._builtin_type', function(name) { - return name; - }); - this.registerFunction('collections.Counter', function(/* iterable */) { - return { __module__: 'collections', __name__: 'Counter' }; - }); - this.registerFunction('collections.defaultdict', function(/* default_factory */) { - return {}; - }); - this.registerFunction('copy_reg._reconstructor', function(cls, base, state) { - // copyreg._reconstructor in Python 3 - if (base === '__builtin__.object' || base === builtins.object) { - return self.invoke(cls, []); - } - else if (base === '__builtin__.tuple' || base === builtins.tuple) { - const obj = self.invoke(cls, []); - for (let i = 0; i < state.length; i++) { - obj[i] = state[i]; - } - return obj; - } - throw new python.Error("Unsupported copy_reg._reconstructor base type '" + base + "'."); - }); - this.registerFunction('dill._dill._create_cell', function(/* args */) { - return function() { - // TODO - }; - }); - this.registerFunction('dill._dill._create_code', function(args) { - return self.invoke('types.CodeType', [ args ]); - }); - this.registerFunction('dill._dill._create_function', function(/* fcode, fglobals, fname, fdefaults, fclosure, fdict, fkwdefaults */) { - return function() { - // TODO - }; - }); - this.registerFunction('dill._dill._get_attr', function(self, name) { - if (Object.prototype.hasOwnProperty.call(self, name)) { - return self[name]; - } - return undefined; - }); - this.registerFunction('dill._dill._import_module', function(import_name, safe) { - try { - return self.context.getx(import_name); - } - catch (err) { - if (safe) { - return null; - } - throw err; - } - }); - this.registerFunction('dill.dill._load_type', function(name) { - return self.context.getx('types.' + name); - }); - this.registerFunction('dill._dill._load_type', function(name) { - return self.context.getx('types.' + name); - }); - this.registerFunction('getattr', function(obj, name, defaultValue) { - if (Object.prototype.hasOwnProperty.call(obj, name)) { - return obj[name]; - } - return defaultValue; - }); - this.registerFunction('numpy.core._multiarray_umath._reconstruct', function(subtype, shape, dtype) { - return self.invoke(subtype, [ shape, dtype ]); - }); - this.registerFunction('numpy.core.multiarray._reconstruct', function(subtype, shape, dtype) { - return self.invoke(subtype, [ shape, dtype ]); - }); - this.registerFunction('numpy.core.multiarray.scalar', function(dtype, rawData) { - let data = rawData; - if (typeof rawData === 'string' || rawData instanceof String) { - data = new Uint8Array(rawData.length); - for (let i = 0; i < rawData.length; i++) { - data[i] = rawData.charCodeAt(i); - } - } - switch (dtype.kind) { - case 'b': { - const view = new DataView(data.buffer, data.byteOffset, data.byteLength); - switch (dtype.itemsize) { - case 1: return view.getInt8(0, true) ? true : false; - default: throw new python.Error("Unsupported scalar dtype boolean itemsize '" + this.itemsize + "'."); - } - } - case 'f': { - const view = new DataView(data.buffer, data.byteOffset, data.byteLength); - switch (dtype.itemsize) { - case 4: return view.getFloat32(0, true); - case 8: return view.getFloat64(0, true); - default: throw new python.Error("Unsupported scalar dtype float itemsize '" + this.itemsize + "'."); - } - } - case 'i': { - const view = new DataView(data.buffer, data.byteOffset, data.byteLength); - switch (dtype.itemsize) { - case 1: return view.getInt8(0, true); - case 2: return view.getInt16(0, true); - case 4: return view.getInt32(0, true); - case 8: return view.getInt64(0, true); - default: throw new python.Error("Unsupported scalar dtype int itemsize '" + this.itemsize + "'."); - } - } - case 'u': { - const view = new DataView(data.buffer, data.byteOffset, data.byteLength); - switch (dtype.itemsize) { - case 1: return view.getUint8(0, true); - case 2: return view.getUint16(0, true); - case 4: return view.getUint32(0, true); - case 8: return view.getUint64(0, true); - default: throw new python.Error("Unsupported scalar dtype uint itemsize '" + this.itemsize + "'."); - } - } - case 'U': { - const view = new DataView(data.buffer, data.byteOffset, data.byteLength); - const list = []; - for (let i = 0; i < dtype.itemsize; i += 4) { - list.push(String.fromCodePoint(view.getUint32(i, true))); - } - return list.join(''); - } - default: { - throw new python.Error("Unsupported scalar dtype kind '" + dtype.kind + "'."); - } - } - }); - this.registerFunction('numpy.core._multiarray_umath.scalar', function(dtype, rawData) { - let data = rawData; - if (typeof rawData === 'string') { - data = new Uint8Array(rawData.length); - for (let i = 0; i < rawData.length; i++) { - data[i] = rawData.charCodeAt(i); - } - } - const dataView = new DataView(data.buffer, data.byteOffset, data.byteLength); - switch (dtype.name) { - case 'uint8': - return dataView.getUint8(0); - case 'float32': - return dataView.getFloat32(0, true); - case 'float64': - return dataView.getFloat64(0, true); - case 'int8': - return dataView.getInt8(0, true); - case 'int16': - return dataView.getInt16(0, true); - case 'int32': - return dataView.getInt32(0, true); - case 'int64': - return dataView.getInt64(0, true); - default: - throw new python.Error("Unsupported scalar type '" + dtype.name + "'."); - } - }); - this.registerFunction('numpy.load', function(file) { - // https://github.com/numpy/numpy/blob/main/numpy/lib/format.py - const signature = [ 0x93, 0x4E, 0x55, 0x4D, 0x50, 0x59 ]; - if (!file.read(6).every((v, i) => v == signature[i])) { - throw new numpy.Error('Invalid signature.'); - } - const major = file.read(1)[0]; - const minor = file.read(1)[0]; - if (major > 3) { - throw new python.Error("Invalid version '" + [ major, minor ].join('.') + "'."); - } - const buffer = new Uint8Array([ 0, 0, 0, 0 ]); - buffer.set(file.read(major >= 2 ? 4 : 2), 0); - const header_length = buffer[3] << 24 | buffer[2] << 16 | buffer[1] << 8 | buffer[0]; - let header = file.read(header_length); - const decoder = new TextDecoder(major >= 3 ? 'utf-8' : 'ascii'); - header = decoder.decode(header); - header = JSON.parse(header.replace(/\(/,'[').replace(/\)/,']').replace('[,','[1,]').replace(',]',',1]').replace(/'/g, '"').replace(/:\s*False\s*,/,':false,').replace(/:\s*True\s*,/,':true,').replace(/,\s*\}/, ' }')); - if (!header.descr || header.descr.length < 2) { - throw new numpy.Error("Missing property 'descr'."); - } - if (!header.shape) { - throw new numpy.Error("Missing property 'shape'."); - } - const shape = header.shape; - const dtype = self.invoke('numpy.dtype', [ header.descr.substring(1) ]); - dtype.byteorder = header.descr[0]; - let data = null; - switch (dtype.byteorder) { - case '|': { - data = file.read(); - break; - } - case '>': - case '<': { - if (header.descr.length !== 3) { - throw new numpy.Error("Unsupported data type '" + header.descr + "'."); - } - const count = shape.length === 0 ? 1 : shape.reduce((a, b) => a * b, 1); - data = file.read(dtype.itemsize * count); - break; - } - default: { - throw new numpy.Error("Unsupported data type '" + header.descr + "'."); - } - } - if (header.fortran_order) { - data = null; - } - return self.invoke('numpy.ndarray', [ shape, dtype, data ]); - }); - this.registerFunction('numpy.save', function(file, arr) { - const descr = arr.dtype.str; - if (descr[0] !== '<' && descr[0] !== '>') { - throw new numpy.Error("Unsupported byte order '" + descr + "'."); - } - if (descr.length !== 3 || (descr[1] !== 'f' && descr[1] !== 'i' && descr[1] !== 'u' && descr.substring(1) !== 'b1')) { - throw new numpy.Error("Unsupported data type '" + descr + "'."); - } - let shape = ''; - switch (arr.shape.length) { - case 0: shape = '()'; break; - case 1: shape = '(' + arr.shape[0].toString() + ',)'; break; - default: shape = '(' + arr.shape.map((dimension) => dimension.toString()).join(', ') + ')'; break; - } - const properties = [ - "'descr': '" + descr + "'", - "'fortran_order': False", - "'shape': " + shape - ]; - let header = '{ ' + properties.join(', ') + ' }'; - header += ' '.repeat(64 - ((header.length + 2 + 8 + 1) & 0x3f)) + '\n'; - const encoder = new TextEncoder('ascii'); - file.write([ 0x93, 0x4E, 0x55, 0x4D, 0x50, 0x59, 0x01, 0x00 ]); // '\\x93NUMPY' + version - file.write([ header.length & 0xff, (header.length >> 8) & 0xff ]); - file.write(encoder.encode(header)); - file.write(arr.tobytes()); - }); - this.registerFunction('numpy.asarray', function(a, dtype) { - const encode = (context, data, dim) => { - const size = context.shape[dim]; - const littleendian = context.littleendian; - if (dim == context.shape.length - 1) { - for (let i = 0; i < size; i++) { - switch (context.dtype) { - case 'f2': - context.view.setFloat16(context.position, data[i], littleendian); - break; - case 'f4': - context.view.setFloat32(context.position, data[i], littleendian); - break; - case 'f8': - context.view.setFloat64(context.position, data[i], littleendian); - break; - case 'i1': - context.view.setInt8(context.position, data[i], littleendian); - break; - case 'i2': - context.view.setInt16(context.position, data[i], littleendian); - break; - case 'i4': - context.view.setInt32(context.position, data[i], littleendian); - break; - case 'i8': - context.view.setInt64(context.position, data[i], littleendian); - break; - case 'u1': - context.view.setUint8(context.position, data[i], littleendian); - break; - case 'u2': - context.view.setUint16(context.position, data[i], littleendian); - break; - case 'u4': - context.view.setUint32(context.position, data[i], littleendian); - break; - case 'u8': - context.view.setUint64(context.position, data[i], littleendian); - break; - default: - throw new numpy.Error("Unsupported tensor data type '" + context.dtype + "'."); - } - context.position += context.itemsize; - } - } - else { - for (let j = 0; j < size; j++) { - encode(context, data[j], dim + 1); - } - } - }; - const array_size = (value) => { - if (value.every((item) => Array.isArray(item))) { - const dims = value.map((item) => array_size(item)); - const dim = dims[0]; - for (let i = 1; i < dims.length; i++) { - if (dim.length === dims[i].length) { - if (!dims[i].every((value, i) => value ===dim[i])) { - throw new python.Error('Invalid array shape.'); - } - } - } - return [ value.length ].concat(dim); - } - return [ value.length ]; - }; - const shape = Array.isArray(a) ? array_size(a) : []; - const size = dtype.itemsize * shape.reduce((a, b) => a * b, 1); - const context = { - position: 0, - itemsize: dtype.itemsize, - dtype: dtype.str.substring(1), - littleendian: dtype.str[0], - shape: shape, - data: new Uint8Array(size) - }; - context.view = new DataView(context.data.buffer, context.data.byteOffset, size); - encode(context, a, 0); - return self.invoke('numpy.ndarray', [ shape, dtype, context.data ]); - - }); - this.registerFunction('numpy.ma.core._mareconstruct', function(subtype, baseclass, baseshape, basetype) { - const data = self.invoke(baseclass, [ baseshape, basetype ]); - // = ndarray.__new__(ndarray, baseshape, make_mask_descr(basetype)) - const mask = self.invoke('numpy.ndarray', [ baseshape, '' ]); - return self.invoke(subtype, [ data, mask, basetype ]); - }); - this.registerFunction('numpy.random.__RandomState_ctor', function() { - return {}; - }); - this.registerFunction('numpy.random._pickle.__randomstate_ctor', function() { - return {}; - }); - this.registerFunction('numpy.core.numeric._frombuffer', function(/* buf, dtype, shape, order */) { - return {}; - }); - this.registerFunction('re._compile', function(pattern, flags) { - return self.invoke('re.Pattern', [ pattern, flags ]); - }); - this.registerFunction('srsly.cloudpickle.cloudpickle._builtin_type', function(name) { - return function() { - return self.invoke('types.' + name, arguments); - }; - }); - } - - get context() { - return this._context; - } - - source(file) { - return this._sources.has(file) ? this._sources.get(file) : null; - } - - debug(/* file */) { - } - - parse(file) { - const buffer = this.source(file); - if (buffer) { - const debug = this.debug(file); - const code = this._utf8Decoder.decode(buffer); - const reader = new python.Parser(code, file, debug); - const program = reader.parse(); - if (!program) { - throw new python.Error("Module '" + file + "' parse error."); - } - return program; - } - return null; - } - - package(name) { - const index = name.lastIndexOf('.'); - if (index > 0) { - this.package(name.substring(0, index)); - } - if (!this._packages.has(name)) { - const file = 'code/' + name.split('.').join('/') + '.py'; - const program = this.parse(file); - if (program) { - let globals = this._context.getx(name); - if (globals === undefined) { - globals = {}; - this._context.setx(name, globals); - } - globals.__class__ = this._context.scope.builtins.module; - globals.__name__ = name; - globals.__file__ = file; - this._packages.set(name, globals); - const context = this._context.push(globals); - this.block(program.body, context); - } - } - return this._packages.get(name); - } - - type(name) { - const type = this._context.getx(name); - if (type !== undefined) { - return type; - } - const parts = name.split('.'); - const className = parts.pop(); - const moduleName = parts.join('.'); - const module = this.package(moduleName); - if (module) { - return module[className]; - } - return null; - } - - invoke(name, args) { - const target = name.__class__ ? name : this.type(name); - if (target) { - if (target.__class__ === this._context.scope.builtins.type) { - if (target.prototype && target.prototype.__class__ === target) { - return Reflect.construct(target, args); - } - const obj = {}; - obj.__proto__ = target; - if (obj.__init__ && typeof obj.__init__ === 'function') { - obj.__init__.apply(obj, args); - } - return obj; - } - else if (target.__class__ === this._context.scope.builtins.function) { - if (target.__call__) { - return target.__call__(args); - } - else { - return target.apply(null, args); - } - } - } - this._raiseUnkownName(name); - this.registerType(name, class {}); - return this.invoke(name, []); - } - - call(target, name, args, context) { - const callTarget = this._target(target, context); - const callArguments = args.map((argument) => this.expression(argument, context)); - if (!callTarget || (name !== null && !callTarget[name])) { - if (name === '__new__' && callArguments.length === 1 && callArguments[0] == callTarget) { - name = null; - callArguments.shift(); - } - else { - const targetName = python.Utility.target(target) + '.' + name; - if (this.type(targetName)) { - return this.invoke(targetName, callArguments); - } - throw new python.Error("Unsupported function '" + targetName + "'."); - } - } - const func = name ? callTarget[name] : callTarget; - if (func.__class__ === this._context.scope.builtins.type) { - if (func.prototype && func.prototype.__class__ === func) { - return Reflect.construct(func, args); - } - const obj = {}; - obj.__proto__ = func; - obj.__class__ = func; - if (obj.__init__ && typeof obj.__init__ === 'function') { - obj.__init__.apply(obj, args); - } - return obj; - } - if (func.__class__ === this._context.scope.builtins.function) { - if (func.__call__) { - return func.__call__(callArguments); - } - } - if (func.__class__ === this._context.scope.builtins.method) { - if (func.__call__) { - return func.__call__([ callTarget ].concat(callArguments)); - } - } - if (typeof func === 'function') { - return func.apply(callTarget, callArguments); - } - throw new python.Error("Unsupported call expression."); - } - - apply(method, args, context) { - const locals = Array.prototype.slice.call(args); - context = context.push(); - for (const parameter of method.parameters) { - let value = locals.shift(); - if (value === undefined && parameter.initializer) { - value = this.expression(parameter.initializer, context); - } - context.set(parameter.name, value); - } - return this.block(method.body.statements, context); - } - - block(statements, context) { - statements = Array.prototype.slice.call(statements); - while (statements.length > 0) { - const statement = statements.shift(); - const value = this.statement(statement, context); - if (value !== undefined) { - return value; - } - } - return undefined; - } - - statement(statement, context) { - switch (statement.type) { - case 'pass': { - break; - } - case 'return': { - return this.expression(statement.expression, context); - } - case 'def': { - const module = context.get('__name__'); - const self = this; - const parent = context.get('__class__'); - let type = null; - if (parent === this._context.scope.builtins.type) { - type = this._context.scope.builtins.method; - } - else if (parent === this._context.scope.builtins.module) { - type = this._context.scope.builtins.function; - } - else { - throw new python.Error('Invalid function scope.'); - } - const func = { - __class__: type, - __globals__: context, - __module__: module, - __name__: statement.name, - __code__: statement, - __call__: function(args) { - return self.apply(this.__code__, args, this.__globals__); - } - }; - context.set(statement.name, func); - break; - } - case 'class': { - const scope = { - __class__:this._context.scope.builtins.type, - __module__: context.get('__name__'), - __name__: statement.name, - }; - context.set(statement.name, scope); - context = context.push(scope); - this.block(statement.body.statements, context); - context = context.pop(); - break; - } - case 'var': { - context.set(statement.name, statement.initializer ? this.expression(statement.initializer, context) : undefined); - break; - } - case '=': { - this.expression(statement, context); - break; - } - case 'if': { - const condition = this.expression(statement.condition, context); - if (condition === true || condition) { - const value = this.block(statement.then.statements, context); - if (value !== undefined) { - return value; - } - break; - } - else if (condition === false) { - const value = this.block(statement.else.statements, context); - if (value !== undefined) { - return value; - } - break; - } - throw new python.Error("Unsupported condition."); - } - case 'for': { - if (statement.target.length == 1 && - statement.variable.length === 1 && statement.variable[0].type === 'id') { - const range = this.expression(statement.target[0], context); - const variable = statement.variable[0]; - for (const current of range) { - this.statement({ type: '=', target: variable, expression: { type: 'number', value: current }}, context); - const value = this.block(statement.body.statements, context); - if (value !== undefined) { - return value; - } - } - break; - } - throw new python.Error("Unsupported 'for' statement."); - } - case 'while': { - const condition = this.expression(statement.condition, context); - if (condition) { - const value = this.block(statement.body.statements, context); - if (value !== undefined) { - return value; - } - } - break; - } - case 'call': { - this.expression(statement, context); - break; - } - case 'import': { - for (const module of statement.modules) { - const moduleName = python.Utility.target(module.name); - const globals = this.package(moduleName); - if (module.as) { - context.set(module.as, globals); - } - } - break; - } - default: { - throw new python.Error("Unsupported statement '" + statement.type + "'."); - } - } - return undefined; - } - - - expression(expression, context) { - const self = context.getx('self'); - switch (expression.type) { - case '=': { - const target = expression.target; - if (target.type === 'id') { - context.set(target.value, this.expression(expression.expression, context)); - return undefined; - } - else if (target.type === '[]') { - if (target.target.type === 'id' && - target.arguments.type === 'list' && - target.arguments.value.length === 1) { - const index = this.expression(target.arguments.value[0], context); - if (target.target.value === '__annotations__') { - context.set(target.target.value, context.get(target.target.value) || {}); - } - context.get(target.target.value)[index] = this.expression(expression.expression, context); - return undefined; - } - } - else if (target.type === '.' && - target.member.type === 'id') { - this.expression(target.target, context)[target.member.value] = this.expression(expression.expression, context); - return undefined; - } - else if (target.type === 'tuple') { - context.target.push(target.value); - const value = this.expression(expression.expression, context); - context.target.pop(); - if (target.value.every((item) => item.type === 'id')) { - if (target.value.length < value.length) { - throw new python.Error('ValueError: too many values to unpack (expected ' + target.value.length + ', actual ' + value.length + ').'); - } - if (target.value.length > value.length) { - throw new python.Error('ValueError: not enough values to unpack (expected ' + target.value.length + ', actual ' + value.length + ').'); - } - for (let i = 0; i < value.length; i++) { - context.set(target.value[i].value, value[i]); - } - return undefined; - } - } - break; - } - case 'list': { - return expression.value.map((item) => this.expression(item, context)); - } - case 'string': { - return expression.value.substring(1, expression.value.length - 1); - } - case 'number': { - return Number(expression.value); - } - case '[]': { - if (expression.target.type === 'id' && - expression.arguments.type === 'list' && - expression.arguments.value.length === 1) { - if (context.get(expression.target.value)) { - const index = this.expression(expression.arguments.value[0], context); - const target = context.get(expression.target.value); - return target[index < 0 ? target.length + index : index]; - } - } - const target = this.expression(expression.target, context); - if (target && expression.arguments.type === 'list' && - (target.__class__ === this.context.scope.typing._TupleType || - target.__class__ === this.context.scope.typing._SpecialGenericAlias || - target.__class__ === this.context.scope.typing._SpecialForm)) { - const type = Object.assign({}, target); - type.__args__ = expression.arguments.value.map((arg) => this.expression(arg, context)); - return type; - } - if (expression.arguments.type === 'list' && expression.arguments.value.length === 1) { - const index = this.expression(expression.arguments.value[0], context); - return target[index < 0 ? target.length + index : index]; - } - break; - } - case '.': { - if (expression.member.type == 'id') { - const target = this._target(expression.target, context); - return target[expression.member.value]; - } - throw new python.Error("Unsupported field expression."); - } - case 'call': { - if (expression.target.type === 'id' && expression.target.value === 'unchecked_cast' && expression.arguments.length === 2) { - return this.expression(expression.arguments[1], context); - } - if (expression.target.type === '.') { - return this.call(expression.target.target, expression.target.member.value, expression.arguments, context); - } - return this.call(expression.target, null, expression.arguments, context); - } - case 'id': { - switch (expression.value) { - case 'self': return self; - case 'None': return null; - case 'True': return true; - case 'False': return false; - default: { - const type = (value) => { - return value && - (value.__class__ === this._context.scope.builtins.type || - value.__class__ === this._context.scope.typing._TupleType || - value.__class__ === this._context.scope.typing._SpecialGenericAlias || - value.__class__ === this._context.scope.typing._SpecialForm); - }; - const builtin = this._context.scope.builtins[expression.value]; - if (type(builtin)) { - return builtin; - } - const value = context.get(expression.value); - if (value === undefined) { - const typing = this._context.scope.typing[expression.value]; - if (type(typing)) { - return typing; - } - const torch = this._context.scope.torch[expression.value]; - if (type(torch)) { - return torch; - } - } - return value; - } - } - } - case 'tuple': { - return expression.value.map((expression) => this.expression(expression, context)); - } - case 'dict': { - const dict = {}; - for (const pair of expression.value) { - if (pair.type !== 'pair') { - throw new python.Error("Unsupported dict item type '" + pair.type + "'."); - } - const key = this.expression(pair.key, context); - const value = this.expression(pair.value, context); - dict[key] = value; - } - return dict; - } - default: { - throw new python.Error("Unsupported expression '" + expression.type + "'."); - } - } - return undefined; - } - - _target(expression, context) { - let current = expression; - let packageName = ''; - for (;;) { - if (current.type === '.' && current.member && current.member.type === 'id') { - packageName = '.' + current.member.value + packageName; - current = current.target; - } - else if (current.type === 'id' && current.value !== 'self' && current.value !== 'CONSTANTS') { - packageName = current.value + packageName; - break; - } - else { - packageName = null; - break; - } - } - if (packageName) { - let target = context.getx(packageName); - if (!target) { - target = this.package(packageName); - if (!target) { - target = context.getx(packageName); - if (!target) { - throw new python.Error("Failed to resolve module '" + packageName + "'."); - } - } - } - return target; - } - return this.expression(expression, context); - } - - registerFunction(name, callback) { - if (this._context.getx(name)) { - throw new python.Error("Function '" + name + "' is already registered."); - } - const parts = name.split('.'); - callback.__class__ = this._context.scope.builtins.function; - callback.__name__ = parts.pop(); - callback.__module__ = parts.join('.'); - this._context.setx(name, callback); - } - - registerType(name, type) { - if (this._context.getx(name)) { - throw new python.Error("Class '" + name + "' is already registered."); - } - const parts = name.split('.'); - type.__class__ = this._context.scope.builtins.type; - type.__name__ = parts.pop(); - type.__module__ = parts.join('.'); - type.prototype.__class__ = type; - this._context.setx(name, type); - } - - registerModule(name) { - let scope = this._context.scope; - const items = name.split('.'); - while (items.length > 0) { - const item = items.shift(); - scope[item] = { __name__: name, __class__: this._context.scope.builtins.module }; - scope = scope[item]; - } - } - - _raiseUnkownName(name) { - if (name && !this._unknownNameMap.has(name)) { - this._unknownNameMap.add(name); - const module = name.split('.').shift(); - if (this._context.scope[module] && this._context.scope[module].__class__ == this._context.scope.builtins.module) { - this._exceptionCallback(new python.Error("Unsupported function '" + name + "'."), false); - } - } - } -}; - -python.Execution.Context = class { - - constructor(parent, scope) { - this._parent = parent || null; - this._scope = scope || {}; - } - - push(scope) { - return new python.Execution.Context(this, scope); - } - - pop() { - return this._parent; - } - - get scope() { - return this._scope; - } - - set(name, value) { - this._scope[name] = value; - } - - get(name) { - if (name in this._scope) { - return this._scope[name]; - } - if (this._parent) { - return this._parent.get(name); - } - return undefined; - } - - setx(name, value) { - if (typeof name !== 'string' || !name.split) { - throw new python.Error("Invalid name '" + JSON.stringify(name) + "'."); - } - const parts = name.split('.'); - if (parts.length == 1) { - this.set(parts[0], value); - } - else { - let parent = this.get(parts[0]); - if (!parent) { - parent = {}; - this.set(parts[0], parent); - } - parts.shift(); - while (parts.length > 1) { - const part = parts.shift(); - parent[part] = parent[part] || {}; - parent = parent[part]; - } - parent[parts[0]] = value; - } - } - - getx(name) { - const parts = name.split('.'); - let value = this.get(parts[0]); - if (value !== undefined) { - parts.shift(); - while (parts.length > 0 && value[parts[0]]) { - value = value[parts[0]]; - parts.shift(); - } - if (parts.length === 0) { - return value; - } - } - return undefined; - } - - get target() { - this._target = this._target || []; - return this._target; - } -}; - -python.Utility = class { - - static target(expression) { - if (expression.type == 'id') { - return expression.value; - } - if (expression.type == '.') { - return python.Utility.target(expression.target) + '.' + python.Utility.target(expression.member); - } - return null; - } -}; - -python.Unpickler = class { - - static open(data) { - const reader = data instanceof Uint8Array ? new python.Unpickler.BinaryReader(data) : new python.Unpickler.StreamReader(data); - if (reader.length > 2) { - const head = reader.peek(2); - if (head[0] === 0x80 && head[1] < 7) { - return new python.Unpickler(reader); - } - reader.seek(-1); - const tail = reader.peek(1); - reader.seek(0); - if (tail[0] === 0x2e) { - return new python.Unpickler(reader); - } - } - return null; - } - - constructor(reader) { - this._reader = reader; - } - - load(function_call, persistent_load) { - const reader = this._reader; - const marker = []; - let stack = []; - const memo = new Map(); - const OpCode = python.Unpickler.OpCode; - while (reader.position < reader.length) { - const opcode = reader.byte(); - switch (opcode) { - case OpCode.PROTO: { - const version = reader.byte(); - if (version > 5) { - throw new python.Error("Unsupported protocol version '" + version + "'."); - } - break; - } - case OpCode.GLOBAL: - stack.push([ reader.line(), reader.line() ].join('.')); - break; - case OpCode.STACK_GLOBAL: - stack.push([ stack.pop(), stack.pop() ].reverse().join('.')); - break; - case OpCode.PUT: { - const index = parseInt(reader.line(), 10); - memo.set(index, stack[stack.length - 1]); - break; - } - case OpCode.OBJ: { - const items = stack; - stack = marker.pop(); - stack.push(function_call(items.pop(), items)); - break; - } - case OpCode.GET: { - const index = parseInt(reader.line(), 10); - stack.push(memo.get(index)); - break; - } - case OpCode.POP: - stack.pop(); - break; - case OpCode.POP_MARK: - stack = marker.pop(); - break; - case OpCode.DUP: - stack.push(stack[stack.length-1]); - break; - case OpCode.PERSID: - stack.push(persistent_load(reader.line())); - break; - case OpCode.BINPERSID: - stack.push(persistent_load(stack.pop())); - break; - case OpCode.REDUCE: { - const items = stack.pop(); - const type = stack.pop(); - stack.push(function_call(type, items)); - break; - } - case OpCode.NEWOBJ: { - const items = stack.pop(); - const type = stack.pop(); - stack.push(function_call(type, items)); - break; - } - case OpCode.BINGET: - stack.push(memo.get(reader.byte())); - break; - case OpCode.INST: { - const module = reader.line(); - const name = reader.line(); - const type = module + '.' + name; - const items = stack; - stack = marker.pop(); - stack.push(function_call(type, items)); - break; - } - case OpCode.LONG_BINGET: - stack.push(memo.get(reader.uint32())); - break; - case OpCode.BINPUT: - memo.set(reader.byte(), stack[stack.length - 1]); - break; - case OpCode.LONG_BINPUT: - memo.set(reader.uint32(), stack[stack.length - 1]); - break; - case OpCode.BININT: - stack.push(reader.int32()); - break; - case OpCode.BININT1: - stack.push(reader.byte()); - break; - case OpCode.LONG: - stack.push(parseInt(reader.line(), 10)); - break; - case OpCode.BININT2: - stack.push(reader.uint16()); - break; - case OpCode.BINBYTES: - stack.push(reader.read(reader.int32())); - break; - case OpCode.BINBYTES8: - stack.push(reader.read(reader.int64())); - break; - case OpCode.SHORT_BINBYTES: - stack.push(reader.read(reader.byte())); - break; - case OpCode.FLOAT: - stack.push(parseFloat(reader.line())); - break; - case OpCode.BINFLOAT: - stack.push(reader.float64()); - break; - case OpCode.INT: { - const value = reader.line(); - if (value == '01') { - stack.push(true); - } - else if (value == '00') { - stack.push(false); - } - else { - stack.push(parseInt(value, 10)); - } - break; - } - case OpCode.EMPTY_LIST: - stack.push([]); - break; - case OpCode.EMPTY_TUPLE: - stack.push([]); - break; - case OpCode.EMPTY_SET: - stack.push([]); - break; - case OpCode.ADDITEMS: { - const items = stack; - stack = marker.pop(); - const obj = stack[stack.length - 1]; - for (let i = 0; i < items.length; i++) { - obj.push(items[i]); - } - break; - } - case OpCode.FROZENSET: { - const items = stack; - stack = marker.pop(); - stack.push(items); - break; - } - case OpCode.DICT: { - const items = stack; - stack = marker.pop(); - const dict = {}; - for (let i = 0; i < items.length; i += 2) { - dict[items[i]] = items[i + 1]; - } - stack.push(dict); - break; - } - case OpCode.LIST: { - const items = stack; - stack = marker.pop(); - stack.push(items); - break; - } - case OpCode.TUPLE: { - const items = stack; - stack = marker.pop(); - stack.push(items); - break; - } - case OpCode.SETITEM: { - const value = stack.pop(); - const key = stack.pop(); - const obj = stack[stack.length - 1]; - if (obj.__setitem__) { - obj.__setitem__(key, value); - } - else { - obj[key] = value; - } - break; - } - case OpCode.SETITEMS: { - const items = stack; - stack = marker.pop(); - const obj = stack[stack.length - 1]; - for (let i = 0; i < items.length; i += 2) { - if (obj.__setitem__) { - obj.__setitem__(items[i], items[i + 1]); - } - else { - obj[items[i]] = items[i + 1]; - } - } - break; - } - case OpCode.EMPTY_DICT: - stack.push({}); - break; - case OpCode.APPEND: { - const append = stack.pop(); - stack[stack.length-1].push(append); - break; - } - case OpCode.APPENDS: { - const appends = stack; - stack = marker.pop(); - const list = stack[stack.length - 1]; - list.push.apply(list, appends); - break; - } - case OpCode.STRING: { - const str = reader.line(); - stack.push(str.substr(1, str.length - 2)); - break; - } - case OpCode.BINSTRING: - stack.push(reader.string(reader.uint32())); - break; - case OpCode.SHORT_BINSTRING: - stack.push(reader.string(reader.byte())); - break; - case OpCode.UNICODE: - stack.push(reader.line()); - break; - case OpCode.BINUNICODE: - stack.push(reader.string(reader.uint32(), 'utf-8')); - break; - case OpCode.SHORT_BINUNICODE: - stack.push(reader.string(reader.byte(), 'utf-8')); - break; - case OpCode.BUILD: { - const state = stack.pop(); - let obj = stack.pop(); - if (obj.__setstate__) { - if (obj.__setstate__.__call__) { - obj.__setstate__.__call__([ obj, state ]); - } - else { - obj.__setstate__(state); - } - } - else if (ArrayBuffer.isView(state) || Object(state) !== state) { - obj.__state__ = state; - } - else if (obj instanceof Map) { - for (const key in state) { - obj.set(key, state[key]); - } - } - else { - Object.assign(obj, state); - } - if (obj.__read__) { - obj = obj.__read__(this); - } - stack.push(obj); - break; - } - case OpCode.MARK: - marker.push(stack); - stack = []; - break; - case OpCode.NEWTRUE: - stack.push(true); - break; - case OpCode.NEWFALSE: - stack.push(false); - break; - case OpCode.LONG1: { - const data = reader.read(reader.byte()); - let number = 0; - switch (data.length) { - case 0: number = 0; break; - case 1: number = data[0]; break; - case 2: number = data[1] << 8 | data[0]; break; - case 3: number = data[2] << 16 | data[1] << 8 | data[0]; break; - case 4: number = data[3] << 24 | data[2] << 16 | data[1] << 8 | data[0]; break; - case 5: number = data[4] * 0x100000000 + ((data[3] << 24 | data[2] << 16 | data[1] << 8 | data[0]) >>> 0); break; - default: number = Array.prototype.slice.call(data, 0); break; - } - stack.push(number); - break; - } - case OpCode.LONG4: - // TODO decode LONG4 - stack.push(reader.read(reader.uint32())); - break; - case OpCode.TUPLE1: - stack.push([ stack.pop() ]); - break; - case OpCode.TUPLE2: { - const b = stack.pop(); - const a = stack.pop(); - stack.push([ a, b ]); - break; - } - case OpCode.TUPLE3: { - const c = stack.pop(); - const b = stack.pop(); - const a = stack.pop(); - stack.push([ a, b, c ]); - break; - } - case OpCode.MEMOIZE: - memo.set(memo.size, stack[stack.length - 1]); - break; - case OpCode.FRAME: - reader.read(8); - break; - case OpCode.BYTEARRAY8: { - stack.push(reader.read(reader.int64())); - break; - } - case OpCode.NONE: - stack.push(null); - break; - case OpCode.STOP: - return stack.pop(); - default: - throw new python.Error('Unknown opcode ' + opcode + ' at position ' + (reader.position - 1).toString() + '.'); - } - } - throw new python.Error('Unexpected end of file.'); - } - - read(size) { - return this._reader.read(size); - } - - stream(size) { - return this._reader.stream(size); - } - - int32() { - return this._reader.int32(); - } - - int64() { - return this._reader.int64(); - } - - unescape(token, size) { - const length = token.length; - const a = new Uint8Array(length); - if (size && size == length) { - for (let p = 0; p < size; p++) { - a[p] = token.charCodeAt(p); - } - return a; - } - let i = 0; - let o = 0; - while (i < length) { - let c = token.charCodeAt(i++); - if (c !== 0x5C || i >= length) { - a[o++] = c; - } - else { - c = token.charCodeAt(i++); - switch (c) { - case 0x27: a[o++] = 0x27; break; // ' - case 0x5C: a[o++] = 0x5C; break; // \\ - case 0x22: a[o++] = 0x22; break; // " - case 0x72: a[o++] = 0x0D; break; // \r - case 0x6E: a[o++] = 0x0A; break; // \n - case 0x74: a[o++] = 0x09; break; // \t - case 0x62: a[o++] = 0x08; break; // \b - case 0x58: // x - case 0x78: { // X - const xsi = i - 1; - const xso = o; - for (let xi = 0; xi < 2; xi++) { - if (i >= length) { - i = xsi; - o = xso; - a[o] = 0x5c; - break; - } - let xd = token.charCodeAt(i++); - xd = xd >= 65 && xd <= 70 ? xd - 55 : xd >= 97 && xd <= 102 ? xd - 87 : xd >= 48 && xd <= 57 ? xd - 48 : -1; - if (xd === -1) { - i = xsi; - o = xso; - a[o] = 0x5c; - break; - } - a[o] = a[o] << 4 | xd; - } - o++; - break; - } - default: - if (c < 48 || c > 57) { // 0-9 - a[o++] = 0x5c; - a[o++] = c; - } - else { - i--; - const osi = i; - const oso = o; - for (let oi = 0; oi < 3; oi++) { - if (i >= length) { - i = osi; - o = oso; - a[o] = 0x5c; - break; - } - const od = token.charCodeAt(i++); - if (od < 48 || od > 57) { - i = osi; - o = oso; - a[o] = 0x5c; - break; - } - a[o] = a[o] << 3 | od - 48; - } - o++; - } - break; - } - } - } - return a.slice(0, o); - } -}; - -// https://svn.python.org/projects/python/trunk/Lib/pickletools.py -// https://github.com/python/cpython/blob/master/Lib/pickle.py -python.Unpickler.OpCode = { - MARK: 40, // '(' - EMPTY_TUPLE: 41, // ')' - STOP: 46, // '.' - POP: 48, // '0' - POP_MARK: 49, // '1' - DUP: 50, // '2' - BINBYTES: 66, // 'B' (Protocol 3) - SHORT_BINBYTES: 67, // 'C' (Protocol 3) - FLOAT: 70, // 'F' - BINFLOAT: 71, // 'G' - INT: 73, // 'I' - BININT: 74, // 'J' - BININT1: 75, // 'K' - LONG: 76, // 'L' - BININT2: 77, // 'M' - NONE: 78, // 'N' - PERSID: 80, // 'P' - BINPERSID: 81, // 'Q' - REDUCE: 82, // 'R' - STRING: 83, // 'S' - BINSTRING: 84, // 'T' - SHORT_BINSTRING: 85, // 'U' - UNICODE: 86, // 'V' - BINUNICODE: 88, // 'X' - EMPTY_LIST: 93, // ']' - APPEND: 97, // 'a' - BUILD: 98, // 'b' - GLOBAL: 99, // 'c' - DICT: 100, // 'd' - APPENDS: 101, // 'e' - GET: 103, // 'g' - BINGET: 104, // 'h' - INST: 105, // 'i' - LONG_BINGET: 106, // 'j' - LIST: 108, // 'l' - OBJ: 111, // 'o' - PUT: 112, // 'p' - BINPUT: 113, // 'q' - LONG_BINPUT: 114, // 'r' - SETITEM: 115, // 's' - TUPLE: 116, // 't' - SETITEMS: 117, // 'u' - EMPTY_DICT: 125, // '}' - PROTO: 128, - NEWOBJ: 129, - TUPLE1: 133, // '\x85' - TUPLE2: 134, // '\x86' - TUPLE3: 135, // '\x87' - NEWTRUE: 136, // '\x88' - NEWFALSE: 137, // '\x89' - LONG1: 138, // '\x8a' - LONG4: 139, // '\x8b' - SHORT_BINUNICODE: 140, // '\x8c' (Protocol 4) - BINUNICODE8: 141, // '\x8d' (Protocol 4) - BINBYTES8: 142, // '\x8e' (Protocol 4) - EMPTY_SET: 143, // '\x8f' (Protocol 4) - ADDITEMS: 144, // '\x90' (Protocol 4) - FROZENSET: 145, // '\x91' (Protocol 4) - NEWOBJ_EX: 146, // '\x92' (Protocol 4) - STACK_GLOBAL: 147, // '\x93' (Protocol 4) - MEMOIZE: 148, // '\x94' (Protocol 4) - FRAME: 149, // '\x95' (Protocol 4) - BYTEARRAY8: 150, // '\x96' (Protocol 5) - NEXT_BUFFER: 151, // '\x97' (Protocol 5) - READONLY_BUFFER: 152 // '\x98' (Protocol 5) -}; - -python.Unpickler.BinaryReader = class { - - constructor(buffer) { - this._buffer = buffer; - this._length = buffer.length; - this._position = 0; - this._view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength); - this._utf8Decoder = new TextDecoder('utf-8'); - this._asciiDecoder = new TextDecoder('ascii'); - } - - get position() { - return this._position; - } - - get length() { - return this._length; - } - - seek(position) { - this._position = position >= 0 ? position : this._length + position; - if (this._position > this._buffer.length) { - throw new Error('Expected ' + (this._position - this._buffer.length) + ' more bytes. The file might be corrupted. Unexpected end of file.'); - } - } - - skip(offset) { - this._position += offset; - if (this._position > this._buffer.length) { - throw new python.Error('Expected ' + (this._position - this._buffer.length) + ' more bytes. The file might be corrupted. Unexpected end of file.'); - } - } - - stream(length) { - const buffer = this.read(length); - return new python.Unpickler.BinaryReader(buffer); - } - - peek(length) { - const position = this._position; - length = length !== undefined ? length : this._length - this._position; - this.skip(length); - const end = this._position; - this.skip(-length); - if (position === 0 && length === this._length) { - return this._buffer; - } - return this._buffer.subarray(position, end); - } - - read(length) { - const position = this._position; - length = length !== undefined ? length : this._length - this._position; - this.skip(length); - if (position === 0 && length === this._length) { - return this._buffer; - } - return this._buffer.subarray(position, this._position); - } - - byte() { - const position = this._position; - this.skip(1); - return this._view.getUint8(position); - } - - uint16() { - const position = this._position; - this.skip(2); - return this._view.getUint16(position, true); - } - - int32() { - const position = this._position; - this.skip(4); - return this._view.getInt32(position, true); - } - - uint32() { - const position = this._position; - this.skip(4); - return this._view.getUint32(position, true); - } - - int64() { - const position = this._position; - this.skip(8); - return this._view.getInt64(position, true).toNumber(); - } - - float32() { - const position = this._position; - this.skip(4); - return this._view.getFloat32(position, true); - } - - float64() { - const position = this._position; - this.skip(8); - return this._view.getFloat64(position, true); - } - - string(size, encoding) { - const data = this.read(size); - return (encoding == 'utf-8') ? - this._utf8Decoder.decode(data) : - this._asciiDecoder.decode(data); - } - - line() { - const index = this._buffer.indexOf(0x0A, this._position); - if (index == -1) { - throw new python.Error("Could not find end of line."); - } - const size = index - this._position; - const text = this.string(size, 'ascii'); - this.skip(1); - return text; - } -}; - -python.Unpickler.StreamReader = class { - - constructor(stream) { - this._stream = stream; - this._length = stream.length; - this._position = 0; - this._utf8Decoder = new TextDecoder('utf-8'); - this._asciiDecoder = new TextDecoder('ascii'); - } - - get position() { - return this._position; - } - - get length() { - return this._length; - } - - seek(position) { - this._stream.seek(position); - this._position = this._stream.position; - } - - skip(offset) { - this._position += offset; - if (this._position > this._length) { - throw new python.Error('Expected ' + (this._position - this._length) + ' more bytes. The file might be corrupted. Unexpected end of file.'); - } - } - - stream(length) { - this._stream.seek(this._position); - this.skip(length); - return this._stream.stream(length); - } - - peek(length) { - this._stream.seek(this._position); - return this._stream.peek(length); - } - - read(length) { - this._stream.seek(this._position); - this.skip(length); - return this._stream.read(length); - } - - byte() { - const position = this._fill(1); - return this._view.getUint8(position); - } - - uint16() { - const position = this._fill(2); - return this._view.getUint16(position, true); - } - - int32() { - const position = this._fill(4); - return this._view.getInt32(position, true); - } - - uint32() { - const position = this._fill(4); - return this._view.getUint32(position, true); - } - - int64() { - const position = this._fill(8); - return this._view.getInt64(position, true).toNumber(); - } - - float32() { - const position = this._fill(4); - return this._view.getFloat32(position, true); - } - - float64() { - const position = this._fill(8); - return this._view.getFloat64(position, true); - } - - string(size, encoding) { - const data = this.read(size); - return (encoding == 'utf-8') ? - this._utf8Decoder.decode(data) : - this._asciiDecoder.decode(data); - } - - line() { - let position = this._fill(0); - let index = this._buffer.indexOf(0x0A, position); - if (index == -1) { - const size = Math.min(0x1000000, this._stream.length - this._position); - this._fill(size); - this.skip(-size); - position = this._fill(0); - index = this._buffer.indexOf(0x0A, position); - if (index == -1) { - throw new python.Error("Could not find end of line."); - } - } - const size = index - position; - const text = this.string(size, 'ascii'); - this.skip(1); - return text; - } - - _fill(length) { - if (this._position + length > this._length) { - throw new Error('Expected ' + (this._position + length - this._length) + ' more bytes. The file might be corrupted. Unexpected end of file.'); - } - if (!this._buffer || this._position < this._offset || this._position + length > this._offset + this._buffer.length) { - this._offset = this._position; - this._stream.seek(this._offset); - this._buffer = this._stream.read(Math.min(0x10000000, this._length - this._offset)); - this._view = new DataView(this._buffer.buffer, this._buffer.byteOffset, this._buffer.byteLength); - } - const position = this._position; - this._position += length; - return position - this._offset; - } -}; - -python.Error = class extends Error { - - constructor(message) { - super(message); - this.name = 'Error loading Python module.'; - } -}; - -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.Execution = python.Execution; - module.exports.Unpickler = python.Unpickler; -} diff --git a/media/CircleEditor/external/tar.js b/media/CircleEditor/external/tar.js deleted file mode 100644 index 9f688461..00000000 --- a/media/CircleEditor/external/tar.js +++ /dev/null @@ -1,171 +0,0 @@ - -var tar = tar || {}; - -tar.Archive = class { - - static open(data) { - const stream = data instanceof Uint8Array ? new tar.BinaryReader(data) : data; - if (stream.length > 512) { - const buffer = stream.peek(512); - const sum = buffer.map((value, index) => (index >= 148 && index < 156) ? 32 : value).reduce((a, b) => a + b, 0); - let checksum = ''; - for (let i = 148; i < 156 && buffer[i] !== 0x00; i++) { - checksum += String.fromCharCode(buffer[i]); - } - checksum = parseInt(checksum, 8); - if (!isNaN(checksum) && sum === checksum) { - return new tar.Archive(stream); - } - } - return null; - } - - constructor(stream) { - this._entries = new Map(); - const position = stream.position; - while (stream.position < stream.length) { - const entry = new tar.Entry(stream); - if (entry.type === '0' || entry.type === '1' || entry.type === '2') { - this._entries.set(entry.name, entry.stream); - } - if (stream.position + 512 > stream.length || - stream.peek(512).every((value) => value === 0x00)) { - break; - } - } - stream.seek(position); - } - - get entries() { - return this._entries; - } -}; - -tar.Entry = class { - - constructor(stream) { - const buffer = stream.read(512); - const reader = new tar.BinaryReader(buffer); - const sum = buffer.map((value, index) => (index >= 148 && index < 156) ? 32 : value).reduce((a, b) => a + b, 0); - let checksum = ''; - for (let i = 148; i < 156 && buffer[i] !== 0x00; i++) { - checksum += String.fromCharCode(buffer[i]); - } - checksum = parseInt(checksum, 8); - if (isNaN(checksum) || sum !== checksum) { - throw new tar.Error('Invalid tar archive.'); - } - this._name = reader.string(100); - reader.string(8); // file mode - reader.string(8); // owner - reader.string(8); // group - const size = parseInt(reader.string(12).trim(), 8); - reader.string(12); // timestamp - reader.string(8); // checksum - this._type = reader.string(1); - reader.string(100); // name of linked file - if (reader.string(6) === 'ustar') { - reader.string(2); // ustar version - reader.string(32); // owner user name - reader.string(32); // owner group name - reader.string(8); // device major number - reader.string(8); // device number number - this._name = reader.string(155) + this._name; - } - this._stream = stream.stream(size); - stream.read(((size % 512) != 0) ? (512 - (size % 512)) : 0); - } - - get type() { - return this._type; - } - - get name() { - return this._name; - } - - get stream() { - return this._stream; - } -}; - -tar.BinaryReader = class { - - constructor(buffer) { - this._buffer = buffer; - this._length = buffer.length; - this._position = 0; - this._view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength); - } - - get position() { - return this._position; - } - - get length() { - return this._length; - } - - create(buffer) { - return new tar.BinaryReader(buffer); - } - - stream(length) { - return this.create(this.read(length)); - } - - seek(position) { - this._position = position >= 0 ? position : this._length + position; - } - - skip(offset) { - this._position += offset; - } - - peek(length) { - if (this._position === 0 && length === undefined) { - return this._buffer; - } - const position = this._position; - this.skip(length !== undefined ? length : this._length - this._position); - const end = this._position; - this.seek(position); - return this._buffer.subarray(position, end); - } - - read(length) { - if (this._position === 0 && length === undefined) { - this._position = this._length; - return this._buffer; - } - const position = this._position; - this.skip(length !== undefined ? length : this._length - this._position); - return this._buffer.subarray(position, this._position); - } - - string(length) { - const buffer = this.read(length); - let position = 0; - let content = ''; - for (let i = 0; i < length; i++) { - const c = buffer[position++]; - if (c === 0) { - break; - } - content += String.fromCharCode(c); - } - return content; - } -}; - -tar.Error = class extends Error { - - constructor(message) { - super(message); - this.name = 'tar Error'; - } -}; - -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.Archive = tar.Archive; -} diff --git a/media/CircleEditor/external/text.js b/media/CircleEditor/external/text.js deleted file mode 100644 index b8d5bcfd..00000000 --- a/media/CircleEditor/external/text.js +++ /dev/null @@ -1,345 +0,0 @@ - -var text = text || {}; - -text.Decoder = class { - - static open(data, encoding) { - if (typeof data === 'string') { - return new text.Decoder.String(data); - } - const assert = (encoding, condition) => { - if (encoding && encoding !== condition) { - throw new text.Error("Invalid encoding '" + encoding + "'."); - } - }; - const buffer = data instanceof Uint8Array ? data : data.peek(); - const length = buffer.length; - if (length >= 3 && buffer[0] === 0xef && buffer[1] === 0xbb && buffer[2] === 0xbf) { - assert(encoding, 'utf-8'); - return new text.Decoder.Utf8(buffer, 3, true); - } - if (length >= 2 && buffer[0] === 0xff && buffer[1] === 0xfe) { - assert(encoding, 'utf-16'); - return new text.Decoder.Utf16LE(buffer, 2); - } - if (length >= 2 && buffer[0] === 0xfe && buffer[1] === 0xff) { - assert(encoding, 'utf-16'); - return new text.Decoder.Utf16BE(buffer, 2); - } - if (length >= 4 && buffer[0] === 0x00 && buffer[1] === 0x00 && buffer[2] === 0xfe && buffer[3] === 0xff) { - assert(encoding, 'utf-32'); - return new text.Decoder.Utf32LE(buffer, 2); - } - if (length >= 4 && buffer[0] === 0xff && buffer[1] === 0xfe && buffer[2] === 0x00 && buffer[3] === 0x00) { - assert(encoding, 'utf-32'); - return new text.Decoder.Utf32BE(buffer, 2); - } - if (length >= 5 && buffer[0] === 0x2B && buffer[1] === 0x2F && buffer[2] === 0x76 && buffer[3] === 0x38 && buffer[4] === 0x2D) { - throw new text.Error("Unsupported UTF-7 encoding."); - } - if (length >= 4 && buffer[0] === 0x2B && buffer[1] === 0x2F && buffer[2] === 0x76 && (buffer[3] === 0x38 || buffer[3] === 0x39 || buffer[3] === 0x2B || buffer[3] === 0x2F)) { - throw new text.Error("Unsupported UTF-7 encoding."); - } - if (length >= 4 && buffer[0] === 0x84 && buffer[1] === 0x31 && buffer[2] === 0x95 && buffer[3] === 0x33) { - throw new text.Error("Unsupported GB-18030 encoding."); - } - if (length > 4 && (length % 2) == 0 && (buffer[0] === 0x00 || buffer[1] === 0x00 || buffer[2] === 0x00 || buffer[3] === 0x00)) { - const lo = new Uint32Array(256); - const hi = new Uint32Array(256); - for (let i = 0; i < length; i += 2) { - lo[buffer[i]]++; - hi[buffer[i + 1]]++; - } - if (lo[0x00] === 0 && (hi[0x00] / (length >> 1)) > 0.5) { - assert(encoding, 'utf-16'); - return new text.Decoder.Utf16LE(buffer, 0); - } - if (hi[0x00] === 0 && (lo[0x00] / (length >> 1)) > 0.5) { - assert(encoding, 'utf-16'); - return new text.Decoder.Utf16BE(buffer, 0); - } - } - if (encoding && (encoding.startsWith('iso-8859-') || encoding.startsWith('latin-'))) { - return new text.Decoder.Latin1(buffer, 0); - } - assert(encoding, 'utf-8'); - return new text.Decoder.Utf8(buffer, 0, encoding === 'utf-8'); - } -}; - -text.Decoder.String = class { - - constructor(buffer) { - this.buffer = buffer ? buffer.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]|[^\uD800-\uDFFF]/g) : []; - this.position = 0; - this.length = this.buffer.length; - } - - get encoding() { - return null; - } - - decode() { - if (this.position < this.length) { - return this.buffer[this.position++]; - } - return undefined; - } -}; - -text.Decoder.Utf8 = class { - - constructor(buffer, position, fatal) { - this.position = position || 0; - this.buffer = buffer; - this.fatal = fatal; - } - - get encoding() { - return 'utf-8'; - } - - decode() { - const c = this.buffer[this.position]; - if (c === undefined) { - return c; - } - this.position++; - if (c < 0x80) { - return String.fromCodePoint(c); - } - if (c >= 0xC2 && c <= 0xDF) { - if (this.buffer[this.position] !== undefined) { - const c2 = this.buffer[this.position]; - this.position++; - return String.fromCharCode(((c & 0x1F) << 6) | (c2 & 0x3F)); - } - } - if (c >= 0xE0 && c <= 0xEF) { - if (this.buffer[this.position + 1] !== undefined) { - const c2 = this.buffer[this.position]; - if ((c !== 0xE0 || c2 >= 0xA0) && (c !== 0xED || c2 <= 0x9f)) { - const c3 = this.buffer[this.position + 1]; - if (c3 >= 0x80 && c3 < 0xFB) { - this.position += 2; - return String.fromCharCode(((c & 0x0F) << 12) | ((c2 & 0x3F) << 6) | ((c3 & 0x3F) << 0)); - } - } - } - } - if (c >= 0xF0 && c <= 0xF4) { - if (this.buffer[this.position + 2] !== undefined) { - const c2 = this.buffer[this.position]; - if (c2 >= 0x80 && c2 <= 0xBF) { - const c3 = this.buffer[this.position + 1]; - if (c3 >= 0x80 && c3 <= 0xBF) { - const c4 = this.buffer[this.position + 2]; - if (c4 >= 0x80 && c4 <= 0xBF) { - const codePoint = ((c & 0x07) << 18) | ((c2 & 0x3F) << 12) | ((c3 & 0x3F) << 6) | (c4 & 0x3F); - if (codePoint <= 0x10FFFF) { - this.position += 3; - return String.fromCodePoint(codePoint); - } - } - } - } - } - } - if (this.fatal) { - throw new text.Error('Invalid utf-8 character.'); - } - return String.fromCharCode(0xfffd); - } -}; - -text.Decoder.Latin1 = class { - - constructor(buffer, position) { - this.position = position || 0; - this.buffer = buffer; - } - - get encoding() { - return 'latin-1'; - } - - decode() { - const c = this.buffer[this.position]; - if (c === undefined) { - return c; - } - this.position++; - return String.fromCodePoint(c); - } -}; - -text.Decoder.Utf16LE = class { - - constructor(buffer, position) { - this.buffer = buffer; - this.position = position || 0; - this.length = buffer.length; - } - - get encoding() { - return 'utf-16'; - } - - decode() { - if (this.position + 1 < this.length) { - const c = this.buffer[this.position++] | (this.buffer[this.position++] << 8); - if (c < 0xD800 || c >= 0xDFFF) { - return String.fromCharCode(c); - } - if (c >= 0xD800 && c < 0xDBFF) { - if (this._position + 1 < this._length) { - const c2 = this._buffer[this._position++] | (this._buffer[this._position++] << 8); - if (c >= 0xDC00 || c < 0xDFFF) { - return String.fromCodePoint(0x10000 + ((c & 0x3ff) << 10) + (c2 & 0x3ff)); - } - } - } - return String.fromCharCode(0xfffd); - } - return undefined; - } -}; - -text.Decoder.Utf16BE = class { - - constructor(buffer, position) { - this.buffer = buffer; - this.position = position || 0; - this.length = buffer.length; - } - - get encoding() { - return 'utf-16'; - } - - decode() { - if (this.position + 1 < this.length) { - const c = (this.buffer[this.position++] << 8) | this.buffer[this.position++]; - if (c < 0xD800 || c >= 0xDFFF) { - return String.fromCharCode(c); - } - if (c >= 0xD800 && c < 0xDBFF) { - if (this._position + 1 < this._length) { - const c2 = (this._buffer[this._position++] << 8) | this._buffer[this._position++]; - if (c >= 0xDC00 || c < 0xDFFF) { - return String.fromCodePoint(0x10000 + ((c & 0x3ff) << 10) + (c2 & 0x3ff)); - } - } - } - return String.fromCharCode(0xfffd); - } - return undefined; - } -}; - -text.Decoder.Utf32LE = class { - - constructor(buffer, position) { - this.buffer = buffer; - this.position = position || 0; - this.length = buffer.length; - } - - get encoding() { - return 'utf-32'; - } - - decode() { - if (this.position + 3 < this.length) { - const c = this.buffer[this.position++] | (this.buffer[this.position++] << 8) || (this.buffer[this.position++] << 16) || (this.buffer[this.position++] << 24); - if (c < 0x10FFFF) { - return String.fromCodePoint(c); - } - return String.fromCharCode(0xfffd); - } - return undefined; - } -}; - -text.Decoder.Utf32BE = class { - - constructor(buffer, position) { - this.buffer = buffer; - this.position = position || 0; - this.length = buffer.length; - } - - get encoding() { - return 'utf-32'; - } - - decode() { - if (this.position + 3 < this.length) { - const c = (this.buffer[this.position++] << 24) || (this.buffer[this.position++] << 16) || (this.buffer[this.position++] << 8) | this.buffer[this.position++]; - if (c < 0x10FFFF) { - return String.fromCodePoint(c); - } - return String.fromCharCode(0xfffd); - } - return undefined; - } -}; - -text.Reader = class { - - constructor(data, length) { - this._decoder = text.Decoder.open(data); - this._position = 0; - this._length = length || Number.MAX_SAFE_INTEGER; - } - - static open(data, length) { - return new text.Reader(data, length); - } - - read() { - if (this._position >= this._length) { - return undefined; - } - let line = ''; - let buffer = null; - for (;;) { - const c = this._decoder.decode(); - if (c === undefined) { - this._length = this._position; - break; - } - this._position++; - if (this._position > this._length) { - break; - } - if (c === '\n') { - break; - } - line += c; - if (line.length >= 32) { - buffer = buffer || []; - buffer.push(line); - line = ''; - } - } - if (buffer) { - buffer.push(line); - return buffer.join(''); - } - return line; - } -}; - -text.Error = class extends Error { - - constructor(message) { - super(message); - this.name = 'Text Error'; - } -}; - -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.Decoder = text.Decoder; - module.exports.Reader = text.Reader; -} diff --git a/media/CircleEditor/external/xml.js b/media/CircleEditor/external/xml.js deleted file mode 100644 index b9fa4880..00000000 --- a/media/CircleEditor/external/xml.js +++ /dev/null @@ -1,1799 +0,0 @@ - -var xml = xml || {}; -var text = text || require('./text'); - -// https://www.w3.org/TR/xml - -xml.TextReader = class { - - static open(data, callback) { - const decoder = text.Decoder.open(data); - for (;;) { - const c = decoder.decode(); - if (c === '<') { - break; - } - if (c === ' ' || c === '\n' || c === '\r' || c === '\t') { - continue; - } - return null; - } - return new xml.TextReader(data, callback); - } - - constructor(data, callback) { - this._data = data; - this._callback = callback; - this._entities = new Map([ [ 'quot', '"' ], [ 'amp', '&' ], [ 'apos', "'" ], [ 'lt', '<' ], [ 'gt', '>' ] ]); - this._nameStartCharRegExp = /[:A-Z_a-z\xC0-\xD6\xD8-\xF6\xF8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]/; - this._nameCharRegExp = new RegExp("[-.0-9\\xB7" + this._nameStartCharRegExp.source.slice(1, -1) + "]"); - xml.Utility.nameStartCharRegExp = this._nameStartCharRegExp; - } - - peek() { - this._peek = true; - const value = this.read(); - delete this._peek; - return value; - } - - read() { - this._stack = []; - this._context = []; - this._pushBuffer(this._data, '', '', false); - this._version = 0; - /* eslint-disable */ - this._charRegExp = /[\x09\x0a\x0d\x20-\uD7FF\uE000-\uFFFD]/; - /* eslint-enable */ - this._parameterEntities = false; - this._characterData = true; - this._push(new xml.Document()); - const document = this._document(); - for (;;) { - this._start = this._position; - switch (this._char) { - case '<': { - this._next(); - switch (this._char) { - case '?': { - this._processingInstruction(); - break; - } - case '!': { - this._next(); - if (this._match('--')) { - this._comment(); - } - else if (this._match('[CDATA')) { - this._assert(this._stack.length > 1); - this._characterData = true; - this._expect('['); - const data = this._terminal(']]>'); - const node = document.createCDATASection(data); - this._appendChild(node); - } - else if (this._match('DOCTYPE')) { - this._assert(this._stack.length > 1 || !document.documentElement || !document.documentType); - this._whitespace(1); - const name = this._name(); - this._assert(name !== null); - let systemId = ''; - let publicId = ''; - let whitespace = this._whitespace(0); - if (whitespace && this._match('SYSTEM')) { - this._whitespace(1); - systemId = this._systemLiteral(); - this._whitespace(0); - whitespace = true; - } - else if (whitespace && this._match('PUBLIC')) { - this._whitespace(1); - publicId = this._pubidLiteral(); - this._whitespace(1); - systemId = this._systemLiteral(); - this._whitespace(0); - whitespace = true; - } - const node = document.createDocumentType(name, publicId, systemId); - this._appendChild(node); - this._push(node); - node.parameterEntities = new xml.NamedNodeMap(); - node.elements = new xml.NamedNodeMap(); - this._parameterEntities = true; - this._characterData = false; - const internalSubset = whitespace && this._match('['); - if (internalSubset) { - this._internalSubset(']'); - } - if (systemId && !this._standalone) { - this._pushResource(systemId, '', true); - this._internalSubset(undefined); - this._popContext(); - } - this._characterData = true; - this._parameterEntities = false; - const values = node.entities.filter((entity) => entity.value).map((entity) => entity.value); - for (const entity of node.entities.filter((entity) => entity.notationName)) { - const reference = '&' + entity.localName + ';'; - if (values.some((value) => value.indexOf(reference) >= 0)) { - this._error("Entity references unparsed entity '" + entity.localName + "'"); - } - } - if (internalSubset) { - this._expect(']'); - this._whitespace(0); - } - this._expect('>'); - this._assert(this._pop().nodeType === xml.NodeType.DocumentType); - } - else { - this._unexpected(); - } - break; - } - case '/': { - this._next(); - const name = this._name(); - this._assert(name !== null); - this._whitespace(0); - this._expect('>'); - const node = this._pop(); - const nodeName = node.prefix ? node.prefix + ':' + node.localName : node.localName; - if (name !== nodeName) { - this._error("Opening tag <" + nodeName + "> and ending tag mismatch", this._start); - } - break; - } - default: { - this._assert(this._stack.length > 1 || !this._document.documentElement); - const name = this._name(); - this._assert(name !== null); - this._assert(!name.startsWith('xmlns:')); - const attributes = []; - let whitespace = this._whitespace(0); - if (whitespace) { - while (this._char !== '/' && this._char !== '>') { - if (!whitespace) { - this._unexpected(); - } - const position = this._position; - const name = this._name(); - if (!name) { - this._unexpected(); - } - this._whitespace(0); - this._expect('='); - this._whitespace(0); - const valuePosition = this._valuePosition; - const value = this._attributeValue(); - attributes.push({ - qualifiedName: name, - value: value, - position: position, - valuePosition: valuePosition - }); - whitespace = this._whitespace(0); - if (name === 'xmlns' && (!this._validateNamespace(value) || value === 'http://www.w3.org/2000/xmlns/' || value === 'http://www.w3.org/XML/1998/namespace')) { - this._error("Invalid namespace '" + value + "'", valuePosition); - } - if (name === 'xml:space' && value !== 'preserve' && value !== 'default') { - this._error("Unexpected xml:space attribute value '" + value + "'", position); - } - } - } - const namespaces = new Map(); - for (const entry of attributes.reverse()) { - const name = entry.qualifiedName; - const value = entry.value; - const pair = xml.Utility.split(name); - this._assert(name !== 'xmlns:'); - entry.prefix = pair[0]; - entry.localName = pair[1]; - if (entry.prefix !== null) { - this._assert(entry.localName !== ''); - if (entry.prefix === 'xmlns' && entry.localName) { - if (!this._validateNamespace(value) || value === 'http://www.w3.org/2000/xmlns/') { - this._error("Invalid namespace '" + value + "'", entry.valuePosition); - } - if (entry.localName === 'xmlns' || (entry.localName === 'xml' && value !== 'http://www.w3.org/XML/1998/namespace') || (entry.localName !== 'xml' && value === 'http://www.w3.org/XML/1998/namespace')) { - this._error("Invalid namespace prefix '" + entry.localName + "'", entry.position); - } - if (this._version === 0 && value.length === 0) { - this._error("Invalid namespace declaration'", entry.position); - } - namespaces.set(entry.localName, value); - } - } - else { - if (entry.localName === 'xmlns') { - namespaces.set('', value); - } - } - } - const pair = xml.Utility.split(name); - const prefix = pair[0] || ''; - const namespaceURI = namespaces.has(prefix) ? namespaces.get(prefix) : this._lookupNamespaceURI(prefix); - let element = null; - const documentType = document.documentType; - const elementType = documentType ? documentType.elements.getNamedItem(name) : null; - if (namespaceURI !== null) { - this._assert(name === ':' || (!name.endsWith(':') && !name.startsWith(':'))); - if (prefix && (namespaceURI === '' || namespaceURI === null)) { - this._error("Invalid namespace prefix '" + prefix + "'", this._start); - } - element = document.createElementNS(namespaceURI, name); - } - else { - this._assert((pair[0] === null && !name.endsWith(':')) || name === ':' || elementType !== null); - element = document.createElement(name); - } - const parent = this._node(); - if (parent.nodeType === xml.NodeType.Document && parent.documentElement !== null) { - this._error('Duplicate document element', this._start); - } - this._appendChild(element); - const keys = new Set(); - for (const attr of attributes) { - const name = attr.qualifiedName; - const prefix = attr.prefix || ''; - const namespaceURI = namespaces.has(prefix) ? namespaces.get(prefix) : this._lookupNamespaceURI(prefix); - let attribute = null; - if (namespaceURI) { - attribute = document.createAttributeNS(namespaceURI, name); - } - else { - const attributeType = elementType ? elementType.attributes.getNamedItem(name) : null; - this._assert(name.indexOf(':') === -1 || attributeType); - attribute = document.createAttribute(name); - } - const key = (attribute.namespaceURI || '') + '|' + attribute.localName; - this._assert(!keys.has(key)); - keys.add(key); - attribute.value = attr.value; - attribute.ownerElement = element; - element.setAttributeNode(attribute); - } - const close = this._match('/'); - this._expect('>'); - if (this._peek && this._stack.length === 1 && this._nodeType() === xml.NodeType.Document) { - return this._pop(); - } - if (!close) { - this._push(element); - } - break; - } - } - break; - } - default: { - while (this._char === undefined && this._context.length > 0) { - this._popContext(); - } - if (this._char === undefined) { - if (this._stack.length === 1 && this._nodeType() === xml.NodeType.Document) { - this._assert(document.documentElement); - const documentType = document.documentType; - if (documentType) { - delete documentType.parameterEntities; - delete documentType.elements; - } - const value = this._pop(); - for (const key of Object.keys(this)) { - if (key !== '_data' && key !== '_callback' && key !== '_entities' && !key.startsWith('_name')) { - delete this[key]; - } - } - return value; - } - this._unexpected(); - } - const node = this._node(); - if (node.nodeType === xml.NodeType.Element) { - const documentType = document.documentType; - const name = node.prefix ? node.prefix + ':' + node.localName : node.localName; - const elementType = documentType ? documentType.elements.getNamedItem(name) : null; - this._characterData = elementType ? elementType.characterData : false; - this._seek(this._position); - const data = []; - while (this._char !== '<' && this._char !== undefined) { - if (this._char === ']' && this._match(']]>')) { - this._unexpected(); - } - data.push(this._content()); - if (data.length > 65536) { - this._error('Invalid character data buffer size.'); - } - } - if (data.length > 0) { - const content = data.splice(0, data.length).join(''); - if (content.trim().length > 0) { - const node = document.createTextNode(content); - this._appendChild(node); - } - } - continue; - } - if (!this._whitespace(0)) { - this._unexpected(); - } - break; - } - } - } - } - - _internalSubset(terminal) { - for (;;) { - this._start = this._position; - switch (this._char) { - case '<': { - this._next(); - switch (this._char) { - case '?': { - this._processingInstruction(); - break; - } - case '!': { - this._next(); - if (this._match('--')) { - this._parameterEntities = false; - this._characterData = true; - this._comment(); - this._parameterEntities = true; - } - else if (this._match('ENTITY')) { - const documentType = this._node(); - this._assert(documentType.nodeType === xml.NodeType.DocumentType); - this._parameterEntities = false; - this._whitespace(1); - const parameter = this._char === '%'; - if (parameter) { - this._next(); - this._whitespace(1); - } - this._parameterEntities = true; - const name = this._entityName(); - const node = documentType.createEntity(name); - let whitespace = this._whitespace(0); - if (whitespace && (this._char === '"' || this._char === "'")) { - node.value = this._entityValue(); - whitespace = this._whitespace(0); - } - else { - if (whitespace && this._match('SYSTEM')) { - this._whitespace(1); - node.systemId = this._systemLiteral(); - whitespace = this._whitespace(0); - } - else if (whitespace && this._match('PUBLIC')) { - this._whitespace(1); - node.publicId = this._pubidLiteral(); - this._whitespace(1); - node.systemId = this._systemLiteral(); - whitespace = this._whitespace(0); - } - else { - this._unexpected(); - } - if (whitespace && !parameter) { - if (this._match('NDATA')) { - this._whitespace(1); - const name = this._name(); - this._assert(name !== null); - node.notationName = name; - this._whitespace(0); - } - } - } - this._expect('>'); - if (parameter) { - documentType.parameterEntities.setNamedItem(node); - } - else { - this._appendChild(node); - } - } - else if (this._match('ELEMENT')) { - const documentType = this._node(); - this._assert(documentType.nodeType === xml.NodeType.DocumentType); - this._whitespace(1); - const name = this._name(); - this._assert(name !== null); - this._whitespace(1); - const elementType = this._elementType(name); - if (this._match('EMPTY')) { - this._whitespace(0); - } - else if (this._match('ANY')) { - this._whitespace(0); - } - else { - this._expect('('); - this._whitespace(0); - if (this._match('#PCDATA')) { - elementType.characterData = true; - this._whitespace(0); - if (this._match(')')) { - this._match('*'); - } - else { - this._whitespace(0); - while (this._match('|')) { - this._whitespace(0); - const name = this._name(); - this._assert(name); - this._whitespace(0); - } - this._expect(')*'); - } - } - else { - this._elementChildren(); - } - } - this._whitespace(0); - this._expect('>'); - } - else if (this._match('ATTLIST')) { - const documentType = this._node(); - this._assert(documentType.nodeType === xml.NodeType.DocumentType); - this._whitespace(1); - const name = this._name(); - this._assert(name !== null); - const elementType = this._elementType(name); - while (this._whitespace(0)) { - const attributeType = this._attributeDefinition(); - if (!attributeType) { - break; - } - elementType.attributes.setNamedItem(attributeType); - } - this._whitespace(0); - this._expect('>'); - } - else if (this._match('NOTATION')) { - this._assert(this._nodeType() === xml.NodeType.DocumentType); - const notation = { systemId: null, publicId: null }; - this._whitespace(1); - notation.name = this._entityName(); - let whitespace = this._whitespace(0); - if (whitespace && this._match('SYSTEM')) { - this._whitespace(1); - notation.systemId = this._systemLiteral(); - whitespace = this._whitespace(0); - } - if (whitespace && this._match('PUBLIC')) { - this._whitespace(1); - notation.publicId = this._pubidLiteral(); - if (this._whitespace(0) && (this._char === '"') || this._char === "'") { - notation.systemId = this._systemLiteral(); - whitespace = this._whitespace(0); - } - } - this._assert(notation.systemId || notation.publicId); - this._expect('>'); - } - else if (this._match('[')) { - this._whitespace(0); - if (this._match('INCLUDE')) { - this._assert(this._context.length > 0); - this._whitespace(0); - this._expect('['); - this._internalSubset(']'); - this._expect(']]>'); - } - else if (this._match('IGNORE')) { - this._whitespace(0); - this._expect('['); - this._ignoreSectContents(); - } - } - else { - this._unexpected(); - } - break; - } - default: { - break; - } - } - break; - } - case '%': { - this._resolveParameterEntityReference(); - break; - } - default: { - if (this._char === terminal) { - return; - } - if (!this._whitespace(0)) { - this._unexpected(); - } - break; - } - } - } - } - - _ignoreSectContents() { - while (!this._match(']]>')) { - if (this._match('= 0x10000 && c <= 0xEFFFF)) { - name.push(this._char); - this._next(); - if (this._char !== undefined) { - let c = this._char.codePointAt(0); - while (this._nameCharRegExp.test(this._char) || (c >= 0x300 && c <= 0x36f) || (c >= 0x203F && c <= 0x2040)) { - name.push(this._char); - this._next(); - if (this._char === undefined || this._implicitSpace) { - break; - } - c = this._char.codePointAt(0); - } - } - } - if (name.length > 0) { - return name.join(''); - } - this._seek(position); - return null; - } - - _nmtoken() { - const position = this._position; - const name = []; - let c = this._char.codePointAt(0); - while (this._nameCharRegExp.test(this._char) || (c >= 0x300 && c <= 0x36f) || (c >= 0x203F && c <= 0x2040)) { - name.push(this._char); - this._next(); - if (this._char === undefined) { - break; - } - c = this._char.codePointAt(0); - } - if (name.length > 0) { - return name.join(''); - } - this._seek(position); - return null; - } - - _entityName() { - const position = this._position; - const name = this._name(); - if (name === null) { - this._error('Expected entity name', position); - } - if (!name.endsWith(':') && name.indexOf(':') !== -1) { - this._error('Invalid colon in entity name', position); - } - return name; - } - - _entityValue() { - const quote = this._char; - this._parameterEntities = false; - this._characterData = true; - const decoder = this._decoder; - const position = this._position; - this._next(); - while (this._char !== quote) { - if (this._char === undefined) { - this._unexpected(); - } - this._next(); - } - const end = this._position; - this._parameterEntities = true; - this._seek(position); - this._next(); - const data = []; - while (this._position !== end || this._decoder !== decoder) { - if (this._char === undefined) { - this._unexpected(); - } - if (this._char === '%') { - if (this._context.length === 0) { - this._error('Invalid parameter entity reference in internal subset'); - } - this._assert(); - this._resolveParameterEntityReference(); - continue; - } - if (this._char === '&') { - data.push(this._entityReference()); - this._expect(';'); - continue; - } - - data.push(this._char); - this._next(); - } - this._next(); - this._parameterEntities = true; - this._characterData = false; - return data.join(''); - } - - _elementType(name) { - const documentType = this._document().documentType; - let elementType = documentType.elements.getNamedItem(name); - if (!elementType) { - elementType = { localName: name, characterData: false, attributes: new xml.NamedNodeMap() }; - documentType.elements.setNamedItem(elementType); - } - return elementType; - } - - _elementChildren() { - let separator = undefined; - const choice = new Set(); - for (;;) { - const name = this._name(); - if (name) { - this._assert(separator !== '|' || !choice.has(name)); - choice.add(name); - this._match('?') || this._match('*') || this._match('+'); - this._whitespace(0); - } - else if (this._match('(')) { - this._elementChildren(); - this._whitespace(0); - } - else { - this._unexpected(); - } - if (this._match(')')) { - break; - } - if (separator && separator !== this._char) { - this._unexpected(); - } - if (this._char !== '|' && this._char !== ',') { - this._unexpected(); - } - separator = this._char; - this._next(); - this._whitespace(0); - } - this._match('?') || this._match('*') || this._match('+'); - } - - _attributeDefinition() { - this._whitespace(0); - const name = this._name(); - if (name) { - this._whitespace(1); - if (this._match('CDATA') || this._match('IDREFS') || this._match('IDREF') || this._match('ID') || this._match('ENTITIES') || this._match('ENTITY') || this._match('NMTOKENS') || this._match('NMTOKEN') || - this._enumeratedType()) { - this._whitespace(1); - if (!this._match('#REQUIRED') && !this._match('#IMPLIED')) { - if (this._match('#FIXED')) { - this._whitespace(1); - } - this._parameterEntities = false; - this._attributeValue(); - this._parameterEntities = true; - } - return { localName: name }; - } - this._assert(false); - } - return null; - } - - _enumeratedType() { - if (this._match('NOTATION')) { - this._whitespace(1); - this._expect('('); - do { - this._whitespace(0); - const name = this._name(); - this._assert(name); - this._whitespace(0); - } - while (this._match('|')); - this._expect(')'); - return true; - } - if (this._match('(')) { - do { - this._whitespace(0); - const name = this._nmtoken(); - this._assert(name); - this._whitespace(0); - } - while (this._match('|')); - this._expect(')'); - return true; - } - return false; - } - - _content() { - const c = this._char !== '&' ? this._char : this._resolveEntityReference(); - if (c === undefined) { - return ''; - } - const code = c.codePointAt(0); - if ((!this._charRegExp.test(c) && (code < 0x10000 || c > 0x10FFFF))) { - this._unexpected(); - } - this._next(); - return c; - } - - _attributeValue() { - const quote = this._char; - if (quote !== '"' && quote !== "'") { - this._unexpected(); - } - this._characterData = true; - const decoder = this._decoder; - const position = this._position; - this._next(); - while (this._char !== quote) { - if (this._char === undefined || this._char === '<') { - this._unexpected(); - } - this._next(); - } - const end = this._position; - this._characterData = false; - this._seek(position); - this._next(); - const data = []; - while (this._position !== end || this._decoder !== decoder) { - if (this._char === undefined && this._context.length > 0) { - this._popContext(); - continue; - } - if (this._char === '<') { - this._unexpected(); - } - data.push(this._content()); - if (data.length > 65536) { - this._error('Invalid character data buffer size.'); - } - } - this._characterData = true; - this._next(); - return data.join(''); - } - - _validateNamespace(value) { - if (value && (value.startsWith('#') || value.indexOf(':') === -1)) { - return false; - } - if (this._version > 0) { - return true; - } - return /^[A-Za-z0-9-._~:/?#[\]@!$&'()*+,;%=]*$/.exec(value) !== null; - } - - _pubidLiteral() { - const quote = this._char; - if (quote !== '"' && quote !== "'") { - this._unexpected(); - } - this._next(); - const data = []; - while (this._char !== quote) { - if (/[a-zA-Z0-9-'()+,./:=?;!*#@$_%]/.test(this._char) || this._char === ' ' || this._char === '\r' || this._char === '\n') { - data.push(this._char); - this._next(); - if (this._char === undefined) { - this._unexpected(); - } - continue; - } - this._unexpected(); - } - this._next(); - return data.join(''); - } - - _systemLiteral() { - const quote = this._char; - if (quote !== '"' && quote !== "'") { - this._unexpected(); - } - this._next(); - const data = []; - while (this._char !== quote) { - data.push(this._char); - this._next(); - if (this._char === undefined) { - this._unexpected(); - } - } - this._next(); - const value = data.join(''); - if (value.indexOf('#') >= 0) { - this._unexpected(); - } - const match = /(.*\/)[^/]*/.exec(this._base); - return (match ? match[1] : '') + value; - } - - _terminal(terminal) { - const data = []; - while (!this._match(terminal)) { - if (this._char === undefined) { - this._unexpected(); - } - const c = this._char.codePointAt(0); - if (c !== 0x09 && c !== 0x0A && c !== 0x0D && (c < 0x20 || c > 0xD7FF) && (c < 0xE000 || c > 0xFFFD) && (c < 0x10000 || c > 0x10FFFF)) { - this._unexpected(); - } - data.push(this._char); - this._next(); - } - return data.join(''); - } - - _resolveParameterEntityReference() { - const position = this._position; - this._next(); - const name = this._name(); - this._assert(name !== null); - if (this._char === ';') { - const entity = this._document().documentType.parameterEntities.getNamedItem(name); - if (entity) { - const implicitSpace = !this._entity && !this._context.some((context) => context.entity); - if (entity.systemId) { - this._pushResource(entity.systemId, name, false); - } - else { - this._pushString(entity.value, name, false); - } - if (implicitSpace) { - this._implicitSpace = true; - } - return; - } - this._error("Undefined ENTITY '" + name + "'", position); - } - this._unexpected(); - } - - _resolveEntityReference() { - const position = this._position; - const entity = this._entityReference(); - const name = entity.substring(1, entity.length - 1); - if (name.startsWith('#x')) { - const value = parseInt(name.substring(2), 16); - return String.fromCodePoint(value); - } - else if (name.startsWith('#')) { - const value = parseInt(name.substring(1), 10); - return String.fromCodePoint(value); - } - else if (this._entities.has(name)) { - return this._entities.get(name); - } - else { - const documentType = this._document().documentType; - const entity = documentType ? documentType.entities.getNamedItem(name) : null; - if (entity) { - if (entity.systemId) { - this._pushResource(entity.systemId, name, true); - } - else { - this._pushString(entity.value, name, true); - } - } - else { - if (this._context.length !== 0 || !documentType || documentType.parameterEntities.length === 0) { - this._error("Undefined ENTITY '" + name + "'", position); - } - } - return undefined; - } - } - - /* eslint-disable consistent-return */ - _entityReference() { - if (this._char === '&') { - const position = this._position; - this._next(); - if (this._match('#x')) { - const data = []; - while (/[0-9a-fA-F]/.test(this._char)) { - data.push(this._char); - this._next(); - if (this._char === undefined) { - this._unexpected(); - } - } - this._assert(this._char === ';'); - if (data.length > 0) { - const text = data.join(''); - const value = parseInt(text, 16); - this._assert(value <= 0x10FFFF, "Invalid value '&#x" + text + ";'", position); - return '&#x' + text + ';'; - } - } - else if (this._match('#')) { - const data = []; - while (/[0-9]/.test(this._char)) { - data.push(this._char); - this._next(); - if (this._char === undefined) { - this._unexpected(); - } - } - this._assert(this._char === ';'); - if (data.length > 0) { - const text = data.join(''); - const value = parseInt(text, 10); - this._assert(value <= 0x10FFFF, "Invalid value '&#" + text + ";'", position); - return '&#' + text + ';'; - } - } - else { - const name = this._name(); - this._assert(name !== null); - this._assert(this._char === ';'); - return '&' + name + ';'; - } - } - this._unexpected(); - } - /* eslint-enable consistent-return */ - - _comment() { - const data = this._terminal('--'); - const node = this._document().createComment(data); - this._appendChild(node); - this._expect('>'); - } - - _processingInstruction() { - this._next(); - const name = this._entityName(); - let whitespace = this._char === '?' ? false : this._whitespace(1); - const position = this._position; - const data = this._terminal('?>'); - if (name.toLowerCase() === 'xml') { - this._seek(position); - this._assert(name === 'xml', "'" + name + "' must be lower case"); - this._assert(this._start === this._prolog, "Prolog must start with XML declaration", this._start); - this._assert(typeof this._data !== 'string', 'Invalid text declaration', this._start); - const obj = { version: '', encoding: '', standalone: 'no' }; - for (const name of Object.keys(obj)) { - const expect = (name == 'version' && this._context.length === 0) || (name == 'encoding' && this._context.length > 0); - if ((whitespace || expect) && (expect ? this._expect(name) : this._match(name))) { - this._whitespace(0); - this._expect('='); - this._whitespace(0); - obj[name] = this._attributeValue(); - whitespace = this._whitespace(0); - } - } - this._expect('?>'); - obj.encoding = obj.encoding.toLowerCase(); - if (this._decoder.encoding && obj.encoding !== this._decoder.encoding) { - const position = this._position; - this._decoder = text.Decoder.open(this._data, obj.encoding); - this._seek(position); - } - if (obj.version.length > 0) { - const match = /^(\d)\.(\d)$/.exec(obj.version); - this._assert(match && match[1] === '1', "Invalid XML version '" + obj.version + "'"); - const version = Number.parseInt(match[2], 10); - if (version > this._version) { - /* eslint-disable */ - this._charRegExp = /[\x01-\uD7FF\uE000-\uFFFD]/; - /* eslint-enable */ - this._version = version; - } - this._assert(this._context.length === 0 || this._context.some((context) => context.version >= this._version)); - } - this._assert(obj.standalone === 'no' || (obj.standalone === 'yes' && !this._entity && this._context.length === 0)); - this._standalone = obj.standalone === 'yes'; - } - const node = this._document().createProcessingInstruction(name, data); - this._appendChild(node); - } - - _whitespace(count) { - const position = this._position; - let index = 0; - if (this._implicitSpace) { - index++; - this._implicitSpace = false; - } - while (this._char === ' ' || this._char === '\n' || this._char === '\r' || this._char === '\t' || (this._version > 0 && this._char === '\x85')) { - index++; - this._next(); - } - if (index < count) { - this._seek(position); - this._unexpected(); - } - return index > 0; - } - - _pushResource(identifier, entity, stop) { - const content = this._callback(identifier); - this._pushBuffer(content, identifier, entity, stop); - } - - _pushBuffer(data, base, entity, stop) { - const signature = text.Decoder.open(data); - const decoder = signature.encoding === 'utf-8' ? text.Decoder.open(data, 'utf-8') : signature; - this._pushContext(decoder, data, base, entity, stop, false); - this._data = data; - } - - _pushString(value, entity, stop) { - const decoder = text.Decoder.open(value); - this._pushContext(decoder, value, this._base, entity, stop); - } - - _pushContext(decoder, data, base, entity, stop) { - if (this._context.some((context) => context && context.base === base && context.entity === entity)) { - this._assert(!entity, "Recursive entity '" + entity + "'"); - this._assert(!base, "Recursive base '" + base + "'"); - } - if (base.length !== 0 || entity.length !== 0) { - this._context.push(this._state); - } - this._stop = stop; - this._entity = entity; - this._base = base; - this._data = data; - this._decoder = decoder; - this._prolog = this._decoder.position; - this._char = ''; - this._next(); - } - - _popContext() { - const entity = this._entity; - this._state = this._context.pop(); - if (entity) { - this._expect(';'); - this._implicitSpace = !this._context.some((context) => context.entity); - } - } - - get _state() { - return { - base: this._base, - data: this._data, - decoder: this._decoder, - position: this._position, - version: this._version, - entity: this._entity, - prolog: this._prolog, - stop: this._stop, - }; - } - - set _state(value) { - this._stop = value.stop; - this._base = value.base; - this._data = value.data; - this._decoder = value.decoder; - this._seek(value.position); - this._version = value.version; - this._entity = value.entity; - this._prolog = value.prolog; - } - - _next() { - if (this._char === undefined) { - this._unexpected(); - } - this._position = this._decoder.position; - this._char = this._decoder.decode(); - this._implicitSpace = false; - if (this._parameterEntities && this._char === '%' && (this._entity || this._base)) { - this._resolveParameterEntityReference(); - } - if (!this._characterData) { - if (this._char === '&' && (this._entity || this._base)) { - const c = this._resolveEntityReference(); - if (c !== undefined) { - this._char = c; - } - } - } - if (this._char === '\uffff' || this._char === '\ufffe' || (this._version > 0 && this._char >= '\x7f' && this._char <= '\x9f' && this._char != '\x85')) { - this._unexpected(); - } - if (this._char === undefined) { - if (!this._stop && this._context.length > 0) { - this._popContext(); - } - } - } - - _seek(position) { - this._decoder.position = position; - this._char = ''; - this._next(); - } - - _expect(value) { - if (!this._match(value)) { - this._unexpected(); - } - return true; - } - - _match(value) { - if (this._char !== value[0]) { - return false; - } - if (value.length === 1) { - this._next(); - return true; - } - if (this._context.length === 0) { - const position = this._position; - for (let i = 0; i < value.length; i++) { - if (this._char !== value[i]) { - this._seek(position); - return false; - } - this._next(); - } - return true; - } - const context = Array.from(this._context); - const state = this._state; - for (let i = 0; i < value.length; i++) { - if (this._char !== value[i]) { - this._context = context; - this._state = state; - return false; - } - this._next(); - } - return true; - } - - _assert(value, message, position) { - if (value === false || value === undefined || value === null) { - this._error(message, position); - } - } - - _error(message, position) { - if (position) { - this._parameterEntities = false; - this._characterData = true; - this._seek(position); - } - if (message) { - throw new xml.Error(message + this._location()); - } - this._unexpected(); - } - - _unexpected() { - let c = this._char; - if (c === undefined) { - throw new xml.Error('Unexpected end of XML input.'); - } - else if (c === '"') { - c = 'string'; - } - else if ((c >= '0' && c <= '9') || c === '-') { - c = 'number'; - } - else { - if (c < ' ' || c > '\x7F') { - c = c.codePointAt(0); - if (c < 0x0100) { - c = '\\x' + ('0' + c.toString(16)).slice(-2); - } - else if (c < 0x010000) { - c = '\\u' + ('000' + c.toString(16)).slice(-4); - } - else { - c = '\\u' + ('00000' + c.toString(16)).slice(-6); - } - } - c = "token '" + c + "'"; - } - this._error('Unexpected ' + c); - } - - _location() { - while (typeof this._data === 'string') { - this._popContext(); - } - this._parameterEntities = false; - this._characterData = true; - let line = 1; - let column = 1; - this._decoder.position = 0; - let c; - do { - if (this._decoder.position === this._position) { - break; - } - c = this._decoder.decode(); - if (c === '\n') { - line++; - column = 1; - } - else { - column++; - } - } - while (c !== undefined); - return ' at ' + (this._base ? this._base + ':' : '') + line.toString() + ':' + column.toString() + '.'; - } -}; - -xml.NodeList = class extends Array { - - constructor() { - super(); - } - - item(index) { - return this[index] || null; - } -}; - -xml.Node = class { - - constructor(document, nodeType) { - this._ownerDocument = document; - this._nodeType = nodeType; - this._childNodes = new xml.NodeList(); - } - - get ownerDocument() { - return this._ownerDocument; - } - - get nodeType() { - return this._nodeType; - } - - get localName() { - throw new xml.Error('Not implemented.'); - } - - get namespaceURI() { - return null; - } - - get childNodes() { - return this._childNodes; - } - - get parentNode() { - return this._parentNode; - } - - set parentNode(value) { - this._parentNode = value; - } - - get firstChild() { - return this._firstChild; - } - - set firstChild(value) { - this._firstChild = value; - } - - get lastChild() { - return this._lastChild || null; - } - - set lastChild(value) { - this._lastChild = value; - } - - get previousSibling() { - return this._previousSibling; - } - - set previousSibling(value) { - this._previousSibling = value; - } - - get nextSibling() { - return this._nextSibling; - } - - set nextSibling(value) { - this._nextSibling = value; - } - - appendChild(newChild) { - this.firstChild = this.firstChild || newChild; - newChild.previousSibling = this.lastChild; - if (newChild.previousSibling) { - newChild.previousSibling.nextSibling = newChild; - } - this.lastChild = newChild; - this.childNodes[this.childNodes.length] = newChild; - newChild.parentNode = this; - } - - lookupNamespaceURI(prefix) { - switch (prefix) { - case 'xml': - return 'http://www.w3.org/XML/1998/namespace'; - case 'xmlns': - return 'http://www.w3.org/2000/xmlns/'; - default: - return null; - } - } -}; - -xml.Element = class extends xml.Node { - - constructor(document, namespaceURI, qualifiedName) { - super(document, xml.NodeType.Element); - this._namespaces = new Map(); - this._attributes = new xml.NamedNodeMap(); - this._namespaceURI = namespaceURI; - if (namespaceURI === null) { - this._prefix = null; - this._localName = qualifiedName; - } - else { - const pair = xml.Utility.split(qualifiedName); - this._prefix = pair[0]; - this._localName = pair[1]; - } - } - - get localName() { - return this._localName; - } - - get prefix() { - return this._prefix; - } - - get namespaceURI() { - return this._namespaceURI; - } - - get attributes() { - return this._attributes; - } - - get textContent() { - return this.childNodes.map((node) => node.nodeType === xml.NodeType.ProcessingInstruction || node.nodeType === xml.NodeType.Comment ? '' : node.textContent).join(''); - } - - getElementsByTagName(tagName) { - const list = new xml.NodeList(); - let node = this.firstChild; - while (node) { - if (node.nodeType === xml.NodeType.Element && (tagName === '*' || tagName === (node.prefix ? node.prefix + ':' + node.localName : node.localName))) { - list.push(node); - } - node = node.nextSibling; - } - return list; - } - - getAttribute(name) { - const node = this.getAttributeNode(name); - return node ? node.value || '' : ''; - } - - getAttributeNode(name) { - return this.attributes.getNamedItem(name); - } - - setAttributeNode(node) { - const oldNode = this.attributes.setNamedItem(node); - if (node.namespaceURI === 'http://www.w3.org/2000/xmlns/') { - const prefix = node.prefix ? node.localName : ''; - this._namespaces.set(prefix, node.value); - } - return oldNode; - } - - lookupNamespaceURI(prefix) { - if (this._namespaces.has(prefix)) { - return this._namespaces.get(prefix); - } - if (this.parentNode) { - return this.parentNode.lookupNamespaceURI(prefix); - } - return super.lookupNamespaceURI(prefix); - } -}; - -xml.Attribute = class extends xml.Node { - - constructor(document, namespaceURI, qualifiedName) { - super(document, xml.NodeType.Attribute); - this._namespaceURI = namespaceURI; - if (namespaceURI === null) { - this._prefix = null; - this._localName = qualifiedName; - } - else { - const pair = xml.Utility.split(qualifiedName); - this._prefix = pair[0]; - this._localName = pair[1]; - } - } - - get ownerElement() { - return this._ownerElement; - } - - set ownerElement(value) { - this._ownerElement = value; - } - - get localName() { - return this._localName; - } - - get prefix() { - return this._prefix; - } - - get namespaceURI() { - return this._namespaceURI; - } - - get value() { - return this._value; - } - - set value(value) { - this._value = value; - } -}; - -xml.CharacterData = class extends xml.Node { - - constructor(document, nodeType, data) { - super(document, nodeType); - this._data = data; - } - - get data() { - return this._data; - } - - get textContent() { - return this._data; - } -}; - -xml.Text = class extends xml.CharacterData { - - constructor(document, data) { - super(document, xml.NodeType.Text, data); - } - - get localName() { - return '#text'; - } -}; - -xml.CDataSection = class extends xml.CharacterData { - - constructor(document, data) { - super(document, xml.NodeType.CDATA, data); - } -}; - -xml.Entity = class extends xml.Node { - - constructor(document, name) { - super(document, xml.NodeType.Entity); - this._name = name; - this._publicId = ''; - this._systemId = ''; - this._notationName = ''; - this._value = ''; - } - - get localName() { - return this._name; - } - - get publicId() { - return this._publicId; - } - - set publicId(value) { - this._publicId = value; - } - - get systemId() { - return this._systemId; - } - - set systemId(value) { - this._systemId = value; - } - - get notationName() { - return this._notationName; - } - - set notationName(value) { - this._notationName = value; - } - - set value(value) { - this._value = value; - } - - get value() { - return this._value; - } -}; - -xml.ProcessingInstruction = class extends xml.Node { - - constructor(document, target, data) { - super(document, xml.NodeType.ProcessingInstruction); - this._target = target; - this._data = data; - } - - get localName() { - return this._target; - } - - get target() { - return this._target; - } - - get data() { - return this._data; - } -}; - -xml.Comment = class extends xml.CharacterData { - - constructor(document, data) { - super(document, xml.NodeType.Comment, data); - } - - get localName() { - return '#comment'; - } -}; - -xml.Document = class extends xml.Node { - - constructor() { - super(null, xml.NodeType.Document); - this._documentElement = null; - this._documentType = null; - } - - get documentElement() { - return this._documentElement; - } - - get documentType() { - return this._documentType; - } - - appendChild(newChild) { - super.appendChild(newChild); - if (newChild.nodeType === xml.NodeType.Element) { - this._documentElement = newChild; - } - if (newChild.nodeType === xml.NodeType.DocumentType) { - this._documentType = newChild; - } - } - - createElement(localName) { - return new xml.Element(this, null, localName); - } - - createElementNS(namespaceURI, qualifiedName) { - return new xml.Element(this, namespaceURI, qualifiedName); - } - - createAttribute(localName) { - return new xml.Attribute(this, null, localName); - } - - createAttributeNS(namespaceURI, qualifiedName) { - return new xml.Attribute(this, namespaceURI, qualifiedName); - } - - createTextNode(data) { - return new xml.Text(this, data); - } - - createCDATASection(data) { - return new xml.CDataSection(this, data); - } - - createProcessingInstruction(target, data) { - return new xml.ProcessingInstruction(this, target, data); - } - - createComment(data) { - return new xml.Comment(this, data); - } - - createDocumentType(qualifiedName, publicId, systemId) { - return new xml.DocumentType(this, qualifiedName, publicId, systemId); - } -}; - -xml.DocumentType = class extends xml.Node { - - constructor(document, qualifiedName, publicId, systemId) { - super(document, xml.NodeType.DocumentType); - this._name = qualifiedName; - this._publicId = publicId; - this._systemId = systemId; - this._entities = new xml.NamedNodeMap(); - } - - get name() { - return this._name; - } - - get publicId() { - return this._publicId; - } - - get systemId() { - return this._systemId; - } - - get entities() { - return this._entities; - } - - appendChild(newChild) { - if (newChild.nodeType === xml.NodeType.Entity) { - this._entities.setNamedItem(newChild); - } - } - - createEntity(name) { - return new xml.Entity(this.ownerDocument, name); - } -}; - -xml.NamedNodeMap = class extends Array { - - getNamedItem(qualifiedName) { - for (let i = this.length - 1; i >= 0; i--) { - const node = this[i]; - const key = node.prefix ? node.prefix + ':' + node.localName : node.localName; - if (qualifiedName == key) { - return node; - } - } - return null; - } - - getNamedItemNS(namespaceURI, localName) { - for (let i = this.length - 1; i >= 0; i--) { - const node = this[i]; - if (localName === node.localName && namespaceURI == node.namespaceURI) { - return node; - } - } - return null; - } - - setNamedItem(node) { - const qualifiedName = node.prefix ? node.prefix + ':' + node.localName : node.localName; - for (let i = this.length - 1; i >= 0; i--) { - const node = this[i]; - const key = node.prefix ? node.prefix + ':' + node.localName : node.localName; - if (qualifiedName == key) { - const oldNode = this[i]; - this[i] = node; - return oldNode; - } - } - this.push(node); - return null; - } -}; - -xml.NodeType = { - None: 0, - Element: 1, - Attribute: 2, - Text: 3, - CDATA: 4, - EntityReference: 5, - Entity: 6, - ProcessingInstruction: 7, - Comment: 8, - Document: 9, - DocumentType: 10, - DocumentFragment: 11, - Notation: 12 -}; - -xml.Utility = class { - - static split(name) { - const index = name.indexOf(':'); - if (index < 0 || index === name.length - 1) { - return [ null, name ]; - } - const localName = name.substring(index + 1); - const c = localName.codePointAt(0); - if (localName.indexOf(':') !== -1 || !xml.Utility.nameStartCharRegExp.test(String.fromCodePoint(c)) && (c < 0x10000 || c > 0xEFFFF)) { - return [ null, name ]; - } - const prefix = name.substring(0, index); - return [ prefix, localName ]; - } -}; - -xml.Error = class extends Error { - - constructor(message) { - super(message); - this.name = 'XML Error'; - } -}; - -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.TextReader = xml.TextReader; -} diff --git a/media/CircleEditor/external/zip.js b/media/CircleEditor/external/zip.js deleted file mode 100644 index 2d929133..00000000 --- a/media/CircleEditor/external/zip.js +++ /dev/null @@ -1,713 +0,0 @@ - -var zip = zip || {}; -var zlib = zlib || {}; - -zip.Archive = class { - - static open(data) { - const stream = data instanceof Uint8Array ? new zip.BinaryReader(data) : data; - if (stream.length > 2) { - const buffer = stream.peek(2); - if (buffer[0] === 0x78) { // zlib - const check = (buffer[0] << 8) + buffer[1]; - if (check % 31 === 0) { - return new zlib.Archive(stream); - } - } - const signature = buffer[0] === 0x50 && buffer[1] === 0x4B; - const position = stream.position; - const seek = (content) => { - let position = stream.length; - do { - position = Math.max(0, position - 66000); - stream.seek(position); - const length = Math.min(stream.length - position, 66000 + 4); - const buffer = stream.read(length); - for (let i = buffer.length - 4; i >= 0; i--) { - if (content[0] === buffer[i] && content[1] === buffer[i + 1] && content[2] === buffer[i + 2] && content[3] === buffer[i + 3]) { - stream.seek(position + i + 4); - return true; - } - } - if (!signature) { - break; - } - } - while (position > 0); - return false; - }; - if (!seek([ 0x50, 0x4B, 0x05, 0x06 ])) { - stream.seek(position); - if (!signature) { - return null; - } - throw new zip.Error('End of Zip central directory not found.'); - } - const reader = new zip.BinaryReader(stream.read(16)); - reader.skip(12); - let offset = reader.uint32(); // central directory offset - if (offset > stream.length) { - if (!seek([ 0x50, 0x4B, 0x06, 0x06 ])) { - stream.seek(position); - throw new zip.Error('End of Zip64 central directory not found.'); - } - const reader = new zip.BinaryReader(stream.read(52)); - reader.skip(44); - offset = reader.uint32(); - if (reader.uint32() !== 0) { - stream.seek(position); - throw new zip.Error('Zip 64-bit central directory offset not supported.'); - } - } - if (offset > stream.length) { - stream.seek(position); - throw new zip.Error('Invalid Zip central directory offset.'); - } - stream.seek(offset); - const archive = new zip.Archive(stream); - stream.seek(position); - return archive; - } - return null; - } - - constructor(stream) { - this._entries = new Map(); - const headers = []; - const signature = [ 0x50, 0x4B, 0x01, 0x02 ]; - while (stream.position + 4 < stream.length && stream.read(4).every((value, index) => value === signature[index])) { - const header = {}; - const reader = new zip.BinaryReader(stream.read(42)); - reader.uint16(); // version made by - reader.skip(2); // version needed to extract - const flags = reader.uint16(); - if ((flags & 1) == 1) { - throw new zip.Error('Encrypted Zip entries not supported.'); - } - header.encoding = flags & 0x800 ? 'utf-8' : 'ascii'; - header.compressionMethod = reader.uint16(); - reader.uint32(); // date - reader.uint32(); // crc32 - header.compressedSize = reader.uint32(); - header.size = reader.uint32(); - header.nameLength = reader.uint16(); // file name length - const extraDataLength = reader.uint16(); - const commentLength = reader.uint16(); - header.disk = reader.uint16(); // disk number start - reader.uint16(); // internal file attributes - reader.uint32(); // external file attributes - header.localHeaderOffset = reader.uint32(); - const nameBuffer = stream.read(header.nameLength); - const decoder = new TextDecoder(header.encoding); - header.name = decoder.decode(nameBuffer); - const extraData = stream.read(extraDataLength); - if (extraData.length > 0) { - const reader = new zip.BinaryReader(extraData); - while (reader.position < reader.length) { - const type = reader.uint16(); - const length = reader.uint16(); - switch (type) { - case 0x0001: - if (header.size === 0xffffffff) { - header.size = reader.uint32(); - if (reader.uint32() !== 0) { - throw new zip.Error('Zip 64-bit offset not supported.'); - } - } - if (header.compressedSize === 0xffffffff) { - header.compressedSize = reader.uint32(); - if (reader.uint32() !== 0) { - throw new zip.Error('Zip 64-bit offset not supported.'); - } - } - if (header.localHeaderOffset === 0xffffffff) { - header.localHeaderOffset = reader.uint32(); - if (reader.uint32() !== 0) { - throw new zip.Error('Zip 64-bit offset not supported.'); - } - } - if (header.disk === 0xffff) { - header.disk = reader.uint32(); - } - break; - default: - reader.skip(length); - break; - } - } - } - stream.read(commentLength); // comment - headers.push(header); - } - for (const header of headers) { - if (header.size === 0 && header.name.endsWith('/')) { - continue; - } - const entry = new zip.Entry(stream, header); - this._entries.set(entry.name, entry.stream); - } - } - - get entries() { - return this._entries; - } -}; - -zip.Entry = class { - - constructor(stream, header) { - stream.seek(header.localHeaderOffset); - const signature = [ 0x50, 0x4B, 0x03, 0x04 ]; - if (stream.position + 4 > stream.length || !stream.read(4).every((value, index) => value === signature[index])) { - throw new zip.Error('Invalid Zip local file header signature.'); - } - const reader = new zip.BinaryReader(stream.read(26)); - reader.skip(22); - header.nameLength = reader.uint16(); - const extraDataLength = reader.uint16(); - header.nameBuffer = stream.read(header.nameLength); - stream.skip(extraDataLength); - const decoder = new TextDecoder(header.encoding); - this._name = decoder.decode(header.nameBuffer); - this._stream = stream.stream(header.compressedSize); - switch (header.compressionMethod) { - case 0: { // stored - if (header.size !== header.compressedSize) { - throw new zip.Error('Invalid compression size.'); - } - break; - } - case 8: { // deflate - this._stream = new zip.InflaterStream(this._stream, header.size); - break; - } - default: - throw new zip.Error('Invalid compression method.'); - } - } - - get name() { - return this._name; - } - - get stream() { - return this._stream; - } -}; - -zip.Inflater = class { - - inflateRaw(data, length) { - let buffer = null; - if (typeof process === 'object' && typeof process.versions == 'object' && typeof process.versions.node !== 'undefined') { - buffer = require('zlib').inflateRawSync(data); - } - else { - const reader = new zip.BitReader(data); - const writer = length === undefined ? new zip.BlockWriter() : new zip.BufferWriter(length); - if (!zip.Inflater._staticLengthTree) { - zip.Inflater._codeLengths = new Uint8Array(19); - zip.Inflater._codeOrder = [ 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 ]; - zip.Inflater._lengthBase = [ 24, 32, 40, 48, 56, 64, 72, 80, 89, 105, 121, 137, 154, 186, 218, 250, 283, 347, 411, 475, 540, 668, 796, 924, 1053, 1309, 1565, 1821, 2064, 7992, 7992, 7992 ]; - zip.Inflater._distanceBase = [ 16, 32, 48, 64, 81, 113, 146, 210, 275, 403, 532, 788, 1045, 1557, 2070, 3094, 4119, 6167, 8216, 12312, 16409, 24601, 32794, 49178, 65563, 98331, 131100, 196636, 262173, 393245, 1048560, 1048560 ]; - } - let type; - do { - type = reader.bits(3); - switch (type >>> 1) { - case 0: { // uncompressed block - this._copyUncompressedBlock(reader, writer); - break; - } - case 1: { // block with fixed huffman trees - if (!zip.Inflater._staticLengthTree) { - zip.Inflater._staticLengthTree = zip.HuffmanTree.create(new Uint8Array([].concat.apply([], [[144, 8], [112, 9], [24, 7], [8, 8]].map((x) => [...Array(x[0])].map(() => x[1]))))); - zip.Inflater._staticDistanceTree = zip.HuffmanTree.create(new Uint8Array([...Array(32)].map(() => 5))); - } - this._lengthTree = zip.Inflater._staticLengthTree; - this._distanceTree = zip.Inflater._staticDistanceTree; - this._inflateBlock(reader, writer); - break; - } - case 2: { // block with dynamic huffman trees - this._decodeTrees(reader); - this._inflateBlock(reader, writer); - break; - } - default: { - throw new zip.Error('Unsupported block type.'); - } - } - } while ((type & 1) == 0); - if (length !== undefined && length !== writer.length) { - throw new zip.Error('Invalid uncompressed size.'); - } - buffer = writer.toBuffer(); - } - if (length !== undefined && length !== buffer.length) { - throw new zip.Error('Invalid uncompressed size.'); - } - return buffer; - } - - _copyUncompressedBlock(reader, writer) { - const length = reader.uint16(); - const inverseLength = reader.uint16(); - if (length !== (~inverseLength & 0xffff)) { - throw new zip.Error('Invalid uncompressed block length.'); - } - writer.write(reader.read(length)); - } - - _decodeTrees(reader) { - const hlit = reader.bits(5) + 257; - const hdist = reader.bits(5) + 1; - const hclen = reader.bits(4) + 4; - const codeLengths = zip.Inflater._codeLengths; - for (let i = 0; i < codeLengths.length; i++) { - codeLengths[i] = 0; - } - const codeOrder = zip.Inflater._codeOrder; - for (let i = 0; i < hclen; i++) { - codeLengths[codeOrder[i]] = reader.bits(3); - } - const codeTree = zip.HuffmanTree.create(codeLengths); - const codeMask = codeTree.length - 1; - const lengths = new Uint8Array(hlit + hdist); - let value = 0; - let length = 0; - for (let i = 0; i < hlit + hdist;) { - const code = codeTree[reader.bits16() & codeMask]; - reader.position += code & 0x0f; - const literal = code >>> 4; - switch (literal) { - case 16: length = reader.bits(2) + 3; break; - case 17: length = reader.bits(3) + 3; value = 0; break; - case 18: length = reader.bits(7) + 11; value = 0; break; - default: length = 1; value = literal; break; - } - for (; length > 0; length--) { - lengths[i++] = value; - } - } - this._lengthTree = zip.HuffmanTree.create(lengths.subarray(0, hlit)); - this._distanceTree = zip.HuffmanTree.create(lengths.subarray(hlit, hlit + hdist)); - } - - _inflateBlock(reader, writer) { - const lengthTree = this._lengthTree; - const distanceTree = this._distanceTree; - const lengthMask = lengthTree.length - 1; - const distanceMask = distanceTree.length - 1; - const buffer = writer.buffer; - const threshold = writer.threshold !== undefined ? writer.threshold : writer.length; - let position = writer.position; - for (;;) { - if (position > threshold) { - position = writer.push(position); - } - const code = lengthTree[reader.bits16() & lengthMask]; - reader.position += code & 0x0f; - const literal = code >>> 4; - if (literal < 256) { - buffer[position++] = literal; - } - else if (literal === 256) { - writer.push(position); - return; - } - else { - let length = literal - 254; - if (literal > 264) { - const lengthBase = zip.Inflater._lengthBase[literal - 257]; - length = (lengthBase >>> 3) + reader.bits(lengthBase & 0x07); - } - const code = distanceTree[reader.bits16() & distanceMask]; - reader.position += code & 0x0f; - const distanceBase = zip.Inflater._distanceBase[code >>> 4]; - const bits = distanceBase & 0x0f; - const distance = (distanceBase >>> 4) + (reader.bits16() & ((1 << bits) - 1)); - reader.position += bits; - let offset = position - distance; - for (let i = 0; i < length; i++) { - buffer[position++] = buffer[offset++]; - } - } - } - } -}; - -zip.HuffmanTree = class { - - static create(tree) { - let bits = tree[0]; - for (let i = 1; i < tree.length; ++i) { - if (tree[i] > bits) { - bits = tree[i]; - } - } - // Algorithm from https://github.com/photopea/UZIP.js - let rev15 = zip.HuffmanTree._rev15; - if (!rev15) { - const length = 1 << 15; - rev15 = new Uint16Array(length); - for (let i = 0; i < length; i++) { - let x = i; - x = (((x & 0xaaaaaaaa) >>> 1) | ((x & 0x55555555) << 1)); - x = (((x & 0xcccccccc) >>> 2) | ((x & 0x33333333) << 2)); - x = (((x & 0xf0f0f0f0) >>> 4) | ((x & 0x0f0f0f0f) << 4)); - x = (((x & 0xff00ff00) >>> 8) | ((x & 0x00ff00ff) << 8)); - rev15[i] = (((x >>> 16) | (x << 16))) >>> 17; - } - zip.HuffmanTree._rev15 = rev15; - zip.HuffmanTree._bitLengthCounts = new Uint16Array(16); - zip.HuffmanTree._nextCodes = new Uint16Array(16); - } - const length = tree.length; - const bitLengthCounts = zip.HuffmanTree._bitLengthCounts; - for (let i = 0; i < 16; i++) { - bitLengthCounts[i] = 0; - } - for (let i = 0; i < length; i++) { - bitLengthCounts[tree[i]]++; - } - const nextCodes = zip.HuffmanTree._nextCodes; - let code = 0; - bitLengthCounts[0] = 0; - for (let i = 0; i < bits; i++) { - code = (code + bitLengthCounts[i]) << 1; - nextCodes[i + 1] = code; - } - const codes = new Uint16Array(length); - for (let i = 0; i < length; i++) { - const index = tree[i]; - if (index !== 0) { - codes[i] = nextCodes[index]; - nextCodes[index]++; - } - } - const shift = 15 - bits; - const table = new Uint16Array(1 << bits); - for (let i = 0; i < length; i++) { - const c = tree[i]; - if (c !== 0) { - const value = (i << 4) | c; - const rest = bits - c; - let index = codes[i] << rest; - const max = index + (1 << rest); - for (; index != max; index++) { - table[rev15[index] >>> shift] = value; - } - } - } - return table; - } -}; - -zip.BitReader = class { - - constructor(buffer) { - this.buffer = buffer; - this.position = 0; - } - - bits(count) { - const offset = (this.position / 8) >> 0; - const shift = (this.position & 7); - this.position += count; - return ((this.buffer[offset] | (this.buffer[offset + 1] << 8)) >>> shift) & ((1 << count) - 1); - } - - bits16() { - const offset = (this.position / 8) >> 0; - return ((this.buffer[offset] | (this.buffer[offset + 1] << 8) | (this.buffer[offset + 2] << 16)) >>> (this.position & 7)); - } - - read(length) { - this.position = (this.position + 7) & ~7; // align - const offset = (this.position / 8) >> 0; - this.position += length * 8; - return this.buffer.subarray(offset, offset + length); - } - - uint16() { - this.position = (this.position + 7) & ~7; // align - const offset = (this.position / 8) >> 0; - this.position += 16; - return this.buffer[offset] | (this.buffer[offset + 1] << 8); - } -}; - -zip.BlockWriter = class { - - constructor() { - this.blocks = []; - this.buffer = new Uint8Array(65536); - this.position = 0; - this.length = 0; - this.threshold = 0xf400; - } - - push(position) { - this.blocks.push(new Uint8Array(this.buffer.subarray(this.position, position))); - this.length += position - this.position; - this.position = position; - return this._reset(); - } - - write(buffer) { - this.blocks.push(buffer); - const length = buffer.length; - this.length += length; - if (length > 32768) { - this.buffer.set(buffer.subarray(length - 32768, length), 0); - this.position = 32768; - } - else { - this._reset(); - this.buffer.set(buffer, this.position); - this.position += length; - } - } - - toBuffer() { - const buffer = new Uint8Array(this.length); - let offset = 0; - for (const block of this.blocks) { - buffer.set(block, offset); - offset += block.length; - } - return buffer; - } - - _reset() { - if (this.position > 32768) { - this.buffer.set(this.buffer.subarray(this.position - 32768, this.position), 0); - this.position = 32768; - } - return this.position; - } -}; - -zip.BufferWriter = class { - - constructor(length) { - this.buffer = new Uint8Array(length); - this.length = length; - this.position = 0; - } - - - push(position) { - this.position = position; - if (this.position > this.length) { - throw new zip.Error('Invalid size.'); - } - return this.position; - } - - write(buffer) { - this.buffer.set(buffer, this.position); - this.position += buffer.length; - if (this.position > this.length) { - throw new zip.Error('Invalid size.'); - } - return this.position; - } - - toBuffer() { - return this.buffer; - } -}; - -zip.InflaterStream = class { - - constructor(stream, length) { - this._stream = stream; - this._offset = this._stream.position; - this._position = 0; - this._length = length; - } - - get position() { - return this._position; - } - - get length() { - if (this._length === undefined) { - this._inflate(); - } - return this._length; - } - - seek(position) { - if (this._buffer === undefined) { - this._inflate(); - } - this._position = position >= 0 ? position : this._length + position; - } - - skip(offset) { - if (this._buffer === undefined) { - this._inflate(); - } - this._position += offset; - } - - peek(length) { - const position = this._position; - length = length !== undefined ? length : this._length - position; - this.skip(length); - const end = this._position; - this.seek(position); - if (position === 0 && length === this._length) { - return this._buffer; - } - return this._buffer.subarray(position, end); - } - - read(length) { - const position = this._position; - length = length !== undefined ? length : this._length - position; - this.skip(length); - if (position === 0 && length === this._length) { - return this._buffer; - } - return this._buffer.subarray(position, this._position); - } - - byte() { - const position = this._position; - this.skip(1); - return this._buffer[position]; - } - - _inflate() { - if (this._buffer === undefined) { - const position = this._stream.position; - this._stream.seek(this._offset); - const buffer = this._stream.peek(); - this._buffer = new zip.Inflater().inflateRaw(buffer, this._length); - this._length = this._buffer.length; - this._stream.seek(position); - delete this._stream; - } - } -}; - -zip.BinaryReader = class { - - constructor(buffer) { - this._buffer = buffer; - this._length = buffer.length; - this._position = 0; - this._view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength); - } - - get position() { - return this._position; - } - - get length() { - return this._length; - } - - create(buffer) { - return new zip.BinaryReader(buffer); - } - - stream(length) { - return this.create(this.read(length)); - } - - seek(position) { - this._position = position >= 0 ? position : this._length + position; - } - - skip(offset) { - this._position += offset; - } - - peek(length) { - if (this._position === 0 && length === undefined) { - return this._buffer; - } - const position = this._position; - this.skip(length !== undefined ? length : this._length - this._position); - const end = this._position; - this.seek(position); - return this._buffer.subarray(position, end); - } - - read(length) { - if (this._position === 0 && length === undefined) { - this._position = this._length; - return this._buffer; - } - const position = this._position; - this.skip(length !== undefined ? length : this._length - this._position); - return this._buffer.subarray(position, this._position); - } - - byte() { - const position = this._position; - this.skip(1); - return this._buffer[position]; - } - - uint16() { - const position = this._position; - this.skip(2); - return this._view.getUint16(position, true); - } - - uint32() { - const position = this._position; - this.skip(4); - return this._view.getUint32(position, true); - } -}; - -zlib.Archive = class { - - constructor(stream) { - const position = stream.position; - stream.read(2); - const entry = new zlib.Entry(stream); - this._entries = new Map([ [ entry.name, entry.stream ] ]); - stream.seek(position); - } - - get entries() { - return this._entries; - } -}; - -zlib.Entry = class { - - constructor(stream) { - this._stream = new zip.InflaterStream(stream); - } - - get name() { - return ''; - } - - get stream() { - return this._stream; - } -}; - -zip.Error = class extends Error { - - constructor(message) { - super(message); - this.name = 'Zip Error'; - this.stack = undefined; - } -}; - -if (typeof module !== 'undefined' && typeof module.exports === 'object') { - module.exports.Archive = zip.Archive; - module.exports.Inflater = zip.Inflater; -} From 711e2913925ea8ccfa839dccfc3afa9b7be04a3a Mon Sep 17 00:00:00 2001 From: Seok NamKoong Date: Tue, 8 Nov 2022 11:11:17 +0900 Subject: [PATCH 03/10] base 3 --- .ahub/sam/exclude.txt | 1 + media/CircleEditor/index.html | 5 +- media/CircleEditor/index.js | 505 +-------- media/CircleEditor/view-grapher.js | 762 ------------- media/CircleEditor/view-sidebar.js | 1612 +--------------------------- media/CircleEditor/view.js | 1015 +----------------- 6 files changed, 28 insertions(+), 3872 deletions(-) delete mode 100644 media/CircleEditor/view-grapher.js diff --git a/.ahub/sam/exclude.txt b/.ahub/sam/exclude.txt index 048afb7e..07f85c91 100644 --- a/.ahub/sam/exclude.txt +++ b/.ahub/sam/exclude.txt @@ -1,3 +1,4 @@ # External code /ONE-vscode/media/CircleGraph/external /ONE-vscode/media/CircleEditor/external +/ONE-vscode/media/external/ diff --git a/media/CircleEditor/index.html b/media/CircleEditor/index.html index 1dbe9ebc..de5086d8 100644 --- a/media/CircleEditor/index.html +++ b/media/CircleEditor/index.html @@ -70,9 +70,10 @@ + + +