UNPKG

jsox

Version:

Java Script Object eXchange.

2 lines (1 loc) 31.7 kB
const e=JSON;if(void 0===t)var t={};const n=t||{};t.JSOX=n,n.version="1.2.121";const r="function"==typeof BigInt,s=["ab","u8","cu8","s8","u16","s16","u32","s32","u64","s64","f32","f64"];let i=null,a=null;const l=[ArrayBuffer,Uint8Array,Uint8ClampedArray,Int8Array,Uint16Array,Int16Array,Uint32Array,Int32Array,null,null,Float32Array,Float64Array],o=29,c=30,u=31,f={true:!0,false:!1,null:null,NaN:NaN,Infinity:1/0,undefined:void 0};class p extends Date{constructor(e,t){super(e),this.ns=t||0}}n.DateNS=p;const g=[];function h(){let e=g.pop();return e||(e={context:0,current_proto:null,current_class:null,current_class_field:0,arrayType:-1,valueType:0,elements:null}),e}function b(e){g.push(e)}n.updateContext=function(){};const y=[];function d(e){y.push(e)}n.escape=function(e){let t,n="";if(!e)return e;for(t=0;t<e.length;t++)'"'!=e[t]&&"\\"!=e[t]&&"`"!=e[t]&&"'"!=e[t]||(n+="\\"),n+=e[t];return n};let m=new WeakMap,v=new Map,k=new Map,w=[];n.reset=function(){m=new WeakMap,v=new Map,k=new Map,w=[]},n.begin=function(e,t){const n={name:null,value_type:0,string:"",contains:null,className:null},i={line:1,col:1};let a,f,g=0,m=new Map,v=0,w=!0,x=!1,_=!1,N=null,O=null,j={first:null,last:null,saved:null,push(e){let t=this.saved;t?(this.saved=t.next,t.node=e,t.next=null,t.prior=this.last):t={node:e,next:null,prior:this.last},this.last?this.last.next=t:this.first=t,this.last=t,this.length++},pop(){let e=this.last;return(this.last=e.prior)||(this.first=null),e.next=this.saved,this.last&&(this.last.next=null),e.next||(e.first=null),this.saved=e,this.length--,e.node},length:0},S=[],A={},T=null,D=null,I=0,M=-1,P=0,C=0,X=!1,J=!1,F=!1,$=!1,B=!1,U={first:null,last:null,saved:null,push(e){let t=this.saved;t?(this.saved=t.next,t.node=e,t.next=null,t.prior=this.last):t={node:e,next:null,prior:this.last},this.last?this.last.next=t:this.first=t,this.last=t},shift(){let e=this.first;return e?((this.first=e.next)||(this.last=null),e.next=this.saved,this.saved=e,e.node):null},unshift(e){let t=this.saved;this.saved=t.next,t.node=e,t.next=this.first,t.prior=null,this.first||(this.last=t),this.first=t}},q=null,R=!1,H=!1,z=!1,W=!1,L=!1,Y=!1,G=!1,V=0,K=0,Q=!1,Z=!1,ee=!1;function te(e){throw new Error(`${e} at ${g} [${i.line}:${i.col}]`)}return{fromJSOX(e,t,n){if(m.get(e))throw new Error("Existing fromJSOX has been registered for prototype");if(t||(t=function(){}),t&&!("constructor"in t))throw new Error("Please pass a prototype like thing...");m.set(e,{protoCon:t.prototype.constructor,cb:n})},registerFromJSOX(e,t){throw new Error("registerFromJSOX is deprecated, please update to use fromJSOX instead:"+e+t.toString())},finalError(){0!==C&&(1===C&&te("Comment began at end of document"),3===C&&te("Open comment '/*' is missing close at end of document"),4===C&&te("Incomplete '/* *' close at end of document")),R&&te("Incomplete string")},value(){this.finalError();let e=N;return N=void 0,e},reset(){v=0,w=!0,U.last&&(U.last.next=U.save),U.save=U.first,U.first=U.last=null,j.last&&(j.last.next=j.save),j.length=0,j.save=U.first,j.first=j.last=null,f=void 0,P=0,S=[],A={},T=null,D=null,I=0,n.value_type=0,n.name=null,n.string="",n.className=null,i.line=1,i.col=1,_=!1,C=0,Q=!1,R=!1,z=!1,W=!1,Z=!1},usePrototype(e,t){A[e]=t},write(n){let r;if("string"!=typeof n&&void 0!==n&&(n=String(n)),!w)throw new Error("Parser is still in an error state, please reset before resuming");for(r=this._write(n,!1);r>0&&("function"==typeof t&&function e(n,r){let s,i,a=n[r];if(a&&"object"==typeof a)for(s in a)Object.prototype.hasOwnProperty.call(a,s)&&(i=e(a,s),void 0!==i?a[s]=i:delete a[s]);return t.call(n,r,a)}({"":N},""),N=e(N),!(r<2));r=this._write());},parse(e,t){"string"!=typeof e&&(e=String(e)),this.reset();const n=this._write(e,!0);if(n>0){let e=this.value();if(void 0===e&&n>1)throw new Error("Pending value could not complete");return e="function"==typeof t?function e(n,r){let s,i,a=n[r];if(a&&"object"==typeof a)for(s in a)Object.prototype.hasOwnProperty.call(a,s)&&(i=e(a,s),void 0!==i?a[s]=i:delete a[s]);return t.call(n,r,a)}({"":e},""):e,e}this.finalError()},_write(e,t){let A,te,ne,re=0;function se(e,t){throw new Error(`${e} '${String.fromCodePoint(t)}' unexpected at ${g} (near '${ne.substr(g>4?g-4:0,g>4?3:g-1)}[${String.fromCodePoint(t)}]${ne.substr(g,10)}') [${i.line}:${i.col}]`)}function ie(){n.value_type=0,n.string="",n.contains=null}function ae(){let e=null;switch(n.value_type){case 5:if(!(n.string.length>13||13==n.string.length&&n[0]>"2")||Z||B||$||J||(ee=!0),ee){if(r)return BigInt(n.string);throw new Error("no builtin BigInt()",0)}if(Z){const e=n.string.match(/\.(\d\d\d\d*)/),t=e?e[1]:null;if(!t||t.length<4){const e=new Date(n.string);return isNaN(e.getTime())&&se("Bad Date format",A),e}{let e=t.substr(3);for(;e.length<6;)e+="0";const r=new p(n.string,Number(e));return isNaN(r.getTime())&&se("Bad DateNS format"+r+r.getTime(),A),r}}return(_?-1:1)*Number(n.string);case 4:if(n.className){if(e=m.get(n.className),e||(e=k.get(n.className)),e&&e.cb)return n.className=null,e.cb.call(n.string);throw new Error("Double string error, no constructor for: new "+n.className+"("+n.string+")")}return n.string;case 2:return!0;case 3:return!1;case 7:case 8:return NaN;case 9:return-1/0;case 10:return 1/0;case 1:return null;case-1:case 12:return;case 6:return n.className&&(e=m.get(n.className),e||(e=k.get(n.className)),n.className=null,e&&e.cb)?n.contains=e.cb.call(n.contains):n.contains;case 13:if(M>=0){let e;if(e=n.contains.length?E(n.contains[0]):E(n.string),0===M)return M=-1,e;{const t=new l[M](e);return M=-1,t}}if(-2===M){let e,t=O;const r=n.contains.length;for(e=0;e<r;e++){const s=n.contains[e];let i=t[s];if(!i){let s=j.first,a=0;for(;s&&a<r&&a<j.length;){const r=n.contains[a];if(!s.next||r!==s.next.node.name)break;if(s.next)if("number"==typeof r){const e=s.next.node.elements;if(e&&r>=e.length){if(a===j.length-1){console.log("This is actually at the current object so use that",a,n.contains,f),i=f,a++,s=s.next;break}if(s.next.next&&r===e.length){i=s.next.next.node.elements,s=s.next,a++,t=i;continue}i=f,a++;break}}else{if(r!==s.next.node.name){i=s.next.node.elements[r],e=a;break}i=s.next.next?s.next.next.node.elements:f}else i=i[r];s=s.next,a++}e=a<r?a-1:a}if("object"==typeof i&&!i)throw new Error("Path did not resolve properly:"+n.contains+" at "+s+"("+e+")");t=i}return M=-3,t}return n.className&&(e=m.get(n.className),e||(e=k.get(n.className)),n.className=null,e&&e.cb)?e.cb.call(n.contains):n.contains;default:console.log("Unhandled value conversion.",n)}}function le(){if(-3==M)return 6===n.value_type&&f.push(n.contains),void(M=-1);if(12===n.value_type)f.push(void 0),delete f[f.length-1];else f.push(ae());ie()}function oe(){if(-3===M&&13===n.value_type)return ie(),void(M=-1);if(12===n.value_type)return;!n.name&&D&&(n.name=D.fields[I++]);let e=ae();T&&T.protoDef&&T.protoDef.cb?(e=T.protoDef.cb.call(f,n.name,e),e&&(f[n.name]=e)):f[n.name]=e,ie()}function ce(e){if(0!==v){switch(_&&se("Negative outside of quotes, being converted to a string (would lose count of leading '-' characters)",e),v){case u:switch(n.value_type){case 2:n.string+="true";break;case 3:n.string+="false";break;case 1:n.string+="null";break;case 10:n.string+="Infinity";break;case 9:n.string+="-Infinity",se("Negative outside of quotes, being converted to a string",e);break;case 8:n.string+="NaN";break;case 7:n.string+="-NaN",se("Negative outside of quotes, being converted to a string",e);break;case-1:n.string+="undefined";break;case 4:case 0:break;default:console.log("Value of type "+n.value_type+" is not restored...")}break;case 1:n.string+="t";break;case 2:n.string+="tr";break;case 3:n.string+="tru";break;case 5:n.string+="f";break;case 6:n.string+="fa";break;case 7:n.string+="fal";break;case 8:n.string+="fals";break;case 9:n.string+="n";break;case 10:n.string+="nu";break;case 11:n.string+="nul";break;case 12:n.string+="u";break;case 13:n.string+="un";break;case 14:n.string+="und";break;case 15:n.string+="unde";break;case 16:n.string+="undef";break;case 17:n.string+="undefi";break;case 18:n.string+="undefin";break;case 19:n.string+="undefine";break;case 20:n.string+="N";break;case 21:n.string+="Na";break;case 22:n.string+="I";break;case 23:n.string+="In";break;case 24:n.string+="Inf";break;case 25:n.string+="Infi";break;case 26:n.string+="Infin";break;case 27:n.string+="Infini";break;case 28:n.string+="Infinit";break;case 0:case o:case c:break;case 32:se("String-keyword recovery fail (after whitespace)",e)}n.value_type=4,v<o&&(v=u)}else v=u,n.value_type=4;if(123==e)pe();else if(91==e)ge();else if(44==e);else{if(32==e||13==e||10==e||9==e||65279==e||8232==e||8233==e)return;44==e||125==e||93==e||58==e?se("Invalid character near identifier",e):n.string+=a}}function ue(e){let t=0;for(;0==t&&g<ne.length;){a=ne.charAt(g);let r=ne.codePointAt(g++);if(r>=65536&&(a+=ne.charAt(g),g++),i.col++,r==e)z?(G?se("Incomplete hexidecimal sequence",r):Y?se("Incomplete long unicode sequence",r):L&&se("Incomplete unicode sequence",r),W?(W=!1,t=1):n.string+=a,z=!1):t=1;else if(z){if(L){if(125==r){n.string+=String.fromCodePoint(V),L=!1,Y=!1,z=!1;continue}if(V*=16,r>=48&&r<=57)V+=r-48;else if(r>=65&&r<=70)V+=r-65+10;else{if(!(r>=97&&r<=102)){se("(escaped character, parsing hex of \\u)",r),t=-1,L=!1,z=!1;continue}V+=r-97+10}continue}if(G||Y){if(0===K&&123===r){L=!0;continue}if(K<2||Y&&K<4){if(V*=16,r>=48&&r<=57)V+=r-48;else if(r>=65&&r<=70)V+=r-65+10;else{if(!(r>=97&&r<=102)){se(Y?"(escaped character, parsing hex of \\u)":"(escaped character, parsing hex of \\x)",r),t=-1,G=!1,z=!1;continue}V+=r-97+10}K++,Y?4==K&&(n.string+=String.fromCodePoint(V),Y=!1,z=!1):2==K&&(n.string+=String.fromCodePoint(V),G=!1,z=!1);continue}}switch(r){case 13:W=!0,i.col=1;continue;case 8232:case 8233:i.col=1;case 10:W?W=!1:i.col=1,i.line++;break;case 116:n.string+="\t";break;case 98:n.string+="\b";break;case 110:n.string+="\n";break;case 114:n.string+="\r";break;case 102:n.string+="\f";break;case 118:n.string+="\v";break;case 48:n.string+="\0";break;case 120:G=!0,K=0,V=0;continue;case 117:Y=!0,K=0,V=0;continue;default:n.string+=a}z=!1}else 92===r?z?(n.string+="\\",z=!1):(z=!0,V=0,K=0):(W&&(W=!1,i.line++,i.col=2),n.string+=a)}return t}function fe(){let e;for(;(e=g)<ne.length;){a=ne.charAt(e);let r=ne.codePointAt(g++);if(r>=256){i.col-=g-e,g=e;break}if(95!=r)if(i.col++,r>=48&&r<=57)F&&(B=!0),n.string+=a;else if(45==r||43==r)0==n.string.length||F&&!$&&!B?(45!=r||F||(_=!_),n.string+=a,$=!0):(_&&(n.string="-"+n.string,_=!1),n.string+=a,Z=!0);else{if(78==r){if(0==v)return H=!1,void(v=20);se("fault while parsing number;",r);break}if(73==r){if(0==v)return H=!1,void(v=22);se("fault while parsing number;",r);break}if(58==r&&Z)_&&(n.string="-"+n.string,_=!1),n.string+=a,Z=!0;else if(84==r&&Z)_&&(n.string="-"+n.string,_=!1),n.string+=a,Z=!0;else if(90==r&&Z)_&&(n.string="-"+n.string,_=!1),n.string+=a,Z=!0;else if(46==r){if(J||X||F){w=!1,se("fault while parsing number;",r);break}n.string+=a,J=!0}else{if(110==r){ee=!0;break}if(X&&(r>=95&&r<=102||r>=65&&r<=70))n.string+=a;else if(120==r||98==r||111==r||88==r||66==r||79==r){if(X||"0"!=n.string){w=!1,se("fault while parsing number;",r);break}X=!0,n.string+=a}else{if(101!=r&&69!=r){if(32==r||13==r||10==r||9==r||47==r||35==r||44==r||125==r||93==r||123==r||91==r||34==r||39==r||96==r||58==r){i.col-=g-e,g=e;break}t&&(w=!1,se("fault while parsing number;",r));break}if(F){w=!1,se("fault while parsing number;",r);break}n.string+=a,F=!0}}}}t||g!=ne.length?(H=!1,n.value_type=5,0==P&&(Q=!0)):H=!0}function pe(){let e,t=2,r=null,s={};if(v>0&&v<o&&ce(123),e=he(),0==P)if(v==o||v==u&&(e||n.string.length)){if(e&&e.protoDef&&e.protoDef.protoCon&&(s=new e.protoDef.protoCon),!e||!e.protoDef&&n.string){if(r=S.find((e=>e.name===n.string)),r)x?(r.fields.length=0,t=4):(s=new r.protoCon,t=5);else{function a(){}S.push(r={name:n.string,protoCon:e&&e.protoDef&&e.protoDef.protoCon||a.constructor,fields:[]}),t=4}x=!1}D=r,v=0}else v=o;else if(v==o||1===P||3===P||5==P)if(0!=v||4==n.value_type){if(e&&e.protoDef)s=new e.protoDef.protoCon;else if(r=S.find((e=>e.name===n.string)),r)t=5,s={};else{function l(){}m.set(n.string,{protoCon:l.prototype.constructor,cb:null}),s=new l}v=0}else v=0;else if(2==P&&0==v)return se("fault while parsing; getting field name unexpected ",A),w=!1,!1;let i=h();return n.value_type=6,0===P?f=s:1==P?n.name=f.length:3!=P&&5!=P||(!n.name&&D&&(n.name=D.fields[I++]),f[n.name]=s),i.context=P,i.elements=f,i.name=n.name,i.current_proto=T,i.current_class=D,i.current_class_field=I,i.valueType=n.value_type,i.arrayType=M,i.className=n.className,n.className=null,n.name=null,T=e,D=r,I=0,f=s,O||(O=f),j.push(i),ie(),P=t,!0}function ge(){if(v>0&&v<o&&ce(91),v==u&&n.string.length){let e=s.findIndex((e=>e===n.string));v=0,e>=0?(M=e,n.className=n.string,n.string=null):"ref"===n.string?(n.className=null,M=-2):m.get(n.string)||k.get(n.string)?n.className=n.string:se(`Unknown type '${n.string}' specified for array`,A)}else if(2==P||v==o||v==c)return se("Fault while parsing; while getting field name unexpected",A),w=!1,!1;{let e=h();n.value_type=13;let t=[];if(0==P)f=t;else if(1==P)-1==M&&f.push(t),n.name=f.length;else if(3==P)if(n.name||(console.log("This says it's resolved......."),M=-3),T&&T.protoDef)if(T.protoDef.cb){const e=T.protoDef.cb.call(f,n.name,t);void 0!==e&&(t=f[n.name]=e)}else f[n.name]=t;else f[n.name]=t;e.context=P,e.elements=f,e.name=n.name,e.current_proto=T,e.current_class=D,e.current_class_field=I,e.valueType=n.value_type,e.arrayType=-1==M?-3:M,e.className=n.className,M=-1,n.className=null,n.name=null,T=null,D=null,I=0,f=t,O||(O=t),j.push(e),ie(),P=1}return!0}function he(){const e={protoDef:null,cls:null};return((e.protoDef=m.get(n.string))||(e.protoDef=k.get(n.string)))&&(n.className||(n.className=n.string,n.string=null)),n.string&&(e.cls=S.find((e=>e.name===n.string)),!e.protoDef&&e.cls),e.protoDef||e.cls?e:null}if(!w)return-1;for(e&&e.length?(te=function(){let e=y.pop();return e?e.n=0:e={buf:null,n:0},e}(),te.buf=e,U.push(te)):(H&&(H=!1,n.value_type=5,0==P&&(Q=!0),re=1),0!==P&&se("Unclosed object at end of stream.",A));w&&(te=U.shift());){if(g=te.n,ne=te.buf,R){let e=ue(q);e<0?w=!1:e>0&&(R=!1,w&&(n.value_type=4))}for(H&&fe();!Q&&w&&g<ne.length;)if(a=ne.charAt(g),A=ne.codePointAt(g++),A>=65536&&(a+=ne.charAt(g),g++),i.col++,C)if(1==C)if(42==A)C=3;else{if(47!=A)return se("fault while parsing;",A);C=2}else 2==C?10!=A&&13!=A||(C=0):3==C?42==A&&(C=4):C=47==A?0:3;else{switch(A){case 35:C=2;break;case 47:C=1;break;case 123:pe();break;case 91:ge();break;case 58:if(5==P)v=0,n.name=n.string,n.string="",n.value_type=0;else if(2==P||4==P)if(4==P){if(!Object.keys(f).length){console.log("This is a full object, not a class def...",n.className);const e=()=>{};m.set(j.last.node.current_class.name,{protoCon:e.prototype.constructor,cb:null}),f=new e,P=3,n.name=n.string,v=0,n.string="",n.value_type=0,console.log("don't do default;s do a revive...")}}else 0!=v&&v!=u&&v!=o&&v!=c&&ce(32),v=0,n.name=n.string,n.string="",P=2===P?3:6,n.value_type=0;else{if(0==P){console.log("Override colon found, allow class redefinition",P),x=!0;break}se(1==P?"(in array, got colon out of string):parsing fault;":3==P?"String unexpected":"(outside any object, got colon out of string):parsing fault;",A),w=!1}break;case 125:if(v==u&&(v=0),4==P)if(D){n.string&&D.fields.push(n.string),ie();let e=j.pop();P=0,v=0,n.name=e.name,f=e.elements,D=e.current_class,I=e.current_class_field,M=e.arrayType,n.value_type=e.valueType,n.className=e.className,O=null,b(e)}else se("State error; gathering class fields, and lost the class",A);else if(2==P||5==P){0!=n.value_type&&(D&&(n.name=D.fields[I++]),oe()),n.value_type=6,T&&T.protoDef&&(console.log("SOMETHING SHOULD AHVE BEEN REPLACED HERE??",T),console.log("The other version only revives on init"),f=new T.protoDef.cb(f,void 0,void 0)),n.contains=f,n.string="";let e=j.pop();P=e.context,n.name=e.name,f=e.elements,D=e.current_class,T=e.current_proto,I=e.current_class_field,M=e.arrayType,n.value_type=e.valueType,n.className=e.className,b(e),0==P&&(Q=!0)}else if(3==P){0===n.value_type&&se("Fault while parsing; unexpected",A),oe(),n.value_type=6,n.contains=f,v=0;let e=j.pop();P=e.context,n.name=e.name,f=e.elements,T=e.current_proto,D=e.current_class,I=e.current_class_field,M=e.arrayType,n.value_type=e.valueType,n.className=e.className,b(e),0==P&&(Q=!0)}else se("Fault while parsing; unexpected",A),w=!1;_=!1;break;case 93:if(v>=c&&(v=0),1==P){0!=n.value_type&&le(),n.contains=f;{let e=j.pop();n.name=e.name,n.className=e.className,P=e.context,f=e.elements,T=e.current_proto,D=e.current_class,I=e.current_class_field,M=e.arrayType,n.value_type=e.valueType,b(e)}n.value_type=13,0==P&&(Q=!0)}else se(`bad context ${P}; fault while parsing`,A),w=!1;_=!1;break;case 44:v<c&&0!=v&&ce(A),v!=u&&v!=o||(v=0),4==P?D?(D.fields.push(n.string),n.string="",v=o):se("State error; gathering class fields, and lost the class",A):2==P?D?(n.name=D.fields[I++],0!=n.value_type&&(oe(),ie())):(n.string||n.value_type)&&se("State error; comma in field name and/or lost the class",A):5==P?(D?(-3==M||n.name||(n.name=D.fields[I++]),0!=n.value_type&&(-3!=M&&oe(),ie())):0!=n.value_type&&(oe(),ie()),n.name=null):1==P?(0==n.value_type&&(n.value_type=12),le(),ie(),v=0):3==P&&0!=n.value_type?(P=2,0!=n.value_type&&(oe(),ie()),v=0):(w=!1,se("bad context; excessive commas while parsing;",A)),_=!1;break;default:switch(A){default:if(0!=P&&(3!=P||v!=o)&&2!=P&&v!=o&&4!=P){if(0==v&&(A>=48&&A<=57||43==A||46==A||45==A))X=!1,F=!1,Z=!1,ee=!1,$=!1,B=!1,J=!1,n.string=a,te.n=g,fe();else if(0==n.value_type)0!=v?ce(A):(v=u,n.string+=a,n.value_type=4);else if(2==P)se("Multiple values found in field name",A);else if(3==P)if(4!=n.value_type&&(6!=n.value_type&&13!=n.value_type||se("String unexpected",A),ce(A)),v==c){he()?n.string=a:se("String unexpected",A)}else v==u?n.string+=a:se("String unexpected",A);else if(1==P){if(v==c){n.className||(n.className=n.string,n.string=""),n.string+=a;break}v==u&&(n.string+=a)}break}switch(A){case 96:case 34:case 39:if(0==v||v==o){n.string.length&&(console.log("IN ARRAY AND FIXING?"),n.className=n.string,n.string=""),ue(A)?n.value_type=4:(q=A,R=!0)}else se("fault while parsing; quote not at start of field name",A);break;case 10:i.line++,i.col=1;case 13:case 32:case 8232:case 8233:case 9:case 65279:if(0===P&&v===u){v=0,0===P&&(Q=!0);break}if(0===v||v===c){0==P&&n.value_type&&(Q=!0);break}if(v===o){if(0===P){v=0,Q=!0;break}n.string.length&&console.log("STEP TO NEXT TOKEN."),v=c}else w=!1,se("fault while parsing; whitepsace unexpected",A);break;default:if(0==v&&(A>=48&&A<=57||43==A||46==A||45==A)){X=!1,F=!1,Z=!1,ee=!1,$=!1,B=!1,J=!1,n.string=a,te.n=g,fe();break}if(v===c&&(w=!1,se("fault while parsing; character unexpected",A)),0===v){v=o,n.value_type=4,n.string+=a;break}if(0==n.value_type)0!==v&&v!==u&&ce(A);else{if(v===u||v===o){n.string+=a;break}if(2==P){if(v==o){n.string+=a;break}se("Multiple values found in field name",A)}3==P&&se("String unexpected",A)}}break;case 96:case 34:case 39:n.string&&(n.className=n.string),n.string="",ue(A)?(n.value_type=4,v=u):(q=A,R=!0);break;case 10:i.line++,i.col=1;case 32:case 9:case 13:case 8232:case 8233:case 65279:if(v==u){if(0==P){v=0,Q=!0;break}if(3==P){v=32;break}if(2==P){v=c;break}if(1==P){v=c;break}}if(0==v||v==c)break;v==o?n.string.length&&(v=c):v<u&&ce(A);break;case 116:0==v?v=1:27==v?v=28:ce(A);break;case 114:1==v?v=2:ce(A);break;case 117:2==v?v=3:9==v?v=10:0==v?v=12:ce(A);break;case 101:3==v?(n.value_type=2,v=u):8==v?(n.value_type=3,v=u):14==v?v=15:18==v?v=19:ce(A);break;case 110:0==v?v=9:12==v?v=13:17==v?v=18:22==v?v=23:25==v?v=26:ce(A);break;case 100:13==v?v=14:19==v?(n.value_type=-1,v=u):ce(A);break;case 105:16==v?v=17:24==v?v=25:26==v?v=27:ce(A);break;case 108:10==v?v=11:11==v?(n.value_type=1,v=u):6==v?v=7:ce(A);break;case 102:0==v?v=5:15==v?v=16:23==v?v=24:ce(A);break;case 97:5==v?v=6:20==v?v=21:ce(A);break;case 115:7==v?v=8:ce(A);break;case 73:0==v?v=22:ce(A);break;case 78:0==v?v=20:21==v?(n.value_type=_?7:8,_=!1,v=u):ce(A);break;case 121:28==v?(n.value_type=_?9:10,_=!1,v=u):ce(A);break;case 45:0==v?_=!_:ce(A);break;case 43:0!==v&&ce(A)}}if(Q){v==u&&(v=0);break}}if(g==ne.length?(d(te),R||H||2==P?re=0:0!=P||0==n.value_type&&!N||(Q=!0,re=1)):(te.n=g,U.unshift(te),re=2),Q){O=null;break}}return w?(Q&&0!=n.value_type&&(v=0,N=ae(),_=!1,n.string="",n.value_type=0),Q=!1,re):-1}}};const x=[Object.freeze(n.begin())];let _=0;function N(){return this&&this.valueOf()}n.parse=function(e,t){let r,s=_++;x.length<=s&&x.push(Object.freeze(n.begin())),r=x[s],"string"!=typeof e&&(e=String(e)),r.reset();const i=r._write(e,!0);if(i>0){let e=r.value();if(void 0===e&&i>1)throw new Error("Pending value could not complete");return e="function"==typeof t?function e(n,r){let s,i,a=n[r];if(a&&"object"==typeof a)for(s in a)Object.prototype.hasOwnProperty.call(a,s)&&(i=e(a,s),void 0!==i?a[s]=i:delete a[s]);return t.call(n,r,a)}({"":e},""):e,_--,e}r.finalError()},n.defineClass=function(e,t){let n,r=Object.keys(t);for(let e=1;e<r.length;e++){let t,n;(t=r[e-1])>(n=r[e])&&(r[e-1]=n,r[e]=t,e?e-=2:e--)}w.push(n={name:e,tag:r.toString(),proto:Object.getPrototypeOf(t),fields:Object.keys(t)});for(let e=1;e<n.fields.length;e++)if(n.fields[e]<n.fields[e-1]){let t=n.fields[e-1];n.fields[e-1]=n.fields[e],n.fields[e]=t,e>1&&(e-=2)}n.proto===Object.getPrototypeOf({})&&(n.proto=null)},n.toJSOX=n.registerToJSOX=function(e,t,n){if(t.prototype&&t.prototype===Object.prototype){let r=Object.keys(t).toString();if(v.get(r))throw new Error("Existing toJSOX has been registered for object type");v.set(r,{external:!0,name:e,cb:n})}else{if(m.get(t.prototype))throw new Error("Existing toJSOX has been registered for prototype");m.set(t.prototype,{external:!0,name:e||n.constructor.name,cb:n})}},n.fromJSOX=function(e,t,n){if(t||(t=function(){}.prototype),k.get(e))throw new Error("Existing fromJSOX has been registered for prototype");if(t&&!("constructor"in t))throw new Error("Please pass a prototype like thing...");k.set(e,{protoCon:t.prototype.constructor,cb:n})},n.registerFromJSOX=function(e,t){throw new Error("deprecated; please adjust code to use fromJSOX:"+e+t.toString())},n.addType=function(e,t,r,s){n.toJSOX(e,t,r),n.fromJSOX(e,t,s)},n.registerToFrom=function(e,t){throw new Error("registerToFrom deprecated; please use addType:"+e+t.toString())},n.stringifier=function(){let t=[],r='"',s=new WeakMap;const l=[];let o=[];const c=new WeakMap,u=new Map;let g=null;const h=[];let b=!1;function y(e){return"string"==typeof e&&""===e?'""':"number"!=typeof e||isNaN(e)?e.includes("\ufeff")||e in f||/[0-9\-]/.test(e[0])||/[\n\r\t #\[\]{}()<>\~!+*/.:,\-"'`]/.test(e)?r+n.escape(e)+r:e:["'",e.toString(),"'"].join("")}m.get(Object.prototype)||(m.set(Object.prototype,{external:!1,name:Object.prototype.constructor.name,cb:null}),m.set(Date.prototype,{external:!1,name:"Date",cb:function(){if(-621672192e5===this.getTime())return"0000-01-01T00:00:00.000Z";let e=-this.getTimezoneOffset(),t=e>=0?"+":"-",n=function(e){let t=Math.floor(Math.abs(e));return(t<10?"0":"")+t};return[this.getFullYear(),"-",n(this.getMonth()+1),"-",n(this.getDate()),"T",n(this.getHours()),":",n(this.getMinutes()),":",n(this.getSeconds()),"."+function(e){let t=Math.floor(Math.abs(e));return(t<100?"0":"")+(t<10?"0":"")+t}(this.getMilliseconds())+t,n(e/60),":",n(e%60)].join("")}}),m.set(p.prototype,{external:!1,name:"DateNS",cb:function(){let e=-this.getTimezoneOffset(),t=e>=0?"+":"-",n=function(e){let t=Math.floor(Math.abs(e));return(t<10?"0":"")+t};return[this.getFullYear(),"-",n(this.getMonth()+1),"-",n(this.getDate()),"T",n(this.getHours()),":",n(this.getMinutes()),":",n(this.getSeconds()),"."+function(e){let t=Math.floor(Math.abs(e));return(t<100?"0":"")+(t<10?"0":"")+t}(this.getMilliseconds())+function(e){let t=Math.floor(Math.abs(e));return(t<1e5?"0":"")+(t<1e4?"0":"")+(t<1e3?"0":"")+(t<100?"0":"")+(t<10?"0":"")+t}(this.ns)+t,n(e/60),":",n(e%60)].join("")}}),m.set(Boolean.prototype,{external:!1,name:"Boolean",cb:N}),m.set(Number.prototype,{external:!1,name:"Number",cb:function(){return isNaN(this)?"NaN":isFinite(this)?String(this):this<0?"-Infinity":"Infinity"}}),m.set(String.prototype,{external:!1,name:"String",cb:function(){return'"'+n.escape(N.apply(this))+'"'}}),"function"==typeof BigInt&&m.set(BigInt.prototype,{external:!1,name:"BigInt",cb:function(){return this+"n"}}),m.set(ArrayBuffer.prototype,{external:!0,name:"ab",cb:function(){return"["+y(S(this))+"]"}}),m.set(Uint8Array.prototype,{external:!0,name:"u8",cb:function(){return"["+y(S(this.buffer))+"]"}}),m.set(Uint8ClampedArray.prototype,{external:!0,name:"uc8",cb:function(){return"["+y(S(this.buffer))+"]"}}),m.set(Int8Array.prototype,{external:!0,name:"s8",cb:function(){return"["+y(S(this.buffer))+"]"}}),m.set(Uint16Array.prototype,{external:!0,name:"u16",cb:function(){return"["+y(S(this.buffer))+"]"}}),m.set(Int16Array.prototype,{external:!0,name:"s16",cb:function(){return"["+y(S(this.buffer))+"]"}}),m.set(Uint32Array.prototype,{external:!0,name:"u32",cb:function(){return"["+y(S(this.buffer))+"]"}}),m.set(Int32Array.prototype,{external:!0,name:"s32",cb:function(){return"["+y(S(this.buffer))+"]"}}),m.set(Float32Array.prototype,{external:!0,name:"f32",cb:function(){return"["+y(S(this.buffer))+"]"}}),m.set(Float64Array.prototype,{external:!0,name:"f64",cb:function(){return"["+y(S(this.buffer))+"]"}}),m.set(Float64Array.prototype,{external:!0,name:"f64",cb:function(){return"["+y(S(this.buffer))+"]"}}),m.set(RegExp.prototype,a={external:!0,name:"regex",cb:function(e,t){return"'"+escape(this.source)+"'"}}),k.set("regex",{protoCon:RegExp,cb:function(e,t){return new RegExp(this)}}),m.set(Map.prototype,a={external:!0,name:"map",cb:null}),k.set("map",{protoCon:Map,cb:function(e,t){if(!e)return this;this.set(e,t)}}),m.set(Array.prototype,i={external:!1,name:Array.prototype.constructor.name,cb:null}));const d={defineClass(e,n){let r,s=Object.keys(n);for(let e=1;e<s.length;e++){let t,n;(t=s[e-1])>(n=s[e])&&(s[e-1]=n,s[e]=t,e?e-=2:e--)}t.push(r={name:e,tag:s.toString(),proto:Object.getPrototypeOf(n),fields:Object.keys(n)});for(let e=1;e<r.fields.length;e++)if(r.fields[e]<r.fields[e-1]){let t=r.fields[e-1];r.fields[e-1]=r.fields[e],r.fields[e]=t,e>1&&(e-=2)}r.proto===Object.getPrototypeOf({})&&(r.proto=null)},setDefaultObjectToJSOX(e){g=e},isEncoding:e=>!!o.find(((t,n)=>t===e&&n<o.length-1)),encodeObject(e){return g?g.apply(e,[this]):e},stringify:(e,n,r)=>function(e,n,r){if(void 0===e)return"undefined";if(null===e)return;let f,p,k,N;const O=typeof r,j=typeof n;if(f="",p="","number"===O)for(N=0;N<r;N+=1)p+=" ";else"string"===O&&(p=r);if(k=n,n&&"function"!==j&&("object"!==j||"number"!=typeof n.length))throw new Error("JSOX.stringify");l.length=0,s=new WeakMap;const S=E("",{"":e});return w.length=0,S;function E(e,n){var r=f;const s=i.cb,N=a.cb;i.cb=j,a.cb=S;const O=A(e,n);return i.cb=s,a.cb=N,O;function j(){let e,t=[],n=l.length;for(let e=0;e<this.length;e+=1)l[n]=e,t[e]=E(e,this)||"null";return l.length=n,o.length=n,e=0===t.length?"[]":f?["[\n",f,t.join(",\n"+f),"\n",r,"]"].join(""):"["+t.join(",")+"]",e}function S(){let e={tmp:null},t="{",n=!0;for(let[r,s]of this){e.tmp=s;let i=l.length;l[i]=r,t+=(n?"":",")+y(r)+":"+E("tmp",e),l.length=i,n=!1}return t+="}",t}function A(e,n){let s,i,a,N,O,j,S,A=l.length,T=!0,D=n[e],I="object"==typeof D;I&&null!==D&&g&&(h.find((e=>e===D))||(h.push(D),o[A]=D,T=!1,D=g.apply(D,[d]),I="object"==typeof D,h.pop(),o.length=A,I="object"==typeof D));const M=null!=D&&Object.getPrototypeOf(D);let P=M&&(c.get(M)||m.get(M)||null),C=!P&&null!=D&&(u.get(Object.keys(D).toString())||v.get(Object.keys(D).toString())||null);"function"==typeof k&&(T=!1,D=k.call(n,e,D));let X=P&&P.cb||C&&C.cb;if(null!=D&&"object"==typeof D&&"function"==typeof X)if(h.find((e=>e===D)))a=x(D);else{if("object"==typeof D&&(a=x(D),a))return a;h.push(D),o[A]=D,D=X.call(D,d),T=!1,h.pop(),P&&P.name&&"string"==typeof D&&"-"!==D[0]&&(D[0]<"0"||D[0]>"9")&&'"'!==D[0]&&"'"!==D[0]&&"`"!==D[0]&&"["!==D[0]&&"{"!==D[0]&&(D=" "+D),o.length=A}else if("object"==typeof D&&(a=x(D),a))return a;switch(typeof D){case"bigint":return D+"n";case"string":{D=T?y(D):D;let n="";return""===e&&(n=t.map((e=>e.name+"{"+e.fields.join(",")+"}")).join(f?"\n":"")+w.map((e=>e.name+"{"+e.fields.join(",")+"}")).join(f?"\n":"")+(f?"\n":"")),P&&P.external?n+P.name+D:C&&C.external?n+C.name+D:n+D}case"number":case"boolean":case"null":return String(D);case"object":if(a)return"ref"+a;if(!D)return"null";if(f+=p,O=null,j=[],k&&"object"==typeof k){for(N=k.length,O=_(D,k),s=0;s<N;s+=1)"string"==typeof k[s]&&(i=k[s],l[A]=i,a=E(i,D),void 0!==a&&(O?j.push(a):j.push(y(i)+(f?": ":":")+a)));l.splice(A,1)}else{O=_(D);let e=[];for(i in D)if((!b||Object.prototype.propertyIsEnumerable.call(D,i))&&Object.prototype.hasOwnProperty.call(D,i)){let t;for(t=0;t<e.length;t++)if(e[t]>i){e.splice(t,0,i);break}t==e.length&&e.push(i)}for(let t=0;t<e.length;t++)i=e[t],Object.prototype.hasOwnProperty.call(D,i)&&(l[A]=i,a=E(i,D),void 0!==a&&(O?j.push(a):j.push(y(i)+(f?": ":":")+a)));l.splice(A,1)}S=""===e?(t.map((e=>e.name+"{"+e.fields.join(",")+"}")).join(f?"\n":"")||w.map((e=>e.name+"{"+e.fields.join(",")+"}")).join(f?"\n":""))+(f?"\n":""):"",P&&P.external&&(S+=y(P.name));let n=null;return O&&(n=y(O.name)),a=S+(0===j.length?"{}":f?(O?n:"")+"{\n"+f+j.join(",\n"+f)+"\n"+r+"}":(O?n:"")+"{"+j.join(",")+"}"),f=r,a}}}}(e,n,r),setQuote(e){r=e},registerToJSOX(e,t,n){return this.toJSOX(e,t,n)},toJSOX(e,t,n){if(t.prototype&&t.prototype!==Object.prototype){if(c.get(t.prototype))throw new Error("Existing toJSOX has been registered for prototype");c.set(t.prototype,{external:!0,name:e||n.constructor.name,cb:n})}else{let r=Object.keys(t).toString();if(u.get(r))throw new Error("Existing toJSOX has been registered for object type");u.set(r,{external:!0,name:e,cb:n})}},get ignoreNonEnumerable(){return b},set ignoreNonEnumerable(e){b=e}};return d;function x(t){if(null===t)return;let n=s.get(t);if(n)return"ref"+n;s.set(t,e.stringify(l))}function _(e,n){let r,s,i=Object.getPrototypeOf(e);if(s=t.find((e=>{if(e.proto&&e.proto===i)return!0})),s)return s;if(t.length||w.length){if(n)n=n.map((e=>"string"==typeof e?e:void 0)),r=n.toString();else{let t=Object.keys(e);for(let e=1;e<t.length;e++){let n,r;(n=t[e-1])>(r=t[e])&&(t[e-1]=r,t[e]=n,e?e-=2:e--)}r=t.toString()}s=t.find((e=>{if(e.tag===r)return!0})),s||(s=w.find((e=>{if(e.tag===r)return!0})))}return s}};const O="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789$_",j={"~":-1,"=":-1,$:62,_:63,"+":62,"-":62,".":62,"/":63,",":63};for(let e=0;e<O.length;e++)j[O[e]]=e;function S(e){let t,n,r,s,i,a="",l=new Uint8Array(e),o=l.byteLength,c=o%3,u=o-c;for(let e=0;e<u;e+=3)i=l[e]<<16|l[e+1]<<8|l[e+2],t=(16515072&i)>>18,n=(258048&i)>>12,r=(4032&i)>>6,s=63&i,a+=O[t]+O[n]+O[r]+O[s];return 1==c?(i=l[u],t=(252&i)>>2,n=(3&i)<<4,a+=O[t]+O[n]+"=="):2==c&&(i=l[u]<<8|l[u+1],t=(64512&i)>>10,n=(1008&i)>>4,r=(15&i)<<2,a+=O[t]+O[n]+O[r]+"="),a}function E(e){let t;t=e.length%4==1?3*((e.length+3)/4|0)-3:e.length%4==2?3*((e.length+3)/4|0)-2:e.length%4==3?3*((e.length+3)/4|0)-1:-1==j[e[e.length-3]]?3*((e.length+3)/4|0)-3:-1==j[e[e.length-2]]?3*((e.length+3)/4|0)-2:-1==j[e[e.length-1]]?3*((e.length+3)/4|0)-1:3*((e.length+3)/4|0);let n,r=new ArrayBuffer(t),s=new Uint8Array(r),i=e.length+3>>2;for(n=0;n<i;n++){let t=j[e[4*n]],r=4*n+1<e.length?j[e[4*n+1]]:-1,i=r>=0&&4*n+2<e.length?j[e[4*n+2]]:-1,a=i>=0&&4*n+3<e.length?j[e[4*n+3]]:-1;r>=0&&(s[3*n+0]=t<<2|r>>4),i>=0&&(s[3*n+1]=r<<4|i>>2&15),a>=0&&(s[3*n+2]=i<<6|63&a)}return r}Object.freeze(j),n.stringify=function(e,t,r){return n.stringifier().stringify(e,t,r)},[[0,256,[16767487,16739071,130048,3670016,0,16777208,16777215,8388607]]].map((e=>({firstChar:e[0],lastChar:e[1],bits:e[2]})));export{n as JSOX};