@yuebai008/cli
Version:
Command line interface for rapid qg-minigame development
1 lines • 11.3 kB
JavaScript
import*as Common from"../../core/common/common.js";import*as Platform from"../../core/platform/platform.js";import*as Root from"../../core/root/root.js";import*as SDK from"../../core/sdk/sdk.js";import*as Workspace from"../workspace/workspace.js";import{CompilerScriptMapping}from"./CompilerScriptMapping.js";import{DebuggerLanguagePluginManager}from"./DebuggerLanguagePlugins.js";import{DefaultScriptMapping}from"./DefaultScriptMapping.js";import{IgnoreListManager}from"./IgnoreListManager.js";import{LiveLocationWithPool}from"./LiveLocation.js";import{NetworkProject}from"./NetworkProject.js";import{ResourceScriptMapping}from"./ResourceScriptMapping.js";let debuggerWorkspaceBindingInstance;export class DebuggerWorkspaceBinding{resourceMapping;#e;#o;#t;pluginManager;#i;constructor(e,o){this.resourceMapping=e,this.#e=[],this.#o=new Map,o.addModelListener(SDK.DebuggerModel.DebuggerModel,SDK.DebuggerModel.Events.GlobalObjectCleared,this.globalObjectCleared,this),o.addModelListener(SDK.DebuggerModel.DebuggerModel,SDK.DebuggerModel.Events.DebuggerResumed,this.debuggerResumed,this),o.observeModels(SDK.DebuggerModel.DebuggerModel,this),this.#i=o,this.#t=new Set,this.pluginManager=Root.Runtime.experiments.isEnabled("wasmDWARFDebugging")?new DebuggerLanguagePluginManager(o,e.workspace,this):null}initPluginManagerForTest(){return Root.Runtime.experiments.isEnabled("wasmDWARFDebugging")?this.pluginManager||(this.pluginManager=new DebuggerLanguagePluginManager(this.#i,this.resourceMapping.workspace,this)):this.pluginManager=null,this.pluginManager}static instance(e={forceNew:null,resourceMapping:null,targetManager:null}){const{forceNew:o,resourceMapping:t,targetManager:i}=e;if(!debuggerWorkspaceBindingInstance||o){if(!t||!i)throw new Error(`Unable to create DebuggerWorkspaceBinding: resourceMapping and targetManager must be provided: ${(new Error).stack}`);debuggerWorkspaceBindingInstance=new DebuggerWorkspaceBinding(t,i)}return debuggerWorkspaceBindingInstance}static removeInstance(){debuggerWorkspaceBindingInstance=void 0}addSourceMapping(e){this.#e.push(e)}removeSourceMapping(e){const o=this.#e.indexOf(e);-1!==o&&this.#e.splice(o,1)}async computeAutoStepRanges(e,o){function t(e,o){const{start:t,end:i}=o;return t.scriptId===e.scriptId&&(!(e.lineNumber<t.lineNumber||e.lineNumber>i.lineNumber)&&(!(e.lineNumber===t.lineNumber&&e.columnNumber<t.columnNumber)&&!(e.lineNumber===i.lineNumber&&e.columnNumber>=i.columnNumber)))}const i=o.location();if(!i)return[];const a=this.pluginManager;let r=[];if(a){if(e===SDK.DebuggerModel.StepMode.StepOut)return await a.getInlinedFunctionRanges(i);const o=await a.rawLocationToUILocation(i);if(o)return r=await a.uiLocationToRawLocationRanges(o.uiSourceCode,o.lineNumber,o.columnNumber)||[],r=r.filter((e=>t(i,e))),e===SDK.DebuggerModel.StepMode.StepOver&&(r=r.concat(await a.getInlinedCalleesRanges(i))),r}const n=this.#o.get(i.debuggerModel)?.compilerMapping;return n?e===SDK.DebuggerModel.StepMode.StepOut?[]:(r=n.getLocationRangesForSameSourceLocation(i),r=r.filter((e=>t(i,e))),r):[]}modelAdded(e){e.setBeforePausedCallback(this.shouldPause.bind(this)),this.#o.set(e,new ModelData(e,this)),e.setComputeAutoStepRangesCallback(this.computeAutoStepRanges.bind(this))}modelRemoved(e){e.setComputeAutoStepRangesCallback(null);const o=this.#o.get(e);o&&(o.dispose(),this.#o.delete(e))}async pendingLiveLocationChangesPromise(){await Promise.all(this.#t)}recordLiveLocationChange(e){e.then((()=>{this.#t.delete(e)})),this.#t.add(e)}async updateLocations(e){const o=this.#o.get(e.debuggerModel);if(o){const t=o.updateLocations(e);this.recordLiveLocationChange(t),await t}}async createLiveLocation(e,o,t){const i=this.#o.get(e.debuggerModel);if(!i)return null;const a=i.createLiveLocation(e,o,t);return this.recordLiveLocationChange(a),a}async createStackTraceTopFrameLiveLocation(e,o,t){console.assert(e.length>0);const i=StackTraceTopFrameLocation.createStackTraceTopFrameLocation(e,this,o,t);return this.recordLiveLocationChange(i),i}async createCallFrameLiveLocation(e,o,t){if(!e.script())return null;const i=e.debuggerModel,a=this.createLiveLocation(e,o,t);this.recordLiveLocationChange(a);const r=await a;return r?(this.registerCallFrameLiveLocation(i,r),r):null}async rawLocationToUILocation(e){for(const o of this.#e){const t=o.rawLocationToUILocation(e);if(t)return t}if(this.pluginManager){const o=await this.pluginManager.rawLocationToUILocation(e);if(o)return o}const o=this.#o.get(e.debuggerModel);return o?o.rawLocationToUILocation(e):null}uiSourceCodeForSourceMapSourceURL(e,o,t){const i=this.#o.get(e);return i?i.compilerMapping.uiSourceCodeForURL(o,t):null}async uiSourceCodeForSourceMapSourceURLPromise(e,o,t){return this.uiSourceCodeForSourceMapSourceURL(e,o,t)||this.waitForUISourceCodeAdded(o,e.target())}async uiSourceCodeForDebuggerLanguagePluginSourceURLPromise(e,o){if(this.pluginManager){return this.pluginManager.uiSourceCodeForURL(e,o)||this.waitForUISourceCodeAdded(o,e.target())}return null}uiSourceCodeForScript(e){const o=this.#o.get(e.debuggerModel);return o?o.uiSourceCodeForScript(e):null}waitForUISourceCodeAdded(e,o){return new Promise((t=>{const i=Workspace.Workspace.WorkspaceImpl.instance(),a=i.addEventListener(Workspace.Workspace.Events.UISourceCodeAdded,(r=>{const n=r.data;n.url()===e&&NetworkProject.targetForUISourceCode(n)===o&&(i.removeEventListener(Workspace.Workspace.Events.UISourceCodeAdded,a.listener),t(n))}))}))}async uiLocationToRawLocations(e,o,t){for(const i of this.#e){const a=i.uiLocationToRawLocations(e,o,t);if(a.length)return a}const i=await(this.pluginManager?.uiLocationToRawLocations(e,o,t));if(i)return i;for(const i of this.#o.values()){const a=i.uiLocationToRawLocations(e,o,t);if(a.length)return a}return[]}async uiLocationRangeToRawLocationRanges(e,o){for(const t of this.#e){const i=t.uiLocationRangeToRawLocationRanges(e,o);if(i)return i}if(null!==this.pluginManager){const t=await this.pluginManager.uiLocationRangeToRawLocationRanges(e,o);if(t)return t}for(const t of this.#o.values()){const i=t.uiLocationRangeToRawLocationRanges(e,o);if(i)return i}return[]}uiLocationToRawLocationsForUnformattedJavaScript(e,o,t){console.assert(e.contentType().isScript());const i=[];for(const a of this.#o.values())i.push(...a.uiLocationToRawLocations(e,o,t));return i}async normalizeUILocation(e){const o=await this.uiLocationToRawLocations(e.uiSourceCode,e.lineNumber,e.columnNumber);for(const e of o){const o=await this.rawLocationToUILocation(e);if(o)return o}return e}async getMappedLines(e){for(const o of this.#o.values()){const t=o.getMappedLines(e);if(null!==t)return t}const{pluginManager:o}=this;return o?await o.getMappedLines(e):null}scriptFile(e,o){const t=this.#o.get(o);return t?t.getResourceScriptMapping().scriptFile(e):null}scriptsForUISourceCode(e){const o=new Set;this.pluginManager&&this.pluginManager.scriptsForUISourceCode(e).forEach((e=>o.add(e)));for(const t of this.#o.values()){const i=t.getResourceScriptMapping().scriptFile(e);i&&i.script&&o.add(i.script),t.compilerMapping.scriptsForUISourceCode(e).forEach((e=>o.add(e)))}return[...o]}supportsConditionalBreakpoints(e){if(!this.pluginManager)return!0;return this.pluginManager.scriptsForUISourceCode(e).every((e=>e.isJavaScript()))}globalObjectCleared(e){this.reset(e.data)}reset(e){const o=this.#o.get(e);if(o){for(const e of o.callFrameLocations.values())this.removeLiveLocation(e);o.callFrameLocations.clear()}}resetForTest(e){const o=e.model(SDK.DebuggerModel.DebuggerModel),t=this.#o.get(o);t&&t.getResourceScriptMapping().resetForTest()}registerCallFrameLiveLocation(e,o){const t=this.#o.get(e);if(t){t.callFrameLocations.add(o)}}removeLiveLocation(e){const o=this.#o.get(e.rawLocation.debuggerModel);o&&o.disposeLocation(e)}debuggerResumed(e){this.reset(e.data)}async shouldPause(e,o){const{callFrames:[t]}=e;if(!t)return!1;const i=t.functionLocation();if(!(o&&"step"===e.reason&&i&&this.pluginManager&&t.script.isWasm()&&Common.Settings.moduleSetting("wasmAutoStepping").get()&&this.pluginManager.hasPluginForScript(t.script)))return!0;return!!await this.pluginManager.rawLocationToUILocation(t.location())||(o.script()!==i.script()||o.columnNumber!==i.columnNumber||o.lineNumber!==i.lineNumber)}}class ModelData{#a;#r;callFrameLocations;#n;#s;#c;compilerMapping;#u;constructor(e,o){this.#a=e,this.#r=o,this.callFrameLocations=new Set;const{workspace:t}=o.resourceMapping;this.#n=new DefaultScriptMapping(e,t,o),this.#s=o.resourceMapping,this.#c=new ResourceScriptMapping(e,t,o),this.compilerMapping=new CompilerScriptMapping(e,t,o),this.#u=new Platform.MapUtilities.Multimap}async createLiveLocation(e,o,t){console.assert(""!==e.scriptId);const i=e.scriptId,a=new Location(i,e,this.#r,o,t);return this.#u.set(i,a),await a.update(),a}disposeLocation(e){this.#u.delete(e.scriptId,e)}async updateLocations(e){const o=[];for(const t of this.#u.get(e.scriptId))o.push(t.update());await Promise.all(o)}rawLocationToUILocation(e){let o=this.compilerMapping.rawLocationToUILocation(e);return o=o||this.#c.rawLocationToUILocation(e),o=o||this.#s.jsLocationToUILocation(e),o=o||this.#n.rawLocationToUILocation(e),o}uiSourceCodeForScript(e){let o=null;return o=o||this.#c.uiSourceCodeForScript(e),o=o||this.#s.uiSourceCodeForScript(e),o=o||this.#n.uiSourceCodeForScript(e),o}uiLocationToRawLocations(e,o,t=0){let i=this.compilerMapping.uiLocationToRawLocations(e,o,t);return i=i.length?i:this.#c.uiLocationToRawLocations(e,o,t),i=i.length?i:this.#s.uiLocationToJSLocations(e,o,t),i=i.length?i:this.#n.uiLocationToRawLocations(e,o,t),i}uiLocationRangeToRawLocationRanges(e,o){let t=this.compilerMapping.uiLocationRangeToRawLocationRanges(e,o);return t??=this.#c.uiLocationRangeToRawLocationRanges(e,o),t??=this.#s.uiLocationRangeToJSLocationRanges(e,o),t??=this.#n.uiLocationRangeToRawLocationRanges(e,o),t}getMappedLines(e){return this.compilerMapping.getMappedLines(e)}dispose(){this.#a.setBeforePausedCallback(null),this.compilerMapping.dispose(),this.#c.dispose(),this.#n.dispose()}getResourceScriptMapping(){return this.#c}}export class Location extends LiveLocationWithPool{scriptId;rawLocation;#g;constructor(e,o,t,i,a){super(i,a),this.scriptId=e,this.rawLocation=o,this.#g=t}async uiLocation(){const e=this.rawLocation;return this.#g.rawLocationToUILocation(e)}dispose(){super.dispose(),this.#g.removeLiveLocation(this)}async isIgnoreListed(){const e=await this.uiLocation();return!!e&&IgnoreListManager.instance().isUserOrSourceMapIgnoreListedUISourceCode(e.uiSourceCode)}}class StackTraceTopFrameLocation extends LiveLocationWithPool{#p;#l;#u;constructor(e,o){super(e,o),this.#p=!0,this.#l=null,this.#u=null}static async createStackTraceTopFrameLocation(e,o,t,i){const a=new StackTraceTopFrameLocation(t,i),r=e.map((e=>o.createLiveLocation(e,a.scheduleUpdate.bind(a),i)));return a.#u=(await Promise.all(r)).filter((e=>Boolean(e))),await a.updateLocation(),a}async uiLocation(){return this.#l?this.#l.uiLocation():null}async isIgnoreListed(){return!!this.#l&&this.#l.isIgnoreListed()}dispose(){if(super.dispose(),this.#u)for(const e of this.#u)e.dispose();this.#u=null,this.#l=null}async scheduleUpdate(){this.#p||(this.#p=!0,queueMicrotask((()=>{this.updateLocation()})))}async updateLocation(){if(this.#p=!1,this.#u&&0!==this.#u.length){this.#l=this.#u[0];for(const e of this.#u)if(!await e.isIgnoreListed()){this.#l=e;break}this.update()}}}