Script.h
1/* Copyright (C) 2020-2026 by Marc Boris Duerner
2 SPDX-License-Identifier: LGPL-2.1-or-later WITH mif-exception
3*/
4
5#ifndef PT_LUA_SCRIPT_H
6#define PT_LUA_SCRIPT_H
7
8#include <Pt/Lua/Api.h>
9#include <Pt/System/Selectable.h>
10#include <Pt/Signal.h>
11
12struct lua_State;
13struct lua_Debug;
14
15#include <string>
16
17#if __cplusplus >= 202002L
18#include <Pt/Slot.h>
19#include <Pt/Coroutine.h>
20#endif
21
22namespace Pt {
23
24class Any;
25
26namespace Reflex { class Type; }
27
28namespace Lua {
29
30class AsyncCall;
31class Context;
32class Call;
33
34#if __cplusplus >= 202002L
35class AsyncAdvance;
36#endif
37
71class PT_LUA_API Script : public System::Selectable
72 , public Connectable
73{
74 public:
84
89 Script(Context& ctx, const char* script);
90
94
98
102
106
112
115 const std::string& errorMessage() const
116 { return _errorMsg; }
117
120 static Script* fromState(lua_State* L);
121
122#if __cplusplus >= 202002L
125 AsyncAdvance advanceAsync();
126#endif
127
128 protected:
129 bool onRun();
130
132
134
135 void onCancel();
136
137 private:
138 Script(const Script&);
139 Script& operator=(const Script&);
140
141#if __cplusplus >= 202002L
142 friend class AsyncAdvance;
143
144 void attachAwaiter(AsyncAdvance& awaiter);
145
146 void detachAwaiter(AsyncAdvance& awaiter);
147#endif
148
149 bool onCall();
150
151 void onAsyncCall();
152
153 void onAsyncCallFinished();
154
155 bool onAsyncCallReady();
156
157 int pushResult(Pt::Any& value, Pt::Reflex::Type& type);
158
159 void resume();
160
161 bool isCancelled() const;
162
163 void setError(const std::string& msg);
164
165 static void yieldHook(lua_State* L, lua_Debug* ar);
166
167 private:
168 Context& _ctx;
169 lua_State* _co;
170 bool _isCancelled;
171 Status _lastStatus;
172 std::string _errorMsg;
173 Pt::Signal<> _advanced;
174
175 Call* _pendingCall;
176 AsyncCall* _pendingAsyncCall;
177 AsyncCall* _activeAsyncCall;
178
179 #if __cplusplus >= 202002L
180 AsyncAdvance* _awaiter = nullptr;
181 #endif
182};
183
184#if __cplusplus >= 202002L
185
200class PT_LUA_API AsyncAdvance : public Pt::Awaiter
201 , public Pt::Connectable
202{
203 public:
208 AsyncAdvance(Script& script);
209
213
217
218 private:
219 friend class Script;
220
221 void onBegin() override;
222
223 void onCancel() override;
224
225 void onDetach();
226
227 void onAdvanced();
228
229 Script& script();
230
231 Script* _script;
232 bool _isPending;
233};
234
235#endif // __cplusplus >= 202002L
236
237} // namespace
238
239} // namespace
240
241#endif // include guard
Value of any copyable type.
Definition Any.h:79
Provides the base class for I/O-driven co_await-able operations.
Definition Coroutine.h:126
Connection Management for Signal and Slot Objects.
Definition Connectable.h:50
Connectable()
Default constructor.
Awaitable advance of a Lua script.
Definition Script.h:202
AsyncAdvance(Script &script)
Creates an awaitable for one advance of script.
~AsyncAdvance()
Cancels a pending step and detaches from the script.
Script::Status await_resume()
Returns the status of the completed advance step.
Asynchronous native call from Lua.
Definition AsyncCall.h:42
Synchronous native call from Lua.
Definition Call.h:38
Lua state with a bound type catalog.
Definition Context.h:53
Status advance()
Advances the script on the calling thread.
Script(Context &ctx, const char *script)
Loads script into ctx as a coroutine.
Status endAdvance() const
Returns the status of the last advance step.
void beginAdvance()
Starts an asynchronous advance step on the event loop.
static Script * fromState(lua_State *L)
Returns the script stored in the registry of L, or a null pointer.
void onCancel()
Blocks until operation has cancelled.
bool onRun()
Check if ready and run.
const std::string & errorMessage() const
Returns the last compile or runtime error text.
Definition Script.h:115
Status
Step reached by the last advance.
Definition Script.h:78
@ ScriptError
Compile or runtime failure.
Definition Script.h:82
@ NativeCall
A reflected or asynchronous native call is pending.
Definition Script.h:80
@ Yield
Cooperative pause of the Lua coroutine.
Definition Script.h:79
@ ScriptOk
The chunk finished.
Definition Script.h:81
~Script()
Cancels pending work and releases the context.
Pt::Signal & advanced()
Signal emitted when an asynchronous advance step completes.
void onDetach(Pt::System::EventLoop &loop)
Detached from loop.
AsyncAdvance advanceAsync()
Returns an awaitable for one asynchronous advance step.
void onAttach(Pt::System::EventLoop &loop)
Attached to loop.
Multicast Signal to call multiple slots.
Definition Signal.h:190
Event loop of a thread or process.
Definition EventLoop.h:83
Operation that runs through an event loop.
Definition Selectable.h:60
Lua runtime and reflected bindings.
Core module.
Definition Allocator.h:33