Skip to content

Commit

Permalink
Fix variable creation and assignment race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephAbbey committed May 25, 2024
1 parent 8ea7e47 commit 842b0cf
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 3 deletions.
10 changes: 10 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"workbench.colorCustomizations": {
"statusBar.background": "#eba0ac",
"statusBar.foreground": "#181825",
"statusBarItem.hoverBackground": "#e27687",
"statusBarItem.remoteBackground": "#eba0ac",
"statusBarItem.remoteForeground": "#181825"
},
"peacock.color": "#eba0ac"
}
28 changes: 28 additions & 0 deletions examples/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"workbench.colorCustomizations": {
"statusBar.background": "#f5c2e7",
"statusBar.foreground": "#181825",
"statusBarItem.hoverBackground": "#ee96d6",
"statusBarItem.remoteBackground": "#f5c2e7",
"statusBarItem.remoteForeground": "#181825"
},
"peacock.color": "#f5c2e7",
"scratch-vhdl-vscode.templates": {
"led4_button4": {
"entity": {
"clk": ["in", "std_logic"],
"reset": ["in", "std_logic"],
"incr": ["in", "std_logic"],
"buttons": ["in", "std_logic_vector(3 downto 0)"],
"leds": ["out", "std_logic_vector(3 downto 0)"]
},
"signals": {},
"aliases": {},
"name": "led4_button4",
"constants": {
"button_tab_c": ["natural", "1"]
},
"libraries": { "ieee": {} }
}
}
}
96 changes: 95 additions & 1 deletion scratch-vhdl-vscode/src/webview/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
provideVSCodeDesignSystem().register(vsCodeButton());

async function updateEntity(data: { entity: Entity; filename: string }) {
const oldEntity = entity;
entity = data.entity;
filename = data.filename;

Expand Down Expand Up @@ -129,6 +130,95 @@ async function updateEntity(data: { entity: Entity; filename: string }) {
...categories,
];

if (oldEntity) {
const es = Object.keys(entity.entity);
const oes = Object.keys(oldEntity.entity);
for (const e in es) {
const v = ws.getVariable(es[e]);
if (!v) {
if (oes[e]) {
const ov = ws.getVariable(oes[e]);
if (ov) {
ws.renameVariableById(ov.getId(), es[e]);
} else {
ws.createVariable(es[e], null, null);
}
} else {
ws.createVariable(es[e], null, null);
}
}
}
const as = Object.keys(entity.aliases);
const oas = Object.keys(oldEntity.aliases);
for (const a in as) {
const v = ws.getVariable(as[a]);
if (!v) {
if (oas[a]) {
const ov = ws.getVariable(oas[a]);
if (ov) {
ws.renameVariableById(ov.getId(), as[a]);
} else {
ws.createVariable(as[a], null, null);
}
} else {
ws.createVariable(as[a], null, null);
}
}
}
const ss = Object.keys(entity.signals);
const oss = Object.keys(oldEntity.signals);
for (const s in ss) {
const v = ws.getVariable(ss[s]);
if (!v) {
if (oss[s]) {
const ov = ws.getVariable(oss[s]);
if (ov) {
ws.renameVariableById(ov.getId(), ss[s]);
} else {
ws.createVariable(ss[s], null, null);
}
} else {
ws.createVariable(ss[s], null, null);
}
}
}
} else {
const es = Object.keys(entity.entity);
for (const e in es) {
console.log(es[e]);
const v = ws.getVariable(es[e]);
console.log(v, !v);
if (!v) {
console.log(ws.createVariable(es[e], null, null));
}
}
const as = Object.keys(entity.aliases);
for (const a in as) {
const v = ws.getVariable(as[a]);
if (!v) {
ws.createVariable(as[a], null, null);
}
}
const ss = Object.keys(entity.signals);
for (const s in ss) {
const v = ws.getVariable(ss[s]);
if (!v) {
ws.createVariable(ss[s], null, null);
}
}
}
for (const v of ws.getAllVariables()) {
if (
!(
v.name in entity.entity ||
v.name in entity.aliases ||
v.name in entity.signals
)
) {
ws.deleteVariableById(v.getId());
}
}

ws.updateToolbox(tb);

ws.getAllBlocks(false).forEach((b) =>
Expand Down Expand Up @@ -368,7 +458,9 @@ export class FieldVar extends Blockly.FieldVariable {
static dropdownCreate(
this: Blockly.FieldVariable
): Blockly.MenuOption[] {
return super.dropdownCreate().slice(0, -2);
const o = super.dropdownCreate();
if (o.length > 2) return o.slice(0, -2);
else return o;
}

protected render_(): void {
Expand Down Expand Up @@ -423,3 +515,5 @@ ws.addChangeListener((e) => {
});

main();

setTimeout(() => updateEntity({ entity, filename }), 1000);
15 changes: 13 additions & 2 deletions scratch_vhdl.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,28 @@
"buttons": ["in", "std_logic_vector(3 downto 0)"],
"leds": ["out", "std_logic_vector(3 downto 0)"]
},
"signals": {},
"aliases": {},
"name": "led4_button4",
"constants": {
"button_tab_c": ["natural", "1"]
}
},
"libraries": { "ieee": {} }
}
},
"markdownlint.config": {
"MD033": false,
"MD024": false
},
"scratch-vhdl-vscode.asm_compile_path": "./design/asm_compile.cmd"
"scratch-vhdl-vscode.asm_compile_path": "./design/asm_compile.cmd",
"workbench.colorCustomizations": {
"statusBar.background": "#cba6f7",
"statusBar.foreground": "#181825",
"statusBarItem.hoverBackground": "#b077f3",
"statusBarItem.remoteBackground": "#cba6f7",
"statusBarItem.remoteForeground": "#181825"
},
"peacock.color": "#cba6f7"
},
"extensions": {
"recommendations": [
Expand Down

0 comments on commit 842b0cf

Please sign in to comment.