|
|
|
@ -1,7 +1,6 @@ |
|
|
|
|
const Engine = @import("./engine.zig").Engine; |
|
|
|
|
const Module = @import("./module.zig").Module; |
|
|
|
|
const Store = @import("./store.zig").Store; |
|
|
|
|
const Func = @import("./func.zig").Func; |
|
|
|
|
const Memory = @import("./memory.zig").Memory; |
|
|
|
|
const Diagnostics = @import("./diagnostics.zig").Diagnostics; |
|
|
|
|
const Error = @import("./error.zig").Error; |
|
|
|
@ -40,50 +39,63 @@ pub const Instance = extern struct { |
|
|
|
|
/// |
|
|
|
|
/// This function does not take ownership of any of its arguments, but all |
|
|
|
|
/// return values (Error or Trap) are owned by the caller. |
|
|
|
|
pub fn init(context: *Store.Context, module: *const Module, imports: []const Extern, diag: ?*Diagnostics) !Instance { |
|
|
|
|
pub fn init( |
|
|
|
|
context: *Store.Context, |
|
|
|
|
module: *const Module, |
|
|
|
|
imports: []const Extern, |
|
|
|
|
diag: ?*Diagnostics, |
|
|
|
|
) !Instance { |
|
|
|
|
var result: Instance = undefined; |
|
|
|
|
var trap: ?*Trap = null; |
|
|
|
|
const err = wasmtime_instance_new(context, module, imports.ptr, imports.len, &result, &trap); |
|
|
|
|
return try Diagnostics.handleErrorOrTrap(err, error.InstanceInit, trap, result, diag); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Get an exported `Extern` by name from this instance. |
|
|
|
|
/// Returns `error.ExportNotFound` if export is not found. |
|
|
|
|
pub fn getExport(self: *const Instance, context: *Store.Context, name: []const u8) !Extern { |
|
|
|
|
/// Gets an exported `Extern` by name from this instance, converted to the |
|
|
|
|
/// specified type. If `T` is `Extern` no conversion is done. |
|
|
|
|
/// |
|
|
|
|
/// Returns `error.ExportNotFound` if export with that name is not found. |
|
|
|
|
/// Returns `error.IncorrectType` if the export isn't the right type. |
|
|
|
|
pub fn get( |
|
|
|
|
self: *const Instance, |
|
|
|
|
context: *Store.Context, |
|
|
|
|
name: []const u8, |
|
|
|
|
comptime T: type, |
|
|
|
|
) !T { |
|
|
|
|
var result: Extern = undefined; |
|
|
|
|
return if (wasmtime_instance_export_get(context, self, name.ptr, name.len, &result)) result else error.ExportNotFound; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Get an exported `Func` by name from this instance. |
|
|
|
|
/// Returns `error.ExportNotFound` if export is not found. |
|
|
|
|
/// Returns `error.ExportIncorrectType` if the export isn't a `Func`. |
|
|
|
|
pub fn getFunc(self: *const Instance, context: *Store.Context, name: []const u8) !Func { |
|
|
|
|
return (try self.getExport(context, name)).asFunc() orelse error.ExportIncorrectType; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Get an exported `Memory` by name from this instance. |
|
|
|
|
/// Returns `error.ExportNotFound` if export is not found. |
|
|
|
|
/// Returns `error.ExportIncorrectType` if the export isn't a `Memory`. |
|
|
|
|
pub fn getMemory(self: *const Instance, context: *Store.Context, name: []const u8) !Memory { |
|
|
|
|
return (try self.getExport(context, name)).asMemory() orelse error.ExportIncorrectType; |
|
|
|
|
return if (wasmtime_instance_export_get(context, self, name.ptr, name.len, &result)) result.as(T) else error.ExportNotFound; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Get an exported `Memory`'s data slice from this instance. |
|
|
|
|
/// Returns `error.ExportNotFound` if export is not found. |
|
|
|
|
/// Returns `error.ExportIncorrectType` if the export isn't a `Memory`. |
|
|
|
|
pub fn getMemoryData(self: *const Instance, context: *Store.Context, name: []const u8) ![]u8 { |
|
|
|
|
return (try self.getMemory(context, name)).getData(context); |
|
|
|
|
/// |
|
|
|
|
/// Returns `error.ExportNotFound` if export with that name is not found. |
|
|
|
|
/// Returns `error.IncorrectType` if the export isn't the a `Memory`. |
|
|
|
|
pub fn getData( |
|
|
|
|
self: *const Instance, |
|
|
|
|
context: *Store.Context, |
|
|
|
|
name: []const u8, |
|
|
|
|
) ![]u8 { |
|
|
|
|
return (try self.get(context, name, Memory)).getData(context); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Get an export by index from an instance, or |
|
|
|
|
/// `error.ExportNotFound` if not found. |
|
|
|
|
pub fn getExportByIndex(self: *const Instance, context: *Store.Context, index: usize) !Extern { |
|
|
|
|
/// Gets an exported `Extern` by index from this instance, converted to |
|
|
|
|
/// the specified type. If `T` is `Extern` no conversion is done. |
|
|
|
|
/// |
|
|
|
|
/// Returns `error.ExportNotFound` if export with that name is not found. |
|
|
|
|
/// Returns `error.IncorrectType` if the export isn't the right type. |
|
|
|
|
pub fn getByIndex( |
|
|
|
|
self: *const Instance, |
|
|
|
|
context: *Store.Context, |
|
|
|
|
index: usize, |
|
|
|
|
comptime T: type, |
|
|
|
|
) !struct { result: T, name: []const u8 } { |
|
|
|
|
var result: Extern = undefined; |
|
|
|
|
// TODO: Add a way to also return `name`? |
|
|
|
|
// NOTE: Apparently `name` is owned by the store and must be used immediately. |
|
|
|
|
var name: [*]const u8 = null; |
|
|
|
|
var name_len: usize = undefined; |
|
|
|
|
return if (wasmtime_instance_export_nth(context, self, index, &name, &name_len, &result)) result else error.ExportNotFound; |
|
|
|
|
return if (wasmtime_instance_export_nth(context, self, index, &name, &name_len, &result)) |
|
|
|
|
.{ .result = result.as(T), .name = name[0..name_len] } |
|
|
|
|
else |
|
|
|
|
error.ExportNotFound; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
extern "c" fn wasmtime_instance_new(*Store.Context, *const Module, [*]const Extern, usize, *Instance, *?*Trap) ?*Error; |
|
|
|
|