diff --git a/HISTORY.md b/HISTORY.md index 5ce7214e..6a49a059 100755 --- a/HISTORY.md +++ b/HISTORY.md @@ -56,6 +56,7 @@ * threads: made block attribute "type" case-insensitive for textual mnemonics * threads: allow variadic slot type declaration using ellipses after type numbers * threads: allow case-insensitive text for custom block scope specification +* threads: update an existing global definition matching DEFINE's label ### 2022-06-23 * objects, threads: turned DEFINE into a command block with an upvar, experimental diff --git a/src/threads.js b/src/threads.js index 55c4759a..fe2ee023 100644 --- a/src/threads.js +++ b/src/threads.js @@ -5990,7 +5990,7 @@ Process.prototype.doDefineBlock = function (upvar, label, context) { vars = this.context.outerContext.variables, type = this.reportTypeOf(context), count = 1, - spec, def; + matches, spec, def; this.assertType(label, 'text'); if (label === '') {return ''; } @@ -6002,6 +6002,29 @@ Process.prototype.doDefineBlock = function (upvar, label, context) { this.compileBlockReferences(context, upvar); } + // identify global custom block matching the specified label + matches = ide.stage.globalBlocks.filter(def => + def.abstractBlockSpec() === label + ); + if (matches.length > 1) { + throw new Error( + 'several block definitions already match this label' + ); + } else if (matches.length === 1) { + // update the existing global definition with the context body + def = matches[0]; + this.doSetBlockAttribute( + 'definition', + def.blockInstance().reify(), + context + ); + + // create the reference to the new block + vars.addVar(upvar); + vars.setVar(upvar, def.blockInstance().reify()); + return; + } + // make a new custom block definition def = new CustomBlockDefinition('BYOB'); // haha! def.type = type;