Revision: 4099 http://vexi.svn.sourceforge.net/vexi/?rev=4099&view=rev Author: clrg Date: 2011-03-29 00:05:36 +0000 (Tue, 29 Mar 2011)
Log Message: ----------- Some outstanding local changes (comments, clean up, not logic changes) Modified Paths: -------------- trunk/org.vexi-library.js/src/main/java/org/ibex/js/Methods.java trunk/org.vexi-library.js/src/main/java/org/ibex/js/Scheduler.java Modified: trunk/org.vexi-library.js/src/main/java/org/ibex/js/Methods.java =================================================================== --- trunk/org.vexi-library.js/src/main/java/org/ibex/js/Methods.java 2011-03-28 22:44:58 UTC (rev 4098) +++ trunk/org.vexi-library.js/src/main/java/org/ibex/js/Methods.java 2011-03-29 00:05:36 UTC (rev 4099) @@ -1,3 +1,7 @@ +// Copyright 2000-2011 the Contributors, as shown in the revision logs. +// Licensed under the Apache Software License 2.0 ("the License"). +// You may not use this file except in compliance with the License. + package org.ibex.js; import java.io.StringReader; @@ -7,7 +11,10 @@ import org.ibex.js.parse.Function; -public class Methods implements Constants{ +/** + * Set of static helper methods for converting between different JS types + */ +public class Methods implements Constants { /** * Decodes the passed UTF-8 String using an algorithm that's compatible with @@ -15,32 +22,41 @@ * <code>null</code> if the String is <code>null</code>. * * @param s The UTF-8 encoded String to be decoded - * @return the decoded String + * @return The decoded String */ static public JS decodeURIComponent(JS s) { - try{ - return JSU.S(URLDecoder.decode(JSU.toString(s), "UTF-8")); - }catch (UnsupportedEncodingException e){ throw new Error(e); } + try { + return JSU.S(URLDecoder.decode(JSU.toString(s), "UTF-8")); + } catch (UnsupportedEncodingException e) { throw new Error(e); } } /** * Encodes the passed String as UTF-8 using an algorithm that's compatible * with JavaScript's <code>encodeURIComponent</code> function. Returns * <code>null</code> if the String is <code>null</code>. + * + * @param s The String to be encoded + * @return The encoded String */ - static public JS encodeURIComponent(JS s) - { - try { - return JSU.S(URLEncoder.encode(JSU.toString(s), "UTF-8") + static public JS encodeURIComponent(JS s) { + try { + return JSU.S(URLEncoder.encode(JSU.toString(s), "UTF-8") .replaceAll("\\+", "%20") .replaceAll("\\%21", "!") .replaceAll("\\%27", "'") .replaceAll("\\%28", "(") .replaceAll("\\%29", ")") .replaceAll("\\%7E", "~")); - } catch (UnsupportedEncodingException e){ throw new Error(e); } - } - + } catch (UnsupportedEncodingException e) { throw new Error(e); } + } + + /** + * Parse the passed String as an Integer formatted with a given radix. + * + * @param arg JSString to parse as an integer + * @param r JSNumber used as a radix + * @return JSNumber or NaN constant + */ static public JS parseInt(JS arg, JS r) throws JSExn { int radix = JSU.toInt(r); String s = JSU.toString(arg); @@ -48,9 +64,12 @@ int sign = 1; int end = s.length(); long n = 0; - if (radix != 0 && (radix < 2 || radix > 36)) return NC_NaN; - while (start < end && Character.isWhitespace(s.charAt(start))) start++; - while (start < end && Character.isWhitespace(s.charAt(end-1))) end--; + if (radix != 0 && (radix < 2 || radix > 36)) + return NC_NaN; + while (start < end && Character.isWhitespace(s.charAt(start))) + start++; + while (start < end && Character.isWhitespace(s.charAt(end-1))) + end--; if ((end >= start+1) && (s.charAt(start) == '+' || s.charAt(start) == '-')) { sign = s.charAt(start) == '+' ? 1 : -1; start++; @@ -74,24 +93,33 @@ String s2 = start == 0 ? s : s.substring(start, end); return JSU.N(sign*Integer.parseInt(s2,radix)); } catch(NumberFormatException e) { } - // fall through to a slower but emca-compliant method + // fall through to a slower but ecma-compliant method for (int i=start;i<end;i++) { int digit = Character.digit(s.charAt(i),radix); if (digit < 0) break; n = n*radix + digit; if (n < 0) return NC_NaN; // overflow; } - if (n <= Integer.MAX_VALUE) return JSU.N(sign*(int)n); + if (n <= Integer.MAX_VALUE) + return JSU.N(sign*(int)n); return JSU.N((long)sign*n); } + /** + * Converts the passed Integer to a String formatted with a given radix. + * + * @param arg JSNumber to convert to a JSString + * @return JSNumber or NaN JS constant + */ static public JS parseFloat(JS arg) throws JSExn { String s = JSU.toString(arg); int start = 0; int length = s.length(); - while(start < length && Character.isWhitespace(s.charAt(0))) start++; + while (start < length && Character.isWhitespace(s.charAt(0))) + start++; int end = length; - while(start < end && Character.isWhitespace(s.charAt(end-1))) end--; + while (start < end && Character.isWhitespace(s.charAt(end-1))) + end--; if (start == end) return NC_NaN; try { JS ret = JSU.N(Double.parseDouble(s.substring(start,end))); @@ -99,33 +127,59 @@ } catch(NumberFormatException e) { } return NC_NaN; } - - + + /** + * Evaluates a String as JS code using an optional scope + * + * @param args An array containing + * - the JSString to evaluate as args[0] (required) + * - the evaluation scope as args[1] (optional) + * @return result of eval (may be null) + */ static public JS eval(JS[] args) throws JSExn { - JS arg = args[0]; - JS globalScope = args.length<2?new JS.Obj():args[1]; - String s = JSU.toString(arg); - Function f = ExecParser.fromReader("<eval>", 1, - new StringReader(s)); + JS arg = args[0]; + JS globalScope = args.length<2?new JS.Obj():args[1]; + String s = JSU.toString(arg); + Function f = ExecParser.fromReader("<eval>", 1, + new StringReader(s)); - return JSU.cloneWithNewGlobalScope(f, globalScope).apply(null, EMPTY_JS_ARRAY); + return JSU.cloneWithNewGlobalScope(f, globalScope).apply(null, EMPTY_JS_ARRAY); } - - static public JS fromCharCode(JS[] args) throws JSExn{ - char buf[] = new char[args.length]; - for(int i=0; i<args.length; i++) buf[i] = (char)(JSU.toInt(args[i]) & 0xffff); - return JSU.S(new String(buf)); + + /** + * Convert a set of character codes into a String + * + * @param args The series of character codes (as JSNumber) + * @return JSString + */ + static public JS fromCharCode(JS[] args) throws JSExn { + char buf[] = new char[args.length]; + for (int i=0; i<args.length; i++) + buf[i] = (char)(JSU.toInt(args[i]) & 0xffff); + return JSU.S(new String(buf)); } - - static public JS isNaN(JS arg) throws JSExn{ - double d = JSU.toDouble(arg); - return d == d ? JSU.F : JSU.T; - + + /** + * Establishes whether the given JS argument is NaN (not a number) + * + * @param arg Dynamically typed JS + * @return true if JS is NaN + */ + static public JS isNaN(JS arg) throws JSExn { + double d = JSU.toDouble(arg); + return d == d ? JSU.F : JSU.T; + } + + /** + * Establishes whether the given JS argument is finite i.e. not infinite + * + * @param arg JS (normally, but not necessarily, JSNumber) + * @return true if JS is finite + */ + static public JS isFinite(JS arg) throws JSExn { + double d = JSU.toDouble(arg); + return (d==d && !Double.isInfinite(d)) ? JSU.T : JSU.F; + } - static public JS isFinite(JS arg) throws JSExn{ - double d = JSU.toDouble(arg); - return (d==d && !Double.isInfinite(d)) ? JSU.T : JSU.F; - } - -} \ No newline at end of file +} Modified: trunk/org.vexi-library.js/src/main/java/org/ibex/js/Scheduler.java =================================================================== --- trunk/org.vexi-library.js/src/main/java/org/ibex/js/Scheduler.java 2011-03-28 22:44:58 UTC (rev 4098) +++ trunk/org.vexi-library.js/src/main/java/org/ibex/js/Scheduler.java 2011-03-29 00:05:36 UTC (rev 4099) @@ -2,7 +2,6 @@ // Licensed under the Apache Software License 2.0 ("the License"). // You may not use this file except in compliance with the License. - package org.ibex.js; import org.ibex.js.JS.Trap; @@ -52,7 +51,7 @@ // scheduler runs or from a js thread. private int forceActive = 0; public void incForceActive(){ - forceActive++; + forceActive++; } synchronized public void decForceActive() { forceActive--; @@ -75,7 +74,7 @@ runnable.append(t); } - /** synchronizd so that we can safely call it from an event-delivery thread, in-context */ + /** synchronized so that we can safely call it from an event-delivery thread, in-context */ synchronized public void renderAll() {} public boolean inactive() { return runnable.size()==0 && forceActive==0; } @@ -135,7 +134,7 @@ // if an Error is thrown it will cause the engine to quit } } finally { - scheduleWakeUp.interrupt(); + scheduleWakeUp.interrupt(); threadlocal.set(null); } } @@ -268,7 +267,7 @@ jsthread.currentInterpreter = new Interpreter(jsthread, t, null, true, trapname); jsthread.currentInterpreter.old = old; // REMARK - this thread is unpausable, so setting this static variable - // cannot be interefered with by other js executions. Could be done in the + // cannot be interfered with by other js executions. Could be done in the // interpreter when returning instead of cascading as well. cascadedTo = null; return (JS)jsthread.currentInterpreter.run(null); @@ -422,7 +421,7 @@ if (i<0) { add(callback); } else { - scheduleWakeUp.insert(i, callback); + scheduleWakeUp.insert(i, callback); } } catch (JSExn e) { throw new JSExn("You cannot sleep or yield in the foreground thread"); @@ -449,8 +448,8 @@ synchronized (this) { // store sleeper threads in order of wake up time for (int j=sleeperThreads.size(); j>=0; j--) { - if (j==0) { - // add as the first thread + if (j==0) { + // add as the first thread sleeperThreads.add(0, t); // been added at the start so must interrupt the wakeup thread // because it is either waiting or waking up a later js thread @@ -458,17 +457,17 @@ scheduleWakeUp.notify(); break; } else if (t.wakeupms > ((SleeperCallback)sleeperThreads.get(j-1)).wakeupms) { - // insert at this point + // insert at this point sleeperThreads.add(j, t); break; } } } - } - public void run() { + } + public void run() { try { synchronized (this) { - while (true) { + while (true) { // nothing to do at this point, await interruption wait(); while (sleeperThreads.size()>0) { @@ -495,7 +494,7 @@ * expects a value to be pushed onto the stack when unpaused. */ // REMARK - pausing is not (currently) possible in nested interpreters (as // we do not deal with potential parent interpreters when we reschedule. - // A JSExn compaining about the foreground thread will be thrown, but this + // A JSExn complaining about the foreground thread will be thrown, but this // will not be accurate. (e.g. load template from a background thread) public Callable pauseJSThread() throws JSExn { jsthread.currentInterpreter.pause(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ Create and publish websites with WebMatrix Use the most popular FREE web apps or write code yourself; WebMatrix provides all the features you need to develop and publish your website. http://p.sf.net/sfu/ms-webmatrix-sf _______________________________________________ Vexi-svn mailing list Vexi-svn@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/vexi-svn