tsuki 0.4.8

Lua 5.4 ported to Rust
Documentation
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
use super::{Args, Context};
use crate::lapi::lua_typename;
use crate::lauxlib::luaL_argerror;
use crate::ldo::luaD_call;
use crate::value::UnsafeValue;
use crate::vm::{F2Ieq, luaV_tointeger, luaV_tonumber_};
use crate::{
    Float, Fp, LuaFn, NON_YIELDABLE_WAKER, Number, Ref, Str, Table, Thread, Type, UserData, Value,
};
use alloc::boxed::Box;
use alloc::format;
use alloc::string::{String, ToString};
use core::any::{Any, type_name};
use core::convert::identity;
use core::fmt::{Display, Write};
use core::num::NonZero;
use core::pin::pin;
use core::ptr::{null, null_mut};
use core::task::{Poll, Waker};
use thiserror::Error;

/// Argument passed from Lua to Rust function.
///
/// Use [Context::arg()] to get the value of this type.
pub struct Arg<'a, 'b, A> {
    cx: &'a Context<'b, A, Args>,
    index: NonZero<usize>,
}

impl<'a, 'b, A> Arg<'a, 'b, A> {
    #[inline(always)]
    pub(super) fn new(cx: &'a Context<'b, A, Args>, index: NonZero<usize>) -> Self {
        Self { cx, index }
    }

    /// Check if this argument exists.
    ///
    /// You can use [Self::exists()] if you want to return an error if this argument does not
    /// exists.
    #[inline(always)]
    pub fn is_exists(&self) -> bool {
        self.index.get() <= self.cx.payload.0
    }

    /// Check if this argument exists.
    ///
    /// This has the same effect as:
    ///
    /// ```
    /// # use tsuki::context::{Args, Context, Ret, ArgNotFound};
    /// # fn f(cx: Context<(), Args>) -> Result<Context<(), Ret>, Box<dyn core::error::Error>> {
    /// # let arg = cx.arg(1);
    /// if !arg.is_exists() {
    ///     return Err(arg.error(ArgNotFound));
    /// }
    /// # Ok(cx.into())
    /// # }
    /// ```
    ///
    /// Other methods like [`Self::get_str()`] already validate if the argument exists. This method
    /// can be used in case you want to verify if the argument exists but don't need its value.
    ///
    /// This has the same semantic as `luaL_checkany`.
    #[inline(always)]
    pub fn exists(self) -> Result<Self, Box<dyn core::error::Error>> {
        if self.is_exists() {
            Ok(self)
        } else {
            Err(self.error(ArgNotFound))
        }
    }

    /// Returns type of this argument.
    ///
    /// Use [`Self::is_int()`] if you want to check if argument is Lua integer.
    #[inline(always)]
    pub fn ty(&self) -> Option<Type> {
        let v = self.get_raw_or_null();

        if v.is_null() {
            None
        } else {
            Some(Type::from_tt(unsafe { (*v).tt_ }))
        }
    }

    /// Check if this argument is Lua integer.
    ///
    /// This has the same semantic as `lua_isinteger`, except it return [`None`] if the argument
    /// does not exists instead of `false`.
    #[inline(always)]
    pub fn is_int(&self) -> Option<bool> {
        let v = self.get_raw_or_null();

        if v.is_null() {
            None
        } else {
            Some(unsafe { (*v).tt_ == 3 | 0 << 4 })
        }
    }

    /// Gets metatable for this argument.
    ///
    /// Returns [None] if this argument does not exists.
    pub fn metatable(&self) -> Option<Option<Ref<'b, Table<A>>>> {
        // Get argument.
        let v = self.get_raw_or_null();

        if v.is_null() {
            return None;
        }

        // Get metatable.
        let g = self.cx.th.hdr.global();
        let mt = unsafe { g.metatable(v) };
        let mt = match mt.is_null() {
            true => None,
            false => Some(unsafe { Ref::new(mt) }),
        };

