lua-in-js icon indicating copy to clipboard operation
lua-in-js copied to clipboard

Task lib

Open 4lve opened this issue 2 years ago • 5 comments

I can't seem to find any task lib.

For example you can't use the function Wait() or task.Wait()

4lve avatar Nov 04 '23 21:11 4lve

image image image

Managed to implement promises

4lve avatar Nov 04 '23 23:11 4lve

Adding await to the call function is a better solution

const call = async (f, ...args) => {
    if (f instanceof Function)
        return ensureArray(await f(...args));
    const mm = f instanceof Table && f.getMetaMethod('__call');
    if (mm)
        return ensureArray(await mm(f, ...args));
    throw new LuaError(`attempt to call an uncallable type`);
};

Aswell as doing this

const exec = new Function('__lua', 'return (async () => {' + chunk + '})();');

And making all lua functions async


return `async (${argsStr}) => {\n${body}${returnStr}\n}`;

Implemented a task lib

const taskLib = new Table({
  wait: async (time: LuaType) => {
    const TIME = utils.coerceArgToNumber(time, "wait", 1);
    return new Promise((resolve) => setTimeout(resolve, TIME * 1000));
  },
  spawn: async (func: LuaType) => {
    const FUNC = utils.coerceArgToFunction(func, "spawn", 1);
    return setImmediate(FUNC as any);
  },
  delay: async (time: LuaType, func: LuaType) => {
    const TIME = utils.coerceArgToNumber(time, "delay", 1);
    const FUNC = utils.coerceArgToFunction(func, "delayFunc", 2);
    const timeout = setTimeout(FUNC as any, TIME * 1000);
    return new Table({
      timeout: () => timeout,
      type: "timeout",
    });
  },
  cancel: async (id: LuaType) => {
    const ID = utils.coerceArgToTable(id, "cancelTimeout", 1);
    if (ID.get("type") === "timeout") {
      ((ID.get("timeout") as Function)() as NodeJS.Timeout).unref();
    }
  },
});

4lve avatar Nov 05 '23 06:11 4lve

Hi, I can't seem to find this functionality in the standard library that Lua provides. I don't think it's a good idea to convert all functions to async functions as that can have unintended performance consequences.

The closest thing Lua has to this is the coroutine library which lua-in-js doesn't currently support.

I'd be open to adding support for the coroutine library rather than a non-standard task library.

teoxoy avatar Nov 26 '23 12:11 teoxoy

task and task.wait is from roblox

niceEli avatar Jan 15 '24 21:01 niceEli