Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
P
protobuf
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
protobuf
Commits
d07a9963
Commit
d07a9963
authored
Jul 25, 2016
by
Josh Haberman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Ruby: fixed string freezing for JRuby.
parent
ff7f68ae
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
30 additions
and
25 deletions
+30
-25
RubyMap.java
ruby/src/main/java/com/google/protobuf/jruby/RubyMap.java
+2
-2
RubyMessage.java
.../src/main/java/com/google/protobuf/jruby/RubyMessage.java
+2
-2
RubyRepeatedField.java
...ain/java/com/google/protobuf/jruby/RubyRepeatedField.java
+2
-2
Utils.java
ruby/src/main/java/com/google/protobuf/jruby/Utils.java
+20
-17
basic.rb
ruby/tests/basic.rb
+4
-2
No files found.
ruby/src/main/java/com/google/protobuf/jruby/RubyMap.java
View file @
d07a9963
...
...
@@ -148,8 +148,8 @@ public class RubyMap extends RubyObject {
*/
@JRubyMethod
(
name
=
"[]="
)
public
IRubyObject
indexSet
(
ThreadContext
context
,
IRubyObject
key
,
IRubyObject
value
)
{
Utils
.
checkType
(
context
,
keyType
,
key
,
(
RubyModule
)
valueTypeClass
);
Utils
.
checkType
(
context
,
valueType
,
value
,
(
RubyModule
)
valueTypeClass
);
key
=
Utils
.
checkType
(
context
,
keyType
,
key
,
(
RubyModule
)
valueTypeClass
);
value
=
Utils
.
checkType
(
context
,
valueType
,
value
,
(
RubyModule
)
valueTypeClass
);
IRubyObject
symbol
;
if
(
valueType
==
Descriptors
.
FieldDescriptor
.
Type
.
ENUM
&&
Utils
.
isRubyNum
(
value
)
&&
...
...
ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java
View file @
d07a9963
...
...
@@ -504,7 +504,7 @@ public class RubyMessage extends RubyObject {
break
;
case
BYTES:
case
STRING:
Utils
.
validateStringEncoding
(
context
.
runtime
,
fieldDescriptor
.
getType
(),
value
);
Utils
.
validateStringEncoding
(
context
,
fieldDescriptor
.
getType
(),
value
);
RubyString
str
=
(
RubyString
)
value
;
switch
(
fieldDescriptor
.
getType
())
{
case
BYTES:
...
...
@@ -695,7 +695,7 @@ public class RubyMessage extends RubyObject {
}
}
if
(
addValue
)
{
Utils
.
checkType
(
context
,
fieldType
,
value
,
(
RubyModule
)
typeClass
);
value
=
Utils
.
checkType
(
context
,
fieldType
,
value
,
(
RubyModule
)
typeClass
);
this
.
fields
.
put
(
fieldDescriptor
,
value
);
}
else
{
this
.
fields
.
remove
(
fieldDescriptor
);
...
...
ruby/src/main/java/com/google/protobuf/jruby/RubyRepeatedField.java
View file @
d07a9963
...
...
@@ -110,7 +110,7 @@ public class RubyRepeatedField extends RubyObject {
@JRubyMethod
(
name
=
"[]="
)
public
IRubyObject
indexSet
(
ThreadContext
context
,
IRubyObject
index
,
IRubyObject
value
)
{
int
arrIndex
=
normalizeArrayIndex
(
index
);
Utils
.
checkType
(
context
,
fieldType
,
value
,
(
RubyModule
)
typeClass
);
value
=
Utils
.
checkType
(
context
,
fieldType
,
value
,
(
RubyModule
)
typeClass
);
IRubyObject
defaultValue
=
defaultValue
(
context
);
for
(
int
i
=
this
.
storage
.
size
();
i
<
arrIndex
;
i
++)
{
this
.
storage
.
set
(
i
,
defaultValue
);
...
...
@@ -166,7 +166,7 @@ public class RubyRepeatedField extends RubyObject {
public
IRubyObject
push
(
ThreadContext
context
,
IRubyObject
value
)
{
if
(!(
fieldType
==
Descriptors
.
FieldDescriptor
.
Type
.
MESSAGE
&&
value
==
context
.
runtime
.
getNil
()))
{
Utils
.
checkType
(
context
,
fieldType
,
value
,
(
RubyModule
)
typeClass
);
value
=
Utils
.
checkType
(
context
,
fieldType
,
value
,
(
RubyModule
)
typeClass
);
}
this
.
storage
.
add
(
value
);
return
this
.
storage
;
...
...
ruby/src/main/java/com/google/protobuf/jruby/Utils.java
View file @
d07a9963
...
...
@@ -64,8 +64,8 @@ public class Utils {
return
context
.
runtime
.
newSymbol
(
typeName
.
replace
(
"TYPE_"
,
""
).
toLowerCase
());
}
public
static
void
checkType
(
ThreadContext
context
,
Descriptors
.
FieldDescriptor
.
Type
fieldType
,
IRubyObject
value
,
RubyModule
typeClass
)
{
public
static
IRubyObject
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:
val
idateStringEncoding
(
context
.
runtime
,
fieldType
,
value
);
val
ue
=
validateStringEncoding
(
context
,
fieldType
,
value
);
break
;
case
MESSAGE:
if
(
value
.
getMetaClass
()
!=
typeClass
)
{
...
...
@@ -127,6 +127,7 @@ public class Utils {
default
:
break
;
}
return
value
;
}
public
static
IRubyObject
wrapPrimaryValue
(
ThreadContext
context
,
Descriptors
.
FieldDescriptor
.
Type
fieldType
,
Object
value
)
{
...
...
@@ -148,10 +149,16 @@ public class Utils {
return
runtime
.
newFloat
((
Double
)
value
);
case
BOOL:
return
(
Boolean
)
value
?
runtime
.
getTrue
()
:
runtime
.
getFalse
();
case
BYTES:
return
runtime
.
newString
(((
ByteString
)
value
).
toStringUtf8
());
case
STRING:
return
runtime
.
newString
(
value
.
toString
());
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
;
}
default
:
return
runtime
.
getNil
();
}
...
...
@@ -180,25 +187,21 @@ public class Utils {
}
}
public
static
void
validateStringEncoding
(
Ruby
runtime
,
Descriptors
.
FieldDescriptor
.
Type
type
,
IRubyObject
value
)
{
public
static
IRubyObject
validateStringEncoding
(
ThreadContext
context
,
Descriptors
.
FieldDescriptor
.
Type
type
,
IRubyObject
value
)
{
if
(!(
value
instanceof
RubyString
))
throw
runtime
.
newTypeError
(
"Invalid argument for string field."
);
Encoding
encoding
=
((
RubyString
)
value
).
getEncoding
();
throw
context
.
runtime
.
newTypeError
(
"Invalid argument for string field."
);
switch
(
type
)
{
case
BYTES:
if
(
encoding
!=
ASCIIEncoding
.
INSTANCE
)
throw
runtime
.
newTypeError
(
"Encoding for bytes fields"
+
" must be \"ASCII-8BIT\", but was "
+
encoding
);
value
=
((
RubyString
)
value
).
encode
(
context
,
context
.
runtime
.
evalScriptlet
(
"Encoding::ASCII_8BIT"
));
break
;
case
STRING:
if
(
encoding
!=
UTF8Encoding
.
INSTANCE
&&
encoding
!=
USASCIIEncoding
.
INSTANCE
)
throw
runtime
.
newTypeError
(
"Encoding for string fields"
+
" must be \"UTF-8\" or \"ASCII\", but was "
+
encoding
);
value
=
((
RubyString
)
value
).
encode
(
context
,
context
.
runtime
.
evalScriptlet
(
"Encoding::UTF_8"
));
break
;
default
:
break
;
}
value
.
setFrozen
(
true
);
return
value
;
}
public
static
void
checkNameAvailability
(
ThreadContext
context
,
String
name
)
{
...
...
ruby/tests/basic.rb
View file @
d07a9963
...
...
@@ -861,8 +861,10 @@ module BasicTest
m2
=
TestMessage
.
decode_json
(
json
)
assert_equal
'foo'
,
m2
.
optional_string
assert_equal
[
'bar1'
,
'bar2'
],
m2
.
repeated_string
assert
m2
.
optional_string
.
frozen?
assert
m2
.
repeated_string
[
0
].
frozen?
if
RUBY_PLATFORM
!=
"java"
assert
m2
.
optional_string
.
frozen?
assert
m2
.
repeated_string
[
0
].
frozen?
end
proto
=
m
.
to_proto
m2
=
TestMessage
.
decode
(
proto
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment