processJson method
Platform-specific: pass the unsigned transaction JSON, call the WASM module, and return the result JSON string.
WASM exports used: alloc, getUnsignedV2Transaction, resultPtr,
resultLen, release.
Implementation
@override
Future<String> processJson(String jsonString) async {
await _ensureInitialized();
final instance = _instance!;
final memory = _memory!;
final bytes = utf8.encode(jsonString);
// 1. Allocate WASM memory and write the input JSON
final alloc = instance.getFunction('alloc')!;
final ptr = alloc.inner(bytes.length) as int;
memory.view.setRange(ptr, ptr + bytes.length, bytes);
try {
// 2. Call the main processing function
final getUnsignedV2 = instance.getFunction('getUnsignedV2Transaction')!;
final packedResult = getUnsignedV2.inner(ptr, bytes.length) as int;
// 3. Extract the result pointer and length
final resultPtrFn = instance.getFunction('resultPtr')!;
final resultLenFn = instance.getFunction('resultLen')!;
final resultPtr = resultPtrFn.inner(packedResult) as int;
final resultLen = resultLenFn.inner(packedResult) as int;
// 4. Read the result bytes, decode, and release the result memory
final resultBytes = memory.view.sublist(resultPtr, resultPtr + resultLen);
instance.getFunction('release')!.inner(resultPtr);
return utf8.decode(resultBytes);
} finally {
// 5. Always release the input memory
instance.getFunction('release')!.inner(ptr);
}
}