        Some(mt)
    }

    /// Checks if this argument is an integer and return it.
    ///
    /// This method will accept a string or float if `convert` is `true`. In this case this method
    /// has the same semantic as `lua_tointegerx`.
    #[inline(always)]
    pub fn as_int(&self, convert: bool) -> Option<i64> {
        let v = self.get_raw_or_null();

        if v.is_null() {
            None
        } else if unsafe { (*v).tt_ == 3 | 0 << 4 } {
            Some(unsafe { (*v).value_.i })
        } else if convert {
            unsafe { luaV_tointeger(v, F2Ieq) }
        } else {
            None
        }
    }

    /// Checks if this argument is a number and return it.
    ///
    /// This method will return [None] if this argument does not exists or not a number.
    #[inline(always)]
    pub fn as_num(&self) -> Option<Number> {
        let v = self.get_raw_or_null();

        if v.is_null() {
            None
        } else if unsafe { (*v).tt_ == 3 | 0 << 4 } {
            Some(Number::Int(unsafe { (*v).value_.i }))
        } else if unsafe { (*v).tt_ == 3 | 1 << 4 } {
            Some(Number::Float(unsafe { (*v).value_.n }))
        } else {
            None
        }
    }

    /// Checks if this argument is a string and return it.
    ///
    /// This method will accept a number if `convert` is `true`. In this case the argument will be
    /// converted to a string **in-place**.
    #[inline(always)]
    pub fn as_str(&self, convert: bool) -> Option<&'a Str<A>> {
        let v = self.get_raw_or_null();

        if v.is_null() {
            return None;
        }

        match unsafe { (*v).tt_ & 0xf } {
            3 if convert => Some(unsafe { &*self.convert_str(v) }),
            4 => Some(unsafe { &*(*v).value_.gc.cast() }),
            _ => None,
        }
    }

    /// Checks if this argument is a table and return it.
    ///
    /// This method will return [None] if this argument does not exists or not a table.
    #[inline(always)]
    pub fn as_table(&self) -> Option<&'a Table<A>> {
        let v = self.get_raw_or_null();

        if v.is_null() {
            None
        } else if unsafe { (*v).tt_ & 0xf == 5 } {
            Some(unsafe { &*(*v).value_.gc.cast() })
        } else {
            None
        }
    }

    /// Checks if this argument is a Rust function and return it.
    ///
    /// This method will return [None] if this argument does not exists or not a Rust function.
    #[inline(always)]
    pub fn as_fp(&self) -> Option<Fp<A>> {
        let v = self.get_raw_or_null();

        if v.is_null() {
            None
        } else if unsafe { (*v).tt_ & 0x3f == 0x02 } {
            Some(Fp(unsafe { (*v).value_.f }))
        } else {
            None
        }
    }

    /// Checks if this argument is a Lua function and return it.
    ///
    /// This method will return [None] if this argument does not exists or not a Lua function.
    #[inline(always)]
    pub fn as_lua_fn(&self) -> Option<&'a LuaFn<A>> {
        let v = self.get_raw_or_null();

        if v.is_null() {
            None
        } else if unsafe { (*v).tt_ & 0x3f == 0x06 } {
            Some(unsafe { &*(*v).value_.gc.cast() })
        } else {
            None
        }
    }

    /// Get address of argument value (if any).
    ///
    /// This has the same semantic as `lua_topointer`.
    #[inline(always)]
    pub fn as_ptr(&self) -> *const u8 {
        let v = self.get_raw_or_null();

        if v.is_null() {
            return null();
        }

        match unsafe { (*v).tt_ & 0x3f } {
            0x02 => unsafe { (*v).value_.f as *const u8 },
            0x12 => unsafe { (*v).value_.y as *const u8 },
            0x22 => unsafe { (*v).value_.a as *const u8 },
            0x32 => todo!(),
            7 => unsafe { (*(*v).value_.gc.cast::<UserData<A, ()>>()).ptr.cast() },
            _ => unsafe {
                if (*v).tt_ & 1 << 6 != 0 {
                    (*v).value_.gc.cast()
                } else {
                    null()
                }
            },
        }
    }

    /// Returns the value of this argument.
    ///
    /// This method is expensive compared to a specialized method like [`Self::get_str()`]. Use this
    /// method only when you need [`Value`]. If you want to check type of this argument use
    /// [`Self::ty()`] instead since it much faster.
    pub fn get(&self) -> Option<Value<'b, A>> {
        let v = self.get_raw_or_null();

        if v.is_null() {
            None
        } else {
            Some(unsafe { Value::from_unsafe(v) })
        }
    }

    /// Checks if this argument is a string and return it.
    ///
    /// This method **does not** convert a number to string. Use [Self::to_str()] if you want that
    /// behavior.
    #[inline(always)]
    pub fn get_str(&self) -> Result<&'a Str<A>, Box<dyn core::error::Error>> {
        let expect = Type::String;
        let v = self.get_raw(expect)?;

        match unsafe { (*v).tt_ & 0xf } {
            4 => Ok(unsafe { &*(*v).value_.gc.cast() }),
            _ => Err(unsafe { self.type_error(expect, v) }),
        }
    }

    /// Checks if this argument is a string and return it.
    ///
    /// This method returns [None] in the following cases:
    ///
    /// - This argument is `nil`.
    /// - This argument does not exists and `required` is `false`.
    ///
    /// This method **does not** convert a number to string. Use [Self::to_nilable_str()] if you
    /// want that behavior.
    #[inline(always)]
    pub fn get_nilable_str(
        &self,
        required: bool,
    ) -> Result<Option<&'a Str<A>>, Box<dyn core::error::Error>> {
        // Get argument.
        let expect = "nil or string";
        let v = self.get_raw_or_null();

        if v.is_null() {
            match required {
                true => return Err(unsafe { self.type_error(expect, v) }),
                false => return Ok(None),
            }
        }

        // Check type.
        match unsafe { (*v).tt_ & 0xf } {
            0 => Ok(None),
            4 => Ok(Some(unsafe { &*(*v).value_.gc.cast() })),
            _ => Err(unsafe { self.type_error(expect, v) }),
        }
    }

    /// Checks if this argument is a table and return it.
    #[inline(always)]
    pub fn get_table(&self) -> Result<&'a Table<A>, Box<dyn core::error::Error>> {
        let expect = lua_typename(5);
        let v = self.get_raw(expect)?;

        match unsafe { (*v).tt_ & 0xf } {
            5 => Ok(unsafe { &*(*v).value_.gc.cast() }),
            _ => Err(unsafe { self.type_error(expect, v) }),
        }
    }

    /// Checks if this argument is a table and return it.
    ///
    /// This method returns [`None`] in the following cases:
    ///
    /// - This argument is `nil`.
    /// - This argument does not exists and `required` is `false`.
    #[inline(always)]
    pub fn get_nilable_table(
        &self,
        required: bool,
    ) -> Result<Option<&'a Table<A>>, Box<dyn core::error::Error>> {
        // Check if argument exists.
        let expect = "nil or table";
        let v = self.get_raw_or_null();

        if v.is_null() {
            match required {
                true => return Err(unsafe { self.type_error(expect, v) }),
                false => return Ok(None),
            }
        }

        // Check type.
        match unsafe { (*v).tt_ & 0xf } {
            0 => Ok(None),
            5 => Ok(Some(unsafe { &*(*v).value_.gc.cast() })),
            _ => Err(unsafe { self.type_error(expect, v) }),
        }
    }

    /// Checks if this argument is a userdata `T` and return it.
    #[inline(always)]
    pub fn get_ud<T: Any>(&self) -> Result<&'a UserData<A, T>, Box<dyn core::error::Error>> {
        let expect = type_name::<T>();
        let v = self.get_raw(expect)?;
        let ud = match unsafe { (*v).tt_ & 0xf } {
            7 => unsafe { (*v).value_.gc.cast::<UserData<A, dyn Any>>() },
            _ => return Err(unsafe { self.type_error(expect, v) }),
        };

        match unsafe { (*ud).downcast() } {
            Some(v) => Ok(v),
            None => Err(unsafe { self.type_error(expect, v) }),
        }
    }

    /// Checks if this argument is a userdata `T` and return it.
    ///
    /// This method returns [`None`] in the following cases:
    ///
    /// - This argument is `nil`.
    /// - This argument does not exists and `required` is `false`.
    #[inline(always)]
    pub fn get_nilable_ud<T: Any>(
        &self,
        required: bool,
    ) -> Result<Option<&'a UserData<A, T>>, Box<dyn core::error::Error>> {
        // Check if argument exists.
        let name = type_name::<T>();
        let expect = format_args!("nil or {name}");
        let v = self.get_raw_or_null();

        if v.is_null() {
            match required {
                true => return Err(unsafe { self.type_error(expect, v) }),
                false => return Ok(None),
            }
        }

        // Check type.
        let ud = match unsafe { (*v).tt_ & 0xf } {
            0 => return Ok(None),
            7 => unsafe { (*v).value_.gc.cast::<UserData<A, dyn Any>>() },
            _ => return Err(unsafe { self.type_error(expect, v) }),
        };

        match unsafe { (*ud).downcast() } {
            Some(v) => Ok(Some(v)),
            None => Err(unsafe { self.type_error(expect, v) }),
        }
    }

    /// Checks if this argument is a thread and return it.
    #[inline(always)]
    pub fn get_thread(&self) -> Result<&'a Thread<A>, Box<dyn core::error::Error>> {
        let expect = lua_typename(8);
        let v = self.get_raw(expect)?;

        match unsafe { (*v).tt_ & 0xf } {
            8 => Ok(unsafe { &*(*v).value_.gc.cast() }),
            _ => Err(unsafe { self.type_error(expect, v) }),
        }
    }

    /// Gets the argument and convert it to Lua boolean.
    ///
    /// This method has the same mechanism as Lua conditional check, which mean it only returns
    /// `false` in the following cases:
    ///
    /// - This argument has `false` value.
    /// - This argument is `nil`.
    ///
    /// All other values will cause this method to return `true`, including **zero**.
    ///
    /// This has the same semantic as `lua_toboolean`, except it return [`None`] if the argument
    /// does not exists instead of `false`.
    #[inline(always)]
    pub fn to_bool(&self) -> Option<bool> {
        let raw = self.get_raw_or_null();

        if raw.is_null() {
            None
        } else if unsafe { (*raw).tt_ == 1 | 0 << 4 || (*raw).tt_ & 0xf == 0 } {
            Some(false)
        } else {
            Some(true)
        }
    }

    /// Gets the argument and convert it to Lua integer.
    ///
    /// This has the same semantic as `luaL_checkinteger`.
    #[inline(always)]
    pub fn to_int(&self) -> Result<i64, Box<dyn core::error::Error>> {
        let raw = self.get_raw(lua_typename(3))?;

        if unsafe { (*raw).tt_ == 3 | 0 << 4 } {
            Ok(unsafe { (*raw).value_.i })
        } else {
            unsafe { self.convert_int(raw) }
        }
    }

    #[inline(never)]
    unsafe fn convert_int(
        &self,
        raw: *const UnsafeValue<A>,
    ) -> Result<i64, Box<dyn core::error::Error>> {
        if let Some(val) = unsafe { luaV_tointeger(raw, F2Ieq) } {
            Ok(val)
        } else if unsafe { (*raw).tt_ == 3 | 1 << 4 } {
            Err(self.error("number has no integer representation"))
        } else {
            Err(unsafe { self.type_error(lua_typename(3), raw) })
        }
    }

    /// Gets the argument and convert it to Lua integer.
    ///
    /// This method returns [`None`] in the following cases:
    ///
    /// - This argument is `nil`.
    /// - This argument does not exists and `required` is `false`.
    ///
    /// This has the same semantic as `luaL_optinteger`.
    pub fn to_nilable_int(
        &self,
        required: bool,
    ) -> Result<Option<i64>, Box<dyn core::error::Error>> {
        // Check type.
        let expect = "nil or number";
        let raw = self.get_raw_or_null();

        if raw.is_null() {
            match required {
                true => return Err(unsafe { self.type_error(expect, raw) }),
                false => return Ok(None),
            }
        } else if unsafe { (*raw).tt_ & 0xf == 0 } {
            return Ok(None);
        } else if unsafe { (*raw).tt_ == 3 | 0 << 4 } {
            return Ok(Some(unsafe { (*raw).value_.i }));
        };

        // Convert to integer.
        if let Some(val) = unsafe { luaV_tointeger(raw, F2Ieq) } {
            Ok(Some(val))
        } else if unsafe { (*raw).tt_ == 3 | 1 << 4 } {
            Err(self.error("number has no integer representation"))
        } else {
            Err(unsafe { self.type_error(expect, raw) })
        }
    }

    /// Gets the argument and convert it to float.
    ///
    /// This has the same semantic as `luaL_checknumber`.
    #[inline(always)]
    pub fn to_float(&self) -> Result<Float, Box<dyn core::error::Error>> {
        // Check if number.
        let expect = lua_typename(3);
        let raw = self.get_raw(expect)?;

        if unsafe { (*raw).tt_ == 3 | 1 << 4 } {
            return Ok(unsafe { (*raw).value_.n });
        }

        // Convert to number.
        if let Some(val) = unsafe { luaV_tonumber_(raw) } {
            Ok(val)
        } else {
            Err(unsafe { self.type_error(expect, raw) })
        }
    }

    /// Gets the argument and convert it to float.
    ///
    /// This method returns [`None`] in the following cases:
    ///
    /// - This argument is `nil`.
    /// - This argument does not exists and `required` is `false`.
    ///
    /// This has the same semantic as `luaL_optnumber`.
    #[inline(always)]
    pub fn to_nilable_float(
        &self,
        required: bool,
    ) -> Result<Option<Float>, Box<dyn core::error::Error>> {
        // Check type.
        let expect = "nil or number";
        let raw = self.get_raw_or_null();

        if raw.is_null() {
            match required {
                true => return Err(unsafe { self.type_error(expect, raw) }),
                false => return Ok(None),
            }
        } else if unsafe { (*raw).tt_ & 0xf == 0 } {
            return Ok(None);
        } else if unsafe { (*raw).tt_ == 3 | 1 << 4 } {
            return Ok(Some(unsafe { (*raw).value_.n }));
        };

        // Convert to number.
        if let Some(val) = unsafe { luaV_tonumber_(raw) } {
            Ok(Some(val))
        } else {
            Err(unsafe { self.type_error(expect, raw) })
        }
    }

    /// Checks if this argument is a string or number and return it as string.
    ///
    /// This has the same semantic as `luaL_checklstring`, which mean it will convert the argument
    /// **in-place** if it is a number.
    ///
    /// You can use [Self::to_utf8()] if you want Rust [str] instead of Lua [Str].
    ///
    /// This method will trigger GC if new string is allocated.
    #[inline(always)]
    pub fn to_str(&self) -> Result<&'a Str<A>, Box<dyn core::error::Error>> {
        let expect = Type::String;
        let v = self.get_raw(expect)?;

        match unsafe { (*v).tt_ & 0xf } {
            3 => Ok(unsafe { &*self.convert_str(v) }),
            4 => Ok(unsafe { &*(*v).value_.gc.cast() }),
            _ => Err(unsafe { self.type_error(expect, v) }),
        }
    }

    /// Checks if this argument is a string or number and return it as string.
    ///
    /// This method returns [None] in the following cases:
    ///
    /// - This argument is `nil`.
    /// - This argument does not exists and `required` is `false`.
    ///
    /// This has the same semantic as `luaL_checklstring`, which mean it will convert the argument
    /// **in-place** if it is a number.
    ///
    /// You can use [Self::to_nilable_utf8()] if you want Rust [str] instead of Lua [Str].
    ///
    /// This method will trigger GC if new string is allocated.
    #[inline(always)]
    pub fn to_nilable_str(
        &self,
        required: bool,
    ) -> Result<Option<&'a Str<A>>, Box<dyn core::error::Error>> {
        // Get argument.
        let expect = "nil or string";
        let v = self.get_raw_or_null();

        if v.is_null() {
            match required {
                true => return Err(unsafe { self.type_error(expect, v) }),
                false => return Ok(None),
            }
        }

        // Check type.
        match unsafe { (*v).tt_ & 0xf } {
            0 => Ok(None),
            3 => Ok(Some(unsafe { &*self.convert_str(v) })),
            4 => Ok(Some(unsafe { &*(*v).value_.gc.cast() })),
            _ => Err(unsafe { self.type_error(expect, v) }),
        }
    }

    /// # Safety
    /// `v` must be a value on Lua stack.
    #[inline(never)]
    unsafe fn convert_str(&self, v: *mut UnsafeValue<A>) -> *const Str<A> {
        // Convert to string.
        let s = if unsafe { (*v).tt_ & 0x3f == 0x03 } {
            unsafe { (*v).value_.i.to_string() }
        } else {
            unsafe { (*v).value_.n.to_string() }
        };

        // Create string.
        let g = self.cx.th.hdr.global();
        let s = unsafe { Str::from_str(g, s) };
        let r = s.unwrap_or_else(identity);

        unsafe { (*v).tt_ = (*r).hdr.tt | 1 << 6 };
        unsafe { (*v).value_.gc = r.cast() };

        if s.is_ok() {
            g.gc.step();
        }

        r
    }

    /// Checks if this argument is a UTF-8 string or number and return it as string.
    ///
    /// This has the same semantic as `luaL_checklstring`, which mean it will convert the argument
    /// **in-place** if it is a number.
    ///
    /// This method will trigger GC if new string is allocated.
    #[inline(always)]
    pub fn to_utf8(&self) -> Result<&'a str, Box<dyn core::error::Error>> {
        self.to_str()?
            .as_utf8()
            .ok_or_else(|| self.error("expect UTF-8 string"))
    }

    /// Checks if this argument is a UTF-8 string or number and return it as string.
    ///
    /// This method returns [None] in the following cases:
    ///
    /// - This argument is `nil`.
    /// - This argument does not exists and `required` is `false`.
    ///
    /// This has the same semantic as `luaL_checklstring`, which mean it will convert the argument
    /// **in-place** if it is a number.
    ///
    /// This method will trigger GC if new string is allocated.
    #[inline(always)]
    pub fn to_nilable_utf8(
        &self,
        required: bool,
    ) -> Result<Option<&'a str>, Box<dyn core::error::Error>> {
        self.to_nilable_str(required)?
            .map(|s| s.as_utf8().ok_or_else(|| self.error("expect UTF-8 string")))
            .transpose()
    }

    /// Gets the argument and convert it to Lua string suitable for display.
    ///
    /// This method does not modify the argument and treat non-existent argument as `nil` the same
    /// as `luaL_tolstring`. Note that this method requires `__tostring` metamethod to return a
    /// UTF-8 string. It also required `__name` metavalue to be UTF-8 string.
    ///
    /// The returned [`Str`] guarantee to be a UTF-8 string. If this argument is a string but it is
    /// not UTF-8 this method will return a new [`Str`] with content `string: CONTENT_IN_LOWER_HEX`
    /// instead.
    pub fn display(&self) -> Result<Ref<'b, Str<A>>, Box<dyn core::error::Error>> {
        // Try __tostring metamethod.
        let t = self.cx.th;
        let g = t.hdr.global();
        let arg = self.get_raw_or_null();
        let mt = match arg.is_null() {
            true => null(),
            false => unsafe { g.metatable(arg) },
        };

        if !mt.is_null() {
            let v = unsafe { (*mt).get_raw_str_key("__tostring") };

            if unsafe { (*v).tt_ & 0xf != 0 } {
                // Assume extra stack.
                unsafe { t.top.write(*v) };
                unsafe { t.top.add(1) };
                unsafe { t.top.write(*arg) };
                unsafe { t.top.add(1) };

                // Invoke.
                {
                    let f = unsafe { t.top.get().sub(2) };
                    let f = pin!(unsafe { luaD_call(t, f, 1) });
                    let w = unsafe { Waker::new(null(), &NON_YIELDABLE_WAKER) };

                    match f.poll(&mut core::task::Context::from_waker(&w)) {
                        Poll::Ready(Ok(_)) => (),
                        Poll::Ready(Err(e)) => return Err(e), // Requires unsized coercion.
                        Poll::Pending => unreachable!(),
                    }
                }

                unsafe { t.top.sub(1) };

                // Get result.
                let r = unsafe { t.top.read(0) };

                match r.tt_ & 0xf {
                    3 => unsafe {
                        // Convert to string.
                        let s = if r.tt_ & 0x3f == 0x03 {
                            r.value_.i.to_string()
                        } else {
                            r.value_.n.to_string()
                        };

                        // Create string.
                        let s = Str::from_str(g, s);
                        let r = Ref::new(s.unwrap_or_else(identity));

                        if s.is_ok() {
                            g.gc.step();
                        }

                        return Ok(r);
                    },
                    4 => unsafe {
                        let r = r.value_.gc.cast::<Str<A>>();

                        if !(*r).is_utf8() {
                            return Err(self.error("'__tostring' must return a UTF-8 string"));
                        }

                        return Ok(Ref::new(r));
                    },
                    _ => return Err("'__tostring' must return a string".into()),
                }
            }
        }

        // Get type.
        let ty = match arg.is_null() {
            true => None,
            false => Some(unsafe { (*arg).tt_ & 0xf }),
        };

        // Check type.
        let v = match ty {
            Some(0) => unsafe { Str::from_str(g, "nil").unwrap_or_else(identity) },
            Some(1) => match unsafe { ((*arg).tt_ >> 4) & 3 } {
                0 => unsafe { Str::from_str(g, "false").unwrap_or_else(identity) },
                _ => unsafe { Str::from_str(g, "true").unwrap_or_else(identity) },
            },
            Some(3) => match unsafe { ((*arg).tt_ >> 4) & 3 } {
                0 => unsafe {
                    Str::from_str(g, (*arg).value_.i.to_string()).unwrap_or_else(identity)
                },
                1 => unsafe {
                    let v = (*arg).value_.n;

                    Str::from_str(g, v.to_string()).unwrap_or_else(identity)
                },
                _ => unreachable!(),
            },
            Some(4) if unsafe { (*(*arg).value_.gc.cast::<Str<A>>()).is_utf8() } => unsafe {
                (*arg).value_.gc.cast::<Str<A>>()
            },
            Some(v) => unsafe {
                // Get __name from metatable.
                let kind = match (mt.is_null() == false)
                    .then(move || (*mt).get_raw_str_key("__name"))
                    .filter(|&v| (*v).tt_ & 0xf == 4)
                    .map(|v| (*v).value_.gc.cast::<Str<A>>())
                {
                    Some(v) => (*v)
                        .as_utf8()
                        .ok_or_else(|| self.error("'__name' must be UTF-8 string"))?,
                    None => lua_typename(v.into()),
                };

                // Build value.
                let mut buf = String::with_capacity(kind.len() + 2 + 18);

                write!(buf, "{kind}: ").unwrap();

                match v {
                    0x02 => write!(buf, "{:p}", (*arg).value_.f).unwrap(),
                    0x12 => write!(buf, "{:p}", (*arg).value_.y).unwrap(),
                    0x22 => write!(buf, "{:p}", (*arg).value_.a).unwrap(),
                    0x32 => todo!(),
                    4 => {
                        let v = (*arg).value_.gc.cast::<Str<A>>();
                        let v = (*v).as_bytes();

                        buf.reserve(v.len().saturating_mul(2).saturating_sub(18));

                        for b in v {
                            write!(buf, "{b:x}").unwrap();
                        }
                    }
                    5 | 6 | 8 => write!(buf, "{:p}", (*arg).value_.gc).unwrap(),
                    7 => write!(
                        buf,
                        "{:p}",
                        (*(*arg).value_.gc.cast::<UserData<A, ()>>()).ptr
                    )
                    .unwrap(),
                    _ => unreachable!(),
                }

                Str::from_str(g, buf).unwrap_or_else(identity)
            },
            None => unsafe {
                Str::from_str(g, format!("{}: {:p}", lua_typename(-1), null::<()>()))
                    .unwrap_or_else(identity)
            },
        };

        Ok(unsafe { Ref::new(v) })
    }

    /// Create invalid type error for this argument.
    #[inline(always)]
    pub fn invalid_type(&self, expect: impl Display) -> Box<dyn core::error::Error> {
        let v = self.get_raw_or_null();

        unsafe { self.type_error(expect, v) }
    }

    /// Create an error for this argument.
    ///
    /// `reason` will become the value of [`core::error::Error::source()`] on the returned error.
    /// The [`core::fmt::Display`] that implemented on the returned error does not include `reason`.
    ///
    /// Use [`ArgNotFound`] if this argument is required but does not exists.
    #[inline(always)]
    pub fn error(
        &self,
        reason: impl Into<Box<dyn core::error::Error>>,
    ) -> Box<dyn core::error::Error> {
        unsafe { luaL_argerror(self.cx.th, self.index, reason) }
    }

    #[inline(always)]
    fn get_raw(
        &self,
        expect: impl Display,
    ) -> Result<*mut UnsafeValue<A>, Box<dyn core::error::Error>> {
        let th = self.cx.th;
        let ci = th.ci.get();

        if self.index.get() > self.cx.payload.0 {
            Err(unsafe { self.type_error(expect, null()) })
        } else {
            Ok(unsafe { th.stack.get().add((*ci).func + self.index.get()).cast() })
        }
    }

    #[inline(always)]
    pub(crate) fn get_raw_or_null(&self) -> *mut UnsafeValue<A> {
        let th = self.cx.th;
        let ci = th.ci.get();

        if self.index.get() > self.cx.payload.0 {
            null_mut()
        } else {
            unsafe { th.stack.get().add((*ci).func + self.index.get()).cast() }
        }
    }

    #[inline(never)]
    unsafe fn type_error(
        &self,
        expect: impl Display,
        actual: *const UnsafeValue<A>,
    ) -> Box<dyn core::error::Error> {
        // Check if no value.
        if actual.is_null() {
            return self.error(format!("{} expected, got {}", expect, lua_typename(-1)));
        }

        // Get type name.
        let actual = self.cx.type_name(unsafe { actual.read() });

        self.error(format!("{} expected, got {}", expect, actual))
    }
}

/// Represents an error when [`Arg`] does not exists.
#[derive(Debug, Error)]
#[error("value expected")]
pub struct ArgNotFound;