Skip to content

Commit

Permalink
AnimateTimeline: extract functions for framescripts into separate __c…
Browse files Browse the repository at this point in the history
…reateScriptCallback() method

This should help with GC because fewer variables will be captured in the closure
  • Loading branch information
joshtynjala committed May 31, 2024
1 parent ff02a84 commit 9980aa2
Showing 1 changed file with 46 additions and 34 deletions.
80 changes: 46 additions & 34 deletions src/swf/exporters/animate/AnimateTimeline.hx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class AnimateTimeline extends Timeline
var frameData:AnimateFrame;

#if hscript
var parser = null;
var parser:Parser = null;
#end

for (i in 0...__symbol.frames.length)
Expand Down Expand Up @@ -98,41 +98,11 @@ class AnimateTimeline extends Timeline
parser.allowTypes = true;
}

var program = parser.parseString(frameData.scriptSource);
var interp = new Interp();

var script = function(scope:MovieClip)
{
interp.variables.set("this", scope);
interp.execute(program);
};

var script = __createScriptCallback(parser, frameData.scriptSource);
scripts.push(new FrameScript(script, frame));
#elseif js
var script = untyped untyped #if haxe4 js.Syntax.code #else __js__ #end ("eval({0})", "(function(){" + frameData.scriptSource + "})");
var wrapper = function(scope:MovieClip)
{
try
{
script.call(scope);
}
catch (e:Dynamic)
{
Log.info("Error evaluating frame script\n "
+ e
+ "\n"
+ haxe.CallStack.exceptionStack().map(function(a)
{
return untyped a[2];
}).join("\n")
+ "\n"
+ e.stack
+ "\n"
+ untyped script.toString());
}
}

scripts.push(new FrameScript(wrapper, frame));
var script = __createScriptCallback(frameData.scriptSource);
scripts.push(new FrameScript(script, frame));
#end
}
catch (e:Dynamic)
Expand Down Expand Up @@ -554,6 +524,48 @@ class AnimateTimeline extends Timeline
}
}
}

#if hscript
@:noCompletion private function __createScriptCallback(parser:Parser, scriptSource:String):MovieClip->Void
{
var program = parser.parseString(scriptSource);
var interp = new Interp();

return function(scope:MovieClip):Void
{
interp.variables.set("this", scope);
interp.execute(program);
};
}
#end

#if js
@:noCompletion private function __createScriptCallback(scriptSource:String):MovieClip->Void
{
var script = untyped untyped #if haxe4 js.Syntax.code #else __js__ #end ("eval({0})", "(function(){" + scriptSource + "})");
return function(scope:MovieClip):Void
{
try
{
script.call(scope);
}
catch (e:Dynamic)
{
Log.info("Error evaluating frame script\n "
+ e
+ "\n"
+ haxe.CallStack.exceptionStack().map(function(a)
{
return untyped a[2];
}).join("\n")
+ "\n"
+ e.stack
+ "\n"
+ untyped script.toString());
}
};
}
#end
}

#if !openfl_debug
Expand Down

0 comments on commit 9980aa2

Please sign in to comment.