| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047 |
- var circle = circle || {};
- var flatbuffers = flatbuffers || require('./flatbuffers');
- var 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("Unknown 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;
- }
- }
- }
- }
- 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 1: denotation += '(RGB)'; break;
- case 2: denotation += '(Grayscale)'; break;
- }
- }
- 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.Reader.open(node.custom_options);
- 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("Unknown 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.';
- }
- };
- flexbuffers.Reader = class {
- static open(buffer) {
- return new flexbuffers.Reader(buffer);
- }
- constructor(buffer) {
- this._reader = new flexbuffers.BinaryReader(buffer);
- }
- read() {
- const end = this._reader.length;
- if (end < 3) {
- throw new flexbuffers.Error('Invalid buffer size.');
- }
- const byteWidth = this._reader.uint(end - 1, 1);
- if (byteWidth > 8) {
- throw new flexbuffers.Error('Invalid byte size.');
- }
- const packedType = this._reader.uint(end - 2, 1);
- const reference = new flexbuffers.Reference(this._reader, end - 2 - byteWidth, byteWidth, 1 << (packedType & 3), packedType >> 2);
- return reference.read();
- }
- };
- 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;
- }
- }
- return undefined;
- }
- _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.BinaryReader = class {
- constructor(buffer) {
- this._buffer = buffer;
- this._length = buffer.length;
- this._view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
- this._utf8Decoder = new TextDecoder('utf-8');
- }
- 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);
- }
- 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);
- }
- 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);
- }
- 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.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.ModelFactory = circle.ModelFactory;
- }
|