diff --git a/.vscode/settings.json b/.vscode/settings.json index 6692f9b..de75110 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -6,5 +6,10 @@ "eslint.validate": [{ "language": "typescript", "autoFix": true }], "eslint.autoFixOnSave": true, "eslint.alwaysShowStatus": true, - "prettier.disableLanguages": ["ts"] + "prettier.disableLanguages": [ + "ts" + ], + "editor.codeActionsOnSave": { + "source.fixAll.eslint": "explicit" + } } diff --git a/src/index.ts b/src/index.ts index 7e4fccf..8e97546 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,17 +1,17 @@ /* eslint-disable import/order */ /* eslint-disable import/no-duplicates */ +import { LuaError } from './LuaError' import { Scope } from './Scope' -import { createG } from './lib/globals' -import { operators } from './operators' import { Table } from './Table' -import { LuaError } from './LuaError' +import { createG } from './lib/globals' import { libMath } from './lib/math' -import { libTable } from './lib/table' -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 { libString, metatable as stringMetatable } from './lib/string' +import { libTable } from './lib/table' +import { operators } from './operators' import { parse as parseScript } from './parser' +import { Config, LuaType, ensureArray } from './utils' interface Script { exec: () => LuaType @@ -61,13 +61,14 @@ function createEnv( LUA_PATH: './?.lua', stdin: '', stdout: console.log, + encoding: 'x-user-defined', ...config } const _G = createG(cfg, execChunk) const { libPackage, _require } = getLibPackage( - (content, moduleName) => execChunk(_G, parseScript(content), moduleName)[0], + (content, moduleName) => execChunk(_G, parseScript(content, cfg.encoding), moduleName)[0], cfg ) const loaded = libPackage.get('loaded') as Table @@ -111,4 +112,5 @@ function createEnv( // eslint-disable-next-line import/first import * as utils from './utils' -export { createEnv, Table, LuaError, utils } +export { LuaError, Table, createEnv, utils } + diff --git a/src/parser.ts b/src/parser.ts index c3d5caf..6fdcbd6 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -739,12 +739,12 @@ const setExtraInfo = (ast: luaparse.Chunk): void => { visitProp(ast, scopeID, gotoID) } -const parse = (data: string): string => { +const parse = (data: string, encodeing: 'pseudo-latin1' | 'x-user-defined' | 'none' = 'none'): string => { const ast = luaparse.parse(data.replace(/^#.*/, ''), { scope: false, comments: false, luaVersion: '5.3', - encodingMode: 'x-user-defined' + encodingMode: encodeing }) checkGoto(ast) setExtraInfo(ast) @@ -752,3 +752,4 @@ const parse = (data: string): string => { } export { parse } + diff --git a/src/utils.ts b/src/utils.ts index 7f565b4..60531c7 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -10,6 +10,7 @@ interface Config { stdin?: string stdout?: (data: string) => void osExit?: (code: number) => void + encoding?: 'pseudo-latin1' | 'x-user-defined' | 'none' } /** Pattern to identify a float string value that can validly be converted to a number in Lua */ @@ -199,18 +200,19 @@ const hasOwnProperty = (obj: Record | unknown[], key: string | Object.prototype.hasOwnProperty.call(obj, key) export { - LuaType, - Config, - type, - tostring, - posrelat, - coerceToBoolean, - coerceToNumber, - coerceToString, + coerceArgToFunction, coerceArgToNumber, coerceArgToString, coerceArgToTable, - coerceArgToFunction, + coerceToBoolean, + coerceToNumber, + coerceToString, + Config, ensureArray, - hasOwnProperty + hasOwnProperty, + LuaType, + posrelat, + tostring, + type } +