-
Notifications
You must be signed in to change notification settings - Fork 9
/
WrapMethodIndex.cr
167 lines (154 loc) · 10.7 KB
/
WrapMethodIndex.cr
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
module Anyolite
module Macro
macro wrap_method_index(rb_interpreter, crystal_class, method_index, ruby_name,
is_constructor = false, is_class_method = false,
operator = "", cut_name = nil,
without_keywords = false, added_keyword_args = nil,
context = nil, return_nil = false,
block_arg_number = nil, block_return_type = nil,
store_block_arg = false, other_source = nil)
{% method_source = other_source ? other_source : crystal_class %}
{% if is_class_method %}
{% method = method_source.resolve.class.methods[method_index] %}
{% else %}
{% method = method_source.resolve.methods[method_index] %}
{% end %}
{% if !operator.empty? %}
{% if cut_name %}
{% if is_class_method %}
{% final_method_name = "#{crystal_class}.#{cut_name}".id %}
{% final_operator = "#{crystal_class}.#{operator.id}" %}
{% else %}
{% final_method_name = cut_name %}
{% final_operator = operator %}
{% end %}
{% else %}
{% final_method_name = Anyolite::Empty %}
{% if is_class_method %}
{% final_operator = "#{crystal_class}.#{operator.id}" %}
{% else %}
{% final_operator = operator %}
{% end %}
{% end %}
{% else %}
{% if is_class_method %}
{% final_method_name = "#{crystal_class}.#{method.name}".id %}
{% else %}
{% final_method_name = method.name %}
{% end %}
{% final_operator = operator %}
{% end %}
{% final_arg_array = added_keyword_args ? added_keyword_args : method.args %}
# Routine to check all arguments for validity and potential self values
{% try_again = false %}
{% for arg, index in final_arg_array %}
{% if arg.is_a?(Arg) %}
{% if arg.restriction.is_a?(Self) %}
{% try_again = true %}
{% if arg.default_value %}
{% final_arg_array[index] = "#{arg.name} : #{crystal_class} = #{arg.default_value}".id %}
{% else %}
{% final_arg_array[index] = "#{arg.name} : #{crystal_class}".id %}
{% end %}
{% end %}
{% elsif arg.is_a?(TypeDeclaration) %}
{% if arg.type.is_a?(Self) %}
{% try_again = true %}
{% if arg.value %}
{% final_arg_array[index] = "#{arg.var} : #{crystal_class} = #{arg.value}".id %}
{% else %}
{% final_arg_array[index] = "#{arg.var} : #{crystal_class}".id %}
{% end %}
{% end %}
{% else %}
{% raise "Argument #{arg} in function #{final_method_name} of class #{crystal_class} is neither Arg nor TypeDeclaration." %}
{% end %}
{% end %}
{% if try_again %}
Anyolite::Macro.wrap_method_index({{rb_interpreter}}, {{crystal_class}}, {{method_index}},
{{ruby_name}}, is_constructor: {{is_constructor}}, is_class_method: {{is_class_method}},
operator: {{operator}}, cut_name: {{cut_name}}, without_keywords: {{without_keywords}},
added_keyword_args: {{final_arg_array}}, context: {{context}}, return_nil: {{return_nil}},
block_arg_number: {{block_arg_number}}, block_return_type: {{block_return_type}},
store_block_arg: {{store_block_arg}}, other_source: {{other_source}})
{% else %}
{% if final_arg_array.empty? %}
{% if is_class_method %}
Anyolite.wrap_class_method({{rb_interpreter}}, {{crystal_class}}, {{ruby_name}}, {{final_method_name}}, operator: {{final_operator}}, context: {{context}}, return_nil: {{return_nil}}, block_arg_number: {{block_arg_number}}, block_return_type: {{block_return_type}}, store_block_arg: {{store_block_arg}})
{% elsif is_constructor %}
# Do not ever let a constructor return nil (for now)
Anyolite.wrap_constructor({{rb_interpreter}}, {{crystal_class}}, context: {{context}}, block_arg_number: {{block_arg_number}}, block_return_type: {{block_return_type}}, store_block_arg: {{store_block_arg}})
{% else %}
Anyolite.wrap_instance_method({{rb_interpreter}}, {{crystal_class}}, {{ruby_name}}, {{final_method_name}}, operator: {{final_operator}}, context: {{context}}, return_nil: {{return_nil}}, block_arg_number: {{block_arg_number}}, block_return_type: {{block_return_type}}, store_block_arg: {{store_block_arg}})
{% end %}
# A complicated check, but it is more stable than simply checking for colons
{% elsif !final_arg_array.find { |m| !((m.is_a?(TypeDeclaration) && m.type) || (m.is_a?(Arg) && m.restriction)) } %}
{% if without_keywords %}
{% if without_keywords >= final_arg_array.size %}
{% regular_arg_partition = nil %}
{% keyword_arg_partition = final_arg_array %}
{% elsif without_keywords == -2 %}
{% first_optional_arg = 0 %}
{% final_arg_array.each { |arg| first_optional_arg += 1 unless (arg.is_a?(TypeDeclaration) && !arg.value.is_a?(Nop)) || (arg.is_a?(Arg) && !arg.default_value.is_a?(Nop)) } %}
{% regular_arg_partition = final_arg_array[0..first_optional_arg - 1] %}
{% keyword_arg_partition = final_arg_array[first_optional_arg..-1] %}
{% regular_arg_partition = nil if first_optional_arg == 0 || regular_arg_partition.empty? %}
{% keyword_arg_partition = nil if keyword_arg_partition.empty? %}
{% elsif without_keywords < 0 %}
{% regular_arg_partition = final_arg_array %}
{% keyword_arg_partition = nil %}
{% else %}
{% regular_arg_partition = final_arg_array[0..without_keywords - 1] %}
{% keyword_arg_partition = final_arg_array[without_keywords..-1] %}
{% regular_arg_partition = nil if without_keywords == 0 || regular_arg_partition.empty? %}
{% keyword_arg_partition = nil if keyword_arg_partition.empty? %}
{% end %}
{% if keyword_arg_partition %}
{% if is_class_method %}
Anyolite.wrap_class_method_with_keywords({{rb_interpreter}}, {{crystal_class}}, {{ruby_name}}, {{final_method_name}},
{{keyword_arg_partition}}, regular_args: {{regular_arg_partition}}, operator: {{final_operator}}, context: {{context}}, return_nil: {{return_nil}}, block_arg_number: {{block_arg_number}}, block_return_type: {{block_return_type}}, store_block_arg: {{store_block_arg}})
{% elsif is_constructor %}
Anyolite.wrap_constructor_with_keywords({{rb_interpreter}}, {{crystal_class}},
{{keyword_arg_partition}}, regular_args: {{regular_arg_partition}}, operator: {{final_operator}}, context: {{context}}, block_arg_number: {{block_arg_number}}, block_return_type: {{block_return_type}}, store_block_arg: {{store_block_arg}})
{% else %}
Anyolite.wrap_instance_method_with_keywords({{rb_interpreter}}, {{crystal_class}}, {{ruby_name}}, {{final_method_name}},
{{keyword_arg_partition}}, regular_args: {{regular_arg_partition}}, operator: {{final_operator}}, context: {{context}}, return_nil: {{return_nil}}, block_arg_number: {{block_arg_number}}, block_return_type: {{block_return_type}}, store_block_arg: {{store_block_arg}})
{% end %}
{% else %}
{% if is_class_method %}
Anyolite.wrap_class_method({{rb_interpreter}}, {{crystal_class}}, {{ruby_name}}, {{final_method_name}},
{{regular_arg_partition}}, operator: {{final_operator}}, context: {{context}}, return_nil: {{return_nil}}, block_arg_number: {{block_arg_number}}, block_return_type: {{block_return_type}}, store_block_arg: {{store_block_arg}})
{% elsif is_constructor %}
Anyolite.wrap_constructor({{rb_interpreter}}, {{crystal_class}},
{{regular_arg_partition}}, operator: {{final_operator}}, context: {{context}}, block_arg_number: {{block_arg_number}}, block_return_type: {{block_return_type}}, store_block_arg: {{store_block_arg}})
{% else %}
Anyolite.wrap_instance_method({{rb_interpreter}}, {{crystal_class}}, {{ruby_name}}, {{final_method_name}},
{{regular_arg_partition}}, operator: {{final_operator}}, context: {{context}}, return_nil: {{return_nil}}, block_arg_number: {{block_arg_number}}, block_return_type: {{block_return_type}}, store_block_arg: {{store_block_arg}})
{% end %}
{% end %}
{% else %}
{% if is_class_method %}
Anyolite.wrap_class_method_with_keywords({{rb_interpreter}}, {{crystal_class}}, {{ruby_name}}, {{final_method_name}},
{{final_arg_array}}, operator: {{final_operator}}, context: {{context}}, return_nil: {{return_nil}}, block_arg_number: {{block_arg_number}}, block_return_type: {{block_return_type}}, store_block_arg: {{store_block_arg}})
{% elsif is_constructor %}
Anyolite.wrap_constructor_with_keywords({{rb_interpreter}}, {{crystal_class}},
{{final_arg_array}}, operator: {{final_operator}}, context: {{context}}, block_arg_number: {{block_arg_number}}, block_return_type: {{block_return_type}}, store_block_arg: {{store_block_arg}})
{% else %}
Anyolite.wrap_instance_method_with_keywords({{rb_interpreter}}, {{crystal_class}}, {{ruby_name}}, {{final_method_name}},
{{final_arg_array}}, operator: {{final_operator}}, context: {{context}}, return_nil: {{return_nil}}, block_arg_number: {{block_arg_number}}, block_return_type: {{block_return_type}}, store_block_arg: {{store_block_arg}})
{% end %}
{% end %}
{% elsif !is_class_method && operator == "==" && final_arg_array[0] && !final_arg_array[1] %}
{% options = {:context => context} %}
Anyolite::Macro.wrap_equality_function({{rb_interpreter}}, {{crystal_class}}, {{ruby_name}}, {{final_method_name}}, operator: {{final_operator}}, options: {{options}})
{% else %}
{% if is_class_method %}
{% puts "\e[33m> INFO: Could not wrap function '#{crystal_class}.#{method.name}' with args #{method.args}.\e[0m" %}
{% else %}
{% puts "\e[33m> INFO: Could not wrap function '#{method.name}' with args #{method.args}.\e[0m" %}
{% end %}
{% end %}
{% end %}
end
end
end