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

Scope seems to be considered differently 'on board' and 'in Archive' #3086

Open
lefouvert opened this issue May 11, 2023 · 4 comments
Open

Comments

@lefouvert
Copy link

Sorry if I dont do this correctly, it's my first time (and english is not my native language)

Step to reproduce :

  1. Have a boot file as this :
@LAZYGLOBAL OFF.

core:part:getmodule("kOSProcessor"):doevent("Open Terminal").

print "LoadError from Archive.".
runOncePath("0:/LoadError.ks").

foobar:error().

copyPath(path("0:/LoadError.ks"), path("1:/LoadError.ks")).

print "LoadError from volume 1.".
runOncePath("1:/LoadError.ks").

// no need to go further, bug before.

  1. Have a 'Class' file as this. Obviously, this file should be named 'LoadError.ks' and be at root of Archive :
@LAZYGLOBAL OFF.

{

    global foobar is LoadError().

    global function LoadError {
        return lexicon("error", error@).
    }

    local function error {
        print "something.".
    }

}

  1. Launch Craft.
  2. Enjoy.

Terminal says at the second runoncepath :
"Undefined Variable Name 'error'."

Without thoses outer braces (witch are legal, if I correctly understand), program don't stop, but i think it reveal a bug witch is a problem in more complex code.

It could be name redondancy, however, as I have coded a minifier who rename variable and function name, before load files in craft, i dont think it's the problem. (in my code, it says "Undefined Variable Name e1", when the source code was a declaration as 'local DeltaVLeft blahblahblah.', so the new name is correctly interpreted -> no name redondancy).

I notice if you don't run from archive before, it work fine.

I assume you would ask yourself why I want to load files AND from archive, AND from craft. It's because, the first run launch procedures witch analyse functions dependencies to load in craft only expected (and minified) files. The second one is to effectively load in the craft all usefull files and function needed to operate in space, out of the scope of any signal from Kerbin (because of remote tech).

Side question, is it possible to «unload» function, or better, all a file scope, as you can unset a variable ? If so, i didn't find how to without 'reboot' (but reboot clear terminal screen, and i don't want this happen).

I hope I did this report fine.
Ask question, if I can help for further information. Love on this team for this wonderfull playground witch is KOS.

@Dunbaratu
Copy link
Member

Out of curiosity, does the same error happen if you reverse the order and run it on volume 1 first, then on volume 0 second?

The reason for the question: I want to see if the problem is that the two volumes behave differently (which seems unlikely) or if it's just because any time the same program is from two different volumes will it always fail on whichever one is run second?

@nuggreat
Copy link

nuggreat commented May 11, 2023

I think something really off is going on with scoping when the file is run for the second time from the local volume. Specifically for some reason the local scope that contains the function error() is getting destroyed when the file is run for the second time from the local volume. This is a problem because once a global function of a given name exists declaring a new function with the same name doesn't remove the first declaration and it is always the first declaration that will be the one that executes.

In an attempt to prove this I changed the test case to be this

@LAZYGLOBAL OFF.

PRINT "running test from Archive.".
RUNONCEPATH("0:/test.ks").

PRINT "running from boot".
testFunc().//doesn't error

COPYPATH("0:/test.ks", "1:/test.ks").

PRINT "running test from volume 1.".
RUNONCEPATH("1:/test.ks").//errors here

and this as the executed file test.ks

@LAZYGLOBAL OFF.

{
    LOCAL testVar TO "test0".
    testFunc().
    GLOBAL FUNCTION testFunc {
        PRINT testVar.
    }
}

The most interesting thing about my case is that I don't get the "undefined var" error until the file is run for the second time from the local volume. Also per dunbaratu's question swapping executing from the local volume first as apposed to the archive didn't change the fact the second run throws an error.

I also found two workarounds to the issue

  1. remove the outer most pair of {} from test.ks in my case or LoadError.ks in the OPs
  2. change the function testFunc() in my case or LoadError() in the OPs to be local as apposed to global in scope, though in my case this also requires removing the testFunc() call from the boot file.

Also while not related to your issue this line of code core:part:getmodule("kOSProcessor"):doevent("Open Terminal"). got a visit from the redundant department of redundancy that you might want to address what with you bothering to use minifiyers.

@lefouvert
Copy link
Author

Thanks for taking a look to my issues. It's reassuring to know it's not because I made a obvious mistake but because something goes wrong.

core:part:getmodule("kOSProcessor"):doevent("Open Terminal").

LOL.
Didn't notice. Should be a residue of copy-paste modified at "to late to go to bed before work" AM. and not reread because in a "I don't really care" piece of code since it's erased-rewrite some seconds after boot with more minimalistic and specialized boot file once the full boot sequence (load boot config files, apply parameters, shearch appropriate "mission definition", search dependancies, minifiye, compile, load smallest, rewrite boot, start mission) is finished.
I will take a look for thoses old files I didn't check for a long time :) thank you ^^.

But hey, minifiye worth it since it's able to cut down my code from 60 KB to 18,5 KB, when complier 'only' crunch it to 26 KB ;) (And on board code have been read, re-read, checked, double checked, triple checked, shared, commented, etc ^^ )

@lefouvert
Copy link
Author

@Dunbaratu I realize i didn't mention, but all reports nuggreat made are the same in my case :

  • No outer braces seems to hide the issue. (but I detect this behavior in a piece of code where I can't do that)
  • It's at the second runOncePath code broke.
  • order doesn't matter, it's always the second whitch broke.

This report seems to invalidate the title of this issue, but as i dont know how to correctly rename it, I will let it as is until the true source is found ^^.

Interessting behavior I think is related :
Context : Craft read files related to ascent from Archive, and circularize files from onboard. Circularize files are minifiyed.

Flight report : I read all files to mix my soup, copy what i need in craft, rewrite a boot file whitch just runOncePath embeded files and call my "main" function, reboot (allow me to bypass the 'second' runoncepath error), launch.
All is ok until the end of the ascent where it stop, saying a variable whitch exists ONLY in circularize files is not declared.
Where it's fun, it's because none of the circularize function are called. Nothing. I just ask for an ascent. dot. A 'Print' after ascent dont even show, it stop just before.
If I dont minifiye code, I dont have this behavior, however, to be sure it's not a mistake i could make in my minifier, this light version just remove duplicates whitespaces.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants