リビジョン | 5eebe2a4407103dbc1060e9e625c2cb2c79b0f0c (tree) |
---|---|
日時 | 2023-03-19 15:42:20 |
作者 | miyakawataku |
コミッター | miyakawataku |
replace invocations of CORE.reraise by Exception.raise
@@ -53,7 +53,7 @@ | ||
53 | 53 | * @param traces the exception traces. |
54 | 54 | * @return an exception val without chaining. |
55 | 55 | */ |
56 | - public ExceptionVal of(String message, List<TraceVal> traces) { | |
56 | + public ExceptionVal of(String message, List<? extends TraceVal> traces) { | |
57 | 57 | return new ExceptionVal(vm, message, traces); |
58 | 58 | } |
59 | 59 |
@@ -26,7 +26,7 @@ | ||
26 | 26 | private ExceptionVal( |
27 | 27 | Vm vm, |
28 | 28 | String message, |
29 | - List<TraceVal> traces, | |
29 | + List<? extends TraceVal> traces, | |
30 | 30 | Optional<ExceptionVal> next) { |
31 | 31 | super(vm); |
32 | 32 | this.message = message; |
@@ -37,14 +37,14 @@ | ||
37 | 37 | /** |
38 | 38 | * Constructs an exception val without cahining. |
39 | 39 | */ |
40 | - ExceptionVal(Vm vm, String message, List<TraceVal> traces) { | |
40 | + ExceptionVal(Vm vm, String message, List<? extends TraceVal> traces) { | |
41 | 41 | this(vm, message, traces, Optional.empty()); |
42 | 42 | } |
43 | 43 | |
44 | 44 | /** |
45 | 45 | * Constructs an exception val with the next in the chain. |
46 | 46 | */ |
47 | - ExceptionVal(Vm vm, String message, List<TraceVal> traces, ExceptionVal next) { | |
47 | + ExceptionVal(Vm vm, String message, List<? extends TraceVal> traces, ExceptionVal next) { | |
48 | 48 | this(vm, message, traces, Optional.of(next)); |
49 | 49 | } |
50 | 50 |
@@ -39,11 +39,8 @@ | ||
39 | 39 | /** The dataStack. */ |
40 | 40 | private final DataStack dataStack; |
41 | 41 | |
42 | - /** Handle of {@code reraise}. */ | |
43 | - private final int reraiseHandle; | |
44 | - | |
45 | - /** Handle of {@code format}. */ | |
46 | - private final int formatHandle; | |
42 | + /** Handle of {@code raise}. */ | |
43 | + private final int raiseHandle; | |
47 | 44 | |
48 | 45 | /** |
49 | 46 | * Constructs a stack machine. |
@@ -52,8 +49,7 @@ | ||
52 | 49 | this.vm = vm; |
53 | 50 | this.callStack = callStack; |
54 | 51 | this.dataStack = dataStack; |
55 | - this.reraiseHandle = vm.sym.getHandle("reraise"); | |
56 | - this.formatHandle = vm.sym.getHandle("format"); | |
52 | + this.raiseHandle = vm.sym.getHandle("raise"); | |
57 | 53 | } |
58 | 54 | |
59 | 55 | /** |
@@ -281,8 +277,8 @@ | ||
281 | 277 | /** |
282 | 278 | * Returns traces on the specified trace. |
283 | 279 | */ |
284 | - private List<Val> getTracesOn(Trace trace) { | |
285 | - List<Val> traces = new ArrayList<>(this.callStack.getTraces().stream() | |
280 | + private List<TraceVal> getTracesOn(Trace trace) { | |
281 | + List<TraceVal> traces = new ArrayList<>(this.callStack.getTraces().stream() | |
286 | 282 | .map(traceCse -> traceCse.toTraceVal(vm)) |
287 | 283 | .collect(Collectors.toList())); |
288 | 284 | traces.add(trace.toTraceVal(this.vm)); |
@@ -290,20 +286,34 @@ | ||
290 | 286 | } |
291 | 287 | |
292 | 288 | /** |
293 | - * Makes a fun to reraise(template.format(...args)). | |
289 | + * Fun to call exception(template.format(...args), traces).raise. | |
294 | 290 | */ |
295 | 291 | private FunVal makeRaiseFormatFun( |
296 | - List<? extends Val> traces, String template, GraphNode... args) { | |
292 | + List<? extends TraceVal> traces, String template, GraphNode... args) { | |
297 | 293 | return vm.fun.make("(raise-after-format)").action(c -> { |
298 | - Val binding = vm.binding.newBinding(); | |
299 | - return c.call(vm.graph.call(vm.graph.of(binding), reraiseHandle).args( | |
300 | - vm.graph.call(vm.graph.of(vm.str.of(template)), formatHandle).args(args), | |
301 | - vm.graph.of(vm.vec.of(traces)))); | |
294 | + var makeMessage = vm.fun.make().action(cc -> cc.call(vm.graph.format(template, args))); | |
295 | + return c.call(makeMessage) | |
296 | + .on((cc, message) -> cc.call(exceptionRaiseCaller(message, traces))); | |
302 | 297 | }); |
303 | 298 | } |
304 | 299 | |
305 | 300 | /** |
306 | - * Transitions to reraise(template.format(...args) traces). | |
301 | + * Fun to call exception(message, traces).raise. | |
302 | + */ | |
303 | + FunVal exceptionRaiseCaller(Val messageVal, List<? extends TraceVal> traces) { | |
304 | + return vm.fun.make().action(c -> { | |
305 | + if (! (messageVal instanceof StrVal messageStr)) { | |
306 | + return c.call(vm.graph.raiseFormat( | |
307 | + "Str.format must return str, but got {}", | |
308 | + vm.graph.repr(messageVal))); | |
309 | + } | |
310 | + var exc = vm.exception.of(messageStr.getString(), traces); | |
311 | + return c.call(exc, raiseHandle); | |
312 | + }); | |
313 | + } | |
314 | + | |
315 | + /** | |
316 | + * Transitions to exception(template.format(...args) traces).new. | |
307 | 317 | */ |
308 | 318 | private void transitionToRaiseFormatOn( |
309 | 319 | Trace trace, String template, GraphNode... args) { |
@@ -2,6 +2,7 @@ | ||
2 | 2 | # Handles Java exceptions or throwables. |
3 | 3 | |
4 | 4 | :JAVA.require_from('kink/javahost/') |
5 | +:EXCEPTION.require_from('kink/') | |
5 | 6 | |
6 | 7 | :Throwable_class <- JAVA.class('java.lang.Throwable') |
7 | 8 | :Vm <- JAVA.wrap(new_val).get_field('vm') |
@@ -17,7 +18,7 @@ | ||
17 | 18 | .format(Th.repr)) |
18 | 19 | :Msg = Th.call_method('toString' []).to_kink_str |
19 | 20 | :Th_traces = traces_from_throwable(Th) |
20 | - reraise(Msg (traces + Th_traces)) | |
21 | + EXCEPTION.new(Msg (traces + Th_traces)).raise | |
21 | 22 | } |
22 | 23 | |
23 | 24 | ## JAVA_EXCEPTION.traces_from_throwable(Th) |
@@ -862,6 +862,16 @@ | ||
862 | 862 | assertThat(msg).contains("something wrong", "RuntimeException"); |
863 | 863 | } |
864 | 864 | |
865 | + @Test | |
866 | + public void exceptionRaiseCaller_checks_message_type() { | |
867 | + var caller = stackMachine.exceptionRaiseCaller(vm.nada, List.of()); | |
868 | + var outcome = stackMachine.run(caller); | |
869 | + var excMessage = outcome.accept( | |
870 | + r -> { throw new AssertionError(); }, | |
871 | + exc -> exc.message()); | |
872 | + assertThat(excMessage).contains("nada").contains("format"); | |
873 | + } | |
874 | + | |
865 | 875 | // }}}1 |
866 | 876 | |
867 | 877 | // test CallContext#getArg {{{1 |
@@ -1,6 +1,7 @@ | ||
1 | 1 | #!/usr/bin/env kink |
2 | 2 | |
3 | 3 | :CONTROL.require_from('kink/') |
4 | +:EXCEPTION.require_from('kink/') | |
4 | 5 | :KONT.require_from('kink/') |
5 | 6 | :TRACE.require_from('kink/') |
6 | 7 | :VEC.require_from('kink/') |
@@ -111,7 +112,7 @@ | ||
111 | 112 | TRACE.new{(:C) C.sym('bar') } |
112 | 113 | ] |
113 | 114 | [:Msg :Traces] = CONTROL.try( |
114 | - { reraise('bang!' Expected_traces) } | |
115 | + { EXCEPTION.new('bang!' Expected_traces).raise } | |
115 | 116 | { raise('not here') } |
116 | 117 | {(:Exc) [Exc.message Exc.traces] } |
117 | 118 | ) |
@@ -59,24 +59,6 @@ | ||
59 | 59 | |
60 | 60 | # }}}1 |
61 | 61 | |
62 | -TEST.group('CORE.reraise'){ # {{{1 | |
63 | - TEST.test('alternative message for non-str argument'){ | |
64 | - CONTROL.try( | |
65 | - { reraise(42 []) } | |
66 | - {(:Result) raise('expected an exception, but got {}'.format(Result.repr)) } | |
67 | - {(:Exc) | |
68 | - Exc.message == 'exception message is not str' || Exc.raise | |
69 | - } | |
70 | - ) | |
71 | - } | |
72 | - | |
73 | - TEST.test('.repr'){ | |
74 | - :Repr = $reraise.repr | |
75 | - Repr == 'Fun(_variant=CORE.reraise(Msg Traces))' || raise('got {}'.format(Repr.repr)) | |
76 | - } | |
77 | -} | |
78 | - | |
79 | -# }}}1 | |
80 | 62 | TEST.group('CORE.require'){ # {{{1 |
81 | 63 | :save_mod_paths <- {(:thunk) |
82 | 64 | CONTROL.with_on_exit{(:on_exit) |