Beispiel #1
0
 private void savewndstate() {
   if (prefs == null) {
     if (getExtendedState() == NORMAL)
     /* Apparent, getSize attempts to return the "outer
      * size" of the window, including WM decorations, even
      * though setSize sets the "inner size" of the
      * window. Therefore, use the Panel's size instead; it
      * ought to correspond to the inner size at all
      * times. */ {
       Dimension dim = p.getSize();
       Utils.setprefc("wndsz", new Coord(dim.width, dim.height));
     }
     Utils.setprefb("wndmax", (getExtendedState() & MAXIMIZED_BOTH) != 0);
   }
 }
Beispiel #2
0
 private static void main2(String[] args) {
   Config.cmdline(args);
   try {
     javabughack();
   } catch (InterruptedException e) {
     return;
   }
   setupres();
   MainFrame f = new MainFrame(null);
   if (Utils.getprefb("fullscreen", false)) f.setfs();
   f.mt.start();
   try {
     f.mt.join();
   } catch (InterruptedException e) {
     f.g.interrupt();
     return;
   }
   dumplist(Resource.remote().loadwaited(), Config.loadwaited);
   dumplist(Resource.remote().cached(), Config.allused);
   if (ResCache.global != null) {
     try {
       Writer w = new OutputStreamWriter(ResCache.global.store("tmp/allused"), "UTF-8");
       try {
         Resource.dumplist(Resource.remote().used(), w);
       } finally {
         w.close();
       }
     } catch (IOException e) {
     }
   }
   System.exit(0);
 }
 public static <T extends RArray> T trace(RArray orig) {
   if (VIEW_TRACING) {
     RArray res;
     if (orig instanceof RList) {
       res = new RListTracingView((RList) orig);
     } else if (orig instanceof RString) {
       res = new RStringTracingView((RString) orig);
     } else if (orig instanceof RComplex) {
       res = new RComplexTracingView((RComplex) orig);
     } else if (orig instanceof RDouble) {
       res = new RDoubleTracingView((RDouble) orig);
     } else if (orig instanceof RInt) {
       res = new RIntTracingView((RInt) orig);
     } else if (orig instanceof RLogical) {
       res = new RLogicalTracingView((RLogical) orig);
     } else if (orig instanceof RRaw) {
       res = new RRawTracingView((RRaw) orig);
     } else {
       assert Utils.check(false, "missed view type");
       res = orig;
     }
     return (T) res;
   } else {
     return (T) orig;
   }
 }
Beispiel #4
0
  public MainFrame(Coord isz) {
    super("Haven and Hearth");
    version = "1.6 (12.22.2015)";
    Coord sz;
    if (isz == null) {
      sz = Utils.getprefc("wndsz", new Coord(800, 600));
      if (sz.x < 640) sz.x = 640;
      if (sz.y < 480) sz.y = 480;
    } else {
      sz = isz;
    }
    this.g = new ThreadGroup(HackThread.tg(), "Haven client");
    this.mt = new HackThread(this.g, this, "Haven main thread");
    p = new HavenPanel(sz.x, sz.y);
    if (fsmode == null) {
      Coord pfm = Utils.getprefc("fsmode", null);
      if (pfm != null) fsmode = findmode(pfm.x, pfm.y);
    }
    if (fsmode == null) {
      DisplayMode cm = getGraphicsConfiguration().getDevice().getDisplayMode();
      fsmode = findmode(cm.getWidth(), cm.getHeight());
    }
    if (fsmode == null) fsmode = findmode(800, 600);
    add(p);
    pack();
    setResizable(!Utils.getprefb("wndlock", false));
    p.requestFocusInWindow();
    seticon();
    setVisible(true);
    p.init();
    addWindowListener(
        new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            g.interrupt();
          }

          public void windowActivated(WindowEvent e) {
            p.bgmode = false;
          }

          public void windowDeactivated(WindowEvent e) {
            p.bgmode = true;
          }
        });
    if ((isz == null) && Utils.getprefb("wndmax", false))
      setExtendedState(getExtendedState() | MAXIMIZED_BOTH);
  }
 private static Field[] getAllFields(Class cls) {
   ArrayList<Field> res = new ArrayList<>();
   Class c = cls;
   while (c != View.class) {
     assert Utils.check(c != null);
     res.addAll(Arrays.asList(c.getDeclaredFields()));
     c = c.getSuperclass();
   }
   return res.toArray(new Field[res.size()]);
 }
Beispiel #6
0
 public <T extends Widget> T add(T child, Coord c) {
   child.c = c;
   if (child instanceof Window) {
     try {
       Window wnd = (Window) child;
       if (Window.persistentwnds.contains(wnd.cap.text))
         child.c = Utils.getprefc(wnd.cap.text + "_c", c);
     } catch (Exception e) {
     }
   }
   return (add(child));
 }
