any luaToAny(lua_State* plua_state, int index) { any a; LuaType type = luaGetType(plua_state, index); switch (type) { case LuaBoolean: a = luaToBoolean(plua_state, index); break; case LuaLightUserData: a = luaToLightUserData(plua_state, index); break; case LuaNumber: a = luaIsInteger(plua_state, index) ? luaToInteger(plua_state, index) : luaToDouble(plua_state, index); break; case LuaString: a = luaToString(plua_state, index); break; default: throw Exception(strFormat("value type (%d) cannot convert to any type", type)); break; } return a; }
/* ** Used to create a Python Tuple from the parameters of a Lua function and/or ** to create a Python Tuple from the return values of a Lua function. ** Because the multiple return values are tied together as a Tuple, Python never ** sees Lua functions returning more than 1 value: the Tuple. All values are inside it. */ static PyObject *luaStack_to_pythonTuple(lua_State *L, int stack_start, int nelements) { int n; PyObject *tuple; int item; VALUE_TYPES type; BOOLEAN succeed; tuple = PyTuple_New(nelements); succeed = True; /* values: Lua --> Python */ for (n = 0; n < nelements; n++) { item = n+stack_start; type = luaGetType(L, item); switch (type) { case (Nothing): PyTuple_SetItem(tuple, n, Py_None); break; case (Number): PyTuple_SetItem(tuple, n, PyFloat_FromDouble(lua_tonumber(L, item))); break; case (String): PyTuple_SetItem(tuple, n, PyString_FromString(lua_tostring(L, item))); break; case (Function): PyTuple_SetItem(tuple, n, Py_None); PyErr_SetString(PyExc_Exception, "LuaPy: passing Lua functions to Python is not implemented. Sorry."); succeed = False; break; case (Pointer): PyTuple_SetItem(tuple, n, PyCObject_FromVoidPtr(lua_touserdata(L, item), NULL)); break; case (Container): { PyObject *function; if ((lua_tag(L, item) == luaGetPythonFunctionTag(L))) { /* lua table that represents a python function --> python function */ function = luaGetPythonFunctionFromLuaTable(L, item); /* ** FALTA IMPLEMENTAR O DECREF CORRESPONDENTE NO GC DE LUA! ** */ Py_INCREF(function); PyTuple_SetItem(tuple, n, function); } else { PyTuple_SetItem(tuple, n, Py_None); PyErr_SetString(PyExc_Exception, "LuaPy: passing Lua tables to Python (as a tuples) is not implemented. Sorry."); succeed = False; } break; } default: PyTuple_SetItem(tuple, n, Py_None); PyErr_SetString(PyExc_Exception, "LuaPy: can't translate an unknown type of parameter or return value from Lua to Python"); succeed = False; } } if (succeed) { return tuple; } else { return NULL; } }