Physics "bug" ?

Hey guys I just was inspecting my code recently and noticed that I was using the physics library in a module which I didnt require physics into. I suspected I had made a global by mistake but came to a wierd conclusion. It seems that when you require physics into any variable, a key “physics” gets formed in the “_G” table.

Here is all you need to reproduce this behavior:

print(\_G["physics"]) -- Will print 'nil' -- Require physics local something = require("physics") -- Check '\_G' table print(\_G["physics"]) -- Prints 'Table' + some characters

I have submitted a bug report and I am using Corona 2013.1127

I don’t think this is a bug. Why should it be one?

You have to load the physics, because otherwise it would take memory even if you don’t need it.

So when you activate it Corona asumes that you want to use it in all modules and storyboard scenes of your app.

Therefor it sets a global variable for it, so you can use it in every case without calling it again every time.

That’s my explanation for this and I think it makes sense :wink:

Or why do you think that’s some kind of a bug?

I mean, your explanation does make sense, however when you require a module it is supposed to put it only in the package.loaded table and return it if you are assigning it to a variable, NOT make a global instance of it. I was just very surprised because this was not documented.

Hi @Into-It Games,

I don’t believe this is a bug. Did you check out last week’s tutorial on banishing globals, and how globals are treated in the _G table? That should shed some light on the issue.

http://www.coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

Regards,

Brent

Yes Brent I understand it however my question is why is it that when I call require(“physics”), a “physics” key gets formed in the _G table in addition to package.loaded (which is in _G table). This doesn’t happen to my own custom modules. Perhaps this will illustrate what I mean:

print(physics) -- prints 'nil' print(CoronaPhysics) -- prints 'nil' local CoronaPhysics = require("physics") print(physics) -- prints some table, why is there a \_G["physics"] value?? print(CoronaPhysics) -- prints some table print(myLibrary) -- prints 'nil' print(example) -- prints 'nil' local example = require("myLibrary") print(myLibrary) -- prints 'nil' as expected, unlike when I require physics print(example) -- prints some table as expected

Here is myLibary.lua, its just a table:

local M = {red = "red"} return M

Is the Corona physics library using the old “module(…, package.seeall)” syntax that pollutes the _G table?

I would think that if you do:

local something = require(“something”)

Then you should not see “something” in _G.   I did a quick test and the package.seeall doesn’t seem to be the culprit.

It’s not a bad bug but it certainly was an unexpected discovery

I don’t think this is a bug. Why should it be one?

You have to load the physics, because otherwise it would take memory even if you don’t need it.

So when you activate it Corona asumes that you want to use it in all modules and storyboard scenes of your app.

Therefor it sets a global variable for it, so you can use it in every case without calling it again every time.

That’s my explanation for this and I think it makes sense :wink:

Or why do you think that’s some kind of a bug?

I mean, your explanation does make sense, however when you require a module it is supposed to put it only in the package.loaded table and return it if you are assigning it to a variable, NOT make a global instance of it. I was just very surprised because this was not documented.

Hi @Into-It Games,

I don’t believe this is a bug. Did you check out last week’s tutorial on banishing globals, and how globals are treated in the _G table? That should shed some light on the issue.

http://www.coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

Regards,

Brent

Yes Brent I understand it however my question is why is it that when I call require(“physics”), a “physics” key gets formed in the _G table in addition to package.loaded (which is in _G table). This doesn’t happen to my own custom modules. Perhaps this will illustrate what I mean:

print(physics) -- prints 'nil' print(CoronaPhysics) -- prints 'nil' local CoronaPhysics = require("physics") print(physics) -- prints some table, why is there a \_G["physics"] value?? print(CoronaPhysics) -- prints some table print(myLibrary) -- prints 'nil' print(example) -- prints 'nil' local example = require("myLibrary") print(myLibrary) -- prints 'nil' as expected, unlike when I require physics print(example) -- prints some table as expected

Here is myLibary.lua, its just a table:

local M = {red = "red"} return M

Is the Corona physics library using the old “module(…, package.seeall)” syntax that pollutes the _G table?

I would think that if you do:

local something = require(“something”)

Then you should not see “something” in _G.   I did a quick test and the package.seeall doesn’t seem to be the culprit.

It’s not a bad bug but it certainly was an unexpected discovery