This repository has been archived by the owner on Jun 9, 2018. It is now read-only.
/
package.t
332 lines (270 loc) · 7.08 KB
/
package.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#! perl
# Copyright (C) 2007-2009, Parrot Foundation.
# $Id$
=head1 Lua Package Library
=head2 Synopsis
% perl t/package.t
=head2 Description
Tests Lua Package Library
(implemented in F<languages/lua/src/lib/luapackage.pir>).
See "Lua 5.1 Reference Manual", section 5.3 "Modules",
L<http://www.lua.org/manual/5.1/manual.html#5.3>.
=cut
use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/../../../lib", "$FindBin::Bin";
use Parrot::Test tests => 16;
use Test::More;
use Parrot::Test::Lua;
my $test_prog = Parrot::Test::Lua::get_test_prog();
language_output_is( 'lua', << 'CODE', << 'OUTPUT', 'function module' );
print(type(mod))
local _G = _G
module("mod")
_G.print(_G.type(_G.mod))
_G.assert(_G.mod == _G.package.loaded.mod)
CODE
nil
table
OUTPUT
language_output_is( 'lua', << 'CODE', << 'OUTPUT', 'function require' );
local m = require "io"
m.write("hello world\n")
CODE
hello world
OUTPUT
unlink("$FindBin::Bin/../complex.lua") if ( -f "$FindBin::Bin/../complex.lua" );
my $X;
open $X, '>', "$FindBin::Bin/../complex.lua";
print {$X} << 'CODE';
complex = {}
function complex.new (r, i) return {r=r, i=i} end
--defines a constant 'i'
complex.i = complex.new(0, 1)
function complex.add (c1, c2)
return complex.new(c1.r + c2.r, c1.i + c2.i)
end
function complex.sub (c1, c2)
return complex.new(c1.r - c2.r, c1.i - c2.i)
end
function complex.mul (c1, c2)
return complex.new(c1.r*c2.r - c1.i*c2.i,
c1.r*c2.i + c1.i*c2.r)
end
local function inv (c)
local n = c.r^2 + c.i^2
return complex.new(c.r/n, -c.i/n)
end
function complex.div (c1, c2)
return complex.mul(c1, inv(c2))
end
return complex
CODE
close $X;
language_output_is( 'lua', << 'CODE', << 'OUTPUT', 'function require' );
m = require "complex"
assert(m == complex)
print(complex.i.r, complex.i.i)
CODE
0 1
OUTPUT
# clean up complex.lua
unlink("$FindBin::Bin/../complex.lua") if ( -f "$FindBin::Bin/../complex.lua" );
language_output_like( 'lua', << 'CODE', << 'OUTPUT', 'function require (no module)' );
require "no_module"
CODE
/^[^:]+: (\w:)?[^:]+:\d+: module 'no_module' not found:\n(\t.*\n)+stack traceback:\n/
OUTPUT
unlink("$FindBin::Bin/../foo.lua") if ( -f "$FindBin::Bin/../foo.lua" );
open $X, '>', "$FindBin::Bin/../foo.lua";
print {$X} '?syntax error?';
close $X;
language_output_like( 'lua', << 'CODE', << 'OUTPUT', 'function require (syntax error)');
require "foo"
CODE
/[^:]+: error loading module 'foo' from file '.*foo.lua':\n/
OUTPUT
# clean up foo.lua
unlink("$FindBin::Bin/../foo.lua") if ( -f "$FindBin::Bin/../foo.lua" );
language_output_is( 'lua', << 'CODE', << 'OUTPUT', 'function require & package.preload' );
foo = {}
foo.bar = 1234
function foo_loader ()
return foo
end
package.preload.foo = foo_loader
m = require 'foo'
assert(m == foo)
print(m.bar)
CODE
1234
OUTPUT
unlink("$FindBin::Bin/../foo.lua") if ( -f "$FindBin::Bin/../foo.lua" );
open $X, '>', "$FindBin::Bin/../foo.lua";
print {$X} 'print("in foo.lua", ...)';
close $X;
language_output_is( 'lua', << 'CODE', << 'OUTPUT', 'function require (arg)' );
require "foo"
CODE
in foo.lua foo
OUTPUT
# clean up foo.lua
unlink("$FindBin::Bin/../foo.lua") if ( -f "$FindBin::Bin/../foo.lua" );
unlink("$FindBin::Bin/../complex.lua") if ( -f "$FindBin::Bin/../complex.lua" );
open $X, '>', "$FindBin::Bin/../complex.lua";
print {$X} << 'CODE';
-- print("complex.lua", ...)
-- module(...)
module("complex")
function new (r, i) return {r=r, i=i} end
--defines a constant 'i'
i = new(0, 1)
function add (c1, c2)
return new(c1.r + c2.r, c1.i + c2.i)
end
function sub (c1, c2)
return new(c1.r - c2.r, c1.i - c2.i)
end
function mul (c1, c2)
return new(c1.r*c2.r - c1.i*c2.i,
c1.r*c2.i + c1.i*c2.r)
end
local function inv (c)
local n = c.r^2 + c.i^2
return new(c.r/n, -c.i/n)
end
function div (c1, c2)
return mul(c1, inv(c2))
end
CODE
close $X;
language_output_is( 'lua', << 'CODE', << 'OUTPUT', 'function require & module' );
require "complex"
print(complex.i.r, complex.i.i)
CODE
0 1
OUTPUT
# clean up complex.lua
unlink("$FindBin::Bin/../complex.lua") if ( -f "$FindBin::Bin/../complex.lua" );
SKIP:
{
skip('only with Parrot', 1) if ($test_prog eq 'lua');
unlink("$FindBin::Bin/../mod_foo.pbc") if ( -f "$FindBin::Bin/../mod_foo.pbc" );
unlink("$FindBin::Bin/../mod_foo.pir") if ( -f "$FindBin::Bin/../mod_foo.pir" );
open $X, '>', "$FindBin::Bin/../mod_foo.pir";
print {$X} <<'PIR';
.HLL 'lua'
.loadlib 'lua_group'
.sub '__onload' :anon :load
# print "__onload mod_foo\n"
.const 'Sub' entry = 'luaopen_mod_foo'
set_root_global 'luaopen_mod_foo', entry
.end
.sub 'luaopen_mod_foo'
# print "luaopen_mod_foo\n"
.local pmc _lua__GLOBAL
_lua__GLOBAL = get_hll_global '_G'
new $P1, 'LuaString'
.local pmc _mod_foo
new _mod_foo, 'LuaTable'
set $P1, 'mod_foo'
_lua__GLOBAL[$P1] = _mod_foo
.const 'Sub' _mod_foo_bar = '_mod_foo_bar'
set $P1, 'bar'
_mod_foo[$P1] = _mod_foo_bar
.return (_mod_foo)
.end
.sub '_mod_foo_bar' :anon
.param pmc extra :slurpy
new $P0, 'LuaNumber'
set $P0, 3.14
.return ($P0)
.end
PIR
close $X;
language_output_is( 'lua', << 'CODE', << 'OUTPUT', 'function require (PIR)' );
assert(nil == mod_foo)
m = require "mod_foo"
assert(m == mod_foo)
assert(m == package.loaded.mod_foo)
print(m.bar())
CODE
3.14
OUTPUT
}
# clean up mod_foo.pbc and/or mod_foo.pir if necessary
unlink("$FindBin::Bin/../mod_foo.pbc") if ( -f "$FindBin::Bin/../mod_foo.pbc" );
unlink("$FindBin::Bin/../mod_foo.pir") if ( -f "$FindBin::Bin/../mod_foo.pir" );
language_output_is( 'lua', << 'CODE', << 'OUTPUT', 'table package.loaded' );
t = {}
for k in pairs(package.loaded) do
table.insert(t, k)
end
table.sort(t)
for k, v in ipairs(t) do
print(v)
end
CODE
_G
coroutine
debug
io
math
os
package
string
table
OUTPUT
language_output_is( 'lua', << 'CODE', << 'OUTPUT', 'table package.path' );
print(type(package.path))
CODE
string
OUTPUT
SKIP:
{
skip('only with Parrot', 3) if ($test_prog eq 'lua');
delete $ENV{LUA_PBCPATH};
language_output_is( 'lua', << 'CODE', << 'OUTPUT', 'table package.pbcpath' );
print(type(package.pbcpath))
print(package.pbcpath)
CODE
string
../languages/lua/library/?.pbc;library/?.pbc;./?.pbc;./?.pir
OUTPUT
$ENV{LUA_PBCPATH} = "?.pbc";
language_output_is( 'lua', << 'CODE', << 'OUTPUT', 'table package.pbcpath' );
print(package.pbcpath)
CODE
?.pbc
OUTPUT
$ENV{LUA_PBCPATH} = ";;languages/lua/?.pbc";
language_output_is( 'lua', << 'CODE', << 'OUTPUT', 'table package.pbcpath' );
print(package.pbcpath)
CODE
;../languages/lua/library/?.pbc;library/?.pbc;./?.pbc;./?.pir;languages/lua/?.pbc
OUTPUT
delete $ENV{LUA_PBCPATH};
}
language_output_is( 'lua', << 'CODE', << 'OUTPUT', 'table package.preload' );
print(type(package.preload))
print(# package.preload)
CODE
table
0
OUTPUT
language_output_is( 'lua', << 'CODE', << 'OUTPUT', 'function package.seeall' );
m = {}
package.seeall(m)
m.print("hello")
CODE
hello
OUTPUT
# clean up temporary files
map { unlink("$FindBin::Bin/../tmp1.$_") if ( -f "$FindBin::Bin/../tmp1.$_" ) } qw(lua pbc pir);
# Local Variables:
# mode: cperl
# cperl-indent-level: 4
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4: