From 486fef81902959be26f137ab690043d00524a941 Mon Sep 17 00:00:00 2001 From: Joseph Humfrey Date: Thu, 7 Apr 2016 12:07:01 +0100 Subject: [PATCH] Fix arguments in external function, which should be reversed prior to use, because of the way they're popped off the stack. Added a new test, and fixed the existing one. --- ink-engine-runtime/Story.cs | 4 ++++ tests/Tests.cs | 16 ++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/ink-engine-runtime/Story.cs b/ink-engine-runtime/Story.cs index b355f244..25a22b2c 100644 --- a/ink-engine-runtime/Story.cs +++ b/ink-engine-runtime/Story.cs @@ -1044,6 +1044,10 @@ internal void CallExternalFunction(string funcName, int numberOfArguments) arguments.Add (valueObj); } + // Reverse arguments from the order they were popped, + // so they're the right way round again. + arguments.Reverse (); + // Run the function! object funcResult = func (arguments.ToArray()); diff --git a/tests/Tests.cs b/tests/Tests.cs index f3d3b98d..7ba8dbe9 100644 --- a/tests/Tests.cs +++ b/tests/Tests.cs @@ -1676,8 +1676,10 @@ public void TestExternalBinding() var story = CompileString (@" EXTERNAL message(x) EXTERNAL multiply(x,y) +EXTERNAL times(i,str) ~ message(""hello world"") {multiply(5.0, 3)} +{times(3, ""knock "")} "); string message = null; @@ -1685,12 +1687,22 @@ EXTERNAL multiply(x,y) message = "MESSAGE: "+arg; }); - story.BindExternalFunction ("multiply", (int arg1, float arg2) => { + story.BindExternalFunction ("multiply", (float arg1, int arg2) => { return arg1 * arg2; }); + story.BindExternalFunction ("times", (int numberOfTimes, string str) => { + string result = ""; + for(int i=0; i