Esempio n. 1
0
static int lua_user_print(lua_State *L)
{
	int i;
	int nargs = lua_gettop(L);
	struct luadebug_user *user;

	assert(lua_islightuserdata(L, 1));
	user = (struct luadebug_user *)lua_topointer(L, 1);
	assert(user);

	for (i=2; i<=nargs; i++) {
		if (i > 2)
			user->print(user, " ");

		user->print(user, "%s", lua_converttostring(L, i, NULL));
		lua_pop(L, 1);
	}

	user->print(user, "\n");

	return 0;
}
Esempio n. 2
0
static json_t *lua_element_to_json(struct lua_State *L, int index)
{
	json_t *ret = NULL;
	const int val_type = lua_type(L, index);
	const int h = lua_gettop(L);

	lua_pushvalue(L, index);

	switch (val_type) {
	case LUA_TBOOLEAN: {
		const int int_val = lua_toboolean(L, -1);
		ret = int_val ? json_true() : json_false();
		if (!ret) error("json boolean conversion error");
		break;
	}
	case LUA_TSTRING:
	case LUA_TUSERDATA: {
		const char *str_val = lua_converttostring(L, -1, NULL);
		if (!str_val) {
			error("cannot convert value to string");
			break;
		}

		ret = json_string(str_val);
		if (!ret) error("json string conversion error");
		break;
	}
	case LUA_TNUMBER: {
		lua_Number num_val = lua_tonumber(L, -1);
		ret = json_real(num_val);
		if (!ret) error("json number conversion error");
		break;
	}
	case LUA_TTABLE: {
		ret = json_object();
		if (!ret) {
			error("json table conversion error");
			break;
		}

		lua_pushnil(L);
		while (lua_next(L, -2) != 0) {
			size_t len;
			const char *str_val = lua_tolstring(L, -2, &len);
			json_t *val = lua_element_to_json(L, -1);
			if (!val) {
				json_decref(ret);
				ret = NULL;
				break;
			}

			if (json_object_set(ret, str_val, val) != 0) {
				error("json table conversion error");
				json_decref(val);
				json_decref(ret);
				ret = NULL;
				break;
			}

			lua_pop(L, 1);
		}

		break;
	}
	case LUA_TFUNCTION: {
		error("function cannot be converted to json");
		break;
	}
	case LUA_TNIL: {
		ret = json_null();
		if (!ret) error("json nil conversion error");
		break;
	}
	default:
		error("invalid value type (%s)", lua_typename(L, val_type));
	}

	lua_settop(L, h);
	return ret;
}