Skip to content

Commit

Permalink
Remove nashornPolyfills #1544
Browse files Browse the repository at this point in the history
  • Loading branch information
rymsha committed Jan 14, 2025
1 parent a13ca0b commit 96730c0
Show file tree
Hide file tree
Showing 14 changed files with 43 additions and 427 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.enonic.lib.react4xp.ssr.engine.EngineFactory;
import com.enonic.lib.react4xp.ssr.errors.ErrorHandler;
import com.enonic.lib.react4xp.ssr.renderer.Renderer;
import com.enonic.lib.react4xp.ssr.renderer.RendererFactory;
Expand Down Expand Up @@ -43,8 +42,7 @@ public void initialize( BeanContext context )
}

public void setup( String appName, String scriptsHome, String libraryName, String chunkfilesHome, String entriesJsonFilename,
String chunksGlobalsJsonFilename, String statsComponentsFilename, Integer ssrMaxThreads,
String engineName )
String chunksGlobalsJsonFilename, String statsComponentsFilename, Integer ssrMaxThreads )
{
if ( isInitialized.compareAndSet( false, true ) )
{
Expand All @@ -59,8 +57,7 @@ public void setup( String appName, String scriptsHome, String libraryName, Strin
statsComponentsFilename );

final ResourceReader resourceReader = new ResourceReaderImpl( resourceServiceSupplier, ApplicationKey.from( config.APP_NAME ) );
final EngineFactory engineFactory = new EngineFactory( engineName );
final RendererFactory rendererFactory = new RendererFactory( engineFactory, resourceReader, config );
final RendererFactory rendererFactory = new RendererFactory( resourceReader, config );

rendererPool = new GenericObjectPool<>( rendererFactory, createPoolConfig( poolSize ) );
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,18 +1,47 @@
package com.enonic.lib.react4xp.ssr.engine;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class EngineFactory
{
private final EngineBuilderPlatform engineBuilder;
private final static Logger LOG = LoggerFactory.getLogger( EngineFactory.class );

private static final ScriptEngineManager SCRIPT_ENGINE_MANAGER;

public EngineFactory( String engineName )
static
{
this.engineBuilder = new EngineBuilderPlatform( engineName );
final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader( ClassLoader.getSystemClassLoader() );
try
{
SCRIPT_ENGINE_MANAGER = new ScriptEngineManager();
}
finally
{
Thread.currentThread().setContextClassLoader( classLoader );
}
}

public ScriptEngine buildEngine()
public static ScriptEngine buildEngine()
{
return engineBuilder.buildEngine();
LOG.debug( "Init script engine");
final ScriptEngine engineByName;

final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader( ClassLoader.getSystemClassLoader() );
try
{
engineByName = SCRIPT_ENGINE_MANAGER.getEngineByName( "Graal.js" );
}
finally
{
Thread.currentThread().setContextClassLoader( classLoader );
}
LOG.debug( "Got engine {}", engineByName.getFactory().getEngineName() );
return engineByName;
}
}
28 changes: 4 additions & 24 deletions src/main/java/com/enonic/lib/react4xp/ssr/renderer/Renderer.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.enonic.lib.react4xp.ssr.renderer;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import javax.script.Bindings;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
Expand All @@ -32,12 +30,8 @@ public class Renderer {

private static final String POLYFILL_BASICS_FILE = "/lib/enonic/polyfill-react4xp/polyfillBasics.js";

private static final String POLYFILL_REACT4XP_NASHORN_FILE = "/lib/enonic/polyfill-react4xp/nashornPolyfills.js";

private static final String POLYFILL_REACT4XP_NODE_FILE = "/lib/enonic/polyfill-react4xp/nodePolyfills.js";

private static final String POLYFILL_REACT4XP_USER_ADDED_FILE = "/lib/enonic/react4xp/nashornPolyfills.userAdded.js";

private final long id;

private final ScriptEngine engine;
Expand All @@ -46,26 +40,21 @@ public class Renderer {

private final AssetLoader assetLoader;

public Renderer( final EngineFactory engineFactory, final ResourceReader resourceReader, final Config config, final long id )
public Renderer( final ResourceReader resourceReader, final Config config, final long id )
{
this.id = id;
this.libraryName = config.LIBRARY_NAME;

LOG.debug( "#{}:{} starting init...", this.id, this.libraryName );

this.engine = engineFactory.buildEngine();
this.engine = EngineFactory.buildEngine();

this.assetLoader = new AssetLoader( resourceReader, config.SCRIPTS_HOME, id, engine );

LOG.debug( "#{}:{} loading polyfills ...", this.id, this.libraryName );

this.assetLoader.loadAssetIntoEngine( POLYFILL_BASICS_FILE, true );
if ( this.engine.getFactory().getEngineName().contains( "Nashorn" ) )
{
this.assetLoader.loadAssetIntoEngine( POLYFILL_REACT4XP_NASHORN_FILE, true );
}
this.assetLoader.loadAssetIntoEngine( POLYFILL_REACT4XP_NODE_FILE, true );
this.assetLoader.loadAssetIntoEngine( POLYFILL_REACT4XP_USER_ADDED_FILE, false );

LOG.debug( "#{}:{} loading globals...", this.id, this.libraryName );

Expand Down Expand Up @@ -114,17 +103,12 @@ public Map<String, String> render( final String entryName, final String props, f

final Object propsJson = parseJson( props );
final Object entryWithProps;
if ( entryObject.get( "default" ) instanceof Function )
{
// Graal.js fails to find "default" method when invokeMethod is used. Call directly
// Hopefully will be fixed in future versions of Graal.js
// entryWithProps = invocable.invokeMethod( entryObject, "default", propsJson );

final var defaultFunction = (Function<Object, Object[]>) entryObject.get( "default" );
entryWithProps = defaultFunction.apply( new Object[]{propsJson} );
}
else
{
entryWithProps = invocable.invokeMethod( entryObject, "default", propsJson );
}

final String renderedHtml = (String) invocable.invokeMethod( this.engine.get( "ReactDOMServer" ), "renderToString", entryWithProps );

Expand Down Expand Up @@ -197,10 +181,6 @@ private static <T> List<T> adaptList( final Object object )
{
return (List<T>) object;
}
else if ( object instanceof Bindings ) // Nashorn case
{
return List.copyOf( (Collection<T>) ( (Bindings) object ).values() );
}
else
{
throw new IllegalArgumentException( "object is not a list" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,26 @@
import org.apache.commons.pool2.impl.DefaultPooledObject;

import com.enonic.lib.react4xp.ssr.Config;
import com.enonic.lib.react4xp.ssr.engine.EngineFactory;
import com.enonic.lib.react4xp.ssr.resources.ResourceReader;

public class RendererFactory extends BasePooledObjectFactory<Renderer>
{
private final EngineFactory engineFactory;

private final ResourceReader resourceReader;

private final Config config;

private final AtomicLong id = new AtomicLong(0);

public RendererFactory( EngineFactory engineFactory, ResourceReader resourceReader, Config config )
public RendererFactory( ResourceReader resourceReader, Config config )
{
this.engineFactory = engineFactory;
this.resourceReader = resourceReader;
this.config = config;
}

@Override
public Renderer create()
{
return new Renderer( engineFactory, resourceReader, config, id.incrementAndGet() );
return new Renderer( resourceReader, config, id.incrementAndGet() );
}

@Override
Expand Down
Loading

0 comments on commit 96730c0

Please sign in to comment.