From 3f727fbe17f3ab73ee3865f953f7022dad80632d Mon Sep 17 00:00:00 2001 From: James Roseborough Date: Mon, 26 Apr 2010 15:59:42 +0000 Subject: [PATCH] Simplify bindings. --- src/jse/org/luaj/vm2/lib/jse/JseMathLib.java | 48 +++++++++++--------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/src/jse/org/luaj/vm2/lib/jse/JseMathLib.java b/src/jse/org/luaj/vm2/lib/jse/JseMathLib.java index 9fca4f48..955fc340 100644 --- a/src/jse/org/luaj/vm2/lib/jse/JseMathLib.java +++ b/src/jse/org/luaj/vm2/lib/jse/JseMathLib.java @@ -23,6 +23,8 @@ package org.luaj.vm2.lib.jse; import org.luaj.vm2.LuaValue; import org.luaj.vm2.lib.MathLib; +import org.luaj.vm2.lib.OneArgFunction; +import org.luaj.vm2.lib.TwoArgFunction; /** * Math library implementation for use on JSE platform. @@ -35,39 +37,41 @@ public class JseMathLib extends org.luaj.vm2.lib.MathLib { public JseMathLib() {} public LuaValue call(LuaValue arg) { - MathLib ml = new MathLib(); - ml.setfenv(env); - LuaValue t = ml.call(arg); - bind1( t, new String[] { + LuaValue t = super.call(arg); + bind( t, JseMathLib1.class, new String[] { "acos", "asin", "atan", "cosh", "exp", "log", "log10", "sinh", "tanh" } ); - bind2( t, new String[] { + bind( t, JseMathLib2.class, new String[] { "atan2", "pow", } ); return t; } - public LuaValue oncall1(int opcode, LuaValue arg) { - switch ( opcode ) { - case 0: return valueOf(Math.acos(arg.todouble())); - case 1: return valueOf(Math.asin(arg.todouble())); - case 2: return valueOf(Math.atan(arg.todouble())); - case 3: return valueOf(Math.cosh(arg.todouble())); - case 4: return valueOf(Math.exp(arg.todouble())); - case 5: return valueOf(Math.log(arg.todouble())); - case 6: return valueOf(Math.log10(arg.todouble())); - case 7: return valueOf(Math.sinh(arg.todouble())); - case 8: return valueOf(Math.tanh(arg.todouble())); + public static final class JseMathLib1 extends OneArgFunction { + public LuaValue call(LuaValue arg) { + switch ( opcode ) { + case 0: return valueOf(Math.acos(arg.todouble())); + case 1: return valueOf(Math.asin(arg.todouble())); + case 2: return valueOf(Math.atan(arg.todouble())); + case 3: return valueOf(Math.cosh(arg.todouble())); + case 4: return valueOf(Math.exp(arg.todouble())); + case 5: return valueOf(Math.log(arg.todouble())); + case 6: return valueOf(Math.log10(arg.todouble())); + case 7: return valueOf(Math.sinh(arg.todouble())); + case 8: return valueOf(Math.tanh(arg.todouble())); + } + return NIL; } - return NIL; } - public LuaValue oncall2(int opcode, LuaValue arg1, LuaValue arg2) { - switch ( opcode ) { - case 0: return valueOf(Math.atan2(arg1.todouble(), arg2.todouble())); - case 1: return valueOf(Math.pow(arg1.todouble(), arg2.todouble())); + public static final class JseMathLib2 extends TwoArgFunction { + public LuaValue call(LuaValue arg1, LuaValue arg2) { + switch ( opcode ) { + case 0: return valueOf(Math.atan2(arg1.todouble(), arg2.todouble())); + case 1: return valueOf(Math.pow(arg1.todouble(), arg2.todouble())); + } + return NIL; } - return NIL; } /** Faster, better version of pow() used by arithmetic operator ^ */