private boolean step() { if (next != NONE) return true; while (next == NONE) { if (buffer.isEmpty()) { if (completed) { return false; } else if (sourceIter.hasNext()) { Object iter = null; if (multi) iter = xf.applyTo(RT.cons(null, sourceIter.next())); else iter = xf.invoke(null, sourceIter.next()); if (RT.isReduced(iter)) { xf.invoke(null); completed = true; } } else { xf.invoke(null); completed = true; } } else { next = buffer.remove(); } } return true; }
public class Compile { private static final String PATH_PROP = "clojure.compile.path"; private static final String REFLECTION_WARNING_PROP = "clojure.compile.warn-on-reflection"; private static final String UNCHECKED_MATH_PROP = "clojure.compile.unchecked-math"; private static final Var compile_path = RT.var("clojure.core", "*compile-path*"); private static final Var compile = RT.var("clojure.core", "compile"); private static final Var warn_on_reflection = RT.var("clojure.core", "*warn-on-reflection*"); private static final Var unchecked_math = RT.var("clojure.core", "*unchecked-math*"); public static void main(String... args) throws IOException { OutputStreamWriter out = (OutputStreamWriter) RT.OUT.deref(); PrintWriter err = RT.errPrintWriter(); String path = System.getProperty(PATH_PROP); int count = args.length; if (path == null) { err.println( "ERROR: Must set system property " + PATH_PROP + "\nto the location for compiled .class files." + "\nThis directory must also be on your CLASSPATH."); System.exit(1); } boolean warnOnReflection = System.getProperty(REFLECTION_WARNING_PROP, "false").equals("true"); String uncheckedMathProp = System.getProperty(UNCHECKED_MATH_PROP); Object uncheckedMath = Boolean.FALSE; if ("true".equals(uncheckedMathProp)) uncheckedMath = Boolean.TRUE; else if ("warn-on-boxed".equals(uncheckedMathProp)) uncheckedMath = Keyword.intern("warn-on-boxed"); try { Var.pushThreadBindings( RT.map( compile_path, path, warn_on_reflection, warnOnReflection, unchecked_math, uncheckedMath)); for (String lib : args) { out.write("Compiling " + lib + " to " + path + '\n'); out.flush(); compile.invoke(Symbol.intern(lib)); } } finally { Var.popThreadBindings(); try { out.flush(); } catch (IOException e) { e.printStackTrace(err); } } } }
public PersistentQueue cons(Object o) { if (f == null) // empty return new PersistentQueue(meta(), cnt + 1, RT.list(o), null); else return new PersistentQueue( meta(), cnt + 1, f, (r != null ? r : PersistentVector.EMPTY).cons(o)); }
public boolean equals(Object obj) { if (!(obj instanceof Sequential)) return false; ISeq ms = RT.seq(obj); for (ISeq s = seq(); s != null; s = s.next(), ms = ms.next()) { if (ms == null || !Util.equals(s.first(), ms.first())) return false; } return ms == null; }
protected void validate(IFn vf, Object val) { try { if (vf != null && !RT.booleanCast(vf.invoke(val))) throw new IllegalStateException("Invalid reference state"); } catch (RuntimeException re) { throw re; } catch (Exception e) { throw new IllegalStateException("Invalid reference state", e); } }
public final synchronized ISeq seq() { sval(); if (sv != null) { Object ls = sv; sv = null; while (ls instanceof LazySeq) { ls = ((LazySeq) ls).sval(); } s = RT.seq(ls); } return s; }
public PersistentQueue pop() { if (f == null) // hmmm... pop of empty queue -> empty queue? return this; // throw new IllegalStateException("popping empty queue"); ISeq f1 = f.next(); PersistentVector r1 = r; if (f1 == null) { f1 = RT.seq(r); r1 = null; } return new PersistentQueue(meta(), cnt - 1, f1, r1); }
protected static Var loadVar(String namespace, String varName) { try { Symbol namespaceSymbol = Symbol.intern(namespace); Namespace ns = Namespace.find(namespaceSymbol); if (ns != null) return (Var) ns.getMapping(Symbol.create(varName)); RT.load(namespace, false); ns = Namespace.find(namespaceSymbol); if (ns != null) return (Var) ns.getMapping(Symbol.create(varName)); final String coreFilename = Clj.nsToFilename(namespace); RT.loadResourceScript(coreFilename); ns = Namespace.find(namespaceSymbol); if (ns != null) return (Var) ns.getMapping(Symbol.create(varName)); throw new RuntimeException( "var still not found after load attempts: " + namespace + "/" + varName); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("Failed to load var:" + namespace + "/" + varName, e); } }
Object doCommute(Ref ref, IFn fn, ISeq args) { if (!info.running()) throw retryex; if (!vals.containsKey(ref)) { Object val = null; try { ref.lock.readLock().lock(); val = ref.tvals == null ? null : ref.tvals.val; } finally { ref.lock.readLock().unlock(); } vals.put(ref, val); } ArrayList<CFn> fns = commutes.get(ref); if (fns == null) commutes.put(ref, fns = new ArrayList<CFn>()); fns.add(new CFn(fn, args)); Object ret = fn.applyTo(RT.cons(vals.get(ref), args)); vals.put(ref, ret); return ret; }
public IPersistentCollection cons(Object o) { if (o instanceof Map.Entry) { Map.Entry e = (Map.Entry) o; return assoc(e.getKey(), e.getValue()); } else if (o instanceof IPersistentVector) { IPersistentVector v = (IPersistentVector) o; if (v.count() != 2) throw new IllegalArgumentException("Vector arg to map conj must be a pair"); return assoc(v.nth(0), v.nth(1)); } IPersistentMap ret = this; for (ISeq es = RT.seq(o); es != null; es = es.next()) { Map.Entry e = (Map.Entry) es.first(); ret = ret.assoc(e.getKey(), e.getValue()); } return ret; }
static void doRun(Action action) { try { nested.set(PersistentVector.EMPTY); Throwable error = null; try { Object oldval = action.agent.state; Object newval = action.fn.applyTo(RT.cons(action.agent.state, action.args)); action.agent.setState(newval); action.agent.notifyWatches(oldval, newval); } catch (Throwable e) { error = e; } if (error == null) { releasePendingSends(); } else { nested.set(null); // allow errorHandler to send if (action.agent.errorHandler != null) { try { action.agent.errorHandler.invoke(action.agent, error); } catch (Throwable e) { } // ignore errorHandler errors } if (action.agent.errorMode == CONTINUE) { error = null; } } boolean popped = false; ActionQueue next = null; while (!popped) { ActionQueue prior = action.agent.aq.get(); next = new ActionQueue(prior.q.pop(), error); popped = action.agent.aq.compareAndSet(prior, next); } if (error == null && next.q.count() > 0) ((Action) next.q.peek()).execute(); } finally { nested.set(null); } }
public ISeq seq() { if (f == null) return null; return new Seq(f, RT.seq(r)); }
public Object peek() { return RT.first(f); }
public Object[] toArray() { return RT.seqToArray(seq()); }
public int count() { return RT.count(f) + RT.count(rseq); }
public static Object applyToHelper(IFn ifn, ISeq arglist) throws Exception { switch (RT.boundedLength(arglist, 20)) { case 0: arglist = null; return ifn.invoke(); case 1: Object a1 = arglist.first(); arglist = null; return ifn.invoke(a1); case 2: return ifn.invoke( arglist.first(), Util.ret1((arglist = arglist.next()).first(), arglist = null)); case 3: return ifn.invoke( arglist.first(), (arglist = arglist.next()).first(), Util.ret1((arglist = arglist.next()).first(), arglist = null)); case 4: return ifn.invoke( arglist.first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), Util.ret1((arglist = arglist.next()).first(), arglist = null)); case 5: return ifn.invoke( arglist.first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), Util.ret1((arglist = arglist.next()).first(), arglist = null)); case 6: return ifn.invoke( arglist.first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), Util.ret1((arglist = arglist.next()).first(), arglist = null)); case 7: return ifn.invoke( arglist.first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), Util.ret1((arglist = arglist.next()).first(), arglist = null)); case 8: return ifn.invoke( arglist.first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), Util.ret1((arglist = arglist.next()).first(), arglist = null)); case 9: return ifn.invoke( arglist.first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), Util.ret1((arglist = arglist.next()).first(), arglist = null)); case 10: return ifn.invoke( arglist.first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), Util.ret1((arglist = arglist.next()).first(), arglist = null)); case 11: return ifn.invoke( arglist.first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), Util.ret1((arglist = arglist.next()).first(), arglist = null)); case 12: return ifn.invoke( arglist.first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), Util.ret1((arglist = arglist.next()).first(), arglist = null)); case 13: return ifn.invoke( arglist.first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), Util.ret1((arglist = arglist.next()).first(), arglist = null)); case 14: return ifn.invoke( arglist.first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), Util.ret1((arglist = arglist.next()).first(), arglist = null)); case 15: return ifn.invoke( arglist.first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), Util.ret1((arglist = arglist.next()).first(), arglist = null)); case 16: return ifn.invoke( arglist.first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), Util.ret1((arglist = arglist.next()).first(), arglist = null)); case 17: return ifn.invoke( arglist.first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), Util.ret1((arglist = arglist.next()).first(), arglist = null)); case 18: return ifn.invoke( arglist.first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), Util.ret1((arglist = arglist.next()).first(), arglist = null)); case 19: return ifn.invoke( arglist.first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), Util.ret1((arglist = arglist.next()).first(), arglist = null)); case 20: return ifn.invoke( arglist.first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), Util.ret1((arglist = arglist.next()).first(), arglist = null)); default: return ifn.invoke( arglist.first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), (arglist = arglist.next()).first(), RT.seqToArray(Util.ret1(arglist.next(), arglist = null))); } }
public Object nextElement() { Object ret = RT.first(seq); seq = RT.next(seq); return ret; }
public Object[] toArray(Object[] a) { return RT.seqToPassedArray(seq(), a); }
public String toString() { return RT.printString(this); }
public Object get(int index) { return RT.nth(this, index); }
public boolean equals(Object o) { ISeq s = seq(); if (s != null) return s.equiv(o); else return (o instanceof Sequential || o instanceof List) && RT.seq(o) == null; }
Object run(Callable fn) throws Exception { boolean done = false; Object ret = null; ArrayList<Ref> locked = new ArrayList<Ref>(); ArrayList<Notify> notify = new ArrayList<Notify>(); for (int i = 0; !done && i < RETRY_LIMIT; i++) { try { getReadPoint(); if (i == 0) { startPoint = readPoint; startTime = System.nanoTime(); } info = new Info(RUNNING, startPoint); ret = fn.call(); // make sure no one has killed us before this point, and can't from now on if (info.status.compareAndSet(RUNNING, COMMITTING)) { for (Map.Entry<Ref, ArrayList<CFn>> e : commutes.entrySet()) { Ref ref = e.getKey(); if (sets.contains(ref)) continue; boolean wasEnsured = ensures.contains(ref); // can't upgrade readLock, so release it releaseIfEnsured(ref); tryWriteLock(ref); locked.add(ref); if (wasEnsured && ref.tvals != null && ref.tvals.point > readPoint) throw retryex; Info refinfo = ref.tinfo; if (refinfo != null && refinfo != info && refinfo.running()) { if (!barge(refinfo)) throw retryex; } Object val = ref.tvals == null ? null : ref.tvals.val; vals.put(ref, val); for (CFn f : e.getValue()) { vals.put(ref, f.fn.applyTo(RT.cons(vals.get(ref), f.args))); } } for (Ref ref : sets) { tryWriteLock(ref); locked.add(ref); } // validate and enqueue notifications for (Map.Entry<Ref, Object> e : vals.entrySet()) { Ref ref = e.getKey(); ref.validate(ref.getValidator(), e.getValue()); } // at this point, all values calced, all refs to be written locked // no more client code to be called long commitPoint = getCommitPoint(); for (Map.Entry<Ref, Object> e : vals.entrySet()) { Ref ref = e.getKey(); Object oldval = ref.tvals == null ? null : ref.tvals.val; Object newval = e.getValue(); int hcount = ref.histCount(); if (ref.tvals == null) { ref.tvals = new Ref.TVal(newval, commitPoint); } else if ((ref.faults.get() > 0 && hcount < ref.maxHistory) || hcount < ref.minHistory) { ref.tvals = new Ref.TVal(newval, commitPoint, ref.tvals); ref.faults.set(0); } else { ref.tvals = ref.tvals.next; ref.tvals.val = newval; ref.tvals.point = commitPoint; } if (ref.getWatches().count() > 0) notify.add(new Notify(ref, oldval, newval)); } done = true; info.status.set(COMMITTED); } } catch (RetryEx retry) { // eat this so we retry rather than fall out } finally { for (int k = locked.size() - 1; k >= 0; --k) { locked.get(k).lock.writeLock().unlock(); } locked.clear(); for (Ref r : ensures) { r.lock.readLock().unlock(); } ensures.clear(); stop(done ? COMMITTED : RETRY); try { if (done) // re-dispatch out of transaction { for (Notify n : notify) { n.ref.notifyWatches(n.oldval, n.newval); } for (Agent.Action action : actions) { Agent.dispatchAction(action); } } } finally { notify.clear(); actions.clear(); } } } if (!done) throw Util.runtimeException("Transaction failed after reaching retry limit"); return ret; }
public Object loadPT(IConstructor[] fileRef, String sourcePath, String sourceName) { IConstructor file = fileRef[0]; ISourceLocation loc = TreeAdapter.getLocation(file); int lineNumber = loc.getBeginLine(); Object ret = null; Var.pushThreadBindings( RT.map( LOADER, RT.makeClassLoader(), SOURCE_PATH, sourcePath, SOURCE, sourceName, METHOD, null, LOCAL_ENV, null, LOOP_LOCALS, null, NEXT_LOCAL_NUM, 0, RT.CURRENT_NS, RT.CURRENT_NS.deref(), LINE_BEFORE, lineNumber, LINE_AFTER, lineNumber, RT.UNCHECKED_MATH, RT.UNCHECKED_MATH.deref(), RT.WARN_ON_REFLECTION, RT.WARN_ON_REFLECTION.deref(), RT.DATA_READERS, RT.DATA_READERS.deref())); UPTRLispReader reader = new UPTRLispReader(vf, errors); try { if (TreeAdapter.isAmb(file)) { System.err.println("Amb"); } // File is start[File], so IConstructor file2 = (IConstructor) TreeAdapter.getArgs(file).get(1); IList args = TreeAdapter.getArgs(file2); // Probably only this is the list of forms; don't forget to fix // below. IList forms = TreeAdapter.getArgs((IConstructor) args.get(0)); IListWriter newArgs = vf.listWriter(); for (int i = 0; i < forms.length(); i++) { IConstructor form = (IConstructor) forms.get(i); // only forms, no literals at this level. UPTRLispReader.Pair p = reader.read(form); newArgs.append(p.tree); LINE_AFTER.set(TreeAdapter.getLocation(form).getEndLine()); ret = eval(p.obj, false); LINE_BEFORE.set(TreeAdapter.getLocation(form).getBeginLine()); if (i < forms.length() - 2) { i++; newArgs.append(forms.get(i)); // layout } } // Fix tree file2 = file2.set("args", newArgs.done()); file = file.set( "args", vf.list(TreeAdapter.getArgs(file).get(0), file2, TreeAdapter.getArgs(file).get(2))); } catch (UPTRLispReader.ReaderException e) { throw new CompilerException(sourcePath, e.line, e.getCause()); } finally { Var.popThreadBindings(); } fileRef[0] = file; return ret; }
public ISeq cons(Object o) { return RT.cons(o, seq()); }