Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/variable.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ function variable_detach(variable) {
variable._outputs.delete(this);
}

// If the given implicit variable is no longer referenced, delete it. Implicit
// variables in the runtime’s builtin module are never deleted.
function variable_prune(variable) {
const module = variable._module;
if (variable._type === TYPE_IMPLICIT
&& !variable._outputs.size
&& module !== module._runtime._builtin
&& module._scope.get(variable._name) === variable) {
variable_delete.call(variable);
}
}

export function variable_undefined() {
throw variable_undefined;
}
Expand Down Expand Up @@ -113,8 +125,10 @@ function variable_resolve(name) {
function variable_defineImpl(name, inputs, definition) {
const scope = this._module._scope, runtime = this._module._runtime;

this._inputs.forEach(variable_detach, this);
const previousInputs = this._inputs;
previousInputs.forEach(variable_detach, this);
inputs.forEach(variable_attach, this);
previousInputs.forEach(variable_prune); // after attach, in case inputs are reused
this._inputs = inputs;
this._definition = definition;
this._value = undefined;
Expand Down
60 changes: 60 additions & 0 deletions test/variable/delete-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,63 @@ it("variable.delete allows a variable to be deleted", async () => {
assert.deepStrictEqual(await valueof(foo), {value: undefined});
assert.deepStrictEqual(await valueof(bar), {error: "RuntimeError: foo is not defined"});
});

it("variable.delete removes an implicit variable that is no longer referenced", async () => {
const runtime = new Runtime();
const main = runtime.module();
const foo = main.variable(true).define("foo", ["bar"], bar => bar);
const baz = main.variable(true).define("baz", ["bar"], bar => bar);
const bar = main._scope.get("bar");
assert.deepStrictEqual(await valueof(foo), {error: "RuntimeError: bar is not defined"});
foo.delete();
await runtime._compute();
assert.strictEqual(main._scope.get("bar"), bar);
baz.delete();
await runtime._compute();
assert.strictEqual(main._scope.has("bar"), false);
assert.strictEqual(runtime._variables.has(bar), false);
});

it("variable.delete removes implicit variables that are transitively no longer referenced", async () => {
const runtime = new Runtime();
const main = runtime.module();
main.builtin("bar", ["baz"], baz => baz);
const foo = main.variable(true).define("foo", ["bar"], bar => bar);
const bar = main._scope.get("bar");
const baz = main._scope.get("baz");
assert.deepStrictEqual(await valueof(foo), {error: "RuntimeError: baz is not defined"});
foo.delete();
await runtime._compute();
assert.strictEqual(main._scope.has("bar"), false);
assert.strictEqual(main._scope.has("baz"), false);
assert.strictEqual(runtime._variables.has(bar), false);
assert.strictEqual(runtime._variables.has(baz), false);
});

it("variable.define does not remove an implicit variable that is still referenced", async () => {
const runtime = new Runtime();
const main = runtime.module();
const foo = main.variable(true).define("foo", ["bar"], bar => bar);
const bar = main._scope.get("bar");
foo.define("foo", ["bar"], bar => bar + 1);
await runtime._compute();
assert.strictEqual(main._scope.get("bar"), bar);
main.variable(true).define("bar", [], () => 1);
assert.deepStrictEqual(await valueof(foo), {value: 2});
});

it("variable.delete does not remove an unreferenced runtime builtin", async () => {
const runtime = new Runtime({bar: 42});
const main = runtime.module();
const builtin = runtime._builtin._scope.get("bar");
const foo = main.variable(true).define("foo", ["bar"], bar => bar);
const bar = main._scope.get("bar");
assert.deepStrictEqual(await valueof(foo), {value: 42});
foo.delete();
await runtime._compute();
assert.strictEqual(main._scope.has("bar"), false);
assert.strictEqual(builtin._outputs.has(bar), false);
assert.strictEqual(runtime._builtin._scope.get("bar"), builtin);
const baz = main.variable(true).define("baz", ["bar"], bar => bar + 1);
assert.deepStrictEqual(await valueof(baz), {value: 43});
});