Index: third_party/protobuf/ruby/src/main/java/com/google/protobuf/jruby/Utils.java |
diff --git a/third_party/protobuf/ruby/src/main/java/com/google/protobuf/jruby/Utils.java b/third_party/protobuf/ruby/src/main/java/com/google/protobuf/jruby/Utils.java |
index f199feb962ec8761ec7f6fc42e56e1fe8ec580cd..596a0979920215b03ea7bcc6fd48d4b974b0f770 100644 |
--- a/third_party/protobuf/ruby/src/main/java/com/google/protobuf/jruby/Utils.java |
+++ b/third_party/protobuf/ruby/src/main/java/com/google/protobuf/jruby/Utils.java |
@@ -64,8 +64,8 @@ public class Utils { |
return context.runtime.newSymbol(typeName.replace("TYPE_", "").toLowerCase()); |
} |
- public static IRubyObject checkType(ThreadContext context, Descriptors.FieldDescriptor.Type fieldType, |
- IRubyObject value, RubyModule typeClass) { |
+ public static void checkType(ThreadContext context, Descriptors.FieldDescriptor.Type fieldType, |
+ IRubyObject value, RubyModule typeClass) { |
Ruby runtime = context.runtime; |
Object val; |
switch(fieldType) { |
@@ -106,7 +106,7 @@ public class Utils { |
break; |
case BYTES: |
case STRING: |
- value = validateStringEncoding(context, fieldType, value); |
+ validateStringEncoding(context.runtime, fieldType, value); |
break; |
case MESSAGE: |
if (value.getMetaClass() != typeClass) { |
@@ -127,7 +127,6 @@ public class Utils { |
default: |
break; |
} |
- return value; |
} |
public static IRubyObject wrapPrimaryValue(ThreadContext context, Descriptors.FieldDescriptor.Type fieldType, Object value) { |
@@ -149,16 +148,10 @@ public class Utils { |
return runtime.newFloat((Double) value); |
case BOOL: |
return (Boolean) value ? runtime.getTrue() : runtime.getFalse(); |
- case BYTES: { |
- IRubyObject wrapped = runtime.newString(((ByteString) value).toStringUtf8()); |
- wrapped.setFrozen(true); |
- return wrapped; |
- } |
- case STRING: { |
- IRubyObject wrapped = runtime.newString(value.toString()); |
- wrapped.setFrozen(true); |
- return wrapped; |
- } |
+ case BYTES: |
+ return runtime.newString(((ByteString) value).toStringUtf8()); |
+ case STRING: |
+ return runtime.newString(value.toString()); |
default: |
return runtime.getNil(); |
} |
@@ -187,21 +180,25 @@ public class Utils { |
} |
} |
- public static IRubyObject validateStringEncoding(ThreadContext context, Descriptors.FieldDescriptor.Type type, IRubyObject value) { |
+ public static void validateStringEncoding(Ruby runtime, Descriptors.FieldDescriptor.Type type, IRubyObject value) { |
if (!(value instanceof RubyString)) |
- throw context.runtime.newTypeError("Invalid argument for string field."); |
+ throw runtime.newTypeError("Invalid argument for string field."); |
+ Encoding encoding = ((RubyString) value).getEncoding(); |
switch(type) { |
case BYTES: |
- value = ((RubyString)value).encode(context, context.runtime.evalScriptlet("Encoding::ASCII_8BIT")); |
+ if (encoding != ASCIIEncoding.INSTANCE) |
+ throw runtime.newTypeError("Encoding for bytes fields" + |
+ " must be \"ASCII-8BIT\", but was " + encoding); |
break; |
case STRING: |
- value = ((RubyString)value).encode(context, context.runtime.evalScriptlet("Encoding::UTF_8")); |
+ if (encoding != UTF8Encoding.INSTANCE |
+ && encoding != USASCIIEncoding.INSTANCE) |
+ throw runtime.newTypeError("Encoding for string fields" + |
+ " must be \"UTF-8\" or \"ASCII\", but was " + encoding); |
break; |
default: |
break; |
} |
- value.setFrozen(true); |
- return value; |
} |
public static void checkNameAvailability(ThreadContext context, String name) { |