Created
February 4, 2013 03:08
-
-
Save Shadow6363/4704816 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
if (typeof jDataView === 'undefined' && typeof require !== 'undefined') { | |
jDataView = require('jDataView'); | |
} | |
// Extend code from underscorejs | |
var extend = function (obj) { | |
for (var i = 1; i < arguments.length; ++i) { | |
var source = arguments[i]; | |
for (var prop in source) { | |
if (source[prop] !== undefined) { | |
obj[prop] = source[prop]; | |
} | |
} | |
} | |
return obj; | |
}; | |
function jComposer(view, structure) { | |
if (!(this instanceof arguments.callee)) { | |
throw new Error("Constructor may not be called as a function"); | |
} | |
if (!(view instanceof jDataView)) { | |
view = new jDataView(view, undefined, undefined, true); | |
} | |
this.view = view; | |
this.view.seek(0); | |
this.structure = extend({}, jComposer.prototype.structure, structure); | |
console.log(this.structure); | |
} | |
function toInt(val) { | |
if (typeof val === 'function') { | |
val = val.call(this); | |
} | |
return val; | |
} | |
jComposer.prototype.structure = { | |
uint8: function (value) { console.log(value); console.log(this); return this.view.setUint8(value); }, | |
uint16: function (value) { return this.view.setUint16(value); }, | |
uint32: function (value) { return this.view.setUint32(value); }, | |
int8: function (value) { return this.view.setInt8(value); }, | |
int16: function (value) { return this.view.setInt16(value); }, | |
int32: function (value) { return this.view.setInt32(value); }, | |
float32: function (value) { return this.view.setFloat32(value); }, | |
float64: function (value) { return this.view.setFloat64(value); }, | |
char: function () { return this.view.getChar(); }, | |
string: function (length) { | |
return this.view.getString(toInt.call(this, length)); | |
}, | |
array: function (type, length) { | |
length = toInt.call(this, length); | |
var results = []; | |
for (var i = 0; i < length; ++i) { | |
results.push(this.compose(type)); | |
} | |
return results; | |
}, | |
seek: function (position, block) { | |
position = toInt.call(this, position); | |
if (block instanceof Function) { | |
var old_position = this.view.tell(); | |
this.view.seek(position); | |
var result = block.call(this); | |
this.view.seek(old_position); | |
return result; | |
} else { | |
return this.view.seek(position); | |
} | |
}, | |
tell: function () { | |
return this.view.tell(); | |
}, | |
skip: function (offset) { | |
offset = toInt.call(this, offset); | |
this.view.seek(this.view.tell() + offset); | |
return offset; | |
} | |
}; | |
jComposer.prototype.seek = jComposer.prototype.structure.seek; | |
jComposer.prototype.tell = jComposer.prototype.structure.tell; | |
jComposer.prototype.skip = jComposer.prototype.structure.skip; | |
jComposer.prototype.compose = function (structure) { | |
//console.log(this.structure[structure]); | |
//bleh = this.structure[structure]; | |
//console.log(this.view); | |
for (var key in structure) { | |
console.log(key); | |
console.log(structure[key]); | |
console.log(this.structure['eepromConfigDefinition'][key]); | |
dataType = this.structure['eepromConfigDefinition'][key]; | |
setter = this.structure[dataType]; | |
//console.log(this.structure[dataType]); | |
if(dataType == 'uint8') { | |
setter(structure[key]); | |
} | |
} | |
/* | |
// f, 1, 2 means f(1, 2) | |
if (structure instanceof Function) { | |
console.log(arguments); | |
return structure.apply(this, Array.prototype.slice.call(arguments, 1)); | |
} | |
// 'int32' is a shortcut for ['int32'] | |
if (typeof structure === 'string') { | |
structure = [structure]; | |
} | |
// ['string', 256] means structure['string'](256) | |
if (structure instanceof Array) { | |
var key = structure[0]; | |
if (!(key in this.structure)) { | |
throw new Error("Missing structure for `" + key + "`"); | |
} | |
return this.compose.apply(this, [this.structure[key]].concat(structure.slice(1))); | |
} | |
// {key: val} means {key: parse(val)} | |
if (typeof structure === 'object') { | |
var output = {}; | |
for (var key in structure) { | |
this.current = output; | |
output[key] = this.compose(structure[key]); | |
} | |
return output; | |
} | |
throw new Error("Unknown structure type `" + structure + "`"); | |
*/ | |
}; | |
var all; | |
if (typeof self !== 'undefined') { | |
all = self; | |
} else if (typeof window !== 'undefined') { | |
all = window; | |
} else if (typeof global !== 'undefined') { | |
all = global; | |
} | |
// Browser + Web Worker | |
all.jComposer = jComposer; | |
// NodeJS + NPM | |
if (typeof module !== 'undefined') { | |
module.exports = jComposer; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment