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

Commit 61a4f9b

Browse files
committed
fix require.main == module (EvalMain)
1 parent c9e1455 commit 61a4f9b

18 files changed

Lines changed: 3294 additions & 2327 deletions

Assets/Examples/Source/ExtensionTest.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,10 @@ public static bool TestWithArgsAndOut(this Transform transform, Vector3 pos, Qua
2929
dist = pos.magnitude;
3030
return true;
3131
}
32+
33+
public static void TestWithScriptObject(this Transform transform, ScriptFunction function)
34+
{
35+
function?.Invoke();
36+
}
3237
}
3338
}

Assets/Examples/Source/Sample.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ void Awake()
8989
byteBufferAllocator = new ByteBufferPooledAllocator(),
9090
binder = DefaultBinder.GetBinder(useReflectBind),
9191
});
92-
_rt.ResolveModule(entryFileName);
92+
_rt.EvalMain(entryFileName);
9393
}
9494

9595
void Update()

Assets/Generated/Typings/jsb.autogen.d.ts

Lines changed: 3192 additions & 2283 deletions
Large diffs are not rendered by default.

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,12 @@ public string GetDefaultTypePrefix()
716716
return "jsb.";
717717
}
718718

719+
public string GetValueOperation(string operation, Type forType)
720+
{
721+
//TODO codegen: lookup js_* (push/get/rebind) for specified type (instead of the partial class 'Values')
722+
return operation;
723+
}
724+
719725
public string GetCSNamespace(Type type)
720726
{
721727
return string.IsNullOrEmpty(type.Namespace) ? "" : (type.Namespace + ".");

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ public EventOperationCodeGen(CodeGenerator cg, EventBindingInfo bindingInfo)
5555
if (declaringType.IsValueType && !eventInfo.GetAddMethod().IsStatic)
5656
{
5757
// 非静态结构体属性修改, 尝试替换实例
58-
this.cg.cs.AppendLine($"js_rebind_this(ctx, this_obj, ref {caller});");
58+
var js_rebind_this = this.cg.bindingManager.GetValueOperation("js_rebind_this", bindingInfo.declaringType);
59+
this.cg.cs.AppendLine($"{js_rebind_this}(ctx, this_obj, ref {caller});");
5960
}
6061
this.cg.cs.AppendLine("return JSApi.JS_UNDEFINED;");
6162
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ public FieldSetterCodeGen(CodeGenerator cg, FieldBindingInfo bindingInfo)
5353
if (declaringType.IsValueType && !fieldInfo.IsStatic)
5454
{
5555
// 非静态结构体字段修改, 尝试替换实例
56-
this.cg.cs.AppendLine($"js_rebind_this(ctx, this_obj, ref {caller});");
56+
var js_rebind_this = this.cg.bindingManager.GetValueOperation("js_rebind_this", declaringType);
57+
this.cg.cs.AppendLine($"{js_rebind_this}(ctx, this_obj, ref {caller});");
5758
}
5859
this.cg.cs.AppendLine("return JSApi.JS_UNDEFINED;");
5960
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -504,13 +504,14 @@ protected void SplitParamters(ParameterInfo[] parameters, int index, List<Parame
504504
}
505505
}
506506

