Skip to content
Browse files

Add a new prototype API function to write a PackFile PMC out to a .pb…

…c file. This will be used to implement the -o and -r commandline options in the front-end
  • Loading branch information...
1 parent 6e800f9 commit 2f0411b6b50f28ca4eb4b4debc90c0120d3728d3 @Whiteknight Whiteknight committed Jan 22, 2011
Showing with 40 additions and 0 deletions.
  1. +40 −0 src/embed/api.c
View
40 src/embed/api.c
@@ -530,6 +530,46 @@ Parrot_api_serialize_bytecode_pmc(Parrot_PMC interp_pmc, Parrot_PMC pbc,
/*
+=item <Parrot_Int Parrot_api_write_bytecode_to_file(Parrot_PMC interp_pmc,
+Parrot_PMC pbc, Parrot_String filename)>
+
+Write out a PackFile PMC to a .pbc file
+
+=cut
+
+*/
+
+PARROT_API
+Parrot_Int
+Parrot_api_write_bytecode_to_file(Parrot_PMC interp_pmc, Parrot_PMC pbc,
+ Parrot_String filename)
+{
+ ASSERT_ARGS(Parrot_api_write_bytecode_to_file)
+ EMBED_API_CALLIN(interp_pmc, interp)
+ PMC * filepmc = PMCNULL;
+ PackFile * const pf = (PackFile *)VTABLE_get_pointer(interp, pbc);
+ if (!pf)
+ Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_UNEXPECTED_NULL,
+ "Could not get packfile.");
+ else {
+ PIOHANDLE fp = PIO_OPEN(interp, filename, PIO_F_WRITE);
+ if (fp == PIO_INVALID_HANDLE)
+ Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_PIO_ERROR,
+ "Cannot open output file %Ss", filename);
+ else {
+ const Parrot_Int size = PackFile_pack_size(interp, pf) * sizeof (opcode_t);
+ opcode_t * const packed = (opcode_t*)mem_sys_allocate(size);
+ PackFile_pack(interp, pf, packed);
+ PIO_WRITE(interp, fp, (char *)packed, size);
+ }
+ PIOCLOSE(interp, fp);
+ }
+
+ EMBED_API_CALLOUT(interp_pmc, interp)
+}
+
+/*
+
=item C<Parrot_Int Parrot_api_set_warnings(Parrot_PMC interp_pmc, Parrot_Int
flags)>

0 comments on commit 2f0411b

Please sign in to comment.
Something went wrong with that request. Please try again.