% WAMVM exception/catch handling regression tests.
%
% Run with:
%   FLOWLOG_WAMVM_REQUIRE_VM=1 ./flowlog --engine wamvm -f tests/wamvm_exceptions_vm.pl -g ok.
%
% This covers a subtle VM bug where catching an exception thrown inside a
% conjunction could corrupt the exception term (due to premature release of
% query-temporary allocations).

:- catch(set_prolog_flag(flowlog_engine, wamvm), _, true).

ok :-
  % Catch an exception thrown from within a conjunction: the caught term must
  % remain valid and matchable by the catcher pattern.
  catch((call(asserta(_)), R0 = []), error(instantiation_error, _), R0 = instantiation_error),
  R0 == instantiation_error,

  % Type errors must preserve their structure/culprit.
  catch(call(asserta((foo :- 4))), error(type_error(callable, 4), _), true),

  % Database op errors should be catchable and stable too.
  catch(abolish(foo/a), error(type_error(integer, a), _), true),
  catch(abolish(foo/ -1), error(domain_error(not_less_than_zero, -1), _), true).
