Skip to content

Commit

Permalink
wrapping up
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaTP committed Oct 8, 2023
1 parent a62c22b commit d061aa8
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.dylibso.chicory.runtime;

import com.dylibso.chicory.runtime.exceptions.ChicoryException;
import com.dylibso.chicory.wasm.exceptions.ChicoryException;
import com.dylibso.chicory.wasm.types.Value;

/**
Expand Down
14 changes: 10 additions & 4 deletions runtime/src/main/java/com/dylibso/chicory/runtime/Machine.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.dylibso.chicory.runtime;

import com.dylibso.chicory.runtime.exceptions.ChicoryException;
import com.dylibso.chicory.wasm.exceptions.ChicoryException;
import com.dylibso.chicory.runtime.exceptions.WASMRuntimeException;
import com.dylibso.chicory.wasm.types.*;

Expand Down Expand Up @@ -516,6 +516,9 @@ void eval(List<Instruction> code) throws ChicoryException {
case I32_DIV_S: {
var b = this.stack.pop().asInt();
var a = this.stack.pop().asInt();
if (a == Integer.MIN_VALUE && b == -1) {
throw new WASMRuntimeException("integer overflow");
}
this.stack.push(Value.i32(a / b));
break;
}
Expand All @@ -528,6 +531,9 @@ void eval(List<Instruction> code) throws ChicoryException {
case I64_DIV_S: {
var b = this.stack.pop().asLong();
var a = this.stack.pop().asLong();
if (a == Long.MIN_VALUE && b == -1L) {
throw new WASMRuntimeException("integer overflow");
}
this.stack.push(Value.i64(a / b));
break;
}
Expand Down Expand Up @@ -768,11 +774,11 @@ void eval(List<Instruction> code) throws ChicoryException {
throw e;
} catch (ArithmeticException e) {
if (e.getMessage().equalsIgnoreCase("/ by zero")) {
throw new WASMRuntimeException("integer divide by zero");
throw new WASMRuntimeException("integer divide by zero: " + e.getMessage(), e);
}
throw new WASMRuntimeException(e.getMessage());
throw new WASMRuntimeException(e.getMessage(), e);
} catch (Exception e) {
throw new WASMRuntimeException(e.getMessage());
throw new WASMRuntimeException(e.getMessage(), e);
}
}

Expand Down
4 changes: 2 additions & 2 deletions runtime/src/main/java/com/dylibso/chicory/runtime/Module.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.dylibso.chicory.runtime;

import com.dylibso.chicory.runtime.exceptions.ChicoryException;
import com.dylibso.chicory.runtime.exceptions.InvalidException;
import com.dylibso.chicory.wasm.exceptions.ChicoryException;
import com.dylibso.chicory.wasm.exceptions.InvalidException;
import com.dylibso.chicory.wasm.Parser;
import com.dylibso.chicory.wasm.types.*;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.dylibso.chicory.runtime;

import com.dylibso.chicory.runtime.exceptions.ChicoryException;
import com.dylibso.chicory.wasm.exceptions.ChicoryException;

import java.util.List;
import java.util.Stack;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.dylibso.chicory.runtime.exceptions;

import com.dylibso.chicory.wasm.exceptions.ChicoryException;

public class WASMRuntimeException extends ChicoryException {
public WASMRuntimeException(String msg) {
super(msg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,24 @@ public void generate(File specFile, File wasmFilesFolder) {
cu.setStorage(sourceTargetFolder.toPath().resolve(testName + ".java"));

// all the imports
// junit imports
cu.addImport("org.junit.Test");
cu.addImport("org.junit.Assert.assertEquals", true, false);
cu.addImport("org.junit.Assert.assertThrows", true, false);
cu.addImport("org.junit.Assert.assertTrue", true, false);

cu.addImport("com.dylibso.chicory.runtime.exceptions.InvalidException");
cu.addImport("com.dylibso.chicory.runtime.exceptions.MalformedException");
// runtime imports
cu.addImport("com.dylibso.chicory.runtime.exceptions.WASMRuntimeException");
cu.addImport("com.dylibso.chicory.runtime.ExportFunction");
cu.addImport("com.dylibso.chicory.runtime.Instance");
cu.addImport("com.dylibso.chicory.runtime.Module");
cu.addImport("com.dylibso.chicory.runtime.ModuleType");

// base imports
cu.addImport("com.dylibso.chicory.wasm.exceptions.InvalidException");
cu.addImport("com.dylibso.chicory.wasm.exceptions.MalformedException");
cu.addImport("com.dylibso.chicory.wasm.types.Value");

cu.addImport("org.junit.Assert.assertEquals", true, false);
cu.addImport("org.junit.Assert.assertThrows", true, false);

var testClass = cu.addClass(testName);

MethodDeclaration method = null;
Expand Down Expand Up @@ -158,7 +161,7 @@ private List<Expression> generateAssert(String varName, Command cmd) {
case ASSERT_TRAP:
var assertDecl = new NameExpr("var exception = assertThrows(WASMRuntimeException.class, () -> "+ varName + invocationMethod + typeConversion + ")");
if (cmd.getText() != null) {
var messageMatch = new NameExpr("assertEquals(\"" + cmd.getText() + "\", exception.getMessage())");
var messageMatch = new NameExpr("assertTrue(exception.getMessage().contains(\"" + cmd.getText() + "\"))");
return List.of(assertDecl, messageMatch);
} else {
return List.of(assertDecl);
Expand Down Expand Up @@ -196,7 +199,7 @@ private List<Expression> generateAssertThrows(Command cmd, File wasmFilesFolder)
var assertThrows = new NameExpr(assignementStmt + "assertThrows(" + exceptionType + ".class, () -> " + generateModuleInstantiation(cmd, wasmFilesFolder) + ")");

if (cmd.getText() != null) {
var messageMatch = new NameExpr("assertEquals(\"" + cmd.getText() + "\", exception.getMessage())");
var messageMatch = new NameExpr("assertTrue(exception.getMessage().contains(\"" + cmd.getText() + "\"))");
return List.of(assertThrows, messageMatch);
} else {
return List.of(assertThrows);
Expand Down
9 changes: 7 additions & 2 deletions wasm/src/main/java/com/dylibso/chicory/wasm/Parser.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.dylibso.chicory.wasm;

import com.dylibso.chicory.wasm.exceptions.MalformedException;
import com.dylibso.chicory.wasm.types.*;

import java.io.FileInputStream;
Expand Down Expand Up @@ -68,9 +69,13 @@ public void parse() {
var buffer = readByteBuffer();

int magicNumber = buffer.getInt();
assert magicNumber == MAGIC_BYTES;
if (magicNumber != MAGIC_BYTES) {
throw new MalformedException("unexpected token: magic number mismatch, found: " + magicNumber + " expected: " + MAGIC_BYTES);
}
int version = buffer.getInt();
assert version == 1;
if (version != 1) {
throw new MalformedException("unexpected token: unsupported version, found: " + version + " expected: " + 1);
}

while (buffer.hasRemaining()) {
var sectionId = (int) readVarUInt32(buffer);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.dylibso.chicory.runtime.exceptions;
package com.dylibso.chicory.wasm.exceptions;

public class ChicoryException extends RuntimeException {
public ChicoryException(String msg) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.dylibso.chicory.runtime.exceptions;
package com.dylibso.chicory.wasm.exceptions;

public class InvalidException extends ChicoryException {
public InvalidException(String msg) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.dylibso.chicory.runtime.exceptions;
package com.dylibso.chicory.wasm.exceptions;

public class MalformedException extends ChicoryException {
public MalformedException(String msg) {
Expand Down

0 comments on commit d061aa8

Please sign in to comment.