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

Commit

Permalink
requiredDefines support for member method
Browse files Browse the repository at this point in the history
  • Loading branch information
ialex32x committed Sep 18, 2021
1 parent 05ac90d commit 730aa5a
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 107 deletions.
20 changes: 5 additions & 15 deletions Assets/Examples/Source/Editor/CustomBinding.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Reflection;
using System;

namespace Example.Editor
{
Expand All @@ -15,20 +16,6 @@ public override string GetBindingProcessName()

public override void OnPreExporting(BindingManager bindingManager)
{
// bindingManager.AddTypePrefixBlacklist("SyntaxTree.");

// bindingManager.AddExportedType(typeof(System.Threading.Tasks.Task))
// .Rename("ATask")
// .SetMemberBlocked("IsCompletedSuccessfully");
// bindingManager.AddExportedType(typeof(System.Threading.Tasks.Task<System.Net.Sockets.Socket>));
// bindingManager.AddExportedType(typeof(System.Threading.Tasks.Task<int>));

// bindingManager.AddExportedType(typeof(System.Net.Sockets.Socket));
// bindingManager.AddExportedType(typeof(System.Net.Sockets.SocketFlags));
// bindingManager.AddExportedType(typeof(System.Net.Sockets.AddressFamily));
// bindingManager.AddExportedType(typeof(System.Net.IPAddress));
// bindingManager.AddExportedType(typeof(System.Net.IPEndPoint));

// bindingManager.TryExportExtensionMethods(typeof(ExtensionTest)); // expose all extension methods
bindingManager.AddExtensionMethod<Transform>(ExtensionTest.ResetAll); // expose single extension method

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

#if CUSTOM_DEF_FOO
#if CUSTOM_DEF_FOO && UNITY_EDITOR
bindingManager.AddExportedType(typeof(FOO)).AddRequiredDefines("CUSTOM_DEF_FOO", "UNITY_EDITOR")
#if CUSTOM_DEF_PROP
.AddRequiredDefinesForMember("propValue", "CUSTOM_DEF_PROP")
#endif
#if CUSTOM_DEF_METHOD
.AddRequiredDefinesForMethod(t => t.GetMethod("Exclusive", Type.EmptyTypes), "CUSTOM_DEF_METHOD")
#endif
;
#endif
Expand Down
9 changes: 9 additions & 0 deletions Assets/Examples/Source/ValueTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ public class FOO
#endif

public static string value = "FOO";

#if CUSTOM_DEF_METHOD
public static void Exclusive()
{
}
#endif
public static void Exclusive(int i32)
{
}
}
#endif

Expand Down
8 changes: 4 additions & 4 deletions Assets/jsb/Source/Binding/Editor/BindingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1735,7 +1735,7 @@ public void Generate(TypeBindingFlags typeBindingFlags)
var cg = new CodeGenerator(this, typeBindingFlags);
var csOutDir = _utils.ReplacePathVars(prefs.outDir);
var tsOutDir = _utils.ReplacePathVars(prefs.typescriptDir);
var extraExt = prefs.extraExt;
var extraExt = prefs.extraExtForTypescript;
// var extraExt = "";

var cancel = false;
Expand All @@ -1762,7 +1762,7 @@ public void Generate(TypeBindingFlags typeBindingFlags)
OnPreGenerateType(typeBindingInfo);
cg.Generate(typeBindingInfo);
OnPostGenerateType(typeBindingInfo);
cg.WriteCSharp(csOutDir, typeBindingInfo.GetFileName(), extraExt);
cg.WriteCSharp(csOutDir, typeBindingInfo.GetFileName(), string.Empty);
cg.WriteTSD(tsOutDir, typeBindingInfo.GetFileName(), extraExt);
}
}
Expand Down Expand Up @@ -1794,7 +1794,7 @@ public void Generate(TypeBindingFlags typeBindingFlags)
{
cg.Clear();
cg.Generate(exportedDelegatesArray, _exportedHotfixDelegates);
cg.WriteCSharp(csOutDir, CodeGenerator.NameOfDelegates, extraExt);
cg.WriteCSharp(csOutDir, CodeGenerator.NameOfDelegates, string.Empty);
cg.WriteTSD(tsOutDir, CodeGenerator.NameOfDelegates, extraExt);
}
}
Expand Down Expand Up @@ -1835,7 +1835,7 @@ orderby t.tsTypeNaming.jsDepth
{
cg.Clear();
cg.GenerateBindingList(modules);
cg.WriteCSharp(csOutDir, CodeGenerator.NameOfBindingList, extraExt);
cg.WriteCSharp(csOutDir, CodeGenerator.NameOfBindingList, string.Empty);
cg.WriteTSD(tsOutDir, CodeGenerator.NameOfBindingList, extraExt);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public ClassCodeGen(CodeGenerator cg, TypeBindingInfo typeBindingInfo)
{
using (new TryCatchGuradCodeGen(cg))
{
using (new MethodCodeGen(cg, this.typeBindingInfo, methodBindingInfo))
using (new CSMethodCodeGen(cg, this.typeBindingInfo, methodBindingInfo))
{
}
}
Expand Down Expand Up @@ -156,7 +156,7 @@ public ClassCodeGen(CodeGenerator cg, TypeBindingInfo typeBindingInfo)
{
using (new TryCatchGuradCodeGen(cg))
{
using (new MethodCodeGen(cg, this.typeBindingInfo, methodBindingInfo))
using (new CSMethodCodeGen(cg, this.typeBindingInfo, methodBindingInfo))
{
}
}
Expand Down
109 changes: 63 additions & 46 deletions Assets/jsb/Source/Binding/Editor/Codegen/CodeGenHelper_Method.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public virtual void Dispose()
{
}

protected virtual IEnumerable<string> GetRequiredDefines(MethodBase methodBase)
{
return null;
}

public string GetParamArrayMatchType(T method)
{
var parameters = method.GetParameters();
Expand Down Expand Up @@ -148,7 +153,6 @@ protected virtual void InvokeVoidReturn()
}

// 输出所有变体绑定
// hasOverrides: 是否需要处理重载
protected void WriteCSAllVariants(TypeBindingInfo typeBindingInfo, MethodBaseBindingInfo<T> methodBindingInfo) // SortedDictionary<int, MethodBaseVariant<T>> variants)
{
var transform = typeBindingInfo.transform;
Expand All @@ -172,12 +176,20 @@ protected void WriteCSAllVariants(TypeBindingInfo typeBindingInfo, MethodBaseBin
if (variant.isVararg)
{
var method = variant.varargMethods[0];
WriteCSMethodBinding(methodBindingInfo, method.method, argc, true, method.isExtension);

using (new CSEditorOnlyCodeGen(cg, GetRequiredDefines(method.method)))
{
WriteCSBindingMethodBody(methodBindingInfo, method.method, argc, true, method.isExtension);
}
}
else
{
var method = variant.plainMethods[0];
WriteCSMethodBinding(methodBindingInfo, method.method, argc, false, method.isExtension);

using (new CSEditorOnlyCodeGen(cg, GetRequiredDefines(method.method)))
{
WriteCSBindingMethodBody(methodBindingInfo, method.method, argc, false, method.isExtension);
}
}
}
}
Expand Down Expand Up @@ -307,18 +319,21 @@ protected void GenMethodVariants(MethodBaseBindingInfo<T> methodBindingInfo, Sor
{
foreach (var method in variant.plainMethods)
{
var fixedMatchers = GetFixedMatchTypes(method.method, false, method.isExtension);
if (fixedMatchers.Length != 0)
using (new CSEditorOnlyCodeGen(cg, GetRequiredDefines(method.method)))
{
cg.cs.AppendLine($"if ({fixedMatchers})");
using (cg.cs.CodeBlockScope())
var fixedMatchers = GetFixedMatchTypes(method.method, false, method.isExtension);
if (fixedMatchers.Length != 0)
{
this.WriteCSMethodBinding(methodBindingInfo, method.method, argc, false, method.isExtension);
cg.cs.AppendLine($"if ({fixedMatchers})");
using (cg.cs.CodeBlockScope())
{
this.WriteCSBindingMethodBody(methodBindingInfo, method.method, argc, false, method.isExtension);
}
}
else
{
this.WriteCSBindingMethodBody(methodBindingInfo, method.method, argc, false, method.isExtension);
}
}
else
{
this.WriteCSMethodBinding(methodBindingInfo, method.method, argc, false, method.isExtension);
}
}

Expand All @@ -331,7 +346,10 @@ protected void GenMethodVariants(MethodBaseBindingInfo<T> methodBindingInfo, Sor
{
// 只有一个定参方法时, 不再判定类型匹配
var method = variant.plainMethods[0];
this.WriteCSMethodBinding(methodBindingInfo, method.method, argc, false, method.isExtension);
using (new CSEditorOnlyCodeGen(cg, GetRequiredDefines(method.method)))
{
this.WriteCSBindingMethodBody(methodBindingInfo, method.method, argc, false, method.isExtension);
}
}
}
}
Expand All @@ -341,21 +359,24 @@ protected void GenMethodVariants(MethodBaseBindingInfo<T> methodBindingInfo, Sor
{
foreach (var method in variant.varargMethods)
{
var fixedMatchers = GetFixedMatchTypes(method.method, true, method.isExtension);
var variantMatchers = GetParamArrayMatchType(method.method);

if (fixedMatchers.Length > 0)
{
cg.cs.AppendLine($"if ({fixedMatchers} && js_match_param_types(ctx, {expectedArgCount}, argv, {variantMatchers}))");
}
else
using (new CSEditorOnlyCodeGen(cg, GetRequiredDefines(method.method)))
{
cg.cs.AppendLine($"if (js_match_param_types(ctx, {expectedArgCount}, argv, {variantMatchers}))");
}
var fixedMatchers = GetFixedMatchTypes(method.method, true, method.isExtension);
var variantMatchers = GetParamArrayMatchType(method.method);

using (cg.cs.CodeBlockScope())
{
this.WriteCSMethodBinding(methodBindingInfo, method.method, argc, true, method.isExtension);
if (fixedMatchers.Length > 0)
{
cg.cs.AppendLine($"if ({fixedMatchers} && js_match_param_types(ctx, {expectedArgCount}, argv, {variantMatchers}))");
}
else
{
cg.cs.AppendLine($"if (js_match_param_types(ctx, {expectedArgCount}, argv, {variantMatchers}))");
}

using (cg.cs.CodeBlockScope())
{
this.WriteCSBindingMethodBody(methodBindingInfo, method.method, argc, true, method.isExtension);
}
}
}
}
Expand Down Expand Up @@ -517,21 +538,13 @@ private void WriteRebindThis(MethodBase method, Type callerType, string caller)
}

// 写入绑定代码
protected void WriteCSMethodBinding(MethodBaseBindingInfo<T> bindingInfo, T method, string argc, bool isVararg, bool isExtension)
protected void WriteCSBindingMethodBody(MethodBaseBindingInfo<T> bindingInfo, T method, string argc, bool isVararg, bool isExtension)
{
if (this.cg.bindingManager.prefs.verboseLog)
{
cg.bindingManager.Info($"WriteCSMethodBinding: {method.Name} {isExtension}");
}

// // 是否接管 cs 绑定代码生成
// var transform = cg.bindingManager.GetTypeTransform(method.DeclaringType);
// if (transform != null && transform.OnBinding(BindingPoints.METHOD_BINDING_FULL, method, cg))
// {
// return;
// }

// var isRaw = method.IsDefined(typeof(JSCFunctionAttribute));
var parameters = method.GetParameters();
Type callerType;
var caller = this.cg.AppendGetThisCS(method, isExtension, out callerType);
Expand Down Expand Up @@ -676,11 +689,24 @@ public ConstructorCodeGen(CodeGenerator cg, TypeBindingInfo typeBindingInfo)
}

// 生成成员方法绑定代码
public class MethodCodeGen : MethodBaseCodeGen<MethodInfo>
public class CSMethodCodeGen : MethodBaseCodeGen<MethodInfo>
{
protected TypeBindingInfo typeBindingInfo;
protected MethodBindingInfo methodBindingInfo;

public CSMethodCodeGen(CodeGenerator cg, TypeBindingInfo typeBindingInfo, MethodBindingInfo methodBindingInfo)
: base(cg)
{
this.typeBindingInfo = typeBindingInfo;
this.methodBindingInfo = methodBindingInfo;
WriteCSAllVariants(this.typeBindingInfo, this.methodBindingInfo);
}

protected override IEnumerable<string> GetRequiredDefines(MethodBase methodBase)
{
return typeBindingInfo.transform.GetRequiredDefinesOfMethod(methodBase);
}

protected override Type GetReturnType(MethodInfo method)
{
return method.ReturnType;
Expand Down Expand Up @@ -808,15 +834,6 @@ protected override string GetInvokeBinding(string caller, MethodInfo method, boo
// 普通成员调用
return $"{caller}.{method.Name}({arglistSig})";
}

public MethodCodeGen(CodeGenerator cg, TypeBindingInfo typeBindingInfo, MethodBindingInfo methodBindingInfo)
: base(cg)
{
this.typeBindingInfo = typeBindingInfo;
this.methodBindingInfo = methodBindingInfo;
WriteCSAllVariants(this.typeBindingInfo, this.methodBindingInfo);
// WriteTSAllVariants(this.bindingInfo);
}
}

public class TSConstructorCodeGen : MethodBaseCodeGen<ConstructorInfo>
Expand All @@ -840,7 +857,7 @@ public TSConstructorCodeGen(CodeGenerator cg, TypeBindingInfo typeBindingInfo, M
WriteTSAllVariants(typeBindingInfo, this.bindingInfo);
}
}

public class TSMethodCodeGen<T> : MethodBaseCodeGen<T>
where T : MethodInfo
{
Expand Down
Loading

0 comments on commit 730aa5a

Please sign in to comment.