Skip to main content
The Basic module (CoreModules.Basic) provides essential Lua functions that form the foundation of any Lua script.

Functions

Prints values to the configured output stream (by default, the debug output).
print("Hello, World!")
print("The answer is", 42)
print("Multiple", "values", "separated", "by", "tabs")
Values are converted to strings and separated by tabs. Uses tostring() metamethod if available. Output:
Hello, World!
The answer is	42
Multiple	values	separated	by	tabs

type(v)

Returns the type of a value as a string.
print(type(nil))        -- "nil"
print(type(42))         -- "number"
print(type("hello"))    -- "string"
print(type(true))       -- "boolean"
print(type({}))         -- "table"
print(type(print))      -- "function"
print(type(coroutine.create(function() end)))  -- "thread"
Returns: One of: "nil", "number", "string", "boolean", "table", "function", "thread", or "userdata"

tostring(v)

Converts a value to a string.
print(tostring(42))       -- "42"
print(tostring(true))     -- "true"
print(tostring(nil))      -- "nil"
print(tostring({a=1}))    -- "table: 0x..."
Respects the __tostring metamethod:
local t = {value = 42}
setmetatable(t, {
    __tostring = function(self)
        return "MyObject(" .. self.value .. ")"
    end
})

print(tostring(t))  -- "MyObject(42)"

tonumber(e [, base])

Converts a value to a number.
print(tonumber("42"))         -- 42
print(tonumber("3.14"))       -- 3.14
print(tonumber("FF", 16))     -- 255
print(tonumber("1010", 2))    -- 10
print(tonumber("not a number"))  -- nil
Parameters:
  • e - Value to convert (number or string)
  • base - Optional base (2-36 for integer conversion)
Returns: Number value, or nil if conversion fails Note: SolarSharp supports bases 2-36, with enhanced support for bases 2-10.

assert(v [, message])

Raises an error if the condition is false.
assert(x > 0, "x must be positive")
assert(file ~= nil)  -- Default message: "assertion failed!"

local function divide(a, b)
    assert(b ~= 0, "division by zero")
    return a / b
end
Returns: All arguments if the condition is true
local x = assert(getValue())  -- Returns the value if not nil/false

error(message [, level])

Terminates execution and raises an error.
error("Something went wrong")          -- Level 1 (default)
error("Invalid argument", 2)           -- Level 2 (caller's caller)
error("Critical error", 0)             -- Level 0 (no location info)
Parameters:
  • message - Error message string
  • level - Stack level for error reporting:
    • 1 (default): Error location is where error was called
    • 2: Error location is the caller of the function that called error
    • 0: No location information added

select(index, …)

Returns selected arguments from a list.
-- Get count of arguments
print(select("#", "a", "b", "c"))  -- 3

-- Get arguments starting from index
print(select(2, "a", "b", "c", "d"))  -- "b" "c" "d"

-- Negative indexing (from end)
print(select(-1, "a", "b", "c"))  -- "c"
print(select(-2, "a", "b", "c"))  -- "b" "c"
Parameters:
  • index - Either:
    • "#" - Returns the count of remaining arguments
    • Number - Returns all arguments starting from that position

collectgarbage([opt [, arg]])

Interface to the .NET garbage collector.
collectgarbage()              -- Force garbage collection
collectgarbage("collect")     -- Force garbage collection
collectgarbage("restart")     -- Force garbage collection
Note: This is largely a stub that delegates to the CLR garbage collector. Only nil, "collect", and "restart" trigger a collection. Other Lua GC options are not supported.

C# Integration

All basic functions are implemented in the BasicModule class:
using SolarSharp.Interpreter.CoreLib;

// The module is automatically loaded with CoreModules.Basic
var script = new Script(CoreModules.Basic);

Examples

Type Checking

function processValue(value)
    local t = type(value)
    
    if t == "number" then
        return value * 2
    elseif t == "string" then
        return value .. "!"
    elseif t == "table" then
        return #value
    else
        error("Unsupported type: " .. t)
    end
end

Safe Type Conversion

function safeToNumber(str)
    local num = tonumber(str)
    if num then
        return num
    else
        error("Invalid number: " .. tostring(str))
    end
end

Argument Validation

function calculateArea(width, height)
    assert(type(width) == "number", "width must be a number")
    assert(type(height) == "number", "height must be a number")
    assert(width > 0, "width must be positive")
    assert(height > 0, "height must be positive")
    
    return width * height
end

Variadic Function Arguments

function sum(...)
    local total = 0
    for i = 1, select("#", ...) do
        local value = select(i, ...)
        total = total + value
    end
    return total
end

print(sum(1, 2, 3, 4, 5))  -- 15

See Also

Build docs developers (and LLMs) love