Revision: 2725
http://vexi.svn.sourceforge.net/vexi/?rev=2725&view=rev
Author: clrg
Date: 2008-01-13 13:23:02 -0800 (Sun, 13 Jan 2008)
Log Message:
-----------
Fix launchpad bug 178037 - "str"-n causing java.lang.NumberFormatException
Modified Paths:
--------------
trunk/core/org.ibex.js/src/org/ibex/js/JSU.jpp
Modified: trunk/core/org.ibex.js/src/org/ibex/js/JSU.jpp
===================================================================
--- trunk/core/org.ibex.js/src/org/ibex/js/JSU.jpp 2008-01-13 13:15:59 UTC
(rev 2724)
+++ trunk/core/org.ibex.js/src/org/ibex/js/JSU.jpp 2008-01-13 21:23:02 UTC
(rev 2725)
@@ -92,7 +92,13 @@
public static long toLong(JS o) throws JSExn {
if(o == null) return 0;
if(o instanceof JSNumber) return ((JSNumber)o).toLong();
- if(o instanceof JSString) return Long.parseLong(o.coerceToString());
+ if(o instanceof JSString) {
+ try {
+ return Long.parseLong(o.coerceToString());
+ } catch (NumberFormatException nfe) {
+ throw new JSExn("string is not a number:
\""+o.coerceToString()+'"');
+ }
+ }
throw new JSExn("can't coerce a " + o.getClass().getName() + " to a
number");
}
//#end
@@ -169,17 +175,17 @@
/** log a message with the current JavaScript sourceName/line */
private static void log(int level, Object message){
- if(message instanceof JSExn.ExnJSObj){
- message = ((JSExn.ExnJSObj)message).getJSExn();
- }else if(message instanceof JS){
- message = toString((JS)message);
- }else if(message == null){
- message = "*null*";
- }
- Interpreter cx = Scheduler.getCurrentJSInterpreter();
- String src = cx==null?null:cx.getSourceName();
- String line = cx==null?null:""+ cx.getLine();
- Log.uLog(src, line, message, level);
+ if(message instanceof JSExn.ExnJSObj){
+ message = ((JSExn.ExnJSObj)message).getJSExn();
+ }else if(message instanceof JS){
+ message = toString((JS)message);
+ }else if(message == null){
+ message = "*null*";
+ }
+ Interpreter cx = Scheduler.getCurrentJSInterpreter();
+ String src = cx==null?null:cx.getSourceName();
+ String line = cx==null?null:""+ cx.getLine();
+ Log.uLog(src, line, message, level);
}
public static void debug(Object message) { log(Log.DEBUG, message); }
@@ -193,131 +199,131 @@
static public final int FOUNTAIN = 4;
// 6, 8
- final static public void checkArgs(JS[] args, int[] types) throws JSExn{
- for(int i=0; i<args.length; i++){
- JS arg = args[i]; int type = types[i];
-
- if(arg==null){
- if((type & NULL)==0) throw new JSExn("Arg " + i
+ " cannot be null");
- else continue;
- }
- switch(type & ~NULL){
- case OBJ:
- // TODO - check probably incomplete ...
- if(arg instanceof JSPrimitive) throw new
JSExn("Arg " + i + " should be a js object");
- break;
- case FOUNTAIN:
- if(getFountain(arg)==null) throw new JSExn("Arg
" + i + " should be a FOUNTAIN");
- break;
- default:
- }
- }
- }
+ final static public void checkArgs(JS[] args, int[] types) throws JSExn{
+ for(int i=0; i<args.length; i++){
+ JS arg = args[i]; int type = types[i];
+
+ if(arg==null){
+ if((type & NULL)==0) throw new JSExn("Arg " + i + " cannot be
null");
+ else continue;
+ }
+ switch(type & ~NULL){
+ case OBJ:
+ // TODO - check probably incomplete ...
+ if(arg instanceof JSPrimitive) throw new JSExn("Arg " + i + "
should be a js object");
+ break;
+ case FOUNTAIN:
+ if(getFountain(arg)==null) throw new JSExn("Arg " + i + "
should be a FOUNTAIN");
+ break;
+ default:
+ }
+ }
+ }
/////////////
- // JSON
+ // JSON
//////////
- static JS json_marshal(JS value) throws JSExn{
- StringBuffer sb=new StringBuffer();
- json_marshal(sb,value);
- return JSU.S(sb.toString());
- }
-
- static void json_marshal(StringBuffer sb, JS value) throws JSExn{
-
- if(value==null){
- sb.append("null");
- return;
- }
- JS type = value.type();
- if(type == SC_object){
- sb.append("{");
- Enumeration ks = value.keys().iterator();
- while(ks.hasNext()){
- JS key = ks.next();
- sb.append("\"");
- sb.append(json_escape(key));
- sb.append("\":");
- json_marshal(sb,value.get(key));
- if(ks.hasNext())sb.append(",");
- }
- sb.append("}");
- }
- else if(type == SC_array){
- sb.append("[");
- JS[] arr = ((JSArrayLike)value).toArray();
- if(arr.length>0) json_marshal(sb,arr[0]);
- for(int i=1; i<arr.length; i++){
- sb.append(",");
- json_marshal(sb,arr[i]);
- }
- sb.append("]");
- }
- else if(type == SC_number || type == SC_boolean){
- sb.append(value.coerceToString());
- }
- else{
- sb.append("\"");
- sb.append(json_escape(value));
- sb.append("\"");
- }
- }
-
- /**
- * " => \" , \ => \\
- * @param s
- * @return
- */
- static String json_escape(JS s_){
- if(s_==null)
- return null;
- String s = s_.coerceToString();
- StringBuffer sb=new StringBuffer();
- for(int i=0;i<s.length();i++){
- char ch=s.charAt(i);
- switch(ch){
- case '"':
- sb.append("\\\"");
- break;
- case '\\':
- sb.append("\\\\");
- break;
- case '\b':
- sb.append("\\b");
- break;
- case '\f':
- sb.append("\\f");
- break;
- case '\n':
- sb.append("\\n");
- break;
- case '\r':
- sb.append("\\r");
- break;
- case '\t':
- sb.append("\\t");
- break;
- case '/':
- sb.append("\\/");
- break;
- default:
- if(ch>='\u0000' && ch<='\u001F'){
- String ss=Integer.toHexString(ch);
- sb.append("\\u");
- for(int k=0;k<4-ss.length();k++){
- sb.append('0');
- }
- sb.append(ss.toUpperCase());
- }
- else{
- sb.append(ch);
- }
- }
- }//for
- return sb.toString();
- }
-
-
+ static JS json_marshal(JS value) throws JSExn{
+ StringBuffer sb=new StringBuffer();
+ json_marshal(sb,value);
+ return JSU.S(sb.toString());
+ }
+
+ static void json_marshal(StringBuffer sb, JS value) throws JSExn{
+
+ if(value==null){
+ sb.append("null");
+ return;
+ }
+ JS type = value.type();
+ if(type == SC_object){
+ sb.append("{");
+ Enumeration ks = value.keys().iterator();
+ while(ks.hasNext()){
+ JS key = ks.next();
+ sb.append("\"");
+ sb.append(json_escape(key));
+ sb.append("\":");
+ json_marshal(sb,value.get(key));
+ if(ks.hasNext())sb.append(",");
+ }
+ sb.append("}");
+ }
+ else if(type == SC_array){
+ sb.append("[");
+ JS[] arr = ((JSArrayLike)value).toArray();
+ if(arr.length>0) json_marshal(sb,arr[0]);
+ for(int i=1; i<arr.length; i++){
+ sb.append(",");
+ json_marshal(sb,arr[i]);
+ }
+ sb.append("]");
+ }
+ else if(type == SC_number || type == SC_boolean){
+ sb.append(value.coerceToString());
+ }
+ else{
+ sb.append("\"");
+ sb.append(json_escape(value));
+ sb.append("\"");
+ }
+ }
+
+ /**
+ * " => \" , \ => \\
+ * @param s
+ * @return
+ */
+ static String json_escape(JS s_){
+ if(s_==null)
+ return null;
+ String s = s_.coerceToString();
+ StringBuffer sb=new StringBuffer();
+ for(int i=0;i<s.length();i++){
+ char ch=s.charAt(i);
+ switch(ch){
+ case '"':
+ sb.append("\\\"");
+ break;
+ case '\\':
+ sb.append("\\\\");
+ break;
+ case '\b':
+ sb.append("\\b");
+ break;
+ case '\f':
+ sb.append("\\f");
+ break;
+ case '\n':
+ sb.append("\\n");
+ break;
+ case '\r':
+ sb.append("\\r");
+ break;
+ case '\t':
+ sb.append("\\t");
+ break;
+ case '/':
+ sb.append("\\/");
+ break;
+ default:
+ if(ch>='\u0000' && ch<='\u001F'){
+ String ss=Integer.toHexString(ch);
+ sb.append("\\u");
+ for(int k=0;k<4-ss.length();k++){
+ sb.append('0');
+ }
+ sb.append(ss.toUpperCase());
+ }
+ else{
+ sb.append(ch);
+ }
+ }
+ }//for
+ return sb.toString();
+ }
+
+
///////
// DEBUG (consider moving to DevUtil in src_dev)
///
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
_______________________________________________
Vexi-svn mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/vexi-svn