Skip to content

Commit 874cdf0

Browse files
committed
Fix error handling in the lua-continuations backend
1 parent 1e61222 commit 874cdf0

5 files changed

Lines changed: 21 additions & 7 deletions

File tree

lib/lunarml/mlinit-continuations.lua

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -461,13 +461,12 @@ end
461461
--END
462462

463463
-- Delimited continuations
464-
--BEGIN _handle _withSubCont _pushSubCont _run: table pcall getmetatable error table_unpack
464+
--BEGIN _handle _withSubCont _pushSubCont _run: table table_move pcall getmetatable error table_unpack
465465
local _handle, _withSubCont, _pushSubCont, _run
466466
do
467467
local ipairs = ipairs
468468
local table_insert = table.insert
469469
local table_remove = table.remove
470-
local table_move = table.move
471470
local coroutine = coroutine
472471
local coroutine_create = coroutine.create
473472
local coroutine_yield = coroutine.yield
@@ -489,9 +488,6 @@ do
489488
success, result = pcall(f)
490489
_depth = olddepth
491490
end
492-
if not success and getmetatable(result) ~= _exn_meta then
493-
result = _Error(result)
494-
end
495491
return success, result
496492
end
497493
function _pushPrompt(tag, f)

src/codegen-lua.sml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2779,11 +2779,13 @@ struct
27792779
val resultOrError = e
27802780
val functionExp = L.FunctionExp (vector [], vector
27812781
(doStat (ctx, env', NONE, body)))
2782+
val pcallId =
2783+
if #hasDelimitedContinuations ctx then L.PredefinedId "_handle"
2784+
else L.PredefinedId "pcall"
27822785
in
27832786
[ L.LocalStat
27842787
( [(status, L.CONST), (resultOrError, L.CONST)]
2785-
, [L.CallExp
2786-
(L.VarExp (L.PredefinedId "pcall"), vector [functionExp])]
2788+
, [L.CallExp (L.VarExp pcallId, vector [functionExp])]
27872789
)
27882790
, L.IfStat
27892791
( L.UnaryExp (L.NOT, L.VarExp (L.UserDefinedId status))

test/Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ should_run += should_run/xorshift64.sml
8383
else ifeq ($(VARIANT), lua-continuations)
8484
should_run += should_run/xorshift64.sml
8585
endif
86+
ifeq ($(VARIANT), luajit)
87+
should_run += should_run/deephandle.sml
88+
else ifeq ($(VARIANT), lua-continuations)
89+
should_run += should_run/deephandle.sml
90+
else ifeq ($(VARIANT), nodejs)
91+
should_run += should_run/deephandle.sml
92+
else ifeq ($(VARIANT), nodejs-cps)
93+
should_run += should_run/deephandle.sml
94+
endif
8695
should_compile += $(addprefix should_compile/,\
8796
signature_sharing1.sml \
8897
signature_sharing2.sml \

test/should_run/deephandle.sml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fun f i =
2+
if i = 0 then
3+
raise Fail "Hello!"
4+
else
5+
f (i - 1) handle e => raise e;
6+
f 500 handle Fail s => print (s ^ "\n");

test/should_run/deephandle.stdout

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello!

0 commit comments

Comments
 (0)