Skip to content

Commit 8d31a7b

Browse files
committed
Combine mainLoop/mainLoopWithContext to inline evalInstruction
1 parent 5675e33 commit 8d31a7b

File tree

5 files changed

+12
-77
lines changed

5 files changed

+12
-77
lines changed

_state.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,6 @@ func newLState(options Options) *LState {
576576
wrapped: false,
577577
uvcache: nil,
578578
hasErrorFunc: false,
579-
mainLoop: mainLoop,
580579
ctx: nil,
581580
}
582581
if options.MinimizeStackMemory {
@@ -1051,9 +1050,9 @@ func (ls *LState) callR(nargs, nret, rbase int) {
10511050
if ls.G.MainThread == nil {
10521051
ls.G.MainThread = ls
10531052
ls.G.CurrentThread = ls
1054-
ls.mainLoop(ls, nil)
1053+
mainLoop(ls, nil)
10551054
} else {
1056-
ls.mainLoop(ls, ls.currentFrame)
1055+
mainLoop(ls, ls.currentFrame)
10571056
}
10581057
if nret != MultRet {
10591058
ls.reg.SetTop(rbase + nret)
@@ -1404,7 +1403,6 @@ func (ls *LState) NewThread() (*LState, context.CancelFunc) {
14041403
thread.Env = ls.Env
14051404
var f context.CancelFunc = nil
14061405
if ls.ctx != nil {
1407-
thread.mainLoop = mainLoopWithContext
14081406
thread.ctx, f = context.WithCancel(ls.ctx)
14091407
thread.ctxCancelFn = f
14101408
}
@@ -2044,9 +2042,8 @@ func (ls *LState) SetMx(mx int) {
20442042
}()
20452043
}
20462044

2047-
// SetContext set a context ctx to this LState. The provided ctx must be non-nil.
2045+
// SetContext set a context ctx to this LState.
20482046
func (ls *LState) SetContext(ctx context.Context) {
2049-
ls.mainLoop = mainLoopWithContext
20502047
ls.ctx = ctx
20512048
}
20522049

@@ -2058,7 +2055,6 @@ func (ls *LState) Context() context.Context {
20582055
// RemoveContext removes the context associated with this LState and returns this context.
20592056
func (ls *LState) RemoveContext() context.Context {
20602057
oldctx := ls.ctx
2061-
ls.mainLoop = mainLoop
20622058
ls.ctx = nil
20632059
return oldctx
20642060
}

_vm.go

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -540,44 +540,16 @@ func mainLoop(L *LState, baseframe *callFrame) {
540540
return
541541
}
542542

543-
for {
543+
for L.ctx == nil || L.ctx.Err() == nil {
544544
cf = L.currentFrame
545545
inst = cf.Fn.Proto.Code[cf.Pc]
546546
cf.Pc++
547547
if evalInstruction(L, inst, baseframe) {
548548
return
549549
}
550550
}
551-
}
552-
553-
func mainLoopWithContext(L *LState, baseframe *callFrame) {
554-
var inst uint32
555-
var cf *callFrame
556-
557-
if L.stack.IsEmpty() {
558-
return
559-
}
560-
561-
L.currentFrame = L.stack.Last()
562-
if L.currentFrame.Fn.IsG {
563-
callGFunction(L, false)
564-
return
565-
}
566551

567-
for {
568-
cf = L.currentFrame
569-
inst = cf.Fn.Proto.Code[cf.Pc]
570-
cf.Pc++
571-
select {
572-
case <-L.ctx.Done():
573-
L.RaiseError(L.ctx.Err().Error())
574-
return
575-
default:
576-
if evalInstruction(L, inst, baseframe) {
577-
return
578-
}
579-
}
580-
}
552+
L.RaiseError(L.ctx.Err().Error())
581553
}
582554

583555
// regv is the first target register to copy the return values to.
@@ -677,7 +649,7 @@ func threadRun(L *LState) {
677649
}
678650
}
679651
}()
680-
L.mainLoop(L, nil)
652+
mainLoop(L, nil)
681653
}
682654

683655
func luaModulo(lhs, rhs LNumber) LNumber {

state.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,6 @@ func newLState(options Options) *LState {
675675
wrapped: false,
676676
uvcache: nil,
677677
hasErrorFunc: false,
678-
mainLoop: mainLoop,
679678
ctx: nil,
680679
}
681680
if options.MinimizeStackMemory {
@@ -1263,9 +1262,9 @@ func (ls *LState) callR(nargs, nret, rbase int) {
12631262
if ls.G.MainThread == nil {
12641263
ls.G.MainThread = ls
12651264
ls.G.CurrentThread = ls
1266-
ls.mainLoop(ls, nil)
1265+
mainLoop(ls, nil)
12671266
} else {
1268-
ls.mainLoop(ls, ls.currentFrame)
1267+
mainLoop(ls, ls.currentFrame)
12691268
}
12701269
if nret != MultRet {
12711270
ls.reg.SetTop(rbase + nret)
@@ -1616,7 +1615,6 @@ func (ls *LState) NewThread() (*LState, context.CancelFunc) {
16161615
thread.Env = ls.Env
16171616
var f context.CancelFunc = nil
16181617
if ls.ctx != nil {
1619-
thread.mainLoop = mainLoopWithContext
16201618
thread.ctx, f = context.WithCancel(ls.ctx)
16211619
thread.ctxCancelFn = f
16221620
}
@@ -2256,9 +2254,8 @@ func (ls *LState) SetMx(mx int) {
22562254
}()
22572255
}
22582256

2259-
// SetContext set a context ctx to this LState. The provided ctx must be non-nil.
2257+
// SetContext set a context ctx to this LState.
22602258
func (ls *LState) SetContext(ctx context.Context) {
2261-
ls.mainLoop = mainLoopWithContext
22622259
ls.ctx = ctx
22632260
}
22642261

@@ -2270,7 +2267,6 @@ func (ls *LState) Context() context.Context {
22702267
// RemoveContext removes the context associated with this LState and returns this context.
22712268
func (ls *LState) RemoveContext() context.Context {
22722269
oldctx := ls.ctx
2273-
ls.mainLoop = mainLoop
22742270
ls.ctx = nil
22752271
return oldctx
22762272
}

value.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,6 @@ type LState struct {
192192
wrapped bool
193193
uvcache *Upvalue
194194
hasErrorFunc bool
195-
mainLoop func(*LState, *callFrame)
196195
ctx context.Context
197196
ctxCancelFn context.CancelFunc
198197
}

vm.go

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1825,44 +1825,16 @@ func mainLoop(L *LState, baseframe *callFrame) {
18251825
return
18261826
}
18271827

1828-
for {
1828+
for L.ctx == nil || L.ctx.Err() == nil {
18291829
cf = L.currentFrame
18301830
inst = cf.Fn.Proto.Code[cf.Pc]
18311831
cf.Pc++
18321832
if evalInstruction(L, inst, baseframe) {
18331833
return
18341834
}
18351835
}
1836-
}
1837-
1838-
func mainLoopWithContext(L *LState, baseframe *callFrame) {
1839-
var inst uint32
1840-
var cf *callFrame
1841-
1842-
if L.stack.IsEmpty() {
1843-
return
1844-
}
1845-
1846-
L.currentFrame = L.stack.Last()
1847-
if L.currentFrame.Fn.IsG {
1848-
callGFunction(L, false)
1849-
return
1850-
}
18511836

1852-
for {
1853-
cf = L.currentFrame
1854-
inst = cf.Fn.Proto.Code[cf.Pc]
1855-
cf.Pc++
1856-
select {
1857-
case <-L.ctx.Done():
1858-
L.RaiseError(L.ctx.Err().Error())
1859-
return
1860-
default:
1861-
if evalInstruction(L, inst, baseframe) {
1862-
return
1863-
}
1864-
}
1865-
}
1837+
L.RaiseError(L.ctx.Err().Error())
18661838
}
18671839

18681840
// regv is the first target register to copy the return values to.
@@ -2092,7 +2064,7 @@ func threadRun(L *LState) {
20922064
}
20932065
}
20942066
}()
2095-
L.mainLoop(L, nil)
2067+
mainLoop(L, nil)
20962068
}
20972069

20982070
func luaModulo(lhs, rhs LNumber) LNumber {

0 commit comments

Comments
 (0)