Author: fperrad
Date: Wed Apr 25 00:22:00 2007
New Revision: 18331

Modified:
   trunk/languages/lua/lib/luaio.pir
   trunk/languages/lua/t/io.t

Log:
[Lua]
- fix the use of environment in io library

Modified: trunk/languages/lua/lib/luaio.pir
==============================================================================
--- trunk/languages/lua/lib/luaio.pir   (original)
+++ trunk/languages/lua/lib/luaio.pir   Wed Apr 25 00:22:00 2007
@@ -40,13 +40,19 @@
 
 #    print "init Lua I/O\n"
 
-    .local pmc _file
-    _file = createmeta()
-
     .local pmc _lua__GLOBAL
     _lua__GLOBAL = global '_G'
     new $P1, .LuaString
 
+    .local pmc _io_env
+    new _io_env, .LuaTable
+
+    .local pmc _popen_env
+    new _popen_env, .LuaTable
+
+    .local pmc _file
+    _file = createmeta(_io_env)
+
     .local pmc _io
     new _io, .LuaTable
     set $P1, 'io'
@@ -55,79 +61,84 @@
     _register($P1, _io)
 
     .const .Sub _io_close = '_io_close'
-    _io_close.'setfenv'(_lua__GLOBAL)
+    _io_close.'setfenv'(_io_env)
     set $P1, 'close'
     _io[$P1] = _io_close
 
     .const .Sub _io_flush = '_io_flush'
-    _io_flush.'setfenv'(_lua__GLOBAL)
+    _io_flush.'setfenv'(_io_env)
     set $P1, 'flush'
     _io[$P1] = _io_flush
 
     .const .Sub _io_input = '_io_input'
-    _io_input.'setfenv'(_lua__GLOBAL)
+    _io_input.'setfenv'(_io_env)
     set $P1, 'input'
     _io[$P1] = _io_input
 
     .const .Sub _io_lines = '_io_lines'
-    _io_lines.'setfenv'(_lua__GLOBAL)
+    _io_lines.'setfenv'(_io_env)
     set $P1, 'lines'
     _io[$P1] = _io_lines
 
     .const .Sub _io_open = '_io_open'
-    _io_open.'setfenv'(_lua__GLOBAL)
+    _io_open.'setfenv'(_io_env)
     set $P1, 'open'
     _io[$P1] = _io_open
 
     .const .Sub _io_output = '_io_output'
-    _io_output.'setfenv'(_lua__GLOBAL)
+    _io_output.'setfenv'(_io_env)
     set $P1, 'output'
     _io[$P1] = _io_output
 
     .const .Sub _io_popen = '_io_popen'
-    _io_popen.'setfenv'(_lua__GLOBAL)
+    _io_popen.'setfenv'(_popen_env)
     set $P1, 'popen'
     _io[$P1] = _io_popen
 
     .const .Sub _io_read = '_io_read'
-    _io_read.'setfenv'(_lua__GLOBAL)
+    _io_read.'setfenv'(_io_env)
     set $P1, 'read'
     _io[$P1] = _io_read
 
     .const .Sub _io_tmpfile = '_io_tmpfile'
-    _io_tmpfile.'setfenv'(_lua__GLOBAL)
+    _io_tmpfile.'setfenv'(_io_env)
     set $P1, 'tmpfile'
     _io[$P1] = _io_tmpfile
 
     .const .Sub _io_type = '_io_type'
-    _io_type.'setfenv'(_lua__GLOBAL)
+    _io_type.'setfenv'(_io_env)
     set $P1, 'type'
     _io[$P1] = _io_type
 
     .const .Sub _io_write = '_io_write'
-    _io_write.'setfenv'(_lua__GLOBAL)
+    _io_write.'setfenv'(_io_env)
     set $P1, 'write'
     _io[$P1] = _io_write
 
+    .const .Sub _readline = 'readline'
+    _readline.'setfenv'(_io_env)
 
-    .local pmc _lua__ENVIRON
-    _lua__ENVIRON = new .LuaTable
-    global '_ENVIRON' = _lua__ENVIRON
 
+    # set default close function
     .const .Sub _io_fclose = '_io_fclose'
-    _io_fclose.'setfenv'(_lua__GLOBAL)
+    _io_fclose.'setfenv'(_io_env)
     set $P1, '__close'
-    _lua__ENVIRON[$P1] = _io_fclose
+    _io_env[$P1] = _io_fclose
 
-    createstdfiles(_file, _io, _lua__ENVIRON)
+    # create (and set) default files
+    createstdfiles(_file, _io, _io_env)
+
+    # environment for 'popen'
+    .const .Sub _io_pclose = '_io_pclose'
+    _io_pclose.'setfenv'(_io_env)
+    set $P1, '__close'
+    _popen_env[$P1] = _io_pclose
 
 .end
 
 
 .sub 'createmeta' :anon
-    .local pmc _lua__GLOBAL
-    _lua__GLOBAL = global '_G'
-
+    .param pmc env
     .local pmc _file
     _file = newmetatable('ParrotIO')
 
@@ -136,47 +147,47 @@
     _file[$P1] = _file
 
     .const .Sub _file_close = '_io_close'
-    _file_close.'setfenv'(_lua__GLOBAL)
+    _file_close.'setfenv'(env)
     set $P1, 'close'
     _file[$P1] = _file_close
 
     .const .Sub _file_flush = '_file_flush'
-    _file_flush.'setfenv'(_lua__GLOBAL)
+    _file_flush.'setfenv'(env)
     set $P1, 'flush'
     _file[$P1] = _file_flush
 
     .const .Sub _file_lines = '_file_lines'
-    _file_lines.'setfenv'(_lua__GLOBAL)
+    _file_lines.'setfenv'(env)
     set $P1, 'lines'
     _file[$P1] = _file_lines
 
     .const .Sub _file_read = '_file_read'
-    _file_read.'setfenv'(_lua__GLOBAL)
+    _file_read.'setfenv'(env)
     set $P1, 'read'
     _file[$P1] = _file_read
 
     .const .Sub _file_seek = '_file_seek'
-    _file_seek.'setfenv'(_lua__GLOBAL)
+    _file_seek.'setfenv'(env)
     set $P1, 'seek'
     _file[$P1] = _file_seek
 
     .const .Sub _file_setvbuf = '_file_setvbuf'
-    _file_setvbuf.'setfenv'(_lua__GLOBAL)
+    _file_setvbuf.'setfenv'(env)
     set $P1, 'setvbuf'
     _file[$P1] = _file_setvbuf
 
     .const .Sub _file_write = '_file_write'
-    _file_write.'setfenv'(_lua__GLOBAL)
+    _file_write.'setfenv'(env)
     set $P1, 'write'
     _file[$P1] = _file_write
 
     .const .Sub _file__gc = '_file__gc'
-    _file__gc.'setfenv'(_lua__GLOBAL)
+    _file__gc.'setfenv'(env)
     set $P1, '__gc'
     _file[$P1] = _file__gc
 
     .const .Sub _file__tostring = '_file__tostring'
-    _file__tostring.'setfenv'(_lua__GLOBAL)
+    _file__tostring.'setfenv'(env)
     set $P1, '__tostring'
     _file[$P1] = _file__tostring
 
@@ -224,13 +235,15 @@
 
 .sub 'getiofile' :anon
     .param int findex
-    .local pmc _lua__ENVIRON
+    .local pmc env
     .local pmc io
     .local pmc file
-    _lua__ENVIRON = global '_ENVIRON'
+    $P0 = getinterp
+    $P1 = $P0['sub'; 1]
+    env = getfenv($P1)
     new io, .LuaNumber
     set io, findex
-    file = _lua__ENVIRON[io]
+    file = env[io]
     .return (file)
 .end
 
@@ -344,12 +357,14 @@
 .sub 'setiofile' :anon
     .param int findex
     .param pmc file
-    .local pmc _lua__ENVIRON
+    .local pmc env
     .local pmc io
-    _lua__ENVIRON = global '_ENVIRON'
+    $P0 = getinterp
+    $P1 = $P0['sub'; 1]
+    env = getfenv($P1)
     new io, .LuaNumber
     set io, findex
-    _lua__ENVIRON[io] = file
+    env[io] = file
 .end
 
 
@@ -400,11 +415,13 @@
 
 .sub 'aux_close' :anon
     .param pmc file
-    # TODO: getfenv
-    $P0 = global '_ENVIRON'
+    .local pmc env
+    $P0 = getinterp
+    $P1 = $P0['sub'; 1]
+    env = getfenv($P1)
     new $P1, .LuaString
     set $P1, '__close'
-    $P0 = $P0[$P1]
+    $P0 = env[$P1]
     .return $P0(file)
 .end
 
@@ -450,7 +467,8 @@
     file = getiofile(IO_OUTPUT)
 L1:
     tofile(file)
-    .return aux_close(file)
+    ret = aux_close(file)
+    .return (ret)
 .end
 
 
@@ -484,6 +502,7 @@
 .sub '_io_input' :anon
     .param pmc file :optional
     .local pmc f
+    .local pmc ret
     if null file goto L1
     unless file goto L1
     $I1 = isa file, 'LuaString'
@@ -502,7 +521,8 @@
     setiofile(IO_INPUT, file)
     goto L1
 L1:
-    .return getiofile(IO_INPUT)
+    ret = getiofile(IO_INPUT)
+    .return (ret)
 .end
 
 
@@ -620,6 +640,7 @@
 .sub '_io_output' :anon
     .param pmc file :optional
     .local pmc f
+    .local pmc ret
     if null file goto L1
     unless file goto L1
     $I1 = isa file, 'LuaString'
@@ -637,7 +658,8 @@
     tofile(file)
     setiofile(IO_OUTPUT, file)
 L1:
-    .return getiofile(IO_OUTPUT)
+    ret = getiofile(IO_OUTPUT)
+    .return (ret)
 .end
 
 
@@ -654,6 +676,15 @@
 =cut
 
 .sub '_io_popen' :anon
+    .param pmc prog :optional
+    .param pmc mode :optional
+    $S1 = checkstring(prog)
+    $S2 = optstring(mode, 'r')
+    not_implemented()
+.end
+
+.sub '_io_pclose' :anon
+    .param pmc file
     not_implemented()
 .end
 

Modified: trunk/languages/lua/t/io.t
==============================================================================
--- trunk/languages/lua/t/io.t  (original)
+++ trunk/languages/lua/t/io.t  Wed Apr 25 00:22:00 2007
@@ -27,9 +27,22 @@
 use FindBin;
 use lib "$FindBin::Bin";
 
-use Parrot::Test tests => 38;
+use Parrot::Test tests => 39;
 use Test::More;
 
+language_output_is( 'lua', << 'CODE', << 'OUTPUT', 'io environment' );
+assert(getfenv(io.lines) == _G)
+local env = debug.getfenv(io.lines)
+print(type(env.__close))
+assert(env[1] == io.stdin)
+assert(env[2] == io.stdout)
+env = debug.getfenv(io.popen)
+print(type(env.__close))
+CODE
+function
+function
+OUTPUT
+
 language_output_like( 'lua', << 'CODE', << 'OUTPUT', 'io.stdin' );
 print(io.stdin)
 CODE

Reply via email to