Skip to content
This repository has been archived by the owner on Jun 9, 2018. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
now use ATTR in LuaFunction PMC
  • Loading branch information
fperrad committed Feb 27, 2009
1 parent 67beb7a commit 2f28f0c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 22 deletions.
1 change: 1 addition & 0 deletions src/pmc/lua_private.h
Expand Up @@ -19,6 +19,7 @@ extern INTVAL dynpmc_LuaUserdata;
#define PMC_type(pmc) ((pmc)->vtable->base_type)

extern PMC * _LuaAny_find_meth(PARROT_INTERP, PMC *obj, const char *name);
extern PMC * _LuaFunction_get_environment(PARROT_INTERP, PMC *obj);
extern PMC * _LuaString_get_metatable(PARROT_INTERP);
extern PMC * _LuaTable_get_metatable(PARROT_INTERP, PMC *obj);
extern PMC * _LuaUserdata_get_metatable(PARROT_INTERP, PMC *obj);
Expand Down
77 changes: 58 additions & 19 deletions src/pmc/luafunction.pmc
Expand Up @@ -19,6 +19,13 @@ with the behaviour of the Lua C<Function> type.

#include "lua_private.h"

#define f_env(pmc) (PARROT_LUAFUNCTION(pmc))->env
#define f_sub(pmc) (PARROT_LUAFUNCTION(pmc))->sub

PMC *
_LuaFunction_get_environment(PARROT_INTERP, PMC *obj) {
return f_env(obj);
}

