Skip to main content
The CoreModules enum is a flags enumeration that specifies which standard Lua libraries should be loaded when creating a Script instance.

Usage

Pass a CoreModules value to the Script constructor:
// Use the default preset
var script = new Script();

// Use a specific preset
var sandboxed = new Script(CoreModules.Preset_HardSandbox);

// Combine specific modules
var custom = new Script(
    CoreModules.Basic | 
    CoreModules.String | 
    CoreModules.Table
);

Module Flags

None
enum
No modules loaded. Creates a completely empty environment.
var script = new Script(CoreModules.None);
// No global functions or tables available
GlobalConsts
enum
Global constants: _G, _VERSION, and _SOLARSHARP.
print(_VERSION)      -- Lua version
print(_SOLARSHARP)   -- SolarSharp info
print(_G == _G)      -- true, self-reference
TableIterators
enum
Table iterators: next, ipairs, and pairs.
for k, v in pairs(myTable) do
    print(k, v)
end
Metatables
enum
Metatable methods: setmetatable, getmetatable, rawset, rawget, rawequal, and rawlen.
local mt = { __index = function() return 42 end }
setmetatable({}, mt)
String
enum
The string package with all string manipulation functions.
print(string.upper("hello"))  -- HELLO
print(string.sub("hello", 1, 2))  -- he
LoadMethods
enum
Load methods: load, loadsafe, loadfile, loadfilesafe, dofile, and require.Warning: Allows dynamic code execution and file system access.
require("mymodule")
dofile("script.lua")
Table
enum
The table package for table manipulation.
table.insert(myArray, "value")
table.sort(myArray)
Basic
enum
Basic methods: assert, collectgarbage, error, print, select, type, tonumber, and tostring.
print(type(42))           -- number
assert(x > 0, "x must be positive")
ErrorHandling
enum
Error handling methods: pcall and xpcall.
local ok, result = pcall(function()
    return riskyOperation()
end)
Math
enum
The math package with all math functions.
print(math.sqrt(16))      -- 4
print(math.sin(math.pi))  -- 0
Coroutine
enum
The coroutine package for coroutine management.
local co = coroutine.create(function()
    coroutine.yield(42)
end)
Bit32
enum
The bit32 package for bitwise operations.
print(bit32.band(0xFF, 0x0F))  -- 15
print(bit32.lshift(1, 3))      -- 8
OS_Time
enum
Time methods of the os package: clock, difftime, date, and time.Safe for sandboxed environments.
print(os.time())          -- Current timestamp
print(os.date("%Y-%m-%d"))  -- 2024-03-15
OS_System
enum
System methods of the os package (excluding time methods).Warning: Not supported under Unity. Provides system-level access.
os.execute("ls -la")  -- Execute shell command
os.exit(0)
IO
enum
The io and file packages for file I/O.Warning: Not supported under Unity. Provides file system access.
local file = io.open("data.txt", "r")
local content = file:read("*a")
file:close()
Debug
enum
The debug package (limited support).
debug.traceback()
debug.getinfo(func)
Json
enum
The json package (introduced by SolarSharp).
local data = json.parse('{"name": "John", "age": 30}')
print(data.name)  -- John

local str = json.serialize({x = 10, y = 20})

Presets

Preset_HardSandbox
enum
A strict sandbox preset for untrusted code.Includes: GlobalConsts, TableIterators, String, Table, Basic, Math, Bit32Use for: Running untrusted user scripts with minimal system access.
var sandbox = new Script(CoreModules.Preset_HardSandbox);
// Safe to run user-submitted code
Preset_SoftSandbox
enum
A softer sandbox with more features.Includes: Preset_HardSandbox + Metatables, ErrorHandling, Coroutine, OS_Time, JsonUse for: Semi-trusted environments where you want Lua features but not file system access.
var script = new Script(CoreModules.Preset_SoftSandbox);
// Can use metatables, coroutines, but no file access
Preset_Default
enum
The default preset with nearly everything except debug.Includes: Preset_SoftSandbox + LoadMethods, OS_System, IOWarning: Allows unlimited system access. Only use with trusted code.
var script = new Script(); // Uses Preset_Default
var script2 = new Script(CoreModules.Preset_Default); // Explicit
Preset_Complete
enum
Everything including debug.Includes: Preset_Default + DebugWarning: Full access to everything. Only for development/debugging.
var devScript = new Script(CoreModules.Preset_Complete);

Examples

Minimal Environment

using SolarSharp.Interpreter;
using SolarSharp.Interpreter.Modules;

// Only basic functions, no libraries
var minimal = new Script(
    CoreModules.Basic | 
    CoreModules.GlobalConsts
);

minimal.DoString(@"
    print('Hello')        -- Works
    print(type(42))       -- Works
    -- string.upper('x') -- Would fail, no string library
");

Custom Combination

// Math and string manipulation only
var mathScript = new Script(
    CoreModules.Basic |
    CoreModules.Math |
    CoreModules.String |
    CoreModules.TableIterators
);

mathScript.DoString(@"
    local x = math.sqrt(16)
    local s = string.upper('hello')
    print(x, s)  -- 4, HELLO
");

Secure Sandbox

// For running untrusted user scripts
var userScript = new Script(CoreModules.Preset_HardSandbox);

// Safely execute user code
userScript.DoString(userCode);

// File I/O not available
try
{
    userScript.DoString("io.open('secret.txt', 'r')");
}
catch
{
    Console.WriteLine("File I/O blocked");
}

Game Modding Environment

// Good balance for game mods
var modScript = new Script(
    CoreModules.Preset_SoftSandbox
);

// Add custom APIs
modScript.Globals["game"] = gameAPI;
modScript.Globals["player"] = playerAPI;

modScript.DoString(@"
    -- Mods can use coroutines, metatables, etc.
    function onPlayerJoin()
        local co = coroutine.create(function()
            game:showMessage('Welcome!')
            coroutine.yield()
            game:giveStarterItems(player)
        end)
        coroutine.resume(co)
    end
");

Development Environment

// Full features for development/debugging
var devScript = new Script(CoreModules.Preset_Complete);

devScript.DoString(@"
    -- All libraries available
    local json = require('json')
    local data = json.parse('{}')
    
    -- Debug info available
    print(debug.traceback())
    
    -- File I/O available
    local file = io.open('log.txt', 'w')
    file:write('Debug info')
    file:close()
");

Testing Individual Modules

// Test if specific module is loaded
var script = new Script(CoreModules.String | CoreModules.Math);

script.DoString(@"
    assert(string, 'string module loaded')
    assert(math, 'math module loaded')
    assert(not io, 'io module not loaded')
    assert(not table, 'table module not loaded')
");

Security Considerations

Safe Modules (Sandbox-Friendly)

  • Basic (except dofile/load which are in LoadMethods)
  • String
  • Table
  • Math
  • Bit32
  • GlobalConsts
  • TableIterators
  • Metatables
  • ErrorHandling
  • Coroutine
  • OS_Time
  • Json

Unsafe Modules (System Access)

  • LoadMethods - Dynamic code execution, file loading
  • OS_System - Shell command execution, process control
  • IO - File system access
  • Debug - Can inspect/modify program state

Checking Loaded Modules

using SolarSharp.Interpreter;
using SolarSharp.Interpreter.Modules;

var modules = CoreModules.String | CoreModules.Math;

// Check if a module is included
if ((modules & CoreModules.String) == CoreModules.String)
{
    Console.WriteLine("String module included");
}

// Extension method (if available)
if (modules.Has(CoreModules.Math))
{
    Console.WriteLine("Math module included");
}

Build docs developers (and LLMs) love