Add result handling for function selection.

This commit is contained in:
Jacques Wagener 2019-06-19 13:09:29 +02:00
parent 0c6fb857c0
commit 732d34ece4
No known key found for this signature in database
GPG Key ID: C294D1025DA0E923
3 changed files with 79 additions and 8 deletions

View File

@ -22,6 +22,7 @@ type
constant*: bool constant*: bool
payable*: bool payable*: bool
method_id*: string method_id*: string
method_sig*: string
proc generate_method_sig*(func_sig: FunctionSignature, v2_sig: bool = false): string = proc generate_method_sig*(func_sig: FunctionSignature, v2_sig: bool = false): string =
@ -82,14 +83,14 @@ proc generate_function_signature*(proc_def: NimNode): FunctionSignature =
discard discard
# raise newException(Exception, "unknown func type" & treeRepr(child)) # raise newException(Exception, "unknown func type" & treeRepr(child))
echo "method_sig: " & generate_method_sig(func_sig) # echo "method_sig: " & generate_method_sig(func_sig)
# var s = newSeq[byte]() # var s = newSeq[byte]()
# var method_hash = generate_method_id(func_sig) # var method_hash = generate_method_id(func_sig)
# for i in method_hash: # for i in method_hash:
# s.add(i) # s.add(i)
# echo "method_hash" & toHex(method_hash) # echo "method_hash" & toHex(method_hash)
func_sig.method_sig = generate_method_sig(func_sig)
func_sig.method_id = generate_method_id(func_sig) func_sig.method_id = generate_method_id(func_sig)
return func_sig return func_sig

View File

@ -128,7 +128,7 @@ proc handleContractInterface(stmts: NimNode): NimNode =
) )
) )
call_and_copy_block.add( call_and_copy_block.add(
nnkLetSection.newTree( # let c: uint256 = Uint256.fromBytesBE(b) nnkLetSection.newTree( # let c: uint256 = Uint256.fromBytesBE(b), TODO: handle different types.
nnkIdentDefs.newTree( nnkIdentDefs.newTree(
newIdentNode(tmp_var_converted), newIdentNode(tmp_var_converted),
newIdentNode(param.var_type), newIdentNode(param.var_type),
@ -144,12 +144,71 @@ proc handleContractInterface(stmts: NimNode): NimNode =
) )
call_to_func.add(newIdentNode(tmp_var_converted)) call_to_func.add(newIdentNode(tmp_var_converted))
start_offset += static_param_size start_offset += static_param_size
# Add final function call.
call_and_copy_block.add(call_to_func)
# echo "⬇️⬇️⬇️⬇️⬇️" # Handle returned data from function.
# echo treeRepr(call_and_copy_block) if len(func_sig.outputs) == 0:
# echo "⬆️⬆️⬆️⬆️⬆️" # Add final function call.
call_and_copy_block.add(call_to_func)
call_and_copy_block.add(
nnkStmtList.newTree(
nnkCall.newTree(
newIdentNode("finish"),
newNilLit(),
newLit(0)
)
)
)
elif len(func_sig.outputs) == 1:
var assign_result_block = nnkAsgn.newTree()
var param = func_sig.outputs[0]
var idx = 0
# create placeholder variables
var tmp_var_res_name = fmt"{func_sig.name}_result_{idx}"
var tmp_var_res_name_array = tmp_var_res_name & "_arr"
call_and_copy_block.add(
nnkVarSection.newTree(
nnkIdentDefs.newTree(
nnkPragmaExpr.newTree(
newIdentNode(tmp_var_res_name),
nnkPragma.newTree(
newIdentNode("noinit")
)
),
newIdentNode(param.var_type),
newEmptyNode()
)
)
)
assign_result_block.add(newIdentNode(tmp_var_res_name))
assign_result_block.add(call_to_func)
call_and_copy_block.add(assign_result_block)
call_and_copy_block.add(
nnkVarSection.newTree(
nnkIdentDefs.newTree(
newIdentNode(tmp_var_res_name_array),
newEmptyNode(),
nnkDotExpr.newTree(
newIdentNode(tmp_var_res_name),
newIdentNode("toByteArrayBE")
)
)
)
)
call_and_copy_block.add(
nnkCall.newTree(
newIdentNode("finish"),
nnkCommand.newTree(
newIdentNode("addr"),
newIdentNode(tmp_var_res_name_array)
),
newLit(get_byte_size_of(param.var_type))
)
)
else:
raise newException(
Exception,
"Can only handle function with a single variable output ATM."
)
selector_CaseStmt.add( selector_CaseStmt.add(
nnkOfBranch.newTree( # of 0x<>'u32: nnkOfBranch.newTree( # of 0x<>'u32:

View File

@ -26,11 +26,22 @@ expandMacros:
# discard # discard
# else: # else:
# revert(nil, 0) # revert(nil, 0)
# dumpAstGen:
# var res {.noinit.}: uint256
dumpAstGen:
var res = hello(a)
var res_a = res.toByteArrayBE
finish(addr res_a, 256)
# finish(nil, 0)
return (123).stuint(256) return (123).stuint(256)
proc world(a: uint256, b: uint256): uint256 {.discardable.} = proc world(a: uint256, b: uint256): uint256 {.discardable.} =
return (456).stuint(256) return (456).stuint(256)
proc do_nothing(a: uint256, b: uint256) =
discard
# func addition(a: uint256, b: uint256): uint256 = # func addition(a: uint256, b: uint256): uint256 =
# return a + b # return a + b