The Script class is the central class for executing Lua code in SolarSharp. Multiple Script objects can coexist in the same program but cannot share data among themselves unless some mechanism is put in place.
Constructors
Creates a new Script instance with default core modules (Preset_Default). var script = new Script ();
Creates a new Script instance with specified core modules. The core modules to be pre-registered in the default global table
var script = new Script ( CoreModules . Preset_HardSandbox );
Properties
The default global table for this script. Execution uses this table unless a different table is intentionally passed. script . Globals [ "myVar" ] = 42 ;
Gets access to the script options for this instance.
A predefined table that can be used by any CLR code to store Lua values. Use unique keys (GUIDs, library names, or userdata with CLR object addresses) to avoid collisions.
Gets the global options that cannot be customized per-script.
Gets or sets the default script options for all newly created scripts.
Gets the number of source code chunks loaded in this script.
Loading Methods
Loads a string containing Lua/SolarSharp code. The global table to bind to this chunk (optional, defaults to script.Globals)
Name used in error messages (optional)
A function which will execute the loaded code when called
LuaValue chunk = script . LoadString ( "return 1 + 1" );
LuaValue result = script . Call ( chunk ); // returns 2
Loads a file containing Lua/SolarSharp code. The global table to bind to this chunk (optional)
The filename to be used in error messages (optional)
A function which will execute the loaded code when called
LuaValue chunk = script . LoadFile ( "script.lua" );
script . Call ( chunk );
Loads a stream containing Lua/SolarSharp code. Does not close the stream. The stream containing code
The global table to bind to this chunk (optional)
Name used in error messages (optional)
A function which will execute the loaded code when called
Execution Methods
Loads and executes a string containing Lua/SolarSharp code. The global context (optional)
Name used in error messages (optional)
The result of executing the code
LuaValue result = script . DoString ( "return 2 + 2" );
Console . WriteLine ( result . Number ); // 4
Loads and executes a file containing Lua/SolarSharp code. The global context (optional)
Name used in error messages (optional)
The result of executing the file
script . DoFile ( "init.lua" );
Calls a Lua function. Arguments to pass to the function (optional)
The return value(s) of the function call
LuaValue func = script . Globals . Get ( "myFunction" );
LuaValue result = script . Call ( func , LuaValue . NewNumber ( 5 ));
Static Helper Methods
Quickly runs Lua code with all defaults. LuaValue result = Script . RunString ( "return 'Hello, World!'" );
Quickly runs a Lua file with all defaults. Script . RunFile ( "script.lua" );
Coroutine Methods
Creates a coroutine pointing at the specified function. The function to wrap in a coroutine
LuaValue func = script . LoadString ( "return coroutine.yield(1)" );
LuaValue co = script . CreateCoroutine ( func );
Gets the metatable for a specific Lua type. The metatable for the type, or null
Sets the metatable for a specific Lua type. The type (must be Nil, Boolean, Number, String, or Function)
var mt = new Table ( script );
mt [ "__add" ] = ( Func < string , string , string >)(( a , b ) => a + b );
script . SetTypeMetatable ( DataType . String , mt );
Constants
The version of the SolarSharp engine (“2.0.0.0”)
The Lua version being supported (“5.2”)
Example Usage
using SolarSharp . Interpreter ;
// Create a script instance
var script = new Script ();
// Execute Lua code
script . DoString ( @"
function greet(name)
return 'Hello, ' .. name .. '!'
end
" );
// Call a Lua function from C#
LuaValue greetFunc = script . Globals . Get ( "greet" );
LuaValue result = script . Call ( greetFunc , "World" );
Console . WriteLine ( result . String ); // "Hello, World!"
// Access global variables
script . Globals [ "myNumber" ] = 42 ;
LuaValue value = script . DoString ( "return myNumber * 2" );
Console . WriteLine ( value . Number ); // 84