Refactor variables handling

introducing Variable objects, all functionality stays the same at this
time
pull/3/merge
jmoenig 2014-09-17 14:40:39 +02:00
rodzic 8f7d1833f6
commit 04168220bd
4 zmienionych plików z 37 dodań i 30 usunięć

Wyświetl plik

@ -2262,3 +2262,7 @@ ______
------
* Threads, Blocks: enable Zombiefication of JS-Functions
* Morphic: Fix #563 (Paste into Chrome), thanks, @Muon, for the hint!
140917
------
* Threads, Objects, Store: Refactor variables handling, introducing Variable objects, all functionality stays the same

Wyświetl plik

@ -125,7 +125,7 @@ PrototypeHatBlockMorph*/
// Global stuff ////////////////////////////////////////////////////////
modules.objects = '2014-July-30';
modules.objects = '2014-September-17';
var SpriteMorph;
var StageMorph;
@ -6735,7 +6735,8 @@ WatcherMorph.prototype.update = function () {
if (this.target && this.getter) {
this.updateLabel();
if (this.target instanceof VariableFrame) {
newValue = this.target.vars[this.getter];
newValue = this.target.vars[this.getter] ?
this.target.vars[this.getter].value : undefined;
} else {
newValue = this.target[this.getter]();
}
@ -6820,7 +6821,7 @@ WatcherMorph.prototype.fixLayout = function () {
this.sliderMorph.button.pressColor.b += 100;
this.sliderMorph.setHeight(fontSize);
this.sliderMorph.action = function (num) {
myself.target.vars[myself.getter] = Math.round(num);
myself.target.vars[myself.getter].value = Math.round(num);
};
this.add(this.sliderMorph);
}

Wyświetl plik

@ -57,11 +57,11 @@ newCanvas, Costume, Sound, Audio, IDE_Morph, ScriptsMorph, BlockMorph,
ArgMorph, InputSlotMorph, TemplateSlotMorph, CommandSlotMorph,
FunctionSlotMorph, MultiArgMorph, ColorSlotMorph, nop, CommentMorph, isNil,
localize, sizeOf, ArgLabelMorph, SVG_Costume, MorphicPreferences,
SyntaxElementMorph*/
SyntaxElementMorph, Variable*/
// Global stuff ////////////////////////////////////////////////////////
modules.store = '2014-July-29';
modules.store = '2014-September-17';
// XML_Serializer ///////////////////////////////////////////////////////
@ -730,8 +730,8 @@ SnapSerializer.prototype.loadVariables = function (varFrame, element) {
return;
}
value = child.children[0];
varFrame.vars[child.attributes.name] = value ?
myself.loadValue(value) : 0;
varFrame.vars[child.attributes.name] = new Variable(value ?
myself.loadValue(value) : 0);
});
};
@ -1509,7 +1509,7 @@ Sound.prototype.toXML = function (serializer) {
VariableFrame.prototype.toXML = function (serializer) {
var myself = this;
return Object.keys(this.vars).reduce(function (vars, v) {
var val = myself.vars[v],
var val = myself.vars[v].value,
dta;
if (val === undefined || val === null) {
dta = serializer.format('<variable name="@"/>', v);

Wyświetl plik

@ -83,7 +83,7 @@ ArgLabelMorph, localize, XML_Element, hex_sha512*/
// Global stuff ////////////////////////////////////////////////////////
modules.threads = '2014-August-13';
modules.threads = '2014-September-17';
var ThreadManager;
var Process;
@ -2982,6 +2982,20 @@ Context.prototype.stackSize = function () {
return 1 + this.parentContext.stackSize();
};
// Variable /////////////////////////////////////////////////////////////////
function Variable(value) {
this.value = value;
}
Variable.prototype.toString = function () {
return 'a Variable [' + this.value + ']';
};
Variable.prototype.copy = function () {
return new Variable(this.value);
};
// VariableFrame ///////////////////////////////////////////////////////
function VariableFrame(parentFrame, owner) {
@ -3049,7 +3063,7 @@ VariableFrame.prototype.setVar = function (name, value) {
*/
var frame = this.find(name);
if (frame) {
frame.vars[name] = value;
frame.vars[name].value = value;
}
};
@ -3063,11 +3077,11 @@ VariableFrame.prototype.changeVar = function (name, delta) {
var frame = this.find(name),
value;
if (frame) {
value = parseFloat(frame.vars[name]);
value = parseFloat(frame.vars[name].value);
if (isNaN(value)) {
frame.vars[name] = delta;
frame.vars[name].value = delta;
} else {
frame.vars[name] = value + parseFloat(delta);
frame.vars[name].value = value + parseFloat(delta);
}
}
};
@ -3077,7 +3091,7 @@ VariableFrame.prototype.getVar = function (name, upvars) {
value,
upvarReference;
if (frame) {
value = frame.vars[name];
value = frame.vars[name].value;
return (value === 0 ? 0
: value === false ? false
: value === '' ? ''
@ -3101,7 +3115,7 @@ VariableFrame.prototype.getVar = function (name, upvars) {
};
VariableFrame.prototype.addVar = function (name, value) {
this.vars[name] = (value === 0 ? 0
this.vars[name] = new Variable(value === 0 ? 0
: value === false ? false
: value === '' ? '' : value || 0);
};
@ -3159,23 +3173,11 @@ VariableFrame.prototype.allNames = function () {
return answer;
};
// Variable /////////////////////////////////////////////////////////////////
function Variable(value) {
this.value = value;
}
Variable.prototype.toString = function () {
return 'a Variable [' + this.value + ']';
};
Variable.prototype.copy = function () {
return new Variable(this.value);
};
// UpvarReference ///////////////////////////////////////////////////////////
// ... quasi-inherits some features from VariableFrame
// current variable refactoring efforts will make the upvar reference
// mechanism obsolete
function UpvarReference(parent) {
this.vars = {}; // structure: {upvarName : [varName, varFrame]}
@ -3207,7 +3209,7 @@ UpvarReference.prototype.find = function (name) {
UpvarReference.prototype.getVar = function (name) {
var varName = this.vars[name][0],
varFrame = this.vars[name][1],
value = varFrame.vars[varName];
value = varFrame.vars[varName].value;
return (value === 0 ? 0 : value || 0); // don't return null
};