From ec08a507693e5833c86349896c37bdc01535dc8c Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Tue, 12 Nov 2019 11:25:41 -0800 Subject: [PATCH] Eliminate function_data_t This struct is now mostly useless and can go. --- src/builtin_function.cpp | 17 ++++++++--------- src/fish_tests.cpp | 10 +++------- src/function.cpp | 16 ++++++---------- src/function.h | 17 ++--------------- 4 files changed, 19 insertions(+), 41 deletions(-) diff --git a/src/builtin_function.cpp b/src/builtin_function.cpp index bec57530e..7c4dec37b 100644 --- a/src/builtin_function.cpp +++ b/src/builtin_function.cpp @@ -256,6 +256,8 @@ int builtin_function(parser_t &parser, io_streams_t &streams, const wcstring_lis auto props = std::make_shared(); props->shadow_scope = opts.shadow_scope; props->named_arguments = std::move(opts.named_arguments); + props->parsed_source = source; + props->body_node = body; // Populate inherit_vars. for (const wcstring &name : opts.inherit_vars) { @@ -264,16 +266,13 @@ int builtin_function(parser_t &parser, io_streams_t &streams, const wcstring_lis } } - props->parsed_source = source; - props->body_node = body; + // Add the function itself. + function_add(function_name, opts.description, props, parser.libdata().current_filename); - function_data_t d; - d.name = function_name; - d.description = opts.description; - d.props = props; - d.events = std::move(opts.events); - - function_add(std::move(d), parser.libdata().current_filename); + // Add any event handlers. + for (const event_description_t &ed : opts.events) { + event_add_handler(std::make_shared(ed, function_name)); + } // Handle wrap targets by creating the appropriate completions. for (const wcstring &wt : opts.wrap_targets) complete_add_wrapper(function_name, wt); diff --git a/src/fish_tests.cpp b/src/fish_tests.cpp index 3704d4c60..2d34e6eac 100644 --- a/src/fish_tests.cpp +++ b/src/fish_tests.cpp @@ -2712,11 +2712,9 @@ static void test_complete() { do_test(newcmdline == LR"(touch test/complete_test/gnarlybracket\\\[abc\] )"); // Add a function and test completing it in various ways. - // Note we're depending on function_data_t not complaining when given missing parsed_source / + // Note we're depending on function_add not complaining when given missing parsed_source / // body_node. - struct function_data_t func_data = {}; - func_data.name = L"scuttlebutt"; - function_add(func_data, nullptr); + function_add(L"scuttlebutt", {}, nullptr, {}); // Complete a function name. completions.clear(); @@ -2828,9 +2826,7 @@ static void test_complete() { // Test abbreviations. auto &pvars = parser_t::principal_parser().vars(); - function_data_t fd; - fd.name = L"testabbrsonetwothreefour"; - function_add(fd, nullptr); + function_add(L"testabbrsonetwothreefour", {}, nullptr, {}); int ret = pvars.set_one(L"_fish_abbr_testabbrsonetwothreezero", ENV_LOCAL, L"expansion"); complete(L"testabbrsonetwothree", &completions, {}, pvars, parser); do_test(ret == 0); diff --git a/src/function.cpp b/src/function.cpp index 6bb5a2537..62b8d75ec 100644 --- a/src/function.cpp +++ b/src/function.cpp @@ -148,31 +148,27 @@ function_info_t::function_info_t(function_properties_ref_t props, wcstring desc, definition_file(intern(def_file)), is_autoload(autoload) {} -void function_add(const function_data_t &data, const wchar_t *filename) { +void function_add(wcstring name, wcstring description, function_properties_ref_t props, + const wchar_t *filename) { ASSERT_IS_MAIN_THREAD(); auto funcset = function_set.acquire(); // Historical check. TODO: rationalize this. - if (data.name.empty()) { + if (name.empty()) { return; } // Remove the old function. - funcset->remove(data.name); + funcset->remove(name); // Check if this is a function that we are autoloading. - bool is_autoload = funcset->autoloader.autoload_in_progress(data.name); + bool is_autoload = funcset->autoloader.autoload_in_progress(name); // Create and store a new function. auto ins = funcset->funcs.emplace( - data.name, function_info_t(data.props, data.description, filename, is_autoload)); + std::move(name), function_info_t(props, std::move(description), filename, is_autoload)); assert(ins.second && "Function should not already be present in the table"); (void)ins; - - // Add event handlers. - for (const event_description_t &ed : data.events) { - event_add_handler(std::make_shared(ed, data.name)); - } } std::shared_ptr function_get_properties(const wcstring &name) { diff --git a/src/function.h b/src/function.h index c897339aa..a5eaaac2d 100644 --- a/src/function.h +++ b/src/function.h @@ -36,22 +36,9 @@ struct function_properties_t { using function_properties_ref_t = std::shared_ptr; -/// Structure describing a function. This is used by the parser to store data on a function while -/// parsing it. It is not used internally to store functions, the function_info_t structure -/// is used for that purpose. Parhaps this should be merged with function_info_t. -struct function_data_t { - /// Name of function. - wcstring name; - /// Description of function. - wcstring description; - /// Function's metadata fields - function_properties_ref_t props; - /// List of all event handlers for this function. - std::vector events; -}; - /// Add a function. -void function_add(const function_data_t &data, const wchar_t *filename); +void function_add(wcstring name, wcstring description, function_properties_ref_t props, + const wchar_t *filename); /// Remove the function with the specified name. void function_remove(const wcstring &name);