-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Call spec #45
Call spec #45
Changes from all commits
5681b7c
d9f1a2c
038a2a0
247d5b5
e694b9d
12db29f
62594f1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,15 +56,18 @@ public Instance instantiate(HostFunction[] hostFunctions) { | |
if (g.getInit().length > 2) | ||
throw new RuntimeException("We don't support this global initializer"); | ||
var instr = g.getInit()[0]; | ||
// TODO we're assuming this is a const value, do we need to eval it? | ||
if (instr.getOpcode() != OpCode.I32_CONST && instr.getOpcode() != OpCode.I64_CONST) { | ||
throw new RuntimeException( | ||
"We only support I32_CONST and I64_CONST on global initializers right now"); | ||
} | ||
if (instr.getOpcode() == OpCode.I32_CONST) { | ||
globals[i] = Value.i32(instr.getOperands()[0]); | ||
} else { | ||
} else if (instr.getOpcode() == OpCode.I64_CONST) { | ||
globals[i] = Value.i64(instr.getOperands()[0]); | ||
} else if (instr.getOpcode() == OpCode.F32_CONST) { | ||
globals[i] = Value.f32(instr.getOperands()[0]); | ||
} else if (instr.getOpcode() == OpCode.F64_CONST) { | ||
globals[i] = Value.f64(instr.getOperands()[0]); | ||
} else { | ||
throw new RuntimeException( | ||
"We only support i32,i64,f32,f64 const opcodes on global initializers right" | ||
+ " now"); | ||
} | ||
} | ||
|
||
|
@@ -143,6 +146,24 @@ public Instance instantiate(HostFunction[] hostFunctions) { | |
exports.put("_start", export); | ||
} | ||
|
||
Table table = null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code is a little bit naive on my part but I think it works for the time being. We can update it when we get to the other specs that address tables and elements. |
||
if (module.getTableSection() != null) { | ||
if (module.getTableSection().getTables().length > 1) { | ||
throw new ChicoryException("We don't currently support more than 1 table"); | ||
} | ||
table = module.getTableSection().getTables()[0]; | ||
if (module.getElementSection() != null) { | ||
for (var el : module.getElementSection().getElements()) { | ||
var idx = el.getTableIndex(); | ||
if (idx != 0) | ||
throw new ChicoryException("We don't currently support more than 1 table"); | ||
for (var fi : el.getFuncIndices()) { | ||
table.addFuncRef((int) fi); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return new Instance( | ||
this, | ||
globalInitializers, | ||
|
@@ -151,7 +172,8 @@ public Instance instantiate(HostFunction[] hostFunctions) { | |
functions, | ||
types, | ||
functionTypes, | ||
hostFuncs); | ||
hostFuncs, | ||
table); | ||
} | ||
|
||
private HostFunction[] mapHostFunctions(Import[] imports, HostFunction[] hostFunctions) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,27 +41,27 @@ public void shouldWorkFactorial() { | |
var module = Module.build("src/test/resources/wasm/iterfact.wat.wasm"); | ||
var instance = module.instantiate(); | ||
var iterFact = instance.getExport("iterFact"); | ||
var result = iterFact.apply(Value.i32(5)); | ||
var result = iterFact.apply(Value.i32(5))[0]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a big deal for now, but I wonder if it makes sense to have another method similar to get export, or maybe something on ExportFunction that can make this a little cleaner if you know it only has 1 return. |
||
assertEquals(120, result.asInt()); | ||
} | ||
|
||
@Test | ||
public void shouldSupportBrTable() { | ||
var instance = Module.build("src/test/resources/wasm/br_table.wat.wasm").instantiate(); | ||
var switchLike = instance.getExport("switch_like"); | ||
var result = switchLike.apply(Value.i32(0)); | ||
var result = switchLike.apply(Value.i32(0))[0]; | ||
assertEquals(102, result.asInt()); | ||
result = switchLike.apply(Value.i32(1)); | ||
result = switchLike.apply(Value.i32(1))[0]; | ||
assertEquals(101, result.asInt()); | ||
result = switchLike.apply(Value.i32(2)); | ||
result = switchLike.apply(Value.i32(2))[0]; | ||
assertEquals(100, result.asInt()); | ||
result = switchLike.apply(Value.i32(-1)); | ||
result = switchLike.apply(Value.i32(-1))[0]; | ||
assertEquals(103, result.asInt()); | ||
result = switchLike.apply(Value.i32(3)); | ||
result = switchLike.apply(Value.i32(3))[0]; | ||
assertEquals(103, result.asInt()); | ||
result = switchLike.apply(Value.i32(4)); | ||
result = switchLike.apply(Value.i32(4))[0]; | ||
assertEquals(103, result.asInt()); | ||
result = switchLike.apply(Value.i32(100)); | ||
result = switchLike.apply(Value.i32(100))[0]; | ||
assertEquals(103, result.asInt()); | ||
} | ||
|
||
|
@@ -70,14 +70,14 @@ public void shouldExerciseBranches() { | |
var module = Module.build("src/test/resources/wasm/branching.wat.wasm").instantiate(); | ||
var foo = module.getExport("foo"); | ||
|
||
var result = foo.apply(Value.i32(0)); | ||
var result = foo.apply(Value.i32(0))[0]; | ||
assertEquals(42, result.asInt()); | ||
|
||
result = foo.apply(Value.i32(1)); | ||
result = foo.apply(Value.i32(1))[0]; | ||
assertEquals(99, result.asInt()); | ||
|
||
for (var i = 2; i < 100; i++) { | ||
result = foo.apply(Value.i32(i)); | ||
result = foo.apply(Value.i32(i))[0]; | ||
assertEquals(7, result.asInt()); | ||
} | ||
} | ||
|
@@ -113,7 +113,7 @@ public void shouldComputeFactorial() { | |
|
||
// don't make this too big we will overflow 32 bits | ||
for (var i = 0; i < 10; i++) { | ||
var result = iterFact.apply(Value.i32(i)); | ||
var result = iterFact.apply(Value.i32(i))[0]; | ||
// test against an oracle Java implementation | ||
assertEquals(factorial(i), result.asInt()); | ||
} | ||
|
@@ -159,7 +159,7 @@ public void shouldTrapOnUnreachable() { | |
public void shouldSupportGlobals() { | ||
var instance = Module.build("src/test/resources/wasm/globals.wat.wasm").instantiate(); | ||
var doit = instance.getExport("doit"); | ||
var result = doit.apply(Value.i32(32)); | ||
var result = doit.apply(Value.i32(32))[0]; | ||
assertEquals(42, result.asInt()); | ||
} | ||
|
||
|
@@ -172,19 +172,19 @@ public void shouldCountVowels() { | |
var memory = instance.getMemory(); | ||
var message = "Hello, World!"; | ||
var len = message.getBytes().length; | ||
var ptr = alloc.apply(Value.i32(len)).asInt(); | ||
var ptr = alloc.apply(Value.i32(len))[0].asInt(); | ||
memory.put(ptr, message); | ||
var result = countVowels.apply(Value.i32(ptr), Value.i32(len)); | ||
dealloc.apply(Value.i32(ptr), Value.i32(len)); | ||
assertEquals(3, result.asInt()); | ||
assertEquals(3, result[0].asInt()); | ||
} | ||
|
||
@Test | ||
public void shouldRunBasicCProgram() { | ||
// check with: wasmtime src/test/resources/wasm/basic.c.wasm --invoke run | ||
var instance = Module.build("src/test/resources/wasm/basic.c.wasm").instantiate(); | ||
var run = instance.getExport("run"); | ||
var result = run.apply(); | ||
var result = run.apply()[0]; | ||
assertEquals(42, result.asInt()); | ||
} | ||
|
||
|
@@ -210,25 +210,26 @@ public void shouldRunBasicCProgram() { | |
public void shouldWorkWithMemoryOps() { | ||
var instance = Module.build("src/test/resources/wasm/memory.wat.wasm").instantiate(); | ||
var run = instance.getExport("run32"); | ||
var result = run.apply(Value.i32(42)); | ||
var results = run.apply(Value.i32(42)); | ||
var result = results[0]; | ||
assertEquals(42, result.asInt()); | ||
|
||
result = run.apply(Value.i32(Integer.MAX_VALUE)); | ||
result = run.apply(Value.i32(Integer.MAX_VALUE))[0]; | ||
assertEquals(Integer.MAX_VALUE, result.asInt()); | ||
|
||
result = run.apply(Value.i32(Integer.MIN_VALUE)); | ||
result = run.apply(Value.i32(Integer.MIN_VALUE))[0]; | ||
assertEquals(Integer.MIN_VALUE, result.asInt()); | ||
|
||
run = instance.getExport("run64"); | ||
result = run.apply(Value.i64(42)); | ||
result = run.apply(Value.i64(42))[0]; | ||
assertEquals(42L, result.asLong()); | ||
|
||
run = instance.getExport("run64"); | ||
result = run.apply(Value.i64(Long.MIN_VALUE)); | ||
result = run.apply(Value.i64(Long.MIN_VALUE))[0]; | ||
assertEquals(Long.MIN_VALUE, result.asLong()); | ||
|
||
run = instance.getExport("run64"); | ||
result = run.apply(Value.i64(Long.MAX_VALUE)); | ||
result = run.apply(Value.i64(Long.MAX_VALUE))[0]; | ||
assertEquals(Long.MAX_VALUE, result.asLong()); | ||
} | ||
|
||
|
@@ -237,7 +238,7 @@ public void shouldRunKitchenSink() { | |
// check with: wasmtime src/test/resources/wasm/kitchensink.wat.wasm --invoke run 100 | ||
var instance = Module.build("src/test/resources/wasm/kitchensink.wat.wasm").instantiate(); | ||
var run = instance.getExport("run"); | ||
assertEquals(6, run.apply(Value.i32(100)).asInt()); | ||
assertEquals(6, run.apply(Value.i32(100))[0].asInt()); | ||
} | ||
|
||
// @Test | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIce!