Skip to content

Commit

Permalink
remove decompiler block limit entirely
Browse files Browse the repository at this point in the history
  • Loading branch information
riknoll committed May 9, 2024
1 parent ef87e6a commit 7a193cb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 61 deletions.
56 changes: 12 additions & 44 deletions pxtcompiler/emitter/decompiler.ts
Expand Up @@ -48,7 +48,6 @@ namespace ts.pxtc.decompiler {

export type Comment = MultiLineComment | SingleLineComment;

export const FILE_TOO_LARGE_CODE = 9266;
export const DECOMPILER_ERROR = 9267;

interface DecompileModuleExport {
Expand All @@ -62,11 +61,6 @@ namespace ts.pxtc.decompiler {

const SK = ts.SyntaxKind;

/**
* Max number of blocks before we bail out of decompilation
*/
const MAX_BLOCKS = 6000;

const lowerCaseAlphabetStartCode = 97;
const lowerCaseAlphabetEndCode = 122;

Expand Down Expand Up @@ -511,31 +505,18 @@ namespace ts.pxtc.decompiler {
}
}
catch (e) {
if ((<any>e).programTooLarge) {
result.success = false;
result.diagnostics = pxtc.patchUpDiagnostics([{
file,
start: file.getFullStart(),
length: file.getFullWidth(),
messageText: e.message,
category: ts.DiagnosticCategory.Error,
code: FILE_TOO_LARGE_CODE
}]);
}
else {
// don't throw
pxt.reportException(e);
result.success = false;
result.diagnostics = pxtc.patchUpDiagnostics([{
file,
start: file.getFullStart(),
length: file.getFullWidth(),
messageText: e.message,
category: ts.DiagnosticCategory.Error,
code: DECOMPILER_ERROR
}]);
return result;
}
// don't throw
pxt.reportException(e);
result.success = false;
result.diagnostics = pxtc.patchUpDiagnostics([{
file,
start: file.getFullStart(),
length: file.getFullWidth(),
messageText: e.message,
category: ts.DiagnosticCategory.Error,
code: DECOMPILER_ERROR
}]);
return result;
}

if (n) {
Expand Down Expand Up @@ -653,15 +634,6 @@ ${output}</xml>`;
}
}

function countBlock() {
emittedBlocks++;
if (emittedBlocks > MAX_BLOCKS) {
let e = new Error(Util.lf("Could not decompile because the script is too large"));
(<any>e).programTooLarge = true;
throw e
}
}

// generated ids with the same entropy as blockly
function blocklyGenUid() {
const soup_ = '!#$%()*+,-./:;=?@[]^_`{|}~ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
Expand Down Expand Up @@ -854,9 +826,6 @@ ${output}</xml>`;
const node = n as ExpressionNode;
const isShadow = shadow || node.isShadow;
const tag = isShadow ? "shadow" : "block";
if (!isShadow) {
countBlock();
}

write(`<${tag} ${node.id ? `id="${node.id}" ` : ''}type="${U.htmlEscape(node.type)}">`)
emitBlockNodeCore(node);
Expand All @@ -865,7 +834,6 @@ ${output}</xml>`;
}

function openBlockTag(type: string, node: StatementNode) {
countBlock();
const id = node && node.id;
write(`<block ${id ? `id="${node.id}" ` : ''}type="${U.htmlEscape(type)}">`)
}
Expand Down
25 changes: 8 additions & 17 deletions webapp/src/monaco.tsx
Expand Up @@ -459,10 +459,10 @@ export class Editor extends toolboxeditor.ToolboxEditor {
tsFile = pxt.MAIN_TS;
}

const failedAsync = (file: string, programTooLarge = false) => {
const failedAsync = (file: string) => {
core.cancelAsyncLoading("switchtoblocks");
this.forceDiagnosticsUpdate();
return this.showBlockConversionFailedDialog(file, programTooLarge);
return this.showBlockConversionFailedDialog(file);
}

// might be undefined
Expand Down Expand Up @@ -519,9 +519,7 @@ export class Editor extends toolboxeditor.ToolboxEditor {
if (!resp.success) {
const failed = resp.failedResponse;
this.currFile.diagnostics = failed.diagnostics;
let tooLarge = false;
failed.diagnostics.forEach(d => tooLarge = (tooLarge || d.code === 9266 /* error code when script is too large */));
return failedAsync(blockFile, tooLarge);
return failedAsync(blockFile);
}
xml = resp.outText;
Util.assert(!!xml);
Expand All @@ -530,7 +528,7 @@ export class Editor extends toolboxeditor.ToolboxEditor {
})
}
else {
return failedAsync(blockFile, false)
return failedAsync(blockFile)
}

})
Expand All @@ -544,29 +542,22 @@ export class Editor extends toolboxeditor.ToolboxEditor {
return initPromise;
}

public showBlockConversionFailedDialog(blockFile: string, programTooLarge: boolean): Promise<void> {
public showBlockConversionFailedDialog(blockFile: string): Promise<void> {
const isPython = this.fileType == pxt.editor.FileType.Python;
const tickLang = isPython ? "python" : "typescript";

let bf = pkg.mainEditorPkg().files[blockFile];
if (programTooLarge) {
pxt.tickEvent(`${tickLang}.programTooLarge`);
}
let body: string;
let disagreeLbl: string;
if (isPython) {
body = programTooLarge ?
lf("Your program is too large to convert into blocks. You can keep working in Python or discard your changes and go back to the previous Blocks version.") :
lf("We are unable to convert your Python code back to blocks. You can keep working in Python or discard your changes and go back to the previous Blocks version.");
body = lf("We are unable to convert your Python code back to blocks. You can keep working in Python or discard your changes and go back to the previous Blocks version.");
disagreeLbl = lf("Stay in Python");
} else {
body = programTooLarge ?
lf("Your program is too large to convert into blocks. You can keep working in JavaScript or discard your changes and go back to the previous Blocks version.") :
lf("We are unable to convert your JavaScript code back to blocks. You can keep working in JavaScript or discard your changes and go back to the previous Blocks version.");
body = lf("We are unable to convert your JavaScript code back to blocks. You can keep working in JavaScript or discard your changes and go back to the previous Blocks version.");
disagreeLbl = lf("Stay in JavaScript");
}
return core.confirmAsync({
header: programTooLarge ? lf("Program too large") : lf("Oops, there is a problem converting your code."),
header: lf("Oops, there is a problem converting your code."),
body,
agreeLbl: lf("Discard and go to Blocks"),
agreeClass: "cancel",
Expand Down

0 comments on commit 7a193cb

Please sign in to comment.