Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
253 lines (175 sloc) 4.29 KB
/*
Copyright (C) 2005-2009, Parrot Foundation.
$Id$
=head1 Lua Function
=head2 Description
C<LuaFunction> extends C<Parrot Sub> and C<LuaAny> to provide a class
with the behaviour of the Lua C<Function> type.
=head3 Overloaded Methods
=over 4
=cut
*/
#include "lua_private.h"
#define f_env(pmc) (PARROT_LUAFUNCTION(pmc))->env
PMC *
_LuaFunction_get_environment(PARROT_INTERP, PMC *obj) {
return f_env(obj);
}
pmclass LuaFunction
extends Sub
extends LuaAny
provides invokable
auto_attrs
dynpmc
group lua_group
hll lua
maps Sub {
ATTR PMC *env;
/*
=item C<void init()>
Initializes the function.
=cut
*/
VTABLE void init() {
Parrot_LuaFunction_attributes *attrs = PARROT_LUAFUNCTION(SELF);
attrs->seg = INTERP->code;
attrs->env = PMCNULL;
PObj_custom_mark_destroy_SETALL(SELF);
}
/*
=item C<void init_pmc(PMC *sub)>
=cut
*/
VTABLE void init_pmc(PMC *sub) {
if (VTABLE_isa(INTERP, sub, Parrot_str_new_constant(INTERP, "Sub"))) {
Parrot_LuaFunction_attributes *attrs = PARROT_LUAFUNCTION(SELF);
/* copy the sub struct */
memcpy(attrs, PMC_data(sub), sizeof (Parrot_Sub_attributes));
attrs->env = PMCNULL;
PObj_custom_mark_destroy_SETALL(SELF);
}
else
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_INVALID_OPERATION,
"not a Sub (%Ss)", sub->vtable->whoami);
}
/*
=item C<void mark()>
Marks the function as live.
=cut
*/
VTABLE void mark() {
SUPER();
if (!PMC_IS_NULL(f_env(SELF)))
Parrot_gc_mark_PMC_alive(INTERP, f_env(SELF));
}
/*
=item C<STRING* name()>
Return the string "function".
=cut
*/
VTABLE STRING* name() {
return Parrot_str_new_constant(INTERP, "function");
}
/*
=item C<PMC *clone()>
=cut
*/
VTABLE PMC* clone() {
PMC* ret = SUPER();
f_env(ret) = f_env(SELF);
return ret;
}
/*
=item C<STRING* get_string()>
=cut
*/
VTABLE STRING* get_string() {
return Parrot_sprintf_c(INTERP, "function: %08X", SELF);
}
/*
=item C<void set_pmc(PMC *value)>
=cut
*/
VTABLE void set_pmc(PMC *value) {
if (PMC_type(SELF) == PMC_type(value)) {
SUPER(value);
f_env(SELF) = f_env(value);
}
else
Parrot_ex_throw_from_c_args(INTERP, NULL,
EXCEPTION_INVALID_OPERATION,
"Can't assign a non-LuaFunction type to a LuaFunction");
}
/*
=back
=head3 non-Vtable Methods
=over 4
=item C<INTVAL is_equal(PMC *value)>
=cut
*/
MULTI INTVAL is_equal(LuaFunction value) {
Parrot_LuaFunction_attributes * const my_sub = PARROT_LUAFUNCTION(SELF);
Parrot_LuaFunction_attributes * const value_sub = PARROT_LUAFUNCTION(value);
return my_sub->start_offs == value_sub->start_offs
&& my_sub->seg == value_sub->seg;
}
MULTI INTVAL is_equal(DEFAULT value) {
return (INTVAL)0;
}
/*
=back
=head3 Specific Methods
=over 4
=cut
*/
METHOD STRING *get_name() {
STRING *name;
GET_ATTR_name(INTERP, SELF, name);
RETURN(STRING *name);
}
/*
=item C<PMC *getfenv()>
=cut
*/
METHOD PMC* getfenv() {
PMC *retval = f_env(SELF);
if (PMC_IS_NULL(retval))
retval = Parrot_pmc_new(INTERP, dynpmc_LuaNil);
RETURN(PMC *retval);
}
/*
=item C<PMC* rawequal(PMC *value)>
=cut
*/
METHOD PMC* rawequal(PMC *value) {
INTVAL b = 0;
PMC *retval;
if (PMC_type(SELF) == PMC_type(value)) {
Parrot_LuaFunction_attributes * const my_sub = PARROT_LUAFUNCTION(SELF);
Parrot_LuaFunction_attributes * const value_sub = PARROT_LUAFUNCTION(value);
b = (my_sub->start_offs == value_sub->start_offs
&& my_sub->seg == value_sub->seg)
? 1 : 0;
}
retval = Parrot_pmc_new(INTERP, dynpmc_LuaBoolean);
VTABLE_set_integer_native(INTERP, retval, b);
RETURN(PMC *retval);
}
/*
=item C<void setfenv(PMC *env)>
=cut
*/
METHOD void setfenv(PMC *env) {
f_env(SELF) = env;
}
}
/*
=back
=cut
*/
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4:
*/
Something went wrong with that request. Please try again.