From d82f60705dcf599a2b38fee42ea68ecd3e353343 Mon Sep 17 00:00:00 2001 From: tequ Date: Mon, 24 Feb 2025 17:28:12 +0900 Subject: [PATCH] add replacer for Stringify bigint value (#437) --- src/ripple/app/hook/impl/applyHook.cpp | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/ripple/app/hook/impl/applyHook.cpp b/src/ripple/app/hook/impl/applyHook.cpp index 9ff4f40b5..f8c30a096 100644 --- a/src/ripple/app/hook/impl/applyHook.cpp +++ b/src/ripple/app/hook/impl/applyHook.cpp @@ -1716,7 +1716,31 @@ DEFINE_JS_FUNCTION( } else { - JSValue sdata = JS_JSONStringify(ctx, data, JS_UNDEFINED, JS_UNDEFINED); + // replacer function that converts BigInts to strings + JSValueConst replacer = JS_NewCFunction( + ctx, + [](JSContext* ctx, + JSValueConst this_val, + int argc, + JSValueConst* argv) -> JSValue { + if (argc < 2) + return JS_DupValue(ctx, argv[1]); + if (JS_IsBigInt(ctx, argv[1])) + { + size_t len; + const char* str = JS_ToCStringLen(ctx, &len, argv[1]); + JSValue ret = JS_NewStringLen(ctx, str, len); + JS_FreeCString(ctx, str); + return ret; + } + return JS_DupValue(ctx, argv[1]); + }, + "replacer", + 2); + + JSValue sdata = JS_JSONStringify(ctx, data, replacer, JS_UNDEFINED); + JS_FreeValue(ctx, replacer); + size_t len; const char* cstr = JS_ToCStringLen(ctx, &len, sdata); if (len > 1023)