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

Global vars 'undefined' in local scope after running any program. #3102

Open
numberlesstim opened this issue Mar 1, 2024 · 6 comments
Open
Milestone

Comments

@numberlesstim
Copy link

numberlesstim commented Mar 1, 2024

In a local scope directly after running a program all vars are 'undefined'. This does happen no matter what program you run. I stumbled over this using an if-block after running a program, but for simplicity i could reduce it further to this:

switch to 1. log "" to "foo.ks".
run foo. {print time.}

Throws an Error:
Undefined Variable Name 'time'.

Weirdly even global vars defined in that local scope are unknown aswell, but local vars work:

run foo. {global x is 1. print x.}

Undefined Variable Name 'x'.

run foo. {local y is 1. print y.}

1

edit: The following only worked due to config:clobberbuiltins being set to true, so those points no longer apply:
Further, it is even possible to overwrite any var globally on the core the code is run on until it is rebooted:

run foo. {set time to "Time for coffee".}
print time.

Time for coffee

After doing that, 'time' in this case behaves like any other var created by the user. It can even be unset.
As far as i can tell this works with any and all vars, even fun stuff like that:

function circ {
    print body:name + " has a sea-level circumference of " + round(2 * constant:pi * body:radius, 1) + " m.".
}
circ(). run foo. {set constant to lex("pi", 3.5).} circ().

Kerbin has a sea-level circumference of 3769911.2 m.
Kerbin has a sea-level circumference of 4200000 m.

@nuggreat
Copy link

nuggreat commented Mar 1, 2024

What version of kOS are you on because this {set time to "Time for coffee".} and this {set constant to lex("pi", 3.5).} should be throwing a compile time errors.

@numberlesstim
Copy link
Author

I'm running v1.4.0.0

@numberlesstim
Copy link
Author

I just realized i had config:clobberbuiltins set to true, so with that off the two points you mentioned no longer hold. The rest still applies though.

@nuggreat
Copy link

nuggreat commented Mar 1, 2024

Good to know I had to ask after the version because we have had a few people who have for one reason or another been on older versions without knowing it and updating fixed the issue.

I was able to reproduce the error on my end though my test cases ended up looking like this:

log "" to "foo.ks".
run foo. { global x is 1. print defined(x). } print defined(x).

and

//used the foo.ks from above
run foo. {print defined(time).} print defined(time).

and

log "global y is 2." to "bar.ks".
run bar. { print defined(y). } print defined(y).

In my tests the log was only run once and restarts occurred between attempts to remove the added global vars.
Both cases ended up printing

false
true

when they should be printing

true
true

I also tested running the tests as files and not commands typed into the terminal and got the expected result.
At a guess this is down to the terminal execution environment is in a strange state after run command and not fully settled until you have a prompt again.

@numberlesstim
Copy link
Author

Actually even with clobberbuiltins enabled this

run foo. {set time to "Time for coffee".}
print time.

should still not print Time for coffee but rather some TIMESTAMP(...), meaning in this setup it does not clobber locally but rather overrides globally.
So it does behave fundamentally different than without running a program first like

{set time to "Time for tea".}
print time.

which prints TIMESTAMP(...) as expected.

@nuggreat
Copy link

nuggreat commented Mar 2, 2024

the abnormal behavior in this case

{set time to "Time for tea".}
print time.

is that it prints TIMESTAMP(...) as the set should be clobbering time in the global scope as without a LOCAL TIME TO ... the SET defaults global.

Which some what counterintuitively means that means that

run foo. {set time to "Time for coffee".}
print time.

is behaving correctly and the other case does not. But as both cases involve clobbering built ins the behavior will always be rather abnormal as it was never intended that you could clobber built ins hence the default setting to prevent it.

@JonnyOThan JonnyOThan added this to the 1.5.0.0 milestone Aug 4, 2024
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