From 185a0515c76143a2e30af14599507c652edab7d7 Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Sun, 17 Nov 2019 17:28:31 +0100 Subject: [PATCH] supports also constructor replacement with a Java method --- .../jwebassembly/module/WasmCodeBuilder.java | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java b/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java index 5e81a9c..80bce5e 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java @@ -414,9 +414,25 @@ public abstract class WasmCodeBuilder { WasmCallInstruction instruction = new WasmCallInstruction( name, javaCodePos, lineNumber, types ); if( "".equals( name.methodName ) ) { + // check if there a factory for the constructor in JavaScript then we need to do some more complex patching Function importAnannotation = functions.getImportAnannotation( name ); - if( importAnannotation != null ) { - // if there a JavaScript replacement for a constructor we need also replace the create instance instruction + FunctionName factoryName = null; + + if( importAnannotation != null ) { // JavaScript replacement for a constructor via import + // The new signature need a return value. The of Java has ever a void return value + String signature = name.signature; + signature = signature.substring( 0, signature.length() - 1 ) + "Ljava/lang/Object;"; + factoryName = new ImportSyntheticFunctionName( "String", "init", signature, importAnannotation ); + } else { + MethodInfo replace = functions.replace( name, null ); + if( replace != null && !"".equals( replace.getName() ) ) { + // the constructor was replaced with a factory method. Typical this method called then a JavaScript replacement + factoryName = new FunctionName( replace ); + } + } + + if( factoryName != null ) { + // the constructor was replaced we need also replace the create instance instruction List instructions = this.instructions; for( int i = instructions.size() - 1; i >= 0; i-- ) { WasmInstruction instr = instructions.get( i ); @@ -432,11 +448,8 @@ public abstract class WasmCodeBuilder { } } } - // The new signature need a return value. The of Java has ever a void return value - String signature = name.signature; - signature = signature.substring( 0, signature.length() - 1 ) + "Ljava/lang/Object;"; - FunctionName name2 = new ImportSyntheticFunctionName( "String", "init", signature, importAnannotation ); - instruction = new WasmCallInstruction( name2, javaCodePos, lineNumber, types ); + // the new instruction + instruction = new WasmCallInstruction( factoryName, javaCodePos, lineNumber, types ); } }