Better string handling
parent
d82e755b35
commit
ce18078480
|
@ -4,9 +4,10 @@ import {
|
||||||
luaLen,
|
luaLen,
|
||||||
LuaMultiRes,
|
LuaMultiRes,
|
||||||
LuaStackFrame,
|
LuaStackFrame,
|
||||||
|
luaToString,
|
||||||
} from "$common/space_lua/runtime.ts";
|
} from "$common/space_lua/runtime.ts";
|
||||||
|
|
||||||
Deno.test("Test Lua Rutime", () => {
|
Deno.test("Test Lua Rutime", async () => {
|
||||||
// Test LuaMultires
|
// Test LuaMultires
|
||||||
assertEquals(new LuaMultiRes([]).flatten().values, []);
|
assertEquals(new LuaMultiRes([]).flatten().values, []);
|
||||||
assertEquals(new LuaMultiRes([1, 2, 3]).flatten().values, [1, 2, 3]);
|
assertEquals(new LuaMultiRes([1, 2, 3]).flatten().values, [1, 2, 3]);
|
||||||
|
@ -42,4 +43,9 @@ Deno.test("Test Lua Rutime", () => {
|
||||||
// Functions in objects
|
// Functions in objects
|
||||||
luaVal = jsToLuaValue({ name: "Pete", first: (l: any[]) => l[0] });
|
luaVal = jsToLuaValue({ name: "Pete", first: (l: any[]) => l[0] });
|
||||||
assertEquals(luaVal.get("first").call(LuaStackFrame.lostFrame, [1, 2, 3]), 1);
|
assertEquals(luaVal.get("first").call(LuaStackFrame.lostFrame, [1, 2, 3]), 1);
|
||||||
|
|
||||||
|
// Test luaToString
|
||||||
|
assertEquals(await luaToString(new Promise((resolve) => resolve(1))), "1");
|
||||||
|
assertEquals(await luaToString({ a: 1 }), "{a = 1}");
|
||||||
|
assertEquals(await luaToString([{ a: 1 }]), "{{a = 1}}");
|
||||||
});
|
});
|
||||||
|
|
|
@ -678,15 +678,54 @@ export function luaTruthy(value: any): boolean {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function luaToString(value: any): string | Promise<string> {
|
export async function luaToString(value: any): Promise<string> {
|
||||||
if (value === null || value === undefined) {
|
if (value === null || value === undefined) {
|
||||||
return "nil";
|
return "nil";
|
||||||
}
|
}
|
||||||
|
if (value instanceof Promise) {
|
||||||
|
return luaToString(await value);
|
||||||
|
}
|
||||||
if (value.toStringAsync) {
|
if (value.toStringAsync) {
|
||||||
return value.toStringAsync();
|
return value.toStringAsync();
|
||||||
}
|
}
|
||||||
if (value.toString) {
|
// Handle plain JavaScript objects in a Lua-like format
|
||||||
return value.toString();
|
if (typeof value === "object") {
|
||||||
|
let result = "{";
|
||||||
|
let first = true;
|
||||||
|
|
||||||
|
// Handle arrays
|
||||||
|
if (Array.isArray(value)) {
|
||||||
|
for (const val of value) {
|
||||||
|
if (first) {
|
||||||
|
first = false;
|
||||||
|
} else {
|
||||||
|
result += ", ";
|
||||||
|
}
|
||||||
|
// Recursively stringify the value
|
||||||
|
const strVal = await luaToString(val);
|
||||||
|
result += strVal;
|
||||||
|
}
|
||||||
|
return result + "}";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle objects
|
||||||
|
for (const [key, val] of Object.entries(value)) {
|
||||||
|
if (first) {
|
||||||
|
first = false;
|
||||||
|
} else {
|
||||||
|
result += ", ";
|
||||||
|
}
|
||||||
|
if (/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(key)) {
|
||||||
|
result += `${key} = `;
|
||||||
|
} else {
|
||||||
|
result += `["${key}"] = `;
|
||||||
|
}
|
||||||
|
// Recursively stringify the value
|
||||||
|
const strVal = await luaToString(val);
|
||||||
|
result += strVal;
|
||||||
|
}
|
||||||
|
result += "}";
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
return String(value);
|
return String(value);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue