print('-- unprotected error in coroutine (test 1) --')
coro = coroutine.create(function()
	print('inside coro')
	error(true)
	print('should never reach here')
end)
print('starting coro')
print(coroutine.resume(coro))
print(coroutine.status(coro))

print('-- unprotected error in coroutine (test 2) --')
coro = coroutine.create(function()
	print('inside coro')
	coroutine.yield("yield value from coro")
	error(true)
	print('should never reach here')
end)
print('starting coro')
print(coroutine.resume(coro))
print(coroutine.status(coro))
print(coroutine.resume(coro))
print(coroutine.status(coro))

print('-- unprotected error in coroutine (test 3) --')
coro = coroutine.wrap(function()
	print('inside coro')
	error(true)
	print('should never reach here')
end)
print('starting coro')
print(pcall(coro))

print('-- unprotected error in coroutine (test 4) --')
coro = coroutine.wrap(function()
	print('inside coro')
	error(true)
	print('should never reach here')
end)
print('starting coro')
print(xpcall(coro, function(err) 
	print('inside main error handler, err=', err)
	return "return value from main error handler"
end))

print('-- unprotected error in coroutine (test 5) --')
coro = coroutine.wrap(function()
	print('inside coro')
	coroutine.yield('yield value from coro')
	error(true)
	print('should never reach here')
end)
print('starting coro')
print(pcall(coro))
print(pcall(coro))

print('-- unprotected error in coroutine (test 6) --')
coro = coroutine.wrap(function()
	print('inside coro')
	coroutine.yield('yield value from coro')
	error(true)
	print('should never reach here')
end)
print('starting coro')
print(xpcall(coro, function(err) 
	print('inside main error handler 1, err=', err)
	return "return value from main error handler"
end))
print(xpcall(coro, function(err) 
	print('inside main error handler 2, err=', err)
	return "return value from main error handler"
end))

print('-- unprotected error in coroutine (test 7) --')
coro = coroutine.create(function()
	print('inside coro')
	coro2 = coroutine.create(function()
		print('inside coro2')
		error(true)
		print('should never reach here')
	end)
	print(coroutine.resume(coro2))
	return "return value crom coro"
end)
print(coroutine.resume(coro))
print(coroutine.status(coro), coroutine.status(coro2))

print('-- unprotected error in coroutine (test 8) --')
coro = coroutine.create(function()
	print('inside coro')
	coro2 = coroutine.create(function()
		print('inside coro2')
		coroutine.yield("yield value from coro2")
		error(true)
		print('should never reach here')
	end)
	print(coroutine.resume(coro2))
	coroutine.yield("yield value form coro")
	print(coroutine.resume(coro2))
	return "return value crom coro"
end)
print(coroutine.resume(coro))
print(coroutine.status(coro), coroutine.status(coro2))
print(coroutine.resume(coro))
print(coroutine.status(coro), coroutine.status(coro2))

print('-- unprotected error in coroutine (test 9) --')
coro = coroutine.create(function()
	print('inside coro')
	coro2 = coroutine.wrap(function()
		print('inside coro2')
		error(true)
		print('should never reach here')
	end)
	print(coro2())
	return "should never reach here"
end)
print(coroutine.resume(coro))
print(coroutine.status(coro))

print('-- unprotected error in coroutine (test 10) --')
coro = coroutine.create(function()
	print('inside coro')
	coro2 = coroutine.wrap(function()
		print('inside coro2')
		coroutine.yield("yield value from coro2")
		error(true)
		print('should never reach here')
	end)
	print("starting coro2")
	print(coro2())
	coroutine.yield("yield value from coro")
	print("resuming coro2")
	print(coro2())
	return "should never reach here"
end)
print(coroutine.resume(coro))
print(coroutine.status(coro))
print(coroutine.resume(coro))
print(coroutine.status(coro))

print('-- unprotected error in coroutine (test 11) --')
coro = coroutine.wrap(function()
	print('inside coro')
	coro2 = coroutine.create(function()
		print('inside coro2')
		error(true)
		print('should never reach here')
	end)
	print('coro2 resume result', coroutine.resume(coro2))
	return "return value from coro"
end)
print(coro())
print(coroutine.status(coro2))

print('-- unprotected error in coroutine (test 12) --')
coro = coroutine.wrap(function()
	print('inside coro')
	coro2 = coroutine.create(function()
		print('inside coro2')
		coroutine.yield("yield value from coro2")
		error(true)
		print('should never reach here')
	end)
	print('coro2 resume result', coroutine.resume(coro2))
	coroutine.yield("yield value from coro")
	print('coro2 resume result 2', coroutine.resume(coro2))
	return "return value from coro"
end)
print(coro())
print(coroutine.status(coro2))
print(coro())
print(coroutine.status(coro2))

