Skip to content

Latest commit

 

History

History
144 lines (102 loc) · 2.42 KB

File metadata and controls

144 lines (102 loc) · 2.42 KB

System Interop Wrappers (sys)

Overview

sys provides safer wrappers around Magnolia's low-level sysproc and syscall built-ins.

Instead of handling multiple raw return shapes at every call site, sys normalizes results into consistent objects and helper predicates.

Import

sys := import('sys')

Functions

ok?(result)

Returns true when result is a successful syscall-style result.

Accepted success tags:

  • :ok
  • :success
if sys.ok?(res) -> println('call succeeded')

error?(result)

Returns true when result is an error result (type = :error).

if sys.error?(res) -> println('call failed')

resolve(library, symbol)

Resolves a procedure symbol from a shared library.

Success shape:

{
    type: :ok
    proc: <proc>
    library: <string>
    symbol: <string>
}

Error shape:

{
    type: :error
    error: <string>
    library: <string>
    symbol: <string>
    detail: <value>
}
pidProc := sys.resolve('kernel32.dll', 'GetCurrentProcessId')
if pidProc.type = :error -> println(pidProc.error)

call(target, args...)

Calls a resolved procedure or address and normalizes malformed payloads.

Success shape:

{
    type: :ok
    r1: <int>
    r2: <int>
    errno?: <int>
}

Error shape:

{
    type: :error
    error: <string>
    detail?: <value>
}
resolved := sys.resolve('kernel32.dll', 'GetCurrentProcessId')
if resolved.type = :ok -> {
    res := sys.call(resolved.proc)
    if sys.ok?(res) -> println('PID: ' + string(res.r1))
}

resolveAndCall(library, symbol, args...)

Convenience API that runs resolve(...) then call(...).

res := sys.resolveAndCall('kernel32.dll', 'GetCurrentProcessId')

valueOr(result, fallback)

Returns result.r1 if successful; otherwise returns fallback.

pid := sys.valueOr(res, -1)

Typical Pattern

sys := import('sys')

fn currentPID {
    res := sys.resolveAndCall('kernel32.dll', 'GetCurrentProcessId')
    sys.valueOr(res, -1)
}

println('PID: ' + string(currentPID()))

Notes

  • Keep direct sysproc and syscall usage inside wrapper code when possible.
  • Validate argument types and pointers before native calls.
  • For memory helpers and concurrency primitives, see the go documentation.

Related Docs