mirror of
https://github.com/fish-shell/fish-shell
synced 2025-01-13 13:39:02 +00:00
Migrate function getters to use function_get_properties
This replaces some of the teensy function getters with the function that just returns a shared_ptr to the properties struct.
This commit is contained in:
parent
db33ed0fc7
commit
976514597d
4 changed files with 46 additions and 52 deletions
|
@ -134,12 +134,13 @@ static wcstring functions_def(const wcstring &name) {
|
||||||
out.append(esc_desc);
|
out.append(esc_desc);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!function_get_shadow_scope(name)) {
|
auto props = function_get_properties(name);
|
||||||
|
assert(props && "Should have function properties");
|
||||||
|
if (!props->shadow_scope) {
|
||||||
out.append(L" --no-scope-shadowing");
|
out.append(L" --no-scope-shadowing");
|
||||||
}
|
}
|
||||||
|
|
||||||
for (size_t i = 0; i < ev.size(); i++) {
|
for (const auto &next : ev) {
|
||||||
const event_t *next = ev.at(i).get();
|
|
||||||
switch (next->type) {
|
switch (next->type) {
|
||||||
case EVENT_SIGNAL: {
|
case EVENT_SIGNAL: {
|
||||||
append_format(out, L" --on-signal %ls", sig2wcs(next->param1.signal));
|
append_format(out, L" --on-signal %ls", sig2wcs(next->param1.signal));
|
||||||
|
@ -172,11 +173,11 @@ static wcstring functions_def(const wcstring &name) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
wcstring_list_t named = function_get_named_arguments(name);
|
const wcstring_list_t &named = props->named_arguments;
|
||||||
if (!named.empty()) {
|
if (!named.empty()) {
|
||||||
append_format(out, L" --argument");
|
append_format(out, L" --argument");
|
||||||
for (size_t i = 0; i < named.size(); i++) {
|
for (const auto &name : named) {
|
||||||
append_format(out, L" %ls", named.at(i).c_str());
|
append_format(out, L" %ls", name.c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -188,17 +189,14 @@ static wcstring functions_def(const wcstring &name) {
|
||||||
|
|
||||||
// Output any inherited variables as `set -l` lines.
|
// Output any inherited variables as `set -l` lines.
|
||||||
std::map<wcstring, env_var_t> inherit_vars = function_get_inherit_vars(name);
|
std::map<wcstring, env_var_t> inherit_vars = function_get_inherit_vars(name);
|
||||||
for (std::map<wcstring, env_var_t>::const_iterator it = inherit_vars.begin(),
|
for (const auto &kv : inherit_vars) {
|
||||||
end = inherit_vars.end();
|
|
||||||
it != end; ++it) {
|
|
||||||
wcstring_list_t lst;
|
wcstring_list_t lst;
|
||||||
it->second.to_list(lst);
|
kv.second.to_list(lst);
|
||||||
|
|
||||||
// This forced tab is crummy, but we don't know what indentation style the function uses.
|
// This forced tab is crummy, but we don't know what indentation style the function uses.
|
||||||
append_format(out, L"\n\tset -l %ls", it->first.c_str());
|
append_format(out, L"\n\tset -l %ls", kv.first.c_str());
|
||||||
for (wcstring_list_t::const_iterator arg_it = lst.begin(), arg_end = lst.end();
|
for (const auto &arg : lst) {
|
||||||
arg_it != arg_end; ++arg_it) {
|
wcstring earg = escape_string(arg, ESCAPE_ALL);
|
||||||
wcstring earg = escape_string(*arg_it, ESCAPE_ALL);
|
|
||||||
out.push_back(L' ');
|
out.push_back(L' ');
|
||||||
out.append(earg);
|
out.append(earg);
|
||||||
}
|
}
|
||||||
|
@ -224,6 +222,7 @@ static int report_function_metadata(const wchar_t *funcname, bool verbose, io_st
|
||||||
int line_number = 0;
|
int line_number = 0;
|
||||||
|
|
||||||
if (function_exists(funcname)) {
|
if (function_exists(funcname)) {
|
||||||
|
auto props = function_get_properties(funcname);
|
||||||
path = function_get_definition_file(funcname);
|
path = function_get_definition_file(funcname);
|
||||||
if (path) {
|
if (path) {
|
||||||
autoloaded = function_is_autoloaded(funcname) ? L"autoloaded" : L"not-autoloaded";
|
autoloaded = function_is_autoloaded(funcname) ? L"autoloaded" : L"not-autoloaded";
|
||||||
|
@ -231,8 +230,7 @@ static int report_function_metadata(const wchar_t *funcname, bool verbose, io_st
|
||||||
} else {
|
} else {
|
||||||
path = L"stdin";
|
path = L"stdin";
|
||||||
}
|
}
|
||||||
shadows_scope =
|
shadows_scope = props->shadow_scope ? L"scope-shadowing" : L"no-scope-shadowing";
|
||||||
function_get_shadow_scope(funcname) ? L"scope-shadowing" : L"no-scope-shadowing";
|
|
||||||
function_get_desc(funcname, &description);
|
function_get_desc(funcname, &description);
|
||||||
description = escape_string(description, ESCAPE_NO_QUOTED);
|
description = escape_string(description, ESCAPE_NO_QUOTED);
|
||||||
}
|
}
|
||||||
|
|
16
src/exec.cpp
16
src/exec.cpp
|
@ -792,19 +792,19 @@ void exec_job(parser_t &parser, job_t *j) {
|
||||||
switch (p->type) {
|
switch (p->type) {
|
||||||
case INTERNAL_FUNCTION: {
|
case INTERNAL_FUNCTION: {
|
||||||
const wcstring func_name = p->argv0();
|
const wcstring func_name = p->argv0();
|
||||||
wcstring def;
|
auto props = function_get_properties(func_name);
|
||||||
bool function_exists = function_get_definition(func_name, &def);
|
if (!props) {
|
||||||
bool shadow_scope = function_get_shadow_scope(func_name);
|
|
||||||
const std::map<wcstring, env_var_t> inherit_vars =
|
|
||||||
function_get_inherit_vars(func_name);
|
|
||||||
|
|
||||||
if (!function_exists) {
|
|
||||||
debug(0, _(L"Unknown function '%ls'"), p->argv0());
|
debug(0, _(L"Unknown function '%ls'"), p->argv0());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
wcstring def;
|
||||||
|
function_get_definition(func_name, &def);
|
||||||
|
const std::map<wcstring, env_var_t> inherit_vars =
|
||||||
|
function_get_inherit_vars(func_name);
|
||||||
|
|
||||||
function_block_t *fb =
|
function_block_t *fb =
|
||||||
parser.push_block<function_block_t>(p, func_name, shadow_scope);
|
parser.push_block<function_block_t>(p, func_name, props->shadow_scope);
|
||||||
function_prepare_environment(func_name, p->get_argv() + 1, inherit_vars);
|
function_prepare_environment(func_name, p->get_argv() + 1, inherit_vars);
|
||||||
parser.forbid_function(func_name);
|
parser.forbid_function(func_name);
|
||||||
|
|
||||||
|
|
|
@ -177,6 +177,16 @@ void function_add(const function_data_t &data, const parser_t &parser) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<const function_properties_t> function_get_properties(const wcstring &name) {
|
||||||
|
if (parser_keywords_is_reserved(name)) return nullptr;
|
||||||
|
scoped_rlock locker(functions_lock);
|
||||||
|
auto where = loaded_functions.find(name);
|
||||||
|
if (where != loaded_functions.end()) {
|
||||||
|
return where->second.props;
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
int function_exists(const wcstring &cmd) {
|
int function_exists(const wcstring &cmd) {
|
||||||
if (parser_keywords_is_reserved(cmd)) return 0;
|
if (parser_keywords_is_reserved(cmd)) return 0;
|
||||||
scoped_rlock locker(functions_lock);
|
scoped_rlock locker(functions_lock);
|
||||||
|
@ -242,24 +252,12 @@ bool function_get_definition(const wcstring &name, wcstring *out_definition) {
|
||||||
return func != NULL;
|
return func != NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
wcstring_list_t function_get_named_arguments(const wcstring &name) {
|
|
||||||
scoped_rlock locker(functions_lock);
|
|
||||||
const function_info_t *func = function_get(name);
|
|
||||||
return func ? func->props->named_arguments : wcstring_list_t();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::map<wcstring, env_var_t> function_get_inherit_vars(const wcstring &name) {
|
std::map<wcstring, env_var_t> function_get_inherit_vars(const wcstring &name) {
|
||||||
scoped_rlock locker(functions_lock);
|
scoped_rlock locker(functions_lock);
|
||||||
const function_info_t *func = function_get(name);
|
const function_info_t *func = function_get(name);
|
||||||
return func ? func->inherit_vars : std::map<wcstring, env_var_t>();
|
return func ? func->inherit_vars : std::map<wcstring, env_var_t>();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool function_get_shadow_scope(const wcstring &name) {
|
|
||||||
scoped_rlock locker(functions_lock);
|
|
||||||
const function_info_t *func = function_get(name);
|
|
||||||
return func ? func->props->shadow_scope : false;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool function_get_desc(const wcstring &name, wcstring *out_desc) {
|
bool function_get_desc(const wcstring &name, wcstring *out_desc) {
|
||||||
// Empty length string goes to NULL.
|
// Empty length string goes to NULL.
|
||||||
scoped_rlock locker(functions_lock);
|
scoped_rlock locker(functions_lock);
|
||||||
|
@ -348,21 +346,20 @@ int function_get_definition_lineno(const wcstring &name) {
|
||||||
void function_prepare_environment(const wcstring &name, const wchar_t *const *argv,
|
void function_prepare_environment(const wcstring &name, const wchar_t *const *argv,
|
||||||
const std::map<wcstring, env_var_t> &inherited_vars) {
|
const std::map<wcstring, env_var_t> &inherited_vars) {
|
||||||
env_set_argv(argv);
|
env_set_argv(argv);
|
||||||
|
auto props = function_get_properties(name);
|
||||||
const wcstring_list_t named_arguments = function_get_named_arguments(name);
|
if (props && !props->named_arguments.empty()) {
|
||||||
if (!named_arguments.empty()) {
|
|
||||||
const wchar_t *const *arg = argv;
|
const wchar_t *const *arg = argv;
|
||||||
for (size_t i = 0; i < named_arguments.size(); i++) {
|
for (const wcstring &named_arg : props->named_arguments) {
|
||||||
if (*arg) {
|
if (*arg) {
|
||||||
env_set_one(named_arguments.at(i), ENV_LOCAL | ENV_USER, *arg);
|
env_set_one(named_arg, ENV_LOCAL | ENV_USER, *arg);
|
||||||
arg++;
|
arg++;
|
||||||
} else {
|
} else {
|
||||||
env_set_empty(named_arguments.at(i), ENV_LOCAL | ENV_USER);
|
env_set_empty(named_arg, ENV_LOCAL | ENV_USER);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto it = inherited_vars.begin(), end = inherited_vars.end(); it != end; ++it) {
|
for (const auto &kv : inherited_vars) {
|
||||||
env_set(it->first, ENV_LOCAL | ENV_USER, it->second.as_list());
|
env_set(kv.first, ENV_LOCAL | ENV_USER, kv.second.as_list());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,18 +53,24 @@ void function_add(const function_data_t &data, const parser_t &parser);
|
||||||
/// Remove the function with the specified name.
|
/// Remove the function with the specified name.
|
||||||
void function_remove(const wcstring &name);
|
void function_remove(const wcstring &name);
|
||||||
|
|
||||||
|
/// Returns the properties for a function, or nullptr if none. This does not trigger autoloading.
|
||||||
|
std::shared_ptr<const function_properties_t> function_get_properties(const wcstring &name);
|
||||||
|
|
||||||
/// Returns by reference the definition of the function with the name \c name. Returns true if
|
/// Returns by reference the definition of the function with the name \c name. Returns true if
|
||||||
/// successful, false if no function with the given name exists.
|
/// successful, false if no function with the given name exists.
|
||||||
|
/// This does not trigger autoloading.
|
||||||
bool function_get_definition(const wcstring &name, wcstring *out_definition);
|
bool function_get_definition(const wcstring &name, wcstring *out_definition);
|
||||||
|
|
||||||
/// Returns by reference the description of the function with the name \c name. Returns true if the
|
/// Returns by reference the description of the function with the name \c name. Returns true if the
|
||||||
/// function exists and has a nonempty description, false if it does not.
|
/// function exists and has a nonempty description, false if it does not.
|
||||||
|
/// This does not trigger autoloading.
|
||||||
bool function_get_desc(const wcstring &name, wcstring *out_desc);
|
bool function_get_desc(const wcstring &name, wcstring *out_desc);
|
||||||
|
|
||||||
/// Sets the description of the function with the name \c name.
|
/// Sets the description of the function with the name \c name.
|
||||||
void function_set_desc(const wcstring &name, const wcstring &desc);
|
void function_set_desc(const wcstring &name, const wcstring &desc);
|
||||||
|
|
||||||
/// Returns true if the function with the name name exists.
|
/// Returns true if the function with the name name exists.
|
||||||
|
/// This may autoload.
|
||||||
int function_exists(const wcstring &name);
|
int function_exists(const wcstring &name);
|
||||||
|
|
||||||
/// Attempts to load a function if not yet loaded. This is used by the completion machinery.
|
/// Attempts to load a function if not yet loaded. This is used by the completion machinery.
|
||||||
|
@ -91,14 +97,9 @@ bool function_is_autoloaded(const wcstring &name);
|
||||||
const wchar_t *function_get_definition_file(const wcstring &name);
|
const wchar_t *function_get_definition_file(const wcstring &name);
|
||||||
|
|
||||||
/// Returns the linenumber where the definition of the specified function started.
|
/// Returns the linenumber where the definition of the specified function started.
|
||||||
///
|
/// This does not trigger autoloading.
|
||||||
/// This function does not autoload functions, it will only work on functions that have already been
|
|
||||||
/// defined.
|
|
||||||
int function_get_definition_lineno(const wcstring &name);
|
int function_get_definition_lineno(const wcstring &name);
|
||||||
|
|
||||||
/// Returns a list of all named arguments of the specified function.
|
|
||||||
wcstring_list_t function_get_named_arguments(const wcstring &name);
|
|
||||||
|
|
||||||
/// Returns a mapping of all variables of the specified function that were inherited from the scope
|
/// Returns a mapping of all variables of the specified function that were inherited from the scope
|
||||||
/// of the function definition to their values.
|
/// of the function definition to their values.
|
||||||
std::map<wcstring, env_var_t> function_get_inherit_vars(const wcstring &name);
|
std::map<wcstring, env_var_t> function_get_inherit_vars(const wcstring &name);
|
||||||
|
@ -107,8 +108,6 @@ std::map<wcstring, env_var_t> function_get_inherit_vars(const wcstring &name);
|
||||||
/// is successful.
|
/// is successful.
|
||||||
bool function_copy(const wcstring &name, const wcstring &new_name);
|
bool function_copy(const wcstring &name, const wcstring &new_name);
|
||||||
|
|
||||||
/// Returns whether this function shadows variables of the underlying function.
|
|
||||||
bool function_get_shadow_scope(const wcstring &name);
|
|
||||||
|
|
||||||
/// Prepares the environment for executing a function.
|
/// Prepares the environment for executing a function.
|
||||||
void function_prepare_environment(const wcstring &name, const wchar_t *const *argv,
|
void function_prepare_environment(const wcstring &name, const wchar_t *const *argv,
|
||||||
|
|
Loading…
Reference in a new issue