Skip to content

Commit d9bac1c

Browse files
committed
fix def lookup
1 parent 22a1a0f commit d9bac1c

File tree

4 files changed

+56
-15
lines changed

4 files changed

+56
-15
lines changed

pxtblocks/blocksProgram.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export interface BlocksProgram {
6363
getVariableQualifiedName(varName: string, workspace: Blockly.Workspace): string;
6464
refreshSymbols?(): void;
6565
getVariableSymbol(varId: string): BlocksVariableSymbol;
66+
getFunctionSymbol(functionName: string): BlocksFunctionSymbol;
6667
}
6768

6869

@@ -136,6 +137,11 @@ export class SingleWorkspaceBlocksProgram implements BlocksProgram {
136137
}
137138
return null;
138139
}
140+
141+
getFunctionSymbol(functionName: string): BlocksFunctionSymbol {
142+
const allFunctions = getFunctionSymbols({ fileName: "main.blocks", workspace: this.workspace });
143+
return allFunctions.find(f => f.name === functionName);
144+
}
139145
}
140146

141147

@@ -332,6 +338,17 @@ export class MultiWorkspaceBlocksProgram implements BlocksProgram {
332338
return null;
333339
}
334340

341+
getFunctionSymbol(functionName: string): BlocksFunctionSymbol {
342+
for (const symbols of this.symbolsCache.values()) {
343+
for (const symbol of symbols) {
344+
if (symbol.type === "function" && symbol.name === functionName) {
345+
return symbol;
346+
}
347+
}
348+
}
349+
return null;
350+
}
351+
335352
protected defineSymbolInWorkspace(symbol: BlocksSymbol, workspace: Blockly.Workspace) {
336353
const map = workspace.getVariableMap();
337354

@@ -463,7 +480,7 @@ function getSymbolFromFunctionDefinitionBlock(block: FunctionDefinitionBlock, fi
463480
functionSymbol.arguments.push({
464481
name: child.getAttribute("name"),
465482
type: child.getAttribute("type"),
466-
id: child.getAttribute("argid")
483+
id: child.getAttribute("id")
467484
});
468485
}
469486
}

pxtblocks/plugins/functions/blocks/functionCallBlocks.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
FUNCTION_CALL_OUTPUT_BLOCK_TYPE,
66
FUNCTION_DEFINITION_BLOCK_TYPE,
77
} from "../constants";
8-
import { getArgMap, getDefinition, getShadowBlockInfoFromType_, isVariableBlockType, mutateCallersAndDefinition, mutationsAreEqual } from "../utils";
8+
import { getArgMap, getDefinition, getShadowBlockInfoFromType_, isVariableBlockType, lookupImportedFunctionDef, mutateCallersAndDefinition, mutationsAreEqual } from "../utils";
99
import { MsgKey } from "../msg";
1010
import { BlocksFunctionSymbol, generateFunctionMutationFromSymbol } from "../../../blocksProgram";
1111

