forked from firejune/struct.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstruct.min.js
More file actions
20 lines (19 loc) · 5.49 KB
/
struct.min.js
File metadata and controls
20 lines (19 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*!
* C-Like Data Structure for JavaScript.
*
* @author Joon Kyoung (aka. Firejune)
* @license MIT
* @version 0.6.9
*
*/
(function(C,q){function y(a){return!!a&&a.constructor===A}function u(a){return!!a&&a===Array||a.constructor===Array}function s(a){return!!a&&a===String||a.constructor===String}function v(a){return!!a&&a===ArrayBuffer||a.constructor===ArrayBuffer}function z(a){return a.charAt(0).toUpperCase()+a.slice(1)}function t(a,c){var b={},e=null,h;for(h in a)if(a.hasOwnProperty(h))if((e=a[h])&&e===Object||e.constructor===Object)b[h]=t(e,c);else if(e=c(e,h,b)){b=e;break}return b}function D(a,c){for(var b in c)a.hasOwnProperty(b)&&
(a[b]&&a[b]===Object||a[b].constructor===Object?a[b]=D(a[b],c[b]):a[b][1]=c[b]);return a}function F(a,c){return t(a,function(b,a,h){var r=u(b)&&b[0]||b,r=y(r)&&r||r.toLowerCase(),l=s(b)?c:b[1],d=u(b)&&3<=b.length&&b[2]||(s(l)||u(l)||v(l))&&l.length||v(l)&&l.byteLength||1;b=u(b)&&(s(d)||1<d)?s(d)&&b[3]===q&&!0||b[3]:!1;h[a]=[r,l,d,b]})}function E(a){var c=0;t(a,function(b,a){var h=b[0],r=b[1],l=b[2],d=b[3],B=w[h],g="uint8";B===q&&y(h)&&(B=h.byteLength);s(l)&&(g=l,l=r.byteLength||r.length);!0===d&&
(c+=w[g]);c+=l*B});return c}var A=function(a,c,b){this.endian=b==q&&!0||b;this.defaultValue=c||0;this.struct=F(a,this.defaultValue);this.byteLength=E(this.struct);this.emptyBuffer=new ArrayBuffer(this.byteLength);this.constructor=A;this._debug=!1;this._struct={};this._debug&&console.log("STRUCT.CREATE","defaultValue:",this.defaultValue,"byteLength:",this.byteLength,"endian:",this.endian,"struct:",this.struct)};A.prototype={update:function(a){this.struct=D(this.struct,a||{});this.byteLength=E(this.struct);
this.emptyBuffer=new ArrayBuffer(this.byteLength);a=this.struct;var c={},b;for(b in a)a.hasOwnProperty(b)&&(c[b]=a[b]);return c},read:function(a,c){if(a===q&&c===q)return t(this.struct,function(a,b,c){c[b]=a[1]});var b=this,e=this.endian,h=a instanceof DataView&&a||new DataView(a);if(0===a.byteLength||a.byteLength<this.byteLength)return Error("Uncaught IndexSizeError: Buffer size was zero byte.");this.offset=c||0;this._debug&&console.info("STRUCT.READ","byteLength:",a.byteLength,"readOffset:",this.offset);
var r=t(this.struct,function(c,d,r){var g=[],p=c[0],m=c[1],k=c[2],n=c[3];c=w[p]||1;var q="uint8",f=0;s(k)&&(q=k,k=m.byteLength||m.length);if(a.byteLength<=b.offset)return Error("Uncaught IndexSizeError: Index or size was negative.");!0===n&&(k=h["get"+z(q)](b.offset,e)/c,b.offset+=w[q],b._struct[d+"Size"]=[q,k*c],b._debug&&console.log(d+"Size",b._struct[d+"Size"],b.offset));if(v(m)){g=b.offset+k*c;if(a.byteLength<g)return Error("Uncaught IndexSizeError: Index or size was negative.");g=a.slice(b.offset,
g);b.offset+=k*c}else for(;f<k;){y(p)?(c=a.slice(b.offset),g[f]=p.read(c),c=p.byteLength):g[f]=h["get"+z(p)](b.offset,e);if(g[f]&&g[f].constructor===Error)return g[f];b.offset+=c;f++}if(v(m)||s(m)||u(m)||1<k){if(s(m)){m=g;k=[];g=m.length;for(c=0;c<g;)k[c]=String.fromCharCode(m[c]),c++;m=k.join("")}else m=g;r[d]=m}else r[d]=g[0];b._struct[d]=[p,r[d]];b._debug&&console.log(d,b._struct[d],b.offset)});r&&r.constructor===Error||(this.byteLength=this.offset);return r},write:function(a){var c=this,b=0,e=
this.endian;a!==q&&this.update(a);var h=new DataView(this.emptyBuffer);this._debug&&console.info("STRUCT.WRITE","byteLength:",this.byteLength);t(this.struct,function(a,l){var d=[],f=a[0],d=a[1],g=a[2],p=a[3],m=w[f],k="uint8",n=0;m===q&&y(f)&&(m=f.byteLength);s(g)&&(k=g,g=d.byteLength||d.length);!0===p&&(h["set"+z(k)](b,g*m,e),b+=w[k],c._struct[l+"Size"]=[k,g*m],c._debug&&console.log(l+"Size",c._struct[l+"Size"],b));c._struct[l]=[f,d];if(v(d)||s(d)||u(d)||1<g){v(d)&&Array.prototype.slice.call(new (C[z(f)+
"Array"])(d));if(p=s(d))for(var p=[],k=d.length,t=0;t<k;)p[t]=d.charCodeAt(t),t++;d=p||d}else d=[d];for(;n<g;){if(y(f))for(p=f.write(),p=new Uint8Array(p),k=0;k<p.length;)h.setUint8(b,p[k],e),b+=1,k++;else h["set"+z(f)](b,d[n],e);b+=m;n++}c._debug&&console.log(l,c._struct[l],b)});b!=this.emptyBuffer.byteLength&&console.warn("Incorrect buffer size writed");return h.buffer}};var w={int8:1,uint8:1,int16:2,uint16:2,int32:4,uint32:4,float32:4,int64:8,uint64:8,float64:8};if(DataView.prototype.getUint64===
q&&DataView.prototype.setUint64===q&&DataView.prototype.getInt64===q&&DataView.prototype.setInt64===q){var n=function(a){return 0<=a&&31>a?1<<a:n[a]||(n[a]=Math.pow(2,a))},f=function(a,c){this.lo=a;this.hi=c};f.prototype={valueOf:function(){return this.lo+n(32)*this.hi},toString:function(){return Number.prototype.toString.apply(this.valueOf(),arguments)}};f.fromNumber=function(a){var c=Math.floor(a/n(32));a-=c*n(32);return new f(a,c)};var x=function(a,c){f.apply(this,arguments)};x.prototype="create"in
Object?Object.create(f.prototype):new f;x.prototype.valueOf=function(){return this.hi<n(31)?f.prototype.valueOf.apply(this,arguments):-(n(32)-this.lo+n(32)*(n(32)-1-this.hi))};x.fromNumber=function(a){var c;0<=a?(c=f.fromNumber(a),a=c.lo,c=c.hi):(c=Math.floor(a/n(32)),a-=c*n(32),c+=n(32));return new x(a,c)};DataView.prototype.getUint64=function(a,c){for(var b=c?[0,4]:[4,0],e=0;2>e;e++)b[e]=this.getUint32(a+b[e],c);return(new f(b[0],b[1])).valueOf()};DataView.prototype.setUint64=function(a,c,b){c=
f.fromNumber(c);var e=b?{lo:0,hi:4}:{lo:4,hi:0},h;for(h in e)this.setUint32(a+e[h],c[h],b)};DataView.prototype.getInt64=function(a,c){for(var b=c?[0,4]:[4,0],e=0;2>e;e++)b[e]=this.getUint32(a+b[e],c);return(new x(b[0],b[1])).valueOf()};DataView.prototype.setInt64=function(a,c){value=x.fromNumber(value);var b=c?{lo:0,hi:4}:{lo:4,hi:0},e;for(e in b)this.setUint32(a+b[e],value[e],c)}}C.Struct=A})(window);