Skip to content

Commit

Permalink
made loadResourceScript throw if resource not found, added maybeLoadL…
Browse files Browse the repository at this point in the history
…oadResourceScript, failIfNotFound flag, based on patch from cemerick.

Added overload of RT.var that takes an initial value.
  • Loading branch information
richhickey committed Aug 6, 2008
1 parent fca8d50 commit 5c97b61
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/clj/clojure/genclass.clj
Expand Up @@ -282,7 +282,8 @@

(. gen push ctype)
(. gen push (str (. name replace \. \/) ".clj"))
(. gen (invokeStatic rt-type (. Method (getMethod "void loadResourceScript(Class,String)"))))
(. gen push 0)
(. gen (invokeStatic rt-type (. Method (getMethod "void loadResourceScript(Class,String,boolean)"))))

(. gen (returnValue))
(. gen (endMethod)))
Expand Down
34 changes: 27 additions & 7 deletions src/jvm/clojure/lang/RT.java
Expand Up @@ -324,11 +324,27 @@ static public Var var(String ns, String name){
return Var.intern(Namespace.findOrCreate(Symbol.intern(null, ns)), Symbol.intern(null, name));
}

static public Var var(String ns, String name, Object init){
return Var.intern(Namespace.findOrCreate(Symbol.intern(null, ns)), Symbol.intern(null, name), init);
}

public static void loadResourceScript(String name) throws Exception{
loadResourceScript(RT.class, name);
loadResourceScript(name, true);
}

public static void maybeLoadResourceScript(String name) throws Exception{
loadResourceScript(name, false);
}

public static void loadResourceScript(String name, boolean failIfNotFound) throws Exception{
loadResourceScript(RT.class, name, failIfNotFound);
}

public static void loadResourceScript(Class c, String name) throws Exception{
loadResourceScript(c, name, true);
}

public static void loadResourceScript(Class c, String name, boolean failIfNotFound) throws Exception{
int slash = name.lastIndexOf('/');
String file = slash >= 0 ? name.substring(slash + 1) : name;
InputStream ins = c.getResourceAsStream("/" + name);
Expand All @@ -337,6 +353,10 @@ public static void loadResourceScript(Class c, String name) throws Exception{
Compiler.load(new InputStreamReader(ins), name, file);
ins.close();
}
else if(failIfNotFound)
{
throw new FileNotFoundException("Could not locate Clojure resource on classpath: " + name);
}
}

static public void init() throws Exception{
Expand All @@ -345,11 +365,11 @@ static public void init() throws Exception{

static void doInit() throws Exception{
loadResourceScript(RT.class, "clojure/boot.clj");
loadResourceScript(RT.class, "clojure/proxy.clj");
loadResourceScript(RT.class, "clojure/genclass.clj");
loadResourceScript(RT.class, "clojure/zip/zip.clj");
loadResourceScript(RT.class, "clojure/xml/xml.clj");
loadResourceScript(RT.class, "clojure/set/set.clj");
loadResourceScript(RT.class, "clojure/proxy.clj", false);
loadResourceScript(RT.class, "clojure/genclass.clj", false);
loadResourceScript(RT.class, "clojure/zip/zip.clj", false);
loadResourceScript(RT.class, "clojure/xml/xml.clj", false);
loadResourceScript(RT.class, "clojure/set/set.clj", false);

Var.pushThreadBindings(
RT.map(CURRENT_NS, CURRENT_NS.get(),
Expand All @@ -363,7 +383,7 @@ static void doInit() throws Exception{
Var refer = var("clojure", "refer");
in_ns.invoke(USER);
refer.invoke(CLOJURE);
loadResourceScript(RT.class, "user.clj");
maybeLoadResourceScript("user.clj");
}
finally
{
Expand Down

0 comments on commit 5c97b61

Please sign in to comment.