From 6f8aafdefca1dd3ae2e239623e8c84bb781c519a Mon Sep 17 00:00:00 2001
From: Cesar Carrasco <cesar@machinemode.com>
Date: Wed, 10 Jul 2024 11:41:41 -0400
Subject: [PATCH] feat: enable LUA_COMPAT for Lua 5.2, 5.3, and 5.4

---
 .../party/iroiro/luajava/LuaTestSuite.java    | 20 +++++++++++++++++++
 lua52/build.gradle                            |  2 ++
 lua53/build.gradle                            |  2 ++
 lua54/build.gradle                            |  2 ++
 4 files changed, 26 insertions(+)

diff --git a/example/suite/src/main/java/party/iroiro/luajava/LuaTestSuite.java b/example/suite/src/main/java/party/iroiro/luajava/LuaTestSuite.java
index d80c75e5..cbe7355f 100644
--- a/example/suite/src/main/java/party/iroiro/luajava/LuaTestSuite.java
+++ b/example/suite/src/main/java/party/iroiro/luajava/LuaTestSuite.java
@@ -82,6 +82,7 @@ public void test() {
         L.openLibraries();
         LuaScriptSuite.addAssertThrows(L);
         test64BitInteger();
+        testCompat();
         testCoroutineDeadlock();
         testDump();
         testException();
@@ -117,6 +118,25 @@ private void testPCall() {
         }
     }
 
+    private void testCompat() {
+        L.run("return _VERSION");
+        String version = L.toString(-1);
+
+        switch (version) {
+            case "Lua 5.4": // LUA_COMPAT_5_3
+            case "Lua 5.3": // LUA_COMPAT_5_2
+                L.openLibrary("math");
+                L.run("return math.pow(2, 2)");
+                assertEquals(4, L.toInteger(-1));
+                break;
+            case "Lua 5.2": // LUA_COMPAT_ALL
+                L.openLibrary("math");
+                L.run("return math.log10(1)");
+                assertEquals(0, L.toInteger(-1));
+                break;
+        }
+    }
+
     private void testCoroutineDeadlock() {
         Thread t = new Thread(() -> {
             try (T L = constructor.get()) {
diff --git a/lua52/build.gradle b/lua52/build.gradle
index c949db34..ca7ae193 100644
--- a/lua52/build.gradle
+++ b/lua52/build.gradle
@@ -51,6 +51,8 @@ jnigen {
     sharedLibName = 'lua52'
 
     all {
+        cFlags += ' -DLUA_COMPAT_ALL'
+        cppFlags += ' -DLUA_COMPAT_ALL'
         headerDirs = ['../../jni/luajava', 'mod', 'lua52']
         cppExcludes = ['lua52/**/*']
         cExcludes = ['lua52/**/*']
diff --git a/lua53/build.gradle b/lua53/build.gradle
index 4f01fb32..0a23ba77 100644
--- a/lua53/build.gradle
+++ b/lua53/build.gradle
@@ -51,6 +51,8 @@ jnigen {
     sharedLibName = 'lua53'
 
     all {
+        cFlags += ' -DLUA_COMPAT_5_2'
+        cppFlags += ' -DLUA_COMPAT_5_2'
         headerDirs = ['../../jni/luajava', 'mod', 'lua53']
         cppExcludes = ['lua53/**/*']
         cExcludes = ['lua53/**/*']
diff --git a/lua54/build.gradle b/lua54/build.gradle
index bbb06257..43a12373 100644
--- a/lua54/build.gradle
+++ b/lua54/build.gradle
@@ -51,6 +51,8 @@ jnigen {
     sharedLibName = 'lua54'
 
     all {
+        cFlags += ' -DLUA_COMPAT_5_3'
+        cppFlags += ' -DLUA_COMPAT_5_3'
         headerDirs = ['../../jni/luajava', 'mod', 'lua54']
         cppExcludes = ['lua54/**/*']
         cExcludes = ['lua54/**/*']