|
|
@ -98,8 +98,7 @@ pub const Val = extern struct { |
|
|
|
wasmtime_val_delete(self); |
|
|
|
wasmtime_val_delete(self); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn fromValue(value: anytype) Val { |
|
|
|
pub fn fromValue(comptime T: type, value: T) Val { |
|
|
|
const T = @TypeOf(value); |
|
|
|
|
|
|
|
return switch (@typeInfo(T)) { |
|
|
|
return switch (@typeInfo(T)) { |
|
|
|
// TODO: Support .ComptimeInt and .ComptimeFloat. |
|
|
|
// TODO: Support .ComptimeInt and .ComptimeFloat. |
|
|
|
.Bool => .{ .kind = ValKind.i32, .of = .{ .i32 = @intFromBool(value) } }, |
|
|
|
.Bool => .{ .kind = ValKind.i32, .of = .{ .i32 = @intFromBool(value) } }, |
|
|
@ -147,35 +146,37 @@ pub const Val = extern struct { |
|
|
|
extern "c" fn wasmtime_val_delete(*Val) void; |
|
|
|
extern "c" fn wasmtime_val_delete(*Val) void; |
|
|
|
|
|
|
|
|
|
|
|
test "convert value to Val and back" { |
|
|
|
test "convert value to Val and back" { |
|
|
|
var small = Val.fromValue(-12); |
|
|
|
var small = Val.fromValue(i32, -12); |
|
|
|
defer small.deinit(); |
|
|
|
defer small.deinit(); |
|
|
|
try expect(small.kind == ValKind.i32); |
|
|
|
try expect(small.kind == ValKind.i32); |
|
|
|
try expect(small.of.i32 == -12); |
|
|
|
try expect(small.of.i32 == -12); |
|
|
|
try expect(small.toValue(i32) == -12); |
|
|
|
try expect(try small.toValue(i32) == -12); |
|
|
|
|
|
|
|
|
|
|
|
var large = Val.fromValue(10000000000000000000); |
|
|
|
var large = Val.fromValue(u64, 10000000000000000000); |
|
|
|
defer large.deinit(); |
|
|
|
defer large.deinit(); |
|
|
|
try expect(large.kind == ValKind.i64); |
|
|
|
try expect(large.kind == ValKind.i64); |
|
|
|
try expect(large.of.i64 == @as(i64, @bitCast(10000000000000000000))); |
|
|
|
// The provided integer can't be represented as an i64, but Wasm |
|
|
|
try expect(large.toValue(u64) == 10000000000000000000); |
|
|
|
// only knows about signed integers, so we have to store it as one. |
|
|
|
|
|
|
|
try expect(large.of.i64 == @as(i64, @bitCast(@as(u64, 10000000000000000000)))); |
|
|
|
|
|
|
|
try expect(try large.toValue(u64) == 10000000000000000000); |
|
|
|
|
|
|
|
|
|
|
|
var pi = Val.fromValue(3.14159); |
|
|
|
var pi = Val.fromValue(f32, 3.14159); |
|
|
|
defer pi.deinit(); |
|
|
|
defer pi.deinit(); |
|
|
|
try expect(pi.kind == ValKind.f32); |
|
|
|
try expect(pi.kind == ValKind.f32); |
|
|
|
try expect(pi.of.f32 == 3.14159); |
|
|
|
try expect(pi.of.f32 == 3.14159); |
|
|
|
try expect(pi.toValue(f32) == 3.14159); |
|
|
|
try expect(try pi.toValue(f32) == 3.14159); |
|
|
|
|
|
|
|
|
|
|
|
var yes = Val.fromValue(true); |
|
|
|
var yes = Val.fromValue(bool, true); |
|
|
|
defer yes.deinit(); |
|
|
|
defer yes.deinit(); |
|
|
|
try expect(yes.kind == ValKind.i32); |
|
|
|
try expect(yes.kind == ValKind.i32); |
|
|
|
try expect(yes.of.i32 == 1); |
|
|
|
try expect(yes.of.i32 == 1); |
|
|
|
try expect(yes.toValue(bool) == true); |
|
|
|
try expect(try yes.toValue(bool) == true); |
|
|
|
|
|
|
|
|
|
|
|
var no = Val.fromValue(false); |
|
|
|
var no = Val.fromValue(bool, false); |
|
|
|
defer no.deinit(); |
|
|
|
defer no.deinit(); |
|
|
|
try expect(no.kind == ValKind.i32); |
|
|
|
try expect(no.kind == ValKind.i32); |
|
|
|
try expect(no.of.i32 == 0); |
|
|
|
try expect(no.of.i32 == 0); |
|
|
|
try expect(no.toValue(bool) == false); |
|
|
|
try expect(try no.toValue(bool) == false); |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|