From 63185629b7aa25ec32a000f669685ae234b5fd85 Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Wed, 24 Jul 2024 19:30:12 +0100 Subject: [PATCH] Use "require" in textutils This avoids us having to support requireless environments inside cc.strings. I do kinda wonder if os.loadAPI-loaded files should also have their own shared "require", just so we're not loading 10 copies of cc.expect. --- .../data/computercraft/lua/rom/apis/textutils.lua | 8 ++++++-- .../data/computercraft/lua/rom/modules/main/cc/expect.lua | 4 ++-- .../computercraft/lua/rom/modules/main/cc/strings.lua | 2 +- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/projects/core/src/main/resources/data/computercraft/lua/rom/apis/textutils.lua b/projects/core/src/main/resources/data/computercraft/lua/rom/apis/textutils.lua index c32b619ea..8c52a8a2c 100644 --- a/projects/core/src/main/resources/data/computercraft/lua/rom/apis/textutils.lua +++ b/projects/core/src/main/resources/data/computercraft/lua/rom/apis/textutils.lua @@ -7,9 +7,13 @@ -- @module textutils -- @since 1.2 -local expect = dofile("rom/modules/main/cc/expect.lua") +local pgk_env = setmetatable({}, { __index = _ENV }) +pgk_env.require = dofile("rom/modules/main/cc/require.lua").make(pgk_env, "rom/modules/main") +local require = pgk_env.require + +local expect = require("cc.expect") local expect, field = expect.expect, expect.field -local wrap = dofile("rom/modules/main/cc/strings.lua").wrap +local wrap = require("cc.strings").wrap --- Slowly writes string text at current cursor position, -- character-by-character. diff --git a/projects/core/src/main/resources/data/computercraft/lua/rom/modules/main/cc/expect.lua b/projects/core/src/main/resources/data/computercraft/lua/rom/modules/main/cc/expect.lua index f4cade7f4..376dcf911 100644 --- a/projects/core/src/main/resources/data/computercraft/lua/rom/modules/main/cc/expect.lua +++ b/projects/core/src/main/resources/data/computercraft/lua/rom/modules/main/cc/expect.lua @@ -118,8 +118,8 @@ end --- Expect a number to be within a specific range. -- -- @tparam number num The value to check. --- @tparam number min The minimum value, if nil then `-math.huge` is used. --- @tparam number max The maximum value, if nil then `math.huge` is used. +-- @tparam[opt=-math.huge] number min The minimum value. +-- @tparam[opt=math.huge] number max The maximum value. -- @return The given `value`. -- @throws If the value is outside of the allowed range. -- @since 1.96.0 diff --git a/projects/core/src/main/resources/data/computercraft/lua/rom/modules/main/cc/strings.lua b/projects/core/src/main/resources/data/computercraft/lua/rom/modules/main/cc/strings.lua index e28356d85..054673f36 100644 --- a/projects/core/src/main/resources/data/computercraft/lua/rom/modules/main/cc/strings.lua +++ b/projects/core/src/main/resources/data/computercraft/lua/rom/modules/main/cc/strings.lua @@ -8,7 +8,7 @@ -- @since 1.95.0 -- @see textutils For additional string related utilities. -local expect = (require and require("cc.expect") or dofile("rom/modules/main/cc/expect.lua")).expect +local expect = require("cc.expect").expect --[[- Wraps a block of text, so that each line fits within the given width.