Skip to content

Commit

Permalink
more idString and make accessor methods work with all encodings.
Browse files Browse the repository at this point in the history
  • Loading branch information
enebo committed Apr 16, 2018
1 parent 63fede2 commit 61703bd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
11 changes: 4 additions & 7 deletions core/src/main/java/org/jruby/RubyModule.java
Expand Up @@ -1871,7 +1871,7 @@ public RubyModule defineModuleUnder(String name) {
}

private void addAccessor(ThreadContext context, RubySymbol identifier, Visibility visibility, boolean readable, boolean writeable) {
String internedIdentifier = identifier.toString();
String internedIdentifier = identifier.idString();

final Ruby runtime = context.runtime;

Expand All @@ -1884,17 +1884,14 @@ private void addAccessor(ThreadContext context, RubySymbol identifier, Visibilit
throw runtime.newNameError("invalid attribute name", internedIdentifier);
}

// FIXME: This only works if identifier's encoding is ASCII-compatible
final String variableName = TypeConverter.checkID(runtime, '@' + internedIdentifier).toString();
final String variableName = identifier.asInstanceVariable().idString();
if (readable) {
addMethod(internedIdentifier, new AttrReaderMethod(methodLocation, visibility, variableName));
callMethod(context, "method_added", identifier);
}
if (writeable) {
// FIXME: This only works if identifier's encoding is ASCII-compatible
identifier = TypeConverter.checkID(runtime, internedIdentifier + '=');
internedIdentifier = identifier.toString();
addMethod(internedIdentifier, new AttrWriterMethod(methodLocation, visibility, variableName));
identifier = identifier.asAccessor();
addMethod(identifier.idString(), new AttrWriterMethod(methodLocation, visibility, variableName));
callMethod(context, "method_added", identifier);
}
}
Expand Down
8 changes: 8 additions & 0 deletions core/src/main/java/org/jruby/RubySymbol.java
Expand Up @@ -180,6 +180,14 @@ public RubySymbol asAccessor() {
return newIDSymbol(getRuntime(), bytes);
}

public RubySymbol asInstanceVariable() {
ByteList bytes = getBytes().dup();

bytes.prepend((byte) '@');

return newIDSymbol(getRuntime(), bytes);
}

/**
* When we know we need an entry in the symbol table because the provided name will be needed to be
* accessed as a valid identifier later we can call this. If there is not already an entry we will
Expand Down

0 comments on commit 61703bd

Please sign in to comment.