ncsoft / Unreal.js

Unreal.js: Javascript runtime built for UnrealEngine
Other
3.66k stars 348 forks source link

Error during cleanup permanently breaks hot-reload #203

Open mcclure opened 6 years ago

mcclure commented 6 years ago

Short description: If an error or exception throw occurs in the cleanup function returned by a script component, hot reloading will no longer work and the same error will be printed on every single script reload even if it is fixed in the code.

Demonstration: I am running Unreal 4.18.0 with the current UnrealJS from the Marketplace. I have a project with the following script as "extension-helloworld.js":

function main() {
    console.log("hello, world!!");
    return () => { // Cleanup
        return;
    };
}

module.exports = () => {
    let cleanup = main();
    return () => cleanup();
};

I try the following:

  1. I add another exclamation point to "hello, world!!"

    Result: I see "hello, world!!!" in the Javascript Console. This is expected.

  2. Inside the // cleanup function before the "return", I place throw new Exception()

    Result: I see "hello, world!!!" in the Javascript Console. This is expected.

  3. I add another exclamation point to "hello, world!!!".

    Result: I see in the Javascript console:

    Error: file:////Users/mcc/Documents/Unreal%20Projects/UnrealTSTemplate%204.18/Content/Scripts/extension-helloworld.js:4: exception!

    This also is expected. The cleanup function from step 2 is running and it a "throw".

  4. I remove the throw new Exception() from the // cleanup function.

    Result: I see in the Javascript console:

    Error: file:////Users/mcc/Documents/Unreal%20Projects/UnrealTSTemplate%204.18/Content/Scripts/extension-helloworld.js:4: exception!

    This is expected because there is a "throw" in the cleanup function from step 3, which runs when it loads my new corrected script.

  5. I add one more exclamation point to "hello, world!!!!".

    Result: I see in the Javascript console:

    Error: file:////Users/mcc/Documents/Unreal%20Projects/UnrealTSTemplate%204.18/Content/Scripts/extension-helloworld.js:4: exception!

    This is not expected. The "exception!" code is not in the cleanup function from step 4. It appears that if there is an error during cleanup, the cleanup function is removed from the list of cleanup functions to run. From this point on I cannot get my code to function except by closing and reopening Unreal.

Expected behavior:

It is very difficult when this bug starts occurring to understand what is wrong, as the same error keeps getting printed over and over even though the code containing the error has been fixed. I would like to recommend two things:

(1) When an exception is thrown/an error occurs during a cleanup function, print a message clearly stating "Error during cleanup:". This way, the message in the javascript console will be easier to understand.

(2) If an error occurs during a cleanup function, remove the cleanup function from the list of cleanup functions to run, so that hot-loading can continue to occur and the user will not have to close Unreal.

mcclure commented 6 years ago

Also, aside from the bug-- I am sorry, I am very confused, but I do not understand why the extension-helloworld.js script is running. It is not attached as a ScriptComponent to anything in the scene (I am using this sample project https://github.com/ConquestArrow/UnrealTSTemplate but there are also similar extension- files in the sample project for the https://github.com/ncsoft/Unreal.js repo). Can you explain, why does extension-helloworld.js run? Does UnrealJS just run any js file beginning with extension- in case it is an editor extension?

crocuis commented 6 years ago

Yes. When the JavascriptEditor Module starts, The script will be executed. https://github.com/ncsoft/Unreal.js-core/blob/master/Content/Scripts/editor.js