|
36 | 36 | local V = _VERSION:gsub("^.*(%d+)%.(%d+)$", "%1%2") |
37 | 37 | if jit then V = "jit" end |
38 | 38 |
|
39 | | -print( "testing Lua API ..." ) |
| 39 | +local mode = "global" |
| 40 | +if arg[1] == "module" then |
| 41 | + mode = "module" |
| 42 | +end |
| 43 | + |
| 44 | + |
40 | 45 | package.path = "../?.lua;../?/init.lua;"..package.path |
41 | 46 | package.cpath = "./?-"..V..".so;./?-"..V..".dll;./?.so;./?.dll" |
42 | | -require("compat53") |
| 47 | +if mode == "module" then |
| 48 | + print( "testing Lua API using `compat53.module` ..." ) |
| 49 | + _ENV = require("compat53.module") |
| 50 | + if setfenv then setfenv(1, _ENV) end |
| 51 | +else |
| 52 | + print( "testing Lua API using `compat53` ..." ) |
| 53 | + require("compat53") |
| 54 | +end |
43 | 55 |
|
44 | 56 | ___'' |
45 | 57 | do |
46 | | - local t = setmetatable( {}, { __index = { 1, false, "three" } } ) |
| 58 | + local t = setmetatable({}, { __index = { 1, false, "three" } }) |
47 | 59 | for i,v in ipairs(t) do |
48 | 60 | print("ipairs", i, v) |
49 | 61 | end |
|
338 | 350 | print("xpcall()", xpcall(func, debug.traceback, false)) |
339 | 351 | print("xpcall()", xpcall(func, debug.traceback, true)) |
340 | 352 | print("xpcall()", xpcall(func, tb, true)) |
341 | | - local function func2(cb) |
342 | | - print("xpcall()", xpcall(cb, debug.traceback, "str")) |
343 | | - end |
344 | | - local function func3(cb) |
345 | | - print("pcall()", pcall(cb, "str")) |
| 353 | + if mode ~= "module" then |
| 354 | + local function func2(cb) |
| 355 | + print("xpcall()", xpcall(cb, debug.traceback, "str")) |
| 356 | + end |
| 357 | + local function func3(cb) |
| 358 | + print("pcall()", pcall(cb, "str")) |
| 359 | + end |
| 360 | + local function cb(arg) |
| 361 | + coroutine.yield(2) |
| 362 | + return arg |
| 363 | + end |
| 364 | + local c = coroutine.wrap(func2) |
| 365 | + print("xpcall()", c(cb)) |
| 366 | + print("xpcall()", c()) |
| 367 | + local c = coroutine.wrap(func3) |
| 368 | + print("pcall()", c(cb)) |
| 369 | + print("pcall()", c()) |
346 | 370 | end |
347 | | - local function cb(arg) |
348 | | - coroutine.yield(2) |
349 | | - return arg |
350 | | - end |
351 | | - local c = coroutine.wrap(func2) |
352 | | - print("xpcall()", c(cb)) |
353 | | - print("xpcall()", c()) |
354 | | - local c = coroutine.wrap(func3) |
355 | | - print("pcall()", c(cb)) |
356 | | - print("pcall()", c()) |
357 | 371 | end |
358 | 372 |
|
359 | 373 |
|
|
387 | 401 | print("coroutine.running()", F(coroutine.running())) |
388 | 402 | local main_co, co1, co2 = coroutine.running() |
389 | 403 | -- coroutine.yield |
390 | | - print("coroutine.yield()", pcall(function() |
391 | | - coroutine.yield(1, 2, 3) |
392 | | - end)) |
| 404 | + if mode ~= "module" then |
| 405 | + print("coroutine.yield()", pcall(function() |
| 406 | + coroutine.yield(1, 2, 3) |
| 407 | + end)) |
| 408 | + end |
393 | 409 | print("coroutine.yield()", coroutine.wrap(function() |
394 | 410 | coroutine.yield(1, 2, 3) |
395 | 411 | end)()) |
|
428 | 444 | local path, prefix = "./?.lua;?/init.lua;../?.lua", "package.searchpath()" |
429 | 445 | print(prefix, package.searchpath("no.such.module", path)) |
430 | 446 | print(prefix, package.searchpath("no.such.module", "")) |
431 | | - print(prefix, package.searchpath("compat52", path)) |
| 447 | + print(prefix, package.searchpath("compat53", path)) |
432 | 448 | print(prefix, package.searchpath("no:such:module", path, ":", "|")) |
433 | 449 | end |
434 | 450 |
|
435 | 451 |
|
436 | 452 | ___'' |
437 | | -do |
| 453 | +if mode ~= "module" then |
438 | 454 | local function mod_func() return {} end |
439 | 455 | local function my_searcher(name) |
440 | 456 | if name == "my.module" then |
@@ -462,19 +478,19 @@ end |
462 | 478 |
|
463 | 479 | ___'' |
464 | 480 | do |
465 | | - print("string.find()", ("abc\0abc\0abc"):find("[^a\0]+")) |
466 | | - print("string.find()", ("abc\0abc\0abc"):find("%w+\0", 5)) |
467 | | - for x in ("abc\0def\0ghi"):gmatch("[^\0]+") do |
| 481 | + print("string.find()", string.find("abc\0abc\0abc", "[^a\0]+")) |
| 482 | + print("string.find()", string.find("abc\0abc\0abc", "%w+\0", 5)) |
| 483 | + for x in string.gmatch("abc\0def\0ghi", "[^\0]+") do |
468 | 484 | print("string.gmatch()", x) |
469 | 485 | end |
470 | | - for x in ("abc\0def\0ghi"):gmatch("%w*\0") do |
| 486 | + for x in string.gmatch("abc\0def\0ghi", "%w*\0") do |
471 | 487 | print("string.gmatch()", #x) |
472 | 488 | end |
473 | | - print("string.gsub()", ("abc\0def\0ghi"):gsub("[\0]", "X")) |
474 | | - print("string.gsub()", ("abc\0def\0ghi"):gsub("%w*\0", "X")) |
475 | | - print("string.gsub()", ("abc\0def\0ghi"):gsub("%A", "X")) |
476 | | - print("string.match()", ("abc\0abc\0abc"):match("([^\0a]+)")) |
477 | | - print("string.match()", #("abc\0abc\0abc"):match(".*\0")) |
| 489 | + print("string.gsub()", string.gsub("abc\0def\0ghi", "[\0]", "X")) |
| 490 | + print("string.gsub()", string.gsub("abc\0def\0ghi", "%w*\0", "X")) |
| 491 | + print("string.gsub()", string.gsub("abc\0def\0ghi", "%A", "X")) |
| 492 | + print("string.match()", string.match("abc\0abc\0abc", "([^\0a]+)")) |
| 493 | + print("string.match()", #string.match("abc\0abc\0abc", ".*\0")) |
478 | 494 | print("string.rep()", string.rep("a", 0)) |
479 | 495 | print("string.rep()", string.rep("b", 1)) |
480 | 496 | print("string.rep()", string.rep("c", 4)) |
|
533 | 549 | print("io.lines()", pcall(function() |
534 | 550 | for l in io.lines("no_such_file.txt") do print(l) end |
535 | 551 | end)) |
536 | | - local f = assert(io.open("test.lua", "r")) |
537 | | - for a,b in f:lines(2, "*l") do |
538 | | - print("file:lines()", a, b) |
539 | | - break |
| 552 | + if mode ~= "module" then |
| 553 | + local f = assert(io.open("test.lua", "r")) |
| 554 | + for a,b in f:lines(2, "*l") do |
| 555 | + print("file:lines()", a, b) |
| 556 | + break |
| 557 | + end |
| 558 | + f:close() |
| 559 | + f = assert(io.open("data.txt", "r")) |
| 560 | + for n1,n2,rest in f:lines("*n", "*n", "*a") do |
| 561 | + print("file:lines()", n1, n2, rest) |
| 562 | + end |
| 563 | + f:close() |
| 564 | + f = assert(io.open("data.txt", "r")) |
| 565 | + for l in f:lines() do |
| 566 | + print("file:lines()", l) |
| 567 | + end |
| 568 | + f:close() |
| 569 | + print("file:lines()", pcall(function() |
| 570 | + for l in f:lines() do print(l) end |
| 571 | + end)) |
| 572 | + print("file:lines()", pcall(function() |
| 573 | + local f = assert(io.open("data.txt", "r")) |
| 574 | + for l in f:lines("*l", "*x") do print(l) end |
| 575 | + f:close() |
| 576 | + end)) |
540 | 577 | end |
541 | | - f:close() |
542 | | - f = assert(io.open("data.txt", "r")) |
543 | | - for n1,n2,rest in f:lines("*n", "*n", "*a") do |
544 | | - print("file:lines()", n1, n2, rest) |
545 | | - end |
546 | | - f:close() |
547 | | - f = assert(io.open("data.txt", "r")) |
548 | | - for l in f:lines() do |
549 | | - print("file:lines()", l) |
550 | | - end |
551 | | - f:close() |
552 | | - print("file:lines()", pcall(function() |
553 | | - for l in f:lines() do print(l) end |
554 | | - end)) |
555 | | - print("file:lines()", pcall(function() |
556 | | - local f = assert(io.open("data.txt", "r")) |
557 | | - for l in f:lines("*l", "*x") do print(l) end |
558 | | - f:close() |
559 | | - end)) |
560 | 578 | os.remove("data.txt") |
561 | 579 | end |
562 | 580 | ___'' |
|
0 commit comments