compile_and_update = (source, named_cell) => {
viewof log.value = "";
const cell = named_cell[1];
try {
let reposition = false,
insertionIndex = -1;
const newVariables = compile(source);
if (!cell || cell.length !== newVariables.length) {
reposition = true;
cell.forEach((v) => v.delete());
for (let i = 0; i < newVariables.length; i++) {
const newVariable = main.variable({});
cell.push(newVariable);
}
}
if (reposition) {
insertionIndex =
[...runtime._variables].findIndex(
(v) => v == mutable editor_state.last_selected_variable[1].at(-1)
) + 1;
}
newVariables.forEach((v, i) => {
const variable = cell[i];
let _fn;
eval("_fn = " + v._definition);
if (v._name) {
variable.define(v._name, v._inputs, _fn);
} else {
variable.define(v._inputs, _fn);
}
if (reposition)
repositionSetElement(runtime._variables, variable, insertionIndex + i);
});
mutable editor_state.last_cell = named_cell;
mutable editor_state.last_module = module.name;
console.log("compile_and_update: mutable editor_state", mutable editor_state);
viewof refresh.dispatchEvent(new Event("input"));
return named_cell;
} catch (e) {
viewof log.value = e.toString();
debugger;
}
}