Skip to content
This repository was archived by the owner on Aug 14, 2023. It is now read-only.

Commit bcddf22

Browse files
fine-tuning the C API (#75)
1 parent c8aaa6f commit bcddf22

10 files changed

Lines changed: 366 additions & 149 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use crate::{impl_from_svm_byte_array, impl_into_svm_byte_array};
2+
3+
use svm_common::Address;
4+
5+
///
6+
/// # Example
7+
///
8+
/// ```rust
9+
/// use svm_common::Address;
10+
/// use svm_runtime_c_api::svm_byte_array;
11+
///
12+
/// let bytes: svm_byte_array = Address:of("@someone").into();
13+
///
14+
/// let addr: Result<Address, String> = bytes.into();
15+
/// assert_eq!(Address::of("@someone"), addr.unwrap());
16+
/// ```
17+
///
18+
impl_from_svm_byte_array!(Address);
19+
impl_into_svm_byte_array!(Address);

crates/svm-runtime-c-api/src/api.rs

Lines changed: 66 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ macro_rules! to_svm_byte_array {
4343
($raw_byte_array:expr, $ptr:expr, $length:expr) => {{
4444
use crate::svm_byte_array;
4545

46-
let byte_array: &mut svm_byte_array = &mut *$raw_byte_array;
47-
byte_array.bytes = $ptr;
48-
byte_array.length = $length as u32;
46+
let bytes: &mut svm_byte_array = &mut *$raw_byte_array;
47+
bytes.bytes = $ptr;
48+
bytes.length = $length as u32;
4949
}};
5050
}
5151

@@ -86,14 +86,13 @@ pub unsafe extern "C" fn svm_imports_alloc(imports: *mut *mut c_void, count: u32
8686
/// // ...
8787
/// }
8888
///
89-
/// let count = 1;
90-
/// let mut imports = std::ptr::null_mut();
91-
/// let _res = unsafe { svm_imports_alloc(&mut imports, count) };
89+
/// // allocate one imports
90+
/// let mut imports = testing::imports_alloc(1);
9291
///
93-
/// let module_name = testing::str_to_svm_byte_array("env");
94-
/// let import_name = testing::str_to_svm_byte_array("foo");
95-
/// let params = testing::svm_value_type_vec_to_array(&vec![]);
96-
/// let returns = testing::svm_value_type_vec_to_array(&vec![]);
92+
/// let module_name = "env".into();
93+
/// let import_name = "foo".into();
94+
/// let params = vec![].into();
95+
/// let returns = vec![].into();
9796
/// let func = foo as *const c_void;
9897
///
9998
/// let res = unsafe { svm_import_func_build(imports, module_name, import_name, func, params, returns) };
@@ -115,7 +114,6 @@ pub unsafe extern "C" fn svm_import_func_build(
115114
assert!(imports.len() < imports.capacity());
116115

117116
let func = NonNull::new(func as *mut c_void);
118-
119117
if func.is_none() {
120118
todo!();
121119
// return svm_result_t::SVM_FAILURE;
@@ -162,13 +160,11 @@ pub unsafe extern "C" fn svm_import_func_build(
162160
/// ```rust, no_run
163161
/// use svm_runtime_c_api::{svm_runtime_create, svm_imports_alloc, testing};
164162
///
165-
/// let count = 0;
166-
/// let mut imports = std::ptr::null_mut();
167-
/// let _res = unsafe { svm_imports_alloc(&mut imports, count) };
168-
///
169163
/// let mut runtime = std::ptr::null_mut();
170-
/// let path = testing::str_to_svm_byte_array("path goes here");
164+
/// let path = "path goes here".into();
171165
/// let host = std::ptr::null_mut();
166+
/// let mut imports = testing::imports_alloc(0);
167+
///
172168
/// let res = unsafe { svm_runtime_create(&mut runtime, path, host, imports) };
173169
/// assert!(res.is_ok());
174170
/// ```
@@ -219,9 +215,7 @@ pub unsafe extern "C" fn svm_runtime_create(
219215
/// use svm_common::Address;
220216
///
221217
/// // allocate imports
222-
/// let count = 0;
223-
/// let mut imports = std::ptr::null_mut();
224-
/// let _res = unsafe { svm_imports_alloc(&mut imports, count) };
218+
/// let mut imports = testing::imports_alloc(0);
225219
///
226220
/// // create runtime
227221
/// let mut kv = std::ptr::null_mut();
@@ -232,10 +226,10 @@ pub unsafe extern "C" fn svm_runtime_create(
232226
///
233227
/// // deploy template
234228
/// let mut template_addr = svm_byte_array::default();
235-
/// let author = Address::of("@author");
229+
/// let author: svm_byte_array = Address::of("@author").into();
236230
/// let host_ctx = svm_byte_array::default();
237-
/// let template = svm_byte_array::default();
238-
/// let res = unsafe { svm_deploy_template(&mut template_addr, runtime, author.as_ptr() as _, host_ctx, template) };
231+
/// let template = vec![0x0C, 0x00, 0x0D, 0x0E].into();
232+
/// let res = unsafe { svm_deploy_template(&mut template_addr, runtime, author, host_ctx, template) };
239233
/// assert!(res.is_ok());
240234
/// ```
241235
///
@@ -244,23 +238,29 @@ pub unsafe extern "C" fn svm_runtime_create(
244238
pub unsafe extern "C" fn svm_deploy_template(
245239
template_addr: *mut svm_byte_array,
246240
runtime: *mut c_void,
247-
author: *const c_void,
241+
author: svm_byte_array,
248242
host_ctx: svm_byte_array,
249243
template: svm_byte_array,
250244
) -> svm_result_t {
251245
debug!("`svm_deploy_template` start`");
252246

253247
let runtime = helpers::cast_to_runtime_mut(runtime);
254-
let author = Address::from(author);
255-
let host_ctx = HostCtx::from_raw_parts(host_ctx.bytes, host_ctx.length);
256-
let bytes = std::slice::from_raw_parts(template.bytes, template.length as usize);
248+
let author: Result<Address, String> = author.into();
249+
250+
if let Err(msg) = author {
251+
todo!()
252+
// return svm_result_t::SVM_FAILURE;
253+
}
257254

255+
let host_ctx = HostCtx::from_raw_parts(host_ctx.bytes, host_ctx.length);
258256
if host_ctx.is_err() {
259257
todo!();
260258
// return svm_result_t::SVM_FAILURE;
261259
}
262260

263-
match runtime.deploy_template(&author, host_ctx.unwrap(), bytes) {
261+
let bytes = std::slice::from_raw_parts(template.bytes, template.length as usize);
262+
263+
match runtime.deploy_template(&author.unwrap(), host_ctx.unwrap(), bytes) {
264264
Ok(addr) => {
265265
// returning deployed `AppTemplate` as `svm_byte_array`
266266
// client should call later `svm_address_destroy`
@@ -287,9 +287,7 @@ pub unsafe extern "C" fn svm_deploy_template(
287287
/// use svm_common::Address;
288288
///
289289
/// // allocate imports
290-
/// let count = 0;
291-
/// let mut imports = std::ptr::null_mut();
292-
/// let _res = unsafe { svm_imports_alloc(&mut imports, count) };
290+
/// let mut imports = testing::imports_alloc(0);
293291
///
294292
/// // create runtime
295293
/// let mut kv = std::ptr::null_mut();
@@ -300,12 +298,12 @@ pub unsafe extern "C" fn svm_deploy_template(
300298
///
301299
/// let mut app_addr = svm_byte_array::default();
302300
/// let mut init_state = svm_byte_array::default();
303-
/// let creator = Address::of("@creator");
301+
/// let creator = Address::of("@creator").into();
304302
/// let mut init_state = svm_byte_array::default();
305303
/// let host_ctx = svm_byte_array::default();
306304
/// let app = svm_byte_array::default();
307305
///
308-
/// let _res = unsafe { svm_spawn_app(&mut app_addr, &mut init_state, runtime, creator.as_ptr() as _, host_ctx, app) };
306+
/// let _res = unsafe { svm_spawn_app(&mut app_addr, &mut init_state, runtime, creator, host_ctx, app) };
309307
/// ```
310308
///
311309
#[must_use]
@@ -314,24 +312,29 @@ pub unsafe extern "C" fn svm_spawn_app(
314312
app_addr: *mut svm_byte_array,
315313
init_state: *mut svm_byte_array,
316314
runtime: *mut c_void,
317-
creator: *const c_void,
315+
creator: svm_byte_array,
318316
host_ctx: svm_byte_array,
319317
app: svm_byte_array,
320318
) -> svm_result_t {
321319
debug!("`svm_spawn_app` start");
322320

323321
let runtime = helpers::cast_to_runtime_mut(runtime);
324-
let creator = Address::from(creator);
325-
let host_ctx = HostCtx::from_raw_parts(host_ctx.bytes, host_ctx.length);
322+
let creator: Result<Address, String> = creator.into();
323+
324+
if let Err(msg) = creator {
325+
todo!();
326+
// return svm_result_t::SVM_FAILURE;
327+
}
326328

329+
let host_ctx = HostCtx::from_raw_parts(host_ctx.bytes, host_ctx.length);
327330
if host_ctx.is_err() {
328331
todo!();
329332
// return svm_result_t::SVM_FAILURE;
330333
}
331334

332335
let bytes = std::slice::from_raw_parts(app.bytes, app.length as usize);
333336

334-
match runtime.spawn_app(&creator, host_ctx.unwrap(), bytes) {
337+
match runtime.spawn_app(&creator.unwrap(), host_ctx.unwrap(), bytes) {
335338
Ok((addr, state)) => {
336339
// returning spawned app `Address` as `svm_byte_array`
337340
// client should call later `svm_address_destroy`
@@ -362,9 +365,7 @@ pub unsafe extern "C" fn svm_spawn_app(
362365
/// use svm_common::Address;
363366
///
364367
/// // allocate imports
365-
/// let count = 0;
366-
/// let mut imports = std::ptr::null_mut();
367-
/// let _res = unsafe { svm_imports_alloc(&mut imports, count) };
368+
/// let mut imports = testing::imports_alloc(0);
368369
///
369370
/// // create runtime
370371
/// let mut kv = std::ptr::null_mut();
@@ -374,26 +375,31 @@ pub unsafe extern "C" fn svm_spawn_app(
374375
/// let _res = unsafe { testing::svm_memory_runtime_create(&mut runtime, kv, host, imports) };
375376
///
376377
/// let mut app_tx = std::ptr::null_mut();
377-
/// let sender = Address::of("@sender");
378-
/// let tx = svm_byte_array::default();
379-
/// let _res = unsafe { svm_parse_exec_app(&mut app_tx, runtime, sender.as_ptr() as _, tx) };
378+
/// let sender = Address::of("@sender").into();
379+
/// let tx = vec![0x00, 0x01, 0x2, 0x3].into();
380+
/// let _res = unsafe { svm_parse_exec_app(&mut app_tx, runtime, sender, tx) };
380381
/// ```
381382
///
382383
#[must_use]
383384
#[no_mangle]
384385
pub unsafe extern "C" fn svm_parse_exec_app(
385386
app_tx: *mut *mut c_void,
386387
runtime: *const c_void,
387-
sender: *const c_void,
388+
sender: svm_byte_array,
388389
tx: svm_byte_array,
389390
) -> svm_result_t {
390391
debug!("`svm_parse_exec_app` start");
391392

392393
let runtime = helpers::cast_to_runtime(runtime);
393-
let sender = Address::from(sender);
394+
let sender: Result<Address, String> = sender.into();
395+
396+
if let Err(msg) = sender {
397+
todo!();
398+
}
399+
394400
let bytes = std::slice::from_raw_parts(tx.bytes, tx.length as usize);
395401

396-
match runtime.parse_exec_app(&sender, bytes) {
402+
match runtime.parse_exec_app(&sender.unwrap(), bytes) {
397403
Ok(tx) => {
398404
// `AppTransaction` will be freed later as part `svm_exec_app`
399405
*app_tx = svm_common::into_raw_mut(tx);
@@ -421,9 +427,7 @@ pub unsafe extern "C" fn svm_parse_exec_app(
421427
/// use svm_common::{State, Address};
422428
///
423429
/// // allocate imports
424-
/// let count = 0;
425-
/// let mut imports = std::ptr::null_mut();
426-
/// let _res = unsafe { svm_imports_alloc(&mut imports, count) };
430+
/// let mut imports = testing::imports_alloc(0);
427431
///
428432
/// // create runtime
429433
/// let mut kv = std::ptr::null_mut();
@@ -442,19 +446,19 @@ pub unsafe extern "C" fn svm_parse_exec_app(
442446
/// };
443447
///
444448
/// let app_tx_ptr = &app_tx as *const AppTransaction as *const c_void;
445-
/// let state = State::empty();
449+
/// let state = State::empty().into();
446450
/// let mut receipt = svm_byte_array::default();
447451
/// let host_ctx = svm_byte_array::default();
448-
/// let _res = unsafe { svm_exec_app(&mut receipt, runtime, app_tx_ptr, state.as_ptr() as _, host_ctx) };
452+
/// let _res = unsafe { svm_exec_app(&mut receipt, runtime, app_tx_ptr, state, host_ctx) };
449453
/// ```
450454
///
451455
#[must_use]
452456
#[no_mangle]
453457
pub unsafe extern "C" fn svm_exec_app(
454-
encoded_receipt: *mut svm_byte_array,
458+
receipt: *mut svm_byte_array,
455459
runtime: *mut c_void,
456460
app_tx: *const c_void,
457-
state: *const c_void,
461+
state: svm_byte_array,
458462
host_ctx: svm_byte_array,
459463
) -> svm_result_t {
460464
debug!("`svm_exec_app` start");
@@ -471,15 +475,19 @@ pub unsafe extern "C" fn svm_exec_app(
471475
let host_ctx = host_ctx.unwrap();
472476
let app_tx = *Box::from_raw(app_tx as *mut AppTransaction);
473477
let runtime = helpers::cast_to_runtime_mut(runtime);
474-
let state = State::from(state);
478+
let state: Result<State, String> = state.into();
475479

476-
match runtime.exec_app(app_tx, state, host_ctx) {
477-
Ok(ref receipt) => {
478-
let mut bytes = crate::receipt::encode_receipt(receipt);
480+
if let Err(msg) = state {
481+
todo!();
482+
}
483+
484+
match runtime.exec_app(app_tx, state.unwrap(), host_ctx) {
485+
Ok(ref native_receipt) => {
486+
let mut bytes = crate::receipt::encode_receipt(native_receipt);
479487

480488
// returning encoded `Receipt` as `svm_byte_array`
481489
// should call later `svm_receipt_destroy`
482-
vec_to_svm_byte_array!(encoded_receipt, bytes);
490+
vec_to_svm_byte_array!(receipt, bytes);
483491

484492
debug!("`svm_exec_app` returns `SVM_SUCCESS`");
485493
svm_result_t::SVM_SUCCESS
@@ -513,9 +521,7 @@ pub unsafe extern "C" fn svm_instance_context_host_get(ctx: *mut c_void) -> *mut
513521
/// use svm_common::Address;
514522
///
515523
/// // allocate imports
516-
/// let count = 0;
517-
/// let mut imports = std::ptr::null_mut();
518-
/// let _res = unsafe { svm_imports_alloc(&mut imports, count) };
524+
/// let mut imports = testing::imports_alloc(0);
519525
///
520526
/// // create runtime
521527
/// let mut kv = std::ptr::null_mut();

0 commit comments

Comments
 (0)