mirror of
https://github.com/neovim/neovim.git
synced 2024-12-29 14:41:06 -07:00
API: nvim_call_dict_function: expect actual function, not name
This commit is contained in:
parent
fe7ab60af7
commit
cabffb0182
@ -370,8 +370,8 @@ Object nvim_call_function(String fn, Array args, Error *err)
|
||||
/// Calls a VimL |Dictionary-function| with the given arguments.
|
||||
///
|
||||
/// @param dict Dictionary, or String evaluating to a VimL |self| dict
|
||||
/// @param fn Function to call
|
||||
/// @param args Functions arguments packed in an Array
|
||||
/// @param fn Name of the function defined on the VimL dict
|
||||
/// @param args Function arguments packed in an Array
|
||||
/// @param[out] err Error details, if any
|
||||
/// @return Result of the function call
|
||||
Object nvim_call_dict_function(Object dict, String fn, Array args, Error *err)
|
||||
@ -410,28 +410,30 @@ Object nvim_call_dict_function(Object dict, String fn, Array args, Error *err)
|
||||
}
|
||||
dict_T *self_dict = rettv.vval.v_dict;
|
||||
if (rettv.v_type != VAR_DICT || !self_dict) {
|
||||
api_set_error(err, kErrorTypeValidation, "Referenced dict does not exist");
|
||||
api_set_error(err, kErrorTypeValidation, "dict not found");
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (dict.type != kObjectTypeDictionary) {
|
||||
if (fn.data && fn.size > 0 && dict.type != kObjectTypeDictionary) {
|
||||
dictitem_T *const di = tv_dict_find(self_dict, fn.data, (ptrdiff_t)fn.size);
|
||||
if (di != NULL) {
|
||||
if (di->di_tv.v_type != VAR_STRING) {
|
||||
api_set_error(err, kErrorTypeValidation,
|
||||
"Value found in dict is not a valid function");
|
||||
if (di == NULL) {
|
||||
api_set_error(err, kErrorTypeValidation, "Not found: %s", fn.data);
|
||||
goto end;
|
||||
}
|
||||
if (di->di_tv.v_type == VAR_PARTIAL) {
|
||||
api_set_error(err, kErrorTypeValidation,
|
||||
"partial function not supported");
|
||||
goto end;
|
||||
}
|
||||
if (di->di_tv.v_type != VAR_FUNC) {
|
||||
api_set_error(err, kErrorTypeValidation, "Not a function: %s", fn.data);
|
||||
goto end;
|
||||
}
|
||||
// XXX: Hack to guess if function is "internal".
|
||||
bool internal = (0 != STRNCMP(di->di_tv.vval.v_string, "function(", 9));
|
||||
if (internal) {
|
||||
fn = (String) {
|
||||
.data = (char *)di->di_tv.vval.v_string,
|
||||
.size = strlen((char *)di->di_tv.vval.v_string),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!fn.data || fn.size < 1) {
|
||||
api_set_error(err, kErrorTypeValidation, "Invalid (empty) function name");
|
||||
|
@ -183,42 +183,51 @@ describe('api', function()
|
||||
it('invokes VimL dict function', function()
|
||||
source([[
|
||||
function! F(name) dict
|
||||
return self.greeting . ", " . a:name . "!"
|
||||
return self.greeting.', '.a:name.'!'
|
||||
endfunction
|
||||
let g:test_dict_fn = { 'greeting':'Hello', 'F':function('F') }
|
||||
|
||||
let g:test_dict_fn2 = { 'greeting':'Hi' }
|
||||
function g:test_dict_fn2.F2(name)
|
||||
return self.greeting.', '.a:name.' ...'
|
||||
endfunction
|
||||
]])
|
||||
|
||||
-- function() ("non-internal") function
|
||||
nvim('set_var', 'dict_function_dict', { greeting = 'Hello', F = 'function("F")' })
|
||||
eq('Hello, World!', nvim('call_dict_function', 'g:dict_function_dict', 'F', {'World'}))
|
||||
eq({ greeting = 'Hello', F = 'function("F")' }, nvim('get_var', 'dict_function_dict'))
|
||||
-- :help Dictionary-function
|
||||
eq('Hello, World!', nvim('call_dict_function', 'g:test_dict_fn', 'F', {'World'}))
|
||||
-- Funcref is sent as NIL over RPC.
|
||||
eq({ greeting = 'Hello', F = NIL }, nvim('get_var', 'test_dict_fn'))
|
||||
|
||||
-- "internal" function
|
||||
nvim('set_var', 'dict_function_dict_i', { greeting = 'Hi', F = "F" })
|
||||
eq('Hi, Moon!', nvim('call_dict_function', 'g:dict_function_dict_i', 'F', {'Moon'}))
|
||||
eq({ greeting = 'Hi', F = "F" }, nvim('get_var', 'dict_function_dict_i'))
|
||||
end)
|
||||
it('invokes RPC dict', function()
|
||||
source('function! G() dict\n return self.result\nendfunction')
|
||||
eq('it works', nvim('call_dict_function', { result = 'it works', G = 'G'}, 'G', {}))
|
||||
-- :help numbered-function
|
||||
eq('Hi, Moon ...', nvim('call_dict_function', 'g:test_dict_fn2', 'F2', {'Moon'}))
|
||||
-- Funcref is sent as NIL over RPC.
|
||||
eq({ greeting = 'Hi', F2 = NIL }, nvim('get_var', 'test_dict_fn2'))
|
||||
|
||||
-- Function specified via RPC dict.
|
||||
source('function! G() dict\n return "@".(self.result)."@"\nendfunction')
|
||||
eq('@it works@', nvim('call_dict_function', { result = 'it works', G = 'G'}, 'G', {}))
|
||||
end)
|
||||
|
||||
it('validates args', function()
|
||||
command('let g:d={"baz":"zub","meep":[]}')
|
||||
expect_err('Error calling function', request,
|
||||
expect_err('Not found: bogus', request,
|
||||
'nvim_call_dict_function', 'g:d', 'bogus', {1,2})
|
||||
expect_err('Error calling function', request,
|
||||
expect_err('Not a function: baz', request,
|
||||
'nvim_call_dict_function', 'g:d', 'baz', {1,2})
|
||||
expect_err('Value found in dict is not a valid function', request,
|
||||
expect_err('Not a function: meep', request,
|
||||
'nvim_call_dict_function', 'g:d', 'meep', {1,2})
|
||||
expect_err('Error calling function', request,
|
||||
'nvim_call_dict_function', { f = '' }, 'f', {1,2})
|
||||
expect_err('Invalid %(empty%) function name', request,
|
||||
expect_err('Not a function: f', request,
|
||||
'nvim_call_dict_function', "{ 'f': '' }", 'f', {1,2})
|
||||
expect_err('dict argument type must be String or Dictionary', request,
|
||||
'nvim_call_dict_function', 42, 'f', {1,2})
|
||||
expect_err('Failed to evaluate dict expression', request,
|
||||
'nvim_call_dict_function', 'foo', 'f', {1,2})
|
||||
expect_err('Referenced dict does not exist', request,
|
||||
expect_err('dict not found', request,
|
||||
'nvim_call_dict_function', '42', 'f', {1,2})
|
||||
expect_err('Invalid %(empty%) function name', request,
|
||||
'nvim_call_dict_function', "{ 'f': '' }", '', {1,2})
|
||||
end)
|
||||
end)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user