diff --git a/currant.js b/currant.js index 51463d6..30e92e4 100644 --- a/currant.js +++ b/currant.js @@ -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; } @@ -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) { @@ -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; } } diff --git a/currant/nodes/blockNode.js b/currant/nodes/blockNode.js index cd30baf..b38e7f4 100644 --- a/currant/nodes/blockNode.js +++ b/currant/nodes/blockNode.js @@ -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) { diff --git a/currant/nodes/node.js b/currant/nodes/node.js index c705c52..31cf7bd 100644 --- a/currant/nodes/node.js +++ b/currant/nodes/node.js @@ -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; } diff --git a/currant/types/customType.js b/currant/types/customType.js index 6a96d02..3a51fe8 100644 --- a/currant/types/customType.js +++ b/currant/types/customType.js @@ -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; } }