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
|
plan(1518);
{
my $sc := nqp::createsc('exampleHandle');
is(nqp::istrue($sc), 1, 'sc works with istrue');
ok(nqp::scsetdesc($sc, "foobar") eq 'foobar', 'scsetdesc has correct return value');
ok(nqp::scgetdesc($sc) eq 'foobar', 'scgetdesc');
ok(nqp::scgethandle($sc) eq 'exampleHandle', 'scgethandle');
my $sc2 := nqp::createsc('exampleHandle2');
ok(nqp::scgethandle(nqp::pushcompsc($sc)) eq 'exampleHandle', 'pushcompsc return correct value');
ok(nqp::scgethandle(nqp::pushcompsc($sc2)) eq 'exampleHandle2', 'pushcompsc return correct value');
ok(nqp::scgethandle(nqp::popcompsc()) eq 'exampleHandle2', 'popping the correct sc');
ok(nqp::scgethandle(nqp::popcompsc()) eq 'exampleHandle', 'again popping the correct sc');
}
sub add_to_sc($sc, $idx, $obj) {
nqp::scsetobj($sc, $idx, $obj);
nqp::setobjsc($obj, $sc);
}
my $in_sc := -1;
sub fresh_in_sc() {
$in_sc := $in_sc + 1;
'TEST_SC_' ~ $in_sc ~ '_IN';
}
my $out_sc := -1;
sub fresh_out_sc() {
$out_sc := $out_sc + 1;
'TEST_SC_' ~ $out_sc ~ '_OUT';
}
# Test nqp::getobjsc/nqp::scgetobjidx
{
say(fresh_in_sc());
my $sc := nqp::createsc(fresh_in_sc);
class T0 is repr('P6int') { }
my $v1 := nqp::box_i(42, T0);
my $v2 := nqp::box_i(43, T0);
ok(nqp::isnull(nqp::getobjsc($v1)), 'nqp::getobjsc on an object without sc');
add_to_sc($sc, 0, $v1);
add_to_sc($sc, 1, $v2);
ok(nqp::eqaddr(nqp::getobjsc($v1), $sc), 'nqp::getobjsc');
ok(nqp::scgetobjidx($sc, $v2) == 1, 'nqp::scgetobjidx');
}
# Serializing an empty SC.
{
my $sc := nqp::createsc(fresh_in_sc());
my $sh := nqp::list_s();
my $serialized := nqp::serialize($sc, $sh);
ok(nqp::chars($serialized) > 0, 'serialized empty SC to non-empty string');
ok(nqp::chars($serialized) >= 36, 'output is at least as long as the header');
my $dsc := nqp::createsc(fresh_out_sc());
nqp::deserialize($serialized, $dsc, $sh, nqp::list(), nqp::null());
ok(nqp::scobjcount($dsc) == 0, 'deserialized SC is also empty');
}
# Serializing an SC with a single object with P6int REPR.
{
my $sc := nqp::createsc(fresh_in_sc());
my $sh := nqp::list_s();
class T1 is repr('P6int') { }
my $v1 := nqp::box_i(42, T1);
add_to_sc($sc, 0, $v1);
my $serialized := nqp::serialize($sc, $sh);
ok(nqp::chars($serialized) > 36, 'serialized SC with P6int output longer than a header');
my $dsc := nqp::createsc(fresh_out_sc());
nqp::deserialize($serialized, $dsc, $sh, nqp::list(), nqp::null());
ok(nqp::scobjcount($dsc) == 1, 'deserialized SC has a single object');
ok(nqp::istype(nqp::scgetobj($dsc, 0), T1), 'deserialized object has correct type');
ok(nqp::unbox_i(nqp::scgetobj($dsc, 0)) == 42, 'deserialized object has correct value');
}
# Serializing an SC with a single object with P6num REPR.
{
my $sc := nqp::createsc(fresh_in_sc());
my $sh := nqp::list_s();
class T2 is repr('P6num') { }
my $v := nqp::box_n(6.9, T2);
add_to_sc($sc, 0, $v);
my $serialized := nqp::serialize($sc, $sh);
ok(nqp::chars($serialized) > 36, 'serialized SC with P6num output longer than a header');
my $dsc := nqp::createsc(fresh_out_sc());
nqp::deserialize($serialized, $dsc, $sh, nqp::list(), nqp::null());
ok(nqp::scobjcount($dsc) == 1, 'deserialized SC has a single object');
ok(nqp::istype(nqp::scgetobj($dsc, 0), T2), 'deserialized object has correct type');
ok(nqp::iseq_n(nqp::unbox_n(nqp::scgetobj($dsc, 0)), 6.9), 'deserialized object has correct value');
}
# Serializing an SC with a single object with P6str REPR.
{
my $sc := nqp::createsc(fresh_in_sc());
my $sh := nqp::list_s();
class T3 is repr('P6str') { }
my $v := nqp::box_s('dugong', T3);
add_to_sc($sc, 0, $v);
my $serialized := nqp::serialize($sc, $sh);
ok(nqp::chars($serialized) > 36, 'serialized SC with P6str output longer than a header');
my $dsc := nqp::createsc(fresh_out_sc());
nqp::deserialize($serialized, $dsc, $sh, nqp::list(), nqp::null());
ok(nqp::scobjcount($dsc) == 1, 'deserialized SC has a single object');
ok(nqp::istype(nqp::scgetobj($dsc, 0), T3), 'deserialized object has correct type');
ok(nqp::unbox_s(nqp::scgetobj($dsc, 0)) eq 'dugong', 'deserialized object has correct value');
}
# Serializing an SC with a P6opaque containing a P6int, P6num and P6str.
{
my $sc := nqp::createsc(fresh_in_sc());
my $sh := nqp::list_s();
class T4 {
has int $!a;
has num $!b;
has str $!c;
method new() {
my $obj := nqp::create(self);
$obj.BUILD();
$obj;
}
method BUILD() {
$!a := 42;
$!b := 6.9;
$!c := 'llama';
}
method a() { $!a }
method b() { $!b }
method c() { $!c }
}
my $v := T4.new();
add_to_sc($sc, 0, $v);
my $serialized := nqp::serialize($sc, $sh);
ok(nqp::chars($serialized) > 36, 'serialized SC with P6opaque output longer than a header');
my $dsc := nqp::createsc(fresh_out_sc());
nqp::deserialize($serialized, $dsc, $sh, nqp::list(), nqp::null());
ok(nqp::scobjcount($dsc) == 1, 'deserialized SC has a single object');
ok(nqp::istype(nqp::scgetobj($dsc, 0), T4), 'deserialized object has correct type');
ok(nqp::scgetobj($dsc, 0).a == 42, 'P6int attribute has correct value');
ok(nqp::scgetobj($dsc, 0).b == 6.9, 'P6num attribute has correct value');
ok(nqp::scgetobj($dsc, 0).c eq 'llama', 'P6str attribute has correct value');
}
# Serializing an SC with P6opaues and circular references
{
my $sc := nqp::createsc(fresh_in_sc());
my $sh := nqp::list_s();
class T5 {
has $!x;
method set_x($x) {
$!x := $x;
}
method x() { $!x }
}
my $v1 := T5.new();
my $v2 := T5.new();
$v1.set_x($v2);
$v2.set_x($v1);
add_to_sc($sc, 0, $v1);
add_to_sc($sc, 1, $v2);
my $serialized := nqp::serialize($sc, $sh);
ok(nqp::chars($serialized) > 36, 'serialized SC with P6opaque output longer than a header');
my $dsc := nqp::createsc(fresh_out_sc());
nqp::deserialize($serialized, $dsc, $sh, nqp::list(), nqp::null());
ok(nqp::scobjcount($dsc) == 2, 'deserialized SC has 2 objects');
ok(nqp::istype(nqp::scgetobj($dsc, 0), T5), 'first deserialized object has correct type');
ok(nqp::istype(nqp::scgetobj($dsc, 1), T5), 'second deserialized object has correct type');
ok(nqp::scgetobj($dsc, 0).x =:= nqp::scgetobj($dsc, 1), 'reference from first object to second ok');
ok(nqp::scgetobj($dsc, 1).x =:= nqp::scgetobj($dsc, 0), 'reference from second object to first ok');
}
# Tracing an object graph.
{
my $sc := nqp::createsc(fresh_in_sc());
my $sh := nqp::list_s();
class T6 {
has $!x;
has int $!v;
method set_xv($x, $v) {
$!x := $x;
$!v := $v;
}
method set_v($v) {
$!v := $v;
}
method x() { $!x }
method v() { $!v }
}
my $v1 := T6.new();
my $v2 := T6.new();
my $v3 := T6.new();
$v1.set_xv($v2, 5);
$v2.set_xv($v3, 8);
$v3.set_v(40);
# Here, we only add *one* of the three explicitly to the SC.
add_to_sc($sc, 0, $v1);
my $serialized := nqp::serialize($sc, $sh);
my $dsc := nqp::createsc(fresh_out_sc());
nqp::deserialize($serialized, $dsc, $sh, nqp::list(), nqp::null());
ok(nqp::scobjcount($dsc) == 3, 'deserialized SC has 3 objects - the one we added and two discovered');
ok(nqp::istype(nqp::scgetobj($dsc, 0), T6), 'first deserialized object has correct type');
ok(nqp::istype(nqp::scgetobj($dsc, 1), T6), 'second deserialized object has correct type');
ok(nqp::istype(nqp::scgetobj($dsc, 2), T6), 'third deserialized object has correct type');
ok(nqp::scgetobj($dsc, 0).x =:= nqp::scgetobj($dsc, 1), 'reference from first object to second ok');
ok(nqp::scgetobj($dsc, 0).v == 5, 'first object value attribute is ok');
ok(nqp::scgetobj($dsc, 1).x =:= nqp::scgetobj($dsc, 2), 'reference from second object to third ok');
ok(nqp::scgetobj($dsc, 1).v == 8, 'second object value attribute is ok');
ok(nqp::scgetobj($dsc, 2).v == 40, 'third object value attribute is ok');
}
# Serializing an SC with a P6opaque containing VM Integer/Float/String
{
my $sc := nqp::createsc(fresh_in_sc());
my $sh := nqp::list_s();
class T7 {
has $!a;
has $!b;
has $!c;
method new() {
my $obj := nqp::create(self);
$obj.BUILD();
$obj;
}
method BUILD() {
$!a := 42;
$!b := 6.9;
$!c := 'llama';
}
method a() { $!a }
method b() { $!b }
method c() { $!c }
}
my $v := T7.new();
add_to_sc($sc, 0, $v);
my $serialized := nqp::serialize($sc, $sh);
my $dsc := nqp::createsc(fresh_out_sc());
nqp::deserialize($serialized, $dsc, $sh, nqp::list(), nqp::null());
ok(nqp::scobjcount($dsc) == 1, 'deserialized SC has a single object');
ok(nqp::istype(nqp::scgetobj($dsc, 0), T7), 'deserialized object has correct type');
ok(nqp::scgetobj($dsc, 0).a == 42, 'Integer survived serialization');
ok(nqp::scgetobj($dsc, 0).b == 6.9, 'Float survived serialization');
ok(nqp::scgetobj($dsc, 0).c eq 'llama', 'String survived serialization');
}
# Array in an object attribute
{
my $sc := nqp::createsc(fresh_in_sc());
my $sh := nqp::list_s();
class T8 {
has $!a;
has $!b;
method new() {
my $obj := nqp::create(self);
$obj.BUILD();
$obj;
}
method BUILD() {
$!a := [1,'lol',3];
$!b := [1,[2,3],4];
}
method a() { $!a }
method b() { $!b }
}
my $v := T8.new();
add_to_sc($sc, 0, $v);
my $serialized := nqp::serialize($sc, $sh);
my $dsc := nqp::createsc(fresh_out_sc());
nqp::deserialize($serialized, $dsc, $sh, nqp::list(), nqp::null());
ok(nqp::istype(nqp::scgetobj($dsc, 0), T8), 'deserialized object has correct type');
ok(nqp::elems(nqp::scgetobj($dsc, 0).a) == 3, 'array a came back with correct element count');
ok(nqp::scgetobj($dsc, 0).a[0] == 1, 'array a first element is correct');
ok(nqp::scgetobj($dsc, 0).a[1] eq 'lol', 'array a second element is correct');
ok(nqp::scgetobj($dsc, 0).a[2] == 3, 'array a third element is fine');
ok(nqp::elems(nqp::scgetobj($dsc, 0).b) == 3, 'array b came back with correct element count');
ok(nqp::scgetobj($dsc, 0).b[0] == 1, 'array b first element is correct');
ok(nqp::elems(nqp::scgetobj($dsc, 0).b[1]) == 2, 'array b nested array has correct element count');
ok(+nqp::scgetobj($dsc, 0).b[1][0] == 2, 'array b nested array first element ok');
ok(+nqp::scgetobj($dsc, 0).b[1][1] == 3, 'array b nested array second element ok');
ok(+nqp::scgetobj($dsc, 0).b[2] == 4, 'array b third element is correct');
}
# Hash in an object attribute.
{
my $sc := nqp::createsc(fresh_in_sc());
my $sh := nqp::list_s();
class T9 {
has $!a;
method new() {
my $obj := nqp::create(self);
$obj.BUILD();
$obj;
}
method BUILD() {
my %h;
%h<a> := 42;
%h<b> := 'polar bear';
$!a := %h;
}
method a() { $!a }
}
my $v := T9.new();
add_to_sc($sc, 0, $v);
my $serialized := nqp::serialize($sc, $sh);
my $dsc := nqp::createsc(fresh_out_sc());
nqp::deserialize($serialized, $dsc, $sh, nqp::list(), nqp::null());
ok(nqp::istype(nqp::scgetobj($dsc, 0), T9), 'deserialized object has correct type');
ok(nqp::elems(nqp::scgetobj($dsc, 0).a) == 2, 'hash came back with correct element count');
ok(nqp::scgetobj($dsc, 0).a<a> == 42, 'hash first element is correct');
ok(nqp::scgetobj($dsc, 0).a<b> eq 'polar bear', 'hash second element is correct');
}
# Integer array (probably needed for NFA serialization).
{
my $sc := nqp::createsc(fresh_in_sc());
my $sh := nqp::list_s();
class T10 {
has $!a;
method new() {
my $obj := nqp::create(self);
$obj.BUILD();
$obj;
}
method BUILD() {
my @a := nqp::list_i();
nqp::bindpos_i(@a, 0, 101);
nqp::bindpos_i(@a, 1, 102);
nqp::bindpos_i(@a, 2, 103);
nqp::bindpos_i(@a, 4, 105);
$!a := @a;
}
method a() { $!a }
}
my $v := T10.new();
add_to_sc($sc, 0, $v);
my $serialized := nqp::serialize($sc, $sh);
my $dsc := nqp::createsc(fresh_out_sc());
nqp::deserialize($serialized, $dsc, $sh, nqp::list(), nqp::null());
ok(nqp::istype(nqp::scgetobj($dsc, 0), T10), 'deserialized object has correct type');
ok(nqp::elems(nqp::scgetobj($dsc, 0).a) == 5, 'integer array came back with correct element count');
ok(nqp::atpos_i(nqp::scgetobj($dsc, 0).a, 0) == 101, 'integer array first element is correct');
ok(nqp::atpos_i(nqp::scgetobj($dsc, 0).a, 1) == 102, 'integer array second element is correct');
ok(nqp::atpos_i(nqp::scgetobj($dsc, 0).a, 2) == 103, 'integer array third element is correct');
ok(nqp::atpos_i(nqp::scgetobj($dsc, 0).a, 3) == 0, 'unset fourth element is correct');
ok(nqp::atpos_i(nqp::scgetobj($dsc, 0).a, 4) == 105, 'unset fourth element is correct');
}
# Number array
# String array (used by Rakudo in signatures)
{
my $sc := nqp::createsc(fresh_in_sc());
my $sh := nqp::list_s();
class T11 {
has $!a;
method new() {
my $obj := nqp::create(self);
$obj.BUILD();
$obj;
}
method BUILD() {
my @a := nqp::list_s();
nqp::bindpos_s(@a, 0, 'cow');
nqp::bindpos_s(@a, 1, 'sheep');
nqp::bindpos_s(@a, 2, 'pig');
$!a := @a;
}
method a() { $!a }
}
my $v := T11.new();
add_to_sc($sc, 0, $v);
my $serialized := nqp::serialize($sc, $sh);
my $dsc := nqp::createsc(fresh_out_sc());
nqp::deserialize($serialized, $dsc, $sh, nqp::list(), nqp::null());
ok(nqp::istype(nqp::scgetobj($dsc, 0), T11), 'deserialized object has correct type');
ok(nqp::elems(nqp::scgetobj($dsc, 0).a) == 3, 'string array came back with correct element count');
ok(nqp::atpos_s(nqp::scgetobj($dsc, 0).a, 0) eq 'cow', 'string array first element is correct');
ok(nqp::atpos_s(nqp::scgetobj($dsc, 0).a, 1) eq 'sheep', 'string array second element is correct');
ok(nqp::atpos_s(nqp::scgetobj($dsc, 0).a, 2) eq 'pig', 'string array third element is correct');
}
# Serializing an SC with a VMArray
{
my $sc := nqp::createsc(fresh_in_sc());
my $sh := nqp::list_s();
class VMArrayClass is repr('VMArray') {
}
my $v := VMArrayClass.new();
nqp::push($v, 123);
nqp::push($v, 456);
$v[3] := 789;
add_to_sc($sc, 0, $v);
my $serialized := nqp::serialize($sc, $sh);
my $dsc := nqp::createsc(fresh_out_sc());
nqp::deserialize($serialized, $dsc, $sh, nqp::list(), nqp::null());
ok(nqp::scobjcount($dsc) == 1, 'deserialized SC has a single object');
ok(nqp::istype(nqp::scgetobj($dsc, 0), VMArrayClass), 'deserialized array has correct type');
is(nqp::elems(nqp::scgetobj($dsc, 0)), 4, 'deserialize array has correct number of elements');
is(nqp::scgetobj($dsc, 0)[0], 123, '0th element of array is correct');
is(nqp::scgetobj($dsc, 0)[1], 456, '1st element of array is correct');
is(nqp::scgetobj($dsc, 0)[3], 789, '3st element of array is correct');
}
{
my knowhow NFAType is repr('NFA') { }
my $EDGE_FATE := 0;
my $EDGE_EPSILON := 1;
my $EDGE_CODEPOINT := 2;
my $EDGE_CODEPOINT_NEG := 3;
my $EDGE_CHARCLASS := 4;
my $EDGE_CHARCLASS_NEG := 5;
my $EDGE_CHARLIST := 6;
my $EDGE_CHARLIST_NEG := 7;
my $EDGE_SUBRULE := 8;
my $EDGE_CODEPOINT_I := 9;
my $EDGE_CODEPOINT_I_NEG := 10;
my $EDGE_GENERIC_VAR := 11;
# the target state of a FATE transition is ignore so we can pass anything
my $nfa := nqp::nfafromstatelist([[11],[$EDGE_CODEPOINT,102,2],[$EDGE_CODEPOINT,111,3],[$EDGE_CODEPOINT,111,4],[$EDGE_FATE,11,666]],NFAType);
my $sc := nqp::createsc(fresh_in_sc());
my $sh := nqp::list_s();
add_to_sc($sc, 0, $nfa);
my $serialized := nqp::serialize($sc, $sh);
my $dsc := nqp::createsc(fresh_out_sc());
nqp::deserialize($serialized, $dsc, $sh, nqp::list(), nqp::null());
my $deserialized_nfa := nqp::scgetobj($dsc, 0);
ok(nqp::istype($deserialized_nfa, NFAType), 'deserialized object has correct type');
my $matching := nqp::nfarunproto($deserialized_nfa,"foo",0);
ok(nqp::elems($matching) == 1,"we can match a simple string using the deserialized NFA");
ok(nqp::atpos_i($matching, 0) == 11,"...and we get the right fate");
}
# nqp::attrinitied works after serialization
{
my $sc := nqp::createsc(fresh_in_sc());
my $sh := nqp::list_s();
my class TestAttrinitied {
has $!written;
has $!read;
has $!not_inited;
has $!null;
method write() {
$!written := 123;
$!null := nqp::null();
}
method read() {
if $!read {
ok(0);
}
}
}
my $v := TestAttrinitied.new();
$v.write();
$v.read();
add_to_sc($sc, 0, $v);
my $serialized := nqp::serialize($sc, $sh);
my $dsc := nqp::createsc(fresh_out_sc());
nqp::deserialize($serialized, $dsc, $sh, nqp::list(), nqp::null());
ok(nqp::attrinited(nqp::scgetobj($dsc, 0), TestAttrinitied, '$!written'), 'nqp::attrinited on an attribute that has been written to');
ok(!nqp::attrinited(nqp::scgetobj($dsc, 0), TestAttrinitied, '$!not_inited'), 'nqp::attinitied on an attribute that has not be initialized');
ok(nqp::attrinited(nqp::scgetobj($dsc, 0), TestAttrinitied, '$!read'), 'nqp::attrinited on an attribute that has been autovivified');
if nqp::getcomp('nqp').backend.name eq 'jvm' {
skip('null handling is broken on the jvm', 1);
}
else {
ok(nqp::attrinited(nqp::scgetobj($dsc, 0), TestAttrinitied, '$!null'), 'nqp::attrinited on an attribute that has been set with null');
}
}
# Serializing an SC with a VMArray
{
my $old-empty-hash := nqp::hash();
my $sc := nqp::createsc(fresh_in_sc());
my $sh := nqp::list_s();
class VMHashClass is repr('VMHash') {
}
my $v := VMHashClass.new();
$v<hi> := 123;
$v<hello> := 456;
$v<ciao> := 789;
add_to_sc($sc, 0, $v);
my $serialized := nqp::serialize($sc, $sh);
my $dsc := nqp::createsc(fresh_out_sc());
nqp::deserialize($serialized, $dsc, $sh, nqp::list(), nqp::null());
ok(nqp::scobjcount($dsc) == 1, 'deserialized SC has a single object');
ok(nqp::istype(nqp::scgetobj($dsc, 0), VMHashClass), 'deserialized hash has correct type');
is(nqp::scgetobj($dsc, 0) + 0, 3, 'deserialized hash has correct number of keys');
is(nqp::scgetobj($dsc, 0)<hi>, 123, 'element of hash is correct');
is(nqp::scgetobj($dsc, 0)<hello>, 456, 'element of hash is correct');
is(nqp::scgetobj($dsc, 0)<ciao>, 789, 'element of hash is correct');
is(nqp::elems($old-empty-hash), 0, 'other hash unaffected by deserialization');
my $new-empty-hash := nqp::hash();
is(nqp::elems($new-empty-hash), 0, 'newly created hash starts empty');
}
{
my $sc := nqp::createsc(fresh_in_sc());
my $sh := nqp::list_s();
class WithNumArray {
has $!a;
method new() {
my $obj := nqp::create(self);
$obj.BUILD();
$obj;
}
method BUILD() {
my @a := nqp::list_n();
nqp::bindpos_n(@a, 0, 101.0);
nqp::bindpos_n(@a, 1, 102.0);
nqp::bindpos_n(@a, 2, 103.0);
$!a := @a;
}
method a() { $!a }
}
my $v := WithNumArray.new();
add_to_sc($sc, 0, $v);
my $serialized := nqp::serialize($sc, $sh);
my $dsc := nqp::createsc(fresh_out_sc());
nqp::deserialize($serialized, $dsc, $sh, nqp::list(), nqp::null());
ok(nqp::istype(nqp::scgetobj($dsc, 0), T18), 'deserialized object has correct type');
ok(nqp::elems(nqp::scgetobj($dsc, 0).a) == 3, 'num array came back with correct element count');
ok(nqp::iseq_n(nqp::atpos_n(nqp::scgetobj($dsc, 0).a, 0), 101.0), 'num array first element is correct');
ok(nqp::iseq_n(nqp::atpos_n(nqp::scgetobj($dsc, 0).a, 1), 102.0), 'num array second element is correct');
ok(nqp::iseq_n(nqp::atpos_n(nqp::scgetobj($dsc, 0).a, 2), 103.0), 'num array third element is correct');
}
# integers
my $seq := 1;
sub round_trip_int_array($desc, @a) {
my $name := 'ROUND_TRIP_SC_' ~ $seq;
$seq := $seq + 1;
$desc := 'for ' ~ $desc ~ ', ';
my $elems := nqp::elems(@a);
my $sc := nqp::createsc($name ~ '_IN');
my $sh := nqp::list_s();
class T12 {
has $!a;
method set_a(@a) { $!a := @a }
method get_a() { $!a }
}
my $v := T12.new();
$v.set_a(@a);
add_to_sc($sc, 0, $v);
my $serialized := nqp::serialize($sc, $sh);
my $dsc := nqp::createsc($name ~ '_OUT');
nqp::deserialize($serialized, $dsc, $sh, nqp::list(), nqp::null());
ok(nqp::istype(nqp::scgetobj($dsc, 0), T12), $desc ~ 'deserialized object has correct type');
my $j := 0;
my @b := nqp::scgetobj($dsc, 0).get_a();
ok(nqp::elems(@b) == $elems, $desc ~ 'array came back with correct element count');
while ($j < $elems) {
ok(nqp::iseq_i(@b[$j], @a[$j]), $desc ~ 'integer ' ~ @a[$j] ~ ' serialization round trip (' ~ $j ~ ')');
++$j;
}
};
{
my @a;
my int $i := -258;
while ($i <= 258) {
nqp::push(@a, $i);
$i := $i + 1;
}
round_trip_int_array('small integers', @a);
}
{
my $i := 9;
my int $b := 512;
while ($i < 63) {
my $backend := nqp::getcomp('nqp').backend.name;
if $i >= 31 && $backend eq 'js'
&& nqp::backendconfig(){"intvalsize"} < 8 {
todo("native NQP ints are only 32 bit on js :-(", 1);
# Sadly this also means that the rest of the tests for these sizes
# are (effectively) meaningless, because $b is 0, and 0 + 0 is still
# 0. However, they don't fail, because 0 - 4 to 0 + 2 serialise just
# fine. So marking them as TODO would give false positive TODO
# passes, which prove would alert us to.
# It's not clear *what* size guarantees NQP gives (and therefore
# expects from the underlying VM). This may need to be revisited
# for JS, because JS won't really cope with integers larger than
# 2**53
}
ok(nqp::isgt_i($b, 511), '$b is positive for 2 ** ' ~ $i);
my @a;
my int $j := -4;
while (nqp::islt_i($j, 2)) {
nqp::push(@a, nqp::add_i($b, $j));
nqp::push(@a, nqp::neg_i(nqp::add_i($b, $j)));
$j := nqp::add_i($j, 1);
}
round_trip_int_array('integers around 2 ** ' ~ $i, @a);
++$i;
$b := nqp::add_i($b, $b);
}
}
{
# values around 2 ** 63, and interesting bit patterns.
# Need to do these as BigInts parsing strings due to a bug in nqp::radix
my @s := (
'9223372036854775805', # 0x7FFFFFFFFFFFFFFD
'9223372036854775806', # 0x7FFFFFFFFFFFFFFE
'9223372036854775807', # 0x7FFFFFFFFFFFFFFF
'-9223372036854775808', # 0x8000000000000000
'-9223372036854775807', # 0x8000000000000001
'-9223372036854775806', # 0x8000000000000002
'-81985529216486896', # 0xFEDCBA9876543210
'1147797409030816545', # 0x0FEDCBA987654321
'-6148914691236517206', # 0xAAAAAAAAAAAAAAAA
'6148914691236517205', # 0x5555555555555555
'-6510615555426900571', # 0xA5A5A5A5A5A5A5A5
'6510615555426900570', # 0x5A5A5A5A5A5A5A5A
);
my $bi_type := nqp::knowhow().new_type(:name("TestBigInt"), :repr("P6bigint"));
$bi_type.HOW.compose($bi_type);
my @a;
for (@s) {
my $bi := nqp::fromstr_I($_, $bi_type);
nqp::push(@a, nqp::unbox_i($bi));
}
round_trip_int_array('special case integers', @a);
}
{
my @a;
my $i := 0;
while ($i < 64) {
nqp::push(@a, nqp::bitxor_i(-1, nqp::bitshiftl_i(1, $i)));
++$i;
}
round_trip_int_array('integers with one zero bit', @a);
}
sub create_buf($type) {
my $buf := nqp::newtype(nqp::null(), 'VMArray');
nqp::composetype($buf, nqp::hash('array', nqp::hash('type', $type)));
$buf
}
sub encode_as_base64($buf) {
my @base64 := nqp::split('', "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
my @str;
my int $i := 0;
my int $size := nqp::elems($buf);
while $i < $size {
my uint $c := nqp::atpos_u($buf, $i++);
$c := $c * 256;
if $i < $size {
$c := $c + nqp::atpos_u($buf, $i);
}
$i++;
$c := $c * 256;
if $i < $size {
$c := $c + nqp::atpos_u($buf, $i);
}
$i++;
nqp::push(@str, @base64[nqp::bitshiftr_i($c +& 0x00fc0000, 18)]);
nqp::push(@str, @base64[nqp::bitshiftr_i($c +& 0x0003f000, 12)]);
if $i > $size + 1 {
nqp::push(@str, '=');
}
else {
nqp::push(@str, @base64[nqp::bitshiftr_i($c +& 0x00000fc0, 6)]);
}
if $i > $size {
nqp::push(@str, '=');
}
else {
nqp::push(@str, @base64[$c +& 0x0000003f]);
}
}
nqp::join('', @str);
}
# Serializing an SC using nqp::serializetobuf
{
my $sc := nqp::createsc(fresh_in_sc());
my $sh := nqp::list_s();
class T1 is repr('P6int') { }
my $v1 := nqp::box_i(42, T1);
add_to_sc($sc, 0, $v1);
my $buf8 := create_buf(uint8);
my $serialized := encode_as_base64(nqp::serializetobuf($sc, $sh, $buf8));
ok(nqp::chars($serialized) > 36, 'nqp::serializetobuf - SC with P6int output longer than a header');
my $dsc := nqp::createsc(fresh_out_sc());
nqp::deserialize($serialized, $dsc, $sh, nqp::list(), nqp::null());
ok(nqp::scobjcount($dsc) == 1, 'nqp::serializetobuf - deserialized SC has a single object');
ok(nqp::istype(nqp::scgetobj($dsc, 0), T1), 'nqp::serializetobuf - deserialized object has correct type');
ok(nqp::unbox_i(nqp::scgetobj($dsc, 0)) == 42, 'nqp::serializetobuf - deserialized object has correct value');
}
|