Skip to content
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

Function .toString() does not contain the arguments of the function. #1557

Closed
dkgof opened this issue May 24, 2023 · 1 comment
Closed

Function .toString() does not contain the arguments of the function. #1557

dkgof opened this issue May 24, 2023 · 1 comment

Comments

@dkgof
Copy link

dkgof commented May 24, 2023

Running the code:

let testMethod = (a,b)=>{return a+b;};
console.log(testMethod.toString());

Results in the output:

"function testMethod() { [native code] }"

Same code run in chrome devtools outputs:

"(a,b)=>{return a+b;}"

It would be very nice if the arguments a and b were present, so it would be possible to detect arguments of a function programatically.

Or is this somehow possible in another way in jint?

@dkgof
Copy link
Author

dkgof commented May 24, 2023

I found a way to work around it, by exposing a c# method to js, and using reflection to find the parameters:

engine.SetValue("getFunctionArguments", new Func<Delegate, string[]>((function)=>{
    List<string> foundParameters = new List<string>();
    foreach(var info in function.Target.GetType().GetProperties()) {
        if(info.Name == "FunctionDeclaration") {
            Esprima.Ast.IFunction functionDeclaration = (Esprima.Ast.IFunction) info.GetValue(function.Target);
            foreach(var param in functionDeclaration.Params.ToArray()) {
                foundParameters.Add(param.ToString());
            }
        }
    }
    return foundParameters.ToArray();
}));

Calling getFunctionArguments from JS, with the function i need arguments for, returns my wanted array of argument names.

@dkgof dkgof closed this as completed May 24, 2023
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant