From 6168a7b26315aba98a906a86471fc43caa6509f4 Mon Sep 17 00:00:00 2001 From: Franklin Waller Date: Wed, 18 Sep 2024 16:17:48 +0200 Subject: [PATCH] feat(to-string): add support for floats --- .../assembly/console.ts | 17 +++++++++++++++++ libs/as-sdk-integration-tests/assembly/index.ts | 11 ++++++++++- .../src/console.test.ts | 12 ++++++++++++ libs/as-sdk/assembly/string-utils.ts | 2 +- 4 files changed, 40 insertions(+), 2 deletions(-) diff --git a/libs/as-sdk-integration-tests/assembly/console.ts b/libs/as-sdk-integration-tests/assembly/console.ts index 9593924..09c0e38 100644 --- a/libs/as-sdk-integration-tests/assembly/console.ts +++ b/libs/as-sdk-integration-tests/assembly/console.ts @@ -19,3 +19,20 @@ export class TestLogByteArray extends OracleProgram { Process.exit(0); } } + +export class TestLogNull extends OracleProgram { + execution(): void { + Console.log(null); + + Process.exit(0); + } +} + +export class TestLogFloat extends OracleProgram { + execution(): void { + const float: f32 = 0.32; + Console.log(float); + + Process.exit(0); + } +} diff --git a/libs/as-sdk-integration-tests/assembly/index.ts b/libs/as-sdk-integration-tests/assembly/index.ts index 7e19c4c..93b24c9 100644 --- a/libs/as-sdk-integration-tests/assembly/index.ts +++ b/libs/as-sdk-integration-tests/assembly/index.ts @@ -9,7 +9,12 @@ import { TestBytesToNumber, TestNumberToBytes, } from "./bytes"; -import { TestLogBuffer, TestLogByteArray } from "./console"; +import { + TestLogBuffer, + TestLogByteArray, + TestLogFloat, + TestLogNull, +} from "./console"; import { TestKeccak256, TestSecp256k1VerifyInvalid, @@ -82,6 +87,10 @@ if (args === "testHttpRejection") { new TestBytesToNumber().run(); } else if (args === "testNumberToBytes") { new TestNumberToBytes().run(); +} else if (args === "testLogNull") { + new TestLogNull().run(); +} else if (args === "testLogFloat") { + new TestLogFloat().run(); } Process.error(Bytes.fromUtf8String("No argument given")); diff --git a/libs/as-sdk-integration-tests/src/console.test.ts b/libs/as-sdk-integration-tests/src/console.test.ts index 8a7df6d..52ae5ac 100644 --- a/libs/as-sdk-integration-tests/src/console.test.ts +++ b/libs/as-sdk-integration-tests/src/console.test.ts @@ -24,4 +24,16 @@ describe("console", () => { expect(result.stdout).toEqual("TypedArray(0x54797065644172726179)\n"); }); + + it("should print a float value", async () => { + const result = await executeDrWasm(wasmBinary, Buffer.from("testLogFloat")); + + expect(result.stdout).toEqual("0.3199999928474426\n"); + }); + + it("should print a null value", async () => { + const result = await executeDrWasm(wasmBinary, Buffer.from("testLogNull")); + + expect(result.stdout).toEqual("null\n"); + }); }); diff --git a/libs/as-sdk/assembly/string-utils.ts b/libs/as-sdk/assembly/string-utils.ts index 6bd9124..4a8aa5f 100644 --- a/libs/as-sdk/assembly/string-utils.ts +++ b/libs/as-sdk/assembly/string-utils.ts @@ -12,7 +12,7 @@ export interface ToString { * @returns */ export function toString(message: T): string { - if (message === null) { + if (!isFloat(message) && message === null) { return "null"; }