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 = type MetaMethods =
// unary op // unary op
@ -149,7 +149,7 @@ class Table {
} }
public toObject(): unknown[] | Record<string, unknown> { 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 ? [] : {} const result: unknown[] | Record<string, unknown> = outputAsArray ? [] : {}
for (let i = 1; i < this.numValues.length; i++) { for (let i = 1; i < this.numValues.length; i++) {
@ -177,68 +177,42 @@ class Table {
return result 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 keys: boolean[] = [] const vals = this.numValues
const keys: boolean[] = []
for (const i in vals) { for (const i in vals) {
if (hasOwnProperty(vals, i)) { if (hasOwnProperty(vals, i)) {
keys[i] = true keys[i] = true
}
} }
}
let j = 0 let j = 0
while (keys[j + 1]) { while (keys[j + 1]) {
j += 1 j += 1
} }
// Following translated from ltable.c (http://www.lua.org/source/5.3/ltable.c.html) // Following translated from ltable.c (http://www.lua.org/source/5.3/ltable.c.html)
if (j > 0 && vals[j] === undefined) { if (j > 0 && vals[j] === undefined) {
/* there is a boundary in the array part: (binary) search for it */ /* there is a boundary in the array part: (binary) search for it */
let i = 0 let i = 0
while (j - i > 1) { while (j - i > 1) {
const m = Math.floor((i + j) / 2) const m = Math.floor((i + j) / 2)
if (vals[m] === undefined) { if (vals[m] === undefined) {
j = m j = m
} else { } else {
i = m i = m
}
} }
return i
} }
return i return j
} }
return j
} }
export { MetaMethods, Table, getn, tostring } export { MetaMethods, Table }

@ -1,10 +1,11 @@
import { parse } from '../parser' import { parse } from '../parser'
import { Table, tostring, getn } from '../Table' import { Table } from '../Table'
import { LuaError } from '../LuaError' import { LuaError } from '../LuaError'
import { import {
LuaType, LuaType,
Config, Config,
type, type,
tostring,
posrelat, posrelat,
coerceToNumber, coerceToNumber,
coerceToString, coerceToString,
@ -201,7 +202,7 @@ function rawget(table: LuaType, index: LuaType): LuaType {
* Returns an integer. * Returns an integer.
*/ */
function rawlen(v: LuaType): number { 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 if (typeof v === 'string') return v.length

@ -1,7 +1,7 @@
import printj from 'printj' import printj from 'printj'
import { Table, tostring } from '../Table' import { Table } from '../Table'
import { LuaError } from '../LuaError' 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> = { const ROSETTA_STONE: Record<string, string> = {
'([^a-zA-Z0-9%(])-': '$1*?', '([^a-zA-Z0-9%(])-': '$1*?',

@ -1,4 +1,4 @@
import { Table, getn } from '../Table' import { Table } from '../Table'
import { import {
LuaType, LuaType,
coerceToBoolean, coerceToBoolean,
@ -9,6 +9,11 @@ import {
} from '../utils' } from '../utils'
import { LuaError } from '../LuaError' 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]. * 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. * 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 { function remove(table: LuaType, pos?: LuaType): LuaType {
const TABLE = coerceArgToTable(table, 'remove', 1) const TABLE = coerceArgToTable(table, 'remove', 1)
const max = getn(TABLE) const max = TABLE.getn()
const POS = pos === undefined ? max : coerceArgToNumber(pos, 'remove', 2) const POS = pos === undefined ? max : coerceArgToNumber(pos, 'remove', 2)
if (POS > max || POS < 0) { 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[] { function unpack(table: LuaType, i?: LuaType, j?: LuaType): LuaType[] {
const TABLE = coerceArgToTable(table, 'unpack', 1) const TABLE = coerceArgToTable(table, 'unpack', 1)
const I = i === undefined ? 1 : coerceArgToNumber(i, 'unpack', 2) 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) 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 { coerceToNumber, coerceToString, LuaType, coerceToBoolean } from './utils'
import { LuaError } from './LuaError' import { LuaError } from './LuaError'
@ -58,7 +58,7 @@ const len = (value: LuaType): number => {
const mm = value.getMetaMethod('__len') const mm = value.getMetaMethod('__len')
if (mm) return mm(value)[0] if (mm) return mm(value)[0]
return getn(value) return value.getn()
} }
if (typeof value === 'string') return value.length 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 */ /* translate a relative string position: negative means back from end */
function posrelat(pos: number, len: number): number { function posrelat(pos: number, len: number): number {
if (pos >= 0) return pos if (pos >= 0) return pos
@ -178,6 +202,7 @@ export {
LuaType, LuaType,
Config, Config,
type, type,
tostring,
posrelat, posrelat,
coerceToBoolean, coerceToBoolean,
coerceToNumber, coerceToNumber,

Loading…
Cancel
Save