|
| 1 | +plan(12); |
| 2 | + |
| 3 | +class Foo { |
| 4 | + has int $!int; |
| 5 | + has str $!str; |
| 6 | + has num $!num; |
| 7 | + method set_int($value) { |
| 8 | + $!int := $value; |
| 9 | + } |
| 10 | + method get_int() { |
| 11 | + $!int; |
| 12 | + } |
| 13 | + method set_num($value) { |
| 14 | + $!num := $value; |
| 15 | + } |
| 16 | + method get_num() { |
| 17 | + $!num; |
| 18 | + } |
| 19 | + method set_str($value) { |
| 20 | + $!str := $value; |
| 21 | + } |
| 22 | + method get_str() { |
| 23 | + $!str; |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +sub make_ref_type($name, $kind, :$ref_kind = 'lexical') { |
| 28 | + my $class := nqp::newtype(NQPMu, 'NativeRef'); |
| 29 | + my $info := nqp::hash(); |
| 30 | + $info<nativeref> := nqp::hash(); |
| 31 | + $info<nativeref><type> := $kind; |
| 32 | + $info<nativeref><refkind> := $ref_kind; |
| 33 | + nqp::composetype($class, $info); |
| 34 | + nqp::setcontspec($class, 'native_ref', nqp::null()); |
| 35 | + return $class; |
| 36 | +} |
| 37 | + |
| 38 | +my %hllconfig; |
| 39 | +%hllconfig<int_attr_ref> := make_ref_type('StubIntAttrRef', int, :ref_kind<attribute>); |
| 40 | +%hllconfig<num_attr_ref> := make_ref_type('StubNumAttrRef', num, :ref_kind<attribute>); |
| 41 | +%hllconfig<str_attr_ref> := make_ref_type('StubStrAttrRef', str, :ref_kind<attribute>); |
| 42 | + |
| 43 | +%hllconfig<int_pos_ref> := make_ref_type('StubIntPosRef', int, :ref_kind<positional>); |
| 44 | +%hllconfig<num_pos_ref> := make_ref_type('StubNumPosRef', num, :ref_kind<positional>); |
| 45 | +%hllconfig<str_pos_ref> := make_ref_type('StubStrPosRef', str, :ref_kind<positional>); |
| 46 | + |
| 47 | +nqp::sethllconfig('nqp', %hllconfig); |
| 48 | + |
| 49 | +my $foo := Foo.new; |
| 50 | +$foo.set_int(100); |
| 51 | +$foo.set_str('hi'); |
| 52 | +$foo.set_num(3.14); |
| 53 | + |
| 54 | +my $int_ref := nqp::getattrref_i($foo, Foo, '$!int'); |
| 55 | +is(nqp::decont_i($int_ref), 100, 'nqp::decont_i on result of nqp::getattrref_i works'); |
| 56 | +nqp::assign_i($int_ref, 200); |
| 57 | +is($foo.get_int, 200, 'nqp::assign_i on result of nqp::getattrref_i works'); |
| 58 | + |
| 59 | +my $num_ref := nqp::getattrref_n($foo, Foo, '$!num'); |
| 60 | +is(nqp::decont_n($num_ref), 3.14, 'nqp::decont_n on result of nqp::getattrref_n works'); |
| 61 | +nqp::assign_n($num_ref, 0.123); |
| 62 | +is($foo.get_num, 0.123, 'nqp::assign_n on result of nqp::getattrref_n works'); |
| 63 | + |
| 64 | +my $str_ref := nqp::getattrref_s($foo, Foo, '$!str'); |
| 65 | +is(nqp::decont_s($str_ref), 'hi', 'nqp::decont_s on result of nqp::getattrref_s works'); |
| 66 | +nqp::assign_s($str_ref, 'hello'); |
| 67 | +is($foo.get_str, 'hello', 'nqp::assign_s on result of nqp::getattrref_s works'); |
| 68 | + |
| 69 | +my $array_i := nqp::list_i(); |
| 70 | +nqp::bindpos_i($array_i, 2, 100); |
| 71 | +my $pos_ref_i := nqp::atposref_i($array_i, 2); |
| 72 | +is(nqp::decont_i($pos_ref_i), 100, 'nqp::decont_i on result of nqp::atposref_i works'); |
| 73 | +nqp::assign_i($pos_ref_i, 200); |
| 74 | +is(nqp::atpos_i($array_i, 2), 200, 'nqp::assign_i on result of nqp::atposref_i works'); |
| 75 | + |
| 76 | +my $array_n := nqp::list_n(); |
| 77 | +nqp::bindpos_n($array_n, 2, 3.14); |
| 78 | +my $pos_ref_n := nqp::atposref_n($array_n, 2); |
| 79 | +is(nqp::decont_n($pos_ref_n), 3.14, 'nqp::decont_n on result of nqp::atposref_n works'); |
| 80 | +nqp::assign_n($pos_ref_n, 1.234); |
| 81 | +is(nqp::atpos_n($array_n, 2), 1.234, 'nqp::assign_n on result of nqp::atposref_n works'); |
| 82 | + |
| 83 | +my $array_s := nqp::list_s(); |
| 84 | +nqp::bindpos_s($array_s, 2, "fancy value"); |
| 85 | +my $pos_ref_s := nqp::atposref_s($array_s, 2); |
| 86 | +is(nqp::decont_s($pos_ref_s), "fancy value", 'nqp::decont_s on result of nqp::atposref_s works'); |
| 87 | +nqp::assign_s($pos_ref_s, "fancier value"); |
| 88 | +is(nqp::atpos_s($array_s, 2), "fancier value", 'nqp::assign_s on result of nqp::atposref_s works'); |
0 commit comments