@yuebai008/cli
Version:
Command line interface for rapid qg-minigame development
1 lines • 3.39 kB
JavaScript
import*as TextUtils from"../../models/text_utils/text_utils.js";import{HeapSnapshotProgress,JSHeapSnapshot}from"./HeapSnapshot.js";export class HeapSnapshotLoader{#s;#t;#n;#a;#i;#o;#e;#h;#r;constructor(s){this.#p(),this.#s=new HeapSnapshotProgress(s),this.#t="",this.#n=null,this.#a=!1,this.#c()}dispose(){this.#p()}#p(){this.#h="",this.#i=void 0}close(){this.#a=!0,this.#n&&this.#n("")}buildSnapshot(){this.#i=this.#i||{},this.#s.updateStatus("Processing snapshot…");const s=new JSHeapSnapshot(this.#i,this.#s);return this.#p(),s}#d(){let s=0;const t="0".charCodeAt(0),n="9".charCodeAt(0),a="]".charCodeAt(0),i=this.#h.length;for(;;){for(;s<i;){const i=this.#h.charCodeAt(s);if(t<=i&&i<=n)break;if(i===a)return this.#h=this.#h.slice(s+1),!1;++s}if(s===i)return this.#h="",!0;let o=0;const e=s;for(;s<i;){const a=this.#h.charCodeAt(s);if(t>a||a>n)break;o*=10,o+=a-t,++s}if(s===i)return this.#h=this.#h.slice(e),!0;if(!this.#o)throw new Error("Array not instantiated");this.#o[this.#e++]=o}}#l(){this.#s.updateStatus("Parsing strings…");const s=this.#h.lastIndexOf("]");if(-1===s)throw new Error("Incomplete JSON");if(this.#h=this.#h.slice(0,s+1),!this.#i)throw new Error("No snapshot in parseStringsArray");this.#i.strings=JSON.parse(this.#h)}write(s){this.#t+=s,this.#n&&(this.#n(this.#t),this.#n=null,this.#t="")}#f(){return this.#a?Promise.resolve(this.#t):new Promise((s=>{this.#n=s}))}async#u(s,t){for(;;){const n=this.#h.indexOf(s,t||0);if(-1!==n)return n;t=this.#h.length-s.length+1,this.#h+=await this.#f()}}async#g(s,t,n){const a=await this.#u(s),i=await this.#u("[",a);for(this.#h=this.#h.slice(i+1),this.#o=n?new Uint32Array(n):[],this.#e=0;this.#d();)n?this.#s.updateProgress(t,this.#e,this.#o.length):this.#s.updateStatus(t),this.#h+=await this.#f();const o=this.#o;return this.#o=null,o}async#c(){const s='"snapshot"',t=await this.#u(s);if(-1===t)throw new Error("Snapshot token not found");this.#s.updateStatus("Loading snapshot info…");const n=this.#h.slice(t+10+1);for(this.#r=new TextUtils.TextUtils.BalancedJSONTokenizer((s=>{this.#h=this.#r.remainder(),this.#r=null,this.#i=this.#i||{},this.#i.snapshot=JSON.parse(s)})),this.#r.write(n);this.#r;)this.#r.write(await this.#f());this.#i=this.#i||{};const a=await this.#g('"nodes"',"Loading nodes… {PH1}%",this.#i.snapshot.meta.node_fields.length*this.#i.snapshot.node_count);this.#i.nodes=a;const i=await this.#g('"edges"',"Loading edges… {PH1}%",this.#i.snapshot.meta.edge_fields.length*this.#i.snapshot.edge_count);if(this.#i.edges=i,this.#i.snapshot.trace_function_count){const s=await this.#g('"trace_function_infos"',"Loading allocation traces… {PH1}%",this.#i.snapshot.meta.trace_function_info_fields.length*this.#i.snapshot.trace_function_count);this.#i.trace_function_infos=s;const t=await this.#u(":"),n=await this.#u('"',t),a=this.#h.indexOf("["),i=this.#h.lastIndexOf("]",n);this.#i.trace_tree=JSON.parse(this.#h.substring(a,i+1)),this.#h=this.#h.slice(i+1)}if(this.#i.snapshot.meta.sample_fields){const s=await this.#g('"samples"',"Loading samples…");this.#i.samples=s}if(this.#i.snapshot.meta.location_fields){const s=await this.#g('"locations"',"Loading locations…");this.#i.locations=s}else this.#i.locations=[];this.#s.updateStatus("Loading strings…");const o=await this.#u('"strings"'),e=await this.#u("[",o);for(this.#h=this.#h.slice(e);!this.#a;)this.#h+=await this.#f();this.#l()}}