private Object readResolve() throws ObjectStreamException { // ensures that serialized namespaces are "deserialized" to the // namespace in the present runtime return findOrCreate(name); } }
for (Object item : ns.getMappings()) { MapEntry entry = (MapEntry) item; if (entry.getValue() instanceof Var) { Var var = (Var) entry.getValue(); if (var.ns.toString().equals(namespace)) { String name = entry.getKey().toString(); if (var.deref() instanceof IFn) {
@Override public Object eval(Reader reader, ScriptContext context) throws ScriptException { try { // Get engine bindings and send them to Clojure Bindings engineBindings = context.getBindings(ScriptContext.ENGINE_SCOPE); engineBindings.entrySet().forEach((entry) -> Var.intern(Namespace.findOrCreate(NAMESPACE_SYMBOL), Symbol.create(entry.getKey().intern()), entry.getValue(), true)); Var.pushThreadBindings( RT.map(RT.CURRENT_NS, RT.CURRENT_NS.deref(), RT.IN, new LineNumberingPushbackReader(context.getReader()), RT.OUT, context.getWriter(), RT.ERR, context.getErrorWriter())); Object result = Compiler.load(reader); return result; } catch (Exception e) { throw new ScriptException(e); } finally { Namespace.remove(NAMESPACE_SYMBOL); } }
public static Var internPrivate(String nsName, String sym){ Namespace ns = Namespace.findOrCreate(Symbol.intern(nsName)); Var ret = intern(ns, Symbol.intern(sym)); ret.setMeta(privateMeta); return ret; }
Object reference(Symbol sym, Object val){ if(sym.ns != null) { throw new IllegalArgumentException("Can't intern namespace-qualified symbol"); } IPersistentMap map = getMappings(); Object o; while((o = map.valAt(sym)) == null) { IPersistentMap newMap = map.assoc(sym, val); mappings.compareAndSet(map, newMap); map = getMappings(); } if(o == val) return o; warnOrFailOnReplace(sym, o, val); while(!mappings.compareAndSet(map, map.assoc(sym, val))) map = getMappings(); return val; }
public Object invoke(Object arg1) { Symbol nsname = (Symbol) arg1; Namespace ns = Namespace.findOrCreate(nsname); ns.addDefaultImports(); CURRENT_NS.set(ns); return ns; } };
private void createUserNamespace() throws ClassNotFoundException, IOException { Namespace userNs = Namespace.findOrCreate(Symbol.create(null, "user")); RT.load("clojure/repl"); List<String> replFns = new ArrayList<String>(Arrays.asList("source", "apropos", "dir")); if (!clojure1_2) { replFns.add("doc"); replFns.add("find-doc"); } for (String name : replFns) { userNs.refer(Symbol.create(null, name), var("clojure.repl", name)); } if (!clojure1_2) { RT.load("clojure/java/javadoc"); userNs.refer(Symbol.create(null, "javadoc"), var("clojure.java.javadoc", "javadoc")); } RT.load("clojure/pprint"); userNs.refer(Symbol.create(null, "pprint"), var("clojure.pprint", "pprint")); RT.load("clj_stacktrace/repl"); userNs.refer(Symbol.create(null, "pst"), var("clj-stacktrace.repl", "pst")); RT.load("cd_client/core"); userNs.refer(Symbol.create(null, "cdoc"), var("cd-client.core", "cdoc")); this.ns.set(userNs); }
Class referenceClass(Symbol sym, Class val){ if(sym.ns != null) { throw new IllegalArgumentException("Can't intern namespace-qualified symbol"); } IPersistentMap map = getMappings(); Class c = (Class) map.valAt(sym); while((c == null) || (areDifferentInstancesOfSameClassName(c, val))) { IPersistentMap newMap = map.assoc(sym, val); mappings.compareAndSet(map, newMap); map = getMappings(); c = (Class) map.valAt(sym); } if(c == val) return c; throw new IllegalStateException(sym + " already refers to: " + c + " in namespace: " + name); }
var = currentNS().intern(name); else var = ns.findInternedVar(name); Object o = currentNS().getMapping(sym); if(o == null) var = currentNS().intern(Symbol.intern(sym.name));
public static Var find(Symbol nsQualifiedSym){ if(nsQualifiedSym.ns == null) throw new IllegalArgumentException("Symbol must be namespace-qualified"); Namespace ns = Namespace.find(Symbol.intern(nsQualifiedSym.ns)); if(ns == null) throw new IllegalArgumentException("No such namespace: " + nsQualifiedSym.ns); return ns.findInternedVar(Symbol.intern(nsQualifiedSym.name)); }
static public Object maybeResolveIn(Namespace n, Symbol sym) { //note - ns-qualified vars must already exist if(sym.ns != null) { Namespace ns = namespaceFor(n, sym); if(ns == null) return null; Var v = ns.findInternedVar(Symbol.intern(sym.name)); if(v == null) return null; return v; } else if(sym.name.indexOf('.') > 0 && !sym.name.endsWith(".") || sym.name.charAt(0) == '[') { return RT.classForName(sym.name); } else if(sym.equals(NS)) return RT.NS_VAR; else if(sym.equals(IN_NS)) return RT.IN_NS_VAR; else { Object o = n.getMapping(sym); return o; } }
public void removeAlias(Symbol alias) { IPersistentMap map = getAliases(); while(map.containsKey(alias)) { IPersistentMap newMap = map.without(alias); aliases.compareAndSet(map, newMap); map = getAliases(); } }
static Namespace namespaceFor(Namespace inns, Symbol sym){ //note, presumes non-nil sym.ns // first check against currentNS' aliases... Symbol nsSym = Symbol.intern(sym.ns); Namespace ns = inns.lookupAlias(nsSym); if(ns == null) { // ...otherwise check the Namespaces map. ns = Namespace.find(nsSym); } return ns; }
else Object o = currentNS().getMapping(sym); if(o instanceof Class) c = (Class) o;
public void unmap(Symbol sym) { if(sym.ns != null) { throw new IllegalArgumentException("Can't unintern namespace-qualified symbol"); } IPersistentMap map = getMappings(); while(map.containsKey(sym)) { IPersistentMap newMap = map.without(sym); mappings.compareAndSet(map, newMap); map = getMappings(); } }
public static Namespace findOrCreate(Symbol name){ Namespace ns = namespaces.get(name); if(ns != null) return ns; Namespace newns = new Namespace(name); ns = namespaces.putIfAbsent(name, newns); return ns == null ? newns : ns; }
@Override public Object eval(Reader reader, ScriptContext context) throws ScriptException { try { // Get engine bindings and send them to Clojure Bindings engineBindings = context.getBindings(ScriptContext.ENGINE_SCOPE); engineBindings.entrySet().forEach((entry) -> Var.intern(Namespace.findOrCreate(NAMESPACE_SYMBOL), Symbol.create(entry.getKey().intern()), entry.getValue(), true)); Var.pushThreadBindings( RT.map(RT.CURRENT_NS, RT.CURRENT_NS.deref(), RT.IN, new LineNumberingPushbackReader(context.getReader()), RT.OUT, context.getWriter(), RT.ERR, context.getErrorWriter())); Object result = Compiler.load(reader); return result; } catch (Exception e) { throw new ScriptException(e); } finally { Namespace.remove(NAMESPACE_SYMBOL); } }
public static Var intern(Namespace ns, Symbol sym){ return ns.intern(sym); }
Object reference(Symbol sym, Object val){ if(sym.ns != null) { throw new IllegalArgumentException("Can't intern namespace-qualified symbol"); } IPersistentMap map = getMappings(); Object o; while((o = map.valAt(sym)) == null) { IPersistentMap newMap = map.assoc(sym, val); mappings.compareAndSet(map, newMap); map = getMappings(); } if(o == val) return o; warnOrFailOnReplace(sym, o, val); while(!mappings.compareAndSet(map, map.assoc(sym, val))) map = getMappings(); return val; }
Class referenceClass(Symbol sym, Class val){ if(sym.ns != null) { throw new IllegalArgumentException("Can't intern namespace-qualified symbol"); } IPersistentMap map = getMappings(); Class c = (Class) map.valAt(sym); while((c == null) || (areDifferentInstancesOfSameClassName(c, val))) { IPersistentMap newMap = map.assoc(sym, val); mappings.compareAndSet(map, newMap); map = getMappings(); c = (Class) map.valAt(sym); } if(c == val) return c; throw new IllegalStateException(sym + " already refers to: " + c + " in namespace: " + name); }