wasmoon icon indicating copy to clipboard operation
wasmoon copied to clipboard

Add MultiReturn support

Open ParadiseFallen opened this issue 2 years ago • 36 comments

It kinda with breaking changes (in filesystem tests) but i tryed my best to bring multret support as softly as i can.

ParadiseFallen avatar Aug 08 '23 19:08 ParadiseFallen

Linked issue is https://github.com/ceifa/wasmoon/issues/84

ParadiseFallen avatar Aug 08 '23 19:08 ParadiseFallen

code that failed wasn't changed. so it looks like skip this one for me. What do you think, @ceifa?

ParadiseFallen avatar Aug 11 '23 18:08 ParadiseFallen

code that failed wasn't changed. so it looks like skip this one for me. What do you think, @ceifa?

can you run npm run lint? I think it will fix the build

ceifa avatar Aug 11 '23 19:08 ceifa

@ceifa done. check now

ParadiseFallen avatar Aug 11 '23 19:08 ParadiseFallen

Im not confortable about this change. Its a little bit weird that the return type changes if you are returning a single value or a multi value, if we understand that we should support it, we should stick with always returning the array, but this causes other problems, like we cant write a function in lua and just translate it to JS, since one will return a value, and the other an array, example:

function sum(x, y) -- return an array in JS
    return x + y
end
function sum(x, y) { // return a number in lua
    return x + y
}

@timstableford do you have any thoughts?

ceifa avatar Aug 12 '23 14:08 ceifa

@ceifa there is soft support of mulret. so basically your first code will return one and only one return. and only if you wrap add , x it will return mulret. So anyone can check return type (which is fine) and do what they need to.

ParadiseFallen avatar Aug 12 '23 14:08 ParadiseFallen

@ceifa i was thinking about this issue too. but mine impl wont bring any breaking changes to all exsisting projects. So if we make it always return an array it will cause many breaking changes.

For now we can use kinda generics


const [a,b,c] = lua.doString<MulRet>(`code`); // return all returns
const a = lua.doString<any>(`code`); // return first one return

ParadiseFallen avatar Aug 12 '23 14:08 ParadiseFallen

Im not confortable about this change. Its a little bit weird that the return type changes if you are returning a single value or a multi value, if we understand that we should support it, we should stick with always returning the array, but this causes other problems, like we cant write a function in lua and just translate it to JS, since one will return a value, and the other an array, example:

function sum(x, y) -- return an array in JS
    return x + y
end
function sum(x, y) { // return a number in lua
    return x + y
}

@timstableford do you have any thoughts?

That's a tough one. I think there's two cases though that could be reasonably kept consistent. When calling into a Lua program from JS as an entrypoint like with doString I'd personally have it always return an array. Then for functions that are passed back to JS from Lua have it only use the single return value, like when calling JSON.stringify from Lua and passing a replacer function.

Perhaps I'm just struggling to see a use case for that half of it though? @ParadiseFallen out of curiosity is this a just for the fun of it PR or did you have a specific use in mind?

timstableford avatar Aug 12 '23 16:08 timstableford

there is no any special use cases but it would be nice to have that feture. if lua uses mulret we can support it softly.

For now there is some built in functions that i want to call and can't bc there is no mulret.

ParadiseFallen avatar Aug 12 '23 17:08 ParadiseFallen

@timstableford why we won't use generics? its seems exatly what we want. so by default it will be just single return and if you pass expected return as <MulRet>() it will wrap it in mullret. even if there is only one return

ParadiseFallen avatar Aug 12 '23 20:08 ParadiseFallen

I'm not against using generics but it'd be necessary to link it to something runtime. As far as I understand TypeScript that could be an argument to the function to say which return mode to use or an alternate function. I'm having a bit of trouble imagining it at the mo but tl;dr I'm sure you can go that route if you work through quirks

timstableford avatar Aug 12 '23 20:08 timstableford

@timstableford well. if you want use it in js then we can made kinda


