Skip to content

Commit ae8ef9f

Browse files
committed
docs: add js-function call with multiple returns and js-function error handling examples to readme
1 parent e825af5 commit ae8ef9f

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,22 @@ console.log(add(5, 7)); // 12
9595
// Call JS from Lua
9696
lua.setGlobal("add", (a, b) => a + b);
9797
console.log(lua.eval("return add(3, 4)")); // 12
98+
99+
// JS function with multiple returns
100+
lua.setGlobal("getUser", () => ["Alice", 30]);
101+
lua.eval("name, age = getUser()");
102+
console.log(lua.getGlobal("name")); // "Alice"
103+
console.log(lua.getGlobal("age")); // 30
104+
105+
// JS function that throws an error
106+
lua.setGlobal("throwError", () => {
107+
throw new Error("Something went wrong");
108+
});
109+
const result = lua.eval(`
110+
local ok, err = pcall(throwError);
111+
return { ok, err }
112+
`);
113+
console.log(result); // { 1: false, 2: "Error: Something went wrong" }
98114
```
99115

100116
**Get Table Length**

0 commit comments

Comments
 (0)