Skip to content

Commit

Permalink
feat(to-string): add support for floats
Browse files Browse the repository at this point in the history
  • Loading branch information
FranklinWaller committed Sep 18, 2024
1 parent 5a4c13c commit 6168a7b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
17 changes: 17 additions & 0 deletions libs/as-sdk-integration-tests/assembly/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
11 changes: 10 additions & 1 deletion libs/as-sdk-integration-tests/assembly/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import {
TestBytesToNumber,
TestNumberToBytes,
} from "./bytes";
import { TestLogBuffer, TestLogByteArray } from "./console";
import {
TestLogBuffer,
TestLogByteArray,
TestLogFloat,
TestLogNull,
} from "./console";
import {
TestKeccak256,
TestSecp256k1VerifyInvalid,
Expand Down Expand Up @@ -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"));
12 changes: 12 additions & 0 deletions libs/as-sdk-integration-tests/src/console.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
});
2 changes: 1 addition & 1 deletion libs/as-sdk/assembly/string-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface ToString {
* @returns
*/
export function toString<T = string>(message: T): string {
if (message === null) {
if (!isFloat(message) && message === null) {
return "null";
}

Expand Down

0 comments on commit 6168a7b

Please sign in to comment.