different implementation of previous commit:
"this" should never be a reference but always be a copy so that changes
to the instance are not shown up on the this so also on the other
instances.
now a realy copy is only created when a new Xxx() is done. Then the
ThisValue of the function is copied instead of referenced, any thing
else it is still referenced.
diff --git a/plugins/org.eclipse.dltk.javascript.core/src/org/eclipse/dltk/internal/javascript/ti/ThisValue.java b/plugins/org.eclipse.dltk.javascript.core/src/org/eclipse/dltk/internal/javascript/ti/ThisValue.java
index 99914d7..c9f2d0c 100644
--- a/plugins/org.eclipse.dltk.javascript.core/src/org/eclipse/dltk/internal/javascript/ti/ThisValue.java
+++ b/plugins/org.eclipse.dltk.javascript.core/src/org/eclipse/dltk/internal/javascript/ti/ThisValue.java
@@ -28,11 +28,6 @@
}
@Override
- public boolean isReference() {
- return false;
- }
-
- @Override
public ReferenceKind getKind() {
return ReferenceKind.THIS;
}
diff --git a/plugins/org.eclipse.dltk.javascript.core/src/org/eclipse/dltk/internal/javascript/ti/TypeInferencerVisitor.java b/plugins/org.eclipse.dltk.javascript.core/src/org/eclipse/dltk/internal/javascript/ti/TypeInferencerVisitor.java
index d7a785e..9b50a69 100644
--- a/plugins/org.eclipse.dltk.javascript.core/src/org/eclipse/dltk/internal/javascript/ti/TypeInferencerVisitor.java
+++ b/plugins/org.eclipse.dltk.javascript.core/src/org/eclipse/dltk/internal/javascript/ti/TypeInferencerVisitor.java
@@ -1243,6 +1243,19 @@
return this;
return super.getChild(name);
}
+
+ @Override
+ public void setValue(IValueReference value) {
+ if (value instanceof ThisValue) {
+ // make sure a copy is created so that the this values of
+ // various instances are not shared over those instances.
+ IValue val = createValue();
+ if (val != null)
+ val.addValue(((ThisValue) value).getValue());
+ } else {
+ super.setValue(value);
+ }
+ }
}
public static class VisitNewResult {
diff --git a/tests/org.eclipse.dltk.javascript.core.tests/src/org/eclipse/dltk/javascript/core/tests/typeinference/TypeInferenceTests.java b/tests/org.eclipse.dltk.javascript.core.tests/src/org/eclipse/dltk/javascript/core/tests/typeinference/TypeInferenceTests.java
index 4a54506..f81295e 100644
--- a/tests/org.eclipse.dltk.javascript.core.tests/src/org/eclipse/dltk/javascript/core/tests/typeinference/TypeInferenceTests.java
+++ b/tests/org.eclipse.dltk.javascript.core.tests/src/org/eclipse/dltk/javascript/core/tests/typeinference/TypeInferenceTests.java
@@ -1318,7 +1318,6 @@
public void testBaseTypeWith2SubClasses() {
final StringList code = new StringList();
-
code.add("/** @constructor */");
code.add("function base() {");
code.add(" this.baseVar = 10;");
@@ -1341,4 +1340,17 @@
Set<String> directChildren = test.getDirectChildren();
assertEquals(2, directChildren.size());
}
+
+ public void test2InstancesOfTheSameFunctionAddingAfieldTo1() {
+ final StringList code = new StringList();
+ code.add("function cust(){}");
+ code.add("var x = new cust();");
+ code.add("x.test = 10;");
+ code.add("var y = new cust();");
+ final IValueCollection collection = inference(code.toString());
+ IValueReference test = collection.getChild("y");
+ Set<String> directChildren = test.getDirectChildren();
+ assertFalse(directChildren.contains("test"));
+ }
+
}
diff --git a/tests/org.eclipse.dltk.javascript.core.tests/src/org/eclipse/dltk/javascript/core/tests/validation/TypeInfoValidationTests.java b/tests/org.eclipse.dltk.javascript.core.tests/src/org/eclipse/dltk/javascript/core/tests/validation/TypeInfoValidationTests.java
index 8b93b8a..ebf9de2 100644
--- a/tests/org.eclipse.dltk.javascript.core.tests/src/org/eclipse/dltk/javascript/core/tests/validation/TypeInfoValidationTests.java
+++ b/tests/org.eclipse.dltk.javascript.core.tests/src/org/eclipse/dltk/javascript/core/tests/validation/TypeInfoValidationTests.java
@@ -3926,4 +3926,20 @@
final List<IProblem> problems = validate(code.toString());
assertEquals(problems.toString(), 1, problems.size());
}
+
+ public void testThisAssignedToVariable() {
+ final StringList code = new StringList();
+ code.add("/**");
+ code.add(" * @constructor ");
+ code.add(" */");
+ code.add("function cust() {");
+ code.add(" var self = this;");
+ code.add(" this.boo = 42;");
+ code.add(" this.func = function(){");
+ code.add(" self.boo.toFixed()");
+ code.add(" }");
+ code.add("}");
+ final List<IProblem> problems = validate(code.toString());
+ assertEquals(problems.toString(), 0, problems.size());
+ }
}