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
Bound function should not inherit from FunctionInstance #1044
Bound function should not inherit from FunctionInstance #1044
Conversation
aa130b2
to
21e3bee
Compare
@lahma BindFunctionInstance was moved from public to internal as part of this change, which prevents casting of JsValues to BindFunctionInstance. Can this be moved back to public? |
Why do you need to cast it? |
I'm the first to admit I'm weak on JavaScript -- but below is a quick sample that runs in Jint 3.0.0-beta-2033 but fails in 3.0.0-beta-2037: using Jint;
using Jint.Native;
using Jint.Native.Function;
using System;
namespace JintTest
{
class Program
{
private Engine _jintEngine;
static void Main(string[] args)
{
new Program().Run();
Console.ReadLine();
}
public Program()
{
var opts = new Options();
opts.AllowClr(typeof(Program).Assembly);
_jintEngine = new Engine(opts);
_jintEngine.SetValue("MyCSharpClass", this);
}
public void log(string logStr)
{
Console.WriteLine($"From JS: '{logStr}'");
}
public void executeScript(JsValue script)
{
if (script is FunctionInstance func)
{
_jintEngine.Invoke(func);
}
else
{
var parser = new Esprima.JavaScriptParser(script.ToString());
var compiledScript = parser.ParseScript();
_jintEngine.Execute(compiledScript);
}
}
public void Run()
{
var js = @"
var outer = {
innerFunc() {
MyCSharpClass.log('test');
},
executeThis(script) {
MyCSharpClass.executeScript(script);
}
}
outer.executeThis(outer.innerFunc.bind(this));
";
_jintEngine.Execute(js);
}
}
} The value passed to the CS In this simple example the Appreciate your input and any guidance you might be able to provide, thanks! |
I didn't quite catch the idea behind using the bind function with interop. Why do you need different |
Well this is just a contrived example I built from the much-more-complex javascript code we're using. We have lots of valid use cases for the .bind() function with the same basic calling convention I'm demonstrating. Again I'm not the JS expert on our team but I can get our expert to help me create a better example if it will make the issue more clear? Seems like I should be able to use the bind() function since it's valid script, but maybe there's something unexpected we're doing. Again thanks for your help. |
Yes it would be great if you could elaborate the valid use case you are referring, it would make the ask clearer and we could possibly then make samples utilizing the paradigm and ensure it can be used. |
I spoke with my JS expert this morning and it turns out there are several cases of our use of .bind() that in hindsight are not necessary -- so perhaps this change actually highlights a need for refactoring we should be doing anyway. We're working through the code to see if we can find a valid use case -- I'll update my example code if we find one. Really appreciate the responses, thanks. |
We've come across an example usage of .bind() that I think justifies making BindFunctionInstance public. In this example, we've implemented our own version of the JS setTimeout() method in C#, in order to support deferred execution of JS functions. We pass an object to be used in the context of the deferred execution by using the .bind() statement. This currently results in the same issue, where the JsValue we receive in the C# executeScript() method is an instance of BindFunctionInstance, but we cannot cast to that class because the class is now marked as internal. This is another watered-down example of much-more-complex code in our solution, but again this is a valid usage of JavaScript that I'm hoping can be supported in Jint. Appreciate your attention, thanks!
|
@paynesworld would you like to create a PR for a new project in solution |
From spec: