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