Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/VM/include/lualib.h
2725 views
1
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
2
// This code is based on Lua 5.x implementation licensed under MIT License; see lua_LICENSE.txt for details
3
#pragma once
4
5
#include "lua.h"
6
7
#define luaL_error(L, fmt, ...) luaL_errorL(L, fmt, ##__VA_ARGS__)
8
#define luaL_typeerror(L, narg, tname) luaL_typeerrorL(L, narg, tname)
9
#define luaL_argerror(L, narg, extramsg) luaL_argerrorL(L, narg, extramsg)
10
11
struct luaL_Reg
12
{
13
const char* name;
14
lua_CFunction func;
15
};
16
typedef struct luaL_Reg luaL_Reg;
17
18
LUALIB_API void luaL_register(lua_State* L, const char* libname, const luaL_Reg* l);
19
LUALIB_API int luaL_getmetafield(lua_State* L, int obj, const char* e);
20
LUALIB_API int luaL_callmeta(lua_State* L, int obj, const char* e);
21
LUALIB_API l_noret luaL_typeerrorL(lua_State* L, int narg, const char* tname);
22
LUALIB_API l_noret luaL_argerrorL(lua_State* L, int narg, const char* extramsg);
23
LUALIB_API const char* luaL_checklstring(lua_State* L, int numArg, size_t* l);
24
LUALIB_API const char* luaL_optlstring(lua_State* L, int numArg, const char* def, size_t* l);
25
LUALIB_API double luaL_checknumber(lua_State* L, int numArg);
26
LUALIB_API double luaL_optnumber(lua_State* L, int nArg, double def);
27
28
LUALIB_API int luaL_checkboolean(lua_State* L, int narg);
29
LUALIB_API int luaL_optboolean(lua_State* L, int narg, int def);
30
31
LUALIB_API int luaL_checkinteger(lua_State* L, int numArg);
32
LUALIB_API int64_t luaL_checkinteger64(lua_State* L, int numArg);
33
LUALIB_API int luaL_optinteger(lua_State* L, int nArg, int def);
34
LUALIB_API int64_t luaL_optinteger64(lua_State* L, int nArg, int64_t def);
35
LUALIB_API unsigned luaL_checkunsigned(lua_State* L, int numArg);
36
LUALIB_API unsigned luaL_optunsigned(lua_State* L, int numArg, unsigned def);
37
38
LUALIB_API const float* luaL_checkvector(lua_State* L, int narg);
39
LUALIB_API const float* luaL_optvector(lua_State* L, int narg, const float* def);
40
41
LUALIB_API void luaL_checkstack(lua_State* L, int sz, const char* msg);
42
LUALIB_API void luaL_checktype(lua_State* L, int narg, int t);
43
LUALIB_API void luaL_checkany(lua_State* L, int narg);
44
45
LUALIB_API int luaL_newmetatable(lua_State* L, const char* tname);
46
LUALIB_API void* luaL_checkudata(lua_State* L, int ud, const char* tname);
47
48
LUALIB_API void* luaL_checkbuffer(lua_State* L, int narg, size_t* len);
49
50
LUALIB_API void luaL_where(lua_State* L, int lvl);
51
LUALIB_API LUA_PRINTF_ATTR(2, 3) l_noret luaL_errorL(lua_State* L, const char* fmt, ...);
52
53
LUALIB_API int luaL_checkoption(lua_State* L, int narg, const char* def, const char* const lst[]);
54
55
LUALIB_API const char* luaL_tolstring(lua_State* L, int idx, size_t* len);
56
57
LUALIB_API lua_State* luaL_newstate(void);
58
59
LUALIB_API const char* luaL_findtable(lua_State* L, int idx, const char* fname, int szhint);
60
61
LUALIB_API const char* luaL_typename(lua_State* L, int idx);
62
63
// wrapper for making calls from yieldable C functions
64
LUALIB_API int luaL_callyieldable(lua_State* L, int nargs, int nresults);
65
66
LUALIB_API void luaL_traceback(lua_State* L, lua_State* L1, const char* msg, int level);
67
68
/*
69
** ===============================================================
70
** some useful macros
71
** ===============================================================
72
*/
73
74
#define luaL_argcheck(L, cond, arg, extramsg) ((void)((cond) ? (void)0 : luaL_argerror(L, arg, extramsg)))
75
#define luaL_argexpected(L, cond, arg, tname) ((void)((cond) ? (void)0 : luaL_typeerror(L, arg, tname)))
76
77
#define luaL_checkstring(L, n) (luaL_checklstring(L, (n), NULL))
78
#define luaL_optstring(L, n, d) (luaL_optlstring(L, (n), (d), NULL))
79
80
#define luaL_getmetatable(L, n) (lua_getfield(L, LUA_REGISTRYINDEX, (n)))
81
82
#define luaL_opt(L, f, n, d) (lua_isnoneornil(L, (n)) ? (d) : f(L, (n)))
83
84
// generic buffer manipulation
85
86
struct luaL_Strbuf
87
{
88
char* p; // current position in buffer
89
char* end; // end of the current buffer
90
lua_State* L;
91
struct TString* storage;
92
char buffer[LUA_BUFFERSIZE];
93
};
94
typedef struct luaL_Strbuf luaL_Strbuf;
95
96
// compatibility typedef: this type is called luaL_Buffer in Lua headers
97
// renamed to luaL_Strbuf to reduce confusion with internal VM buffer type
98
typedef struct luaL_Strbuf luaL_Buffer;
99
100
// when internal buffer storage is exhausted, a mutable string value 'storage' will be placed on the stack
101
// in general, functions expect the mutable string buffer to be placed on top of the stack (top-1)
102
// with the exception of luaL_addvalue that expects the value at the top and string buffer further away (top-2)
103
104
#define luaL_addchar(B, c) ((void)((B)->p < (B)->end || luaL_prepbuffsize(B, 1)), (*(B)->p++ = (char)(c)))
105
#define luaL_addstring(B, s) luaL_addlstring(B, s, strlen(s))
106
107
LUALIB_API void luaL_buffinit(lua_State* L, luaL_Strbuf* B);
108
LUALIB_API char* luaL_buffinitsize(lua_State* L, luaL_Strbuf* B, size_t size);
109
LUALIB_API char* luaL_prepbuffsize(luaL_Buffer* B, size_t size);
110
LUALIB_API void luaL_addlstring(luaL_Strbuf* B, const char* s, size_t l);
111
LUALIB_API void luaL_addvalue(luaL_Strbuf* B);
112
LUALIB_API void luaL_addvalueany(luaL_Strbuf* B, int idx);
113
LUALIB_API void luaL_pushresult(luaL_Strbuf* B);
114
LUALIB_API void luaL_pushresultsize(luaL_Strbuf* B, size_t size);
115
116
// builtin libraries
117
LUALIB_API int luaopen_base(lua_State* L);
118
119
#define LUA_COLIBNAME "coroutine"
120
LUALIB_API int luaopen_coroutine(lua_State* L);
121
122
#define LUA_TABLIBNAME "table"
123
LUALIB_API int luaopen_table(lua_State* L);
124
125
#define LUA_OSLIBNAME "os"
126
LUALIB_API int luaopen_os(lua_State* L);
127
128
#define LUA_STRLIBNAME "string"
129
LUALIB_API int luaopen_string(lua_State* L);
130
131
#define LUA_BITLIBNAME "bit32"
132
LUALIB_API int luaopen_bit32(lua_State* L);
133
134
#define LUA_BUFFERLIBNAME "buffer"
135
LUALIB_API int luaopen_buffer(lua_State* L);
136
137
#define LUA_UTF8LIBNAME "utf8"
138
LUALIB_API int luaopen_utf8(lua_State* L);
139
140
#define LUA_MATHLIBNAME "math"
141
LUALIB_API int luaopen_math(lua_State* L);
142
143
#define LUA_DBLIBNAME "debug"
144
LUALIB_API int luaopen_debug(lua_State* L);
145
146
#define LUA_VECLIBNAME "vector"
147
LUALIB_API int luaopen_vector(lua_State* L);
148
149
#define LUA_INTLIBNAME "integer"
150
LUALIB_API int luaopen_integer(lua_State* L);
151
152
// open all builtin libraries
153
LUALIB_API void luaL_openlibs(lua_State* L);
154
155
// sandbox libraries and globals
156
LUALIB_API void luaL_sandbox(lua_State* L);
157
LUALIB_API void luaL_sandboxthread(lua_State* L);
158
159