move getn and tostring out of Table file

pull/6/head
Teoxoy 6 years ago
parent d26d88bde9
commit 3d6f35a579

@ -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<string, unknown> {
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<string, unknown> = outputAsArray ? [] : {}
for (let i = 1; i < this.numValues.length; i++) {
@ -177,36 +177,9 @@ 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
public getn(): number {
const vals = this.numValues
const keys: boolean[] = []
for (const i in vals) {
@ -240,5 +213,6 @@ function getn(table: LuaType): number {
return j
}
}
export { MetaMethods, Table, getn, tostring }
export { MetaMethods, Table }

@ -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

@ -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<string, string> = {
'([^a-zA-Z0-9%(])-': '$1*?',

@ -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)
}

@ -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

@ -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,

Loading…
Cancel
Save