├── .gitignore ├── src ├── bus.h ├── pending_call.h ├── error.h ├── message.h ├── ldbus.h ├── error.c ├── Makefile ├── watch.h ├── timeout.h ├── message_iter.h ├── message │ └── variant.lua ├── connection.h ├── pending_call.c ├── timeout.c ├── watch.c ├── bus.c ├── ldbus.c ├── message_iter.c ├── message.c └── connection.c ├── vendor └── compat-5.3 │ ├── .gitignore │ ├── tests │ ├── test-bit32.lua │ └── testmod.c │ ├── rockspecs │ ├── bit32-5.3.5-1.rockspec │ ├── bit32-scm-1.rockspec │ ├── compat53-0.1-1.rockspec │ ├── compat53-0.8-1.rockspec │ ├── compat53-scm-0.rockspec │ ├── compat53-0.2-1.rockspec │ ├── compat53-0.3-1.rockspec │ ├── compat53-0.4-1.rockspec │ ├── compat53-0.5-1.rockspec │ └── compat53-0.7-1.rockspec │ ├── LICENSE │ ├── .travis.yml │ ├── lprefix.h │ ├── lbitlib.c │ ├── lutf8lib.c │ ├── README.md │ ├── c-api │ └── compat-5.3.h │ ├── compat53 │ └── init.lua │ └── ltablib.c ├── LICENSE ├── ldbus-scm-0.rockspec ├── example.lua └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | *.so 2 | *.o 3 | *.rock 4 | -------------------------------------------------------------------------------- /src/bus.h: -------------------------------------------------------------------------------- 1 | #ifndef LDBUS_BUS_H 2 | #define LDBUS_BUS_H 3 | 4 | #include 5 | 6 | int luaopen_ldbus_bus(lua_State *L); 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /vendor/compat-5.3/.gitignore: -------------------------------------------------------------------------------- 1 | # generated files 2 | *.so 3 | *.dll 4 | *.o 5 | *.obj 6 | HISTO 7 | 8 | # vim temporaries 9 | .*.swp 10 | 11 | -------------------------------------------------------------------------------- /vendor/compat-5.3/tests/test-bit32.lua: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env lua 2 | 3 | local bit32 = require("bit32") 4 | 5 | 6 | assert(bit32.bnot(0) == 2^32-1) 7 | assert(bit32.bnot(-1) == 0) 8 | assert(bit32.band(1, 3, 5) == 1) 9 | assert(bit32.bor(1, 3, 5) == 7) 10 | 11 | -------------------------------------------------------------------------------- /src/pending_call.h: -------------------------------------------------------------------------------- 1 | #ifndef LDBUS_PENDING_CALL_H 2 | #define LDBUS_PENDING_CALL_H 3 | 4 | #include 5 | 6 | #include 7 | 8 | #include "ldbus.h" 9 | 10 | LDBUS_INTERNAL void push_DBusPendingCall(lua_State *L, DBusPendingCall*); 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /src/error.h: -------------------------------------------------------------------------------- 1 | #ifndef LDBUS_ERROR_H 2 | #define LDBUS_ERROR_H 3 | 4 | #include 5 | 6 | #include 7 | 8 | #include "ldbus.h" 9 | 10 | #define DBUS_ERROR_METATABLE "ldbus_DBusError" 11 | 12 | #define check_DBusError(L, arg) ((DBusError*)luaL_checkudata((L), (arg), DBUS_ERROR_METATABLE)) 13 | 14 | LDBUS_INTERNAL DBusError* new_DBusError(lua_State *L); 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /src/message.h: -------------------------------------------------------------------------------- 1 | #ifndef LDBUS_MESSAGE_H 2 | #define LDBUS_MESSAGE_H 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | #include "ldbus.h" 10 | 11 | #define DBUS_MESSAGE_METATABLE "ldbus_DBusMessage" 12 | 13 | #define check_DBusMessage(L, arg) (*(DBusMessage **)luaL_checkudata((L), (arg), DBUS_MESSAGE_METATABLE)) 14 | 15 | 16 | LDBUS_INTERNAL void push_DBusMessage(lua_State *L, DBusMessage * message); 17 | int luaopen_ldbus_message(lua_State *L); 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /src/ldbus.h: -------------------------------------------------------------------------------- 1 | #ifndef LDBUS_H 2 | #define LDBUS_H 3 | 4 | #include 5 | 6 | #define UNUSED(x) (void)(x) 7 | #define LDBUS_INTERNAL __attribute__ ((visibility ("internal"))) 8 | #define LDBUS_NO_MEMORY "no memory" 9 | 10 | enum ldbus_callback_indexes { 11 | DBUS_LUA_FUNC_FUNC = 1, 12 | DBUS_LUA_FUNC_THREAD = 2, 13 | /* for watch and timeout */ 14 | DBUS_LUA_FUNC_ADD = 1, 15 | DBUS_LUA_FUNC_REMOVE = 3, 16 | DBUS_LUA_FUNC_TOGGLE = 4 17 | }; 18 | 19 | LDBUS_INTERNAL int tostring(lua_State *); 20 | 21 | int luaopen_ldbus(lua_State *); 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /src/error.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | 6 | #include "ldbus.h" 7 | #include "error.h" 8 | 9 | static int ldbus_error_free(lua_State *L) { 10 | DBusError *error = check_DBusError(L, 1); 11 | dbus_error_free(error); 12 | return 0; 13 | } 14 | 15 | /* Creates a new DBusError, and pushes it onto the lua stack */ 16 | LDBUS_INTERNAL DBusError* new_DBusError(lua_State *L) { 17 | DBusError *res = lua_newuserdata(L, sizeof(DBusError)); 18 | if (luaL_newmetatable(L, DBUS_ERROR_METATABLE)) { 19 | lua_newtable(L); 20 | lua_pushcfunction(L, ldbus_error_free); 21 | lua_setfield(L, -2, "__gc"); 22 | } 23 | dbus_error_init(res); 24 | return res; 25 | } 26 | -------------------------------------------------------------------------------- /src/Makefile: -------------------------------------------------------------------------------- 1 | LUA_LIBDIR = /usr/local/lib/lua/5.3 2 | PKG_CONFIG = pkg-config 3 | LUA_PKGNAME = lua5.3 4 | 5 | CFLAGS += -Wall -Wextra --pedantic -Wno-long-long 6 | CFLAGS += `$(PKG_CONFIG) --cflags $(LUA_PKGNAME) dbus-1` 7 | CFLAGS += -fPIC 8 | CFLAGS += -I ../vendor/compat-5.3/c-api 9 | 10 | LIBS = `$(PKG_CONFIG) --libs dbus-1` 11 | 12 | OBJS = ldbus.o error.o pending_call.o connection.o bus.o message.o message_iter.o timeout.o watch.o ../vendor/compat-5.3/c-api/compat-5.3.o 13 | 14 | all: ldbus.so 15 | 16 | ldbus.so: $(OBJS) 17 | $(CC) -o ldbus.so -shared $(CFLAGS) $(LDFLAGS) $^ $(LIBS) 18 | 19 | clean: 20 | rm -f -- ldbus.so $(OBJS) 21 | 22 | install: ldbus.so 23 | mkdir -p $(DESTDIR)$(LUA_LIBDIR) 24 | cp ldbus.so $(DESTDIR)$(LUA_LIBDIR) 25 | -------------------------------------------------------------------------------- /src/watch.h: -------------------------------------------------------------------------------- 1 | #ifndef LDBUS_WATCH_H 2 | #define LDBUS_WATCH_H 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | #include "ldbus.h" 10 | 11 | #define DBUS_WATCH_METATABLE "ldbus_DBusWatch" 12 | 13 | #define check_DBusWatch(L, arg) (*(DBusWatch **)luaL_checkudata((L), (arg), DBUS_WATCH_METATABLE)) 14 | 15 | LDBUS_INTERNAL void push_DBusWatch(lua_State *L, DBusWatch *watch); 16 | LDBUS_INTERNAL dbus_bool_t ldbus_watch_add_function(DBusWatch *watch, void *data); 17 | LDBUS_INTERNAL void ldbus_watch_remove_function(DBusWatch *watch, void *data); 18 | LDBUS_INTERNAL void ldbus_watch_toggled_function(DBusWatch *watch, void *data); 19 | LDBUS_INTERNAL void ldbus_watch_free_data_function(void *data); 20 | 21 | int luaopen_ldbus_watch(lua_State *L); 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /src/timeout.h: -------------------------------------------------------------------------------- 1 | #ifndef LDBUS_TIMEOUT_H 2 | #define LDBUS_TIMEOUT_H 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | #include "ldbus.h" 10 | 11 | #define DBUS_TIMEOUT_METATABLE "ldbus_DbusTimeout" 12 | 13 | #define check_DBusTimeout(L, arg) (*(DBusTimeout **)luaL_checkudata((L), (arg), DBUS_TIMEOUT_METATABLE)) 14 | 15 | LDBUS_INTERNAL void push_DBusTimeout(lua_State *L, DBusTimeout *timeout); 16 | LDBUS_INTERNAL dbus_bool_t ldbus_timeout_add_function(DBusTimeout *timeout, void *data); 17 | LDBUS_INTERNAL void ldbus_timeout_remove_function(DBusTimeout *timeout, void *data); 18 | LDBUS_INTERNAL void ldbus_timeout_toggled_function(DBusTimeout *timeout, void *data); 19 | LDBUS_INTERNAL void ldbus_timeout_free_data_function(void *data); 20 | 21 | int lua_open_ldbus_timeout(lua_State *L); 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /vendor/compat-5.3/rockspecs/bit32-5.3.5-1.rockspec: -------------------------------------------------------------------------------- 1 | package = "bit32" 2 | version = "5.3.5-1" 3 | source = { 4 | url = "https://github.com/keplerproject/lua-compat-5.3/archive/v0.9.zip", 5 | dir = "lua-compat-5.3-0.9", 6 | } 7 | description = { 8 | summary = "Lua 5.2 bit manipulation library", 9 | detailed = [[ 10 | bit32 is the native Lua 5.2 bit manipulation library, in the version 11 | from Lua 5.3; it is compatible with Lua 5.1, 5.2 and 5.3. 12 | ]], 13 | homepage = "http://www.lua.org/manual/5.2/manual.html#6.7", 14 | license = "MIT" 15 | } 16 | dependencies = { 17 | "lua >= 5.1, < 5.5" 18 | } 19 | build = { 20 | type = "builtin", 21 | modules = { 22 | bit32 = { 23 | sources = { "lbitlib.c" }, 24 | defines = { "LUA_COMPAT_BITLIB" }, 25 | incdirs = { "c-api" }, 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /vendor/compat-5.3/rockspecs/bit32-scm-1.rockspec: -------------------------------------------------------------------------------- 1 | package = "bit32" 2 | version = "scm-1" 3 | source = { 4 | url = "https://github.com/keplerproject/lua-compat-5.3/archive/master.zip", 5 | dir = "lua-compat-5.3-master", 6 | } 7 | description = { 8 | summary = "Lua 5.2 bit manipulation library", 9 | detailed = [[ 10 | bit32 is the native Lua 5.2 bit manipulation library, in the version 11 | from Lua 5.3; it is compatible with Lua 5.1, 5.2 and 5.3. 12 | ]], 13 | homepage = "http://www.lua.org/manual/5.2/manual.html#6.7", 14 | license = "MIT" 15 | } 16 | dependencies = { 17 | "lua >= 5.1, < 5.5" 18 | } 19 | build = { 20 | type = "builtin", 21 | modules = { 22 | bit32 = { 23 | sources = { "lbitlib.c" }, 24 | defines = { "LUA_COMPAT_BITLIB" }, 25 | incdirs = { "c-api" }, 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/message_iter.h: -------------------------------------------------------------------------------- 1 | #ifndef LDBUS_MESSAGE_ITER_H 2 | #define LDBUS_MESSAGE_ITER_H 3 | 4 | #include 5 | 6 | #include "ldbus.h" 7 | 8 | typedef struct { 9 | /* This must be the first member, thus for binding functions that doesn't 10 | care about the underlying DBusMessage, this structure could be safely 11 | referred with a DBusMessageIter pointer. 12 | */ 13 | DBusMessageIter iter; 14 | /* The message which this iterator refers to. NULL if this iterator hasn't 15 | been initialized with a message. 16 | We need to unref it when the iterator is garbagecollected. 17 | */ 18 | DBusMessage *message; 19 | } lDBusMessageIter; 20 | 21 | #define DBUS_MESSAGE_ITER_METATABLE "ldbus_DBusMessageIter" 22 | 23 | LDBUS_INTERNAL int push_DBusMessageIter(lua_State *L); 24 | LDBUS_INTERNAL void load_dbus_message_iter(lua_State *L); 25 | LDBUS_INTERNAL void unref_ldbus_message_iter(lDBusMessageIter *iter); 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /src/message/variant.lua: -------------------------------------------------------------------------------- 1 | -- Simple abstraction for variants 2 | 3 | local variant_methods = {} 4 | local variant_mt = { __index = variant_methods; } 5 | 6 | function variant_methods:get_signature() 7 | return self._signature 8 | end 9 | 10 | function variant_methods:set_signature(new) 11 | local old = self._signature 12 | self._signature = new; 13 | return old 14 | end 15 | 16 | function variant_methods:get_value() 17 | return self._value 18 | end 19 | 20 | function variant_methods:set_value(new) 21 | local old = self._value 22 | self._value = new 23 | return old 24 | end 25 | 26 | local function new_variant(signature, value) 27 | local self = setmetatable({ 28 | _signature = nil; 29 | _value = nil; 30 | }, variant_mt) 31 | self:set_signature(signature) 32 | self:set_value(value) 33 | return self; 34 | end 35 | 36 | local function is_variant(ob) 37 | return getmetatable(ob) == variant_mt; 38 | end 39 | 40 | return { 41 | new = new_variant; 42 | is = is_variant; 43 | } 44 | -------------------------------------------------------------------------------- /src/connection.h: -------------------------------------------------------------------------------- 1 | #ifndef LDBUS_CONNECTION_H 2 | #define LDBUS_CONNECTION_H 3 | 4 | #include 5 | 6 | #include 7 | 8 | #include 9 | 10 | #include "ldbus.h" 11 | 12 | /* What we store in a DBusConnection userdata */ 13 | typedef struct { 14 | /* First field should be the pointer to DBusConnection, 15 | so we can cast this struct to a 'DBusConnection*' 16 | */ 17 | DBusConnection *connection; 18 | /* Whether this connection is 'shared' 19 | If so, we need to call `dbus_connection_close` on collection 20 | */ 21 | bool close; 22 | } lDBusConnection; 23 | 24 | #define DBUS_CONNECTION_METATABLE "ldbus_DBusConnection" 25 | 26 | #define check_lDBusConnection(L, arg) ((lDBusConnection *)luaL_checkudata((L), (arg), DBUS_CONNECTION_METATABLE)) 27 | #define check_DBusConnection(L, arg) (check_lDBusConnection((L), (arg))->connection) 28 | 29 | LDBUS_INTERNAL void push_DBusConnection(lua_State *L, DBusConnection *connection, bool close); 30 | 31 | int luaopen_ldbus_connection(lua_State *L); 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /vendor/compat-5.3/rockspecs/compat53-0.1-1.rockspec: -------------------------------------------------------------------------------- 1 | package = "compat53" 2 | version = "0.1-1" 3 | source = { 4 | url = "https://github.com/keplerproject/lua-compat-5.3/archive/v0.1.zip", 5 | dir = "lua-compat-5.3-0.1", 6 | } 7 | description = { 8 | summary = "Compatibility module providing Lua-5.3-style APIs for Lua 5.2 and 5.1", 9 | detailed = [[ 10 | This is a small module that aims to make it easier to write Lua 11 | code in a Lua-5.3-style that runs on Lua 5.3, 5.2, and 5.1. 12 | It does *not* make Lua 5.2 (or even 5.1) entirely compatible 13 | with Lua 5.3, but it brings the API closer to that of Lua 5.3. 14 | ]], 15 | homepage = "https://github.com/keplerproject/lua-compat-5.3", 16 | license = "MIT" 17 | } 18 | dependencies = { 19 | "lua >= 5.1, < 5.4", 20 | --"struct" -- make Roberto's struct module optional 21 | } 22 | build = { 23 | type = "builtin", 24 | modules = { 25 | ["compat53"] = "compat53.lua", 26 | ["compat53.utf8"] = "lutf8lib.c", 27 | ["compat53.table"] = "ltablib.c", 28 | ["compat53.string"] = "lstrlib.c", 29 | } 30 | } 31 | 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2010-2014 Daurnimator 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /vendor/compat-5.3/LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Kepler Project. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /vendor/compat-5.3/rockspecs/compat53-0.8-1.rockspec: -------------------------------------------------------------------------------- 1 | package = "compat53" 2 | version = "0.8-1" 3 | source = { 4 | url = "https://github.com/keplerproject/lua-compat-5.3/archive/v0.8.zip", 5 | dir = "lua-compat-5.3-0.8", 6 | } 7 | description = { 8 | summary = "Compatibility module providing Lua-5.3-style APIs for Lua 5.2 and 5.1", 9 | detailed = [[ 10 | This is a small module that aims to make it easier to write Lua 11 | code in a Lua-5.3-style that runs on Lua 5.1+. 12 | It does *not* make Lua 5.2 (or even 5.1) entirely compatible 13 | with Lua 5.3, but it brings the API closer to that of Lua 5.3. 14 | ]], 15 | homepage = "https://github.com/keplerproject/lua-compat-5.3", 16 | license = "MIT" 17 | } 18 | dependencies = { 19 | "lua >= 5.1, < 5.5", 20 | --"struct" -- make Roberto's struct module optional 21 | } 22 | build = { 23 | type = "builtin", 24 | modules = { 25 | ["compat53.init"] = "compat53/init.lua", 26 | ["compat53.module"] = "compat53/module.lua", 27 | ["compat53.utf8"] = "lutf8lib.c", 28 | ["compat53.table"] = "ltablib.c", 29 | ["compat53.string"] = "lstrlib.c", 30 | } 31 | } 32 | 33 | -------------------------------------------------------------------------------- /vendor/compat-5.3/rockspecs/compat53-scm-0.rockspec: -------------------------------------------------------------------------------- 1 | package = "compat53" 2 | version = "scm-0" 3 | source = { 4 | url = "https://github.com/keplerproject/lua-compat-5.3/archive/master.zip", 5 | dir = "lua-compat-5.3-master", 6 | } 7 | description = { 8 | summary = "Compatibility module providing Lua-5.3-style APIs for Lua 5.2 and 5.1", 9 | detailed = [[ 10 | This is a small module that aims to make it easier to write Lua 11 | code in a Lua-5.3-style that runs on Lua 5.1+. 12 | It does *not* make Lua 5.2 (or even 5.1) entirely compatible 13 | with Lua 5.3, but it brings the API closer to that of Lua 5.3. 14 | ]], 15 | homepage = "https://github.com/keplerproject/lua-compat-5.3", 16 | license = "MIT" 17 | } 18 | dependencies = { 19 | "lua >= 5.1, < 5.5", 20 | --"struct" -- make Roberto's struct module optional 21 | } 22 | build = { 23 | type = "builtin", 24 | modules = { 25 | ["compat53.init"] = "compat53/init.lua", 26 | ["compat53.module"] = "compat53/module.lua", 27 | ["compat53.utf8"] = "lutf8lib.c", 28 | ["compat53.table"] = "ltablib.c", 29 | ["compat53.string"] = "lstrlib.c", 30 | } 31 | } 32 | 33 | -------------------------------------------------------------------------------- /vendor/compat-5.3/rockspecs/compat53-0.2-1.rockspec: -------------------------------------------------------------------------------- 1 | package = "compat53" 2 | version = "0.2-1" 3 | source = { 4 | url = "https://github.com/keplerproject/lua-compat-5.3/archive/v0.2.zip", 5 | dir = "lua-compat-5.3-0.2", 6 | } 7 | description = { 8 | summary = "Compatibility module providing Lua-5.3-style APIs for Lua 5.2 and 5.1", 9 | detailed = [[ 10 | This is a small module that aims to make it easier to write Lua 11 | code in a Lua-5.3-style that runs on Lua 5.3, 5.2, and 5.1. 12 | It does *not* make Lua 5.2 (or even 5.1) entirely compatible 13 | with Lua 5.3, but it brings the API closer to that of Lua 5.3. 14 | ]], 15 | homepage = "https://github.com/keplerproject/lua-compat-5.3", 16 | license = "MIT" 17 | } 18 | dependencies = { 19 | "lua >= 5.1, < 5.4", 20 | --"struct" -- make Roberto's struct module optional 21 | } 22 | build = { 23 | type = "builtin", 24 | modules = { 25 | ["compat53.init"] = "compat53/init.lua", 26 | ["compat53.module"] = "compat53/module.lua", 27 | ["compat53.utf8"] = "lutf8lib.c", 28 | ["compat53.table"] = "ltablib.c", 29 | ["compat53.string"] = "lstrlib.c", 30 | } 31 | } 32 | 33 | -------------------------------------------------------------------------------- /vendor/compat-5.3/rockspecs/compat53-0.3-1.rockspec: -------------------------------------------------------------------------------- 1 | package = "compat53" 2 | version = "0.3-1" 3 | source = { 4 | url = "https://github.com/keplerproject/lua-compat-5.3/archive/v0.3.zip", 5 | dir = "lua-compat-5.3-0.3", 6 | } 7 | description = { 8 | summary = "Compatibility module providing Lua-5.3-style APIs for Lua 5.2 and 5.1", 9 | detailed = [[ 10 | This is a small module that aims to make it easier to write Lua 11 | code in a Lua-5.3-style that runs on Lua 5.3, 5.2, and 5.1. 12 | It does *not* make Lua 5.2 (or even 5.1) entirely compatible 13 | with Lua 5.3, but it brings the API closer to that of Lua 5.3. 14 | ]], 15 | homepage = "https://github.com/keplerproject/lua-compat-5.3", 16 | license = "MIT" 17 | } 18 | dependencies = { 19 | "lua >= 5.1, < 5.4", 20 | --"struct" -- make Roberto's struct module optional 21 | } 22 | build = { 23 | type = "builtin", 24 | modules = { 25 | ["compat53.init"] = "compat53/init.lua", 26 | ["compat53.module"] = "compat53/module.lua", 27 | ["compat53.utf8"] = "lutf8lib.c", 28 | ["compat53.table"] = "ltablib.c", 29 | ["compat53.string"] = "lstrlib.c", 30 | } 31 | } 32 | 33 | -------------------------------------------------------------------------------- /vendor/compat-5.3/rockspecs/compat53-0.4-1.rockspec: -------------------------------------------------------------------------------- 1 | package = "compat53" 2 | version = "0.4-1" 3 | source = { 4 | url = "https://github.com/keplerproject/lua-compat-5.3/archive/v0.4.zip", 5 | dir = "lua-compat-5.3-0.4", 6 | } 7 | description = { 8 | summary = "Compatibility module providing Lua-5.3-style APIs for Lua 5.2 and 5.1", 9 | detailed = [[ 10 | This is a small module that aims to make it easier to write Lua 11 | code in a Lua-5.3-style that runs on Lua 5.3, 5.2, and 5.1. 12 | It does *not* make Lua 5.2 (or even 5.1) entirely compatible 13 | with Lua 5.3, but it brings the API closer to that of Lua 5.3. 14 | ]], 15 | homepage = "https://github.com/keplerproject/lua-compat-5.3", 16 | license = "MIT" 17 | } 18 | dependencies = { 19 | "lua >= 5.1, < 5.4", 20 | --"struct" -- make Roberto's struct module optional 21 | } 22 | build = { 23 | type = "builtin", 24 | modules = { 25 | ["compat53.init"] = "compat53/init.lua", 26 | ["compat53.module"] = "compat53/module.lua", 27 | ["compat53.utf8"] = "lutf8lib.c", 28 | ["compat53.table"] = "ltablib.c", 29 | ["compat53.string"] = "lstrlib.c", 30 | } 31 | } 32 | 33 | -------------------------------------------------------------------------------- /vendor/compat-5.3/rockspecs/compat53-0.5-1.rockspec: -------------------------------------------------------------------------------- 1 | package = "compat53" 2 | version = "0.5-1" 3 | source = { 4 | url = "https://github.com/keplerproject/lua-compat-5.3/archive/v0.5.zip", 5 | dir = "lua-compat-5.3-0.5", 6 | } 7 | description = { 8 | summary = "Compatibility module providing Lua-5.3-style APIs for Lua 5.2 and 5.1", 9 | detailed = [[ 10 | This is a small module that aims to make it easier to write Lua 11 | code in a Lua-5.3-style that runs on Lua 5.3, 5.2, and 5.1. 12 | It does *not* make Lua 5.2 (or even 5.1) entirely compatible 13 | with Lua 5.3, but it brings the API closer to that of Lua 5.3. 14 | ]], 15 | homepage = "https://github.com/keplerproject/lua-compat-5.3", 16 | license = "MIT" 17 | } 18 | dependencies = { 19 | "lua >= 5.1, < 5.4", 20 | --"struct" -- make Roberto's struct module optional 21 | } 22 | build = { 23 | type = "builtin", 24 | modules = { 25 | ["compat53.init"] = "compat53/init.lua", 26 | ["compat53.module"] = "compat53/module.lua", 27 | ["compat53.utf8"] = "lutf8lib.c", 28 | ["compat53.table"] = "ltablib.c", 29 | ["compat53.string"] = "lstrlib.c", 30 | } 31 | } 32 | 33 | -------------------------------------------------------------------------------- /vendor/compat-5.3/rockspecs/compat53-0.7-1.rockspec: -------------------------------------------------------------------------------- 1 | package = "compat53" 2 | version = "0.7-1" 3 | source = { 4 | url = "https://github.com/keplerproject/lua-compat-5.3/archive/v0.7.zip", 5 | dir = "lua-compat-5.3-0.7", 6 | } 7 | description = { 8 | summary = "Compatibility module providing Lua-5.3-style APIs for Lua 5.2 and 5.1", 9 | detailed = [[ 10 | This is a small module that aims to make it easier to write Lua 11 | code in a Lua-5.3-style that runs on Lua 5.3, 5.2, and 5.1. 12 | It does *not* make Lua 5.2 (or even 5.1) entirely compatible 13 | with Lua 5.3, but it brings the API closer to that of Lua 5.3. 14 | ]], 15 | homepage = "https://github.com/keplerproject/lua-compat-5.3", 16 | license = "MIT" 17 | } 18 | dependencies = { 19 | "lua >= 5.1, < 5.4", 20 | --"struct" -- make Roberto's struct module optional 21 | } 22 | build = { 23 | type = "builtin", 24 | modules = { 25 | ["compat53.init"] = "compat53/init.lua", 26 | ["compat53.module"] = "compat53/module.lua", 27 | ["compat53.utf8"] = "lutf8lib.c", 28 | ["compat53.table"] = "ltablib.c", 29 | ["compat53.string"] = "lstrlib.c", 30 | } 31 | } 32 | 33 | -------------------------------------------------------------------------------- /ldbus-scm-0.rockspec: -------------------------------------------------------------------------------- 1 | package = "ldbus" 2 | version = "scm-0" 3 | 4 | source = { 5 | url = "git://github.com/daurnimator/ldbus.git"; 6 | } 7 | 8 | description = { 9 | summary = "A Lua library to access dbus."; 10 | homepage = "https://github.com/daurnimator/ldbus"; 11 | license = "MIT/X11"; 12 | } 13 | 14 | dependencies = { 15 | "lua >= 5.1, < 5.5"; 16 | } 17 | 18 | external_dependencies = { 19 | DBUS = { 20 | header = "dbus/dbus.h"; 21 | library = "dbus-1"; 22 | }; 23 | DBUS_ARCH = { 24 | header = "dbus/dbus-arch-deps.h"; 25 | }; 26 | } 27 | 28 | build = { 29 | type = "builtin"; 30 | modules = { 31 | ldbus = { 32 | sources = { 33 | "src/ldbus.c"; 34 | "src/error.c"; 35 | "src/bus.c"; 36 | "src/connection.c"; 37 | "src/message.c"; 38 | "src/message_iter.c"; 39 | "src/pending_call.c"; 40 | "src/timeout.c"; 41 | "src/watch.c"; 42 | "vendor/compat-5.3/c-api/compat-5.3.c"; 43 | }; 44 | libraries = { 45 | "dbus-1"; 46 | }; 47 | incdirs = { 48 | "$(DBUS_INCDIR)"; 49 | "$(DBUS_ARCH_INCDIR)"; 50 | "vendor/compat-5.3/c-api/"; 51 | }; 52 | libdirs = { 53 | "$(DBUS_LIBDIR)"; 54 | }; 55 | }; 56 | ["ldbus.message.variant"] = "src/message/variant.lua"; 57 | }; 58 | } 59 | -------------------------------------------------------------------------------- /vendor/compat-5.3/.travis.yml: -------------------------------------------------------------------------------- 1 | language: c 2 | compiler: gcc 3 | 4 | sudo: false 5 | 6 | env: 7 | - LUA="lua=5.1" 8 | - LUA="lua=5.1" EXTERNAL=true 9 | - LUA="lua=5.1" COMPILER="g++" 10 | - LUA="lua=5.1" EXTERNAL=true COMPILER="g++" 11 | - LUA="luajit=@v2.1 --compat=none" 12 | - LUA="luajit=@v2.1 --compat=none" EXTERNAL=true 13 | - LUA="luajit=@v2.1 --compat=all" 14 | - LUA="luajit=@v2.1 --compat=all" EXTERNAL=true 15 | - LUA="lua=5.2" 16 | - LUA="lua=5.2" EXTERNAL=true 17 | - LUA="lua=5.2" COMPILER="g++" 18 | - LUA="lua=5.2" EXTERNAL=true COMPILER="g++" 19 | 20 | branches: 21 | only: 22 | - master 23 | 24 | git: 25 | depth: 3 26 | 27 | notifications: 28 | email: false 29 | 30 | before_install: 31 | - pip install --user hererocks 32 | - hererocks old --$LUA 33 | - test -e old/bin/lua || (cd old/bin && ln -s luajit* lua) 34 | - hererocks new --lua=5.3 35 | 36 | install: 37 | - export CC="${COMPILER:-gcc}" DEF="" SRC="" CFLAGS="-Wall -Wextra -Ic-api -O2 -fPIC" 38 | - if [ "x${EXTERNAL:-}" = xtrue ]; then DEF="-DCOMPAT53_PREFIX=compat53" SRC="c-api/compat-5.3.c"; fi 39 | - ${CC} ${CFLAGS} -Iold/include ${DEF} -shared -o old/testmod.so tests/testmod.c ${SRC} 40 | - ${CC} ${CFLAGS} -Inew/include ${DEF} -shared -o new/testmod.so tests/testmod.c ${SRC} 41 | - gcc ${CFLAGS} -Iold/include ${DEF} -shared -o old/compat53.so ltablib.c lutf8lib.c lstrlib.c ${SRC} 42 | 43 | script: 44 | - (cd old && bin/lua ../tests/test.lua) > old.txt 45 | - (cd new && bin/lua ../tests/test.lua) > new.txt 46 | - diff old.txt new.txt || true 47 | 48 | -------------------------------------------------------------------------------- /src/pending_call.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include "compat-5.3.h" 6 | 7 | #include 8 | 9 | #include "ldbus.h" 10 | #include "message.h" 11 | 12 | #include "pending_call.h" 13 | 14 | static DBusPendingCall* checkPendingCall(lua_State *L, int arg) { 15 | return *(DBusPendingCall**)luaL_checkudata(L, arg, "ldbus_DBusPendingCall"); 16 | } 17 | 18 | static int ldbus_pending_call_unref(lua_State *L) { 19 | DBusPendingCall* pending = checkPendingCall(L, 1); 20 | dbus_pending_call_unref(pending); 21 | return 0; 22 | } 23 | 24 | static void pending_notify_function(DBusPendingCall *pending, void *data) { 25 | lua_State *L = *(lua_State**)data; 26 | int top = lua_gettop(L); 27 | UNUSED(pending); 28 | lua_rawgetp(L, LUA_REGISTRYINDEX, data); 29 | lua_getuservalue(L, -1); 30 | lua_remove(L, top + 1); /* remove userdata */ 31 | lua_rawgeti(L, -1, DBUS_LUA_FUNC_FUNC); 32 | lua_remove(L, top + 1); /* remove uservalue table */ 33 | if (lua_pcall(L, 0, 0, 0) != LUA_OK) { 34 | /* unhandled error */ 35 | lua_pop(L, 1); 36 | } 37 | } 38 | 39 | static void free_data_function(void *data) { 40 | lua_State *L = *(lua_State**)data; 41 | lua_pushnil(L); 42 | lua_rawsetp(L, LUA_REGISTRYINDEX, data); 43 | } 44 | 45 | static int ldbus_pending_call_set_notify(lua_State *L) { 46 | DBusPendingCall* pending = checkPendingCall(L, 1); 47 | lua_State **data; 48 | luaL_checktype(L, 2, LUA_TFUNCTION); 49 | data = lua_newuserdata(L, sizeof(lua_State*)); 50 | *data = L; 51 | lua_createtable(L, 2, 0); 52 | lua_pushvalue(L, 2); 53 | lua_rawseti(L, -2, DBUS_LUA_FUNC_FUNC); 54 | lua_pushthread(L); 55 | lua_rawseti(L, -2, DBUS_LUA_FUNC_THREAD); 56 | lua_setuservalue(L, -2); 57 | lua_rawsetp(L, LUA_REGISTRYINDEX, data); 58 | if (!dbus_pending_call_set_notify(pending, pending_notify_function, data, free_data_function)) { 59 | free_data_function(data); 60 | return luaL_error(L, LDBUS_NO_MEMORY); 61 | } 62 | lua_pushboolean(L, 1); 63 | return 1; 64 | } 65 | 66 | static int ldbus_pending_call_cancel(lua_State *L) { 67 | DBusPendingCall* pending = checkPendingCall(L, 1); 68 | dbus_pending_call_cancel(pending); 69 | return 0; 70 | } 71 | 72 | static int ldbus_pending_call_get_completed(lua_State *L) { 73 | DBusPendingCall* pending = checkPendingCall(L, 1); 74 | lua_pushboolean(L, dbus_pending_call_get_completed(pending)); 75 | return 1; 76 | } 77 | 78 | static int ldbus_pending_call_steal_reply(lua_State *L) { 79 | DBusPendingCall* pending = checkPendingCall(L, 1); 80 | DBusMessage* message = dbus_pending_call_steal_reply(pending); 81 | if (message == NULL) { 82 | lua_pushnil(L); 83 | } else { 84 | push_DBusMessage(L, message); 85 | } 86 | return 1; 87 | } 88 | 89 | static int ldbus_pending_call_block(lua_State *L) { 90 | DBusPendingCall* pending = checkPendingCall(L, 1); 91 | dbus_pending_call_block(pending); 92 | return 0; 93 | } 94 | 95 | LDBUS_INTERNAL void push_DBusPendingCall(lua_State *L, DBusPendingCall* pending) { 96 | static luaL_Reg const methods [] = { 97 | { "set_notify", ldbus_pending_call_set_notify }, 98 | { "cancel", ldbus_pending_call_cancel }, 99 | { "get_completed", ldbus_pending_call_get_completed }, 100 | { "steal_reply", ldbus_pending_call_steal_reply }, 101 | { "block", ldbus_pending_call_block }, 102 | { NULL, NULL } 103 | }; 104 | 105 | DBusPendingCall ** udata = lua_newuserdata(L, sizeof(DBusPendingCall*)); 106 | *udata = pending; 107 | 108 | if (luaL_newmetatable(L, "ldbus_DBusPendingCall")) { 109 | luaL_newlib(L, methods); 110 | lua_setfield(L, -2, "__index"); 111 | 112 | lua_pushcfunction(L, ldbus_pending_call_unref); 113 | lua_setfield(L, -2, "__gc"); 114 | 115 | lua_pushcfunction(L, tostring); 116 | lua_setfield(L, -2, "__tostring"); 117 | 118 | lua_pushstring(L, "DBusPendingCall"); 119 | lua_setfield(L, -2, "__udtype"); 120 | } 121 | lua_setmetatable(L, -2); 122 | } 123 | -------------------------------------------------------------------------------- /example.lua: -------------------------------------------------------------------------------- 1 | local ldbus = require "ldbus" 2 | sleep = require"socket".sleep 3 | 4 | function sendsignal ( sigvalue ) 5 | print ( "Sending signal with value " .. sigvalue ) 6 | 7 | local conn = assert ( ldbus.bus.get ( "session" ) ) 8 | assert ( ldbus.bus.request_name ( conn , "test.signal.source" , { replace_existing = true } ) ) 9 | local msg = assert ( ldbus.message.new_signal ( "/test/signal/Object" , "test.signal.Type" , "Test" ) , "Message Null" ) 10 | 11 | local iter = ldbus.message.iter.new ( ) 12 | msg:iter_init_append ( iter ) 13 | 14 | assert ( iter:append_basic ( sigvalue ) , "Out of Memory" ) 15 | 16 | local ok , serial = assert ( conn:send ( msg ) ) 17 | conn:flush ( ) 18 | 19 | print ( "Signal Sent" ) 20 | end 21 | 22 | function query ( param ) 23 | print ( "Calling remote method with " .. param ) 24 | 25 | local conn = assert ( ldbus.bus.get ( "session" ) ) 26 | assert ( ldbus.bus.request_name ( conn , "test.signal.source" , { replace_existing = true } ) ) 27 | 28 | local msg = assert ( ldbus.message.new_method_call ( "test.method.server" , "/test/method/Object" , "test.method.Type" , "Method" ) , "Message Null" ) 29 | local iter = ldbus.message.iter.new ( ) 30 | msg:iter_init_append ( iter ) 31 | 32 | assert ( iter:append_basic ( param ) , "Out of Memory" ) 33 | 34 | local reply = assert ( conn:send_with_reply_and_block ( msg ) ) 35 | assert ( reply:iter_init ( iter ) , "Message has no arguments" ) 36 | assert ( iter:get_arg_type ( ) == ldbus.types.boolean , "Argument not boolean!" ) 37 | local stat = iter:get_basic ( ) 38 | assert ( iter:next ( ) ) 39 | assert ( iter:get_arg_type ( ) == ldbus.types.uint32 , "Argument not int!" ) 40 | local level = iter:get_basic ( ) 41 | print( "Got reply: " .. tostring ( stat ) .. ", " .. level ) 42 | end 43 | 44 | local function reply_to_method_call ( msg , conn ) 45 | local stat , level = true , 21614 46 | 47 | local iter = ldbus.message.iter.new ( ) 48 | assert ( msg:iter_init ( iter ) , "Message has no arguments" ) 49 | 50 | assert ( iter:get_arg_type ( ) == ldbus.types.string , "Argument is not a string" ) 51 | local param = iter:get_basic ( ) 52 | 53 | print ( "Method called with " .. param ) 54 | 55 | local reply = assert ( msg:new_method_return ( ) ) 56 | reply:iter_init_append ( iter ) 57 | assert ( iter:append_basic ( stat , ldbus.types.boolean ) , "Out of Memory" ) 58 | assert ( iter:append_basic ( level , ldbus.types.uint32 ) , "Out of Memory" ) 59 | 60 | local ok , serial = assert ( conn:send ( reply ) , "Out of Memory" ) 61 | conn:flush ( ) 62 | end 63 | 64 | function listen ( ) 65 | print ( "Listening for method calls" ) 66 | 67 | local conn = assert ( ldbus.bus.get ( "session" ) ) 68 | assert ( assert ( ldbus.bus.request_name ( conn , "test.method.server" , { replace_existing = true } ) ) == "primary_owner" , "Not Primary Owner" ) 69 | 70 | while conn:read_write ( 0 ) do 71 | local msg = conn:pop_message ( ) 72 | if not msg then 73 | sleep ( 1 ) 74 | elseif msg:get_type ( ) == "method_call" then 75 | reply_to_method_call ( msg , conn ) 76 | end 77 | end 78 | end 79 | 80 | function receive ( ) 81 | print ( "Listening for signals" ) 82 | 83 | local conn = assert ( ldbus.bus.get ( "session" ) ) 84 | assert ( assert ( ldbus.bus.request_name ( conn , "test.signal.sink" , { replace_existing = true } ) ) == "primary_owner" , "Not Primary Owner" ) 85 | 86 | assert ( ldbus.bus.add_match ( conn , "type='signal',interface='test.signal.Type'" ) ) 87 | conn:flush ( ) 88 | 89 | print ( "Match rule sent" ) 90 | 91 | while conn:read_write ( 0 ) do 92 | local msg = conn:pop_message ( ) 93 | if not msg then 94 | sleep ( 1 ) 95 | elseif msg:get_type ( ) == "signal" then 96 | local iter = ldbus.message.iter.new ( ) 97 | assert ( msg:iter_init ( iter ) , "Message has no parameters" ) 98 | 99 | assert ( iter:get_arg_type ( ) == ldbus.types.string , "Argument is not a string" ) 100 | local sigvalue = iter:get_basic ( ) 101 | 102 | print ( "Got signal with value " .. sigvalue ) 103 | end 104 | end 105 | end 106 | 107 | local op = arg [ 1 ] 108 | if op == "send" then 109 | sendsignal ( arg [ 2 ] or "no param" ) 110 | elseif op == "receive" then 111 | receive ( ) 112 | elseif op == "listen" then 113 | listen ( ) 114 | elseif op == "query" then 115 | query ( arg [ 2 ] or "no param" ) 116 | else 117 | print ( "Syntax: dbus-example [send|receive|listen|query] []" ) 118 | os.exit ( 1 ) 119 | end 120 | -------------------------------------------------------------------------------- /src/timeout.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include "compat-5.3.h" 6 | 7 | #include 8 | 9 | #include "ldbus.h" 10 | #include "timeout.h" 11 | 12 | 13 | static int ldbus_timeout_get_interval(lua_State *L) { 14 | DBusTimeout *timeout = check_DBusTimeout(L, 1); 15 | int interval; 16 | if (timeout == NULL) { 17 | lua_pushnil(L); 18 | } else { 19 | interval = dbus_timeout_get_interval(timeout); 20 | lua_pushnumber(L, ((double)interval)/1000); 21 | } 22 | return 1; 23 | } 24 | 25 | static int ldbus_timeout_handle(lua_State *L) { 26 | DBusTimeout *timeout = check_DBusTimeout(L, 1); 27 | if (timeout == NULL) { 28 | lua_pushnil(L); 29 | } else { 30 | lua_pushboolean(L, dbus_timeout_handle(timeout)); 31 | } 32 | return 1; 33 | } 34 | 35 | static int ldbus_timeout_get_enabled(lua_State *L) { 36 | DBusTimeout *timeout = check_DBusTimeout(L, 1); 37 | dbus_bool_t enabled = (timeout == NULL) ? FALSE : dbus_timeout_get_enabled(timeout); 38 | lua_pushboolean(L, enabled); 39 | return 1; 40 | } 41 | 42 | LDBUS_INTERNAL void push_DBusTimeout(lua_State *L, DBusTimeout *timeout) { 43 | DBusTimeout **udata = lua_newuserdata(L, sizeof(DBusTimeout*)); 44 | *udata = timeout; 45 | luaL_setmetatable(L, DBUS_TIMEOUT_METATABLE); 46 | } 47 | 48 | LDBUS_INTERNAL dbus_bool_t ldbus_timeout_add_function(DBusTimeout *timeout, void *data) { 49 | lua_State *L = *(lua_State**)data; 50 | int top = lua_gettop(L); 51 | if (!lua_checkstack(L, 2)) return FALSE; 52 | lua_rawgetp(L, LUA_REGISTRYINDEX, data); 53 | lua_getuservalue(L, -1); 54 | lua_remove(L, top + 1); /* remove userdata */ 55 | lua_rawgeti(L, -1, DBUS_LUA_FUNC_ADD); 56 | lua_remove(L, top + 1); /* remove uservalue table */ 57 | push_DBusTimeout(L, timeout); /* XXX: could throw */ 58 | /* Save DBusTimeout in registry */ 59 | lua_pushvalue(L, -1); 60 | lua_rawsetp(L, LUA_REGISTRYINDEX, timeout); 61 | switch(lua_pcall(L, 1, 0, 0)) { 62 | case LUA_OK: 63 | return TRUE; 64 | case LUA_ERRMEM: 65 | lua_pop(L, 1); 66 | return FALSE; 67 | default: 68 | /* unhandled error */ 69 | lua_pop(L, 1); 70 | return TRUE; 71 | } 72 | } 73 | 74 | LDBUS_INTERNAL void ldbus_timeout_remove_function(DBusTimeout *timeout, void *data) { 75 | lua_State *L = *(lua_State**)data; 76 | int top = lua_gettop(L); 77 | DBusTimeout **udata; 78 | 79 | /* Grab added timeout from registry */ 80 | /* We need to keep a reference to userdata around so we can invalidate it */ 81 | lua_rawgetp(L, LUA_REGISTRYINDEX, timeout); 82 | /* Delete from registry */ 83 | lua_pushnil(L); 84 | lua_rawsetp(L, LUA_REGISTRYINDEX, timeout); 85 | 86 | lua_rawgetp(L, LUA_REGISTRYINDEX, data); 87 | lua_getuservalue(L, -1); 88 | lua_remove(L, top + 2); /* remove userdata */ 89 | lua_rawgeti(L, -1, DBUS_LUA_FUNC_REMOVE); 90 | lua_remove(L, top + 2); /* remove uservalue table */ 91 | lua_pushvalue(L, top + 1); /* push timeout as argument */ 92 | if (lua_pcall(L, 1, 0, 0) != LUA_OK) { 93 | /* unhandled error */ 94 | } 95 | 96 | /* Invalidate timeout object */ 97 | udata = lua_touserdata(L, top+1); 98 | if (udata != NULL) { 99 | *udata = NULL; 100 | } 101 | 102 | lua_settop(L, top); 103 | return; 104 | } 105 | 106 | LDBUS_INTERNAL void ldbus_timeout_toggled_function(DBusTimeout *timeout, void *data) { 107 | lua_State *L = *(lua_State**)data; 108 | int top = lua_gettop(L); 109 | lua_rawgetp(L, LUA_REGISTRYINDEX, data); 110 | lua_getuservalue(L, -1); 111 | lua_remove(L, top + 1); /* remove userdata */ 112 | lua_rawgeti(L, -1, DBUS_LUA_FUNC_TOGGLE); 113 | lua_remove(L, top + 1); /* remove uservalue table */ 114 | lua_rawgetp(L, LUA_REGISTRYINDEX, timeout); /* grab added timeout from registry */ 115 | if (lua_pcall(L, 1, 0, 0) != LUA_OK) { 116 | /* unhandled error */ 117 | lua_pop(L, 1); 118 | } 119 | return; 120 | } 121 | 122 | LDBUS_INTERNAL void ldbus_timeout_free_data_function(void *data) { 123 | lua_State *L = *(lua_State**)data; 124 | lua_pushnil(L); 125 | lua_rawsetp(L, LUA_REGISTRYINDEX, data); 126 | } 127 | 128 | int lua_open_ldbus_timeout(lua_State *L) { 129 | static luaL_Reg const methods [] = { 130 | { "get_interval", ldbus_timeout_get_interval }, 131 | { "handle", ldbus_timeout_handle }, 132 | { "get_enabled", ldbus_timeout_get_enabled }, 133 | { NULL, NULL } 134 | }; 135 | 136 | if (luaL_newmetatable(L, DBUS_TIMEOUT_METATABLE)) { 137 | luaL_newlib(L, methods); 138 | lua_setfield(L, -2, "__index"); 139 | 140 | lua_pushcfunction(L, tostring); 141 | lua_setfield(L, -2, "__tostring"); 142 | 143 | lua_pushstring(L, "DBusTimeout"); 144 | lua_setfield(L, -2, "__udtype"); 145 | } 146 | return 0; 147 | } 148 | -------------------------------------------------------------------------------- /vendor/compat-5.3/lprefix.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lprefix.h,v 1.2 2014/12/29 16:54:13 roberto Exp $ 3 | ** Definitions for Lua code that must come before any other header file 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lprefix_h 8 | #define lprefix_h 9 | 10 | 11 | /* 12 | ** Allows POSIX/XSI stuff 13 | */ 14 | #if !defined(LUA_USE_C89) /* { */ 15 | 16 | #if !defined(_XOPEN_SOURCE) 17 | #define _XOPEN_SOURCE 600 18 | #elif _XOPEN_SOURCE == 0 19 | #undef _XOPEN_SOURCE /* use -D_XOPEN_SOURCE=0 to undefine it */ 20 | #endif 21 | 22 | /* 23 | ** Allows manipulation of large files in gcc and some other compilers 24 | */ 25 | #if !defined(LUA_32BITS) && !defined(_FILE_OFFSET_BITS) 26 | #define _LARGEFILE_SOURCE 1 27 | #define _FILE_OFFSET_BITS 64 28 | #endif 29 | 30 | #endif /* } */ 31 | 32 | 33 | /* 34 | ** Windows stuff 35 | */ 36 | #if defined(_WIN32) /* { */ 37 | 38 | #if !defined(_CRT_SECURE_NO_WARNINGS) 39 | #define _CRT_SECURE_NO_WARNINGS /* avoid warnings about ISO C functions */ 40 | #endif 41 | 42 | #endif /* } */ 43 | 44 | 45 | /* COMPAT53 adaptation */ 46 | #include "c-api/compat-5.3.h" 47 | 48 | #undef LUAMOD_API 49 | #define LUAMOD_API extern 50 | 51 | 52 | #ifdef lutf8lib_c 53 | # define luaopen_utf8 luaopen_compat53_utf8 54 | /* we don't support the %U format string of lua_pushfstring! 55 | * code below adapted from the Lua 5.3 sources: 56 | */ 57 | static const char *compat53_utf8_escape (lua_State* L, long x) { 58 | if (x < 0x80) { /* ASCII */ 59 | char c = (char)x; 60 | lua_pushlstring(L, &c, 1); 61 | } else { 62 | char buff[8] = { 0 }; 63 | unsigned int mfb = 0x3f; 64 | int n = 1; 65 | do { 66 | buff[8 - (n++)] = (char)(0x80|(x & 0x3f)); 67 | x >>= 6; 68 | mfb >>= 1; 69 | } while (x > mfb); 70 | buff[8-n] = (char)((~mfb << 1) | x); 71 | lua_pushlstring(L, buff+8-n, n); 72 | } 73 | return lua_tostring(L, -1); 74 | } 75 | # define lua_pushfstring(L, fmt, l) \ 76 | compat53_utf8_escape(L, l) 77 | #endif 78 | 79 | 80 | #ifdef ltablib_c 81 | # define luaopen_table luaopen_compat53_table 82 | # ifndef LUA_MAXINTEGER 83 | /* conservative estimate: */ 84 | # define LUA_MAXINTEGER INT_MAX 85 | # endif 86 | #endif /* ltablib_c */ 87 | 88 | 89 | #ifdef lstrlib_c 90 | #include 91 | #include 92 | /* move the string library open function out of the way (we only take 93 | * the string packing functions)! 94 | */ 95 | # define luaopen_string luaopen_string_XXX 96 | /* used in string.format implementation, which we don't use: */ 97 | # ifndef LUA_INTEGER_FRMLEN 98 | # define LUA_INTEGER_FRMLEN "" 99 | # define LUA_NUMBER_FRMLEN "" 100 | # endif 101 | # ifndef LUA_MININTEGER 102 | # define LUA_MININTEGER 0 103 | # endif 104 | # ifndef LUA_INTEGER_FMT 105 | # define LUA_INTEGER_FMT "%d" 106 | # endif 107 | # ifndef LUAI_UACINT 108 | # define LUAI_UACINT lua_Integer 109 | # endif 110 | /* different Lua 5.3 versions have conflicting variants of this macro 111 | * in luaconf.h, there's a fallback implementation in lstrlib.c, and 112 | * the macro isn't used for string (un)packing anyway! 113 | * */ 114 | # undef lua_number2strx 115 | # if LUA_VERSION_NUM < 503 116 | /* lstrlib assumes that lua_Integer and lua_Unsigned have the same 117 | * size, so we use the unsigned equivalent of ptrdiff_t! */ 118 | # define lua_Unsigned size_t 119 | # endif 120 | # ifndef l_mathlim 121 | # ifdef LUA_NUMBER_DOUBLE 122 | # define l_mathlim(n) (DBL_##n) 123 | # else 124 | # define l_mathlim(n) (FLT_##n) 125 | # endif 126 | # endif 127 | # ifndef l_mathop 128 | # ifdef LUA_NUMBER_DOUBLE 129 | # define l_mathop(op) op 130 | # else 131 | # define l_mathop(op) op##f 132 | # endif 133 | # endif 134 | # ifndef lua_getlocaledecpoint 135 | # define lua_getlocaledecpoint() (localeconv()->decimal_point[0]) 136 | # endif 137 | # ifndef l_sprintf 138 | # if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L 139 | # define l_sprintf(s,sz,f,i) (snprintf(s, sz, f, i)) 140 | # else 141 | # define l_sprintf(s,sz,f,i) ((void)(sz), sprintf(s, f, i)) 142 | # endif 143 | # endif 144 | 145 | static int str_pack (lua_State *L); 146 | static int str_packsize (lua_State *L); 147 | static int str_unpack (lua_State *L); 148 | LUAMOD_API int luaopen_compat53_string (lua_State *L) { 149 | luaL_Reg const funcs[] = { 150 | { "pack", str_pack }, 151 | { "packsize", str_packsize }, 152 | { "unpack", str_unpack }, 153 | { NULL, NULL } 154 | }; 155 | luaL_newlib(L, funcs); 156 | return 1; 157 | } 158 | /* fake CLANG feature detection on other compilers */ 159 | # ifndef __has_attribute 160 | # define __has_attribute(x) 0 161 | # endif 162 | /* make luaopen_string(_XXX) static, so it (and all other referenced 163 | * string functions) won't be included in the resulting dll 164 | * (hopefully). 165 | */ 166 | # undef LUAMOD_API 167 | # if defined(__GNUC__) || __has_attribute(__unused__) 168 | # define LUAMOD_API __attribute__((__unused__)) static 169 | # else 170 | # define LUAMOD_API static 171 | # endif 172 | #endif /* lstrlib.c */ 173 | 174 | #endif 175 | 176 | -------------------------------------------------------------------------------- /src/watch.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include "compat-5.3.h" 6 | 7 | #include 8 | 9 | #include "ldbus.h" 10 | #include "watch.h" 11 | 12 | 13 | static int ldbus_watch_get_unix_fd(lua_State *L) { 14 | DBusWatch *watch = check_DBusWatch(L, 1); 15 | int fd; 16 | if (watch == NULL || (fd = dbus_watch_get_unix_fd(watch)) == -1) { 17 | lua_pushnil(L); 18 | } else { 19 | lua_pushinteger(L, fd); 20 | } 21 | return 1; 22 | } 23 | 24 | static int ldbus_watch_get_socket(lua_State *L) { 25 | DBusWatch *watch = check_DBusWatch(L, 1); 26 | int fd; 27 | if (watch == NULL || (fd = dbus_watch_get_socket(watch)) == -1) { 28 | lua_pushnil(L); 29 | } else { 30 | lua_pushinteger(L, fd); 31 | } 32 | return 1; 33 | } 34 | 35 | static int ldbus_watch_get_flags(lua_State *L) { 36 | DBusWatch *watch = check_DBusWatch(L, 1); 37 | unsigned int flags = (watch == NULL) ? 0 : dbus_watch_get_flags(watch); 38 | lua_pushinteger(L, flags); 39 | return 1; 40 | } 41 | 42 | static int ldbus_watch_handle(lua_State *L) { 43 | DBusWatch *watch = check_DBusWatch(L, 1); 44 | int flags; 45 | dbus_bool_t ok; 46 | luaL_argcheck(L, watch != NULL, 1, "watch invalid"); 47 | flags = luaL_checkinteger(L, 2); 48 | ok = dbus_watch_handle(watch, flags); 49 | lua_pushboolean(L, ok); 50 | return 1; 51 | } 52 | 53 | static int ldbus_watch_get_enabled(lua_State *L) { 54 | DBusWatch *watch = check_DBusWatch(L, 1); 55 | dbus_bool_t enabled = (watch == NULL) ? FALSE : dbus_watch_get_enabled(watch); 56 | lua_pushboolean(L, enabled); 57 | return 1; 58 | } 59 | 60 | LDBUS_INTERNAL void push_DBusWatch(lua_State *L, DBusWatch *watch) { 61 | DBusWatch **udata = lua_newuserdata(L, sizeof(DBusWatch*)); 62 | *udata = watch; 63 | luaL_setmetatable(L, DBUS_WATCH_METATABLE); 64 | } 65 | 66 | LDBUS_INTERNAL dbus_bool_t ldbus_watch_add_function(DBusWatch *watch, void *data) { 67 | lua_State *L = *(lua_State**)data; 68 | int top = lua_gettop(L); 69 | if (!lua_checkstack(L, 2)) return FALSE; 70 | lua_rawgetp(L, LUA_REGISTRYINDEX, data); 71 | lua_getuservalue(L, -1); 72 | lua_remove(L, top + 1); /* remove userdata */ 73 | lua_rawgeti(L, -1, DBUS_LUA_FUNC_ADD); 74 | lua_remove(L, top + 1); /* remove uservalue table */ 75 | push_DBusWatch(L, watch); /* XXX: could throw */ 76 | /* Save DBusWatch in registry */ 77 | lua_pushvalue(L, -1); 78 | lua_rawsetp(L, LUA_REGISTRYINDEX, watch); 79 | switch(lua_pcall(L, 1, 0, 0)) { 80 | case LUA_OK: 81 | return TRUE; 82 | case LUA_ERRMEM: 83 | lua_pop(L, 1); 84 | return FALSE; 85 | default: 86 | /* unhandled error */ 87 | lua_pop(L, 1); 88 | return TRUE; 89 | } 90 | } 91 | 92 | LDBUS_INTERNAL void ldbus_watch_remove_function(DBusWatch *watch, void *data) { 93 | lua_State *L = *(lua_State**)data; 94 | int top = lua_gettop(L); 95 | DBusWatch **udata; 96 | 97 | /* Grab added watch from registry */ 98 | /* We need to keep a reference to userdata around so we can invalidate it */ 99 | lua_rawgetp(L, LUA_REGISTRYINDEX, watch); 100 | /* Delete from registry */ 101 | lua_pushnil(L); 102 | lua_rawsetp(L, LUA_REGISTRYINDEX, watch); 103 | 104 | lua_rawgetp(L, LUA_REGISTRYINDEX, data); 105 | lua_getuservalue(L, -1); 106 | lua_remove(L, top + 2); /* remove userdata */ 107 | lua_rawgeti(L, -1, DBUS_LUA_FUNC_REMOVE); 108 | lua_remove(L, top + 2); /* remove uservalue table */ 109 | lua_pushvalue(L, top + 1); /* push watch as argument */ 110 | if (lua_pcall(L, 1, 0, 0) != LUA_OK) { 111 | /* unhandled error */ 112 | } 113 | 114 | /* Invalidate watch object */ 115 | udata = lua_touserdata(L, top+1); 116 | if (udata != NULL) { 117 | *udata = NULL; 118 | } 119 | 120 | lua_settop(L, top); 121 | return; 122 | } 123 | 124 | LDBUS_INTERNAL void ldbus_watch_toggled_function(DBusWatch *watch, void *data) { 125 | lua_State *L = *(lua_State**)data; 126 | int top = lua_gettop(L); 127 | lua_rawgetp(L, LUA_REGISTRYINDEX, data); 128 | lua_getuservalue(L, -1); 129 | lua_remove(L, top + 1); /* remove userdata */ 130 | lua_rawgeti(L, -1, DBUS_LUA_FUNC_TOGGLE); 131 | lua_remove(L, top + 1); /* remove uservalue table */ 132 | lua_rawgetp(L, LUA_REGISTRYINDEX, watch); /* grab added watch from registry */ 133 | if (lua_pcall(L, 1, 0, 0) != LUA_OK) { 134 | /* unhandled error */ 135 | lua_pop(L, 1); 136 | } 137 | return; 138 | } 139 | 140 | LDBUS_INTERNAL void ldbus_watch_free_data_function(void *data) { 141 | lua_State *L = *(lua_State**)data; 142 | lua_pushnil(L); 143 | lua_rawsetp(L, LUA_REGISTRYINDEX, data); 144 | } 145 | 146 | int luaopen_ldbus_watch(lua_State *L) { 147 | static luaL_Reg const methods [] = { 148 | { "get_unix_fd", ldbus_watch_get_unix_fd }, 149 | { "get_socket", ldbus_watch_get_socket }, 150 | { "get_flags", ldbus_watch_get_flags }, 151 | { "handle", ldbus_watch_handle }, 152 | { "get_enabled", ldbus_watch_get_enabled }, 153 | { NULL, NULL } 154 | }; 155 | 156 | if (luaL_newmetatable(L, DBUS_WATCH_METATABLE)) { 157 | luaL_newlib(L, methods); 158 | lua_setfield(L, -2, "__index"); 159 | 160 | lua_pushcfunction(L, tostring); 161 | lua_setfield(L, -2, "__tostring"); 162 | 163 | lua_pushstring(L, "DBusWatch"); 164 | lua_setfield(L, -2, "__udtype"); 165 | } 166 | 167 | lua_createtable(L, 0, 3); 168 | lua_pushinteger(L, DBUS_WATCH_READABLE); lua_setfield(L, -2, "READABLE"); 169 | lua_pushinteger(L, DBUS_WATCH_WRITABLE); lua_setfield(L, -2, "WRITABLE"); 170 | lua_pushinteger(L, DBUS_WATCH_HANGUP); lua_setfield(L, -2, "HANGUP"); 171 | lua_pushinteger(L, DBUS_WATCH_ERROR); lua_setfield(L, -2, "ERROR"); 172 | 173 | return 1; 174 | } 175 | -------------------------------------------------------------------------------- /vendor/compat-5.3/lbitlib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lbitlib.c,v 1.30.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Standard library for bitwise operations 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lbitlib_c 8 | #define LUA_LIB 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include "lua.h" 14 | 15 | #include "lauxlib.h" 16 | #include "lualib.h" 17 | 18 | 19 | #if defined(LUA_COMPAT_BITLIB) /* { */ 20 | 21 | 22 | #define pushunsigned(L,n) (sizeof(lua_Integer) > 4 ? lua_pushinteger(L, (lua_Integer)(n)) : lua_pushnumber(L, (lua_Number)(n))) 23 | static lua_Unsigned checkunsigned(lua_State *L, int i) { 24 | if (sizeof(lua_Integer) > 4) 25 | return (lua_Unsigned)luaL_checkinteger(L, i); 26 | else { 27 | lua_Number d = luaL_checknumber(L, i); 28 | if (d < 0) 29 | d = (d + 1) + (~(lua_Unsigned)0); 30 | luaL_argcheck(L, d >= 0 && d <= (~(lua_Unsigned)0), i, "value out of range"); 31 | return (lua_Unsigned)d; 32 | } 33 | } 34 | 35 | 36 | /* number of bits to consider in a number */ 37 | #if !defined(LUA_NBITS) 38 | #define LUA_NBITS 32 39 | #endif 40 | 41 | 42 | /* 43 | ** a lua_Unsigned with its first LUA_NBITS bits equal to 1. (Shift must 44 | ** be made in two parts to avoid problems when LUA_NBITS is equal to the 45 | ** number of bits in a lua_Unsigned.) 46 | */ 47 | #define ALLONES (~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1)) 48 | 49 | 50 | /* macro to trim extra bits */ 51 | #define trim(x) ((x) & ALLONES) 52 | 53 | 54 | /* builds a number with 'n' ones (1 <= n <= LUA_NBITS) */ 55 | #define mask(n) (~((ALLONES << 1) << ((n) - 1))) 56 | 57 | 58 | 59 | static lua_Unsigned andaux (lua_State *L) { 60 | int i, n = lua_gettop(L); 61 | lua_Unsigned r = ~(lua_Unsigned)0; 62 | for (i = 1; i <= n; i++) 63 | r &= checkunsigned(L, i); 64 | return trim(r); 65 | } 66 | 67 | 68 | static int b_and (lua_State *L) { 69 | lua_Unsigned r = andaux(L); 70 | pushunsigned(L, r); 71 | return 1; 72 | } 73 | 74 | 75 | static int b_test (lua_State *L) { 76 | lua_Unsigned r = andaux(L); 77 | lua_pushboolean(L, r != 0); 78 | return 1; 79 | } 80 | 81 | 82 | static int b_or (lua_State *L) { 83 | int i, n = lua_gettop(L); 84 | lua_Unsigned r = 0; 85 | for (i = 1; i <= n; i++) 86 | r |= checkunsigned(L, i); 87 | pushunsigned(L, trim(r)); 88 | return 1; 89 | } 90 | 91 | 92 | static int b_xor (lua_State *L) { 93 | int i, n = lua_gettop(L); 94 | lua_Unsigned r = 0; 95 | for (i = 1; i <= n; i++) 96 | r ^= checkunsigned(L, i); 97 | pushunsigned(L, trim(r)); 98 | return 1; 99 | } 100 | 101 | 102 | static int b_not (lua_State *L) { 103 | lua_Unsigned r = ~checkunsigned(L, 1); 104 | pushunsigned(L, trim(r)); 105 | return 1; 106 | } 107 | 108 | 109 | static int b_shift (lua_State *L, lua_Unsigned r, lua_Integer i) { 110 | if (i < 0) { /* shift right? */ 111 | i = -i; 112 | r = trim(r); 113 | if (i >= LUA_NBITS) r = 0; 114 | else r >>= i; 115 | } 116 | else { /* shift left */ 117 | if (i >= LUA_NBITS) r = 0; 118 | else r <<= i; 119 | r = trim(r); 120 | } 121 | pushunsigned(L, r); 122 | return 1; 123 | } 124 | 125 | 126 | static int b_lshift (lua_State *L) { 127 | return b_shift(L, checkunsigned(L, 1), luaL_checkinteger(L, 2)); 128 | } 129 | 130 | 131 | static int b_rshift (lua_State *L) { 132 | return b_shift(L, checkunsigned(L, 1), -luaL_checkinteger(L, 2)); 133 | } 134 | 135 | 136 | static int b_arshift (lua_State *L) { 137 | lua_Unsigned r = checkunsigned(L, 1); 138 | lua_Integer i = luaL_checkinteger(L, 2); 139 | if (i < 0 || !(r & ((lua_Unsigned)1 << (LUA_NBITS - 1)))) 140 | return b_shift(L, r, -i); 141 | else { /* arithmetic shift for 'negative' number */ 142 | if (i >= LUA_NBITS) r = ALLONES; 143 | else 144 | r = trim((r >> i) | ~(trim(~(lua_Unsigned)0) >> i)); /* add signal bit */ 145 | pushunsigned(L, r); 146 | return 1; 147 | } 148 | } 149 | 150 | 151 | static int b_rot (lua_State *L, lua_Integer d) { 152 | lua_Unsigned r = checkunsigned(L, 1); 153 | int i = d & (LUA_NBITS - 1); /* i = d % NBITS */ 154 | r = trim(r); 155 | if (i != 0) /* avoid undefined shift of LUA_NBITS when i == 0 */ 156 | r = (r << i) | (r >> (LUA_NBITS - i)); 157 | pushunsigned(L, trim(r)); 158 | return 1; 159 | } 160 | 161 | 162 | static int b_lrot (lua_State *L) { 163 | return b_rot(L, luaL_checkinteger(L, 2)); 164 | } 165 | 166 | 167 | static int b_rrot (lua_State *L) { 168 | return b_rot(L, -luaL_checkinteger(L, 2)); 169 | } 170 | 171 | 172 | /* 173 | ** get field and width arguments for field-manipulation functions, 174 | ** checking whether they are valid. 175 | ** ('luaL_error' called without 'return' to avoid later warnings about 176 | ** 'width' being used uninitialized.) 177 | */ 178 | static int fieldargs (lua_State *L, int farg, int *width) { 179 | lua_Integer f = luaL_checkinteger(L, farg); 180 | lua_Integer w = luaL_optinteger(L, farg + 1, 1); 181 | luaL_argcheck(L, 0 <= f, farg, "field cannot be negative"); 182 | luaL_argcheck(L, 0 < w, farg + 1, "width must be positive"); 183 | if (f + w > LUA_NBITS) 184 | luaL_error(L, "trying to access non-existent bits"); 185 | *width = (int)w; 186 | return (int)f; 187 | } 188 | 189 | 190 | static int b_extract (lua_State *L) { 191 | int w; 192 | lua_Unsigned r = trim(checkunsigned(L, 1)); 193 | int f = fieldargs(L, 2, &w); 194 | r = (r >> f) & mask(w); 195 | pushunsigned(L, r); 196 | return 1; 197 | } 198 | 199 | 200 | static int b_replace (lua_State *L) { 201 | int w; 202 | lua_Unsigned r = trim(checkunsigned(L, 1)); 203 | lua_Unsigned v = trim(checkunsigned(L, 2)); 204 | int f = fieldargs(L, 3, &w); 205 | lua_Unsigned m = mask(w); 206 | r = (r & ~(m << f)) | ((v & m) << f); 207 | pushunsigned(L, r); 208 | return 1; 209 | } 210 | 211 | 212 | static const luaL_Reg bitlib[] = { 213 | {"arshift", b_arshift}, 214 | {"band", b_and}, 215 | {"bnot", b_not}, 216 | {"bor", b_or}, 217 | {"bxor", b_xor}, 218 | {"btest", b_test}, 219 | {"extract", b_extract}, 220 | {"lrotate", b_lrot}, 221 | {"lshift", b_lshift}, 222 | {"replace", b_replace}, 223 | {"rrotate", b_rrot}, 224 | {"rshift", b_rshift}, 225 | {NULL, NULL} 226 | }; 227 | 228 | 229 | 230 | LUAMOD_API int luaopen_bit32 (lua_State *L) { 231 | luaL_newlib(L, bitlib); 232 | return 1; 233 | } 234 | 235 | 236 | #else /* }{ */ 237 | 238 | 239 | LUAMOD_API int luaopen_bit32 (lua_State *L) { 240 | return luaL_error(L, "library 'bit32' has been deprecated"); 241 | } 242 | 243 | #endif /* } */ 244 | -------------------------------------------------------------------------------- /src/bus.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include "compat-5.3.h" 6 | 7 | #include 8 | 9 | #include "ldbus.h" 10 | #include "connection.h" 11 | #include "message.h" 12 | #include "error.h" 13 | 14 | #include "bus.h" 15 | 16 | static const char *const BusType_lst [] = { 17 | "session", /* DBUS_BUS_SESSION == 0 */ 18 | "system", /* DBUS_BUS_SYSTEM == 1 */ 19 | "starter", /* DBUS_BUS_STARTER == 2 */ 20 | NULL 21 | }; 22 | 23 | static const char *const Request_Name_Reply_lst [] = { 24 | NULL, /* No 0... */ 25 | "primary_owner", /* DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER == 1 */ 26 | "in_queue", /* DBUS_REQUEST_NAME_REPLY_IN_QUEUE == 2 */ 27 | "exists", /* DBUS_REQUEST_NAME_REPLY_EXISTS == 3 */ 28 | "already_owner", /* DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER == 4 */ 29 | NULL 30 | }; 31 | 32 | static const char *const Release_Name_Reply_lst [] = { 33 | NULL, /* No 0... */ 34 | "released", /* DBUS_RELEASE_NAME_REPLY_RELEASED == 1 */ 35 | "non_existent", /* DBUS_RELEASE_NAME_REPLY_NON_EXISTENT == 2 */ 36 | "not_owner", /* DBUS_RELEASE_NAME_REPLY_NOT_OWNER == 3 */ 37 | NULL 38 | }; 39 | 40 | static const char *const Start_Reply_lst [] = { 41 | NULL, /* No 0... */ 42 | "success", /* DBUS_START_REPLY_SUCCESS == 1 */ 43 | "already_running", /* DBUS_START_REPLY_ALREADY_RUNNING == 2 */ 44 | NULL 45 | }; 46 | 47 | static int ldbus_bus_get(lua_State *L) { 48 | int type = luaL_checkoption(L, 1, NULL, BusType_lst); 49 | 50 | DBusError *error = new_DBusError(L); 51 | DBusConnection *connection = dbus_bus_get(type, error); 52 | 53 | if (dbus_error_is_set(error)) { 54 | lua_pushboolean(L, FALSE); 55 | lua_pushstring(L, error->message); 56 | return 2; 57 | } else { 58 | dbus_connection_set_exit_on_disconnect(connection, FALSE); 59 | push_DBusConnection(L, connection, FALSE); 60 | return 1; 61 | } 62 | } 63 | 64 | static int ldbus_bus_get_private(lua_State *L) { 65 | int type = luaL_checkoption(L, 1, NULL, BusType_lst); 66 | 67 | DBusError *error = new_DBusError(L); 68 | DBusConnection *connection = dbus_bus_get_private(type, error); 69 | 70 | if (dbus_error_is_set(error)) { 71 | lua_pushboolean(L, FALSE); 72 | lua_pushstring(L, error->message); 73 | return 2; 74 | } else { 75 | dbus_connection_set_exit_on_disconnect(connection, FALSE); 76 | push_DBusConnection(L, connection, TRUE); 77 | return 1; 78 | } 79 | } 80 | 81 | static int ldbus_bus_register(lua_State *L) { 82 | DBusConnection *connection = check_DBusConnection(L, 1); 83 | 84 | DBusError *error = new_DBusError(L); 85 | dbus_bus_register(connection, error); 86 | 87 | if (dbus_error_is_set(error)) { 88 | lua_pushnil(L); 89 | lua_pushstring(L, error->message); 90 | return 2; 91 | } else { 92 | lua_pushboolean(L, TRUE); 93 | return 1; 94 | } 95 | } 96 | 97 | static int ldbus_bus_set_unique_name(lua_State *L) { 98 | DBusConnection *connection = check_DBusConnection(L, 1); 99 | const char *unique_name = luaL_checkstring(L, 2); 100 | 101 | lua_pushboolean(L, dbus_bus_set_unique_name(connection, unique_name)); 102 | 103 | return 1; 104 | } 105 | 106 | static int ldbus_bus_get_unique_name(lua_State *L) { 107 | DBusConnection *connection = check_DBusConnection(L, 1); 108 | 109 | const char *unique_name = dbus_bus_get_unique_name(connection); 110 | if (unique_name == NULL) { 111 | lua_pushnil(L); 112 | } else { 113 | lua_pushstring(L, unique_name); 114 | } 115 | 116 | return 1; 117 | } 118 | 119 | static int ldbus_bus_request_name(lua_State *L) { 120 | DBusConnection *connection = check_DBusConnection(L, 1); 121 | const char *name = luaL_checkstring(L, 2); 122 | unsigned int flags = 0; 123 | DBusError *error; 124 | int result; 125 | 126 | switch (lua_type(L, 3)) { 127 | case LUA_TNIL: 128 | case LUA_TNONE: 129 | break; 130 | case LUA_TTABLE: 131 | lua_getfield(L, 3, "allow_replacement"); 132 | if (lua_toboolean(L, -1)) flags |= DBUS_NAME_FLAG_ALLOW_REPLACEMENT; 133 | lua_getfield(L, 3, "do_not_queue"); 134 | if (lua_toboolean(L, -1)) flags |= DBUS_NAME_FLAG_DO_NOT_QUEUE; 135 | lua_getfield(L, 3, "replace_existing"); 136 | if (lua_toboolean(L, -1)) flags |= DBUS_NAME_FLAG_REPLACE_EXISTING; 137 | break; 138 | default: 139 | return luaL_argerror(L, 3, lua_pushfstring(L, "table or nil expected, got %s", luaL_typename(L, 3))); 140 | break; 141 | } 142 | 143 | error = new_DBusError(L); 144 | result = dbus_bus_request_name(connection, name, flags, error); 145 | if (dbus_error_is_set(error)) { 146 | lua_pushnil(L); 147 | lua_pushstring(L, error->message); 148 | return 2; 149 | } else { 150 | lua_pushstring(L, Request_Name_Reply_lst [ result ]); 151 | return 1; 152 | } 153 | } 154 | 155 | static int ldbus_bus_release_name(lua_State *L) { 156 | DBusConnection *connection = check_DBusConnection(L, 1); 157 | const char *name = luaL_checkstring(L, 2); 158 | int result; 159 | 160 | DBusError *error = new_DBusError(L); 161 | result = dbus_bus_release_name(connection, name, error); 162 | 163 | if (dbus_error_is_set(error)) { 164 | lua_pushnil(L); 165 | lua_pushstring(L, error->message); 166 | return 2; 167 | } else { 168 | lua_pushstring(L, Release_Name_Reply_lst [ result ]); 169 | return 1; 170 | } 171 | } 172 | 173 | static int ldbus_bus_name_has_owner(lua_State *L) { 174 | DBusConnection *connection = check_DBusConnection(L, 1); 175 | const char *name = luaL_checkstring(L, 2); 176 | int result; 177 | 178 | DBusError *error = new_DBusError(L); 179 | result = dbus_bus_name_has_owner(connection, name, error); 180 | 181 | if (dbus_error_is_set(error)) { 182 | lua_pushnil(L); 183 | lua_pushstring(L, error->message); 184 | return 2; 185 | } else { 186 | lua_pushboolean(L, result); 187 | return 1; 188 | } 189 | } 190 | 191 | static int ldbus_bus_start_service_by_name(lua_State *L) { 192 | DBusConnection *connection = check_DBusConnection(L, 1); 193 | const char *name = luaL_checkstring(L, 2); 194 | unsigned int result; 195 | 196 | DBusError *error = new_DBusError(L); 197 | dbus_bus_start_service_by_name(connection, name, 0, &result, error); 198 | 199 | if (dbus_error_is_set(error)) { 200 | lua_pushnil(L); 201 | lua_pushstring(L, error->message); 202 | return 2; 203 | } else { 204 | lua_pushstring(L, Start_Reply_lst [ result ]); 205 | return 1; 206 | } 207 | } 208 | 209 | static int ldbus_bus_add_match(lua_State *L) { 210 | DBusConnection *connection = check_DBusConnection(L, 1); 211 | const char *rule = luaL_checkstring(L, 2); 212 | 213 | DBusError *error = new_DBusError(L); 214 | dbus_bus_add_match(connection, rule, error); 215 | 216 | if (dbus_error_is_set(error)) { 217 | lua_pushnil(L); 218 | lua_pushstring(L, error->message); 219 | return 2; 220 | } else { 221 | lua_pushboolean(L, TRUE); 222 | return 1; 223 | } 224 | } 225 | 226 | static int ldbus_bus_remove_match(lua_State *L) { 227 | DBusConnection *connection = check_DBusConnection(L, 1); 228 | const char *rule = luaL_checkstring(L, 2); 229 | 230 | DBusError *error = new_DBusError(L); 231 | dbus_bus_remove_match(connection, rule, error); 232 | 233 | if (dbus_error_is_set(error)) { 234 | lua_pushnil(L); 235 | lua_pushstring(L, error->message); 236 | return 2; 237 | } else { 238 | lua_pushboolean(L, TRUE); 239 | return 1; 240 | } 241 | } 242 | 243 | int luaopen_ldbus_bus(lua_State *L) { 244 | static const struct luaL_Reg ldbus_bus [] = { 245 | { "get", ldbus_bus_get }, 246 | { "get_private", ldbus_bus_get_private }, 247 | { "register", ldbus_bus_register }, 248 | { "set_unique_name", ldbus_bus_set_unique_name }, 249 | { "get_unique_name", ldbus_bus_get_unique_name }, 250 | { "request_name", ldbus_bus_request_name }, 251 | { "release_name", ldbus_bus_release_name }, 252 | { "name_has_owner", ldbus_bus_name_has_owner }, 253 | { "start_service_by_name", ldbus_bus_start_service_by_name }, 254 | { "add_match", ldbus_bus_add_match }, 255 | { "remove_match", ldbus_bus_remove_match }, 256 | { NULL, NULL } 257 | }; 258 | luaL_newlib(L, ldbus_bus); 259 | return 1; 260 | } 261 | -------------------------------------------------------------------------------- /vendor/compat-5.3/lutf8lib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lutf8lib.c,v 1.16.1.1 2017/04/19 17:29:57 roberto Exp $ 3 | ** Standard library for UTF-8 manipulation 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lutf8lib_c 8 | #define LUA_LIB 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | #include "lua.h" 19 | 20 | #include "lauxlib.h" 21 | #include "lualib.h" 22 | 23 | #define MAXUNICODE 0x10FFFF 24 | 25 | #define iscont(p) ((*(p) & 0xC0) == 0x80) 26 | 27 | 28 | /* from strlib */ 29 | /* translate a relative string position: negative means back from end */ 30 | static lua_Integer u_posrelat (lua_Integer pos, size_t len) { 31 | if (pos >= 0) return pos; 32 | else if (0u - (size_t)pos > len) return 0; 33 | else return (lua_Integer)len + pos + 1; 34 | } 35 | 36 | 37 | /* 38 | ** Decode one UTF-8 sequence, returning NULL if byte sequence is invalid. 39 | */ 40 | static const char *utf8_decode (const char *o, int *val) { 41 | static const unsigned int limits[] = {0xFF, 0x7F, 0x7FF, 0xFFFF}; 42 | const unsigned char *s = (const unsigned char *)o; 43 | unsigned int c = s[0]; 44 | unsigned int res = 0; /* final result */ 45 | if (c < 0x80) /* ascii? */ 46 | res = c; 47 | else { 48 | int count = 0; /* to count number of continuation bytes */ 49 | while (c & 0x40) { /* still have continuation bytes? */ 50 | int cc = s[++count]; /* read next byte */ 51 | if ((cc & 0xC0) != 0x80) /* not a continuation byte? */ 52 | return NULL; /* invalid byte sequence */ 53 | res = (res << 6) | (cc & 0x3F); /* add lower 6 bits from cont. byte */ 54 | c <<= 1; /* to test next bit */ 55 | } 56 | res |= ((c & 0x7F) << (count * 5)); /* add first byte */ 57 | if (count > 3 || res > MAXUNICODE || res <= limits[count]) 58 | return NULL; /* invalid byte sequence */ 59 | s += count; /* skip continuation bytes read */ 60 | } 61 | if (val) *val = res; 62 | return (const char *)s + 1; /* +1 to include first byte */ 63 | } 64 | 65 | 66 | /* 67 | ** utf8len(s [, i [, j]]) --> number of characters that start in the 68 | ** range [i,j], or nil + current position if 's' is not well formed in 69 | ** that interval 70 | */ 71 | static int utflen (lua_State *L) { 72 | int n = 0; 73 | size_t len; 74 | const char *s = luaL_checklstring(L, 1, &len); 75 | lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len); 76 | lua_Integer posj = u_posrelat(luaL_optinteger(L, 3, -1), len); 77 | luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 2, 78 | "initial position out of string"); 79 | luaL_argcheck(L, --posj < (lua_Integer)len, 3, 80 | "final position out of string"); 81 | while (posi <= posj) { 82 | const char *s1 = utf8_decode(s + posi, NULL); 83 | if (s1 == NULL) { /* conversion error? */ 84 | lua_pushnil(L); /* return nil ... */ 85 | lua_pushinteger(L, posi + 1); /* ... and current position */ 86 | return 2; 87 | } 88 | posi = s1 - s; 89 | n++; 90 | } 91 | lua_pushinteger(L, n); 92 | return 1; 93 | } 94 | 95 | 96 | /* 97 | ** codepoint(s, [i, [j]]) -> returns codepoints for all characters 98 | ** that start in the range [i,j] 99 | */ 100 | static int codepoint (lua_State *L) { 101 | size_t len; 102 | const char *s = luaL_checklstring(L, 1, &len); 103 | lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len); 104 | lua_Integer pose = u_posrelat(luaL_optinteger(L, 3, posi), len); 105 | int n; 106 | const char *se; 107 | luaL_argcheck(L, posi >= 1, 2, "out of range"); 108 | luaL_argcheck(L, pose <= (lua_Integer)len, 3, "out of range"); 109 | if (posi > pose) return 0; /* empty interval; return no values */ 110 | if (pose - posi >= INT_MAX) /* (lua_Integer -> int) overflow? */ 111 | return luaL_error(L, "string slice too long"); 112 | n = (int)(pose - posi) + 1; 113 | luaL_checkstack(L, n, "string slice too long"); 114 | n = 0; 115 | se = s + pose; 116 | for (s += posi - 1; s < se;) { 117 | int code; 118 | s = utf8_decode(s, &code); 119 | if (s == NULL) 120 | return luaL_error(L, "invalid UTF-8 code"); 121 | lua_pushinteger(L, code); 122 | n++; 123 | } 124 | return n; 125 | } 126 | 127 | 128 | static void pushutfchar (lua_State *L, int arg) { 129 | lua_Integer code = luaL_checkinteger(L, arg); 130 | luaL_argcheck(L, 0 <= code && code <= MAXUNICODE, arg, "value out of range"); 131 | lua_pushfstring(L, "%U", (long)code); 132 | } 133 | 134 | 135 | /* 136 | ** utfchar(n1, n2, ...) -> char(n1)..char(n2)... 137 | */ 138 | static int utfchar (lua_State *L) { 139 | int n = lua_gettop(L); /* number of arguments */ 140 | if (n == 1) /* optimize common case of single char */ 141 | pushutfchar(L, 1); 142 | else { 143 | int i; 144 | luaL_Buffer b; 145 | luaL_buffinit(L, &b); 146 | for (i = 1; i <= n; i++) { 147 | pushutfchar(L, i); 148 | luaL_addvalue(&b); 149 | } 150 | luaL_pushresult(&b); 151 | } 152 | return 1; 153 | } 154 | 155 | 156 | /* 157 | ** offset(s, n, [i]) -> index where n-th character counting from 158 | ** position 'i' starts; 0 means character at 'i'. 159 | */ 160 | static int byteoffset (lua_State *L) { 161 | size_t len; 162 | const char *s = luaL_checklstring(L, 1, &len); 163 | lua_Integer n = luaL_checkinteger(L, 2); 164 | lua_Integer posi = (n >= 0) ? 1 : len + 1; 165 | posi = u_posrelat(luaL_optinteger(L, 3, posi), len); 166 | luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3, 167 | "position out of range"); 168 | if (n == 0) { 169 | /* find beginning of current byte sequence */ 170 | while (posi > 0 && iscont(s + posi)) posi--; 171 | } 172 | else { 173 | if (iscont(s + posi)) 174 | return luaL_error(L, "initial position is a continuation byte"); 175 | if (n < 0) { 176 | while (n < 0 && posi > 0) { /* move back */ 177 | do { /* find beginning of previous character */ 178 | posi--; 179 | } while (posi > 0 && iscont(s + posi)); 180 | n++; 181 | } 182 | } 183 | else { 184 | n--; /* do not move for 1st character */ 185 | while (n > 0 && posi < (lua_Integer)len) { 186 | do { /* find beginning of next character */ 187 | posi++; 188 | } while (iscont(s + posi)); /* (cannot pass final '\0') */ 189 | n--; 190 | } 191 | } 192 | } 193 | if (n == 0) /* did it find given character? */ 194 | lua_pushinteger(L, posi + 1); 195 | else /* no such character */ 196 | lua_pushnil(L); 197 | return 1; 198 | } 199 | 200 | 201 | static int iter_aux (lua_State *L) { 202 | size_t len; 203 | const char *s = luaL_checklstring(L, 1, &len); 204 | lua_Integer n = lua_tointeger(L, 2) - 1; 205 | if (n < 0) /* first iteration? */ 206 | n = 0; /* start from here */ 207 | else if (n < (lua_Integer)len) { 208 | n++; /* skip current byte */ 209 | while (iscont(s + n)) n++; /* and its continuations */ 210 | } 211 | if (n >= (lua_Integer)len) 212 | return 0; /* no more codepoints */ 213 | else { 214 | int code; 215 | const char *next = utf8_decode(s + n, &code); 216 | if (next == NULL || iscont(next)) 217 | return luaL_error(L, "invalid UTF-8 code"); 218 | lua_pushinteger(L, n + 1); 219 | lua_pushinteger(L, code); 220 | return 2; 221 | } 222 | } 223 | 224 | 225 | static int iter_codes (lua_State *L) { 226 | luaL_checkstring(L, 1); 227 | lua_pushcfunction(L, iter_aux); 228 | lua_pushvalue(L, 1); 229 | lua_pushinteger(L, 0); 230 | return 3; 231 | } 232 | 233 | 234 | /* pattern to match a single UTF-8 character */ 235 | #define UTF8PATT "[\0-\x7F\xC2-\xF4][\x80-\xBF]*" 236 | 237 | 238 | static const luaL_Reg funcs[] = { 239 | {"offset", byteoffset}, 240 | {"codepoint", codepoint}, 241 | {"char", utfchar}, 242 | {"len", utflen}, 243 | {"codes", iter_codes}, 244 | /* placeholders */ 245 | {"charpattern", NULL}, 246 | {NULL, NULL} 247 | }; 248 | 249 | 250 | LUAMOD_API int luaopen_utf8 (lua_State *L) { 251 | luaL_newlib(L, funcs); 252 | lua_pushlstring(L, UTF8PATT, sizeof(UTF8PATT)/sizeof(char) - 1); 253 | lua_setfield(L, -2, "charpattern"); 254 | return 1; 255 | } 256 | 257 | -------------------------------------------------------------------------------- /src/ldbus.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "compat-5.3.h" 6 | 7 | #include 8 | 9 | #include "bus.h" 10 | #include "connection.h" 11 | #include "message.h" 12 | #include "watch.h" 13 | #include "ldbus.h" 14 | 15 | int tostring(lua_State *L) { 16 | if (!luaL_getmetafield(L, 1, "__udtype")) { 17 | lua_pushstring(L, "object with a generic __tostring metamethod but no __type metafield"); 18 | } 19 | lua_pushfstring(L, "%s: %p", lua_tostring(L, -1), lua_topointer(L, -2)); 20 | return 1; 21 | } 22 | 23 | static int ldbus_get_version(lua_State *L) { 24 | int maj, min, mic; 25 | dbus_get_version(&maj, &min, &mic); 26 | 27 | lua_pushinteger(L, maj); 28 | lua_pushinteger(L, min); 29 | lua_pushinteger(L, mic); 30 | 31 | return 3; 32 | } 33 | 34 | int luaopen_ldbus(lua_State *L) { 35 | static const struct luaL_Reg ldbus [] = { 36 | { "get_version", ldbus_get_version }, 37 | { NULL, NULL } 38 | }; 39 | luaL_newlib(L, ldbus); 40 | 41 | /* Type Constants: */ 42 | lua_newtable(L); 43 | lua_pushliteral(L, DBUS_TYPE_INVALID_AS_STRING); 44 | lua_setfield(L, -2, "invalid"); 45 | lua_pushliteral(L, DBUS_TYPE_BYTE_AS_STRING); 46 | lua_setfield(L, -2, "byte"); 47 | lua_pushliteral(L, DBUS_TYPE_BOOLEAN_AS_STRING); 48 | lua_setfield(L, -2, "boolean"); 49 | lua_pushliteral(L, DBUS_TYPE_DOUBLE_AS_STRING); 50 | lua_setfield(L, -2, "double"); 51 | lua_pushliteral(L, DBUS_TYPE_INT16_AS_STRING); 52 | lua_setfield(L, -2, "int16"); 53 | lua_pushliteral(L, DBUS_TYPE_UINT16_AS_STRING); 54 | lua_setfield(L, -2, "uint16"); 55 | lua_pushliteral(L, DBUS_TYPE_INT32_AS_STRING); 56 | lua_setfield(L, -2, "int32"); 57 | lua_pushliteral(L, DBUS_TYPE_UINT32_AS_STRING); 58 | lua_setfield(L, -2, "uint32"); 59 | lua_pushliteral(L, DBUS_TYPE_INT64_AS_STRING); 60 | lua_setfield(L, -2, "int64"); 61 | lua_pushliteral(L, DBUS_TYPE_UINT64_AS_STRING); 62 | lua_setfield(L, -2, "uint64"); 63 | lua_pushliteral(L, DBUS_TYPE_STRING_AS_STRING); 64 | lua_setfield(L, -2, "string"); 65 | lua_pushliteral(L, DBUS_TYPE_OBJECT_PATH_AS_STRING); 66 | lua_setfield(L, -2, "object_path"); 67 | lua_pushliteral(L, DBUS_TYPE_SIGNATURE_AS_STRING); 68 | lua_setfield(L, -2, "signature"); 69 | lua_pushliteral(L, DBUS_TYPE_ARRAY_AS_STRING); 70 | lua_setfield(L, -2, "array"); 71 | lua_pushliteral(L, DBUS_TYPE_VARIANT_AS_STRING); 72 | lua_setfield(L, -2, "variant"); 73 | lua_pushliteral(L, DBUS_TYPE_STRUCT_AS_STRING); 74 | lua_setfield(L, -2, "struct"); 75 | lua_pushliteral(L, DBUS_TYPE_DICT_ENTRY_AS_STRING); 76 | lua_setfield(L, -2, "dict_entry"); 77 | lua_setfield(L, -2, "types"); 78 | 79 | /* Seperate table of just the basic types */ 80 | lua_newtable(L); 81 | lua_pushliteral(L, DBUS_TYPE_BYTE_AS_STRING); 82 | lua_setfield(L, -2, "byte"); 83 | lua_pushliteral(L, DBUS_TYPE_BOOLEAN_AS_STRING); 84 | lua_setfield(L, -2, "boolean"); 85 | lua_pushliteral(L, DBUS_TYPE_DOUBLE_AS_STRING); 86 | lua_setfield(L, -2, "double"); 87 | lua_pushliteral(L, DBUS_TYPE_INT16_AS_STRING); 88 | lua_setfield(L, -2, "int16"); 89 | lua_pushliteral(L, DBUS_TYPE_UINT16_AS_STRING); 90 | lua_setfield(L, -2, "uint16"); 91 | lua_pushliteral(L, DBUS_TYPE_INT32_AS_STRING); 92 | lua_setfield(L, -2, "int32"); 93 | lua_pushliteral(L, DBUS_TYPE_UINT32_AS_STRING); 94 | lua_setfield(L, -2, "uint32"); 95 | lua_pushliteral(L, DBUS_TYPE_INT64_AS_STRING); 96 | lua_setfield(L, -2, "int64"); 97 | lua_pushliteral(L, DBUS_TYPE_UINT64_AS_STRING); 98 | lua_setfield(L, -2, "uint64"); 99 | lua_pushliteral(L, DBUS_TYPE_STRING_AS_STRING); 100 | lua_setfield(L, -2, "string"); 101 | lua_pushliteral(L, DBUS_TYPE_OBJECT_PATH_AS_STRING); 102 | lua_setfield(L, -2, "object_path"); 103 | lua_pushliteral(L, DBUS_TYPE_SIGNATURE_AS_STRING); 104 | lua_setfield(L, -2, "signature"); 105 | lua_setfield(L, -2, "basic_types"); 106 | 107 | /* Errors */ 108 | lua_newtable(L); 109 | lua_pushstring(L, DBUS_ERROR_FAILED); 110 | lua_setfield(L, -2, "Failed"); 111 | lua_pushstring(L, DBUS_ERROR_NO_MEMORY); 112 | lua_setfield(L, -2, "NoMemory"); 113 | lua_pushstring(L, DBUS_ERROR_SERVICE_UNKNOWN); 114 | lua_setfield(L, -2, "ServiceUnknown"); 115 | lua_pushstring(L, DBUS_ERROR_NAME_HAS_NO_OWNER); 116 | lua_setfield(L, -2, "NameHasNoOwner"); 117 | lua_pushstring(L, DBUS_ERROR_NO_REPLY); 118 | lua_setfield(L, -2, "NoReply"); 119 | lua_pushstring(L, DBUS_ERROR_IO_ERROR); 120 | lua_setfield(L, -2, "IOError"); 121 | lua_pushstring(L, DBUS_ERROR_BAD_ADDRESS); 122 | lua_setfield(L, -2, "BadAddress"); 123 | lua_pushstring(L, DBUS_ERROR_NOT_SUPPORTED); 124 | lua_setfield(L, -2, "NotSupported"); 125 | lua_pushstring(L, DBUS_ERROR_LIMITS_EXCEEDED); 126 | lua_setfield(L, -2, "LimitsExceeded"); 127 | lua_pushstring(L, DBUS_ERROR_ACCESS_DENIED); 128 | lua_setfield(L, -2, "AccessDenied"); 129 | lua_pushstring(L, DBUS_ERROR_AUTH_FAILED); 130 | lua_setfield(L, -2, "AuthFailed"); 131 | lua_pushstring(L, DBUS_ERROR_NO_SERVER); 132 | lua_setfield(L, -2, "NoServer"); 133 | lua_pushstring(L, DBUS_ERROR_TIMEOUT); 134 | lua_setfield(L, -2, "Timeout"); 135 | lua_pushstring(L, DBUS_ERROR_NO_NETWORK); 136 | lua_setfield(L, -2, "NoNetwork"); 137 | lua_pushstring(L, DBUS_ERROR_ADDRESS_IN_USE); 138 | lua_setfield(L, -2, "AddressInUse"); 139 | lua_pushstring(L, DBUS_ERROR_DISCONNECTED); 140 | lua_setfield(L, -2, "Disconnected"); 141 | lua_pushstring(L, DBUS_ERROR_INVALID_ARGS); 142 | lua_setfield(L, -2, "InvalidArgs"); 143 | lua_pushstring(L, DBUS_ERROR_FILE_NOT_FOUND); 144 | lua_setfield(L, -2, "FileNotFound"); 145 | lua_pushstring(L, DBUS_ERROR_FILE_EXISTS); 146 | lua_setfield(L, -2, "FileExists"); 147 | lua_pushstring(L, DBUS_ERROR_UNKNOWN_METHOD); 148 | lua_setfield(L, -2, "UnknownMethod"); 149 | lua_pushstring(L, DBUS_ERROR_TIMED_OUT); 150 | lua_setfield(L, -2, "TimedOut"); 151 | lua_pushstring(L, DBUS_ERROR_MATCH_RULE_NOT_FOUND); 152 | lua_setfield(L, -2, "MatchRuleNotFound"); 153 | lua_pushstring(L, DBUS_ERROR_MATCH_RULE_INVALID); 154 | lua_setfield(L, -2, "MatchRuleInvalid"); 155 | lua_newtable(L); 156 | lua_pushstring(L, DBUS_ERROR_SPAWN_EXEC_FAILED); 157 | lua_setfield(L, -2, "ExecFailed"); 158 | lua_pushstring(L, DBUS_ERROR_SPAWN_FORK_FAILED); 159 | lua_setfield(L, -2, "ForkFailed"); 160 | lua_pushstring(L, DBUS_ERROR_SPAWN_CHILD_EXITED); 161 | lua_setfield(L, -2, "ChildExited"); 162 | lua_pushstring(L, DBUS_ERROR_SPAWN_CHILD_SIGNALED); 163 | lua_setfield(L, -2, "ChildSignaled"); 164 | lua_pushstring(L, DBUS_ERROR_SPAWN_FAILED); 165 | lua_setfield(L, -2, "Failed"); 166 | lua_pushstring(L, DBUS_ERROR_SPAWN_SETUP_FAILED); 167 | lua_setfield(L, -2, "FailedToSetup"); 168 | lua_pushstring(L, DBUS_ERROR_SPAWN_CONFIG_INVALID); 169 | lua_setfield(L, -2, "ConfigInvalid"); 170 | lua_pushstring(L, DBUS_ERROR_SPAWN_SERVICE_INVALID); 171 | lua_setfield(L, -2, "ServiceNotValid"); 172 | lua_pushstring(L, DBUS_ERROR_SPAWN_SERVICE_NOT_FOUND); 173 | lua_setfield(L, -2, "ServiceNotFound"); 174 | lua_pushstring(L, DBUS_ERROR_SPAWN_PERMISSIONS_INVALID); 175 | lua_setfield(L, -2, "PermissionsInvalid"); 176 | lua_pushstring(L, DBUS_ERROR_SPAWN_FILE_INVALID); 177 | lua_setfield(L, -2, "FileInvalid"); 178 | lua_pushstring(L, DBUS_ERROR_SPAWN_NO_MEMORY); 179 | lua_setfield(L, -2, "NoMemory"); 180 | lua_setfield(L, -2, "Spawn"); 181 | lua_pushstring(L, DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN); 182 | lua_setfield(L, -2, "UnixProcessIdUnknown"); 183 | lua_pushstring(L, DBUS_ERROR_INVALID_SIGNATURE); 184 | lua_setfield(L, -2, "InvalidSignature"); 185 | lua_pushstring(L, DBUS_ERROR_INVALID_FILE_CONTENT); 186 | lua_setfield(L, -2, "InvalidFileContent"); 187 | lua_pushstring(L, DBUS_ERROR_SELINUX_SECURITY_CONTEXT_UNKNOWN); 188 | lua_setfield(L, -2, "SELinuxSecurityContextUnknown"); 189 | 190 | /* Unofficial but common additions: */ 191 | lua_pushstring(L, "org.freedesktop.UnknownObject"); 192 | lua_setfield(L, -2, "UnknownObject"); 193 | lua_pushstring(L, "org.freedesktop.UnknownMethod"); 194 | lua_setfield(L, -2, "UnknownMethod"); 195 | lua_setfield(L, -2, "errors"); 196 | 197 | luaL_requiref(L, "ldbus.connection", luaopen_ldbus_connection, 0); 198 | lua_setfield(L, -2, "connection"); 199 | 200 | luaL_requiref(L, "ldbus.bus", luaopen_ldbus_bus, 0); 201 | lua_setfield(L, -2, "bus"); 202 | 203 | luaL_requiref(L, "ldbus.message", luaopen_ldbus_message, 0); 204 | lua_setfield(L, -2, "message"); 205 | 206 | luaL_requiref(L, "ldbus.watch", luaopen_ldbus_watch, 0); 207 | lua_setfield(L, -2, "watch"); 208 | 209 | return 1; 210 | } 211 | -------------------------------------------------------------------------------- /vendor/compat-5.3/tests/testmod.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "compat-5.3.h" 5 | 6 | 7 | static int test_isinteger (lua_State *L) { 8 | lua_pushboolean(L, lua_isinteger(L, 1)); 9 | return 1; 10 | } 11 | 12 | 13 | static int test_rotate (lua_State *L) { 14 | int r = (int)luaL_checkinteger(L, 1); 15 | int n = lua_gettop(L)-1; 16 | luaL_argcheck(L, (r < 0 ? -r : r) <= n, 1, "not enough arguments"); 17 | lua_rotate(L, 2, r); 18 | return n; 19 | } 20 | 21 | 22 | static int test_str2num (lua_State *L) { 23 | const char *s = luaL_checkstring(L, 1); 24 | size_t len = lua_stringtonumber(L, s); 25 | if (len == 0) 26 | lua_pushnumber(L, 0); 27 | lua_pushinteger(L, (lua_Integer)len); 28 | return 2; 29 | } 30 | 31 | 32 | static int my_mod (lua_State *L ) { 33 | lua_newtable(L); 34 | lua_pushboolean(L, 1); 35 | lua_setfield(L, -2, "boolean"); 36 | return 1; 37 | } 38 | 39 | static int test_requiref (lua_State *L) { 40 | lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); 41 | lua_newtable(L); 42 | lua_pushboolean(L, 0); 43 | lua_setfield(L, -2, "boolean"); 44 | lua_setfield(L, -2, "requiref3"); 45 | lua_pop(L, 1); 46 | luaL_requiref(L, "requiref1", my_mod, 0); 47 | luaL_requiref(L, "requiref2", my_mod, 1); 48 | luaL_requiref(L, "requiref3", my_mod, 1); 49 | return 3; 50 | } 51 | 52 | static int test_getseti (lua_State *L) { 53 | lua_Integer k = luaL_checkinteger(L, 2); 54 | lua_Integer n = 0; 55 | if (lua_geti(L, 1, k) == LUA_TNUMBER) { 56 | n = lua_tointeger(L, -1); 57 | } else { 58 | lua_pop(L, 1); 59 | lua_pushinteger(L, n); 60 | } 61 | lua_pushinteger(L, n+1); 62 | lua_seti(L, 1, k); 63 | return 1; 64 | } 65 | 66 | 67 | #ifndef LUA_EXTRASPACE 68 | #define LUA_EXTRASPACE (sizeof(void*)) 69 | #endif 70 | 71 | static int test_getextraspace (lua_State *L) { 72 | size_t len = 0; 73 | char const* s = luaL_optlstring(L, 1, NULL, &len); 74 | char* p = (char*)lua_getextraspace(L); 75 | lua_pushstring(L, p); 76 | if (s) 77 | memcpy(p, s, len > LUA_EXTRASPACE-1 ? LUA_EXTRASPACE-1 : len+1); 78 | return 1; 79 | } 80 | 81 | 82 | /* additional tests for Lua5.1 */ 83 | #define NUP 3 84 | 85 | static int test_newproxy (lua_State *L) { 86 | lua_settop(L, 0); 87 | lua_newuserdata(L, 0); 88 | lua_newtable(L); 89 | lua_pushvalue(L, -1); 90 | lua_pushboolean(L, 1); 91 | lua_setfield(L, -2, "__gc"); 92 | lua_setmetatable(L, -3); 93 | return 2; 94 | } 95 | 96 | static int test_absindex (lua_State *L) { 97 | int i = 1; 98 | for (i = 1; i <= NUP; ++i) 99 | lua_pushvalue(L, lua_absindex(L, lua_upvalueindex(i))); 100 | lua_pushvalue(L, lua_absindex(L, LUA_REGISTRYINDEX)); 101 | lua_pushstring(L, lua_typename(L, lua_type(L, lua_absindex(L, -1)))); 102 | lua_replace(L, lua_absindex(L, -2)); 103 | lua_pushvalue(L, lua_absindex(L, -2)); 104 | lua_pushvalue(L, lua_absindex(L, -4)); 105 | lua_pushvalue(L, lua_absindex(L, -6)); 106 | i += 3; 107 | lua_pushvalue(L, lua_absindex(L, 1)); 108 | lua_pushvalue(L, lua_absindex(L, 2)); 109 | lua_pushvalue(L, lua_absindex(L, 3)); 110 | i += 3; 111 | return i; 112 | } 113 | 114 | static int test_arith (lua_State *L) { 115 | lua_settop(L, 2); 116 | lua_pushvalue(L, 1); 117 | lua_pushvalue(L, 2); 118 | lua_arith(L, LUA_OPADD); 119 | lua_pushvalue(L, 1); 120 | lua_pushvalue(L, 2); 121 | lua_arith(L, LUA_OPSUB); 122 | lua_pushvalue(L, 1); 123 | lua_pushvalue(L, 2); 124 | lua_arith(L, LUA_OPMUL); 125 | lua_pushvalue(L, 1); 126 | lua_pushvalue(L, 2); 127 | lua_arith(L, LUA_OPDIV); 128 | lua_pushvalue(L, 1); 129 | lua_pushvalue(L, 2); 130 | lua_arith(L, LUA_OPMOD); 131 | lua_pushvalue(L, 1); 132 | lua_pushvalue(L, 2); 133 | lua_arith(L, LUA_OPPOW); 134 | lua_pushvalue(L, 1); 135 | lua_arith(L, LUA_OPUNM); 136 | return lua_gettop(L)-2; 137 | } 138 | 139 | static int test_compare (lua_State *L) { 140 | luaL_checknumber(L, 1); 141 | luaL_checknumber(L, 2); 142 | lua_settop(L, 2); 143 | lua_pushboolean(L, lua_compare(L, 1, 2, LUA_OPEQ)); 144 | lua_pushboolean(L, lua_compare(L, 1, 2, LUA_OPLT)); 145 | lua_pushboolean(L, lua_compare(L, 1, 2, LUA_OPLE)); 146 | return 3; 147 | } 148 | 149 | static int test_globals (lua_State *L) { 150 | lua_pushglobaltable(L); 151 | return 1; 152 | } 153 | 154 | static int test_tonumber (lua_State *L) { 155 | int isnum = 0; 156 | lua_Number n = lua_tonumberx(L, 1, &isnum); 157 | if (!isnum) 158 | lua_pushnil(L); 159 | else 160 | lua_pushnumber(L, n); 161 | return 1; 162 | } 163 | 164 | static int test_tointeger (lua_State *L) { 165 | int isnum = 0; 166 | lua_Integer n = lua_tointegerx(L, 1, &isnum); 167 | if (!isnum) 168 | lua_pushnil(L); 169 | else 170 | lua_pushinteger(L, n); 171 | lua_pushinteger(L, lua_tointeger(L, 1)); 172 | return 2; 173 | } 174 | 175 | static int test_len (lua_State *L) { 176 | luaL_checkany(L, 1); 177 | lua_len(L, 1); 178 | lua_pushinteger(L, luaL_len(L, 1)); 179 | return 2; 180 | } 181 | 182 | static int test_copy (lua_State *L) { 183 | int args = lua_gettop(L); 184 | if (args >= 2) { 185 | int i = 0; 186 | for (i = args-1; i > 0; --i) 187 | lua_copy(L, args, i); 188 | } 189 | return args; 190 | } 191 | 192 | /* need an address */ 193 | static char const dummy = 0; 194 | 195 | static int test_rawxetp (lua_State *L) { 196 | if (lua_gettop(L) > 0) 197 | lua_pushvalue(L, 1); 198 | else 199 | lua_pushliteral(L, "hello again"); 200 | lua_rawsetp(L, LUA_REGISTRYINDEX, &dummy); 201 | lua_settop(L, 0); 202 | lua_rawgetp(L, LUA_REGISTRYINDEX, &dummy); 203 | return 1; 204 | } 205 | 206 | static int test_udata (lua_State *L) { 207 | const char *tname = luaL_optstring(L, 1, "utype1"); 208 | void *u1 = lua_newuserdata(L, 1); 209 | int u1pos = lua_gettop(L); 210 | void *u2 = lua_newuserdata(L, 1); 211 | int u2pos = lua_gettop(L); 212 | luaL_newmetatable(L, "utype1"); 213 | luaL_newmetatable(L, "utype2"); 214 | lua_pop(L, 2); 215 | luaL_setmetatable(L, "utype2"); 216 | lua_pushvalue(L, u1pos); 217 | luaL_setmetatable(L, "utype1"); 218 | lua_pop(L, 1); 219 | (void)u1; 220 | (void)u2; 221 | lua_pushlightuserdata(L, luaL_testudata(L, u1pos, tname)); 222 | lua_pushlightuserdata(L, luaL_testudata(L, u2pos, tname)); 223 | luaL_getmetatable(L, "utype1"); 224 | lua_getfield(L, -1, "__name"); 225 | lua_replace(L, -2); 226 | return 3; 227 | } 228 | 229 | static int test_subtable (lua_State *L) { 230 | luaL_checktype(L, 1, LUA_TTABLE); 231 | lua_settop(L, 1); 232 | if (luaL_getsubtable(L, 1, "xxx")) { 233 | lua_pushliteral(L, "oldtable"); 234 | } else { 235 | lua_pushliteral(L, "newtable"); 236 | } 237 | return 2; 238 | } 239 | 240 | static int test_uservalue (lua_State *L) { 241 | void *udata = lua_newuserdata(L, 1); 242 | int ui = lua_gettop(L); 243 | lua_newtable(L); 244 | lua_setuservalue(L, ui); 245 | lua_pushinteger(L, lua_getuservalue(L, ui)); 246 | (void)udata; 247 | return 2; 248 | } 249 | 250 | static int test_upvalues (lua_State *L) { 251 | int i = 1; 252 | for (i = 1; i <= NUP; ++i) 253 | lua_pushvalue(L, lua_upvalueindex(i)); 254 | return NUP; 255 | } 256 | 257 | static int test_tolstring (lua_State *L) { 258 | size_t len = 0; 259 | luaL_tolstring(L, 1, &len); 260 | lua_pushinteger(L, (int)len); 261 | return 2; 262 | } 263 | 264 | static int test_pushstring (lua_State *L) { 265 | lua_pushstring(L, lua_pushliteral(L, "abc")); 266 | lua_pushstring(L, lua_pushlstring(L, "abc", 2)); 267 | lua_pushstring(L, lua_pushlstring(L, NULL, 0)); 268 | lua_pushstring(L, lua_pushstring(L, "abc")); 269 | lua_pushboolean(L, NULL == lua_pushstring(L, NULL)); 270 | return 10; 271 | } 272 | 273 | static int test_buffer (lua_State *L) { 274 | luaL_Buffer b; 275 | char *p = luaL_buffinitsize(L, &b, LUAL_BUFFERSIZE+1); 276 | p[0] = 'a'; 277 | p[1] = 'b'; 278 | luaL_addsize(&b, 2); 279 | luaL_addstring(&b, "c"); 280 | lua_pushliteral(L, "d"); 281 | luaL_addvalue(&b); 282 | luaL_addchar(&b, 'e'); 283 | luaL_pushresult(&b); 284 | return 1; 285 | } 286 | 287 | static int test_exec (lua_State *L) { 288 | const char *cmd = luaL_checkstring(L, 1); 289 | errno = 0; 290 | return luaL_execresult(L, system(cmd)); 291 | } 292 | 293 | static int test_loadstring (lua_State *L) { 294 | size_t len = 0; 295 | char const* s = luaL_checklstring(L, 1, &len); 296 | char const* mode = luaL_optstring(L, 2, "bt"); 297 | lua_pushinteger(L, luaL_loadbufferx(L, s, len, s, mode)); 298 | return 2; 299 | } 300 | 301 | static int test_loadfile (lua_State *L) { 302 | char filename[L_tmpnam+1] = { 0 }; 303 | size_t len = 0; 304 | char const* s = luaL_checklstring(L, 1, &len); 305 | char const* mode = luaL_optstring(L, 2, "bt"); 306 | if (tmpnam(filename)) { 307 | FILE* f = fopen(filename, "wb"); 308 | if (f) { 309 | fwrite(s, 1, len, f); 310 | fclose(f); 311 | lua_pushinteger(L, luaL_loadfilex(L, filename, mode)); 312 | remove(filename); 313 | return 2; 314 | } else 315 | remove(filename); 316 | } 317 | return 0; 318 | } 319 | 320 | 321 | static const luaL_Reg funcs[] = { 322 | { "isinteger", test_isinteger }, 323 | { "rotate", test_rotate }, 324 | { "strtonum", test_str2num }, 325 | { "requiref", test_requiref }, 326 | { "getseti", test_getseti }, 327 | { "extraspace", test_getextraspace }, 328 | { "newproxy", test_newproxy }, 329 | { "arith", test_arith }, 330 | { "compare", test_compare }, 331 | { "tonumber", test_tonumber }, 332 | { "tointeger", test_tointeger }, 333 | { "len", test_len }, 334 | { "copy", test_copy }, 335 | { "rawxetp", test_rawxetp }, 336 | { "subtable", test_subtable }, 337 | { "udata", test_udata }, 338 | { "uservalue", test_uservalue }, 339 | { "globals", test_globals }, 340 | { "tolstring", test_tolstring }, 341 | { "pushstring", test_pushstring }, 342 | { "buffer", test_buffer }, 343 | { "exec", test_exec }, 344 | { "loadstring", test_loadstring }, 345 | { "loadfile", test_loadfile }, 346 | { NULL, NULL } 347 | }; 348 | 349 | static const luaL_Reg more_funcs[] = { 350 | { "getupvalues", test_upvalues }, 351 | { "absindex", test_absindex }, 352 | { NULL, NULL } 353 | }; 354 | 355 | 356 | #ifdef __cplusplus 357 | extern "C" { 358 | #endif 359 | int luaopen_testmod (lua_State *L) { 360 | int i = 1; 361 | luaL_newlib(L, funcs); 362 | for (i = 1; i <= NUP; ++i) 363 | lua_pushnumber(L, i); 364 | luaL_setfuncs(L, more_funcs, NUP); 365 | return 1; 366 | } 367 | #ifdef __cplusplus 368 | } 369 | #endif 370 | 371 | -------------------------------------------------------------------------------- /vendor/compat-5.3/README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/keplerproject/lua-compat-5.3.svg?branch=master)](https://travis-ci.org/keplerproject/lua-compat-5.3) 2 | 3 | # lua-compat-5.3 4 | 5 | Lua-5.3-style APIs for Lua 5.2 and 5.1. 6 | 7 | ## What is it 8 | 9 | This is a small module that aims to make it easier to write code 10 | in a Lua-5.3-style that is compatible with Lua 5.1, Lua 5.2, and Lua 11 | 5.3. This does *not* make Lua 5.2 (or even Lua 5.1) entirely 12 | compatible with Lua 5.3, but it brings the API closer to that of Lua 13 | 5.3. 14 | 15 | It includes: 16 | 17 | * _For writing Lua_: The Lua module `compat53`, which can be require'd 18 | from Lua scripts and run in Lua 5.1, 5.2, and 5.3, including a 19 | backport of the `utf8` module, the 5.3 `table` module, and the 20 | string packing functions straight from the Lua 5.3 sources. 21 | * _For writing C_: A C header and file which can be linked to your 22 | Lua module written in C, providing some functions from the C API 23 | of Lua 5.3 that do not exist in Lua 5.2 or 5.1, making it easier to 24 | write C code that compiles with all three versions of liblua. 25 | 26 | ## How to use it 27 | 28 | ### Lua module 29 | 30 | ```lua 31 | require("compat53") 32 | ``` 33 | 34 | `compat53` makes changes to your global environment and does not return 35 | a meaningful return value, so the usual idiom of storing the return of 36 | `require` in a local variable makes no sense. 37 | 38 | When run under Lua 5.3+, this module does nothing. 39 | 40 | When run under Lua 5.2 or 5.1, it replaces some of your standard 41 | functions and adds new ones to bring your environment closer to that 42 | of Lua 5.3. It also tries to load the backported `utf8`, `table`, and 43 | string packing modules automatically. If unsuccessful, pure Lua 44 | versions of the new `table` functions are used as a fallback, and 45 | [Roberto's struct library][1] is tried for string packing. 46 | 47 | #### Lua submodules 48 | 49 | ```lua 50 | local _ENV = require("compat53.module") 51 | if setfenv then setfenv(1, _ENV) end 52 | ``` 53 | 54 | The `compat53.module` module does not modify the global environment, 55 | and so it is safe to use in modules without affecting other Lua files. 56 | It is supposed to be set as the current environment (see above), i.e. 57 | cherry picking individual functions from this module is expressly 58 | *not* supported!). Not all features are available when using this 59 | module (e.g. yieldable (x)pcall support, string/file methods, etc.), 60 | so it is recommended to use plain `require("compat53")` whenever 61 | possible. 62 | 63 | ### C code 64 | 65 | There are two ways of adding the C API compatibility functions/macros to 66 | your project: 67 | * If `COMPAT53_PREFIX` is *not* `#define`d, `compat-5.3.h` `#include`s 68 | `compat-5.3.c`, and all functions are made `static`. You don't have to 69 | compile/link/add `compat-5.3.c` yourself. This is useful for one-file 70 | projects. 71 | * If `COMPAT53_PREFIX` is `#define`d, all exported functions are renamed 72 | behind the scenes using this prefix to avoid linker conflicts with other 73 | code using this package. This doesn't change the way you call the 74 | compatibility functions in your code. You have to compile and link 75 | `compat-5.3.c` to your project yourself. You can change the way the 76 | functions are exported using the `COMPAT53_API` macro (e.g. if you need 77 | some `__declspec` magic). While it is technically possible to use 78 | the "lua" prefix (and it looks better in the debugger), this is 79 | discouraged because LuaJIT has started to implement its own Lua 5.2+ 80 | C API functions, and with the "lua" prefix you'd violate the 81 | one-definition rule with recent LuaJIT versions. 82 | 83 | ## What's implemented 84 | 85 | ### Lua 86 | 87 | * the `utf8` module backported from the Lua 5.3 sources 88 | * `string.pack`, `string.packsize`, and `string.unpack` from the Lua 89 | 5.3 sources or from the `struct` module. (`struct` is not 100% 90 | compatible to Lua 5.3's string packing!) (See [here][4]) 91 | * `math.maxinteger` and `math.mininteger`, `math.tointeger`, `math.type`, 92 | and `math.ult` (see [here][5]) 93 | * `assert` accepts non-string error messages 94 | * `ipairs` respects `__index` metamethod 95 | * `table.move` 96 | * `table` library respects metamethods 97 | 98 | For Lua 5.1 additionally: 99 | * `load` and `loadfile` accept `mode` and `env` parameters 100 | * `table.pack` and `table.unpack` 101 | * string patterns may contain embedded zeros (but see [here][6]) 102 | * `string.rep` accepts `sep` argument 103 | * `string.format` calls `tostring` on arguments for `%s` 104 | * `math.log` accepts base argument 105 | * `xpcall` takes additional arguments 106 | * `pcall` and `xpcall` can execute functions that yield (see 107 | [here][22] for a possible problem with `coroutine.running`) 108 | * `pairs` respects `__pairs` metamethod (see [here][7]) 109 | * `rawlen` (but `#` still doesn't respect `__len` for tables) 110 | * `package.searchers` as alias for `package.loaders` 111 | * `package.searchpath` (see [here][8]) 112 | * `coroutine` functions dealing with the main coroutine (see 113 | [here][22] for a possible problem with `coroutine.running`) 114 | * `coroutine.create` accepts functions written in C 115 | * return code of `os.execute` (see [here][9]) 116 | * `io.write` and `file:write` return file handle 117 | * `io.lines` and `file:lines` accept format arguments (like `io.read`) 118 | (see [here][10] and [here][11]) 119 | * `debug.setmetatable` returns object 120 | * `debug.getuservalue` (see [here][12]) 121 | * `debug.setuservalue` (see [here][13]) 122 | 123 | ### C 124 | 125 | * `lua_KContext` (see [here][14]) 126 | * `lua_KFunction` (see [here][14]) 127 | * `lua_dump` (extra `strip` parameter, ignored, see [here][15]) 128 | * `lua_getextraspace` (limited compatibilitiy, see [here][24]) 129 | * `lua_getfield` (return value) 130 | * `lua_geti` and `lua_seti` 131 | * `lua_getglobal` (return value) 132 | * `lua_getmetafield` (return value) 133 | * `lua_gettable` (return value) 134 | * `lua_getuservalue` (limited compatibility, see [here][16]) 135 | * `lua_setuservalue` (limited compatibility, see [here][17]) 136 | * `lua_isinteger` 137 | * `lua_numbertointeger` 138 | * `lua_callk` and `lua_pcallk` (limited compatibility, see [here][14]) 139 | * `lua_resume` 140 | * `lua_rawget` and `lua_rawgeti` (return values) 141 | * `lua_rawgetp` and `lua_rawsetp` 142 | * `luaL_requiref` (now checks `package.loaded` first) 143 | * `lua_rotate` 144 | * `lua_stringtonumber` (see [here][18]) 145 | 146 | For Lua 5.1 additionally: 147 | * `LUA_OK` 148 | * `LUA_ERRGCMM` 149 | * `LUA_OP*` macros for `lua_arith` and `lua_compare` 150 | * `LUA_FILEHANDLE` 151 | * `lua_Unsigned` 152 | * `luaL_Stream` (limited compatibility, see [here][19]) 153 | * `lua_absindex` 154 | * `lua_arith` (see [here][20]) 155 | * `lua_compare` 156 | * `lua_len`, `lua_rawlen`, and `luaL_len` 157 | * `lua_load` (mode argument) 158 | * `lua_pushstring`, `lua_pushlstring` (return value) 159 | * `lua_copy` 160 | * `lua_pushglobaltable` 161 | * `luaL_testudata` 162 | * `luaL_setfuncs`, `luaL_newlibtable`, and `luaL_newlib` 163 | * `luaL_setmetatable` 164 | * `luaL_getsubtable` 165 | * `luaL_traceback` 166 | * `luaL_execresult` 167 | * `luaL_fileresult` 168 | * `luaL_loadbufferx` 169 | * `luaL_loadfilex` 170 | * `luaL_checkversion` (with empty body, only to avoid compile errors, 171 | see [here][21]) 172 | * `luaL_tolstring` 173 | * `luaL_buffinitsize`, `luaL_prepbuffsize`, and `luaL_pushresultsize` 174 | (see [here][22]) 175 | * `lua_pushunsigned`, `lua_tounsignedx`, `lua_tounsigned`, 176 | `luaL_checkunsigned`, `luaL_optunsigned`, if 177 | `LUA_COMPAT_APIINTCASTS` is defined. 178 | 179 | ## What's not implemented 180 | 181 | * bit operators 182 | * integer division operator 183 | * utf8 escape sequences 184 | * 64 bit integers 185 | * `coroutine.isyieldable` 186 | * Lua 5.1: `_ENV`, `goto`, labels, ephemeron tables, etc. See 187 | [`lua-compat-5.2`][2] for a detailed list. 188 | * the following C API functions/macros: 189 | * `lua_isyieldable` 190 | * `lua_arith` (new operators missing) 191 | * `lua_push(v)fstring` (new formats missing) 192 | * `lua_upvalueid` (5.1) 193 | * `lua_upvaluejoin` (5.1) 194 | * `lua_version` (5.1) 195 | * `lua_yieldk` (5.1) 196 | 197 | ## See also 198 | 199 | * For Lua-5.2-style APIs under Lua 5.1, see [lua-compat-5.2][2], 200 | which also is the basis for most of the code in this project. 201 | * For Lua-5.1-style APIs under Lua 5.0, see [Compat-5.1][3] 202 | 203 | ## Credits 204 | 205 | This package contains code written by: 206 | 207 | * [The Lua Team](http://www.lua.org) 208 | * Philipp Janda ([@siffiejoe](http://github.com/siffiejoe)) 209 | * Tomás Guisasola Gorham ([@tomasguisasola](http://github.com/tomasguisasola)) 210 | * Hisham Muhammad ([@hishamhm](http://github.com/hishamhm)) 211 | * Renato Maia ([@renatomaia](http://github.com/renatomaia)) 212 | * [@ThePhD](http://github.com/ThePhD) 213 | * [@Daurnimator](http://github.com/Daurnimator) 214 | 215 | 216 | [1]: http://www.inf.puc-rio.br/~roberto/struct/ 217 | [2]: http://github.com/keplerproject/lua-compat-5.2/ 218 | [3]: http://keplerproject.org/compat/ 219 | [4]: https://github.com/keplerproject/lua-compat-5.3/wiki/string_packing 220 | [5]: https://github.com/keplerproject/lua-compat-5.3/wiki/math.type 221 | [6]: https://github.com/keplerproject/lua-compat-5.3/wiki/pattern_matching 222 | [7]: https://github.com/keplerproject/lua-compat-5.3/wiki/pairs 223 | [8]: https://github.com/keplerproject/lua-compat-5.3/wiki/package.searchpath 224 | [9]: https://github.com/keplerproject/lua-compat-5.3/wiki/os.execute 225 | [10]: https://github.com/keplerproject/lua-compat-5.3/wiki/io.lines 226 | [11]: https://github.com/keplerproject/lua-compat-5.3/wiki/file.lines 227 | [12]: https://github.com/keplerproject/lua-compat-5.3/wiki/debug.getuservalue 228 | [13]: https://github.com/keplerproject/lua-compat-5.3/wiki/debug.setuservalue 229 | [14]: https://github.com/keplerproject/lua-compat-5.3/wiki/yieldable_c_functions 230 | [15]: https://github.com/keplerproject/lua-compat-5.3/wiki/lua_dump 231 | [16]: https://github.com/keplerproject/lua-compat-5.3/wiki/lua_getuservalue 232 | [17]: https://github.com/keplerproject/lua-compat-5.3/wiki/lua_setuservalue 233 | [18]: https://github.com/keplerproject/lua-compat-5.3/wiki/lua_stringtonumber 234 | [19]: https://github.com/keplerproject/lua-compat-5.3/wiki/luaL_Stream 235 | [20]: https://github.com/keplerproject/lua-compat-5.3/wiki/lua_arith 236 | [21]: https://github.com/keplerproject/lua-compat-5.3/wiki/luaL_checkversion 237 | [22]: https://github.com/keplerproject/lua-compat-5.3/wiki/luaL_Buffer 238 | [23]: https://github.com/keplerproject/lua-compat-5.3/wiki/coroutine.running 239 | [24]: https://github.com/keplerproject/lua-compat-5.3/wiki/lua_getextraspace 240 | 241 | -------------------------------------------------------------------------------- /src/message_iter.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | #include 7 | #include "compat-5.3.h" 8 | 9 | #include 10 | 11 | #include "ldbus.h" 12 | 13 | #include "message_iter.h" 14 | 15 | 16 | static int ldbus_message_iter_clone(lua_State *L) { 17 | lDBusMessageIter *iter = luaL_checkudata(L, 1, DBUS_MESSAGE_ITER_METATABLE); 18 | lDBusMessageIter *clone; 19 | push_DBusMessageIter(L); 20 | clone = lua_touserdata(L, -1); 21 | memcpy(clone, iter, sizeof(lDBusMessageIter)); 22 | 23 | if (clone->message) { 24 | dbus_message_ref(clone->message); 25 | } 26 | 27 | return 1; 28 | } 29 | 30 | static int ldbus_message_iter_has_next(lua_State *L) { 31 | DBusMessageIter *iter = luaL_checkudata(L, 1, DBUS_MESSAGE_ITER_METATABLE); 32 | 33 | lua_pushboolean(L, dbus_message_iter_has_next(iter)); 34 | 35 | return 1; 36 | } 37 | 38 | static int ldbus_message_iter_next(lua_State *L) { 39 | DBusMessageIter *iter = luaL_checkudata(L, 1, DBUS_MESSAGE_ITER_METATABLE); 40 | 41 | lua_pushboolean(L, dbus_message_iter_next(iter)); 42 | 43 | return 1; 44 | } 45 | 46 | static int ldbus_message_iter_get_arg_type(lua_State *L) { 47 | DBusMessageIter *iter = luaL_checkudata(L, 1, DBUS_MESSAGE_ITER_METATABLE); 48 | 49 | char type = (char)dbus_message_iter_get_arg_type(iter); 50 | if (type == DBUS_TYPE_INVALID) { 51 | lua_pushnil(L); 52 | } else { 53 | lua_pushlstring(L, &type, 1); 54 | } 55 | 56 | return 1; 57 | } 58 | 59 | static int ldbus_message_iter_get_element_count(lua_State *L) { 60 | DBusMessageIter *iter = luaL_checkudata(L, 1, DBUS_MESSAGE_ITER_METATABLE); 61 | 62 | lua_pushinteger(L, dbus_message_iter_get_element_count(iter)); 63 | 64 | return 1; 65 | } 66 | 67 | static int ldbus_message_iter_get_element_type(lua_State *L) { 68 | DBusMessageIter *iter = luaL_checkudata(L, 1, DBUS_MESSAGE_ITER_METATABLE); 69 | 70 | char type = (char)dbus_message_iter_get_element_type(iter); 71 | if (type == DBUS_TYPE_INVALID) { 72 | lua_pushnil(L); 73 | } else { 74 | lua_pushlstring(L, &type, 1); 75 | } 76 | 77 | return 1; 78 | } 79 | 80 | static int ldbus_message_iter_recurse(lua_State *L) { 81 | lDBusMessageIter *iter = luaL_checkudata(L, 1, DBUS_MESSAGE_ITER_METATABLE); 82 | lDBusMessageIter *sub; 83 | if (lua_gettop(L) == 1) { 84 | push_DBusMessageIter(L); 85 | sub = lua_touserdata(L, 2); 86 | } else { 87 | lua_settop(L, 2); 88 | sub = luaL_checkudata(L, 2, DBUS_MESSAGE_ITER_METATABLE); 89 | /* remove possible reference to previously referred message */ 90 | unref_ldbus_message_iter(sub); 91 | } 92 | 93 | dbus_message_iter_recurse(&iter->iter, &sub->iter); 94 | sub->message = iter->message; 95 | dbus_message_ref(sub->message); 96 | 97 | return 1; 98 | } 99 | 100 | static int ldbus_message_iter_get_signature(lua_State *L) { 101 | DBusMessageIter *iter = luaL_checkudata(L, 1, DBUS_MESSAGE_ITER_METATABLE); 102 | 103 | char * sig = dbus_message_iter_get_signature(iter); 104 | 105 | lua_pushstring(L, sig); 106 | 107 | dbus_free(sig); 108 | 109 | return 1; 110 | } 111 | static int ldbus_message_iter_get_basic(lua_State *L) { 112 | DBusMessageIter *iter = luaL_checkudata(L, 1, DBUS_MESSAGE_ITER_METATABLE); 113 | 114 | DBusBasicValue value; 115 | switch (dbus_message_iter_get_arg_type(iter)) { 116 | case DBUS_TYPE_BOOLEAN: 117 | dbus_message_iter_get_basic(iter, &value.u32); 118 | lua_pushboolean(L, value.u32); 119 | break; 120 | case DBUS_TYPE_BYTE: 121 | dbus_message_iter_get_basic(iter, &value.byt); 122 | lua_pushinteger(L, value.byt); 123 | break; 124 | case DBUS_TYPE_INT16: 125 | dbus_message_iter_get_basic(iter, &value.i16); 126 | lua_pushinteger(L, value.i16); 127 | break; 128 | case DBUS_TYPE_UINT16: 129 | dbus_message_iter_get_basic(iter, &value.u16); 130 | lua_pushinteger(L, value.u16); 131 | break; 132 | case DBUS_TYPE_INT32: 133 | dbus_message_iter_get_basic(iter, &value.i32); 134 | lua_pushinteger(L, value.i32); 135 | break; 136 | case DBUS_TYPE_UINT32: 137 | dbus_message_iter_get_basic(iter, &value.u32); 138 | lua_pushinteger(L, value.u32); 139 | break; 140 | case DBUS_TYPE_INT64: 141 | dbus_message_iter_get_basic(iter, &value.i64); 142 | lua_pushinteger(L, value.i64); 143 | break; 144 | case DBUS_TYPE_UINT64: 145 | dbus_message_iter_get_basic(iter, &value.u64); 146 | lua_pushinteger(L, value.u64); 147 | break; 148 | case DBUS_TYPE_DOUBLE: 149 | dbus_message_iter_get_basic(iter, &value.dbl); 150 | lua_pushnumber(L, value.dbl); 151 | break; 152 | case DBUS_TYPE_STRING: 153 | case DBUS_TYPE_OBJECT_PATH: 154 | case DBUS_TYPE_SIGNATURE: 155 | dbus_message_iter_get_basic(iter, &value.str); 156 | lua_pushstring(L, value.str); 157 | break; 158 | case DBUS_TYPE_ARRAY: 159 | case DBUS_TYPE_STRUCT: 160 | case DBUS_TYPE_DICT_ENTRY: 161 | case DBUS_TYPE_VARIANT: 162 | default: 163 | lua_pushnil(L); 164 | lua_pushstring(L, "Encountered non-basic type"); 165 | return 2; 166 | } 167 | return 1; 168 | } 169 | 170 | static int ldbus_message_iter_append_basic(lua_State *L) { 171 | int argtype; 172 | size_t l; 173 | DBusBasicValue value; 174 | DBusMessageIter *iter = luaL_checkudata(L, 1, DBUS_MESSAGE_ITER_METATABLE); 175 | luaL_checkany(L, 2); 176 | 177 | /* Get basic type we're appending... 0 if we want to use what's natural for lua. */ 178 | switch (lua_type(L, 3)) { 179 | case LUA_TNONE: 180 | case LUA_TNIL: 181 | argtype = 0; 182 | break; 183 | default: 184 | argtype = (int) luaL_checklstring(L, 3, &l) [ 0 ]; 185 | if (l != 1) return luaL_argerror(L, 3, lua_pushfstring(L, "character or nil expected, got %s", luaL_typename(L, 3))); 186 | break; 187 | } 188 | 189 | switch (lua_type(L, 2)) { 190 | case LUA_TNUMBER: 191 | if (lua_isinteger(L, 2)) { 192 | value.i64 = lua_tointeger(L, 2); 193 | switch (argtype) { 194 | case 0: 195 | argtype = DBUS_TYPE_INT64; 196 | case DBUS_TYPE_INT64: 197 | break; 198 | case DBUS_TYPE_BYTE: 199 | value.byt = value.i64; 200 | break; 201 | case DBUS_TYPE_INT16: 202 | value.i16 = value.i64; 203 | break; 204 | case DBUS_TYPE_UINT16: 205 | value.u16 = value.i64; 206 | break; 207 | case DBUS_TYPE_INT32: 208 | value.i32 = value.i64; 209 | break; 210 | case DBUS_TYPE_BOOLEAN: 211 | case DBUS_TYPE_UINT32: 212 | value.u32 = value.i64; 213 | break; 214 | case DBUS_TYPE_UINT64: 215 | value.u64 = value.i64; 216 | break; 217 | case DBUS_TYPE_DOUBLE: 218 | value.dbl = value.i64; 219 | break; 220 | default: 221 | return luaL_argerror(L, 2, "cannot convert number to given type"); 222 | } 223 | } else { 224 | value.dbl = lua_tonumber(L, 2); 225 | switch (argtype) { 226 | case 0: 227 | argtype = DBUS_TYPE_DOUBLE; /* Lua numbers are doubles by default */ 228 | case DBUS_TYPE_DOUBLE: 229 | break; 230 | case DBUS_TYPE_BYTE: 231 | value.byt = value.dbl; 232 | break; 233 | case DBUS_TYPE_INT16: 234 | value.i16 = value.dbl; 235 | break; 236 | case DBUS_TYPE_UINT16: 237 | value.u16 = value.dbl; 238 | break; 239 | case DBUS_TYPE_INT32: 240 | value.i32 = value.dbl; 241 | break; 242 | case DBUS_TYPE_BOOLEAN: 243 | case DBUS_TYPE_UINT32: 244 | value.u32 = value.dbl; 245 | break; 246 | case DBUS_TYPE_INT64: 247 | value.i64 = value.dbl; 248 | break; 249 | case DBUS_TYPE_UINT64: 250 | value.u64 = value.dbl; 251 | break; 252 | default: 253 | return luaL_argerror(L, 2, "cannot convert number to given type"); 254 | } 255 | } 256 | break; 257 | case LUA_TBOOLEAN: 258 | value.u32 = lua_toboolean(L, 2); 259 | switch (argtype) { 260 | case 0: 261 | argtype = DBUS_TYPE_BOOLEAN; 262 | case DBUS_TYPE_BOOLEAN: 263 | case DBUS_TYPE_BYTE: 264 | case DBUS_TYPE_INT16: 265 | case DBUS_TYPE_UINT16: 266 | case DBUS_TYPE_INT32: 267 | case DBUS_TYPE_UINT32: 268 | case DBUS_TYPE_INT64: 269 | case DBUS_TYPE_UINT64: 270 | break; 271 | case DBUS_TYPE_DOUBLE: 272 | value.dbl = value.u32; 273 | break; 274 | default: 275 | return luaL_argerror(L, 2, "cannot convert boolean to given type"); 276 | } 277 | break; 278 | case LUA_TSTRING: 279 | value.str = (char *)lua_tostring(L, 2); 280 | switch (argtype) { 281 | case 0: 282 | argtype = DBUS_TYPE_STRING; 283 | case DBUS_TYPE_STRING: 284 | case DBUS_TYPE_OBJECT_PATH: 285 | case DBUS_TYPE_SIGNATURE: 286 | break; 287 | default: 288 | return luaL_argerror(L, 2, "cannot convert string to given type"); 289 | } 290 | break; 291 | case LUA_TNIL: 292 | case LUA_TTABLE: 293 | case LUA_TFUNCTION: 294 | case LUA_TUSERDATA: 295 | case LUA_TTHREAD: 296 | case LUA_TLIGHTUSERDATA: 297 | default: 298 | return luaL_argerror(L, 2, lua_pushfstring(L, "number, boolean or string expected, got %s", luaL_typename(L, 2))); 299 | } 300 | 301 | lua_pushboolean(L, dbus_message_iter_append_basic(iter, argtype, &value)); 302 | 303 | return 1; 304 | } 305 | 306 | static int ldbus_message_iter_open_container(lua_State *L) { 307 | lDBusMessageIter *iter = luaL_checkudata(L, 1, DBUS_MESSAGE_ITER_METATABLE); 308 | int argtype; 309 | const char *contained_signature; 310 | lDBusMessageIter *sub; 311 | if (lua_type(L, 2) != LUA_TSTRING || lua_rawlen(L, 2) != 1) { 312 | return luaL_argerror(L, 2, lua_pushfstring(L, "character expected, got %s", luaL_typename(L, 2))); 313 | } 314 | argtype = lua_tostring(L, 2)[0]; 315 | contained_signature = luaL_optstring(L, 3, NULL); 316 | if (lua_gettop(L) < 4) { 317 | push_DBusMessageIter(L); 318 | sub = lua_touserdata(L, -1); 319 | } else { 320 | lua_settop(L, 4); 321 | sub = luaL_checkudata(L, 4, DBUS_MESSAGE_ITER_METATABLE); 322 | /* remove possible reference to previously referred message */ 323 | unref_ldbus_message_iter(sub); 324 | } 325 | 326 | if (!dbus_message_iter_open_container(&iter->iter, argtype, contained_signature, &sub->iter)) { 327 | sub->message = NULL; 328 | return luaL_error(L, LDBUS_NO_MEMORY); 329 | } 330 | 331 | sub->message = iter->message; 332 | dbus_message_ref(sub->message); 333 | 334 | return 1; 335 | } 336 | 337 | static int ldbus_message_iter_close_container(lua_State *L) { 338 | DBusMessageIter *iter = luaL_checkudata(L, 1, DBUS_MESSAGE_ITER_METATABLE); 339 | DBusMessageIter *sub = luaL_checkudata(L, 2, DBUS_MESSAGE_ITER_METATABLE); 340 | 341 | lua_pushboolean(L, dbus_message_iter_close_container(iter, sub)); 342 | 343 | return 1; 344 | } 345 | 346 | LDBUS_INTERNAL void unref_ldbus_message_iter(lDBusMessageIter *iter) { 347 | if (iter->message) { 348 | dbus_message_unref(iter->message); 349 | iter->message = NULL; 350 | } 351 | } 352 | 353 | static int ldbus_message_iter_gc(lua_State *L) 354 | { 355 | lDBusMessageIter *iter = luaL_checkudata(L, 1, DBUS_MESSAGE_ITER_METATABLE); 356 | 357 | unref_ldbus_message_iter(iter); 358 | 359 | return 0; 360 | } 361 | 362 | LDBUS_INTERNAL int push_DBusMessageIter(lua_State *L) { 363 | static luaL_Reg const methods [] = { 364 | { "clone", ldbus_message_iter_clone }, 365 | { "has_next", ldbus_message_iter_has_next }, 366 | { "next", ldbus_message_iter_next }, 367 | { "get_arg_type", ldbus_message_iter_get_arg_type }, 368 | { "get_element_count", ldbus_message_iter_get_element_count }, 369 | { "get_element_type", ldbus_message_iter_get_element_type }, 370 | { "recurse", ldbus_message_iter_recurse }, 371 | { "get_signature", ldbus_message_iter_get_signature }, 372 | { "get_basic", ldbus_message_iter_get_basic }, 373 | { "append_basic", ldbus_message_iter_append_basic }, 374 | { "open_container", ldbus_message_iter_open_container }, 375 | { "close_container", ldbus_message_iter_close_container }, 376 | { NULL, NULL } 377 | }; 378 | 379 | lDBusMessageIter *iter = lua_newuserdata(L, sizeof(lDBusMessageIter)); 380 | iter->message = NULL; 381 | 382 | if (luaL_newmetatable(L, DBUS_MESSAGE_ITER_METATABLE)) { 383 | luaL_newlib(L, methods); 384 | lua_setfield(L, -2, "__index"); 385 | 386 | lua_pushcfunction(L, tostring); 387 | lua_setfield(L, -2, "__tostring"); 388 | 389 | lua_pushcfunction(L, ldbus_message_iter_gc); 390 | lua_setfield(L, -2, "__gc"); 391 | 392 | lua_pushstring(L, "DBusMessageIter"); 393 | lua_setfield(L, -2, "__udtype"); 394 | } 395 | lua_setmetatable(L, -2); 396 | 397 | return 1; 398 | } 399 | 400 | LDBUS_INTERNAL void load_dbus_message_iter(lua_State *L) { 401 | static const struct luaL_Reg ldbus_message_iter [] = { 402 | { "new", push_DBusMessageIter }, 403 | { NULL, NULL } 404 | }; 405 | luaL_newlib(L, ldbus_message_iter); 406 | } 407 | -------------------------------------------------------------------------------- /src/message.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include "compat-5.3.h" 6 | 7 | #include 8 | 9 | #include "ldbus.h" 10 | 11 | #include "message_iter.h" 12 | 13 | #include "message.h" 14 | 15 | 16 | static int ldbus_message_get_serial(lua_State *L) { 17 | DBusMessage *message = check_DBusMessage(L, 1); 18 | 19 | lua_pushinteger(L, dbus_message_get_serial(message)); 20 | 21 | return 1; 22 | } 23 | 24 | static int ldbus_message_set_reply_serial(lua_State *L) { 25 | DBusMessage *message = check_DBusMessage(L, 1); 26 | dbus_uint32_t reply_serial = luaL_checkinteger(L, 2); 27 | 28 | lua_pushboolean(L, dbus_message_set_reply_serial(message, reply_serial)); 29 | 30 | return 1; 31 | } 32 | 33 | static int ldbus_message_get_reply_serial(lua_State *L) { 34 | DBusMessage *message = check_DBusMessage(L, 1); 35 | 36 | lua_pushinteger(L, dbus_message_get_reply_serial(message)); 37 | 38 | return 1; 39 | } 40 | 41 | static int ldbus_message_new(lua_State *L) { 42 | int message_type = dbus_message_type_from_string(luaL_checkstring(L, 1)); 43 | 44 | DBusMessage * message = dbus_message_new(message_type); 45 | if (message == NULL) { 46 | return luaL_error(L, LDBUS_NO_MEMORY); 47 | } 48 | 49 | push_DBusMessage(L, message); 50 | return 1; 51 | } 52 | 53 | static int ldbus_message_new_method_call(lua_State *L) { 54 | const char * destination = luaL_optstring(L, 1, NULL); 55 | const char * path = luaL_checkstring(L, 2); 56 | const char * interface = luaL_optstring(L, 3, NULL); 57 | const char * method = luaL_checkstring(L, 4); 58 | 59 | DBusMessage * message = dbus_message_new_method_call(destination, path, interface, method); 60 | if (message == NULL) { 61 | return luaL_error(L, LDBUS_NO_MEMORY); 62 | } 63 | 64 | push_DBusMessage(L, message); 65 | return 1; 66 | } 67 | 68 | static int ldbus_message_new_method_return(lua_State *L) { 69 | DBusMessage *methodcall = check_DBusMessage(L, 1); 70 | 71 | DBusMessage *message = dbus_message_new_method_return(methodcall); 72 | if (message == NULL) { 73 | return luaL_error(L, LDBUS_NO_MEMORY); 74 | } 75 | 76 | push_DBusMessage(L, message); 77 | return 1; 78 | } 79 | 80 | static int ldbus_message_new_signal(lua_State *L) { 81 | const char * path = luaL_checkstring(L, 1); 82 | const char * interface = luaL_checkstring(L, 2); 83 | const char * name = luaL_checkstring(L, 3); 84 | 85 | DBusMessage * message = dbus_message_new_signal(path, interface, name); 86 | if (message == NULL) { 87 | return luaL_error(L, LDBUS_NO_MEMORY); 88 | } 89 | 90 | push_DBusMessage(L, message); 91 | return 1; 92 | } 93 | 94 | static int ldbus_message_new_error(lua_State *L) { 95 | DBusMessage *reply_to = check_DBusMessage(L, 1); 96 | const char *name = luaL_checkstring(L, 2); 97 | const char *error_message = luaL_optstring(L, 3, NULL); 98 | 99 | DBusMessage * message = dbus_message_new_error(reply_to, name, error_message); 100 | if (message == NULL) { 101 | return luaL_error(L, LDBUS_NO_MEMORY); 102 | } 103 | 104 | push_DBusMessage(L, message); 105 | return 1; 106 | } 107 | 108 | static int ldbus_message_copy(lua_State *L) { 109 | DBusMessage *orig = check_DBusMessage(L, 1); 110 | 111 | DBusMessage *message = dbus_message_copy(orig); 112 | if (message == NULL) { 113 | return luaL_error(L, LDBUS_NO_MEMORY); 114 | } 115 | 116 | push_DBusMessage(L, message); 117 | return 1; 118 | } 119 | 120 | static int ldbus_message_unref(lua_State *L) { 121 | DBusMessage *message = check_DBusMessage(L, 1); 122 | 123 | dbus_message_unref(message); 124 | 125 | return 0; 126 | } 127 | 128 | static int ldbus_message_get_type(lua_State *L) { 129 | DBusMessage *message = check_DBusMessage(L, 1); 130 | 131 | lua_pushstring(L, dbus_message_type_to_string(dbus_message_get_type(message))); 132 | return 1; 133 | } 134 | 135 | static int ldbus_message_iter_init(lua_State *L) { 136 | DBusMessage *message = check_DBusMessage(L, 1); 137 | lDBusMessageIter *iter; 138 | if (lua_gettop(L) == 1) { 139 | push_DBusMessageIter(L); 140 | iter = lua_touserdata(L, 2); 141 | } else { 142 | lua_settop(L, 2); 143 | iter = luaL_checkudata(L, 2, DBUS_MESSAGE_ITER_METATABLE); 144 | unref_ldbus_message_iter(iter); 145 | } 146 | 147 | if (!dbus_message_iter_init(message, &iter->iter)) { 148 | lua_pushnil(L); 149 | iter->message = NULL; 150 | } else { 151 | iter->message = message; 152 | dbus_message_ref(message); 153 | } 154 | 155 | return 1; 156 | } 157 | 158 | static int ldbus_message_iter_init_append(lua_State *L) { 159 | DBusMessage *message = check_DBusMessage(L, 1); 160 | lDBusMessageIter *iter; 161 | if (lua_gettop(L) == 1) { 162 | push_DBusMessageIter(L); 163 | iter = lua_touserdata(L, 2); 164 | } else { 165 | lua_settop(L, 2); 166 | iter = luaL_checkudata(L, 2, DBUS_MESSAGE_ITER_METATABLE); 167 | unref_ldbus_message_iter(iter); 168 | } 169 | 170 | dbus_message_iter_init_append(message, &iter->iter); 171 | 172 | iter->message = message; 173 | dbus_message_ref(iter->message); 174 | 175 | return 1; 176 | } 177 | 178 | static int ldbus_message_set_no_reply(lua_State *L) { 179 | DBusMessage *message = check_DBusMessage(L, 1); 180 | int no_reply = (luaL_checktype(L, 2, LUA_TBOOLEAN), lua_toboolean(L, 2)); 181 | 182 | dbus_message_set_no_reply(message, no_reply); 183 | 184 | return 0; 185 | } 186 | 187 | static int ldbus_message_get_no_reply(lua_State *L) { 188 | DBusMessage *message = check_DBusMessage(L, 1); 189 | 190 | lua_pushboolean(L, dbus_message_get_no_reply(message)); 191 | 192 | return 1; 193 | } 194 | 195 | static int ldbus_message_set_auto_start(lua_State *L) { 196 | DBusMessage *message = check_DBusMessage(L, 1); 197 | int auto_start = (luaL_checktype(L, 2, LUA_TBOOLEAN), lua_toboolean(L, 2)); 198 | 199 | dbus_message_set_auto_start(message, auto_start); 200 | 201 | return 0; 202 | } 203 | 204 | static int ldbus_message_get_auto_start(lua_State *L) { 205 | DBusMessage *message = check_DBusMessage(L, 1); 206 | 207 | lua_pushboolean(L, dbus_message_get_auto_start(message)); 208 | 209 | return 1; 210 | } 211 | 212 | static int ldbus_message_set_path(lua_State *L) { 213 | DBusMessage *message = check_DBusMessage(L, 1); 214 | const char * object_path = luaL_optstring(L, 2, NULL); 215 | 216 | lua_pushboolean(L, dbus_message_set_path(message, object_path)); 217 | 218 | return 1; 219 | } 220 | 221 | static int ldbus_message_get_path(lua_State *L) { 222 | DBusMessage *message = check_DBusMessage(L, 1); 223 | 224 | const char * object_path = dbus_message_get_path(message); 225 | if (object_path == NULL) { 226 | lua_pushnil(L); 227 | } else { 228 | lua_pushstring(L, object_path); 229 | } 230 | 231 | return 1; 232 | } 233 | 234 | static int ldbus_message_get_path_decomposed(lua_State *L) { 235 | int i; 236 | DBusMessage *message = check_DBusMessage(L, 1); 237 | 238 | char ** path; 239 | if (dbus_message_get_path_decomposed(message, &path) == FALSE) { 240 | lua_pushboolean(L, FALSE); 241 | } else if (path == NULL) { 242 | lua_pushnil(L); 243 | } else { 244 | lua_newtable(L); 245 | for (i=0 ; path [ i ] != NULL ;) { 246 | lua_pushstring(L, path [ i ]); 247 | lua_rawseti(L, -2, ++i); 248 | } 249 | dbus_free_string_array(path); 250 | } 251 | 252 | return 1; 253 | } 254 | 255 | static int ldbus_message_set_interface(lua_State *L) { 256 | DBusMessage *message = check_DBusMessage(L, 1); 257 | const char * interface = luaL_optstring(L, 2, NULL); 258 | 259 | lua_pushboolean(L, dbus_message_set_interface(message, interface)); 260 | 261 | return 1; 262 | } 263 | 264 | static int ldbus_message_get_interface(lua_State *L) { 265 | DBusMessage *message = check_DBusMessage(L, 1); 266 | 267 | const char * interface = dbus_message_get_interface(message); 268 | if (interface == NULL) { 269 | lua_pushnil(L); 270 | } else { 271 | lua_pushstring(L, interface); 272 | } 273 | 274 | return 1; 275 | } 276 | 277 | static int ldbus_message_set_member(lua_State *L) { 278 | DBusMessage *message = check_DBusMessage(L, 1); 279 | const char * member = luaL_optstring(L, 2, NULL); 280 | 281 | lua_pushboolean(L, dbus_message_set_member(message, member)); 282 | 283 | return 1; 284 | } 285 | 286 | static int ldbus_message_get_member(lua_State *L) { 287 | DBusMessage *message = check_DBusMessage(L, 1); 288 | 289 | const char * member = dbus_message_get_member(message); 290 | if (member == NULL) { 291 | lua_pushnil(L); 292 | } else { 293 | lua_pushstring(L, member); 294 | } 295 | 296 | return 1; 297 | } 298 | 299 | static int ldbus_message_set_error_name(lua_State *L) { 300 | DBusMessage *message = check_DBusMessage(L, 1); 301 | const char * error_name = luaL_optstring(L, 2, NULL); 302 | 303 | lua_pushboolean(L, dbus_message_set_error_name(message, error_name)); 304 | 305 | return 1; 306 | } 307 | 308 | static int ldbus_message_get_error_name(lua_State *L) { 309 | DBusMessage *message = check_DBusMessage(L, 1); 310 | 311 | const char * error_name = dbus_message_get_error_name(message); 312 | if (error_name == NULL) { 313 | lua_pushnil(L); 314 | } else { 315 | lua_pushstring(L, error_name); 316 | } 317 | 318 | return 1; 319 | } 320 | 321 | static int ldbus_message_set_destination(lua_State *L) { 322 | DBusMessage *message = check_DBusMessage(L, 1); 323 | const char * destination = luaL_optstring(L, 2, NULL); 324 | 325 | lua_pushboolean(L, dbus_message_set_destination(message, destination)); 326 | 327 | return 1; 328 | } 329 | 330 | static int ldbus_message_get_destination(lua_State *L) { 331 | DBusMessage *message = check_DBusMessage(L, 1); 332 | 333 | const char * destination = dbus_message_get_destination(message); 334 | if (destination == NULL) { 335 | lua_pushnil(L); 336 | } else { 337 | lua_pushstring(L, destination); 338 | } 339 | 340 | return 1; 341 | } 342 | 343 | static int ldbus_message_set_sender(lua_State *L) { 344 | DBusMessage *message = check_DBusMessage(L, 1); 345 | const char * sender = luaL_optstring(L, 2, NULL); 346 | 347 | lua_pushboolean(L, dbus_message_set_sender(message, sender)); 348 | 349 | return 1; 350 | } 351 | 352 | static int ldbus_message_get_sender(lua_State *L) { 353 | DBusMessage *message = check_DBusMessage(L, 1); 354 | 355 | const char * sender = dbus_message_get_sender(message); 356 | if (sender == NULL) { 357 | lua_pushnil(L); 358 | } else { 359 | lua_pushstring(L, sender); 360 | } 361 | 362 | return 1; 363 | } 364 | 365 | static int ldbus_message_get_signature(lua_State *L) { 366 | DBusMessage *message = check_DBusMessage(L, 1); 367 | 368 | const char * signature = dbus_message_get_signature(message); 369 | if (signature == NULL) { 370 | lua_pushnil(L); 371 | } else { 372 | lua_pushstring(L, signature); 373 | } 374 | 375 | return 1; 376 | } 377 | 378 | LDBUS_INTERNAL void push_DBusMessage(lua_State *L, DBusMessage * message) { 379 | static luaL_Reg const methods [] = { 380 | { "get_serial", ldbus_message_get_serial }, 381 | { "set_reply_serial", ldbus_message_set_reply_serial }, 382 | { "get_reply_serial", ldbus_message_get_reply_serial }, 383 | { "new_method_return", ldbus_message_new_method_return }, 384 | { "new_error", ldbus_message_new_error }, 385 | { "copy", ldbus_message_copy }, 386 | { "get_type", ldbus_message_get_type }, 387 | { "iter_init", ldbus_message_iter_init }, 388 | { "iter_init_append", ldbus_message_iter_init_append }, 389 | { "set_no_reply", ldbus_message_set_no_reply }, 390 | { "get_no_reply", ldbus_message_get_no_reply }, 391 | { "set_auto_start", ldbus_message_set_auto_start }, 392 | { "get_auto_start", ldbus_message_get_auto_start }, 393 | { "set_path", ldbus_message_set_path }, 394 | { "get_path", ldbus_message_get_path }, 395 | { "get_path_decomposed", ldbus_message_get_path_decomposed }, 396 | { "set_interface", ldbus_message_set_interface }, 397 | { "get_interface", ldbus_message_get_interface }, 398 | { "set_member", ldbus_message_set_member }, 399 | { "get_member", ldbus_message_get_member }, 400 | { "set_error_name", ldbus_message_set_error_name }, 401 | { "get_error_name", ldbus_message_get_error_name }, 402 | { "set_destination", ldbus_message_set_destination }, 403 | { "get_destination", ldbus_message_get_destination }, 404 | { "set_sender", ldbus_message_set_sender }, 405 | { "get_sender", ldbus_message_get_sender }, 406 | { "get_signature", ldbus_message_get_signature }, 407 | { NULL, NULL } 408 | }; 409 | 410 | DBusMessage ** udata = lua_newuserdata(L, sizeof(DBusMessage *)); 411 | *udata = message; 412 | 413 | if (luaL_newmetatable(L, DBUS_MESSAGE_METATABLE)) { 414 | luaL_newlib(L, methods); 415 | lua_setfield(L, -2, "__index"); 416 | 417 | lua_pushcfunction(L, ldbus_message_unref) ; 418 | lua_setfield(L, -2, "__gc"); 419 | 420 | lua_pushcfunction(L, tostring); 421 | lua_setfield(L, -2, "__tostring"); 422 | 423 | lua_pushstring(L, "DBusMessage"); 424 | lua_setfield(L, -2, "__udtype"); 425 | } 426 | lua_setmetatable(L, -2); 427 | } 428 | 429 | int luaopen_ldbus_message(lua_State *L) { 430 | static const struct luaL_Reg ldbus_message [] = { 431 | { "new", ldbus_message_new }, 432 | { "new_method_call", ldbus_message_new_method_call }, 433 | { "new_signal", ldbus_message_new_signal }, 434 | { NULL, NULL } 435 | }; 436 | luaL_newlib(L, ldbus_message); 437 | 438 | load_dbus_message_iter(L); 439 | lua_setfield(L, -2, "iter"); 440 | return 1; 441 | } 442 | -------------------------------------------------------------------------------- /vendor/compat-5.3/c-api/compat-5.3.h: -------------------------------------------------------------------------------- 1 | #ifndef COMPAT53_H_ 2 | #define COMPAT53_H_ 3 | 4 | #include 5 | #include 6 | #include 7 | #if defined(__cplusplus) && !defined(COMPAT53_LUA_CPP) 8 | extern "C" { 9 | #endif 10 | #include 11 | #include 12 | #include 13 | #if defined(__cplusplus) && !defined(COMPAT53_LUA_CPP) 14 | } 15 | #endif 16 | 17 | 18 | #undef COMPAT53_INCLUDE_SOURCE 19 | #if defined(COMPAT53_PREFIX) 20 | /* - change the symbol names of functions to avoid linker conflicts 21 | * - compat-5.3.c needs to be compiled (and linked) separately 22 | */ 23 | # if !defined(COMPAT53_API) 24 | # define COMPAT53_API extern 25 | # endif 26 | #else /* COMPAT53_PREFIX */ 27 | /* - make all functions static and include the source. 28 | * - compat-5.3.c doesn't need to be compiled (and linked) separately 29 | */ 30 | # define COMPAT53_PREFIX compat53 31 | # undef COMPAT53_API 32 | # if defined(__GNUC__) || defined(__clang__) 33 | # define COMPAT53_API __attribute__((__unused__)) static 34 | # else 35 | # define COMPAT53_API static 36 | # endif 37 | # define COMPAT53_INCLUDE_SOURCE 38 | #endif /* COMPAT53_PREFIX */ 39 | 40 | #define COMPAT53_CONCAT_HELPER(a, b) a##b 41 | #define COMPAT53_CONCAT(a, b) COMPAT53_CONCAT_HELPER(a, b) 42 | 43 | 44 | 45 | /* declarations for Lua 5.1 */ 46 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501 47 | 48 | /* XXX not implemented: 49 | * lua_arith (new operators) 50 | * lua_upvalueid 51 | * lua_upvaluejoin 52 | * lua_version 53 | * lua_yieldk 54 | */ 55 | 56 | #ifndef LUA_OK 57 | # define LUA_OK 0 58 | #endif 59 | #ifndef LUA_OPADD 60 | # define LUA_OPADD 0 61 | #endif 62 | #ifndef LUA_OPSUB 63 | # define LUA_OPSUB 1 64 | #endif 65 | #ifndef LUA_OPMUL 66 | # define LUA_OPMUL 2 67 | #endif 68 | #ifndef LUA_OPDIV 69 | # define LUA_OPDIV 3 70 | #endif 71 | #ifndef LUA_OPMOD 72 | # define LUA_OPMOD 4 73 | #endif 74 | #ifndef LUA_OPPOW 75 | # define LUA_OPPOW 5 76 | #endif 77 | #ifndef LUA_OPUNM 78 | # define LUA_OPUNM 6 79 | #endif 80 | #ifndef LUA_OPEQ 81 | # define LUA_OPEQ 0 82 | #endif 83 | #ifndef LUA_OPLT 84 | # define LUA_OPLT 1 85 | #endif 86 | #ifndef LUA_OPLE 87 | # define LUA_OPLE 2 88 | #endif 89 | 90 | /* LuaJIT/Lua 5.1 does not have the updated 91 | * error codes for thread status/function returns (but some patched versions do) 92 | * define it only if it's not found 93 | */ 94 | #if !defined(LUA_ERRGCMM) 95 | /* Use + 2 because in some versions of Lua (Lua 5.1) 96 | * LUA_ERRFILE is defined as (LUA_ERRERR+1) 97 | * so we need to avoid it (LuaJIT might have something at this 98 | * integer value too) 99 | */ 100 | # define LUA_ERRGCMM (LUA_ERRERR + 2) 101 | #endif /* LUA_ERRGCMM define */ 102 | 103 | typedef size_t lua_Unsigned; 104 | 105 | typedef struct luaL_Buffer_53 { 106 | luaL_Buffer b; /* make incorrect code crash! */ 107 | char *ptr; 108 | size_t nelems; 109 | size_t capacity; 110 | lua_State *L2; 111 | } luaL_Buffer_53; 112 | #define luaL_Buffer luaL_Buffer_53 113 | 114 | /* In PUC-Rio 5.1, userdata is a simple FILE* 115 | * In LuaJIT, it's a struct where the first member is a FILE* 116 | * We can't support the `closef` member 117 | */ 118 | typedef struct luaL_Stream { 119 | FILE *f; 120 | } luaL_Stream; 121 | 122 | #define lua_absindex COMPAT53_CONCAT(COMPAT53_PREFIX, _absindex) 123 | COMPAT53_API int lua_absindex (lua_State *L, int i); 124 | 125 | #define lua_arith COMPAT53_CONCAT(COMPAT53_PREFIX, _arith) 126 | COMPAT53_API void lua_arith (lua_State *L, int op); 127 | 128 | #define lua_compare COMPAT53_CONCAT(COMPAT53_PREFIX, _compare) 129 | COMPAT53_API int lua_compare (lua_State *L, int idx1, int idx2, int op); 130 | 131 | #define lua_copy COMPAT53_CONCAT(COMPAT53_PREFIX, _copy) 132 | COMPAT53_API void lua_copy (lua_State *L, int from, int to); 133 | 134 | #define lua_getuservalue(L, i) \ 135 | (lua_getfenv((L), (i)), lua_type((L), -1)) 136 | #define lua_setuservalue(L, i) \ 137 | (luaL_checktype((L), -1, LUA_TTABLE), lua_setfenv((L), (i))) 138 | 139 | #define lua_len COMPAT53_CONCAT(COMPAT53_PREFIX, _len) 140 | COMPAT53_API void lua_len (lua_State *L, int i); 141 | 142 | #define lua_pushstring(L, s) \ 143 | (lua_pushstring((L), (s)), lua_tostring((L), -1)) 144 | 145 | #define lua_pushlstring(L, s, len) \ 146 | ((((len) == 0) ? lua_pushlstring((L), "", 0) : lua_pushlstring((L), (s), (len))), lua_tostring((L), -1)) 147 | 148 | #ifndef luaL_newlibtable 149 | # define luaL_newlibtable(L, l) \ 150 | (lua_createtable((L), 0, sizeof((l))/sizeof(*(l))-1)) 151 | #endif 152 | #ifndef luaL_newlib 153 | # define luaL_newlib(L, l) \ 154 | (luaL_newlibtable((L), (l)), luaL_register((L), NULL, (l))) 155 | #endif 156 | 157 | #define lua_pushglobaltable(L) \ 158 | lua_pushvalue((L), LUA_GLOBALSINDEX) 159 | 160 | #define lua_rawgetp COMPAT53_CONCAT(COMPAT53_PREFIX, _rawgetp) 161 | COMPAT53_API int lua_rawgetp (lua_State *L, int i, const void *p); 162 | 163 | #define lua_rawsetp COMPAT53_CONCAT(COMPAT53_PREFIX, _rawsetp) 164 | COMPAT53_API void lua_rawsetp(lua_State *L, int i, const void *p); 165 | 166 | #define lua_rawlen(L, i) lua_objlen((L), (i)) 167 | 168 | #define lua_tointeger(L, i) lua_tointegerx((L), (i), NULL) 169 | 170 | #define lua_tonumberx COMPAT53_CONCAT(COMPAT53_PREFIX, _tonumberx) 171 | COMPAT53_API lua_Number lua_tonumberx (lua_State *L, int i, int *isnum); 172 | 173 | #define luaL_checkversion COMPAT53_CONCAT(COMPAT53_PREFIX, L_checkversion) 174 | COMPAT53_API void luaL_checkversion (lua_State *L); 175 | 176 | #define lua_load COMPAT53_CONCAT(COMPAT53_PREFIX, _load_53) 177 | COMPAT53_API int lua_load (lua_State *L, lua_Reader reader, void *data, const char* source, const char* mode); 178 | 179 | #define luaL_loadfilex COMPAT53_CONCAT(COMPAT53_PREFIX, L_loadfilex) 180 | COMPAT53_API int luaL_loadfilex (lua_State *L, const char *filename, const char *mode); 181 | 182 | #define luaL_loadbufferx COMPAT53_CONCAT(COMPAT53_PREFIX, L_loadbufferx) 183 | COMPAT53_API int luaL_loadbufferx (lua_State *L, const char *buff, size_t sz, const char *name, const char *mode); 184 | 185 | #define luaL_checkstack COMPAT53_CONCAT(COMPAT53_PREFIX, L_checkstack_53) 186 | COMPAT53_API void luaL_checkstack (lua_State *L, int sp, const char *msg); 187 | 188 | #define luaL_getsubtable COMPAT53_CONCAT(COMPAT53_PREFIX, L_getsubtable) 189 | COMPAT53_API int luaL_getsubtable (lua_State* L, int i, const char *name); 190 | 191 | #define luaL_len COMPAT53_CONCAT(COMPAT53_PREFIX, L_len) 192 | COMPAT53_API lua_Integer luaL_len (lua_State *L, int i); 193 | 194 | #define luaL_setfuncs COMPAT53_CONCAT(COMPAT53_PREFIX, L_setfuncs) 195 | COMPAT53_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup); 196 | 197 | #define luaL_setmetatable COMPAT53_CONCAT(COMPAT53_PREFIX, L_setmetatable) 198 | COMPAT53_API void luaL_setmetatable (lua_State *L, const char *tname); 199 | 200 | #define luaL_testudata COMPAT53_CONCAT(COMPAT53_PREFIX, L_testudata) 201 | COMPAT53_API void *luaL_testudata (lua_State *L, int i, const char *tname); 202 | 203 | #define luaL_traceback COMPAT53_CONCAT(COMPAT53_PREFIX, L_traceback) 204 | COMPAT53_API void luaL_traceback (lua_State *L, lua_State *L1, const char *msg, int level); 205 | 206 | #define luaL_fileresult COMPAT53_CONCAT(COMPAT53_PREFIX, L_fileresult) 207 | COMPAT53_API int luaL_fileresult (lua_State *L, int stat, const char *fname); 208 | 209 | #define luaL_execresult COMPAT53_CONCAT(COMPAT53_PREFIX, L_execresult) 210 | COMPAT53_API int luaL_execresult (lua_State *L, int stat); 211 | 212 | #define lua_callk(L, na, nr, ctx, cont) \ 213 | ((void)(ctx), (void)(cont), lua_call((L), (na), (nr))) 214 | #define lua_pcallk(L, na, nr, err, ctx, cont) \ 215 | ((void)(ctx), (void)(cont), lua_pcall((L), (na), (nr), (err))) 216 | 217 | #define lua_resume(L, from, nargs) \ 218 | ((void)(from), lua_resume((L), (nargs))) 219 | 220 | #define luaL_buffinit COMPAT53_CONCAT(COMPAT53_PREFIX, _buffinit_53) 221 | COMPAT53_API void luaL_buffinit (lua_State *L, luaL_Buffer_53 *B); 222 | 223 | #define luaL_prepbuffsize COMPAT53_CONCAT(COMPAT53_PREFIX, _prepbufsize_53) 224 | COMPAT53_API char *luaL_prepbuffsize (luaL_Buffer_53 *B, size_t s); 225 | 226 | #define luaL_addlstring COMPAT53_CONCAT(COMPAT53_PREFIX, _addlstring_53) 227 | COMPAT53_API void luaL_addlstring (luaL_Buffer_53 *B, const char *s, size_t l); 228 | 229 | #define luaL_addvalue COMPAT53_CONCAT(COMPAT53_PREFIX, _addvalue_53) 230 | COMPAT53_API void luaL_addvalue (luaL_Buffer_53 *B); 231 | 232 | #define luaL_pushresult COMPAT53_CONCAT(COMPAT53_PREFIX, _pushresult_53) 233 | COMPAT53_API void luaL_pushresult (luaL_Buffer_53 *B); 234 | 235 | #undef luaL_buffinitsize 236 | #define luaL_buffinitsize(L, B, s) \ 237 | (luaL_buffinit((L), (B)), luaL_prepbuffsize((B), (s))) 238 | 239 | #undef luaL_prepbuffer 240 | #define luaL_prepbuffer(B) \ 241 | luaL_prepbuffsize((B), LUAL_BUFFERSIZE) 242 | 243 | #undef luaL_addchar 244 | #define luaL_addchar(B, c) \ 245 | ((void)((B)->nelems < (B)->capacity || luaL_prepbuffsize((B), 1)), \ 246 | ((B)->ptr[(B)->nelems++] = (c))) 247 | 248 | #undef luaL_addsize 249 | #define luaL_addsize(B, s) \ 250 | ((B)->nelems += (s)) 251 | 252 | #undef luaL_addstring 253 | #define luaL_addstring(B, s) \ 254 | luaL_addlstring((B), (s), strlen((s))) 255 | 256 | #undef luaL_pushresultsize 257 | #define luaL_pushresultsize(B, s) \ 258 | (luaL_addsize((B), (s)), luaL_pushresult((B))) 259 | 260 | #if defined(LUA_COMPAT_APIINTCASTS) 261 | #define lua_pushunsigned(L, n) \ 262 | lua_pushinteger((L), (lua_Integer)(n)) 263 | #define lua_tounsignedx(L, i, is) \ 264 | ((lua_Unsigned)lua_tointegerx((L), (i), (is))) 265 | #define lua_tounsigned(L, i) \ 266 | lua_tounsignedx((L), (i), NULL) 267 | #define luaL_checkunsigned(L, a) \ 268 | ((lua_Unsigned)luaL_checkinteger((L), (a))) 269 | #define luaL_optunsigned(L, a, d) \ 270 | ((lua_Unsigned)luaL_optinteger((L), (a), (lua_Integer)(d))) 271 | #endif 272 | 273 | #endif /* Lua 5.1 only */ 274 | 275 | 276 | 277 | /* declarations for Lua 5.1 and 5.2 */ 278 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 502 279 | 280 | typedef int lua_KContext; 281 | 282 | typedef int (*lua_KFunction)(lua_State *L, int status, lua_KContext ctx); 283 | 284 | #define lua_dump(L, w, d, s) \ 285 | ((void)(s), lua_dump((L), (w), (d))) 286 | 287 | #define lua_getfield(L, i, k) \ 288 | (lua_getfield((L), (i), (k)), lua_type((L), -1)) 289 | 290 | #define lua_gettable(L, i) \ 291 | (lua_gettable((L), (i)), lua_type((L), -1)) 292 | 293 | #define lua_geti COMPAT53_CONCAT(COMPAT53_PREFIX, _geti) 294 | COMPAT53_API int lua_geti (lua_State *L, int index, lua_Integer i); 295 | 296 | #define lua_getextraspace COMPAT53_CONCAT(COMPAT53_PREFIX, _getextraspace) 297 | COMPAT53_API void *lua_getextraspace (lua_State *L); 298 | 299 | #define lua_isinteger COMPAT53_CONCAT(COMPAT53_PREFIX, _isinteger) 300 | COMPAT53_API int lua_isinteger (lua_State *L, int index); 301 | 302 | #define lua_tointegerx COMPAT53_CONCAT(COMPAT53_PREFIX, _tointegerx_53) 303 | COMPAT53_API lua_Integer lua_tointegerx (lua_State *L, int i, int *isnum); 304 | 305 | #define lua_numbertointeger(n, p) \ 306 | ((*(p) = (lua_Integer)(n)), 1) 307 | 308 | #define lua_rawget(L, i) \ 309 | (lua_rawget((L), (i)), lua_type((L), -1)) 310 | 311 | #define lua_rawgeti(L, i, n) \ 312 | (lua_rawgeti((L), (i), (n)), lua_type((L), -1)) 313 | 314 | #define lua_rotate COMPAT53_CONCAT(COMPAT53_PREFIX, _rotate) 315 | COMPAT53_API void lua_rotate (lua_State *L, int idx, int n); 316 | 317 | #define lua_seti COMPAT53_CONCAT(COMPAT53_PREFIX, _seti) 318 | COMPAT53_API void lua_seti (lua_State *L, int index, lua_Integer i); 319 | 320 | #define lua_stringtonumber COMPAT53_CONCAT(COMPAT53_PREFIX, _stringtonumber) 321 | COMPAT53_API size_t lua_stringtonumber (lua_State *L, const char *s); 322 | 323 | #define luaL_tolstring COMPAT53_CONCAT(COMPAT53_PREFIX, L_tolstring) 324 | COMPAT53_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len); 325 | 326 | #define luaL_getmetafield(L, o, e) \ 327 | (luaL_getmetafield((L), (o), (e)) ? lua_type((L), -1) : LUA_TNIL) 328 | 329 | #define luaL_newmetatable(L, tn) \ 330 | (luaL_newmetatable((L), (tn)) ? (lua_pushstring((L), (tn)), lua_setfield((L), -2, "__name"), 1) : 0) 331 | 332 | #define luaL_requiref COMPAT53_CONCAT(COMPAT53_PREFIX, L_requiref_53) 333 | COMPAT53_API void luaL_requiref (lua_State *L, const char *modname, 334 | lua_CFunction openf, int glb ); 335 | 336 | #endif /* Lua 5.1 and Lua 5.2 */ 337 | 338 | 339 | 340 | /* declarations for Lua 5.2 */ 341 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 502 342 | 343 | /* XXX not implemented: 344 | * lua_isyieldable 345 | * lua_arith (new operators) 346 | * lua_pushfstring (new formats) 347 | */ 348 | 349 | #define lua_getglobal(L, n) \ 350 | (lua_getglobal((L), (n)), lua_type((L), -1)) 351 | 352 | #define lua_getuservalue(L, i) \ 353 | (lua_getuservalue((L), (i)), lua_type((L), -1)) 354 | 355 | #define lua_pushlstring(L, s, len) \ 356 | (((len) == 0) ? lua_pushlstring((L), "", 0) : lua_pushlstring((L), (s), (len))) 357 | 358 | #define lua_rawgetp(L, i, p) \ 359 | (lua_rawgetp((L), (i), (p)), lua_type((L), -1)) 360 | 361 | #define LUA_KFUNCTION(_name) \ 362 | static int (_name)(lua_State *L, int status, lua_KContext ctx); \ 363 | static int (_name ## _52)(lua_State *L) { \ 364 | lua_KContext ctx; \ 365 | int status = lua_getctx(L, &ctx); \ 366 | return (_name)(L, status, ctx); \ 367 | } \ 368 | static int (_name)(lua_State *L, int status, lua_KContext ctx) 369 | 370 | #define lua_pcallk(L, na, nr, err, ctx, cont) \ 371 | lua_pcallk((L), (na), (nr), (err), (ctx), cont ## _52) 372 | 373 | #define lua_callk(L, na, nr, ctx, cont) \ 374 | lua_callk((L), (na), (nr), (ctx), cont ## _52) 375 | 376 | #define lua_yieldk(L, nr, ctx, cont) \ 377 | lua_yieldk((L), (nr), (ctx), cont ## _52) 378 | 379 | #ifdef lua_call 380 | # undef lua_call 381 | # define lua_call(L, na, nr) \ 382 | (lua_callk)((L), (na), (nr), 0, NULL) 383 | #endif 384 | 385 | #ifdef lua_pcall 386 | # undef lua_pcall 387 | # define lua_pcall(L, na, nr, err) \ 388 | (lua_pcallk)((L), (na), (nr), (err), 0, NULL) 389 | #endif 390 | 391 | #ifdef lua_yield 392 | # undef lua_yield 393 | # define lua_yield(L, nr) \ 394 | (lua_yieldk)((L), (nr), 0, NULL) 395 | #endif 396 | 397 | #endif /* Lua 5.2 only */ 398 | 399 | 400 | 401 | /* other Lua versions */ 402 | #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 501 || LUA_VERSION_NUM > 504 403 | 404 | # error "unsupported Lua version (i.e. not Lua 5.1, 5.2, 5.3, or 5.4)" 405 | 406 | #endif /* other Lua versions except 5.1, 5.2, 5.3, and 5.4 */ 407 | 408 | 409 | 410 | /* helper macro for defining continuation functions (for every version 411 | * *except* Lua 5.2) */ 412 | #ifndef LUA_KFUNCTION 413 | #define LUA_KFUNCTION(_name) \ 414 | static int (_name)(lua_State *L, int status, lua_KContext ctx) 415 | #endif 416 | 417 | 418 | #if defined(COMPAT53_INCLUDE_SOURCE) 419 | # include "compat-5.3.c" 420 | #endif 421 | 422 | 423 | #endif /* COMPAT53_H_ */ 424 | 425 | -------------------------------------------------------------------------------- /vendor/compat-5.3/compat53/init.lua: -------------------------------------------------------------------------------- 1 | local lua_version = _VERSION:sub(-3) 2 | 3 | 4 | if lua_version < "5.3" then 5 | 6 | local _G, pairs, require, select, type = 7 | _G, pairs, require, select, type 8 | local debug, io = debug, io 9 | local unpack = lua_version == "5.1" and unpack or table.unpack 10 | 11 | local M = require("compat53.module") 12 | 13 | -- select the most powerful getmetatable function available 14 | local gmt = type(debug) == "table" and debug.getmetatable or 15 | getmetatable or function() return false end 16 | -- metatable for file objects from Lua's standard io library 17 | local file_meta = gmt(io.stdout) 18 | 19 | 20 | -- make '*' optional for file:read and file:lines 21 | if type(file_meta) == "table" and type(file_meta.__index) == "table" then 22 | 23 | local function addasterisk(fmt) 24 | if type(fmt) == "string" and fmt:sub(1, 1) ~= "*" then 25 | return "*"..fmt 26 | else 27 | return fmt 28 | end 29 | end 30 | 31 | local file_lines = file_meta.__index.lines 32 | file_meta.__index.lines = function(self, ...) 33 | local n = select('#', ...) 34 | for i = 1, n do 35 | local a = select(i, ...) 36 | local b = addasterisk(a) 37 | -- as an optimization we only allocate a table for the 38 | -- modified format arguments when we have a '*' somewhere 39 | if a ~= b then 40 | local args = { ... } 41 | args[i] = b 42 | for j = i+1, n do 43 | args[j] = addasterisk(args[j]) 44 | end 45 | return file_lines(self, unpack(args, 1, n)) 46 | end 47 | end 48 | return file_lines(self, ...) 49 | end 50 | 51 | local file_read = file_meta.__index.read 52 | file_meta.__index.read = function(self, ...) 53 | local n = select('#', ...) 54 | for i = 1, n do 55 | local a = select(i, ...) 56 | local b = addasterisk(a) 57 | -- as an optimization we only allocate a table for the 58 | -- modified format arguments when we have a '*' somewhere 59 | if a ~= b then 60 | local args = { ... } 61 | args[i] = b 62 | for j = i+1, n do 63 | args[j] = addasterisk(args[j]) 64 | end 65 | return file_read(self, unpack(args, 1, n)) 66 | end 67 | end 68 | return file_read(self, ...) 69 | end 70 | 71 | end -- got a valid metatable for file objects 72 | 73 | 74 | -- changes for Lua 5.1 only 75 | if lua_version == "5.1" then 76 | 77 | -- cache globals 78 | local error, pcall, rawset, setmetatable, tostring, xpcall = 79 | error, pcall, rawset, setmetatable, tostring, xpcall 80 | local coroutine, package, string = coroutine, package, string 81 | local coroutine_resume = coroutine.resume 82 | local coroutine_running = coroutine.running 83 | local coroutine_status = coroutine.status 84 | local coroutine_yield = coroutine.yield 85 | local io_type = io.type 86 | 87 | 88 | -- detect LuaJIT (including LUAJIT_ENABLE_LUA52COMPAT compilation flag) 89 | local is_luajit = (string.dump(function() end) or ""):sub(1, 3) == "\027LJ" 90 | local is_luajit52 = is_luajit and 91 | #setmetatable({}, { __len = function() return 1 end }) == 1 92 | 93 | 94 | -- make package.searchers available as an alias for package.loaders 95 | local p_index = { searchers = package.loaders } 96 | setmetatable(package, { 97 | __index = p_index, 98 | __newindex = function(p, k, v) 99 | if k == "searchers" then 100 | rawset(p, "loaders", v) 101 | p_index.searchers = v 102 | else 103 | rawset(p, k, v) 104 | end 105 | end 106 | }) 107 | 108 | 109 | if type(file_meta) == "table" and type(file_meta.__index) == "table" then 110 | if not is_luajit then 111 | local function helper(_, var_1, ...) 112 | if var_1 == nil then 113 | if (...) ~= nil then 114 | error((...), 2) 115 | end 116 | end 117 | return var_1, ... 118 | end 119 | 120 | local function lines_iterator(st) 121 | return helper(st, st.f:read(unpack(st, 1, st.n))) 122 | end 123 | 124 | local file_write = file_meta.__index.write 125 | file_meta.__index.write = function(self, ...) 126 | local res, msg, errno = file_write(self, ...) 127 | if res then 128 | return self 129 | else 130 | return nil, msg, errno 131 | end 132 | end 133 | 134 | file_meta.__index.lines = function(self, ...) 135 | if io_type(self) == "closed file" then 136 | error("attempt to use a closed file", 2) 137 | end 138 | local st = { f=self, n=select('#', ...), ... } 139 | for i = 1, st.n do 140 | local t = type(st[i]) 141 | if t == "string" then 142 | local fmt = st[i]:match("^*?([aln])") 143 | if not fmt then 144 | error("bad argument #"..(i+1).." to 'for iterator' (invalid format)", 2) 145 | end 146 | st[i] = "*"..fmt 147 | elseif t ~= "number" then 148 | error("bad argument #"..(i+1).." to 'for iterator' (invalid format)", 2) 149 | end 150 | end 151 | return lines_iterator, st 152 | end 153 | end -- not luajit 154 | end -- file_meta valid 155 | 156 | 157 | -- the (x)pcall implementations start a new coroutine internally 158 | -- to allow yielding even in Lua 5.1. to allow for accurate 159 | -- stack traces we keep track of the nested coroutine activations 160 | -- in the weak tables below: 161 | local weak_meta = { __mode = "kv" } 162 | -- maps the internal pcall coroutines to the user coroutine that 163 | -- *should* be running if pcall didn't use coroutines internally 164 | local pcall_mainOf = setmetatable({}, weak_meta) 165 | -- table that maps each running coroutine started by pcall to 166 | -- the coroutine that resumed it (user coroutine *or* pcall 167 | -- coroutine!) 168 | local pcall_previous = setmetatable({}, weak_meta) 169 | -- reverse of `pcall_mainOf`. maps a user coroutine to the 170 | -- currently active pcall coroutine started within it 171 | local pcall_callOf = setmetatable({}, weak_meta) 172 | -- similar to `pcall_mainOf` but is used only while executing 173 | -- the error handler of xpcall (thus no nesting is necessary!) 174 | local xpcall_running = setmetatable({}, weak_meta) 175 | 176 | -- handle debug functions 177 | if type(debug) == "table" then 178 | local debug_getinfo = debug.getinfo 179 | local debug_traceback = debug.traceback 180 | 181 | if not is_luajit then 182 | local function calculate_trace_level(co, level) 183 | if level ~= nil then 184 | for out = 1, 1/0 do 185 | local info = (co==nil) and debug_getinfo(out, "") or debug_getinfo(co, out, "") 186 | if info == nil then 187 | local max = out-1 188 | if level <= max then 189 | return level 190 | end 191 | return nil, level-max 192 | end 193 | end 194 | end 195 | return 1 196 | end 197 | 198 | local stack_pattern = "\nstack traceback:" 199 | local stack_replace = "" 200 | function debug.traceback(co, msg, level) 201 | local lvl 202 | local nilmsg 203 | if type(co) ~= "thread" then 204 | co, msg, level = coroutine_running(), co, msg 205 | end 206 | if msg == nil then 207 | msg = "" 208 | nilmsg = true 209 | elseif type(msg) ~= "string" then 210 | return msg 211 | end 212 | if co == nil then 213 | msg = debug_traceback(msg, level or 1) 214 | else 215 | local xpco = xpcall_running[co] 216 | if xpco ~= nil then 217 | lvl, level = calculate_trace_level(xpco, level) 218 | if lvl then 219 | msg = debug_traceback(xpco, msg, lvl) 220 | else 221 | msg = msg..stack_pattern 222 | end 223 | lvl, level = calculate_trace_level(co, level) 224 | if lvl then 225 | local trace = debug_traceback(co, "", lvl) 226 | msg = msg..trace:gsub(stack_pattern, stack_replace) 227 | end 228 | else 229 | co = pcall_callOf[co] or co 230 | lvl, level = calculate_trace_level(co, level) 231 | if lvl then 232 | msg = debug_traceback(co, msg, lvl) 233 | else 234 | msg = msg..stack_pattern 235 | end 236 | end 237 | co = pcall_previous[co] 238 | while co ~= nil do 239 | lvl, level = calculate_trace_level(co, level) 240 | if lvl then 241 | local trace = debug_traceback(co, "", lvl) 242 | msg = msg..trace:gsub(stack_pattern, stack_replace) 243 | end 244 | co = pcall_previous[co] 245 | end 246 | end 247 | if nilmsg then 248 | msg = msg:gsub("^\n", "") 249 | end 250 | msg = msg:gsub("\n\t%(tail call%): %?", "\000") 251 | msg = msg:gsub("\n\t%.%.%.\n", "\001\n") 252 | msg = msg:gsub("\n\t%.%.%.$", "\001") 253 | msg = msg:gsub("(%z+)\001(%z+)", function(some, other) 254 | return "\n\t(..."..#some+#other.."+ tail call(s)...)" 255 | end) 256 | msg = msg:gsub("\001(%z+)", function(zeros) 257 | return "\n\t(..."..#zeros.."+ tail call(s)...)" 258 | end) 259 | msg = msg:gsub("(%z+)\001", function(zeros) 260 | return "\n\t(..."..#zeros.."+ tail call(s)...)" 261 | end) 262 | msg = msg:gsub("%z+", function(zeros) 263 | return "\n\t(..."..#zeros.." tail call(s)...)" 264 | end) 265 | msg = msg:gsub("\001", function() 266 | return "\n\t..." 267 | end) 268 | return msg 269 | end 270 | end -- is not luajit 271 | end -- debug table available 272 | 273 | 274 | if not is_luajit52 then 275 | local coroutine_running52 = M.coroutine.running 276 | function M.coroutine.running() 277 | local co, ismain = coroutine_running52() 278 | if ismain then 279 | return co, true 280 | else 281 | return pcall_mainOf[co] or co, false 282 | end 283 | end 284 | end 285 | 286 | if not is_luajit then 287 | local function pcall_results(current, call, success, ...) 288 | if coroutine_status(call) == "suspended" then 289 | return pcall_results(current, call, coroutine_resume(call, coroutine_yield(...))) 290 | end 291 | if pcall_previous then 292 | pcall_previous[call] = nil 293 | local main = pcall_mainOf[call] 294 | if main == current then current = nil end 295 | pcall_callOf[main] = current 296 | end 297 | pcall_mainOf[call] = nil 298 | return success, ... 299 | end 300 | 301 | local function pcall_exec(current, call, ...) 302 | local main = pcall_mainOf[current] or current 303 | pcall_mainOf[call] = main 304 | if pcall_previous then 305 | pcall_previous[call] = current 306 | pcall_callOf[main] = call 307 | end 308 | return pcall_results(current, call, coroutine_resume(call, ...)) 309 | end 310 | 311 | local coroutine_create52 = M.coroutine.create 312 | 313 | local function pcall_coroutine(func) 314 | if type(func) ~= "function" then 315 | local callable = func 316 | func = function (...) return callable(...) end 317 | end 318 | return coroutine_create52(func) 319 | end 320 | 321 | function M.pcall(func, ...) 322 | local current = coroutine_running() 323 | if not current then return pcall(func, ...) end 324 | return pcall_exec(current, pcall_coroutine(func), ...) 325 | end 326 | 327 | local function xpcall_catch(current, call, msgh, success, ...) 328 | if not success then 329 | xpcall_running[current] = call 330 | local ok, result = pcall(msgh, ...) 331 | xpcall_running[current] = nil 332 | if not ok then 333 | return false, "error in error handling ("..tostring(result)..")" 334 | end 335 | return false, result 336 | end 337 | return true, ... 338 | end 339 | 340 | function M.xpcall(f, msgh, ...) 341 | local current = coroutine_running() 342 | if not current then 343 | local args, n = { ... }, select('#', ...) 344 | return xpcall(function() return f(unpack(args, 1, n)) end, msgh) 345 | end 346 | local call = pcall_coroutine(f) 347 | return xpcall_catch(current, call, msgh, pcall_exec(current, call, ...)) 348 | end 349 | end -- not luajit 350 | 351 | end -- lua 5.1 352 | 353 | 354 | -- handle exporting to global scope 355 | local function extend_table(from, to) 356 | if from ~= to then 357 | for k,v in pairs(from) do 358 | if type(v) == "table" and 359 | type(to[k]) == "table" and 360 | v ~= to[k] then 361 | extend_table(v, to[k]) 362 | else 363 | to[k] = v 364 | end 365 | end 366 | end 367 | end 368 | 369 | extend_table(M, _G) 370 | 371 | end -- lua < 5.3 372 | 373 | -- vi: set expandtab softtabstop=3 shiftwidth=3 : 374 | -------------------------------------------------------------------------------- /vendor/compat-5.3/ltablib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltablib.c,v 1.93.1.1 2017/04/19 17:20:42 roberto Exp $ 3 | ** Library for Table Manipulation 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define ltablib_c 8 | #define LUA_LIB 9 | 10 | #include "lprefix.h" 11 | 12 | 13 | #include 14 | #include 15 | #include 16 | 17 | #include "lua.h" 18 | 19 | #include "lauxlib.h" 20 | #include "lualib.h" 21 | 22 | 23 | /* 24 | ** Operations that an object must define to mimic a table 25 | ** (some functions only need some of them) 26 | */ 27 | #define TAB_R 1 /* read */ 28 | #define TAB_W 2 /* write */ 29 | #define TAB_L 4 /* length */ 30 | #define TAB_RW (TAB_R | TAB_W) /* read/write */ 31 | 32 | 33 | #define aux_getn(L,n,w) (checktab(L, n, (w) | TAB_L), luaL_len(L, n)) 34 | 35 | 36 | static int checkfield (lua_State *L, const char *key, int n) { 37 | lua_pushstring(L, key); 38 | return (lua_rawget(L, -n) != LUA_TNIL); 39 | } 40 | 41 | 42 | /* 43 | ** Check that 'arg' either is a table or can behave like one (that is, 44 | ** has a metatable with the required metamethods) 45 | */ 46 | static void checktab (lua_State *L, int arg, int what) { 47 | if (lua_type(L, arg) != LUA_TTABLE) { /* is it not a table? */ 48 | int n = 1; /* number of elements to pop */ 49 | if (lua_getmetatable(L, arg) && /* must have metatable */ 50 | (!(what & TAB_R) || checkfield(L, "__index", ++n)) && 51 | (!(what & TAB_W) || checkfield(L, "__newindex", ++n)) && 52 | (!(what & TAB_L) || checkfield(L, "__len", ++n))) { 53 | lua_pop(L, n); /* pop metatable and tested metamethods */ 54 | } 55 | else 56 | luaL_checktype(L, arg, LUA_TTABLE); /* force an error */ 57 | } 58 | } 59 | 60 | 61 | #if defined(LUA_COMPAT_MAXN) 62 | static int maxn (lua_State *L) { 63 | lua_Number max = 0; 64 | luaL_checktype(L, 1, LUA_TTABLE); 65 | lua_pushnil(L); /* first key */ 66 | while (lua_next(L, 1)) { 67 | lua_pop(L, 1); /* remove value */ 68 | if (lua_type(L, -1) == LUA_TNUMBER) { 69 | lua_Number v = lua_tonumber(L, -1); 70 | if (v > max) max = v; 71 | } 72 | } 73 | lua_pushnumber(L, max); 74 | return 1; 75 | } 76 | #endif 77 | 78 | 79 | static int tinsert (lua_State *L) { 80 | lua_Integer e = aux_getn(L, 1, TAB_RW) + 1; /* first empty element */ 81 | lua_Integer pos; /* where to insert new element */ 82 | switch (lua_gettop(L)) { 83 | case 2: { /* called with only 2 arguments */ 84 | pos = e; /* insert new element at the end */ 85 | break; 86 | } 87 | case 3: { 88 | lua_Integer i; 89 | pos = luaL_checkinteger(L, 2); /* 2nd argument is the position */ 90 | luaL_argcheck(L, 1 <= pos && pos <= e, 2, "position out of bounds"); 91 | for (i = e; i > pos; i--) { /* move up elements */ 92 | lua_geti(L, 1, i - 1); 93 | lua_seti(L, 1, i); /* t[i] = t[i - 1] */ 94 | } 95 | break; 96 | } 97 | default: { 98 | return luaL_error(L, "wrong number of arguments to 'insert'"); 99 | } 100 | } 101 | lua_seti(L, 1, pos); /* t[pos] = v */ 102 | return 0; 103 | } 104 | 105 | 106 | static int tremove (lua_State *L) { 107 | lua_Integer size = aux_getn(L, 1, TAB_RW); 108 | lua_Integer pos = luaL_optinteger(L, 2, size); 109 | if (pos != size) /* validate 'pos' if given */ 110 | luaL_argcheck(L, 1 <= pos && pos <= size + 1, 1, "position out of bounds"); 111 | lua_geti(L, 1, pos); /* result = t[pos] */ 112 | for ( ; pos < size; pos++) { 113 | lua_geti(L, 1, pos + 1); 114 | lua_seti(L, 1, pos); /* t[pos] = t[pos + 1] */ 115 | } 116 | lua_pushnil(L); 117 | lua_seti(L, 1, pos); /* t[pos] = nil */ 118 | return 1; 119 | } 120 | 121 | 122 | /* 123 | ** Copy elements (1[f], ..., 1[e]) into (tt[t], tt[t+1], ...). Whenever 124 | ** possible, copy in increasing order, which is better for rehashing. 125 | ** "possible" means destination after original range, or smaller 126 | ** than origin, or copying to another table. 127 | */ 128 | static int tmove (lua_State *L) { 129 | lua_Integer f = luaL_checkinteger(L, 2); 130 | lua_Integer e = luaL_checkinteger(L, 3); 131 | lua_Integer t = luaL_checkinteger(L, 4); 132 | int tt = !lua_isnoneornil(L, 5) ? 5 : 1; /* destination table */ 133 | checktab(L, 1, TAB_R); 134 | checktab(L, tt, TAB_W); 135 | if (e >= f) { /* otherwise, nothing to move */ 136 | lua_Integer n, i; 137 | luaL_argcheck(L, f > 0 || e < LUA_MAXINTEGER + f, 3, 138 | "too many elements to move"); 139 | n = e - f + 1; /* number of elements to move */ 140 | luaL_argcheck(L, t <= LUA_MAXINTEGER - n + 1, 4, 141 | "destination wrap around"); 142 | if (t > e || t <= f || (tt != 1 && !lua_compare(L, 1, tt, LUA_OPEQ))) { 143 | for (i = 0; i < n; i++) { 144 | lua_geti(L, 1, f + i); 145 | lua_seti(L, tt, t + i); 146 | } 147 | } 148 | else { 149 | for (i = n - 1; i >= 0; i--) { 150 | lua_geti(L, 1, f + i); 151 | lua_seti(L, tt, t + i); 152 | } 153 | } 154 | } 155 | lua_pushvalue(L, tt); /* return destination table */ 156 | return 1; 157 | } 158 | 159 | 160 | static void addfield (lua_State *L, luaL_Buffer *b, lua_Integer i) { 161 | lua_geti(L, 1, i); 162 | if (!lua_isstring(L, -1)) 163 | luaL_error(L, "invalid value (%s) at index %d in table for 'concat'", 164 | luaL_typename(L, -1), i); 165 | luaL_addvalue(b); 166 | } 167 | 168 | 169 | static int tconcat (lua_State *L) { 170 | luaL_Buffer b; 171 | lua_Integer last = aux_getn(L, 1, TAB_R); 172 | size_t lsep; 173 | const char *sep = luaL_optlstring(L, 2, "", &lsep); 174 | lua_Integer i = luaL_optinteger(L, 3, 1); 175 | last = luaL_optinteger(L, 4, last); 176 | luaL_buffinit(L, &b); 177 | for (; i < last; i++) { 178 | addfield(L, &b, i); 179 | luaL_addlstring(&b, sep, lsep); 180 | } 181 | if (i == last) /* add last value (if interval was not empty) */ 182 | addfield(L, &b, i); 183 | luaL_pushresult(&b); 184 | return 1; 185 | } 186 | 187 | 188 | /* 189 | ** {====================================================== 190 | ** Pack/unpack 191 | ** ======================================================= 192 | */ 193 | 194 | static int pack (lua_State *L) { 195 | int i; 196 | int n = lua_gettop(L); /* number of elements to pack */ 197 | lua_createtable(L, n, 1); /* create result table */ 198 | lua_insert(L, 1); /* put it at index 1 */ 199 | for (i = n; i >= 1; i--) /* assign elements */ 200 | lua_seti(L, 1, i); 201 | lua_pushinteger(L, n); 202 | lua_setfield(L, 1, "n"); /* t.n = number of elements */ 203 | return 1; /* return table */ 204 | } 205 | 206 | 207 | static int unpack (lua_State *L) { 208 | lua_Unsigned n; 209 | lua_Integer i = luaL_optinteger(L, 2, 1); 210 | lua_Integer e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1)); 211 | if (i > e) return 0; /* empty range */ 212 | n = (lua_Unsigned)e - i; /* number of elements minus 1 (avoid overflows) */ 213 | if (n >= (unsigned int)INT_MAX || !lua_checkstack(L, (int)(++n))) 214 | return luaL_error(L, "too many results to unpack"); 215 | for (; i < e; i++) { /* push arg[i..e - 1] (to avoid overflows) */ 216 | lua_geti(L, 1, i); 217 | } 218 | lua_geti(L, 1, e); /* push last element */ 219 | return (int)n; 220 | } 221 | 222 | /* }====================================================== */ 223 | 224 | 225 | 226 | /* 227 | ** {====================================================== 228 | ** Quicksort 229 | ** (based on 'Algorithms in MODULA-3', Robert Sedgewick; 230 | ** Addison-Wesley, 1993.) 231 | ** ======================================================= 232 | */ 233 | 234 | 235 | /* type for array indices */ 236 | typedef unsigned int IdxT; 237 | 238 | 239 | /* 240 | ** Produce a "random" 'unsigned int' to randomize pivot choice. This 241 | ** macro is used only when 'sort' detects a big imbalance in the result 242 | ** of a partition. (If you don't want/need this "randomness", ~0 is a 243 | ** good choice.) 244 | */ 245 | #if !defined(l_randomizePivot) /* { */ 246 | 247 | #include 248 | 249 | /* size of 'e' measured in number of 'unsigned int's */ 250 | #define sof(e) (sizeof(e) / sizeof(unsigned int)) 251 | 252 | /* 253 | ** Use 'time' and 'clock' as sources of "randomness". Because we don't 254 | ** know the types 'clock_t' and 'time_t', we cannot cast them to 255 | ** anything without risking overflows. A safe way to use their values 256 | ** is to copy them to an array of a known type and use the array values. 257 | */ 258 | static unsigned int l_randomizePivot (void) { 259 | clock_t c = clock(); 260 | time_t t = time(NULL); 261 | unsigned int buff[sof(c) + sof(t)]; 262 | unsigned int i, rnd = 0; 263 | memcpy(buff, &c, sof(c) * sizeof(unsigned int)); 264 | memcpy(buff + sof(c), &t, sof(t) * sizeof(unsigned int)); 265 | for (i = 0; i < sof(buff); i++) 266 | rnd += buff[i]; 267 | return rnd; 268 | } 269 | 270 | #endif /* } */ 271 | 272 | 273 | /* arrays larger than 'RANLIMIT' may use randomized pivots */ 274 | #define RANLIMIT 100u 275 | 276 | 277 | static void set2 (lua_State *L, IdxT i, IdxT j) { 278 | lua_seti(L, 1, i); 279 | lua_seti(L, 1, j); 280 | } 281 | 282 | 283 | /* 284 | ** Return true iff value at stack index 'a' is less than the value at 285 | ** index 'b' (according to the order of the sort). 286 | */ 287 | static int sort_comp (lua_State *L, int a, int b) { 288 | if (lua_isnil(L, 2)) /* no function? */ 289 | return lua_compare(L, a, b, LUA_OPLT); /* a < b */ 290 | else { /* function */ 291 | int res; 292 | lua_pushvalue(L, 2); /* push function */ 293 | lua_pushvalue(L, a-1); /* -1 to compensate function */ 294 | lua_pushvalue(L, b-2); /* -2 to compensate function and 'a' */ 295 | lua_call(L, 2, 1); /* call function */ 296 | res = lua_toboolean(L, -1); /* get result */ 297 | lua_pop(L, 1); /* pop result */ 298 | return res; 299 | } 300 | } 301 | 302 | 303 | /* 304 | ** Does the partition: Pivot P is at the top of the stack. 305 | ** precondition: a[lo] <= P == a[up-1] <= a[up], 306 | ** so it only needs to do the partition from lo + 1 to up - 2. 307 | ** Pos-condition: a[lo .. i - 1] <= a[i] == P <= a[i + 1 .. up] 308 | ** returns 'i'. 309 | */ 310 | static IdxT partition (lua_State *L, IdxT lo, IdxT up) { 311 | IdxT i = lo; /* will be incremented before first use */ 312 | IdxT j = up - 1; /* will be decremented before first use */ 313 | /* loop invariant: a[lo .. i] <= P <= a[j .. up] */ 314 | for (;;) { 315 | /* next loop: repeat ++i while a[i] < P */ 316 | while (lua_geti(L, 1, ++i), sort_comp(L, -1, -2)) { 317 | if (i == up - 1) /* a[i] < P but a[up - 1] == P ?? */ 318 | luaL_error(L, "invalid order function for sorting"); 319 | lua_pop(L, 1); /* remove a[i] */ 320 | } 321 | /* after the loop, a[i] >= P and a[lo .. i - 1] < P */ 322 | /* next loop: repeat --j while P < a[j] */ 323 | while (lua_geti(L, 1, --j), sort_comp(L, -3, -1)) { 324 | if (j < i) /* j < i but a[j] > P ?? */ 325 | luaL_error(L, "invalid order function for sorting"); 326 | lua_pop(L, 1); /* remove a[j] */ 327 | } 328 | /* after the loop, a[j] <= P and a[j + 1 .. up] >= P */ 329 | if (j < i) { /* no elements out of place? */ 330 | /* a[lo .. i - 1] <= P <= a[j + 1 .. i .. up] */ 331 | lua_pop(L, 1); /* pop a[j] */ 332 | /* swap pivot (a[up - 1]) with a[i] to satisfy pos-condition */ 333 | set2(L, up - 1, i); 334 | return i; 335 | } 336 | /* otherwise, swap a[i] - a[j] to restore invariant and repeat */ 337 | set2(L, i, j); 338 | } 339 | } 340 | 341 | 342 | /* 343 | ** Choose an element in the middle (2nd-3th quarters) of [lo,up] 344 | ** "randomized" by 'rnd' 345 | */ 346 | static IdxT choosePivot (IdxT lo, IdxT up, unsigned int rnd) { 347 | IdxT r4 = (up - lo) / 4; /* range/4 */ 348 | IdxT p = rnd % (r4 * 2) + (lo + r4); 349 | lua_assert(lo + r4 <= p && p <= up - r4); 350 | return p; 351 | } 352 | 353 | 354 | /* 355 | ** QuickSort algorithm (recursive function) 356 | */ 357 | static void auxsort (lua_State *L, IdxT lo, IdxT up, 358 | unsigned int rnd) { 359 | while (lo < up) { /* loop for tail recursion */ 360 | IdxT p; /* Pivot index */ 361 | IdxT n; /* to be used later */ 362 | /* sort elements 'lo', 'p', and 'up' */ 363 | lua_geti(L, 1, lo); 364 | lua_geti(L, 1, up); 365 | if (sort_comp(L, -1, -2)) /* a[up] < a[lo]? */ 366 | set2(L, lo, up); /* swap a[lo] - a[up] */ 367 | else 368 | lua_pop(L, 2); /* remove both values */ 369 | if (up - lo == 1) /* only 2 elements? */ 370 | return; /* already sorted */ 371 | if (up - lo < RANLIMIT || rnd == 0) /* small interval or no randomize? */ 372 | p = (lo + up)/2; /* middle element is a good pivot */ 373 | else /* for larger intervals, it is worth a random pivot */ 374 | p = choosePivot(lo, up, rnd); 375 | lua_geti(L, 1, p); 376 | lua_geti(L, 1, lo); 377 | if (sort_comp(L, -2, -1)) /* a[p] < a[lo]? */ 378 | set2(L, p, lo); /* swap a[p] - a[lo] */ 379 | else { 380 | lua_pop(L, 1); /* remove a[lo] */ 381 | lua_geti(L, 1, up); 382 | if (sort_comp(L, -1, -2)) /* a[up] < a[p]? */ 383 | set2(L, p, up); /* swap a[up] - a[p] */ 384 | else 385 | lua_pop(L, 2); 386 | } 387 | if (up - lo == 2) /* only 3 elements? */ 388 | return; /* already sorted */ 389 | lua_geti(L, 1, p); /* get middle element (Pivot) */ 390 | lua_pushvalue(L, -1); /* push Pivot */ 391 | lua_geti(L, 1, up - 1); /* push a[up - 1] */ 392 | set2(L, p, up - 1); /* swap Pivot (a[p]) with a[up - 1] */ 393 | p = partition(L, lo, up); 394 | /* a[lo .. p - 1] <= a[p] == P <= a[p + 1 .. up] */ 395 | if (p - lo < up - p) { /* lower interval is smaller? */ 396 | auxsort(L, lo, p - 1, rnd); /* call recursively for lower interval */ 397 | n = p - lo; /* size of smaller interval */ 398 | lo = p + 1; /* tail call for [p + 1 .. up] (upper interval) */ 399 | } 400 | else { 401 | auxsort(L, p + 1, up, rnd); /* call recursively for upper interval */ 402 | n = up - p; /* size of smaller interval */ 403 | up = p - 1; /* tail call for [lo .. p - 1] (lower interval) */ 404 | } 405 | if ((up - lo) / 128 > n) /* partition too imbalanced? */ 406 | rnd = l_randomizePivot(); /* try a new randomization */ 407 | } /* tail call auxsort(L, lo, up, rnd) */ 408 | } 409 | 410 | 411 | static int sort (lua_State *L) { 412 | lua_Integer n = aux_getn(L, 1, TAB_RW); 413 | if (n > 1) { /* non-trivial interval? */ 414 | luaL_argcheck(L, n < INT_MAX, 1, "array too big"); 415 | if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */ 416 | luaL_checktype(L, 2, LUA_TFUNCTION); /* must be a function */ 417 | lua_settop(L, 2); /* make sure there are two arguments */ 418 | auxsort(L, 1, (IdxT)n, 0); 419 | } 420 | return 0; 421 | } 422 | 423 | /* }====================================================== */ 424 | 425 | 426 | static const luaL_Reg tab_funcs[] = { 427 | {"concat", tconcat}, 428 | #if defined(LUA_COMPAT_MAXN) 429 | {"maxn", maxn}, 430 | #endif 431 | {"insert", tinsert}, 432 | {"pack", pack}, 433 | {"unpack", unpack}, 434 | {"remove", tremove}, 435 | {"move", tmove}, 436 | {"sort", sort}, 437 | {NULL, NULL} 438 | }; 439 | 440 | 441 | LUAMOD_API int luaopen_table (lua_State *L) { 442 | luaL_newlib(L, tab_funcs); 443 | #if defined(LUA_COMPAT_UNPACK) 444 | /* _G.unpack = table.unpack */ 445 | lua_getfield(L, -1, "unpack"); 446 | lua_setglobal(L, "unpack"); 447 | #endif 448 | return 1; 449 | } 450 | 451 | -------------------------------------------------------------------------------- /src/connection.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | #include 6 | #include "compat-5.3.h" 7 | 8 | #include 9 | 10 | #include "ldbus.h" 11 | #include "message.h" 12 | #include "pending_call.h" 13 | #include "timeout.h" 14 | #include "watch.h" 15 | #include "error.h" 16 | #include "connection.h" 17 | 18 | static const char *const DispatchStatus_lst [] = { 19 | "data_remains", /* DBUS_DISPATCH_DATA_REMAINS == 0 */ 20 | "complete", /* DBUS_DISPATCH_COMPLETE == 1 */ 21 | "need_memory", /* DBUS_DISPATCH_NEED_MEMORY == 2 */ 22 | NULL 23 | }; 24 | 25 | static int ldbus_connection_open(lua_State *L) { 26 | const char *address = luaL_checkstring(L, 1); 27 | 28 | DBusError *error = new_DBusError(L); 29 | DBusConnection *connection = dbus_connection_open(address, error); 30 | 31 | if (dbus_error_is_set(error)) { 32 | lua_pushnil(L); 33 | lua_pushstring(L, error->message); 34 | return 2; 35 | } else { 36 | push_DBusConnection(L, connection, FALSE); 37 | return 1; 38 | } 39 | } 40 | 41 | static int ldbus_connection_gc(lua_State *L) { 42 | lDBusConnection *udata = check_lDBusConnection(L, 1); 43 | 44 | if (udata->close) { 45 | /* You must close a connection prior to releasing the last reference to the connection. 46 | If you dbus_connection_unref() for the last time without closing the connection, 47 | the results are undefined 48 | */ 49 | dbus_connection_close(udata->connection); 50 | } 51 | 52 | dbus_connection_unref(udata->connection); 53 | 54 | return 0; 55 | } 56 | 57 | static int ldbus_connection_get_is_connected(lua_State *L) { 58 | DBusConnection *connection = check_DBusConnection(L, 1); 59 | 60 | lua_pushboolean(L, dbus_connection_get_is_connected(connection)); 61 | 62 | return 1; 63 | } 64 | 65 | static int ldbus_connection_get_is_authenticated(lua_State *L) { 66 | DBusConnection *connection = check_DBusConnection(L, 1); 67 | 68 | lua_pushboolean(L, dbus_connection_get_is_authenticated(connection)); 69 | 70 | return 1; 71 | } 72 | 73 | static int ldbus_connection_get_is_anonymous(lua_State *L) { 74 | DBusConnection *connection = check_DBusConnection(L, 1); 75 | 76 | lua_pushboolean(L, dbus_connection_get_is_anonymous(connection)); 77 | 78 | return 1; 79 | } 80 | 81 | static int ldbus_connection_get_server_id(lua_State *L) { 82 | DBusConnection *connection = check_DBusConnection(L, 1); 83 | 84 | char *result = dbus_connection_get_server_id(connection); 85 | if (result == NULL) { 86 | lua_pushnil(L); 87 | } else { 88 | lua_pushstring(L, result); 89 | } 90 | 91 | return 1; 92 | } 93 | 94 | static int ldbus_connection_send(lua_State *L) { 95 | DBusConnection *connection = check_DBusConnection(L, 1); 96 | DBusMessage *message = check_DBusMessage(L, 2); 97 | 98 | unsigned int serial; 99 | lua_pushboolean(L, dbus_connection_send(connection, message, &serial)); 100 | lua_pushinteger(L, serial); 101 | 102 | return 2; 103 | } 104 | 105 | static int ldbus_connection_send_with_reply(lua_State *L) { 106 | DBusConnection *connection = check_DBusConnection(L, 1); 107 | DBusMessage *message = check_DBusMessage(L, 2); 108 | int timeout_milliseconds = lua_isnoneornil(L, 3) ? (-1) : (luaL_checknumber(L, 3) * 1000); 109 | 110 | DBusPendingCall *pending; 111 | if (!dbus_connection_send_with_reply(connection, message, &pending, timeout_milliseconds)) { 112 | return luaL_error(L, LDBUS_NO_MEMORY); 113 | } 114 | 115 | push_DBusPendingCall(L, pending); 116 | return 1; 117 | } 118 | 119 | static int ldbus_connection_send_with_reply_and_block(lua_State *L) { 120 | DBusConnection *connection = check_DBusConnection(L, 1); 121 | DBusMessage *message = check_DBusMessage(L, 2); 122 | int timeout_milliseconds = lua_isnoneornil(L, 3) ? (-1) : (luaL_checknumber(L, 3) * 1000); 123 | DBusMessage *reply; 124 | DBusError error; 125 | dbus_error_init(&error); 126 | reply = dbus_connection_send_with_reply_and_block(connection, message, timeout_milliseconds, &error); 127 | if (dbus_error_is_set(&error)) { 128 | lua_pushnil(L); 129 | lua_pushstring(L, error.message); 130 | dbus_error_free(&error); 131 | return 2; 132 | } else { 133 | push_DBusMessage(L, reply); 134 | return 1; 135 | } 136 | } 137 | 138 | static int ldbus_connection_flush(lua_State *L) { 139 | DBusConnection *connection = check_DBusConnection(L, 1); 140 | 141 | dbus_connection_flush(connection); 142 | 143 | return 0; 144 | } 145 | 146 | static int ldbus_connection_read_write_dispatch(lua_State *L) { 147 | DBusConnection *connection = check_DBusConnection(L, 1); 148 | int timeout_milliseconds = luaL_optinteger(L, 2, -1); 149 | 150 | lua_pushboolean(L, dbus_connection_read_write_dispatch(connection, timeout_milliseconds)); 151 | 152 | return 1; 153 | } 154 | 155 | static int ldbus_connection_read_write(lua_State *L) { 156 | DBusConnection *connection = check_DBusConnection(L, 1); 157 | int timeout_milliseconds = luaL_optinteger(L, 2, -1); 158 | 159 | lua_pushboolean(L, dbus_connection_read_write(connection, timeout_milliseconds)); 160 | 161 | return 1; 162 | } 163 | 164 | static int ldbus_connection_pop_message(lua_State *L) { 165 | DBusConnection *connection = check_DBusConnection(L, 1); 166 | 167 | DBusMessage *message = dbus_connection_pop_message(connection); 168 | 169 | if (message == NULL) { 170 | lua_pushnil(L); 171 | } else { 172 | push_DBusMessage(L, message); 173 | } 174 | return 1; 175 | } 176 | 177 | static int ldbus_connection_get_dispatch_status(lua_State *L) { 178 | DBusConnection *connection = check_DBusConnection(L, 1); 179 | 180 | lua_pushstring(L, DispatchStatus_lst [ dbus_connection_get_dispatch_status(connection) ]); 181 | return 1; 182 | } 183 | 184 | static int ldbus_connection_dispatch(lua_State *L) { 185 | DBusConnection *connection = check_DBusConnection(L, 1); 186 | 187 | lua_pushstring(L, DispatchStatus_lst [ dbus_connection_dispatch(connection) ]); 188 | return 1; 189 | } 190 | 191 | static int ldbus_connection_set_watch_functions(lua_State *L) { 192 | lua_State **data; 193 | DBusConnection *connection = check_DBusConnection(L, 1); 194 | int has_toggle = lua_isnil(L, 4); 195 | 196 | /* make sure ldbus.watch has been loaded */ 197 | luaL_requiref(L, "ldbus.watch", luaopen_ldbus_watch, FALSE); 198 | lua_pop(L, 1); 199 | 200 | data = lua_newuserdata(L, sizeof(lua_State*)); 201 | *data = L; 202 | lua_createtable(L, 4, 0); 203 | lua_pushvalue(L, 2); 204 | lua_rawseti(L, -2, DBUS_LUA_FUNC_ADD); 205 | lua_pushvalue(L, 3); 206 | lua_rawseti(L, -2, DBUS_LUA_FUNC_REMOVE); 207 | if (has_toggle) { 208 | lua_pushvalue(L, 4); 209 | lua_rawseti(L, -2, DBUS_LUA_FUNC_TOGGLE); 210 | } 211 | lua_pushthread(L); 212 | lua_rawseti(L, -2, DBUS_LUA_FUNC_THREAD); 213 | lua_setuservalue(L, -2); 214 | lua_rawsetp(L, LUA_REGISTRYINDEX, data); 215 | if (!dbus_connection_set_watch_functions(connection, 216 | ldbus_watch_add_function, ldbus_watch_remove_function, 217 | has_toggle ? NULL : ldbus_watch_toggled_function, 218 | (void *)data, ldbus_watch_free_data_function)) { 219 | ldbus_watch_free_data_function(data); 220 | return luaL_error(L, LDBUS_NO_MEMORY); 221 | }; 222 | lua_pushboolean(L, TRUE); 223 | return 1; 224 | } 225 | 226 | static int ldbus_connection_set_timeout_functions(lua_State *L) { 227 | lua_State **data; 228 | DBusConnection *connection = check_DBusConnection(L, 1); 229 | 230 | /* make sure ldbus.watch has been loaded */ 231 | luaL_requiref(L, "ldbus.timeout", lua_open_ldbus_timeout, FALSE); 232 | lua_pop(L, 1); 233 | 234 | data = lua_newuserdata(L, sizeof(lua_State*)); 235 | *data = L; 236 | lua_createtable(L, 4, 0); 237 | lua_pushvalue(L, 2); 238 | lua_rawseti(L, -2, DBUS_LUA_FUNC_ADD); 239 | lua_pushvalue(L, 3); 240 | lua_rawseti(L, -2, DBUS_LUA_FUNC_REMOVE); 241 | lua_pushvalue(L, 4); 242 | lua_rawseti(L, -2, DBUS_LUA_FUNC_TOGGLE); 243 | lua_pushthread(L); 244 | lua_rawseti(L, -2, DBUS_LUA_FUNC_THREAD); 245 | lua_setuservalue(L, -2); 246 | lua_rawsetp(L, LUA_REGISTRYINDEX, data); 247 | 248 | if (!dbus_connection_set_timeout_functions(connection, 249 | ldbus_timeout_add_function, ldbus_timeout_remove_function, ldbus_timeout_toggled_function, 250 | (void *)data, ldbus_timeout_free_data_function)) { 251 | ldbus_timeout_free_data_function(data); 252 | return luaL_error(L, LDBUS_NO_MEMORY); 253 | }; 254 | lua_pushboolean(L, TRUE); 255 | return 1; 256 | } 257 | 258 | static void free_data_function(void *data) { 259 | lua_State *L = *(lua_State**)data; 260 | lua_pushnil(L); 261 | lua_rawsetp(L, LUA_REGISTRYINDEX, data); 262 | } 263 | 264 | static void wakeup_main_function(void *data) { 265 | lua_State *L = *(lua_State**)data; 266 | int top = lua_gettop(L); 267 | lua_rawgetp(L, LUA_REGISTRYINDEX, data); 268 | lua_getuservalue(L, -1); 269 | lua_remove(L, top + 1); /* remove userdata */ 270 | lua_rawgeti(L, -1, DBUS_LUA_FUNC_FUNC); 271 | lua_remove(L, top + 1); /* remove uservalue table */ 272 | if (lua_pcall(L, 0, 0, 0) != LUA_OK) { 273 | /* unhandled error */ 274 | lua_pop(L, 1); 275 | } 276 | } 277 | 278 | static int ldbus_connection_set_wakeup_main_function(lua_State *L) { 279 | DBusConnection *connection = check_DBusConnection(L, 1); 280 | lua_State **data; 281 | luaL_checktype(L, 2, LUA_TFUNCTION); 282 | data = lua_newuserdata(L, sizeof(lua_State*)); 283 | *data = L; 284 | lua_createtable(L, 2, 0); 285 | lua_pushvalue(L, 2); 286 | lua_rawseti(L, -2, DBUS_LUA_FUNC_FUNC); 287 | lua_pushthread(L); 288 | lua_rawseti(L, -2, DBUS_LUA_FUNC_THREAD); 289 | lua_setuservalue(L, -2); 290 | lua_rawsetp(L, LUA_REGISTRYINDEX, data); 291 | dbus_connection_set_wakeup_main_function(connection, wakeup_main_function, data, free_data_function); 292 | lua_pushboolean(L, 1); 293 | return 1; 294 | } 295 | 296 | static void dispatch_status_function(DBusConnection *connection, DBusDispatchStatus new_status, void *data) { 297 | lua_State *L = *(lua_State**)data; 298 | int top = lua_gettop(L); 299 | UNUSED(connection); 300 | lua_rawgetp(L, LUA_REGISTRYINDEX, data); 301 | lua_getuservalue(L, -1); 302 | lua_remove(L, top + 1); /* remove userdata */ 303 | lua_rawgeti(L, -1, DBUS_LUA_FUNC_FUNC); 304 | lua_remove(L, top + 1); /* remove uservalue table */ 305 | lua_pushstring(L, DispatchStatus_lst[new_status]); /* XXX: could fail */ 306 | if (lua_pcall(L, 1, 0, 0) != LUA_OK) { 307 | /* unhandled error */ 308 | lua_pop(L, 1); 309 | } 310 | } 311 | 312 | static int ldbus_connection_set_dispatch_status_function(lua_State *L) { 313 | DBusConnection *connection = check_DBusConnection(L, 1); 314 | lua_State **data; 315 | luaL_checktype(L, 2, LUA_TFUNCTION); 316 | data = lua_newuserdata(L, sizeof(lua_State*)); 317 | *data = L; 318 | lua_createtable(L, 2, 0); 319 | lua_pushvalue(L, 2); 320 | lua_rawseti(L, -2, DBUS_LUA_FUNC_FUNC); 321 | lua_pushthread(L); 322 | lua_rawseti(L, -2, DBUS_LUA_FUNC_THREAD); 323 | lua_setuservalue(L, -2); 324 | lua_rawsetp(L, LUA_REGISTRYINDEX, data); 325 | dbus_connection_set_dispatch_status_function(connection, dispatch_status_function, data, free_data_function); 326 | lua_pushboolean(L, 1); 327 | return 1; 328 | } 329 | 330 | static int ldbus_connection_set_max_message_size(lua_State *L) { 331 | DBusConnection *connection = check_DBusConnection(L, 1); 332 | long size = luaL_checkinteger(L, 2); 333 | 334 | dbus_connection_set_max_message_size(connection, size); 335 | 336 | return 0; 337 | } 338 | 339 | static int ldbus_connection_get_max_message_size(lua_State *L) { 340 | DBusConnection *connection = check_DBusConnection(L, 1); 341 | 342 | lua_pushinteger(L, dbus_connection_get_max_message_size(connection)); 343 | 344 | return 1; 345 | } 346 | 347 | static int ldbus_connection_set_max_received_size(lua_State *L) { 348 | DBusConnection *connection = check_DBusConnection(L, 1); 349 | long size = luaL_checkinteger(L, 2); 350 | 351 | dbus_connection_set_max_received_size(connection, size); 352 | 353 | return 0; 354 | } 355 | 356 | static int ldbus_connection_get_max_received_size(lua_State *L) { 357 | DBusConnection *connection = check_DBusConnection(L, 1); 358 | 359 | lua_pushinteger(L, dbus_connection_get_max_received_size(connection)); 360 | 361 | return 1; 362 | } 363 | 364 | static int ldbus_connection_get_outgoing_size(lua_State *L) { 365 | DBusConnection *connection = check_DBusConnection(L, 1); 366 | 367 | lua_pushinteger(L, dbus_connection_get_outgoing_size(connection)); 368 | 369 | return 1; 370 | } 371 | 372 | static int ldbus_connection_has_messages_to_send(lua_State *L) { 373 | DBusConnection *connection = check_DBusConnection(L, 1); 374 | 375 | lua_pushboolean(L, dbus_connection_has_messages_to_send(connection)); 376 | 377 | return 1; 378 | } 379 | 380 | static void unregister_function(DBusConnection *connection, void *data) { 381 | lua_State *L = *(lua_State**)data; 382 | UNUSED(connection); 383 | lua_pushnil(L); 384 | lua_rawsetp(L, LUA_REGISTRYINDEX, data); 385 | } 386 | 387 | static DBusHandlerResult message_function(DBusConnection *connection, DBusMessage *message, void *data) { 388 | lua_State *L = *(lua_State**)data; 389 | int top = lua_gettop(L); 390 | UNUSED(connection); 391 | if (!lua_checkstack(L, 2)) { 392 | return DBUS_HANDLER_RESULT_NEED_MEMORY; 393 | } 394 | lua_rawgetp(L, LUA_REGISTRYINDEX, data); 395 | lua_getuservalue(L, -1); 396 | lua_remove(L, top + 1); /* remove userdata */ 397 | lua_rawgeti(L, -1, DBUS_LUA_FUNC_FUNC); 398 | lua_remove(L, top + 1); /* remove uservalue table */ 399 | dbus_message_ref(message); /* Keep a reference for lua */ 400 | push_DBusMessage(L, message); /* XXX: Could throw? */ 401 | switch(lua_pcall(L, 1, 1, 0)) { 402 | case LUA_OK: 403 | if (lua_toboolean(L, -1)) { 404 | return DBUS_HANDLER_RESULT_HANDLED; 405 | } else { 406 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; 407 | } 408 | case LUA_ERRMEM: 409 | lua_pop(L, 1); 410 | return DBUS_HANDLER_RESULT_NEED_MEMORY; 411 | default: 412 | lua_pop(L, 1); 413 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; 414 | } 415 | } 416 | 417 | static const DBusObjectPathVTable VTable = { 418 | unregister_function, 419 | message_function, 420 | NULL, NULL, NULL, NULL 421 | }; 422 | 423 | static int ldbus_connection_register_object_path(lua_State *L) { 424 | DBusConnection *connection = check_DBusConnection(L, 1); 425 | const char *path = luaL_checkstring(L, 2); 426 | lua_State **data; 427 | luaL_checktype(L, 3, LUA_TFUNCTION); 428 | data = lua_newuserdata(L, sizeof(lua_State*)); 429 | *data = L; 430 | lua_createtable(L, 2, 0); 431 | lua_pushvalue(L, 3); 432 | lua_rawseti(L, -2, DBUS_LUA_FUNC_FUNC); 433 | lua_pushthread(L); 434 | lua_rawseti(L, -2, DBUS_LUA_FUNC_THREAD); 435 | lua_setuservalue(L, -2); 436 | lua_rawsetp(L, LUA_REGISTRYINDEX, data); 437 | if (!dbus_connection_register_object_path(connection, path, &VTable, data)) { 438 | unregister_function(connection, data); 439 | return luaL_error(L, "unknown error"); 440 | } 441 | lua_pushboolean(L, 1); 442 | return 1; 443 | } 444 | 445 | static int ldbus_connection_register_fallback(lua_State *L) { 446 | DBusConnection *connection = check_DBusConnection(L, 1); 447 | const char *path = luaL_checkstring(L, 2); 448 | lua_State **data; 449 | luaL_checktype(L, 3, LUA_TFUNCTION); 450 | data = lua_newuserdata(L, sizeof(lua_State*)); 451 | *data = L; 452 | lua_createtable(L, 2, 0); 453 | lua_pushvalue(L, 3); 454 | lua_rawseti(L, -2, DBUS_LUA_FUNC_FUNC); 455 | lua_pushthread(L); 456 | lua_rawseti(L, -2, DBUS_LUA_FUNC_THREAD); 457 | lua_setuservalue(L, -2); 458 | lua_rawsetp(L, LUA_REGISTRYINDEX, data); 459 | if (!dbus_connection_register_fallback(connection, path, &VTable, data)) { 460 | unregister_function(connection, data); 461 | return luaL_error(L, "unknown error"); 462 | } 463 | lua_pushboolean(L, 1); 464 | return 1; 465 | } 466 | 467 | static int ldbus_connection_unregister_object_path(lua_State *L) { 468 | DBusConnection *connection = check_DBusConnection(L, 1); 469 | const char *path = luaL_checkstring(L, 2); 470 | if (!dbus_connection_unregister_object_path(connection, path)) { 471 | return luaL_error(L, LDBUS_NO_MEMORY); 472 | } 473 | lua_pushboolean(L, 1); 474 | return 1; 475 | } 476 | 477 | static luaL_Reg const methods [] = { 478 | { "get_is_connected", ldbus_connection_get_is_connected }, 479 | { "get_is_authenticated", ldbus_connection_get_is_authenticated }, 480 | { "get_is_anonymous", ldbus_connection_get_is_anonymous }, 481 | { "get_server_id", ldbus_connection_get_server_id }, 482 | { "send", ldbus_connection_send }, 483 | { "send_with_reply", ldbus_connection_send_with_reply }, 484 | { "send_with_reply_and_block", ldbus_connection_send_with_reply_and_block }, 485 | { "flush", ldbus_connection_flush }, 486 | { "read_write_dispatch", ldbus_connection_read_write_dispatch}, 487 | { "read_write", ldbus_connection_read_write }, 488 | { "pop_message", ldbus_connection_pop_message }, 489 | { "get_dispatch_status", ldbus_connection_get_dispatch_status }, 490 | { "dispatch", ldbus_connection_dispatch }, 491 | { "set_watch_functions", ldbus_connection_set_watch_functions }, 492 | { "set_timeout_functions", ldbus_connection_set_timeout_functions }, 493 | { "set_dispatch_status_function", ldbus_connection_set_dispatch_status_function }, 494 | { "set_wakeup_main_function", ldbus_connection_set_wakeup_main_function }, 495 | { "set_max_message_size", ldbus_connection_set_max_message_size }, 496 | { "get_max_message_size", ldbus_connection_get_max_message_size }, 497 | { "set_max_received_size", ldbus_connection_set_max_received_size }, 498 | { "get_max_received_size", ldbus_connection_get_max_received_size }, 499 | { "get_outgoing_size", ldbus_connection_get_outgoing_size }, 500 | { "has_messages_to_send", ldbus_connection_has_messages_to_send }, 501 | { "register_object_path", ldbus_connection_register_object_path }, 502 | { "register_fallback", ldbus_connection_register_fallback }, 503 | { "unregister_object_path", ldbus_connection_unregister_object_path }, 504 | { NULL, NULL } 505 | }; 506 | 507 | LDBUS_INTERNAL void push_DBusConnection(lua_State *L, DBusConnection *connection, bool close) { 508 | lDBusConnection *udata = lua_newuserdata(L, sizeof(lDBusConnection)); 509 | udata->connection = connection; 510 | udata->close = close; 511 | 512 | if (luaL_newmetatable(L, DBUS_CONNECTION_METATABLE)) { 513 | luaL_newlib(L, methods); 514 | lua_setfield(L, -2, "__index"); 515 | 516 | lua_pushcfunction(L, ldbus_connection_gc) ; 517 | lua_setfield(L, -2, "__gc"); 518 | 519 | lua_pushcfunction(L, tostring); 520 | lua_setfield(L, -2, "__tostring"); 521 | 522 | lua_pushstring(L, "DBusConnection"); 523 | lua_setfield(L, -2, "__udtype"); 524 | } 525 | lua_setmetatable(L, -2); 526 | } 527 | 528 | static const struct luaL_Reg ldbus_connection [] = { 529 | { "open", ldbus_connection_open }, 530 | { NULL, NULL } 531 | }; 532 | int luaopen_ldbus_connection(lua_State *L) { 533 | luaL_newlib(L, ldbus_connection); 534 | return 1; 535 | } 536 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # [DBus](http://www.freedesktop.org/wiki/Software/dbus/) for Lua. 2 | 3 | ldbus is a C binding to dbus for Lua. 4 | 5 | Compatible with Lua 5.1, 5.2, 5.3 and 5.4 (thanks [compat-5.3](https://github.com/keplerproject/lua-compat-5.3)). 6 | 7 | 8 | # Status 9 | 10 | 11 | # Installation 12 | 13 | ldbus is on luarocks: https://luarocks.org/modules/daurnimator/ldbus 14 | 15 | Install via luarocks, you will need to provide paths to `dbus/dbus.h` and `dbus/dbus-arch-deps.h` 16 | 17 | luarocks install --server=http://luarocks.org/manifests/daurnimator ldbus DBUS_INCDIR=/usr/include/dbus-1.0/ DBUS_ARCH_INCDIR=/usr/lib/dbus-1.0/include 18 | 19 | 20 | # Usage 21 | 22 | example.lua is a lua version of [Matthew Johnson's example](https://web.archive.org/web/20101224005221/http://dbus.freedesktop.org/doc/dbus/libdbus-tutorial.html) 23 | 24 | 25 | ### Bound from C 26 | 27 | - Functions that normally take a `DBusError` return `nil, "error_message"` on failure. 28 | - Errors will be thrown on out of memory 29 | 30 | C | Lua | Comments 31 | -----------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------|-------------------------- 32 | [`dbus_bus_get()`](http://dbus.freedesktop.org/doc/api/html/group__DBusBus.html#ga77ba5250adb84620f16007e1b023cf26) | `ldbus.bus.get()` | Takes `type` as one of `"session"`, `"system"` or `"starter"`. The connection will *not* have `exit_on_disconnect` set. 33 | [`dbus_bus_get_private()`](https://dbus.freedesktop.org/doc/api/html/group__DBusBus.html#ga9c62186f19cf3bd3c7c604bdcefb4e09) | `ldbus.bus.get_private()` | Takes `type` as one of `"session"`, `"system"` or `"starter"`. The connection will *not* have `exit_on_disconnect` set. 34 | [`dbus_bus_register()`](http://dbus.freedesktop.org/doc/api/html/group__DBusBus.html#ga0c21cf74d05c0bd8003846b56a588a4b) | `ldbus.bus.register()` | 35 | [`dbus_bus_set_unique_name()`](http://dbus.freedesktop.org/doc/api/html/group__DBusBus.html#ga0366177076e88bf37771341f32b0551c) | `ldbus.bus.set_unique_name()` | 36 | [`dbus_bus_get_unique_name()`](http://dbus.freedesktop.org/doc/api/html/group__DBusBus.html#ga8c10339a1e62f6a2e5752d9c2270d37b) | `ldbus.bus.get_unique_name()` | 37 | [`dbus_bus_request_name()`](http://dbus.freedesktop.org/doc/api/html/group__DBusBus.html#ga8a9024c78c4ea89b6271f19dbc7861b2) | `ldbus.bus.request_name()` | Takes `flags` as a table contain keys `"allow_replacement"`, `"do_not_queue"` and `"replace_existing"`; On success returns one of `"primary_owner"`, `"in_queue"`, `"exists"` or `"already_owner"` 38 | [`dbus_bus_release_name()`](http://dbus.freedesktop.org/doc/api/html/group__DBusBus.html#gaa4aa5ebe51cdbe8c0651609fc72e841a) | `ldbus.bus.release_name()` | On success, returns one of `"released"`, `"non_existant"` or `"not_owner"` 39 | [`dbus_bus_name_has_owner()`](http://dbus.freedesktop.org/doc/api/html/group__DBusBus.html#ga5331b172dd8ed00eec130e894c5c2a0b) | `ldbus.bus.name_has_owner()` | 40 | [`dbus_bus_start_service_by_name()`](http://dbus.freedesktop.org/doc/api/html/group__DBusBus.html#ga81d303bf29d7c97ad4690ce35071b090) | `ldbus.bus.start_service_by_name()` | 41 | [`dbus_bus_add_match()`](http://dbus.freedesktop.org/doc/api/html/group__DBusBus.html#ga4eb6401ba014da3dbe3dc4e2a8e5b3ef) | `ldbus.bus.add_match()` | On success, returns one of `"success"` or `"already_running"` 42 | [`dbus_bus_remove_match()`](http://dbus.freedesktop.org/doc/api/html/group__DBusBus.html#ga6e6b98e19fa4400de7ef99c27b866b99) | `ldbus.bus.remove_match()` | 43 | [`dbus_connection_open()`](http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html#gacd32f819820266598c6b6847dfddaf9c) | `ldbus.connection.open()` | 44 | [`dbus_connection_get_is_connected()`](http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html#ga611ae94556af36fe30bfb547366ca4e1) | `my_conn:get_is_connected()` | 45 | [`dbus_connection_get_is_authenticated()`](http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html#ga2f1fa02c9897b6f07f4d33c862de4a1d) | `my_conn:get_is_authenticated()` | 46 | [`dbus_connection_get_is_anonymous()`](http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html#gaa6c5d523e16d8a5b9316c92d9ff1ac17) | `my_conn:get_is_anonymous()` | 47 | [`dbus_connection_get_server_id()`](http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html#gae6c19e146a37f9de6a06c1617874bed9) | `my_conn:get_server_id()` | 48 | [`dbus_connection_send()`](http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html#gae1cb64f4cf550949b23fd3a756b2f7d0) | `my_conn:send()` | 49 | [`dbus_connection_send_with_reply()`](http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html#gaa215df7ab7ca6dce7be153c6b9cc80ba) | `pending_call = my_conn:send_with_reply(msg [, timeout])` | Timeout is in seconds, when not provided there will be no timeout. 50 | [`dbus_connection_send_with_reply_and_block()`](http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html#ga8d6431f17a9e53c9446d87c2ba8409f0) | `response_msg = my_conn:send_with_reply_and_block(msg [, timeout])` | Timeout is in seconds, when not provided there will be no timeout. 51 | [`dbus_connection_flush()`](http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html#ga10e68d9d2f41d655a4151ddeb807ff54) | `my_conn:flush()` | 52 | [`dbus_connection_read_write_dispatch()`](http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html#ga580d8766c23fe5f49418bc7d87b67dc6) | `my_conn:read_write_dispatch()` | 53 | [`dbus_connection_read_write()`](http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html#ga371163b4955a6e0bf0f1f70f38390c14) | `my_conn:read_write()` | 54 | [`dbus_connection_pop_message()`](http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html#ga1e40d994ea162ce767e78de1c4988566) | `my_conn:pop_message()` | 55 | [`dbus_connection_get_dispatch_status()`](http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html#ga893d18d8b36ffb371f16d13645071289) | `my_conn:get_dispatch_status()` | 56 | [`dbus_connection_dispatch()`](http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html#ga66ba7df50d75f4bda6b6e942430b81c7) | `my_conn:dispatch()` | 57 | [`dbus_connection_set_watch_functions()`](http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html#gaebf031eb444b4f847606aa27daa3d8e6) | `my_conn:set_watch_functions()` | 58 | [`dbus_connection_set_timeout_functions()`](http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html#gab3cbc68eec427e9ce1783b25d44fe93c) | `my_conn:set_timeout_functions()` | 59 | [`dbus_connection_set_dispatch_status_function()`](http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html#ga55ff88cd22c0672441c7deffbfb68fbf) | `my_conn:set_dispatch_status_function()` | 60 | [`dbus_connection_set_wakeup_main_function()`](http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html#ga2b1df13251c7ec348bcba39c0924e881) | `my_conn:set_wakeup_main_function(callback)` | 61 | [`dbus_connection_register_object_path()`](http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html#ga24730ca6fd2e9132873962a32df7628c) | `my_conn:register_object_path()` | 62 | [`dbus_connection_register_fallback()`](http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html#gac4473b37bfa74ccf7459959d27e7bc59) | `my_conn:register_fallback()` | 63 | [`dbus_connection_unregister_object_path()`](http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html#ga6ae8f005dedf24c5f2df1768795392fb) | `my_conn:unregister_object_path()` | 64 | [`dbus_connection_set_max_message_size()`](http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html#ga0d783462274a6c71d3767f5821c29ce9) | `my_conn:set_max_message_size()` | 65 | [`dbus_connection_get_max_message_size()`](http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html#ga7a459e64d7dca7b592136cec0a73422c) | `my_conn:get_max_message_size()` | 66 | [`dbus_connection_set_max_received_size()`](http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html#ga6565d75f16e6e803372b2ae3d94d991b) | `my_conn:set_max_received_size()` | 67 | [`dbus_connection_get_max_received_size()`](http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html#ga376529acf41d1d34b4f46c0d9d515c85) | `my_conn:get_max_received_size()` | 68 | [`dbus_connection_get_outgoing_size()`](http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html#ga47aff801f586e7116f9c54532bb1baf9) | `my_conn:get_outgoing_size()` | 69 | [`dbus_connection_has_messages_to_send()`](http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html#gac40563ec4c0e309d936daf3163ba9bb7) | `my_conn:has_messages_to_send()` | 70 | [`dbus_message_new()`](http://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#gab9e5bf8d87a95c5ca7026a791148ebd4) | `ldbus.message.new(type_str)` | The type is taken as a string (uses [`dbus_message_type_from_string()`](http://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#gabc69747028f1e5adedc68f5ffd538c74)) 71 | [`dbus_message_new_method_call()`](http://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#ga98ddc82450d818138ef326a284201ee0) | `ldbus.message.new_method_call()` | 72 | [`dbus_message_new_signal()`](http://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#ga6ce3213dfb17be7956affba40207a5a0) | `ldbus.message.new_signal()` | 73 | [`dbus_message_get_serial()`](http://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#ga390710c25564c80025a006c376da2030) | `my_message:get_serial()` | 74 | [`dbus_message_set_reply_serial()`](http://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#gaec08603ff3d49bbcded67d25188a23f1) | `my_message:set_reply_serial()` | 75 | [`dbus_message_get_reply_serial()`](http://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#ga94c43b2b237d842a6b91da6f94818d47) | `my_message:get_reply_serial()` | 76 | [`dbus_message_new_method_return()`](http://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#ga95142bd8288f397194ee0eefb1d27125) | `my_message:new_method_return()` | 77 | [`dbus_message_new_error()`](http://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#ga2ab896965aec97fb21293affeed36232) | `my_message:new_error()` | 78 | [`dbus_message_copy()`](http://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#ga4bed3858b3b48ec7c86d9fc56a6ce372) | `my_message:copy()` | 79 | [`dbus_message_get_type()`](http://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#ga41cace31999105137772b6257ea540f9) | `my_message:get_type()` | 80 | [`dbus_message_iter_init()`](http://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#ga9f98b47c84f0e401ea985e681de4e963) | `my_iter = my_message:iter_init([my_iter])` | Returns `nil` if message has no arguments 81 | [`dbus_message_iter_init_append()`](http://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#gaf733047c467ce21f4a53b65a388f1e9d) | `my_iter = my_message:iter_init_append([my_iter])` | 82 | [`dbus_message_set_no_reply()`](http://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#ga0e86aeb2dc6831ccc9a21fcbf8cc16f7) | `my_message:set_no_reply()` | 83 | [`dbus_message_get_no_reply()`](http://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#ga622d051a2e5f578814116a958b240aa4) | `my_message:get_no_reply()` | 84 | [`dbus_message_set_auto_start()`](http://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#ga1596d92a8d604f954b48c7410263d2f0) | `my_message:set_auto_start()` | 85 | [`dbus_message_get_auto_start()`](http://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#ga85d396a3a774e15c3dbb7704aa173384) | `my_message:get_auto_start()` | 86 | [`dbus_message_set_path()`](http://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#gaaf25da0ba1482266293d329314c21786) | `my_message:set_path()` | 87 | [`dbus_message_get_path()`](http://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#ga18adf731bb42d324fe2624407319e4af) | `my_message:get_path()` | 88 | [`dbus_message_get_path_decomposed()`](http://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#gaf2b5b3319da838b1f1b360c04a33f153) | `my_message:get_path_decomposed()` | 89 | [`dbus_message_set_interface()`](http://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#ga02b754855e4d9a1cade8e4fc17a3f5c7) | `my_message:set_interface()` | 90 | [`dbus_message_get_interface()`](http://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#ga1ad192bd4538cae556121a71b4e09d42) | `my_message:get_interface()` | 91 | [`dbus_message_set_member()`](http://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#ga3afdda6016816cc70b451d8657065208) | `my_message:set_member()` | 92 | [`dbus_message_get_member()`](http://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#gaf5c6b705c53db07a5ae2c6b76f230cf9) | `my_message:get_member()` | 93 | [`dbus_message_set_error_name()`](http://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#ga892f9857707371c2a53cec6b54c843c7) | `my_message:set_error_name()` | 94 | [`dbus_message_get_error_name()`](http://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#ga4e98b2283707a8e0313fc7c6fe3b1b5f) | `my_message:get_error_name()` | 95 | [`dbus_message_set_destination()`](http://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#gacc47c1af23addfc4198b70084ba068fc) | `my_message:set_destination()` | 96 | [`dbus_message_get_destination()`](http://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#gaac65c926e6253e49aa689b4f032fad45) | `my_message:get_destination()` | 97 | [`dbus_message_set_sender()`](http://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#gaa2170744c2c19217d9df02551f16bc92) | `my_message:set_sender()` | 98 | [`dbus_message_get_sender()`](http://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#ga13ce514ceb2d1598751f3a7760cf1375) | `my_message:get_sender()` | 99 | [`dbus_message_get_signature()`](http://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#gaed63e4c2baaa50d782e8ebb7643def19) | `my_message:get_signature()` | 100 | [`dbus_message_iter_has_next()`](http://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#gaaffc75a699c96ff6197287f166df2149) | `bool = my_message_iter:has_next()` | 101 | [`dbus_message_iter_next()`](http://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#ga554e9fafd4dcc84cebe9da9344846a82) | `bool = my_message_iter:next()` | 102 | [`dbus_message_iter_get_element_count()`](https://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#gabe60e5d5c3f06f90254eef3e72b5cf49) | `number = my_message_iter:get_element_count()` | 103 | [`dbus_message_iter_get_element_type()`](http://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#ga868a7aeddb9b54b2805776b512f68cb4) | `type = my_message_iter:get_element_type()` | `DBUS_TYPE_INVALID` is returned as `nil` 104 | [`dbus_message_iter_recurse()`](http://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#ga7652e1208743da5dd4ecc5aef07bf207) | `sub_iter = my_message_iter:recurse([sub_iter])` | Creates a new `DBusMessageIter` if one is not passed 105 | [`dbus_message_iter_get_signature()`](http://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#gab4579a88a1a7eaf648350466f585ef8b) | `sig = my_message_iter:get_signature()` | 106 | [`dbus_message_iter_get_basic()`](http://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#ga41c23a05e552d0574d0444d4693d18ab) | `object = my_message_iter:get_basic()` | number types are all returned as lua numbers (even 64bit integers at the moment; so watch out); strings, object paths and signatures will be returned as strings 107 | [`dbus_message_iter_append_basic()`](http://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#ga17491f3b75b3203f6fc47dcc2e3b221b) | `my_message_iter:append_basic(object [, type])` | 108 | [`dbus_message_iter_open_container()`](http://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#ga943150f4e87fd8507da224d22c266100) | `sub_iter = my_message_iter:open_container(type [, sign [, sub_iter]])` | Creates `sub_iter` if not passed. Returns the `sub_iter` or `nil` on failure 109 | [`dbus_message_iter_close_container()`](http://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#gaf00482f63d4af88b7851621d1f24087a) | `ok = my_message_iter:close_container(sub_iter)` | 110 | [`dbus_pending_call_set_notify()`](http://dbus.freedesktop.org/doc/api/html/group__DBusPendingCall.html#ga16b67b418b1dc27cfdda6b20f7447670) | `my_pending_call:set_notify(callback)` | Callback gets no arguments 111 | [`dbus_pending_call_cancel()`](http://dbus.freedesktop.org/doc/api/html/group__DBusPendingCall.html#ga6530d18f891d3ca5f5df87ea7c2b155c) | `my_pending_call:cancel()` | 112 | [`dbus_pending_call_get_completed()`](http://dbus.freedesktop.org/doc/api/html/group__DBusPendingCall.html#gacbf33ae8a1cc125628f9ea44d175c159) | `completed = my_pending_call:get_completed()` | 113 | [`dbus_pending_call_steal_reply()`](http://dbus.freedesktop.org/doc/api/html/group__DBusPendingCall.html#ga5a738928c2369fef093ce00658903d06) | `message = my_pending_call:steal_reply()` | 114 | [`dbus_pending_call_block()`](http://dbus.freedesktop.org/doc/api/html/group__DBusPendingCall.html#ga67b99f749a7f477c7b5d70f2acee5452) | `my_pending_call:block()` | 115 | [`dbus_timeout_get_interval()`](http://dbus.freedesktop.org/doc/api/html/group__DBusTimeout.html#ga67ba21b6189438875c6007ee79da5e37) | `secs = my_timeout:get_interval()` | Returned number is in seconds (rather than milliseconds) 116 | [`dbus_timeout_handle()`](http://dbus.freedesktop.org/doc/api/html/group__DBusTimeout.html#ga038b67c8d3db2624a1e4a8bc45f25d12) | `ok = my_timeout:handle()` | 117 | [`dbus_timeout_get_enabled()`](http://dbus.freedesktop.org/doc/api/html/group__DBusTimeout.html#ga58954b2edb45ec1632529d35525ea45c) | `enabled = my_timeout:get_enabled()` | 118 | [`DBUS_WATCH_READABLE`](http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html#ga0556779e61aeb19eb9cf6b6466bd1b98) | `ldbus.watch.READABLE` | 119 | [`DBUS_WATCH_WRITABLE`](http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html#ga0556779e61aeb19eb9cf6b6466bd1b98) | `ldbus.watch.WRITABLE` | 120 | [`DBUS_WATCH_HANGUP`](http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html#ga0556779e61aeb19eb9cf6b6466bd1b98) | `ldbus.watch.HANGUP` | 121 | [`DBUS_WATCH_ERROR`](http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html#ga0556779e61aeb19eb9cf6b6466bd1b98) | `ldbus.watch.ERROR` | 122 | [`dbus_watch_get_unix_fd()`](http://dbus.freedesktop.org/doc/api/html/group__DBusWatch.html#ga15df7f6120ead3e09bec8a70d3c43c0d) | `fd = my_watch:get_unix_fd()` | Returns `nil` when no fd is available 123 | [`dbus_watch_get_socket()`](http://dbus.freedesktop.org/doc/api/html/group__DBusWatch.html#ga91308f393d41b31babda17c83833517f) | `fd = my_watch:get_socket()` | Returns `nil` when no fd is available 124 | [`dbus_watch_get_flags()`](http://dbus.freedesktop.org/doc/api/html/group__DBusWatch.html#gaf172a2b1d1f82333e67cec8d99c9204a) | `flags = my_watch:get_flags()` | `flags` will be the a number 125 | [`dbus_watch_handle()`](http://dbus.freedesktop.org/doc/api/html/group__DBusWatch.html#gac2acdb1794450ac01a43ec4c3e07ebf7) | `ok = my_watch:handle(flags)` | 126 | [`dbus_watch_get_enabled()`](http://dbus.freedesktop.org/doc/api/html/group__DBusWatch.html#gae7a91e6d4d1bc187419c47c522e33a8f) | `enabled = my_watch:get_enabled()` | 127 | 128 | 129 | ### Other 130 | 131 | #### `ldbus.types` 132 | 133 | A table of the dbus types from name to type (as a character) 134 | 135 | key | value 136 | ----------------|------- 137 | `"boolean"` | `"b"` 138 | `"byte"` | `"y"` 139 | `"double"` | `"d"` 140 | `"int16"` | `"n"` 141 | `"int32"` | `"i"` 142 | `"int64"` | `"x"` 143 | `"uint16"` | `"q"` 144 | `"uint32"` | `"u"` 145 | `"uint64"` | `"t"` 146 | `"string"` | `"s"` 147 | `"signature"` | `"g"` 148 | `"object_path"` | `"o"` 149 | `"array"` | `"a"` 150 | `"dict_entry"` | `"e"` 151 | `"variant"` | `"v"` 152 | `"struct"` | `"r"` 153 | 154 | 155 | #### `ldbus.basic_types` 156 | 157 | A table of the dbus basic types from name to type (as a character) 158 | 159 | key | value 160 | ----------------|------- 161 | `"boolean"` | `"b"` 162 | `"byte"` | `"y"` 163 | `"double"` | `"d"` 164 | `"int16"` | `"n"` 165 | `"int32"` | `"i"` 166 | `"int64"` | `"x"` 167 | `"uint16"` | `"q"` 168 | `"uint32"` | `"u"` 169 | `"uint64"` | `"t"` 170 | `"string"` | `"s"` 171 | `"signature"` | `"g"` 172 | `"object_path"` | `"o"` 173 | 174 | 175 | #### `my_iter = ldbus.message.iter.new()` 176 | 177 | Creates a new `DBusMessageIter` object 178 | 179 | 180 | #### `new_iter = my_iter:clone()` 181 | 182 | Creates a copy of the given `DBusMessageIter` 183 | 184 | --------------------------------------------------------------------------------