Skip to content

Remove nashornPolyfills #1544 #1573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ repositories {
xp.enonicRepo()
}

sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = sourceCompatibility

dependencies {
compileOnly "com.enonic.xp:core-api:${xpVersion}"
compileOnly "com.enonic.xp:portal-api:${xpVersion}"
Expand Down
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
Loading