diff --git a/Eluant/LuaApi.cs b/Eluant/LuaApi.cs index 9b306ca..1a0eeeb 100644 --- a/Eluant/LuaApi.cs +++ b/Eluant/LuaApi.cs @@ -293,6 +293,14 @@ public static void luaL_getmetatable(IntPtr L, string name) [DllImport(LUA_DLL, CallingConvention=LUA_CALLING_CONVENTION)] public static extern int luaL_loadstring(IntPtr L, [MarshalAs(UnmanagedType.LPStr)] string s); + [DllImport(LUA_DLL, CallingConvention=LUA_CALLING_CONVENTION)] + public static extern int luaL_loadbuffer(IntPtr L, [MarshalAs(UnmanagedType.LPStr)] string s, UIntPtr size, string n); + + public static int luaL_loadbuffer(IntPtr L, string s, int size, string n) + { + return luaL_loadbuffer(L, s, new UIntPtr(unchecked((ulong)size)), n); + } + [DllImport(LUA_DLL, CallingConvention=LUA_CALLING_CONVENTION)] public static extern int luaL_newmetatable(IntPtr L, [MarshalAs(UnmanagedType.LPStr)] string tname); diff --git a/Eluant/LuaRuntime.cs b/Eluant/LuaRuntime.cs index b1d97e9..f215294 100644 --- a/Eluant/LuaRuntime.cs +++ b/Eluant/LuaRuntime.cs @@ -204,7 +204,7 @@ private void Initialize() LuaApi.lua_pop(LuaState, 1); - DoString(Scripts.BindingSupport).Dispose(); + DoBuffer(Scripts.BindingSupport, "BindingSupport.lua").Dispose(); createManagedCallWrapper = (LuaFunction)Globals["eluant_create_managed_call_wrapper"]; @@ -492,9 +492,9 @@ private void ProcessReleasedReferences() } } - private void LoadString(string str) + private void LoadBuffer(string str, string name) { - if (LuaApi.luaL_loadstring(LuaState, str) != 0) { + if (LuaApi.luaL_loadbuffer(LuaState, str, str.Length, name) != 0) { var error = LuaApi.lua_tostring(LuaState, -1); LuaApi.lua_pop(LuaState, 1); @@ -508,7 +508,20 @@ public LuaVararg DoString(string str) CheckDisposed(); - LoadString(str); + LoadBuffer(str, str); + + // Compiled code is on the stack, now call it. + return Call(new LuaValue[0]); + } + + public LuaVararg DoBuffer(string str, string name) + { + if (str == null) { throw new ArgumentNullException("str"); } + if (name == null) { throw new ArgumentNullException("name"); } + + CheckDisposed(); + + LoadBuffer(str, name); // Compiled code is on the stack, now call it. return Call(new LuaValue[0]); @@ -520,7 +533,7 @@ public LuaFunction CompileString(string str) CheckDisposed(); - LoadString(str); + LoadBuffer(str, str); var fn = Wrap(-1);