API: nvim_call_dict_function: eliminate internal param

The `internal` param is difficult to explain, and will rarely be
anything but `true`.  To avoid it, use a hack: check if the resolved
dict value starts with "function(".
This commit is contained in:
Justin M. Keyes 2018-05-04 08:20:37 +02:00
parent 19c2ce1901
commit fe7ab60af7
2 changed files with 45 additions and 41 deletions

View File

@ -317,7 +317,7 @@ static Object _call_function(String fn, Array args, dict_T *self, Error *err)
Object rv = OBJECT_INIT; Object rv = OBJECT_INIT;
if (args.size > MAX_FUNC_ARGS) { if (args.size > MAX_FUNC_ARGS) {
api_set_error(err, kErrorTypeValidation, api_set_error(err, kErrorTypeValidation,
"Function called with too many arguments."); "Function called with too many arguments");
return rv; return rv;
} }
@ -338,7 +338,7 @@ static Object _call_function(String fn, Array args, dict_T *self, Error *err)
vim_args, NULL, curwin->w_cursor.lnum, vim_args, NULL, curwin->w_cursor.lnum,
curwin->w_cursor.lnum, &dummy, true, NULL, self); curwin->w_cursor.lnum, &dummy, true, NULL, self);
if (r == FAIL) { if (r == FAIL) {
api_set_error(err, kErrorTypeException, "Error calling function."); api_set_error(err, kErrorTypeException, "Error calling function");
} }
if (!try_end(err)) { if (!try_end(err)) {
rv = vim_to_object(&rettv); rv = vim_to_object(&rettv);
@ -371,12 +371,10 @@ Object nvim_call_function(String fn, Array args, Error *err)
/// ///
/// @param dict Dictionary, or String evaluating to a VimL |self| dict /// @param dict Dictionary, or String evaluating to a VimL |self| dict
/// @param fn Function to call /// @param fn Function to call
/// @param internal true if the function is stored on the dict
/// @param args Functions arguments packed in an Array /// @param args Functions arguments packed in an Array
/// @param[out] err Error details, if any /// @param[out] err Error details, if any
/// @return Result of the function call /// @return Result of the function call
Object nvim_call_dict_function(Object dict, String fn, Boolean internal, Object nvim_call_dict_function(Object dict, String fn, Array args, Error *err)
Array args, Error *err)
FUNC_API_SINCE(4) FUNC_API_SINCE(4)
{ {
Object rv = OBJECT_INIT; Object rv = OBJECT_INIT;
@ -399,13 +397,8 @@ Object nvim_call_dict_function(Object dict, String fn, Boolean internal,
break; break;
} }
case kObjectTypeDictionary: { case kObjectTypeDictionary: {
if (internal) { if (!object_to_vim(dict, &rettv, err)) {
api_set_error(err, kErrorTypeValidation, goto end;
"Cannot invoke RPC dict as a VimL reference");
return rv;
} else if (!object_to_vim(dict, &rettv, err)) {
tv_clear(&rettv);
return rv;
} }
break; break;
} }
@ -421,22 +414,25 @@ Object nvim_call_dict_function(Object dict, String fn, Boolean internal,
goto end; goto end;
} }
if (internal) { if (dict.type != kObjectTypeDictionary) {
dictitem_T *const di = tv_dict_find(self_dict, fn.data, (ptrdiff_t)fn.size); dictitem_T *const di = tv_dict_find(self_dict, fn.data, (ptrdiff_t)fn.size);
if (di == NULL) { if (di != NULL) {
api_set_error(err, kErrorTypeValidation, "Function not found in dict"); if (di->di_tv.v_type != VAR_STRING) {
goto end; api_set_error(err, kErrorTypeValidation,
"Value found in dict is not a valid function");
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 (di->di_tv.v_type != VAR_STRING) {
api_set_error(err, kErrorTypeValidation,
"Value found in dict is not a valid function");
goto end;
}
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) { if (!fn.data || fn.size < 1) {
api_set_error(err, kErrorTypeValidation, "Invalid (empty) function name"); api_set_error(err, kErrorTypeValidation, "Invalid (empty) function name");
goto end; goto end;

View File

@ -180,37 +180,45 @@ describe('api', function()
end) end)
describe('nvim_call_dict_function', function() describe('nvim_call_dict_function', function()
it('invokes VimL dict', function() it('invokes VimL dict function', function()
source('function! F(name) dict\n return self.greeting . ", " . a:name . "!"\nendfunction') source([[
function! F(name) dict
return self.greeting . ", " . a:name . "!"
endfunction
]])
-- function() ("non-internal") function
nvim('set_var', 'dict_function_dict', { greeting = 'Hello', F = 'function("F")' }) nvim('set_var', 'dict_function_dict', { greeting = 'Hello', F = 'function("F")' })
eq('Hello, World!', nvim('call_dict_function', 'g:dict_function_dict', 'F', false, {'World'})) 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')) eq({ greeting = 'Hello', F = 'function("F")' }, nvim('get_var', 'dict_function_dict'))
-- "internal" function
nvim('set_var', 'dict_function_dict_i', { greeting = 'Hi', F = "F" }) nvim('set_var', 'dict_function_dict_i', { greeting = 'Hi', F = "F" })
eq('Hi, Moon!', nvim('call_dict_function', 'g:dict_function_dict_i', 'F', true, {'Moon'})) 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')) eq({ greeting = 'Hi', F = "F" }, nvim('get_var', 'dict_function_dict_i'))
end) end)
it('invokes RPC dict', function() it('invokes RPC dict', function()
source('function! G() dict\n return self.result\nendfunction') source('function! G() dict\n return self.result\nendfunction')
eq('self', nvim('call_dict_function', { result = 'self', G = 'G'}, 'G', false, {})) eq('it works', nvim('call_dict_function', { result = 'it works', G = 'G'}, 'G', {}))
end) end)
it('validates args', function() it('validates args', function()
command('let g:d={"baz":"zub","meep":[]}') command('let g:d={"baz":"zub","meep":[]}')
expect_err('Function not found in dict', request, expect_err('Error calling function', request,
'nvim_call_dict_function', 'g:d', 'bogus', true, {1,2}) 'nvim_call_dict_function', 'g:d', 'bogus', {1,2})
expect_err('Error calling function.', request, expect_err('Error calling function', request,
'nvim_call_dict_function', 'g:d', 'baz', true, {1,2}) 'nvim_call_dict_function', 'g:d', 'baz', {1,2})
expect_err('Value found in dict is not a valid function', request, expect_err('Value found in dict is not a valid function', request,
'nvim_call_dict_function', 'g:d', 'meep', true, {1,2}) 'nvim_call_dict_function', 'g:d', 'meep', {1,2})
expect_err('Cannot invoke RPC dict as a VimL reference', request, expect_err('Error calling function', request,
'nvim_call_dict_function', { f = '' }, 'f', true, {1,2}) 'nvim_call_dict_function', { f = '' }, 'f', {1,2})
expect_err('Invalid %(empty%) function name', request, expect_err('Invalid %(empty%) function name', request,
'nvim_call_dict_function', "{ 'f': '' }", 'f', true, {1,2}) 'nvim_call_dict_function', "{ 'f': '' }", 'f', {1,2})
expect_err('dict argument type must be String or Dictionary', request, expect_err('dict argument type must be String or Dictionary', request,
'nvim_call_dict_function', 42, 'f', true, {1,2}) 'nvim_call_dict_function', 42, 'f', {1,2})
expect_err('Failed to evaluate dict expression', request, expect_err('Failed to evaluate dict expression', request,
'nvim_call_dict_function', 'foo', 'f', true, {1,2}) 'nvim_call_dict_function', 'foo', 'f', {1,2})
expect_err('Referenced dict does not exist', request, expect_err('Referenced dict does not exist', request,
'nvim_call_dict_function', '42', 'f', true, {1,2}) 'nvim_call_dict_function', '42', 'f', {1,2})
end) end)
end) end)