Modules and Host Boundaries
Lesson 15 of 29 in Coddy's Lua for Luau Developers course.
Module resolution belongs partly to the host. Standalone Luau can provide file-based require, but its resolver is not the Lua 5.1 package search system. In this target runner, require("pricing") loads the sibling pricing.lua module through Lua package loading and returns its exported table. Keep filenames stable and return the module API explicitly. Luau type aliases and export type declarations do not create runtime fields; remove their syntax while preserving value exports. Standard Luau omits general filesystem libraries for sandboxing, although a runner can add input adapters. The target locked driver owns io.lines() and passes ordinary strings to your module.
Luau
type Pricing = {subtotal: (number, number) -> number}
local pricing: Pricing = {subtotal=function(p, q) return p*q end}
print(pricing.subtotal(4, 3))Lua 5.1
package.preload["pricing_demo"] = function()
return {subtotal=function(p,q) return p*q end}
end
local pricing = require("pricing_demo")
print(pricing.subtotal(4, 3))Porting trap
Do not infer portable library availability from a runner convenience. Port the pure computation and adapt loading and input at the host boundary.
Challenge
EasyInput is integer price and nonnegative quantity. Implement subtotal in pricing.lua and call it from app.lua. Return the product. Do not print during require.
Edit app.lua, pricing.lua. The visible locked main.lua reads stdin into a sequence of lines, calls app.run(lines), and prints its returned value with one final newline. Do not print diagnostics or change the module names. Empty input gives an empty sequence. All numeric inputs are small enough to avoid overflow.
Public API checks: pricing.subtotal(price, quantity) must work independently of app.run, including zero quantity and signed price. The locked driver checks this behavior before calling app.run. These checks print nothing when successful.
Try it yourself
-- Public API checks
do
local pricing=require("pricing")
assert(pricing.subtotal(4,3)==12 and pricing.subtotal(4,0)==0 and pricing.subtotal(-2,3)==-6,"pricing.subtotal must implement the public product API")
end
local app = require("app")
local lines = {}
for line in io.lines() do lines[#lines + 1] = line end
print(app.run(lines))
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Lua for Luau Developers
3Collections and Libraries
Table Library PortingRecords and Map TraversalClone and Shared ReferencesRecap: Count InventoryModules and Host BoundariesProtected Calls and TypesPractice on your own: Online Lua compiler