-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add functionInfo builtin #8961
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Add functionInfo builtin #8961
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2790,6 +2790,52 @@ static RegisterPrimOp primop_functionArgs({ | |
| .fun = prim_functionArgs, | ||
| }); | ||
|
|
||
| static void prim_functionInfo(EvalState & state, const PosIdx pos, Value * * args, Value & v) | ||
| { | ||
| state.forceValue(*args[0], pos); | ||
| if (args[0]->isPrimOpApp() || args[0]->isPrimOp()) { | ||
| v.mkAttrs(&state.emptyBindings); | ||
| return; | ||
| } | ||
| if (!args[0]->isLambda()) | ||
| state.debugThrowLastTrace(TypeError({ | ||
| .msg = hintfmt("'functionArgs' requires a function"), | ||
| .errPos = state.positions[pos] | ||
| })); | ||
|
|
||
| if (!args[0]->lambda.fun->hasFormals()) { | ||
| v.mkAttrs(&state.emptyBindings); | ||
| return; | ||
| } | ||
|
|
||
| auto formals = state.buildBindings(args[0]->lambda.fun->formals->formals.size()); | ||
| for (auto & i : args[0]->lambda.fun->formals->formals) | ||
| // !!! should optimise booleans (allocate only once) | ||
| formals.alloc(i.name, i.pos).mkBool(i.def); | ||
| auto attrs = state.buildBindings(3); | ||
| if(args[0]->lambda.fun->arg) attrs.alloc("arg").mkString(state.symbols[args[0]->lambda.fun->arg]); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe we should not expose this information. It is not needed, and it will introduce unnecessary coupling that users will have to be aware of when renaming what is otherwise a completely internal name. |
||
| attrs.alloc("ellipsis").mkBool(args[0]->lambda.fun->formals->ellipsis); | ||
| attrs.alloc("formals").mkAttrs(formals); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe formals is an unfortunate name, and it has so far only been part of documentation, and not the language itself. This would be the last chance to fix that mistake. |
||
| v.mkAttrs(attrs); | ||
| } | ||
|
|
||
| static RegisterPrimOp primop_functionInfo({ | ||
| .name = "__functionInfo", | ||
| .args = {"f"}, | ||
| .doc = R"( | ||
| Return information about the function including the following: | ||
|
|
||
| * arg - The name of the argument, this value is present for functions | ||
| that don't use attrset matching(i.e. `x: true`) and functions | ||
| that use an @-pattern(i.e. `{...}@argName`). Otherwise this | ||
| attr is absent. | ||
| * ellipsis - Set to true for functions of the form `{ a, b, ... }`. | ||
| * formals - Returns formal arguments, identical to | ||
| `builtins.functionArgs`. | ||
| )", | ||
| .fun = prim_functionInfo, | ||
| }); | ||
|
|
||
| /* */ | ||
| static void prim_mapAttrs(EvalState & state, const PosIdx pos, Value * * args, Value & v) | ||
| { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This appears to be common with the
functionArgsprimop and should be factored out.