|
|
|
@ -2,7 +2,6 @@ const std = @import("std"); |
|
|
|
|
const expect = std.testing.expect; |
|
|
|
|
|
|
|
|
|
const Func = @import("./func.zig").Func; |
|
|
|
|
const Extern = @import("./extern.zig").Extern; |
|
|
|
|
const Vec = @import("./vec.zig").Vec; |
|
|
|
|
|
|
|
|
|
/// A host-defined un-forgeable reference to pass into WebAssembly. |
|
|
|
@ -144,48 +143,41 @@ pub const Val = extern struct { |
|
|
|
|
|
|
|
|
|
extern "c" fn wasmtime_val_copy(*Val, *const Val) void; |
|
|
|
|
extern "c" fn wasmtime_val_delete(*Val) void; |
|
|
|
|
|
|
|
|
|
test "convert value to Val and back" { |
|
|
|
|
var small = Val.fromValue(i32, -12); |
|
|
|
|
defer small.deinit(); |
|
|
|
|
try expect(small.kind == ValKind.i32); |
|
|
|
|
try expect(small.of.i32 == -12); |
|
|
|
|
try expect(try small.toValue(i32) == -12); |
|
|
|
|
|
|
|
|
|
var large = Val.fromValue(u64, 10000000000000000000); |
|
|
|
|
defer large.deinit(); |
|
|
|
|
try expect(large.kind == ValKind.i64); |
|
|
|
|
// 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(f32, 3.14159); |
|
|
|
|
defer pi.deinit(); |
|
|
|
|
try expect(pi.kind == ValKind.f32); |
|
|
|
|
try expect(pi.of.f32 == 3.14159); |
|
|
|
|
try expect(try pi.toValue(f32) == 3.14159); |
|
|
|
|
|
|
|
|
|
var yes = Val.fromValue(bool, true); |
|
|
|
|
defer yes.deinit(); |
|
|
|
|
try expect(yes.kind == ValKind.i32); |
|
|
|
|
try expect(yes.of.i32 == 1); |
|
|
|
|
try expect(try yes.toValue(bool) == true); |
|
|
|
|
|
|
|
|
|
var no = Val.fromValue(bool, false); |
|
|
|
|
defer no.deinit(); |
|
|
|
|
try expect(no.kind == ValKind.i32); |
|
|
|
|
try expect(no.of.i32 == 0); |
|
|
|
|
try expect(try no.toValue(bool) == false); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
// pub const ValVec = Vec(Val, wasm_val_vec_new_empty, wasm_val_vec_new_uninitialized, wasm_val_vec_new, wasm_val_vec_copy, wasm_val_vec_delete); |
|
|
|
|
// extern "c" fn wasm_val_vec_new_empty(*anyopaque, usize) void; |
|
|
|
|
// extern "c" fn wasm_val_vec_new_uninitialized(*anyopaque, usize) void; |
|
|
|
|
// extern "c" fn wasm_val_vec_new(*anyopaque, usize, [*]const Val) void; |
|
|
|
|
// extern "c" fn wasm_val_vec_copy(*anyopaque, *const anyopaque) void; |
|
|
|
|
// extern "c" fn wasm_val_vec_delete(*anyopaque) void; |
|
|
|
|
test Val { |
|
|
|
|
var small = Val.fromValue(i32, -12); |
|
|
|
|
defer small.deinit(); |
|
|
|
|
try expect(small.kind == ValKind.i32); |
|
|
|
|
try expect(small.of.i32 == -12); |
|
|
|
|
try expect(try small.toValue(i32) == -12); |
|
|
|
|
|
|
|
|
|
var large = Val.fromValue(u64, 10000000000000000000); |
|
|
|
|
defer large.deinit(); |
|
|
|
|
try expect(large.kind == ValKind.i64); |
|
|
|
|
// 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(f32, 3.14159); |
|
|
|
|
defer pi.deinit(); |
|
|
|
|
try expect(pi.kind == ValKind.f32); |
|
|
|
|
try expect(pi.of.f32 == 3.14159); |
|
|
|
|
try expect(try pi.toValue(f32) == 3.14159); |
|
|
|
|
|
|
|
|
|
var yes = Val.fromValue(bool, true); |
|
|
|
|
defer yes.deinit(); |
|
|
|
|
try expect(yes.kind == ValKind.i32); |
|
|
|
|
try expect(yes.of.i32 == 1); |
|
|
|
|
try expect(try yes.toValue(bool) == true); |
|
|
|
|
|
|
|
|
|
var no = Val.fromValue(bool, false); |
|
|
|
|
defer no.deinit(); |
|
|
|
|
try expect(no.kind == ValKind.i32); |
|
|
|
|
try expect(no.of.i32 == 0); |
|
|
|
|
try expect(try no.toValue(bool) == false); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// An object representing the type of a value. |
|
|
|
|
pub const ValType = opaque { |
|
|
|
|