tlo
Version:
Reader of binary serialized Type Language Schema
16 lines (13 loc) • 19.1 kB
JavaScript
!function(t,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("fs")):"function"==typeof define&&define.amd?define(["exports","fs"],r):r((t="undefined"!=typeof globalThis?globalThis:t||self).tlo={},t.fs)}(this,(function(t,r){"use strict";function e(t){if(t&&t.__esModule)return t;var r=Object.create(null);return t&&Object.keys(t).forEach((function(e){if("default"!==e){var n=Object.getOwnPropertyDescriptor(t,e);Object.defineProperty(r,e,n.get?n:{enumerable:!0,get:function(){return t[e]}})}})),r.default=t,Object.freeze(r)}var n=e(r),o=function(t,r){return(o=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,r){t.__proto__=r}||function(t,r){for(var e in r)Object.prototype.hasOwnProperty.call(r,e)&&(t[e]=r[e])})(t,r)};
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */function i(t,r){if("function"!=typeof r&&null!==r)throw new TypeError("Class extends value "+String(r)+" is not a constructor or null");function e(){this.constructor=t}o(t,r),t.prototype=null===r?Object.create(r):(e.prototype=r.prototype,new e)}var a=void 0!==a?a:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{};function u(t){return null!=t&&(!!t._isBuffer||c(t)||function(t){return"function"==typeof t.readFloatLE&&"function"==typeof t.slice&&c(t.slice(0,0))}(t))}function c(t){return!!t.constructor&&"function"==typeof t.constructor.isBuffer&&t.constructor.isBuffer(t)}void 0===a.TYPED_ARRAY_SUPPORT||a.TYPED_ARRAY_SUPPORT;var f="function"==typeof Object.create?function(t,r){t.super_=r,t.prototype=Object.create(r.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}})}:function(t,r){t.super_=r;var e=function(){};e.prototype=r.prototype,t.prototype=new e,t.prototype.constructor=t};function s(t,r){var e={seen:[],stylize:p};return arguments.length>=3&&(e.depth=arguments[2]),arguments.length>=4&&(e.colors=arguments[3]),d(r)?e.showHidden=r:r&&k(e,r),m(e.showHidden)&&(e.showHidden=!1),m(e.depth)&&(e.depth=2),m(e.colors)&&(e.colors=!1),m(e.customInspect)&&(e.customInspect=!0),e.colors&&(e.stylize=l),_(e,t,e.depth)}function l(t,r){var e=s.styles[r];return e?"["+s.colors[e][0]+"m"+t+"["+s.colors[e][1]+"m":t}function p(t,r){return t}function _(t,r,e){if(t.customInspect&&r&&O(r.inspect)&&r.inspect!==s&&(!r.constructor||r.constructor.prototype!==r)){var n=r.inspect(e,t);return v(n)||(n=_(t,n,e)),n}var o=function(t,r){if(m(r))return t.stylize("undefined","undefined");if(v(r)){var e="'"+JSON.stringify(r).replace(/^"|"$/g,"").replace(/'/g,"\\'").replace(/\\"/g,'"')+"'";return t.stylize(e,"string")}if(n=r,"number"==typeof n)return t.stylize(""+r,"number");var n;if(d(r))return t.stylize(""+r,"boolean");if(h(r))return t.stylize("null","null")}(t,r);if(o)return o;var i=Object.keys(r),a=function(t){var r={};return t.forEach((function(t,e){r[t]=!0})),r}(i);if(t.showHidden&&(i=Object.getOwnPropertyNames(r)),x(r)&&(i.indexOf("message")>=0||i.indexOf("description")>=0))return y(r);if(0===i.length){if(O(r)){var u=r.name?": "+r.name:"";return t.stylize("[Function"+u+"]","special")}if(w(r))return t.stylize(RegExp.prototype.toString.call(r),"regexp");if(E(r))return t.stylize(Date.prototype.toString.call(r),"date");if(x(r))return y(r)}var c,f,l="",p=!1,b=["{","}"];(c=r,Array.isArray(c)&&(p=!0,b=["[","]"]),O(r))&&(l=" [Function"+(r.name?": "+r.name:"")+"]");return w(r)&&(l=" "+RegExp.prototype.toString.call(r)),E(r)&&(l=" "+Date.prototype.toUTCString.call(r)),x(r)&&(l=" "+y(r)),0!==i.length||p&&0!=r.length?e<0?w(r)?t.stylize(RegExp.prototype.toString.call(r),"regexp"):t.stylize("[Object]","special"):(t.seen.push(r),f=p?function(t,r,e,n,o){for(var i=[],a=0,u=r.length;a<u;++a)A(r,String(a))?i.push(g(t,r,e,n,String(a),!0)):i.push("");return o.forEach((function(o){o.match(/^\d+$/)||i.push(g(t,r,e,n,o,!0))})),i}(t,r,e,a,i):i.map((function(n){return g(t,r,e,a,n,p)})),t.seen.pop(),function(t,r,e){if(t.reduce((function(t,r){return r.indexOf("\n"),t+r.replace(/\u001b\[\d\d?m/g,"").length+1}),0)>60)return e[0]+(""===r?"":r+"\n ")+" "+t.join(",\n ")+" "+e[1];return e[0]+r+" "+t.join(", ")+" "+e[1]}(f,l,b)):b[0]+l+b[1]}function y(t){return"["+Error.prototype.toString.call(t)+"]"}function g(t,r,e,n,o,i){var a,u,c;if((c=Object.getOwnPropertyDescriptor(r,o)||{value:r[o]}).get?u=c.set?t.stylize("[Getter/Setter]","special"):t.stylize("[Getter]","special"):c.set&&(u=t.stylize("[Setter]","special")),A(n,o)||(a="["+o+"]"),u||(t.seen.indexOf(c.value)<0?(u=h(e)?_(t,c.value,null):_(t,c.value,e-1)).indexOf("\n")>-1&&(u=i?u.split("\n").map((function(t){return" "+t})).join("\n").substr(2):"\n"+u.split("\n").map((function(t){return" "+t})).join("\n")):u=t.stylize("[Circular]","special")),m(a)){if(i&&o.match(/^\d+$/))return u;(a=JSON.stringify(""+o)).match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)?(a=a.substr(1,a.length-2),a=t.stylize(a,"name")):(a=a.replace(/'/g,"\\'").replace(/\\"/g,'"').replace(/(^"|"$)/g,"'"),a=t.stylize(a,"string"))}return a+": "+u}function d(t){return"boolean"==typeof t}function h(t){return null===t}function v(t){return"string"==typeof t}function m(t){return void 0===t}function w(t){return b(t)&&"[object RegExp]"===S(t)}function b(t){return"object"==typeof t&&null!==t}function E(t){return b(t)&&"[object Date]"===S(t)}function x(t){return b(t)&&("[object Error]"===S(t)||t instanceof Error)}function O(t){return"function"==typeof t}function j(t){return null===t||"boolean"==typeof t||"number"==typeof t||"string"==typeof t||"symbol"==typeof t||void 0===t}function S(t){return Object.prototype.toString.call(t)}function k(t,r){if(!r||!b(r))return t;for(var e=Object.keys(r),n=e.length;n--;)t[e[n]]=r[e[n]];return t}function A(t,r){return Object.prototype.hasOwnProperty.call(t,r)}function z(t,r){if(t===r)return 0;for(var e=t.length,n=r.length,o=0,i=Math.min(e,n);o<i;++o)if(t[o]!==r[o]){e=t[o],n=r[o];break}return e<n?-1:n<e?1:0}s.colors={bold:[1,22],italic:[3,23],underline:[4,24],inverse:[7,27],white:[37,39],grey:[90,39],black:[30,39],blue:[34,39],cyan:[36,39],green:[32,39],magenta:[35,39],red:[31,39],yellow:[33,39]},s.styles={special:"cyan",number:"yellow",boolean:"yellow",undefined:"grey",null:"bold",string:"green",date:"magenta",regexp:"red"};var P,D=Object.prototype.hasOwnProperty,T=Object.keys||function(t){var r=[];for(var e in t)D.call(t,e)&&r.push(e);return r},q=Array.prototype.slice;function C(){return void 0!==P?P:P="foo"===function(){}.name}function B(t){return Object.prototype.toString.call(t)}function R(t){return!u(t)&&("function"==typeof a.ArrayBuffer&&("function"==typeof ArrayBuffer.isView?ArrayBuffer.isView(t):!!t&&(t instanceof DataView||!!(t.buffer&&t.buffer instanceof ArrayBuffer))))}function F(t,r){t||W(t,!0,r,"==",$)}var I=/\s*function\s+([^\(\s]*)\s*/;function U(t){if(O(t)){if(C())return t.name;var r=t.toString().match(I);return r&&r[1]}}function V(t){this.name="AssertionError",this.actual=t.actual,this.expected=t.expected,this.operator=t.operator,t.message?(this.message=t.message,this.generatedMessage=!1):(this.message=function(t){return M(N(t.actual),128)+" "+t.operator+" "+M(N(t.expected),128)}(this),this.generatedMessage=!0);var r=t.stackStartFunction||W;if(Error.captureStackTrace)Error.captureStackTrace(this,r);else{var e=new Error;if(e.stack){var n=e.stack,o=U(r),i=n.indexOf("\n"+o);if(i>=0){var a=n.indexOf("\n",i+1);n=n.substring(a+1)}this.stack=n}}}function M(t,r){return"string"==typeof t?t.length<r?t:t.slice(0,r):t}function N(t){if(C()||!O(t))return s(t);var r=U(t);return"[Function"+(r?": "+r:"")+"]"}function W(t,r,e,n,o){throw new V({message:e,actual:t,expected:r,operator:n,stackStartFunction:o})}function $(t,r){t||W(t,!0,r,"==",$)}function H(t,r,e,n){if(t===r)return!0;if(u(t)&&u(r))return 0===z(t,r);if(E(t)&&E(r))return t.getTime()===r.getTime();if(w(t)&&w(r))return t.source===r.source&&t.global===r.global&&t.multiline===r.multiline&&t.lastIndex===r.lastIndex&&t.ignoreCase===r.ignoreCase;if(null!==t&&"object"==typeof t||null!==r&&"object"==typeof r){if(R(t)&&R(r)&&B(t)===B(r)&&!(t instanceof Float32Array||t instanceof Float64Array))return 0===z(new Uint8Array(t.buffer),new Uint8Array(r.buffer));if(u(t)!==u(r))return!1;var o=(n=n||{actual:[],expected:[]}).actual.indexOf(t);return-1!==o&&o===n.expected.indexOf(r)||(n.actual.push(t),n.expected.push(r),function(t,r,e,n){if(null==t||null==r)return!1;if(j(t)||j(r))return t===r;if(e&&Object.getPrototypeOf(t)!==Object.getPrototypeOf(r))return!1;var o=Y(t),i=Y(r);if(o&&!i||!o&&i)return!1;if(o)return H(t=q.call(t),r=q.call(r),e);var a,u,c=T(t),f=T(r);if(c.length!==f.length)return!1;for(c.sort(),f.sort(),u=c.length-1;u>=0;u--)if(c[u]!==f[u])return!1;for(u=c.length-1;u>=0;u--)if(!H(t[a=c[u]],r[a],e,n))return!1;return!0}(t,r,e,n))}return e?t===r:t==r}function Y(t){return"[object Arguments]"==Object.prototype.toString.call(t)}function G(t,r){if(!t||!r)return!1;if("[object RegExp]"==Object.prototype.toString.call(r))return r.test(t);try{if(t instanceof r)return!0}catch(t){}return!Error.isPrototypeOf(r)&&!0===r.call({},t)}function J(t,r,e,n){var o;if("function"!=typeof r)throw new TypeError('"block" argument must be a function');"string"==typeof e&&(n=e,e=null),o=function(t){var r;try{t()}catch(t){r=t}return r}(r),n=(e&&e.name?" ("+e.name+").":".")+(n?" "+n:"."),t&&!o&&W(o,e,"Missing expected exception"+n);var i="string"==typeof n,a=!t&&o&&!e;if((!t&&x(o)&&i&&G(o,e)||a)&&W(o,e,"Got unwanted exception"+n),t&&o&&e&&!G(o,e)||!t&&o)throw o}F.AssertionError=V,f(V,Error),F.fail=W,F.ok=$,F.equal=function t(r,e,n){r!=e&&W(r,e,n,"==",t)},F.notEqual=function t(r,e,n){r==e&&W(r,e,n,"!=",t)},F.deepEqual=function t(r,e,n){H(r,e,!1)||W(r,e,n,"deepEqual",t)},F.deepStrictEqual=function t(r,e,n){H(r,e,!0)||W(r,e,n,"deepStrictEqual",t)},F.notDeepEqual=function t(r,e,n){H(r,e,!1)&&W(r,e,n,"notDeepEqual",t)},F.notDeepStrictEqual=function t(r,e,n){H(r,e,!0)&&W(r,e,n,"notDeepStrictEqual",t)},F.strictEqual=function t(r,e,n){r!==e&&W(r,e,n,"===",t)},F.notStrictEqual=function t(r,e,n){r===e&&W(r,e,n,"!==",t)},F.throws=function(t,r,e){J(!0,t,r,e)},F.doesNotThrow=function(t,r,e){J(!1,t,r,e)},F.ifError=function(t){if(t)throw t};var L=1<<21,Z=function(){},K=function(t){function r(r,e,n,o,i,a){var u=t.call(this)||this;return u.name=r,u.flags=e||0,u.var_num=n,u.exist_var_num=o,u.exist_var_bit=i,u.type=a,u}return i(r,t),r}(Z),Q=function(t){function r(r,e,n,o,i,a){var u=t.call(this)||this;return u.id=r,u.name=e,u.var_count=n||0,u.type_id=o||0,u.args=i,u.result=a,u}return i(r,t),r}(Z),X=function(t){function r(r,e,n,o,i,a,u){var c=t.call(this)||this;return c.add_constructor=function(t){c.constructors&&c.constructors.push(t)},c.id=r,c.name=e,c.arity=n,c.flags=o||0,c.simple_constructors=i,c.constructors_num=a,c.constructors=u,c}return i(r,t),r}(Z),tt=function(t){function r(r){var e=t.call(this)||this;return e.get_type=function(){throw new Error("Not Implemented")},e.flags=r,e}return i(r,t),r}(Z),rt=function(t){function r(r,e){var n=t.call(this,r)||this;return n.get_type=function(){return 1},n.type=e,n.children=[],n}return i(r,t),r}(tt),et=function(t){function r(r,e){var n=t.call(this,r)||this;return n.get_type=function(){return 2},n.num=e,n}return i(r,t),r}(tt),nt=function(t){function r(r,e){var n=t.call(this,r)||this;return n.get_type=function(){return 3},n.var_num=e,n}return i(r,t),r}(tt),ot=function(t){function r(r,e,n){var o=t.call(this,r)||this;return o.get_type=function(){return 4},o.var_num=e,o.diff=n,o}return i(r,t),r}(tt),it=function(t){function r(r,e,n){var o=t.call(this,r)||this;return o.get_type=function(){return 5},o.multiplicity=e,o.args=n,o}return i(r,t),r}(tt),at=function(t){function r(){var r=t.call(this)||this;return r.add_type=function(t){t.id&&t.name&&(r.types.push(t),r.id_to_type[t.id]=t,r.name_to_type[t.name]=t)},r.get_type=function(t){return"number"==typeof t?r.id_to_type[t]:r.name_to_type[t]},r.add_function=function(t){t.id&&t.name&&(r.functions.push(t),r.id_to_function[t.id]=t,r.name_to_function[t.name]=t)},r.get_function=function(t){return"number"==typeof t?r.id_to_function[t]:r.name_to_function[t]},r.get_type_count=function(){return r.types.length},r.get_type_by_num=function(t){return r.types[t]},r.get_function_count=function(){return r.functions.length},r.get_function_by_num=function(t){return r.functions[t]},r.types=[],r.id_to_type={},r.name_to_type={},r.functions=[],r.id_to_function={},r.name_to_function={},r}return i(r,t),r}(Z),ut=function(t){var r=this;this.set_error=function(t){null!=r.error?(F(null!=t),r.error=t,r.error_pos=r.data_begin_len-r.offset,r.data=new ArrayBuffer(8),r.data_len=0):(r.data=new ArrayBuffer(8),F(0==r.data_len))},this.check_len=function(t){r.data_len<t?r.set_error("Not enough data to read"):r.data_len-=t},this.get_error=function(){return r.error},this.get_error_pos=function(){return r.error_pos},this.fetch_int=function(){r.check_len(4);var t=new DataView(r.data,r.offset).getUint32(0,!0);return r.offset+=4,t},this.fetch_long=function(){r.check_len(8);var t=new DataView(r.data,r.offset).getBigUint64(0,!0);return r.offset+=8,t},this.unpack_uchar=function(t){return void 0===t&&(t=0),new DataView(r.data,r.offset+t).getInt8(0)},this.fetch_string=function(){r.check_len(4);var t=r.unpack_uchar();if(t<254){r.check_len(4*(t>>2));for(var e=[],n=new DataView(r.data,r.offset+1),o=0;o<t;o++)e.push(n.getInt8(1*o));var i=e.map((function(t){return String.fromCharCode(t)}));return r.offset+=4*(1+(t>>2)),i.join("")}if(254==t){var a=r.unpack_uchar(1)+(r.unpack_uchar(2)<<8)+(r.unpack_uchar(3)<<16);for(r.check_len(4*(a+3>>2)),e=[],n=new DataView(r.data,r.offset+4),o=0;o<a;o++)e.push(n.getInt8(1*o));return i=e.map((function(t){return String.fromCharCode(t)})),r.offset+=4*(a+7>>2),i.join("")}return r.set_error("Can't fetch string, 255 found"),""},this.fetch_end=function(){r.data_len&&r.set_error("Too much data to fetch")},this.data=function(t){for(var r=new ArrayBuffer(t.length),e=new Uint8Array(r),n=0;n<t.length;++n)e[n]=t[n];return r}(t),this.data_begin_len=t.length,this.data_len=t.length,this.offset=0,this.error="",this.error_pos=0},ct=function(t){function r(r){var e=t.call(this)||this;return e.parse_config=function(){if(e.schema_version=e.get_schema_version(e.try_parse_int()),e.schema_version<2)throw Error("Unsupported tl-schema version "+e.schema_version);e.try_parse_int(),e.try_parse_int();for(var t=e.try_parse_int(),r=0,n=0;n<t;n++){var o=e.read_type();e.config.add_type(o),r+=o.constructors_num||0}var i=e.try_parse_int();F(i==r);for(n=0;n<i;n++){var a=e.read_combinator();e.config.get_type(a.type_id).add_constructor(a)}var u=e.try_parse_int();for(n=0;n<u;n++)e.config.add_function(e.read_combinator());return e.p.fetch_end(),e.try_parse(0),e.config},e.get_schema_version=function(t){switch(t){case 2427226327:return 4;case 3836239947:return 3;case 976198626:return 2;default:return-1}},e.read_type=function(){var t=e.try_parse_int();if(317408134!=t)throw Error("Wrong tls_type magic "+t);var r=new X;if(r.id=e.try_parse_int(),r.name=e.try_parse_string(),r.constructors_num=e.try_parse_int(),r.constructors=[],r.flags=e.try_parse_int(),r.flags&=-1050,0!=r.flags)throw Error("Type "+r.name+" has non-zero flags: "+r.flags);return r.arity=e.try_parse_int(),e.try_parse_long(),r},e.read_combinator=function(){var t=e.try_parse_int();if(1544167125!=t)throw Error("Wrong tls_combinator magic "+t);var r=new Q;r.id=e.try_parse_int(),r.name=e.try_parse_string(),r.type_id=e.try_parse_int(),r.var_count=0;var n=e.try_parse_int();if(1276298969==n)r.args=e.read_args_list(r);else if(3441500003!=n)throw Error("Wrong tls_combinator_left magic "+n);var o=e.try_parse_int();if(738607986!=o)throw Error("'Wrong tls_combinator_right magic "+o);return r.result=e.read_type_expr(r),r},e.read_num_const=function(){var t=e.try_parse_int();return new et(L,t)},e.read_num_var=function(t){var r=e.try_parse_int(),n=e.try_parse_int();return n>=t.var_count&&(t.var_count=n+1),new ot(0,n,r)},e.read_nat_expr=function(t){var r=e.try_parse_int();switch(r){case 3702823896:case 2364096689:return e.read_num_const();case 1317672176:return e.read_num_var(t);default:throw Error("tree_type = "+r)}},e.read_expr=function(t){var r=e.try_parse_int();switch(r){case 3702823896:return e.read_nat_expr(t);case 3972651640:return e.read_type_expr(t);default:throw Error("tree_type = "+r)}},e.read_args_list=function(t){for(var r=2<<(e.schema_version>=3?1:0),n=6^r,o=e.try_parse_int(),i=[],a=0;a<o;a++){var u=new K,c=e.try_parse_int();if(702539291!=c)throw Error("Wrong tls_arg magic "+c);u.name=e.try_parse_string(),u.flags=e.try_parse_int();var f=!1;u.flags&r&&(u.flags&=~r,f=!0),u.flags&n?(u.flags&=~n,u.var_num=e.try_parse_int()):u.var_num=-1,u.var_num>=t.var_count&&(t.var_count=u.var_num+1),f?(u.exist_var_num=e.try_parse_int(),u.exist_var_bit=e.try_parse_int()):(u.exist_var_num=-1,u.exist_var_bit=0),u.type=e.read_type_expr(t),u.type.flags&L&&(u.flags|=L),i.push(u)}return i},e.read_type_expr=function(t){var r=e.try_parse_int();switch(r){case 21155502:return e.read_type_var(t);case 3246800136:return e.read_type_tree(t);case 3657113822:return e.read_array(t);default:throw Error("tree_type = "+r)}},e.read_type_var=function(t){var r=e.try_parse_int(),n=e.try_parse_int();return r>=t.var_count&&(t.var_count=r+1),F(!(2097153&n)),new nt(n,r)},e.read_type_tree=function(t){var r=e.config.get_type(e.try_parse_int());F(null!=r);var n=e.try_parse_int()|L,o=e.try_parse_int();F(r.arity==o);for(var i=new rt(n,r),a=0;a<o;a++){var u=e.read_expr(t);i.children.push(u),u.flags&L||(i.flags&=-2097153)}return i},e.read_array=function(t){for(var r=e.read_nat_expr(t),n=new it(2097152,r,e.read_args_list(t)),o=0;o<n.args.length;o++)n.args[o].flags&L||(n.flags&=-2097153);return n},e.try_parse=function(t){if(e.p.get_error())throw Error("Wrong TL-scheme specified: "+e.p.get_error()+" at "+e.p.get_error_pos());return t},e.try_parse_int=function(){return e.try_parse(e.p.fetch_int())},e.try_parse_long=function(){return e.try_parse(e.p.fetch_long())},e.try_parse_string=function(){return e.try_parse(e.p.fetch_string())},e.p=new ut(r),e.schema_version=-1,e.config=new at,e}return i(r,t),r}(Z),ft=function(t){if(!t)throw Error("Config data is empty");if(t.length%4!=0)throw Error("Config size = "+t.length+" is not multiple of 4");return new ct(t).parse_config()};t.read_tl_config=ft,t.read_tl_config_from_file=function(t,r){n.readFile(t,(function(t,e){if(t)throw t;r(ft(e))}))},Object.defineProperty(t,"__esModule",{value:!0})}));