struct channel *chn;
struct filter *filter;
size_t input, output;
- int offset, len;
+ int offset, len = 0;
chn = MAY_LJMP(hlua_checkchannel(L, 1));
if (offset < 0)
offset = MAX(0, (int)input + offset);
offset += output;
- if (offset < output || offset > input + output) {
+ if (offset < output) {
lua_pushfstring(L, "offset out of range.");
WILL_LJMP(lua_error(L));
}
}
+ if (offset >= output+input)
+ goto wait_more_data;
+
len = output + input - offset;
if (lua_gettop(L) == 3) {
len = MAY_LJMP(luaL_checkinteger(L, 3));
* is no data or not enough data was received.
*/
if (!len || offset + len > output + input) {
+ wait_more_data:
if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) {
/* Yield waiting for more data, as requested */
MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_data_yield, TICK_ETERNITY, 0));
}
- /* Return 'nil' if there is no data and the channel can't receive more data */
- if (!len) {
+ if (!input) {
+ /* Return 'nil' if there is no data and the channel can't receive more data */
lua_pushnil(L);
return -1;
}
+ if (!len) {
+ lua_pushstring(L, "");
+ return 1;
+ }
+
/* Otherwise, return all data */
len = output + input - offset;
}
struct channel *chn;
struct filter *filter;
size_t l, input, output;
- int offset, len;
+ int offset, len = 0;
chn = MAY_LJMP(hlua_checkchannel(L, 1));
output = co_data(chn);
if (offset < 0)
offset = MAX(0, (int)input + offset);
offset += output;
- if (offset < output || offset > input + output) {
+ if (offset < output) {
lua_pushfstring(L, "offset out of range.");
WILL_LJMP(lua_error(L));
}
}
+ if (offset > output + input)
+ goto wait_more_data;
len = output + input - offset;
if (lua_gettop(L) == 3) {
* specified or not enough data was received.
*/
if (lua_gettop(L) != 3 || offset + len > output + input) {
+ wait_more_data:
if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) {
/* Yield waiting for more data */
MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_line_yield, TICK_ETERNITY, 0));
}
- /* Return 'nil' if there is no data and the channel can't receive more data */
- if (!len) {
+ if (!input) {
+ /* Return 'nil' if there is no data and the channel can't receive more data */
lua_pushnil(L);
return -1;
}
+ if (!len) {
+ lua_pushstring(L, "");
+ return 1;
+ }
+
/* Otherwise, return all data */
len = output + input - offset;
}