Skip to content

[BUGFIX] Prevent Script Values being dropped early #209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 32 additions & 16 deletions polymod/hscript/_internal/PolymodInterpEx.hx
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,9 @@ class PolymodInterpEx extends Interp
locals.set(n, {r: result});
return null;
case EFunction(params, fexpr, name, _): // Fix to ensure callback functions catch thrown errors.
var capturedLocals = duplicate(locals);
var me = this;
// Using a clone to prevent locals getting wiped out.
var clone = this.clone();

var hasOpt = false, minParams = 0;
for (p in params)
{
Expand Down Expand Up @@ -284,27 +285,24 @@ class PolymodInterpEx extends Interp
}
args = args2;
}
var old = me.locals;
var depth = me.depth;
me.depth++;
me.locals = me.duplicate(capturedLocals);

clone.depth++;

for (i in 0...params.length)
{
me.locals.set(params[i].name, {r: args[i]});
clone.locals.set(params[i].name, {r: args[i]});
}
var r = null;
var oldDecl = declared.length;

if (inTry)
{
// True if the SCRIPT wraps the function in a try/catch block.
try
{
r = me.exprReturn(fexpr);
r = clone.exprReturn(fexpr);
}
catch (e:Dynamic)
{
me.locals = old;
me.depth = depth;
#if neko
neko.Lib.rethrow(e);
#else
Expand All @@ -317,7 +315,7 @@ class PolymodInterpEx extends Interp
// There is no try/catch block. We can add some custom error handling.
try
{
r = me.exprReturn(fexpr);
r = clone.exprReturn(fexpr);
}
catch (err:PolymodExprEx.ErrorEx)
{
Expand All @@ -334,9 +332,6 @@ class PolymodInterpEx extends Interp
throw err;
}
}
restore(oldDecl);
me.locals = old;
me.depth = depth;
return r;
};

Expand All @@ -354,7 +349,7 @@ class PolymodInterpEx extends Interp
declared.push({n: name, old: locals.get(name)});
var ref = {r: newFun};
locals.set(name, ref);
capturedLocals.set(name, ref); // allow self-recursion
clone.locals.set(name, ref); // allow self-recursion
}
}
return newFun;
Expand Down Expand Up @@ -900,6 +895,27 @@ class PolymodInterpEx extends Interp
}
}
}

public function clone():PolymodInterpEx
{
var output = new PolymodInterpEx(this.targetCls, this._proxy);

// Copy only the non-default values.
for (key => value in this.variables)
if (!output.variables.exists(key)) output.variables.set(key, value);

for (key => value in this.locals)
if (!output.locals.exists(key)) output.locals.set(key, value);

for (val in this.declared)
if (!output.declared.contains(val)) output.declared.push(val);

output._nextCallObject = this._nextCallObject;
output.depth = this.depth;
output.curExpr = this.curExpr;
output.inTry = this.inTry;
return output;
}
}

private class ArrayIterator<T>
Expand Down