X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fxwt%2Fjs%2FJSMath.java;h=75cb48adc83ea12bac5dcddafc8ed7bc4588f623;hb=de378041d5ca2aca1a2b5a31ef15ae90a86c977f;hp=91a8a9ecc8c3c0b98c5cf03ed0dbda645d5ad320;hpb=d3ccf61eac01b22a5133b876b9e19581015de13e;p=org.ibex.core.git diff --git a/src/org/xwt/js/JSMath.java b/src/org/xwt/js/JSMath.java index 91a8a9e..75cb48a 100644 --- a/src/org/xwt/js/JSMath.java +++ b/src/org/xwt/js/JSMath.java @@ -1,4 +1,4 @@ -// Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL] +// Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL ] package org.xwt.js; import org.xwt.util.*; @@ -6,39 +6,10 @@ import java.io.*; import java.util.*; /** The JavaScript Math object */ -public class JSMath extends JSCallable { +public class JSMath extends JS { public static JSMath singleton = new JSMath(); - public Object call(Object method, JSArray args) { - if (method == null) throw new JS.Exn("you cannot call this object"); - //#switch(method) - case "ceil": return new Long((long)java.lang.Math.ceil(JS.toNumber(args.elementAt(0)).doubleValue())); - case "floor": return new Long((long)java.lang.Math.floor(JS.toNumber(args.elementAt(0)).doubleValue())); - case "round": return new Long((long)java.lang.Math.round(JS.toNumber(args.elementAt(0)).doubleValue())); - case "min": return new Double(java.lang.Math.min(JS.toNumber(args.elementAt(0)).doubleValue(), - JS.toNumber(args.elementAt(1)).doubleValue())); - case "max": return new Double(java.lang.Math.max(JS.toNumber(args.elementAt(0)).doubleValue(), - JS.toNumber(args.elementAt(1)).doubleValue())); - case "pow": return new Double(java.lang.Math.pow(JS.toNumber(args.elementAt(0)).doubleValue(), - JS.toNumber(args.elementAt(1)).doubleValue())); - case "atan2": return new Double(java.lang.Math.atan2(JS.toNumber(args.elementAt(0)).doubleValue(), - JS.toNumber(args.elementAt(1)).doubleValue())); - case "abs": return new Double(java.lang.Math.abs(JS.toNumber(args.elementAt(0)).doubleValue())); - case "sin": return new Double(java.lang.Math.sin(JS.toNumber(args.elementAt(0)).doubleValue())); - case "cos": return new Double(java.lang.Math.cos(JS.toNumber(args.elementAt(0)).doubleValue())); - case "tan": return new Double(java.lang.Math.tan(JS.toNumber(args.elementAt(0)).doubleValue())); - case "asin": return new Double(java.lang.Math.asin(JS.toNumber(args.elementAt(0)).doubleValue())); - case "acos": return new Double(java.lang.Math.acos(JS.toNumber(args.elementAt(0)).doubleValue())); - case "atan": return new Double(java.lang.Math.atan(JS.toNumber(args.elementAt(0)).doubleValue())); - case "sqrt": return new Double(java.lang.Math.sqrt(JS.toNumber(args.elementAt(0)).doubleValue())); - case "exp": return new Double(java.lang.Math.exp(JS.toNumber(args.elementAt(0)).doubleValue())); - case "log": return new Double(java.lang.Math.log(JS.toNumber(args.elementAt(0)).doubleValue())); - case "random": return new Double(java.lang.Math.random()); - //#end - return null; - } - private static final Double E = new Double(java.lang.Math.E); private static final Double PI = new Double(java.lang.Math.PI); private static final Double LN10 = new Double(java.lang.Math.log(10)); @@ -48,9 +19,48 @@ public class JSMath extends JSCallable { private static final Double SQRT1_2 = new Double(1/java.lang.Math.sqrt(2)); private static final Double SQRT2 = new Double(java.lang.Math.sqrt(2)); + public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn { + switch(nargs) { + case 0: { + //#switch(method) + case "random": return new Double(java.lang.Math.random()); + //#end + break; + } + case 1: { + //#switch(method) + case "ceil": return new Long((long)java.lang.Math.ceil(toDouble(a0))); + case "floor": return new Long((long)java.lang.Math.floor(toDouble(a0))); + case "round": return new Long((long)java.lang.Math.round(toDouble(a0))); + case "abs": return new Double(java.lang.Math.abs(toDouble(a0))); + case "sin": return new Double(java.lang.Math.sin(toDouble(a0))); + case "cos": return new Double(java.lang.Math.cos(toDouble(a0))); + case "tan": return new Double(java.lang.Math.tan(toDouble(a0))); + case "asin": return new Double(java.lang.Math.asin(toDouble(a0))); + case "acos": return new Double(java.lang.Math.acos(toDouble(a0))); + case "atan": return new Double(java.lang.Math.atan(toDouble(a0))); + case "sqrt": return new Double(java.lang.Math.sqrt(toDouble(a0))); + case "exp": return new Double(java.lang.Math.exp(toDouble(a0))); + case "log": return new Double(java.lang.Math.log(toDouble(a0))); + //#end + break; + } + case 2: { + //#switch(method) + case "min": return new Double(java.lang.Math.min(toDouble(a0), toDouble(a1))); + case "max": return new Double(java.lang.Math.max(toDouble(a0), toDouble(a1))); + case "pow": return new Double(java.lang.Math.pow(toDouble(a0), toDouble(a1))); + case "atan2": return new Double(java.lang.Math.atan2(toDouble(a0), toDouble(a1))); + //#end + break; + } + } + return super.callMethod(method, a0, a1, a2, rest, nargs); + } + public void put(Object key, Object val) { return; } - public Object get(Object key, Object val) { + public Object get(Object key) throws JSExn { //#switch(key) case "E": return E; case "LN10": return LN10; @@ -60,7 +70,25 @@ public class JSMath extends JSCallable { case "PI": return PI; case "SQRT1_2": return SQRT1_2; case "SQRT2": return SQRT2; + case "ceil": return METHOD; + case "floor": return METHOD; + case "round": return METHOD; + case "min": return METHOD; + case "max": return METHOD; + case "pow": return METHOD; + case "atan2": return METHOD; + case "abs": return METHOD; + case "sin": return METHOD; + case "cos": return METHOD; + case "tan": return METHOD; + case "asin": return METHOD; + case "acos": return METHOD; + case "atan": return METHOD; + case "sqrt": return METHOD; + case "exp": return METHOD; + case "log": return METHOD; + case "random": return METHOD; //#end - return null; + return super.get(key); } }