6363import org .snakeyaml .engine .v2 .events .DocumentEndEvent ;
6464import org .snakeyaml .engine .v2 .events .DocumentStartEvent ;
6565import org .snakeyaml .engine .v2 .events .Event ;
66+ import org .snakeyaml .engine .v2 .events .ImplicitTuple ;
6667import org .snakeyaml .engine .v2 .events .MappingStartEvent ;
6768import org .snakeyaml .engine .v2 .events .ScalarEvent ;
6869import 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}
0 commit comments