![]() ![]() Note the actual method call here is done via the InterfaceFuncInvoker1::Invoke function, which is in the GeneratedInterfaceInvokers.h file. InterfaceFuncInvoker1::Invoke(&Interface_MethodOnInterface_m22_MethodInfo, L_1, (String_t*) &_stringLiteral1) Important_ctor_m0(L_0, /*hidden argument*/&Important_ctor_m0_MethodInfo) This call is implemented by il2cpp.exe similar to a virtual method call: Important_t1 * L_0 = (Important_t1 *)il2cpp_codegen_object_new (InitializedTypeInfo(&Important_t1_il2cpp_TypeInfo)) It’s also possible to call a method in C# via an interface. Note that in the process of looking at a delegate method call, we’ve also seen how a call via a virtual method works. Calling a method via a delegate is significantly more costly than calling the same method directly. By making what looks like a simple change to the C# code, we’ve now gone from a single call to a C++ free function to multiple function calls, plus a table lookup. It is this ImportantMethodDelegate_Invoke_m5 function which will actually make the call to the method named Method in the C# code. This is a virtual method, so we need to make a virtual call. If we search for the method named "ImportantMethodDelegate_Invoke_m5" in the generated code, we see that the call is actually to the managed Invoke method on the ImportantMethodDelegate type. The actual method we are going to call is passed in via the MethodInfo* (method metadata) argument: ImportantMethodDelegate_Invoke_m5_MethodInfo. In this case at least, we did not think that forking the generated code for C++11 compilers was worth the additional complexity.īut why is this a virtual method call? Aren’t we calling an instance method in the C# code? Recall that we are calling the instance method via a C# delegate. However, the C++ code generated by il2cpp.exe has to work with some C++ compilers which don’t yet support all C++ 11 features, including variadic templates. Why don’t we used C++11 variadic templates to implement these VirtFuncInvokerN methods? This looks like a situation begging for variadic templates, and indeed it is. The call into libil2cpp GetVirtualInvokeData looks up a virtual method in the vtable struct generated based on the managed code, then it makes a call to that method. Return ((Func)thodInfo->method)(data.target, p1, thodInfo) VirtualInvokeData data = il2cpp::vm::Runtime::GetVirtualInvokeData (method, obj) Static inline R Invoke (MethodInfo* method, void* obj, T1 p1) Typedef R (*Func)(void*, T1, MethodInfo*) Instead, il2cpp.exe creates this file based on the usage of virtual functions that return a value ( VirtFuncInvoker N) and those that don’t ( VirtActionInvoker N), where N is the number of arguments to the method. This file is generated by il2cpp.exe, but it doesn’t come from any IL code. The method VirtFuncInvoker1::Invoke is located in the GeneratedVirtualInvokers.h file. Note that the actual method called here is not part of the generated code. I’ve added a few comments to indicate the different parts of the generated code. ![]() VirtFuncInvoker1::Invoke(&ImportantMethodDelegate_Invoke_m5_MethodInfo, L_4, (String_t*) &_stringLiteral1) ImportantMethodDelegate_ctor_m4(L_3, L_1, L_2, /*hidden argument*/&ImportantMethodDelegate_ctor_m4_MethodInfo) ![]() ImportantMethodDelegate_t4 * L_3 = (ImportantMethodDelegate_t4 *)il2cpp_codegen_object_new (InitializedTypeInfo(&ImportantMethodDelegate_t4_il2cpp_TypeInfo)) The script starts with an interface and class definition: interface Interface I’ll build with a single script file, modified from the last post so that we can see the different types of method calls. I’m building with the “Development Player” option enabled, and the “Enable Exceptions” option set to a value of “Full”. I’ll run the editor on Windows, and build for the WebGL platform. We like to expose and discuss details like this when it is possible though! Please take everything discussed in this series as implementation details. However, the concepts should remain the same. In each case, we will focus on what the generated C++ code is doing and, specifically, on how much those instructions will cost.Īs with all of the posts in this series, we will be exploring code that is subject to change and, in fact, is likely to change in a newer version of Unity.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |