Fixed issue: #47

This commit is contained in:
UnlegitDqrk
2026-03-02 15:31:22 +01:00
parent 63edacbb5f
commit 06c74072be
7 changed files with 120 additions and 0 deletions

View File

@@ -0,0 +1,41 @@
package org.luaj.vm2.libs.jse;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.libs.ApproxLib;
/**
* Host-side helpers for mirroring Java state into Lua without reacting to
* insignificant floating-point drift.
*/
public final class LuaStateSync {
private LuaStateSync() {
}
public static boolean setIfApproxChanged(LuaValue target, LuaValue key, double value, double epsilon) {
if (epsilon < 0d) {
throw new IllegalArgumentException("epsilon must be >= 0");
}
LuaValue current = target.get(key);
if (current.isnumber() && ApproxLib.approxEqual(current.todouble(), value, epsilon)) {
return false;
}
target.set(key, LuaValue.valueOf(value));
return true;
}
public static boolean setIfApproxChanged(LuaValue target, String key, double value, double epsilon) {
return setIfApproxChanged(target, LuaValue.valueOf(key), value, epsilon);
}
public static boolean setIfApproxChanged(LuaValue target, int key, double value, double epsilon) {
return setIfApproxChanged(target, LuaValue.valueOf(key), value, epsilon);
}
public static boolean approxEqual(double a, double b, double epsilon) {
if (epsilon < 0d) {
throw new IllegalArgumentException("epsilon must be >= 0");
}
return ApproxLib.approxEqual(a, b, epsilon);
}
}

View File

@@ -32,6 +32,7 @@ import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.luaj.vm2.libs.jse.JsePlatform;
import org.luaj.vm2.libs.ApproxLib;
import org.luaj.vm2.luajc.LuaJC;
/**
@@ -373,6 +374,22 @@ public class FragmentsTest extends TestSuite {
"end\n");
}
public void testApproxEqLibrary() {
try {
Globals globals = JsePlatform.standardGlobals();
globals.load(new ApproxLib());
Varargs result = globals.load(
"return approx_eq(1.0, 0.9999999995, 1e-8), " +
"approx.eq(1.0, 0.9, 1e-8)\n",
"approx.lua").invoke();
assertEquals(LuaValue.TRUE, result.arg1());
assertEquals(LuaValue.FALSE, result.arg(2));
} catch (Exception e) {
e.printStackTrace();
fail(e.toString());
}
}
public void testTableMove() {
runFragment(
LuaValue.varargsOf(new LuaValue[] {

View File

@@ -30,6 +30,7 @@ import junit.framework.TestCase;
import org.luaj.vm2.TypeTest.MyData;
import org.luaj.vm2.libs.ZeroArgFunction;
import org.luaj.vm2.libs.jse.JsePlatform;
import org.luaj.vm2.libs.jse.LuaStateSync;
public class LuaOperationsTest extends TestCase {
@@ -173,4 +174,14 @@ public class LuaOperationsTest extends TestCase {
assertEquals( eee, c.call() );
}
}
public void testSetIfApproxChanged() {
LuaTable target = LuaValue.tableOf();
assertTrue(LuaStateSync.setIfApproxChanged(target, "height", 1.0d, 1e-6d));
assertEquals(LuaValue.valueOf(1.0d), target.get("height"));
assertFalse(LuaStateSync.setIfApproxChanged(target, "height", 0.999999999d, 1e-6d));
assertEquals(LuaValue.valueOf(1.0d), target.get("height"));
assertTrue(LuaStateSync.setIfApproxChanged(target, "height", 1.1d, 1e-6d));
assertEquals(LuaValue.valueOf(1.1d), target.get("height"));
}
}