From 7b2f606316a4cc3f767da1272344ad8065487430 Mon Sep 17 00:00:00 2001 From: Paul Kaplan Date: Tue, 5 Jun 2018 10:45:51 -0400 Subject: [PATCH 1/3] Fix an issue where variable lookup would error if stage did not exist. --- src/engine/target.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/engine/target.js b/src/engine/target.js index 223254221..5e4d2e08a 100644 --- a/src/engine/target.js +++ b/src/engine/target.js @@ -154,7 +154,7 @@ class Target extends EventEmitter { // If the stage has a global copy, return it. if (this.runtime && !this.isStage) { const stage = this.runtime.getTargetForStage(); - if (stage.variables.hasOwnProperty(id)) { + if (stage && stage.variables.hasOwnProperty(id)) { return stage.variables[id]; } } From 3a697ae0acdf6fcbcc0ea19805e14f2ecc712ec7 Mon Sep 17 00:00:00 2001 From: Paul Kaplan Date: Tue, 5 Jun 2018 10:46:06 -0400 Subject: [PATCH 2/3] Add top-level API for getting and setting variable values --- src/virtual-machine.js | 36 +++++++++++++++++++++++++++++ test/unit/virtual-machine.js | 44 ++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/src/virtual-machine.js b/src/virtual-machine.js index ca6d8d173..224d95e4d 100644 --- a/src/virtual-machine.js +++ b/src/virtual-machine.js @@ -1071,6 +1071,42 @@ class VirtualMachine extends EventEmitter { this.editingTarget.postSpriteInfo(data); } } + + /** + * Set a target variables value if it exists. Return whether it succeeded. + * @param {!string} targetId ID of the target which owns the variable. + * @param {!string} variableId ID of the variable to set. + * @param {!*} value The new value of that variable. + * @returns {boolean} whether the target and variable were found and updated. + */ + setVariableValue (targetId, variableId, value) { + const target = this.runtime.getTargetById(targetId); + if (target) { + const variable = target.lookupVariableById(variableId); + if (variable) { + variable.value = value; + return true; + } + } + return false; + } + + /** + * Set a target variables value if it exists. Return whether it succeeded. + * @param {!string} targetId ID of the target which owns the variable. + * @param {!string} variableId ID of the variable to set. + * @returns {?*} The value of the variable, or null if it could not be looked up. + */ + getVariableValue (targetId, variableId) { + const target = this.runtime.getTargetById(targetId); + if (target) { + const variable = target.lookupVariableById(variableId); + if (variable) { + return variable.value; + } + } + return null; + } } module.exports = VirtualMachine; diff --git a/test/unit/virtual-machine.js b/test/unit/virtual-machine.js index 70d06fee3..f86267ae8 100644 --- a/test/unit/virtual-machine.js +++ b/test/unit/virtual-machine.js @@ -1,6 +1,7 @@ const test = require('tap').test; const VirtualMachine = require('../../src/virtual-machine.js'); const Sprite = require('../../src/sprites/sprite.js'); +const Variable = require('../../src/engine/variable.js'); test('addSprite throws on invalid string', t => { const vm = new VirtualMachine(); @@ -452,3 +453,46 @@ test('select original after dragging clone', t => { t.equal(newEditingTargetId, 'sprite1_original'); t.end(); }); + +test('setVariableValue', t => { + const vm = new VirtualMachine(); + const spr = new Sprite(null, vm.runtime); + const target = spr.createClone(); + target.createVariable('a-variable', 'a-name', Variable.SCALAR_TYPE); + + vm.runtime.targets = [target]; + + // Returns false if there is no variable to set + t.equal(vm.setVariableValue(target.id, 'not-a-variable', 100), false); + + // Returns false if there is no target with that id + t.equal(vm.setVariableValue('not-a-target', 'a-variable', 100), false); + + // Returns true and updates the value if variable is present + t.equal(vm.setVariableValue(target.id, 'a-variable', 100), true); + t.equal(target.lookupVariableById('a-variable').value, 100); + + t.end(); +}); + +test('getVariableValue', t => { + const vm = new VirtualMachine(); + const spr = new Sprite(null, vm.runtime); + const target = spr.createClone(); + target.createVariable('a-variable', 'a-name', Variable.SCALAR_TYPE); + + vm.runtime.targets = [target]; + + // Returns null if there is no variable with that id + t.equal(vm.getVariableValue(target.id, 'not-a-variable'), null); + + // Returns null if there is no variable with that id + t.equal(vm.getVariableValue('not-a-target', 'a-variable'), null); + + // Returns true and updates the value if variable is present + t.equal(vm.getVariableValue(target.id, 'a-variable'), 0); + vm.setVariableValue(target.id, 'a-variable', 'string'); + t.equal(vm.getVariableValue(target.id, 'a-variable'), 'string'); + + t.end(); +}); From a11d1be3fce91170f5e7441865b189a5e8334b86 Mon Sep 17 00:00:00 2001 From: Paul Kaplan Date: Tue, 5 Jun 2018 16:08:14 -0400 Subject: [PATCH 3/3] Fix comments --- src/virtual-machine.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/virtual-machine.js b/src/virtual-machine.js index 224d95e4d..1c9c2883d 100644 --- a/src/virtual-machine.js +++ b/src/virtual-machine.js @@ -1073,7 +1073,7 @@ class VirtualMachine extends EventEmitter { } /** - * Set a target variables value if it exists. Return whether it succeeded. + * Set a target's variable's value. Return whether it succeeded. * @param {!string} targetId ID of the target which owns the variable. * @param {!string} variableId ID of the variable to set. * @param {!*} value The new value of that variable. @@ -1092,7 +1092,7 @@ class VirtualMachine extends EventEmitter { } /** - * Set a target variables value if it exists. Return whether it succeeded. + * Get a target's variable's value. Return null if the target or variable does not exist. * @param {!string} targetId ID of the target which owns the variable. * @param {!string} variableId ID of the variable to set. * @returns {?*} The value of the variable, or null if it could not be looked up.