jtor-stdlib 0.1.1-java → 0.1.2-java
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 +4 -4
- data/lib/jtor-stdlib.rb +1 -0
- data/lib/jtor-stdlib/base.rb +68 -29
- data/lib/jtor-stdlib/superobject.rb +18 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 23edc4304941a8c8b28d4ed49943f1c381c092cf
|
4
|
+
data.tar.gz: 17f06f61a48310cc6cf8944451d844b7b736d550
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dabbfe23ec0faceb3a6ec3d6a9920a9bdd8fc323b0d2f04484241ca89138673376f7ac7b402480ee5dab002260cbaf152d31dbcc7819a84639f001277dc8f617
|
7
|
+
data.tar.gz: 54e182ce43ee02084b0a73f8b00eba09bf64657a46a8d6019d80d5d7aff7113590bcd8c21222bf522eac3f5b823fab52d6a58a43dc1646f77b4d563b189aaf85
|
data/lib/jtor-stdlib.rb
CHANGED
data/lib/jtor-stdlib/base.rb
CHANGED
@@ -1,42 +1,81 @@
|
|
1
1
|
module Jtor
|
2
2
|
module StdLib
|
3
3
|
module Base
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
def
|
9
|
-
|
10
|
-
unless method_defined?(name)
|
11
|
-
define_method(name) { |*args| self.class.lookup(name, *args) }
|
12
|
-
end
|
4
|
+
def self.included(base)
|
5
|
+
base.extend(ClassMethods)
|
6
|
+
end
|
7
|
+
|
8
|
+
def sup
|
9
|
+
@_sup ||= SuperObject.new(self)
|
13
10
|
end
|
14
11
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
12
|
+
module ClassMethods
|
13
|
+
def _lookup_table
|
14
|
+
@_lookup_table ||= {}
|
15
|
+
end
|
16
|
+
|
17
|
+
def _constructor_calls
|
18
|
+
@_constructor_calls ||= {}
|
19
|
+
end
|
20
|
+
|
21
|
+
# Method overloading is not a ruby thing, so we implement a pseudo-lookup
|
22
|
+
# mechanism for method based on the type/count of their args
|
23
|
+
def add_java_method(name, param_types, &block)
|
24
|
+
previously_defined = _lookup_table[name]
|
25
|
+
add_method_to_lookup(name, param_types, &block)
|
26
|
+
return if previously_defined
|
27
|
+
define_method(name) do |*args|
|
28
|
+
instance_exec(*args, &self.class.lookup(name, *args))
|
23
29
|
end
|
24
30
|
end
|
25
|
-
method_missing(name, *args)
|
26
|
-
end
|
27
31
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
return
|
32
|
+
def add_java_constructor(param_types, constructor_call = nil, &block)
|
33
|
+
add_method_to_lookup(:initialize, param_types, &block)
|
34
|
+
previously_defined = _constructor_calls.any?
|
35
|
+
_constructor_calls[param_types] = constructor_call
|
36
|
+
return if previously_defined
|
37
|
+
define_method(:initialize) do |*args|
|
38
|
+
method, constructor_call = self.class.lookup(:initialize, *args)
|
39
|
+
# I really tried to avoid using eval fellows, but JRuby is very fixed
|
40
|
+
# on `super` being called here first than anything, and it wouldn't
|
41
|
+
# accept a `proc` calling `initialize` on `sup`, so this will have
|
42
|
+
# to do for now.
|
43
|
+
eval(constructor_call) #if constructor_call
|
44
|
+
instance_exec(*args, &method)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def lookup(name, *args)
|
49
|
+
if _lookup_table[name]
|
50
|
+
_lookup_table[name].each do |param_types, method|
|
51
|
+
# NOTE: This is an oversimplification of the way Java determines
|
52
|
+
# which method to invoke. I may work further into this later (see
|
53
|
+
# http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12)
|
54
|
+
next unless params_match(param_types, args)
|
55
|
+
return (if (constructor_call = _constructor_calls[param_types])
|
56
|
+
[method, constructor_call]
|
57
|
+
else
|
58
|
+
method
|
59
|
+
end)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
method_missing(name, *args)
|
33
63
|
end
|
34
|
-
true
|
35
|
-
end
|
36
64
|
|
37
|
-
|
38
|
-
|
39
|
-
|
65
|
+
def params_match(types, values)
|
66
|
+
# TODO: Handle var args (...)
|
67
|
+
return false if types.size != values.size
|
68
|
+
types.each_with_index do |type, index|
|
69
|
+
value = values[index]
|
70
|
+
return false unless value.is_a?(type) || value.to_java.is_a?(type)
|
71
|
+
end
|
72
|
+
true
|
73
|
+
end
|
74
|
+
|
75
|
+
def add_method_to_lookup(name, param_types, &block)
|
76
|
+
_lookup_table[name] ||= {}
|
77
|
+
_lookup_table[name][param_types] = block
|
78
|
+
end
|
40
79
|
end
|
41
80
|
end
|
42
81
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Jtor
|
2
|
+
module StdLib
|
3
|
+
class SuperObject
|
4
|
+
def initialize(obj)
|
5
|
+
@obj = obj
|
6
|
+
end
|
7
|
+
|
8
|
+
def method_missing(symbol, *args, &block)
|
9
|
+
superclass = @obj.class.superclass
|
10
|
+
if superclass < Base # If it's a Jtor class
|
11
|
+
superclass.lookup(@obj, symbol, *args)
|
12
|
+
else # All other classes (from http://stackoverflow.com/a/1251199)
|
13
|
+
superclass.instance_method(symbol).bind(@obj).call(*args, &block)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jtor-stdlib
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: java
|
6
6
|
authors:
|
7
7
|
- Alejandro Rodríguez
|
@@ -19,6 +19,7 @@ files:
|
|
19
19
|
- lib/jtor-stdlib.rb
|
20
20
|
- lib/jtor-stdlib/base.rb
|
21
21
|
- lib/jtor-stdlib/jtor_import.rb
|
22
|
+
- lib/jtor-stdlib/superobject.rb
|
22
23
|
homepage: https://gitlab.com/eReGeBe/jtor-stdlib
|
23
24
|
licenses:
|
24
25
|
- MIT
|