moonsharp icon indicating copy to clipboard operation
moonsharp copied to clipboard

Wrong extension methods caching?

Open yakiro-nvg opened this issue 10 years ago • 5 comments

In DispatchingUserDataDescriptor.cs

if (v == null && m_ExtMethodsVersion < UserData.GetExtensionMethodsChangeVersion())
{
    m_ExtMethodsVersion = UserData.GetExtensionMethodsChangeVersion();

    v = TryIndexOnExtMethod(script, obj, index.String);
    if (v == null) v = TryIndexOnExtMethod(script, obj, UpperFirstLetter(index.String));
    if (v == null) v = TryIndexOnExtMethod(script, obj, Camelify(index.String));
    if (v == null) v = TryIndexOnExtMethod(script, obj, UpperFirstLetter(Camelify(index.String)));
}   

In my case, it only work if the condition is: if (v == null && m_ExtMethodsVersion <= UserData.GetExtensionMethodsChangeVersion())

yakiro-nvg avatar Jul 10 '15 02:07 yakiro-nvg

Scenario is: UserData.RegisterExtensionType(t1) UserData.RegisterExtensionType(t2) UserData.GetExtensionMethodsChangeVersion() = 2 call t1.method m_ExtMethodsVersion = 2 but now only t1.method in the cache so call t2.method failed

yakiro-nvg avatar Jul 10 '15 02:07 yakiro-nvg

Yes, sounds suspicious .. I'll check asap. Thanks

xanathar avatar Jul 10 '15 08:07 xanathar

No repro :( I've added Interop_Overloads_Twice_ExtMethods1 and Interop_Overloads_Twice_ExtMethods2 tests in UserDataOverloadsTest.cs ... let me know if you still have the problem or what is missing from the test...

xanathar avatar Oct 23 '15 04:10 xanathar

fixed in here: https://github.com/xanathar/moonsharp/pull/130

havietisov avatar Mar 11 '16 22:03 havietisov

I'm running into the same issue, #130 does fix it for me, but there will likely be impacted performance as a result as after that patch it'll always check for extension methods. Not sure the best way to handle it, maybe try caching all extension methods (rather than just the requested one) the first time you need them?

I believe this is what is going on: It seems that if you have a single DispatchingUserDataDescriptor and then call two separate extension methods on it, the second one will fail because when the first is called m_ExtMethodsVersion is set to the latest version but only the first method is cached. When the second is called, m_ExtMethodsVersion is already at latest so it doesn't try and lookup the new method and falsely assumes it was already cached.

redxdev avatar Dec 12 '19 04:12 redxdev