- private static final JSFunction newBrowserWindow = new JSFunction() {
- public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
- if (args.length != 1 || args[0] == null) return null;
- Platform.newBrowserWindow(args[0].toString());
- return null;
- }
- };
-
- private static final JSFunction yield = new JSFunction() {
- public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
- sleep.call(cx, null, null, null);
- return null;
- }
- };
-
- private static final JSFunction println = new JSFunction() {
- public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
- if (args.length == 1)
- if (Log.on)
- Log.log(cx.interpreterSourceFile, args[0] == null ? "null" : args[0].toString());
- return null;
- }
- };
-
- private static final JSFunction date = new JSFunction() {
- public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
- try { return Context.enter().newObject(org.xwt.util.JSObject.defaultObjects, "Date", args);
- } catch (Exception e) {
- if (Log.on) Log.log(this, "Exception in Context.newObject() -- this should never happen");
- if (Log.on) Log.log(this, e);
- return null;
- }
- }
- };
-
- private static final JSFunction regexp = new JSFunction() {
- public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
- try { return Context.enter().newObject(org.xwt.util.JSObject.defaultObjects, "RegExp", args);
- } catch (Exception e) {
- if (Log.on) Log.log(this, "Exception in Context.newObject() -- this should never happen");
- if (Log.on) Log.log(this, e);
- return null;
- }
- }
- };
-
- private static final JSFunction listfonts = new JSFunction() {
- public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
- return Context.enter().newArray(org.xwt.util.JSObject.defaultObjects, Platform.listFonts());
- }
- };
-
- private static final JSFunction theme = new JSFunction() {
- public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
- if (args.length != 2) return null;
- if (args[0] == null || args[1] == null) return null;
- Template.retheme(args[0].toString(), args[1].toString());
- return null;
- }
- };
-
- private static final JSFunction xmlrpc = new JSFunction() {
- public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
- if (args.length != 1 || args[0] == null) return null;
- return new XMLRPC(args[0].toString(), "");
- }
- };
-
- private static final JSFunction soap = new JSFunction() {
- public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
- if (args.length == 1 && args[0] != null) return new SOAP(args[0].toString(), "", null, null);
- else if (args.length == 2 && args[0] != null && args[1] != null)
- return new SOAP(args[0].toString(), "", args[1].toString(), null);
- else if (args.length == 3 && args[0] != null && args[1] != null && args[2] != null)
- return new SOAP(args[0].toString(), "", args[1].toString(), args[2].toString());
- else return null;
- }
- };
-
- private static final JSFunction textwidth = new JSFunction() {
- public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) throws JavaScriptException {
- if (args.length < 1 || args.length > 2) return null;
- if (args[0] == null || (args.length == 2 && args[1] == null)) return null;
- String font = args.length == 1 ? Platform.getDefaultFont() : args[0].toString();
- String text = args.length == 1 ? args[0].toString() : args[1].toString();
- XWF xwf = XWF.getXWF(font);
- if (xwf == null) return new Integer(Platform.stringWidth(font, text));
- else return new Integer(xwf.stringWidth(text));
- }
- };
-
-
- private static final JSFunction textheight = new JSFunction() {
- public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
- if (args.length > 1) return null;
- if (args.length == 1 && args[0] == null) return null;
- String font = args.length == 0 || args[0] == null ? Platform.getDefaultFont() : args[0].toString();
- XWF xwf = XWF.getXWF(font);
- if (xwf == null) return new Integer(Platform.getMaxAscent(font) + Platform.getMaxDescent(font));
- else return new Integer(xwf.getMaxAscent() + xwf.getMaxDescent());
- }
- };
-
- private static final JSFunction newBox = new JSFunction() {
- public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
- Box ret = new Box(args.length == 0 || args[0] == null ? "box" : args[0].toString(), Template.defaultImportList);
- for(int i=1; i<args.length; i++)
- if (args[i] instanceof Box)
- ret.put(ret.numChildren(), null, (Box)args[i]);
- for(int i=1; i<args.length; i++)
- if (args[i] instanceof Scriptable && !(args[i] instanceof Box)) {
- Scriptable s = (Scriptable)args[i];
- Object[] keys = s.getIds();
-
- // FIXME: need to ensure that this is putGlobally(), but still run traps...
- for(int j=0; j<keys.length; j++) ret.put(keys[j].toString(), null, s.get(keys[j].toString(), s));
- }
- return ret;
- }
- };
-
- private static final JSFunction sleep = new JSFunction() {
- public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
- if (args != null && (args.length != 1 || args[0] == null)) return null;
- int i = args == null ? 0 : SpecialBoxProperty.stoi(args[0].toString());
-
- Thread thread = Thread.currentThread();
- if (!(thread instanceof ThreadMessage)) {
- if (Log.on) Log.log(this, "cannot sleep() or yield() in the foreground thread");
- return null;
- }
- ThreadMessage mythread = (ThreadMessage)thread;
- mythread.done.release();
-
- if (i > 0) try { Thread.sleep(i); } catch (Exception e) { }
-
- MessageQueue.add(mythread);
- mythread.go.block();