Skip to content

Commit 5f58446

Browse files
gumb0axic
authored andcommitted
capi: Add FizzyExecutionContext parameter to fizzy_execute
1 parent 59737b8 commit 5f58446

File tree

7 files changed

+59
-48
lines changed

7 files changed

+59
-48
lines changed

bindings/rust/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,12 @@ impl Instance {
481481
/// # Safety
482482
/// This function expects a valid `func_idx` and appropriate number of `args`.
483483
pub unsafe fn unsafe_execute(&mut self, func_idx: u32, args: &[Value]) -> ExecutionResult {
484-
ExecutionResult(sys::fizzy_execute(self.0.as_ptr(), func_idx, args.as_ptr()))
484+
ExecutionResult(sys::fizzy_execute(
485+
self.0.as_ptr(),
486+
func_idx,
487+
args.as_ptr(),
488+
std::ptr::null_mut(),
489+
))
485490
}
486491

487492
/// Find function type for a given index. Must be a valid index otherwise behaviour is undefined.

include/fizzy/fizzy.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -600,14 +600,16 @@ bool fizzy_find_exported_global(
600600
///
601601
/// @param instance Pointer to module instance. Cannot be NULL.
602602
/// @param args Pointer to the argument array. Can be NULL if function has 0 inputs.
603+
/// @param ctx Opaque pointer to execution context. If NULL new execution context
604+
/// will be allocated.
603605
/// @return Result of execution.
604606
///
605607
/// @note
606608
/// No validation is done on the number of arguments passed in @p args, nor on their types.
607609
/// When number of passed arguments or their types are different from the ones defined by the
608610
/// function type, behaviour is undefined.
609-
FizzyExecutionResult fizzy_execute(
610-
FizzyInstance* instance, uint32_t func_idx, const FizzyValue* args) FIZZY_NOEXCEPT;
611+
FizzyExecutionResult fizzy_execute(FizzyInstance* instance, uint32_t func_idx,
612+
const FizzyValue* args, FizzyExecutionContext* ctx) FIZZY_NOEXCEPT;
611613

612614
#ifdef __cplusplus
613615
}

lib/fizzy/capi.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -706,10 +706,14 @@ size_t fizzy_get_instance_memory_size(FizzyInstance* instance) noexcept
706706
return memory->size();
707707
}
708708

709-
FizzyExecutionResult fizzy_execute(
710-
FizzyInstance* instance, uint32_t func_idx, const FizzyValue* args) noexcept
711-
{
712-
const auto result = fizzy::execute(*unwrap(instance), func_idx, unwrap(args));
709+
FizzyExecutionResult fizzy_execute(FizzyInstance* c_instance, uint32_t func_idx,
710+
const FizzyValue* c_args, FizzyExecutionContext* c_ctx) noexcept
711+
{
712+
auto* instance = unwrap(c_instance);
713+
const auto* args = unwrap(c_args);
714+
const auto result =
715+
(c_ctx == nullptr ? fizzy::execute(*instance, func_idx, args) :
716+
fizzy::execute(*instance, func_idx, args, unwrap(c_ctx)));
713717
return wrap(result);
714718
}
715719

test/unittests/capi_execute_test.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ TEST(capi_execute, execute)
3030
module, nullptr, 0, nullptr, nullptr, nullptr, 0, FizzyMemoryPagesLimitDefault, nullptr);
3131
ASSERT_NE(instance, nullptr);
3232

33-
EXPECT_THAT(fizzy_execute(instance, 0, nullptr), CResult());
34-
EXPECT_THAT(fizzy_execute(instance, 1, nullptr), CResult(42_u32));
33+
EXPECT_THAT(fizzy_execute(instance, 0, nullptr, nullptr), CResult());
34+
EXPECT_THAT(fizzy_execute(instance, 1, nullptr, nullptr), CResult(42_u32));
3535
FizzyValue args[] = {{42}, {2}};
36-
EXPECT_THAT(fizzy_execute(instance, 2, args), CResult(21_u32));
37-
EXPECT_THAT(fizzy_execute(instance, 3, nullptr), CTraps());
36+
EXPECT_THAT(fizzy_execute(instance, 2, args, nullptr), CResult(21_u32));
37+
EXPECT_THAT(fizzy_execute(instance, 3, nullptr, nullptr), CTraps());
3838

3939
fizzy_free_instance(instance);
4040
}
@@ -71,10 +71,10 @@ TEST(capi_execute, execute_with_host_function)
7171
module, host_funcs, 2, nullptr, nullptr, nullptr, 0, FizzyMemoryPagesLimitDefault, nullptr);
7272
ASSERT_NE(instance, nullptr);
7373

