refactor DEFINE_HOOK_FUNCNARG

This commit is contained in:
tequ
2025-11-20 12:05:46 +09:00
parent 4c0d3ab99d
commit 2fc07955c8
2 changed files with 33 additions and 46 deletions

View File

@@ -24,8 +24,21 @@
#define VA_NARGS_IMPL( \
_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, N, ...) \
N
#define VA_NARGS(__drop, ...) \
VA_NARGS_IMPL(__VA_ARGS__, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
#define VA_NARGS(__drop, ...) \
VA_NARGS_IMPL( \
__VA_OPT__(__VA_ARGS__ COMMA) 12, \
11, \
10, \
9, \
8, \
7, \
6, \
5, \
4, \
3, \
2, \
1, \
0)
#define FIRST(a, b) a
#define SECOND(a, b) b
#define STRIP_TYPES(...) FOR_VARS(SECOND, 0, __VA_ARGS__)
@@ -109,21 +122,21 @@
const WasmEdge_Value* in, \
WasmEdge_Value* out) \
{ \
int _stack = 0; \
FOR_VARS(VAR_ASSIGN, 2, __VA_ARGS__); \
__VA_OPT__(int _stack = 0;) \
__VA_OPT__(FOR_VARS(VAR_ASSIGN, 2, __VA_ARGS__);) \
hook::HookContext* hookCtx = \
reinterpret_cast<hook::HookContext*>(data_ptr); \
R return_code = hook_api::F( \
*hookCtx, \
*const_cast<WasmEdge_CallingFrameContext*>(frameCtx), \
STRIP_TYPES(__VA_ARGS__)); \
*const_cast<WasmEdge_CallingFrameContext*>(frameCtx) \
__VA_OPT__(COMMA STRIP_TYPES(__VA_ARGS__))); \
if (return_code == RC_ROLLBACK || return_code == RC_ACCEPT) \
return WasmEdge_Result_Terminate; \
out[0] = RET_ASSIGN(R, return_code); \
return WasmEdge_Result_Success; \
}; \
WasmEdge_ValType hook_api::WasmFunctionParams##F[] = { \
FOR_VARS(WASM_VAL_TYPE, 0, __VA_ARGS__)}; \
__VA_OPT__(FOR_VARS(WASM_VAL_TYPE, 0, __VA_ARGS__))}; \
WasmEdge_ValType hook_api::WasmFunctionResult##F[1] = { \
WASM_VAL_TYPE(R, dummy)}; \
WasmEdge_FunctionTypeContext* hook_api::WasmFunctionType##F = \
@@ -136,34 +149,8 @@
WasmEdge_StringCreateByCString(#F); \
R hook_api::F( \
hook::HookContext& hookCtx, \
WasmEdge_CallingFrameContext const& frameCtx, \
__VA_ARGS__)
#define DEFINE_HOOK_FUNCNARG(R, F) \
WasmEdge_Result hook_api::WasmFunction##F( \
void* data_ptr, \
const WasmEdge_CallingFrameContext* frameCtx, \
const WasmEdge_Value* in, \
WasmEdge_Value* out) \
{ \
hook::HookContext* hookCtx = \
reinterpret_cast<hook::HookContext*>(data_ptr); \
R return_code = hook_api::F( \
*hookCtx, *const_cast<WasmEdge_CallingFrameContext*>(frameCtx)); \
if (return_code == RC_ROLLBACK || return_code == RC_ACCEPT) \
return WasmEdge_Result_Terminate; \
out[0] = CAT2(RET_, R(return_code)); \
return WasmEdge_Result_Success; \
}; \
WasmEdge_ValType hook_api::WasmFunctionResult##F[1] = { \
WASM_VAL_TYPE(R, dummy)}; \
WasmEdge_FunctionTypeContext* hook_api::WasmFunctionType##F = \
WasmEdge_FunctionTypeCreate({}, 0, WasmFunctionResult##F, 1); \
WasmEdge_String hook_api::WasmFunctionName##F = \
WasmEdge_StringCreateByCString(#F); \
R hook_api::F( \
hook::HookContext& hookCtx, \
WasmEdge_CallingFrameContext const& frameCtx)
WasmEdge_CallingFrameContext const& frameCtx __VA_OPT__( \
COMMA __VA_ARGS__))
#define HOOK_SETUP() \
try \

View File

@@ -2275,7 +2275,7 @@ DEFINE_HOOK_FUNCTION(
}
// Return the tt (Transaction Type) numeric code of the originating transaction
DEFINE_HOOK_FUNCNARG(int64_t, otxn_type)
DEFINE_HOOK_FUNCTION(int64_t, otxn_type)
{
HOOK_SETUP(); // populates memory_ctx, memory, memory_length, applyCtx,
// hookCtx on current stack
@@ -2325,7 +2325,7 @@ DEFINE_HOOK_FUNCTION(int64_t, otxn_slot, uint32_t slot_into)
// Return the burden of the originating transaction... this will be 1 unless the
// originating transaction was itself an emitted transaction from a previous
// hook invocation
DEFINE_HOOK_FUNCNARG(int64_t, otxn_burden)
DEFINE_HOOK_FUNCTION(int64_t, otxn_burden)
{
HOOK_SETUP(); // populates memory_ctx, memory, memory_length, applyCtx,
// hookCtx on current stack
@@ -2362,7 +2362,7 @@ DEFINE_HOOK_FUNCNARG(int64_t, otxn_burden)
// Return the generation of the originating transaction... this will be 1 unless
// the originating transaction was itself an emitted transaction from a previous
// hook invocation
DEFINE_HOOK_FUNCNARG(int64_t, otxn_generation)
DEFINE_HOOK_FUNCTION(int64_t, otxn_generation)
{
HOOK_SETUP(); // populates memory_ctx, memory, memory_length, applyCtx,
// hookCtx on current stack
@@ -2394,14 +2394,14 @@ DEFINE_HOOK_FUNCNARG(int64_t, otxn_generation)
}
// Return the generation of a hypothetically emitted transaction from this hook
DEFINE_HOOK_FUNCNARG(int64_t, etxn_generation)
DEFINE_HOOK_FUNCTION(int64_t, etxn_generation)
{
// proxy only, no setup or teardown
return otxn_generation(hookCtx, frameCtx) + 1;
}
// Return the current ledger sequence number
DEFINE_HOOK_FUNCNARG(int64_t, ledger_seq)
DEFINE_HOOK_FUNCTION(int64_t, ledger_seq)
{
HOOK_SETUP();
@@ -2431,7 +2431,7 @@ DEFINE_HOOK_FUNCTION(
HOOK_TEARDOWN();
}
DEFINE_HOOK_FUNCNARG(int64_t, ledger_last_time)
DEFINE_HOOK_FUNCTION(int64_t, ledger_last_time)
{
HOOK_SETUP();
@@ -3889,7 +3889,7 @@ DEFINE_HOOK_FUNCTION(int64_t, etxn_reserve, uint32_t count)
}
// Compute the burden of an emitted transaction based on a number of factors
DEFINE_HOOK_FUNCNARG(int64_t, etxn_burden)
DEFINE_HOOK_FUNCTION(int64_t, etxn_burden)
{
HOOK_SETUP(); // populates memory_ctx, memory, memory_length, applyCtx,
// hookCtx on current stack
@@ -4672,7 +4672,7 @@ DEFINE_HOOK_FUNCTION(
}
// Return the current fee base of the current ledger (multiplied by a margin)
DEFINE_HOOK_FUNCNARG(int64_t, fee_base)
DEFINE_HOOK_FUNCTION(int64_t, fee_base)
{
HOOK_SETUP(); // populates memory_ctx, memory, memory_length, applyCtx,
// hookCtx on current stack
@@ -5590,7 +5590,7 @@ DEFINE_HOOK_FUNCTION(int64_t, float_divide, int64_t float1, int64_t float2)
HOOK_TEARDOWN();
}
DEFINE_HOOK_FUNCNARG(int64_t, float_one)
DEFINE_HOOK_FUNCTION(int64_t, float_one)
{
return float_one_internal;
}
@@ -5986,12 +5986,12 @@ DEFINE_HOOK_FUNCTION(
HOOK_TEARDOWN();
}
DEFINE_HOOK_FUNCNARG(int64_t, hook_pos)
DEFINE_HOOK_FUNCTION(int64_t, hook_pos)
{
return hookCtx.result.hookChainPosition;
}
DEFINE_HOOK_FUNCNARG(int64_t, hook_again)
DEFINE_HOOK_FUNCTION(int64_t, hook_again)
{
HOOK_SETUP();