luau/tests/conformance/cyield.luau
Andy Friesen c51743268b
Sync to upstream/release/671 (#1787)
# General

* Internally rename `ClassType` to `ExternType`. In definition files,
the syntax to define these types has changed to `declare extern type Foo
with prop: type end`
* Add `luarequire_registermodule` to Luau.Require
* Support yieldable Luau C functions calling other functions
* Store return types as `AstTypePack*` on Ast nodes

## New Solver

* Improve the logic that determines constraint dispatch ordering
* Fix a crash in the type solver that arose when using multi-return
functions with `string.format`
* Fix https://github.com/luau-lang/luau/issues/1736
* Initial steps toward rethinking function generalization:
* Instead of generalizing every type in a function all at once, we will
instead generalize individual type variables once their bounds have been
fully resolved. This will make it possible to properly interleave type
function reduction and generalization.
* Magic functions are no longer considered magical in cases where they
are not explicitly called by the code.
* The most prominent example of this is in `for..in` loops where the
function call is part of the desugaring process.
* Almost all magic functions work by directly inspecting the AST, so
they can't work without an AST fragment anyway.
* Further, none of the magic functions we have are usefully used in this
way.

Co-authored-by: Andy Friesen <afriesen@roblox.com>
Co-authored-by: Ariel Weiss <aaronweiss@roblox.com>
Co-authored-by: Hunter Goldstein <hgoldstein@roblox.com>
Co-authored-by: Sora Kanosue <skanosue@roblox.com>
Co-authored-by: Talha Pathan <tpathan@roblox.com>
Co-authored-by: Varun Saini <vsaini@roblox.com>
Co-authored-by: Vighnesh Vijay <vvijay@roblox.com>
Co-authored-by: Vyacheslav Egorov <vegorov@roblox.com>
2025-04-25 14:19:27 -07:00

135 lines
3.2 KiB
Text

-- This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
print('testing yields from C functions')
-- Regular yield from a C function
do
local co = coroutine.wrap(singleYield)
assert(co() == 2)
assert(co() == 4)
end
-- Multiple yields from a C function
do
local co = coroutine.wrap(multipleYields)
assert(co(10) == 11)
assert(co() == 12)
assert(co() == 13)
assert(co() == 14)
end
-- Multiple yields from a C function (unused extra arguments)
do
local co = coroutine.wrap(multipleYields)
assert(co(10, -1) == 11)
assert(co(-1, -2) == 12)
assert(co(-1, -2) == 13)
assert(co(-1, -2) == 14)
end
-- Multiple yields from nested yieldable C functions
do
local co = coroutine.wrap(multipleYieldsWithNestedCall)
assert(co(10, true) == 105)
assert(co() == 115)
assert(co() == 210)
assert(co() == 220)
end
-- Multiple yields from nested yieldable C functions (unused extra arguments)
do
local co = coroutine.wrap(multipleYieldsWithNestedCall)
assert(co(10, true, -1) == 105)
assert(co(-1, -2) == 115)
assert(co(-1, -2) == 210)
assert(co(-1, -2) == 220)
end
do
local co = coroutine.wrap(multipleYieldsWithNestedCall)
assert(co(10, false) == 110)
assert(co() == 210)
assert(co() == 220)
end
local function nonyieldable(x, y)
return x / y
end
local function yieldable(x, y)
local a, b = coroutine.yield(x + y)
for i = 1,10 do
a, b = coroutine.yield(a * b)
end
coroutine.yield(a - b)
return x / y
end
-- yieldable Luau function that acts as a pass-through
do
local co = coroutine.create(function(x, y)
return yieldable(x, y)
end)
local s, x = coroutine.resume(co, 1, 2)
assert(s)
assert(x == 3)
for i = 1, 10 do
local s, x = coroutine.resume(co, 4, 8)
assert(s)
assert(x == 32)
end
local s, x = coroutine.resume(co, 10, 5)
assert(s)
assert(x == 5)
local s, x = coroutine.resume(co)
assert(s)
assert(x == 0.5)
end
-- yieldable C function that acts as a pass-through (regular)
local function passthroughcheck(f)
local x = f(nonyieldable, 1, 2)
assert(x == 0.5)
end
passthroughcheck(passthroughCall)
passthroughcheck(passthroughCallMoreResults)
passthroughcheck(passthroughCallArgReuse)
passthroughcheck(passthroughCallVaradic)
passthroughcheck(passthroughCallWithState)
-- yieldable C function that acts as a pass-through (yield)
local function passthroughcheckyield(f)
local co = coroutine.create(function(x, y)
return f(yieldable, x, y)
end)
local s, x = coroutine.resume(co, 1, 2)
assert(s)
assert(x == 3)
for i = 1, 10 do
local s, x = coroutine.resume(co, 4, 8)
assert(s)
assert(x == 32)
end
local s, x = coroutine.resume(co, 10, 5)
assert(s)
assert(x == 5)
local s, x = coroutine.resume(co)
assert(s)
assert(x == 0.5)
end
passthroughcheckyield(passthroughCall)
passthroughcheckyield(passthroughCallMoreResults)
passthroughcheckyield(passthroughCallArgReuse)
passthroughcheckyield(passthroughCallVaradic)
passthroughcheckyield(passthroughCallWithState)
return "OK"