tarantool / tarantool/doc

Write a How-to guide about loading modules in the Tarantool application

Open
#3,518 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
CSS
Stars
15
Forks
49
Avg merge
1d 13h
Merged PRs (30d)
3

Description

Product: Tarantool
Root document:

SME: @ Totktonada

Details

Currently, information about loading modules is placed mostly in the API descriptions and looks a bit fragmented.
It might be convenient to make the How-to guide (in Modules, rocks and applications?) that has the common context for all the API members and describes specifics of loading modules in the Tarantool app.

  • RFC

  • More info about loaders:

$ cat source/loaders.rst 
.. vim: ts=4 sw=4 et

Loaders
=======

Introduction
------------

This section describes a machinery that works behind the `require()` call in Lua.

It partially repeats Lua 5.1 Reference Manual
(https://www.lua.org/manual/5.1/manual.html#5.3), but adds LuaJIT and Tarantool
specifics and describes everything from a point of a developer (how things are
implemented rather than how they bevahe).

require()
---------

The source of the function is in ``third_party/luajit/src/lib_package.c``, the
function is called ``lj_cf_package_require``.

There is a ``_LOADED`` table in the Lua registry, it holds already loaded
modules. This table is also accessible as ``package.loaded``.

``require()`` works basically this way:

1. Look at ``_LOADED[module_name]``. If it exists, return it.
2. Otherwise, for each loader in ``package.loaders``:
   * Call ``package.loaders[i](module_name)`` XXX

package.loaders
---------------



``src/lua/init.lua``

.. code-block:: lua
    -- loader_preload 1
    table.insert(package.loaders, 2, gen_loader_func(search_lua, load_lua))
    table.insert(package.loaders, 3, gen_loader_func(search_lib, load_lib))
    table.insert(package.loaders, 4, gen_loader_func(search_rocks_lua, load_lua))
    table.insert(package.loaders, 5, gen_loader_func(search_rocks_lib, load_lib))
    -- package.path   6
    -- package.cpath  7
    -- croot          8

package.path and package.cpath
------------------------------

tarantool_lua_setpaths(), MODULE_LUAPATH, MODULE_LIBPATH

package.searchpath() and package.search()
-----------------------------------------

``package.searchpath`` comes from Lua 5.2 (implemented in LuaJIT 2.1).

https://www.lua.org/manual/5.2/manual.html#pdf-package.searchpath

``package.search`` is introduced by tarantool.

package.searchroot() and package.setsearchroot()
------------------------------------------------

https://github.com/tarantool/tarantool/pull/4193

debug.__file__ and debug.__dir__
--------------------------------

https://github.com/tarantool/tarantool/pull/4260

More insane
-----------

Moonscript sets ``package.loaders[2]`` when you call ``require "moonscript"``.

https://github.com/tarantool/tarantool/issues/3136

How luarocks usually works?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the existing How-to page on Modules, rocks and applications and the API descriptions under reference_lua/other/. Read source/loaders.rst and the linked RFC to understand the loader flow and Tarantool-specific behavior. Done means a coherent How-to guide covering the common context and module-loading specifics that are currently fragmented across the API documentation.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua
Domain
documentation
Issue type
Documentation
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.