hookjs rollback

This commit is contained in:
Richard Holland
2024-04-23 13:26:41 +10:00
committed by RichardAH
parent a2c3567caf
commit 80df3adac7
3 changed files with 47 additions and 27 deletions

View File

@@ -338,6 +338,27 @@ JSValue hook_api::JSFunction##F(JSContext *ctx, JSValueConst this_val,\
: RC_ROLLBACK); \
}
#define HOOK_EXIT_JS(error_msg, error_code, exit_type)\
{\
int64_t val = 0;\
if (JS_IsNumber(error_code))\
JS_ToInt64(ctx, &val, error_code);\
hookCtx.result.exitCode = val;\
hookCtx.result.exitType = exit_type;\
if (JS_IsString(error_msg))\
{\
size_t len;\
const char* cstr = JS_ToCStringLen(ctx, &len, error_msg);\
if (len > 256)\
len = 256;\
hookCtx.result.exitReason = std::string(cstr, len);\
JS_FreeCString(ctx, cstr);\
}\
return JS_NewInt64(ctx,\
exit_type == hook_api::ExitType::ACCEPT ? RC_ACCEPT \
: RC_ROLLBACK); \
}
#define WRITE_WASM_MEMORY_OR_RETURN_AS_INT64( \
write_ptr_in, write_len_in, data_ptr_in, data_len_in, is_account_in) \
{ \

View File

@@ -70,12 +70,26 @@ DECLARE_WASM_FUNCTION(
uint32_t read_ptr,
uint32_t read_len,
int64_t error_code);
DECLARE_JS_FUNCTION(
int64_t,
accept,
JSValue error_msg,
JSValue error_code);
DECLARE_WASM_FUNCTION(
int64_t,
rollback,
uint32_t read_ptr,
uint32_t read_len,
int64_t error_code);
DECLARE_JS_FUNCTION(
int64_t,
rollback,
JSValue error_msg,
JSValue error_code);
DECLARE_WASM_FUNCTION(
int64_t,
util_raddr,
@@ -410,14 +424,6 @@ DECLARE_WASM_FUNCTION(
uint32_t slot_no_meta);
/// JS
DECLARE_JS_FUNCTION(
int64_t,
accept,
JSValue error_msg,
JSValue error_code);
} /* end namespace hook_api */
namespace hook {

View File

@@ -2188,25 +2188,7 @@ DEFINE_JS_FUNCTION(
JSValue error_code)
{
JS_HOOK_SETUP();
int64_t val = 0;
if (JS_IsNumber(error_code))
JS_ToInt64(ctx, &val, error_code);
hookCtx.result.exitCode = val;
hookCtx.result.exitType = hook_api::ExitType::ACCEPT;
if (JS_IsString(error_msg))
{
size_t len;
const char* cstr = JS_ToCStringLen(ctx, &len, error_msg);
if (len > 256)
len = 256;
hookCtx.result.exitReason = std::string(cstr, len);
JS_FreeCString(ctx, cstr);
}
return JS_NewInt64(ctx, RC_ACCEPT);
HOOK_EXIT_JS(error_msg, error_code, hook_api::ExitType::ACCEPT);
JS_HOOK_TEARDOWN();
}
@@ -2224,6 +2206,17 @@ DEFINE_WASM_FUNCTION(
WASM_HOOK_TEARDOWN();
}
DEFINE_JS_FUNCTION(
int64_t,
rollback,
JSValue error_msg,
JSValue error_code)
{
JS_HOOK_SETUP();
HOOK_EXIT_JS(error_msg, error_code, hook_api::ExitType::ROLLBACK);
JS_HOOK_TEARDOWN();
}
// Write the TxnID of the originating transaction into the write_ptr
DEFINE_WASM_FUNCTION(
int64_t,