Beispiel #7
0
 /** Try to create a Java object using a one-string-param constructor. */
 public static Object newStringConstructor(String type, String param) throws Exception {
   Constructor c = Utils.getClass(type).getConstructor(String.class);
   try {
     return c.newInstance(param);
   } catch (InvocationTargetException e) {
     Throwable t = e.getTargetException();
     if (t instanceof Exception) {
       throw (Exception) t;
     } else {
       throw e;
     }
   }
 }
    public Site(StackTraceElement[] site) {
      this.site = site;

      int i = 0;
      while (!THIS_FILE_NAME.equals(site[i].getFileName())) {
        i++;
      }
      while (THIS_FILE_NAME.equals(site[i].getFileName())) {
        i++;
      }
      assert Utils.check(i < site.length - 1);
      this.offset = i;
    }
Beispiel #9
0
  public static void main(final String[] args) {

    File logdir = new File(LOG_DIR);
    if (!logdir.exists()) logdir.mkdirs();
    File log = new File(logdir, "client.log");
    // redirect all console output to the file
    try {
      PrintStream out = new PrintStream(new FileOutputStream(log, true), true);
      out.format("[%s] ===== Client started =====%n", timestampf.format(new Date()));
      System.setOut(out);
      System.setErr(out);
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }

    /* Set up the error handler as early as humanly possible. */
    ThreadGroup g = new ThreadGroup("Haven main group");
    String ed;
    if (!(ed = Utils.getprop("haven.errorurl", "")).equals("")) {
      try {
        final haven.error.ErrorHandler hg = new haven.error.ErrorHandler(new java.net.URL(ed));
        hg.sethandler(
            new haven.error.ErrorGui(null) {
              public void errorsent() {
                hg.interrupt();
              }
            });
        g = hg;
      } catch (java.net.MalformedURLException e) {
      }
    }
    Thread main =
        new HackThread(
            g,
            new Runnable() {
              public void run() {
                main2(args);
              }
            },
            "Haven main thread");
    main.start();
  }
 private static void linkChildren(RArray parentRealView, ViewTrace parentTrace) {
   Class viewClass = parentRealView.getClass();
   Field[] fields = getAllFields(viewClass);
   for (Field f : fields) {
     if (f.isSynthetic()) {
       continue;
     }
     Class fieldClass = f.getType();
     if (RArray.class.isAssignableFrom(fieldClass)) {
       try {
         f.setAccessible(true);
         Object o = f.get(parentRealView);
         if (o instanceof TracingView) {
           ((TracingView) o).getTrace().parentView = parentTrace;
         }
       } catch (IllegalAccessException e) {
         assert Utils.check(false, "can't read a view field " + e);
       }
     }
   }
 }
Beispiel #11
0
 /**
  * This method attempts to create an object of the given "type" using the "value" parameter. e.g.
  * calling createObjectFromString("java.lang.Integer", "10") will return an Integer object
  * initialized to 10.
  */
 public static Object createObjectFromString(String type, String value) throws Exception {
   Object result;
   if (primitiveToWrapper.containsKey(type)) {
     if (type.equals(Character.TYPE.getName())) {
       result = new Character(value.charAt(0));
     } else {
       result = newStringConstructor(((Class<?>) primitiveToWrapper.get(type)).getName(), value);
     }
   } else if (type.equals(Character.class.getName())) {
     result = new Character(value.charAt(0));
   } else if (Number.class.isAssignableFrom(Utils.getClass(type))) {
     result = createNumberFromStringValue(value);
   } else if (value == null || value.toString().equals("null")) {
     // hack for null value
     result = null;
   } else {
     // try to create a Java object using
     // the one-string-param constructor
     result = newStringConstructor(type, value);
   }
   return result;
 }
