2 # Copyright (C) 2001-2007, The Perl Foundation.
7 use lib qw( . lib ../lib ../../lib );
9 use Parrot::Test tests => 17;
13 t/pmc/eval.t - Dynamic Code Evaluation
21 Tests on-the-fly PASM, PIR and PAST compilation and invocation.
25 pasm_output_is( <<'CODE', <<'OUTPUT', "eval_sc" );
26 compreg P1, "PASM" # get compiler
27 set_args "0", "print \"in eval\\n\"\nset_returns \"()\"\nreturncc\n"
30 invokecc P0 # eval code P0
38 pasm_output_is( <<'CODE', <<'OUTPUT', "call subs in evaled code " );
39 set S5, ".pcc_sub _foo:\n"
40 concat S5, "print \"foo\\n\"\n"
41 concat S5, "set_returns \"()\"\n"
42 concat S5, "returncc\n"
55 pasm_output_is( <<'CODE', <<'OUTPUT', "call 2 subs in evaled code " );
56 set S5, ".pcc_sub _foo:\n"
57 concat S5, "print \"foo\\n\"\n"
58 concat S5, "set_returns \"()\"\n"
59 concat S5, "returncc\n"
60 concat S5, ".pcc_sub _bar:\n"
61 concat S5, "print \"bar\\n\"\n"
62 concat S5, "set_returns \"()\"\n"
63 concat S5, "returncc\n"
82 pir_output_is( <<'CODE', <<'OUTPUT', "PIR compiler sub" );
86 get_global compiler, "xcompile"
87 compreg "XPASM", compiler
88 .local pmc my_compiler
89 my_compiler = compreg "XPASM"
92 code = "print \"ok\\n\"\n"
93 code .= "set_returns \"()\"\n"
95 the_sub = my_compiler("_foo", code)
97 the_sub = global "_foo"
102 .param string sub_name
108 .local pmc pasm_compiler
109 pasm_compiler = compreg "PASM"
111 $P0 = pasm_compiler($S0)
119 pir_output_is( <<'CODE', <<'OUTPUT', "bug #31467" );
123 $P0 = find_name "_builtin"
127 $S0 = ".sub main\nprint \"dynamic\\n\"\n.end\n"
131 set_global "funcs", $P1
133 $S0 = ".sub main\n$P1 = get_global\"funcs\"\n"
134 $S0 .= "$P0 = $P1['dynamic']\n$P0()\n"
135 $S0 .= "$P0 = $P1['builtin']\n$P0()\n"
152 pir_output_is( <<'CODE', <<'OUTPUT', "PIR compiler sub PASM" );
156 .local pmc compiler, invokable
157 compiler = compreg "PUTS"
159 invokable = compiler("ok 1")
164 .sub register_compiler
165 $P0 = get_global "puts"
170 .param string printme
172 .local pmc pasm_compiler, retval
173 pasm_compiler = compreg "PASM"
180 code .= "set_returns \"()\"\n"
183 retval = pasm_compiler( code )
191 pir_output_is( <<'CODE', <<'OUTPUT', "PIR compiler sub PIR" );
195 .local pmc compiler, invokable
196 compiler = compreg "PUTS"
198 invokable = compiler( "ok 1" )
203 .sub register_compiler
205 counter = new 'Integer'
207 set_global "counter", counter
209 $P0 = get_global "_puts"
214 .param string printme
216 .local pmc pir_compiler, retval
217 pir_compiler = compreg "PIR"
220 counter = get_global "counter"
224 code = ".sub anonymous"
233 retval = pir_compiler( code )
241 pir_output_is( <<'CODE', <<'OUTPUT', "eval.get_string" );
246 f1 = compi("foo_1", "hello from foo_1")
248 io = open "temp.pbc", ">"
251 load_bytecode "temp.pbc"
252 f2 = compi("foo_2", "hello from foo_2")
253 io = open "temp2.pbc", ">"
256 load_bytecode "temp2.pbc"
261 .param string printme
263 .local pmc pir_compiler, retval
264 pir_compiler = compreg "PIR"
273 retval = pir_compiler(code)
281 pir_output_is( <<'CODE', <<'OUTPUT', "check loaded lib hash" );
283 load_bytecode "temp.pbc"
284 load_bytecode "temp2.pbc"
285 .local pmc pbc_hash, interp
286 .include 'iglobals.pasm'
288 pbc_hash = interp[.IGLOBALS_PBC_LIBS]
289 $I0 = elements pbc_hash
292 $I1 = exists pbc_hash['temp']
295 $I2 = exists pbc_hash['temp2']
298 $S0 = pbc_hash['temp2']
299 # print $S0 not portable
300 $I3 = index $S0, 'temp2.pbc'
310 pir_output_is( <<'CODE', <<'OUTPUT', "eval.get_string - same file" );
315 f1 = compi("foo_1", "hello from foo_1")
317 io = open "temp.pbc", ">"
320 load_bytecode "temp.pbc"
323 f2 = compi("foo_2", "hello from foo_2")
324 io = open "temp.pbc", ">"
327 load_bytecode "temp.pbc"
332 .param string printme
334 .local pmc pir_compiler, retval
335 pir_compiler = compreg "PIR"
344 retval = pir_compiler(code)
352 unlink "temp.pbc", "temp2.pbc", "temp.file";
355 pir_output_is( <<'CODE', <<'OUTPUT', "eval.freeze" );
359 f = compi("foo_1", "hello from foo_1")
361 io = open "temp.file", ">"
369 .param string printme
371 .local pmc pir_compiler, retval
372 pir_compiler = compreg "PIR"
381 retval = pir_compiler(code)
388 pir_output_is( <<'CODE', <<'OUTPUT', "eval.thaw" );
395 size = stat file, .STAT_FILESIZE
401 e = get_global "foo_1"
409 pir_output_is( <<'CODE', <<'OUTPUT', "eval.freeze+thaw" );
413 f = compi("foo_1", "hello from foo_1")
415 io = open "temp.file", ">"
424 .param string printme
426 .local pmc pir_compiler, retval
427 pir_compiler = compreg "PIR"
442 retval = pir_compiler(code)
452 size = stat file, .STAT_FILESIZE
458 e = get_global "foo_1"
467 pir_output_is( <<'CODE', <<'OUTPUT', "get_pmc_keyed_int" );
470 .local pmc e, s, compi
479 compi = compreg "PIR"
493 pir_output_is( <<'CODE', <<'OUTPUT', "catch compile err: RT:#39892" );
512 open my $TEMP, '>', "temp.pir" or die "can't open 'temp.pir': $!";
513 END { unlink "temp.pir" }
521 pir_error_output_like( <<'CODE', <<'OUTPUT', "compile err in load_bytecode" );
523 load_bytecode "temp.pir"
528 /undefined identifier/
531 pir_output_is( <<'CODE', <<'OUTPUT', "catch compile err in load_bytecode" );
534 load_bytecode "temp.pir"
546 # cperl-indent-level: 4
549 # vim: expandtab shiftwidth=4: