forked from CommonWealthRobotics/bowler-script-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClojureHelper.java
More file actions
112 lines (95 loc) · 2.75 KB
/
ClojureHelper.java
File metadata and controls
112 lines (95 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package com.neuronrobotics.bowlerstudio.scripting;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import clojure.lang.RT;
import clojure.lang.Symbol;
import clojure.lang.Var;
import eu.mihosoft.vrl.v3d.parametrics.CSGDatabaseInstance;
/**
* Class containing static utility methods for Java to Clojure interop
*
* @author Mike
* https://github.com/mikera/clojure-utils/blob/master/src/main/java/mikera/cljutils/Clojure.java
*/
public class ClojureHelper implements IScriptingLanguage {
public static Var REQUIRE = var("clojure.core", "require");
public static Var META = var("clojure.core", "meta");
public static Var EVAL = var("clojure.core", "eval");
public static Var READ_STRING = var("clojure.core", "load-string");
/**
* Require a namespace by name, loading it if necessary.
*
* Calls clojure.core/require
*/
public static Object require(String nsName) {
return REQUIRE.invoke(Symbol.intern(nsName));
}
public static Object readString(String s) {
return READ_STRING.invoke(s);
}
/**
* Looks up a var by name in the clojure.core namespace.
*
* The var can subsequently be invoked if it is a function.
*/
public static Var var(String varName) {
return var("clojure.core", varName);
}
/**
* Looks up a var by name in the given namespace.
*
* The var can subsequently be invoked if it is a function.
*/
public static Var var(String nsName, String varName) {
return RT.var(nsName, varName);
}
/**
* Evaluates a String, which should contain valid Clojure code.
*/
public static Object eval(String string) {
return EVAL.invoke(readString(string));
}
@Override
public Object inlineScriptRun(CSGDatabaseInstance db,File code, ArrayList<Object> args) {
byte[] bytes;
try {
bytes = Files.readAllBytes(code.toPath());
String s = new String(bytes, "UTF-8");
return inlineScriptRun( db,s, args);
} catch (IOException e1) {
// Auto-generated catch block
com.neuronrobotics.sdk.common.Log.error(e1);
}
// com.neuronrobotics.sdk.common.Log.error("Clojure returned of type="+ret.getClass()+" value="+ret);
return null;
}
@Override
public Object inlineScriptRun(CSGDatabaseInstance db,String code, ArrayList<Object> args) {
return ClojureHelper.eval(code);
}
@Override
public String getShellType() {
return "Clojure";
}
@Override
public boolean getIsTextFile() {
// Auto-generated method stub
return true;
}
/**
* Get the contents of an empty file
*
* @return
*/
public String getDefaultContents() {
return "(println \"hello world\")";
}
@Override
public ArrayList<String> getFileExtension() {
// Auto-generated method stub
return new ArrayList<>(Arrays.asList("clj", "cljs", "cljc"));
}
}