Skip to content

Commit

Permalink
Fix functionTypes indexes of imported functions (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
illarionov authored Feb 10, 2024
1 parent 0e52356 commit 75c55b4
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 20 deletions.
28 changes: 9 additions & 19 deletions runtime/src/main/java/com/dylibso/chicory/runtime/Machine.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,28 +61,18 @@ public static Value[] call(
eval(stack, instance, callStack);
} else {
callStack.push(new StackFrame(instance, funcId, args, List.of()));
var imprt = instance.imports().index()[funcId];
var imprt = instance.imports().function(funcId);
if (imprt == null) {
throw new ChicoryException("Missing host import, number: " + funcId);
}

switch (imprt.type()) {
case FUNCTION:
var hostFunc = ((HostFunction) imprt).handle();
var results = hostFunc.apply(instance, args);
// a host function can return null or an array of ints
// which we will push onto the stack
if (results != null) {
for (var result : results) {
stack.push(result);
}
}
break;
case GLOBAL:
stack.push(((HostGlobal) imprt).value());
break;
default:
throw new ChicoryException("Not implemented");
var hostFunc = imprt.handle();
var results = hostFunc.apply(instance, args);
// a host function can return null or an array of ints
// which we will push onto the stack
if (results != null) {
for (var result : results) {
stack.push(result);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public Instance instantiate(HostImports hostImports) {
case FUNCTION:
{
var type = ((FunctionImport) imprt).typeIndex();
functionTypes[importId] = type;
functionTypes[funcIdx] = type;
// The global function id increases on this table
// function ids are assigned on imports first
imports[importId++] = imprt;
Expand Down
44 changes: 44 additions & 0 deletions runtime/src/test/java/com/dylibso/chicory/runtime/ModuleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.dylibso.chicory.runtime.exceptions.WASMMachineException;
import com.dylibso.chicory.wasm.types.MemoryLimits;
import com.dylibso.chicory.wasm.types.Value;
import com.dylibso.chicory.wasm.types.ValueType;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.jupiter.api.Test;

public class ModuleTest {
Expand Down Expand Up @@ -248,4 +250,46 @@ public void shouldRunKitchenSink() {
// var run = instance.getExport("run");
// assertEquals(-25438, run.apply(Value.i32(100)).asInt());
// }

@Test
public void shouldRunMixedImports() {
var cbrtFunc =
new HostFunction(
(Instance instance, Value... args) -> {
var x = args[0].asInt();
var cbrt = Math.cbrt(x);
return new Value[] {Value.fromDouble(cbrt)};
},
"env",
"cbrt",
List.of(ValueType.I32),
List.of(ValueType.F64));
var logResult = new AtomicReference<String>(null);
var logFunc =
new HostFunction(
(Instance instance, Value... args) -> {
var logLevel = args[0].asInt();
var value = (int) args[1].asDouble();
logResult.set(logLevel + ": " + value);
return null;
},
"env",
"log",
List.of(ValueType.I32, ValueType.F64),
List.of());
var memory = new HostMemory("env", "memory", new Memory(new MemoryLimits(1)));

var module = Module.builder("compiled/mixed-imports.wat.wasm").build();
var hostImports =
new HostImports(
new HostFunction[] {cbrtFunc, logFunc},
new HostGlobal[0],
memory,
new HostTable[0]);
var instance = module.instantiate(hostImports);

var run = instance.export("main");
run.apply();
assertEquals("1: 164", logResult.get());
}
}
Binary file not shown.
35 changes: 35 additions & 0 deletions wasm-corpus/src/test/resources/wat/mixed-imports.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
(module
(type (;0;) (func (param i32) (result f64)))
(type (;1;) (func (param f64) (result f64)))
(type (;2;) (func (param i32 f64)))
(type (;3;) (func (param f64 f64) (result f64)))
(type (;4;) (func))

(import "env" "memory" (memory 1))
(import "env" "cbrt" (func $cbrt (type 0)))
(import "env" "log" (func $log (type 2)))

(func $pow2 (type 1) (param f64) (result f64)
local.get 0
local.get 0
f64.mul
)
(func $add (type 3) (param f64 f64) (result f64)
local.get 0
local.get 1
f64.add
)
(func $main (type 4)
i32.const 1
i32.const 512
call $cbrt
call $pow2
f64.const 100.0
call $add
call $log
)

(export "pow2" (func $pow2))
(export "main" (func $main))
)

0 comments on commit 75c55b4

Please sign in to comment.