Skip to content

Commit

Permalink
Fix instance allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
ngallagher committed Aug 21, 2016
1 parent b195cf4 commit 1e9430a
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class StaticConstructorTest extends TestCase {
" }\n"+
"}\n"+
"var x = new X();\n"+
"assert x.pattern == '.*';\n"+
"println(x.pattern);\n";

public void testThis() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public PrimitiveInstance(Module module, Model model, Scope scope, Type type) {

@Override
public Instance getInner() {
return new ObjectInstance(module, model, this, type);
return new CompoundInstance(module, model, this, this, type);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static org.snapscript.core.Reserved.METHOD_TO_STRING;
import static org.snapscript.core.Reserved.METHOD_WAIT;
import static org.snapscript.core.Reserved.TYPE_CONSTRUCTOR;
import static org.snapscript.core.Reserved.TYPE_THIS;

import java.util.List;

Expand All @@ -17,8 +18,11 @@
import org.snapscript.core.Result;
import org.snapscript.core.ResultType;
import org.snapscript.core.Scope;
import org.snapscript.core.State;
import org.snapscript.core.Type;
import org.snapscript.core.TypeLoader;
import org.snapscript.core.Value;
import org.snapscript.core.ValueType;
import org.snapscript.core.define.Instance;
import org.snapscript.core.function.Function;
import org.snapscript.core.function.Invocation;
Expand Down Expand Up @@ -72,6 +76,10 @@ public NewInvocation() {
public Result invoke(Scope scope, Object object, Object... list) throws Exception {
Type real = (Type)list[0];
Instance instance = builder.create(scope, real);
State state = instance.getState();
Value value = ValueType.getReference(object, real); // this needs to be a blank

state.addValue(TYPE_THIS, value); // reference to 'this'

return ResultType.getNormal(instance);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.snapscript.core.State;
import org.snapscript.core.Type;
import org.snapscript.core.Value;
import org.snapscript.core.ValueType;
import org.snapscript.core.define.Initializer;
import org.snapscript.core.define.Instance;
import org.snapscript.core.function.Invocation;
Expand All @@ -28,10 +27,12 @@ public Instance allocate(Scope scope, Instance base, Object... list) throws Exce
Type real = (Type)list[0];
Instance object = builder.create(scope, base, real);// we need to pass the base type up!!
State state = object.getState();
Value constant = ValueType.getReference(object, real); // this needs to be a blank

state.addValue(TYPE_THIS, constant); // reference to 'this'
initializer.execute(object, real);
Value value = state.getValue(TYPE_THIS);

if(object != base) { // false if this(...) called
initializer.execute(object, real);
}
value.setValue(object); // set the 'this' variable
invocation.invoke(object, object, list);

return object;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package org.snapscript.tree.define;

import static org.snapscript.core.Reserved.TYPE_THIS;

import java.util.concurrent.atomic.AtomicBoolean;

import org.snapscript.core.Result;
import org.snapscript.core.ResultType;
import org.snapscript.core.Scope;
import org.snapscript.core.State;
import org.snapscript.core.Type;
import org.snapscript.core.Value;
import org.snapscript.core.define.Initializer;
import org.snapscript.core.define.Instance;
import org.snapscript.core.function.Invocation;
Expand Down Expand Up @@ -43,20 +39,6 @@ public Result invoke(Scope scope, Instance base, Object... list) throws Exceptio
initializer.compile(scope, type); // static stuff if needed
}
Instance result = allocator.allocate(scope, inner, list);

if(real == type){
Instance next = result;

while(next != null){
State state = next.getState();
Value value = state.getValue(TYPE_THIS);

if(value != null){
value.setValue(result);
}
next = next.getSuper();
}
}
return ResultType.getNormal(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ public ObjectInstanceBuilder(Type type) {
}

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

return new ObjectInstance(module, model, base, real); // create the first instance
if(actual != ObjectInstance.class) { // false if this(...) is called
Model model = scope.getModel();
Module module = type.getModule();

return new ObjectInstance(module, model, base, real); // create the first instance
}
return base;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public void collect(Type type) throws Exception {
Scope scope = type.getScope();
State state = scope.getState();

for(Type base : types) {
if(type != base) {
List<Property> properties = base.getProperties();
for(Type next : types) {
if(next != type) {
List<Property> properties = next.getProperties();

for(Property property : properties) {
String name = property.getName();
Expand Down

0 comments on commit 1e9430a

Please sign in to comment.