Script Class Reference

#include <Pt/Lua/Script.h>

Lua script. More...

Inherits Selectable, and Connectable.

Public Types

enum  Status {
  Yield ,
  NativeCall ,
  ScriptOk ,
  ScriptError
}
 Step reached by the last advance. More...

Public Member Functions

 Script (Context &ctx, const char *script)
 Loads script into ctx as a coroutine.
 ~Script ()
 Cancels pending work and releases the context.
void beginAdvance ()
 Starts an asynchronous advance step on the event loop.
Status endAdvance () const
 Returns the status of the last advance step.
Pt::Signaladvanced ()
 Signal emitted when an asynchronous advance step completes.
Status advance ()
 Advances the script on the calling thread.
const std::string & errorMessage () const
 Returns the last compile or runtime error text.
AsyncAdvance advanceAsync ()
 Returns an awaitable for one asynchronous advance step.
void setActive (EventLoop &parent)
 Sets the parent loop, so that operations can be run.
void detach ()
 Remove from event loop and cancels outstanding operations.
void cancel ()
 Cancels all operations.
bool run ()
 Run operation if it is ready.
EventLoop * parent () const
 Returns the used event loop.

Static Public Member Functions

static ScriptfromState (lua_State *L)
 Returns the script stored in the registry of L, or a null pointer.

Protected Member Functions

bool onRun ()
 Check if ready and run.
void onAttach (Pt::System::EventLoop &loop)
 Attached to loop.
void onDetach (Pt::System::EventLoop &loop)
 Detached from loop.
void onCancel ()
 Blocks until operation has cancelled.
void post ()
 Posts this selectable to its event loop from any thread.

Detailed Description

Script is the coroutine that runs Lua source in a Context. The source is a C string loaded at construction. Only one script may use a context at a time; a second construction throws std::logic_error. A syntax error does not throw: the status is ScriptError, errorMessage() holds the compiler text, and the context is free for another script.

The script is a Selectable. Blocking advance() runs on the calling thread and returns at the next step. Continue while the status is Yield or NativeCall. Yield is a cooperative pause so other work can run. NativeCall means Lua invoked a reflected method, property, or constructor; the next advance() performs that Call and resumes. Stop on ScriptOk or ScriptError. Blocking advance() throws if an AsyncCall is pending.

Asynchronous work attaches the script with setActive() on an EventLoop. beginAdvance() starts a step, advanced() is emitted when the step completes, and endAdvance() returns the status. Call beginAdvance() again while the status is Yield or NativeCall. The loop does not own the script. Keep it alive until no step is waiting. cancel() aborts a pending step and any active async call.

C++20 advanceAsync() is that asynchronous step as an awaitable. fromState() finds the script stored in a Lua registry, for Lua C API code that needs it. The script is not copyable.

Member Enumeration Documentation

◆ Status

enum Status
Enumerator
Yield 

Cooperative pause of the Lua coroutine.

NativeCall 

A reflected or asynchronous native call is pending.

ScriptOk 

The chunk finished.

ScriptError 

Compile or runtime failure.

Constructor & Destructor Documentation

◆ Script()

Script ( Context & ctx,
const char * script )
Exceptions
%std::logic_errorif ctx already has a script.

Member Function Documentation

◆ advance()

Status advance ( )
Exceptions
%std::logic_errorif an asynchronous native call is pending.