print('-- unprotected error in coroutine (test 13) --')
coro = coroutine.wrap(function()
	print('inside coro')
	coro2 = coroutine.wrap(function()
		print('inside coro2')
		error(true)
		print('should never reach here')
	end)
	print('coro2 resume result', coro2())
	return "should never reach here"
end)
print(pcall(function()
	print(coro())
	print("should never reach here")
end))

print('-- unprotected error in coroutine (test 14) --')
coro = coroutine.wrap(function()
	print('inside coro')
	coro2 = coroutine.wrap(function()
		print('inside coro2')
		coroutine.yield("yield value from coro2")
		error(true)
		print('should never reach here')
	end)
	print('coro2 resume result', coro2())
	coroutine.yield("yield value from coro")
	print('coro2 resume result', coro2())
	return "should never reach here"
end)
print(pcall(function()
	print(coro())
	return "should succeed"
end))
print(pcall(function()
	print(coro())
	print("should never reach here")
end))

print('-- unprotected error in coroutine (test 15) --')
coro = coroutine.wrap(function()
	print('inside coro')
	coro2 = coroutine.wrap(function()
		print('inside coro2')
		error(true)
		print('should never reach here')
	end)
	print('coro2 resume result', coro2())
	return "should never reach here"
end)
print(xpcall(function()
	print(coro())
	print("should never reach here")
end, function(err)
	print("in main err handler, err=", err)
	return "error from main err handler"
end))

print('-- unprotected error in coroutine (test 16) --')
coro = coroutine.wrap(function()
	print('inside coro')
	coro2 = coroutine.wrap(function()
		print('inside coro2')
		coroutine.yield("yield value from coro2")
		error(true)
		print('should never reach here')
	end)
	print('coro2 resume result', coro2())
	coroutine.yield("yield value from coro")
	print('coro2 resume result', coro2())
	return "should never reach here"
end)
print(xpcall(function()
	print(coro())
	return "should succeed"
end, function(err)
	print("should never reach here")
end))
print(xpcall(function()
	print(coro())
	print("should never reach here")
end, function(err)
	print("in main err handler, err=", err)
	return "error from main err handler"
end))

print('-- unprotected error in coroutine (test 17) --')
coro = coroutine.wrap(function()
	print('inside coro')
	coro2 = coroutine.wrap(function()
		print('inside coro2')
		coro3 = coroutine.wrap(function()
			print('inside coro3')
			error(true)
			print('should never reach here')
		end)
		print('coro3 resume result', coro3())
		print('should never reach here')
	end)
	print('coro2 resume result', coro2())
	return "should never reach here"
end)
print(xpcall(function()
	print(coro())
	print("should never reach here")
end, function(err)
	print("in main err handler, err=", err)
	return "error from main err handler"
end))

print('-- unprotected error in coroutine (test 18) --')
coro = coroutine.wrap(function()
	print('inside coro')
	coro2 = coroutine.wrap(function()
		print('inside coro2')
		coro3 = coroutine.wrap(function()
			print('inside coro3')
			coroutine.yield("yield value from coro3")
			error(true)
			print('should never reach here')
		end)
		print('coro3 resume result', coro3())
		coroutine.yield("yield value from coro2")
		print('coro3 resume result', coro3())
		print('should never reach here')
	end)
	print('coro2 resume result', coro2())
	coroutine.yield("yield value from coro1")
	print('coro2 resume result', coro2())
	return "should never reach here"
end)
print(xpcall(function()
	print(coro())
	print("should reach here")
end, function(err)
	print("should never reach here")
end))
print(xpcall(function()
	print(coro())
	print("should never reach here")
end, function(err)
	print("in main err handler, err=", err)
	return "error from main err handler"
end))

print('-- unprotected error in coroutine (test 19) --')
coro = coroutine.create(function()
	print('inside coro')
	coro2 = coroutine.wrap(function()
		print('inside coro2')
		coro3 = coroutine.wrap(function()
			print('inside coro3')
			error(true)
			print('should never reach here')
		end)
		print('coro3 resume result', coro3())
		print('should never reach here')
	end)
	print('coro2 resume result', coro2())
	return "should never reach here"
end)
print(coroutine.resume(coro))
print(coroutine.status(coro))

print('-- unprotected error in coroutine (test 20) --')
coro = coroutine.create(function()
	print('inside coro')
	coro2 = coroutine.wrap(function()
		print('inside coro2')
		coro3 = coroutine.wrap(function()
			print('inside coro3')
			coroutine.yield("yield value from coro3")
			error(true)
			print('should never reach here')
		end)
		print('coro3 resume result', coro3())
		coroutine.yield("yield value from coro2")
		print('coro3 resume result', coro3())
		print('should never reach here')
	end)
	print('coro2 resume result', coro2())
	coroutine.yield("yield value from coro1")
	print('coro2 resume result', coro2())
	return "should never reach here"
end)
print(coroutine.resume(coro))
print(coroutine.status(coro))
print(coroutine.resume(coro))
print(coroutine.status(coro))

