Skip to content

Commit

Permalink
Add the compiler flag "IgnoreNative" to generate stubs for rall not r…
Browse files Browse the repository at this point in the history
…eplaced, referenced native methods. #43
  • Loading branch information
Horcrux7 committed Jun 26, 2022
1 parent 330634e commit 9da6d8e
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/de/inetsoftware/jwebassembly/JWebAssembly.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ public class JWebAssembly {
*/
public static final String WASM_USE_EH = "wasm.use_eh";

/**
* Compiler property to ignore all referenced native methods without declared replacement in a library and replace them with a stub that throws an exception at runtime.
*/
public static final String IGNORE_NATIVE = "IgnoreNative";

/**
* The logger instance
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2019 Volker Berlin (i-net software)
Copyright 2019 - 2022 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.
Expand Down Expand Up @@ -66,6 +66,10 @@ public ArraySyntheticFunctionName( String className, String name, String signatu
*/
@Override
public Iterator<AnyType> getSignature( TypeManager types ) {
return Arrays.asList( signatureTypes ).iterator();
if( signatureTypes != null ) {
return Arrays.asList( signatureTypes ).iterator();
} else {
return super.getSignature( types );
}
}
}
10 changes: 9 additions & 1 deletion src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,15 @@ private WasmCodeBuilder createInstructions( MethodInfo method ) throws IOExcepti
strings.getStringConstantFunction();
return null;
}
throw new WasmException( "Abstract or native method can not be used: " + name.signatureName +"\nIf you want to use classes with native code, you must use a library that implements these native methods, such as 'de.inetsoftware:jwebassembly-api:+'.", -1 );

if( writer.options.ignoreNative() ) {
JWebAssembly.LOGGER.severe( "Native method will throw an exception at runtime: " + name.signatureName );
WatCodeSyntheticFunctionName nativeStub = new WatCodeSyntheticFunctionName( name.className, name.methodName, name.signature, "unreachable", (AnyType[])null );
functions.markAsNeededAndReplaceIfExists( nativeStub );
return null;
}

throw new WasmException( "Native methods cannot be compiled to WebAssembly: " + name.signatureName +"\nIf you want to use classes with native code, you must use a library that implements these native methods, such as 'de.inetsoftware:jwebassembly-api:+' or implements this native method self.", -1 );
}
} catch( Throwable ioex ) {
int lineNumber = code == null ? -1 : code.getFirstLineNr();
Expand Down
13 changes: 13 additions & 0 deletions src/de/inetsoftware/jwebassembly/module/WasmOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public class WasmOptions {

private final boolean useEH;

private final boolean ignoreNative;

@Nonnull
private final String sourceMapBase;

Expand Down Expand Up @@ -76,6 +78,8 @@ public WasmOptions( HashMap<String, String> properties ) {
debugNames = Boolean.parseBoolean( properties.get( JWebAssembly.DEBUG_NAMES ) );
useGC = Boolean.parseBoolean( properties.getOrDefault( JWebAssembly.WASM_USE_GC, "false" ) );
useEH = Boolean.parseBoolean( properties.getOrDefault( JWebAssembly.WASM_USE_EH, "false" ) );
ignoreNative = Boolean.parseBoolean( properties.getOrDefault( JWebAssembly.IGNORE_NATIVE, "false" ) );

String base = properties.getOrDefault( JWebAssembly.SOURCE_MAP_BASE, "" );
if( !base.isEmpty() && !base.endsWith( "/" ) ) {
base += "/";
Expand Down Expand Up @@ -110,6 +114,15 @@ public boolean useEH() {
return useEH;
}

/**
* Compiler property to add a stub for all referenced native methods that has no replacement.
*
* @return true, if ignore missing native methods
*/
public boolean ignoreNative() {
return ignoreNative;
}

/**
* Get the relative path between the final wasm file location and the source files location.
* If not empty it should end with a slash like "../../src/main/java/".
Expand Down
4 changes: 2 additions & 2 deletions test/de/inetsoftware/jwebassembly/runtime/RuntimeErrors.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2021 Volker Berlin (i-net software)
* Copyright 2017 - 2022 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.
Expand Down Expand Up @@ -88,7 +88,7 @@ float function() {

@Test
public void nativeMethod() throws IOException {
compileErrorTest( "Abstract or native method can not be used:", NativeMethod.class );
compileErrorTest( "Native methods cannot be compiled to WebAssembly:", NativeMethod.class );
}

static class NativeMethod {
Expand Down

0 comments on commit 9da6d8e

Please sign in to comment.