From 3d6f35a5798a5eb82a4caf33c4033b61eff4c03e Mon Sep 17 00:00:00 2001 From: Teoxoy <28601907+Teoxoy@users.noreply.github.com> Date: Thu, 21 Nov 2019 16:59:13 +0100 Subject: [PATCH] move getn and tostring out of Table file --- src/Table.ts | 82 ++++++++++++++++------------------------------ src/lib/globals.ts | 5 +-- src/lib/string.ts | 4 +-- src/lib/table.ts | 11 +++++-- src/operators.ts | 4 +-- src/utils.ts | 25 ++++++++++++++ 6 files changed, 68 insertions(+), 63 deletions(-) diff --git a/src/Table.ts b/src/Table.ts index dfcbb48..77a2378 100644 --- a/src/Table.ts +++ b/src/Table.ts @@ -1,4 +1,4 @@ -import { hasOwnProperty, LuaType, coerceArgToTable, coerceToString } from './utils' +import { hasOwnProperty, LuaType, tostring } from './utils' type MetaMethods = // unary op @@ -149,7 +149,7 @@ class Table { } public toObject(): unknown[] | Record { - const outputAsArray = Object.keys(this.strValues).length === 0 && getn(this) > 0 + const outputAsArray = Object.keys(this.strValues).length === 0 && this.getn() > 0 const result: unknown[] | Record = outputAsArray ? [] : {} for (let i = 1; i < this.numValues.length; i++) { @@ -177,68 +177,42 @@ class Table { return result } -} - -function tostring(v: LuaType): string { - if (v instanceof Table) { - const mm = v.getMetaMethod('__tostring') - if (mm) return mm(v)[0] - - return valToStr(v, 'table: 0x') - } - - if (v instanceof Function) { - return valToStr(v, 'function: 0x') - } - - return coerceToString(v) - - function valToStr(v: LuaType, prefix: string): string { - const s = v.toString() - if (s.indexOf(prefix) > -1) return s - - const str = prefix + Math.floor(Math.random() * 0xffffffff).toString(16) - v.toString = () => str - return str - } -} - -function getn(table: LuaType): number { - const TABLE = coerceArgToTable(table, 'getn', 1) - const vals = TABLE.numValues - const keys: boolean[] = [] + public getn(): number { + const vals = this.numValues + const keys: boolean[] = [] - for (const i in vals) { - if (hasOwnProperty(vals, i)) { - keys[i] = true + for (const i in vals) { + if (hasOwnProperty(vals, i)) { + keys[i] = true + } } - } - let j = 0 - while (keys[j + 1]) { - j += 1 - } + let j = 0 + while (keys[j + 1]) { + j += 1 + } - // Following translated from ltable.c (http://www.lua.org/source/5.3/ltable.c.html) - if (j > 0 && vals[j] === undefined) { - /* there is a boundary in the array part: (binary) search for it */ - let i = 0 + // Following translated from ltable.c (http://www.lua.org/source/5.3/ltable.c.html) + if (j > 0 && vals[j] === undefined) { + /* there is a boundary in the array part: (binary) search for it */ + let i = 0 - while (j - i > 1) { - const m = Math.floor((i + j) / 2) + while (j - i > 1) { + const m = Math.floor((i + j) / 2) - if (vals[m] === undefined) { - j = m - } else { - i = m + if (vals[m] === undefined) { + j = m + } else { + i = m + } } + + return i } - return i + return j } - - return j } -export { MetaMethods, Table, getn, tostring } +export { MetaMethods, Table } diff --git a/src/lib/globals.ts b/src/lib/globals.ts index 4a2908d..ad27fac 100644 --- a/src/lib/globals.ts +++ b/src/lib/globals.ts @@ -1,10 +1,11 @@ import { parse } from '../parser' -import { Table, tostring, getn } from '../Table' +import { Table } from '../Table' import { LuaError } from '../LuaError' import { LuaType, Config, type, + tostring, posrelat, coerceToNumber, coerceToString, @@ -201,7 +202,7 @@ function rawget(table: LuaType, index: LuaType): LuaType { * Returns an integer. */ function rawlen(v: LuaType): number { - if (v instanceof Table) return getn(v) + if (v instanceof Table) return v.getn() if (typeof v === 'string') return v.length diff --git a/src/lib/string.ts b/src/lib/string.ts index 9652227..f704dab 100644 --- a/src/lib/string.ts +++ b/src/lib/string.ts @@ -1,7 +1,7 @@ import printj from 'printj' -import { Table, tostring } from '../Table' +import { Table } from '../Table' import { LuaError } from '../LuaError' -import { posrelat, coerceArgToNumber, coerceArgToString, hasOwnProperty, LuaType } from '../utils' +import { tostring, posrelat, coerceArgToNumber, coerceArgToString, hasOwnProperty, LuaType } from '../utils' const ROSETTA_STONE: Record = { '([^a-zA-Z0-9%(])-': '$1*?', diff --git a/src/lib/table.ts b/src/lib/table.ts index fb111d3..aed3df2 100644 --- a/src/lib/table.ts +++ b/src/lib/table.ts @@ -1,4 +1,4 @@ -import { Table, getn } from '../Table' +import { Table } from '../Table' import { LuaType, coerceToBoolean, @@ -9,6 +9,11 @@ import { } from '../utils' import { LuaError } from '../LuaError' +function getn(table: LuaType): number { + const TABLE = coerceArgToTable(table, 'getn', 1) + return TABLE.getn() +} + /** * Given a list where all elements are strings or numbers, returns the string list[i]..sep..list[i+1] ยทยทยท sep..list[j]. * The default value for sep is the empty string, the default for i is 1, and the default for j is #list. @@ -103,7 +108,7 @@ function pack(...args: LuaType[]): Table { */ function remove(table: LuaType, pos?: LuaType): LuaType { const TABLE = coerceArgToTable(table, 'remove', 1) - const max = getn(TABLE) + const max = TABLE.getn() const POS = pos === undefined ? max : coerceArgToNumber(pos, 'remove', 2) if (POS > max || POS < 0) { @@ -162,7 +167,7 @@ function sort(table: Table, comp?: Function): void { function unpack(table: LuaType, i?: LuaType, j?: LuaType): LuaType[] { const TABLE = coerceArgToTable(table, 'unpack', 1) const I = i === undefined ? 1 : coerceArgToNumber(i, 'unpack', 2) - const J = j === undefined ? getn(TABLE) : coerceArgToNumber(j, 'unpack', 3) + const J = j === undefined ? TABLE.getn() : coerceArgToNumber(j, 'unpack', 3) return TABLE.numValues.slice(I, J + 1) } diff --git a/src/operators.ts b/src/operators.ts index bf6bfc6..95b441b 100644 --- a/src/operators.ts +++ b/src/operators.ts @@ -1,4 +1,4 @@ -import { MetaMethods, Table, getn } from './Table' +import { MetaMethods, Table } from './Table' import { coerceToNumber, coerceToString, LuaType, coerceToBoolean } from './utils' import { LuaError } from './LuaError' @@ -58,7 +58,7 @@ const len = (value: LuaType): number => { const mm = value.getMetaMethod('__len') if (mm) return mm(value)[0] - return getn(value) + return value.getn() } if (typeof value === 'string') return value.length diff --git a/src/utils.ts b/src/utils.ts index c0a90f3..7f565b4 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -37,6 +37,30 @@ function type(v: LuaType): 'string' | 'number' | 'boolean' | 'function' | 'nil' } } +function tostring(v: LuaType): string { + if (v instanceof Table) { + const mm = v.getMetaMethod('__tostring') + if (mm) return mm(v)[0] + + return valToStr(v, 'table: 0x') + } + + if (v instanceof Function) { + return valToStr(v, 'function: 0x') + } + + return coerceToString(v) + + function valToStr(v: LuaType, prefix: string): string { + const s = v.toString() + if (s.indexOf(prefix) > -1) return s + + const str = prefix + Math.floor(Math.random() * 0xffffffff).toString(16) + v.toString = () => str + return str + } +} + /* translate a relative string position: negative means back from end */ function posrelat(pos: number, len: number): number { if (pos >= 0) return pos @@ -178,6 +202,7 @@ export { LuaType, Config, type, + tostring, posrelat, coerceToBoolean, coerceToNumber,