pmclass LuaFunction
extends Sub
Expand All @@ -31,6 +38,28 @@ pmclass LuaFunction
hll lua
maps Sub {

ATTR PMC *env;

/*

=item C<void init()>

Initializes the function.

=cut

*/
VTABLE void init() {
Parrot_LuaFunction_attributes *attrs =
mem_allocate_zeroed_typed(Parrot_LuaFunction_attributes);

attrs->sub = mem_allocate_zeroed_typed(Parrot_sub);
attrs->sub->seg = INTERP->code;
attrs->env = NULL;
PMC_data(SELF) = attrs;
PObj_custom_mark_destroy_SETALL(SELF);
}

/*

=item C<void init_pmc(PMC *sub)>
Expand All @@ -40,12 +69,15 @@ pmclass LuaFunction
*/
VTABLE void init_pmc(PMC *sub) {
if (VTABLE_isa(INTERP, sub, Parrot_str_new_constant(INTERP, "Sub"))) {
PMC_struct_val(SELF) = mem_allocate_zeroed_typed(Parrot_sub);
PMC_pmc_val(SELF) = NULL;
PMC_metadata(SELF) = NULL;
Parrot_LuaFunction_attributes *attrs =
mem_allocate_zeroed_typed(Parrot_LuaFunction_attributes);

attrs->sub = mem_allocate_zeroed_typed(Parrot_sub);
attrs->env = NULL;
PMC_data(SELF) = attrs;
PObj_custom_mark_destroy_SETALL(SELF);
/* copy the sub struct */
memcpy(PMC_sub(SELF), PMC_sub(sub), sizeof (Parrot_sub));
memcpy(attrs->sub, f_sub(sub), sizeof (Parrot_sub));
}
else
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_INVALID_OPERATION,
Expand All @@ -63,8 +95,8 @@ Marks the function as live.
*/
VTABLE void mark() {
SUPER();
if (PMC_metadata(SELF))
pobject_lives(INTERP, (PObj *)PMC_metadata(SELF));
if (f_env(SELF))
pobject_lives(INTERP, (PObj *)f_env(SELF));
}

/*
Expand All @@ -89,7 +121,7 @@ Return the string "function".
*/
VTABLE PMC* clone() {
PMC* ret = SUPER();
PMC_metadata(ret) = PMC_metadata(SELF);
f_env(ret) = f_env(SELF);
return ret;
}

Expand All @@ -112,8 +144,8 @@ Return the string "function".

*/
VTABLE void set_pmc(PMC *value) {
PMC_struct_val(SELF) = PMC_struct_val(value);
PMC_metadata(SELF) = PMC_metadata(value);
SUPER(value);
f_env(SELF) = f_env(value);
}

/*
Expand All @@ -130,8 +162,10 @@ Return the string "function".

*/
MULTI INTVAL is_equal(LuaFunction value) {
return (PMC_sub(SELF))->start_offs == (PMC_sub(value))->start_offs
&& (PMC_sub(SELF))->seg == (PMC_sub(value))->seg;
Parrot_sub * const my_sub = f_sub(SELF);
Parrot_sub * const value_sub = f_sub(value);
return my_sub->start_offs == value_sub->start_offs
&& my_sub->seg == value_sub->seg;
}

MULTI INTVAL is_equal(DEFAULT value) {
Expand All @@ -150,7 +184,7 @@ Return the string "function".

*/
METHOD STRING *get_name() {
const Parrot_sub * const sub = PMC_sub(SELF);
Parrot_sub * const sub = f_sub(SELF);
STRING * const retval = Parrot_str_copy(INTERP, sub->name);
RETURN(STRING *retval);
}
Expand All @@ -163,7 +197,7 @@ Return the string "function".

*/
METHOD PMC* getfenv() {
PMC *retval = PMC_metadata(SELF);
PMC *retval = f_env(SELF);

if (!retval)
retval = pmc_new(INTERP, dynpmc_LuaNil);
Expand All @@ -179,11 +213,16 @@ Return the string "function".

*/
METHOD PMC* rawequal(PMC *value) {
const INTVAL b = (PMC_type(SELF) == PMC_type(value)
&& (PMC_sub(SELF))->start_offs == (PMC_sub(value))->start_offs
&& (PMC_sub(SELF))->seg == (PMC_sub(value))->seg)
? 1 : 0;
PMC * const retval = pmc_new(INTERP, dynpmc_LuaBoolean);
INTVAL b = 0;
PMC *retval;
if (PMC_type(SELF) == PMC_type(value)) {
Parrot_sub * const my_sub = f_sub(SELF);
Parrot_sub * const value_sub = f_sub(value);
b = (my_sub->start_offs == value_sub->start_offs
&& my_sub->seg == value_sub->seg)
? 1 : 0;
}
retval = pmc_new(INTERP, dynpmc_LuaBoolean);
VTABLE_set_integer_native(INTERP, retval, b);
RETURN(PMC *retval);
}
Expand All @@ -196,7 +235,7 @@ Return the string "function".

*/
METHOD void setfenv(PMC *env) {
PMC_metadata(SELF) = env;
f_env(SELF) = env;
}

}
Expand Down
2 changes: 1 addition & 1 deletion src/pmc/luathread.pmc
Expand Up @@ -32,7 +32,7 @@ static PMC* getcurrenv(PARROT_INTERP) {
PMC *env = NULL;
PMC * const sub = curr_func(interp);
if (sub) {
env = PMC_metadata(sub);
env = _LuaFunction_get_environment(interp, sub);
}
return env;
}
Expand Down
5 changes: 3 additions & 2 deletions src/pmc/luauserdata.pmc
Expand Up @@ -32,7 +32,8 @@ static PMC* curr_func(PARROT_INTERP) {
Parrot_Context *sub_ctx = CONTEXT(interp)->caller_ctx;
while (1) {
PMC *cont;
if (sub_ctx->current_sub && PMC_metadata(sub_ctx->current_sub))
if (sub_ctx->current_sub
&& _LuaFunction_get_environment(interp, sub_ctx->current_sub))
return sub_ctx->current_sub;
cont = sub_ctx->current_cont;
if (!cont)
Expand All @@ -47,7 +48,7 @@ static PMC* curr_func(PARROT_INTERP) {
static PMC* getcurrenv(PARROT_INTERP) {
PMC * const sub = curr_func(interp);
if (sub) {
return PMC_metadata(sub);
return _LuaFunction_get_environment(interp, sub);
}
return NULL;
}
Expand Down

0 comments on commit 2f28f0c

Please sign in to comment.