ialex32x / unity-jsb

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

Various cross-platform fixes, enable DynamicType parent, pending jobs improvements #78

Closed KurtGokhan closed 2 years ago

KurtGokhan commented 2 years ago

Summary of changes:

1 - WSA and WebGL native binaries were causing issue in build. Had to change their meta. 2 - Some fixes when JSB_UNITYLESS is defined even when we are inside UNITY_EDITOR 3 - Codegen & NetStandard issues (Probably we should unify define variables which are used across the project. NET_STANDARD_2_0 is correct but there is also NET_STANDARD_2_1 and NET_STANDARD, but they may not be available in all Unity versions) 4 - ~Added JSB_NEWTONSOFT_JSON flag. When using JSB_UNITYLESS inside Unity, System.Text.Json namespace isn't available by default. This is not a nice solution, so I am open to suggestions.~ Edit: Modified code to use Unity's own serializer if inside Unity, even if JSB_UNITYLESS is defined. 5 - Enabled back DynamicType prototype inheritance. This time without GC leaks. 6 - Improved ExecutePendingJob to check if there are pending jobs remaining. This is a weird one, because you would expect QuickJS to do this automatically. But some cases did not work. For example:

console.log('Executed');

Promise.resolve().then(() => {
  console.log('PostPromise Depth 1');

  Promise.resolve().then(() => {
    console.log('PostPromise Depth 2');

    Promise.resolve().then(() => {
      console.log('PostPromise Depth 3');

      Promise.resolve().then(() => {
        console.log('PostPromise Depth 4');
      });

      console.log('P4');
    });

    console.log('P3');
  });

  console.log('P2');
});

When this code executes, it shows the following in browser:

image

But in unity-jsb, it shows only up to "P3". It shows the rest in the next updates.

KurtGokhan commented 2 years ago

7 - Added another commit. This one adds JSB_RUNTIME_REFLECT_BINDING which will include jsb.editor.binding and jsb.editor.unity assemblies in the build. Because of heavy coupling with UnityEditor in those assemblies, JSB_UNITYLESS must also be defined for building without a problem.

ialex32x commented 2 years ago

The parentType still has a few issues which can't meet all cases. Because which types are exported are in a more static way for now for better type management (and d.ts generating). Some classes could be deliberately skipped for the binding process. For example, class A -> class B -> class C in C# could be exported as class A -> class C in typescript.

And another case is that types could be exported with StaticBind (because DynamicType is still available at runtime with StaticBind mode). A more flexible way is needed to implement parentType access in the future to cover these cases.