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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
mod util;
#[cfg(feature = "local-rt")]
mod tests {
/*
struct HasConstructors
a::DataType
b::Union{Int16, Int32}
HasConstructors(i::Int16) = new(Int16, i)
HasConstructors(i::Int32) = new(Int32, i)
HasConstructors(v::Bool) = new(Bool, v ? one(Int32) : zero(Int32))
end
HasConstructors() = HasConstructors(false)
*/
use jlrs::{data::managed::union_all::UnionAll, prelude::*};
use crate::util::JULIA;
fn call_outer_constructor() {
JULIA.with(|handle| {
handle.borrow_mut().with_stack(|mut stack| {
stack.scope(|mut frame| {
unsafe {
let ty = Module::main(&frame)
.submodule(&frame, "JlrsTests")
.unwrap()
.as_managed()
.global(&frame, "HasConstructors")
.unwrap()
.as_value();
assert!(ty.is::<DataType>());
let res = ty.call(&mut frame, []);
assert!(res.is_ok());
let value = res.unwrap();
let is_bool = value
.field_accessor()
.field("a")
.unwrap()
.access::<WeakDataType>()
.unwrap()
.as_managed()
.is::<Bool>();
assert!(is_bool);
let field_b = value
.field_accessor()
.field("b")
.unwrap()
.access::<i32>()
.unwrap();
assert_eq!(field_b, 0);
};
});
stack.scope(|mut frame| {
unsafe {
let ty = Module::main(&frame)
.submodule(&frame, "JlrsTests")
.unwrap()
.as_managed()
.global(&frame, "HasConstructors")
.unwrap()
.as_value();
assert!(ty.is::<DataType>());
let res = ty.call(&mut frame, []);
assert!(res.is_ok());
let value = res.unwrap();
let is_bool = value
.field_accessor()
.field("a")
.unwrap()
.access::<WeakDataType>()
.unwrap()
.as_managed()
.is::<Bool>();
assert!(is_bool);
let field_b = value
.field_accessor()
.field("b")
.unwrap()
.access::<i32>()
.unwrap();
assert_eq!(field_b, 0);
};
})
});
});
}
fn call_inner_constructor() {
JULIA.with(|handle| {
handle.borrow_mut().with_stack(|mut stack| {
stack.scope(|mut frame| {
unsafe {
let ty = Module::main(&frame)
.submodule(&frame, "JlrsTests")
.unwrap()
.as_managed()
.global(&frame, "HasConstructors")
.unwrap()
.as_value();
let arg = Value::new(&mut frame, 1i16);
let res = ty.call(&mut frame, [arg]);
assert!(res.is_ok());
let value = res.unwrap();
let is_i16 = value
.field_accessor()
.field("a")
.unwrap()
.access::<WeakDataType>()
.unwrap()
.as_managed()
.is::<i16>();
assert!(is_i16);
let field_b = value
.field_accessor()
.field("b")
.unwrap()
.access::<i16>()
.unwrap();
assert_eq!(field_b, 1);
};
})
});
});
}
fn create_dict<'target, Tgt: Target<'target>>(
tgt: Tgt,
key_ty: Value<'_, 'static>,
value_ty: Value<'_, 'static>,
) -> ValueResult<'target, 'static, Tgt> {
tgt.with_local_scope::<_, 2>(|tgt, mut frame| {
let dict_ua = Module::base(&frame)
.global(&mut frame, "Dict")
.unwrap()
.cast::<UnionAll>()
.unwrap();
unsafe {
let dict_ty = dict_ua.apply_types(&mut frame, [key_ty, value_ty]).unwrap();
dict_ty.call(tgt, [])
}
})
}
fn dict_constructor() {
JULIA.with(|handle| {
handle.borrow().local_scope::<_, 2>(|mut frame| {
let key_ty = DataType::string_type(&frame).as_value();
let value_ty = DataType::int32_type(&frame).as_value();
let dict = create_dict(&mut frame, key_ty, value_ty).unwrap();
let dict2 =
unsafe { Value::eval_string(&mut frame, "Dict{String, Int32}()").unwrap() };
assert_eq!(dict.datatype(), dict2.datatype());
})
});
}
#[test]
fn constructor_tests() {
call_outer_constructor();
call_inner_constructor();
dict_constructor();
}
}