2017-04-08 21:18:27 +02:00
/ *
2018-06-11 18:47:10 +02:00
* Copyright 2017 - 2018 Volker Berlin ( i - net software )
2017-04-08 21:18:27 +02:00
*
* 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 .
* /
2017-04-17 08:23:26 +02:00
package de.inetsoftware.jwebassembly.runtime ;
2017-04-08 21:18:27 +02:00
import static org.junit.Assert.assertEquals ;
2017-04-14 16:31:35 +02:00
import static org.junit.Assert.assertTrue ;
import static org.junit.Assert.fail ;
2017-04-08 21:18:27 +02:00
2017-04-14 16:31:35 +02:00
import java.io.IOException ;
2017-04-08 21:18:27 +02:00
import java.util.Collection ;
import org.junit.ClassRule ;
import org.junit.Test ;
import org.junit.runner.RunWith ;
import org.junit.runners.Parameterized ;
import org.junit.runners.Parameterized.Parameters ;
import de.inetsoftware.jwebassembly.ScriptEngine ;
2017-04-14 16:31:35 +02:00
import de.inetsoftware.jwebassembly.WasmException ;
2017-04-08 21:18:27 +02:00
import de.inetsoftware.jwebassembly.WasmRule ;
2018-06-11 18:47:10 +02:00
import de.inetsoftware.jwebassembly.api.annotation.Export ;
2018-12-13 22:32:51 +01:00
import de.inetsoftware.jwebassembly.api.annotation.Import ;
2017-04-08 21:18:27 +02:00
/ * *
* @author Volker Berlin
* /
2017-04-09 13:05:34 +02:00
@RunWith ( Parameterized . class )
2017-04-08 21:18:27 +02:00
public class RuntimeErrors {
@ClassRule
public static WasmRule rule = new WasmRule ( TestClass . class ) ;
private final ScriptEngine script ;
public RuntimeErrors ( ScriptEngine script ) {
this . script = script ;
}
@Parameters ( name = " {0} " )
2018-03-25 12:57:58 +02:00
public static Collection < ScriptEngine [ ] > data ( ) {
2017-04-08 21:18:27 +02:00
return ScriptEngine . testParams ( ) ;
}
@Test
public void longReturn ( ) {
2018-03-24 17:36:26 +01:00
String error = rule . evalWasm ( script , " longReturn " ) ;
int newlineIdx = error . indexOf ( '\n' ) ;
if ( newlineIdx > 0 ) {
error = error . substring ( 0 , newlineIdx ) ;
}
2018-08-03 22:15:12 +02:00
String expected = script = = ScriptEngine . SpiderMonkey ? " TypeError: cannot pass i64 to or from JS " : " TypeError: wasm function signature contains illegal type " ;
2018-03-24 17:36:26 +01:00
assertEquals ( expected , error ) ;
2017-04-08 21:18:27 +02:00
}
static class TestClass {
@Export
static long longReturn ( ) {
return Long . MAX_VALUE ;
}
}
2017-04-14 16:31:35 +02:00
2018-12-13 22:32:51 +01:00
private void compileErrorTest ( String expectedMessge , Class < ? > classes ) throws IOException {
WasmRule wasm = new WasmRule ( classes ) ;
2017-04-14 16:31:35 +02:00
try {
wasm . compile ( ) ;
2018-12-13 22:32:51 +01:00
fail ( " Exception expected with: " + expectedMessge ) ;
2017-04-14 16:31:35 +02:00
} catch ( WasmException ex ) {
2018-12-13 22:32:51 +01:00
assertTrue ( " Wrong error message: " + ex . getMessage ( ) , ex . getMessage ( ) . contains ( expectedMessge ) ) ;
2017-04-14 16:31:35 +02:00
} finally {
wasm . delete ( ) ;
}
2018-12-13 22:32:51 +01:00
}
@Test
public void floatRem ( ) throws IOException {
compileErrorTest ( " Modulo/Remainder " , TestModulo . class ) ;
2017-04-14 16:31:35 +02:00
}
static class TestModulo {
@Export
static float longReturn ( ) {
float a = 3 . 4F ;
return a % 2F ;
}
}
2018-12-13 22:32:51 +01:00
@Test
public void nonStaticExport ( ) throws IOException {
compileErrorTest ( " Export method must be static: " , NonStaticExport . class ) ;
}
static class NonStaticExport {
@Export
float function ( ) {
return 1 ;
}
}
@Test
public void nonStaticImport ( ) throws IOException {
compileErrorTest ( " Import method must be static: " , NonStaticImport . class ) ;
}
static class NonStaticImport {
@Import ( module = " m " , name = " n " )
float function ( ) {
return 1 ;
}
}
2017-04-08 21:18:27 +02:00
}