@@ -2058,3 +2058,72 @@ if (jvm && typeof jvm.addVirtualMethod === "function" && jvm.classes && jvm.clas
20582058 emitDiagLine ( "PARPAR:DIAG:INIT:shim=baseTestOnShowLambdaDispatch" ) ;
20592059 }
20602060}
2061+
2062+ // ---------------------------------------------------------------------------
2063+ // Shim: CodenameOneImplementation.initImpl – guard against getClass()/getName()
2064+ // failures on the Runnable argument passed to Display.init().
2065+ //
2066+ // In the ParparVM JS translation, Object.getClass() may return null or
2067+ // Class.getName() may return a name with underscores instead of dots.
2068+ // The base initImpl calls m.getClass().getName() and then
2069+ // String.substring(0, String.lastIndexOf('.')) which can throw a TypeError
2070+ // (null receiver) or StringIndexOutOfBoundsException (-1 index).
2071+ //
2072+ // This shim wraps the original initImpl; if it fails it falls back to calling
2073+ // init(m) directly and setting the packageName field from the class name of the
2074+ // bootstrap object.
2075+ // ---------------------------------------------------------------------------
2076+ const initImplMethodId = "cn1_com_codename1_impl_CodenameOneImplementation_initImpl_java_lang_Object" ;
2077+ const initImplOriginal = ( function ( ) {
2078+ if ( ! jvm || ! jvm . classes ) {
2079+ return null ;
2080+ }
2081+ const cls = jvm . classes [ "com_codename1_impl_CodenameOneImplementation" ] ;
2082+ if ( cls && cls . methods && typeof cls . methods [ initImplMethodId ] === "function" ) {
2083+ return cls . methods [ initImplMethodId ] ;
2084+ }
2085+ return typeof global [ initImplMethodId ] === "function" ? global [ initImplMethodId ] :
2086+ typeof global [ initImplMethodId + "__impl" ] === "function" ? global [ initImplMethodId + "__impl" ] : null ;
2087+ } ) ( ) ;
2088+
2089+ bindCiFallback ( "CodenameOneImplementation.initImplSafe" , [
2090+ initImplMethodId ,
2091+ initImplMethodId + "__impl"
2092+ ] , function * ( __cn1ThisObject , m ) {
2093+ if ( typeof initImplOriginal === "function" ) {
2094+ try {
2095+ return yield * initImplOriginal ( __cn1ThisObject , m ) ;
2096+ } catch ( err ) {
2097+ const message = String ( err && err . message ? err . message : err || "" ) ;
2098+ if ( message . indexOf ( "__classDef" ) >= 0 || message . indexOf ( "lastIndexOf" ) >= 0 || message . indexOf ( "substring" ) >= 0 ) {
2099+ emitCiFallbackMarker ( "CodenameOneImplementation.initImplSafe.recover" , "HIT" ) ;
2100+ // The original initImpl calls init(m) first, then m.getClass().getName().
2101+ // If we land here, init(m) already succeeded – only the getClass/getName
2102+ // chain failed. Do NOT call init(m) again; just set the missing fields.
2103+ const className = ( m && m . __class ) ? String ( m . __class ) . replace ( / _ / g, "." ) : "com.codename1.impl.html5" ;
2104+ const dotIndex = className . lastIndexOf ( "." ) ;
2105+ const pkg = dotIndex >= 0 ? className . substring ( 0 , dotIndex ) : className ;
2106+ __cn1ThisObject [ "cn1_com_codename1_impl_CodenameOneImplementation_packageName" ] = jvm . createStringLiteral ( pkg ) ;
2107+ __cn1ThisObject [ "cn1_com_codename1_impl_CodenameOneImplementation_initiailized" ] = 1 ;
2108+ return null ;
2109+ }
2110+ throw err ;
2111+ }
2112+ }
2113+ // No original method found – perform safe init inline
2114+ const initMethodId2 = "cn1_com_codename1_impl_CodenameOneImplementation_init_java_lang_Object" ;
2115+ try {
2116+ const initMethod2 = jvm . resolveVirtual ( __cn1ThisObject . __class , initMethodId2 ) ;
2117+ if ( typeof initMethod2 === "function" ) {
2118+ yield * initMethod2 ( __cn1ThisObject , m ) ;
2119+ }
2120+ } catch ( _ignore ) {
2121+ // Best effort – init may already have been called
2122+ }
2123+ const className2 = ( m && m . __class ) ? String ( m . __class ) . replace ( / _ / g, "." ) : "com.codename1.impl.html5" ;
2124+ const dotIndex2 = className2 . lastIndexOf ( "." ) ;
2125+ const pkg2 = dotIndex2 >= 0 ? className2 . substring ( 0 , dotIndex2 ) : className2 ;
2126+ __cn1ThisObject [ "cn1_com_codename1_impl_CodenameOneImplementation_packageName" ] = jvm . createStringLiteral ( pkg2 ) ;
2127+ __cn1ThisObject [ "cn1_com_codename1_impl_CodenameOneImplementation_initiailized" ] = 1 ;
2128+ return null ;
2129+ } ) ;
0 commit comments