1
0

auxlib.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. package lua
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io"
  6. "os"
  7. "strings"
  8. )
  9. /* checkType {{{ */
  10. func (ls *LState) CheckAny(n int) LValue {
  11. if n > ls.GetTop() {
  12. ls.ArgError(n, "value expected")
  13. }
  14. return ls.Get(n)
  15. }
  16. func (ls *LState) CheckInt(n int) int {
  17. v := ls.Get(n)
  18. if intv, ok := v.(LNumber); ok {
  19. return int(intv)
  20. }
  21. ls.TypeError(n, LTNumber)
  22. return 0
  23. }
  24. func (ls *LState) CheckInt64(n int) int64 {
  25. v := ls.Get(n)
  26. if intv, ok := v.(LNumber); ok {
  27. return int64(intv)
  28. }
  29. ls.TypeError(n, LTNumber)
  30. return 0
  31. }
  32. func (ls *LState) CheckNumber(n int) LNumber {
  33. v := ls.Get(n)
  34. if lv, ok := v.(LNumber); ok {
  35. return lv
  36. }
  37. if lv, ok := v.(LString); ok {
  38. if num, err := parseNumber(string(lv)); err == nil {
  39. return num
  40. }
  41. }
  42. ls.TypeError(n, LTNumber)
  43. return 0
  44. }
  45. func (ls *LState) CheckString(n int) string {
  46. v := ls.Get(n)
  47. if lv, ok := v.(LString); ok {
  48. return string(lv)
  49. } else if LVCanConvToString(v) {
  50. return ls.ToString(n)
  51. }
  52. ls.TypeError(n, LTString)
  53. return ""
  54. }
  55. func (ls *LState) CheckBool(n int) bool {
  56. v := ls.Get(n)
  57. if lv, ok := v.(LBool); ok {
  58. return bool(lv)
  59. }
  60. ls.TypeError(n, LTBool)
  61. return false
  62. }
  63. func (ls *LState) CheckTable(n int) *LTable {
  64. v := ls.Get(n)
  65. if lv, ok := v.(*LTable); ok {
  66. return lv
  67. }
  68. ls.TypeError(n, LTTable)
  69. return nil
  70. }
  71. func (ls *LState) CheckFunction(n int) *LFunction {
  72. v := ls.Get(n)
  73. if lv, ok := v.(*LFunction); ok {
  74. return lv
  75. }
  76. ls.TypeError(n, LTFunction)
  77. return nil
  78. }
  79. func (ls *LState) CheckUserData(n int) *LUserData {
  80. v := ls.Get(n)
  81. if lv, ok := v.(*LUserData); ok {
  82. return lv
  83. }
  84. ls.TypeError(n, LTUserData)
  85. return nil
  86. }
  87. func (ls *LState) CheckThread(n int) *LState {
  88. v := ls.Get(n)
  89. if lv, ok := v.(*LState); ok {
  90. return lv
  91. }
  92. ls.TypeError(n, LTThread)
  93. return nil
  94. }
  95. func (ls *LState) CheckType(n int, typ LValueType) {
  96. v := ls.Get(n)
  97. if v.Type() != typ {
  98. ls.TypeError(n, typ)
  99. }
  100. }
  101. func (ls *LState) CheckTypes(n int, typs ...LValueType) {
  102. vt := ls.Get(n).Type()
  103. for _, typ := range typs {
  104. if vt == typ {
  105. return
  106. }
  107. }
  108. buf := []string{}
  109. for _, typ := range typs {
  110. buf = append(buf, typ.String())
  111. }
  112. ls.ArgError(n, strings.Join(buf, " or ")+" expected, got "+ls.Get(n).Type().String())
  113. }
  114. func (ls *LState) CheckOption(n int, options []string) int {
  115. str := ls.CheckString(n)
  116. for i, v := range options {
  117. if v == str {
  118. return i
  119. }
  120. }
  121. ls.ArgError(n, fmt.Sprintf("invalid option: %s (must be one of %s)", str, strings.Join(options, ",")))
  122. return 0
  123. }
  124. /* }}} */
  125. /* optType {{{ */
  126. func (ls *LState) OptInt(n int, d int) int {
  127. v := ls.Get(n)
  128. if v == LNil {
  129. return d
  130. }
  131. if intv, ok := v.(LNumber); ok {
  132. return int(intv)
  133. }
  134. ls.TypeError(n, LTNumber)
  135. return 0
  136. }
  137. func (ls *LState) OptInt64(n int, d int64) int64 {
  138. v := ls.Get(n)
  139. if v == LNil {
  140. return d
  141. }
  142. if intv, ok := v.(LNumber); ok {
  143. return int64(intv)
  144. }
  145. ls.TypeError(n, LTNumber)
  146. return 0
  147. }
  148. func (ls *LState) OptNumber(n int, d LNumber) LNumber {
  149. v := ls.Get(n)
  150. if v == LNil {
  151. return d
  152. }
  153. if lv, ok := v.(LNumber); ok {
  154. return lv
  155. }
  156. ls.TypeError(n, LTNumber)
  157. return 0
  158. }
  159. func (ls *LState) OptString(n int, d string) string {
  160. v := ls.Get(n)
  161. if v == LNil {
  162. return d
  163. }
  164. if lv, ok := v.(LString); ok {
  165. return string(lv)
  166. }
  167. ls.TypeError(n, LTString)
  168. return ""
  169. }
  170. func (ls *LState) OptBool(n int, d bool) bool {
  171. v := ls.Get(n)
  172. if v == LNil {
  173. return d
  174. }
  175. if lv, ok := v.(LBool); ok {
  176. return bool(lv)
  177. }
  178. ls.TypeError(n, LTBool)
  179. return false
  180. }
  181. func (ls *LState) OptTable(n int, d *LTable) *LTable {
  182. v := ls.Get(n)
  183. if v == LNil {
  184. return d
  185. }
  186. if lv, ok := v.(*LTable); ok {
  187. return lv
  188. }
  189. ls.TypeError(n, LTTable)
  190. return nil
  191. }
  192. func (ls *LState) OptFunction(n int, d *LFunction) *LFunction {
  193. v := ls.Get(n)
  194. if v == LNil {
  195. return d
  196. }
  197. if lv, ok := v.(*LFunction); ok {
  198. return lv
  199. }
  200. ls.TypeError(n, LTFunction)
  201. return nil
  202. }
  203. func (ls *LState) OptUserData(n int, d *LUserData) *LUserData {
  204. v := ls.Get(n)
  205. if v == LNil {
  206. return d
  207. }
  208. if lv, ok := v.(*LUserData); ok {
  209. return lv
  210. }
  211. ls.TypeError(n, LTUserData)
  212. return nil
  213. }
  214. /* }}} */
  215. /* error operations {{{ */
  216. func (ls *LState) ArgError(n int, message string) {
  217. ls.RaiseError("bad argument #%v to %v (%v)", n, ls.rawFrameFuncName(ls.currentFrame), message)
  218. }
  219. func (ls *LState) TypeError(n int, typ LValueType) {
  220. ls.RaiseError("bad argument #%v to %v (%v expected, got %v)", n, ls.rawFrameFuncName(ls.currentFrame), typ.String(), ls.Get(n).Type().String())
  221. }
  222. /* }}} */
  223. /* debug operations {{{ */
  224. func (ls *LState) Where(level int) string {
  225. return ls.where(level, false)
  226. }
  227. /* }}} */
  228. /* table operations {{{ */
  229. func (ls *LState) FindTable(obj *LTable, n string, size int) LValue {
  230. names := strings.Split(n, ".")
  231. curobj := obj
  232. for _, name := range names {
  233. if curobj.Type() != LTTable {
  234. return LNil
  235. }
  236. nextobj := ls.RawGet(curobj, LString(name))
  237. if nextobj == LNil {
  238. tb := ls.CreateTable(0, size)
  239. ls.RawSet(curobj, LString(name), tb)
  240. curobj = tb
  241. } else if nextobj.Type() != LTTable {
  242. return LNil
  243. } else {
  244. curobj = nextobj.(*LTable)
  245. }
  246. }
  247. return curobj
  248. }
  249. /* }}} */
  250. /* register operations {{{ */
  251. func (ls *LState) RegisterModule(name string, funcs map[string]LGFunction) LValue {
  252. tb := ls.FindTable(ls.Get(RegistryIndex).(*LTable), "_LOADED", 1)
  253. mod := ls.GetField(tb, name)
  254. if mod.Type() != LTTable {
  255. newmod := ls.FindTable(ls.Get(GlobalsIndex).(*LTable), name, len(funcs))
  256. if newmodtb, ok := newmod.(*LTable); !ok {
  257. ls.RaiseError("name conflict for module(%v)", name)
  258. } else {
  259. for fname, fn := range funcs {
  260. newmodtb.RawSetString(fname, ls.NewFunction(fn))
  261. }
  262. ls.SetField(tb, name, newmodtb)
  263. return newmodtb
  264. }
  265. }
  266. return mod
  267. }
  268. func (ls *LState) SetFuncs(tb *LTable, funcs map[string]LGFunction, upvalues ...LValue) *LTable {
  269. for fname, fn := range funcs {
  270. tb.RawSetString(fname, ls.NewClosure(fn, upvalues...))
  271. }
  272. return tb
  273. }
  274. /* }}} */
  275. /* metatable operations {{{ */
  276. func (ls *LState) NewTypeMetatable(typ string) *LTable {
  277. regtable := ls.Get(RegistryIndex)
  278. mt := ls.GetField(regtable, typ)
  279. if tb, ok := mt.(*LTable); ok {
  280. return tb
  281. }
  282. mtnew := ls.NewTable()
  283. ls.SetField(regtable, typ, mtnew)
  284. return mtnew
  285. }
  286. func (ls *LState) GetMetaField(obj LValue, event string) LValue {
  287. return ls.metaOp1(obj, event)
  288. }
  289. func (ls *LState) GetTypeMetatable(typ string) LValue {
  290. return ls.GetField(ls.Get(RegistryIndex), typ)
  291. }
  292. func (ls *LState) CallMeta(obj LValue, event string) LValue {
  293. op := ls.metaOp1(obj, event)
  294. if op.Type() == LTFunction {
  295. ls.reg.Push(op)
  296. ls.reg.Push(obj)
  297. ls.Call(1, 1)
  298. return ls.reg.Pop()
  299. }
  300. return LNil
  301. }
  302. /* }}} */
  303. /* load and function call operations {{{ */
  304. func (ls *LState) LoadFile(path string) (*LFunction, error) {
  305. var file *os.File
  306. var err error
  307. if len(path) == 0 {
  308. file = os.Stdin
  309. } else {
  310. file, err = os.Open(path)
  311. defer file.Close()
  312. if err != nil {
  313. return nil, newApiErrorE(ApiErrorFile, err)
  314. }
  315. }
  316. reader := bufio.NewReader(file)
  317. // get the first character.
  318. c, err := reader.ReadByte()
  319. if err != nil && err != io.EOF {
  320. return nil, newApiErrorE(ApiErrorFile, err)
  321. }
  322. if c == byte('#') {
  323. // Unix exec. file?
  324. // skip first line
  325. _, err, _ = readBufioLine(reader)
  326. if err != nil {
  327. return nil, newApiErrorE(ApiErrorFile, err)
  328. }
  329. }
  330. if err != io.EOF {
  331. // if the file is not empty,
  332. // unread the first character of the file or newline character(readBufioLine's last byte).
  333. err = reader.UnreadByte()
  334. if err != nil {
  335. return nil, newApiErrorE(ApiErrorFile, err)
  336. }
  337. }
  338. return ls.Load(reader, path)
  339. }
  340. func (ls *LState) LoadString(source string) (*LFunction, error) {
  341. return ls.Load(strings.NewReader(source), "<string>")
  342. }
  343. func (ls *LState) DoFile(path string) error {
  344. if fn, err := ls.LoadFile(path); err != nil {
  345. return err
  346. } else {
  347. ls.Push(fn)
  348. return ls.PCall(0, MultRet, nil)
  349. }
  350. }
  351. func (ls *LState) DoString(source string) error {
  352. if fn, err := ls.LoadString(source); err != nil {
  353. return err
  354. } else {
  355. ls.Push(fn)
  356. return ls.PCall(0, MultRet, nil)
  357. }
  358. }
  359. /* }}} */
  360. /* GopherLua original APIs {{{ */
  361. // ToStringMeta returns string representation of given LValue.
  362. // This method calls the `__tostring` meta method if defined.
  363. func (ls *LState) ToStringMeta(lv LValue) LValue {
  364. if fn, ok := ls.metaOp1(lv, "__tostring").(*LFunction); ok {
  365. ls.Push(fn)
  366. ls.Push(lv)
  367. ls.Call(1, 1)
  368. return ls.reg.Pop()
  369. } else {
  370. return LString(lv.String())
  371. }
  372. }
  373. // Set a module loader to the package.preload table.
  374. func (ls *LState) PreloadModule(name string, loader LGFunction) {
  375. preload := ls.GetField(ls.GetField(ls.Get(EnvironIndex), "package"), "preload")
  376. if _, ok := preload.(*LTable); !ok {
  377. ls.RaiseError("package.preload must be a table")
  378. }
  379. ls.SetField(preload, name, ls.NewFunction(loader))
  380. }
  381. // Checks whether the given index is an LChannel and returns this channel.
  382. func (ls *LState) CheckChannel(n int) chan LValue {
  383. v := ls.Get(n)
  384. if ch, ok := v.(LChannel); ok {
  385. return (chan LValue)(ch)
  386. }
  387. ls.TypeError(n, LTChannel)
  388. return nil
  389. }
  390. // If the given index is a LChannel, returns this channel. If this argument is absent or is nil, returns ch. Otherwise, raises an error.
  391. func (ls *LState) OptChannel(n int, ch chan LValue) chan LValue {
  392. v := ls.Get(n)
  393. if v == LNil {
  394. return ch
  395. }
  396. if ch, ok := v.(LChannel); ok {
  397. return (chan LValue)(ch)
  398. }
  399. ls.TypeError(n, LTChannel)
  400. return nil
  401. }
  402. /* }}} */
  403. //