diff --git a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java index 4e53909..bf863e3 100644 --- a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java +++ b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java @@ -42,6 +42,7 @@ import de.inetsoftware.jwebassembly.JWebAssembly; import de.inetsoftware.jwebassembly.WasmException; import de.inetsoftware.jwebassembly.javascript.JavaScriptWriter; import de.inetsoftware.jwebassembly.module.TypeManager.StructType; +import de.inetsoftware.jwebassembly.module.nativecode.ReplacementForEnums; import de.inetsoftware.jwebassembly.wasm.AnyType; import de.inetsoftware.jwebassembly.wasm.FunctionType; import de.inetsoftware.jwebassembly.wasm.ValueType; @@ -185,6 +186,20 @@ public class ModuleGenerator { } } + if( classFile.isEnum() ) { + //temporary Hack because the generated Enum.valueOf(String) use Reflection which currently is not supported + for( MethodInfo method : classFile.getMethods() ) { + if( "valueOf".equals( method.getName() ) && method.getType().startsWith( "(Ljava/lang/String;)" ) ) { + FunctionName valueOf = new FunctionName( method ); + String replaceForEnums = ReplacementForEnums.class.getName().replace( ".", "/" ); + ClassFile file = classFileLoader.get( replaceForEnums ); + MethodInfo method2 = file.getMethod( "valueOf_", "(Ljava/lang/String;)L" + replaceForEnums + ";" ); + functions.addReplacement( valueOf, method2 ); + break; + } + } + } + iterateMethods( classFile, m -> prepareMethod( m ) ); } diff --git a/src/de/inetsoftware/jwebassembly/module/nativecode/ReplacementForEnums.java b/src/de/inetsoftware/jwebassembly/module/nativecode/ReplacementForEnums.java new file mode 100644 index 0000000..b2db65d --- /dev/null +++ b/src/de/inetsoftware/jwebassembly/module/nativecode/ReplacementForEnums.java @@ -0,0 +1,56 @@ +/* + Copyright 2021 Volker Berlin (i-net software) + + 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 + + http://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 de.inetsoftware.jwebassembly.module.nativecode; + +import java.util.HashMap; +import java.util.Map; + +/** + * Replacement for java.lang.Enum + * + * @author Volker Berlin + */ +public enum ReplacementForEnums { + ; + private transient Map enumConstantDirectory; + + /** + * Replacement code for generated Enum.valueOf( String ) + * @param name the enum name + * @return The singleton instance for the name + */ + ReplacementForEnums valueOf_( String name ) { + Map map = enumConstantDirectory; + if( map == null ) { + ReplacementForEnums[] universe = values(); + map = new HashMap<>( 2 * universe.length ); + for( ReplacementForEnums constant : universe ) { + map.put( constant.name(), constant ); + } + enumConstantDirectory = map; + } + + ReplacementForEnums result = map.get( name ); + if( result != null ) { + return result; + } + if( name == null ) { + throw new NullPointerException( "Name is null" ); + } + throw new IllegalArgumentException( "No enum constant " + getClass().getCanonicalName() + "." + name ); + } +}