Posted 12 March 2016 - 02:45 PM
VERSION:
[indent=1]I say 1.7.4, but really it occurs through all versions of Computercraft.[/indent]
DESCRIPTION:
[indent=1]Issue 1:[/indent]
[indent=2]os.loadAPI doesn't handle files with extensions. When loading an api os.loadAPI doesn't strip the extention of the file being loaded when inserting it into the global table (_G), meaning the only way to access the api is by indexing the global table with the full file name i.e _G["filename.lua"].[/indent]
[indent=2]Solution: replace fs.getName with string.gsub(file, "(.*/)(.*)(%..*)$", "%2"), though really os.loadAPI should sanitize file names for other characters that are illegal name character[/indent]
[indent=1]Issue 2:[/indent]
[indent=2]os.loadAPI incorrectly copies the loaded files environment. When loading a file os.loadAPI peforms a shallow copy of the api's environment, meaning modifications of the api's variables by the program that imported it, will not be seen by the api.[/indent]
[indent=2]Solution: replace the shallow copy with a proxy table to the original api environment.[/indent]
REPRODUCTION STEPS:
[indent=1]Issue 1 Example:[/indent]
[indent=2]create the following files:
[indent=1]
Issue 2 Example:[/indent]
[indent=2]rename the file in the first example to just api
create the following files:
[indent=1]I say 1.7.4, but really it occurs through all versions of Computercraft.[/indent]
DESCRIPTION:
[indent=1]Issue 1:[/indent]
[indent=2]os.loadAPI doesn't handle files with extensions. When loading an api os.loadAPI doesn't strip the extention of the file being loaded when inserting it into the global table (_G), meaning the only way to access the api is by indexing the global table with the full file name i.e _G["filename.lua"].[/indent]
[indent=2]Solution: replace fs.getName with string.gsub(file, "(.*/)(.*)(%..*)$", "%2"), though really os.loadAPI should sanitize file names for other characters that are illegal name character[/indent]
[indent=1]Issue 2:[/indent]
[indent=2]os.loadAPI incorrectly copies the loaded files environment. When loading a file os.loadAPI peforms a shallow copy of the api's environment, meaning modifications of the api's variables by the program that imported it, will not be seen by the api.[/indent]
[indent=2]Solution: replace the shallow copy with a proxy table to the original api environment.[/indent]
REPRODUCTION STEPS:
[indent=1]Issue 1 Example:[/indent]
[indent=2]create the following files:
--name this file api.lua
variable = true
function make_false()
variable = false
end
os.loadAPI("api.lua") --should create table in _G called api, not api.lua[/indent]
api.make_false() --will error because api is nil
[/indent][indent=1]
Issue 2 Example:[/indent]
[indent=2]rename the file in the first example to just api
create the following files:
os.loadAPI("api")
api.make_false()
print(api.variable) --prints true, since it's a copy of the original
[/indent]