Skip to content

Commit a576b8b

Browse files
committed
Cleanup externs and add additional ByteVec helper
1 parent f59e6dc commit a576b8b

File tree

2 files changed

+50
-41
lines changed

2 files changed

+50
-41
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
gyro.lock

src/main.zig

Lines changed: 49 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub const Config = opaque {
2929
return config;
3030
}
3131

32-
extern fn wasm_config_new() ?*Config;
32+
extern "c" fn wasm_config_new() ?*Config;
3333
};
3434

3535
pub const Engine = opaque {
@@ -47,9 +47,9 @@ pub const Engine = opaque {
4747
wasm_engine_delete(self);
4848
}
4949

50-
extern fn wasm_engine_new() ?*Engine;
51-
extern fn wasm_engine_new_with_config(*Config) ?*Engine;
52-
extern fn wasm_engine_delete(*Engine) void;
50+
extern "c" fn wasm_engine_new() ?*Engine;
51+
extern "c" fn wasm_engine_new_with_config(*Config) ?*Engine;
52+
extern "c" fn wasm_engine_delete(*Engine) void;
5353
};
5454

5555
pub const Store = opaque {
@@ -63,8 +63,8 @@ pub const Store = opaque {
6363
wasm_store_delete(self);
6464
}
6565

66-
extern fn wasm_store_new(*Engine) ?*Store;
67-
extern fn wasm_store_delete(*Store) void;
66+
extern "c" fn wasm_store_new(*Engine) ?*Store;
67+
extern "c" fn wasm_store_delete(*Store) void;
6868
};
6969

7070
pub const Module = opaque {
@@ -87,8 +87,8 @@ pub const Module = opaque {
8787
wasm_module_delete(self);
8888
}
8989

90-
extern fn wasm_module_new(*Store, *const ByteVec) ?*Module;
91-
extern fn wasm_module_delete(*Module) void;
90+
extern "c" fn wasm_module_new(*Store, *const ByteVec) ?*Module;
91+
extern "c" fn wasm_module_delete(*Module) void;
9292
};
9393

9494
fn cb(params: ?*const Valtype, results: ?*Valtype) callconv(.C) ?*Trap {
@@ -225,12 +225,12 @@ pub const Func = opaque {
225225
};
226226
}
227227

228-
extern fn wasm_func_new(*Store, ?*c_void, Callback) ?*Func;
229-
extern fn wasm_func_as_extern(*Func) ?*Extern;
230-
extern fn wasm_func_copy(*Func) ?*Func;
231-
extern fn wasm_func_call(*Func, *const ValVec, *ValVec) ?*Trap;
232-
extern fn wasm_func_result_arity(*Func) usize;
233-
extern fn wasm_func_param_arity(*Func) usize;
228+
extern "c" fn wasm_func_new(*Store, ?*c_void, Callback) ?*Func;
229+
extern "c" fn wasm_func_as_extern(*Func) ?*Extern;
230+
extern "c" fn wasm_func_copy(*Func) ?*Func;
231+
extern "c" fn wasm_func_call(*Func, *const ValVec, *ValVec) ?*Trap;
232+
extern "c" fn wasm_func_result_arity(*Func) usize;
233+
extern "c" fn wasm_func_param_arity(*Func) usize;
234234
};
235235

236236
pub const Instance = opaque {
@@ -336,8 +336,8 @@ pub const Trap = opaque {
336336
return bytes.?;
337337
}
338338

339-
extern fn wasm_trap_delete(*Trap) void;
340-
extern fn wasm_trap_message(*const Trap, out: *?*ByteVec) void;
339+
extern "c" fn wasm_trap_delete(*Trap) void;
340+
extern "c" fn wasm_trap_message(*const Trap, out: *?*ByteVec) void;
341341
};
342342

343343
pub const Extern = opaque {
@@ -499,7 +499,7 @@ pub const ExportType = opaque {
499499
return self.wasm_exporttype_name().?;
500500
}
501501

502-
extern fn wasm_exporttype_name(*ExportType) ?*ByteVec;
502+
extern "c" fn wasm_exporttype_name(*ExportType) ?*ByteVec;
503503
};
504504

505505
pub const ExportTypeVec = extern struct {
@@ -517,7 +517,7 @@ pub const ExportTypeVec = extern struct {
517517
self.wasm_exporttype_vec_delete();
518518
}
519519

520-
extern fn wasm_exporttype_vec_delete(*ExportTypeVec) void;
520+
extern "c" fn wasm_exporttype_vec_delete(*ExportTypeVec) void;
521521
};
522522

523523
pub const InstanceType = opaque {
@@ -532,8 +532,8 @@ pub const InstanceType = opaque {
532532
return export_vec;
533533
}
534534

535-
extern fn wasm_instancetype_delete(*InstanceType) void;
536-
extern fn wasm_instancetype_exports(*InstanceType, ?*ExportTypeVec) void;
535+
extern "c" fn wasm_instancetype_delete(*InstanceType) void;
536+
extern "c" fn wasm_instancetype_exports(*InstanceType, ?*ExportTypeVec) void;
537537
};
538538

539539
pub const Callback = fn (?*const Valtype, ?*Valtype) callconv(.C) ?*Trap;
@@ -549,6 +549,13 @@ pub const ByteVec = extern struct {
549549
return bytes;
550550
}
551551

