Index: mojo/bindings/js/message_file_parser.js |
diff --git a/mojo/bindings/js/message_file_parser.js b/mojo/bindings/js/message_file_parser.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..e5c8af36e84f08b0300f540e3dee9f54aa7a9e3d |
--- /dev/null |
+++ b/mojo/bindings/js/message_file_parser.js |
@@ -0,0 +1,564 @@ |
+// Copyright 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+define([ |
+ "console", |
+ "gin/test/expect", |
+ "mojo/public/js/bindings/fileio" |
+], function (console, expect, fileio) { |
abarth-chromium
2014/07/22 03:51:00
If this module is for testing, should it be in a "
hansmuller
2014/07/22 15:49:06
Do you mean a test subdirectory below /mojo/bindin
|
+ |
+ // The little-endian and 64 bit code below was lifted from codec.js. |
+ |
+ var kHostIsLittleEndian = (function () { |
+ var endianArrayBuffer = new ArrayBuffer(2); |
+ var endianUint8Array = new Uint8Array(endianArrayBuffer); |
+ var endianUint16Array = new Uint16Array(endianArrayBuffer); |
+ endianUint16Array[0] = 1; |
+ return endianUint8Array[0] == 1; |
+ })(); |
abarth-chromium
2014/07/22 03:51:00
This code looks copy/pasted from codec.js. Should
hansmuller
2014/07/22 15:49:06
Yes. I'd wanted to do as much in a separate CL sin
|
+ |
+ var kHighWordMultiplier = 0x100000000; |
+ |
+ function align(size) { |
+ return size + (kAlignment - (size % kAlignment)) % kAlignment; |
+ } |
+ |
+ function setInt64(dataView, byteOffset, value) { |
+ var hi = Math.floor(value / kHighWordMultiplier); |
+ if (kHostIsLittleEndian) { |
+ dataView.setInt32(byteOffset, value, kHostIsLittleEndian); |
+ dataView.setInt32(byteOffset + 4, hi, kHostIsLittleEndian); |
+ } else { |
+ dataView.setInt32(byteOffset, hi, kHostIsLittleEndian); |
+ dataView.setInt32(byteOffset + 4, value, kHostIsLittleEndian); |
+ } |
+ } |
+ |
+ function setUint64(dataView, byteOffset, value) { |
+ var hi = (value / kHighWordMultiplier) | 0; |
+ if (kHostIsLittleEndian) { |
+ dataView.setInt32(byteOffset, value, kHostIsLittleEndian); |
+ dataView.setInt32(byteOffset + 4, hi, kHostIsLittleEndian); |
+ } else { |
+ dataView.setInt32(byteOffset, hi, kHostIsLittleEndian); |
+ dataView.setInt32(byteOffset + 4, value, kHostIsLittleEndian); |
+ } |
+ } |
+ |
+ // MessageData contains the sequence of bytes loaded from a ".data" file. |
+ |
+ function MessageData(byteLength) { |
+ this.buffer = new ArrayBuffer(byteLength); |
+ this.data = new DataView(this.buffer); |
+ this.byteLength = byteLength; |
+ } |
+ |
+ MessageData.prototype.getU1 = function(offset) { |
+ this.data.getUint8(offset); |
+ } |
+ |
+ MessageData.prototype.setU1 = function(offset, value) { |
+ this.data.setUint8(offset, value); |
+ } |
+ MessageData.prototype.setU2 = function(offset, value) { |
+ this.data.setUint16(offset, value, kHostIsLittleEndian); |
+ } |
+ MessageData.prototype.setU4 = function(offset, value) { |
+ this.data.setUint32(offset, value, kHostIsLittleEndian); |
+ } |
+ MessageData.prototype.setU8 = function(offset, value) { |
+ setUint64(this.data, offset, value, kHostIsLittleEndian); |
+ } |
+ MessageData.prototype.setS1 = function(offset, value) { |
+ this.data.setInt8(offset, value); |
+ } |
+ MessageData.prototype.setS2 = function(offset, value) { |
+ this.data.setInt16(offset, value, kHostIsLittleEndian); |
+ } |
+ MessageData.prototype.setS4 = function(offset, value) { |
+ this.data.setInt32(offset, value, kHostIsLittleEndian); |
+ } |
+ MessageData.prototype.setS8 = function(offset, value) { |
+ setInt64(this.data, offset, value, kHostIsLittleEndian); |
+ } |
+ MessageData.prototype.setF = function(offset, value) { |
+ this.data.setFloat32(offset, value, kHostIsLittleEndian); |
+ } |
+ MessageData.prototype.setD = function(offset, value) { |
+ this.data.setFloat64(offset, value, kHostIsLittleEndian); |
+ } |
+ |
+ // Files and Lines represent the raw text from a ".data" file. |
+ |
+ function InputError(message, line) { |
+ this.message = message; |
+ this.line = line; |
+ } |
+ |
+ InputError.prototype.toString = function() { |
+ var s = 'Error: ' + this.message; |
+ if (this.line) |
+ s += ', at line ' + |
+ (this.line.number + 1) + ': "' + this.line.contents + '"'; |
+ return s; |
+ } |
+ |
+ function File(contents) { |
+ this.contents = contents; |
+ this.index = 0; |
+ this.lineNumber = 0; |
+ } |
+ |
+ File.prototype.isEmpty = function() { |
+ return this.index >= this.contents.length; |
+ } |
+ |
+ File.prototype.nextLine = function() { |
+ if (this.isEmpty()) |
+ return null; |
+ var start = this.index; |
+ var end = this.contents.indexOf('\n', start); |
+ if (end == -1) |
+ end = this.contents.length; |
+ this.index = end + 1; |
+ return new Line(this.contents.substring(start, end), this.lineNumber++); |
+ } |
+ |
+ function Line(contents, number) { |
+ var i = contents.indexOf('//'); |
+ var s = (i == -1) ? contents.trim() : contents.substring(0, i).trim(); |
+ this.contents = contents; |
+ this.items = (s.length > 0) ? s.split(/\s+/) : []; |
+ this.index = 0; |
+ this.number = number; |
+ } |
+ |
+ Line.prototype.isEmpty = function() { |
+ return this.index >= this.items.length; |
+ } |
+ |
+ var itemTypeSizes = { |
+ u1: 1, u2: 2, u4: 4, u8: 8, s1: 1, s2: 2, s4: 4, s8: 8, b: 1, f: 4, d: 8, |
+ dist4: 4, dist8: 8, anchr: 0, handles: 0 |
+ }; |
+ |
+ function isValidItemType(type) { |
+ return itemTypeSizes[type] !== undefined; |
+ } |
+ |
+ Line.prototype.nextItem = function() { |
+ if (this.isEmpty()) |
+ return null; |
+ |
+ var itemString = this.items[this.index++]; |
+ var type = 'u1'; |
+ var value = itemString; |
+ |
+ if (itemString.charAt(0) == '[') { |
+ var i = itemString.indexOf(']'); |
+ if (i != -1 && i + 1 < itemString.length) { |
+ type = itemString.substring(1, i); |
+ value = itemString.substring(i + 1); |
+ } else { |
+ throw new InputError('invalid item', this); |
+ } |
+ } |
+ if (!isValidItemType(type)) |
+ throw new InputError('invalid item type', this); |
+ |
+ return new Item(this, type, value); |
+ } |
+ |
+ // The text for Each whitespace delimited binary data "item" loaded |
+ // from a ".data" file is represented by an Item. |
+ |
+ function Item(line, type, value) { |
+ this.line = line; |
+ this.type = type; |
+ this.value = value; |
+ this.size = itemTypeSizes[type]; |
+ } |
+ |
+ Item.prototype.isFloat = function() { |
+ return this.type == 'f' || this.type == 'd'; |
+ } |
+ |
+ Item.prototype.isInteger = function() { |
+ return ['u1', 'u2', 'u4', 'u8', |
+ 's1', 's2', 's4', 's8'].indexOf(this.type) != -1; |
+ } |
+ |
+ Item.prototype.isNumber = function() { |
+ return this.isFloat() || this.isInteger(); |
+ } |
+ |
+ Item.prototype.isByte = function() { |
+ return this.type == 'b'; |
+ } |
+ |
+ Item.prototype.isDistance = function() { |
+ return this.type == 'dist4' || this.type == 'dist8'; |
+ } |
+ |
+ Item.prototype.isAnchor = function() { |
+ return this.type == 'anchr'; |
+ } |
+ |
+ Item.prototype.isHandles = function() { |
+ return this.type == 'handles'; |
+ } |
+ |
+ // A TestMessage represents the complete binary message loaded from |
+ // ".data" file. The parseTestMessage() function below constructs a |
+ // TestMessage from a File. |
+ |
+ function TestMessage(byteLength) { |
+ this.index = 0; |
+ this.data = new MessageData(byteLength); |
+ this.distances = {}; |
+ this.handleCount = 0; |
+ } |
+ |
+ function checkItemNumberValue(item, n, min, max) { |
+ if (n < min || n > max) |
+ throw new InputError('invalid item value', item.line); |
+ } |
+ |
+ TestMessage.prototype.addNumber = function(item) { |
+ var n = item.isInteger() ? parseInt(item.value) : parseFloat(item.value); |
+ if (Number.isNaN(n)) |
+ throw new InputError("can't parse item value", item.line); |
+ |
+ switch(item.type) { |
+ case 'u1': |
+ checkItemNumberValue(item, n, 0, 0xFF); |
+ this.data.setU1(this.index, n); |
+ this.index += 1; |
+ break; |
+ case 'u2': |
+ checkItemNumberValue(item, n, 0, 0xFFFF); |
+ this.data.setU2(this.index, n); |
+ this.index += 2; |
+ break; |
+ case 'u4': |
+ checkItemNumberValue(item, n, 0, 0xFFFFFFFF); |
+ this.data.setU4(this.index, n); |
+ this.index += 4; |
+ break; |
+ case 'u8': |
+ checkItemNumberValue(item, n, 0, Number.MAX_SAFE_INTEGER); |
+ this.data.setU8(this.index, n); |
+ this.index += 8; |
+ break; |
+ case 's1': |
+ checkItemNumberValue(item, n, -128, 127); |
+ this.data.setS1(this.index, n); |
+ this.index += 1; |
+ break; |
+ case 's2': |
+ checkItemNumberValue(item, n, -32768, 32767); |
+ this.data.setS2(this.index, n); |
+ this.index += 2; |
+ break; |
+ case 's4': |
+ checkItemNumberValue(item, n, -2147483648, 2147483647); |
+ this.data.setS4(this.index, n); |
+ this.index += 4; |
+ break; |
+ case 's8': |
+ checkItemNumberValue(item, n, |
+ Number.MIN_SAFE_INTEGER, |
+ Number.MAX_SAFE_INTEGER); |
+ this.data.setS8(this.index, n); |
+ this.index += 8; |
+ break; |
+ case 'f': |
+ this.data.setF(this.index, n); |
+ this.index += 4; |
+ break; |
+ case 'd': |
+ this.data.setD(this.index, n); |
+ this.index += 8; |
+ break; |
+ |
+ default: |
+ throw new InputError('unrecognized item type', item.line); |
+ } |
+ } |
+ |
+ TestMessage.prototype.addByte = function(item) { |
+ if (!/^[01]{8}$/.test(item.value)) |
+ throw new InputError('invalid byte item value', item.line); |
+ function b(i) { |
+ return (item.value.charAt(i) == '1') ? 1 << i : 0; |
+ } |
+ var n = b(0) | b(1) | b(2) | b(3) | b(4) | b(5) | b(6) | b(7); |
+ this.data.setU1(this.index, n); |
+ this.index += 1; |
+ } |
+ |
+ TestMessage.prototype.addDistance = function(item) { |
+ if (this.distances[item.value]) |
+ throw new InputError('duplicate distance item', item.line); |
+ this.distances[item.value] = {index: this.index, item: item}; |
+ this.index += item.type == 'dist4' ? 4 : 8; |
+ } |
+ |
+ TestMessage.prototype.addAnchor = function(item) { |
+ var dist = this.distances[item.value]; |
+ if (!dist) |
+ throw new InputError('unmatched anchor item', item.line); |
+ delete this.distances[item.value]; |
+ |
+ var n = this.index - dist.index; |
+ // TODO(hansmuller): validate n |
+ |
+ if (dist.item.type == 'dist4') |
+ this.data.setU4(dist.index, n, kHostIsLittleEndian); |
+ else if (dist.item.type == 'dist8') |
+ this.data.setU8(this.data, dist.index, n); |
+ else |
+ throw new InputError('unrecognzed distance item type', dist.item.line); |
+ } |
+ |
+ TestMessage.prototype.addHandles = function(item) { |
+ this.handleCount = parseInt(item.value); |
+ if (Number.isNaN(this.handleCount)) |
+ throw new InputError("can't parse handleCount", item.line); |
+ } |
+ |
+ TestMessage.prototype.addItem = function(item) { |
+ if (item.isNumber()) |
+ this.addNumber(item); |
+ else if (item.isByte()) |
+ this.addByte(item); |
+ else if (item.isDistance()) |
+ this.addDistance(item); |
+ else if (item.isAnchor()) |
+ this.addAnchor(item); |
+ else if (item.isHandles()) |
+ this.addHandles(item); |
+ else |
+ throw new InputError('unrecognized item type', item.line); |
+ } |
+ |
+ TestMessage.prototype.unanchoredDistances = function() { |
+ var names = null; |
+ for (var name in this.distances) { |
+ if (this.distances.hasOwnProperty(name)) |
+ names = (names === null) ? name : names + ' ' + name; |
+ } |
+ return names; |
+ } |
+ |
+ function parseTestMessage(file) { |
+ var items = []; |
+ var messageLength = 0; |
+ while(!file.isEmpty()) { |
+ var line = file.nextLine(); |
+ while (!line.isEmpty()) { |
+ var item = line.nextItem(); |
+ if (item.isHandles() && items.length > 0) |
+ throw new InputError('handles item is not first'); |
+ messageLength += item.size; |
+ items.push(item); |
+ } |
+ } |
+ |
+ var msg = new TestMessage(messageLength); |
+ for (var i = 0; i < items.length; i++) |
+ msg.addItem(items[i]); |
+ |
+ if (messageLength != msg.index) |
+ throw new InputError('failed to compute message length'); |
+ var names = msg.unanchoredDistances(); |
+ if (names) |
+ throw new InputError('no anchors for ' + names, 0); |
+ |
+ return msg; |
+ } |
+ |
+ // Verify that the TestMessage (et al) data file loading code is OK. |
+ |
+ function checkMessageFileParser() { |
+ function ParserTestFailure(message, file) { |
+ this.message = message; |
+ this.file = file; |
+ } |
+ |
+ ParserTestFailure.prototype.toString = function() { |
+ return 'Error: ' + this.message + ' for "' + this.file.contents + '"'; |
+ } |
+ |
+ function checkData(data, expectedData, file) { |
+ if (data.byteLength != expectedData.byteLength) { |
+ var s = "message length (" + data.byteLength + ") doesn't match " + |
+ "expected length: " + expectedData.byteLength; |
+ throw new ParserTestFailure(s, file); |
+ } |
+ |
+ for (var i = 0; i < data.byteLength; i++) { |
+ if (data.getU1(i) != expectedData.getU1(i)) { |
+ var s = 'message data mismatch at byte offset ' + i; |
+ throw new ParserTestFailure(s, file); |
+ } |
+ } |
+ } |
+ |
+ function testFloatItems() { |
+ var file = new File('[f]+.3e9 [d]-10.03'); |
+ var msg = parseTestMessage(file); |
+ var expectedData = new MessageData(12); |
+ expectedData.setF(0, +.3e9); |
+ expectedData.setD(4, -10.03); |
+ checkData(msg.data, expectedData, file); |
+ } |
+ |
+ function testUnsignedIntegerItems() { |
+ var file = new File('[u1]0x10// hello world !! \n\r \t [u2]65535 \n' + |
+ '[u4]65536 [u8]0xFFFFFFFFFFFFF 0 0Xff'); |
+ var msg = parseTestMessage(file); |
+ var expectedData = new MessageData(17); |
+ expectedData.setU1(0, 0x10); |
+ expectedData.setU2(1, 65535); |
+ expectedData.setU4(3, 65536); |
+ expectedData.setU8(7, 0xFFFFFFFFFFFFF); |
+ expectedData.setU1(15, 0); |
+ expectedData.setU1(16, 0xff); |
+ checkData(msg.data, expectedData, file); |
+ } |
+ |
+ function testSignedIntegerItems() { |
+ var file = new File('[s8]-0x800 [s1]-128\t[s2]+0 [s4]-40'); |
+ var msg = parseTestMessage(file); |
+ var expectedData = new MessageData(15); |
+ expectedData.setS8(0, -0x800); |
+ expectedData.setS1(8, -128); |
+ expectedData.setS2(9, 0); |
+ expectedData.setS4(11, -40); |
+ checkData(msg.data, expectedData, file); |
+ } |
+ |
+ function testByteItems() { |
+ var file = |
+ new File('[b]00001011 [b]10000000 // hello world\n [b]00000000'); |
+ var msg = parseTestMessage(file); |
+ var expectedData = new MessageData(3); |
+ expectedData.setU1(0, 11); |
+ expectedData.setU1(1, 128); |
+ expectedData.setU1(2, 0); |
+ checkData(msg.data, expectedData, file); |
+ } |
+ |
+ function testAnchors() { |
+ var file = new File('[dist4]foo 0 [dist8]bar 0 [anchr]foo [anchr]bar'); |
+ var msg = parseTestMessage(file); |
+ var expectedData = new MessageData(14); |
+ expectedData.setU4(0, 14); |
+ expectedData.setU1(4, 0); |
+ expectedData.setU8(5, 9); |
+ expectedData.setU1(13, 0); |
+ checkData(msg.data, expectedData, file); |
+ } |
+ |
+ function testHandles() { |
+ var file = new File('// This message has handles! \n[handles]50 [u8]2'); |
+ var msg = parseTestMessage(file); |
+ var expectedData = new MessageData(8); |
+ expectedData.setU8(0, 2); |
+ |
+ if (msg.handleCount != 50) { |
+ var s = 'wrong handle count (' + msg.handleCount + ')'; |
+ throw new ParserTestFailure(s, file); |
+ } |
+ checkData(msg.data, expectedData, file); |
+ } |
+ |
+ function testEmptyInput() { |
+ var file = new File(''); |
+ var msg = parseTestMessage(file); |
+ if (msg.data.byteLength != 0) |
+ throw new ParserTestFailure('expected empty message', file); |
+ } |
+ |
+ function testBlankInput() { |
+ var file = |
+ new File(' \t // hello world \n\r \t// the answer is 42 '); |
+ var msg = parseTestMessage(file); |
+ if (msg.data.byteLength != 0) |
+ throw new ParserTestFailure('expected empty message', file); |
+ } |
+ |
+ function testInvalidInput() { |
+ function parserShouldFail(fileContents) { |
+ var file = new File(fileContents); |
+ try { |
+ parseTestMessage(file); |
+ } catch (e) { |
+ if (e instanceof InputError) |
+ return; |
+ throw new ParserTestFailure( |
+ 'unexpected exception ' + e.toString(), file); |
+ } |
+ throw new ParserTestFailure("didn't detect invalid input", file); |
+ } |
+ |
+ ['/ hello world', |
+ '[u1]x', |
+ '[u2]-1000', |
+ '[u1]0x100', |
+ '[s2]-0x8001', |
+ '[b]1', |
+ '[b]1111111k', |
+ '[dist4]unmatched', |
+ '[anchr]hello [dist8]hello', |
+ '[dist4]a [dist4]a [anchr]a', |
+ // '[dist4]a [anchr]a [dist4]a [anchr]a', |
+ '0 [handles]50' |
+ ].forEach(parserShouldFail); |
+ } |
+ |
+ try { |
+ testFloatItems(); |
+ testUnsignedIntegerItems(); |
+ testSignedIntegerItems(); |
+ testByteItems(); |
+ testInvalidInput(); |
+ testEmptyInput(); |
+ testBlankInput(); |
+ testHandles(); |
+ testAnchors(); |
+ } catch (e) { |
+ console.log("unexpected exception " + e.toString()); |
+ return e; |
+ } |
+ return null; |
+ } |
+ |
+ function readTestMessage(filename) { |
+ function readFile(filename) { |
+ var contents = fileio.readSourceRelativeFileAsString(filename); |
+ if (contents === null) { |
+ console.log('empty message file "' + filename + '"'); |
+ return new File(""); |
+ } |
+ return new File(contents); |
+ } |
+ var dataFile = readFile(filename + ".data"); |
+ var expectedFile = readFile(filename + ".expected"); |
+ return { |
+ message: parseTestMessage(dataFile), |
+ expected: expectedFile.contents.trim() |
+ }; |
+ } |
+ |
+ function testMessageParser() { |
+ expect(checkMessageFileParser()).toBeNull(); |
+ } |
+ |
+ var exports = {}; |
+ exports.readTestMessage = readTestMessage; |
+ exports.testMessageParser = testMessageParser; |
+ return exports; |
+}); |