Skip to content

Commit 7c86f75

Browse files
committed
Read the instructions array patterns use from persisted IR
Array patterns compile to instructions that IRReaderStream couldn't read back: TemporaryIntVariable operands, b_int, int_math and as_fixnum. Each class already had a decode method, but the reader's switches never called them, so loading a jrubyc-compiled file with an array pattern failed with a NullPointerException. IntegerMathInstr also didn't write its operands, so it couldn't have been read back even with a reader case. It now writes them in the order its decode method reads them. Fixes jruby#9834
1 parent 32d3e80 commit 7c86f75

4 files changed

Lines changed: 13 additions & 0 deletions

File tree

‎core/src/main/java/org/jruby/ir/instructions/IntegerMathInstr.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public static IntegerMathInstr decode(IRReaderDecoder d) {
4747
@Override
4848
public void encode(IRWriterEncoder e) {
4949
super.encode(e);
50+
e.encode(getOperand1());
51+
e.encode(getOperand2());
5052
e.encode(op.ordinal());
5153
}
5254

‎core/src/main/java/org/jruby/ir/operands/TemporaryLocalVariable.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public static TemporaryLocalVariable decode(IRReaderDecoder d) {
9090
case CURRENT_MODULE: return TemporaryCurrentModuleVariable.decode(d);
9191
case FLOAT: return TemporaryFloatVariable.decode(d);
9292
case FIXNUM: return TemporaryFixnumVariable.decode(d);
93+
case INT: return TemporaryIntVariable.decode(d);
9394
case BOOLEAN: return TemporaryBooleanVariable.decode(d);
9495
case LOCAL: return d.getCurrentScope().getManager().newTemporaryLocalVariable(d.decodeInt());
9596
}

‎core/src/main/java/org/jruby/ir/persistence/IRReaderStream.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,10 @@ public Instr decodeInstr() {
252252
case ALIAS: return AliasInstr.decode(this);
253253
case ARRAY_DEREF: return ArrayDerefInstr.decode(this);
254254
case ATTR_ASSIGN: return AttrAssignInstr.decode(this);
255+
case AS_FIXNUM: return AsFixnumInstr.decode(this);
255256
case AS_STRING: return AsStringInstr.decode(this);
256257
case B_FALSE: return BFalseInstr.decode(this);
258+
case B_INT: return BIntInstr.decode(this);
257259
case B_NIL: return BNilInstr.decode(this);
258260
case B_SWITCH: return BSwitchInstr.decode(this);
259261
case B_TRUE: return BTrueInstr.decode(this);
@@ -301,6 +303,7 @@ public Instr decodeInstr() {
301303
case GVAR_ALIAS: return GVarAliasInstr.decode(this);
302304
case INHERITANCE_SEARCH_CONST: return InheritanceSearchConstInstr.decode(this);
303305
case INSTANCE_SUPER: return InstanceSuperInstr.decode(this);
306+
case INT_MATH: return IntegerMathInstr.decode(this);
304307
case JUMP: return JumpInstr.decode(this);
305308
case LABEL: return LabelInstr.decode(this);
306309
case LAMBDA: return BuildLambdaInstr.decode(this);

‎spec/compiler/general_spec.rb‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,13 @@ class AFromLocal < a
964964
run("def foo; case 0; when 1; return 2 if self.nil?; end; return 3; end; foo") {|result| expect(result).to eq 3 }
965965
end
966966

967+
# See https://github.com/jruby/jruby/issues/9834
968+
it "compiles array patterns" do
969+
run('[([1] in [1]), ([1, 2] in [_, 2]), ([1, 2, 3] in [1, *r]) && r, (case [3]; in [x] then x; end)]') do |x|
970+
expect(x).to eq [true, true, [2, 3], 3]
971+
end
972+
end
973+
967974
it "assigns named groups in regular expressions to local variables" do
968975
# named groups with capture
969976
run("

0 commit comments

Comments
 (0)