Commit e7b1b1df authored by Kunshan Wang's avatar Kunshan Wang

WIP: Fixed the simplest test.

parent 3f6a1cd1
......@@ -30,7 +30,7 @@ object NativeCallHelper {
/**
* Helps calling native functions and supports callbacks from native. Based on JFFI.
*/
class NativeCallHelper(implicit microVM: MicroVM) {
class NativeCallHelper {
import NativeCallHelper._
/** A mapping of Mu types to JFFI types. Cached for struct types. */
......@@ -76,6 +76,17 @@ class NativeCallHelper(implicit microVM: MicroVM) {
* Map each address of closure handle to the DynExpFunc record so that the closure handle can be disposed.
*/
val exposedFuncs = new HashMap[Word, DynExpFunc]()
/**
* The current NativeStackKeeper instance that makes the native call.
* <p>
* It is set just before entering native, by calling a native function, or returning to a native function which called
* back to Mu.
* <p>
* It is cleared after returning to JVM, either when returning from a native function, or when the native calls back
* to Mu (to JVM).
*/
val currentNativeStackKeeper = new ThreadLocal[NativeStackKeeper]()
/**
* Call a native function. Must be called by a NativeStackKeeper.Slave thread.
......@@ -88,6 +99,8 @@ class NativeCallHelper(implicit microVM: MicroVM) {
for ((mty, vb) <- (sig.paramTy zip args)) {
putArg(hib, mty, vb)
}
currentNativeStackKeeper.set(nsk)
val inv = Invoker.getInstance
......
......@@ -2,13 +2,11 @@ package uvm.refimpl.nat
import org.scalatest.FlatSpec
import org.scalatest.Matchers
import com.kenai.jffi.CallingConvention
import com.kenai.jffi.Closure
import com.kenai.jffi.Closure.Buffer
import com.kenai.jffi.ClosureManager
import com.kenai.jffi.{ Type => JType }
import uvm.FuncSig
import uvm.refimpl.itpr.BoxDouble
import uvm.refimpl.itpr.BoxInt
......@@ -16,14 +14,15 @@ import uvm.refimpl.itpr.BoxPointer
import uvm.types.TypeDouble
import uvm.types.TypeFuncPtr
import uvm.types.TypeInt
import uvm.refimpl.MicroVM
class NativeStackKeeperTest extends FlatSpec with Matchers {
behavior of "NativeStackKeeper"
implicit val nh = new NativeHelper()
val lib = NativeLibraryTestHelper.loadTestLibrary("callbacktest")
implicit val nch = new NativeCallHelper()
def autoClose[T](nsk: NativeStackKeeper)(f: => T) = {
try {
f
......@@ -62,6 +61,8 @@ class NativeStackKeeperTest extends FlatSpec with Matchers {
it should "handle one-level callback" in {
val addr = lib.getSymbolAddress("one_level")
addr should not be 0
val nsk = new NativeStackKeeper()
......@@ -83,13 +84,17 @@ class NativeStackKeeperTest extends FlatSpec with Matchers {
val closHandle = ClosureManager.getInstance.newClosure(clos, JType.DOUBLE, Array(JType.DOUBLE, JType.POINTER), CallingConvention.DEFAULT)
closHandle.getAddress should not be 0
val b1 = BoxDouble(3.0)
val b2 = BoxPointer(0)
val br = BoxInt(-1)
val b2 = BoxPointer(closHandle.getAddress)
val br = BoxDouble(-1.0)
nsk.callNative(sig, addr, Seq(b1, b2), br)
br.value shouldBe 7
closHandle.dispose()
br.value shouldBe 25.0
}
}
}
\ No newline at end of file
......@@ -4,11 +4,11 @@ int add(int a, int b) {
return a + b;
}
double (*DoubleToDouble)(double value);
typedef double (*DoubleToDouble)(double value);
double one_level(double v1, DoubleToDouble cb) {
double v2, v3;
v2 = cb(v1+1.0);
v3 = cb(v1+2.0);
v2 = cb(v1);
v3 = cb(v1+1.0);
return v2 + v3;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment