rsense-core 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +1 -0
- data/README.md +35 -0
- data/Rakefile +84 -0
- data/TypeAnnotation.tokens +41 -0
- data/build.xml +84 -0
- data/build_lib/antlr-3.2.jar +0 -0
- data/lib/jars/ant-1.7.0.jar +0 -0
- data/lib/jars/ant-launcher-1.7.0.jar +0 -0
- data/lib/jars/antlr-runtime-3.2.jar +0 -0
- data/lib/jars/bsf-2.3.0.jar +0 -0
- data/lib/rsense-core.rb +28 -0
- data/lib/rsense.jar +0 -0
- data/lib/rsense/core.rb +5 -0
- data/lib/rsense/core/version.rb +5 -0
- data/lib/rsense/parser.rb +6 -0
- data/lib/rsense/ruby.rb +19 -0
- data/lib/rsense/typing.rb +13 -0
- data/lib/rsense/typing/annotation.rb +20 -0
- data/lib/rsense/typing/runtime.rb +23 -0
- data/lib/rsense/typing/vertex.rb +15 -0
- data/lib/rsense/util.rb +9 -0
- data/rsense-core.gemspec +30 -0
- data/src/org/cx4a/rsense/CodeAssist.java +744 -0
- data/src/org/cx4a/rsense/CodeAssistError.java +31 -0
- data/src/org/cx4a/rsense/CodeAssistResult.java +42 -0
- data/src/org/cx4a/rsense/CodeCompletionResult.java +65 -0
- data/src/org/cx4a/rsense/FindDefinitionResult.java +24 -0
- data/src/org/cx4a/rsense/LoadResult.java +19 -0
- data/src/org/cx4a/rsense/Main.java +916 -0
- data/src/org/cx4a/rsense/Options.java +353 -0
- data/src/org/cx4a/rsense/Project.java +103 -0
- data/src/org/cx4a/rsense/TypeInferenceResult.java +25 -0
- data/src/org/cx4a/rsense/WhereResult.java +19 -0
- data/src/org/cx4a/rsense/parser/TypeAnnotation.g +221 -0
- data/src/org/cx4a/rsense/parser/TypeAnnotationLexer.java +1759 -0
- data/src/org/cx4a/rsense/parser/TypeAnnotationParser.java +2025 -0
- data/src/org/cx4a/rsense/ruby/Block.java +10 -0
- data/src/org/cx4a/rsense/ruby/Context.java +75 -0
- data/src/org/cx4a/rsense/ruby/DynamicMethod.java +10 -0
- data/src/org/cx4a/rsense/ruby/DynamicScope.java +51 -0
- data/src/org/cx4a/rsense/ruby/Frame.java +95 -0
- data/src/org/cx4a/rsense/ruby/IRubyObject.java +17 -0
- data/src/org/cx4a/rsense/ruby/LocalScope.java +43 -0
- data/src/org/cx4a/rsense/ruby/MetaClass.java +50 -0
- data/src/org/cx4a/rsense/ruby/Ruby.java +242 -0
- data/src/org/cx4a/rsense/ruby/RubyClass.java +146 -0
- data/src/org/cx4a/rsense/ruby/RubyModule.java +255 -0
- data/src/org/cx4a/rsense/ruby/RubyObject.java +94 -0
- data/src/org/cx4a/rsense/ruby/Scope.java +7 -0
- data/src/org/cx4a/rsense/ruby/SpecialObject.java +15 -0
- data/src/org/cx4a/rsense/ruby/Visibility.java +17 -0
- data/src/org/cx4a/rsense/typing/Graph.java +1690 -0
- data/src/org/cx4a/rsense/typing/Propagation.java +73 -0
- data/src/org/cx4a/rsense/typing/Template.java +84 -0
- data/src/org/cx4a/rsense/typing/TemplateAttribute.java +158 -0
- data/src/org/cx4a/rsense/typing/TypeSet.java +48 -0
- data/src/org/cx4a/rsense/typing/annotation/ClassType.java +57 -0
- data/src/org/cx4a/rsense/typing/annotation/MethodType.java +79 -0
- data/src/org/cx4a/rsense/typing/annotation/TypeAnnotation.java +4 -0
- data/src/org/cx4a/rsense/typing/annotation/TypeAny.java +7 -0
- data/src/org/cx4a/rsense/typing/annotation/TypeApplication.java +37 -0
- data/src/org/cx4a/rsense/typing/annotation/TypeConstraint.java +29 -0
- data/src/org/cx4a/rsense/typing/annotation/TypeExpression.java +11 -0
- data/src/org/cx4a/rsense/typing/annotation/TypeIdentity.java +59 -0
- data/src/org/cx4a/rsense/typing/annotation/TypeOptional.java +22 -0
- data/src/org/cx4a/rsense/typing/annotation/TypePragma.java +22 -0
- data/src/org/cx4a/rsense/typing/annotation/TypeSplat.java +22 -0
- data/src/org/cx4a/rsense/typing/annotation/TypeTuple.java +35 -0
- data/src/org/cx4a/rsense/typing/annotation/TypeUnion.java +23 -0
- data/src/org/cx4a/rsense/typing/annotation/TypeVariable.java +44 -0
- data/src/org/cx4a/rsense/typing/runtime/AliasMethod.java +94 -0
- data/src/org/cx4a/rsense/typing/runtime/AnnotationHelper.java +69 -0
- data/src/org/cx4a/rsense/typing/runtime/AnnotationResolver.java +523 -0
- data/src/org/cx4a/rsense/typing/runtime/Array.java +84 -0
- data/src/org/cx4a/rsense/typing/runtime/ClassTag.java +27 -0
- data/src/org/cx4a/rsense/typing/runtime/DefaultMethod.java +115 -0
- data/src/org/cx4a/rsense/typing/runtime/Hash.java +131 -0
- data/src/org/cx4a/rsense/typing/runtime/LoopTag.java +21 -0
- data/src/org/cx4a/rsense/typing/runtime/Method.java +32 -0
- data/src/org/cx4a/rsense/typing/runtime/MonomorphicObject.java +77 -0
- data/src/org/cx4a/rsense/typing/runtime/ObjectAllocator.java +40 -0
- data/src/org/cx4a/rsense/typing/runtime/PolymorphicObject.java +90 -0
- data/src/org/cx4a/rsense/typing/runtime/Proc.java +100 -0
- data/src/org/cx4a/rsense/typing/runtime/RuntimeHelper.java +1339 -0
- data/src/org/cx4a/rsense/typing/runtime/SpecialMethod.java +119 -0
- data/src/org/cx4a/rsense/typing/runtime/TypeVarMap.java +112 -0
- data/src/org/cx4a/rsense/typing/runtime/VertexHolder.java +48 -0
- data/src/org/cx4a/rsense/typing/vertex/CallVertex.java +122 -0
- data/src/org/cx4a/rsense/typing/vertex/MultipleAsgnVertex.java +23 -0
- data/src/org/cx4a/rsense/typing/vertex/PassThroughVertex.java +20 -0
- data/src/org/cx4a/rsense/typing/vertex/SValueVertex.java +24 -0
- data/src/org/cx4a/rsense/typing/vertex/SplatVertex.java +24 -0
- data/src/org/cx4a/rsense/typing/vertex/ToAryVertex.java +24 -0
- data/src/org/cx4a/rsense/typing/vertex/TypeVarVertex.java +22 -0
- data/src/org/cx4a/rsense/typing/vertex/Vertex.java +221 -0
- data/src/org/cx4a/rsense/typing/vertex/YieldVertex.java +70 -0
- data/src/org/cx4a/rsense/util/HereDocReader.java +48 -0
- data/src/org/cx4a/rsense/util/Logger.java +111 -0
- data/src/org/cx4a/rsense/util/NodeUtil.java +198 -0
- data/src/org/cx4a/rsense/util/SourceLocation.java +70 -0
- data/src/org/cx4a/rsense/util/StringUtil.java +63 -0
- data/src/resources/org/cx4a/rsense/rsense.properties +1 -0
- data/stubs/1.8/_builtin.rb +3006 -0
- data/stubs/1.8/bigdecimal.rb +131 -0
- data/stubs/1.8/cgi.rb +257 -0
- data/stubs/1.8/date.rb +147 -0
- data/stubs/1.8/optparse.rb +113 -0
- data/stubs/1.8/rational.rb +47 -0
- data/stubs/1.8/set.rb +94 -0
- data/stubs/1.8/socket.rb +461 -0
- data/stubs/1.8/stringio.rb +129 -0
- data/test/data/a file.rb +1 -0
- data/test/data/benchmark.rb +12 -0
- data/test/data/crlf.rb +5 -0
- data/test/data/test.rb +19 -0
- data/test/script/all.rsense +2 -0
- data/test/script/array_dynamic.rsense +25 -0
- data/test/script/block_nested.rsense +7 -0
- data/test/script/builtin.rsense +785 -0
- data/test/script/class_method_partial_update.rsense +52 -0
- data/test/script/class_partial_update.rsense +17 -0
- data/test/script/find-definition.rsense +72 -0
- data/test/script/method_arg_onearg.rsense +6 -0
- data/test/script/method_arg_optional.rsense +7 -0
- data/test/script/method_partial_update.rsense +14 -0
- data/test/script/method_yield_arrayarg.rsense +8 -0
- data/test/script/method_yield_arrayarg_expand.rsense +8 -0
- data/test/script/method_yield_arrayarg_splat.rsense +17 -0
- data/test/script/misc.rsense +2 -0
- data/test/script/proc_higher_order.rsense +22 -0
- data/test/script/regression.rsense +95 -0
- data/test/script/stdlib.rsense +66 -0
- data/test/script/where.rsense +41 -0
- metadata +315 -0
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
package org.cx4a.rsense.ruby;
|
|
2
|
+
|
|
3
|
+
import java.util.List;
|
|
4
|
+
import java.util.ArrayList;
|
|
5
|
+
import java.util.Set;
|
|
6
|
+
|
|
7
|
+
import org.cx4a.rsense.util.SourceLocation;
|
|
8
|
+
import org.cx4a.rsense.ruby.Ruby;
|
|
9
|
+
|
|
10
|
+
public class RubyClass extends RubyModule {
|
|
11
|
+
protected RubyClass superClass;
|
|
12
|
+
|
|
13
|
+
public static RubyClass newClass(Ruby runtime, String baseName, RubyClass superClass) {
|
|
14
|
+
return newClass(runtime, baseName, superClass, null);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
public static RubyClass newClassWithLocation(Ruby runtime, String baseName, RubyClass superClass, SourceLocation location) {
|
|
18
|
+
return newClassWithLocation(runtime, baseName, superClass, null, location);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
public static RubyClass newClass(Ruby runtime, String baseName, RubyClass superClass, RubyModule parent) {
|
|
22
|
+
return newClassWithLocation(runtime, baseName, superClass, parent, null);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public static RubyClass newClassWithLocation(Ruby runtime, String baseName, RubyClass superClass, RubyModule parent, SourceLocation location) {
|
|
26
|
+
if (superClass == null) {
|
|
27
|
+
superClass = runtime.getObject();
|
|
28
|
+
}
|
|
29
|
+
RubyClass klass = new RubyClass(runtime, superClass, parent, location);
|
|
30
|
+
klass.setBaseName(baseName);
|
|
31
|
+
klass.makeMetaClass(superClass.getMetaClass());
|
|
32
|
+
klass.setLocation(location);
|
|
33
|
+
return klass;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public static RubyClass newBootClass(Ruby runtime, String baseName, RubyClass superClass) {
|
|
37
|
+
RubyClass klass = new RubyClass(runtime, superClass);
|
|
38
|
+
klass.setBaseName(baseName);
|
|
39
|
+
return klass;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
protected RubyClass(Ruby runtime, RubyClass superClass) {
|
|
43
|
+
this(runtime, superClass, null);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
protected RubyClass(Ruby runtime, RubyClass superClass, RubyModule parent) {
|
|
47
|
+
this(runtime, runtime.getClassClass(), superClass, parent, null);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
protected RubyClass(Ruby runtime, RubyClass superClass, RubyModule parent, SourceLocation location) {
|
|
51
|
+
this(runtime, runtime.getClassClass(), superClass, parent, location);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
protected RubyClass(Ruby runtime, RubyClass metaClass, RubyClass superClass, RubyModule parent, SourceLocation location) {
|
|
55
|
+
super(runtime, metaClass, parent, location);
|
|
56
|
+
this.superClass = superClass;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
public RubyClass getRealClass() {
|
|
60
|
+
return this;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
public RubyClass getSuperClass() {
|
|
64
|
+
return superClass;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
public void setSuperClass(RubyClass superClass) {
|
|
68
|
+
this.superClass = superClass;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
public boolean isSingleton() {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
public void addSubclass(RubyClass subclass) {
|
|
76
|
+
// FIXME
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
@Override
|
|
80
|
+
public IRubyObject getConstant(String name) {
|
|
81
|
+
IRubyObject constant = super.getConstant(name);
|
|
82
|
+
if (constant == null && superClass != null && this != getRuntime().getObject()) {
|
|
83
|
+
constant = superClass.getConstant(name);
|
|
84
|
+
}
|
|
85
|
+
return constant;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
@Override
|
|
89
|
+
public Set<String> getConstants(boolean inheritedToo) {
|
|
90
|
+
Set<String> result = super.getConstants(inheritedToo);
|
|
91
|
+
RubyClass sclass = superClass;
|
|
92
|
+
while (sclass != null) {
|
|
93
|
+
result.addAll(sclass.getConstants(inheritedToo));
|
|
94
|
+
sclass = sclass.getSuperClass();
|
|
95
|
+
}
|
|
96
|
+
return result;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
@Override
|
|
100
|
+
public RubyModule getConstantModule(String name) {
|
|
101
|
+
RubyModule module = super.getConstantModule(name);
|
|
102
|
+
RubyClass sclass = superClass;
|
|
103
|
+
while (module == null && sclass != null) {
|
|
104
|
+
module = sclass.getConstantModule(name);
|
|
105
|
+
sclass = sclass.getSuperClass();
|
|
106
|
+
}
|
|
107
|
+
return module;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
@Override
|
|
111
|
+
public DynamicMethod searchMethod(String name) {
|
|
112
|
+
DynamicMethod method = getMethod(name);
|
|
113
|
+
if (method == null && superClass != null) {
|
|
114
|
+
method = superClass.searchMethod(name);
|
|
115
|
+
}
|
|
116
|
+
if (method == null) {
|
|
117
|
+
method = super.searchMethod(name);
|
|
118
|
+
}
|
|
119
|
+
return method;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
@Override
|
|
123
|
+
public Set<String> getMethods(boolean inheritedToo) {
|
|
124
|
+
Set<String> result = super.getMethods(inheritedToo);
|
|
125
|
+
RubyClass sclass = superClass;
|
|
126
|
+
while (sclass != null) {
|
|
127
|
+
result.addAll(sclass.getPublicMethods(inheritedToo));
|
|
128
|
+
sclass = sclass.getSuperClass();
|
|
129
|
+
}
|
|
130
|
+
return result;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
@Override
|
|
134
|
+
public List<RubyModule> getIncludes(boolean inheritedToo) {
|
|
135
|
+
List<RubyModule> result = super.getIncludes(inheritedToo);
|
|
136
|
+
if (inheritedToo) {
|
|
137
|
+
result = new ArrayList<RubyModule>(result);
|
|
138
|
+
RubyClass sclass = superClass;
|
|
139
|
+
while (sclass != null) {
|
|
140
|
+
result.addAll(sclass.getIncludes(inheritedToo));
|
|
141
|
+
sclass = sclass.getSuperClass();
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return result;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
package org.cx4a.rsense.ruby;
|
|
2
|
+
|
|
3
|
+
import java.util.List;
|
|
4
|
+
import java.util.ArrayList;
|
|
5
|
+
import java.util.Set;
|
|
6
|
+
import java.util.HashSet;
|
|
7
|
+
import java.util.Map;
|
|
8
|
+
import java.util.HashMap;
|
|
9
|
+
|
|
10
|
+
import org.cx4a.rsense.util.Logger;
|
|
11
|
+
import org.cx4a.rsense.util.SourceLocation;
|
|
12
|
+
|
|
13
|
+
public class RubyModule extends RubyObject {
|
|
14
|
+
private String baseName;
|
|
15
|
+
private RubyModule parent;
|
|
16
|
+
private Map<String, DynamicMethod> methods;
|
|
17
|
+
private Map<String, IRubyObject> constants;
|
|
18
|
+
private Map<String, IRubyObject> classVars;
|
|
19
|
+
private List<RubyModule> includes;
|
|
20
|
+
private SourceLocation location;
|
|
21
|
+
|
|
22
|
+
public static RubyModule newModule(Ruby runtime, String baseName, RubyModule parent) {
|
|
23
|
+
return newModuleWithLocation(runtime, baseName, parent, null);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public static RubyModule newModuleWithLocation(Ruby runtime, String baseName, RubyModule parent, SourceLocation location) {
|
|
27
|
+
RubyModule module = new RubyModule(runtime, parent);
|
|
28
|
+
module.setBaseName(baseName);
|
|
29
|
+
module.makeMetaClass(runtime.getModule());
|
|
30
|
+
return module;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
protected RubyModule(Ruby runtime) {
|
|
34
|
+
this(runtime, null);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
protected RubyModule(Ruby runtime, RubyModule parent) {
|
|
38
|
+
this(runtime, runtime.getModule(), parent);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
protected RubyModule(Ruby runtime, RubyClass metaClass, RubyModule parent) {
|
|
42
|
+
this(runtime, runtime.getModule(), parent, null);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
protected RubyModule(Ruby runtime, RubyClass metaClass, RubyModule parent, SourceLocation location) {
|
|
46
|
+
super(runtime, metaClass);
|
|
47
|
+
this.parent = parent;
|
|
48
|
+
this.methods = new HashMap<String, DynamicMethod>();
|
|
49
|
+
this.constants = new HashMap<String, IRubyObject>();
|
|
50
|
+
this.classVars = new HashMap<String, IRubyObject>();
|
|
51
|
+
this.includes = new ArrayList<RubyModule>();
|
|
52
|
+
this.location = location;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
public String getBaseName() {
|
|
56
|
+
return baseName;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
public void setBaseName(String baseName) {
|
|
60
|
+
this.baseName = baseName;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
public RubyModule getParent() {
|
|
64
|
+
return parent;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
public void setConstant(String name, IRubyObject constant) {
|
|
68
|
+
constants.put(name, constant);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
public IRubyObject getConstant(String name) {
|
|
72
|
+
return getConstant(name, null);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
private IRubyObject getConstant(String name, Set<RubyModule> seen) {
|
|
76
|
+
if (seen != null && seen.contains(this)) {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
IRubyObject constant = getConstantUnder(name);
|
|
81
|
+
if (constant == null && parent != null) {
|
|
82
|
+
constant = parent.getConstant(name, seen);
|
|
83
|
+
}
|
|
84
|
+
if (constant == null) {
|
|
85
|
+
if (seen == null) {
|
|
86
|
+
seen = new HashSet<RubyModule>();
|
|
87
|
+
}
|
|
88
|
+
seen.add(this);
|
|
89
|
+
for (RubyModule module : includes) {
|
|
90
|
+
constant = module.getConstant(name, seen);
|
|
91
|
+
if (constant != null) {
|
|
92
|
+
return constant;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return constant;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
public IRubyObject getConstantUnder(String name) {
|
|
100
|
+
return constants.get(name);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
public Set<String> getConstants(boolean inheritedToo) {
|
|
104
|
+
Set<String> result = new HashSet<String>(constants.keySet());
|
|
105
|
+
if (inheritedToo) {
|
|
106
|
+
for (RubyModule module : includes) {
|
|
107
|
+
result.addAll(module.getConstants(inheritedToo));
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return result;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
public RubyModule getConstantModule(String name) {
|
|
114
|
+
// FIXME return pairs at getConstants
|
|
115
|
+
if (constants.containsKey(name)) {
|
|
116
|
+
return this;
|
|
117
|
+
} else {
|
|
118
|
+
for (RubyModule module : includes) {
|
|
119
|
+
module = module.getConstantModule(name);
|
|
120
|
+
if (module != null) {
|
|
121
|
+
return module;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
public boolean isConstantDefined(String name) {
|
|
129
|
+
return constants.containsKey(name);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
public IRubyObject getClassVar(String name) {
|
|
133
|
+
return classVars.get(name);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
public void setClassVar(String name, IRubyObject value) {
|
|
137
|
+
classVars.put(name, value);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
public RubyModule defineOrGetClassUnder(String name, RubyClass superClass, SourceLocation location) {
|
|
141
|
+
if (isConstantDefined(name)) {
|
|
142
|
+
IRubyObject object = getConstant(name);
|
|
143
|
+
if (object instanceof RubyModule) {
|
|
144
|
+
return (RubyModule) object;
|
|
145
|
+
} else {
|
|
146
|
+
Logger.error("%s is not class", name);
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
} else {
|
|
150
|
+
RubyClass klass = RubyClass.newClassWithLocation(getRuntime(), name, superClass, this, location);
|
|
151
|
+
setConstant(name, klass);
|
|
152
|
+
return klass;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
public RubyModule defineOrGetModuleUnder(String name, SourceLocation location) {
|
|
157
|
+
if (isConstantDefined(name)) {
|
|
158
|
+
IRubyObject object = getConstant(name);
|
|
159
|
+
if (object.getClass() == RubyModule.class) {
|
|
160
|
+
return (RubyModule) object;
|
|
161
|
+
} else {
|
|
162
|
+
Logger.error("%s is not module", name);
|
|
163
|
+
return null;
|
|
164
|
+
}
|
|
165
|
+
} else {
|
|
166
|
+
RubyModule module = RubyModule.newModuleWithLocation(getRuntime(), name, this, location);
|
|
167
|
+
setConstant(name, module);
|
|
168
|
+
return module;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
public void addMethod(String name, DynamicMethod method) {
|
|
173
|
+
methods.put(name, method);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
public DynamicMethod getMethod(String name) {
|
|
177
|
+
return methods.get(name);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
public Set<String> getMethods(boolean inheritedToo) {
|
|
181
|
+
Set<String> result = new HashSet<String>(methods.keySet());
|
|
182
|
+
if (inheritedToo) {
|
|
183
|
+
for (RubyModule module : includes) {
|
|
184
|
+
result.addAll(module.getPublicMethods(inheritedToo));
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return result;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
public Set<String> getPublicMethods(boolean inheritedToo) {
|
|
191
|
+
Set<String> result = getVisibleMethods(Visibility.PUBLIC);
|
|
192
|
+
if (inheritedToo) {
|
|
193
|
+
for (RubyModule module : includes) {
|
|
194
|
+
result.addAll(module.getPublicMethods(inheritedToo));
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return result;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
private Set<String> getVisibleMethods(Visibility visibility) {
|
|
201
|
+
Set<String> result = new HashSet<String>();
|
|
202
|
+
for (Map.Entry<String, DynamicMethod> entry : methods.entrySet()) {
|
|
203
|
+
if (entry.getValue().getVisibility() == visibility) {
|
|
204
|
+
result.add(entry.getKey());
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return result;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
public DynamicMethod searchMethod(String name) {
|
|
211
|
+
DynamicMethod method = getMethod(name);
|
|
212
|
+
if (method == null) {
|
|
213
|
+
for (RubyModule module : includes) {
|
|
214
|
+
if ((method = module.searchMethod(name)) != null) {
|
|
215
|
+
break;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return method;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
public void includeModule(RubyModule module) {
|
|
223
|
+
includes.add(module);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
public List<RubyModule> getIncludes(boolean inheritedToo) {
|
|
227
|
+
return includes;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
public void setLocation(SourceLocation location) {
|
|
231
|
+
this.location = location;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
public SourceLocation getLocation() {
|
|
235
|
+
return location;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
public String getMethodPath(String name) {
|
|
239
|
+
String path = toString();
|
|
240
|
+
if (name != null)
|
|
241
|
+
path += "#" + name;
|
|
242
|
+
return path;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
@Override
|
|
246
|
+
public String toString() {
|
|
247
|
+
StringBuilder sb = new StringBuilder();
|
|
248
|
+
if (parent != null && parent != getRuntime().getObject()) {
|
|
249
|
+
sb.append(parent.toString());
|
|
250
|
+
sb.append("::");
|
|
251
|
+
}
|
|
252
|
+
sb.append(baseName != null ? baseName : super.toString());
|
|
253
|
+
return sb.toString();
|
|
254
|
+
}
|
|
255
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
package org.cx4a.rsense.ruby;
|
|
2
|
+
|
|
3
|
+
import java.util.Map;
|
|
4
|
+
import java.util.HashMap;
|
|
5
|
+
|
|
6
|
+
public class RubyObject implements IRubyObject {
|
|
7
|
+
protected Ruby runtime;
|
|
8
|
+
protected RubyClass metaClass;
|
|
9
|
+
private Map<String, IRubyObject> instVars;
|
|
10
|
+
private Object tag;
|
|
11
|
+
|
|
12
|
+
protected RubyObject(Ruby runtime) {
|
|
13
|
+
this(runtime, null);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
public RubyObject(Ruby runtime, RubyClass metaClass) {
|
|
17
|
+
this.runtime = runtime;
|
|
18
|
+
this.metaClass = metaClass;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
public Ruby getRuntime() {
|
|
22
|
+
return runtime;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public RubyClass getMetaClass() {
|
|
26
|
+
return metaClass;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
public void setMetaClass(RubyClass metaClass) {
|
|
30
|
+
this.metaClass = metaClass;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
public RubyClass getSingletonClass() {
|
|
34
|
+
RubyClass klass;
|
|
35
|
+
if (getMetaClass().isSingleton() && ((MetaClass) getMetaClass()).getAttached() == this) {
|
|
36
|
+
klass = getMetaClass();
|
|
37
|
+
} else {
|
|
38
|
+
klass = makeMetaClass(getMetaClass());
|
|
39
|
+
}
|
|
40
|
+
return klass;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
public RubyClass makeMetaClass(RubyClass superClass) {
|
|
44
|
+
MetaClass klass = new MetaClass(runtime, superClass);
|
|
45
|
+
setMetaClass(klass);
|
|
46
|
+
|
|
47
|
+
klass.setAttached(this);
|
|
48
|
+
klass.setMetaClass(superClass.getRealClass().getMetaClass());
|
|
49
|
+
|
|
50
|
+
superClass.addSubclass(klass);
|
|
51
|
+
|
|
52
|
+
return klass;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
public IRubyObject getInstVar(String name) {
|
|
56
|
+
return instVars == null ? null : instVars.get(name);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
public void setInstVar(String name, IRubyObject value) {
|
|
60
|
+
if (instVars == null) {
|
|
61
|
+
instVars = new HashMap<String, IRubyObject>();
|
|
62
|
+
}
|
|
63
|
+
instVars.put(name, value);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
public boolean isNil() {
|
|
67
|
+
return this == getRuntime().getNil();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
public boolean isInstanceOf(RubyModule klass) {
|
|
71
|
+
return runtime.isInstanceOf(this, klass);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
public boolean isKindOf(RubyModule klass) {
|
|
75
|
+
return runtime.isKindOf(this, klass);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
public Object getTag() {
|
|
79
|
+
return tag;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
public void setTag(Object tag) {
|
|
83
|
+
this.tag = tag;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
public int hashCode(int depth) {
|
|
87
|
+
return hashCode();
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
@Override
|
|
91
|
+
public String toString() {
|
|
92
|
+
return "<obj:" + metaClass.toString() + ">";
|
|
93
|
+
}
|
|
94
|
+
}
|