Jump to content

Component Registry: Difference between revisions

From Advanced Computers Wiki
No edit summary
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
The Component Registry allows accessing all currently available [[Component|Components]] and is the most convenient way of accessing components.
The Component Registry allows accessing all currently available [[Component|Components]] and is the most convenient way of accessing components. Every Lua vm instance contains a global variable called <code>components</code> which contains this object.


{| class="wikitable"
{| class="wikitable"
Line 6: Line 6:
! Name !! Type !! Description
! Name !! Type !! Description
|-
|-
| style="white-space: nowrap;" | <code>:list()</code> || <code>iterator -> (string, userdata)</code> || Returns an iterator function that, in each invocation returns a string representing the type of the current component along with the actual userdata instance.
| style="white-space: nowrap;" | <code>:list()</code> || <code>function() -> (string, userdata), table, nil</code> || Returns an iterator function that, in each invocation returns a string representing the type of the current component along with the actual userdata instance.
|-
|-
| style="white-space: nowrap;" | <code>:getFirst(componentType:string)</code> || <code>userdata</code>|| Returns the first component of the given <code>componentType</code> or <code>nil</code> if no matching component exists.
| style="white-space: nowrap;" | <code>:getFirst(componentType:string)</code> || <code>userdata</code>|| Returns the first component of the given <code>componentType</code> or <code>nil</code> if no matching component exists.
|}
|}


While <code>:list()</code> looks very intimidating, it can simply be used in a for-loop in place of <code>ipairs(*)</code>. The built-in [[UEFI]] uses the following code to iterate over all components to find any bootable devices:<syntaxhighlight lang="lua" line="1">
local bootables = {}
for compType, elem in components:list() do
    print("- " .. compType)
    if compType == "massStorage" then
        if elem:fileExists("boot.lua") then
            -- we found a bootable medium, use it as default if not already set
            defaultBoot = defaultBoot == nil and idx or defaultBoot
            bootables[idx] = elem
        end
        idx = idx + 1
    end
end
</syntaxhighlight>


{{Navbox content}}
== Notes ==
Technically the component registry is not a component, but it still is listed in this category as it is closely tied to this topic.{{Navbox content}}

Latest revision as of 11:45, 22 August 2026

The Component Registry allows accessing all currently available Components and is the most convenient way of accessing components. Every Lua vm instance contains a global variable called components which contains this object.

ComponentRegistryUD methods
Name Type Description
:list() function() -> (string, userdata), table, nil Returns an iterator function that, in each invocation returns a string representing the type of the current component along with the actual userdata instance.
:getFirst(componentType:string) userdata Returns the first component of the given componentType or nil if no matching component exists.

While :list() looks very intimidating, it can simply be used in a for-loop in place of ipairs(*). The built-in UEFI uses the following code to iterate over all components to find any bootable devices:

local bootables = {}
for compType, elem in components:list() do
    print("- " .. compType)
    if compType == "massStorage" then
        if elem:fileExists("boot.lua") then
            -- we found a bootable medium, use it as default if not already set
            defaultBoot = defaultBoot == nil and idx or defaultBoot
            bootables[idx] = elem
        end
        idx = idx + 1
    end
end

Notes

Technically the component registry is not a component, but it still is listed in this category as it is closely tied to this topic.