diff --git a/Clojure/Clojure/Hosting/Clojure.cs b/Clojure/Clojure/Hosting/Clojure.cs new file mode 100644 index 0000000..02c87e3 --- /dev/null +++ b/Clojure/Clojure/Hosting/Clojure.cs @@ -0,0 +1,68 @@ +using System.Dynamic; + +namespace clojure.lang.Hosting +{ + /// + /// Convenience class for interfacing with Clojure from other .NET languages. + /// Provides an extremely simple way to retreive Clojure vars and invoke Clojure + /// functions. + /// + public class Clojure + { + private static readonly Var REQUIRE = RT.var("clojure.core", "require"); + + /// + /// Calls (require nsname) on the namespace name provided by + /// . + /// + /// The name of the namespace to require, ex.: + /// clojure.string + public static void Require(string nsname) + { + if (!nsname.Equals("clojure.core")) + REQUIRE.invoke(Symbol.intern(nsname)); + } + + /// + /// Returns the Var object refered to in the namespace called + /// with the name . + /// + /// + /// var myVar = Clojure.GetVar("mynamespace", "my-var"); + /// myVar.invoke("Hello World"); + /// + /// The namespace name + /// The name of the Var + /// The requested Var + public static Var GetVar(string nsname, string varName) + { + Require(nsname); + return RT.var(nsname, varName); + } + + /// + /// Allows a namespace to be loaded from a directory whose path corresponds to + /// only part of the namespace name. This can be used to load .clj files properly + /// from the repl that are being stored as embedded resources in mixed language .NET + /// DLL's (for example existing C# and Visual Basic projects). + /// + /// + /// + /// + /// Adding a load mapping from "MyCompany.MyProject" to "MyProject" allows the namespace "A.B.C" to be + /// stored in "MyProject/C.clj" on the file system. Assuming the default namespace + /// (configurable in a Visual Studio project's properties) is "MyCompany.MyProject", + /// "C.clj" can be stored as an embedded resource in "MyProject.dll" and still loaded + /// properly by the Clojure namespace loader. This is because embedded resources receive a virtual filename + /// in a DLL based on both the DLL's default namespace and the file's actual path relative to the project. + /// In order for the namespace to be loaded from the repl, a load mapping must be created. + /// + /// AddNamespaceLoadMapping("MyCompany.MyProject", "MySolutionDir/MyProject"); + /// + /// + public static void AddNamespaceLoadMapping(string namespaceBase, string directory) + { + RT.var("clojure.core", "add-ns-load-mapping").invoke(namespaceBase, directory); + } + } +}