Skip to content

Commit

Permalink
[Lua]
Browse files Browse the repository at this point in the history
- refactor IO with lua_newuserdata & lua_getmetatable

git-svn-id: https://svn.parrot.org/parrot/trunk@26443 d31e2699-5ff4-0310-a27c-f18f2fbe73fe
  • Loading branch information
fperrad committed Mar 17, 2008
1 parent 2d1555f commit e05914f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 34 deletions.
40 changes: 12 additions & 28 deletions languages/lua/lib/luaio.pir
Original file line number Diff line number Diff line change
Expand Up @@ -149,29 +149,23 @@ L<http://www.lua.org/manual/5.1/manual.html#5.7>.

set $P1, 'stdin'
$P2 = getstdin
new $P0, 'LuaUserdata'
setattribute $P0, 'data', $P2
$P0.'set_metatable'(mt)
$P0 = lua_newuserdata($P2, mt)
$P0.'setfenv'(env)
io[$P1] = $P0
set $P3, IO_INPUT
io_env[$P3] = $P0

set $P1, 'stdout'
$P2 = getstdout
new $P0, 'LuaUserdata'
setattribute $P0, 'data', $P2
$P0.'set_metatable'(mt)
$P0 = lua_newuserdata($P2, mt)
$P0.'setfenv'(env)
io[$P1] = $P0
set $P3, IO_OUTPUT
io_env[$P3] = $P0

set $P1, 'stderr'
$P2 = getstderr
new $P0, 'LuaUserdata'
setattribute $P0, 'data', $P2
$P0.'set_metatable'(mt)
$P0 = lua_newuserdata($P2, mt)
$P0.'setfenv'(env)
io[$P1] = $P0
.end
Expand Down Expand Up @@ -243,14 +237,10 @@ L<http://www.lua.org/manual/5.1/manual.html#5.7>.


.sub 'newfile' :anon
.param pmc f
.local pmc file
new file, 'LuaUserdata'
.local pmc _lua__REGISTRY
_lua__REGISTRY = get_hll_global '_REGISTRY'
.const .LuaString key = 'ParrotIO'
.local pmc mt
mt = _lua__REGISTRY[key]
file.'set_metatable'(mt)
$P0 = lua_getmetatable('ParrotIO')
file = lua_newuserdata(f, $P0)
.return (file)
.end

Expand Down Expand Up @@ -473,8 +463,7 @@ error code.
unless null f goto L3
lua_argerror(1, file)
L3:
$P0 = newfile()
setattribute $P0, 'data', f
$P0 = newfile(f)
setiofile(IO_INPUT, $P0)
goto L1
L2:
Expand Down Expand Up @@ -521,8 +510,7 @@ input file. In this case it does not close the file when the loop ends.
unless null f goto L2
lua_argerror(1, $S1)
L2:
file = newfile()
setattribute file, 'data', f
file = newfile(f)
.return aux_lines(file, 1)
.end

Expand Down Expand Up @@ -581,8 +569,7 @@ in the standard C function C<fopen>.
if $S0 == '' goto L1
f = open $S1, $S0
unless f goto L1
res = newfile()
setattribute res, 'data', f
res = newfile(f)
.return (res)
L1:
new res, 'LuaNil'
Expand Down Expand Up @@ -616,8 +603,7 @@ Similar to C<io.input>, but operates over the default output file.
unless null f goto L3
lua_argerror(1, file)
L3:
$P0 = newfile()
setattribute $P0, 'data', f
$P0 = newfile(f)
setiofile(IO_OUTPUT, $P0)
goto L1
L2:
Expand Down Expand Up @@ -651,8 +637,7 @@ This function is system dependent and is not available on all platforms.
if $S0 == '' goto L1
f = open $S1, $S0
unless f goto L1
res = newfile()
setattribute res, 'data', f
res = newfile(f)
.return (res)
L1:
new res, 'LuaNil'
Expand Down Expand Up @@ -697,8 +682,7 @@ it is automatically removed when the program ends.
$S0 = $P0.'tmpname'()
f = open $S0, '+>'
unless f goto L1
res = newfile()
setattribute res, 'data', f
res = newfile(f)
new $P0, 'OS'
$P0.'rm'($S0)
.return (res)
Expand Down
19 changes: 13 additions & 6 deletions languages/lua/pmc/luauserdata.pmc
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,19 @@ typedef struct userdata_t {
#define u_env(pmc) (PMC_data_typed((pmc), userdata_t *))->env

static PMC* curr_func(PARROT_INTERP) {
parrot_context_t *ctx = CONTEXT(interp->ctx);
PMC *sub = ctx->current_sub;
PMC *cont = ctx->current_cont;
ctx = PMC_cont(cont)->to_ctx;
sub = ctx->current_sub;
return sub;
parrot_context_t *sub_ctx = CONTEXT(interp->ctx)->caller_ctx;
while (1) {
PMC *cont;
if (sub_ctx->current_sub && PMC_metadata(sub_ctx->current_sub))
return sub_ctx->current_sub;
cont = sub_ctx->current_cont;
if (!cont)
break;
sub_ctx = PMC_cont(cont)->to_ctx;
if (!sub_ctx)
break;
}
return NULL;
}

static PMC* getcurrenv(PARROT_INTERP) {
Expand Down

0 comments on commit e05914f

Please sign in to comment.