Skip to content
This repository
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 1130 lines (815 sloc) 19.238 kb
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 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
/*
Copyright (C) 2001-2013, Parrot Foundation.

=head1 NAME

src/nci_test.c - shared library used for testing the Native Call Interface

=head1 DESCRIPTION

From this code a shared library can be compiled and linked with a command like:

cc -shared -fpic nci_test.c -o libnci_test.so -g

For non-Unix platforms the above command has to be modified appropriately.

The resulting shared library should be copied to a location like:

parrot/runtime/parrot/dynext/libnci_test.so

At that location the shared library is loadable with the opcode 'loadlib'.
The functions in the library are available with the opcode 'dlfunc'.
The variables in the library are available with the opcode 'dlvar'.

=head2 Functions

The name of a test function is usually 'nci_<signature>'. E.g. the function
'nci_ip' takes a 'pointer' and returns a 'int'.

=over 4

=cut

*/

#define PARROT_IN_EXTENSION
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "parrot/parrot.h"

#ifdef __cplusplus
extern "C" {
#endif


/* Declarations of structs */

typedef struct Nested {
    int y;
} Nested;

typedef struct Outer {
    int x;
    Nested *nested;
} Outer;

typedef struct Rect_Like {
    int x, y;
    int w, h;
} Rect_Like;

typedef struct Opaque {
    int x;
} Opaque;

/* Function declarations.

*** If you add a new test function here,
*** please update src/libnci_test.def and src/nci/extra_thunks.nci too. ***

*/

PARROT_DYNEXT_EXPORT int call_back(PARROT_INTERP, char *);
PARROT_DYNEXT_EXPORT char nci_c(void);
PARROT_DYNEXT_EXPORT char nci_csc(short, char);
PARROT_DYNEXT_EXPORT double nci_d(void);
PARROT_DYNEXT_EXPORT double nci_dd(double);
PARROT_DYNEXT_EXPORT float nci_f(void);
PARROT_DYNEXT_EXPORT float nci_fff(float, float);
PARROT_DYNEXT_EXPORT int nci_i(void);
PARROT_DYNEXT_EXPORT int nci_ib(int *);
PARROT_DYNEXT_EXPORT int nci_iiii(int, int, int);
PARROT_DYNEXT_EXPORT int nci_ip(void *);
PARROT_DYNEXT_EXPORT int nci_isc(short, char);
PARROT_DYNEXT_EXPORT long nci_l(void);
PARROT_DYNEXT_EXPORT int * nci_p(void);
PARROT_DYNEXT_EXPORT void * nci_pi(int);
PARROT_DYNEXT_EXPORT void * nci_pii(int, int);
PARROT_DYNEXT_EXPORT void * nci_piiii(int, int, int, int);
PARROT_DYNEXT_EXPORT void nci_pip(int, ARGIN(const Rect_Like *));
PARROT_DYNEXT_EXPORT void * nci_pp(void *);
PARROT_DYNEXT_EXPORT short nci_s(void);
PARROT_DYNEXT_EXPORT short nci_ssc(short, char);
PARROT_DYNEXT_EXPORT char * nci_t(void);
PARROT_DYNEXT_EXPORT char * nci_tt(const char *);
PARROT_DYNEXT_EXPORT void nci_v(void);
PARROT_DYNEXT_EXPORT void nci_vP(void *);
PARROT_DYNEXT_EXPORT void nci_vpii(ARGMOD(Outer *), int, int);
PARROT_DYNEXT_EXPORT void nci_vv(void);
PARROT_DYNEXT_EXPORT void nci_vp(ARGIN(const Opaque*));
PARROT_DYNEXT_EXPORT void * nci_pv(void);
PARROT_DYNEXT_EXPORT void nci_vfff(float, float, float);
PARROT_DYNEXT_EXPORT char * nci_cstring_cstring(const char *);

/* Declarations for callback tests */

typedef void (*cb_C1_func)(const char*, void*);
PARROT_DYNEXT_EXPORT void nci_cb_C1(cb_C1_func, void*);

typedef void (*cb_C2_func)(int, void*);
PARROT_DYNEXT_EXPORT void nci_cb_C2(cb_C2_func, void*);

typedef void (*cb_C3_func)(void*, void*);
PARROT_DYNEXT_EXPORT void nci_cb_C3(cb_C3_func, void*);

typedef void (*cb_D1_func)(void*, const char*);
PARROT_DYNEXT_EXPORT void nci_cb_D1(cb_D1_func, void*);

typedef void (*cb_D2_func)(void*, int);
PARROT_DYNEXT_EXPORT void nci_cb_D2(cb_D2_func, void*);

typedef void (*cb_D3_func)(void*, void*);
PARROT_DYNEXT_EXPORT void nci_cb_D3(cb_D3_func, void*);

typedef void (*cb_D4_func)(void*, void*);
PARROT_DYNEXT_EXPORT void nci_cb_D4(cb_D4_func, void*);

/* Variable definitions */
/* Note that PARROT_DATA (i.e. extern) or static will not work with
our nci test. We want only one symbol, exported as visible.
*/
PARROT_DYNEXT_EXPORT int int_cb_D4 = -55555;
PARROT_DYNEXT_EXPORT int nci_dlvar_char = 22;
PARROT_DYNEXT_EXPORT int nci_dlvar_short = 333;
PARROT_DYNEXT_EXPORT int nci_dlvar_int = -4444;
PARROT_DYNEXT_EXPORT long nci_dlvar_long = -7777777;
PARROT_DYNEXT_EXPORT float nci_dlvar_float = -333.0;
PARROT_DYNEXT_EXPORT double nci_dlvar_double = -55555.55555;
PARROT_DYNEXT_EXPORT char nci_dlvar_cstring[] = "This is a C-string.\n";

/* Function definitions */

/*

=item C<PARROT_DYNEXT_EXPORT char nci_c(void)>

Returns the value of the variable C<nci_dlvar_char>, which is set to 22 by
default.

=cut

*/

PARROT_DYNEXT_EXPORT
PARROT_PURE_FUNCTION
char
nci_c(void)
{
    return nci_dlvar_char;
}

/*

=item C<PARROT_DYNEXT_EXPORT char nci_csc(short l1, char l2)>

Multiplies C<l1> and C<l2> together and returns the first byte of the result.

=cut

*/

PARROT_DYNEXT_EXPORT
PARROT_CONST_FUNCTION
char
nci_csc(short l1, char l2)
{
    return l1 * l2;
}

/*

=item C<PARROT_DYNEXT_EXPORT double nci_d(void)>

Multiplies the current value of C<nci_dlvar_double> by 10.0, and returns
the new value.

=cut

*/

PARROT_DYNEXT_EXPORT
double
nci_d(void)
{
    nci_dlvar_double *= 10.0;

    return nci_dlvar_double;
}

/*

=item C<PARROT_DYNEXT_EXPORT double nci_dd(double d)>

Returns the value C<d> multiplied by 2.0.

=cut

*/

PARROT_DYNEXT_EXPORT
PARROT_CONST_FUNCTION
double
nci_dd(double d)
{
    return d * 2.0;
}

/*

=item C<PARROT_DYNEXT_EXPORT float nci_f(void)>

Multiplies the value C<nci_dlvar_float> by 10.0 and returns the new
value.

=cut

*/

PARROT_DYNEXT_EXPORT
float
nci_f(void)
{
    nci_dlvar_float *= 10.0;

    return nci_dlvar_float;
}

/*

=item C<PARROT_DYNEXT_EXPORT float nci_fff(float l1, float l2)>

Returns the result of C<l1> / C<l2>.

=cut

*/

PARROT_DYNEXT_EXPORT
PARROT_CONST_FUNCTION
float
nci_fff(float l1, float l2)
{
    return l1 / l2;
}

/*

=item C<PARROT_DYNEXT_EXPORT int nci_i(void)>

Returns the current value of <nci_dlvar_int>.

=cut

*/

PARROT_DYNEXT_EXPORT
PARROT_PURE_FUNCTION
int
nci_i(void)
{
    return nci_dlvar_int;
}

/*

=item C<PARROT_DYNEXT_EXPORT int nci_isc(short l1, char l2)>

Returns the int product of C<l1 * l2>.

=cut

*/

PARROT_DYNEXT_EXPORT
PARROT_CONST_FUNCTION
int
nci_isc(short l1, char l2)
{
    return l1 * l2;
}

/*

=item C<PARROT_DYNEXT_EXPORT int nci_ip(void *p)>

Performs a series of operations on values stored at pointer C<p>.

=cut

*/

PARROT_DYNEXT_EXPORT
int
nci_ip(void *p)
{
    typedef struct _dfi {
        double d;
        float f;
        int i;
        char *s;
    } dfi;
    const dfi * const sp = (const dfi*) p;
    puts(sp->s);
    fflush(stdout);

    return (int) (sp->d + sp->f + sp->i);
}


/*

=item C<PARROT_DYNEXT_EXPORT long nci_l(void)>

Returns the value of C<nci_dlvar_long>.

=cut

*/

PARROT_DYNEXT_EXPORT
PARROT_PURE_FUNCTION
long
nci_l(void)
{
    return nci_dlvar_long;
}

/*

=item C<PARROT_DYNEXT_EXPORT int * nci_p(void)>

Returns the address of C<nci_dlvar_int>.

=cut

*/

PARROT_DYNEXT_EXPORT
PARROT_CONST_FUNCTION
int *
nci_p(void)
{
    return &nci_dlvar_int;
}

/*

=item C<PARROT_DYNEXT_EXPORT void * nci_pp(void *p)>

Returns the value C<p> directly.

=cut

*/

PARROT_DYNEXT_EXPORT
PARROT_CONST_FUNCTION
void *
nci_pp(void *p)
{
    return p;
}

/*

=item C<PARROT_DYNEXT_EXPORT int nci_iiii(int i1, int i2, int i3)>

Prints three integers separated by whitespace to C<stderr>.
Returns 2.

=cut

*/

PARROT_DYNEXT_EXPORT
int
nci_iiii(int i1, int i2, int i3)
{
    fprintf(stderr, "%d %d %d\n", i1, i2, i3);
    fflush(stderr);

    return 2;
}

/*

=item C<PARROT_DYNEXT_EXPORT int call_back(PARROT_INTERP, char *cstr)>

writes the string C<str> to stdout and returns the value 4711.

=cut

*/

PARROT_DYNEXT_EXPORT
int
call_back(SHIM_INTERP, char *cstr)
{
    puts(cstr);
    fflush(stdout);
    return 4711;
}

/*

=item C<PARROT_DYNEXT_EXPORT void * nci_pi(int test)>

Performs one from a series of tests, depending on the value given for C<test>.

=cut

*/

PARROT_DYNEXT_EXPORT
void *
nci_pi(int test)
{
    switch (test) {
      case 0:
        {
            static struct {
                int i[2];
                char c;
            } t = {
                {42, 100},
                'B'
            };
            return &t;
        }
      case 1:
        {
            static struct {
                float f[2];
                double d;
            } t = {
                {42.0, 100.0},
                47.11
            };
            return &t;
          }
      case 2:
        {
            static struct {
                char c;
                int i;
            } t = {
                10,
                20
            };
            return &t;
        }
      case 3:
        {
            static struct {
                const char *c;
                int i;
            } t = {
                "hello",
                20
            };
            return &t;
        }
      case 4:
        {
            static struct _x {
                int i;
                int j;
                double d;
            } xx = { 100, 77, 200.0 };
            static struct {
                char c;
                struct _x *x;
            } t = {
                10,
                &xx
            };
            return &t;
        }
      case 5:
        {
            static struct {
                int (*f)(PARROT_INTERP, char *);
            } t = {
                call_back
            };
            return &t;
        }
      case 6:
        {
            static struct xt {
                int x;
                struct yt {
                    int i;
                    int j;
                } _y;
                int z;
            } _x = {
                32,
                { 127, 12345 },
                33
            };
            return &_x;
        }
      case 7:
        {
            static struct xt {
                char x;
                struct yt {
                    char i;
                    int j;
                } _y;
                char z;
            } _x = {
                32,
                { 127, 12345 },
                33
            };
            return &_x;
        }
      case 8:
        {
            static struct _z {
                int i;
                int j;
            } zz = { 100, 77 };
            static struct xt {
                int x;
                struct yt {
                    int i;
                    int j;
                    struct _z *z;
                } _y;
            } _x = {
                32,
                { 127, 12345, &zz },
            };
            return &_x;
        }
      case 9:
        {
            static int i = 55555;
            return &i;
        }
      case 10:
        return NULL;
      default:
        fprintf(stderr, "unknown test number\n");
    }

    return NULL;
}

/*

=item C<PARROT_DYNEXT_EXPORT short nci_s(void)>

Returns the value of C<nci_dlvar_short>.

=cut

*/

PARROT_DYNEXT_EXPORT
PARROT_PURE_FUNCTION
short
nci_s(void)
{
    return nci_dlvar_short;
}

/*

=item C<PARROT_DYNEXT_EXPORT short nci_ssc(short l1, char l2)>

Returns the product of C<l1 * l2>.

=cut

*/

PARROT_DYNEXT_EXPORT
PARROT_CONST_FUNCTION
short
nci_ssc(short l1, char l2)
{
    return l1 * l2;
}

/*

=item C<PARROT_DYNEXT_EXPORT void nci_vP(void *pmc)>

Prints "ok" if C<PMC> is not null, prints "got null" otherwise.

=cut

*/

PARROT_DYNEXT_EXPORT
void
nci_vP(SHIM(void *pmc))
{
    /* TODO:
* Disable this test until someone figures a way to check for
* PMCNULL without using libparrot.
if (!PMC_IS_NULL(pmc))
puts("ok");
else
*/
    puts("got null");
}


/*

=back

=head2 Functions used for pdd16 tests

=over 4

=cut

*/

/*

=item C<PARROT_DYNEXT_EXPORT void nci_cb_C1(cb_C1_func cb, void* user_data)>

Calls C<cb> function with the string "result" and the given C<user_data>.
No return value.

=cut

*/

PARROT_DYNEXT_EXPORT
void
nci_cb_C1(cb_C1_func cb, void* user_data)
{
    const char * const result = "succeeded";
    /* call the cb synchronously */
    (cb)(result, user_data);

    return;
}

/*

=item C<PARROT_DYNEXT_EXPORT void nci_cb_C2(cb_C2_func cb, void* user_data)>

Calls the function C<cb> with the integer 77 and the given C<user_data>.
No return value.

=cut

*/

PARROT_DYNEXT_EXPORT
void
nci_cb_C2(cb_C2_func cb, void* user_data)
{
    /* call the cb synchronously */
    (cb)(77, user_data);

    return;
}

/*

=item C<PARROT_DYNEXT_EXPORT void nci_cb_C3(cb_C3_func cb, void* user_data)>

Calls function C<cb> with C<&int_cb_C3> and the givn C<user_data>.
No return value.

=cut

*/

static int int_cb_C3 = 99;

PARROT_DYNEXT_EXPORT
void
nci_cb_C3(cb_C3_func cb, void* user_data)
{
    /* call the cb synchronously */
    (cb)(&int_cb_C3, user_data);

    return;
}

/*

=item C<PARROT_DYNEXT_EXPORT void nci_cb_D1(cb_D1_func cb, void* user_data)>

Calls function C<cb> with data C<user_data> and the string "succeeded".
No return value.

=cut

*/

PARROT_DYNEXT_EXPORT
void
nci_cb_D1(cb_D1_func cb, void* user_data)
{
    const char * const result = "succeeded";
    /* call the cb synchronously */
    (cb)(user_data, result);

    return;
}

/*

=item C<PARROT_DYNEXT_EXPORT void nci_cb_D2(cb_D2_func cb, void* user_data)>

Calls function C<cb> with data C<user_data> and the integer 88.
No return value.

=cut

*/

PARROT_DYNEXT_EXPORT
void
nci_cb_D2(cb_D2_func cb, void* user_data)
{
    /* call the cb synchronously */
    (cb)(user_data, 88);

    return;
}

/*

=item C<PARROT_DYNEXT_EXPORT void nci_cb_D3(cb_D3_func cb, void* user_data)>

Calls function C<cb> with data C<user_data> and C<&int_cb_D3>.
No return value.

=cut

*/

static int int_cb_D3 = 111;

PARROT_DYNEXT_EXPORT
void
nci_cb_D3(cb_D3_func cb, void* user_data)
{
    /* call the cb synchronously */
    (cb)(user_data, &int_cb_D3);

    return;
}

/*

=item C<PARROT_DYNEXT_EXPORT void nci_cb_D4(cb_D4_func times_ten, void*
user_data)>

Calls function C<times_ten> with data C<user_data> and C<&int_cb_D4> 10 times
in a loop, incrementing C<int_cb_D4> after every call.
No return value.

=cut

*/

PARROT_DYNEXT_EXPORT
void
nci_cb_D4(cb_D4_func times_ten, void* user_data)
{
    int count;
    for (count = 0; count < 9; ++count)
    {
        (times_ten)(user_data, &int_cb_D4);
        ++int_cb_D4;
    }

    return;
}

/*

=item C<PARROT_DYNEXT_EXPORT void nci_pip(int count, const Rect_Like *rects)>

Prints a count integer and the coordinates of 4 rectangles.

=cut

*/

PARROT_DYNEXT_EXPORT
void
nci_pip(int count, ARGIN(const Rect_Like *rects))
{
    int i;
    printf("Count: %d\n", count);
    for (i = 0; i < 4; ++i)
        printf("X: %d\nY: %d\nW: %d\nH: %d\n",
        rects[i].x, rects[i].y, rects[i].w, rects[i].h);
}

/*

=item C<PARROT_DYNEXT_EXPORT void nci_vpii(Outer *my_data, int my_x, int my_y)>

Updates data in structure pointer C<my_data> with the given data C<my_x> and
C<my_y>.

=cut

*/

PARROT_DYNEXT_EXPORT
void
nci_vpii(ARGMOD(Outer *my_data), int my_x, int my_y)
{
    my_data->x = my_x;
    my_data->nested->y = my_y;
}

/*

=item C<PARROT_DYNEXT_EXPORT void * nci_piiii(int alpha, int beta, int gamma,
int delta)>

Stores 4 integer values into an array structure, and returns the address
of that structure.

=cut

*/

static int my_array[4];

PARROT_DYNEXT_EXPORT
void *
nci_piiii(int alpha, int beta, int gamma, int delta)
{
    static struct array_container
    {
        int x;
        int *array;
    } container;

    my_array[0] = alpha;
    my_array[1] = beta;
    my_array[2] = gamma;
    my_array[3] = delta;

    container.x = 4;
    container.array = my_array;

    return &container;
}

/*

=item C<PARROT_DYNEXT_EXPORT void * nci_pii(int fac1, int fac2)>

Returns the address of global variable C<nci_dlvar_int> whose value is set
to the product of C<fac1 * fac2>.

=cut

*/

PARROT_DYNEXT_EXPORT
void *
nci_pii(int fac1, int fac2)
{
    nci_dlvar_int = fac1 * fac2;

    return &nci_dlvar_int;
}


/*

=item C<PARROT_DYNEXT_EXPORT char * nci_t(void)>

Returns the value of C<nci_dlvar_cstring>.

=cut

*/

PARROT_DYNEXT_EXPORT
PARROT_CONST_FUNCTION
char *
nci_t(void)
{
    return nci_dlvar_cstring;
}


/*

=item C<PARROT_DYNEXT_EXPORT char * nci_tt(const char *p)>

Returns "xx worked", where "xx" is replaced with the first two character values
of C<p>, in reverse order.

=cut

*/

static char s[] = "xx worked\n";

PARROT_DYNEXT_EXPORT
char *
nci_tt(const char *p)
{
    s[0] = p[1];
    s[1] = p[0];

    return s;
}


/*

=item C<PARROT_DYNEXT_EXPORT void nci_v(void)>

Multiplies the global variable C<nci_dlvar_int> times 10.

=cut

*/

PARROT_DYNEXT_EXPORT
void
nci_v(void)
{
    nci_dlvar_int *= 10;
}

/*

=item C<PARROT_DYNEXT_EXPORT void nci_vv(void)>

Multiplies the global variable C<nci_dlvar_int> by 3.

=cut

*/

PARROT_DYNEXT_EXPORT
void
nci_vv(void)
{
    nci_dlvar_int *= 3;
}

/*

=item C<PARROT_DYNEXT_EXPORT void nci_vp(const Opaque *inOpaque)>

Test that a previously generated opaque struct gets passed back
to an NCI function correctly.

=cut

*/

PARROT_DYNEXT_EXPORT
void
nci_vp(ARGIN(const Opaque *inOpaque))
{
    if (inOpaque)
        printf("got %d\n", inOpaque->x);
    else
        printf("got null\n");
}

/*

=item C<PARROT_DYNEXT_EXPORT void * nci_pv(void)>

Return the pointer to the global variable C<nci_dlvar_int>.

=cut

*/

PARROT_DYNEXT_EXPORT
void *
nci_pv(void)
{
    return &nci_dlvar_int;
}

/*

=item C<static void validate_float(float f, double checkval)>

Check that a float value f is has an error ratio of less than 0.01
when compared to a double value checkval

=cut

*/

static void
validate_float(float f, double checkval)
{
    int valid;
    double error_ratio;
    error_ratio = (((double)f) - checkval) / checkval;
    valid = error_ratio <= 0.01 && error_ratio >= -0.01;
    printf("%i\n", valid);
}

/*

=item C<PARROT_DYNEXT_EXPORT void nci_vfff(float l1, float l2, float l3)>

Checks that C<[ l1, l2, l3 ]> = C<[ 3456.54, 10.1999, 14245.567 ]> within an
error of 0.01.

=cut

*/

PARROT_DYNEXT_EXPORT
void
nci_vfff(float l1, float l2, float l3)
{
    validate_float(l1, 3456.54);
    validate_float(l2, 10.1999);
    validate_float(l3, 14245.567);
}

/*

=item C<PARROT_DYNEXT_EXPORT char * nci_cstring_cstring(const char * src)>

Copy the content of src to a static buffer, replacing 'l' with 'L' and
return a pointer for the buffer.

=cut

*/

PARROT_DYNEXT_EXPORT
char *
nci_cstring_cstring(const char * src)
{
    static char buffer[64];
    const int maxl = sizeof buffer - 1;
    int l = strlen(src);
    int i;
    if (l > maxl)
        l = maxl;
    for (i = 0; i < l; ++i) {
        char c = src[i];
        if (c == 'l')
            c = 'L';
        buffer[i] = c;
    }
    buffer[i] = '\0';
    return buffer;
}

#ifdef TEST

char l2 = 4;
float f2 = 4.0;

/*

=item C<int main(void)>

Calls test functions C<nci_ssc> and C<nci_fff> and prints their results.

=cut

*/

int
main(void)
{
    short l1 = 3;
    float f, f1 = 3.0;
    int l = nci_ssc(l1, l2);
    printf("%d\n", l);
    f = nci_fff(f1, f2);
    printf("%f\n", f);

    return 0;
}

#endif

#ifdef __cplusplus
}
#endif

/*

=back

=head1 SEE ALSO

F<docs/pdds/draft/pdd16_native_call.pod>, F<config/gen/makefiles/root.in>, F<t/pmc/nci.t>

=cut

*/


/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
*/
Something went wrong with that request. Please try again.