-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
imp: ReactJS - Another round of fixes and improvements (#846)
- Loading branch information
Showing
12 changed files
with
293 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
Micronaut React SSR has the following known issues and limitations: | ||
|
||
- There is currently no way to extend the Javascript execution environment with custom Java-side objects. | ||
- There is no built-in support for server side fetching. | ||
- The rendering isn't streamed to the user. | ||
- `<Suspense>` is not supported. |
66 changes: 0 additions & 66 deletions
66
views-react/src/main/java/io/micronaut/views/react/IntrospectableToPolyglotObject.java
This file was deleted.
Oops, something went wrong.
43 changes: 43 additions & 0 deletions
43
views-react/src/main/java/io/micronaut/views/react/JSBeanFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2017-2024 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micronaut.views.react; | ||
|
||
import jakarta.inject.Singleton;; | ||
import io.micronaut.context.annotation.Factory; | ||
import io.micronaut.core.annotation.Internal; | ||
import org.graalvm.polyglot.HostAccess; | ||
|
||
/** | ||
* Allows the default Javascript context and host access policy to be controlled. | ||
*/ | ||
@Factory | ||
@Internal | ||
class JSBeanFactory { | ||
/** | ||
* This defaults to | ||
* {@link HostAccess#ALL} if the sandbox is disabled, or {@link HostAccess#CONSTRAINED} if it's on. | ||
* By replacing the {@link HostAccess} bean you can whitelist methods/properties by name or | ||
* annotation, which can be useful for exposing third party libraries where you can't add the | ||
* normal {@link HostAccess.Export} annotation, or allowing sandboxed JS to extend or implement | ||
* Java types. | ||
*/ | ||
@Singleton | ||
HostAccess hostAccess(ReactViewsRendererConfiguration configuration) { | ||
return configuration.getSandbox() | ||
? HostAccess.CONSTRAINED | ||
: HostAccess.ALL; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 165 additions & 0 deletions
165
views-react/src/main/java/io/micronaut/views/react/ProxyObjectWithIntrospectableSupport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
/* | ||
* Copyright 2017-2020 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micronaut.views.react; | ||
|
||
import io.micronaut.core.annotation.Internal; | ||
import io.micronaut.core.annotation.Nullable; | ||
import io.micronaut.core.beans.BeanIntrospection; | ||
import io.micronaut.core.beans.BeanIntrospector; | ||
import io.micronaut.core.beans.BeanMap; | ||
import io.micronaut.core.beans.BeanMethod; | ||
import org.graalvm.polyglot.Context; | ||
import org.graalvm.polyglot.Value; | ||
import org.graalvm.polyglot.proxy.ProxyArray; | ||
import org.graalvm.polyglot.proxy.ProxyExecutable; | ||
import org.graalvm.polyglot.proxy.ProxyObject; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Map; | ||
|
||
/** | ||
* A proxy object similar to that returned by {@link ProxyObject#fromMap(Map)}, but with support | ||
* for Micronaut's bean introspection system (a form of compile-time reflection code generation). | ||
* Reading a key whose value is an introspectable bean will use the {@link BeanMap} instead of | ||
* the regular polyglot mapping. | ||
*/ | ||
@Internal | ||
class ProxyObjectWithIntrospectableSupport implements ProxyObject { | ||
private final Context context; | ||
private final Object target; | ||
private final boolean isStringMap; | ||
|
||
@Nullable | ||
private final BeanIntrospection<?> introspection; | ||
|
||
ProxyObjectWithIntrospectableSupport(Context context, Object targetObject) { | ||
this.context = context; | ||
|
||
if (targetObject == null) { | ||
throw new NullPointerException("Cannot proxy a null"); | ||
} | ||
|
||
this.target = targetObject; | ||
this.isStringMap = isStringMap(targetObject); | ||
this.introspection = isStringMap ? null : BeanIntrospector.SHARED.findIntrospection(targetObject.getClass()).orElseThrow(); | ||
} | ||
|
||
private ProxyObjectWithIntrospectableSupport(Context context, Object target, boolean isStringMap, BeanIntrospection<?> introspection) { | ||
this.context = context; | ||
this.target = target; | ||
this.isStringMap = isStringMap; | ||
this.introspection = introspection; | ||
} | ||
|
||
private static boolean isStringMap(Object obj) { | ||
if (obj instanceof Map<?, ?> map) { | ||
return map.keySet().stream().allMatch(it -> it instanceof String); | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
@Override | ||
public Object getMember(String key) { | ||
Map<String, Object> map = asMap(); | ||
|
||
// Is it a property? | ||
Object result = map.get(key); | ||
if (result != null) { | ||
boolean resultIsMap = isStringMap(result); | ||
var resultIntrospection = BeanIntrospector.SHARED.findIntrospection(result.getClass()); | ||
if (resultIsMap || resultIntrospection.isPresent()) { | ||
return new ProxyObjectWithIntrospectableSupport(context, result, resultIsMap, resultIntrospection.orElseThrow()); | ||
} else { | ||
return context.asValue(result); | ||
} | ||
} | ||
|
||
// Can it be an @Executable method? | ||
if (introspection != null) { | ||
Collection<? extends BeanMethod<?, Object>> beanMethods = introspection.getBeanMethods(); | ||
for (BeanMethod<?, Object> method : beanMethods) { | ||
if (method.getName().equals(key)) { | ||
return new PolyglotBeanMethod(beanMethods); | ||
} | ||
} | ||
} | ||
|
||
return context.asValue(null); | ||
} | ||
|
||
@Override | ||
public Object getMemberKeys() { | ||
return ProxyArray.fromList(getInvokableNames()); | ||
} | ||
|
||
@Override | ||
public boolean hasMember(String key) { | ||
return getInvokableNames().contains(key); | ||
} | ||
|
||
private ArrayList<Object> getInvokableNames() { | ||
ArrayList<Object> propNames = new ArrayList<>(asMap().keySet()); | ||
if (introspection != null) { | ||
introspection.getBeanMethods().forEach(it -> propNames.add(it.getName())); | ||
} | ||
return propNames; | ||
} | ||
|
||
@Override | ||
public void putMember(String key, Value value) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private Map<String, Object> asMap() { | ||
return isStringMap ? (Map<String, Object>) target : BeanMap.of(target); | ||
} | ||
|
||
@SuppressWarnings("rawtypes") | ||
private final class PolyglotBeanMethod implements ProxyExecutable { | ||
private final Collection<? extends BeanMethod<?, Object>> candidates; | ||
|
||
private PolyglotBeanMethod(Collection<? extends BeanMethod<?, Object>> candidates) { | ||
assert !candidates.isEmpty(); | ||
this.candidates = candidates; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public Object execute(Value... arguments) { | ||
BeanMethod candidate = findCandidateByNumberOfArguments(arguments); | ||
Object[] convertedArgs = new Object[arguments.length]; | ||
for (int i = 0; i < arguments.length; i++) { | ||
convertedArgs[i] = arguments[i].as(Object.class); | ||
} | ||
return context.asValue(candidate.invoke(target, convertedArgs)); | ||
} | ||
|
||
private BeanMethod findCandidateByNumberOfArguments(Value[] arguments) { | ||
int minNeeded = Integer.MAX_VALUE; | ||
for (BeanMethod candidate : candidates) { | ||
int numArgs = candidate.getArguments().length; | ||
minNeeded = Math.min(minNeeded, numArgs); | ||
if (numArgs == arguments.length) { | ||
return candidate; | ||
} | ||
} | ||
throw new UnsupportedOperationException(String.format("No candidates found with the right number of arguments for method %s, needed at least %d but got %d", candidates.iterator().next().getName(), minNeeded, arguments.length)); | ||
} | ||
} | ||
} |
Oops, something went wrong.