change API to allow for parsing code once then executing multiple times

pull/6/head
Teoxoy 6 years ago
parent b969443812
commit 993aca2c5f

@ -44,17 +44,19 @@ const luaEnv = luainjs.createEnv({
})
```
### Run a script or file
### Execute a script or file
```js
luaEnv.run('print(\'Hello world!\')')
const luaScript = luaEnv.parse('print(\'Hello world!\')')
const returnValue = luaScript.exec()
```
```js
luaEnv.runfile('somefile.lua')
const luaScript = luaEnv.parseFile('somefile.lua')
const returnValue = luaScript.exec()
```
`runfile` uses `config.fileExists` and `config.loadFile`
`parseFile` uses `config.fileExists` and `config.loadFile`
### Create a global library
@ -69,7 +71,7 @@ function helloBuilder(name) {
const myLib = new luainjs.Table({ helloBuilder })
luaEnv.loadLib('myLib', myLib)
const helloStr = luaEnv.run(`return myLib.helloBuilder('John')`)
const helloStr = luaEnv.parse(`return myLib.helloBuilder('John')`).exec()
console.log(helloStr)
```

@ -1,6 +1,6 @@
{
"name": "lua-in-js",
"version": "2.1.1",
"version": "2.2.0",
"description": "A Lua to JS transpiler/runtime",
"keywords": [
"lua",

@ -11,7 +11,11 @@ import { libString, metatable as stringMetatable } from './lib/string'
import { getLibOS } from './lib/os'
import { getLibPackage } from './lib/package'
import { LuaType, ensureArray, Config } from './utils'
import { parse } from './parser'
import { parse as parseScript } from './parser'
interface Script {
exec: () => LuaType
}
const call = (f: Function | Table, ...args: LuaType[]): LuaType[] => {
if (f instanceof Function) return ensureArray(f(...args))
@ -49,8 +53,8 @@ const execChunk = (_G: Table, chunk: string, chunkName?: string): LuaType[] => {
function createEnv(
config: Config = {}
): {
run: (script: string) => LuaType
runfile: (path: string) => LuaType
parse: (script: string) => Script
parseFile: (path: string) => Script
loadLib: (name: string, value: Table) => void
} {
const cfg: Config = {
@ -63,7 +67,7 @@ function createEnv(
const _G = createG(cfg, execChunk)
const { libPackage, _require } = getLibPackage(
(content, moduleName) => execChunk(_G, parse(content), moduleName)[0],
(content, moduleName) => execChunk(_G, parseScript(content), moduleName)[0],
cfg
)
const loaded = libPackage.get('loaded') as Table
@ -82,19 +86,25 @@ function createEnv(
_G.rawset('require', _require)
const run = (script: string): LuaType => execChunk(_G, parse(script))[0]
const runfile = (filename: string): LuaType => {
if (!cfg.fileExists) throw new LuaError('runfile requires the config.fileExists function')
if (!cfg.loadFile) throw new LuaError('runfile requires the config.loadFile function')
const parse = (code: string): Script => {
const script = parseScript(code)
return {
exec: () => execChunk(_G, script)[0]
}
}
const parseFile = (filename: string): Script => {
if (!cfg.fileExists) throw new LuaError('parseFile requires the config.fileExists function')
if (!cfg.loadFile) throw new LuaError('parseFile requires the config.loadFile function')
if (!cfg.fileExists(filename)) throw new LuaError('file not found')
return run(cfg.loadFile(filename))
return parse(cfg.loadFile(filename))
}
return {
run,
runfile,
parse,
parseFile,
loadLib
}
}

@ -11,7 +11,7 @@ let exitCode = 0
loadFile: p => fs.readFileSync(path.join(rootPath, p), { encoding: 'utf8' }),
osExit: code => (exitCode += code)
})
luaEnv.runfile('test-runner.lua')
luaEnv.parseFile('test-runner.lua').exec()
}
// TODO: make more official lua 5.3 tests pass (most of them don't pass because they `require "debug"`)
@ -22,8 +22,8 @@ let exitCode = 0
loadFile: p => fs.readFileSync(path.join(rootPath, p), { encoding: 'utf8' }),
osExit: code => process.exit(code)
})
luaEnv.runfile('goto.lua')
luaEnv.runfile('bwcoercion.lua')
luaEnv.parseFile('goto.lua').exec()
luaEnv.parseFile('bwcoercion.lua').exec()
}
{
@ -34,7 +34,7 @@ let exitCode = 0
}
const myLib = new luainjs.Table({ helloBuilder })
luaEnv.loadLib('myLib', myLib)
const str = luaEnv.run(`return myLib.helloBuilder('John')`)
const str = luaEnv.parse(`return myLib.helloBuilder('John')`).exec()
if (str !== 'Hello John!') {
throw Error("Strings don't match!")
}

Loading…
Cancel
Save