-
From a technical point of view, I do not understand why exchanging an object directly works but not via a Func. Is this an error in Jint or are there are some technical reasons for that? var engine = new Jint.Engine();
engine.SetValue("Example", new Example());
engine.Execute("""
const obj = {
value: 42
};
const res = Example.ExchangeObject(obj); // works
const res2 = Example.ExchangeObjectViaFunc(() => 3); // works, only to ensure that not Func is the issue
const res3 = Example.ExchangeObjectViaFunc(() => obj); // does not work, throws 'Object must implement IConvertible'
""");
class Example()
{
public T ExchangeObject<T>(T obj)
{
return obj;
}
public T ExchangeObjectViaFunc<T>(Func<T> objViaFunc)
{
return objViaFunc();
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 5 replies
-
This is quite exotic use case due to the generic and probably hasn't been tested/implemented. As JS doesn't have generics the type system isn't prepared for such.. |
Beta Was this translation helpful? Give feedback.
-
Hmm, do you mean the generic T in the Example class, or that Func is based on generics. var engine = new Jint.Engine();
engine.SetValue("Example", new Example());
engine.Execute("""
const obj = {
value: 42
};
const res = Example.ExchangeObject(obj); // works
const res2 = Example.ExchangeObjectViaFunc(() => 3); // works, only to ensure that not Func is the issue
const res3 = Example.ExchangeObjectViaFunc(() => obj); // does not work, throws 'Object must implement IConvertible'
""");
class Example()
{
public object ExchangeObject(object obj)
{
return obj;
}
public object ExchangeObjectViaFunc(Func<object> objViaFunc)
{
return objViaFunc();
}
} What I do not understand is why it makes a difference if an object is passed directly or is returned by a Func. I both cases the object is passed from js to c#. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
I have found the code part that is responsible for the issue. It is the With this change these Func definitions work as expected public object ExchangeObjectViaFunc(Func<object> objViaFunc)
{
object res = objViaFunc();
return res;
} public T ExchangeObjectViaFunc<T>(Func<T> objViaFunc)
{
T res = objViaFunc();
return res;
} public int ExchangeBasicValueViaFunc<int>(Func<int> objViaFunc)
{
int res = objViaFunc();
return res;
} What do you think about it? Would you accept a PR with this kind of code change or in this direction assuming that all other tests still succeeds? |
Beta Was this translation helpful? Give feedback.
Looks good to me. Let me know if you have any concerns or further suggestions. #2053