This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
TheOddByte's profile picture

Getting the global name of an API

Started by TheOddByte, 28 September 2014 - 08:54 AM
TheOddByte #1
Posted 28 September 2014 - 10:54 AM
Hey, I'm kinda wondering how to get the name of an API when it gets loaded with os.loadAPI, the reason is I want to add functions after it's been loaded.
Example, I have a file called 'foo' in the folder APIs. How could I in the file let it know that it's been loaded as 'foo'


--# I tested this, it didn't work
-- local sName = fs.getName( shell.getRunningProgram() )

--# foo file
function bar()
	print( "API loaded as " .. <API Name> )
end
Because then I want to be able to add a function like this

_G[<API Name>][<function name>] = function()
	print( "blah" )
end
Lignum #2
Posted 28 September 2014 - 11:21 AM
The problem is that shell is nil inside an API because there could be multiple shells running. You need to find a way to provide your API with the current shell. In this case you might as well pass the API's name over instead of a shell.
GlassOS #3
Posted 28 September 2014 - 11:31 AM
I don't see how you could look for your API maybe with fs someone could figure a cool file search program
TheOddByte #4
Posted 28 September 2014 - 11:40 AM
The problem is that shell is nil inside an API because there could be multiple shells running. You need to find a way to provide your API with the current shell. In this case you might as well pass the API's name over instead of a shell.
How do you mean? Can you give an example?
I don't see how you could look for your API maybe with fs someone could figure a cool file search program
Uhmm.. Are you sure you know what I meant? I'm not trying to locate my file, I'm trying to figure out how to get it's global name.
For example, when you load a file called 'foo' and it has a function called bar you call the function by using foo.bar(), but if someone were to call the file something else like blah then you would have to use it as blah.bar().
Bomb Bloke #5
Posted 28 September 2014 - 11:52 AM
I suspect that even with the shell API accessible, shell.getRunningProgram() would return the script loading the API, not the API itself. I wouldn't know, though.

It's also worth pointing out that first the API's code is executed, and then any "global" functions/etc it defined are loaded into _G. An API hence can't access itself (in the way that the script which loads it does) while it itself is loading, and thus can't search for itself by keeping tabs on _G.

When, how and from where are you wanting to load more functions into your API?
TheOddByte #6
Posted 28 September 2014 - 12:21 PM
I have a function in the API that basically loads another file that's supposed to add to the global table of the API.
I'm creating a Object API that's supposed to have a function that'll be able to load other objects into itself

os.loadAPI( "Object" )
Object.load( "Textbox" )
local textbox = Object.Textbox.new( ... ) -- Textbox now exists in the Object table etc.
...
Lyqyd #7
Posted 28 September 2014 - 12:24 PM
Why not require the user to use colon syntax, and use the table returned as the first argument to place the additional keys into?
Bomb Bloke #8
Posted 28 September 2014 - 12:31 PM
I was thinking of having the API find itself after being loaded:

local myPointer

function someFunction()
	-- Do stuff
end

function load(target)
	if not myPointer then for _,value in pairs(_G) do if type(value) == "table" and value.someFunction == someFunction then
		myPointer = value
		break
	end end end
	
	-- Load "target" into a function here.
	
	myPointer[target] = <loaded function>
end

… but colon syntax would certainly be simpler.
TheOddByte #9
Posted 28 September 2014 - 02:09 PM
Got it working, how could I have been so blind and not realized this simple way O.O

function load( self, path )
    if not fs.isDir( path ) then
        self[fs.getName( path )] = dofile( path )
    end
end
Thanks for the help everyone! :D/>
Bomb Bloke #10
Posted 28 September 2014 - 03:38 PM
Given this:

Object.load( Object, "obj" ) -- the first argument is what the Object API has been loaded as, the second is the path of the external object

… it may be worth noting that that function can also be called like this:

Object:load( "obj" ) -- the argument is the path of the external object

… without changing the "load" function from how you've written it. This is what the "colon syntax" refers to.
TheOddByte #11
Posted 28 September 2014 - 05:16 PM
- Snip -
Thanks, totally forgot to add that into the topic in AaU