-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathClojureAgent.java
More file actions
108 lines (93 loc) · 3.31 KB
/
ClojureAgent.java
File metadata and controls
108 lines (93 loc) · 3.31 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
package org.jruby.clojure;
import org.jruby.Ruby;
import org.jruby.RubyClass;
import org.jruby.RubyModule;
import org.jruby.RubyObject;
import org.jruby.anno.JRubyMethod;
import org.jruby.anno.JRubyClass;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import clojure.lang.Agent;
import clojure.lang.ArraySeq;
import org.jruby.javasupport.JavaUtil;
import org.jruby.runtime.Block;
import org.jruby.RubyBoolean;
import static org.jruby.runtime.Visibility.*;
@JRubyClass(name = "Agent")
public class ClojureAgent extends RubyObject {
public static RubyClass createAgentClass(Ruby runtime) {
RubyClass agentc = runtime.defineClass("Agent", runtime.getObject(), AGENT_ALLOCATOR);
agentc.setReifiedClass(ClojureAgent.class);
agentc.kindOf = new RubyModule.KindOf() {
@Override
public boolean isKindOf(IRubyObject obj, RubyModule type) {
return obj instanceof ClojureAgent;
}
};
agentc.defineAnnotatedMethods(ClojureAgent.class);
return agentc;
}
private static ObjectAllocator AGENT_ALLOCATOR = new ObjectAllocator() {
public IRubyObject allocate(Ruby runtime, RubyClass klass) {
return new ClojureAgent(runtime, klass);
}
};
Ruby ruby;
Agent agent;
ClojureAgent(Ruby runtime, RubyClass klass) {
super(runtime, klass);
ruby = null;
agent = null;
}
@JRubyMethod(name = "shutdown", meta = true)
public static IRubyObject shutdown(ThreadContext context, IRubyObject recv) {
Agent.shutdown();
return context.getRuntime().getNil();
}
@JRubyMethod(visibility = PRIVATE)
public IRubyObject initialize(final ThreadContext context, final IRubyObject state, Block block) {
ruby = context.getRuntime();
try {
agent = new Agent(state);
this.setErrorHandler(context, block);
} catch (Exception e) {
throw ruby.newConcurrencyError(e.getLocalizedMessage());
}
return this;
}
@JRubyMethod(name = "dispatch", rest = true)
public IRubyObject dispatch(final ThreadContext context, final IRubyObject[] args, final Block block) {
agent.dispatch( new ClojureFunction(context, block), ArraySeq.create((Object[])args), true);
return ruby.getNil();
}
@JRubyMethod
public Object deref(final ThreadContext context) {
try {
return agent.deref();
} catch (Exception e) {
throw context.getRuntime().newConcurrencyError(e.getLocalizedMessage());
}
}
@JRubyMethod(name = "get_error")
public Object getError(final ThreadContext context) {
return agent.getError();
}
@JRubyMethod
public Object restart(final ThreadContext context, final IRubyObject newState) {
return restart(context, newState, context.getRuntime().getTrue());
}
@JRubyMethod
public Object restart(final ThreadContext context, final IRubyObject newState, final IRubyObject clearActions) {
try {
return agent.restart(newState, clearActions.isTrue());
} catch (Exception e) {
throw context.getRuntime().newConcurrencyError(e.getLocalizedMessage());
}
}
@JRubyMethod(name = "set_error_handler")
public IRubyObject setErrorHandler(final ThreadContext context, final Block block) {
agent.setErrorHandler(new ClojureFunction(context, block));
return context.getRuntime().getNil();
}
}