Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 730aa5a

Browse files
committed
requiredDefines support for member method
1 parent 05ac90d commit 730aa5a

7 files changed

Lines changed: 127 additions & 107 deletions

File tree

Assets/Examples/Source/Editor/CustomBinding.cs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Reflection;
2+
using System;
23

34
namespace Example.Editor
45
{
@@ -15,20 +16,6 @@ public override string GetBindingProcessName()
1516

1617
public override void OnPreExporting(BindingManager bindingManager)
1718
{
18-
// bindingManager.AddTypePrefixBlacklist("SyntaxTree.");
19-
20-
// bindingManager.AddExportedType(typeof(System.Threading.Tasks.Task))
21-
// .Rename("ATask")
22-
// .SetMemberBlocked("IsCompletedSuccessfully");
23-
// bindingManager.AddExportedType(typeof(System.Threading.Tasks.Task<System.Net.Sockets.Socket>));
24-
// bindingManager.AddExportedType(typeof(System.Threading.Tasks.Task<int>));
25-
26-
// bindingManager.AddExportedType(typeof(System.Net.Sockets.Socket));
27-
// bindingManager.AddExportedType(typeof(System.Net.Sockets.SocketFlags));
28-
// bindingManager.AddExportedType(typeof(System.Net.Sockets.AddressFamily));
29-
// bindingManager.AddExportedType(typeof(System.Net.IPAddress));
30-
// bindingManager.AddExportedType(typeof(System.Net.IPEndPoint));
31-
3219
// bindingManager.TryExportExtensionMethods(typeof(ExtensionTest)); // expose all extension methods
3320
bindingManager.AddExtensionMethod<Transform>(ExtensionTest.ResetAll); // expose single extension method
3421

@@ -53,10 +40,13 @@ public override void OnPreExporting(BindingManager bindingManager)
5340
bindingManager.AddExportedType(typeof(TWrapper<Vector3>));
5441
bindingManager.AddExportedType(typeof(DisposableObject)).SetDisposable();
5542

56-
#if CUSTOM_DEF_FOO
43+
#if CUSTOM_DEF_FOO && UNITY_EDITOR
5744
bindingManager.AddExportedType(typeof(FOO)).AddRequiredDefines("CUSTOM_DEF_FOO", "UNITY_EDITOR")
5845
#if CUSTOM_DEF_PROP
5946
.AddRequiredDefinesForMember("propValue", "CUSTOM_DEF_PROP")
47+
#endif
48+
#if CUSTOM_DEF_METHOD
49+
.AddRequiredDefinesForMethod(t => t.GetMethod("Exclusive", Type.EmptyTypes), "CUSTOM_DEF_METHOD")
6050
#endif
6151
;
6252
#endif

Assets/Examples/Source/ValueTest.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ public class FOO
6161
#endif
6262

6363
public static string value = "FOO";
64+
65+
#if CUSTOM_DEF_METHOD
66+
public static void Exclusive()
67+
{
68+
}
69+
#endif
70+
public static void Exclusive(int i32)
71+
{
72+
}
6473
}
6574
#endif
6675

Assets/jsb/Source/Binding/Editor/BindingManager.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,7 +1735,7 @@ public void Generate(TypeBindingFlags typeBindingFlags)
17351735
var cg = new CodeGenerator(this, typeBindingFlags);
17361736
var csOutDir = _utils.ReplacePathVars(prefs.outDir);
17371737
var tsOutDir = _utils.ReplacePathVars(prefs.typescriptDir);
1738-
var extraExt = prefs.extraExt;
1738+
var extraExt = prefs.extraExtForTypescript;
17391739
// var extraExt = "";
17401740

17411741
var cancel = false;
@@ -1762,7 +1762,7 @@ public void Generate(TypeBindingFlags typeBindingFlags)
17621762
OnPreGenerateType(typeBindingInfo);
17631763
cg.Generate(typeBindingInfo);
17641764
OnPostGenerateType(typeBindingInfo);
1765-
cg.WriteCSharp(csOutDir, typeBindingInfo.GetFileName(), extraExt);
1765+
cg.WriteCSharp(csOutDir, typeBindingInfo.GetFileName(), string.Empty);
17661766
cg.WriteTSD(tsOutDir, typeBindingInfo.GetFileName(), extraExt);
17671767
}
17681768
}
@@ -1794,7 +1794,7 @@ public void Generate(TypeBindingFlags typeBindingFlags)
17941794
{
17951795
cg.Clear();
17961796
cg.Generate(exportedDelegatesArray, _exportedHotfixDelegates);
1797-
cg.WriteCSharp(csOutDir, CodeGenerator.NameOfDelegates, extraExt);
1797+
cg.WriteCSharp(csOutDir, CodeGenerator.NameOfDelegates, string.Empty);
17981798
cg.WriteTSD(tsOutDir, CodeGenerator.NameOfDelegates, extraExt);
17991799
}
18001800
}
@@ -1835,7 +1835,7 @@ orderby t.tsTypeNaming.jsDepth
18351835
{
18361836
cg.Clear();
18371837
cg.GenerateBindingList(modules);
1838-
cg.WriteCSharp(csOutDir, CodeGenerator.NameOfBindingList, extraExt);
1838+
cg.WriteCSharp(csOutDir, CodeGenerator.NameOfBindingList, string.Empty);
18391839
cg.WriteTSD(tsOutDir, CodeGenerator.NameOfBindingList, extraExt);
18401840
}
18411841
}

Assets/jsb/Source/Binding/Editor/Codegen/CodeGenHelper_Class.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public ClassCodeGen(CodeGenerator cg, TypeBindingInfo typeBindingInfo)
115115
{
116116
using (new TryCatchGuradCodeGen(cg))
117117
{
118-
using (new MethodCodeGen(cg, this.typeBindingInfo, methodBindingInfo))
118+
using (new CSMethodCodeGen(cg, this.typeBindingInfo, methodBindingInfo))
119119
{
120120
}
121121
}
@@ -156,7 +156,7 @@ public ClassCodeGen(CodeGenerator cg, TypeBindingInfo typeBindingInfo)
156156
{
157157
using (new TryCatchGuradCodeGen(cg))
158158
{
159-
using (new MethodCodeGen(cg, this.typeBindingInfo, methodBindingInfo))
159+
using (new CSMethodCodeGen(cg, this.typeBindingInfo, methodBindingInfo))
160160
{
161161
}
162162
}

Assets/jsb/Source/Binding/Editor/Codegen/CodeGenHelper_Method.cs

Lines changed: 63 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)