Permalink
Browse files

Don't use Object.defineProperty for _offset and _data after initial d…

…ef. Fix Buffer tests and definition to use existing Buffer correctly. Update browser build. Update tests
  • Loading branch information...
1 parent a1544b8 commit 054e108f3e6eb8ad7202cc72ad050c1387f50d97 @Benvie committed Mar 23, 2012
Showing with 60 additions and 63 deletions.
  1. +9 −0 browser/build.js
  2. +5 −7 browser/event-emitter2.js
  3. +2 −3 lib/array.js
  4. +1 −1 lib/bitfield.js
  5. +10 −13 lib/buffer.js
  6. +2 −2 lib/genesis.js
  7. +1 −1 lib/numeric.js
  8. +2 −2 lib/struct.js
  9. +23 −29 reified-browser.js
  10. +1 −1 test/array.js
  11. +2 −2 test/bitfield.js
  12. +2 −2 test/numeric.js
View
@@ -33,5 +33,14 @@ var output = [
].join('\n');
fs.writeFileSync('../reified-browser.js', output);
+fs.writeFileSync('../reified-browser.min.js', compress(output));
+
+function compress(src) {
+ var parse = require('uglify-js').parser.parse;
+ var ug = require('uglify-js').uglify;
+ var opts = { make_seqs: true };
+ return ug.gen_code(ug.ast_squeeze_more(ug.ast_squeeze(ug.ast_mangle(parse(src)), opts)));
+}
+
console.log(require('../reified-browser'));
View
@@ -7,27 +7,25 @@
module.exports.EventEmitter = EventEmitter;
-var isArray = Array.isArray ? Array.isArray : function _isArray(obj) {
- return Object.prototype.toString.call(obj) === "[object Array]";
-};
+var isArray = Array.isArray;
var defaultMaxListeners = 10;
function init() {
- this._events = new Object;
+ this._events = {};
}
function configure(conf) {
if (conf) {
conf.delimiter && (this.delimiter = conf.delimiter);
conf.wildcard && (this.wildcard = conf.wildcard);
if (this.wildcard) {
- this.listenerTree = new Object;
+ this.listenerTree = {};
}
}
}
function EventEmitter(conf) {
- this._events = new Object;
+ this._events = {};
configure.call(this, conf);
}
@@ -161,7 +159,7 @@ function growListenerTree(type, listener) {
while (name) {
if (!tree[name]) {
- tree[name] = new Object;
+ tree[name] = {};
}
tree = tree[name];
View
@@ -135,9 +135,8 @@ genesis.Type(ArrayType, {
},
realign: function realign(offset, deallocate){
-
- genesis.api(this, '_offset', offset || 0);
- // use realiagn as a chance to DEALLOCATE since everything is being reset essentially
+ this._offset = offset = +offset || 0;
+ // use realiagn as a chance to deallocate since everything is being reset essentially
Object.keys(this).forEach(function(i){
if (isFinite(i)) {
if (deallocate) this[i] = null;
View
@@ -47,7 +47,7 @@ function BitfieldType(name, flags, bytes){
data = null;
}
this.rebase(data);
- this.realign(offset);
+ genesis.api(this, '_offset', +offset || 0);
if (Array.isArray(values)) {
values.forEach(function(flag){ this[flag] = true }, this);
View
@@ -17,15 +17,18 @@ function copy(to, from){
}
-if (typeof Buffer !== 'function'){
- var Buffer = function Buffer(subject, offset, length){
+
+var Buffer = function(global){
+ if (typeof global.Buffer === 'function') return global.Buffer;
+
+ function Buffer(subject, offset, length){
return new ArrayBuffer(subject, offset, length);
}
Buffer.isBuffer = function isBuffer(o){
return o instanceof ArrayBuffer;
}
-}
-
+ return Buffer;
+}(Function('return this')());
var ArrayBuffers = { ArrayBuffer: ArrayBuffer };
@@ -123,17 +126,11 @@ DataBuffer.prototype = {
map: function(){
return [].map.apply(this.typed('Uint8'), arguments);
},
- slice: function(offset, length, encoding){
- offset = toNum(offset);
- var end = isFinite(length) ? toNum(length) + offset : this.length - offset;
- return this.subarray(offset, end).toString(encoding || 'ascii');
+ slice: function(start, end, encoding){
+ return this.subarray(start, end).toString(encoding || 'ascii');
},
- // inspect: function(){
- // return '<DataBuffer '+this.length+'b>';
- // },
toArray: function(type){
- type = type || 'Uint8';
- return [].map.call(this.typed(type), function(x){ return x });
+ return [].map.call(this.typed(type || 'Uint8'), function(x){ return x });
},
toString: function(encoding){
switch (encoding) {
View
@@ -142,7 +142,7 @@ function Subtype(name, bytes, ctor){
var Data = Type.prototype = {
__proto__: EventEmitter.prototype,
Class: 'Data',
- toString: function toString(){ return '[object '+this.constructor.name+'Data]' },
+ toString: function toString(){ return '[object '+this.constructor.name+']' },
rebase: function rebase(data){
if (data == null) {
data = new DataBuffer(this.bytes);
@@ -155,7 +155,7 @@ var Data = Type.prototype = {
api(this, '_data', data);
},
realign: function realign(offset){
- api(this, '_offset', offset || 0);
+ this._offset = +offset || 0;
},
clone: function clone(){
return new this.constructor(this._data, this._offset);
View
@@ -71,7 +71,7 @@ function NumericType(name, bytes){
data = null;
}
this.rebase(data);
- this.realign(offset);
+ genesis.api(this, '_offset', +offset || 0);
if (value != null) {
this.write(value);
View
@@ -38,7 +38,7 @@ function StructType(name, fields){
data = null;
}
this.rebase(data);
- this.realign(offset);
+ genesis.api(this, '_offset', +offset || 0);
if (values) {
Object.keys(values).forEach(function(field){
@@ -121,7 +121,7 @@ genesis.Type(StructType, {
},
realign: function realign(offset, deallocate){
- genesis.api(this, '_offset', offset || 0);
+ this._offset = offset = +offset || 0;
// use realiagn as a chance to DEALLOCATE since everything is being reset essentially
Object.keys(this).forEach(function(field){
if (deallocate) this[field] = null;
View
@@ -16,27 +16,25 @@ Object.defineProperty(module, 'exports', {
module.exports.EventEmitter = EventEmitter;
-var isArray = Array.isArray ? Array.isArray : function _isArray(obj) {
- return Object.prototype.toString.call(obj) === "[object Array]";
-};
+var isArray = Array.isArray;
var defaultMaxListeners = 10;
function init() {
- this._events = new Object;
+ this._events = {};
}
function configure(conf) {
if (conf) {
conf.delimiter && (this.delimiter = conf.delimiter);
conf.wildcard && (this.wildcard = conf.wildcard);
if (this.wildcard) {
- this.listenerTree = new Object;
+ this.listenerTree = {};
}
}
}
function EventEmitter(conf) {
- this._events = new Object;
+ this._events = {};
configure.call(this, conf);
}
@@ -170,7 +168,7 @@ function growListenerTree(type, listener) {
while (name) {
if (!tree[name]) {
- tree[name] = new Object;
+ tree[name] = {};
}
tree = tree[name];
@@ -633,15 +631,18 @@ function copy(to, from){
}
-if (typeof Buffer !== 'function'){
- var Buffer = function Buffer(subject, offset, length){
+
+var Buffer = function(global){
+ if (typeof global.Buffer === 'function') return global.Buffer;
+
+ function Buffer(subject, offset, length){
return new ArrayBuffer(subject, offset, length);
}
Buffer.isBuffer = function isBuffer(o){
return o instanceof ArrayBuffer;
}
-}
-
+ return Buffer;
+}(Function('return this')());
var ArrayBuffers = { ArrayBuffer: ArrayBuffer };
@@ -739,17 +740,11 @@ DataBuffer.prototype = {
map: function(){
return [].map.apply(this.typed('Uint8'), arguments);
},
- slice: function(offset, length, encoding){
- offset = toNum(offset);
- var end = isFinite(length) ? toNum(length) + offset : this.length - offset;
- return this.subarray(offset, end).toString(encoding || 'ascii');
+ slice: function(start, end, encoding){
+ return this.subarray(start, end).toString(encoding || 'ascii');
},
- // inspect: function(){
- // return '<DataBuffer '+this.length+'b>';
- // },
toArray: function(type){
- type = type || 'Uint8';
- return [].map.call(this.typed(type), function(x){ return x });
+ return [].map.call(this.typed(type || 'Uint8'), function(x){ return x });
},
toString: function(encoding){
switch (encoding) {
@@ -945,7 +940,7 @@ function Subtype(name, bytes, ctor){
var Data = Type.prototype = {
__proto__: EventEmitter.prototype,
Class: 'Data',
- toString: function toString(){ return '[object '+this.constructor.name+'Data]' },
+ toString: function toString(){ return '[object '+this.constructor.name+']' },
rebase: function rebase(data){
if (data == null) {
data = new DataBuffer(this.bytes);
@@ -958,7 +953,7 @@ var Data = Type.prototype = {
api(this, '_data', data);
},
realign: function realign(offset){
- api(this, '_offset', offset || 0);
+ this._offset = +offset || 0;
},
clone: function clone(){
return new this.constructor(this._data, this._offset);
@@ -1070,7 +1065,7 @@ function NumericType(name, bytes){
data = null;
}
this.rebase(data);
- this.realign(offset);
+ genesis.api(this, '_offset', +offset || 0);
if (value != null) {
this.write(value);
@@ -1167,7 +1162,7 @@ function StructType(name, fields){
data = null;
}
this.rebase(data);
- this.realign(offset);
+ genesis.api(this, '_offset', +offset || 0);
if (values) {
Object.keys(values).forEach(function(field){
@@ -1250,7 +1245,7 @@ genesis.Type(StructType, {
},
realign: function realign(offset, deallocate){
- genesis.api(this, '_offset', offset || 0);
+ this._offset = offset = +offset || 0;
// use realiagn as a chance to DEALLOCATE since everything is being reset essentially
Object.keys(this).forEach(function(field){
if (deallocate) this[field] = null;
@@ -1415,9 +1410,8 @@ genesis.Type(ArrayType, {
},
realign: function realign(offset, deallocate){
-
- genesis.api(this, '_offset', offset || 0);
- // use realiagn as a chance to DEALLOCATE since everything is being reset essentially
+ this._offset = offset = +offset || 0;
+ // use realiagn as a chance to deallocate since everything is being reset essentially
Object.keys(this).forEach(function(i){
if (isFinite(i)) {
if (deallocate) this[i] = null;
@@ -1488,7 +1482,7 @@ function BitfieldType(name, flags, bytes){
data = null;
}
this.rebase(data);
- this.realign(offset);
+ genesis.api(this, '_offset', +offset || 0);
if (Array.isArray(values)) {
values.forEach(function(flag){ this[flag] = true }, this);
View
@@ -16,7 +16,7 @@ test('constructor properties', function(t){
t.equal(ArrayT.name, 'Int8x10', 'named correctly');
t.equal(ArrayT.bytes, 10, 'correct bytes');
t.equal(ArrayT.__proto__, ArrayType.prototype, 'ctor inherits from ArrayType.prototype');
- t.equal(ArrayT+'', '[object Type]', 'toString shows class "Type"');
+ t.equal(ArrayT+'', '[object Int8x10Type]', 'toString shows class "Type"');
t.equal(ArrayT.prototype.DataType, 'array', 'prototype is correct DataType');
t.equal(ArrayT.count, 10, 'array is correct length');
t.end();
View
@@ -23,15 +23,15 @@ test('constructor properties', function(t){
t.equal(Desc.name, 'DescriptorFlags', 'named');
t.equal(Desc.bytes, 1, 'correct bytes');
t.equal(Desc.__proto__, Bitfield.prototype, 'ctor inherits from numeric.prototype');
- t.equal(Desc+'', '[object Type]', 'toString shows class "Type"');
+ t.equal(Desc+'', '[object DescriptorFlagsType]', 'toString shows class "DescriptorFlagsType"');
t.end();
});
test('prototype properties', function(t){
t.equal(Desc.bytes, Desc.prototype.bytes, 'prototype bytes match constructor bytes');
t.equal(Desc.prototype.__proto__, Bitfield.prototype.prototype, 'prototype inherits from numeric.prototype.prototype');
t.equal(Desc.prototype.DataType, 'bitfield', 'DataType is correct');
- t.equal(Desc.prototype+'', '[object Data]', 'toString shows class "Data"');
+ t.equal(Desc.prototype+'', '[object DescriptorFlags]', 'toString shows class "DescriptorFlags"');
t.end();
});
View
@@ -18,7 +18,7 @@ test('constructor properties', function(t){
t.equal(Int8.name, 'Int8', 'named');
t.equal(Int8.bytes, 1, 'correct bytes');
t.equal(Int8.__proto__, numeric.prototype, 'ctor inherits from numeric.prototype');
- t.equal(Int8+'', '[object Type]', 'toString shows class "Type"');
+ t.equal(Int8+'', '[object Int8Type]', 'toString shows class "Int8Type"');
t.end();
});
@@ -27,7 +27,7 @@ test('prototype properties', function(t){
t.equal(Int8.prototype.__proto__, numeric.prototype.prototype, 'prototype inherits from numeric.prototype.prototype');
t.equal(Int8.prototype.DataType, 'numeric', 'DataType is correct');
t.equal(Int8.prototype.Subtype, 'Int8', 'Subtype is correct');
- t.equal(Int8.prototype+'', '[object Data]', 'toString shows class "Data"');
+ t.equal(Int8.prototype+'', '[object Int8]', 'toString shows class "Int8"');
t.end();
});

0 comments on commit 054e108

Please sign in to comment.