Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Enable iteratoin of native arrays.
  • Loading branch information
jnthn committed Jun 21, 2013
1 parent eab1b7c commit d4a13c5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
14 changes: 13 additions & 1 deletion src/vm/jvm/runtime/org/perl6/nqp/runtime/Ops.java
Expand Up @@ -2269,7 +2269,19 @@ public static SixModelObject iter(SixModelObject agg, ThreadContext tc) {
iter.target = agg;
iter.idx = -1;
iter.limit = agg.elems(tc);
iter.iterMode = VMIterInstance.MODE_ARRAY;
switch (agg.st.REPR.get_value_storage_spec(tc, agg.st).boxed_primitive) {
case StorageSpec.BP_INT:
iter.iterMode = VMIterInstance.MODE_ARRAY_INT;
break;
case StorageSpec.BP_NUM:
iter.iterMode = VMIterInstance.MODE_ARRAY_NUM;
break;
case StorageSpec.BP_STR:
iter.iterMode = VMIterInstance.MODE_ARRAY_STR;
break;
default:
iter.iterMode = VMIterInstance.MODE_ARRAY;
}
return iter;
}
else if (agg.st.REPR instanceof VMHash) {
Expand Down
Expand Up @@ -3,6 +3,7 @@
import java.util.Iterator;

import org.perl6.nqp.runtime.ExceptionHandling;
import org.perl6.nqp.runtime.Ops;
import org.perl6.nqp.runtime.ThreadContext;
import org.perl6.nqp.sixmodel.SixModelObject;

Expand Down Expand Up @@ -34,7 +35,10 @@ public class VMIterInstance extends SixModelObject {
* Possible modes.
*/
public final static byte MODE_ARRAY = 1;
public final static byte MODE_HASH = 2;
public final static byte MODE_ARRAY_INT = 2;
public final static byte MODE_ARRAY_NUM = 3;
public final static byte MODE_ARRAY_STR = 4;
public final static byte MODE_HASH = 5;

/**
* Iterators work like things you can shift from. This is mostly because
Expand All @@ -48,6 +52,24 @@ public SixModelObject shift_boxed(ThreadContext tc) {
if (idx >= limit)
throw ExceptionHandling.dieInternal(tc, "Iteration past end of iterator");
return target.at_pos_boxed(tc, idx);
case MODE_ARRAY_INT:
idx++;
if (idx >= limit)
throw ExceptionHandling.dieInternal(tc, "Iteration past end of iterator");
target.at_pos_native(tc, idx);
return Ops.box_i(tc.native_i, tc.curFrame.codeRef.staticInfo.compUnit.hllConfig.intBoxType, tc);
case MODE_ARRAY_NUM:
idx++;
if (idx >= limit)
throw ExceptionHandling.dieInternal(tc, "Iteration past end of iterator");
target.at_pos_native(tc, idx);
return Ops.box_n(tc.native_n, tc.curFrame.codeRef.staticInfo.compUnit.hllConfig.numBoxType, tc);
case MODE_ARRAY_STR:
idx++;
if (idx >= limit)
throw ExceptionHandling.dieInternal(tc, "Iteration past end of iterator");
target.at_pos_native(tc, idx);
return Ops.box_s(tc.native_s, tc.curFrame.codeRef.staticInfo.compUnit.hllConfig.strBoxType, tc);
case MODE_HASH:
curKey = hashKeyIter.next();
curValue = target.at_key_boxed(tc, curKey);
Expand All @@ -63,6 +85,9 @@ public SixModelObject shift_boxed(ThreadContext tc) {
public boolean boolify() {
switch (iterMode) {
case MODE_ARRAY:
case MODE_ARRAY_INT:
case MODE_ARRAY_NUM:
case MODE_ARRAY_STR:
return idx + 1 < limit;
case MODE_HASH:
return hashKeyIter.hasNext();
Expand Down

0 comments on commit d4a13c5

Please sign in to comment.