tagged release 0.6.4
[parrot.git] / languages / lua / src / pmc / luathread.pmc
blob3a0b530852673c02022790ace73c816c52f46aad
1 /*
2 Copyright (C) 2005-2008, The Perl Foundation.
3 $Id$
5 =head1 NAME
7 pmc/luathread.pmc - Lua Thread
9 =head1 DESCRIPTION
11 C<LuaThread> extends C<LuaAny> to provide a class with the behaviour
12 of the Lua C<Thread> type.
13 This implementation is based on C<Parrot::Coroutine>.
15 =head2 Overloaded Methods
17 =over 4
19 =cut
23 #include "lua_private.h"
25 static PMC* curr_func(PARROT_INTERP) {
26     parrot_context_t *ctx = CONTEXT(interp);
27     PMC *sub = ctx->current_sub;
28     return sub;
31 static PMC* getcurrenv(PARROT_INTERP) {
32     PMC *env = NULL;
33     PMC *sub = curr_func(interp);
34     if (sub) {
35         env = PMC_metadata(sub);
36     }
37     return env;
41 pmclass LuaThread
42     extends LuaAny
43     dynpmc
44     need_ext
45     group lua_group
46     hll Lua {
50 =item C<void init()>
52 Raises an exception. Use C<init_pmc()>.
54 =cut
57     void init() {
58         real_exception(INTERP, NULL, E_Exception,
59                        "LuaThread init without sub");
60     }
64 =item C<void init_pmc(PMC *sub)>
66 =cut
69     void init_pmc(PMC *sub) {
70         PMC *classobj = Parrot_oo_get_class_str(INTERP,
71                           const_string(INTERP, "Parrot::Coroutine"));
72         PMC *init_args;
74         if (PMC_IS_NULL(classobj))
75             real_exception(INTERP, NULL, E_Exception,
76                            "Parrot::Coroutine not found");
78         init_args = pmc_new(INTERP, enum_class_Hash);
79         VTABLE_set_pmc_keyed_str(INTERP, init_args,
80                                  const_string(INTERP, "initial_sub"), sub);
81         PMC_pmc_val(SELF) = VTABLE_instantiate(INTERP, classobj, init_args);
82         PMC_metadata(SELF) = getcurrenv(INTERP);
83         PObj_custom_mark_SET(SELF);
84     }
88 =item C<void mark()>
90 Marks the Parrot::Coroutine as live.
92 =cut
95     void mark() {
96         if (PMC_pmc_val(SELF))
97             pobject_lives(INTERP, (PObj *)PMC_pmc_val(SELF));
98         if (PMC_metadata(SELF))
99             pobject_lives(INTERP, (PObj *)PMC_metadata(SELF));
100     }
104 =item C<STRING* name()>
106 Return the string "thread".
108 =cut
111     STRING* name() {
112         return const_string(INTERP, "thread");
113     }
117 =item C<PMC* clone()>
119 =cut
122     PMC* clone() {
123         return SELF;
124     }
128 =item C<PMC* get_attr_str(STRING *key)>
130 =cut
133     PMC* get_attr_str(STRING *key) {
134         return PMC_pmc_val(SELF);
135     }
139 =item C<STRING* get_string()>
141 =cut
144     STRING* get_string() {
145         return Parrot_sprintf_c(INTERP, "thread: %08X", SELF);
146     }
150 =item C<void set_pmc(PMC *value)>
152 =cut
155     void set_pmc(PMC *value) {
156         PMC_pmc_val(SELF) = PMC_pmc_val(value);
157     }
161 =back
163 =head2 non-Vtable Methods
165 =over 4
167 =item C<INTVAL is_equal(PMC *value)>
169 =cut
172     INTVAL is_equal(PMC *value) {
173 MMD_LuaThread: {
174             if (SELF == value)
175                 return (INTVAL)1;
176             else
177                 return (INTVAL)0;
178         }
179 MMD_DEFAULT: {
180             return (INTVAL)0;
181         }
182     }
186 =back
188 =head2 Specific Methods
190 =over 4
192 =item C<PMC *getfenv()>
194 =cut
197     METHOD PMC* getfenv() {
198         PMC *retval = PMC_metadata(SELF);
200         if (!retval)
201             retval = pmc_new(INTERP, dynpmc_LuaNil);
203         RETURN(PMC *retval);
204     }
208 =item C<PMC* rawequal(PMC *value)>
210 =cut
213     METHOD PMC* rawequal(PMC *value) {
214         PMC *retval = pmc_new(INTERP, dynpmc_LuaBoolean);
216         if (PMC_type(SELF) == PMC_type(value) && SELF == value)
217             VTABLE_set_integer_native(INTERP, retval, 1);
218         else
219             VTABLE_set_integer_native(INTERP, retval, 0);
221         RETURN(PMC *retval);
222     }
226 =item C<void setfenv(PMC *env)>
228 =cut
231     METHOD void setfenv(PMC *env) {
232         PMC_metadata(SELF) = env;
233     }
239 =back
241 =head1 AUTHORS
243 Francois Perrad
245 =cut
250  * Local variables:
251  *   c-file-style: "parrot"
252  * End:
253  * vim: expandtab shiftwidth=4:
254  */