state.go 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306
  1. package lua
  2. ////////////////////////////////////////////////////////
  3. // This file was generated by go-inline. DO NOT EDIT. //
  4. ////////////////////////////////////////////////////////
  5. import (
  6. "context"
  7. "fmt"
  8. "io"
  9. "math"
  10. "os"
  11. "runtime"
  12. "strings"
  13. "sync"
  14. "sync/atomic"
  15. "time"
  16. "github.com/yuin/gopher-lua/parse"
  17. )
  18. const MultRet = -1
  19. const RegistryIndex = -10000
  20. const EnvironIndex = -10001
  21. const GlobalsIndex = -10002
  22. /* ApiError {{{ */
  23. type ApiError struct {
  24. Type ApiErrorType
  25. Object LValue
  26. StackTrace string
  27. // Underlying error. This attribute is set only if the Type is ApiErrorFile or ApiErrorSyntax
  28. Cause error
  29. }
  30. func newApiError(code ApiErrorType, object LValue) *ApiError {
  31. return &ApiError{code, object, "", nil}
  32. }
  33. func newApiErrorS(code ApiErrorType, message string) *ApiError {
  34. return newApiError(code, LString(message))
  35. }
  36. func newApiErrorE(code ApiErrorType, err error) *ApiError {
  37. return &ApiError{code, LString(err.Error()), "", err}
  38. }
  39. func (e *ApiError) Error() string {
  40. if len(e.StackTrace) > 0 {
  41. return fmt.Sprintf("%s\n%s", e.Object.String(), e.StackTrace)
  42. }
  43. return e.Object.String()
  44. }
  45. type ApiErrorType int
  46. const (
  47. ApiErrorSyntax ApiErrorType = iota
  48. ApiErrorFile
  49. ApiErrorRun
  50. ApiErrorError
  51. ApiErrorPanic
  52. )
  53. /* }}} */
  54. /* ResumeState {{{ */
  55. type ResumeState int
  56. const (
  57. ResumeOK ResumeState = iota
  58. ResumeYield
  59. ResumeError
  60. )
  61. /* }}} */
  62. /* P {{{ */
  63. type P struct {
  64. Fn LValue
  65. NRet int
  66. Protect bool
  67. Handler *LFunction
  68. }
  69. /* }}} */
  70. /* Options {{{ */
  71. // Options is a configuration that is used to create a new LState.
  72. type Options struct {
  73. // Call stack size. This defaults to `lua.CallStackSize`.
  74. CallStackSize int
  75. // Data stack size. This defaults to `lua.RegistrySize`.
  76. RegistrySize int
  77. // Allow the registry to grow from the registry size specified up to a value of RegistryMaxSize. A value of 0
  78. // indicates no growth is permitted. The registry will not shrink again after any growth.
  79. RegistryMaxSize int
  80. // If growth is enabled, step up by an additional `RegistryGrowStep` each time to avoid having to resize too often.
  81. // This defaults to `lua.RegistryGrowStep`
  82. RegistryGrowStep int
  83. // Controls whether or not libraries are opened by default
  84. SkipOpenLibs bool
  85. // Tells whether a Go stacktrace should be included in a Lua stacktrace when panics occur.
  86. IncludeGoStackTrace bool
  87. // If `MinimizeStackMemory` is set, the call stack will be automatically grown or shrank up to a limit of
  88. // `CallStackSize` in order to minimize memory usage. This does incur a slight performance penalty.
  89. MinimizeStackMemory bool
  90. }
  91. /* }}} */
  92. /* Debug {{{ */
  93. type Debug struct {
  94. frame *callFrame
  95. Name string
  96. What string
  97. Source string
  98. CurrentLine int
  99. NUpvalues int
  100. LineDefined int
  101. LastLineDefined int
  102. }
  103. /* }}} */
  104. /* callFrame {{{ */
  105. type callFrame struct {
  106. Idx int
  107. Fn *LFunction
  108. Parent *callFrame
  109. Pc int
  110. Base int
  111. LocalBase int
  112. ReturnBase int
  113. NArgs int
  114. NRet int
  115. TailCall int
  116. }
  117. type callFrameStack interface {
  118. Push(v callFrame)
  119. Pop() *callFrame
  120. Last() *callFrame
  121. SetSp(sp int)
  122. Sp() int
  123. At(sp int) *callFrame
  124. IsFull() bool
  125. IsEmpty() bool
  126. FreeAll()
  127. }
  128. type fixedCallFrameStack struct {
  129. array []callFrame
  130. sp int
  131. }
  132. func newFixedCallFrameStack(size int) callFrameStack {
  133. return &fixedCallFrameStack{
  134. array: make([]callFrame, size),
  135. sp: 0,
  136. }
  137. }
  138. func (cs *fixedCallFrameStack) IsEmpty() bool { return cs.sp == 0 }
  139. func (cs *fixedCallFrameStack) IsFull() bool { return cs.sp == len(cs.array) }
  140. func (cs *fixedCallFrameStack) Clear() {
  141. cs.sp = 0
  142. }
  143. func (cs *fixedCallFrameStack) Push(v callFrame) {
  144. cs.array[cs.sp] = v
  145. cs.array[cs.sp].Idx = cs.sp
  146. cs.sp++
  147. }
  148. func (cs *fixedCallFrameStack) Sp() int {
  149. return cs.sp
  150. }
  151. func (cs *fixedCallFrameStack) SetSp(sp int) {
  152. cs.sp = sp
  153. }
  154. func (cs *fixedCallFrameStack) Last() *callFrame {
  155. if cs.sp == 0 {
  156. return nil
  157. }
  158. return &cs.array[cs.sp-1]
  159. }
  160. func (cs *fixedCallFrameStack) At(sp int) *callFrame {
  161. return &cs.array[sp]
  162. }
  163. func (cs *fixedCallFrameStack) Pop() *callFrame {
  164. cs.sp--
  165. return &cs.array[cs.sp]
  166. }
  167. func (cs *fixedCallFrameStack) FreeAll() {
  168. // nothing to do for fixed callframestack
  169. }
  170. // FramesPerSegment should be a power of 2 constant for performance reasons. It will allow the go compiler to change
  171. // the divs and mods into bitshifts. Max is 256 due to current use of uint8 to count how many frames in a segment are
  172. // used.
  173. const FramesPerSegment = 8
  174. type callFrameStackSegment struct {
  175. array [FramesPerSegment]callFrame
  176. }
  177. type segIdx uint16
  178. type autoGrowingCallFrameStack struct {
  179. segments []*callFrameStackSegment
  180. segIdx segIdx
  181. // segSp is the number of frames in the current segment which are used. Full 'sp' value is segIdx * FramesPerSegment + segSp.
  182. // It points to the next stack slot to use, so 0 means to use the 0th element in the segment, and a value of
  183. // FramesPerSegment indicates that the segment is full and cannot accommodate another frame.
  184. segSp uint8
  185. }
  186. var segmentPool sync.Pool
  187. func newCallFrameStackSegment() *callFrameStackSegment {
  188. seg := segmentPool.Get()
  189. if seg == nil {
  190. return &callFrameStackSegment{}
  191. }
  192. return seg.(*callFrameStackSegment)
  193. }
  194. func freeCallFrameStackSegment(seg *callFrameStackSegment) {
  195. segmentPool.Put(seg)
  196. }
  197. // newCallFrameStack allocates a new stack for a lua state, which will auto grow up to a max size of at least maxSize.
  198. // it will actually grow up to the next segment size multiple after maxSize, where the segment size is dictated by
  199. // FramesPerSegment.
  200. func newAutoGrowingCallFrameStack(maxSize int) callFrameStack {
  201. cs := &autoGrowingCallFrameStack{
  202. segments: make([]*callFrameStackSegment, (maxSize+(FramesPerSegment-1))/FramesPerSegment),
  203. segIdx: 0,
  204. }
  205. cs.segments[0] = newCallFrameStackSegment()
  206. return cs
  207. }
  208. func (cs *autoGrowingCallFrameStack) IsEmpty() bool {
  209. return cs.segIdx == 0 && cs.segSp == 0
  210. }
  211. // IsFull returns true if the stack cannot receive any more stack pushes without overflowing
  212. func (cs *autoGrowingCallFrameStack) IsFull() bool {
  213. return int(cs.segIdx) == len(cs.segments) && cs.segSp >= FramesPerSegment
  214. }
  215. func (cs *autoGrowingCallFrameStack) Clear() {
  216. for i := segIdx(1); i <= cs.segIdx; i++ {
  217. freeCallFrameStackSegment(cs.segments[i])
  218. cs.segments[i] = nil
  219. }
  220. cs.segIdx = 0
  221. cs.segSp = 0
  222. }
  223. func (cs *autoGrowingCallFrameStack) FreeAll() {
  224. for i := segIdx(0); i <= cs.segIdx; i++ {
  225. freeCallFrameStackSegment(cs.segments[i])
  226. cs.segments[i] = nil
  227. }
  228. }
  229. // Push pushes the passed callFrame onto the stack. it panics if the stack is full, caller should call IsFull() before
  230. // invoking this to avoid this.
  231. func (cs *autoGrowingCallFrameStack) Push(v callFrame) {
  232. curSeg := cs.segments[cs.segIdx]
  233. if cs.segSp >= FramesPerSegment {
  234. // segment full, push new segment if allowed
  235. if cs.segIdx < segIdx(len(cs.segments)-1) {
  236. curSeg = newCallFrameStackSegment()
  237. cs.segIdx++
  238. cs.segments[cs.segIdx] = curSeg
  239. cs.segSp = 0
  240. } else {
  241. panic("lua callstack overflow")
  242. }
  243. }
  244. curSeg.array[cs.segSp] = v
  245. curSeg.array[cs.segSp].Idx = int(cs.segSp) + FramesPerSegment*int(cs.segIdx)
  246. cs.segSp++
  247. }
  248. // Sp retrieves the current stack depth, which is the number of frames currently pushed on the stack.
  249. func (cs *autoGrowingCallFrameStack) Sp() int {
  250. return int(cs.segSp) + int(cs.segIdx)*FramesPerSegment
  251. }
  252. // SetSp can be used to rapidly unwind the stack, freeing all stack frames on the way. It should not be used to
  253. // allocate new stack space, use Push() for that.
  254. func (cs *autoGrowingCallFrameStack) SetSp(sp int) {
  255. desiredSegIdx := segIdx(sp / FramesPerSegment)
  256. desiredFramesInLastSeg := uint8(sp % FramesPerSegment)
  257. for {
  258. if cs.segIdx <= desiredSegIdx {
  259. break
  260. }
  261. freeCallFrameStackSegment(cs.segments[cs.segIdx])
  262. cs.segments[cs.segIdx] = nil
  263. cs.segIdx--
  264. }
  265. cs.segSp = desiredFramesInLastSeg
  266. }
  267. func (cs *autoGrowingCallFrameStack) Last() *callFrame {
  268. curSeg := cs.segments[cs.segIdx]
  269. segSp := cs.segSp
  270. if segSp == 0 {
  271. if cs.segIdx == 0 {
  272. return nil
  273. }
  274. curSeg = cs.segments[cs.segIdx-1]
  275. segSp = FramesPerSegment
  276. }
  277. return &curSeg.array[segSp-1]
  278. }
  279. func (cs *autoGrowingCallFrameStack) At(sp int) *callFrame {
  280. segIdx := segIdx(sp / FramesPerSegment)
  281. frameIdx := uint8(sp % FramesPerSegment)
  282. return &cs.segments[segIdx].array[frameIdx]
  283. }
  284. // Pop pops off the most recent stack frame and returns it
  285. func (cs *autoGrowingCallFrameStack) Pop() *callFrame {
  286. curSeg := cs.segments[cs.segIdx]
  287. if cs.segSp == 0 {
  288. if cs.segIdx == 0 {
  289. // stack empty
  290. return nil
  291. }
  292. freeCallFrameStackSegment(curSeg)
  293. cs.segments[cs.segIdx] = nil
  294. cs.segIdx--
  295. cs.segSp = FramesPerSegment
  296. curSeg = cs.segments[cs.segIdx]
  297. }
  298. cs.segSp--
  299. return &curSeg.array[cs.segSp]
  300. }
  301. /* }}} */
  302. /* registry {{{ */
  303. type registryHandler interface {
  304. registryOverflow()
  305. }
  306. type registry struct {
  307. array []LValue
  308. top int
  309. growBy int
  310. maxSize int
  311. alloc *allocator
  312. handler registryHandler
  313. }
  314. func newRegistry(handler registryHandler, initialSize int, growBy int, maxSize int, alloc *allocator) *registry {
  315. return &registry{make([]LValue, initialSize), 0, growBy, maxSize, alloc, handler}
  316. }
  317. func (rg *registry) checkSize(requiredSize int) { // +inline-start
  318. if requiredSize > cap(rg.array) {
  319. rg.resize(requiredSize)
  320. }
  321. } // +inline-end
  322. func (rg *registry) resize(requiredSize int) { // +inline-start
  323. newSize := requiredSize + rg.growBy // give some padding
  324. if newSize > rg.maxSize {
  325. newSize = rg.maxSize
  326. }
  327. if newSize < requiredSize {
  328. rg.handler.registryOverflow()
  329. return
  330. }
  331. rg.forceResize(newSize)
  332. } // +inline-end
  333. func (rg *registry) forceResize(newSize int) {
  334. newSlice := make([]LValue, newSize)
  335. copy(newSlice, rg.array[:rg.top]) // should we copy the area beyond top? there shouldn't be any valid values there so it shouldn't be necessary.
  336. rg.array = newSlice
  337. }
  338. func (rg *registry) SetTop(topi int) { // +inline-start
  339. // this section is inlined by go-inline
  340. // source function is 'func (rg *registry) checkSize(requiredSize int) ' in '_state.go'
  341. {
  342. requiredSize := topi
  343. if requiredSize > cap(rg.array) {
  344. rg.resize(requiredSize)
  345. }
  346. }
  347. oldtopi := rg.top
  348. rg.top = topi
  349. for i := oldtopi; i < rg.top; i++ {
  350. rg.array[i] = LNil
  351. }
  352. // values beyond top don't need to be valid LValues, so setting them to nil is fine
  353. // setting them to nil rather than LNil lets us invoke the golang memclr opto
  354. if rg.top < oldtopi {
  355. nilRange := rg.array[rg.top:oldtopi]
  356. for i := range nilRange {
  357. nilRange[i] = nil
  358. }
  359. }
  360. //for i := rg.top; i < oldtop; i++ {
  361. // rg.array[i] = LNil
  362. //}
  363. } // +inline-end
  364. func (rg *registry) Top() int {
  365. return rg.top
  366. }
  367. func (rg *registry) Push(v LValue) {
  368. newSize := rg.top + 1
  369. // this section is inlined by go-inline
  370. // source function is 'func (rg *registry) checkSize(requiredSize int) ' in '_state.go'
  371. {
  372. requiredSize := newSize
  373. if requiredSize > cap(rg.array) {
  374. rg.resize(requiredSize)
  375. }
  376. }
  377. rg.array[rg.top] = v
  378. rg.top++
  379. }
  380. func (rg *registry) Pop() LValue {
  381. v := rg.array[rg.top-1]
  382. rg.array[rg.top-1] = LNil
  383. rg.top--
  384. return v
  385. }
  386. func (rg *registry) Get(reg int) LValue {
  387. return rg.array[reg]
  388. }
  389. // CopyRange will move a section of values from index `start` to index `regv`
  390. // It will move `n` values.
  391. // `limit` specifies the maximum end range that can be copied from. If it's set to -1, then it defaults to stopping at
  392. // the top of the registry (values beyond the top are not initialized, so if specifying an alternative `limit` you should
  393. // pass a value <= rg.top.
  394. // If start+n is beyond the limit, then nil values will be copied to the destination slots.
  395. // After the copy, the registry is truncated to be at the end of the copied range, ie the original of the copied values
  396. // are nilled out. (So top will be regv+n)
  397. // CopyRange should ideally be renamed to MoveRange.
  398. func (rg *registry) CopyRange(regv, start, limit, n int) { // +inline-start
  399. newSize := regv + n
  400. // this section is inlined by go-inline
  401. // source function is 'func (rg *registry) checkSize(requiredSize int) ' in '_state.go'
  402. {
  403. requiredSize := newSize
  404. if requiredSize > cap(rg.array) {
  405. rg.resize(requiredSize)
  406. }
  407. }
  408. if limit == -1 || limit > rg.top {
  409. limit = rg.top
  410. }
  411. for i := 0; i < n; i++ {
  412. srcIdx := start + i
  413. if srcIdx >= limit || srcIdx < 0 {
  414. rg.array[regv+i] = LNil
  415. } else {
  416. rg.array[regv+i] = rg.array[srcIdx]
  417. }
  418. }
  419. // values beyond top don't need to be valid LValues, so setting them to nil is fine
  420. // setting them to nil rather than LNil lets us invoke the golang memclr opto
  421. oldtop := rg.top
  422. rg.top = regv + n
  423. if rg.top < oldtop {
  424. nilRange := rg.array[rg.top:oldtop]
  425. for i := range nilRange {
  426. nilRange[i] = nil
  427. }
  428. }
  429. } // +inline-end
  430. // FillNil fills the registry with nil values from regm to regm+n and then sets the registry top to regm+n
  431. func (rg *registry) FillNil(regm, n int) { // +inline-start
  432. newSize := regm + n
  433. // this section is inlined by go-inline
  434. // source function is 'func (rg *registry) checkSize(requiredSize int) ' in '_state.go'
  435. {
  436. requiredSize := newSize
  437. if requiredSize > cap(rg.array) {
  438. rg.resize(requiredSize)
  439. }
  440. }
  441. for i := 0; i < n; i++ {
  442. rg.array[regm+i] = LNil
  443. }
  444. // values beyond top don't need to be valid LValues, so setting them to nil is fine
  445. // setting them to nil rather than LNil lets us invoke the golang memclr opto
  446. oldtop := rg.top
  447. rg.top = regm + n
  448. if rg.top < oldtop {
  449. nilRange := rg.array[rg.top:oldtop]
  450. for i := range nilRange {
  451. nilRange[i] = nil
  452. }
  453. }
  454. } // +inline-end
  455. func (rg *registry) Insert(value LValue, reg int) {
  456. top := rg.Top()
  457. if reg >= top {
  458. // this section is inlined by go-inline
  459. // source function is 'func (rg *registry) Set(regi int, vali LValue) ' in '_state.go'
  460. {
  461. regi := reg
  462. vali := value
  463. newSize := regi + 1
  464. // this section is inlined by go-inline
  465. // source function is 'func (rg *registry) checkSize(requiredSize int) ' in '_state.go'
  466. {
  467. requiredSize := newSize
  468. if requiredSize > cap(rg.array) {
  469. rg.resize(requiredSize)
  470. }
  471. }
  472. rg.array[regi] = vali
  473. if regi >= rg.top {
  474. rg.top = regi + 1
  475. }
  476. }
  477. return
  478. }
  479. top--
  480. for ; top >= reg; top-- {
  481. // FIXME consider using copy() here if Insert() is called enough
  482. // this section is inlined by go-inline
  483. // source function is 'func (rg *registry) Set(regi int, vali LValue) ' in '_state.go'
  484. {
  485. regi := top + 1
  486. vali := rg.Get(top)
  487. newSize := regi + 1
  488. // this section is inlined by go-inline
  489. // source function is 'func (rg *registry) checkSize(requiredSize int) ' in '_state.go'
  490. {
  491. requiredSize := newSize
  492. if requiredSize > cap(rg.array) {
  493. rg.resize(requiredSize)
  494. }
  495. }
  496. rg.array[regi] = vali
  497. if regi >= rg.top {
  498. rg.top = regi + 1
  499. }
  500. }
  501. }
  502. // this section is inlined by go-inline
  503. // source function is 'func (rg *registry) Set(regi int, vali LValue) ' in '_state.go'
  504. {
  505. regi := reg
  506. vali := value
  507. newSize := regi + 1
  508. // this section is inlined by go-inline
  509. // source function is 'func (rg *registry) checkSize(requiredSize int) ' in '_state.go'
  510. {
  511. requiredSize := newSize
  512. if requiredSize > cap(rg.array) {
  513. rg.resize(requiredSize)
  514. }
  515. }
  516. rg.array[regi] = vali
  517. if regi >= rg.top {
  518. rg.top = regi + 1
  519. }
  520. }
  521. }
  522. func (rg *registry) Set(regi int, vali LValue) { // +inline-start
  523. newSize := regi + 1
  524. // this section is inlined by go-inline
  525. // source function is 'func (rg *registry) checkSize(requiredSize int) ' in '_state.go'
  526. {
  527. requiredSize := newSize
  528. if requiredSize > cap(rg.array) {
  529. rg.resize(requiredSize)
  530. }
  531. }
  532. rg.array[regi] = vali
  533. if regi >= rg.top {
  534. rg.top = regi + 1
  535. }
  536. } // +inline-end
  537. func (rg *registry) SetNumber(regi int, vali LNumber) { // +inline-start
  538. newSize := regi + 1
  539. // this section is inlined by go-inline
  540. // source function is 'func (rg *registry) checkSize(requiredSize int) ' in '_state.go'
  541. {
  542. requiredSize := newSize
  543. if requiredSize > cap(rg.array) {
  544. rg.resize(requiredSize)
  545. }
  546. }
  547. rg.array[regi] = rg.alloc.LNumber2I(vali)
  548. if regi >= rg.top {
  549. rg.top = regi + 1
  550. }
  551. } // +inline-end
  552. func (rg *registry) IsFull() bool {
  553. return rg.top >= cap(rg.array)
  554. }
  555. /* }}} */
  556. /* Global {{{ */
  557. func newGlobal() *Global {
  558. return &Global{
  559. MainThread: nil,
  560. Registry: newLTable(0, 32),
  561. Global: newLTable(0, 64),
  562. builtinMts: make(map[int]LValue),
  563. tempFiles: make([]*os.File, 0, 10),
  564. }
  565. }
  566. /* }}} */
  567. /* package local methods {{{ */
  568. func panicWithTraceback(L *LState) {
  569. err := newApiError(ApiErrorRun, L.Get(-1))
  570. err.StackTrace = L.stackTrace(0)
  571. panic(err)
  572. }
  573. func panicWithoutTraceback(L *LState) {
  574. err := newApiError(ApiErrorRun, L.Get(-1))
  575. panic(err)
  576. }
  577. func newLState(options Options) *LState {
  578. al := newAllocator(32)
  579. ls := &LState{
  580. G: newGlobal(),
  581. Parent: nil,
  582. Panic: panicWithTraceback,
  583. Dead: false,
  584. Options: options,
  585. stop: 0,
  586. alloc: al,
  587. currentFrame: nil,
  588. wrapped: false,
  589. uvcache: nil,
  590. hasErrorFunc: false,
  591. mainLoop: mainLoop,
  592. ctx: nil,
  593. }
  594. if options.MinimizeStackMemory {
  595. ls.stack = newAutoGrowingCallFrameStack(options.CallStackSize)
  596. } else {
  597. ls.stack = newFixedCallFrameStack(options.CallStackSize)
  598. }
  599. ls.reg = newRegistry(ls, options.RegistrySize, options.RegistryGrowStep, options.RegistryMaxSize, al)
  600. ls.Env = ls.G.Global
  601. return ls
  602. }
  603. func (ls *LState) printReg() {
  604. println("-------------------------")
  605. println("thread:", ls)
  606. println("top:", ls.reg.Top())
  607. if ls.currentFrame != nil {
  608. println("function base:", ls.currentFrame.Base)
  609. println("return base:", ls.currentFrame.ReturnBase)
  610. } else {
  611. println("(vm not started)")
  612. }
  613. println("local base:", ls.currentLocalBase())
  614. for i := 0; i < ls.reg.Top(); i++ {
  615. println(i, ls.reg.Get(i).String())
  616. }
  617. println("-------------------------")
  618. }
  619. func (ls *LState) printCallStack() {
  620. println("-------------------------")
  621. for i := 0; i < ls.stack.Sp(); i++ {
  622. print(i)
  623. print(" ")
  624. frame := ls.stack.At(i)
  625. if frame == nil {
  626. break
  627. }
  628. if frame.Fn.IsG {
  629. println("IsG:", true, "Frame:", frame, "Fn:", frame.Fn)
  630. } else {
  631. println("IsG:", false, "Frame:", frame, "Fn:", frame.Fn, "pc:", frame.Pc)
  632. }
  633. }
  634. println("-------------------------")
  635. }
  636. func (ls *LState) closeAllUpvalues() { // +inline-start
  637. for cf := ls.currentFrame; cf != nil; cf = cf.Parent {
  638. if !cf.Fn.IsG {
  639. ls.closeUpvalues(cf.LocalBase)
  640. }
  641. }
  642. } // +inline-end
  643. func (ls *LState) raiseError(level int, format string, args ...interface{}) {
  644. if !ls.hasErrorFunc {
  645. ls.closeAllUpvalues()
  646. }
  647. message := format
  648. if len(args) > 0 {
  649. message = fmt.Sprintf(format, args...)
  650. }
  651. if level > 0 {
  652. message = fmt.Sprintf("%v %v", ls.where(level-1, true), message)
  653. }
  654. if ls.reg.IsFull() {
  655. // if the registry is full then it won't be possible to push a value, in this case, force a larger size
  656. ls.reg.forceResize(ls.reg.Top() + 1)
  657. }
  658. ls.reg.Push(LString(message))
  659. ls.Panic(ls)
  660. }
  661. func (ls *LState) findLocal(frame *callFrame, no int) string {
  662. fn := frame.Fn
  663. if !fn.IsG {
  664. if name, ok := fn.LocalName(no, frame.Pc-1); ok {
  665. return name
  666. }
  667. }
  668. var top int
  669. if ls.currentFrame == frame {
  670. top = ls.reg.Top()
  671. } else if frame.Idx+1 < ls.stack.Sp() {
  672. top = ls.stack.At(frame.Idx + 1).Base
  673. } else {
  674. return ""
  675. }
  676. if top-frame.LocalBase >= no {
  677. return "(*temporary)"
  678. }
  679. return ""
  680. }
  681. func (ls *LState) where(level int, skipg bool) string {
  682. dbg, ok := ls.GetStack(level)
  683. if !ok {
  684. return ""
  685. }
  686. cf := dbg.frame
  687. proto := cf.Fn.Proto
  688. sourcename := "[G]"
  689. if proto != nil {
  690. sourcename = proto.SourceName
  691. } else if skipg {
  692. return ls.where(level+1, skipg)
  693. }
  694. line := ""
  695. if proto != nil {
  696. line = fmt.Sprintf("%v:", proto.DbgSourcePositions[cf.Pc-1])
  697. }
  698. return fmt.Sprintf("%v:%v", sourcename, line)
  699. }
  700. func (ls *LState) stackTrace(level int) string {
  701. buf := []string{}
  702. header := "stack traceback:"
  703. if ls.currentFrame != nil {
  704. i := 0
  705. for dbg, ok := ls.GetStack(i); ok; dbg, ok = ls.GetStack(i) {
  706. cf := dbg.frame
  707. buf = append(buf, fmt.Sprintf("\t%v in %v", ls.Where(i), ls.formattedFrameFuncName(cf)))
  708. if !cf.Fn.IsG && cf.TailCall > 0 {
  709. for tc := cf.TailCall; tc > 0; tc-- {
  710. buf = append(buf, "\t(tailcall): ?")
  711. i++
  712. }
  713. }
  714. i++
  715. }
  716. }
  717. buf = append(buf, fmt.Sprintf("\t%v: %v", "[G]", "?"))
  718. buf = buf[intMax(0, intMin(level, len(buf))):len(buf)]
  719. if len(buf) > 20 {
  720. newbuf := make([]string, 0, 20)
  721. newbuf = append(newbuf, buf[0:7]...)
  722. newbuf = append(newbuf, "\t...")
  723. newbuf = append(newbuf, buf[len(buf)-7:len(buf)]...)
  724. buf = newbuf
  725. }
  726. return fmt.Sprintf("%s\n%s", header, strings.Join(buf, "\n"))
  727. }
  728. func (ls *LState) formattedFrameFuncName(fr *callFrame) string {
  729. name, ischunk := ls.frameFuncName(fr)
  730. if ischunk {
  731. return name
  732. }
  733. if name[0] != '(' && name[0] != '<' {
  734. return fmt.Sprintf("function '%s'", name)
  735. }
  736. return fmt.Sprintf("function %s", name)
  737. }
  738. func (ls *LState) rawFrameFuncName(fr *callFrame) string {
  739. name, _ := ls.frameFuncName(fr)
  740. return name
  741. }
  742. func (ls *LState) frameFuncName(fr *callFrame) (string, bool) {
  743. frame := fr.Parent
  744. if frame == nil {
  745. if ls.Parent == nil {
  746. return "main chunk", true
  747. } else {
  748. return "corountine", true
  749. }
  750. }
  751. if !frame.Fn.IsG {
  752. pc := frame.Pc - 1
  753. for _, call := range frame.Fn.Proto.DbgCalls {
  754. if call.Pc == pc {
  755. name := call.Name
  756. if (name == "?" || fr.TailCall > 0) && !fr.Fn.IsG {
  757. name = fmt.Sprintf("<%v:%v>", fr.Fn.Proto.SourceName, fr.Fn.Proto.LineDefined)
  758. }
  759. return name, false
  760. }
  761. }
  762. }
  763. if !fr.Fn.IsG {
  764. return fmt.Sprintf("<%v:%v>", fr.Fn.Proto.SourceName, fr.Fn.Proto.LineDefined), false
  765. }
  766. return "(anonymous)", false
  767. }
  768. func (ls *LState) isStarted() bool {
  769. return ls.currentFrame != nil
  770. }
  771. func (ls *LState) kill() {
  772. ls.Dead = true
  773. if ls.ctxCancelFn != nil {
  774. ls.ctxCancelFn()
  775. }
  776. }
  777. func (ls *LState) indexToReg(idx int) int {
  778. base := ls.currentLocalBase()
  779. if idx > 0 {
  780. return base + idx - 1
  781. } else if idx == 0 {
  782. return -1
  783. } else {
  784. tidx := ls.reg.Top() + idx
  785. if tidx < base {
  786. return -1
  787. }
  788. return tidx
  789. }
  790. }
  791. func (ls *LState) currentLocalBase() int {
  792. base := 0
  793. if ls.currentFrame != nil {
  794. base = ls.currentFrame.LocalBase
  795. }
  796. return base
  797. }
  798. func (ls *LState) currentEnv() *LTable {
  799. return ls.Env
  800. /*
  801. if ls.currentFrame == nil {
  802. return ls.Env
  803. }
  804. return ls.currentFrame.Fn.Env
  805. */
  806. }
  807. func (ls *LState) rkValue(idx int) LValue {
  808. /*
  809. if OpIsK(idx) {
  810. return ls.currentFrame.Fn.Proto.Constants[opIndexK(idx)]
  811. }
  812. return ls.reg.Get(ls.currentFrame.LocalBase + idx)
  813. */
  814. if (idx & opBitRk) != 0 {
  815. return ls.currentFrame.Fn.Proto.Constants[idx & ^opBitRk]
  816. }
  817. return ls.reg.array[ls.currentFrame.LocalBase+idx]
  818. }
  819. func (ls *LState) rkString(idx int) string {
  820. if (idx & opBitRk) != 0 {
  821. return ls.currentFrame.Fn.Proto.stringConstants[idx & ^opBitRk]
  822. }
  823. return string(ls.reg.array[ls.currentFrame.LocalBase+idx].(LString))
  824. }
  825. func (ls *LState) closeUpvalues(idx int) { // +inline-start
  826. if ls.uvcache != nil {
  827. var prev *Upvalue
  828. for uv := ls.uvcache; uv != nil; uv = uv.next {
  829. if uv.index >= idx {
  830. if prev != nil {
  831. prev.next = nil
  832. } else {
  833. ls.uvcache = nil
  834. }
  835. uv.Close()
  836. }
  837. prev = uv
  838. }
  839. }
  840. } // +inline-end
  841. func (ls *LState) findUpvalue(idx int) *Upvalue {
  842. var prev *Upvalue
  843. var next *Upvalue
  844. if ls.uvcache != nil {
  845. for uv := ls.uvcache; uv != nil; uv = uv.next {
  846. if uv.index == idx {
  847. return uv
  848. }
  849. if uv.index > idx {
  850. next = uv
  851. break
  852. }
  853. prev = uv
  854. }
  855. }
  856. uv := &Upvalue{reg: ls.reg, index: idx, closed: false}
  857. if prev != nil {
  858. prev.next = uv
  859. } else {
  860. ls.uvcache = uv
  861. }
  862. if next != nil {
  863. uv.next = next
  864. }
  865. return uv
  866. }
  867. func (ls *LState) metatable(lvalue LValue, rawget bool) LValue {
  868. var metatable LValue = LNil
  869. switch obj := lvalue.(type) {
  870. case *LTable:
  871. metatable = obj.Metatable
  872. case *LUserData:
  873. metatable = obj.Metatable
  874. default:
  875. if table, ok := ls.G.builtinMts[int(obj.Type())]; ok {
  876. metatable = table
  877. }
  878. }
  879. if !rawget && metatable != LNil {
  880. oldmt := metatable
  881. if tb, ok := metatable.(*LTable); ok {
  882. metatable = tb.RawGetString("__metatable")
  883. if metatable == LNil {
  884. metatable = oldmt
  885. }
  886. }
  887. }
  888. return metatable
  889. }
  890. func (ls *LState) metaOp1(lvalue LValue, event string) LValue {
  891. if mt := ls.metatable(lvalue, true); mt != LNil {
  892. if tb, ok := mt.(*LTable); ok {
  893. return tb.RawGetString(event)
  894. }
  895. }
  896. return LNil
  897. }
  898. func (ls *LState) metaOp2(value1, value2 LValue, event string) LValue {
  899. if mt := ls.metatable(value1, true); mt != LNil {
  900. if tb, ok := mt.(*LTable); ok {
  901. if ret := tb.RawGetString(event); ret != LNil {
  902. return ret
  903. }
  904. }
  905. }
  906. if mt := ls.metatable(value2, true); mt != LNil {
  907. if tb, ok := mt.(*LTable); ok {
  908. return tb.RawGetString(event)
  909. }
  910. }
  911. return LNil
  912. }
  913. func (ls *LState) metaCall(lvalue LValue) (*LFunction, bool) {
  914. if fn, ok := lvalue.(*LFunction); ok {
  915. return fn, false
  916. }
  917. if fn, ok := ls.metaOp1(lvalue, "__call").(*LFunction); ok {
  918. return fn, true
  919. }
  920. return nil, false
  921. }
  922. func (ls *LState) initCallFrame(cf *callFrame) { // +inline-start
  923. if cf.Fn.IsG {
  924. ls.reg.SetTop(cf.LocalBase + cf.NArgs)
  925. } else {
  926. proto := cf.Fn.Proto
  927. nargs := cf.NArgs
  928. np := int(proto.NumParameters)
  929. if nargs < np {
  930. // default any missing arguments to nil
  931. newSize := cf.LocalBase + np
  932. // this section is inlined by go-inline
  933. // source function is 'func (rg *registry) checkSize(requiredSize int) ' in '_state.go'
  934. {
  935. rg := ls.reg
  936. requiredSize := newSize
  937. if requiredSize > cap(rg.array) {
  938. rg.resize(requiredSize)
  939. }
  940. }
  941. for i := nargs; i < np; i++ {
  942. ls.reg.array[cf.LocalBase+i] = LNil
  943. }
  944. nargs = np
  945. ls.reg.top = newSize
  946. }
  947. if (proto.IsVarArg & VarArgIsVarArg) == 0 {
  948. if nargs < int(proto.NumUsedRegisters) {
  949. nargs = int(proto.NumUsedRegisters)
  950. }
  951. newSize := cf.LocalBase + nargs
  952. // this section is inlined by go-inline
  953. // source function is 'func (rg *registry) checkSize(requiredSize int) ' in '_state.go'
  954. {
  955. rg := ls.reg
  956. requiredSize := newSize
  957. if requiredSize > cap(rg.array) {
  958. rg.resize(requiredSize)
  959. }
  960. }
  961. for i := np; i < nargs; i++ {
  962. ls.reg.array[cf.LocalBase+i] = LNil
  963. }
  964. ls.reg.top = cf.LocalBase + int(proto.NumUsedRegisters)
  965. } else {
  966. /* swap vararg positions:
  967. closure
  968. namedparam1 <- lbase
  969. namedparam2
  970. vararg1
  971. vararg2
  972. TO
  973. closure
  974. nil
  975. nil
  976. vararg1
  977. vararg2
  978. namedparam1 <- lbase
  979. namedparam2
  980. */
  981. nvarargs := nargs - np
  982. if nvarargs < 0 {
  983. nvarargs = 0
  984. }
  985. ls.reg.SetTop(cf.LocalBase + nargs + np)
  986. for i := 0; i < np; i++ {
  987. //ls.reg.Set(cf.LocalBase+nargs+i, ls.reg.Get(cf.LocalBase+i))
  988. ls.reg.array[cf.LocalBase+nargs+i] = ls.reg.array[cf.LocalBase+i]
  989. //ls.reg.Set(cf.LocalBase+i, LNil)
  990. ls.reg.array[cf.LocalBase+i] = LNil
  991. }
  992. if CompatVarArg {
  993. ls.reg.SetTop(cf.LocalBase + nargs + np + 1)
  994. if (proto.IsVarArg & VarArgNeedsArg) != 0 {
  995. argtb := newLTable(nvarargs, 0)
  996. for i := 0; i < nvarargs; i++ {
  997. argtb.RawSetInt(i+1, ls.reg.Get(cf.LocalBase+np+i))
  998. }
  999. argtb.RawSetString("n", LNumber(nvarargs))
  1000. //ls.reg.Set(cf.LocalBase+nargs+np, argtb)
  1001. ls.reg.array[cf.LocalBase+nargs+np] = argtb
  1002. } else {
  1003. ls.reg.array[cf.LocalBase+nargs+np] = LNil
  1004. }
  1005. }
  1006. cf.LocalBase += nargs
  1007. maxreg := cf.LocalBase + int(proto.NumUsedRegisters)
  1008. ls.reg.SetTop(maxreg)
  1009. }
  1010. }
  1011. } // +inline-end
  1012. func (ls *LState) pushCallFrame(cf callFrame, fn LValue, meta bool) { // +inline-start
  1013. if meta {
  1014. cf.NArgs++
  1015. ls.reg.Insert(fn, cf.LocalBase)
  1016. }
  1017. if cf.Fn == nil {
  1018. ls.RaiseError("attempt to call a non-function object")
  1019. }
  1020. if ls.stack.IsFull() {
  1021. ls.RaiseError("stack overflow")
  1022. }
  1023. ls.stack.Push(cf)
  1024. newcf := ls.stack.Last()
  1025. // this section is inlined by go-inline
  1026. // source function is 'func (ls *LState) initCallFrame(cf *callFrame) ' in '_state.go'
  1027. {
  1028. cf := newcf
  1029. if cf.Fn.IsG {
  1030. ls.reg.SetTop(cf.LocalBase + cf.NArgs)
  1031. } else {
  1032. proto := cf.Fn.Proto
  1033. nargs := cf.NArgs
  1034. np := int(proto.NumParameters)
  1035. if nargs < np {
  1036. // default any missing arguments to nil
  1037. newSize := cf.LocalBase + np
  1038. // this section is inlined by go-inline
  1039. // source function is 'func (rg *registry) checkSize(requiredSize int) ' in '_state.go'
  1040. {
  1041. rg := ls.reg
  1042. requiredSize := newSize
  1043. if requiredSize > cap(rg.array) {
  1044. rg.resize(requiredSize)
  1045. }
  1046. }
  1047. for i := nargs; i < np; i++ {
  1048. ls.reg.array[cf.LocalBase+i] = LNil
  1049. }
  1050. nargs = np
  1051. ls.reg.top = newSize
  1052. }
  1053. if (proto.IsVarArg & VarArgIsVarArg) == 0 {
  1054. if nargs < int(proto.NumUsedRegisters) {
  1055. nargs = int(proto.NumUsedRegisters)
  1056. }
  1057. newSize := cf.LocalBase + nargs
  1058. // this section is inlined by go-inline
  1059. // source function is 'func (rg *registry) checkSize(requiredSize int) ' in '_state.go'
  1060. {
  1061. rg := ls.reg
  1062. requiredSize := newSize
  1063. if requiredSize > cap(rg.array) {
  1064. rg.resize(requiredSize)
  1065. }
  1066. }
  1067. for i := np; i < nargs; i++ {
  1068. ls.reg.array[cf.LocalBase+i] = LNil
  1069. }
  1070. ls.reg.top = cf.LocalBase + int(proto.NumUsedRegisters)
  1071. } else {
  1072. /* swap vararg positions:
  1073. closure
  1074. namedparam1 <- lbase
  1075. namedparam2
  1076. vararg1
  1077. vararg2
  1078. TO
  1079. closure
  1080. nil
  1081. nil
  1082. vararg1
  1083. vararg2
  1084. namedparam1 <- lbase
  1085. namedparam2
  1086. */
  1087. nvarargs := nargs - np
  1088. if nvarargs < 0 {
  1089. nvarargs = 0
  1090. }
  1091. ls.reg.SetTop(cf.LocalBase + nargs + np)
  1092. for i := 0; i < np; i++ {
  1093. //ls.reg.Set(cf.LocalBase+nargs+i, ls.reg.Get(cf.LocalBase+i))
  1094. ls.reg.array[cf.LocalBase+nargs+i] = ls.reg.array[cf.LocalBase+i]
  1095. //ls.reg.Set(cf.LocalBase+i, LNil)
  1096. ls.reg.array[cf.LocalBase+i] = LNil
  1097. }
  1098. if CompatVarArg {
  1099. ls.reg.SetTop(cf.LocalBase + nargs + np + 1)
  1100. if (proto.IsVarArg & VarArgNeedsArg) != 0 {
  1101. argtb := newLTable(nvarargs, 0)
  1102. for i := 0; i < nvarargs; i++ {
  1103. argtb.RawSetInt(i+1, ls.reg.Get(cf.LocalBase+np+i))
  1104. }
  1105. argtb.RawSetString("n", LNumber(nvarargs))
  1106. //ls.reg.Set(cf.LocalBase+nargs+np, argtb)
  1107. ls.reg.array[cf.LocalBase+nargs+np] = argtb
  1108. } else {
  1109. ls.reg.array[cf.LocalBase+nargs+np] = LNil
  1110. }
  1111. }
  1112. cf.LocalBase += nargs
  1113. maxreg := cf.LocalBase + int(proto.NumUsedRegisters)
  1114. ls.reg.SetTop(maxreg)
  1115. }
  1116. }
  1117. }
  1118. ls.currentFrame = newcf
  1119. } // +inline-end
  1120. func (ls *LState) callR(nargs, nret, rbase int) {
  1121. base := ls.reg.Top() - nargs - 1
  1122. if rbase < 0 {
  1123. rbase = base
  1124. }
  1125. lv := ls.reg.Get(base)
  1126. fn, meta := ls.metaCall(lv)
  1127. ls.pushCallFrame(callFrame{
  1128. Fn: fn,
  1129. Pc: 0,
  1130. Base: base,
  1131. LocalBase: base + 1,
  1132. ReturnBase: rbase,
  1133. NArgs: nargs,
  1134. NRet: nret,
  1135. Parent: ls.currentFrame,
  1136. TailCall: 0,
  1137. }, lv, meta)
  1138. if ls.G.MainThread == nil {
  1139. ls.G.MainThread = ls
  1140. ls.G.CurrentThread = ls
  1141. ls.mainLoop(ls, nil)
  1142. } else {
  1143. ls.mainLoop(ls, ls.currentFrame)
  1144. }
  1145. if nret != MultRet {
  1146. ls.reg.SetTop(rbase + nret)
  1147. }
  1148. }
  1149. func (ls *LState) getField(obj LValue, key LValue) LValue {
  1150. curobj := obj
  1151. for i := 0; i < MaxTableGetLoop; i++ {
  1152. tb, istable := curobj.(*LTable)
  1153. if istable {
  1154. ret := tb.RawGet(key)
  1155. if ret != LNil {
  1156. return ret
  1157. }
  1158. }
  1159. metaindex := ls.metaOp1(curobj, "__index")
  1160. if metaindex == LNil {
  1161. if !istable {
  1162. ls.RaiseError("attempt to index a non-table object(%v) with key '%s'", curobj.Type().String(), key.String())
  1163. }
  1164. return LNil
  1165. }
  1166. if metaindex.Type() == LTFunction {
  1167. ls.reg.Push(metaindex)
  1168. ls.reg.Push(curobj)
  1169. ls.reg.Push(key)
  1170. ls.Call(2, 1)
  1171. return ls.reg.Pop()
  1172. } else {
  1173. curobj = metaindex
  1174. }
  1175. }
  1176. ls.RaiseError("too many recursions in gettable")
  1177. return nil
  1178. }
  1179. func (ls *LState) getFieldString(obj LValue, key string) LValue {
  1180. curobj := obj
  1181. for i := 0; i < MaxTableGetLoop; i++ {
  1182. tb, istable := curobj.(*LTable)
  1183. if istable {
  1184. ret := tb.RawGetString(key)
  1185. if ret != LNil {
  1186. return ret
  1187. }
  1188. }
  1189. metaindex := ls.metaOp1(curobj, "__index")
  1190. if metaindex == LNil {
  1191. if !istable {
  1192. ls.RaiseError("attempt to index a non-table object(%v) with key '%s'", curobj.Type().String(), key)
  1193. }
  1194. return LNil
  1195. }
  1196. if metaindex.Type() == LTFunction {
  1197. ls.reg.Push(metaindex)
  1198. ls.reg.Push(curobj)
  1199. ls.reg.Push(LString(key))
  1200. ls.Call(2, 1)
  1201. return ls.reg.Pop()
  1202. } else {
  1203. curobj = metaindex
  1204. }
  1205. }
  1206. ls.RaiseError("too many recursions in gettable")
  1207. return nil
  1208. }
  1209. func (ls *LState) setField(obj LValue, key LValue, value LValue) {
  1210. curobj := obj
  1211. for i := 0; i < MaxTableGetLoop; i++ {
  1212. tb, istable := curobj.(*LTable)
  1213. if istable {
  1214. if tb.RawGet(key) != LNil {
  1215. ls.RawSet(tb, key, value)
  1216. return
  1217. }
  1218. }
  1219. metaindex := ls.metaOp1(curobj, "__newindex")
  1220. if metaindex == LNil {
  1221. if !istable {
  1222. ls.RaiseError("attempt to index a non-table object(%v) with key '%s'", curobj.Type().String(), key.String())
  1223. }
  1224. ls.RawSet(tb, key, value)
  1225. return
  1226. }
  1227. if metaindex.Type() == LTFunction {
  1228. ls.reg.Push(metaindex)
  1229. ls.reg.Push(curobj)
  1230. ls.reg.Push(key)
  1231. ls.reg.Push(value)
  1232. ls.Call(3, 0)
  1233. return
  1234. } else {
  1235. curobj = metaindex
  1236. }
  1237. }
  1238. ls.RaiseError("too many recursions in settable")
  1239. }
  1240. func (ls *LState) setFieldString(obj LValue, key string, value LValue) {
  1241. curobj := obj
  1242. for i := 0; i < MaxTableGetLoop; i++ {
  1243. tb, istable := curobj.(*LTable)
  1244. if istable {
  1245. if tb.RawGetString(key) != LNil {
  1246. tb.RawSetString(key, value)
  1247. return
  1248. }
  1249. }
  1250. metaindex := ls.metaOp1(curobj, "__newindex")
  1251. if metaindex == LNil {
  1252. if !istable {
  1253. ls.RaiseError("attempt to index a non-table object(%v) with key '%s'", curobj.Type().String(), key)
  1254. }
  1255. tb.RawSetString(key, value)
  1256. return
  1257. }
  1258. if metaindex.Type() == LTFunction {
  1259. ls.reg.Push(metaindex)
  1260. ls.reg.Push(curobj)
  1261. ls.reg.Push(LString(key))
  1262. ls.reg.Push(value)
  1263. ls.Call(3, 0)
  1264. return
  1265. } else {
  1266. curobj = metaindex
  1267. }
  1268. }
  1269. ls.RaiseError("too many recursions in settable")
  1270. }
  1271. /* }}} */
  1272. /* api methods {{{ */
  1273. func NewState(opts ...Options) *LState {
  1274. var ls *LState
  1275. if len(opts) == 0 {
  1276. ls = newLState(Options{
  1277. CallStackSize: CallStackSize,
  1278. RegistrySize: RegistrySize,
  1279. })
  1280. ls.OpenLibs()
  1281. } else {
  1282. if opts[0].CallStackSize < 1 {
  1283. opts[0].CallStackSize = CallStackSize
  1284. }
  1285. if opts[0].RegistrySize < 128 {
  1286. opts[0].RegistrySize = RegistrySize
  1287. }
  1288. if opts[0].RegistryMaxSize < opts[0].RegistrySize {
  1289. opts[0].RegistryMaxSize = 0 // disable growth if max size is smaller than initial size
  1290. } else {
  1291. // if growth enabled, grow step is set
  1292. if opts[0].RegistryGrowStep < 1 {
  1293. opts[0].RegistryGrowStep = RegistryGrowStep
  1294. }
  1295. }
  1296. ls = newLState(opts[0])
  1297. if !opts[0].SkipOpenLibs {
  1298. ls.OpenLibs()
  1299. }
  1300. }
  1301. return ls
  1302. }
  1303. func (ls *LState) IsClosed() bool {
  1304. return ls.stack == nil
  1305. }
  1306. func (ls *LState) Close() {
  1307. atomic.AddInt32(&ls.stop, 1)
  1308. for _, file := range ls.G.tempFiles {
  1309. // ignore errors in these operations
  1310. file.Close()
  1311. os.Remove(file.Name())
  1312. }
  1313. ls.stack.FreeAll()
  1314. ls.stack = nil
  1315. }
  1316. /* registry operations {{{ */
  1317. func (ls *LState) GetTop() int {
  1318. return ls.reg.Top() - ls.currentLocalBase()
  1319. }
  1320. func (ls *LState) SetTop(idx int) {
  1321. base := ls.currentLocalBase()
  1322. newtop := ls.indexToReg(idx) + 1
  1323. if newtop < base {
  1324. ls.reg.SetTop(base)
  1325. } else {
  1326. ls.reg.SetTop(newtop)
  1327. }
  1328. }
  1329. func (ls *LState) Replace(idx int, value LValue) {
  1330. base := ls.currentLocalBase()
  1331. if idx > 0 {
  1332. reg := base + idx - 1
  1333. if reg < ls.reg.Top() {
  1334. ls.reg.Set(reg, value)
  1335. }
  1336. } else if idx == 0 {
  1337. } else if idx > RegistryIndex {
  1338. if tidx := ls.reg.Top() + idx; tidx >= base {
  1339. ls.reg.Set(tidx, value)
  1340. }
  1341. } else {
  1342. switch idx {
  1343. case RegistryIndex:
  1344. if tb, ok := value.(*LTable); ok {
  1345. ls.G.Registry = tb
  1346. } else {
  1347. ls.RaiseError("registry must be a table(%v)", value.Type().String())
  1348. }
  1349. case EnvironIndex:
  1350. if ls.currentFrame == nil {
  1351. ls.RaiseError("no calling environment")
  1352. }
  1353. if tb, ok := value.(*LTable); ok {
  1354. ls.currentFrame.Fn.Env = tb
  1355. } else {
  1356. ls.RaiseError("environment must be a table(%v)", value.Type().String())
  1357. }
  1358. case GlobalsIndex:
  1359. if tb, ok := value.(*LTable); ok {
  1360. ls.G.Global = tb
  1361. } else {
  1362. ls.RaiseError("_G must be a table(%v)", value.Type().String())
  1363. }
  1364. default:
  1365. fn := ls.currentFrame.Fn
  1366. index := GlobalsIndex - idx - 1
  1367. if index < len(fn.Upvalues) {
  1368. fn.Upvalues[index].SetValue(value)
  1369. }
  1370. }
  1371. }
  1372. }
  1373. func (ls *LState) Get(idx int) LValue {
  1374. base := ls.currentLocalBase()
  1375. if idx > 0 {
  1376. reg := base + idx - 1
  1377. if reg < ls.reg.Top() {
  1378. return ls.reg.Get(reg)
  1379. }
  1380. return LNil
  1381. } else if idx == 0 {
  1382. return LNil
  1383. } else if idx > RegistryIndex {
  1384. tidx := ls.reg.Top() + idx
  1385. if tidx < base {
  1386. return LNil
  1387. }
  1388. return ls.reg.Get(tidx)
  1389. } else {
  1390. switch idx {
  1391. case RegistryIndex:
  1392. return ls.G.Registry
  1393. case EnvironIndex:
  1394. if ls.currentFrame == nil {
  1395. return ls.Env
  1396. }
  1397. return ls.currentFrame.Fn.Env
  1398. case GlobalsIndex:
  1399. return ls.G.Global
  1400. default:
  1401. fn := ls.currentFrame.Fn
  1402. index := GlobalsIndex - idx - 1
  1403. if index < len(fn.Upvalues) {
  1404. return fn.Upvalues[index].Value()
  1405. }
  1406. return LNil
  1407. }
  1408. }
  1409. return LNil
  1410. }
  1411. func (ls *LState) Push(value LValue) {
  1412. ls.reg.Push(value)
  1413. }
  1414. func (ls *LState) Pop(n int) {
  1415. for i := 0; i < n; i++ {
  1416. if ls.GetTop() == 0 {
  1417. ls.RaiseError("register underflow")
  1418. }
  1419. ls.reg.Pop()
  1420. }
  1421. }
  1422. func (ls *LState) Insert(value LValue, index int) {
  1423. reg := ls.indexToReg(index)
  1424. top := ls.reg.Top()
  1425. if reg >= top {
  1426. ls.reg.Set(reg, value)
  1427. return
  1428. }
  1429. if reg <= ls.currentLocalBase() {
  1430. reg = ls.currentLocalBase()
  1431. }
  1432. top--
  1433. for ; top >= reg; top-- {
  1434. ls.reg.Set(top+1, ls.reg.Get(top))
  1435. }
  1436. ls.reg.Set(reg, value)
  1437. }
  1438. func (ls *LState) Remove(index int) {
  1439. reg := ls.indexToReg(index)
  1440. top := ls.reg.Top()
  1441. switch {
  1442. case reg >= top:
  1443. return
  1444. case reg < ls.currentLocalBase():
  1445. return
  1446. case reg == top-1:
  1447. ls.Pop(1)
  1448. return
  1449. }
  1450. for i := reg; i < top-1; i++ {
  1451. ls.reg.Set(i, ls.reg.Get(i+1))
  1452. }
  1453. ls.reg.SetTop(top - 1)
  1454. }
  1455. /* }}} */
  1456. /* object allocation {{{ */
  1457. func (ls *LState) NewTable() *LTable {
  1458. return newLTable(defaultArrayCap, defaultHashCap)
  1459. }
  1460. func (ls *LState) CreateTable(acap, hcap int) *LTable {
  1461. return newLTable(acap, hcap)
  1462. }
  1463. // NewThread returns a new LState that shares with the original state all global objects.
  1464. // If the original state has context.Context, the new state has a new child context of the original state and this function returns its cancel function.
  1465. func (ls *LState) NewThread() (*LState, context.CancelFunc) {
  1466. thread := newLState(ls.Options)
  1467. thread.G = ls.G
  1468. thread.Env = ls.Env
  1469. var f context.CancelFunc = nil
  1470. if ls.ctx != nil {
  1471. thread.mainLoop = mainLoopWithContext
  1472. thread.ctx, f = context.WithCancel(ls.ctx)
  1473. thread.ctxCancelFn = f
  1474. }
  1475. return thread, f
  1476. }
  1477. func (ls *LState) NewFunctionFromProto(proto *FunctionProto) *LFunction {
  1478. return newLFunctionL(proto, ls.Env, int(proto.NumUpvalues))
  1479. }
  1480. func (ls *LState) NewUserData() *LUserData {
  1481. return &LUserData{
  1482. Env: ls.currentEnv(),
  1483. Metatable: LNil,
  1484. }
  1485. }
  1486. func (ls *LState) NewFunction(fn LGFunction) *LFunction {
  1487. return newLFunctionG(fn, ls.currentEnv(), 0)
  1488. }
  1489. func (ls *LState) NewClosure(fn LGFunction, upvalues ...LValue) *LFunction {
  1490. cl := newLFunctionG(fn, ls.currentEnv(), len(upvalues))
  1491. for i, lv := range upvalues {
  1492. cl.Upvalues[i] = &Upvalue{}
  1493. cl.Upvalues[i].Close()
  1494. cl.Upvalues[i].SetValue(lv)
  1495. }
  1496. return cl
  1497. }
  1498. /* }}} */
  1499. /* toType {{{ */
  1500. func (ls *LState) ToBool(n int) bool {
  1501. return LVAsBool(ls.Get(n))
  1502. }
  1503. func (ls *LState) ToInt(n int) int {
  1504. if lv, ok := ls.Get(n).(LNumber); ok {
  1505. return int(lv)
  1506. }
  1507. if lv, ok := ls.Get(n).(LString); ok {
  1508. if num, err := parseNumber(string(lv)); err == nil {
  1509. return int(num)
  1510. }
  1511. }
  1512. return 0
  1513. }
  1514. func (ls *LState) ToInt64(n int) int64 {
  1515. if lv, ok := ls.Get(n).(LNumber); ok {
  1516. return int64(lv)
  1517. }
  1518. if lv, ok := ls.Get(n).(LString); ok {
  1519. if num, err := parseNumber(string(lv)); err == nil {
  1520. return int64(num)
  1521. }
  1522. }
  1523. return 0
  1524. }
  1525. func (ls *LState) ToNumber(n int) LNumber {
  1526. return LVAsNumber(ls.Get(n))
  1527. }
  1528. func (ls *LState) ToString(n int) string {
  1529. return LVAsString(ls.Get(n))
  1530. }
  1531. func (ls *LState) ToTable(n int) *LTable {
  1532. if lv, ok := ls.Get(n).(*LTable); ok {
  1533. return lv
  1534. }
  1535. return nil
  1536. }
  1537. func (ls *LState) ToFunction(n int) *LFunction {
  1538. if lv, ok := ls.Get(n).(*LFunction); ok {
  1539. return lv
  1540. }
  1541. return nil
  1542. }
  1543. func (ls *LState) ToUserData(n int) *LUserData {
  1544. if lv, ok := ls.Get(n).(*LUserData); ok {
  1545. return lv
  1546. }
  1547. return nil
  1548. }
  1549. func (ls *LState) ToThread(n int) *LState {
  1550. if lv, ok := ls.Get(n).(*LState); ok {
  1551. return lv
  1552. }
  1553. return nil
  1554. }
  1555. /* }}} */
  1556. /* error & debug operations {{{ */
  1557. func (ls *LState) registryOverflow() {
  1558. ls.RaiseError("registry overflow")
  1559. }
  1560. // This function is equivalent to luaL_error( http://www.lua.org/manual/5.1/manual.html#luaL_error ).
  1561. func (ls *LState) RaiseError(format string, args ...interface{}) {
  1562. ls.raiseError(1, format, args...)
  1563. }
  1564. // This function is equivalent to lua_error( http://www.lua.org/manual/5.1/manual.html#lua_error ).
  1565. func (ls *LState) Error(lv LValue, level int) {
  1566. if str, ok := lv.(LString); ok {
  1567. ls.raiseError(level, string(str))
  1568. } else {
  1569. if !ls.hasErrorFunc {
  1570. ls.closeAllUpvalues()
  1571. }
  1572. ls.Push(lv)
  1573. ls.Panic(ls)
  1574. }
  1575. }
  1576. func (ls *LState) GetInfo(what string, dbg *Debug, fn LValue) (LValue, error) {
  1577. if !strings.HasPrefix(what, ">") {
  1578. fn = dbg.frame.Fn
  1579. } else {
  1580. what = what[1:]
  1581. }
  1582. f, ok := fn.(*LFunction)
  1583. if !ok {
  1584. return LNil, newApiErrorS(ApiErrorRun, "can not get debug info(an object in not a function)")
  1585. }
  1586. retfn := false
  1587. for _, c := range what {
  1588. switch c {
  1589. case 'f':
  1590. retfn = true
  1591. case 'S':
  1592. if dbg.frame != nil && dbg.frame.Parent == nil {
  1593. dbg.What = "main"
  1594. } else if f.IsG {
  1595. dbg.What = "G"
  1596. } else if dbg.frame != nil && dbg.frame.TailCall > 0 {
  1597. dbg.What = "tail"
  1598. } else {
  1599. dbg.What = "Lua"
  1600. }
  1601. if !f.IsG {
  1602. dbg.Source = f.Proto.SourceName
  1603. dbg.LineDefined = f.Proto.LineDefined
  1604. dbg.LastLineDefined = f.Proto.LastLineDefined
  1605. }
  1606. case 'l':
  1607. if !f.IsG && dbg.frame != nil {
  1608. if dbg.frame.Pc > 0 {
  1609. dbg.CurrentLine = f.Proto.DbgSourcePositions[dbg.frame.Pc-1]
  1610. }
  1611. } else {
  1612. dbg.CurrentLine = -1
  1613. }
  1614. case 'u':
  1615. dbg.NUpvalues = len(f.Upvalues)
  1616. case 'n':
  1617. if dbg.frame != nil {
  1618. dbg.Name = ls.rawFrameFuncName(dbg.frame)
  1619. }
  1620. default:
  1621. return LNil, newApiErrorS(ApiErrorRun, "invalid what: "+string(c))
  1622. }
  1623. }
  1624. if retfn {
  1625. return f, nil
  1626. }
  1627. return LNil, nil
  1628. }
  1629. func (ls *LState) GetStack(level int) (*Debug, bool) {
  1630. frame := ls.currentFrame
  1631. for ; level > 0 && frame != nil; frame = frame.Parent {
  1632. level--
  1633. if !frame.Fn.IsG {
  1634. level -= frame.TailCall
  1635. }
  1636. }
  1637. if level == 0 && frame != nil {
  1638. return &Debug{frame: frame}, true
  1639. } else if level < 0 && ls.stack.Sp() > 0 {
  1640. return &Debug{frame: ls.stack.At(0)}, true
  1641. }
  1642. return &Debug{}, false
  1643. }
  1644. func (ls *LState) GetLocal(dbg *Debug, no int) (string, LValue) {
  1645. frame := dbg.frame
  1646. if name := ls.findLocal(frame, no); len(name) > 0 {
  1647. return name, ls.reg.Get(frame.LocalBase + no - 1)
  1648. }
  1649. return "", LNil
  1650. }
  1651. func (ls *LState) SetLocal(dbg *Debug, no int, lv LValue) string {
  1652. frame := dbg.frame
  1653. if name := ls.findLocal(frame, no); len(name) > 0 {
  1654. ls.reg.Set(frame.LocalBase+no-1, lv)
  1655. return name
  1656. }
  1657. return ""
  1658. }
  1659. func (ls *LState) GetUpvalue(fn *LFunction, no int) (string, LValue) {
  1660. if fn.IsG {
  1661. return "", LNil
  1662. }
  1663. no--
  1664. if no >= 0 && no < len(fn.Upvalues) {
  1665. return fn.Proto.DbgUpvalues[no], fn.Upvalues[no].Value()
  1666. }
  1667. return "", LNil
  1668. }
  1669. func (ls *LState) SetUpvalue(fn *LFunction, no int, lv LValue) string {
  1670. if fn.IsG {
  1671. return ""
  1672. }
  1673. no--
  1674. if no >= 0 && no < len(fn.Upvalues) {
  1675. fn.Upvalues[no].SetValue(lv)
  1676. return fn.Proto.DbgUpvalues[no]
  1677. }
  1678. return ""
  1679. }
  1680. /* }}} */
  1681. /* env operations {{{ */
  1682. func (ls *LState) GetFEnv(obj LValue) LValue {
  1683. switch lv := obj.(type) {
  1684. case *LFunction:
  1685. return lv.Env
  1686. case *LUserData:
  1687. return lv.Env
  1688. case *LState:
  1689. return lv.Env
  1690. }
  1691. return LNil
  1692. }
  1693. func (ls *LState) SetFEnv(obj LValue, env LValue) {
  1694. tb, ok := env.(*LTable)
  1695. if !ok {
  1696. ls.RaiseError("cannot use %v as an environment", env.Type().String())
  1697. }
  1698. switch lv := obj.(type) {
  1699. case *LFunction:
  1700. lv.Env = tb
  1701. case *LUserData:
  1702. lv.Env = tb
  1703. case *LState:
  1704. lv.Env = tb
  1705. }
  1706. /* do nothing */
  1707. }
  1708. /* }}} */
  1709. /* table operations {{{ */
  1710. func (ls *LState) RawGet(tb *LTable, key LValue) LValue {
  1711. return tb.RawGet(key)
  1712. }
  1713. func (ls *LState) RawGetInt(tb *LTable, key int) LValue {
  1714. return tb.RawGetInt(key)
  1715. }
  1716. func (ls *LState) GetField(obj LValue, skey string) LValue {
  1717. return ls.getFieldString(obj, skey)
  1718. }
  1719. func (ls *LState) GetTable(obj LValue, key LValue) LValue {
  1720. return ls.getField(obj, key)
  1721. }
  1722. func (ls *LState) RawSet(tb *LTable, key LValue, value LValue) {
  1723. if n, ok := key.(LNumber); ok && math.IsNaN(float64(n)) {
  1724. ls.RaiseError("table index is NaN")
  1725. } else if key == LNil {
  1726. ls.RaiseError("table index is nil")
  1727. }
  1728. tb.RawSet(key, value)
  1729. }
  1730. func (ls *LState) RawSetInt(tb *LTable, key int, value LValue) {
  1731. tb.RawSetInt(key, value)
  1732. }
  1733. func (ls *LState) SetField(obj LValue, key string, value LValue) {
  1734. ls.setFieldString(obj, key, value)
  1735. }
  1736. func (ls *LState) SetTable(obj LValue, key LValue, value LValue) {
  1737. ls.setField(obj, key, value)
  1738. }
  1739. func (ls *LState) ForEach(tb *LTable, cb func(LValue, LValue)) {
  1740. tb.ForEach(cb)
  1741. }
  1742. func (ls *LState) GetGlobal(name string) LValue {
  1743. return ls.GetField(ls.Get(GlobalsIndex), name)
  1744. }
  1745. func (ls *LState) SetGlobal(name string, value LValue) {
  1746. ls.SetField(ls.Get(GlobalsIndex), name, value)
  1747. }
  1748. func (ls *LState) Next(tb *LTable, key LValue) (LValue, LValue) {
  1749. return tb.Next(key)
  1750. }
  1751. /* }}} */
  1752. /* unary operations {{{ */
  1753. func (ls *LState) ObjLen(v1 LValue) int {
  1754. if v1.Type() == LTString {
  1755. return len(string(v1.(LString)))
  1756. }
  1757. op := ls.metaOp1(v1, "__len")
  1758. if op.Type() == LTFunction {
  1759. ls.Push(op)
  1760. ls.Push(v1)
  1761. ls.Call(1, 1)
  1762. ret := ls.reg.Pop()
  1763. if ret.Type() == LTNumber {
  1764. return int(ret.(LNumber))
  1765. }
  1766. } else if v1.Type() == LTTable {
  1767. return v1.(*LTable).Len()
  1768. }
  1769. return 0
  1770. }
  1771. /* }}} */
  1772. /* binary operations {{{ */
  1773. func (ls *LState) Concat(values ...LValue) string {
  1774. top := ls.reg.Top()
  1775. for _, value := range values {
  1776. ls.reg.Push(value)
  1777. }
  1778. ret := stringConcat(ls, len(values), ls.reg.Top()-1)
  1779. ls.reg.SetTop(top)
  1780. return LVAsString(ret)
  1781. }
  1782. func (ls *LState) LessThan(lhs, rhs LValue) bool {
  1783. return lessThan(ls, lhs, rhs)
  1784. }
  1785. func (ls *LState) Equal(lhs, rhs LValue) bool {
  1786. return equals(ls, lhs, rhs, false)
  1787. }
  1788. func (ls *LState) RawEqual(lhs, rhs LValue) bool {
  1789. return equals(ls, lhs, rhs, true)
  1790. }
  1791. /* }}} */
  1792. /* register operations {{{ */
  1793. func (ls *LState) Register(name string, fn LGFunction) {
  1794. ls.SetGlobal(name, ls.NewFunction(fn))
  1795. }
  1796. /* }}} */
  1797. /* load and function call operations {{{ */
  1798. func (ls *LState) Load(reader io.Reader, name string) (*LFunction, error) {
  1799. chunk, err := parse.Parse(reader, name)
  1800. if err != nil {
  1801. return nil, newApiErrorE(ApiErrorSyntax, err)
  1802. }
  1803. proto, err := Compile(chunk, name)
  1804. if err != nil {
  1805. return nil, newApiErrorE(ApiErrorSyntax, err)
  1806. }
  1807. return newLFunctionL(proto, ls.currentEnv(), 0), nil
  1808. }
  1809. func (ls *LState) Call(nargs, nret int) {
  1810. ls.callR(nargs, nret, -1)
  1811. }
  1812. func (ls *LState) PCall(nargs, nret int, errfunc *LFunction) (err error) {
  1813. err = nil
  1814. sp := ls.stack.Sp()
  1815. base := ls.reg.Top() - nargs - 1
  1816. oldpanic := ls.Panic
  1817. ls.Panic = panicWithoutTraceback
  1818. if errfunc != nil {
  1819. ls.hasErrorFunc = true
  1820. }
  1821. defer func() {
  1822. ls.Panic = oldpanic
  1823. ls.hasErrorFunc = false
  1824. rcv := recover()
  1825. if rcv != nil {
  1826. if _, ok := rcv.(*ApiError); !ok {
  1827. err = newApiErrorS(ApiErrorPanic, fmt.Sprint(rcv))
  1828. if ls.Options.IncludeGoStackTrace {
  1829. buf := make([]byte, 4096)
  1830. runtime.Stack(buf, false)
  1831. err.(*ApiError).StackTrace = strings.Trim(string(buf), "\000") + "\n" + ls.stackTrace(0)
  1832. }
  1833. } else {
  1834. err = rcv.(*ApiError)
  1835. }
  1836. if errfunc != nil {
  1837. ls.Push(errfunc)
  1838. ls.Push(err.(*ApiError).Object)
  1839. ls.Panic = panicWithoutTraceback
  1840. defer func() {
  1841. ls.Panic = oldpanic
  1842. rcv := recover()
  1843. if rcv != nil {
  1844. if _, ok := rcv.(*ApiError); !ok {
  1845. err = newApiErrorS(ApiErrorPanic, fmt.Sprint(rcv))
  1846. if ls.Options.IncludeGoStackTrace {
  1847. buf := make([]byte, 4096)
  1848. runtime.Stack(buf, false)
  1849. err.(*ApiError).StackTrace = strings.Trim(string(buf), "\000") + ls.stackTrace(0)
  1850. }
  1851. } else {
  1852. err = rcv.(*ApiError)
  1853. err.(*ApiError).StackTrace = ls.stackTrace(0)
  1854. }
  1855. ls.stack.SetSp(sp)
  1856. ls.currentFrame = ls.stack.Last()
  1857. ls.reg.SetTop(base)
  1858. }
  1859. }()
  1860. ls.Call(1, 1)
  1861. err = newApiError(ApiErrorError, ls.Get(-1))
  1862. } else if len(err.(*ApiError).StackTrace) == 0 {
  1863. err.(*ApiError).StackTrace = ls.stackTrace(0)
  1864. }
  1865. ls.stack.SetSp(sp)
  1866. ls.currentFrame = ls.stack.Last()
  1867. ls.reg.SetTop(base)
  1868. }
  1869. ls.stack.SetSp(sp)
  1870. if sp == 0 {
  1871. ls.currentFrame = nil
  1872. }
  1873. }()
  1874. ls.Call(nargs, nret)
  1875. return
  1876. }
  1877. func (ls *LState) GPCall(fn LGFunction, data LValue) error {
  1878. ls.Push(newLFunctionG(fn, ls.currentEnv(), 0))
  1879. ls.Push(data)
  1880. return ls.PCall(1, MultRet, nil)
  1881. }
  1882. func (ls *LState) CallByParam(cp P, args ...LValue) error {
  1883. ls.Push(cp.Fn)
  1884. for _, arg := range args {
  1885. ls.Push(arg)
  1886. }
  1887. if cp.Protect {
  1888. return ls.PCall(len(args), cp.NRet, cp.Handler)
  1889. }
  1890. ls.Call(len(args), cp.NRet)
  1891. return nil
  1892. }
  1893. /* }}} */
  1894. /* metatable operations {{{ */
  1895. func (ls *LState) GetMetatable(obj LValue) LValue {
  1896. return ls.metatable(obj, false)
  1897. }
  1898. func (ls *LState) SetMetatable(obj LValue, mt LValue) {
  1899. switch mt.(type) {
  1900. case *LNilType, *LTable:
  1901. default:
  1902. ls.RaiseError("metatable must be a table or nil, but got %v", mt.Type().String())
  1903. }
  1904. switch v := obj.(type) {
  1905. case *LTable:
  1906. v.Metatable = mt
  1907. case *LUserData:
  1908. v.Metatable = mt
  1909. default:
  1910. ls.G.builtinMts[int(obj.Type())] = mt
  1911. }
  1912. }
  1913. /* }}} */
  1914. /* coroutine operations {{{ */
  1915. func (ls *LState) Status(th *LState) string {
  1916. status := "suspended"
  1917. if th.Dead {
  1918. status = "dead"
  1919. } else if ls.G.CurrentThread == th {
  1920. status = "running"
  1921. } else if ls.Parent == th {
  1922. status = "normal"
  1923. }
  1924. return status
  1925. }
  1926. func (ls *LState) Resume(th *LState, fn *LFunction, args ...LValue) (ResumeState, error, []LValue) {
  1927. isstarted := th.isStarted()
  1928. if !isstarted {
  1929. base := 0
  1930. th.stack.Push(callFrame{
  1931. Fn: fn,
  1932. Pc: 0,
  1933. Base: base,
  1934. LocalBase: base + 1,
  1935. ReturnBase: base,
  1936. NArgs: 0,
  1937. NRet: MultRet,
  1938. Parent: nil,
  1939. TailCall: 0,
  1940. })
  1941. }
  1942. if ls.G.CurrentThread == th {
  1943. return ResumeError, newApiErrorS(ApiErrorRun, "can not resume a running thread"), nil
  1944. }
  1945. if th.Dead {
  1946. return ResumeError, newApiErrorS(ApiErrorRun, "can not resume a dead thread"), nil
  1947. }
  1948. th.Parent = ls
  1949. ls.G.CurrentThread = th
  1950. if !isstarted {
  1951. cf := th.stack.Last()
  1952. th.currentFrame = cf
  1953. th.SetTop(0)
  1954. for _, arg := range args {
  1955. th.Push(arg)
  1956. }
  1957. cf.NArgs = len(args)
  1958. th.initCallFrame(cf)
  1959. th.Panic = panicWithoutTraceback
  1960. } else {
  1961. for _, arg := range args {
  1962. th.Push(arg)
  1963. }
  1964. }
  1965. top := ls.GetTop()
  1966. threadRun(th)
  1967. haserror := LVIsFalse(ls.Get(top + 1))
  1968. ret := make([]LValue, 0, ls.GetTop())
  1969. for idx := top + 2; idx <= ls.GetTop(); idx++ {
  1970. ret = append(ret, ls.Get(idx))
  1971. }
  1972. if len(ret) == 0 {
  1973. ret = append(ret, LNil)
  1974. }
  1975. ls.SetTop(top)
  1976. if haserror {
  1977. return ResumeError, newApiError(ApiErrorRun, ret[0]), nil
  1978. } else if th.stack.IsEmpty() {
  1979. return ResumeOK, nil, ret
  1980. }
  1981. return ResumeYield, nil, ret
  1982. }
  1983. func (ls *LState) Yield(values ...LValue) int {
  1984. ls.SetTop(0)
  1985. for _, lv := range values {
  1986. ls.Push(lv)
  1987. }
  1988. return -1
  1989. }
  1990. func (ls *LState) XMoveTo(other *LState, n int) {
  1991. if ls == other {
  1992. return
  1993. }
  1994. top := ls.GetTop()
  1995. n = intMin(n, top)
  1996. for i := n; i > 0; i-- {
  1997. other.Push(ls.Get(top - i + 1))
  1998. }
  1999. ls.SetTop(top - n)
  2000. }
  2001. /* }}} */
  2002. /* GopherLua original APIs {{{ */
  2003. // Set maximum memory size. This function can only be called from the main thread.
  2004. func (ls *LState) SetMx(mx int) {
  2005. if ls.Parent != nil {
  2006. ls.RaiseError("sub threads are not allowed to set a memory limit")
  2007. }
  2008. go func() {
  2009. limit := uint64(mx * 1024 * 1024) //MB
  2010. var s runtime.MemStats
  2011. for atomic.LoadInt32(&ls.stop) == 0 {
  2012. runtime.ReadMemStats(&s)
  2013. if s.Alloc >= limit {
  2014. fmt.Println("out of memory")
  2015. os.Exit(3)
  2016. }
  2017. time.Sleep(100 * time.Millisecond)
  2018. }
  2019. }()
  2020. }
  2021. // SetContext set a context ctx to this LState. The provided ctx must be non-nil.
  2022. func (ls *LState) SetContext(ctx context.Context) {
  2023. ls.mainLoop = mainLoopWithContext
  2024. ls.ctx = ctx
  2025. }
  2026. // Context returns the LState's context. To change the context, use WithContext.
  2027. func (ls *LState) Context() context.Context {
  2028. return ls.ctx
  2029. }
  2030. // RemoveContext removes the context associated with this LState and returns this context.
  2031. func (ls *LState) RemoveContext() context.Context {
  2032. oldctx := ls.ctx
  2033. ls.mainLoop = mainLoop
  2034. ls.ctx = nil
  2035. return oldctx
  2036. }
  2037. // Converts the Lua value at the given acceptable index to the chan LValue.
  2038. func (ls *LState) ToChannel(n int) chan LValue {
  2039. if lv, ok := ls.Get(n).(LChannel); ok {
  2040. return (chan LValue)(lv)
  2041. }
  2042. return nil
  2043. }
  2044. // RemoveCallerFrame removes the stack frame above the current stack frame. This is useful in tail calls. It returns
  2045. // the new current frame.
  2046. func (ls *LState) RemoveCallerFrame() *callFrame {
  2047. cs := ls.stack
  2048. sp := cs.Sp()
  2049. parentFrame := cs.At(sp - 2)
  2050. currentFrame := cs.At(sp - 1)
  2051. parentsParentFrame := parentFrame.Parent
  2052. *parentFrame = *currentFrame
  2053. parentFrame.Parent = parentsParentFrame
  2054. parentFrame.Idx = sp - 2
  2055. cs.Pop()
  2056. return parentFrame
  2057. }
  2058. /* }}} */
  2059. /* }}} */
  2060. //