Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/userlib/js/src/idl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,21 @@ test('IDL encoding (nat)', () => {
expect(() => IDL.encode([IDL.Nat], [-1])).toThrow(/Invalid nat argument/);
});

test('IDL encoding (float64)', () => {
// Float64
test_(IDL.Float64, 3, '4449444c0001720000000000000840', 'Float');
test_(IDL.Float64, 6, '4449444c0001720000000000001840', 'Float');
test_(IDL.Float64, 0.5, '4449444c000172000000000000e03f', 'Float');
test_(IDL.Float64, Number.NaN, '4449444c000172010000000000f07f', 'NaN');
test_(IDL.Float64, Number.POSITIVE_INFINITY, '4449444c000172000000000000f07f', '+infinity');
test_(IDL.Float64, Number.NEGATIVE_INFINITY, '4449444c000172000000000000f0ff', '-infinity');
test_(IDL.Float64, Number.EPSILON, '4449444c000172000000000000b03c', 'eps');
test_(IDL.Float64, Number.MIN_VALUE, '4449444c0001720100000000000000', 'min_value');
test_(IDL.Float64, Number.MAX_VALUE, '4449444c000172ffffffffffffef7f', 'max_value');
test_(IDL.Float64, Number.MIN_SAFE_INTEGER, '4449444c000172ffffffffffff3fc3', 'min_safe_integer');
test_(IDL.Float64, Number.MAX_SAFE_INTEGER, '4449444c000172ffffffffffff3f43', 'max_safe_integer');
});

test('IDL encoding (fixed-width number)', () => {
// Fixed-width number
test_(IDL.Int8, 0, '4449444c00017700', 'Int8');
Expand Down
42 changes: 42 additions & 0 deletions src/userlib/js/src/idl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const enum IDLTypeIds {
Bool = -2,
Nat = -3,
Int = -4,
Float64 = -14,
Text = -15,
Empty = -17,
Opt = -18,
Expand Down Expand Up @@ -111,6 +112,9 @@ export abstract class Visitor<D, R> {
public visitNat(t: NatClass, data: D): R {
return this.visitPrimitive(t, data);
}
public visitFloat(t: FloatClass, data: D): R {
return this.visitPrimitive(t, data);
}
public visitFixedInt(t: FixedIntClass, data: D): R {
return this.visitPrimitive(t, data);
}
Expand Down Expand Up @@ -412,6 +416,42 @@ export class NatClass extends PrimitiveType<BigNumber> {
}
}

/**
* Represents an IDL Float
*/
export class FloatClass extends PrimitiveType<number> {
public accept<D, R>(v: Visitor<D, R>, d: D): R {
return v.visitFloat(this, d);
}

public covariant(x: any): x is number {
return typeof x === 'number' || x instanceof Number;
}

public encodeValue(x: number) {
const buf = Buffer.allocUnsafe(8);
buf.writeDoubleLE(x, 0);
return buf;
}

public encodeType() {
return slebEncode(IDLTypeIds.Float64);
}

public decodeValue(b: Pipe) {
const x = b.read(8);
return x.readDoubleLE(0);
}

get name() {
return 'float64';
}

public valueToString(x: number) {
return x.toString();
}
}

/**
* Represents an IDL fixed-width Int(n)
*/
Expand Down Expand Up @@ -1189,6 +1229,8 @@ export const Text = new TextClass();
export const Int = new IntClass();
export const Nat = new NatClass();

export const Float64 = new FloatClass();

export const Int8 = new FixedIntClass(8);
export const Int16 = new FixedIntClass(16);
export const Int32 = new FixedIntClass(32);
Expand Down