parrotcode: Parrot embedding system | |
Contents | Documentation |
embed.pod - Parrot embedding system
#include <parrot.h>
#include <parrot/embed.h>
#include <parrot/extend.h>
int main(int argc, char* argv[])
{
Parrot_Interp interp;
Parrot_PackFile pf;
interp = Parrot_new(NULL);
if (!interp) {
return 1;
}
pf = Parrot_readbc(interp, "foo.pbc");
Parrot_loadbc(interp, pf);
Parrot_runcode(pf, argc, argv);
Parrot_destroy(interp);
return 0;
}
This is the documentation for Parrot's embedding API.
Parrot_Interp
Parrot_Interp
, which represents a Parrot interpreter. It is a required argument to almost every Parrot API function. The structure is opaque in an embedded environment, so you cannot directly access any of its members.Parrot_PackFile
Parrot_String
Parrot_PMC
Parrot_PMC
declaration per line.Parrot_Int
Parrot_Float
Parrot_Int
Parrot_UInt
Not documented yet.
These are used with the Parrot_call_sub family of functions.
Parrot_Interp Parrot_new(Parrot_Interp parent)
Parrot_set_flag(Parrot_Interp interp, Parrot_int flags)
void Parrot_set_run_core(Parrot_Interp interp, Parrot_Run_core_t core)
Parrot_set_trace(Parrot_Interp, Parrot_UInt flags)
void Parrot_set_executable_name(Parrot_Interp interp, Parrot_string name)
void Parrot_destroy(Parrot_Interp interp)
void Parrot_really_destroy(Parrot_Interp interp, int exit_code)
void Parrot_exit(Parrot_Interp interp, int status)
status
. Before exiting, the function calls all registered exit handlers in LIFO order. Parrot_really_destroy()
is usually called as the last exit handler.void Parrot_on_exit(Parrot_Interp interp, void (*handler)(Parrot_Interp, int, void *), void *arg)
Parrot_exit()
in LIFO order. The handler function should accept as arguments an interpreter, an integer exit code, and an argument (which can be NULL).void imcc_init(Parrot_Interp interp)
Parrot_PackFile Parrot_readbc(Parrot_Interp interp, const char *path)
path
. Returns a packfile structure for use by Parrot_loadbc()
.void Parrot_loadbc(Parrot_Interp interp, Parrot_PackFile pf)
void Parrot_runcode(Parrot_Interp interp, int argc, char *argv[])
argc
and argv[]
to pass arguments to the bytecode.Parrot_PackFile PackFile_new_dummy(Parrot_Interp interp, char *name)
void Parrot_load_bytecode(Parrot_Interp interp, const char *path)
path
. You should create a dummy packfile beforehand; see PackFile_new_dummy
for details. Due to the void return type, the behavior of this function on error is unclear.int Parrot_PMC_typenum(Parrot_Interp interp, const char *type)
type
. Useful for instantiating various Parrot data types.char *string_to_cstring(Parrot_Interp interp)
STRING *string_from_cstring(Parrot_Interp interp, const char *string, int len)
string_from_literal(Parrot_Interp interp, const char *string)
string_from_cstring
.Parrot_PMC Parrot_PMC_new(Parrot_Interp interp, int typenum)
typenum
. Use Parrot_PMC_typenum
to obtain the correct type number.void Parrot_register_pmc(Parrot_PMC pmc)
void Parrot_unregister_pmc(Parrot_PMC pmc)
Parrot_PMC Parrot_find_global_cur(Parrot_Interp interp, Parrot_String name)
name
in the current namespace. Returns PMCNULL
if not found.Parrot_PMC Parrot_find_global_n(Parrot_Interp interp, Parrot_String namespace, Parrot_String name)
Parrot_PMC Parrot_find_global_s(Parrot_Interp interp, Parrot_String namespace, Parrot_String name)
name
in the namespace namespace
. Returns PMCNULL
if not found.void Parrot_store_global_cur(Parrot_Interp interp, Parrot_String name, Parrot_PMC val)
name
in the current namespace. Does nothing if the global is not found.void Parrot_store_global_n(Parrot_Interp interp, Parrot_String namespace, Parrot_String name, Parrot_PMC val)
void Parrot_store_global_s(Parrot_Interp interp, Parrot_String namespace, Parrot_String name, Parrot_PMC val)
name
in the namespace namespace
. Does nothing if the global is not found.Parrot_PMC Parrot_find_global_k(Parrot_Interp interp, Parrot_PMC namespace_key, Parrot_String name)
name
in the keyed namespace namespace
. Returns PMCNULL
if not found.void Parrot_store_global_k(Parrot_Interp interp, Parrot_PMC namespace_key, Parrot_String name, Parrot_PMC val)
name
in the keyed namespace namespace
. Does nothing if the global is not found.Not documented yet.
void *Parrot_call_sub(Parrot_Interp interp, Parrot_PMC sub, const_char *signature)
Parrot_Int Parrot_call_sub_ret_int(Parrot_Interp interp, Parrot_PMC sub, const_char *signature)
Parrot_Float Parrot_call_sub_ret_float(Parrot_Interp interp, Parrot_PMC sub, const_char *signature)
Parrot_PMC Parrot_oo_get_class(Parrot_Interp interp, Parrot_PMC namespace)
Parrot_PMC Parrot_Class_instantiate(Parrot_Interp interp, Parrot_PMC the_class Parrot_PMC arg)
the_class
, which can be obtained from Parrot_oo_get_class()
. Passes an optional PMC argument arg
to the constructor (see init versus init_pmc). Use PMCNULL
if you are not supplying an argument.Not documented yet.
Note: This section is aimed at you if you are writing an application external to parrot which links against an installed parrot library.
Several API functions are missing prototypes in Parrot's header files. This means you may receive type warnings during compilation even though the types of your arguments and return variables are correct. In this case it is safe to cast to the correct type; not doing so may cause undesired behavior.
Your application will need to include the appropriate header files and link against parrot and its dependencies.
Because the location of these files can vary from platform to platform, and build to build, a general method is provided to find out the necessary flags to use.
pkg-config is a helper tool, now common on many platforms, which many packages have adopted to provide the necessary compiler and linker flags required to build against a library. parrot will install a file called parrot.pc which can be queried using pkg-config.
To start with, find out what version of parrot is installed by running pkg-config with the --modversion
flag. If this command fails with an error, skip to the end of this section.
pkg-config --modversion parrot
To find out the necessary -I
flags, use --cflags
:
pkg-config --cflags parrot
... and to find the necessary -L
and -l
flags, use --libs
:
pkg-config --libs parrot
Where both compiling and linking are performed in one step, query both sets of flags with:
pkg-config --cflags --libs parrot
The pkg-config command can be incorporated with a compile as shown here.
cc src/disassemble.c `pkg-config --cflags --libs parrot`
Most applications will probably choose to run pkg-config as part of a configure script, so if you are using autoconf you could use a test such as this.
PARROT_REQUIRED_VERSION=0.4.1
AC_SUBST(PARROT_REQUIRED_VERSION)
PKG_CHECK_MODULES(PARROT, parrot >= $PARROT_REQUIRED_VERSION,
[AC_DEFINE([HAVE_PARROT], 1, [define if have parrot])])
AC_SUBST(PARROT_LIBS)
AC_SUBST(PARROT_CFLAGS)
If parrot has been installed system-wide, then any of the previous lines should have returned the relevant flags. If it is not installed in one of the standard places that pkg-config looks, then you will get an error message.
pkg-config --libs parrot
Package parrot was not found in the pkg-config search path.
Perhaps you should add the directory containing `parrot.pc'
to the PKG_CONFIG_PATH environment variable
No package 'parrot' found
As stated in the error message, use an environment variable to make pkg-config look in more locations.
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
The last part of the variable will almost certainly be .../lib/pkgconfig. Set this variable in your login scripts if you need it to be available in future.
#include <parrot/parrot.h>
#include <parrot/embed.h>
#include <parrot/extend.h>
int main(int argc, char *argv[])
{
Parrot_Interp interp;
Parrot_PackFile pf;
Parrot_PMC sub;
Parrot_String pstr;
interp = Parrot_new(NULL);
imcc_init(interp);
/* create a new packfile -- any name will do */
pf = PackFile_new_dummy(interp, "my-parrot-code");
pstr = string_from_literal(interp, "foo.pir");
Parrot_load_bytecode(interp, pstr);
/* find the subroutine named "foo" in the global namespace */
pstr = string_from_literal(interp, "foo");
sub = Parrot_find_global_cur(interp, pstr);
/* run foo(), which returns nothing */
Parrot_call_sub(interp, sub, "v");
Parrot_destroy(interp);
return(0);
}
src/main.c and t/src/*.t for Parrot's use of the embedding system.
http://pkgconfig.freedesktop.org/wiki/ A pkg-config page
|