74-
EXPECT_THAT(fizzy_execute(instance, 0, nullptr), CResult(42_u32));
74+
EXPECT_THAT(fizzy_execute(instance, 0, nullptr, nullptr), CResult(42_u32));
7575

7676
FizzyValue args[] = {{42}, {2}};
77-
EXPECT_THAT(fizzy_execute(instance, 1, args), CResult(21_u32));
77+
EXPECT_THAT(fizzy_execute(instance, 1, args, nullptr), CResult(21_u32));
7878

7979
fizzy_free_instance(instance);
8080
}
@@ -102,7 +102,7 @@ TEST(capi_execute, imported_function_traps)
102102
module, host_funcs, 1, nullptr, nullptr, nullptr, 0, FizzyMemoryPagesLimitDefault, nullptr);
103103
ASSERT_NE(instance, nullptr);
104104

105-
EXPECT_THAT(fizzy_execute(instance, 1, nullptr), CTraps());
105+
EXPECT_THAT(fizzy_execute(instance, 1, nullptr, nullptr), CTraps());
106106

107107
fizzy_free_instance(instance);
108108
}
@@ -132,7 +132,7 @@ TEST(capi_execute, imported_function_void)
132132
module, host_funcs, 1, nullptr, nullptr, nullptr, 0, FizzyMemoryPagesLimitDefault, nullptr);
133133
ASSERT_NE(instance, nullptr);
134134

135-
EXPECT_THAT(fizzy_execute(instance, 1, nullptr), CResult());
135+
EXPECT_THAT(fizzy_execute(instance, 1, nullptr, nullptr), CResult());
136136
EXPECT_TRUE(called);
137137

138138
fizzy_free_instance(instance);
@@ -182,7 +182,7 @@ TEST(capi_execute, imported_function_from_another_module)
182182
ASSERT_NE(instance2, nullptr);
183183

184184
FizzyValue args[] = {{44}, {2}};
185-
EXPECT_THAT(fizzy_execute(instance2, 1, args), CResult(42_u32));
185+
EXPECT_THAT(fizzy_execute(instance2, 1, args, nullptr), CResult(42_u32));
186186

187187
fizzy_free_exported_function(&func);
188188
fizzy_free_instance(instance2);
@@ -224,7 +224,7 @@ TEST(capi_execute, imported_table_from_another_module)
224224
module2, nullptr, 0, &table, nullptr, nullptr, 0, FizzyMemoryPagesLimitDefault, nullptr);
225225
ASSERT_NE(instance2, nullptr);
226226

227-
EXPECT_THAT(fizzy_execute(instance2, 0, nullptr), CResult(42_u32));
227+
EXPECT_THAT(fizzy_execute(instance2, 0, nullptr, nullptr), CResult(42_u32));
228228

229229
fizzy_free_instance(instance2);
230230
fizzy_free_instance(instance1);
@@ -262,7 +262,7 @@ TEST(capi_execute, imported_memory_from_another_module)
262262
module2, nullptr, 0, nullptr, &memory, nullptr, 0, FizzyMemoryPagesLimitDefault, nullptr);
263263
ASSERT_NE(instance2, nullptr);
264264

265-
EXPECT_THAT(fizzy_execute(instance2, 0, nullptr), CResult(0x00ffaa00_u32));
265+
EXPECT_THAT(fizzy_execute(instance2, 0, nullptr, nullptr), CResult(0x00ffaa00_u32));
266266

267267
fizzy_free_instance(instance2);
268268
fizzy_free_instance(instance1);
@@ -300,7 +300,7 @@ TEST(capi_execute, imported_global_from_another_module)
300300
module2, nullptr, 0, nullptr, nullptr, &global, 1, FizzyMemoryPagesLimitDefault, nullptr);
301301
ASSERT_NE(instance2, nullptr);
302302

303-
EXPECT_THAT(fizzy_execute(instance2, 0, nullptr), CResult(42_u32));
303+
EXPECT_THAT(fizzy_execute(instance2, 0, nullptr, nullptr), CResult(42_u32));
304304

305305
fizzy_free_instance(instance2);
306306
fizzy_free_instance(instance1);

test/unittests/capi_instantiate_test.cpp

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,10 @@ TEST(capi_instantiate, instantiate_imported_globals)
108108
module, nullptr, 0, nullptr, nullptr, globals, 4, FizzyMemoryPagesLimitDefault, nullptr);
109109
EXPECT_NE(instance, nullptr);
110110

