Skip to content

Commit

Permalink
Fixed a problem with object creation
Browse files Browse the repository at this point in the history
Fixed a problem with function literals passed as function call parameters in object constructor calls, where the passed function's scope would not become a child of the object scope
  • Loading branch information
schwalbe-t authored Jan 26, 2023
1 parent 2022e3d commit 33525b4
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 24 deletions.
30 changes: 18 additions & 12 deletions currant.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ class CurrantNode {
if(this.children[childIndex] === null) instance.addChild(null);
else instance.addChild(this.children[childIndex].copy(childBlock));
}
if(typeof instance.afterCopy === "function") instance.afterCopy();
if(typeof instance.afterCopy === "function") instance.afterCopy(this);
return instance;
}

Expand Down Expand Up @@ -662,8 +662,17 @@ class CurrantBlockNode extends CurrantNode {
this.currant = null;
}

afterCopy() {
this.variables = new Map(this.variables);
afterCopy(copiedInstance) {
const oldVariables = this.variables;
this.variables = new Map();
if(typeof oldVariables === "undefined") return;
for(const keyName of oldVariables.keys()) {
this.variables.set(keyName, new CurrantBlockVariableWrapperObject(oldVariables.get(keyName).get().copy()));
const value = this.variables.get(keyName).get().get();
if(value !== null && value instanceof CurrantFunction && value.body.block === copiedInstance) {
value.body.block = this;
}
}
}

setRuntime(interpreter) {
Expand Down Expand Up @@ -2026,16 +2035,13 @@ class CurrantCustomType extends CurrantType {
class CurrantCustomObject {

constructor(data) {
this.variables = new Map();
this.block = data.block;
for(const keyName of data.variables.keys()) {
this.variables.set(keyName, new CurrantBlockVariableWrapperObject(data.variables.get(keyName).get().copy()));
const value = this.variables.get(keyName).get().get();
if(value !== null && value.constructor === CurrantFunction) {
// should the function member be defined as a child of data, make it a child of this
if(value.body.block === data) value.body.block = this;
}
if(data instanceof CurrantCustomObject) {
this.funcBody = data.funcBody.copy(data.funcBody.block);
} else {
this.funcBody = data;
}
this.variables = this.funcBody.variables;
this.block = this.funcBody.block;
}

}
Expand Down
13 changes: 11 additions & 2 deletions currant/nodes/blockNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,17 @@ class CurrantBlockNode extends CurrantNode {
this.currant = null;
}

afterCopy() {
this.variables = new Map(this.variables);
afterCopy(copiedInstance) {
const oldVariables = this.variables;
this.variables = new Map();
if(typeof oldVariables === "undefined") return;
for(const keyName of oldVariables.keys()) {
this.variables.set(keyName, new CurrantBlockVariableWrapperObject(oldVariables.get(keyName).get().copy()));
const value = this.variables.get(keyName).get().get();
if(value !== null && value instanceof CurrantFunction && value.body.block === copiedInstance) {
value.body.block = this;
}
}
}

setRuntime(interpreter) {
Expand Down
2 changes: 1 addition & 1 deletion currant/nodes/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class CurrantNode {
if(this.children[childIndex] === null) instance.addChild(null);
else instance.addChild(this.children[childIndex].copy(childBlock));
}
if(typeof instance.afterCopy === "function") instance.afterCopy();
if(typeof instance.afterCopy === "function") instance.afterCopy(this);
return instance;
}

Expand Down
15 changes: 6 additions & 9 deletions currant/types/customType.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,13 @@ class CurrantCustomType extends CurrantType {
class CurrantCustomObject {

constructor(data) {
this.variables = new Map();
this.block = data.block;
for(const keyName of data.variables.keys()) {
this.variables.set(keyName, new CurrantBlockVariableWrapperObject(data.variables.get(keyName).get().copy()));
const value = this.variables.get(keyName).get().get();
if(value !== null && value.constructor === CurrantFunction) {
// should the function member be defined as a child of data, make it a child of this
if(value.body.block === data) value.body.block = this;
}
if(data instanceof CurrantCustomObject) {
this.funcBody = data.funcBody.copy(data.funcBody.block);
} else {
this.funcBody = data;
}
this.variables = this.funcBody.variables;
this.block = this.funcBody.block;
}

}
Expand Down

0 comments on commit 33525b4

Please sign in to comment.