6262import org .snakeyaml .engine .v2 .events .DocumentEndEvent ;
6363import org .snakeyaml .engine .v2 .events .DocumentStartEvent ;
6464import org .snakeyaml .engine .v2 .events .Event ;
65+ import org .snakeyaml .engine .v2 .events .ImplicitTuple ;
6566import org .snakeyaml .engine .v2 .events .MappingStartEvent ;
6667import org .snakeyaml .engine .v2 .events .ScalarEvent ;
6768import org .snakeyaml .engine .v2 .events .SequenceStartEvent ;
@@ -147,17 +148,26 @@ private IRubyObject stringOrNilFor(ThreadContext context, Optional<String> value
147148 private IRubyObject stringFor (ThreadContext context , String value ) {
148149 Ruby runtime = context .runtime ;
149150
151+ boolean isUTF8 = true ;
152+ Charset charset = RubyEncoding .UTF8 ;
153+
150154 Encoding encoding = runtime .getDefaultInternalEncoding ();
151155 if (encoding == null ) {
152156 encoding = UTF8Encoding .INSTANCE ;
157+ charset = RubyEncoding .UTF8 ;
158+ } else {
159+ Charset encodingCharset = encoding .getCharset ();
160+ if (encodingCharset != null ) {
161+ isUTF8 = encodingCharset == RubyEncoding .UTF8 ;
162+ charset = encodingCharset ;
163+ }
153164 }
154165
155- Charset charset = RubyEncoding .UTF8 ;
156- if (encoding .getCharset () != null ) {
157- charset = encoding .getCharset ();
158- }
159-
160- ByteList bytes = new ByteList (value .getBytes (charset ), encoding );
166+ ByteList bytes = new ByteList (
167+ isUTF8 ?
168+ RubyEncoding .encodeUTF8 (value ) :
169+ RubyEncoding .encode (value , charset ),
170+ encoding );
161171 RubyString string = RubyString .newString (runtime , bytes );
162172
163173 return string ;
@@ -324,18 +334,23 @@ private void handleDocumentStart(ThreadContext context, DocumentStartEvent dse,
324334 RubyArray .newEmptyArray (runtime );
325335
326336 Map <String , String > tagsMap = dse .getTags ();
327- RubyArray tags = RubyArray .newArray (runtime );
328- if (tagsMap != null && tagsMap .size () > 0 ) {
337+ RubyArray tags ;
338+ int size ;
339+ if (tagsMap != null && (size = tagsMap .size ()) > 0 ) {
340+ tags = RubyArray .newArray (runtime , size );
329341 for (Map .Entry <String , String > tag : tagsMap .entrySet ()) {
330- IRubyObject key = stringFor (context , tag .getKey ());
342+ IRubyObject key = stringFor (context , tag .getKey ());
331343 IRubyObject value = stringFor (context , tag .getValue ());
332344
333345 tags .append (RubyArray .newArray (runtime , key , value ));
334346 }
347+ } else {
348+ tags = RubyArray .newEmptyArray (runtime );
335349 }
350+
336351 IRubyObject notExplicit = runtime .newBoolean (!dse .isExplicit ());
337352
338- invoke (context , handler , "start_document" , version , tags , notExplicit );
353+ start_document . call (context , this , handler , version , tags , notExplicit );
339354 }
340355
341356 private void handleMappingStart (ThreadContext context , MappingStartEvent mse , IRubyObject handler ) {
@@ -353,8 +368,9 @@ private void handleScalar(ThreadContext context, ScalarEvent se, IRubyObject han
353368
354369 IRubyObject anchor = stringOrNilForAnchor (context , se .getAnchor ());
355370 IRubyObject tag = stringOrNilFor (context , se .getTag ());
356- IRubyObject plain_implicit = runtime .newBoolean (se .getImplicit ().canOmitTagInPlainScalar ());
357- IRubyObject quoted_implicit = runtime .newBoolean (se .getImplicit ().canOmitTagInNonPlainScalar ());
371+ ImplicitTuple implicit = se .getImplicit ();
372+ IRubyObject plain_implicit = runtime .newBoolean (implicit .canOmitTagInPlainScalar ());
373+ IRubyObject quoted_implicit = runtime .newBoolean (implicit .canOmitTagInNonPlainScalar ());
358374 IRubyObject style = runtime .newFixnum (translateStyle (se .getScalarStyle ()));
359375 IRubyObject val = stringFor (context , se .getValue ());
360376
@@ -373,34 +389,32 @@ private void handleSequenceStart(ThreadContext context, SequenceStartEvent sse,
373389 }
374390
375391 private static void raiseParserException (ThreadContext context , ReaderException re , IRubyObject rbPath ) {
376- Ruby runtime ;
392+ Ruby runtime = context . runtime ;
377393 RubyClass se ;
378394 IRubyObject exception ;
379395
380- runtime = context .runtime ;
381- se = (RubyClass )runtime .getModule ("Psych" ).getConstant ("SyntaxError" );
396+ se = (RubyClass ) runtime .getModule ("Psych" ).getConstant ("SyntaxError" );
382397
383398 exception = se .newInstance (context ,
384399 new IRubyObject [] {
385400 rbPath ,
386- runtime . newFixnum ( 0 ),
387- runtime . newFixnum ( 0 ),
401+ RubyFixnum . zero ( runtime ),
402+ RubyFixnum . zero ( runtime ),
388403 runtime .newFixnum (re .getPosition ()),
389- (null == re .getName () ? runtime . getNil () : runtime .newString (re .getName ())),
390- (null == re .toString () ? runtime . getNil () : runtime .newString (re .toString ()))
404+ (null == re .getName () ? context . nil : runtime .newString (re .getName ())),
405+ (null == re .toString () ? context . nil : runtime .newString (re .toString ()))
391406 },
392407 Block .NULL_BLOCK );
393408
394409 RubyKernel .raise (context , runtime .getKernel (), new IRubyObject [] { exception }, Block .NULL_BLOCK );
395410 }
396411
397412 private static void raiseParserException (ThreadContext context , MarkedYamlEngineException mye , IRubyObject rbPath ) {
398- Ruby runtime ;
413+ Ruby runtime = context . runtime ;
399414 Mark mark ;
400415 RubyClass se ;
401416 IRubyObject exception ;
402417
403- runtime = context .runtime ;
404418 se = (RubyClass )runtime .getModule ("Psych" ).getConstant ("SyntaxError" );
405419
406420 mark = mye .getProblemMark ().get ();
@@ -411,32 +425,31 @@ private static void raiseParserException(ThreadContext context, MarkedYamlEngine
411425 runtime .newFixnum (mark .getLine () + 1 ),
412426 runtime .newFixnum (mark .getColumn () + 1 ),
413427 runtime .newFixnum (mark .getIndex ()),
414- (null == mye .getProblem () ? runtime . getNil () : runtime .newString (mye .getProblem ())),
415- (null == mye .getContext () ? runtime . getNil () : runtime .newString (mye .getContext ()))
428+ (null == mye .getProblem () ? context . nil : runtime .newString (mye .getProblem ())),
429+ (null == mye .getContext () ? context . nil : runtime .newString (mye .getContext ()))
416430 },
417431 Block .NULL_BLOCK );
418432
419433 RubyKernel .raise (context , runtime .getKernel (), new IRubyObject [] { exception }, Block .NULL_BLOCK );
420434 }
421435
422436 private static void raiseParserException (ThreadContext context , MalformedInputException mie , IRubyObject rbPath ) {
423- Ruby runtime ; ;
437+ Ruby runtime = context . runtime ;
424438 RubyClass se ;
425439 IRubyObject exception ;
426440
427- runtime = context .runtime ;
428441 se = (RubyClass )runtime .getModule ("Psych" ).getConstant ("SyntaxError" );
429442
430443 mie .getInputLength ();
431444
432445 exception = se .newInstance (context ,
433446 arrayOf (
434447 rbPath ,
435- runtime . newFixnum (- 1 ),
436- runtime . newFixnum (- 1 ),
448+ RubyFixnum . minus_one ( runtime ),
449+ RubyFixnum . minus_one ( runtime ),
437450 runtime .newFixnum (mie .getInputLength ()),
438- runtime . getNil () ,
439- runtime . getNil ()
451+ context . nil ,
452+ context . nil
440453 ),
441454 Block .NULL_BLOCK );
442455
@@ -471,6 +484,7 @@ public IRubyObject mark(ThreadContext context) {
471484
472485 Event event = null ;
473486
487+ Parser parser = this .parser ;
474488 if (parser != null ) {
475489 if (parser .hasNext ()) {
476490 event = parser .peekEvent ();
@@ -480,7 +494,7 @@ public IRubyObject mark(ThreadContext context) {
480494 }
481495
482496 if (event == null ) {
483- return ((RubyClass )context . runtime .getClassFromPath ("Psych::Parser::Mark" )).newInstance (
497+ return ((RubyClass ) runtime .getClassFromPath ("Psych::Parser::Mark" )).newInstance (
484498 context ,
485499 RubyFixnum .zero (runtime ),
486500 RubyFixnum .zero (runtime ),
@@ -491,7 +505,7 @@ public IRubyObject mark(ThreadContext context) {
491505
492506 Mark mark = event .getStartMark ().orElseThrow (RuntimeException ::new );
493507
494- return ((RubyClass )context . runtime .getClassFromPath ("Psych::Parser::Mark" )).newInstance (
508+ return ((RubyClass ) runtime .getClassFromPath ("Psych::Parser::Mark" )).newInstance (
495509 context ,
496510 RubyFixnum .zero (runtime ),
497511 runtime .newFixnum (mark .getLine ()),
0 commit comments