foxygit / fake-08-xbox360 Log in
commit 38f5be6817a2a7a35eb23a4ab32fcba75517f549
Author:     Jon Bell <j.to.the.bell@gmail.com>
AuthorDate: Sat Jan 10 09:42:13 2026 -0700
Commit:     Jon Bell <j.to.the.bell@gmail.com>
CommitDate: Sat Jan 10 09:42:13 2026 -0700

    add workaround for access to api even when using local _ENV
---
 libs/z8lua                                     |   2 +-
 source/p8GlobalLuaFunctions.h                  |   2 +
 test/carts/nested_env_test.p8                  |  61 +++++++++++++++++++++++++
 test/carts/screenshots/nested_env_test_f01.png | Bin 0 -> 374 bytes
 test/endtoendtests.cpp                         |  16 +++++++
 5 files changed, 80 insertions(+), 1 deletion(-)

diff --git a/libs/z8lua b/libs/z8lua
index 5594eb5..876d147 160000
--- a/libs/z8lua
+++ b/libs/z8lua
@@ -1 +1 @@
-Subproject commit 5594eb5218d750347321d973463377236f9c529c
+Subproject commit 876d147e1f5fab9659b126dd8e7466fe7bf644f4
diff --git a/source/p8GlobalLuaFunctions.h b/source/p8GlobalLuaFunctions.h
index 670862e..2a729a2 100644
--- a/source/p8GlobalLuaFunctions.h
+++ b/source/p8GlobalLuaFunctions.h
@@ -576,6 +576,8 @@ function __z8_run_cart(cart_code)
         end

         __cart_sandbox = create_sandbox()
+        -- Store sandbox in registry for C fallback (used when _ENV is modified)
+        rawset(debug.getregistry(), "__PICO8_SANDBOX", __cart_sandbox)
         local code, ex = __z8_load_code(cart_code..glue_code, nil, nil,
                                         __cart_sandbox)

diff --git a/test/carts/nested_env_test.p8 b/test/carts/nested_env_test.p8
new file mode 100644
index 0000000..43cc150
--- /dev/null
+++ b/test/carts/nested_env_test.p8
@@ -0,0 +1,61 @@
+pico-8 cartridge // http://www.pico-8.com
+version 43
+__lua__
+
+bullet1 = {x = 10, y = 20, pairs = pairs, result = ""}
+bullet2 = {x = 30, y = 40, pairs = pairs, result = ""}
+gbullets = {bullet1, bullet2}
+
+function draw_gbullets_old()
+	local g=_ENV
+		for b in all(gbullets) do
+			_ENV=b
+
+			--working
+			circfill(x,y,3,7)
+			circ(x,y,3,8)
+			rectfill(x,y,3,3,8)
+			rect(x,y,3,3,7)
+			line(x,y,x+7,y+7,7)
+			pset(x,y,7)
+			pget(x,y)
+            --ovals work, but drawing algorithm doesn't match per pixel, so leave out of this test
+			--ovalfill(x,y,3,3,8)
+			--oval(x,y,3,3,7)
+			spr(1,x,y)
+			sspr(0,0,3,3,x,y,6,6)
+			fset(7,0,true)
+			fget(7,0)
+			clip(x,y,3,3)
+			clip()
+			pal(7)
+			pal()
+
+
+			--not working
+			--print("bullets drawn")
+			--printh
+			--rrectfill(x,y,3,3,3,8)
+			--rrect(x,y,3,3,3,7)
+			--map(0,0,x,y,3,3,7)
+			--camera(x,y)
+			--camera()
+		end
+		_ENV=g
+   end
+
+function _draw()
+ cls()
+ draw_gbullets_old()
+end
+
+
+__gfx__
+00000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00000000000551110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00700700055500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00077000555559900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00077000cccc99990000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00700700cc9999990000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00000000cc99999c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00000000cccccccc0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
diff --git a/test/carts/screenshots/nested_env_test_f01.png b/test/carts/screenshots/nested_env_test_f01.png
new file mode 100644
index 0000000..e269854
Binary files /dev/null and b/test/carts/screenshots/nested_env_test_f01.png differ
diff --git a/test/endtoendtests.cpp b/test/endtoendtests.cpp
index 28b3e83..d342071 100644
--- a/test/endtoendtests.cpp
+++ b/test/endtoendtests.cpp
@@ -1040,6 +1040,22 @@ TEST_CASE("Loading and running carts") {

         vm->CloseCart();
     }
+    SUBCASE("env modification test"){
+        vm->LoadCart("nested_env_test.p8", false);
+        vm->vm_run();
+        vm->Step();
+
+        SUBCASE("No error reported"){
+            CHECK(vm->GetBiosError() == "");
+        }
+        SUBCASE("screen matches screenshot"){
+            vm->Step();
+
+            CHECK(verifyScreenshot(vm, host, "carts/screenshots/nested_env_test_f01.png"));
+        }
+
+        vm->CloseCart();
+    }

     delete vm;
     delete host;