function doString(code: string, returnStategy: IReturnStrategy = SingleReturn)
{
  // just as normal code
  return returnStrategy.makeReturn(thread);
}

but need to look for how to hookup it when you want mulret (bc we need start top)

ParadiseFallen avatar Aug 12 '23 21:08 ParadiseFallen

or even just use enum for that

export enum ReturnType{
  SingleValue = 0,
  MultipleValues = 1,
}

ParadiseFallen avatar Aug 12 '23 21:08 ParadiseFallen

That's a tough one. I think there's two cases though that could be reasonably kept consistent. When calling into a Lua program from JS as an entrypoint like with doString I'd personally have it always return an array. Then for functions that are passed back to JS from Lua have it only use the single return value, like when calling JSON.stringify from Lua and passing a replacer function.

You are right, it would break implementations like that, which is very common.

@ceifa there is soft support of mulret. so basically your first code will return one and only one return. and only if you wrap add , x it will return mulret. So anyone can check return type (which is fine) and do what they need to.

I'm not a fan of this approach, returning always the same type would be better for maintainability.

I will let the PR open for now, and think better about it later

ceifa avatar Aug 17 '23 00:08 ceifa

@ceifa any updates? i suggest move to always returning an array expecting multiparams

ParadiseFallen avatar Oct 11 '23 11:10 ParadiseFallen

@ParadiseFallen I've come up with something that might make this more palatable (to me, I can't speak for @ceifa who will be merging it).

What you could do is overload the function signatures like this https://stackoverflow.com/a/13212871 For instance with doFile

public doFile(filename: string, returnMode: 'multi'): Promise<MultiReturn>;
public doFile(filename: string, returnMode?: 'single' | 'multi'): Promise<any> {
 // Implementation here
}

That would mean the current function signatures would remain compatible and return a single item but if return mode was set to multi the return type would be MultiReturn instead

tims-bsquare avatar Nov 30 '23 10:11 tims-bsquare

Автор

@ParadiseFallen I've come up with something that might make this more palatable (to me, I can't speak for @ceifa who will be merging it).

What you could do is overload the function signatures like this https://stackoverflow.com/a/13212871 For instance with doFile

public doFile(filename: string, returnMode: 'multi'): Promise<MultiReturn>;
public doFile(filename: string, returnMode?: 'single' | 'multi'): Promise<any> {
 // Implementation here
}

That would mean the current function signatures would remain compatible and return a single item but if return mode was set to multi the return type would be MultiReturn instead

I already proposed that. we dont even need overload for that.

ParadiseFallen avatar Nov 30 '23 10:11 ParadiseFallen

You're right that you proposed a return mode, which I generally liked but the explicit return type of multireturn I think will make it easier.

I've just been reading through the comments again after your prompt and the other part that I think was tripping us up was that functions returned from Lua to JS would need to be single return for JS compatibility. I'd be okay with you passing it in as an option to the engine whether to do single returns or multi-returns in that circumstance.

It's been a while since I've put any thought into this, so sorry for rehashing a bit.

tims-bsquare avatar Nov 30 '23 11:11 tims-bsquare

@tims-bsquare why just single return? its easilly supported with just mulret

const mulret = call();
const [r1, r2, r3] = call();

ParadiseFallen avatar Nov 30 '23 11:11 ParadiseFallen

For JS interoperability unless I'm misunderstanding you. Consider exposing the JSON.stringify function to Lua as an example. So Lua does

JSON.stringify({}, function (replacer) return something end)

stringify expects a function that return a single argument, by not keeping single as default the above breaks

tims-bsquare avatar Nov 30 '23 12:11 tims-bsquare

@tims-bsquare there many pain points on example like that. also it looke like

-- lua
-- return generic builder
someJsFunc(function (startIndex) 
-- some wrap for example.
return function ()
  local x = startIndex;
  -- indexer
  return function ()
    x = x + 1;
    return x;
  end
end

end)

