Commit c91c84c7 authored by Kunshan Wang's avatar Kunshan Wang

handle return values

parent 7fe83fed
...@@ -373,9 +373,9 @@ struct MuCtx { ...@@ -373,9 +373,9 @@ struct MuCtx {
void (*push_frame )(MuCtx *ctx, MuStackRefValue stack, MuFuncRefValue func); void (*push_frame )(MuCtx *ctx, MuStackRefValue stack, MuFuncRefValue func);
// 64-bit tagged reference operations // 64-bit tagged reference operations
int (*tr64_is_fp )(MuCtx *ctx, MuTagRef64Value value); int (*tr64_is_fp )(MuCtx *ctx, MuTagRef64Value value); /// MUAPIPARSER RV:bool
int (*tr64_is_int )(MuCtx *ctx, MuTagRef64Value value); int (*tr64_is_int )(MuCtx *ctx, MuTagRef64Value value); /// MUAPIPARSER RV:bool
int (*tr64_is_ref )(MuCtx *ctx, MuTagRef64Value value); int (*tr64_is_ref )(MuCtx *ctx, MuTagRef64Value value); /// MUAPIPARSER RV:bool
MuDoubleValue (*tr64_to_fp )(MuCtx *ctx, MuTagRef64Value value); MuDoubleValue (*tr64_to_fp )(MuCtx *ctx, MuTagRef64Value value);
MuIntValue (*tr64_to_int )(MuCtx *ctx, MuTagRef64Value value); MuIntValue (*tr64_to_int )(MuCtx *ctx, MuTagRef64Value value);
MuRefValue (*tr64_to_ref )(MuCtx *ctx, MuTagRef64Value value); MuRefValue (*tr64_to_ref )(MuCtx *ctx, MuTagRef64Value value);
......
...@@ -63,23 +63,33 @@ def type_is_ptr(ty): ...@@ -63,23 +63,33 @@ def type_is_ptr(ty):
def type_is_handle_array(ty): def type_is_handle_array(ty):
return type_is_ptr(ty) and type_is_handle(ty[:-1]) return type_is_ptr(ty) and type_is_handle(ty[:-1])
def to_jffi_ty(cty): def to_jffi_ty(raw_type):
if cty in _primitive_types: if raw_type in _primitive_types:
jty = _primitive_types[cty][1] jty = _primitive_types[raw_type][1]
elif type_is_ptr(cty): elif type_is_ptr(raw_type):
jty = "POINTER" jty = "POINTER"
else: else:
raise ValueError("No JFFI JType: " + cty) raise ValueError("No JFFI JType: " + raw_type)
return "JType." + jty return "JType." + jty
def to_jffi_getter(cty): def to_jffi_getter(raw_type):
if cty in _primitive_types: if raw_type in _primitive_types:
getter = _primitive_types[cty][2] getter = _primitive_types[raw_type][2]
elif type_is_ptr(cty): elif type_is_ptr(raw_type):
getter = "getAddress" getter = "getAddress"
else: else:
raise ValueError("No JFFI Buffer getter: " + cty) raise ValueError("No JFFI Buffer getter: " + raw_type)
return getter
def to_jffi_setter(raw_type):
if raw_type in _primitive_types:
getter = _primitive_types[raw_type][3]
elif type_is_ptr(raw_type):
getter = "setAddressReturn"
else:
raise ValueError("No JFFI Buffer getter: " + raw_type)
return getter return getter
...@@ -174,6 +184,12 @@ _special_converters = { ...@@ -174,6 +184,12 @@ _special_converters = {
"MuDestKind" : "toDestKind", "MuDestKind" : "toDestKind",
} }
_special_return_converters = {
"MuName" : "exposeString",
"MuCtx*" : "exposeMuCtx",
"MuVM*" : "exposeMicroVM",
}
def param_converter(pn, pt, rn, rt, is_optional, array_sz, is_bool, is_out): def param_converter(pn, pt, rn, rt, is_optional, array_sz, is_bool, is_out):
if pt == "void": if pt == "void":
raise ValueError("Parameter cannot be void. Param name: {}".format(pn)) raise ValueError("Parameter cannot be void. Param name: {}".format(pn))
...@@ -293,6 +309,27 @@ def generate_method(typedefs, strname, meth) -> Tuple[str, str]: ...@@ -293,6 +309,27 @@ def generate_method(typedefs, strname, meth) -> Tuple[str, str]:
# return value # return value
if ret_ty != "void":
raw_ret_ty = to_basic_type(typedefs, ret_ty)
jffi_setter = to_jffi_setter(raw_ret_ty)
if type_is_handle(ret_ty):
assert(strname == "MuCtx")
assert(jffi_setter == "setAddressReturn")
stmts.append("val _RV_FAK = exposeMuValue({}, _RV)".format(
self_param_name))
stmts.append("_jffiBuffer.{}(_RV_FAK)".format(jffi_setter))
elif ret_ty in _special_return_converters:
assert(jffi_setter == "setAddressReturn")
stmts.append("val _RV_FAK = {}(_RV)".format(
_special_return_converters[ret_ty]))
stmts.append("_jffiBuffer.{}(_RV_FAK)".format(jffi_setter))
elif ("RV", "bool") in pragmas:
stmts.append("_jffiBuffer.{}(if(_RV) 1 else 0)".format(jffi_setter))
else:
stmts.append("_jffiBuffer.{}(_RV)".format(jffi_setter))
footer = "}" footer = "}"
return (valname, "\n".join([header] + stmts + [footer])) return (valname, "\n".join([header] + stmts + [footer]))
......
This diff is collapsed.
...@@ -767,7 +767,7 @@ object ClientAccessibleClassExposer { ...@@ -767,7 +767,7 @@ object ClientAccessibleClassExposer {
def retLong(buffer: Buffer, v: Any): Unit = buffer.setLongReturn(v.asInstanceOf[Long]) def retLong(buffer: Buffer, v: Any): Unit = buffer.setLongReturn(v.asInstanceOf[Long])
def retFloat(buffer: Buffer, v: Any): Unit = buffer.setFloatReturn(v.asInstanceOf[Float]) def retFloat(buffer: Buffer, v: Any): Unit = buffer.setFloatReturn(v.asInstanceOf[Float])
def retDouble(buffer: Buffer, v: Any): Unit = buffer.setDoubleReturn(v.asInstanceOf[Double]) def retDouble(buffer: Buffer, v: Any): Unit = buffer.setDoubleReturn(v.asInstanceOf[Double])
def retString(buffer: Buffer, v: Any): Unit = buffer.setLongReturn(exposeStr(v.asInstanceOf[String])) def retString(buffer: Buffer, v: Any): Unit = buffer.setLongReturn(exposeString(v.asInstanceOf[String]))
private def getStr(buffer: Buffer, index: Int): String = { private def getStr(buffer: Buffer, index: Int): String = {
val addr = buffer.getAddress(index) val addr = buffer.getAddress(index)
...@@ -789,17 +789,6 @@ object ClientAccessibleClassExposer { ...@@ -789,17 +789,6 @@ object ClientAccessibleClassExposer {
} }
} }
private def exposeStr(str: String): Word = {
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, bytes.length)
newPtr.putByte(bytes.length, 0)
newPtr
})
ptr.address()
}
private def getObjFromFuncTableAddr[T](buffer: Buffer, index: Int, orm: ObjectReferenceManager[T]): T = { private def getObjFromFuncTableAddr[T](buffer: Buffer, index: Int, orm: ObjectReferenceManager[T]): T = {
val funcTableAddr = buffer.getLong(index) val funcTableAddr = buffer.getLong(index)
NativeClientSupport.getObjFromFuncTableAddrNotNull(funcTableAddr, orm) NativeClientSupport.getObjFromFuncTableAddrNotNull(funcTableAddr, orm)
...@@ -1051,6 +1040,17 @@ object NativeClientSupport { ...@@ -1051,6 +1040,17 @@ object NativeClientSupport {
// Expose and unexpose objects // Expose and unexpose objects
def exposeString(str: String): Word = {
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, bytes.length)
newPtr.putByte(bytes.length, 0)
newPtr
})
ptr.address()
}
/** Expose a MicroVM. Return a pointer to the C MuVM structure. */ /** Expose a MicroVM. Return a pointer to the C MuVM structure. */
def exposeMicroVM(microVM: MicroVM): FuncTablePtr = { def exposeMicroVM(microVM: MicroVM): FuncTablePtr = {
val objFak = microVMs.add(microVM).address() val objFak = microVMs.add(microVM).address()
......
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