Closed hariseldon78 closed 1 year ago
Ok i found the answer, i'll close this issue but leave it for somebody that could have the same problem:
the solution is to add :await()
inside lua to the returned js_promise. Btw: the other tests were working because i didn't actually used the values, just returned them, so they were awaited on js side on returning.
The fixed unit test is:
import { LuaFactory, LuaEngine } from 'wasmoon';
describe('Wasmoon lua library', () => {
let factory: LuaFactory;
let lua: LuaEngine;
beforeAll(async () => {
factory = new LuaFactory();
});
beforeEach(async () => {
lua=await factory.createEngine();
lua.global.set('testResults', {a: 1, b: 2, c: 3});
});
it('should run a lua script', async () => {
const result = await lua.doString('return testResults.a');
expect(result).toBe(1);
});
it('should run a lua script 2', async () => {
const result = await lua.doString('return testResults.b + testResults.c');
expect(result).toBe(5);
});
// sync functions
it('shold run a function', async () => {
function f() {
return 42;
}
lua.global.set('f', f);
const result = await lua.doString('return f()');
expect(result).toBe(42);
});
it('should run a function with arguments', async () => {
function f(a: number, b: number) {
return a + b;
}
lua.global.set('f', f);
const result = await lua.doString('return f(1, 2)');
expect(result).toBe(3);
});
it('should run a function with arguments and return a table', async () => {
function f(a: number, b: number) {
return { a, b };
}
lua.global.set('f', f);
const result = await lua.doString(`
local res=f(1, 2)
return res.b+res.a
`);
expect(result).toEqual(3);
});
it('should run a function with arguments and return a closure', async () => {
function f(a: number, b: number) {
return { a, b, c: () => a + b };
}
lua.global.set('f', f);
const result = await lua.doString('return f(1, 2)');
expect(result.c()).toEqual(3);
});
// async functions
it('should run an async function', async () => {
async function f() {
await new Promise((resolve) => setTimeout(resolve, 100));
return 42;
}
lua.global.set('f', f);
const result = await lua.doString('return f()');
expect(result).toBe(42);
});
it('should run an async function 2', async () => {
async function f() {
await new Promise((resolve) => setTimeout(resolve, 100));
return 42;
}
lua.global.set('f', f);
const result = await lua.doString('return f():await()+f():await()');
expect(result).toBe(84);
});
it('should run an async function with arguments', async () => {
async function f(a: number, b: number) {
await new Promise((resolve) => setTimeout(resolve, 100));
return a + b;
}
lua.global.set('f', f);
const result = await lua.doString('return f(1, 2)');
expect(result).toBe(3);
});
it('should run an async function with arguments and use the returned table', async () => {
async function f(a: number, b: number) {
await new Promise((resolve) => setTimeout(resolve, 100));
return { a, b };
}
lua.global.set('f', f);
const result = await lua.doString(`
local res=f(1, 2):await()
print(res)
return res.b+res.a
`);
expect(result).toEqual(3);
});
it('should run an async function with arguments and return a table', async () => {
async function f(a: number, b: number) {
await new Promise((resolve) => setTimeout(resolve, 100));
return { a, b };
}
lua.global.set('f', f);
const result = await lua.doString('return f(1, 2)');
expect(result).toEqual({ a: 1, b: 2 });
});
});
If i declare an async function from javascript, that returns a table, when I run it inside the lua script i obtain a 'js_promise' object instead. If instead the async function returns a simple value i correctly receive that value.
With the following unit test:
the result is
As you can see, the returned table is not read correctly on lua side; the two previous tests instead are reading the values correctly and passing. Am I missing something obvious? Should I call the async function in a special way? (thread? could you give me an example?)