Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Unity] Bug: new JsEnv() can't be always called on a separate thread in the Editor #1847

Open
3 tasks done
DoctorGester opened this issue Sep 23, 2024 · 0 comments
Open
3 tasks done
Assignees
Labels
bug Something isn't working Unity

Comments

@DoctorGester
Copy link

DoctorGester commented Sep 23, 2024

前置阅读 | Pre-reading

Puer的版本 | Puer Version

2.1.0

Unity的版本 | Unity Version

2023.1.13f1

发生在哪个平台 | Platform

Editor(mac)

错误信息 | Error Message

No response

问题重现 | Bug reproduce

This highly depends on installed packages, but JsEnv constructor calls TypeManager.InitArrayTypeId(); which in turn calls Puerts.Utils.GetExtensionMethodsOf(typeof(Array)). Certain packages seem to call Unity-specific methods in their static constructors which produces an error when called outside of the main thread.

We have this hack in the main thread before launching a Thread which creates JsEnv

    private void MaybeInitializePuertsHack() {
        // Workaround to run JsEnv on a separate thread, otherwise it tries to discover some extension methods
        //  of Array class which trigger static init of things which can only be initialized on the main thread, sigh...
        // Only seems to happen in the editor though
#if UNITY_EDITOR
        if (!initializedPuertsHack) {
            Puerts.Utils.GetExtensionMethodsOf(typeof(Array));
            initializedPuertsHack = true;
        }
#endif
    }

We don't need reflection at all because all of our methods are registered from the generated source bindings but it seems like there is no way to turn off discovery of extension methods specifically for Array class when in the editor. It also produces a huge multi-second delay for us.

image
@DoctorGester DoctorGester added bug Something isn't working Unity labels Sep 23, 2024
@DoctorGester DoctorGester changed the title [Unity] Bug: new JsEnv() can't be called on a separate thread in the Editor [Unity] Bug: new JsEnv() can't be always called on a separate thread in the Editor Sep 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Unity
Projects
None yet
Development

No branches or pull requests

2 participants