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 ```js
luaEnv.run('print(\'Hello world!\')') const luaScript = luaEnv.parse('print(\'Hello world!\')')
const returnValue = luaScript.exec()
``` ```
```js ```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 ### Create a global library
@ -69,7 +71,7 @@ function helloBuilder(name) {
const myLib = new luainjs.Table({ helloBuilder }) const myLib = new luainjs.Table({ helloBuilder })
luaEnv.loadLib('myLib', myLib) luaEnv.loadLib('myLib', myLib)
const helloStr = luaEnv.run(`return myLib.helloBuilder('John')`) const helloStr = luaEnv.parse(`return myLib.helloBuilder('John')`).exec()
console.log(helloStr) console.log(helloStr)
``` ```

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

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

@ -11,7 +11,7 @@ let exitCode = 0
loadFile: p => fs.readFileSync(path.join(rootPath, p), { encoding: 'utf8' }), loadFile: p => fs.readFileSync(path.join(rootPath, p), { encoding: 'utf8' }),
osExit: code => (exitCode += code) 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"`) // 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' }), loadFile: p => fs.readFileSync(path.join(rootPath, p), { encoding: 'utf8' }),
osExit: code => process.exit(code) osExit: code => process.exit(code)
}) })
luaEnv.runfile('goto.lua') luaEnv.parseFile('goto.lua').exec()
luaEnv.runfile('bwcoercion.lua') luaEnv.parseFile('bwcoercion.lua').exec()
} }
{ {
@ -34,7 +34,7 @@ let exitCode = 0
} }
const myLib = new luainjs.Table({ helloBuilder }) const myLib = new luainjs.Table({ helloBuilder })
luaEnv.loadLib('myLib', myLib) 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!') { if (str !== 'Hello John!') {
throw Error("Strings don't match!") throw Error("Strings don't match!")
} }

Loading…
Cancel
Save