Commit 23932cb1 authored by Kunshan Wang's avatar Kunshan Wang

WIP: Tested id_of and name_of.

parent 325749b4
......@@ -101,15 +101,21 @@ object ClientAccessibleClassExposer {
val ptr = NativeClientSupport.stringPool.getOrElseUpdate(str, {
val bytes = str.getBytes(StandardCharsets.US_ASCII)
val newPtr = jnrMemoryManager.allocateDirect(bytes.size + 1)
newPtr.put(0L, bytes, 0, 0)
newPtr.put(0L, bytes, 0, bytes.length)
newPtr.putByte(bytes.length, 0)
newPtr
})
ptr.address()
}
private def getObj[T](buffer: Buffer, index: Int, orm: ObjectReferenceManager[T]): T = {
val addr = buffer.getLong(index)
orm.get(jnrMemoryManager.newPointer(addr))
val arg0 = buffer.getLong(index)
val objAddr = theMemory.getAddress(arg0)
val obj = orm.get(jnrMemoryManager.newPointer(objAddr))
if (obj == null) {
throw new UvmRefImplException("Exposed object not found. Address: %d 0x%x".format(objAddr, objAddr))
}
obj
}
// Reflection utils.
......
......@@ -13,31 +13,37 @@ class NativeClientSupportTest extends UvmBundleTesterBase {
"uvm.refimpl.nat" -> DEBUG)
preloadBundles("tests/uvm-refimpl-test/primitives.uir")
val fileName = "./tests/c-snippets/ncs_tests.so"
if (!new java.io.File(fileName).isFile()) {
throw new RuntimeException("Need to compile the ncs_tests.so library. cd into tests/c-snippets and invoke 'make'.")
}
trait NcsTestsLib {
def test_basic(mvm: Word): Unit
def test_basic(mvm: Word, theID: Int, theName: String): Int
}
val ncs_tests = LibraryLoader.create(classOf[NcsTestsLib]).load(fileName)
"The ClientAccessibleClassExposer" should "enumerate declared methods" in {
val cace = new ClientAccessibleClassExposer(NativeMuVM)
val microVMAddr = NativeClientSupport.exposeMicroVM(microVM)
val funcTable = cace.makeFuncTable(microVMAddr)
val funcTablePtr = NativeClientSupport.funcTableToPointer(funcTable)
val header = funcTablePtr.getAddress(0)
val mvm =NativeClientSupport.microVMs.get(jnrMemoryManager.newPointer(header))
val mvm = NativeClientSupport.microVMs.get(jnrMemoryManager.newPointer(header))
mvm shouldBe microVM
ncs_tests.test_basic(funcTable)
val theName = "@i64"
val theID = microVM.idOf(theName)
val result = ncs_tests.test_basic(funcTable, theID, theName)
if (result == 0) {
fail("Failed in the native program.")
}
}
}
\ No newline at end of file
#include <stdio.h>
#include <string.h>
#include <muapi.h>
void test_basic(MuVM *mvm) {
int test_basic(MuVM *mvm, int theID, char *theName) {
printf("[C] header: %p\n", mvm->header);
printf("[C] new_context: %p\n", mvm->new_context);
printf("[C] id_of: %p\n", mvm->id_of);
printf("[C] name_of: %p\n", mvm->name_of);
printf("[C] set_trap_handler: %p\n", mvm->set_trap_handler);
int id = mvm->id_of(mvm, theName);
printf("[C] id = %d\n", id);
if (id != theID) {
printf("[C] ID %d is not equal to %d\n", id, theID);
return 0;
}
char *name = mvm->name_of(mvm, theID);
printf("[C] name = %s\n", name);
if (strcmp(name, theName) != 0) {
printf("[C] name %s is not equal to %s\n", name, theName);
return 0;
}
return 1;
}
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