this example will breaks. same for js side.

ParadiseFallen avatar Nov 30 '23 12:11 ParadiseFallen

Sorry @ParadiseFallen I'm not following the problem you're describing. Currently the example I showed works and I'm not sure what your function is showing

tims-bsquare avatar Nov 30 '23 13:11 tims-bsquare

@tims-bsquare it's showing another problem, that not mentioned there. sometimes wrapper breaks

ParadiseFallen avatar Nov 30 '23 13:11 ParadiseFallen

@tims-bsquare it's showing another problem, that not mentioned there. sometimes wrapper breaks

I'm a little bit confused too, can you elaborate the problem you are showing?

ceifa avatar Nov 30 '23 14:11 ceifa

@ceifa will do it later and on another issue

ParadiseFallen avatar Nov 30 '23 17:11 ParadiseFallen

@ceifa @tims-bsquare https://github.com/ceifa/wasmoon/issues/98

ParadiseFallen avatar Nov 30 '23 18:11 ParadiseFallen

My two cents as a user:

In Lua, a = (function() return "a", "truncated" end)() does not assign a table to a but simply truncates the stack (i.e. a = "a"). And I think it is only natural to keep this as default.

Alternatively, if multiple return values are wanted, I can always wrap them in a table like return { "a", "b" } and can comfortably achieve anything I want without necessitating this MULTI_RET change.

Some more justifications:

  1. The Array when >= 2 (JS) approach:

    In Lua, changing from return 1 to return 1, "extra" is usually not a breaking change due to how the stack truncation works. However, with the current PR (return 1 => (JS) 1, return 1, 2 => (JS) [1, 2]), it will. So either we stick with our previous truncation approach or we always return an array.

  2. The Always array approach:

    Always returning an array has its own problem: it poses a limit on the library's capability - Lua functions will no longer be able to simulate any JS function that can return anything.

The following matrix outlines whether the user will be able to do something with code change in only one language:

Get a single value Return multiple values
Previous (JS side) / Function wrapper
Previous (Lua side) / Wrap in tables
Always array (JS) Function wrapper /
Always array (Lua) You can't /
Array when >= 2 (JS) Sometimes / wrapper Sometimes / wrapper
Array when >= 2 (Lua) Sometimes / wrapper Sometimes / wrapper

Therefore, speaking as a user, I am against this breaking change. (The overloading approach is non-breaking and feels better.)

gudzpoz avatar Dec 03 '23 09:12 gudzpoz

@ceifa so any updates on this issue?

ParadiseFallen avatar Mar 19 '24 15:03 ParadiseFallen

lets take this as example

-- IdGenerator
return function (start,step)
  local current = start
  return function ()
    current = current + step
    return current
  end
end

-- main
local IdGenerator = require 'IdGenerator'

-- this method will called from js
function tryCreateGenerator(start)
  
  if start < 0 then
    return false
  end
  
  return true, IdGenerator(start, 1) 
end

return tryCreateGenerator

Then lets call it from js

const factory = new LuaFactory();
const engine = await factory.createEngine();

const tryCreateGenerator = engine.doString(/*code goes here*/);
// we can also get `tryCreateGenerator` just by engine.global.get('name'). 

// and now we facing few issues
// first one is mulret. it won't work
// second one is returning of lua function. it just breakes
const [created, generator] = tryCreateGenerator(2) // won't work

ParadiseFallen avatar Mar 19 '24 16:03 ParadiseFallen

also it shows one more issue. if generator() will return 2 values ie

function createGenerator(start, step)
  local current = start
  -- generator itself
  return function()
    current = current + step
    if current > 100 then
      return false, current - step
    end
    return true, current
  end
end

it won't work. bc internal function will called on js side. and it must return mulret. but we can't specify which value we expect. mulret or not. So this option muse be specified a bit higher that just doString or doFile

ParadiseFallen avatar Mar 19 '24 17:03 ParadiseFallen