@@ -103,11 +103,7 @@ public class PsychParser extends RubyObject {
103
103
public static void initPsychParser (Ruby runtime , RubyModule psych ) {
104
104
RubyClass psychParser = runtime .defineClassUnder ("Parser" , runtime .getObject (), PsychParser ::new , psych );
105
105
106
- CachingCallSite [] sites =
107
- Arrays .stream (Call .values ())
108
- .map ((call ) -> new FunctionalCachingCallSite (call .name ()))
109
- .toArray (CachingCallSite []::new );
110
- psychParser .setInternalVariable (JRUBY_CALL_SITES , sites );
106
+ psychParser .setInternalVariable (JRUBY_CALL_SITES , new CallSites ());
111
107
112
108
runtime .getLoadService ().require ("psych/syntax_error" );
113
109
psychParser .defineConstant ("ANY" , runtime .newFixnum (YAML_ANY_ENCODING .ordinal ()));
@@ -128,19 +124,7 @@ public static void initPsychParser(Ruby runtime, RubyModule psych) {
128
124
public PsychParser (Ruby runtime , RubyClass klass ) {
129
125
super (runtime , klass );
130
126
131
- CachingCallSite [] sites = (CachingCallSite []) klass .getInternalVariable (JRUBY_CALL_SITES );
132
- this .path = sites [Call .path .ordinal ()];
133
- this .event_location = sites [Call .event_location .ordinal ()];
134
- this .start_stream = sites [Call .start_stream .ordinal ()];
135
- this .start_document = sites [Call .start_document .ordinal ()];
136
- this .end_document = sites [Call .end_document .ordinal ()];
137
- this .alias = sites [Call .alias .ordinal ()];
138
- this .scalar = sites [Call .scalar .ordinal ()];
139
- this .start_sequence = sites [Call .start_sequence .ordinal ()];
140
- this .end_sequence = sites [Call .end_sequence .ordinal ()];
141
- this .start_mapping = sites [Call .start_mapping .ordinal ()];
142
- this .end_mapping = sites [Call .end_mapping .ordinal ()];
143
- this .end_stream = sites [Call .end_stream .ordinal ()];
127
+ this .sites = (CallSites ) klass .getInternalVariable (JRUBY_CALL_SITES );
144
128
145
129
// prepare settings builder and apply global defaults
146
130
LoadSettingsBuilder lsb = LoadSettings .builder ();
@@ -257,7 +241,7 @@ public IRubyObject parse(ThreadContext context, IRubyObject handler, IRubyObject
257
241
parser = new ParserImpl (loadSettings , new ScannerImpl (loadSettings , readerFor (context , yaml , loadSettings )));
258
242
259
243
if (path .isNil () && yaml .respondsTo ("path" )) {
260
- path = this .path .call (context , this , yaml );
244
+ path = sites .path .call (context , this , yaml );
261
245
}
262
246
263
247
while (parser .hasNext ()) {
@@ -271,24 +255,24 @@ public IRubyObject parse(ThreadContext context, IRubyObject handler, IRubyObject
271
255
IRubyObject end_line = runtime .newFixnum (end .getLine ());
272
256
IRubyObject end_column = runtime .newFixnum (end .getColumn ());
273
257
274
- event_location .call (context , this , handler , start_line , start_column , end_line , end_column );
258
+ sites . event_location .call (context , this , handler , start_line , start_column , end_line , end_column );
275
259
276
260
switch (event .getEventId ()) {
277
261
case StreamStart :
278
- start_stream .call (context , this , handler , runtime .newFixnum (YAML_ANY_ENCODING .ordinal ()));
262
+ sites . start_stream .call (context , this , handler , runtime .newFixnum (YAML_ANY_ENCODING .ordinal ()));
279
263
break ;
280
264
case DocumentStart :
281
265
handleDocumentStart (context , (DocumentStartEvent ) event , handler );
282
266
break ;
283
267
case DocumentEnd :
284
268
IRubyObject notExplicit = runtime .newBoolean (!((DocumentEndEvent ) event ).isExplicit ());
285
269
286
- end_document .call (context , this , handler , notExplicit );
270
+ sites . end_document .call (context , this , handler , notExplicit );
287
271
break ;
288
272
case Alias :
289
273
IRubyObject alias = stringOrNilForAnchor (context , ((AliasEvent ) event ).getAnchor ());
290
274
291
- this .alias .call (context , this , handler , alias );
275
+ sites .alias .call (context , this , handler , alias );
292
276
break ;
293
277
case Scalar :
294
278
handleScalar (context , (ScalarEvent ) event , handler );
@@ -297,16 +281,16 @@ public IRubyObject parse(ThreadContext context, IRubyObject handler, IRubyObject
297
281
handleSequenceStart (context , (SequenceStartEvent ) event , handler );
298
282
break ;
299
283
case SequenceEnd :
300
- end_sequence .call (context , this , handler );
284
+ sites . end_sequence .call (context , this , handler );
301
285
break ;
302
286
case MappingStart :
303
287
handleMappingStart (context , (MappingStartEvent ) event , handler );
304
288
break ;
305
289
case MappingEnd :
306
- end_mapping .call (context , this , handler );
290
+ sites . end_mapping .call (context , this , handler );
307
291
break ;
308
292
case StreamEnd :
309
- end_stream .call (context , this , handler );
293
+ sites . end_stream .call (context , this , handler );
310
294
break ;
311
295
}
312
296
}
@@ -369,7 +353,7 @@ private void handleDocumentStart(ThreadContext context, DocumentStartEvent dse,
369
353
370
354
IRubyObject notExplicit = runtime .newBoolean (!dse .isExplicit ());
371
355
372
- start_document .call (context , this , handler , version , tags , notExplicit );
356
+ sites . start_document .call (context , this , handler , version , tags , notExplicit );
373
357
}
374
358
375
359
private void handleMappingStart (ThreadContext context , MappingStartEvent mse , IRubyObject handler ) {
@@ -379,7 +363,7 @@ private void handleMappingStart(ThreadContext context, MappingStartEvent mse, IR
379
363
IRubyObject implicit = runtime .newBoolean (mse .isImplicit ());
380
364
IRubyObject style = runtime .newFixnum (translateFlowStyle (mse .getFlowStyle ()));
381
365
382
- start_mapping .call (context , this , handler , anchor , tag , implicit , style );
366
+ sites . start_mapping .call (context , this , handler , anchor , tag , implicit , style );
383
367
}
384
368
385
369
private void handleScalar (ThreadContext context , ScalarEvent se , IRubyObject handler ) {
@@ -393,7 +377,7 @@ private void handleScalar(ThreadContext context, ScalarEvent se, IRubyObject han
393
377
IRubyObject style = runtime .newFixnum (translateStyle (se .getScalarStyle ()));
394
378
IRubyObject val = stringFor (context , se .getValue ());
395
379
396
- scalar .call (context , this , handler , val , anchor , tag , plain_implicit ,
380
+ sites . scalar .call (context , this , handler , val , anchor , tag , plain_implicit ,
397
381
quoted_implicit , style );
398
382
}
399
383
@@ -404,7 +388,7 @@ private void handleSequenceStart(ThreadContext context, SequenceStartEvent sse,
404
388
IRubyObject implicit = runtime .newBoolean (sse .isImplicit ());
405
389
IRubyObject style = runtime .newFixnum (translateFlowStyle (sse .getFlowStyle ()));
406
390
407
- start_sequence .call (context , this , handler , anchor , tag , implicit , style );
391
+ sites . start_sequence .call (context , this , handler , anchor , tag , implicit , style );
408
392
}
409
393
410
394
private static void raiseParserException (ThreadContext context , ReaderException re , IRubyObject rbPath ) {
@@ -656,10 +640,21 @@ private LoadSettings buildSettings() {
656
640
private Parser parser ;
657
641
private Event event ;
658
642
private final LoadSettingsBuilder loadSettingsBuilder ;
659
-
660
- private enum Call {
661
- path , event_location , start_stream , start_document , end_document , alias , scalar , start_sequence , end_sequence , start_mapping , end_mapping , end_stream
643
+ private final CallSites sites ;
644
+
645
+ private static class CallSites {
646
+ private final CachingCallSite path = new FunctionalCachingCallSite ("path" );
647
+ private final CachingCallSite event_location = new FunctionalCachingCallSite ("event_location" );
648
+ private final CachingCallSite start_stream = new FunctionalCachingCallSite ("start_stream" );
649
+ private final CachingCallSite start_document = new FunctionalCachingCallSite ("start_document" );
650
+ private final CachingCallSite end_document = new FunctionalCachingCallSite ("end_document" );
651
+ private final CachingCallSite alias = new FunctionalCachingCallSite ("alias" );
652
+ private final CachingCallSite scalar = new FunctionalCachingCallSite ("scalar" );
653
+ private final CachingCallSite start_sequence = new FunctionalCachingCallSite ("start_sequence" );
654
+ private final CachingCallSite end_sequence = new FunctionalCachingCallSite ("end_sequence" );
655
+ private final CachingCallSite start_mapping = new FunctionalCachingCallSite ("start_mapping" );
656
+ private final CachingCallSite end_mapping = new FunctionalCachingCallSite ("end_mapping" );
657
+ private final CachingCallSite end_stream = new FunctionalCachingCallSite ("end_stream" );
662
658
}
663
659
664
- private final CachingCallSite path , event_location , start_stream , start_document , end_document , alias , scalar , start_sequence , end_sequence , start_mapping , end_mapping , end_stream ;
665
660
}
0 commit comments