MightyPirates / OpenComputers

Home of the OpenComputers mod for Minecraft.
https://oc.cil.li
Other
1.59k stars 430 forks source link

[1.6.2.7] load() / loadfile() not working with Component API #2318

Closed Pizzn closed 7 years ago

Pizzn commented 7 years ago

Whenever I try to execute load()'ed code pointing to any of the Component API, I get "attempt to index a nil value". Does not happen if using Lua API. Additionally, loadfile() always returns nil, or the usual error if not providing any args.

Using OC 1.6.2.7, MC 1.10.2, Forge 2185.

mc-load

mc-load2

xarses commented 7 years ago

load() is the same as if you have executed a file from the command line(hint: dofile()) in that they don't have the helper modules pre-loaded like the lua shell has. To resolve this you should require("computer") or if you want to name it a specific value comp = require("computer").

P.S. You will also find that component isn't pre-loaded for you either

Pizzn commented 7 years ago

Thanks for the fast reply, but I should have mentioned that I get the exact same behaviour when loading the libs via require(). Using global variables in the script just prints a newline if executed.

mc-load3 mc-load4

magik6k commented 7 years ago

load by default passes current execution env, not passing any local variables, so your second screenshoot is expected behavior. Though it seems that there is bug in lua interpreter not passing current env in load* functions. @payonel

Real lua example

xarses commented 7 years ago

It's been bothering me, why do some of your screen shots show Lua 5.3 and others show 5.2? Is this a world that you've updated many times with? If so do you get the same result if you craft a fresh OpenOS floppy and a fresh CPU?

Pizzn commented 7 years ago

Yeah, the first screens were made in my actual world. But all cases were re-tested in a fresh world with OC as only mod, using a computer spawned by /oc_spawnComputer.

payonel commented 7 years ago

@magik6k thanks for pointing that out - I'll fix that

payonel commented 7 years ago

@pizzn the issue that @magik6k pointed out:

there is bug in lua interpreter not passing current env in load* functions

Is the only issue I see here, otherwise things work as expected.

payonel commented 7 years ago

d1b7cb977f3e42f158b066108e2c4d9ea4a0c3f4