@@ -167,10 +167,10 @@ const FUNCTION_CALL_MIXIN: FunctionCallMixin = {
167167
// Propagate the functionId of the definition to the caller
168168
this.functionId_ = def.functionId_;
169169
} else {
170-
const importedDef = this.workspace.getVariableMap().getVariableById(this.functionId_);
170+
const importedFunction = lookupImportedFunctionDef(name, this.workspace, this.functionId_);
171171

172-
if (importedDef) {
173-
const mutation = generateFunctionMutationFromSymbol(JSON.parse(importedDef.getName()) as BlocksFunctionSymbol);
172+
if (importedFunction) {
173+
const mutation = generateFunctionMutationFromSymbol(importedFunction);
174174

175175
if (!mutationsAreEqual(this.mutationToDom(), mutation)) {
176176
mutateCallersAndDefinition(this.getName(), this.workspace, this.mutationToDom(), mutation);
@@ -200,9 +200,9 @@ const FUNCTION_CALL_MIXIN: FunctionCallMixin = {
200200
// this caller.
201201
const name = this.getName();
202202
const def = getDefinition(name, this.workspace);
203-
const importedDef = !def && this.workspace.getVariableMap().getVariableById(this.functionId_);
203+
const importedFunction = !def && lookupImportedFunctionDef(name, this.workspace, this.functionId_);
204204

205-
if (!def && !importedDef) {
205+
if (!def && !importedFunction) {
206206
Blockly.Events.setGroup(event.group);
207207
this.dispose(true);
208208
Blockly.Events.setGroup(false);

pxtblocks/plugins/functions/commonFunctionMixin.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import {
55
FUNCTION_DECLARATION_BLOCK_TYPE,
66
FUNCTION_DEFINITION_BLOCK_TYPE,
77
} from "./constants";
8-
import { getDefinition, idsInUse, isCustomType, StringMap } from "./utils";
8+
import { getDefinition, idsInUse, isCustomType, lookupImportedFunctionDef, StringMap } from "./utils";
99
import { MsgKey } from "./msg";
1010
import { BlocksFunctionSymbol } from "../../blocksProgram";
11+
import { external } from "../..";
1112

1213
type CommonFunctionMixinType = typeof COMMON_FUNCTION_MIXIN;
1314

@@ -288,17 +289,18 @@ export const COMMON_FUNCTION_MIXIN = {
288289
}
289290
}
290291
else {
291-
const importedDef = this.workspace.getVariableMap().getVariableById(this.functionId_);
292-
if (importedDef) {
293-
const importedDefArgs = JSON.parse(importedDef.getName()) as BlocksFunctionSymbol;
292+
const importedFunction = lookupImportedFunctionDef(this.name_, this.workspace, this.functionId_);
293+
if (importedFunction) {
294294
for (let i = 0; i < this.arguments_.length; ++i) {
295-
for (let j = 0; j < importedDefArgs.arguments.length; ++j) {
296-
if (importedDefArgs.arguments[j].name == this.arguments_[i].name) {
297-
this.arguments_[i].id = importedDefArgs.arguments[j].id;
295+
for (let j = 0; j < importedFunction.arguments.length; ++j) {
296+
if (importedFunction.arguments[j].name == this.arguments_[i].name) {
297+
this.arguments_[i].id = importedFunction.arguments[j].id;
298298
break;
299299
}
300300
}
301301
}
302+
303+
this.functionId_ = importedFunction.id;
302304
}
303305
}
304306
break;

pxtblocks/plugins/functions/utils.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { ArgumentReporterBlock } from "./blocks/argumentReporterBlocks";
2121
import { DRAGGABLE_PARAM_INPUT_PREFIX } from "../../loader";
2222
import { getGlobalProgram } from "../../external";
2323
import { createFlyoutGroupLabel } from "../../toolbox";
24+
import { BlocksFunctionSymbol, IMPORTED_FUNCTION_TYPE } from "../../blocksProgram";
2425

2526

2627
export type StringMap<T> = { [index: string]: T };
@@ -231,7 +232,7 @@ export function mutateCallersAndDefinition(name: string, ws: Blockly.Workspace,
231232
} as CallerChange))
232233

233234
const change = new MutateFunctionEvent(
234-
definitionBlock.id,
235+
definitionBlock?.id || newMutation.getAttribute("functionid"),
235236
callerChanges,
236237
Blockly.Xml.domToText(oldMutation),
237238
Blockly.Xml.domToText(newMutation),
@@ -595,6 +596,27 @@ export function isVariableBlockType(type: string) {
595596
return false;
596597
}
597598

599+
export function lookupImportedFunctionDef(name: string, workspace: Blockly.Workspace, functionId?: string) {
600+
const program = getGlobalProgram();
601+
const symbol = program?.getFunctionSymbol(name);
602+
603+
let varModel: Blockly.IVariableModel<Blockly.IVariableState> | undefined;
604+
605+
if (symbol) {
606+
varModel = workspace.getVariableMap().getVariableById(symbol.id);
607+
}
608+
if (!varModel && functionId) {
609+
varModel = workspace.getVariableMap().getVariableById(functionId);
610+
}
611+
612+
if (varModel?.getType() === IMPORTED_FUNCTION_TYPE) {
613+
if (symbol) return symbol;
614+
return JSON.parse(varModel.getName()) as BlocksFunctionSymbol;
615+
}
616+
617+
return undefined;
618+
}
619+
598620
interface DescendantChange {
599621
id: string;
600622
type: string;

0 commit comments

Comments
 (0)