2 # Copyright (C) 2001-2010, Parrot Foundation.
7 use lib qw( . lib ../lib ../../lib );
15 t/src/embed.t - Embedding parrot
29 # Provide a #line directive for the C code in the heredoc
30 # starting immediately after where this sub is called.
31 my $linenum = shift() + 1;
32 return "#line " . $linenum . ' "' . __FILE__ . '"' . "\n";
35 c_output_is(linedirective(__LINE__) . <<'CODE', <<'OUTPUT', "Minimal embed, using just the embed.h header" );
40 #include "parrot/embed.h"
42 void fail(const char *msg);
44 void fail(const char *msg)
46 fprintf(stderr, "failed: %s\n", msg);
50 int main(int argc, const char **argv)
53 interp = Parrot_new(NULL);
55 fail("Cannot create parrot interpreter");
58 Parrot_destroy(interp);
65 my $common = linedirective(__LINE__) . <<'CODE';
69 #include "parrot/embed.h"
70 #include "parrot/extend.h"
72 static void fail(const char *msg);
73 static Parrot_String createstring(Parrot_Interp interp, const char * value);
75 static void fail(const char *msg)
77 fprintf(stderr, "failed: %s\n", msg);
81 static Parrot_String createstring(Parrot_Interp interp, const char * value)
83 return Parrot_new_string(interp, value, strlen(value), (const char*)NULL, 0);
88 c_output_is($common . linedirective(__LINE__) . <<'CODE', <<'OUTPUT', 'Parrot_compile_string populates the error string when an opcode is given improper arguments');
90 int main(int argc, const char **argv)
93 Parrot_String err, lang;
97 interp = Parrot_new(NULL);
99 fail("Cannot create parrot interpreter");
100 lang = createstring(interp, "PIR");
102 func_pmc = Parrot_compile_string(interp, lang, ".sub foo\n copy\n.end", &err);
103 Parrot_printf(interp, "%Ss\n", err);
104 Parrot_destroy(interp);
108 The opcode 'copy' (copy<0>) was not found. Check the type and number of the arguments
111 c_output_is($common . linedirective(__LINE__) . <<'CODE', <<'OUTPUT', 'Parrot_compile_string populates the error string when given invalid language string');
113 int main(int argc, const char **argv)
115 Parrot_Interp interp;
116 Parrot_String err, lang;
120 interp = Parrot_new(NULL);
122 fail("Cannot create parrot interpreter");
123 lang = createstring(interp, "Foo");
125 func_pmc = Parrot_compile_string(interp, lang, "This doesn't matter", &err);
126 Parrot_printf(interp, "%Ss\n", err);
127 Parrot_destroy(interp);
131 Invalid interpreter type
135 c_output_is($common . linedirective(__LINE__) . <<'CODE', <<'OUTPUT', 'Parrot_compile_string populates the error string when there is an IMCC syntax error', 'todo' => 'TT #1610 : does not properly catch IMCC errors');
137 int main(int argc, const char **argv)
139 Parrot_Interp interp;
140 Parrot_String err, lang;
144 interp = Parrot_new(NULL);
146 fail("Cannot create parrot interpreter");
147 lang = createstring(interp, "PIR");
149 func_pmc = Parrot_compile_string(interp, lang, "The sleeper must awake", &err);
150 Parrot_printf(interp,"Caught exception\n");
151 Parrot_printf(interp, "%Ss\n", err);
152 Parrot_destroy(interp);
157 error:imcc:syntax error, unexpected IDENTIFIER ('The')
161 c_output_is($common . linedirective(__LINE__) . <<'CODE', <<'OUTPUT', "Hello world from main" );
165 Parrot_Interp interp;
166 Parrot_String compiler;
167 Parrot_String errstr;
170 /* Create the interpreter and show a message using parrot io */
171 interp = Parrot_new(NULL);
173 fail("Cannot create parrot interpreter");
174 Parrot_printf(interp, "Hello, parrot\n");
176 /* Compile and execute a pir sub */
177 compiler = createstring(interp, "PIR");
178 code = Parrot_compile_string(interp, compiler,
180 " say 'Hello, pir'\n"
186 Parrot_ext_call(interp, code, "->");
188 Parrot_destroy(interp);
196 c_output_is($common . linedirective(__LINE__) . <<'CODE', <<'OUTPUT', "Hello world from a sub" );
200 Parrot_Interp interp;
201 Parrot_String compiler;
202 Parrot_String errstr;
205 Parrot_String parrotname;
207 Parrot_String subname;
210 /* Create the interpreter */
211 interp = Parrot_new(NULL);
213 fail("Cannot create parrot interpreter");
215 /* Compile pir code */
216 compiler = createstring(interp, "PIR");
217 code = Parrot_compile_string(interp, compiler,
219 " say 'Must not be seen!'\n"
224 " say 'Hello, sub'\n"
231 /* Get parrot namespace */
232 rootns = Parrot_get_root_namespace(interp);
233 parrotname = createstring(interp, "parrot");
234 parrotns = Parrot_PMC_get_pmc_keyed_str(interp, rootns, parrotname);
236 subname = createstring(interp, "hello");
237 sub = Parrot_PMC_get_pmc_keyed_str(interp, parrotns, subname);
239 Parrot_ext_call(interp, sub, "->");
241 Parrot_destroy(interp);
248 c_output_is($common . linedirective(__LINE__) . <<'CODE', <<'OUTPUT', "calling a sub with argument and return" );
252 Parrot_Interp interp;
253 Parrot_String compiler;
254 Parrot_String errstr;
257 Parrot_String parrotname;
259 Parrot_String subname;
262 Parrot_String retstr;
264 /* Create the interpreter */
265 interp = Parrot_new(NULL);
267 fail("Cannot create parrot interpreter");
269 /* Compile pir code */
270 compiler = createstring(interp, "PIR");
271 code = Parrot_compile_string(interp, compiler,
273 " say 'Must not be seen!'\n"
280 " .return('world!')\n"
287 /* Get parrot namespace */
288 rootns = Parrot_get_root_namespace(interp);
289 parrotname = createstring(interp, "parrot");
290 parrotns = Parrot_PMC_get_pmc_keyed_str(interp, rootns, parrotname);
292 subname = createstring(interp, "hello");
293 sub = Parrot_PMC_get_pmc_keyed_str(interp, parrotns, subname);
296 msg = createstring(interp, "Hello, ");
297 Parrot_ext_call(interp, sub, "S->S", msg, &retstr);
298 Parrot_printf(interp, "%Ss\n", retstr);
300 Parrot_destroy(interp);
307 c_output_is($common . linedirective(__LINE__) . <<'CODE', <<'OUTPUT', "External sub" );
309 void hello(Parrot_Interp interp);
311 void hello(Parrot_Interp interp)
313 Parrot_printf(interp, "Hello from C\n");
318 Parrot_Interp interp;
319 Parrot_String compiler;
320 Parrot_String errstr;
324 /* Create the interpreter */
325 interp = Parrot_new(NULL);
327 fail("Cannot create parrot interpreter");
330 compiler = createstring(interp, "PIR");
331 code = Parrot_compile_string(interp, compiler,
340 hellosub = Parrot_sub_new_from_c_func(interp, (void (*)())& hello, "vJ");
341 Parrot_ext_call(interp, code, "P->", hellosub);
343 Parrot_destroy(interp);
350 c_output_is($common . linedirective(__LINE__) . <<'CODE', <<'OUTPUT', "Insert external sub in namespace" );
352 void hello(Parrot_Interp interp);
354 void hello(Parrot_Interp interp)
356 Parrot_printf(interp, "Hello from C\n");
361 Parrot_Interp interp;
362 Parrot_String compiler;
363 Parrot_String errstr;
367 Parrot_String parrotname;
369 Parrot_String helloname;
372 /* Create the interpreter */
373 interp = Parrot_new(NULL);
375 fail("Cannot create parrot interpreter");
378 compiler = createstring(interp, "PIR");
379 code = Parrot_compile_string(interp, compiler,
388 /* Create extern sub and insert in parrot namespace */
389 rootns = Parrot_get_root_namespace(interp);
390 parrotname = createstring(interp, "parrot");
391 parrotns = Parrot_PMC_get_pmc_keyed_str(interp, rootns, parrotname);
392 hellosub = Parrot_sub_new_from_c_func(interp, (void (*)())& hello, "vJ");
393 helloname = createstring(interp, "hello");
394 Parrot_PMC_set_pmc_keyed_str(interp, parrotns, helloname, hellosub);
397 Parrot_ext_call(interp, code, "->");
399 Parrot_destroy(interp);
406 # Old tests, skipped al
410 skip('TT #306; many symbols not exported, embedding parrot fails', 1);
412 ########################################################################
414 c_output_is( <<'CODE', <<'OUTPUT', "Parrot Compile API Single call" );
417 #include "parrot/embed.h"
420 run(PARROT_INTERP, int argc, cosnt char *argv[])
422 const char *c_src = ".sub main :main\n" " print \"ok\\n\"\n" ".end\n";
424 Parrot_String *src, *smain;
427 Parrot_String *error;
429 /* get PIR compiler - TODO API */
430 PMC *compreg = Parrot_PMC_get_pmc_keyed_int(interp,
432 IGLOBALS_COMPREG_HASH);
433 Parrot_String *pir = Parrot_str_new_constant(interp, "PIR");
434 PMC *comp = Parrot_PMC_get_pmc_keyed_str(interp, compreg, pir);
436 if (PMC_IS_NULL(comp) || !Parrot_PMC_defined(interp, comp)) {
437 Parrot_io_eprintf(interp, "Pir compiler not loaded");
442 prog = Parrot_compile_string(interp, pir, c_src, &error);
444 if (PMC_IS_NULL(prog) || !Parrot_PMC_defined(interp, prog)) {
445 Parrot_io_eprintf(interp, "Pir compiler returned no prog");
449 /* keep eval PMC alive */
450 gc_register_pmc(interp, prog);
452 /* locate function to run */
453 smain = Parrot_str_new_constant(interp, "main");
454 entry = Parrot_ns_find_current_namespace_global(interp, smain);
456 /* location of the entry */
457 interp->current_cont = new_ret_continuation_pmc(interp, NULL);
458 dest = Parrot_PMC_invoke(interp, entry, NULL);
461 interp->resume_offset = dest -interp->code->base.data;
464 Parrot_runcode(interp, argc, (char **)argv);
469 main(int margc, const char *margv[])
471 Parrot_Interp interp;
474 const char *argv[] = { "test", NULL };
476 PackFile_Segment *seg;
478 /* Interpreter set-up */
479 interp = Parrot_new(NULL);
483 /* dummy pf and segment to get things started */
484 pf = PackFile_new_dummy(interp, "test_code");
486 /* Parrot_set_flag(interp, PARROT_TRACE_FLAG); */
487 run(interp, argc, (char **)argv);
488 Parrot_exit(interp, 0);
494 c_output_is( <<'CODE', <<'OUTPUT', "Parrot Compile API Multiple Calls" );
497 #include "parrot/embed.h"
500 compile_run(PARROT_INTERP, const char *src, Parrot_String *type, int argc,
503 Parrot_String *smain;
505 Parrot_String *error;
507 PMC *prog = Parrot_compile_string(interp, type, src, &error);
509 if (PMC_IS_NULL(prog) || !Parrot_PMC_defined(interp, prog)) {
510 Parrot_io_eprintf(interp, "Pir compiler returned no prog");
514 /* keep eval PMC alive */
515 gc_register_pmc(interp, prog);
517 /* locate function to run */
518 smain = Parrot_str_new_constant(interp, "main");
519 entry = Parrot_ns_find_current_namespace_global(interp, smain);
521 /* location of the entry */
522 interp->current_cont = new_ret_continuation_pmc(interp, NULL);
523 dest = Parrot_PMC_invoke(interp, entry, NULL);
526 interp->resume_offset = dest -interp->code->base.data;
529 Parrot_runcode(interp, argc, (char **)argv);
533 run(PARROT_INTERP, int argc, const char *argv[])
535 const char *c_src = ".sub main :main\n" " print \"ok\\n\"\n" ".end\n";
538 ".sub main :main\n" " print \"hola\\n\"\n" ".end\n";
540 Parrot_String *src, *smain;
542 /* get PIR compiler - TODO API */
543 PMC *compreg = Parrot_PMC_get_pmc_keyed_int(interp,
545 IGLOBALS_COMPREG_HASH);
546 Parrot_String *pir = Parrot_str_new_constant(interp, "PIR");
547 PMC *comp = Parrot_PMC_get_pmc_keyed_str(interp, compreg, pir);
549 if (PMC_IS_NULL(comp) || !Parrot_PMC_defined(interp, comp)) {
550 Parrot_io_eprintf(interp, "Pir compiler not loaded");
554 compile_run(interp, c_src, pir, argc, argv);
555 compile_run(interp, c2_src, pir, argc, argv);
559 main(int margc, const char *margv[])
561 Parrot_Interp interp;
564 const char *argv[] = { "test", NULL };
566 PackFile_Segment *seg;
568 /* Interpreter set-up */
569 interp = Parrot_new(NULL);
573 /* dummy pf and segment to get things started */
574 pf = PackFile_new_dummy(interp, "test_code");
576 /* Parrot_set_flag(interp, PARROT_TRACE_FLAG); */
577 run(interp, argc, (char **) argv);
578 Parrot_exit(interp, 0);
585 c_output_is( <<'CODE', <<'OUTPUT', "Parrot Compile API Multiple 1st bad PIR" );
588 #include "parrot/embed.h"
591 compile_run(PARROT_INTERP, const char *src, Parrot_String *type, int argc,
594 Parrot_String *smain;
596 Parrot_String *error;
598 PMC *prog = Parrot_compile_string(interp, type, src, &error);
600 if (PMC_IS_NULL(prog) || !Parrot_PMC_defined(interp, prog)) {
601 Parrot_io_eprintf(interp, "Pir compiler returned no prog\n");
605 /* keep eval PMC alive */
606 gc_register_pmc(interp, prog);
608 /* locate function to run */
609 smain = Parrot_str_new_constant(interp, "main");
610 entry = Parrot_ns_find_current_namespace_global(interp, smain);
612 /* location of the entry */
613 interp->current_cont = new_ret_continuation_pmc(interp, NULL);
614 dest = Parrot_PMC_invoke(interp, entry, NULL);
617 interp->resume_offset = dest -interp->code->base.data;
620 Parrot_runcode(interp, argc, (char **) argv);
624 run(PARROT_INTERP, int argc, const char *argv[])
626 const char *c_src = ".sub main :main\n" " print ok\\n\"\n" ".end\n";
629 ".sub main :main\n" " print \"hola\\n\"\n" ".end\n";
631 Parrot_String *src, *smain;
633 /* get PIR compiler - TODO API */
634 PMC *compreg = Parrot_PMC_get_pmc_keyed_int(interp,
636 IGLOBALS_COMPREG_HASH);
637 Parrot_String *pir = Parrot_str_new_constant(interp, "PIR");
638 PMC *comp = Parrot_PMC_get_pmc_keyed_str(interp, compreg, pir);
640 if (PMC_IS_NULL(comp) || !Parrot_PMC_defined(interp, comp)) {
641 Parrot_io_eprintf(interp, "Pir compiler not loaded");
645 compile_run(interp, c_src, pir, argc, argv);
646 compile_run(interp, c2_src, pir, argc, argv);
650 main(int margc, const char *margv[])
652 Parrot_Interp interp;
655 const char *argv[] = { "test", NULL };
657 PackFile_Segment *seg;
659 /* Interpreter set-up */
660 interp = Parrot_new(NULL);
664 /* dummy pf and segment to get things started */
665 pf = PackFile_new_dummy(interp, "test_code");
667 /* Parrot_set_flag(interp, PARROT_TRACE_FLAG); */
668 run(interp, argc, argv);
669 Parrot_exit(interp, 0);
673 Pir compiler returned no prog
676 c_output_is( <<'CODE', <<'OUTPUT', "Parrot Compile API Multiple 2nd bad PIR" );
679 #include "parrot/embed.h"
682 compile_run(PARROT_INTERP, const char *src, Parrot_String *type, int argc,
685 Parrot_String *smain;
687 Parrot_String *error;
689 PMC *prog = Parrot_compile_string(interp, type, src, &error);
691 if (PMC_IS_NULL(prog) || !Parrot_PMC_defined(interp, prog)) {
692 Parrot_io_eprintf(interp, "Pir compiler returned no prog\n");
696 /* keep eval PMC alive */
697 gc_register_pmc(interp, prog);
699 /* locate function to run */
700 smain = Parrot_str_new_constant(interp, "main");
701 entry = Parrot_ns_find_current_namespace_global(interp, smain);
703 /* location of the entry */
704 interp->current_cont = new_ret_continuation_pmc(interp, NULL);
705 dest = Parrot_PMC_invoke(interp, entry, NULL);
708 interp->resume_offset = dest -interp->code->base.data;
711 Parrot_runcode(interp, argc, (char **)argv);
715 run(PARROT_INTERP, int argc, const char *argv[])
717 const char *c_src = ".sub main :main\n" " print ok\\n\"\n" ".end\n";
720 ".sub main :main\n" " print \"hola\\n\"\n" ".end\n";
722 Parrot_String *src, *smain;
723 /* get PIR compiler - TODO API */
724 PMC *compreg = Parrot_PMC_get_pmc_keyed_int(interp,
726 IGLOBALS_COMPREG_HASH);
727 Parrot_String *pir = Parrot_str_new_constant(interp, "PIR");
728 PMC *comp = Parrot_PMC_get_pmc_keyed_str(interp, compreg, pir);
730 if (PMC_IS_NULL(comp) || !Parrot_PMC_defined(interp, comp)) {
731 Parrot_io_eprintf(interp, "Pir compiler not loaded");
735 compile_run(interp, c2_src, pir, argc, argv);
736 compile_run(interp, c_src, pir, argc, argv);
740 main(int margc, const char *margv[])
742 Parrot_Interp interp;
745 char *argv[] = { "test", NULL };
747 PackFile_Segment *seg;
749 /* Interpreter set-up */
750 interp = Parrot_new(NULL);
754 /* dummy pf and segment to get things started */
755 pf = PackFile_new_dummy(interp, "test_code");
757 /* Parrot_set_flag(interp, PARROT_TRACE_FLAG); */
758 run(interp, argc, argv);
759 Parrot_exit(interp, 0);
764 Pir compiler returned no prog
766 c_output_is( <<'CODE', <<'OUTPUT', "Parrot Compile API Multiple bad PIR" );
769 #include "parrot/embed.h"
772 compile_run(PARROT_INTERP, const char *src, Parrot_String *type, int argc,
775 Parrot_String *smain;
777 Parrot_String *error;
779 PMC *prog = Parrot_compile_string(interp, type, src, &error);
781 if (PMC_IS_NULL(prog) || !Parrot_PMC_defined(interp, prog)) {
782 Parrot_io_eprintf(interp, "Pir compiler returned no prog\n");
786 /* keep eval PMC alive */
787 gc_register_pmc(interp, prog);
789 /* locate function to run */
790 smain = Parrot_str_new_constant(interp, "main");
791 entry = Parrot_ns_find_current_namespace_global(interp, smain);
793 /* location of the entry */
794 interp->current_cont = new_ret_continuation_pmc(interp, NULL);
795 dest = Parrot_PMC_invoke(interp, entry, NULL);
798 interp->resume_offset = dest -interp->code->base.data;
801 Parrot_runcode(interp, argc, (char **)argv);
805 run(PARROT_INTERP, int argc, const char *argv[])
807 const char *c_src = ".sub main :main\n" " print ok\\n\"\n" ".end\n";
809 const char *c2_src = ".sub main :main\n" " print hola\\n\"\n" ".end\n";
811 Parrot_String *src, *smain;
812 /* get PIR compiler - TODO API */
813 PMC *compreg = Parrot_PMC_get_pmc_keyed_int(interp,
815 IGLOBALS_COMPREG_HASH);
816 Parrot_String *pir = Parrot_str_new_constant(interp, "PIR");
817 PMC *comp = Parrot_PMC_get_pmc_keyed_str(interp, compreg, pir);
819 if (PMC_IS_NULL(comp) || !Parrot_PMC_defined(interp, comp)) {
820 Parrot_io_eprintf(interp, "Pir compiler not loaded");
824 compile_run(interp, c_src, pir, argc, argv);
825 compile_run(interp, c2_src, pir, argc, argv);
829 main(int margc, const char *margv[])
831 Parrot_Interp interp;
834 char *argv[] = { "test", NULL };
836 PackFile_Segment *seg;
838 /* Interpreter set-up */
839 interp = Parrot_new(NULL);
843 /* dummy pf and segment to get things started */
844 pf = PackFile_new_dummy(interp, "test_code");
846 /* Parrot_set_flag(interp, PARROT_TRACE_FLAG); */
847 run(interp, argc, argv);
848 Parrot_exit(interp, 0);
852 Pir compiler returned no prog
853 Pir compiler returned no prog
860 # cperl-indent-level: 4
863 # vim: expandtab shiftwidth=4: