ialex32x / unity-jsb

It brings Javascript runtime capability to Unity3D by integrating QuickJS.
MIT License
335 stars 41 forks source link

Subclass Of ScriptValue #89

Closed 0xF6 closed 2 years ago

0xF6 commented 2 years ago

Im trying to get ScriptValue in custom methods when it calling in js. (ScriptFunction is work corretly, ScriptValue not)

I see this: https://github.com/ialex32x/unity-jsb/blob/67be3c1be8b94d9e6f213c6602ffc2b75d8a1c13/Packages/cc.starlessnight.unity-jsb/Source/Binding/Values_match.cs#L130 But ScriptValue is sealed and ctor is not public\protected

It normal?

i try remove sealed and set ctor of ScriptValue as public, and try call from js C# fn:

public class SampleObject : ScriptValue { public SampleObject (ScriptContext context, JSValue jsValue) : base(context, jsValue) {} }
public class StaticTest {
    public static void Test(SampleObject o) {...}
}
require("Test").StaticTest.Test({ foo: "bar" });

And got expcetion in https://github.com/ialex32x/unity-jsb/blob/67be3c1be8b94d9e6f213c6602ffc2b75d8a1c13/Packages/cc.starlessnight.unity-jsb/Source/Binding/DynamicMethod.cs#L191 with message: Object of type 'QuickJS.ScriptValue' cannot be converted to type 'Test.SampleObject '.'

Therefore https://github.com/ialex32x/unity-jsb/blob/67be3c1be8b94d9e6f213c6602ffc2b75d8a1c13/Packages/cc.starlessnight.unity-jsb/Source/Binding/Values_match.cs#L130 Is a incorrect, there should be

return type == typeof(ScriptValue);

or (maybe need save check subClass, idk)

return type == typeof(ScriptValue) || type.IsSubclassOf(typeof(ScriptValue));
ialex32x commented 2 years ago

ScriptValue is changed to sealed because it's better to ensure a unique mapping between JSValue and ScriptValue (but few code are not updated corresponding to this change yet) If there are other concrete types inherited from ScriptValue, ScriptFunction as an example, there will be two mapping objects for a single JSValue simultaneously.

0xF6 commented 2 years ago

Okay, should I create a PR to fix at https://github.com/ialex32x/unity-jsb/blob/67be3c1be8b94d9e6f213c6602ffc2b75d8a1c13/Packages/cc.starlessnight.unity-jsb/Source/Binding/Values_match.cs#L130 to be able to get ScriptValue as an argument when calling a C# function from JS?

ialex32x commented 2 years ago

Of course, thanks a lot 👍