add Package.State API - #20
Conversation
In order to minimize indexing/usage faults of the State, this introduces an api for its use.
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/Package.zig`:
- Line 288: Remove the lua.pop(1) call in Package.init so the package table
remains available at pkg.lua_idx when build_package.zig invokes lua.getField for
"build"; do not alter the existing stack-index usage.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: bcc4e514-8643-4085-aa8a-eb96bb5f9efe
📒 Files selected for processing (5)
src/Package.zigsrc/actions/build_package.zigsrc/actions/info.zigsrc/actions/install.zigsrc/string.zig
| } | ||
| pop_count += 1; // deps | ||
| lua.pop(pop_count); | ||
| lua.pop(1); // package |
There was a problem hiding this comment.
🎯 Functional Correctness | 🔴 Critical | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
set -euo pipefail
rg -n -C 4 'lua_idx|const pkg = lua\.getTop\(\)|lua\.pop\(1\).*package|getField\(pkg\.lua_idx' srcRepository: D-Berg/packa
Length of output: 3250
🏁 Script executed:
#!/bin/bash
set -euo pipefail
printf '%s\n' '--- src/Package.zig: Package.init and surrounding stack operations ---'
sed -n '100,215p' src/Package.zig
sed -n '250,310p' src/Package.zig
printf '%s\n' '--- src/actions/build_package.zig: package access and call context ---'
sed -n '1,130p' src/actions/build_package.zig
rg -n -C 6 'buildPackage|Package\.init|lua_idx|lua\.setTop|lua\.pop|lua\.push|pcall' src/actions src
printf '%s\n' '--- Lua API wrapper declarations and index semantics ---'
rg -n -C 5 'pub .*getField|fn getField|lua_getfield|pub .*pop|fn pop|lua_pop|absIndex|toAbsolute|registry|LUA_REGISTRYINDEX' srcRepository: D-Berg/packa
Length of output: 50368
🏁 Script executed:
#!/bin/bash
set -euo pipefail
printf '%s\n' '--- Package.collect and state access ---'
sed -n '304,390p' src/Package.zig
rg -n -C 5 'pub fn get\(|fn get\(|getDependencies|Package\.collect|collect\(' src/Package.zig src/actions/build_package.zig
printf '%s\n' '--- Lua wrapper files and relevant APIs ---'
git ls-files | rg '(^|/)(lua|zlua|.*Lua.*)|build.zig.zon|build.zig$' | head -80
rg -n -C 4 'getField|pub fn pop|fn pop|lua_getfield|lua_settop|setTop|REGISTRYINDEX' . -g '*.zig' -g '*.zon' -g '!src/actions/build_package.zig' -g '!src/Package.zig' | head -240
printf '%s\n' '--- Exact stack-affecting calls in Package.init ---'
sed -n '170,305p' src/Package.zig | rg -n 'getTop|getField|rawGetI|pop|push|pcall|loadBuffer|setField'Repository: D-Berg/packa
Length of output: 10817
🏁 Script executed:
#!/bin/bash
set -euo pipefail
python3 - <<'PY'
from dataclasses import dataclass
`@dataclass`
class Stack:
values: list[str]
def top(self) -> int:
return len(self.values)
def push(self, value: str) -> None:
self.values.append(value)
def pop(self, count: int = 1) -> None:
if count < 0 or count > len(self.values):
raise AssertionError(f"invalid pop({count}) for {self.values}")
del self.values[-count:] if count else []
def get_field(self, index: int, field: str) -> str:
if index < 1 or index > len(self.values):
return "nil"
self.push(f"{self.values[index - 1]}.{field}")
return self.values[-1]
def raw_get_i(self, index: int, i: int) -> str:
if index < 1 or index > len(self.values):
return "nil"
self.push(f"{self.values[index - 1]}[{i}]")
return self.values[-1]
def package_init(initial_stack: list[str], dep_count: int = 0):
s = Stack(initial_stack[:])
s.push("package") # pcall(0, 1, 0)
pkg = s.top() # const pkg = lua.getTop()
for field in ("name", "version", "desc", "homepage", "license", "url", "hash"):
assert s.get_field(pkg, field) != "nil"
s.pop()
assert s.get_field(pkg, "build") != "nil"
s.pop()
pop_count = 0
s.get_field(pkg, "deps") # deps
pop_count += 1
# Model the compile/runtime table and dependency element pushes.
if dep_count:
for section in ("compile", "runtime"):
s.get_field(s.top() - 0, section)
for i in range(1, dep_count + 1):
s.raw_get_i(s.top() - 0, i)
s.pop()
pop_count += 1
pop_count += 1 # section table
s.pop(pop_count + 1) # deps and its children
before_final_pop = s.values[:]
s.pop(1) # lua.pop(1); // package
return pkg, before_final_pop, s.values
for initial in ([], ["setup residue"]):
pkg, before, after = package_init(initial, dep_count=2)
print({
"initial_stack": initial,
"stored_lua_idx": pkg,
"stack_before_package_pop": before,
"stack_after_package_pop": after,
"stored_index_is_package": pkg <= len(after) and after[pkg - 1] == "package",
})
PYRepository: D-Berg/packa
Length of output: 282
🏁 Script executed:
#!/bin/bash
set -euo pipefail
python3 - <<'PY'
from dataclasses import dataclass
`@dataclass`
class Stack:
values: list[str]
def top(self) -> int:
return len(self.values)
def push(self, value: str) -> None:
self.values.append(value)
def pop(self, count: int = 1) -> None:
if count < 0 or count > len(self.values):
raise AssertionError(f"invalid pop({count}) for {self.values}")
if count:
del self.values[-count:]
def get_field(self, index: int, field: str) -> None:
if index < 1 or index > len(self.values):
self.push("nil")
else:
self.push(f"{self.values[index - 1]}.{field}")
def raw_get_i(self, index: int, i: int) -> None:
if index < 1 or index > len(self.values):
self.push("nil")
else:
self.push(f"{self.values[index - 1]}[{i}]")
def package_init(initial_stack: list[str], dep_count: int = 2):
s = Stack(initial_stack[:])
s.push("package")
pkg = s.top()
for field in ("name", "version", "desc", "homepage", "license", "url", "hash"):
s.get_field(pkg, field)
s.pop()
s.get_field(pkg, "build")
s.pop()
pop_count = 0
s.get_field(pkg, "deps")
lua_deps = s.top()
pop_count += 1
# The source leaves compile/runtime tables and rawGetI results on the stack.
for section in ("compile", "runtime"):
s.get_field(lua_deps, section)
section_index = s.top()
for i in range(1, dep_count + 1):
s.raw_get_i(section_index, i)
pop_count += 1
pop_count += 1
pop_count += 1
before_final_pop = s.values[:]
s.pop(pop_count)
assert s.values == initial_stack + ["package"], (s.values, initial_stack)
s.pop(1)
return pkg, before_final_pop, s.values
for initial in ([], ["setup residue"]):
pkg, before, after = package_init(initial)
print({
"initial_stack": initial,
"stored_lua_idx": pkg,
"stack_before_package_pop": before,
"stack_after_package_pop": after,
"stored_index_is_package": pkg <= len(after) and after[pkg - 1] == "package",
})
PYRepository: D-Berg/packa
Length of output: 292
🏁 Script executed:
#!/bin/bash
set -euo pipefail
python3 - <<'PY'
from dataclasses import dataclass
`@dataclass`
class Stack:
values: list[str]
def top(self) -> int:
return len(self.values)
def push(self, value: str) -> None:
self.values.append(value)
def pop(self, count: int = 1) -> None:
if count < 0 or count > len(self.values):
raise AssertionError(f"invalid pop({count}) for {self.values}")
if count:
del self.values[-count:]
def get_field(self, index: int, field: str) -> None:
self.push(
f"{self.values[index - 1]}.{field}"
if 1 <= index <= len(self.values) else "nil"
)
def raw_get_i(self, index: int, i: int) -> None:
self.push(
f"{self.values[index - 1]}[{i}]"
if 1 <= index <= len(self.values) else "nil"
)
def package_init(initial_stack: list[str], dep_count: int = 2):
s = Stack(initial_stack[:])
s.push("package")
pkg = s.top()
for field in ("name", "version", "desc", "homepage", "license", "url", "hash"):
s.get_field(pkg, field)
s.pop()
s.get_field(pkg, "build")
s.pop()
pop_count = 0
s.get_field(pkg, "deps")
lua_deps = s.top()
for section in ("compile", "runtime"):
s.get_field(lua_deps, section)
section_index = s.top()
for i in range(1, dep_count + 1):
s.raw_get_i(section_index, i)
pop_count += 1
pop_count += 1
pop_count += 1 # deps
before_cleanup = s.values[:]
s.pop(pop_count)
assert s.values == initial_stack + ["package"], (s.values, initial_stack)
s.pop(1) # lua.pop(1); // package
return pkg, before_cleanup, s.values
for initial in ([], ["setup residue"]):
pkg, before, after = package_init(initial)
print({
"initial_stack": initial,
"stored_lua_idx": pkg,
"stack_before_cleanup": before,
"stack_after_package_pop": after,
"stored_index_is_package": pkg <= len(after) and after[pkg - 1] == "package",
})
PYRepository: D-Berg/packa
Length of output: 831
Keep the package table alive until build completes.
Package.init stores its stack index in lua_idx, then lua.pop(1) removes that table. build_package.zig later calls lua.getField(pkg.lua_idx, "build"), so the index no longer identifies the package table. Remove this pop or store the table in the Lua registry.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@src/Package.zig` at line 288, Remove the lua.pop(1) call in Package.init so
the package table remains available at pkg.lua_idx when build_package.zig
invokes lua.getField for "build"; do not alter the existing stack-index usage.
In order to minimize indexing/usage faults of the State,
this introduces an api for its use.
Summary by CodeRabbit
New Features
Bug Fixes
Documentation
Refactor