Jump to content




"API-fying" this?


  • You cannot reply to this topic
6 replies to this topic

#1 robhol

  • Members
  • 182 posts

Posted 28 January 2014 - 02:30 PM

https://gist.github....9a5674d81c0b57a (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?

#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

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.

#3 robhol

  • Members
  • 182 posts

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 by robhol, 28 January 2014 - 03:22 PM.


#4 surferpup

  • Members
  • 286 posts
  • LocationUnited States

Posted 28 January 2014 - 03:29 PM

Quote

(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 by surferpup, 28 January 2014 - 03:32 PM.


#5 robhol

  • Members
  • 182 posts

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 by robhol, 28 January 2014 - 03:33 PM.


#6 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

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


#7 surferpup

  • Members
  • 286 posts
  • LocationUnited States

Posted 28 January 2014 - 05:26 PM

View Postrobhol, on 28 January 2014 - 03:31 PM, said:

Couldn't != didn't want to.

Next time you have trouble, simply type the following:

[code]
... your code ...
[/code]

Edited by surferpup, 29 January 2014 - 07:56 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users