Skip to content

Commit 1e9430a

Browse files
committed
Fix instance allocation
1 parent b195cf4 commit 1e9430a

File tree

7 files changed

+27
-30
lines changed

7 files changed

+27
-30
lines changed

snap-compile/src/test/java/org/snapscript/compile/StaticConstructorTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class StaticConstructorTest extends TestCase {
1919
" }\n"+
2020
"}\n"+
2121
"var x = new X();\n"+
22+
"assert x.pattern == '.*';\n"+
2223
"println(x.pattern);\n";
2324

2425
public void testThis() throws Exception {

snap-core/src/main/java/org/snapscript/core/define/PrimitiveInstance.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public PrimitiveInstance(Module module, Model model, Scope scope, Type type) {
2323

2424
@Override
2525
public Instance getInner() {
26-
return new ObjectInstance(module, model, this, type);
26+
return new CompoundInstance(module, model, this, this, type);
2727
}
2828

2929
@Override

snap-tree/src/main/java/org/snapscript/tree/define/AnyDefinition.java

+8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import static org.snapscript.core.Reserved.METHOD_TO_STRING;
1010
import static org.snapscript.core.Reserved.METHOD_WAIT;
1111
import static org.snapscript.core.Reserved.TYPE_CONSTRUCTOR;
12+
import static org.snapscript.core.Reserved.TYPE_THIS;
1213

1314
import java.util.List;
1415

@@ -17,8 +18,11 @@
1718
import org.snapscript.core.Result;
1819
import org.snapscript.core.ResultType;
1920
import org.snapscript.core.Scope;
21+
import org.snapscript.core.State;
2022
import org.snapscript.core.Type;
2123
import org.snapscript.core.TypeLoader;
24+
import org.snapscript.core.Value;
25+
import org.snapscript.core.ValueType;
2226
import org.snapscript.core.define.Instance;
2327
import org.snapscript.core.function.Function;
2428
import org.snapscript.core.function.Invocation;
@@ -72,6 +76,10 @@ public NewInvocation() {
7276
public Result invoke(Scope scope, Object object, Object... list) throws Exception {
7377
Type real = (Type)list[0];
7478
Instance instance = builder.create(scope, real);
79+
State state = instance.getState();
80+
Value value = ValueType.getReference(object, real); // this needs to be a blank
81+
82+
state.addValue(TYPE_THIS, value); // reference to 'this'
7583

7684
return ResultType.getNormal(instance);
7785
}

snap-tree/src/main/java/org/snapscript/tree/define/InstanceAllocator.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import org.snapscript.core.State;
77
import org.snapscript.core.Type;
88
import org.snapscript.core.Value;
9-
import org.snapscript.core.ValueType;
109
import org.snapscript.core.define.Initializer;
1110
import org.snapscript.core.define.Instance;
1211
import org.snapscript.core.function.Invocation;
@@ -28,10 +27,12 @@ public Instance allocate(Scope scope, Instance base, Object... list) throws Exce
2827
Type real = (Type)list[0];
2928
Instance object = builder.create(scope, base, real);// we need to pass the base type up!!
3029
State state = object.getState();
31-
Value constant = ValueType.getReference(object, real); // this needs to be a blank
32-
33-
state.addValue(TYPE_THIS, constant); // reference to 'this'
34-
initializer.execute(object, real);
30+
Value value = state.getValue(TYPE_THIS);
31+
32+
if(object != base) { // false if this(...) called
33+
initializer.execute(object, real);
34+
}
35+
value.setValue(object); // set the 'this' variable
3536
invocation.invoke(object, object, list);
3637

3738
return object;

snap-tree/src/main/java/org/snapscript/tree/define/NewInvocation.java

-18
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
package org.snapscript.tree.define;
22

3-
import static org.snapscript.core.Reserved.TYPE_THIS;
4-
53
import java.util.concurrent.atomic.AtomicBoolean;
64

75
import org.snapscript.core.Result;
86
import org.snapscript.core.ResultType;
97
import org.snapscript.core.Scope;
10-
import org.snapscript.core.State;
118
import org.snapscript.core.Type;
12-
import org.snapscript.core.Value;
139
import org.snapscript.core.define.Initializer;
1410
import org.snapscript.core.define.Instance;
1511
import org.snapscript.core.function.Invocation;
@@ -43,20 +39,6 @@ public Result invoke(Scope scope, Instance base, Object... list) throws Exceptio
4339
initializer.compile(scope, type); // static stuff if needed
4440
}
4541
Instance result = allocator.allocate(scope, inner, list);
46-
47-
if(real == type){
48-
Instance next = result;
49-
50-
while(next != null){
51-
State state = next.getState();
52-
Value value = state.getValue(TYPE_THIS);
53-
54-
if(value != null){
55-
value.setValue(result);
56-
}
57-
next = next.getSuper();
58-
}
59-
}
6042
return ResultType.getNormal(result);
6143
}
6244
}

snap-tree/src/main/java/org/snapscript/tree/define/ObjectInstanceBuilder.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@ public ObjectInstanceBuilder(Type type) {
1616
}
1717

1818
public Instance create(Scope scope, Instance base, Type real) throws Exception {
19-
Model model = scope.getModel();
20-
Module module = type.getModule();
19+
Class actual = base.getClass();
2120

22-
return new ObjectInstance(module, model, base, real); // create the first instance
21+
if(actual != ObjectInstance.class) { // false if this(...) is called
22+
Model model = scope.getModel();
23+
Module module = type.getModule();
24+
25+
return new ObjectInstance(module, model, base, real); // create the first instance
26+
}
27+
return base;
2328
}
2429
}

snap-tree/src/main/java/org/snapscript/tree/define/StaticConstantCollector.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ public void collect(Type type) throws Exception {
3535
Scope scope = type.getScope();
3636
State state = scope.getState();
3737

38-
for(Type base : types) {
39-
if(type != base) {
40-
List<Property> properties = base.getProperties();
38+
for(Type next : types) {
39+
if(next != type) {
40+
List<Property> properties = next.getProperties();
4141

4242
for(Property property : properties) {
4343
String name = property.getName();

0 commit comments

Comments
 (0)