From 993aca2c5f62b3c839c133f96b07f8f26e4451c7 Mon Sep 17 00:00:00 2001 From: Teoxoy <28601907+Teoxoy@users.noreply.github.com> Date: Fri, 22 Nov 2019 20:41:18 +0100 Subject: [PATCH] change API to allow for parsing code once then executing multiple times --- README.md | 12 +++++++----- package.json | 2 +- src/index.ts | 32 +++++++++++++++++++++----------- tests/test.js | 8 ++++---- 4 files changed, 33 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 02ada4a..e542184 100644 --- a/README.md +++ b/README.md @@ -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) ``` diff --git a/package.json b/package.json index edc82d9..43303ca 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.ts b/src/index.ts index 324b138..7e4fccf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 } } diff --git a/tests/test.js b/tests/test.js index 3782604..062c99d 100644 --- a/tests/test.js +++ b/tests/test.js @@ -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!") }