Skip to content

Fix global loadstring lookup under strict mode #50#52

Closed
harningt wants to merge 1 commit into
developfrom
feature/strict-mode-fix
Closed

Fix global loadstring lookup under strict mode #50#52
harningt wants to merge 1 commit into
developfrom
feature/strict-mode-fix

Conversation

@harningt

Copy link
Copy Markdown
Owner

Description

This pull request resolves Issue #50 where importing/using the json module under strict mode (e.g. from strict.lua) fails.

The failure happens under Lua 5.2+ because loadstring or load performs a global lookup for the loadstring variable. Since loadstring is not declared or defined in these versions, accessing it directly triggers the global table's __index metamethod, causing a strict mode violation error:

variable 'loadstring' is not declared

Proposed Changes

  • Safe loadstring Check: Replaced loadstring or load with rawget(_G or {}, "loadstring") or load in:
    • lua/json/encode/output.lua
    • lua/json/encode/output_utility.lua
    • tests/hook_require.lua
  • Strict Mode Test Coverage: Implemented a filtered strict mode check in tests/hook_require.lua when the TEST_STRICT environment variable is active. The check is configured to only throw for undeclared global accesses originating from the library files (lua/json/*), bypassing third-party loader environments (like luarocks loader which references unpack).
  • CI Integration: Configured the GitHub Actions testing matrix (.github/workflows/test.yml) to run the entire test suite (across all Lua versions and interpreters) with TEST_STRICT=1 enabled.

Closes #50

- Replaced loadstring lookup with rawget(_G or {}, 'loadstring') or load in output.lua, output_utility.lua, and hook_require.lua to prevent undeclared global accesses.
- Added strict mode validation to the test suite under the TEST_STRICT environment variable.
- Configured GitHub Actions test suite matrix to run with TEST_STRICT=1.
@harningt harningt self-assigned this May 25, 2026
@harningt harningt changed the title Fix global loadstring lookup under strict mode (#50) Fix global loadstring lookup under strict mode #50 May 25, 2026
@codecov-commenter

codecov-commenter commented May 25, 2026

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.90%. Comparing base (65f7d76) to head (30bac5e).
⚠️ Report is 13 commits behind head on develop.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop      #52      +/-   ##
===========================================
+ Coverage    95.69%   95.90%   +0.21%     
===========================================
  Files           18       18              
  Lines         1021     1026       +5     
===========================================
+ Hits           977      984       +7     
+ Misses          44       42       -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@harningt harningt changed the base branch from master to develop May 25, 2026 21:30
@harningt harningt closed this May 25, 2026
@harningt harningt deleted the feature/strict-mode-fix branch May 25, 2026 21:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

loadstring or load can cause import errors

2 participants