tagged release 0.7.1
[parrot.git] / languages / lua / src / pmc / luaclosure.pmc
blob02c5f6f937ddc4f2e41ff2b9a2097423d1c2afce
1 /*
2 Copyright (C) 2006-2008, The Perl Foundation.
3 $Id$
5 =head1 NAME
7 pmc/luaclosure.pmc - Lua Closure
9 =head1 DESCRIPTION
11 C<LuaClosure> extends C<Parrot Closure> and C<LuaAny> to provide a class with
12 the behaviour of the Lua C<Function> type.  C<LuaClosure> is used by functions
13 written in Lua.
15 See also: F<languages/lua/pmc/luafunction.pmc>
17 =head2 Overloaded Methods
19 =over 4
21 =cut
25 #include "lua_private.h"
28 pmclass LuaClosure
29     extends  Closure
30     extends  LuaAny
31     provides scalar
32     provides sub
33     dynpmc
34     need_ext
35     group    lua_group
36     hll      Lua
37     maps     Closure {
41 =item C<void init_pmc(PMC *sub)>
43 =cut
46     void init_pmc(PMC *sub) {
47         if (VTABLE_isa(INTERP, sub, const_string(INTERP, "Closure"))) {
48             PMC_struct_val(SELF) = mem_allocate_zeroed_typed(Parrot_sub);
49             PMC_pmc_val(SELF)    = NULL;
50             PMC_metadata(SELF)   = NULL;
51             PObj_custom_mark_destroy_SETALL(SELF);
52             /* copy the sub struct */
53             memcpy(PMC_sub(SELF), PMC_sub(sub), sizeof (Parrot_sub));
54         }
55         else
56             Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_INVALID_OPERATION,
57                 "not a Closure (%Ss)", SELF->vtable->whoami);
58     }
62 =item C<void mark()>
64 Marks the closure as live.
66 =cut
69     void mark() {
70         SUPER();
71         if (PMC_metadata(SELF))
72             pobject_lives(INTERP, (PObj *)PMC_metadata(SELF));
73     }
77 =item C<STRING* name()>
79 Return the string "function".
81 =cut
84     STRING* name() {
85         return const_string(INTERP, "function");
86     }
90 =item C<PMC *clone()>
92 =cut
95     PMC* clone() {
96         PMC *ret = SUPER();
97         PMC_metadata(ret) = PMC_metadata(SELF);
98         PObj_custom_mark_destroy_SETALL(ret);
99         return ret;
100     }
104 =item C<STRING* get_string()>
106 =cut
109     STRING* get_string() {
110         return Parrot_sprintf_c(INTERP, "function: %08X", SELF);
111     }
115 =item C<void set_pmc(PMC *value)>
117 =cut
120     void set_pmc(PMC *value) {
121         PMC_struct_val(SELF) = PMC_struct_val(value);
122         PMC_metadata(SELF)   = PMC_metadata(value);
123     }
127 =item C<INTVAL is_equal(PMC* value)>
129 =cut
132     INTVAL is_equal(PMC* value) {
133 MMD_LuaClosure: {
134             return (PMC_sub(SELF))->start_offs == (PMC_sub(value))->start_offs
135                 && (PMC_sub(SELF))->seg        == (PMC_sub(value))->seg;
136         }
137 MMD_DEFAULT: {
138             return (INTVAL)0;
139         }
140     }
144 =back
146 =head2 Specific Methods
148 =over 4
150 =item C<PMC *getfenv()>
152 =cut
155     METHOD PMC* getfenv() {
156         PMC *retval = PMC_metadata(SELF);
158         if (!retval)
159             retval = pmc_new(INTERP, dynpmc_LuaNil);
161         RETURN(PMC *retval);
162     }
166 =item C<PMC* rawequal(PMC* value)>
168 =cut
171     METHOD PMC* rawequal(PMC* value) {
172         PMC *retval = pmc_new(INTERP, dynpmc_LuaBoolean);
174         if (PMC_type(SELF)             == PMC_type(value)
175         && (PMC_sub(SELF))->start_offs == (PMC_sub(value))->start_offs
176         && (PMC_sub(SELF))->seg        == (PMC_sub(value))->seg)
177             VTABLE_set_integer_native(INTERP, retval, 1);
178         else
179             VTABLE_set_integer_native(INTERP, retval, 0);
181         RETURN(PMC *retval);
182     }
186 =item C<void setfenv(PMC *env)>
188 =cut
191     METHOD void setfenv(PMC *env) {
192         PMC_metadata(SELF) = env;
193     }
199 =back
201 =head1 AUTHORS
203 Francois Perrad
205 =cut
210  * Local variables:
211  *   c-file-style: "parrot"
212  * End:
213  * vim: expandtab shiftwidth=4:
214  */