@yuebai008/cli
Version:
Command line interface for rapid qg-minigame development
1 lines • 2.42 kB
JavaScript
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 Formatter from"../../../../models/formatter/formatter.js";import*as SourceMapScopes from"../../../../models/source_map_scopes/source_map_scopes.js";import*as UI from"../../legacy.js";import{RemoteObjectPreviewFormatter}from"./RemoteObjectPreviewFormatter.js";export class JavaScriptREPL{static wrapObjectLiteral(e){const t=/^\s*\{\s*(.*)\s*\}[\s;]*$/.exec(e);if(null===t)return e;const[,r]=t;let o=0;for(const t of r)if("{"===t)o++;else if("}"===t&&--o<0)return e;const a=(async()=>0).constructor;try{a("return {"+r+"};");const e="({"+r+"})";return a(e),e}catch(t){return e}}static async evaluateAndBuildPreview(e,t,r,o,a,i,n=!1,s=!1){const c=UI.Context.Context.instance().flavor(SDK.RuntimeModel.ExecutionContext),l=e.length>maxLengthForEvaluation;if(!e||!c||t&&l)return{preview:document.createDocumentFragment(),result:null};let p=e;if(Root.Runtime.experiments.isEnabled("evaluateExpressionsWithSourceMaps")){const e=c.debuggerModel.selectedCallFrame();if(e){const t=await SourceMapScopes.NamesResolver.allVariablesInCallFrame(e);try{p=await Formatter.FormatterWorkerPool.formatterWorkerPool().javaScriptSubstitute(p,t)}catch{}}}p=JavaScriptREPL.wrapObjectLiteral(p);const m={expression:p,generatePreview:!0,includeCommandLineAPI:!0,throwOnSideEffect:t,timeout:o,objectGroup:i,disableBreaks:!0,replMode:r,silent:s},u=await c.evaluate(m,!1,n);return{preview:JavaScriptREPL.buildEvaluationPreview(u,a),result:u}}static buildEvaluationPreview(e,t){const r=document.createDocumentFragment();if("error"in e)return r;if(e.exceptionDetails&&e.exceptionDetails.exception&&e.exceptionDetails.exception.description){const o=e.exceptionDetails.exception.description;return(o.startsWith("TypeError: ")||t)&&(r.createChild("span").textContent=e.exceptionDetails.text+" "+o),r}const o=new RemoteObjectPreviewFormatter,{preview:a,type:i,subtype:n,className:s,description:c}=e.object;if(a&&"object"===i&&"node"!==n&&"trustedtype"!==n)o.appendObjectPreview(r,a,!1);else{const e=o.renderPropertyPreview(i,n,s,Platform.StringUtilities.trimEndWithMaxLength(c||"",400));r.appendChild(e)}return r}}let maxLengthForEvaluation=2e3;export function setMaxLengthForEvaluation(e){maxLengthForEvaluation=e}export function getMaxLengthForEvaluation(){return maxLengthForEvaluation}