as-variant
Advanced tools
Comparing version 0.1.3 to 0.1.4
import { Variant } from "../index"; | ||
describe("instantiation", () => { | ||
describe("Variant/from, Variant/is, Variant/get", () => { | ||
it("should init as i32 type", () => { | ||
@@ -18,7 +18,12 @@ let val = Variant.from(123); | ||
it("should init as f64 type", () => { | ||
let val = Variant.from(123.0); | ||
let val = Variant.from(-123.0); | ||
expect(val.is<f64>()).toBe(true); | ||
expect(val.get<f64>()).toBe(123.0); | ||
expect(val.get<f64>()).toBe(-123.0); | ||
}); | ||
it("should init as NaN value", () => { | ||
let val = Variant.from(NaN); | ||
expect(val.get<f64>()).toBeNaN(); | ||
}); | ||
it("should init as bool type", () => { | ||
@@ -51,2 +56,17 @@ let val = Variant.from(true); | ||
}); | ||
it("should init as variant type", () => { | ||
let val = Variant.from(Variant.from(123)); | ||
expect(val.is<Variant>()).toBe(true); | ||
expect(val.get<Variant>().get<i32>()).toBe(123); | ||
}); | ||
}); | ||
describe("Variant/set", () => { | ||
it("should set variant(i32) as variant(string)", () => { | ||
let val = Variant.from(123); | ||
val.set('foobaz'); | ||
expect(val.is<string>()).toBe(true); | ||
expect(val.get<string>()).toBe('foobaz'); | ||
}); | ||
}); |
@@ -89,10 +89,13 @@ | ||
@inline uncheckedGet<T>(): T { | ||
let type!: T, val = this.value; | ||
if (type instanceof f32) { | ||
let value!: T, val = this.value; | ||
if (value instanceof f32) { | ||
// @ts-ignore | ||
return reinterpret<f32>(<u32>val); | ||
} else if (type instanceof f64) { | ||
} else if (value instanceof f64) { | ||
// @ts-ignore | ||
return reinterpret<f64>(val); | ||
} else if (isReference<T>()) { | ||
if (!isNullable<T>() && !val) { | ||
throw new Error("unexpected null"); | ||
} | ||
return changetype<T>(<usize>val); | ||
@@ -107,17 +110,17 @@ } else { | ||
let | ||
type!: T, | ||
value!: T, | ||
ty = this.discriminator; | ||
if (type instanceof bool) return ty == VariantTy.Bool; | ||
else if (type instanceof i8) return ty == VariantTy.I8; | ||
else if (type instanceof i16) return ty == VariantTy.I16; | ||
else if (type instanceof i32) return ty == VariantTy.I32; | ||
else if (type instanceof i64) return ty == VariantTy.I64; | ||
else if (type instanceof u8) return ty == VariantTy.U8; | ||
else if (type instanceof u16) return ty == VariantTy.U16; | ||
else if (type instanceof u32) return ty == VariantTy.U32; | ||
else if (type instanceof u64) return ty == VariantTy.U64; | ||
else if (type instanceof f32) return ty == VariantTy.F32; | ||
else if (type instanceof f64) return ty == VariantTy.F64; | ||
else return ty == VariantTy.ManagedRef + idof<T>(); | ||
if (value instanceof bool) return ty == VariantTy.Bool; | ||
else if (value instanceof i8) return ty == VariantTy.I8; | ||
else if (value instanceof i16) return ty == VariantTy.I16; | ||
else if (value instanceof i32) return ty == VariantTy.I32; | ||
else if (value instanceof i64) return ty == VariantTy.I64; | ||
else if (value instanceof u8) return ty == VariantTy.U8; | ||
else if (value instanceof u16) return ty == VariantTy.U16; | ||
else if (value instanceof u32) return ty == VariantTy.U32; | ||
else if (value instanceof u64) return ty == VariantTy.U64; | ||
else if (value instanceof f32) return ty == VariantTy.F32; | ||
else if (value instanceof f64) return ty == VariantTy.F64; | ||
else return ty == VariantTy.ManagedRef + idof<T>(); | ||
} | ||
@@ -124,0 +127,0 @@ |
{ | ||
"name": "as-variant", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"ascMain": "assembly/index.ts", | ||
@@ -5,0 +5,0 @@ "main": "assembly/index.ts", |
@@ -6,3 +6,3 @@ Variant Data Type for AssemblyScript | ||
Supports any builtin types like i32, bool, string and any custom classes. | ||
Supports any builtin types like i32, bool, string and any custom classes (managed and unmanaged). | ||
@@ -16,14 +16,14 @@ ### Basic Usage | ||
let vNum = Variant.from(123) // stored as i32 | ||
let vStr = Variant.from('hello') // stored as string | ||
let vFoo = Variant.from(new Foo()) // stored as string | ||
let vNum = Variant.from(123) // stored as i32 | ||
let vStr = Variant.from('hello') // stored as string | ||
let vFoo = Variant.from(new Foo()) // stored as string | ||
vNum.set(2.0) // now stored as f64 | ||
vNum.set(2.0) // now stored as f64 | ||
assert(vNum.is<f64>() == true) // ok | ||
assert(vStr.is<f64>() == false) // ok | ||
assert(vStr.is<string>() == true) // ok | ||
assert(vNum.is<f64>() == true) // ok | ||
assert(vStr.is<f64>() == false) // ok | ||
assert(vStr.is<string>() == true) // ok | ||
let valF64 = vNum.get<f64>() // safely extract value | ||
let tryStr = vNum.get<string>() // will throw exception! | ||
let valF64 = vNum.get<f64>() // safely extract value | ||
let tryStr = vNum.get<string>() // will throw exception! | ||
``` | ||
@@ -30,0 +30,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
570613
187