@newStackCall

@newStackCall

@newStackCall(new_stack: []align(target_stack_align) u8, function: var, args: ...) var

This calls a function, in the same way that invoking an expression with parentheses does. However, instead of using the same stack as the caller, the function uses the stack provided in the new_stack parameter.

The new stack must be aligned to target_stack_align bytes. This is a target-specific number. A safe value that will work on all targets is 16. This value can also be obtained by using @sizeOf on the @Frame type of Async Functions.

test.zig

const std = @import("std");
const assert = std.debug.assert;

var new_stack_bytes: [1024]u8 align(16) = undefined;

test "calling a function with a new stack" {
    const arg = 1234;

    const a = @newStackCall(new_stack_bytes[0..512], targetFunction, arg);
    const b = @newStackCall(new_stack_bytes[512..], targetFunction, arg);
    _ = targetFunction(arg);

    assert(arg == 1234);
    assert(a < b);
}

fn targetFunction(x: i32) usize {
    assert(x == 1234);

    var local_variable: i32 = 42;
    const ptr = &local_variable;
    ptr.* += 1;

    assert(local_variable == 43);
    return @ptrToInt(ptr);
}
``` ```
$ zig test test.zig
1/1 test "calling a function with a new stack"...OK
All tests passed.