UNPKG

@yuebai008/cli

Version:

Command line interface for rapid qg-minigame development

1 lines 10.4 kB
import*as SDK from"../../core/sdk/sdk.js";import*as Bindings from"../bindings/bindings.js";import*as Formatter from"../formatter/formatter.js";import*as TextUtils from"../text_utils/text_utils.js";import*as Platform from"../../core/platform/platform.js";import{ScopeTreeCache}from"./ScopeTreeCache.js";const scopeToCachedIdentifiersMap=new WeakMap,cachedMapByCallFrame=new WeakMap,cachedTextByDeferredContent=new WeakMap;async function getTextFor(e){const t=await e.requestContent();let n=cachedTextByDeferredContent.get(t);if(void 0===n){const{content:e}=t;n=e?new TextUtils.Text.Text(e):null,cachedTextByDeferredContent.set(t,n)}return n}export class IdentifierPositions{name;positions;constructor(e,t=[]){this.name=e,this.positions=t}addPosition(e,t){this.positions.push({lineNumber:e,columnNumber:t})}}const computeScopeTree=async function(e){if(!e.sourceMapURL)return null;const t=await getTextFor(e);if(!t)return null;const n=await ScopeTreeCache.instance().scopeTreeForScript(e);return n?{scopeTree:n,text:t}:null},findScopeChain=function(e,t){if(!i(e,t))return[];let n=e;const r=[e];for(;;){let e=!1;for(const a of n.children){if(i(a,t)){r.push(a),n=a,e=!0;break}if(!(o=t,s=a,o.end<=s.start||s.end<=o.start||i(t,a)))return console.error("Wrong nesting of scopes"),[]}if(!e)break}var o,s;return r;function i(e,t){return e.start<=t.start&&e.end>=t.end}};export async function findScopeChainForDebuggerScope(e){const t=e.range()?.start,n=e.range()?.end;if(!t||!n)return[];const r=t.script();if(!r)return[];const o=await computeScopeTree(r);if(!o)return[];const{scopeTree:s,text:i}=o,a={start:i.offsetFromPosition(t.lineNumber,t.columnNumber),end:i.offsetFromPosition(n.lineNumber,n.columnNumber)};return findScopeChain(s,a)}export const scopeIdentifiers=async function(e,t,n){const r=await getTextFor(e);if(!r)return null;const o=[],s=new TextUtils.TextCursor.TextCursor(r.lineEndings());for(const e of t.variables){if(3===e.kind&&e.offsets.length<=1)continue;const t=new IdentifierPositions(e.name);for(const n of e.offsets)s.resetTo(n),t.addPosition(s.lineNumber(),s.columnNumber());o.push(t)}const i=[];for(const e of n)for(const n of e.variables){let e=null;for(const r of n.offsets)r>=t.start&&r<t.end&&(e||(e=new IdentifierPositions(n.name)),s.resetTo(r),e.addPosition(s.lineNumber(),s.columnNumber()));e&&i.push(e)}return{boundVariables:o,freeVariables:i}};const identifierAndPunctuationRegExp=/^\s*([A-Za-z_$][A-Za-z_$0-9]*)\s*([.;,=]?)\s*$/,resolveDebuggerScope=async e=>{const t=e.callFrame().script,n=await findScopeChainForDebuggerScope(e);return resolveScope(t,n)},resolveScope=async(e,t)=>{const n=t[t.length-1];if(!n)return{variableMapping:new Map,thisMapping:null};let r=scopeToCachedIdentifiersMap.get(n);const o=e.debuggerModel.sourceMapManager().sourceMapForClient(e);if(!r||r.sourceMap!==o){const i=(async()=>{const r=new Map;let i=null;if(!o)return{variableMapping:r,thisMapping:i};const a=[],c=(t,n)=>{for(const e of t.positions){const t=o.findEntry(e.lineNumber,e.columnNumber);if(t&&t.name)return void n(t.name)}a.push(async function(){if(o)for(const r of t.positions){const i=await s(e,o,t.name,r);if(i)return void n(i)}}())},u=await scopeIdentifiers(e,n,t.slice(0,-1));if(!u)return{variableMapping:r,thisMapping:i};for(const e of u.boundVariables)c(e,(t=>{"this"!==t&&r.set(e.name,t)}));for(const e of u.freeVariables)c(e,(t=>{"this"===t&&(i=e.name)}));return await Promise.all(a).then(getScopeResolvedForTest()),{variableMapping:r,thisMapping:i}})();r={sourceMap:o,mappingPromise:i},scopeToCachedIdentifiersMap.set(n,{sourceMap:o,mappingPromise:i})}return await r.mappingPromise;async function s(e,t,n,r){const o=t.findEntryRanges(r.lineNumber,r.columnNumber);if(!o)return null;const s=Bindings.DebuggerWorkspaceBinding.DebuggerWorkspaceBinding.instance().uiSourceCodeForSourceMapSourceURL(e.debuggerModel,o.sourceURL,e.isContentScript());if(!s)return null;const i=await getTextFor(e);if(!i)return null;const a=d(i.extract(o.range));if(!a)return null;const{name:c,punctuation:u}=a;if(c!==n)return null;const l=await getTextFor(s);if(!l)return null;const p=d(l.extract(o.sourceRange));if(!p)return null;const{name:g,punctuation:f}=p;return u===f||"comma"===u&&"semicolon"===f?g:null;function d(e){const t=e.match(identifierAndPunctuationRegExp);if(!t)return null;const n=t[1];let r=null;switch(t[2]){case".":r="dot";break;case",":r="comma";break;case";":r="semicolon";break;case"=":r="equals";break;case"":r="none";break;default:return console.error(`Name token parsing error: unexpected token "${t[2]}"`),null}return{name:n,punctuation:r}}}};export const resolveScopeChain=async function(e){if(!e)return null;const{pluginManager:t}=Bindings.DebuggerWorkspaceBinding.DebuggerWorkspaceBinding.instance();if(t){const n=await t.resolveScopeChain(e);if(n)return n}return e.scopeChain()};export const allVariablesInCallFrame=async e=>{const t=cachedMapByCallFrame.get(e);if(t)return t;const n=e.scopeChain(),r=await Promise.all(n.map(resolveDebuggerScope)),o=new Map,s=new Set;for(const{variableMapping:e}of r)for(const[t,n]of e)if(n){if(!o.has(n)){const e=s.has(t)?null:t;o.set(n,e)}s.add(t)}return cachedMapByCallFrame.set(e,o),o};export const allVariablesAtPosition=async e=>{const t=new Map,n=e.script();if(!n)return t;const r=await computeScopeTree(n);if(!r)return t;const{scopeTree:o,text:s}=r,i=s.offsetFromPosition(e.lineNumber,e.columnNumber),a=findScopeChain(o,{start:i,end:i}),c=new Set;for(;a.length>0;){const{variableMapping:e}=await resolveScope(n,a);for(const[n,r]of e)if(r){if(!t.has(r)){const e=c.has(n)?null:n;t.set(r,e)}c.add(n)}a.pop()}return t};export const resolveExpression=async(e,t,n,r,o,s)=>{if("application/wasm"===n.mimeType())return`memories["${t}"] ?? locals["${t}"] ?? tables["${t}"] ?? functions["${t}"] ?? globals["${t}"]`;if(!n.contentType().isFromSourceMap())return"";const i=await allVariablesInCallFrame(e);if(i.has(t))return i.get(t);const a=(await Bindings.DebuggerWorkspaceBinding.DebuggerWorkspaceBinding.instance().uiLocationToRawLocations(n,r,o)).find((t=>t.debuggerModel===e.debuggerModel));if(!a)return"";const c=a.script();if(!c)return"";const u=c.debuggerModel.sourceMapManager().sourceMapForClient(c);if(!u)return"";const l=await getTextFor(c);if(!l)return"";const p=u.reverseMapTextRanges(n.url(),new TextUtils.TextRange.TextRange(r,o,r,s));if(1!==p.length)return"";const[g]=p,f=l.extract(g);if(!f)return"";const d=await getTextFor(n);if(!d)return"";const m=u.findEntryRanges(g.startLine,g.startColumn),b=0===g.endColumn?g.endLine-1:g.endLine,h=0===g.endColumn?l.lineEndings()[b]:g.endColumn-1,x=u.findEntryRanges(b,h);if(!m||!x)return"";const w=d.extract(new TextUtils.TextRange.TextRange(m.sourceRange.startLine,m.sourceRange.startColumn,x.sourceRange.endLine,x.sourceRange.endColumn));return new RegExp(`^[\\s,;]*${Platform.StringUtilities.escapeForRegExp(t)}`,"g").test(w)?await Formatter.FormatterWorkerPool.formatterWorkerPool().evaluatableJavaScriptSubstring(f):""};export const resolveThisObject=async e=>{if(!e)return null;const t=e.scopeChain();if(0===t.length)return e.thisObject();const{thisMapping:n}=await resolveDebuggerScope(t[0]);if(!n)return e.thisObject();const r=await e.evaluate({expression:n,objectGroup:"backtrace",includeCommandLineAPI:!1,silent:!0,returnByValue:!1,generatePreview:!0});return"exceptionDetails"in r?!r.exceptionDetails&&r.object?r.object:e.thisObject():null};export const resolveScopeInObject=function(e){const t=e.range()?.end,n=e.range()?.start.script()??null;return"global"!==e.type()&&n&&t&&n.sourceMapURL?new RemoteObject(e):e.object()};export class RemoteObject extends SDK.RemoteObject.RemoteObject{scope;object;constructor(e){super(),this.scope=e,this.object=e.object()}customPreview(){return this.object.customPreview()}get objectId(){return this.object.objectId}get type(){return this.object.type}get subtype(){return this.object.subtype}get value(){return this.object.value}get description(){return this.object.description}get hasChildren(){return this.object.hasChildren}get preview(){return this.object.preview}arrayLength(){return this.object.arrayLength()}getOwnProperties(e){return this.object.getOwnProperties(e)}async getAllProperties(e,t){const n=await this.object.getAllProperties(e,t),{variableMapping:r}=await resolveDebuggerScope(this.scope),o=n.properties,s=n.internalProperties,i=o?.map((e=>{const t=r.get(e.name);return void 0!==t?e.cloneWithNewName(t):e}));return{properties:i??[],internalProperties:s}}async setPropertyValue(e,t){const{variableMapping:n}=await resolveDebuggerScope(this.scope);let r;r="string"==typeof e?e:e.value;let o=r;for(const e of n.keys())if(n.get(e)===r){o=e;break}return this.object.setPropertyValue(o,t)}async deleteProperty(e){return this.object.deleteProperty(e)}callFunction(e,t){return this.object.callFunction(e,t)}callFunctionJSON(e,t){return this.object.callFunctionJSON(e,t)}release(){this.object.release()}debuggerModel(){return this.object.debuggerModel()}runtimeModel(){return this.object.runtimeModel()}isNode(){return this.object.isNode()}}async function getFunctionNameFromScopeStart(e,t,n){const r=e.debuggerModel.sourceMapManager().sourceMapForClient(e);if(!r)return null;const o=r.findEntry(t,n);if(!o||!o.sourceURL)return null;const s=r.findScopeEntry(o.sourceURL,o.sourceLineNumber,o.sourceColumnNumber)?.scopeName();if(s)return s;const i=o.name;if(!i)return null;const a=await getTextFor(e);if(!a)return null;const c=new TextUtils.TextRange.TextRange(t,n,t,n+1);return"("!==a.extract(c)?null:i}export async function resolveDebuggerFrameFunctionName(e){const t=e.localScope()?.range()?.start;return t?await getFunctionNameFromScopeStart(e.script,t.lineNumber,t.columnNumber):null}export async function resolveProfileFrameFunctionName({scriptId:e,lineNumber:t,columnNumber:n},r){if(!r||void 0===t||void 0===n||void 0===e)return null;const o=r.model(SDK.DebuggerModel.DebuggerModel),s=o?.scriptForId(String(e));if(!o||!s)return null;const i=Bindings.DebuggerWorkspaceBinding.DebuggerWorkspaceBinding.instance(),a=new SDK.DebuggerModel.Location(o,e,t,n),c=await(i.pluginManager?.getFunctionInfo(s,a));if(c&&"frames"in c){const e=c.frames.at(-1);if(e?.name)return e.name}return await getFunctionNameFromScopeStart(s,t,n)}let _scopeResolvedForTest=function(){};export const getScopeResolvedForTest=()=>_scopeResolvedForTest;export const setScopeResolvedForTest=e=>{_scopeResolvedForTest=e};