Merge PR #1354 'Fixes to channel.c, input.c and helpers.lua'

This commit is contained in:
Thiago de Arruda
2014-10-28 11:04:55 -03:00
4 changed files with 73 additions and 56 deletions

View File

@@ -338,6 +338,8 @@ static void parse_msgpack(RStream *rstream, void *data, bool eof)
if (eof) { if (eof) {
close_channel(channel); close_channel(channel);
call_set_error(channel, "Channel was closed by the client");
return;
} }
size_t count = rstream_pending(rstream); size_t count = rstream_pending(rstream);
@@ -730,7 +732,7 @@ static WBuffer *serialize_response(uint64_t channel_id,
} }
#if MIN_LOG_LEVEL <= DEBUG_LOG_LEVEL #if MIN_LOG_LEVEL <= DEBUG_LOG_LEVEL
#define REQ "[response] " #define REQ "[request] "
#define RES "[response] " #define RES "[response] "
#define NOT "[notification] " #define NOT "[notification] "

View File

@@ -1,3 +1,4 @@
#include <assert.h>
#include <string.h> #include <string.h>
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
@@ -237,18 +238,23 @@ static void convert_input(void)
if (convert) { if (convert) {
// Perform input conversion according to `input_conv` // Perform input conversion according to `input_conv`
size_t unconverted_length; size_t unconverted_length = 0;
data = (char *)string_convert_ext(&input_conv, data = (char *)string_convert_ext(&input_conv,
(uint8_t *)data, (uint8_t *)data,
(int *)&converted_length, (int *)&converted_length,
(int *)&unconverted_length); (int *)&unconverted_length);
data_length = rbuffer_pending(read_buffer) - unconverted_length; data_length -= unconverted_length;
} }
// Write processed data to input buffer // The conversion code will be gone eventually, for now assume `input_buffer`
size_t consumed = rbuffer_write(input_buffer, data, data_length); // always has space for the converted data(it's many times the size of
// `read_buffer`, so it's hard to imagine a scenario where the converted data
// doesn't fit)
assert(converted_length <= rbuffer_available(input_buffer));
// Write processed data to input buffer.
(void)rbuffer_write(input_buffer, data, converted_length);
// Adjust raw buffer pointers // Adjust raw buffer pointers
rbuffer_consumed(read_buffer, consumed); rbuffer_consumed(read_buffer, data_length);
if (convert) { if (convert) {
// data points to memory allocated by `string_convert_ext`, free it. // data points to memory allocated by `string_convert_ext`, free it.

View File

@@ -396,6 +396,7 @@ static void close_cb(uv_handle_t *handle)
static void rbuffer_relocate(RBuffer *rbuffer) static void rbuffer_relocate(RBuffer *rbuffer)
{ {
assert(rbuffer->rpos <= rbuffer->wpos);
// Move data ... // Move data ...
memmove( memmove(
rbuffer->data, // ...to the beginning of the buffer(rpos 0) rbuffer->data, // ...to the beginning of the buffer(rpos 0)

View File

@@ -25,19 +25,63 @@ end
local session local session
local rawfeed
local function restart() local function restart()
local loop = Loop.new() local loop = Loop.new()
local msgpack_stream = MsgpackStream.new(loop) local msgpack_stream = MsgpackStream.new(loop)
local async_session = AsyncSession.new(msgpack_stream) local async_session = AsyncSession.new(msgpack_stream)
session = Session.new(async_session) session = Session.new(async_session)
loop:spawn(nvim_argv) loop:spawn(nvim_argv)
rawfeed([[:function BeforeEachTest()
set all&
redir => groups
silent augroup
redir END
for group in split(groups)
exe 'augroup '.group
autocmd!
augroup END
endfor
autocmd!
tabnew
let curbufnum = eval(bufnr('%'))
redir => buflist
silent ls!
redir END
let bufnums = []
for buf in split(buflist, '\n')
let bufnum = eval(split(buf, '[ u]')[0])
if bufnum != curbufnum
call add(bufnums, bufnum)
endif
endfor
if len(bufnums) > 0
exe 'silent bwipeout! '.join(bufnums, ' ')
endif
silent tabonly
for k in keys(g:)
exe 'unlet g:'.k
endfor
filetype plugin indent off
mapclear
mapclear!
abclear
comclear
endfunction
]])
end end
restart()
local loop_running, last_error
local function request(method, ...) local function request(method, ...)
local status, rv = session:request(method, ...) local status, rv = session:request(method, ...)
if not status then if not status then
error(rv[2]) if loop_running then
last_error = rv[2]
session:stop()
else
error(rv[2])
end
end end
return rv return rv
end end
@@ -47,7 +91,14 @@ local function next_message()
end end
local function run(request_cb, notification_cb, setup_cb) local function run(request_cb, notification_cb, setup_cb)
loop_running = true
session:run(request_cb, notification_cb, setup_cb) session:run(request_cb, notification_cb, setup_cb)
loop_running = false
if last_error then
local err = last_error
last_error = nil
error(err)
end
end end
local function stop() local function stop()
@@ -115,7 +166,7 @@ local function feed(...)
end end
end end
local function rawfeed(...) function rawfeed(...)
for _, v in ipairs({...}) do for _, v in ipairs({...}) do
nvim_feed(dedent(v), 'nt') nvim_feed(dedent(v), 'nt')
end end
@@ -138,14 +189,6 @@ local function execute(...)
end end
end end
local function eval(expr)
local status, result = pcall(function() return nvim_eval(expr) end)
if not status then
error('Failed to evaluate expression "' .. expr .. '"')
end
return result
end
local function eq(expected, actual) local function eq(expected, actual)
return assert.are.same(expected, actual) return assert.are.same(expected, actual)
end end
@@ -158,44 +201,6 @@ local function expect(contents, first, last, buffer_index)
return eq(dedent(contents), buffer_slice(first, last, buffer_index)) return eq(dedent(contents), buffer_slice(first, last, buffer_index))
end end
rawfeed([[:function BeforeEachTest()
set all&
redir => groups
silent augroup
redir END
for group in split(groups)
exe 'augroup '.group
autocmd!
augroup END
endfor
autocmd!
tabnew
let curbufnum = eval(bufnr('%'))
redir => buflist
silent ls!
redir END
let bufnums = []
for buf in split(buflist, '\n')
let bufnum = eval(split(buf, '[ u]')[0])
if bufnum != curbufnum
call add(bufnums, bufnum)
endif
endfor
if len(bufnums) > 0
exe 'silent bwipeout! '.join(bufnums, ' ')
endif
silent tabonly
for k in keys(g:)
exe 'unlet g:'.k
endfor
filetype plugin indent off
mapclear
mapclear!
abclear
comclear
endfunction
]])
local function ok(expr) local function ok(expr)
assert.is_true(expr) assert.is_true(expr)
@@ -245,6 +250,8 @@ local function curtab(method, ...)
return tabpage(method, tab, ...) return tabpage(method, tab, ...)
end end
restart()
return { return {
clear = clear, clear = clear,
restart = restart, restart = restart,
@@ -252,7 +259,8 @@ return {
insert = insert, insert = insert,
feed = feed, feed = feed,
execute = execute, execute = execute,
eval = eval, eval = nvim_eval,
command = nvim_command,
request = request, request = request,
next_message = next_message, next_message = next_message,
run = run, run = run,