* }}} */ * load and function call operations {{{ */
(path string)
| 350 | /* load and function call operations {{{ */ |
| 351 | |
| 352 | func (ls *LState) LoadFile(path string) (*LFunction, error) { |
| 353 | var file *os.File |
| 354 | var err error |
| 355 | if len(path) == 0 { |
| 356 | file = os.Stdin |
| 357 | } else { |
| 358 | file, err = os.Open(path) |
| 359 | defer file.Close() |
| 360 | if err != nil { |
| 361 | return nil, newApiErrorE(ApiErrorFile, err) |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | reader := bufio.NewReader(file) |
| 366 | // get the first character. |
| 367 | c, err := reader.ReadByte() |
| 368 | if err != nil && err != io.EOF { |
| 369 | return nil, newApiErrorE(ApiErrorFile, err) |
| 370 | } |
| 371 | if c == byte('#') { |
| 372 | // Unix exec. file? |
| 373 | // skip first line |
| 374 | _, err, _ = readBufioLine(reader) |
| 375 | if err != nil { |
| 376 | return nil, newApiErrorE(ApiErrorFile, err) |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | if err != io.EOF { |
| 381 | // if the file is not empty, |
| 382 | // unread the first character of the file or newline character(readBufioLine's last byte). |
| 383 | err = reader.UnreadByte() |
| 384 | if err != nil { |
| 385 | return nil, newApiErrorE(ApiErrorFile, err) |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | return ls.Load(reader, path) |
| 390 | } |
| 391 | |
| 392 | func (ls *LState) LoadString(source string) (*LFunction, error) { |
| 393 | return ls.Load(strings.NewReader(source), "<string>") |