/// Performs an incremental step of garbage collection. The step "size" is controlled by data (larger values mean more steps) in a non-specified way. If you want to control the step size you must experimentally tune the value of data. The function returns 1 ifthe step finished a garbage-collection cycle.
/// Performs an incremental step of garbage collection. The step "size" is controlled by data (larger values mean more steps) in a non-specified way. ifyou want to control the step size you must experimentally tune the value of data. The function returns 1 ifthe step finished a garbage-collection cycle.
/// </summary>
/// </summary>
Step=5,
Step=5,
...
@@ -140,925 +141,47 @@ namespace LuaWrap
...
@@ -140,925 +141,47 @@ namespace LuaWrap
publicenumPseudoIndex:int
publicenumPseudoIndex:int
{
{
Registry=-10000,
Registry=(-10000),
Environment=-10001,
Environment=(-10001),
Globals=-10002
Globals=(-10002)
}
}
/// <summary>
publicstaticclassLuaLib
/// A delegate for C# function callbacks passed to Lua.
/// If an error happens outside any protected environment, Lua calls a panic function and then calls exit(EXIT_FAILURE), thus exiting the host application. Your panic function can avoid this exit by never returning (e.g., doing a long jump). The panic function can access the error message at the top of the stack.
/// </summary>
/// <param name="state">
/// A Lua State. <see cref="IntPtr"/>
/// </param>
/// <param name="cb">
/// A new panic function. <see cref="CallbackFunction"/>
/// To call a function you must use the following protocol: first, the function to be called is pushed onto the stack; then, the arguments to the function are pushed in direct order; that is, the first argument is pushed first. Finally you call lua_call.
/// </summary>
/// <param name="state">
/// A Lua State <see cref="IntPtr"/>
/// </param>
/// <param name="nargs">
/// Number of arguments to pass to the function. A <see cref="System.Int32"/>
/// </param>
/// <param name="nresults">
/// Number of results expected, or Globals.MultiRet for all results. A <see cref="System.Int32"/>
/// Ensures that there are at least extra free stack slots in the stack. It returns false if it cannot grow the stack to that size. This function never shrinks the stack; if the stack is already larger than the new size, it is left unchanged.
/// </summary>
/// <param name="state">
/// A Lua State. <see cref="IntPtr"/>
/// </param>
/// <param name="extra">
/// Extra slots to request. A <see cref="System.Int32"/>
/// Destroys all objects in the given Lua state (calling the corresponding garbage-collection metamethods, if any) and frees all dynamic memory used by this state.
/// </summary>
/// <param name="state">
/// A Lua State to be destroyed. <see cref="IntPtr"/>
/// Concatenates the n values at the top of the stack, pops them, and leaves the result at the top. If n is 1, the result is the single value on the stack (that is, the function does nothing); if n is 0, the result is the empty string. Concatenation is performed following the usual semantics of Lua.
/// Creates a new empty table and pushes it onto the stack. The new table has space pre-allocated for narr array elements and nrec non-array elements. This pre-allocation is useful when you know exactly how many elements the table will have. Otherwise you can use the function lua_newtable.
/// </summary>
/// <param name="state">
/// A <see cref="IntPtr"/>
/// </param>
/// <param name="narr">
/// Number of pre-allocated array elements. A <see cref="System.Int32"/>
/// </param>
/// <param name="nrec">
/// Number of pre-allocated non-array elements. A <see cref="System.Int32"/>
/// Returns 1 if the two values in acceptable indices index1 and index2 are equal, following the semantics of the Lua == operator (that is, may call metamethods). Otherwise returns 0. Also returns 0 if any of the indices is non valid.
/// </summary>
/// <param name="state">
/// A Lua State <see cref="IntPtr"/>
/// </param>
/// <param name="index1">
/// First index to compare. A <see cref="System.Int32"/>
/// </param>
/// <param name="index2">
/// Second index to compare. A <see cref="System.Int32"/>
/// </param>
/// <returns>
/// A result code integer <see cref="System.Int32"/>
/// Generates a Lua error. The error message (which can actually be a Lua value of any type) must be on the stack top. This function does a long jump, and therefore never returns. (see luaL_error).
/// Pushes onto the stack the value t[k], where t is the value at the given valid index. As in Lua, this function may trigger a metamethod for the "index" event
/// </summary>
/// <param name="state">
/// A Lua State <see cref="IntPtr"/>
/// </param>
/// <param name="index">
/// The stack index for the table to query. A <see cref="System.Int32"/>
/// </param>
/// <param name="key">
/// The key to aquire. A <see cref="System.String"/>
/// Pushes onto the stack the metatable of the value at the given acceptable index. If the index is not valid, or if the value does not have a metatable, the function returns 0 and pushes nothing on the stack.
/// </summary>
/// <param name="state">
/// A Lua State. <see cref="IntPtr"/>
/// </param>
/// <param name="index">
/// A valid stack index. <see cref="System.Int32"/>
/// Returns the index of the top element in the stack. Because indices start at 1, this result is equal to the number of elements in the stack (and so 0 means an empty stack).
/// Moves the top element into the given valid index, shifting up the elements above this index to open space. Cannot be called with a pseudo-index, because a pseudo-index is not an actual stack position.
/// </summary>
/// <param name="state">
/// A Lua State. <see cref="IntPtr"/>
/// </param>
/// <param name="index">
/// A valid stack index. <see cref="System.Int32"/>
/// Returns true if the value at acceptable index index1 is smaller than the value at acceptable index index2, following the semantics of the Lua < operator (that is, may call metamethods). Otherwise returns false. Also returns false if any of the indices is non valid.
/// This function allocates a new block of memory with the given size, pushes onto the stack a new full userdata with the block address, and returns this address.
/// Pops a key from the stack, and pushes a key-value pair from the table at the given index (the "next" pair after the given key). If there are no more elements in the table, then lua_next returns false (and pushes nothing).
/// Returns the "length" of the value at the given acceptable index: for strings, this is the string length; for tables, this is the result of the length operator ('#'); for userdata, this is the size of the block of memory allocated for the userdata; for other values, it is 0.
/// Both nargs and nresults have the same meaning as in lua_call. If there are no errors during the call, lua_pcall behaves exactly like lua_call. However, if there is any error, lua_pcall catches it, pushes a single value on the stack (the error message), and returns an error code. Like lua_call, lua_pcall always removes the function and its arguments from the stack.
/// If errfunc is 0, then the error message returned on the stack is exactly the original error message. Otherwise, errfunc is the stack index of an error handler function. (In the current implementation, this index cannot be a pseudo-index.) In case of runtime errors, this function will be called with the error message and its return value will be the message returned on the stack by lua_pcall.
/// Typically, the error handler function is used to add more debug information to the error message, such as a stack traceback. Such information cannot be gathered after the return of lua_pcall, since by then the stack has unwound.
/// When a C function is created, it is possible to associate some values with it, thus creating a C closure; these values are then accessible to the function whenever it is called. To associate values with a C function, first these values should be pushed onto the stack (when there are multiple values, the first value is pushed first). Then lua_pushcclosure is called to create and push the C function onto the stack, with the argument n telling how many values should be associated with the function. lua_pushcclosure also pops these values from the stack.
/// Pushes a C function onto the stack. This function receives a pointer to a C function and pushes onto the stack a Lua value of type function that, when called, invokes the corresponding C function.
/// Userdata represent C values in Lua. A light userdata represents a pointer. It is a value (like a number): you do not create it, it has no individual metatable, and it is not collected (as it was never created). A light userdata is equal to "any" light userdata with the same C address.
/// Pushes the string s with size len onto the stack. Lua makes (or reuses) an internal copy of the given string, so the memory at s can be freed or reused immediately after the function returns. The string can contain embedded zeros.
/// Returns true if the two values in acceptable indices index1 and index2 are primitively equal (that is, without calling metamethods). Otherwise returns false. Also returns false if any of the indices are non valid.
/// Removes the element at the given valid index, shifting down the elements above this index to fill the gap. Cannot be called with a pseudo-index, because a pseudo-index is not an actual stack position.
/// Pops a table from the stack and sets it as the new environment for the value at the given index. If the value at the given index is neither a function nor a thread nor a userdata, lua_setfenv returns false. Otherwise it returns true.
/// Does the equivalent to t[k] = v, where t is the value at the given valid index, v is the value at the top of the stack, and k is the value just below the top.
/// This function pops both the key and the value from the stack. As in Lua, this function may trigger a metamethod for the "newindex" event.
/// Accepts any acceptable index, or 0, and sets the stack top to this index. If the new top is larger than the old one, then the new elements are filled with nil. If index is 0, then all stack elements are removed.
/// Converts the Lua value at the given acceptable index to a boolean value. Like all tests in Lua, lua_toboolean returns true for any Lua value different from false and nil; otherwise it returns false. It also returns false when called with a non-valid index. (If you want to accept only actual boolean values, use lua_isboolean to test the value's type.)
/// Converts the Lua value at the given acceptable index to the signed integral type lua_Integer. The Lua value must be a number or a string convertible to a number (see §2.2.1); otherwise, lua_tointeger returns 0.
/// Converts the Lua value at the given acceptable index to a C string. If len is not NULL, it also sets *len with the string length. The Lua value must be a string or a number; otherwise, the function returns NULL. If the value is a number, then lua_tolstring also changes the actual value in the stack to a string. (This change confuses lua_next when lua_tolstring is applied to keys during a table traversal.)
/// lua_tolstring returns a fully aligned pointer to a string inside the Lua state. This string always has a zero ('\0') after its last character (as in C), but can contain other zeros in its body. Because Lua has garbage collection, there is no guarantee that the pointer returned by lua_tolstring will be valid after the corresponding value is removed from the stack.
/// Converts the Lua value at the given acceptable index to a number. The Lua value must be a number or a string convertible to a number, otherwise returns 0.
/// Converts the value at the given acceptable index to a generic pointer. The value can be a userdata, a table, a thread, or a function; otherwise, lua_topointer returns NULL. Different objects will give different pointers. There is no way to convert the pointer back to its original value.
/// If the value at the given acceptable index is a full userdata, returns its block address. If the value is a light userdata, returns its pointer. Otherwise, returns NULL.
/// Returns the name of the type encoded by the value tp.
/// Pushes a C function onto the stack. This function receives a pointer to a C function and pushes onto the stack a Lua value of type function that, when called, invokes the corresponding C function.
/// If the object at index obj has a metatable and this metatable has a field e, this function calls this field and passes the object as its only argument. In this case this function returns 1 and pushes onto the stack the value returned by the call. If there is no metatable or no metamethod, this function returns 0 (without pushing any value on the stack).
/// Raises an error. The error message format is given by fmt plus any extra arguments, following the same rules of lua_pushfstring. It also adds at the beginning of the message the file name and the line number where the error occurred, if this information is available.
/// Pushes onto the stack the field key from the metatable of the object at the given index. If the object does not have a metatable, or if the metatable does not have this field, returns false and pushes nothing.
/// Loads a buffer as a Lua chunk. This function uses lua_load to load the chunk in the buffer pointed to by buff with size sz. name is the chunk name, used for debug and errors.
/// Loads a file as a Lua chunk. This function uses lua_load to load the chunk in the file named filename. If filename is NULL, then it loads from the standard input. The first line in the file is ignored if it starts with a #.
/// If the registry already has the given key, returns false. Otherwise, creates a new table to be used as a metatable for userdata, adds it to the registry with the given key, and returns true.
/// In both cases pushes onto the stack the final value associated with the given key in the registry.
/// Creates a new Lua state. It calls lua_newstate with an allocator based on the standard C realloc function and then sets a panic function (see lua_atpanic) that prints an error message to the standard error output in case of fatal errors.
/// Creates and returns a reference, in the table at index t, for the object at the top of the stack (and pops the object).
/// A reference is a unique integer key. As long as you do not manually add integer keys into table t, luaL_ref ensures the uniqueness of the key it returns. You can retrieve an object referred by reference r by calling lua_rawgeti(L, t, r). Function luaL_unref frees a reference and its associated object.
/// If the object at the top of the stack is nil, luaL_ref returns the constant LUA_REFNIL. The constant LUA_NOREF is guaranteed to be different from any reference returned by luaL_ref.
/// </summary>
/// <param name="state">
/// A <see cref="IntPtr"/>
/// </param>
/// <param name="t">
/// A <see cref="System.Int32"/>
/// </param>
/// <returns>
/// A <see cref="System.Int32"/>
/// </returns>
publicstaticexternintluaL_ref(IntPtrstate,intt);
/// <summary>
/// <summary>
/// Pops the value referenced by reference by r in the table at index t onto the stack.
/// Pops the value referenced by reference by r in the table at index t onto the stack.
/// Releases reference ref from the table at index t (see luaL_ef). The entry is removed from the table, so that the referred object can be collected. The reference ref is also freed to be used again.
/// Pushes onto the stack a string identifying the current position of the control at level lvl in the call stack. Typically this string has the following format:
/// chunkname:currentline:
/// Level 0 is the running function, level 1 is the function that called the running function, etc.
// If the argument is a mere string, we are free to add extra info to it (as opposed to some private C# exception object or somesuch, which we just pass up)
// If the argument is a mere string, we are free to add extra info to it (as opposed to some private C# exception object or somesuch, which we just pass up)
if(eisstring)
if(eisstring)
{
{
// We use this to remove anything pushed by luaL_where
// We use this to remove anything pushed by luaL_where
intoldTop=LuaLib.lua_gettop(luaState);
intoldTop=KopiLua.Lua.lua_gettop(luaState);
// Stack frame #1 is our C# wrapper, so not very interesting to the user
// Stack frame #1 is our C# wrapper, so not very interesting to the user
// Stack frame #2 must be the lua code that called us, so that's what we want to use
// Stack frame #2 must be the lua code that called us, so that's what we want to use
LuaLib.luaL_where(luaState,2);
KopiLua.Lua.luaL_where(luaState,2);
object[]curlev=popValues(luaState,oldTop);
object[]curlev=popValues(luaState,oldTop);
// Debug.WriteLine(curlev);
// Debug.WriteLine(curlev);
if(curlev.Length>0)
if(curlev.Length>0)
e=curlev[0].ToString()+e;
e=curlev[0].ToString()+e;
}
}
push(luaState,e);
push(luaState,e);
LuaLib.lua_error(luaState);
KopiLua.Lua.lua_error(luaState);
}
}
/*
/*
* Implementation of load_assembly. Throws an error
* Implementation of load_assembly. Throws an error
// Note: starting with lua5.1 the garbage collector may remove weak reference items (such as our luaNet_objects values) when the initial GC sweep
// Note: starting with lua5.1 the garbage collector may remove weak reference items (such as our luaNet_objects values) when the initial GC sweep
// occurs, but the actual call of the __gc finalizer for that object may not happen until a little while later. During that window we might call
// occurs, but the actual call of the __gc finalizer for that object may not happen until a little while later. During that window we might call
// this routine and find the element missing from luaNet_objects, but collectObject() has not yet been called. In that case, we go ahead and call collect
// this routine and find the element missing from luaNet_objects, but collectObject() has not yet been called. In that case, we go ahead and call collect
// object here
// object here
// did we find a non nil object in our table? if not, we need to call collect object
// did we find a non nil object in our table? if not, we need to call collect object