4141package org .jruby ;
4242
4343import org .jcodings .specific .UTF8Encoding ;
44- import org .jruby .anno .FrameField ;
4544import org .jruby .anno .TypePopulator ;
4645import org .jruby .api .Create ;
4746import org .jruby .api .Define ;
6867import org .jruby .parser .ParserManager ;
6968import org .jruby .parser .StaticScope ;
7069import org .jruby .runtime .JavaSites ;
71- import org .jruby .runtime .MethodIndex ;
7270import org .jruby .runtime .TraceEventManager ;
7371import org .jruby .runtime .invokedynamic .InvokeDynamicSupport ;
7472import org .jruby .specialized .RubyObjectSpecializer ;
9694import org .jruby .ast .executable .RuntimeCache ;
9795import org .jruby .ast .executable .Script ;
9896import org .jruby .ast .executable .ScriptAndCode ;
99- import org .jruby .common .IRubyWarnings .ID ;
10097import org .jruby .common .RubyWarnings ;
10198import org .jruby .compiler .JITCompiler ;
10299import org .jruby .embed .Extension ;
112109import org .jruby .internal .runtime .ThreadService ;
113110import org .jruby .internal .runtime .ValueAccessor ;
114111import org .jruby .internal .runtime .methods .DynamicMethod ;
115- import org .jruby .internal .runtime .methods .JavaMethod ;
116112import org .jruby .ir .Compiler ;
117113import org .jruby .ir .IRManager ;
118114import org .jruby .ir .interpreter .Interpreter ;
135131import org .jruby .runtime .ObjectSpace ;
136132import org .jruby .runtime .RubyEvent ;
137133import org .jruby .runtime .ThreadContext ;
138- import org .jruby .runtime .Visibility ;
139134import org .jruby .runtime .builtin .IRubyObject ;
140135import org .jruby .runtime .encoding .EncodingService ;
141136import org .jruby .runtime .invokedynamic .MethodNames ;
225220import static org .jruby .api .Create .newEmptyString ;
226221import static org .jruby .api .Create .newFrozenString ;
227222import static org .jruby .api .Error .*;
228- import static org .jruby .internal . runtime . GlobalVariable . Scope . GLOBAL ;
223+ import static org .jruby .api . Warn . warn ;
229224import static org .jruby .parser .ParserType .*;
225+ import static org .jruby .runtime .ObjectAllocator .NOT_ALLOCATABLE_ALLOCATOR ;
230226import static org .jruby .util .RubyStringBuilder .str ;
231227import static org .jruby .util .RubyStringBuilder .ids ;
232228import static org .jruby .util .RubyStringBuilder .types ;
@@ -346,11 +342,9 @@ private Ruby(RubyInstanceConfig config) {
346342 classClass .makeMetaClass (metaClass );
347343 refinementClass .makeMetaClass (metaClass );
348344
349- RubyBasicObject .createBasicObjectClass (this , basicObjectClass );
350345 RubyObject .createObjectClass (objectClass );
351346 RubyModule .createModuleClass (moduleClass );
352347 RubyClass .createClassClass (this , classClass );
353- RubyModule .createRefinementClass (refinementClass );
354348
355349 // set constants now that they're initialized
356350 basicObjectClass .setConstant ("BasicObject" , basicObjectClass );
@@ -363,18 +357,13 @@ private Ruby(RubyInstanceConfig config) {
363357 // specializer for RubyObject subclasses
364358 objectSpecializer = new RubyObjectSpecializer (this );
365359
366- // Initialize Kernel and include into Object
367- RubyModule kernel = kernelModule = RubyKernel .createKernelModule (this , objectClass , config );
368- // In 1.9 and later, Kernel.gsub is defined only when '-p' or '-n' is given on the command line
369- initKernelGsub (kernel );
370-
371- // Object is ready, create top self
372- topSelf = TopSelfFactory .createTopSelf (this ,objectClass , false );
360+ kernelModule = defineModuleUnder ("Kernel" , objectClass ); // Initialize Kernel and include into Object
361+ topSelf = new RubyObject (this , objectClass ); // Object is ready, create top self
373362
374363 // Pre-create all the core classes potentially referenced during startup
375- nilClass = RubyNil . createNilClass ( this , objectClass );
376- falseClass = RubyBoolean . createFalseClass ( this , objectClass );
377- trueClass = RubyBoolean . createTrueClass ( this , objectClass );
364+ nilClass = defineClass ( "NilClass" , objectClass , NOT_ALLOCATABLE_ALLOCATOR );
365+ falseClass = defineClass ( "FalseClass" , objectClass , NOT_ALLOCATABLE_ALLOCATOR );
366+ trueClass = defineClass ( "TrueClass" , objectClass , NOT_ALLOCATABLE_ALLOCATOR );
378367
379368 nilObject = new RubyNil (this , nilClass );
380369 nilPrefilledArray = new IRubyObject [NIL_PREFILLED_ARRAY_SIZE ];
@@ -392,6 +381,16 @@ private Ruby(RubyInstanceConfig config) {
392381 // Get the main threadcontext (gets constructed for us)
393382 final ThreadContext context = getCurrentContext ();
394383
384+ RubyModule .finishCreateModuleClass (context , moduleClass );
385+ RubyClass .finishCreateClassClass (context , classClass );
386+ RubyKernel .finishKernelModule (context , kernelModule , config );
387+ RubyNil .finishNilClass (context , nilClass );
388+ RubyBoolean .finishFalseClass (context , falseClass );
389+ RubyBoolean .finishTrueClass (context , trueClass );
390+ RubyModule .finishRefinementClass (context , refinementClass );
391+ RubyBasicObject .finishBasicObjectClass (context , basicObjectClass );
392+ TopSelfFactory .finishTopSelf (context , topSelf , objectClass , false );
393+
395394 // includeModule uses TC.
396395 objectClass .includeModule (kernelModule );
397396
@@ -433,7 +432,7 @@ private Ruby(RubyInstanceConfig config) {
433432 encodingService .defineEncodings ();
434433 encodingService .defineAliases ();
435434
436- initDefaultEncodings ();
435+ initDefaultEncodings (context );
437436
438437 complexClass = profile .allowClass ("Complex" ) ? RubyComplex .createComplexClass (context , numericClass ) : null ;
439438 rationalClass = profile .allowClass ("Rational" ) ? RubyRational .createRationalClass (context , numericClass ) : null ;
@@ -573,7 +572,7 @@ private void initProfiling(ThreadContext context) {
573572 // recache core methods, since they'll have profiling wrappers now
574573 kernelModule .invalidateCacheDescendants (); // to avoid already-cached methods
575574 RubyKernel .recacheBuiltinMethods (this , kernelModule );
576- RubyBasicObject .recacheBuiltinMethods (this );
575+ RubyBasicObject .recacheBuiltinMethods (context , basicObjectClass );
577576 }
578577
579578 private void initBootLibraries (ThreadContext context ) {
@@ -600,20 +599,7 @@ private void initBootLibraries(ThreadContext context) {
600599 }
601600
602601 private void initKernelGsub (RubyModule kernel ) {
603- if (this .config .getKernelGsubDefined ()) {
604- MethodIndex .addMethodReadFields ("gsub" , FrameField .LASTLINE , FrameField .BACKREF );
605- kernel .addMethod ("gsub" , new JavaMethod (kernel , Visibility .PRIVATE , "gsub" ) {
606-
607- @ Override
608- public IRubyObject call (ThreadContext context1 , IRubyObject self , RubyModule clazz , String name , IRubyObject [] args , Block block ) {
609- return switch (args .length ) {
610- case 1 -> RubyKernel .gsub (context1 , self , args [0 ], block );
611- case 2 -> RubyKernel .gsub (context1 , self , args [0 ], args [1 ], block );
612- default -> throw argumentError (context1 , String .format ("wrong number of arguments %d for 1..2" , args .length ));
613- };
614- }
615- });
616- }
602+
617603 }
618604
619605 private ObjectSpacer initObjectSpacer (RubyInstanceConfig config ) {
@@ -643,7 +629,7 @@ private JRubyClassLoader initJRubyClassLoader(RubyInstanceConfig config) {
643629 return jrubyClassLoader ;
644630 }
645631
646- private void initDefaultEncodings () {
632+ private void initDefaultEncodings (ThreadContext context ) {
647633 // External should always have a value, but Encoding.external_encoding{,=} will lazily setup
648634 String encoding = this .config .getExternalEncoding ();
649635 if (encoding != null && !encoding .isEmpty ()) {
@@ -658,7 +644,7 @@ private void initDefaultEncodings() {
658644
659645 // Filesystem should always have a value
660646 if (Platform .IS_WINDOWS ) {
661- setDefaultFilesystemEncoding (encodingService .getWindowsFilesystemEncoding (this ));
647+ setDefaultFilesystemEncoding (encodingService .getWindowsFilesystemEncoding (context ));
662648 } else {
663649 setDefaultFilesystemEncoding (getDefaultExternalEncoding ());
664650 }
@@ -1490,7 +1476,7 @@ public RubyClass defineClassUnder(String id, RubyClass superClass, ObjectAllocat
14901476 if (superClass == null ) {
14911477 IRubyObject className = parentIsObject ? ids (this , id ) :
14921478 parent .toRubyString (getCurrentContext ()).append (newString ("::" )).append (ids (this , id ));
1493- warnings . warn (ID . NO_SUPER_CLASS , str (this , "no super class for '" , className , "', Object assumed" ));
1479+ warn (getCurrentContext () , str (this , "no super class for '" , className , "', Object assumed" ));
14941480
14951481 superClass = objectClass ;
14961482 }
@@ -3114,8 +3100,9 @@ public void loadScript(Script script, boolean wrap) {
31143100 * @param wrap Whether to use a new "self" for toplevel
31153101 */
31163102 public void loadExtension (String extName , BasicLibraryService extension , boolean wrap ) {
3117- IRubyObject self = wrap ? TopSelfFactory .createTopSelf (this , objectClass , true ) : getTopSelf ();
31183103 ThreadContext context = getCurrentContext ();
3104+ var topSelf = new RubyObject (this , objectClass );
3105+ IRubyObject self = wrap ? TopSelfFactory .finishTopSelf (context , topSelf , objectClass , true ) : getTopSelf ();
31193106
31203107 try {
31213108 context .preExtensionLoad (self );
@@ -6008,7 +5995,7 @@ public RaiseException newFrozenError(String objectType, boolean runtimeError) {
60085995
60095996 @ Deprecated
60105997 public synchronized void addEventHook (EventHook hook ) {
6011- traceEvents .addEventHook (hook );
5998+ traceEvents .addEventHook (getCurrentContext (), hook );
60125999 }
60136000
60146001 @ Deprecated
0 commit comments