@@ -411,24 +411,44 @@ fn anonCallback(ptr: ?*anyopaque, loop: *xev.Loop, _: *xev.Completion, r: xev.As
411411 return .disarm ;
412412}
413413
414- pub fn waitForLua (
415- l : * Lua ,
416- num_args : i32 ,
417- ) ! void {
414+ pub fn waitForLua (l : * Lua , waiting_on : union (enum ) {
415+ function_with_num_args : i32 ,
416+ async_function_with_num_args : i32 ,
417+ promise : void ,
418+ }) ! void {
418419 const builtin = @import ("builtin" );
419420 const a = try xev .Async .init ();
420421 const top = if (builtin .mode == .Debug ) blk : {
421422 const top = l .getTop ();
423+ const num_args : i32 = switch (waiting_on ) {
424+ .promise = > 0 ,
425+ .function_with_num_args , .async_function_with_num_args = > | n | n ,
426+ };
422427 std .debug .assert (top >= num_args + 2 + 1 );
423428 break :blk top ;
424429 };
425- lu .load (l , "seamstress.async.Promise" );
426- l .rotate (- num_args - 4 , 3 ); // stack is now: closure closure Promise function ...
427- try lu .doCall (l , num_args + 1 , 1 ); // stack is now closure closure returned_promise
430+ switch (waiting_on ) {
431+ .promise = > l .rotate (-3 , 2 ), // stack is now: closure closure promise
432+ .function_with_num_args = > | num_args | {
433+ lu .load (l , "seamstress.async.Promise" );
434+ l .rotate (- num_args - 4 , 3 ); // stack is now: closure closure Promise function ...
435+ try lu .doCall (l , num_args + 1 , 1 ); // stack is now closure closure returned_promise
436+ },
437+ .async_function_with_num_args = > | num_args | {
438+ l .rotate (- num_args - 3 , 3 ); // stack is now: closure closure function ...
439+ try lu .doCall (l , num_args , 1 ); // stack is now closure closure returned_promise
440+ },
441+ }
428442 const promise : * Promise = l .newUserdata (Promise , 3 );
429443 l .pushValue (-1 ); // copy it
430444 const handle = try l .ref (ziglua .registry_index ); // ref pops
431- defer if (builtin .mode == .Debug ) std .debug .assert (l .getTop () + num_args + 2 == top );
445+ defer if (builtin .mode == .Debug ) {
446+ const num_args : i32 = switch (waiting_on ) {
447+ .promise = > 0 ,
448+ .function_with_num_args , .async_function_with_num_args = > | n | n ,
449+ };
450+ std .debug .assert (l .getTop () + num_args + 2 == top );
451+ };
432452 const thread = l .newThread ();
433453 l .rotate (-5 , -2 ); // stack is now: returned_promise new_promise thread closure closure
434454 l .xMove (thread , 2 ); // pops closures
@@ -438,8 +458,11 @@ pub fn waitForLua(
438458 .data = .{ .@"async" = a },
439459 };
440460 _ = l .getUserValue (-2 , 2 ) catch unreachable ; // get the table of dependent promises
461+ l .len (-1 ); // get its length
462+ const len = l .toInteger (-1 ) catch unreachable ;
463+ l .pop (1 );
441464 l .pushValue (-2 ); // copy new_promise
442- l .setIndex (-2 , 1 ); // assign as dependent promise
465+ l .setIndex (-2 , len + 1 ); // assign as dependent promise
443466 l .pop (1 ); // pop the table
444467 l .rotate (-2 , 1 ); // stack is now new_promise returned_promise
445468 l .setUserValue (-2 , 3 ) catch unreachable ;
0 commit comments