@@ -33,6 +33,11 @@ public virtual void Dispose()
3333 {
3434 }
3535
36+ protected virtual IEnumerable < string > GetRequiredDefines ( MethodBase methodBase )
37+ {
38+ return null ;
39+ }
40+
3641 public string GetParamArrayMatchType ( T method )
3742 {
3843 var parameters = method . GetParameters ( ) ;
@@ -148,7 +153,6 @@ protected virtual void InvokeVoidReturn()
148153 }
149154
150155 // 输出所有变体绑定
151- // hasOverrides: 是否需要处理重载
152156 protected void WriteCSAllVariants ( TypeBindingInfo typeBindingInfo , MethodBaseBindingInfo < T > methodBindingInfo ) // SortedDictionary<int, MethodBaseVariant<T>> variants)
153157 {
154158 var transform = typeBindingInfo . transform ;
@@ -172,12 +176,20 @@ protected void WriteCSAllVariants(TypeBindingInfo typeBindingInfo, MethodBaseBin
172176 if ( variant . isVararg )
173177 {
174178 var method = variant . varargMethods [ 0 ] ;
175- WriteCSMethodBinding ( methodBindingInfo , method . method , argc , true , method . isExtension ) ;
179+
180+ using ( new CSEditorOnlyCodeGen ( cg , GetRequiredDefines ( method . method ) ) )
181+ {
182+ WriteCSBindingMethodBody ( methodBindingInfo , method . method , argc , true , method . isExtension ) ;
183+ }
176184 }
177185 else
178186 {
179187 var method = variant . plainMethods [ 0 ] ;
180- WriteCSMethodBinding ( methodBindingInfo , method . method , argc , false , method . isExtension ) ;
188+
189+ using ( new CSEditorOnlyCodeGen ( cg , GetRequiredDefines ( method . method ) ) )
190+ {
191+ WriteCSBindingMethodBody ( methodBindingInfo , method . method , argc , false , method . isExtension ) ;
192+ }
181193 }
182194 }
183195 }
@@ -307,18 +319,21 @@ protected void GenMethodVariants(MethodBaseBindingInfo<T> methodBindingInfo, Sor
307319 {
308320 foreach ( var method in variant . plainMethods )
309321 {
310- var fixedMatchers = GetFixedMatchTypes ( method . method , false , method . isExtension ) ;
311- if ( fixedMatchers . Length != 0 )
322+ using ( new CSEditorOnlyCodeGen ( cg , GetRequiredDefines ( method . method ) ) )
312323 {
313- cg . cs . AppendLine ( $ "if ( { fixedMatchers } )" ) ;
314- using ( cg . cs . CodeBlockScope ( ) )
324+ var fixedMatchers = GetFixedMatchTypes ( method . method , false , method . isExtension ) ;
325+ if ( fixedMatchers . Length != 0 )
315326 {
316- this . WriteCSMethodBinding ( methodBindingInfo , method . method , argc , false , method . isExtension ) ;
327+ cg . cs . AppendLine ( $ "if ({ fixedMatchers } )") ;
328+ using ( cg . cs . CodeBlockScope ( ) )
329+ {
330+ this . WriteCSBindingMethodBody ( methodBindingInfo , method . method , argc , false , method . isExtension ) ;
331+ }
332+ }
333+ else
334+ {
335+ this . WriteCSBindingMethodBody ( methodBindingInfo , method . method , argc , false , method . isExtension ) ;
317336 }
318- }
319- else
320- {
321- this . WriteCSMethodBinding ( methodBindingInfo , method . method , argc , false , method . isExtension ) ;
322337 }
323338 }
324339
@@ -331,7 +346,10 @@ protected void GenMethodVariants(MethodBaseBindingInfo<T> methodBindingInfo, Sor
331346 {
332347 // 只有一个定参方法时, 不再判定类型匹配
333348 var method = variant . plainMethods [ 0 ] ;
334- this . WriteCSMethodBinding ( methodBindingInfo , method . method , argc , false , method . isExtension ) ;
349+ using ( new CSEditorOnlyCodeGen ( cg , GetRequiredDefines ( method . method ) ) )
350+ {
351+ this . WriteCSBindingMethodBody ( methodBindingInfo , method . method , argc , false , method . isExtension ) ;
352+ }
335353 }
336354 }
337355 }
@@ -341,21 +359,24 @@ protected void GenMethodVariants(MethodBaseBindingInfo<T> methodBindingInfo, Sor
341359 {
342360 foreach ( var method in variant . varargMethods )
343361 {
344- var fixedMatchers = GetFixedMatchTypes ( method . method , true , method . isExtension ) ;
345- var variantMatchers = GetParamArrayMatchType ( method . method ) ;
346-
347- if ( fixedMatchers . Length > 0 )
348- {
349- cg . cs . AppendLine ( $ "if ({ fixedMatchers } && js_match_param_types(ctx, { expectedArgCount } , argv, { variantMatchers } ))") ;
350- }
351- else
362+ using ( new CSEditorOnlyCodeGen ( cg , GetRequiredDefines ( method . method ) ) )
352363 {
353- cg . cs . AppendLine ( $ "if (js_match_param_types(ctx, { expectedArgCount } , argv, { variantMatchers } ))" ) ;
354- }
364+ var fixedMatchers = GetFixedMatchTypes ( method . method , true , method . isExtension ) ;
365+ var variantMatchers = GetParamArrayMatchType ( method . method ) ;
355366
356- using ( cg . cs . CodeBlockScope ( ) )
357- {
358- this . WriteCSMethodBinding ( methodBindingInfo , method . method , argc , true , method . isExtension ) ;
367+ if ( fixedMatchers . Length > 0 )
368+ {
369+ cg . cs . AppendLine ( $ "if ({ fixedMatchers } && js_match_param_types(ctx, { expectedArgCount } , argv, { variantMatchers } ))") ;
370+ }
371+ else
372+ {
373+ cg . cs . AppendLine ( $ "if (js_match_param_types(ctx, { expectedArgCount } , argv, { variantMatchers } ))") ;
374+ }
375+
376+ using ( cg . cs . CodeBlockScope ( ) )
377+ {
378+ this . WriteCSBindingMethodBody ( methodBindingInfo , method . method , argc , true , method . isExtension ) ;
379+ }
359380 }
360381 }
361382 }
@@ -517,21 +538,13 @@ private void WriteRebindThis(MethodBase method, Type callerType, string caller)
517538 }
518539
519540 // 写入绑定代码
520- protected void WriteCSMethodBinding ( MethodBaseBindingInfo < T > bindingInfo , T method , string argc , bool isVararg , bool isExtension )
541+ protected void WriteCSBindingMethodBody ( MethodBaseBindingInfo < T > bindingInfo , T method , string argc , bool isVararg , bool isExtension )
521542 {
522543 if ( this . cg . bindingManager . prefs . verboseLog )
523544 {
524545 cg . bindingManager . Info ( $ "WriteCSMethodBinding: { method . Name } { isExtension } ") ;
525546 }
526547
527- // // 是否接管 cs 绑定代码生成
528- // var transform = cg.bindingManager.GetTypeTransform(method.DeclaringType);
529- // if (transform != null && transform.OnBinding(BindingPoints.METHOD_BINDING_FULL, method, cg))
530- // {
531- // return;
532- // }
533-
534- // var isRaw = method.IsDefined(typeof(JSCFunctionAttribute));
535548 var parameters = method . GetParameters ( ) ;
536549 Type callerType ;
537550 var caller = this . cg . AppendGetThisCS ( method , isExtension , out callerType ) ;
@@ -676,11 +689,24 @@ public ConstructorCodeGen(CodeGenerator cg, TypeBindingInfo typeBindingInfo)
676689 }
677690
678691 // 生成成员方法绑定代码
679- public class MethodCodeGen : MethodBaseCodeGen < MethodInfo >
692+ public class CSMethodCodeGen : MethodBaseCodeGen < MethodInfo >
680693 {
681694 protected TypeBindingInfo typeBindingInfo ;
682695 protected MethodBindingInfo methodBindingInfo ;
683696
697+ public CSMethodCodeGen ( CodeGenerator cg , TypeBindingInfo typeBindingInfo , MethodBindingInfo methodBindingInfo )
698+ : base ( cg )
699+ {
700+ this . typeBindingInfo = typeBindingInfo ;
701+ this . methodBindingInfo = methodBindingInfo ;
702+ WriteCSAllVariants ( this . typeBindingInfo , this . methodBindingInfo ) ;
703+ }
704+
705+ protected override IEnumerable < string > GetRequiredDefines ( MethodBase methodBase )
706+ {
707+ return typeBindingInfo . transform . GetRequiredDefinesOfMethod ( methodBase ) ;
708+ }
709+
684710 protected override Type GetReturnType ( MethodInfo method )
685711 {
686712 return method . ReturnType ;
@@ -808,15 +834,6 @@ protected override string GetInvokeBinding(string caller, MethodInfo method, boo
808834 // 普通成员调用
809835 return $ "{ caller } .{ method . Name } ({ arglistSig } )";
810836 }
811-
812- public MethodCodeGen ( CodeGenerator cg , TypeBindingInfo typeBindingInfo , MethodBindingInfo methodBindingInfo )
813- : base ( cg )
814- {
815- this . typeBindingInfo = typeBindingInfo ;
816- this . methodBindingInfo = methodBindingInfo ;
817- WriteCSAllVariants ( this . typeBindingInfo , this . methodBindingInfo ) ;
818- // WriteTSAllVariants(this.bindingInfo);
819- }
820837 }
821838
822839 public class TSConstructorCodeGen : MethodBaseCodeGen < ConstructorInfo >
@@ -840,7 +857,7 @@ public TSConstructorCodeGen(CodeGenerator cg, TypeBindingInfo typeBindingInfo, M
840857 WriteTSAllVariants ( typeBindingInfo , this . bindingInfo ) ;
841858 }
842859 }
843-
860+
844861 public class TSMethodCodeGen < T > : MethodBaseCodeGen < T >
845862 where T : MethodInfo
846863 {
0 commit comments