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

"API-fying" this?

Started by robhol, 28 January 2014 - 01:30 PM
robhol #1
Posted 28 January 2014 - 02:30 PM
https://gist.github.com/anonymous/f6e6a9a5674d81c0b57a (because the code tags on these forums are bad.)

Basically, this should be loadAPI-able.
Now, the issue is that "someFunction" ends up at myApi.myApi.foo.bar.

The "sub-apis" may conflict with certain default globals (so I can't just remove "myApi." from the definitions), and I've tried _G.myApi=myApi, also with getfenv(0).

Ideas?
Lyqyd #2
Posted 28 January 2014 - 02:46 PM
Can you show is the actual code? I'm fairly certain there's an easy way to work around the issue you're having, but the example code doesn't particularly make sense. The local myAPI table wouldn't be available after the API was loaded anyway.
robhol #3
Posted 28 January 2014 - 03:21 PM
Sorry, ignore the "local" - the table is there, it's just global. Just made a slight change to test and forgot to undo it in the gist-ed version.

Apart from that, the code itself has that general form (myApi.subApi.derpFunction) and there's actually nothing more to show. It establishes the tables perfectly when ran on its own, but ends up inside another table (created by loadAPI) when loaded.
Edited on 28 January 2014 - 02:22 PM
surferpup #4
Posted 28 January 2014 - 03:29 PM
(because the code tags on these forums are bad.)

Here is your code that you claim you can't post:

local myApi = {};
myApi.version = 1.0;

myApi.herp = {};
myApi.herp.derp = someFunction;

myApi.burp = {};

1 mouse click, one paste. Done
Edited on 28 January 2014 - 02:32 PM
robhol #5
Posted 28 January 2014 - 03:31 PM
Couldn't != didn't want to.

You could fix the "local " while you're at it. It's not supposed to be there. :P/>
Edited on 28 January 2014 - 02:33 PM
Lyqyd #6
Posted 28 January 2014 - 04:43 PM
So, all you'd need to do is ensure that the file name is "myAPI" and not use the myAPI table inside it, so it would be vaguely thus:


herp = {}
herp.derp = someFunction

Now, if you're saying that the problem is that "herp" is actually "term" or some other existing global, that is potentially troublesome. To get around this, you might try unloading and reloading the API. Other programs and APIs won't be expecting you to do this, so it could be a nasty nasty trick to play. A better approach might be to getfenv(1) to get the environment table your API is supposed to fill and directly place things into it. This will mean that it will only work when your API is loaded, and not when it is simply run.


local env = getfenv()

env.herp = {}
env.herp.derp = someFunction
surferpup #7
Posted 28 January 2014 - 05:26 PM
Couldn't != didn't want to.

Next time you have trouble, simply type the following:

[code]
… your code …
Edited on 29 January 2014 - 06:56 AM