From 8ea2f2734a60f0c4fa9c82083469165283a8b810 Mon Sep 17 00:00:00 2001 From: megacz Date: Fri, 30 Jan 2004 07:36:05 +0000 Subject: [PATCH] 2003/09/21 10:26:53 darcs-hash:20040130073605-2ba56-2ea857d984104433a980345567169c914c18cbbe.gz --- Makefile | 2 +- src/org/xwt/Box.java.pp | 56 +++------- src/org/xwt/HTTP.java | 2 +- src/org/xwt/Main.java | 23 +++- src/org/xwt/Platform.java | 10 -- src/org/xwt/Res.java | 13 +++ src/org/xwt/Template.java | 132 ++++++---------------- src/org/xwt/Trap.java | 175 +++++++++--------------------- src/org/xwt/Weak.java | 9 -- src/org/xwt/XWT.java | 22 +--- src/org/xwt/js/CompiledFunctionImpl.java | 70 ++++++++---- src/org/xwt/js/JS.java | 1 + src/org/xwt/js/Parser.java | 15 ++- 13 files changed, 202 insertions(+), 328 deletions(-) delete mode 100644 src/org/xwt/Weak.java diff --git a/Makefile b/Makefile index 7d0cb52..d3a6b13 100644 --- a/Makefile +++ b/Makefile @@ -116,7 +116,7 @@ jpeg_sources += jquant1.c jquant2.c jerror.c jutils.c jmemnobs.c jmemmgr.c upstream/jpeg-6b/build-$(target)/libjpeg.a: .install_jpeg-6b_$(target) java_objects := $(nonplat_java_sources:build/java/%.java=build/$(platform)/%.java.o) -build/$(platform)/$(platform).ar: $(java_objects) build/$(platform)/org/xwt/plat/$(platform).cc.o build/$(platform)/org/xwt/builtin.res.o build/$(platform)/freetype.res.o $(plat_java_sources:build/java/%.java=build/$(platform)/%.java.o) +build/$(platform)/$(platform).ar: $(java_objects) build/$(platform)/org/xwt/plat/$(platform).cc.o build/$(platform)/builtin.o $(plat_java_sources:build/java/%.java=build/$(platform)/%.java.o) @echo -e "\n\033[1marchiving .o -> .a\033[0m" mkdir -p build/$(platform) upstream/install/bin/$(target)-ar rc $@ $? diff --git a/src/org/xwt/Box.java.pp b/src/org/xwt/Box.java.pp index 245a2c1..eb8c0c0 100644 --- a/src/org/xwt/Box.java.pp +++ b/src/org/xwt/Box.java.pp @@ -105,6 +105,7 @@ public final class Box extends JS.Scope { * changes. */ static int FONT_CHANGED_FLAG = 0x00000100; + static int ISROOT_FLAG = 0x00000200; static int ALIGN_FLAG = 0x00000000; static int FIXEDASPECT_FLAG = 0x00000000; @@ -477,7 +478,6 @@ public final class Box extends JS.Scope { // FIXME: clipping char c = text.charAt(i); Glyph g = Glyph.getGlyph(font, fontsize, c); - System.out.println("rendering glyph for " + c + " as " + g + " @ " + (x+hpad) + ", " + (y+vpad)); buf.drawPicture(g.p, x + hpad, y + vpad + g.max_ascent - g.baseline, @@ -512,7 +512,9 @@ public final class Box extends JS.Scope { Template t = Template.buildTemplate(res.getInputStream(), "fromResource"); if (ThreadMessage.suspendThread()) try { JS.Callable callback = args.length() < 2 ? null : (Callable)args.elementAt(1); - t.apply(this, null, null, callback, 0, t.numUnits()); + + // FIXME!!! needs to be xwt.apply(template, box) + t.apply(this, null, null, callback, 0, t.numUnits(), null); } finally { ThreadMessage.resumeThread(); } @@ -527,7 +529,8 @@ public final class Box extends JS.Scope { } else { if (ThreadMessage.suspendThread()) try { JS.Callable callback = args.length() < 2 ? null : (Callable)args.elementAt(1); - t.apply(this, null, null, callback, 0, t.numUnits()); + // FIXME!!! needs to be xwt.apply(template, box) + t.apply(this, null, null, callback, 0, t.numUnits(), null); } finally { ThreadMessage.resumeThread(); } @@ -649,17 +652,9 @@ public final class Box extends JS.Scope { String name = (String)name_; if (name.equals("")) return null; - // See if we're reading back the function value of a trap - if (name.charAt(0) == '_') { - if (name.charAt(1) == '_') name = name.substring(2); - else name = name.substring(1); - Trap t = Trap.getTrap(this, name); - return t == null ? null : t.f; - } - // See if we're triggering a trap Trap t = traps == null || ignoretraps ? null : (Trap)traps.get(name); - if (t != null && t.isreadtrap) return t.perform(Trap.emptyargs); + if (t != null) return t.perform(); // Check for a special handler SpecialBoxProperty gph = (SpecialBoxProperty)SpecialBoxProperty.specialBoxProperties.get(name); @@ -677,6 +672,10 @@ public final class Box extends JS.Scope { return ret; } + public void addTrap(Object name, Object value) { Trap.addTrap(this, name, ((JS.CompiledFunction)value)); } + public void delTrap(Object name, Object value) { Trap.delTrap(this, name, ((JS.CompiledFunction)value)); } + + /** * Scriptable.put() * @param ignoretraps if set, no traps will be triggered (set when 'cascade' reaches the bottom of the trap stack) @@ -692,10 +691,7 @@ public final class Box extends JS.Scope { if (!ignoretraps && traps != null) { Trap t = (Trap)traps.get(name); if (t != null) { - JS.Array arg = new JS.Array(); - arg.addElement(value); - t.perform(arg); - arg.setElementAt(null, 0); + t.perform(value); return; } } @@ -706,25 +702,6 @@ public final class Box extends JS.Scope { SpecialBoxProperty gph = (SpecialBoxProperty)SpecialBoxProperty.specialBoxProperties.get(name); if (gph != null) { gph.put(name, this, value); return; } - if (name.charAt(0) == '_') { - if (value != null && !(value instanceof JS.Callable)) { - if (Log.on) Log.logJS(this, "attempt to put a non function value (" + value + ") to " + name); - } else if (value != null && !(value instanceof JS.CompiledFunction)) { - if (Log.on) Log.logJS(this, "attempt to put a non-compiled function value (" + value + ") to " + name); - } else if (name.charAt(1) == '_') { - name = name.substring(2).intern(); - Trap t = Trap.getTrap(this, name); - if (t != null) t.delete(); - if (value != null) Trap.addTrap(this, name, ((JS.CompiledFunction)value), true, rp); - } else { - name = name.substring(1).intern(); - Trap t = Trap.getTrap(this, name); - if (t != null) t.delete(); - if (value != null) Trap.addTrap(this, name, ((JS.CompiledFunction)value), false, rp); - } - return; - } - super.put(name, value); } @@ -1312,10 +1289,13 @@ public final class Box extends JS.Scope { specialBoxProperties.put("DoubleClick3", mouseEventHandler); specialBoxProperties.put("root", new SpecialBoxProperty() { + public void put(Box b, Object value) { + if (stob(value)) b.flags |= ISROOT_FLAG; + else b.flags &= ~ISROOT_FLAG; + } public Object get(Box b) { - if (b.getRoot() == null) return null; - else if (b.parent == null) return b; - else return b.getRoot().getRootProxy(); + if (b.parent == null || ((b.flags & ISROOT_FLAG) != 0)) return b; + else return get(b.parent); } }); specialBoxProperties.put("Minimized", new SpecialBoxProperty() { diff --git a/src/org/xwt/HTTP.java b/src/org/xwt/HTTP.java index 3dc73da..a5c7114 100644 --- a/src/org/xwt/HTTP.java +++ b/src/org/xwt/HTTP.java @@ -774,7 +774,7 @@ public class HTTP { MessageQueue.add(new Message() { public void perform() { Box b = new Box(); - Template.getTemplate("org.xwt.builtin.proxy_authorization", null).apply(b, null, null, null, 0, 0); + Template.getTemplate("org.xwt.builtin.proxy_authorization", null).apply(b, null, null, null, 0, 0, null); b.put("realm", realm); b.put("proxyIP", proxyIP); } diff --git a/src/org/xwt/Main.java b/src/org/xwt/Main.java index 4f7ec28..d55d9bb 100644 --- a/src/org/xwt/Main.java +++ b/src/org/xwt/Main.java @@ -73,10 +73,12 @@ public class Main { Surface.scarPicture = Platform.createPicture(png.getData(), png.getWidth(), png.getHeight()); String initialTemplate = "main"; + Res initialRR = null; if (args.length > startargs) { if (args[startargs].startsWith("http://")) { if (Log.on) Log.log(Main.class, "downloading xwar"); + initialRR = Res.stringToRes("file:" + args[startargs] + "!"); origin = args[startargs]; initialTemplate = "org.xwt.builtin.splash"; @@ -90,9 +92,10 @@ public class Main { File f = new File(args[startargs]); origin = "file:" + f.getAbsolutePath(); if (f.isDirectory()) { - Resources.loadDirectory(f); + initialRR = Res.stringToRes("file:" + args[startargs]); Surface.scarAllSurfacesFromNowOn = true; } else { + initialRR = Res.stringToRes("file:" + args[startargs] + "!"); initialTemplate = "org.xwt.builtin.splash"; } @@ -102,14 +105,22 @@ public class Main { if (Log.on) Log.log(Main.class, "instantiating " + initialTemplate); final String initialTemplate_f = initialTemplate; + final Res initialRR_f = initialRR; ThreadMessage.newthread(new JS.Callable() { public Object call(JS.Array args) throws JS.Exn { Box b = new Box(); - Template.getTemplate(initialTemplate_f, null).apply(b, null, null, null, 0, 0); - doneInitializing = true; - if (Surface.allSurfaces.size() == 0) { - Log.log(this, "exiting because all surfaces are gone"); - Platform.exit(); + try { + Template t = Template.buildTemplate(Res.stringToRes("file:.").getInputStream(), "RESOURCE"); + t.apply(b, (Vec)null, (Vec)null, (JS.Callable)null, 0, 0, initialRR_f); + //FIXME + //Template.getTemplate(initialTemplate_f, null).apply(b, (Vec)null, (Vec)null, (JS.Callable)null, 0, 0, initialRR_f); + doneInitializing = true; + if (Surface.allSurfaces.size() == 0) { + Log.log(this, "exiting because all surfaces are gone"); + Platform.exit(); + } + } catch (Exception e) { + Log.log(Main.class, e); } return null; } diff --git a/src/org/xwt/Platform.java b/src/org/xwt/Platform.java index 30b46e1..049404e 100644 --- a/src/org/xwt/Platform.java +++ b/src/org/xwt/Platform.java @@ -170,13 +170,6 @@ public class Platform { /** returns the maximum number of threads that the XWT engine can create without adversely affecting the host OS */ protected int _maxThreads() { return 25; } - /** creates a weak reference */ - protected org.xwt.Weak _getWeak(final Object o) { - return new org.xwt.Weak() { - public Object get() { return o; } - }; - } - /** Called once XWT is initialized and the application is running. */ protected void _running() {} @@ -275,9 +268,6 @@ public class Platform { /** returns a list of all platform-specific fonts available */ public static String[] listFonts() { return platform._listFonts(); } - /** creates a weak reference */ - public static org.xwt.Weak getWeak(Object o) { return platform._getWeak(o); } - /** opens a connection to the resource identified by URL u, and returns an InputStream */ public static InputStream urlToInputStream(URL u) throws IOException { return platform._urlToInputStream(u); } diff --git a/src/org/xwt/Res.java b/src/org/xwt/Res.java index b956931..402f2d6 100644 --- a/src/org/xwt/Res.java +++ b/src/org/xwt/Res.java @@ -17,6 +17,19 @@ public abstract class Res extends JS { private Hash refCache = null; public Object get(Object key) { + if ("".equals(key)) { + try { + Res who = this; + //who = addExtension(".xwt"); + // FIXME: cache templates by the Res that created them, not their nodename + Template.buildTemplate(who.getInputStream(), "Resource"); + // FIXME: return the static here + return null; + } catch (IOException e) { + Log.logJS(this, e); + return null; + } + } Object ret = refCache == null ? null : refCache.get(key); if (ret != null) return ret; ret = new Ref(this, key); diff --git a/src/org/xwt/Template.java b/src/org/xwt/Template.java index ee0bea5..11bb1fc 100644 --- a/src/org/xwt/Template.java +++ b/src/org/xwt/Template.java @@ -165,7 +165,7 @@ public class Template { * @param pboxes a vector of all box parents on which to put $-references * @param ptemplates a vector of the nodeNames to recieve private references on the pboxes */ - void apply(Box b, Vec pboxes, Vec ptemplates, JS.Callable callback, int numerator, int denominator) { + void apply(Box b, Vec pboxes, Vec ptemplates, JS.Callable callback, int numerator, int denominator, Res resourceRoot) { int original_numerator = numerator; @@ -192,13 +192,13 @@ public class Template { for(int i=0; _preapply != null && i<_preapply.length; i++) if (_preapply[i] != null) { - _preapply[i].apply(b, null, null, callback, numerator, denominator); + _preapply[i].apply(b, null, null, callback, numerator, denominator, resourceRoot); numerator += _preapply[i].numUnits(); } for (int i=0; children != null && i elements of any - // of the templates which would be applied in step 7." - Vec keys = new Vec(); - b.template.gatherPreserves(keys); - Object[] vals = new Object[keys.size()]; - for(int i=0; i= 0; i--) { - kids[i] = b.redirect.getChild(i); - kids[i].remove(); - } - } - - // Ref 7.5.4: "Set the box's redirect target to self" - b.redirect = b; - - // Ref 7.5.5: "Remove all of the box's immediate children" - for(Box cur = b.getChild(b.numChildren() - 1); cur != null;) { - Box oldcur = cur; - cur = cur.prevSibling(); - oldcur.remove(); - } - - // Ref 7.5.6: "Remove all traps set by scripts run during the application of any template to this box" - Trap.removeAllTrapsByBox(b); - - // Ref 7.5.7: "Apply the template to the box according to the usual application procedure" - b.template.apply(b, null, null, null, 0, 1); - - // Ref 7.5.8: "Re-add the saved children which were removed in step 3" - for(int i=0; kids != null && ichange as needed */ void link() { link(false); } @@ -701,6 +608,35 @@ public class Template { } } + private static class PerInstantiationScope extends JS.Scope { + Res resourceRoot = null; + public PerInstantiationScope(Scope parentScope, Res resourceRoot) { + super(parentScope); + this.resourceRoot = resourceRoot; + } + public boolean isTransparent() { return true; } + public boolean has(Object key) { return false; } + public void declare(String s) { super.declare(s); } + public Object get(Object key) { + // FIXME: access statics here + if (Box.SpecialBoxProperty.specialBoxProperties.get(key) == null && + !super.has(key)) { + Object ret = resourceRoot.get(key); + if (ret != null) return ret; + throw new JS.Exn("must declare " + key + " before using it!"); + } + return super.get(key); + } + public void put(Object key, Object val) { + // FIXME: access statics here + if (Box.SpecialBoxProperty.specialBoxProperties.get(key) == null && + !super.has(key)) { + throw new JS.Exn("must declare " + key + " before using it!"); + } + super.put(key, val); + } + } + } diff --git a/src/org/xwt/Trap.java b/src/org/xwt/Trap.java index 5f803cb..3e6302b 100644 --- a/src/org/xwt/Trap.java +++ b/src/org/xwt/Trap.java @@ -15,9 +15,6 @@ public class Trap { // Static Data ////////////////////////////////////////////////////////////// - /** a vector of weak references to all instances of Trap; used for retheming */ - private static Hashtable allTraps = new Hashtable(1000); - /** List of properties that cannot be trapped */ private static final Hash PROHIBITED = new Hash(120, 3); @@ -32,34 +29,20 @@ public class Trap { for(int i=0; iname of box b by the currently-running script */ - public static Trap getTrap(Box b, String name) { - if (b.traps == null) return null; - - String currentFunctionNodeName = JS.Thread.fromJavaThread(java.lang.Thread.currentThread()).getCurrentCompiledFunction().getSourceName(); - for(Trap cur = (Trap)b.traps.get(name); cur != null; cur = cur.next) - if (cur.placerNodeName.equals(currentFunctionNodeName)) - return cur; - return null; + /** + * deletes a trap. + * @param trapee the box to remove the trap from + * @param name the name of the property to trap on + * @param f the function to remove + */ + static void delTrap(Box trapee, Object name, JS.CompiledFunction f) { + if (trapee.traps != null) { + Trap t = (Trap)trapee.traps.get(name); + if (t.f == f) { + trapee.traps.put(name, t.next); + return; + } + for(; t.next != null; t = t.next) + if (t.next.f == f) { + t.next = t.next.next; + return; + } + } + Log.logJS("warning: tried to remove a trap that had not been placed"); } + // Instance Methods ////////////////////////////////////////////////////////////////////////// - private Trap() { allTraps.put(myWeak, dummy); } + private Trap() { } /** the empty object, used for get-traps */ public static JS.Array emptyargs = new JS.Array(); - /** perform this trap -- arg.length == 0 if this is a get; otherwise it contains a single element to be put */ - public Object perform(JS.Array jsArrayArgs) throws JS.Exn { - // TrapContext tc = TrapContext.get(); - if (jsArrayArgs == null) jsArrayArgs = emptyargs; - TrapArgs args = new TrapArgs(this,jsArrayArgs); - - // invoke the trap function - try { - if (!isreadtrap && args.length() == 0) return cascade(args); - - if (f == null) { - if (Log.verbose) Log.log(this, "debug: reclaimed a dangling trap on property " + name); - Object ret = cascade(args); - delete(); - return ret; - } - - Object ret = f.call(args); - - // autocascade if required - if(args.length() > 0 && !isreadtrap && !args.cascadeHappened) cascade(args); - - return ret; - - } finally { - - } + public Object perform() throws JS.Exn { + if (f.getNumFormalArgs() > 0) return cascade(); + return f.call(new TrapArgs(this)); } - public Object cascade(JS.Array args) { - // if we've hit the end of the trap stack, just do a put(,,,true) - if (next == null) { - if (args.length() == 0) return trapee.get(name, true); - trapee.put(name, args.elementAt(0), true); - return null; - } - return next.perform(args); + public void perform(Object val) throws JS.Exn { + if (f.getNumFormalArgs()== 0) cascade(val); + f.call(new TrapArgs(this, val)); + } + + public Object cascade() { + if (next != null) return next.perform(); + return trapee.get(name, true); } - /** removes this trap */ - public void delete() { - for(Trap last = null, cur = (Trap)trapee.traps.get(name); cur != null; last = cur, cur = cur.next) { - if (cur != this) continue; - else if (last != null) last.next = cur.next; - else if (cur.next == null) trapee.traps.remove(name); - else trapee.traps.put(name, cur.next); - } - /* FIXME - if (trapee.surface != null && !trapee.is_trapped("KeyPressed") && !trapee.is_trapped("KeyReleased")) - trapee.surface.keywatchers.removeElement(trapee); - */ - allTraps.remove(myWeak); + public void cascade(Object val) { + if (next != null) next.perform(val); + trapee.put(name, val, true); } - + private static class TrapArgs extends JS.Array { - public boolean cascadeHappened; private Trap t; - public TrapArgs(Trap t,JS.Array args) { - int size = args.length(); - setSize(size); - for(int i=0;i= 0) args[count] = t.pop(); - if(args[0] instanceof String) { - StringBuffer sb = new StringBuffer(64); - for(int i=0;i= 0) args[count] = t.pop(); + if(args[0] instanceof String) { StringBuffer sb = new StringBuffer(64); - sb.append(JS.toString(new Double(d))); - while(i < args.length) sb.append(JS.toString(args[i++])); + for(int i=0;i