From c784e53ed96a00ebc170fa85313389ba7ece23b7 Mon Sep 17 00:00:00 2001 From: Jan Date: Fri, 19 Dec 2014 20:12:05 +0100 Subject: [PATCH 1/2] Added passing an array to LuaFunctions --- luad/lfunction.d | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/luad/lfunction.d b/luad/lfunction.d index 4fd6e92..d370217 100644 --- a/luad/lfunction.d +++ b/luad/lfunction.d @@ -67,6 +67,35 @@ struct LuaFunction return callWithRet!T(this.state, args.length); } + /** + * Call this function using an array. + * Params: + * T = expected return type. + * args = list of arguments. + * Returns: + * Return value of type $(D T), or nothing if $(D T) was unspecified. + * See $(DPMODULE2 conversions,functions) for how to + * catch multiple return values. + * Examples: + * ------------------ + lua.doString(`function ask(question) return 42 end`); + auto ask = lua.get!LuaFunction("ask"); + + auto params = ["How many letters are in the alphabet?", "5", "20", "10", "26"]; + + auto answer = ask.call!int(params); + assert(answer == "26"); + * ------------------ + */ + T call(T = void, U)(U[] args...) + { + this.push(); + foreach(arg; args) + pushValue(this.state, arg); + + return callWithRet!T(this.state, args.length); + } + /** * Set a new environment for this function. * From d73891dbdb672326a1d2750ba6f26cfa81cd95ba Mon Sep 17 00:00:00 2001 From: Jan Date: Fri, 19 Dec 2014 20:15:00 +0100 Subject: [PATCH 2/2] Fixed example for call(T = void, U)(U[] args...) --- luad/lfunction.d | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/luad/lfunction.d b/luad/lfunction.d index d370217..d3d987f 100644 --- a/luad/lfunction.d +++ b/luad/lfunction.d @@ -78,12 +78,12 @@ struct LuaFunction * catch multiple return values. * Examples: * ------------------ - lua.doString(`function ask(question) return 42 end`); - auto ask = lua.get!LuaFunction("ask"); + lua.doString(`function answerD(question, a, b, c, d) print(question) return d end`); + auto answerD = lua.get!LuaFunction("answerD"); auto params = ["How many letters are in the alphabet?", "5", "20", "10", "26"]; - auto answer = ask.call!int(params); + auto answer = answerD.call!int(params); assert(answer == "26"); * ------------------ */