-
Notifications
You must be signed in to change notification settings - Fork 9
/
ArgTuples.cr
40 lines (40 loc) · 2.37 KB
/
ArgTuples.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
module Anyolite
module Macro
macro generate_arg_tuple(rb, args, options = {} of Symbol => NoReturn)
Tuple.new(
{% if args %}
{% for arg in args %}
{% if arg.is_a?(TypeDeclaration) %}
{% if arg.value %}
{% if ANYOLITE_INTERNAL_FLAG_USE_GENERAL_OBJECT_FORMAT_CHARS %}
Anyolite::Macro.pointer_type({{arg}}, options: {{options}}).malloc(size: 1, value: Anyolite::RbCast.return_value({{rb}}, {{arg.value}})),
{% else %}
{% if arg.type.is_a?(Union) %}
# This does work, but I'm a bit surprised
Anyolite::Macro.pointer_type({{arg}}, options: {{options}}).malloc(size: 1, value: Anyolite::RbCast.return_value({{rb}}, {{arg.value}})),
{% elsif arg.type.resolve <= String %}
# The outer gods bless my wretched soul that this does neither segfault nor leak
Anyolite::Macro.pointer_type({{arg}}, options: {{options}}).malloc(size: 1, value: {{arg.value}}.to_unsafe),
{% elsif arg.type.resolve <= Anyolite::RbRef %}
Anyolite::Macro.pointer_type({{arg}}, options: {{options}}).malloc(size: 1, value: {{arg.value}}),
# NOTE: This might need some extensions
{% elsif arg.type.resolve <= Bool %}
Anyolite::Macro.pointer_type({{arg}}, options: {{options}}).malloc(size: 1, value: Anyolite::Macro.type_in_ruby({{arg}}, options: {{options}}).new({{arg.value}} ? 1 : 0)),
{% elsif arg.type.resolve <= Number %}
Anyolite::Macro.pointer_type({{arg}}, options: {{options}}).malloc(size: 1, value: Anyolite::Macro.type_in_ruby({{arg}}, options: {{options}}).new({{arg.value}})),
{% else %}
Anyolite::Macro.pointer_type({{arg}}, options: {{options}}).malloc(size: 1, value: Anyolite::RbCast.return_value({{rb}}, {{arg.value}})),
{% end %}
{% end %}
{% else %}
Anyolite::Macro.pointer_type({{arg}}, options: {{options}}).malloc(size: 1),
{% end %}
{% else %}
Anyolite::Macro.pointer_type({{arg}}, options: {{options}}).malloc(size: 1),
{% end %}
{% end %}
{% end %}
)
end
end
end