(1 Kb)
Updated: 05/21/19 03:03 PM
Elsweyr (5.0.5)
Updated:05/21/19 03:03 PM
Created:03/02/14 04:10 PM
Monthly downloads:30,992
Total downloads:427,889
LibStub  Popular! (More than 5000 hits)
Version: 1.0 r6
by: sirinsidiator, Seerah
Addon Authors: LibStub is no longer needed if you plan to write a new library.
The game can manage the versioning itself, so just write your library like you would any other addon and make sure you add the "IsLibrary" and "AddOnVersion" directive.
## AddOnVersion: 6
## IsLibrary: true
Check the wiki for more more details about the directives.

LibStub was written for World of Warcraft by Kaelten, Cladhaire, ckknight, Mikk, Ammo, Nevcairiel and joshborke. It was ported to Elder Scrolls Online by Seerah. The below description was copied from WowAce with a few minor revisions.

LibStub is a minimalistic versioning library that allows other libraries to easily register themselves and upgrade. It is meant to be a cross-community library sharing system.

LibStub is hereby placed in the Public Domain

LibStub-1.0 API

:GetLibrary(major [, silent])
The table instance of a registered library or nil if not found and the minor version of the library as the second return value.
The name of the library you are requesting
(Optional) Suppresses errors when the library is not found. You can pass LibStub.SILENT for better readability
An iterator over the registered major libraries.
:NewLibrary(major , minor)
The table to be used by the library as well as the minor version of the previously registered library, if any.
The name of the library you are requesting
The minor for the registering library

Who uses LibStub?
... and hopefully many more to come!

How to include LibStub in a library or addon

  • get a copy of the current version
  • copy LibStub.lua into your library's folder
  • set up your <library>.txt file to load LibStub.lua (in case your library supports being installed standalone)
  • don't set LibStub as OptionalDependsOn

  • get a copy of the current version
  • copy LibStub.lua into your addon's folder or a subfolder of it
  • set up your <addon>.txt file to load LibStub.lua
  • don't set LibStub as OptionalDependsOn

Basic Example

Lua Code:
  1. local lib = LibStub:NewLibrary("MyLibrary-1.0", 1)
  3. if not lib then
  4.   return    -- already loaded and no upgrade necessary
  5. end
  7. function lib:SomeFunction()
  8.   -- do stuff here
  9. end
  11. function lib:SomeOtherFunction()
  12.   -- do other stuff here
  13. end
1.0 r6
- added IsLibrary flag to manifest
- api bump (see below)

1.0 r5
- api bump (for all the poor people who don't like to check a checkbox)

1.0 r4
- added a named constant for the silent flag as proposed by merlight

1.0 r3
- added support for fractional numbers in minor library version as proposed by merlight

1.0 r2
- fixed error handling

1.0 r1
- updated version number in LibStub.txt to match internal Major, minor revisions (1.0 r1 instead of 1.0.1)
Archived Files (6)
File Name
1.0 r5
11/02/18 08:08 AM
1.0 r4
07/23/15 02:57 PM
1.0 r3
06/14/15 12:55 PM
1.0 r2
07/13/14 09:24 PM
1.0 r1
03/14/14 07:29 PM
03/02/14 04:10 PM

Post A Reply Comment Options
Unread 06/30/14, 11:18 AM  
AddOn Author - Click to view AddOns

Forum posts: 281
File comments: 26
Uploads: 3
Need some info on LibStub usage

Since I was never part of the WoW Addon community, so I had some questions regarding how exactly libStub works.
The features I could identify so far:
If you have more then instance of the same library (local copy and several embedded ones), Stub makes certain only the most current stays in memory.
That also means some minor loadigntiem increase, as already loaded embedded libs do not have thier code executed again.
It also keeps the global namespace clean. Nothing about the lib needs to be globally exposed, as libStub is globaly exposed and can hand out references.

Now to my questions (specifically towards the examples):

    local lib = LibStub:NewLibrary("MyLibrary-1.0", 1)

    if not lib then
      return    -- already loaded and no upgrade necessary
As I understand it, this will give you either:
A reference to a libs Table to assign your libs functions too.
Or nil/false in case this lib (with same or higher version) has already been run
Here you check if the return values is nil or false and then just cancel the .lua files execution (hence prevent older version from overwriting newer ones and save some processing time).

    function lib:SomeFunction()
      -- do stuff here

    function lib:SomeOtherFunction()
      -- do other stuff here
Here you assign two functions using this variant of the Lua Namespace Syntax:

Could I also assign them like this:
    local function SomeFunction()
      -- do stuff here

    local function SomeOtherFunction()
      -- do other stuff here
lib.SomeFunction = SomeFunction
lib.SomeOtherFunction = SomeOtherFunction
:GetLibrary(major [, silent])
This will always only get me a refence to the highest version of hte library currently loaded.
But will this be a clone of the table currently registerd in LibStub, or the real table (wich means I have to look out not to assign anything).

Also what happens with the reference if a never version is loaded only after I called GetLibrary? Would the functions calls still target the previous version of the lib, or would even that "old" reference link to the new functions?
I generally call it in the OnLoaded event only, but I don't know if others might call it in code.

You also say "don't set LibStub as OptionalDependsOn", but I wonder why?
Is it not properly versioning itself?
Should I install a local copy of libStub? (the idea is that usually local copies are more likely up to date then embedded ones; and with optional dependencies libStub can get the local copy first).
What happens if a local copy is loaded by pure chance before the last embedded is run?
What if the local copy runs last by pure chance?
Last edited by zgrssd : 06/30/14 at 11:20 AM.
Report comment to moderator  
Reply With Quote
Post A Reply

Category Jump: