Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/Require/include/Luau/Require.h
2727 views
1
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
2
#pragma once
3
4
#include "lua.h"
5
6
#include <stdbool.h>
7
#include <stddef.h>
8
9
////////////////////////////////////////////////////////////////////////////////
10
//
11
// Require-by-string assumes that the context in which it is embedded adheres to
12
// a particular structure.
13
//
14
// Each component in a require path either represents a module or a directory.
15
// Modules contain Luau code, whereas directories serve solely as organizational
16
// units. For the purposes of navigation, both modules and directories are
17
// functionally identical: modules and directories can both have children, which
18
// could themselves be modules or directories, and both types can have at most
19
// one parent, which could also be either a module or a directory.
20
//
21
// Without more context, it is impossible to tell which components in a given
22
// path "./foo/bar/baz" are modules and which are directories. To provide this
23
// context, the require-by-string runtime library must be opened with a
24
// luarequire_Configuration object, which defines the navigation behavior of the
25
// context in which Luau is embedded.
26
//
27
// Calls to to_parent and to_child signal a move up or down the context's
28
// hierarchy. The context is expected to maintain an internal state so that
29
// when is_module_present is called, require-by-string can determine whether it
30
// is currently pointing at a module or a directory.
31
//
32
// In a conventional filesystem context, "modules" map either to *.luau files or
33
// to directories on disk containing an init.luau file, whereas "directories"
34
// map to directories on disk not containing an init.luau file. In a more
35
// abstract context, a module and a directory could be represented by any
36
// nestable code unit and organizational unit, respectively.
37
//
38
// Require-by-string's runtime behavior can be additionally be configured in
39
// configuration files, such as .luaurc or .config.luau files in a filesystem
40
// context. The presence of a configuration file in the current context is
41
// signaled by the get_config_status function. Both modules and directories can
42
// contain configuration files; however, note that a given configuration file's
43
// scope is limited to the descendants of the module or directory in which it
44
// resides. In other words, when searching for a relevant configuration file for
45
// a given module, the search begins at the module's parent context and proceeds
46
// up the hierarchy from there, resolving to the first configuration file found.
47
//
48
////////////////////////////////////////////////////////////////////////////////
49
50
typedef enum luarequire_NavigateResult
51
{
52
NAVIGATE_SUCCESS,
53
NAVIGATE_AMBIGUOUS,
54
NAVIGATE_NOT_FOUND
55
} luarequire_NavigateResult;
56
57
// Functions returning WRITE_SUCCESS are expected to set their size_out argument
58
// to the number of bytes written to the buffer. If WRITE_BUFFER_TOO_SMALL is
59
// returned, size_out should be set to the required buffer size.
60
typedef enum luarequire_WriteResult
61
{
62
WRITE_SUCCESS,
63
WRITE_BUFFER_TOO_SMALL,
64
WRITE_FAILURE
65
} luarequire_WriteResult;
66
67
// Represents whether a configuration file is present, and if so, its syntax.
68
typedef enum luarequire_ConfigStatus
69
{
70
CONFIG_ABSENT,
71
CONFIG_AMBIGUOUS, // Signals the presence of multiple configuration files.
72
CONFIG_PRESENT_JSON,
73
CONFIG_PRESENT_LUAU,
74
} luarequire_ConfigStatus;
75
76
typedef struct luarequire_Configuration
77
{
78
// Returns whether requires are permitted from the given chunkname.
79
bool (*is_require_allowed)(lua_State* L, void* ctx, const char* requirer_chunkname);
80
81
// Resets the internal state to point at the requirer module.
82
luarequire_NavigateResult (*reset)(lua_State* L, void* ctx, const char* requirer_chunkname);
83
84
// Resets the internal state to point at an aliased module, given its exact
85
// path from a configuration file. This function is only called when an
86
// alias's path cannot be resolved relative to its configuration file.
87
luarequire_NavigateResult (*jump_to_alias)(lua_State* L, void* ctx, const char* path);
88
89
// Provides an initial alias override opportunity prior to searching for
90
// configuration files. If NAVIGATE_SUCCESS is returned, the internal state
91
// must be updated to point at the aliased location. Can be left undefined.
92
luarequire_NavigateResult (*to_alias_override)(lua_State* L, void* ctx, const char* alias_unprefixed);
93
94
// Provides a final opportunity to resolve an alias if it cannot be found in
95
// configuration files. If NAVIGATE_SUCCESS is returned, the internal state
96
// must be updated to point at the aliased location. Can be left undefined.
97
luarequire_NavigateResult (*to_alias_fallback)(lua_State* L, void* ctx, const char* alias_unprefixed);
98
99
// Navigates through the context by making mutations to the internal state.
100
luarequire_NavigateResult (*to_parent)(lua_State* L, void* ctx);
101
luarequire_NavigateResult (*to_child)(lua_State* L, void* ctx, const char* name);
102
103
// Returns whether the context is currently pointing at a module.
104
bool (*is_module_present)(lua_State* L, void* ctx);
105
106
// Provides a chunkname for the current module. This will be accessible
107
// through the debug library. This function is only called if
108
// is_module_present returns true.
109
luarequire_WriteResult (*get_chunkname)(lua_State* L, void* ctx, char* buffer, size_t buffer_size, size_t* size_out);
110
111
// Provides a loadname that identifies the current module and is passed to
112
// load. This function is only called if is_module_present returns true.
113
luarequire_WriteResult (*get_loadname)(lua_State* L, void* ctx, char* buffer, size_t buffer_size, size_t* size_out);
114
115
// Provides a cache key representing the current module. This function is
116
// only called if is_module_present returns true.
117
luarequire_WriteResult (*get_cache_key)(lua_State* L, void* ctx, char* buffer, size_t buffer_size, size_t* size_out);
118
119
// Returns whether a configuration file is present in the current context,
120
// and if so, its syntax. If not present, require-by-string will call
121
// to_parent until either a configuration file is present or
122
// NAVIGATE_FAILURE is returned (at root).
123
luarequire_ConfigStatus (*get_config_status)(lua_State* L, void* ctx);
124
125
// Parses the configuration file in the current context for the given alias
126
// and returns its value or WRITE_FAILURE if not found. This function is
127
// only called if get_config_status returns true. If this function pointer
128
// is set, get_config must not be set. Opting in to this function pointer
129
// disables parsing configuration files internally and can be used for finer
130
// control over the configuration file parsing process.
131
luarequire_WriteResult (*get_alias)(lua_State* L, void* ctx, const char* alias, char* buffer, size_t buffer_size, size_t* size_out);
132
133
// Provides the contents of the configuration file in the current context.
134
// This function is only called if get_config_status does not return
135
// CONFIG_ABSENT. If this function pointer is set, get_alias must not be
136
// set. Opting in to this function pointer enables parsing configuration
137
// files internally.
138
luarequire_WriteResult (*get_config)(lua_State* L, void* ctx, char* buffer, size_t buffer_size, size_t* size_out);
139
140
// Returns the maximum number of milliseconds to allow for executing a given
141
// Luau-syntax configuration file. This function is only called if
142
// get_config_status returns CONFIG_PRESENT_LUAU and can be left undefined
143
// if support for Luau-syntax configuration files is not needed. A default
144
// value of 2000ms is used. Negative values are treated as infinite.
145
int (*get_luau_config_timeout)(lua_State* L, void* ctx);
146
147
// Executes the module and places the result on the stack. Returns the
148
// number of results placed on the stack. Returning -1 directs the requiring
149
// thread to yield. In this case, this thread should be resumed with the
150
// module result pushed onto its stack.
151
int (*load)(lua_State* L, void* ctx, const char* path, const char* chunkname, const char* loadname);
152
} luarequire_Configuration;
153
154
// Populates function pointers in the given luarequire_Configuration.
155
typedef void (*luarequire_Configuration_init)(luarequire_Configuration* config);
156
157
// Initializes and pushes the require closure onto the stack without
158
// registration.
159
LUALIB_API int luarequire_pushrequire(lua_State* L, luarequire_Configuration_init config_init, void* ctx);
160
161
// Initializes the require library and registers it globally.
162
LUALIB_API void luaopen_require(lua_State* L, luarequire_Configuration_init config_init, void* ctx);
163
164
// Initializes and pushes a "proxyrequire" closure onto the stack. This function
165
// takes two parameters: the string path to resolve and the chunkname of an
166
// existing module. The path is resolved as if it were being required from the
167
// module that the chunkname represents.
168
LUALIB_API int luarequire_pushproxyrequire(lua_State* L, luarequire_Configuration_init config_init, void* ctx);
169
170
// Registers an aliased require path to a result. After registration, the given
171
// result will always be immediately returned when the given path is required.
172
// Expects the path and table to be passed as arguments on the stack.
173
LUALIB_API int luarequire_registermodule(lua_State* L);
174
175
// Clears the entry associated with the given cache key from the require cache.
176
// Expects the cache key to be passed as an argument on the stack.
177
LUALIB_API int luarequire_clearcacheentry(lua_State* L);
178
179
// Clears all entries from the require cache.
180
LUALIB_API int luarequire_clearcache(lua_State* L);
181
182