111-
EXPECT_THAT(fizzy_execute(instance, 0, nullptr), CResult(42_u32));
112-
EXPECT_THAT(fizzy_execute(instance, 1, nullptr), CResult(43_u64));
113-
EXPECT_THAT(fizzy_execute(instance, 2, nullptr), CResult(44.4f));
114-
EXPECT_THAT(fizzy_execute(instance, 3, nullptr), CResult(45.5));
111+
EXPECT_THAT(fizzy_execute(instance, 0, nullptr, nullptr), CResult(42_u32));
112+
EXPECT_THAT(fizzy_execute(instance, 1, nullptr, nullptr), CResult(43_u64));
113+
EXPECT_THAT(fizzy_execute(instance, 2, nullptr, nullptr), CResult(44.4f));
114+
EXPECT_THAT(fizzy_execute(instance, 3, nullptr, nullptr), CResult(45.5));
115115

116116
fizzy_free_instance(instance);
117117

@@ -329,10 +329,10 @@ TEST(capi_instantiate, resolve_instantiate_functions)
329329
ASSERT_NE(instance, nullptr);
330330

331331
FizzyValue arg;
332-
EXPECT_THAT(fizzy_execute(instance, 0, &arg), CResult(42_u32));
333-
EXPECT_THAT(fizzy_execute(instance, 1, &arg), CResult(43_u64));
334-
EXPECT_THAT(fizzy_execute(instance, 2, &arg), CResult(44.44f));
335-
EXPECT_THAT(fizzy_execute(instance, 3, &arg), CResult(45.45));
332+
EXPECT_THAT(fizzy_execute(instance, 0, &arg, nullptr), CResult(42_u32));
333+
EXPECT_THAT(fizzy_execute(instance, 1, &arg, nullptr), CResult(43_u64));
334+
EXPECT_THAT(fizzy_execute(instance, 2, &arg, nullptr), CResult(44.44f));
335+
EXPECT_THAT(fizzy_execute(instance, 3, &arg, nullptr), CResult(45.45));
336336

337337
fizzy_free_instance(instance);
338338

@@ -390,8 +390,8 @@ TEST(capi_instantiate, resolve_instantiate_function_duplicate)
390390
module, host_funcs, 1, nullptr, nullptr, nullptr, 0, FizzyMemoryPagesLimitDefault, nullptr);
391391
ASSERT_NE(instance, nullptr);
392392

393-
EXPECT_THAT(fizzy_execute(instance, 0, nullptr), CResult(42_u32));
394-
EXPECT_THAT(fizzy_execute(instance, 1, nullptr), CResult(42_u32));
393+
EXPECT_THAT(fizzy_execute(instance, 0, nullptr, nullptr), CResult(42_u32));
394+
EXPECT_THAT(fizzy_execute(instance, 1, nullptr, nullptr), CResult(42_u32));
395395

396396
fizzy_free_instance(instance);
397397
}
@@ -449,10 +449,10 @@ TEST(capi_instantiate, resolve_instantiate_globals)
449449
4, FizzyMemoryPagesLimitDefault, nullptr);
450450
ASSERT_NE(instance, nullptr);
451451

452-
EXPECT_THAT(fizzy_execute(instance, 1, nullptr), CResult(42_u32));
453-
EXPECT_THAT(fizzy_execute(instance, 2, nullptr), CResult(43_u32));
454-
EXPECT_THAT(fizzy_execute(instance, 3, nullptr), CResult(44_u64));
455-
EXPECT_THAT(fizzy_execute(instance, 4, nullptr), CResult(45_u64));
452+
EXPECT_THAT(fizzy_execute(instance, 1, nullptr, nullptr), CResult(42_u32));
453+
EXPECT_THAT(fizzy_execute(instance, 2, nullptr, nullptr), CResult(43_u32));
454+
EXPECT_THAT(fizzy_execute(instance, 3, nullptr, nullptr), CResult(44_u64));
455+
EXPECT_THAT(fizzy_execute(instance, 4, nullptr, nullptr), CResult(45_u64));
456456

457457
fizzy_free_instance(instance);
458458

@@ -465,10 +465,10 @@ TEST(capi_instantiate, resolve_instantiate_globals)
465465
host_globals_reordered, 4, FizzyMemoryPagesLimitDefault, nullptr);
466466
EXPECT_NE(instance, nullptr);
467467

468-
EXPECT_THAT(fizzy_execute(instance, 1, nullptr), CResult(42_u32));
469-
EXPECT_THAT(fizzy_execute(instance, 2, nullptr), CResult(43_u32));
470-
EXPECT_THAT(fizzy_execute(instance, 3, nullptr), CResult(44_u64));
471-
EXPECT_THAT(fizzy_execute(instance, 4, nullptr), CResult(45_u64));
468+
EXPECT_THAT(fizzy_execute(instance, 1, nullptr, nullptr), CResult(42_u32));
469+
EXPECT_THAT(fizzy_execute(instance, 2, nullptr, nullptr), CResult(43_u32));
470+
EXPECT_THAT(fizzy_execute(instance, 3, nullptr, nullptr), CResult(44_u64));
471+
EXPECT_THAT(fizzy_execute(instance, 4, nullptr, nullptr), CResult(45_u64));
472472

