File tree Expand file tree Collapse file tree 2 files changed +38
-4
lines changed Expand file tree Collapse file tree 2 files changed +38
-4
lines changed Original file line number Diff line number Diff line change @@ -330,4 +330,33 @@ function test()
330
330
t6 .sec = " 5"
331
331
assert (os.time (t1 ) == os.time (t6 ))
332
332
end
333
- test ()
333
+ test ()
334
+
335
+ -- issue #331
336
+ function test ()
337
+ local select_a = function ()
338
+ return select (3 , " 1" )
339
+ end
340
+ assert (true == pcall (select_a ))
341
+ local select_b = function ()
342
+ return select (0 )
343
+ end
344
+ assert (false == pcall (select_b ))
345
+ local select_c = function ()
346
+ return select (1 / 9 )
347
+ end
348
+ assert (false == pcall (select_c ))
349
+ local select_d = function ()
350
+ return select (1 , " a" )
351
+ end
352
+ assert (" a" == select_d ())
353
+ local select_e = function ()
354
+ return select (3 , " a" , " b" , " c" )
355
+ end
356
+ assert (" c" == select_e ())
357
+ local select_f = function ()
358
+ return select (0 )(select (1 / 9 ))
359
+ end
360
+ assert (false == pcall (select_f ))
361
+ end
362
+ test ()
Original file line number Diff line number Diff line change @@ -321,11 +321,16 @@ func baseSelect(L *LState) int {
321
321
switch lv := L .Get (1 ).(type ) {
322
322
case LNumber :
323
323
idx := int (lv )
324
- num := L .reg . Top () - L . indexToReg ( int ( lv )) - 1
324
+ num := L .GetTop ()
325
325
if idx < 0 {
326
- num ++
326
+ idx = num + idx
327
+ } else if idx > num {
328
+ idx = num
327
329
}
328
- return num
330
+ if 1 > idx {
331
+ L .ArgError (1 , "index out of range" )
332
+ }
333
+ return num - idx
329
334
case LString :
330
335
if string (lv ) != "#" {
331
336
L .ArgError (1 , "invalid string '" + string (lv )+ "'" )
You can’t perform that action at this time.
0 commit comments