Skip to content

Commit

Permalink
Encoding and Decoding now understand how to encode/decode byte[] and …
Browse files Browse the repository at this point in the history
…Encoding.

FrozenString and StringLiteral fixed up to properly dump encoding and coderange.
  • Loading branch information
enebo committed Mar 11, 2015
1 parent 4192682 commit b173aca
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 13 deletions.
5 changes: 3 additions & 2 deletions core/src/main/java/org/jruby/ir/operands/FrozenString.java
Expand Up @@ -8,6 +8,7 @@
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.ByteList;
import org.jruby.util.StringSupport;

/**
* Represents a literal string value.
Expand All @@ -18,11 +19,11 @@
*/
public class FrozenString extends StringLiteral {
public FrozenString(ByteList byteList, int cr) {
super(byteList, cr);
super(OperandType.FROZEN_STRING, byteList, cr);
}

public FrozenString(String s) {
super(s);
this(ByteList.create(s), StringSupport.CR_7BIT);
}

@Override
Expand Down
8 changes: 6 additions & 2 deletions core/src/main/java/org/jruby/ir/operands/StringLiteral.java
Expand Up @@ -33,11 +33,15 @@ public class StringLiteral extends Operand {
final public int coderange;

public StringLiteral(ByteList val) {
this(val, StringSupport.CR_7BIT);
this(OperandType.STRING_LITERAL, val, StringSupport.CR_7BIT);
}

public StringLiteral(ByteList val, int coderange) {
super(OperandType.STRING_LITERAL);
this(OperandType.STRING_LITERAL, val, coderange);
}

protected StringLiteral(OperandType type, ByteList val, int coderange) {
super(type);

bytelist = val;
this.coderange = coderange;
Expand Down
Expand Up @@ -6,6 +6,7 @@

package org.jruby.ir.persistence;

import org.jcodings.Encoding;
import org.jruby.ir.IRScope;
import org.jruby.ir.IRScopeType;
import org.jruby.ir.Operation;
Expand Down Expand Up @@ -37,6 +38,8 @@ public interface IRReaderDecoder {
public OperandType decodeOperandType();
public boolean decodeBoolean();
public byte decodeByte();
public byte[] decodeByteArray();
public Encoding decodeEncoding();
public ByteList decodeByteList();
public char decodeChar();
public int decodeInt();
Expand Down
16 changes: 11 additions & 5 deletions core/src/main/java/org/jruby/ir/persistence/IRReaderFile.java
Expand Up @@ -7,6 +7,7 @@
package org.jruby.ir.persistence;

import org.jcodings.Encoding;
import org.jcodings.EncodingDB;
import org.jruby.RubyInstanceConfig;
import org.jruby.ir.IRManager;
import org.jruby.ir.IRScope;
Expand Down Expand Up @@ -64,15 +65,20 @@ public IRReaderFile(IRManager manager, File file) {

@Override
public ByteList decodeByteList() {
return new ByteList(decodeByteArray(), decodeEncoding());
}

@Override
public byte[] decodeByteArray() {
int size = decodeInt();
byte[] bytes = new byte[size];
buf.get(bytes);
// FIXME: This is horrible :)
// FIXME: Add direct encode/decode support for Encoding
String encoding = decodeString();
Encoding actualEncoding = Encoding.load(encoding);
return bytes;
}

return new ByteList(bytes, actualEncoding);
@Override
public Encoding decodeEncoding() {
return EncodingDB.getEncodings().get(decodeByteArray()).getEncoding();
}

@Override
Expand Down
11 changes: 11 additions & 0 deletions core/src/main/java/org/jruby/ir/persistence/IRWriterAnalzer.java
Expand Up @@ -6,6 +6,7 @@

package org.jruby.ir.persistence;

import org.jcodings.Encoding;
import org.jruby.ir.IRScope;
import org.jruby.ir.IRScopeType;
import org.jruby.ir.Operation;
Expand Down Expand Up @@ -40,6 +41,16 @@ public void encode(Instr instr) {
public void encode(ByteList value) {
}

@Override
public void encode(byte[] value) {

}

@Override
public void encode(Encoding encoding) {

}

@Override
public void encode(String value) {
}
Expand Down
@@ -1,5 +1,6 @@
package org.jruby.ir.persistence;

import org.jcodings.Encoding;
import org.jruby.ir.IRScope;
import org.jruby.ir.IRScopeType;
import org.jruby.ir.Operation;
Expand All @@ -18,6 +19,7 @@
public interface IRWriterEncoder {

public void encode(ByteList bytelist);
public void encode(Encoding encoding);
public void encode(String value);
public void encode(String[] values);
public void encode(Instr value);
Expand All @@ -28,6 +30,7 @@ public interface IRWriterEncoder {
public void encode(Operand value);
public void encode(Operand[] value);
public void encode(OperandType value);
public void encode(byte[] values);
public void encode(boolean value);
public void encode(byte value);
public void encode(char value);
Expand Down
14 changes: 11 additions & 3 deletions core/src/main/java/org/jruby/ir/persistence/IRWriterFile.java
Expand Up @@ -6,6 +6,7 @@

package org.jruby.ir.persistence;

import org.jcodings.Encoding;
import org.jruby.ir.IRScope;
import org.jruby.ir.IRScopeType;
import org.jruby.ir.Operation;
Expand Down Expand Up @@ -115,12 +116,19 @@ public void encode(double value) {

@Override
public void encode(ByteList value) {
byte[] bytes = value.bytes();
encode(value.bytes());
encode(value.getEncoding());
}

@Override
public void encode(byte[] bytes) {
encode(bytes.length);
buf.put(bytes);
// FIXME: Consider writing this out differently?
encode(value.getEncoding().toString());
}

@Override
public void encode(Encoding encoding) {
encode(encoding.getName());
}

@Override
Expand Down
Expand Up @@ -42,6 +42,7 @@ public Operand decode(OperandType type) {
case DYNAMIC_SYMBOL: return new DynamicSymbol(d.decodeOperand());
case FIXNUM: return new Fixnum(d.decodeLong());
case FLOAT: return new org.jruby.ir.operands.Float(d.decodeDouble());
case FROZEN_STRING: return FrozenString.decode(d);
case GLOBAL_VARIABLE: return new GlobalVariable(d.decodeString());
case HASH: return decodeHash();
case IR_EXCEPTION: return IRException.getExceptionFromOrdinal(d.decodeByte());
Expand All @@ -55,7 +56,7 @@ public Operand decode(OperandType type) {
case SELF: return Self.SELF;
case SPLAT: return new Splat(d.decodeOperand());
case STANDARD_ERROR: return new StandardError();
case STRING_LITERAL: return new StringLiteral(d.decodeString());
case STRING_LITERAL: return StringLiteral.decode(d);
case SVALUE: return new SValue(d.decodeOperand());
// FIXME: This is broken since there is no encode/decode for encoding
case SYMBOL: return new Symbol(d.decodeString(), USASCIIEncoding.INSTANCE);
Expand Down

0 comments on commit b173aca

Please sign in to comment.