473473
fizzy_free_instance(instance);
474474

@@ -481,10 +481,10 @@ TEST(capi_instantiate, resolve_instantiate_globals)
481481
4, FizzyMemoryPagesLimitDefault, nullptr);
482482
EXPECT_NE(instance, nullptr);
483483

484-
EXPECT_THAT(fizzy_execute(instance, 1, nullptr), CResult(42_u32));
485-
EXPECT_THAT(fizzy_execute(instance, 2, nullptr), CResult(43_u32));
486-
EXPECT_THAT(fizzy_execute(instance, 3, nullptr), CResult(44_u64));
487-
EXPECT_THAT(fizzy_execute(instance, 4, nullptr), CResult(45_u64));
484+
EXPECT_THAT(fizzy_execute(instance, 1, nullptr, nullptr), CResult(42_u32));
485+
EXPECT_THAT(fizzy_execute(instance, 2, nullptr, nullptr), CResult(43_u32));
486+
EXPECT_THAT(fizzy_execute(instance, 3, nullptr, nullptr), CResult(44_u64));
487+
EXPECT_THAT(fizzy_execute(instance, 4, nullptr, nullptr), CResult(45_u64));
488488

489489
fizzy_free_instance(instance);
490490

@@ -521,8 +521,8 @@ TEST(capi_instantiate, resolve_instantiate_global_duplicate)
521521
FizzyMemoryPagesLimitDefault, nullptr);
522522
ASSERT_NE(instance, nullptr);
523523

524-
EXPECT_THAT(fizzy_execute(instance, 0, nullptr), CResult(42_u32));
525-
EXPECT_THAT(fizzy_execute(instance, 1, nullptr), CResult(42_u32));
524+
EXPECT_THAT(fizzy_execute(instance, 0, nullptr, nullptr), CResult(42_u32));
525+
EXPECT_THAT(fizzy_execute(instance, 1, nullptr, nullptr), CResult(42_u32));
526526

527527
fizzy_free_instance(instance);
528528
}

test/unittests/capi_test.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ TEST(capi, memory_access)
271271
memory[0] = 0xaa;
272272
memory[1] = 0xbb;
273273

274-
EXPECT_THAT(fizzy_execute(instance, 0, nullptr), CResult(0x22bbaa_u32));
274+
EXPECT_THAT(fizzy_execute(instance, 0, nullptr, nullptr), CResult(0x22bbaa_u32));
275275

276276
fizzy_free_instance(instance);
277277
}
@@ -314,7 +314,7 @@ TEST(capi, imported_memory_access)
314314
module, nullptr, 0, nullptr, &memory, nullptr, 0, FizzyMemoryPagesLimitDefault, nullptr);
315315
ASSERT_NE(instance, nullptr);
316316

317-
EXPECT_EQ(fizzy_execute(instance, 0, nullptr).value.i32, 0x221100);
317+
EXPECT_EQ(fizzy_execute(instance, 0, nullptr, nullptr).value.i32, 0x221100);
318318

319319
EXPECT_EQ(fizzy_get_instance_memory_size(instance), 65536);
320320

@@ -324,8 +324,8 @@ TEST(capi, imported_memory_access)
324324
memory_data[0] = 0xaa;
325325
memory_data[1] = 0xbb;
326326

327-
EXPECT_EQ(fizzy_execute(instance_memory, 0, nullptr).value.i32, 0x22bbaa);
328-
EXPECT_EQ(fizzy_execute(instance, 0, nullptr).value.i32, 0x22bbaa);
327+
EXPECT_EQ(fizzy_execute(instance_memory, 0, nullptr, nullptr).value.i32, 0x22bbaa);
328+
EXPECT_EQ(fizzy_execute(instance, 0, nullptr, nullptr).value.i32, 0x22bbaa);
329329

330330
fizzy_free_instance(instance);
331331
fizzy_free_instance(instance_memory);

test/utils/fizzy_c_engine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ WasmEngine::Result FizzyCEngine::execute(
124124
assert(func_type.output != FizzyValueTypeF32 && func_type.output != FizzyValueTypeF64 &&
125125
"floating point result types are not supported");
126126
const auto first_arg = reinterpret_cast<const FizzyValue*>(args.data());
127-
const auto status = fizzy_execute(m_instance.get(), func_idx, first_arg);
127+
const auto status = fizzy_execute(m_instance.get(), func_idx, first_arg, nullptr);
128128
if (status.trapped)
129129
return {true, std::nullopt};
130130
else if (status.has_value)

0 commit comments

Comments
 (0)