552+
/// Initializes and copies contents of the input slice
553+
pub fn fromSlice(slice: []const u8) ByteVec {
554+
var bytes: ByteVec = undefined;
555+
wasm_byte_vec_new(&bytes, slice.len, slice.ptr);
556+
return bytes;
557+
}
558+
552559
/// Returns a slice to the byte vector
553560
pub fn toSlice(self: ByteVec) []const u8 {
554561
return self.data[0..self.size];
@@ -559,8 +566,9 @@ pub const ByteVec = extern struct {
559566
wasm_byte_vec_delete(self);
560567
}
561568

562-
extern fn wasm_byte_vec_new_uninitialized(ptr: *ByteVec, size: usize) void;
563-
extern fn wasm_byte_vec_delete(ptr: *ByteVec) void;
569+
extern "c" fn wasm_byte_vec_new(*ByteVec, usize, [*]const u8) void;
570+
extern "c" fn wasm_byte_vec_new_uninitialized(*ByteVec, usize) void;
571+
extern "c" fn wasm_byte_vec_delete(*ByteVec) void;
564572
};
565573

566574
pub const NameVec = extern struct {
@@ -590,9 +598,9 @@ pub const ExternVec = extern struct {
590598
return externs;
591599
}
592600

593-
extern fn wasm_extern_vec_new_empty(ptr: *ExternVec) void;
594-
extern fn wasm_extern_vec_new_uninitialized(ptr: *ExternVec, size: usize) void;
595-
extern fn wasm_extern_vec_delete(ptr: *ExternVec) void;
601+
extern "c" fn wasm_extern_vec_new_empty(*ExternVec) void;
602+
extern "c" fn wasm_extern_vec_new_uninitialized(*ExternVec, usize) void;
603+
extern "c" fn wasm_extern_vec_delete(*ExternVec) void;
596604
};
597605

598606
pub const Valkind = extern enum(u8) {
@@ -630,9 +638,9 @@ pub const Valtype = opaque {
630638
return @intToEnum(Valkind, wasm_valtype_kind(self));
631639
}
632640

633-
extern fn wasm_valtype_new(kind: u8) *Valtype;
634-
extern fn wasm_valtype_delete(*Valkind) void;
635-
extern fn wasm_valtype_kind(*Valkind) u8;
641+
extern "c" fn wasm_valtype_new(kind: u8) *Valtype;
642+
extern "c" fn wasm_valtype_delete(*Valkind) void;
643+
extern "c" fn wasm_valtype_kind(*Valkind) u8;
636644
};
637645

638646
pub const ValtypeVec = extern struct {
@@ -658,13 +666,13 @@ pub const ValVec = extern struct {
658666
self.wasm_val_vec_delete();
659667
}
660668

661-
extern fn wasm_val_vec_new_uninitialized(*ValVec, usize) void;
662-
extern fn wasm_val_vec_delete(*ValVec) void;
669+
extern "c" fn wasm_val_vec_new_uninitialized(*ValVec, usize) void;
670+
extern "c" fn wasm_val_vec_delete(*ValVec) void;
663671
};
664672

665673
// Func
666-
pub extern fn wasm_functype_new(args: *ValtypeVec, results: *ValtypeVec) ?*c_void;
667-
pub extern fn wasm_functype_delete(functype: *c_void) void;
674+
pub extern "c" fn wasm_functype_new(args: *ValtypeVec, results: *ValtypeVec) ?*c_void;
675+
pub extern "c" fn wasm_functype_delete(functype: *c_void) c_void;
668676

669677
pub const WasiConfig = opaque {
670678
/// Options to inherit when inherriting configs
@@ -716,13 +724,13 @@ pub const WasiConfig = opaque {
716724
wasi_config_inherit_stderr(self);
717725
}
718726

719-
extern fn wasi_config_new() ?*WasiConfig;
720-
extern fn wasi_config_delete(?*WasiConfig) void;
721-
extern fn wasi_config_inherit_argv(?*WasiConfig) void;
722-
extern fn wasi_config_inherit_env(?*WasiConfig) void;
723-
extern fn wasi_config_inherit_stdin(?*WasiConfig) void;
724-
extern fn wasi_config_inherit_stdout(?*WasiConfig) void;
725-
extern fn wasi_config_inherit_stderr(?*WasiConfig) void;
727+
extern "c" fn wasi_config_new() ?*WasiConfig;
728+
extern "c" fn wasi_config_delete(?*WasiConfig) void;
729+
extern "c" fn wasi_config_inherit_argv(?*WasiConfig) void;
730+
extern "c" fn wasi_config_inherit_env(?*WasiConfig) void;
731+
extern "c" fn wasi_config_inherit_stdin(?*WasiConfig) void;
732+
extern "c" fn wasi_config_inherit_stdout(?*WasiConfig) void;
733+
extern "c" fn wasi_config_inherit_stderr(?*WasiConfig) void;
726734
};
727735

728736
pub const WasiInstance = opaque {
@@ -734,8 +742,8 @@ pub const WasiInstance = opaque {
734742
wasm_instance_delete(self);
735743
}
736744

737-
extern fn wasi_instance_new(?*Store, [*:0]const u8, ?*WasiConfig, *?*Trap) ?*WasiInstance;
738-
extern fn wasm_instance_delete(?*WasiInstance) void;
745+
extern "c" fn wasi_instance_new(?*Store, [*:0]const u8, ?*WasiConfig, *?*Trap) ?*WasiInstance;
746+
extern "c" fn wasm_instance_delete(?*WasiInstance) void;
739747
};
740748

741749
test "" {

0 commit comments

Comments
 (0)