Closed ZhaoTzuHsien closed 1 day ago
@ChuWeiChang
Please describe how you want to implement this feature.
block.execute serves as a "little host", which parse the input data -> feed into "handle" and get the return data -> turn the data into json -> output the json i am currently thinking if some checks are needed
Can you describe more about what types of checks are needed? If they are not related to the parsing scope, feel free to open a new issue.
I am thinking about checking if return values' type consistent with the annotation, since Cpython interpreter dont check it.
ex:
def add(x: int, y: int) -> int:
return "ss"
b = add(2,3)
print(b)
this program runs somehow But i am not sure if it's turned to wasm, same thing could happen or not I will make some tests to see if it's handled automatically, and add the checking codes if neccessary
Be aware that CPythom doesn't validate the annotation of parameters either. It's good to enforce type-checking when data passes the boundary between JSON and Python types.
WIT definition
You can always refer to
wit/block.wit
for latest definition.The type of
payload
in WITI changed the type of
Payload
tolist<u8>
in WIT in order to support different encoding methods in the future, such as Protocol Buffers or Cap'n Proto. In Python's perspective,list<u8>
is equivalent tobytes
.Payload
formatCurrently, we decide to use JSON as the format of
Payload
for its simplicity. It models as an JSON array with each of its items being a valid JSON value for user-defined parameters. Here is an example of a payload:When receiving the above payload, we know there are 3 parameters for user-defined
handle
function:Return format
Since Python supports tuple return, we collect each item in the returned tuple and combine them into a JSON array.
For example, if the user define the following Python code:
The library should parse the data into the following JSON value and convert it to bytes:
Be aware that if the user raise an exception in the
handle
function, theexecute
function should also raise the exception in order to notify the host that there is an error in the block.