Val.fromValue accepts type param, fix tests

main
copygirl 9 months ago
parent 5ac929f086
commit f5acd5b4e2
  1. 3
      build.zig
  2. 8
      src/func.zig
  3. 27
      src/val.zig

@ -28,6 +28,9 @@ pub fn build(b: *std.Build) !void {
.optimize = optimize,
});
main_tests.linkLibC();
main_tests.linkSystemLibrary("wasmtime");
const run_main_tests = b.addRunArtifact(main_tests);
const test_step = b.step("test", "Run library tests");

@ -137,9 +137,9 @@ pub const Func = extern struct {
// Pass the result(s) back to WASM.
if (comptime std.meta.trait.isTuple(ResultType)) {
inline for (func_results, 0..) |res, i|
results[i] = Val.fromValue(res);
results[i] = Val.fromValue(@TypeOf(res), res);
} else if (ResultType != void)
results[0] = Val.fromValue(func_results);
results[0] = Val.fromValue(@TypeOf(func_results), func_results);
return null;
}
@ -165,7 +165,7 @@ pub const Func = extern struct {
// Set up function arguments.
var args_array: [args.len]Val = undefined;
inline for (&args_array, args) |*val, arg|
val.* = Val.fromValue(arg);
val.* = Val.fromValue(@TypeOf(arg), arg);
// Set up function results.
const num_results = comptime getNumResultsFromResultType(TResult);
@ -264,7 +264,7 @@ pub const FuncType = opaque {
var results = if (comptime std.meta.trait.isTuple(ResultType)) blk: {
var res = ValTypeVec.initUninitialized(ResultType.len);
inline for (ResultType, res.toSlice()) |T, *r|
r.* = Val.fromValue(T);
r.* = ValType.fromType(T);
break :blk res;
} else if (ResultType != void) blk: {
var res = ValTypeVec.initUninitialized(1);

@ -98,8 +98,7 @@ pub const Val = extern struct {
wasmtime_val_delete(self);
}
pub fn fromValue(value: anytype) Val {
const T = @TypeOf(value);
pub fn fromValue(comptime T: type, value: T) Val {
return switch (@typeInfo(T)) {
// TODO: Support .ComptimeInt and .ComptimeFloat.
.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;
test "convert value to Val and back" {
var small = Val.fromValue(-12);
var small = Val.fromValue(i32, -12);
defer small.deinit();
try expect(small.kind == ValKind.i32);
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();
try expect(large.kind == ValKind.i64);
try expect(large.of.i64 == @as(i64, @bitCast(10000000000000000000)));
try expect(large.toValue(u64) == 10000000000000000000);
// The provided integer can't be represented as an i64, but Wasm
// 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();
try expect(pi.kind == ValKind.f32);
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();
try expect(yes.kind == ValKind.i32);
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();
try expect(no.kind == ValKind.i32);
try expect(no.of.i32 == 0);
try expect(no.toValue(bool) == false);
try expect(try no.toValue(bool) == false);
}
};

Loading…
Cancel
Save