-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.mojo
548 lines (417 loc) · 16.5 KB
/
main.mojo
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
# ===----------------------------------------------------------------------=== #
# Licensed under the Apache License v2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ===----------------------------------------------------------------------=== #
"""Implement mojo dynamic dispatch using trait and union type.
Mojo generic can only support same type `T` as parameter. How to achieve subtype dynamic
dispatch, such as `Box<dyn trait>` in rust, or `std::shared_ptr<Base *>` in c++?
We can combine the idea of Variant and trait in Mojo to achieve this.
A Variant is a sum type, such as `enum` in rust, or `tagged union` in c++ (or std::variant
in c++17). It can hold a value that could take on several different, but fixed types.
We can define several struct implementing one same trait, such as `Echoable`. And define a
Variant from all these types, which is constrained by one same trait. Then we implement
the trait `Echoable` for the Variant.
For example:
```mojo
struct MyVariant[*Ts: Echoable](
Echoable,
):
...
fn echo(self) -> String:
var res: String = String("")
var cur = self._get_state()[]
@parameter
fn each[i: Int]():
if cur == i:
alias T = Ts[i]
res = self[T].echo()
unroll[each, len(VariadicList(Ts))]()
return res
```
As the above code shows, when we have a Variant at runtime, we known its underlying type.
We can compare it with all the candidate types, and then get the actual object, call the
trait function. This has exactly the same effect of dynamic dispatch in rust or c++.
The comparing between types is actually comparing using an `int8`, which is determined by
the order of types in compile time. And loop of the types is unrolled in compile time using
`@parameter` meta programming. At runtime, there are just some `int8` comparing to determine
which object to use. Only Exactly one of them will be true. I'm not sure how fast it is
comparing to v-table in c++ or rust, but it should be really fast with only some `int8` comparing.
Wow, awesome mojo! We just implement dynamic dispatch in a library, not in the compiler! Just
as mojo's design principle said, implement the language in library, not in compiler.
Right now we cannot use trait as generic parameters. So Variant cannot be reused directly.
Most of code are same as implementation in mojo `stdlib/src/utils/variant.mojo`, so I just
copied the code. Maybe when macro are supported in the future, we can have a more generic
implementation.
You can use the same idea to implement your dynamic dispatch (like subtype in c++).
For example:
```mojo
alias AOrB = MyVariant[A, B]
var a = A()
var b = B()
var x = AOrB(a)
var y = AOrB(b)
# result: this is A
print(x.echo())
# result: this is B
print(y.echo())
```
"""
from python import Python
from sys import alignof, sizeof
from sys.intrinsics import _type_is_eq
from memory import UnsafePointer
from memory.unsafe_pointer import (
initialize_pointee_move,
move_from_pointee,
move_pointee,
)
from utils import unroll
@always_inline
fn align_up(value: Int, alignment: Int) -> Int:
var div_ceil = (value + alignment - 1)._positive_div(alignment)
return div_ceil * alignment
# ===----------------------------------------------------------------------=== #
# Variant
# ===----------------------------------------------------------------------=== #
trait Echoable(CollectionElement, ExplicitlyCopyable):
fn echo(self) -> String:
...
struct UnionSize[*Ts: Echoable]():
@staticmethod
fn compute() -> Int:
var size = 0
@parameter
fn each[i: Int]():
size = max(size, align_up(sizeof[Ts[i]](), alignof[Ts[i]]()))
unroll[each, len(VariadicList(Ts))]()
return align_up(size, alignof[Int]())
struct UnionTypeIndex[T: Echoable, *Ts: Echoable]:
@staticmethod
fn compute() -> Int8:
var result = -1
@parameter
fn each[i: Int]():
alias q = Ts[i]
@parameter
if _type_is_eq[q, T]():
result = i
unroll[each, len(VariadicList(Ts))]()
return result
struct MyVariant[*Ts: Echoable](
Echoable,
):
"""A runtime-variant type.
Data for this type is stored internally. Currently, its size is the
largest size of any of its variants plus a 16-bit discriminant.
You can
- use `isa[T]()` to check what type a variant is
- use `unsafe_take[T]()` to take a value from the variant
- use `[T]` to get a value out of a variant
- This currently does an extra copy/move until we have lifetimes
- It also temporarily requires the value to be mutable
- use `set[T](owned new_value: T)` to reset the variant to a new value
Example:
```mojo
alias IntOrString = MyVariant[Int, String]
fn to_string(inout x: IntOrString) -> String:
if x.isa[String]():
return x[String][]
# x.isa[Int]()
return str(x[Int][])
# They have to be mutable for now, and implement Echoable
var an_int = IntOrString(4)
var a_string = IntOrString(String("I'm a string!"))
var who_knows = IntOrString(0)
import random
if random.random_ui64(0, 1):
who_knows.set[String]("I'm actually a string too!")
print(to_string(an_int))
print(to_string(a_string))
print(to_string(who_knows))
```
Parameters:
Ts: The elements of the variadic.
"""
# Fields
alias _sentinel: Int = -1
alias _mlir_type = __mlir_type[
`!kgen.variant<[rebind(:`, __type_of(Ts), ` `, Ts, `)]>`
]
var _impl: Self._mlir_type
# ===-------------------------------------------------------------------===#
# Life cycle methods
# ===-------------------------------------------------------------------===#
fn __init__(inout self, *, unsafe_uninitialized: ()):
"""Unsafely create an uninitialized Variant.
Args:
unsafe_uninitialized: Marker argument indicating this initializer is unsafe.
"""
self._impl = __mlir_attr[`#kgen.unknown : `, Self._mlir_type]
fn __init__[T: Echoable](inout self, owned value: T):
"""Create a variant with one of the types.
Parameters:
T: The type to initialize the variant to. Generally this should
be able to be inferred from the call type, eg. `Variant[Int, String](4)`.
Args:
value: The value to initialize the variant with.
"""
self._impl = __mlir_attr[`#kgen.unknown : `, self._mlir_type]
self._get_state()[] = Self._check[T]()
initialize_pointee_move(self._get_ptr[T](), value^)
fn __init__(inout self, other: Self):
"""Explicitly creates a deep copy of an existing variant.
Args:
other: The value to copy from.
"""
self = Self(unsafe_uninitialized=())
self._get_state()[] = other._get_state()[]
@parameter
fn each[i: Int]():
if self._get_state()[] == i:
alias T = Ts[i]
initialize_pointee_move(
UnsafePointer.address_of(self._impl).bitcast[T](),
UnsafePointer.address_of(other._impl).bitcast[T]()[],
)
unroll[each, len(VariadicList(Ts))]()
fn __copyinit__(inout self, other: Self):
"""Creates a deep copy of an existing variant.
Args:
other: The variant to copy from.
"""
# Delegate to explicit copy initializer.
self = Self(other=other)
fn __moveinit__(inout self, owned other: Self):
"""Move initializer for the variant.
Args:
other: The variant to move.
"""
self._impl = __mlir_attr[`#kgen.unknown : `, self._mlir_type]
self._get_state()[] = other._get_state()[]
@parameter
fn each[i: Int]():
if self._get_state()[] == i:
alias T = Ts[i]
# Calls the correct __moveinit__
move_pointee(src=other._get_ptr[T](), dst=self._get_ptr[T]())
unroll[each, len(VariadicList(Ts))]()
fn __del__(owned self):
"""Destroy the variant."""
self._call_correct_deleter()
# ===-------------------------------------------------------------------===#
# Operator dunders
# ===-------------------------------------------------------------------===#
fn __getitem__[
T: Echoable
](self: Reference[Self, _, _]) -> ref [self.lifetime] T:
"""Get the value out of the variant as a type-checked type.
This explicitly check that your value is of that type!
If you haven't verified the type correctness at runtime, the program
will abort!
For now this has the limitations that it
- requires the variant value to be mutable
Parameters:
T: The type of the value to get out.
Returns:
The internal data represented as a `Reference[T]`.
"""
if not self[].isa[T]():
abort("get: wrong variant type")
return self[].unsafe_get[T]()[]
# ===-------------------------------------------------------------------===#
# Methods
# ===-------------------------------------------------------------------===#
fn _get_ptr[T: Echoable](self) -> UnsafePointer[T]:
constrained[
Self._check[T]() != Self._sentinel, "not a union element type"
]()
return UnsafePointer.address_of(self._impl).bitcast[T]()
fn _get_state(
self: Reference[Self, _, _]
) -> Reference[Int8, self.is_mutable, self.lifetime]:
var int8_self = UnsafePointer(self).bitcast[Int8]()
return (int8_self + UnionSize[Ts].compute())[]
@always_inline
fn _call_correct_deleter(inout self):
@parameter
fn each[i: Int]():
if self._get_state()[] == i:
alias q = Ts[i]
destroy_pointee(self._get_ptr[q]().address)
unroll[each, len(VariadicList(Ts))]()
@always_inline
fn take[T: Echoable](inout self) -> T:
"""Take the current value of the variant with the provided type.
The caller takes ownership of the underlying value.
This explicitly check that your value is of that type!
If you haven't verified the type correctness at runtime, the program
will abort!
Parameters:
T: The type to take out.
Returns:
The underlying data to be taken out as an owned value.
"""
if not self.isa[T]():
abort("taking the wrong type!")
return self.unsafe_take[T]()
@always_inline
fn unsafe_take[T: Echoable](inout self) -> T:
"""Unsafely take the current value of the variant with the provided type.
The caller takes ownership of the underlying value.
This doesn't explicitly check that your value is of that type!
If you haven't verified the type correctness at runtime, you'll get
a type that _looks_ like your type, but has potentially unsafe
and garbage member data.
Parameters:
T: The type to take out.
Returns:
The underlying data to be taken out as an owned value.
"""
debug_assert(self.isa[T](), "taking wrong type")
# don't call the variant's deleter later
self._get_state()[] = Self._sentinel
return move_from_pointee(self._get_ptr[T]())
@always_inline
fn replace[
Tin: Echoable, Tout: Echoable
](inout self, value: Tin) -> Tout:
"""Replace the current value of the variant with the provided type.
The caller takes ownership of the underlying value.
This explicitly check that your value is of that type!
If you haven't verified the type correctness at runtime, the program
will abort!
Parameters:
Tin: The type to put in.
Tout: The type to take out.
Args:
value: The value to put in.
Returns:
The underlying data to be taken out as an owned value.
"""
if not self.isa[Tout]():
abort("taking out the wrong type!")
return self.unsafe_replace[Tin, Tout](value)
@always_inline
fn unsafe_replace[
Tin: Echoable, Tout: Echoable
](inout self, value: Tin) -> Tout:
"""Unsafely replace the current value of the variant with the provided type.
The caller takes ownership of the underlying value.
This doesn't explicitly check that your value is of that type!
If you haven't verified the type correctness at runtime, you'll get
a type that _looks_ like your type, but has potentially unsafe
and garbage member data.
Parameters:
Tin: The type to put in.
Tout: The type to take out.
Args:
value: The value to put in.
Returns:
The underlying data to be taken out as an owned value.
"""
debug_assert(self.isa[Tout](), "taking out the wrong type!")
var x = self.unsafe_take[Tout]()
self.set[Tin](value)
return x^
fn set[T: Echoable](inout self, owned value: T):
"""Set the variant value.
This will call the destructor on the old value, and update the variant's
internal type and data to the new value.
Parameters:
T: The new variant type. Must be one of the Variant's type arguments.
Args:
value: The new value to set the variant to.
"""
self = Self(value^)
fn isa[T: Echoable](self) -> Bool:
"""Check if the variant contains the required type.
Parameters:
T: The type to check.
Returns:
True if the variant contains the requested type.
"""
alias idx = Self._check[T]()
return self._get_state()[] == idx
fn unsafe_get[
T: Echoable
](self: Reference[Self, _, _]) -> Reference[
T, self.is_mutable, self.lifetime
]:
"""Get the value out of the variant as a type-checked type.
This doesn't explicitly check that your value is of that type!
If you haven't verified the type correctness at runtime, you'll get
a type that _looks_ like your type, but has potentially unsafe
and garbage member data.
For now this has the limitations that it
- requires the variant value to be mutable
Parameters:
T: The type of the value to get out.
Returns:
The internal data represented as a `Reference[T]`.
"""
debug_assert(self[].isa[T](), "get: wrong variant type")
return self[]._get_ptr[T]()[]
@staticmethod
fn _check[T: Echoable]() -> Int8:
return UnionTypeIndex[T, Ts].compute()
fn echo(self) -> String:
var res: String = String("")
var cur = self._get_state()[]
@parameter
fn each[i: Int]():
if cur == i:
alias T = Ts[i]
res = self[T].echo()
unroll[each, len(VariadicList(Ts))]()
return res
@value
struct A(
Echoable,
):
# ===-------------------------------------------------------------------===#
# Life cycle methods
# ===-------------------------------------------------------------------===#
fn __init__(inout self, other: Self):
pass
fn echo(self) -> String:
return "this is A"
@value
struct B(
Echoable
):
# ===-------------------------------------------------------------------===#
# Life cycle methods
# ===-------------------------------------------------------------------===#
fn __init__(inout self, other: Self):
pass
fn echo(self) -> String:
return "this is B"
@value
struct C(
Echoable
):
# ===-------------------------------------------------------------------===#
# Life cycle methods
# ===-------------------------------------------------------------------===#
fn __init__(inout self, other: Self):
pass
fn echo(self) -> String:
return "this is C"
fn test_dyn():
alias AOrBOrC = MyVariant[A, B, C]
var a = A()
var b = B()
var c = C()
var x = AOrBOrC(a)
var y = AOrBOrC(b)
var z = AOrBOrC(c)
print(x.echo())
print(y.echo())
print(z.echo())
fn main() raises:
test_dyn()