1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
8 /** The JavaScript Math object */
9 class Math extends JS.Obj {
10 public static Math singleton = new Math();
11 Math() { setSeal(true); }
13 // FIXME: clean up, finish implementing
14 public Object get(Object name) {
15 if ("ceil".equals(name)) return new JS.Callable() { public Object call(JS.Array args)
16 { if (args.elementAt(0) == null) return null;
17 return new Long((long)java.lang.Math.ceil(Double.parseDouble(args.elementAt(0).toString()))); } };
18 else if ("floor".equals(name)) return new JS.Callable() { public Object call(JS.Array args)
19 { if (args.elementAt(0) == null) return null;
20 return new Long((long)java.lang.Math.floor(Double.parseDouble(args.elementAt(0).toString()))); } };
21 else if ("round".equals(name)) return new JS.Callable() { public Object call(JS.Array args)
22 { if (args.elementAt(0) == null) return null;
23 return new Long((long)java.lang.Math.round(Double.parseDouble(args.elementAt(0).toString()))); } };
24 else if ("abs".equals(name)) return new JS.Callable() { public Object call(JS.Array args)
25 { if (args.elementAt(0) == null) return null;
26 return new Long((long)java.lang.Math.abs(Double.parseDouble(args.elementAt(0).toString()))); } };
27 else if ("min".equals(name)) return new JS.Callable() { public Object call(JS.Array args) {
28 if (args.length() < 2 || args.elementAt(0) == null || args.elementAt(1) == null) return args.elementAt(0);
29 return new Double(java.lang.Math.min(((Number)args.elementAt(0)).doubleValue(),
30 ((Number)args.elementAt(1)).doubleValue())); } };
31 else if ("max".equals(name)) return new JS.Callable() { public Object call(JS.Array args) {
32 if (args.length() < 2) return args.elementAt(0);
33 return new Double(java.lang.Math.max(((Number)args.elementAt(0)).doubleValue(),
34 ((Number)args.elementAt(1)).doubleValue())); } };
35 else if ("cos".equals(name)) return new JS.Callable() { public Object call(JS.Array args) {
36 return new Double(java.lang.Math.cos(((Number)args.elementAt(0)).doubleValue())); } };
37 else if ("sin".equals(name)) return new JS.Callable() { public Object call(JS.Array args) {
38 return new Double(java.lang.Math.sin(((Number)args.elementAt(0)).doubleValue())); } };
39 else if ("tan".equals(name)) return new JS.Callable() { public Object call(JS.Array args) {
40 return new Double(java.lang.Math.tan(((Number)args.elementAt(0)).doubleValue())); } };
41 else if ("acos".equals(name)) return new JS.Callable() { public Object call(JS.Array args) {
42 return new Double(java.lang.Math.acos(((Number)args.elementAt(0)).doubleValue())); } };
43 else if ("asin".equals(name)) return new JS.Callable() { public Object call(JS.Array args) {
44 return new Double(java.lang.Math.asin(((Number)args.elementAt(0)).doubleValue())); } };
45 else if ("atan".equals(name)) return new JS.Callable() { public Object call(JS.Array args) {
46 return new Double(java.lang.Math.atan(((Number)args.elementAt(0)).doubleValue())); } };
47 else if ("sqrt".equals(name)) return new JS.Callable() { public Object call(JS.Array args) {
48 return new Double(java.lang.Math.sqrt(((Number)args.elementAt(0)).doubleValue())); } };