Skip to content

Commit 5b5d945

Browse files
authored
Merge branch 'master' into expose_load_settings
2 parents a0e5247 + ccf3b07 commit 5b5d945

7 files changed

Lines changed: 107 additions & 77 deletions

File tree

ext/java/org/jruby/ext/psych/PsychEmitter.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@
3333
import org.jruby.RubyArray;
3434
import org.jruby.RubyBoolean;
3535
import org.jruby.RubyClass;
36+
import org.jruby.RubyEncoding;
3637
import org.jruby.RubyModule;
3738
import org.jruby.RubyObject;
3839
import org.jruby.RubyString;
3940
import org.jruby.anno.JRubyMethod;
4041
import org.jruby.runtime.ThreadContext;
4142
import org.jruby.runtime.builtin.IRubyObject;
43+
import org.jruby.util.ByteList;
4244
import org.jruby.util.IOOutputStream;
4345
import org.jruby.util.TypeConverter;
4446
import org.jruby.util.io.EncodingUtils;
@@ -116,17 +118,14 @@ public IRubyObject start_stream(ThreadContext context, IRubyObject encoding) {
116118

117119
initEmitter(context, encoding);
118120

119-
StreamStartEvent event = new StreamStartEvent(NULL_MARK, NULL_MARK);
120-
121-
emit(context, event);
121+
emit(context, NULL_STREAM_START_EVENT);
122122

123123
return this;
124124
}
125125

126126
@JRubyMethod
127127
public IRubyObject end_stream(ThreadContext context) {
128-
StreamEndEvent event = new StreamEndEvent(NULL_MARK, NULL_MARK);
129-
emit(context, event);
128+
emit(context, NULL_STREAM_START_EVENT);
130129
return this;
131130
}
132131

@@ -366,17 +365,18 @@ private DumpSettings buildDumpSettings() {
366365
return dumpSettingsBuilder.build();
367366
}
368367

369-
private String exportToUTF8(ThreadContext context, IRubyObject tag, RubyClass stringClass) {
370-
if (tag.isNil()) {
368+
private String exportToUTF8(ThreadContext context, IRubyObject maybeString, RubyClass stringClass) {
369+
if (maybeString.isNil()) {
371370
return null;
372371
}
373372

374-
RubyString tagStr;
373+
RubyString string;
375374

376-
TypeConverter.checkType(context, tag, stringClass);
377-
tagStr = (RubyString) tag;
375+
TypeConverter.checkType(context, maybeString, stringClass);
376+
string = (RubyString) maybeString;
377+
ByteList bytes = string.getByteList();
378378

379-
return EncodingUtils.strConvEnc(context, tagStr, tagStr.getEncoding(), UTF8Encoding.INSTANCE).asJavaString();
379+
return RubyEncoding.decodeUTF8(bytes.unsafeBytes(), bytes.begin(), bytes.realSize());
380380
}
381381

382382
Emitter emitter;
@@ -385,6 +385,7 @@ private String exportToUTF8(ThreadContext context, IRubyObject tag, RubyClass st
385385
IRubyObject io;
386386

387387
private static final Optional<Mark> NULL_MARK = Optional.empty();
388+
private static final StreamStartEvent NULL_STREAM_START_EVENT = new StreamStartEvent(NULL_MARK, NULL_MARK);
388389

389390
// Map style constants from Psych values (ANY = 0 ... FOLDED = 5)
390391
// to SnakeYaml values; see psych/nodes/scalar.rb.

ext/java/org/jruby/ext/psych/PsychParser.java

Lines changed: 70 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
import org.snakeyaml.engine.v2.events.DocumentEndEvent;
6464
import org.snakeyaml.engine.v2.events.DocumentStartEvent;
6565
import org.snakeyaml.engine.v2.events.Event;
66+
import org.snakeyaml.engine.v2.events.ImplicitTuple;
6667
import org.snakeyaml.engine.v2.events.MappingStartEvent;
6768
import org.snakeyaml.engine.v2.events.ScalarEvent;
6869
import org.snakeyaml.engine.v2.events.SequenceStartEvent;
@@ -117,7 +118,19 @@ public static void initPsychParser(Ruby runtime, RubyModule psych) {
117118
public PsychParser(Ruby runtime, RubyClass klass) {
118119
super(runtime, klass);
119120

120-
this.sites = (CachingCallSite[]) klass.getInternalVariable(JRUBY_CALL_SITES);
121+
CachingCallSite[] sites = (CachingCallSite[]) klass.getInternalVariable(JRUBY_CALL_SITES);
122+
this.path = sites[Call.path.ordinal()];
123+
this.event_location = sites[Call.event_location.ordinal()];
124+
this.start_stream = sites[Call.start_stream.ordinal()];
125+
this.start_document = sites[Call.start_document.ordinal()];
126+
this.end_document = sites[Call.end_document.ordinal()];
127+
this.alias = sites[Call.alias.ordinal()];
128+
this.scalar = sites[Call.scalar.ordinal()];
129+
this.start_sequence = sites[Call.start_sequence.ordinal()];
130+
this.end_sequence = sites[Call.end_sequence.ordinal()];
131+
this.start_mapping = sites[Call.start_mapping.ordinal()];
132+
this.end_mapping = sites[Call.end_mapping.ordinal()];
133+
this.end_stream = sites[Call.end_stream.ordinal()];
121134
this.loadSettingsBuilder = LoadSettings.builder().setSchema(new CoreSchema());
122135
}
123136

@@ -136,17 +149,26 @@ private IRubyObject stringOrNilFor(ThreadContext context, Optional<String> value
136149
private IRubyObject stringFor(ThreadContext context, String value) {
137150
Ruby runtime = context.runtime;
138151

152+
boolean isUTF8 = true;
153+
Charset charset = RubyEncoding.UTF8;
154+
139155
Encoding encoding = runtime.getDefaultInternalEncoding();
140156
if (encoding == null) {
141157
encoding = UTF8Encoding.INSTANCE;
158+
charset = RubyEncoding.UTF8;
159+
} else {
160+
Charset encodingCharset = encoding.getCharset();
161+
if (encodingCharset != null) {
162+
isUTF8 = encodingCharset == RubyEncoding.UTF8;
163+
charset = encodingCharset;
164+
}
142165
}
143166

144-
Charset charset = RubyEncoding.UTF8;
145-
if (encoding.getCharset() != null) {
146-
charset = encoding.getCharset();
147-
}
148-
149-
ByteList bytes = new ByteList(value.getBytes(charset), encoding);
167+
ByteList bytes = new ByteList(
168+
isUTF8 ?
169+
RubyEncoding.encodeUTF8(value) :
170+
RubyEncoding.encode(value, charset),
171+
encoding);
150172
RubyString string = RubyString.newString(runtime, bytes);
151173

152174
return string;
@@ -217,7 +239,7 @@ public IRubyObject parse(ThreadContext context, IRubyObject handler, IRubyObject
217239
parser = new ParserImpl(loadSettings, new ScannerImpl(loadSettings, readerFor(context, yaml, loadSettings)));
218240

219241
if (path.isNil() && yaml.respondsTo("path")) {
220-
path = sites[Call.path.ordinal()].call(context, this, yaml);
242+
path = this.path.call(context, this, yaml);
221243
}
222244

223245
while (parser.hasNext()) {
@@ -231,24 +253,24 @@ public IRubyObject parse(ThreadContext context, IRubyObject handler, IRubyObject
231253
IRubyObject end_line = runtime.newFixnum(end.getLine());
232254
IRubyObject end_column = runtime.newFixnum(end.getColumn());
233255

234-
sites[Call.event_location.ordinal()].call(context, this, handler, start_line, start_column, end_line, end_column);
256+
event_location.call(context, this, handler, start_line, start_column, end_line, end_column);
235257

236258
switch (event.getEventId()) {
237259
case StreamStart:
238-
sites[Call.start_stream.ordinal()].call(context, this, handler, runtime.newFixnum(YAML_ANY_ENCODING.ordinal()));
260+
start_stream.call(context, this, handler, runtime.newFixnum(YAML_ANY_ENCODING.ordinal()));
239261
break;
240262
case DocumentStart:
241263
handleDocumentStart(context, (DocumentStartEvent) event, handler);
242264
break;
243265
case DocumentEnd:
244266
IRubyObject notExplicit = runtime.newBoolean(!((DocumentEndEvent) event).isExplicit());
245267

246-
sites[Call.end_document.ordinal()].call(context, this, handler, notExplicit);
268+
end_document.call(context, this, handler, notExplicit);
247269
break;
248270
case Alias:
249271
IRubyObject alias = stringOrNilForAnchor(context, ((AliasEvent) event).getAnchor());
250272

251-
sites[Call.alias.ordinal()].call(context, this, handler, alias);
273+
this.alias.call(context, this, handler, alias);
252274
break;
253275
case Scalar:
254276
handleScalar(context, (ScalarEvent) event, handler);
@@ -257,16 +279,16 @@ public IRubyObject parse(ThreadContext context, IRubyObject handler, IRubyObject
257279
handleSequenceStart(context, (SequenceStartEvent) event, handler);
258280
break;
259281
case SequenceEnd:
260-
sites[Call.end_sequence.ordinal()].call(context, this, handler);
282+
end_sequence.call(context, this, handler);
261283
break;
262284
case MappingStart:
263285
handleMappingStart(context, (MappingStartEvent) event, handler);
264286
break;
265287
case MappingEnd:
266-
sites[Call.end_mapping.ordinal()].call(context, this, handler);
288+
end_mapping.call(context, this, handler);
267289
break;
268290
case StreamEnd:
269-
sites[Call.end_stream.ordinal()].call(context, this, handler);
291+
end_stream.call(context, this, handler);
270292
break;
271293
}
272294
}
@@ -313,18 +335,23 @@ private void handleDocumentStart(ThreadContext context, DocumentStartEvent dse,
313335
RubyArray.newEmptyArray(runtime);
314336

315337
Map<String, String> tagsMap = dse.getTags();
316-
RubyArray tags = RubyArray.newArray(runtime);
317-
if (tagsMap != null && tagsMap.size() > 0) {
338+
RubyArray tags;
339+
int size;
340+
if (tagsMap != null && (size = tagsMap.size()) > 0) {
341+
tags = RubyArray.newArray(runtime, size);
318342
for (Map.Entry<String, String> tag : tagsMap.entrySet()) {
319-
IRubyObject key = stringFor(context, tag.getKey());
343+
IRubyObject key = stringFor(context, tag.getKey());
320344
IRubyObject value = stringFor(context, tag.getValue());
321345

322346
tags.append(RubyArray.newArray(runtime, key, value));
323347
}
348+
} else {
349+
tags = RubyArray.newEmptyArray(runtime);
324350
}
351+
325352
IRubyObject notExplicit = runtime.newBoolean(!dse.isExplicit());
326353

327-
invoke(context, handler, "start_document", version, tags, notExplicit);
354+
start_document.call(context, this, handler, version, tags, notExplicit);
328355
}
329356

330357
private void handleMappingStart(ThreadContext context, MappingStartEvent mse, IRubyObject handler) {
@@ -334,20 +361,21 @@ private void handleMappingStart(ThreadContext context, MappingStartEvent mse, IR
334361
IRubyObject implicit = runtime.newBoolean(mse.isImplicit());
335362
IRubyObject style = runtime.newFixnum(translateFlowStyle(mse.getFlowStyle()));
336363

337-
sites[Call.start_mapping.ordinal()].call(context, this, handler, anchor, tag, implicit, style);
364+
start_mapping.call(context, this, handler, anchor, tag, implicit, style);
338365
}
339366

340367
private void handleScalar(ThreadContext context, ScalarEvent se, IRubyObject handler) {
341368
Ruby runtime = context.runtime;
342369

343370
IRubyObject anchor = stringOrNilForAnchor(context, se.getAnchor());
344371
IRubyObject tag = stringOrNilFor(context, se.getTag());
345-
IRubyObject plain_implicit = runtime.newBoolean(se.getImplicit().canOmitTagInPlainScalar());
346-
IRubyObject quoted_implicit = runtime.newBoolean(se.getImplicit().canOmitTagInNonPlainScalar());
372+
ImplicitTuple implicit = se.getImplicit();
373+
IRubyObject plain_implicit = runtime.newBoolean(implicit.canOmitTagInPlainScalar());
374+
IRubyObject quoted_implicit = runtime.newBoolean(implicit.canOmitTagInNonPlainScalar());
347375
IRubyObject style = runtime.newFixnum(translateStyle(se.getScalarStyle()));
348376
IRubyObject val = stringFor(context, se.getValue());
349377

350-
sites[Call.scalar.ordinal()].call(context, this, handler, val, anchor, tag, plain_implicit,
378+
scalar.call(context, this, handler, val, anchor, tag, plain_implicit,
351379
quoted_implicit, style);
352380
}
353381

@@ -358,38 +386,36 @@ private void handleSequenceStart(ThreadContext context, SequenceStartEvent sse,
358386
IRubyObject implicit = runtime.newBoolean(sse.isImplicit());
359387
IRubyObject style = runtime.newFixnum(translateFlowStyle(sse.getFlowStyle()));
360388

361-
sites[Call.start_sequence.ordinal()].call(context, this, handler, anchor, tag, implicit, style);
389+
start_sequence.call(context, this, handler, anchor, tag, implicit, style);
362390
}
363391

364392
private static void raiseParserException(ThreadContext context, ReaderException re, IRubyObject rbPath) {
365-
Ruby runtime;
393+
Ruby runtime = context.runtime;
366394
RubyClass se;
367395
IRubyObject exception;
368396

369-
runtime = context.runtime;
370-
se = (RubyClass)runtime.getModule("Psych").getConstant("SyntaxError");
397+
se = (RubyClass) runtime.getModule("Psych").getConstant("SyntaxError");
371398

372399
exception = se.newInstance(context,
373400
new IRubyObject[] {
374401
rbPath,
375-
runtime.newFixnum(0),
376-
runtime.newFixnum(0),
402+
RubyFixnum.zero(runtime),
403+
RubyFixnum.zero(runtime),
377404
runtime.newFixnum(re.getPosition()),
378-
(null == re.getName() ? runtime.getNil() : runtime.newString(re.getName())),
379-
(null == re.toString() ? runtime.getNil() : runtime.newString(re.toString()))
405+
(null == re.getName() ? context.nil : runtime.newString(re.getName())),
406+
(null == re.toString() ? context.nil : runtime.newString(re.toString()))
380407
},
381408
Block.NULL_BLOCK);
382409

383410
RubyKernel.raise(context, runtime.getKernel(), new IRubyObject[] { exception }, Block.NULL_BLOCK);
384411
}
385412

386413
private static void raiseParserException(ThreadContext context, MarkedYamlEngineException mye, IRubyObject rbPath) {
387-
Ruby runtime;
414+
Ruby runtime = context.runtime;
388415
Mark mark;
389416
RubyClass se;
390417
IRubyObject exception;
391418

392-
runtime = context.runtime;
393419
se = (RubyClass)runtime.getModule("Psych").getConstant("SyntaxError");
394420

395421
mark = mye.getProblemMark().get();
@@ -400,32 +426,31 @@ private static void raiseParserException(ThreadContext context, MarkedYamlEngine
400426
runtime.newFixnum(mark.getLine() + 1),
401427
runtime.newFixnum(mark.getColumn() + 1),
402428
runtime.newFixnum(mark.getIndex()),
403-
(null == mye.getProblem() ? runtime.getNil() : runtime.newString(mye.getProblem())),
404-
(null == mye.getContext() ? runtime.getNil() : runtime.newString(mye.getContext()))
429+
(null == mye.getProblem() ? context.nil : runtime.newString(mye.getProblem())),
430+
(null == mye.getContext() ? context.nil : runtime.newString(mye.getContext()))
405431
},
406432
Block.NULL_BLOCK);
407433

408434
RubyKernel.raise(context, runtime.getKernel(), new IRubyObject[] { exception }, Block.NULL_BLOCK);
409435
}
410436

411437
private static void raiseParserException(ThreadContext context, MalformedInputException mie, IRubyObject rbPath) {
412-
Ruby runtime;;
438+
Ruby runtime = context.runtime;
413439
RubyClass se;
414440
IRubyObject exception;
415441

416-
runtime = context.runtime;
417442
se = (RubyClass)runtime.getModule("Psych").getConstant("SyntaxError");
418443

419444
mie.getInputLength();
420445

421446
exception = se.newInstance(context,
422447
arrayOf(
423448
rbPath,
424-
runtime.newFixnum(-1),
425-
runtime.newFixnum(-1),
449+
RubyFixnum.minus_one(runtime),
450+
RubyFixnum.minus_one(runtime),
426451
runtime.newFixnum(mie.getInputLength()),
427-
runtime.getNil(),
428-
runtime.getNil()
452+
context.nil,
453+
context.nil
429454
),
430455
Block.NULL_BLOCK);
431456

@@ -460,6 +485,7 @@ public IRubyObject mark(ThreadContext context) {
460485

461486
Event event = null;
462487

488+
Parser parser = this.parser;
463489
if (parser != null) {
464490
if (parser.hasNext()) {
465491
event = parser.peekEvent();
@@ -469,7 +495,7 @@ public IRubyObject mark(ThreadContext context) {
469495
}
470496

471497
if (event == null) {
472-
return ((RubyClass)context.runtime.getClassFromPath("Psych::Parser::Mark")).newInstance(
498+
return ((RubyClass) runtime.getClassFromPath("Psych::Parser::Mark")).newInstance(
473499
context,
474500
RubyFixnum.zero(runtime),
475501
RubyFixnum.zero(runtime),
@@ -480,7 +506,7 @@ public IRubyObject mark(ThreadContext context) {
480506

481507
Mark mark = event.getStartMark().orElseThrow(RuntimeException::new);
482508

483-
return ((RubyClass)context.runtime.getClassFromPath("Psych::Parser::Mark")).newInstance(
509+
return ((RubyClass) runtime.getClassFromPath("Psych::Parser::Mark")).newInstance(
484510
context,
485511
RubyFixnum.zero(runtime),
486512
runtime.newFixnum(mark.getLine()),
@@ -549,5 +575,5 @@ private enum Call {
549575
path, event_location, start_stream, start_document, end_document, alias, scalar, start_sequence, end_sequence, start_mapping, end_mapping, end_stream
550576
}
551577

552-
final CachingCallSite[] sites;
578+
private final CachingCallSite path, event_location, start_stream, start_document, end_document, alias, scalar, start_sequence, end_sequence, start_mapping, end_mapping, end_stream;
553579
}

ext/psych/extconf.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
args = [
2323
yaml_configure,
2424
"--enable-#{shared ? 'shared' : 'static'}",
25-
"--host=#{RbConfig::CONFIG['host'].sub(/-unknown-/, '-')}",
25+
"--host=#{RbConfig::CONFIG['host'].sub(/-unknown-/, '-').sub(/arm64/, 'arm')}",
2626
"CC=#{RbConfig::CONFIG['CC']}",
2727
*(["CFLAGS=-w"] if RbConfig::CONFIG["GCC"] == "yes"),
2828
]

lib/psych/versions.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module Psych
44
# The version of Psych you are using
5-
VERSION = '5.0.1'
5+
VERSION = '5.0.2'
66

77
if RUBY_ENGINE == 'jruby'
88
DEFAULT_SNAKEYAML_VERSION = '2.6'.freeze

lib/psych/visitors/yaml_tree.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -568,15 +568,15 @@ def accept target
568568
raise BadAlias, "Tried to dump an aliased object"
569569
end
570570

571-
unless @permitted_classes[target.class]
571+
unless Symbol === target || @permitted_classes[target.class]
572572
raise DisallowedClass.new('dump', target.class.name || target.class.inspect)
573573
end
574574

575575
super
576576
end
577577

578578
def visit_Symbol sym
579-
unless @permitted_symbols[sym]
579+
unless @permitted_classes[Symbol] || @permitted_symbols[sym]
580580
raise DisallowedClass.new('dump', "Symbol(#{sym.inspect})")
581581
end
582582

0 commit comments

Comments
 (0)