From 170f711c38658410885749ec3a89f5fdf16894e1 Mon Sep 17 00:00:00 2001 From: Raj Nakarja Date: Thu, 29 Oct 2026 11:49:12 +0100 Subject: [PATCH] Update the Lua notes for require caching, whole-file reads and yearday Firmware now caches require, returns the whole file from a bare read, and starts yearday at 1, so the notes no longer warn about the old behaviour. --- plugins/superstack/skills/s2-lua/SKILL.md | 9 ++++----- .../superstack/skills/s2-lua/references/footguns.md | 13 +++++++------ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/plugins/superstack/skills/s2-lua/SKILL.md b/plugins/superstack/skills/s2-lua/SKILL.md index 60c922d..91957f3 100644 --- a/plugins/superstack/skills/s2-lua/SKILL.md +++ b/plugins/superstack/skills/s2-lua/SKILL.md @@ -28,8 +28,8 @@ Superstack. These facts shape all code: - **Code is a bundle.** `main.lua` is the entry point. Other `.lua` files in the bundle load with `require("name")`, which runs `name.lua` and returns - its first value. `require` has no cache, so it runs the file again on every - call. Code files are read-only. + its first value. A file runs once, and later calls return the same value. + Code files are read-only. - Standard libraries: basic, `math`, `string`, `table`, `utf8`, `coroutine`, plus a built-in `json` with `json.encode` and `json.decode`. **There is no `io`, `os`, `debug` or `package`.** Use `storage.*`, `time.*` and `device.*` @@ -112,9 +112,8 @@ in [references/footguns.md](references/footguns.md). one crashes the program. 10. **Temporary files die with the run**: `storage.open()` returns a handle to a new temporary file, deleted on close and whenever the code starts, - stops, crashes or is replaced. `handle:read()` with no options returns - **line 1 only**. Nothing on the device survives a restart, so keep durable - state on the user's server. + stops, crashes or is replaced. Nothing on the device survives a restart, + so keep durable state on the user's server. 11. **Power**: ports boot **live at 1.8 V**, so a 3.3 V sensor on an unconfigured port half-works rather than failing cleanly. Call `device.power.set_vout(3.3)` in setup. It sets **all** ports at once (1.8 to diff --git a/plugins/superstack/skills/s2-lua/references/footguns.md b/plugins/superstack/skills/s2-lua/references/footguns.md index 7465e8b..5157613 100644 --- a/plugins/superstack/skills/s2-lua/references/footguns.md +++ b/plugins/superstack/skills/s2-lua/references/footguns.md @@ -230,9 +230,11 @@ named files. - `handle:write(data)` replaces the contents. `handle:append(data)` adds to them. -- `handle:read()` with no options returns **line 1 only**. Use +- `handle:read()` with no options returns the whole file, and raises + `not enough memory` when the file does not fit in free memory. Use `handle:read{ line = n }` for line `n` (negative counts from the end), or - `handle:read{ length = n, offset = o }` for bytes. + `handle:read{ length = n, offset = o }` for bytes, which also reads a large + file in parts. - `handle:close()` deletes the file. - **Every temporary file is deleted when the code starts, stops, restarts or crashes, when new code arrives, and at reboot.** Nothing survives the run @@ -241,9 +243,9 @@ named files. `there is not enough storage`. Code files in the bundle are read-only. Load one with `require("name")`, which -runs `name.lua` and returns its first value. `require` has no cache and runs -the file again on every call, and the name is a path: `require("lib/sensor")` -loads `lib/sensor.lua`. +runs `name.lua` once and returns its first value, and later calls return the +same value. The name is a path: `require("lib/sensor")` loads +`lib/sensor.lua`. ## Retry pattern for a dropped connection @@ -364,5 +366,4 @@ Checked against firmware 3.0.0: | SPI | Pin options `sclk_pin`, `mosi_pin`, `miso_pin` and `cs_pin` are all or none. At most 8191 bytes per transfer | | Digital input | `get_input` and `assign_input_event` pull **down** unless `pull` says otherwise. `set_output` needs a boolean, not `1` or `0` | | PSM timing | Requested as a 900 s periodic update and 30 s active time, hence "up to 15 minutes". The network may grant different values | -| `get_time_date().yearday` | 0-based: January 1 is 0 | | State across new code | Globals do not survive. Code starts from the top of `main.lua`, and temporary files are gone |