From 447bc5853a079bf6807b7a3acf13757f679b5cb3 Mon Sep 17 00:00:00 2001 From: James Roseborough Date: Thu, 24 Jul 2008 18:32:02 +0000 Subject: [PATCH] Remove many unused and unimplemented functions. --- src/core/org/luaj/lib/BaseLib.java | 6 +- src/core/org/luaj/vm/LuaState.java | 735 +------------------- src/j2se/org/luaj/lib/j2se/LuajavaLib.java | 2 +- src/test/java/org/luaj/vm/LuaStateTest.java | 4 +- version.properties | 2 +- 5 files changed, 23 insertions(+), 726 deletions(-) diff --git a/src/core/org/luaj/lib/BaseLib.java b/src/core/org/luaj/lib/BaseLib.java index 475d00ee..5fe53439 100644 --- a/src/core/org/luaj/lib/BaseLib.java +++ b/src/core/org/luaj/lib/BaseLib.java @@ -143,7 +143,7 @@ public class BaseLib extends LFunction { case IPAIRS: { LTable t = vm.checktable(2); vm.resettop(); - vm.pushjavafunction( inext ); + vm.pushfunction( inext ); vm.pushlvalue( t ); vm.pushinteger( 0 ); break; @@ -151,7 +151,7 @@ public class BaseLib extends LFunction { case PAIRS: { LTable t = vm.checktable(2); vm.resettop(); - vm.pushjavafunction( next ); + vm.pushfunction( next ); vm.pushlvalue( t ); vm.pushnil(); break; @@ -392,7 +392,7 @@ public class BaseLib extends LFunction { private static LValue getfunc (LuaState vm, boolean opt) { if ( vm.isfunction(2) ) - return vm.tojavafunction(2); + return vm.tofunction(2); else { int level = opt? vm.optint(2, 1): vm.checkint(2); vm.argcheck(level >= 0, 2, "level must be non-negative"); diff --git a/src/core/org/luaj/vm/LuaState.java b/src/core/org/luaj/vm/LuaState.java index 78182425..f6d7af52 100644 --- a/src/core/org/luaj/vm/LuaState.java +++ b/src/core/org/luaj/vm/LuaState.java @@ -996,31 +996,6 @@ public class LuaState extends Lua { // Lua Java API //=============================================================== - private void notImplemented() { - throw new LuaErrorException("AbstractStack: not yet implemented"); - } - - /** - * Sets a new panic function and returns the old one. [-0, +0, -] - * - * - *

- * 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 may 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. - */ - public LFunction atpanic(LFunction panicf) { - LFunction f = panic; - panic = panicf; - return f; - } - /** * Returns the current program counter for the given call frame. * @param ci -- A call frame @@ -1092,176 +1067,12 @@ public class LuaState extends Lua { } /** - * Raises an error with the default level. + * Raises an error with the default level. */ public void error(String message) { throw new LuaErrorException( this, message, 1 ); } - - /** - * Generates a Lua error. [-1, +0, v] - * - *

- * 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). - * - */ - public void error() { - throw new LuaErrorException( this, tostring(-1), 0); - } - - /** - * Dumps a function as a binary chunk. [-0, +0, - * m] - * - *

- * Receives a Lua function on the top of the stack and produces a binary - * chunk that, if loaded again, results in a function equivalent to the one - * dumped. As it produces parts of the chunk, lua_dump - * calls function writer (see lua_Writer) - * with the given data to write them. - *

- * The value returned is the error code returned by the last call to the - * writer; 0 means no errors. - * - * - *

- * This function does not pop the Lua function from the stack. - * - */ - public void dump() { - notImplemented(); - } - - /** - * Pushes a new C closure onto the stack. [-n, +1, - * m] - * - * - *

- * When a Java function is created, it is possible to associate some - * values with it, thus creating a C closure (see §3.4); 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. - */ - public void pushclosure(LFunction fn, int n) { - notImplemented(); - } - - /** - * Calls the C function func in protected mode. [-0, +(0|1), -] - * - *

- * func starts with only one element in its stack, a light - * userdata containing ud. In case of errors, lua_cpcall returns the same error - * codes as lua_pcall, plus the - * error object on the top of the stack; otherwise, it returns zero, and - * does not change the stack. All values returned by func are - * discarded. - */ - public int javapcall(LFunction func, Object ud) { - this.pushjavafunction(func); - this.pushlightuserdata(ud); - return this.pcall(1, 0, 0); - } - - /** - * Format and push a string. [-0, +1, m] - * - *

- * Pushes onto the stack a formatted string and returns a pointer to this - * string. It is similar to the C function sprintf, but - * has some important differences: - * - *

- */ - public String pushfstring(String fmt, Object[] args) { - notImplemented(); - return null; - } - - /** - * Format and push a string. [-0, +1, m] - * - *

- * Equivalent to lua_pushfstring, - * except that it receives a va_list instead of a variable - * number of arguments. - */ - public void pushvfstring(String format, Object[] args) { - notImplemented(); - } - - /** - * Test if two objects are the same object. [-0, +0, - * -] - * - *

- * Returns 1 if the two values in acceptable indices index1 - * and index2 are primitively equal (that is, without calling - * metamethods). Otherwise returns 0. Also returns 0 if any of the - * indices are non valid. - */ - public void rawequal(int index1, int index2) { - notImplemented(); - } - - /** - * Pushes a value's environment table. [-0, +1, - * -] - * - *

- * Pushes onto the stack the environment table of the value at the given - * index. - */ - public void getfenv(int index) { - LValue f = topointer(index); - pushlvalue( ((LClosure) f).env ); - } - - /** - * Set the environment for a value. [-1, +0, -] - * - *

- * 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. - */ - public boolean setfenv(int index) { - LTable t = totable(-1); - LValue f = topointer(index); - pop(1); - return f.luaSetEnv(t); - } - - /** * * Ensures that there are at least extra free stack slots in @@ -1282,125 +1093,6 @@ public class LuaState extends Lua { } } - - /** - * Closes the given Lua state. [-0, +0, -] - * - *

- * 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. On several platforms, you may not need to call this - * function, because all resources are naturally released when the host - * program ends. On the other hand, long-running programs, such as a daemon - * or a web server, might need to release states as soon as they are not - * needed, to avoid growing too large. - */ - public void close() { - stack = new LValue[20]; - base = top = 0; - } - - /** - * Concatenates the n values at the top of the stack. [-n, +1, e] - * - *

- * 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 (see §2.5.4). - */ - public void concat(int n) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - for ( int i=-n; i<0; i++ ) { - LString ls = tolstring(i); - baos.write(ls.m_bytes, ls.m_offset, ls.m_length); - } - pop(n); - pushlvalue( new LString(baos.toByteArray())); - } - - /** - * Creates a new empty table and pushes it onto the stack. [-0, +1, m] - * - *

- * 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. - */ - public void createtable(int narr, int nrec) { - pushlvalue( new LTable(narr, nrec) ); - } - - /** - * Tests if two items on the stack are equal. [-0, +0, - * e] - * - *

- * 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. - * - * - */ - public boolean equal(int index1, int index2) { - return topointer(index2).luaBinOpUnknown(Lua.OP_EQ, - topointer(index1)).toJavaBoolean(); - } - - /** - * Controls the garbage collector. [-0, +0, e] - * - *

- * This function performs several tasks, according to the value of the - * parameter what: - * - *

- */ - public void gc(int what, int data) { - notImplemented(); - } - /** * Dereference a tables field. [-0, +1, e] * @@ -1450,26 +1142,6 @@ public class LuaState extends Lua { return false; } - /** - * Dereference a table's list element. [-1, +1, - * e] - * - *

- * Pushes onto the stack the value t[k], where - * t is the value at the given valid index and k - * is the value at the top of the stack. - * - *

- * This function pops the key from the stack (putting the resulting value in - * its place). As in Lua, this function may trigger a metamethod for the - * "index" event (see §2.8). - */ - public void gettable(int index) { - LValue t = totable(index); - LValue k = poplvalue(); - pushlvalue( this.luaV_gettable(t, k) ); - } - /** * Insert the top item somewhere in the stack. [-1, +1, * -] @@ -1511,29 +1183,6 @@ public class LuaState extends Lua { return type(index) == Lua.LUA_TFUNCTION; } - /** - * Test if a value is a JavaFunction. [-0, +0, -] - * - *

- * Returns 1 if the value at the given acceptable index is a - * C function, and 0 otherwise. - * - */ - public boolean isjavafunction(int index) { - return topointer(index) instanceof LFunction; - } - - /** - * Test if a value is light user data [-0, +0, -] - * - *

- * Returns 1 if the value at the given acceptable index is a light userdata, - * and 0 otherwise. - */ - public boolean islightuserdata(int index) { - return false; - } - /** * Test if a value is nil [-0, +0, -] * @@ -1545,17 +1194,6 @@ public class LuaState extends Lua { return topointer(index).isNil(); } - /** - * Test if a value is not valid [-0, +0, -] - * - *

- * Returns 1 if the the given acceptable index is not valid (that is, it - * refers to an element outside the current stack), and 0 otherwise. - */ - public boolean isnone(int index) { - return topointer(index) == null; - } - /** * Test if a value is nil or not valid [-0, +0, * -] @@ -1636,136 +1274,6 @@ public class LuaState extends Lua { return type(index) == Lua.LUA_TUSERDATA; } - /** - * Compare two values [-0, +0, e] - * - *

- * Returns 1 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 0. Also returns 0 if - * any of the indices is non valid. - */ - public boolean lessthan(int index1, int index2) { - return topointer(index2).luaBinOpUnknown(Lua.OP_LT, - topointer(index1)).toJavaBoolean(); - } - - /** - * Create a table [-0, +1, m] - * - *

- * Creates a new empty table and pushes it onto the stack. It is equivalent - * to lua_createtable(L, 0, 0). - */ - public void newtable() { - pushlvalue( new LTable() ); - } - - /** - * Create a thread [-0, +1, m] - * - *

- * Creates a new thread, pushes it on the stack, and returns a pointer to a - * lua_State that represents this - * new thread. The new state returned by this function shares with the - * original state all global objects (such as tables), but has an - * independent execution stack. - * - * - *

- * There is no explicit function to close or to destroy a thread. Threads - * are subject to garbage collection, like any Lua object. - */ - public void newthread() { - notImplemented(); - } - - /** - * Create a userdata [-0, +1, m] - * - *

- * 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. - * - * - *

- * Userdata represent C values in Lua. A full userdata - * represents a block of memory. It is an object (like a table): you must - * create it, it can have its own metatable, and you can detect when it is - * being collected. A full userdata is only equal to itself (under raw - * equality). - * - * - *

- * When Lua collects a full userdata with a gc metamethod, - * Lua calls the metamethod and marks the userdata as finalized. When this - * userdata is collected again then Lua frees its corresponding memory. - */ - public void newuserdata(Object o) { - pushlvalue( new LUserData(o) ); - } - - /** - * Traverse to the next table item. [-1, +(2|0), - * e] - * - *

- * 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 0 (and pushes nothing). - * - * - *

- * A typical traversal looks like this: - * - *

-	 * // table is in the stack at index 't' 
-	 * lua_pushnil(L); // first key 
-	 * while (lua_next(L, t) != 0) {
-	 * 	// uses 'key' (at index -2) and 'value' (at index -1) 
-	 * 	printf("%s - %s\n", lua_typename(L, lua_type(L, -2)), lua_typename(L,
-	 * 			lua_type(L, -1)));
-	 * 	// removes 'value'; keeps 'key' for next iteration 
-	 * 	lua_pop(L, 1);
-	 * }
-	 * 
- * - *

- * While traversing a table, do not call lua_tolstring - * directly on a key, unless you know that the key is actually a string. - * Recall that lua_tolstring - * changes the value at the given index; this confuses the next - * call to lua_next. - */ - public int next(int index) { - notImplemented(); - return 0; - } - - /** - * Get the length of an object [-0, +0, -] - * - *

- * 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. - */ - public int objlen(int index) { - LValue p = topointer( index ); - switch ( p.luaGetType() ) { - case LUA_TTABLE: - case LUA_TSTRING: - return p.luaLength(); - default: - return 0; - } - } - /** * Pops n elements from the stack. [-n, * +0, -] @@ -1817,41 +1325,24 @@ public class LuaState extends Lua { } /** - * Pushes a Java function onto the stack. [-0, +1, + * Pushes a function onto the stack. [-0, +1, * m] * *

- * This function receives a pointer to a C function and pushes onto the + * This function receives an LFunction and pushes onto the * stack a Lua value of type function that, when called, - * invokes the corresponding C function. + * invokes the corresponding function. * * *

* Any function to be registered in Lua must follow the correct protocol to - * receive its parameters and return its results (see lua_CFunction). - * + * receive its parameters and return its results + * @see LFunction */ - public void pushjavafunction(LFunction f) { + public void pushfunction(LFunction f) { pushlvalue( f ); } - /** - * Pushes a light userdata onto the stack. [-0, +1, - * -] - * - * - *

- * 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. - */ - public void pushlightuserdata(Object p) { - notImplemented(); - } - /** * Push an LString onto the stack. [-0, +1, @@ -1917,16 +1408,6 @@ public class LuaState extends Lua { else pushlstring( LString.valueOf(s) ); } - - /** - * Push a thread onto the stack. [-0, +1, -] - * - * Pushes the thread represented by L onto the stack. Returns - * 1 if this thread is the main thread of its state. - */ - public void pushthread() { - notImplemented(); - } /** * Push a value from the stack onto the stack. [-0, +1, @@ -1939,18 +1420,6 @@ public class LuaState extends Lua { pushlvalue(topointer(index)); } - /** - * Do a table get without metadata calls. [-1, +1, - * -] - * - *

- * Similar to lua_gettable, but - * does a raw access (i.e., without metamethods). - */ - public void rawget(int index) { - pushlvalue( totable(index).get(poplvalue()) ); - } - /** * Do a integer-key table get without metadata calls. [-0, +1, -] @@ -1959,65 +1428,12 @@ public class LuaState extends Lua { * Pushes onto the stack the value t[n], where * t is the value at the given valid index. The access is * raw; that is, it does not invoke metamethods. + * @deprecated should get the table and do a raw get instead */ public void rawgeti(int index, int n) { pushlvalue( totable(index).get(n) ); } - /** - * Do a table set without metadata calls. [-2, +0, - * m] - * - *

- * Similar to lua_settable, but - * does a raw assignment (i.e., without metamethods). - */ - public void rawset(int index) { - LTable t = totable(index); - LValue v = poplvalue(); - LValue k = poplvalue(); - t.put(k,v); - } - - /** - * Do a integer-key table set without metadata calls. [-1, +0, m] - * - *

- * Does the equivalent of t[n] = v, where t - * is the value at the given valid index and v is the value - * at the top of the stack. - * - * - *

- * This function pops the value from the stack. The assignment is raw; that - * is, it does not invoke metamethods. - */ - public void rawseti(int index, int n) { - LTable t = totable(index); - LValue v = poplvalue(); - t.put(n,v); - } - - /** - * Register a LFunction with a specific name. [-0, +0, - * m] - * - *

- * Sets the function f as the new value of global - * name. It is defined as a macro: - * - *

-	 * 	 #define lua_register(L,n,f) \
-	 * 	 (lua_pushcfunction(L, f), lua_setglobal(L, n))
-	 * 	
-	 * 
- */ - public void register(String name, LFunction f) { - pushjavafunction(f); - setglobal(name); - } - /** * Remove an element from the stack. [-1, +0, -] * @@ -2045,32 +1461,6 @@ public class LuaState extends Lua { stack[ai] = poplvalue(); } - /** - * Starts and resumes a coroutine in a given thread. [-?, - * +?, -] - * - * - *

- * To start a coroutine, you first create a new thread (see lua_newthread); then you push - * onto its stack the main function plus any arguments; then you call lua_resume, with - * narg being the number of arguments. This call returns when - * the coroutine suspends or finishes its execution. When it returns, the - * stack contains all values passed to lua_yield, - * or all values returned by the body function. lua_resume - * returns LUA_YIELD if the - * coroutine yields, 0 if the coroutine finishes its execution without - * errors, or an error code in case of errors (see lua_pcall). - * In case of errors, the stack is not unwound, so you can use the debug API - * over it. The error message is on the top of the stack. To restart a - * coroutine, you put on its stack only the values to be passed as results - * from yield, and then call lua_resume. - */ - public void resume(int narg) { - notImplemented(); - } - /** * Set the value of a table field. [-1, +0, e] * @@ -2107,75 +1497,6 @@ public class LuaState extends Lua { this.luaV_settable(_G, new LString(name), poplvalue()); } - /** - * Set the metatable of a value. [-1, +0, -] - * - *

- * Pops a table from the stack and sets it as the new metatable for the - * value at the given acceptable index. - */ - public void setmetatable(int index) { - LTable t = totable(index); - LValue v = poplvalue(); - LValue n = t.luaSetMetatable(v); - if ( n != null ) { - pushlvalue(n); - replace(index); - } - } - - /** - * Set the value of a table for a key. [-2, +0, - * e] - * - *

- * 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 (see §2.8). - */ - public void settable(int index) { - LTable t = totable(index); - LValue v = poplvalue(); - LValue k = poplvalue(); - this.luaV_settable(t, k, v); - } - - /** - * Returns the status of the thread L. [-0, +0, -] - * - * - * - *

- * The status can be 0 for a normal thread, an error code if the thread - * finished its execution with an error, or LUA_YIELD - * if the thread is suspended. - * - */ - public void status() { - notImplemented(); - } - - /** - * Get a thread value from the stack. [-0, +0, -] - * - *

- * Converts the value at the given acceptable index to a Lua thread - * (represented as lua_State*). This value must be a thread; - * otherwise, the function returns NULL. - */ - public LuaState tothread(int index) { - notImplemented(); - return null; - } - /** * Get a value as a boolean. [-0, +0, -] * @@ -2214,22 +1535,23 @@ public class LuaState extends Lua { } /** - * Get a value as a JavaFunction. + * Get a value as a LFunction. *


- *

lua_tocfunction

+ *

tofunction

*

* [-0, +0, -] * *

-	 * lua_CFunction lua_tocfunction (lua_State *L, int index);
+	 * LFunction tofunction (lua_State *L, int index);
 	 * 
* *

* Converts a value at the given acceptable index to a C function. That - * value must be a C function; otherwise, returns NULL. + * value must be a function; otherwise, returns NULL. */ - public LFunction tojavafunction(int index) { - return (LFunction) topointer(index); + public LValue tofunction(int index) { + LValue v = topointer(index); + return v.isFunction()? v: LNil.NIL; } /** @@ -2434,31 +1756,6 @@ public class LuaState extends Lua { ss.top += n; } } - - /** - * Yields a coroutine. [-?, +?, -] - * - * - *

- * This function should only be called as the return expression of a - * C function, as follows: - * - *

-	 * return lua_yield(L, nresults);
-	 * 
- * - *

- * When a C function calls lua_yield - * in that way, the running coroutine suspends its execution, and the call - * to lua_resume that started - * this coroutine returns. The parameter nresults is the - * number of values from the stack that are passed as results to lua_resume. - * - */ - public void yield(int nresults) { - notImplemented(); - } // ============================= conversion to and from Java boxed types ==================== @@ -2558,7 +1855,7 @@ public class LuaState extends Lua { if ( o == null ) pushnil(); else - newuserdata( o ); + pushlvalue( new LUserData(o) ); } diff --git a/src/j2se/org/luaj/lib/j2se/LuajavaLib.java b/src/j2se/org/luaj/lib/j2se/LuajavaLib.java index 9d0b1fdf..bac9d6a0 100644 --- a/src/j2se/org/luaj/lib/j2se/LuajavaLib.java +++ b/src/j2se/org/luaj/lib/j2se/LuajavaLib.java @@ -126,7 +126,7 @@ public final class LuajavaLib extends LFunction { final int ninterfaces = Math.max(0,vm.gettop()-2); if ( ninterfaces <= 0 ) throw new LuaErrorException("no interfaces"); - final LValue function = vm.tojavafunction(-1); + final LValue function = vm.tofunction(-1); try { // get the interfaces final Class[] ifaces = new Class[ninterfaces]; diff --git a/src/test/java/org/luaj/vm/LuaStateTest.java b/src/test/java/org/luaj/vm/LuaStateTest.java index 8fba15fd..3cc33910 100644 --- a/src/test/java/org/luaj/vm/LuaStateTest.java +++ b/src/test/java/org/luaj/vm/LuaStateTest.java @@ -25,7 +25,7 @@ public class LuaStateTest extends TestCase { public void testFuncCall() throws IOException { vm.pushstring("bogus"); - vm.pushjavafunction(new SomeFunc( "arg" )); + vm.pushfunction(new SomeFunc( "arg" )); vm.pushstring("arg"); vm.call(1, 1); assertEquals( 2, vm.gettop() ); @@ -36,7 +36,7 @@ public class LuaStateTest extends TestCase { public void testFuncCall2() throws IOException { vm.pushstring("bogus"); - vm.pushjavafunction(new SomeFunc( "nil" )); + vm.pushfunction(new SomeFunc( "nil" )); vm.call(0, 1); assertEquals( 2, vm.gettop() ); assertEquals( "bogus", vm.tostring(1) ); diff --git a/version.properties b/version.properties index e0e4f2bb..05bc6ae8 100644 --- a/version.properties +++ b/version.properties @@ -1 +1 @@ -version: 0.42 +version: 0.43