cartesi / machine-emulator

The off-chain implementation of the Cartesi Machine
GNU Lesser General Public License v3.0
58 stars 32 forks source link

Simplify the C API #183

Open edubart opened 5 months ago

edubart commented 5 months ago

Context

Our C API is currently being used to create machine bindings in Lua, Rust, Go and TypeScript languages. Since this, we noticed there are some pain points when creating C bindings in those languages, we could make a refactor to improve it, make it smaller and simpler.

Subtasks

tuler commented 4 months ago

Those changes would help as well with the Node.js binding I'm working on. I have ~400 lines (probably badly written) only to handle config.

tuler commented 4 months ago

Side node: cm_reset_uarch should be called cm_reset_uarch_halt_flag, for consistency with other reset methods.

diegonehab commented 4 months ago

That’s not what it does. It really resets the entirety of uarch. Including RAM and all other registers.

tuler commented 4 months ago

That’s not what it does. It really resets the entirety of uarch. Including RAM and all other registers.

So documentation should be fixed. https://github.com/cartesi/machine-emulator/blob/77fb1736e280720921292f9175f0a0217b9804cf/src/machine-c-api.h#L1845

diegonehab commented 4 months ago

@mpernambuco can you take a look?

mpernambuco commented 4 months ago

@mpernambuco can you take a look?

https://github.com/cartesi/machine-emulator/pull/211

GCdePaula commented 3 months ago

I wanted to relate our experience on creating Rust binding.

I was initially confused me about the machine config. I think it's because there are instances of it created and managed by us (like in the create function), and there are instances of it created and managed by the emulator. It was easy to deal with the one created and managed by the emulator. I had no problem accessing the fields. No problem freeing memory either, we just need to wrap it in another Rust object and implement Drop correctly.

On using json for machine config. For creating a machine, I think that may help a lot. For querying the machine about the initial config, I think it would not be super ergonomic, but it would be easy, and it may be the best solution

I have a feeling most users will be after the same few fields in the machine config (like the address and length of the tx/rx buffer). Maybe dedicated methods to read commonly needed fields could help?

Or maybe specific functions for common operations, like clearing rx/tx buffer. Or writing an input (setting all the correct registers and buffers). Or reading an input (parsing all the correct registers). I was copying these operations from cartesi-machine.lua. Having them on the API would be more ergonomic, but I don't know if it makes sense. It could give you more freedom to change things though.