Beispiel #12
0
  /**
   * Given class of the interface for a fluid engine, build a concrete instance of that interface
   * that calls back into a Basic Engine for all methods in the interface.
   */
  private static BasicEngine buildCallbackEngine(String engineInterface, Class cEngine) {
    // build up list of callbacks for <engineInterface>
    StringBuffer decls = new StringBuffer();
    Method[] engineMethods = cEngine.getMethods();
    for (int i = 0; i < engineMethods.length; i++) {
      Method meth = engineMethods[i];
      // skip methods that aren't declared in the interface
      if (meth.getDeclaringClass() != cEngine) {
        continue;
      }
      decls.append(
          "    public " + Utils.asTypeDecl(meth.getReturnType()) + " " + meth.getName() + "(");
      Class[] params = meth.getParameterTypes();
      for (int j = 0; j < params.length; j++) {
        decls.append(Utils.asTypeDecl(params[j]) + " p" + j);
        if (j != params.length - 1) {
          decls.append(", ");
        }
      }
      decls.append(") {\n");
      decls.append(
          "        return ("
              + Utils.asTypeDecl(meth.getReturnType())
              + ")super.callNative(\""
              + meth.getName()
              + "\", new Object[] {");
      for (int j = 0; j < params.length; j++) {
        decls.append("p" + j);
        if (j != params.length - 1) {
          decls.append(", ");
        }
      }
      decls.append("} );\n");
      decls.append("    }\n\n");
    }

    // write template file
    String engineName = "BasicEngine_" + engineInterface;
    String fileName = engineName + ".java";
    try {
      String contents = Utils.readFile("lava/engine/basic/BasicEngineTemplate.java").toString();
      // remove package name
      contents = Utils.replaceAll(contents, "package lava.engine.basic;", "");
      // for class decl
      contents =
          Utils.replaceAll(
              contents, "extends BasicEngine", "extends BasicEngine implements " + engineInterface);
      // for constructor
      contents = Utils.replaceAll(contents, "BasicEngineTemplate", engineName);
      // for methods
      contents = Utils.replaceAll(contents, "// INSERT METHODS HERE", decls.toString());
      Utils.writeFile(fileName, contents);
    } catch (IOException e) {
      e.printStackTrace();
      System.exit(1);
    }

    // compile the file
    try {
      Process jProcess = Runtime.getRuntime().exec("jikes " + fileName, null, null);
      jProcess.waitFor();
    } catch (Exception e) {
      System.err.println("Error compiling auto-generated file " + fileName);
      e.printStackTrace();
      System.exit(1);
    }

    // instantiate the class
    BasicEngine result = null;
    try {
      result = (BasicEngine) Class.forName(engineName).newInstance();
      // cleanup the files we made
      new File(engineName + ".java").delete();
      new File(engineName + ".class").delete();
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
    }

    return result;
  }
    private static void extractViewPattern(int depth, ViewTrace trace, StringBuilder p) {
      if (!processedViewsForPatterns.add(trace)) {
        p.append("(ALIASED) ");
      }

      p.append(trace.realView.getClass() + " size = " + trace.realView.size() + "\n");
      indent(depth, p);

      p.append("    use:");
      if (trace.materializeCount == 0 && trace.sumCount == 0 && trace.getCount == 0) {
        p.append(" UNUSED");
      } else {
        if (trace.getCount > 0) {
          p.append(" get");
        }
        if (trace.materializeCount > 0) {
          p.append(" materialize");
        }
        if (trace.sumCount > 0) {
          p.append(" sum");
        }
      }
      p.append("\n");

      if (false) {
        p.append("    allocationSite =");
        Site.printSite(trace.allocationSite, p);
        p.append("\n");
        indent(depth, p);
      }

      RArray view = trace.realView;
      Class viewClass = view.getClass();
      Field[] fields = getAllFields(viewClass);
      boolean printedField = false;

      for (Field f : fields) {
        if (f.isSynthetic()) {
          continue;
        }
        Class fieldClass = f.getType();
        if (RArray.class.isAssignableFrom(fieldClass)) {
          continue; // these later
        }
        indent(depth, p);
        p.append("    " + f.getName() + " ");
        try {
          f.setAccessible(true);
          Object o = f.get(view);
          p.append(o == null ? "null (" + fieldClass + ")" : o.getClass());
          p.append("\n");
          printedField = true;
        } catch (IllegalAccessException e) {
          assert Utils.check(false, "can't read a view field " + e);
        }
      }

      boolean printNewline = printedField;
      for (Field f : fields) {
        if (f.isSynthetic()) {
          continue;
        }
        Class fieldClass = f.getType();
        if (!RArray.class.isAssignableFrom(fieldClass)) {
          continue;
        }
        if (printNewline) {
          p.append("\n");
          printNewline = false;
        }
        indent(depth, p);
        p.append("    " + f.getName() + " ");
        try {
          f.setAccessible(true);
          Object o = f.get(view);
          if (o instanceof TracingView) {
            p.append("VIEW ");
            TracingView child = (TracingView) o;
            extractViewPattern(depth + 2, child.getTrace(), p);
          } else {
            p.append("ARRAY " + o.getClass() + " size = " + ((RArray) o).size());
            if (o instanceof View) {
              ps.println("MISSED VIEW " + o);
            }
          }
          p.append("\n");
        } catch (IllegalAccessException e) {
          assert Utils.check(false, "can't read a view field " + e);
        }
      }
    }
    private static void dumpView(int depth, ViewTrace trace) {
      printedIndividualViews.add(trace);

      ps.println(trace.realView + " size = " + trace.realView.size());
      if (TRACE_ALLOCATION_SITE) {
        indent(depth);
        ps.print("    allocationSite =");
        Site.printSite(trace.allocationSite);
        ps.println();
      }

      int unused = trace.unusedElements();
      int redundant = trace.redundantGets();

      boolean singleUse;
      Site[] useSites;

      if (TRACE_USE_SITES) {
        useSites = trace.useSites.toArray(new Site[trace.useSites.size()]);
        singleUse = (useSites.length == 1);
      } else if (TRACE_SINGLE_USE_SITE) {
        useSites = null;
        singleUse = !trace.multipleUseSites;
      } else {
        useSites = null;
        singleUse = false;
      }
      if (singleUse) {
        indent(depth);
        ps.print("    singleUseSite = US");
        Site.printSite(useSites != null ? useSites[0] : trace.singleUseSite);

        if (trace.getCount > 0) {
          ps.println(" (get)");
        } else if (trace.sumCount > 0) {
          ps.println(" (sum)");
        } else {
          ps.println(" (materialize)");
        }

      } else if (trace.getCount > 0) {
        if (TRACE_FIRST_GET_SITE) {
          indent(depth);
          ps.print("    firstGetSite =");
          Site.printSite(trace.firstGetSite);
          ps.println();
        }
        if (trace.materializeCount == 0 && trace.sumCount == 0) {
          if (unused > 0) {
            indent(depth);
            ps.println("    unusedElements = " + unused);
          }
          if (redundant > 0) {
            indent(depth);
            ps.println("    redundantGets = " + redundant + " (no materialize, sum)");
          }
        }
      } else {
        if (trace.materializeCount == 0 && trace.sumCount == 0) {
          indent(depth);
          ps.println("    UNUSED");
        } else {
          indent(depth);
          ps.println(
              "    materializeCount = "
                  + trace.materializeCount
                  + " sumCount = "
                  + trace.sumCount
                  + " getCount = "
                  + trace.getCount);
        }
      }
      if (TRACE_FIRST_MATERIALIZE_SITE && trace.materializeCount > 0 && !singleUse) {
        indent(depth);
        ps.print("    firstMaterializeSite =");
        Site.printSite(trace.firstMaterializeSite);
        ps.println();
      }
      if (TRACE_FIRST_SUM_SITE && trace.sumCount > 0 && !singleUse) {
        indent(depth);
        ps.print("    firstSumSite =");
        Site.printSite(trace.firstSumSite);
        ps.println();
      }
      if (TRACE_USE_SITES) {
        if (useSites.length != 1) {
          indent(depth);
          ps.println("    useSites (" + useSites.length + "):");
          for (Site s : useSites) {
            indent(depth);
            ps.print("        US");
            Site.printSite(s);
            ps.println();
          }
        }
      }

      ps.println();
      RArray view = trace.realView;
      Class viewClass = view.getClass();
      Field[] fields = getAllFields(viewClass);
      boolean printedField = false;

      for (Field f : fields) {
        if (f.isSynthetic()) {
          continue;
        }
        Class fieldClass = f.getType();
        if (RArray.class.isAssignableFrom(fieldClass)) {
          continue; // these later
        }
        indent(depth);
        ps.print("    " + f.getName() + " ");
        try {
          f.setAccessible(true);
          ps.println(f.get(view));
          printedField = true;
        } catch (IllegalAccessException e) {
          assert Utils.check(false, "can't read a view field " + e);
        }
      }

      boolean printNewline = printedField;
      for (Field f : fields) {
        if (f.isSynthetic()) {
          continue;
        }
        Class fieldClass = f.getType();
        if (!RArray.class.isAssignableFrom(fieldClass)) {
          continue;
        }
        if (printNewline) {
          ps.println();
          printNewline = false;
        }
        indent(depth);
        ps.print("    " + f.getName() + " ");
        try {
          f.setAccessible(true);
          Object o = f.get(view);
          if (o instanceof TracingView) {
            ps.print("VIEW ");
            TracingView child = (TracingView) o;
            dumpView(depth + 2, child.getTrace());
          } else {
            ps.print("ARRAY " + o + " size = " + ((RArray) o).size());
            if (o instanceof View) {
              ps.println("MISSED VIEW " + o);
            }
          }
          ps.println();
        } catch (IllegalAccessException e) {
          assert Utils.check(false, "can't read a view field " + e);
        }
      }
    }