Skip to content

Commit

Permalink
Add luaL_loadbuffer.
Browse files Browse the repository at this point in the history
This is exposed via LuaRuntime.DoBuffer, and used internally in order to mask the first text snippet of BindingSupport.lua in LuaExceptions.
  • Loading branch information
pchote committed Apr 9, 2014
1 parent 74c46df commit 866eb48
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
8 changes: 8 additions & 0 deletions Eluant/LuaApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
23 changes: 18 additions & 5 deletions Eluant/LuaRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"];

Expand Down Expand Up @@ -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);

Expand All @@ -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]);
Expand All @@ -520,7 +533,7 @@ public LuaFunction CompileString(string str)

CheckDisposed();

LoadString(str);
LoadBuffer(str, str);

var fn = Wrap(-1);

Expand Down

0 comments on commit 866eb48

Please sign in to comment.