Other package components | Tarantool
Reference Built-in modules reference Other package components

Other package components

All the Tarantool modules are, at some level, inside a package which, appropriately, is named package. There are also miscellaneous functions and variables which are outside all modules.

Name Use
tonumber64() Convert a string or a Lua number to a 64-bit integer
dostring() Parse and execute an arbitrary chunk of Lua code
package.path Where Tarantool looks for Lua additions
package.cpath Where Tarantool looks for C additions
package.loaded What Tarantool has already looked for and found
package.setsearchroot Set the root path for a directory search
package.searchroot Get the root path for a directory search
tonumber64(value)

Convert a string or a Lua number to a 64-bit integer. The input value can be expressed in decimal, binary (for example 0b1010), or hexadecimal (for example -0xffff). The result can be used in arithmetic, and the arithmetic will be 64-bit integer arithmetic rather than floating-point arithmetic. (Operations on an unconverted Lua number use floating-point arithmetic.) The tonumber64() function is added by Tarantool; the name is global.

Example:

tarantool> type(123456789012345), type(tonumber64(123456789012345))
---
- number
- number
...
tarantool> i = tonumber64('1000000000')
---
...
tarantool> type(i), i / 2, i - 2, i * 2, i + 2, i % 2, i ^ 2
---
- number
- 500000000
- 999999998
- 2000000000
- 1000000002
- 0
- 1000000000000000000
...

Warning: There is an underlying LuaJIT library that operates with C rules. Therefore you should expect odd results if you compare unsigned and signed (for example 0ULL > -1LL is false), or if you use numbers outside the 64-bit integer range (for example 9223372036854775808LL is negative). Also you should be aware that type(number-literal-ending-in-ULL) is cdata, not a Lua arithmetic type, which prevents direct use with some functions in Lua libraries such as math. See the LuaJIT reference and look for the phrase “64 bit integer arithmetic”. and the phrase “64 bit integer comparison”. Or see the comments on Issue#4089.

dostring(lua-chunk-string[, lua-chunk-string-argument ...])

Parse and execute an arbitrary chunk of Lua code. This function is mainly useful to define and run Lua code without having to introduce changes to the global Lua environment.

Parameters:
  • lua-chunk-string (string) – Lua code
  • lua-chunk-string-argument (lua-value) – zero or more scalar values which will be appended to, or substitute for, items in the Lua chunk.
Return:

whatever is returned by the Lua code chunk.

Possible errors: If there is a compilation error, it is raised as a Lua error.

Example:

tarantool> dostring('abc')
---
error: '[string "abc"]:1: ''='' expected near ''<eof>'''
...
tarantool> dostring('return 1')
---
- 1
...
tarantool> dostring('return ...', 'hello', 'world')
---
- hello
- world
...
tarantool> dostring([[
         >   local f = function(key)
         >     local t = box.space.tester:select{key}
         >     if t ~= nil then
         >       return t[1]
         >     else
         >       return nil
         >     end
         >   end
         >   return f(...)]], 1)
---
- null
...
package.path

This is a string that Tarantool uses to search for Lua modules, especially important for require(). See Modules, rocks and applications.

package.cpath

This is a string that Tarantool uses to search for C modules, especially important for require(). See Modules, rocks and applications.

package.loaded

This is a string that shows what Lua or C modules Tarantool has loaded, so that their functions and members are available. Initially it has all the pre-loaded modules, which don’t need require().

package.setsearchroot([search-root])

Set the search root. The search root is the root directory from which dependencies are loaded.

Parameters:
  • search-root (string) – the path. Default = current directory.

The search-root string must contain a relative or absolute path. If it is a relative path, then it will be expanded to an absolute path. If search-root is omitted, or is box.NULL, then the search root is reset to the current directory, which is found with debug.sourcedir().

Example:

Suppose that a Lua file myapp/init.lua is the project root.
Suppose the current path is /home/tara.
Add this as the first line of myapp/init.lua:
package.setsearchroot()
Start the project with
$ tarantool myapp/init.lua
The search root will be the default, made absolute: /home/tara/myapp. Within the Lua application all dependencies will be searched relative to /home/tara/myapp.

package.searchroot()

Return a string with the current search root. After package.setsearchroot('/home') the returned string will be /home'.

Found what you were looking for?
Feedback