Mercurial Hosting > luan
comparison src/luan/Lua.java @ 36:2a35154aec14
implement more basic lib functions
git-svn-id: https://luan-java.googlecode.com/svn/trunk@37 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Tue, 18 Dec 2012 09:53:42 +0000 |
parents | e51906de0f11 |
children | 8a57ebfdfd78 |
comparison
equal
deleted
inserted
replaced
35:e51906de0f11 | 36:2a35154aec14 |
---|---|
18 | 18 |
19 public static boolean toBoolean(Object obj) { | 19 public static boolean toBoolean(Object obj) { |
20 return obj != null && !Boolean.FALSE.equals(obj); | 20 return obj != null && !Boolean.FALSE.equals(obj); |
21 } | 21 } |
22 | 22 |
23 public static String toString(Object obj) { | |
24 if( obj == null ) | |
25 return "nil"; | |
26 return obj.toString(); | |
27 } | |
28 | |
29 public static String asString(Object obj) { | 23 public static String asString(Object obj) { |
30 if( obj instanceof String || obj instanceof LuaNumber ) | 24 if( obj instanceof String || obj instanceof LuaNumber ) |
31 return obj.toString(); | 25 return obj.toString(); |
32 return null; | 26 return null; |
33 } | 27 } |
38 throw new LuaException( "attempt to use a " + Lua.type(obj) + " as a string" ); | 32 throw new LuaException( "attempt to use a " + Lua.type(obj) + " as a string" ); |
39 return s; | 33 return s; |
40 } | 34 } |
41 | 35 |
42 public static LuaNumber toNumber(Object obj) { | 36 public static LuaNumber toNumber(Object obj) { |
37 return toNumber(obj,null); | |
38 } | |
39 | |
40 public static LuaNumber toNumber(Object obj,Integer base) { | |
43 if( obj instanceof LuaNumber ) | 41 if( obj instanceof LuaNumber ) |
44 return (LuaNumber)obj; | 42 return (LuaNumber)obj; |
45 if( obj instanceof String ) { | 43 if( obj instanceof String ) { |
46 String s = (String)obj; | 44 String s = (String)obj; |
47 try { | 45 try { |
48 return new LuaNumber( Double.parseDouble(s) ); | 46 if( base==null ) |
47 return new LuaNumber( Double.parseDouble(s) ); | |
48 else | |
49 return new LuaNumber( Long.parseLong(s,base) ); | |
49 } catch(NumberFormatException e) {} | 50 } catch(NumberFormatException e) {} |
50 } | 51 } |
51 return null; | 52 return null; |
52 } | 53 } |
53 | 54 |