507-
private void WriteRebindThis(MethodBase method, string caller)
507+
private void WriteRebindThis(MethodBase method, Type callerType, string caller)
508508
{
509509
if (!method.IsStatic && method.DeclaringType.IsValueType) // struct 非静态方法 检查 Mutable 属性
510510
{
511511
if (!string.IsNullOrEmpty(caller))
512512
{
513-
cg.cs.AppendLine($"js_rebind_this(ctx, this_obj, ref {caller});");
513+
var js_rebind_this = this.cg.bindingManager.GetValueOperation("js_rebind_this", callerType);
514+
cg.cs.AppendLine($"{js_rebind_this}(ctx, this_obj, ref {caller});");
514515
}
515516
}
516517
}
@@ -532,7 +533,8 @@ protected void WriteCSMethodBinding(MethodBaseBindingInfo<T> bindingInfo, T meth
532533

533534
// var isRaw = method.IsDefined(typeof(JSCFunctionAttribute));
534535
var parameters = method.GetParameters();
535-
var caller = this.cg.AppendGetThisCS(method, isExtension);
536+
Type callerType;
537+
var caller = this.cg.AppendGetThisCS(method, isExtension, out callerType);
536538
var returnType = GetReturnType(method);
537539

538540
if (returnType == null || returnType == typeof(void))
@@ -543,7 +545,7 @@ protected void WriteCSMethodBinding(MethodBaseBindingInfo<T> bindingInfo, T meth
543545
this.EndInvokeBinding();
544546

545547
_WriteBackParameters(isExtension, parameters);
546-
WriteRebindThis(method, caller);
548+
WriteRebindThis(method, callerType, caller);
547549
InvokeVoidReturn();
548550
}
549551
else

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ public PropertyGetterCodeGen(CodeGenerator cg, PropertyBindingInfo bindingInfo)
1616
this.cg = cg;
1717
this.bindingInfo = bindingInfo;
1818

19-
var caller = this.cg.AppendGetThisCS(bindingInfo.getMethod, false);
19+
Type callerType;
20+
var caller = this.cg.AppendGetThisCS(bindingInfo.getMethod, false, out callerType);
2021

2122
this.cg.cs.AppendLine("var ret = {0}.{1};", caller, bindingInfo.propertyInfo.Name);
2223
var pusher = this.cg.AppendValuePusher(bindingInfo.propertyType, "ret");
@@ -40,7 +41,8 @@ public PropertySetterCodeGen(CodeGenerator cg, PropertyBindingInfo propertyBindi
4041

4142
var propertyInfo = this.bindingInfo.propertyInfo;
4243
var declaringType = propertyInfo.DeclaringType;
43-
var caller = this.cg.AppendGetThisCS(propertyBindingInfo.setMethod, false);
44+
Type callerType;
45+
var caller = this.cg.AppendGetThisCS(propertyBindingInfo.setMethod, false, out callerType);
4446
var propertyType = this.cg.bindingManager.GetCSTypeFullName(propertyInfo.PropertyType);
4547

4648
this.cg.cs.AppendLine("{0} value;", propertyType);
@@ -54,7 +56,8 @@ public PropertySetterCodeGen(CodeGenerator cg, PropertyBindingInfo propertyBindi
5456
if (declaringType.IsValueType && !propertyBindingInfo.setMethod.IsStatic)
5557
{
5658
// 非静态结构体属性修改, 尝试替换实例
57-
this.cg.cs.AppendLine($"js_rebind_this(ctx, this_obj, ref {caller});");
59+
var js_rebind_this = this.cg.bindingManager.GetValueOperation("js_rebind_this", callerType);
60+
this.cg.cs.AppendLine($"{js_rebind_this}(ctx, this_obj, ref {caller});");
5861
}
5962
this.cg.cs.AppendLine("return JSApi.JS_UNDEFINED;");
6063
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,17 +512,22 @@ public string AppendGetThisCS(EventBindingInfo bindingInfo)
512512
return caller;
513513
}
514514

515-
public string AppendGetThisCS(MethodBase method, bool asExtensionAnyway)
515+
public string AppendGetThisCS(MethodBase method, bool asExtensionAnyway, out Type thisType)
516516
{
517517
if (method.IsConstructor)
518518
{
519+
thisType = null;
519520
return null;
520521
}
522+
521523
if (asExtensionAnyway)
522524
{
523525
var parameters = method.GetParameters();
526+
thisType = parameters[0].ParameterType;
524527
return AppendGetThisCS(false, parameters[0].ParameterType);
525528
}
529+
530+
thisType = method.DeclaringType;
526531
return AppendGetThisCS(method.IsStatic, method.DeclaringType);
527532
}
528533

Assets/jsb/Source/JSWorker.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ private void Start(JSContext ctx, JSValue value, string scriptPath)
9090
_runtime = runtime;
9191
_runtime.OnAfterDestroy += OnWorkerAfterDestroy;
9292
RegisterGlobalObjects();
93-
_runtime.ResolveModule(scriptPath);
93+
_runtime.EvalMain(scriptPath);
9494

9595
_thread = new Thread(new ThreadStart(Run));
9696
_thread.Priority = ThreadPriority.Lowest;

0 commit comments

Comments
 (0)