• R/O
  • SSH

kink: Commit

Kink runtime


Commit MetaInfo

Revision42c77f773ff8c6e633185f1009abb4dae5e81e30 (tree)
Time2020-10-14 00:30:04
Authormiyakawataku
Commitermiyakawataku

Log Message

make abort and replay take CallStackSlice instead of Cse[]

Change Summary

Incremental Difference

diff -r f2383b755af9 -r 42c77f773ff8 src/main/java/org/kink_lang/kink/KontMod.java
--- a/src/main/java/org/kink_lang/kink/KontMod.java Mon Oct 12 23:36:20 2020 +0900
+++ b/src/main/java/org/kink_lang/kink/KontMod.java Wed Oct 14 00:30:04 2020 +0900
@@ -4,8 +4,7 @@
44
55 import org.kink_lang.kink.hostfun.CallContext;
66 import org.kink_lang.kink.hostfun.HostResult;
7-import org.kink_lang.kink.internal.callstack.CallStack;
8-import org.kink_lang.kink.internal.callstack.Cse;
7+import org.kink_lang.kink.internal.callstack.CallStackSlice;
98 import org.kink_lang.kink.internal.callstack.ResetFrame;
109
1110 /**
@@ -117,10 +116,10 @@
117116 stackMachine.transitionToRaise(msg);
118117 return;
119118 }
120- Cse[] callSlice = stackMachine.getCallStack().abort(rtag);
119+ CallStackSlice callSlice = stackMachine.getCallStack().abort(rtag);
121120
122121 stackMachine.getDataStack().removeFromOffset(0);
123- int dataStackUsage = CallStack.dataStackUsage(callSlice);
122+ int dataStackUsage = callSlice.dataStackUsage();
124123 Val[] dataSlice = stackMachine.getDataStack().sliceTop(dataStackUsage);
125124 stackMachine.getDataStack().decreaseBp(dataStackUsage);
126125 stackMachine.getDataStack().removeFromOffset(0);
@@ -139,7 +138,7 @@
139138 /**
140139 * Implementation of continuation funs.
141140 */
142- static FunVal kontFunVal(Vm vm, Cse[] callSlice, Val[] dataSlice) {
141+ static FunVal kontFunVal(Vm vm, CallStackSlice callSlice, Val[] dataSlice) {
143142 return new FunVal(vm) {
144143 @Override String getRepr() {
145144 return String.format(Locale.ROOT, "Fun(val_id=%s _variant=kont)", getIdentity());
diff -r f2383b755af9 -r 42c77f773ff8 src/main/java/org/kink_lang/kink/StackMachine.java
--- a/src/main/java/org/kink_lang/kink/StackMachine.java Mon Oct 12 23:36:20 2020 +0900
+++ b/src/main/java/org/kink_lang/kink/StackMachine.java Wed Oct 14 00:30:04 2020 +0900
@@ -21,8 +21,8 @@
2121 import org.kink_lang.kink.internal.contract.Preconds;
2222 import org.kink_lang.kink.function.ThrowingFunction2;
2323 import org.kink_lang.kink.internal.callstack.ConsumerFrame;
24-import org.kink_lang.kink.internal.callstack.Cse;
2524 import org.kink_lang.kink.internal.callstack.CallStack;
25+import org.kink_lang.kink.internal.callstack.CallStackSlice;
2626 import org.kink_lang.kink.internal.callstack.Location;
2727 import org.kink_lang.kink.internal.callstack.ResumeQblockFrame;
2828 import org.kink_lang.kink.internal.callstack.RetValHandlerFrame;
@@ -358,8 +358,8 @@
358358 List<Val> deepCleanups = this.callStack.getDynStack("kink/CONTROL-cleanup");
359359
360360 if (this.callStack.canAbort("kink/CONTROL-try")) {
361- Cse[] cses = this.callStack.abort("kink/CONTROL-try");
362- int dataStackUsage = CallStack.dataStackUsage(cses);
361+ CallStackSlice callSlice = this.callStack.abort("kink/CONTROL-try");
362+ int dataStackUsage = callSlice.dataStackUsage();
363363 this.dataStack.decreaseBp(dataStackUsage);
364364 this.dataStack.removeFromOffset(0);
365365
diff -r f2383b755af9 -r 42c77f773ff8 src/main/java/org/kink_lang/kink/internal/callstack/CallStack.java
--- a/src/main/java/org/kink_lang/kink/internal/callstack/CallStack.java Mon Oct 12 23:36:20 2020 +0900
+++ b/src/main/java/org/kink_lang/kink/internal/callstack/CallStack.java Wed Oct 14 00:30:04 2020 +0900
@@ -234,7 +234,7 @@
234234 * @throws IllegalArgumentException
235235 * if the call stack contains no {@link ResetFrame} with the rtag.
236236 */
237- public Cse[] abort(String rtag) {
237+ public CallStackSlice abort(String rtag) {
238238 int delimInd = findDelimiterIndex(rtag);
239239 Preconds.checkArg(delimInd >= 0, "rtag must exist on the call stack");
240240 int contEnd = this.sp;
@@ -246,7 +246,7 @@
246246 // here the top cse is ResetFrame, thus there are no trace span
247247 this.curTraceSpanLength = 0;
248248
249- return cont;
249+ return new CallStackSlice(cont);
250250 }
251251
252252 /**
@@ -254,9 +254,7 @@
254254 */
255255 private Cse[] extractCont(int delimInd, int contSize) {
256256 Cse[] cont = new Cse[contSize];
257- for (int i = 0; i < contSize; ++ i) {
258- cont[i] = this.elems[delimInd + i];
259- }
257+ System.arraycopy(this.elems, delimInd, cont, 0, contSize);
260258 return cont;
261259 }
262260
@@ -277,27 +275,13 @@
277275 }
278276
279277 /**
280- * Returns the total usage of the dataStack by the elems.
278+ * Returns true if the call stack can replay the slice.
281279 *
282- * @param elems the elems.
283- * @return the total usage of the dataStack by the elems.
284- */
285- public static int dataStackUsage(Cse[] elems) {
286- int sum = 0;
287- for (Cse elem : elems) {
288- sum += elem.getDataStackUsage();
289- }
290- return sum;
291- }
292-
293- /**
294- * Returns true if the call stack can replay the cont elems.
295- *
296- * @param contFrames the delimited continuation elems.
280+ * @param callSlice the delimited continuation slice.
297281 * @return true if the call stack can replay the cont elems.
298282 */
299- public boolean canReplay(Cse[] contFrames) {
300- return this.sp + 1 + contFrames.length <= this.maxModerateSize;
283+ public boolean canReplay(CallStackSlice callSlice) {
284+ return this.sp + 1 + callSlice.getCallStackElements().length <= this.maxModerateSize;
301285 }
302286
303287 /**
@@ -306,18 +290,19 @@
306290 * <p>Precondition: {@link #canReplay(Cse[])} must return true.
307291 * {@code contFrames} must be a result of {@link #abort(String)}</p>
308292 *
309- * @param contElems the delimited continuation elems.
293+ * @param callSlice the delimited continuation slice.
310294 */
311- public void replay(Cse[] contElems) {
312- Preconds.checkState(canReplay(contElems),
295+ public void replay(CallStackSlice callSlice) {
296+ Preconds.checkState(canReplay(callSlice),
313297 "stack overflow; must check by canReplay beforehand");
314- ensureCapa(this.sp + 1 + contElems.length);
298+ int elemCount = callSlice.getCallStackElements().length;
299+ ensureCapa(this.sp + 1 + elemCount);
315300 if (this.curTraceSpanLength >= 1) {
316301 push(TraceSpanLengthCse.of(this.curTraceSpanLength));
317302 this.curTraceSpanLength = 0;
318303 }
319- for (Cse elem : contElems) {
320- push(elem.copy());
304+ for (Cse elem : callSlice.getCallStackElements()) {
305+ push(elem);
321306 }
322307 restoreTraceSpanLength();
323308 }
diff -r f2383b755af9 -r 42c77f773ff8 src/main/java/org/kink_lang/kink/internal/callstack/Cse.java
--- a/src/main/java/org/kink_lang/kink/internal/callstack/Cse.java Mon Oct 12 23:36:20 2020 +0900
+++ b/src/main/java/org/kink_lang/kink/internal/callstack/Cse.java Wed Oct 14 00:30:04 2020 +0900
@@ -15,16 +15,6 @@
1515 return 0;
1616 }
1717
18- /**
19- * Returns a copy of this call stack element.
20- * If the the element is immutable, the method can return {@code this}.
21- *
22- * @return a copy of this call stack element.
23- */
24- public default Cse copy() {
25- return this;
26- }
27-
2818 }
2919
3020 // vim: et sw=4 sts=4 fdm=marker
diff -r f2383b755af9 -r 42c77f773ff8 src/test/java/org/kink_lang/kink/KontModTest.java
--- a/src/test/java/org/kink_lang/kink/KontModTest.java Mon Oct 12 23:36:20 2020 +0900
+++ b/src/test/java/org/kink_lang/kink/KontModTest.java Wed Oct 14 00:30:04 2020 +0900
@@ -12,6 +12,7 @@
1212 import mockit.Injectable;
1313
1414 import org.kink_lang.kink.internal.callstack.CallStack;
15+import org.kink_lang.kink.internal.callstack.CallStackSlice;
1516 import org.kink_lang.kink.internal.callstack.Cse;
1617 import org.kink_lang.kink.internal.callstack.ResetFrame;
1718 import org.kink_lang.kink.internal.callstack.TraceCse;
@@ -167,7 +168,7 @@
167168 stackMachine.transitionToRaise(withMatch(".*continuation.*"));
168169 }};
169170
170- Cse[] callSlice = { new ResetFrame("boo") };
171+ CallStackSlice callSlice = new CallStackSlice(new Cse[] { new ResetFrame("boo") });
171172 Val[] dataSlice = { vm.num.of(0), vm.num.of(1) };
172173 FunVal kont = KontMod.kontFunVal(vm, callSlice, dataSlice);
173174 kont.run(stackMachine);
@@ -185,10 +186,10 @@
185186 stackMachine.transitionToRaise(withMatch(".*continuation.*"));
186187 }};
187188
188- Cse[] callSlice = {
189+ CallStackSlice callSlice = new CallStackSlice(new Cse[] {
189190 new ResetFrame("boo"), new ResetFrame("ping"), new ResetFrame("pong"),
190191 new ResetFrame("voxxx"), new ResetFrame("twfit"),
191- };
192+ });
192193 Val[] dataSlice = { vm.num.of(0), vm.num.of(1) };
193194 FunVal kont = KontMod.kontFunVal(vm, callSlice, dataSlice);
194195 kont.run(stackMachine);
diff -r f2383b755af9 -r 42c77f773ff8 src/test/java/org/kink_lang/kink/internal/callstack/CallStackTest.java
--- a/src/test/java/org/kink_lang/kink/internal/callstack/CallStackTest.java Mon Oct 12 23:36:20 2020 +0900
+++ b/src/test/java/org/kink_lang/kink/internal/callstack/CallStackTest.java Wed Oct 14 00:30:04 2020 +0900
@@ -222,7 +222,7 @@
222222 cs.pushTrace(TraceCse.of(2)); // removed from continuation
223223
224224 assertThat(cs.canAbort("kink/CONTROL-try")).isTrue();
225- assertThat(cs.abort("kink/CONTROL-try")).containsExactly(
225+ assertThat(cs.abort("kink/CONTROL-try").getCallStackElements()).containsExactly(
226226 new ResetFrame("kink/CONTROL-try"),
227227 new RetValHandlerFrame(xHandler, 1),
228228 new ResetFrame("coroutine"),
@@ -251,6 +251,7 @@
251251 }
252252
253253 // }}}1
254+
254255 // test getDynStack {{{1
255256
256257 @Test
@@ -271,27 +272,7 @@
271272 }
272273
273274 // }}}1
274- // test calculation of stack usages {{{1
275275
276- @Test
277- public void test_dataStackUsage() {
278- Cse[] frames = {
279- frameWithUsage(1),
280- frameWithUsage(3),
281- frameWithUsage(5),
282- };
283- assertThat(CallStack.dataStackUsage(frames)).isEqualTo(1 + 3 + 5);
284- }
285-
286- private static Cse frameWithUsage(int dataStackUsage) {
287- return new Cse() {
288- @Override public int getDataStackUsage() {
289- return dataStackUsage;
290- }
291- };
292- }
293-
294- // }}}
295276 // test replay {{{1
296277
297278 @Test
@@ -300,10 +281,9 @@
300281 cs.pushNonTrace(new ResetFrame("kink/CONTROL-try"));
301282 cs.pushNonTrace(new ResetFrame("start"));
302283 cs.pushTrace(TraceCse.of(1));
303- Cse[] dcont1 = new Cse[] {
304- new ResetFrame("coroutine"),
305- TraceCse.of(2)
306- };
284+ CallStackSlice dcont1 = new CallStackSlice(new Cse[] {
285+ new ResetFrame("coroutine"), TraceCse.of(2)
286+ });
307287 assertThat(cs.canReplay(dcont1)).isTrue();
308288 cs.replay(dcont1);
309289 assertThat(cs.getElems()).containsExactly(
@@ -314,9 +294,10 @@
314294 new ResetFrame("coroutine"),
315295 TraceCse.of(2));
316296
317- Cse[] dcont2 = new Cse[] { new ResetFrame("coroutine") };
297+ CallStackSlice dcont2 = new CallStackSlice(new Cse[] { new ResetFrame("coroutine") });
318298 assertThat(cs.canReplay(dcont2)).isFalse();
319- assertThatThrownBy(() -> cs.replay(dcont1)).isInstanceOf(IllegalStateException.class);
299+ assertThatThrownBy(() -> cs.replay(dcont1))
300+ .isInstanceOf(IllegalStateException.class);
320301 }
321302
322303 @Test
@@ -324,10 +305,9 @@
324305 CallStack cs = new CallStack(2, 5, 1, 1);
325306 cs.pushNonTrace(new ResetFrame("kink/CONTROL-try"));
326307 cs.pushNonTrace(new ResetFrame("start"));
327- Cse[] dcont1 = new Cse[] {
328- new ResetFrame("coroutine"),
329- TraceCse.of(2)
330- };
308+ CallStackSlice dcont1 = new CallStackSlice(new Cse[] {
309+ new ResetFrame("coroutine"), TraceCse.of(2)
310+ });
331311 assertThat(cs.canReplay(dcont1)).isTrue();
332312 cs.replay(dcont1);
333313 assertThat(cs.getElems()).containsExactly(
@@ -336,9 +316,10 @@
336316 new ResetFrame("coroutine"),
337317 TraceCse.of(2));
338318
339- Cse[] dcont2 = new Cse[] { new ResetFrame("coroutine") };
319+ CallStackSlice dcont2 = new CallStackSlice(new Cse[] { new ResetFrame("coroutine") });
340320 assertThat(cs.canReplay(dcont2)).isFalse();
341- assertThatThrownBy(() -> cs.replay(dcont1)).isInstanceOf(IllegalStateException.class);
321+ assertThatThrownBy(() -> cs.replay(dcont1))
322+ .isInstanceOf(IllegalStateException.class);
342323 }
343324
344325 // }}}1
diff -r f2383b755af9 -r 42c77f773ff8 src/test/java/org/kink_lang/kink/internal/callstack/CseTest.java
--- a/src/test/java/org/kink_lang/kink/internal/callstack/CseTest.java Mon Oct 12 23:36:20 2020 +0900
+++ b/src/test/java/org/kink_lang/kink/internal/callstack/CseTest.java Wed Oct 14 00:30:04 2020 +0900
@@ -12,12 +12,6 @@
1212 assertThat(cse.getDataStackUsage()).isEqualTo(0);
1313 }
1414
15- @Test
16- public void test_default_copy() {
17- Cse cse = new Cse() {};
18- assertThat(cse.copy()).isSameAs(cse);
19- }
20-
2115 }
2216
2317 // vim: et sw=4 sts=4 fdm=marker
Show on old repository browser