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

Commit

Permalink
fully support of delegate with by-ref parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
ialex32x committed Sep 27, 2021
1 parent 58ec619 commit c584aec
Show file tree
Hide file tree
Showing 15 changed files with 560 additions and 469 deletions.
86 changes: 71 additions & 15 deletions Assets/Examples/Source/BasicRun.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,90 @@
using QuickJS;
using QuickJS.Native;
using System.Collections;
using System.Collections.Generic;

namespace Example
{
using UnityEngine;

public class BasicRun : MonoBehaviour
{
public bool execRuntime;
public enum RunCase
{
None,
QuickJSCodebase,
Codegen,
}
public RunCase runCase;

unsafe void Start()
{
if (execRuntime)
switch (runCase)
{
var rt = JSApi.JS_NewRuntime();
JSMemoryUsage s;
JSApi.JS_ComputeMemoryUsage(rt, &s);
Debug.Log($"test {rt}: {s.malloc_count} {s.malloc_size} {s.malloc_limit}");
var ctx = JSApi.JS_NewContext(rt);
Debug.Log("test:" + ctx);
case RunCase.QuickJSCodebase:
{
var rt = JSApi.JS_NewRuntime();
JSMemoryUsage s;
JSApi.JS_ComputeMemoryUsage(rt, &s);
Debug.Log($"test {rt}: {s.malloc_count} {s.malloc_size} {s.malloc_limit}");
var ctx = JSApi.JS_NewContext(rt);
Debug.Log("test:" + ctx);

// // // JSApi.JS_AddIntrinsicOperators(ctx);
// // var obj = JSApi.JS_NewObject(ctx);
// // JSApi.JS_FreeValue(ctx, obj);

// // // JSApi.JS_AddIntrinsicOperators(ctx);
// // var obj = JSApi.JS_NewObject(ctx);
// // JSApi.JS_FreeValue(ctx, obj);
JSApi.JS_FreeContext(ctx);
JSApi.JS_FreeRuntime(rt);
Debug.Log("it's a good day");
GameObject.CreatePrimitive(PrimitiveType.Cube);
break;
}
case RunCase.Codegen:
{
var options = new Dictionary<string, string>();
System.Reflection.MethodInfo Call = null;
using (var p = System.CodeDom.Compiler.CodeDomProvider.CreateProvider("cs", options))
{
var compilerParameters = new System.CodeDom.Compiler.CompilerParameters();
compilerParameters.GenerateInMemory = true;
compilerParameters.TreatWarningsAsErrors = false;
compilerParameters.CompilerOptions = "-unsafe";
compilerParameters.ReferencedAssemblies.Add(typeof(Debug).Assembly.Location);
var result = p.CompileAssemblyFromSource(compilerParameters, @"
using UnityEngine;
namespace _Hidden {
public static class Foo {
public static unsafe void Call(ref int a0) {
Debug.Log(""Hello "" + a0);
a0 += 1;
}
}
}
");
if (result.Errors.HasErrors)
{
foreach (var err in result.Errors)
{
Debug.LogError(err);
}
}
else
{
var Foo = result.CompiledAssembly.GetType("_Hidden.Foo");
Call = Foo.GetMethod("Call");
}
}

JSApi.JS_FreeContext(ctx);
JSApi.JS_FreeRuntime(rt);
Debug.Log("it's a good day");
GameObject.CreatePrimitive(PrimitiveType.Cube);
if (Call != null)
{
var v = 99;
var ps = new object[] { v };
Call.Invoke(null, ps);
Debug.Log($"Call: {ps[0]}");
}
break;
}
}
}
}
Expand Down
15 changes: 14 additions & 1 deletion Assets/Examples/Source/DelegateTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,32 @@ namespace Example
{
public delegate int WithByRefParametersCallback(int b, ref int a, out int v);

public delegate void WithByRefParametersCallback2(ref UnityEngine.Vector3 v);

[JSType]
public class DelegateTest
{
public WithByRefParametersCallback complexCall;
public WithByRefParametersCallback2 complexCall2;

public void TestComplexCall()
{
if (complexCall != null)
{
int b = 1;
int a = 2;
int v;
UnityEngine.Debug.Log($"TestComplexCall (before): b={b} a={a}");
int r = complexCall(b, ref a, out v);
UnityEngine.Debug.Log($"TestComplexCall: b={b} a={a} v={v} r={r}");
UnityEngine.Debug.Log($"TestComplexCall (after): b={b} a={a} v={v} r={r}");
}

if (complexCall2 != null)
{
var v = new UnityEngine.Vector3(1f, 2f, 3f);
UnityEngine.Debug.Log($"TestComplexCall2 (before): v={v}");
complexCall2(ref v);
UnityEngine.Debug.Log($"TestComplexCall2 (after): v={v}");
}
}

Expand Down
2 changes: 0 additions & 2 deletions Assets/Examples/Source/Editor/CustomBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ public override void OnPreExporting(BindingManager bindingManager)
bindingManager.AddExportedType(typeof(TWrapper<Vector3>));
bindingManager.AddExportedType(typeof(DisposableObject)).SetDisposable();

bindingManager.CollectRefectedDelegateTemplates(typeof(CustomReflectBindDelegates));

#if CUSTOM_DEF_FOO && UNITY_EDITOR
bindingManager.AddExportedType(typeof(FOO)).AddRequiredDefines("CUSTOM_DEF_FOO", "UNITY_EDITOR")
#if CUSTOM_DEF_PROP
Expand Down
105 changes: 0 additions & 105 deletions Assets/Examples/Source/Editor/CustomReflectBindDelegates.cs

This file was deleted.

11 changes: 0 additions & 11 deletions Assets/Examples/Source/Editor/CustomReflectBindDelegates.cs.meta

This file was deleted.

4 changes: 4 additions & 0 deletions Assets/Generated/Typings/jsb.autogen.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19705,6 +19705,7 @@ declare module "Example" {
declare module "Example" {
import * as jsb from "jsb";
import { Object, Array } from "System";
import { Vector3 } from "UnityEngine";
class DelegateTest extends Object {
constructor()
TestComplexCall(): void
Expand All @@ -19723,6 +19724,9 @@ declare module "Example" {
complexCall(op: "get"): (b: number, a: jsb.Ref<number>, v: jsb.Out<number>) => number
complexCall(op: "add" | "remove" | "set", fn?: (b: number, a: jsb.Ref<number>, v: jsb.Out<number>) => number): void
complexCall(op: "add" | "remove" | "set" | "get", fn?: (b: number, a: jsb.Ref<number>, v: jsb.Out<number>) => number): (b: number, a: jsb.Ref<number>, v: jsb.Out<number>) => number | void
complexCall2(op: "get"): (v: jsb.Ref<Vector3>) => void
complexCall2(op: "add" | "remove" | "set", fn?: (v: jsb.Ref<Vector3>) => void): void
complexCall2(op: "add" | "remove" | "set" | "get", fn?: (v: jsb.Ref<Vector3>) => void): (v: jsb.Ref<Vector3>) => void | void
actionFieldRW(op: "get"): () => void
actionFieldRW(op: "add" | "remove" | "set", fn?: () => void): void
actionFieldRW(op: "add" | "remove" | "set" | "get", fn?: () => void): () => void | void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ namespace QuickJS.Binding
{
public interface IBindingCallback
{
void Begin(BindingManager bindingManager);
void BeginStaticModule(string moduleName, int capacity);
void AddTypeReference(string moduleName, TypeBindingInfo typeBindingInfo);
void EndStaticModule(string moduleName);

void AddDelegate(DelegateBridgeBindingInfo bindingInfo);
void End();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace QuickJS.Binding
public class ReflectBindingCallback : IBindingCallback
{
private ScriptRuntime _runtime;
private BindingManager _bindingManager;
private Module.ProxyModuleRegister _moduleReg;

public ReflectBindingCallback(ScriptRuntime runtime)
Expand Down Expand Up @@ -46,9 +47,18 @@ public ReflectBindingCallback(ScriptRuntime runtime)
#endif
}

public void Begin(BindingManager bindingManager)
{
_bindingManager = bindingManager;
}

public void End()
{
}

public void BeginStaticModule(string moduleName, int capacity)
{
_moduleReg = _runtime.AddStaticModuleProxy(moduleName);
_moduleReg = _runtime.AddStaticModuleProxy(moduleName);
// _moduleReg = new Module.ProxyModuleRegister(_runtime);
}

Expand All @@ -65,11 +75,17 @@ public void EndStaticModule(string moduleName)
public void AddDelegate(DelegateBridgeBindingInfo bindingInfo)
{
var typeDB = _runtime.GetTypeDB();
var method = bindingInfo.reflect;

foreach (var delegateType in bindingInfo.types)
{
typeDB.AddDelegate(delegateType, method);
if (!typeDB.ContainsDelegate(delegateType))
{
var method = _bindingManager.GetReflectedDelegateMethod(bindingInfo.returnType, bindingInfo.parameters);
if (method != null)
{
typeDB.AddDelegate(delegateType, method);
}
}
}

}
Expand Down
Loading

0 comments on commit c584aec

Please sign in to comment.