-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwasm.zig
150 lines (112 loc) · 4.08 KB
/
wasm.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const std = @import("std");
const helper = @import("./helper.zig");
const gpa = helper.gpa;
const js = helper.js;
const AnyType = helper.AnyType;
const Void = helper.Void;
const Bool = helper.Bool;
const Integer = helper.Integer;
const UnsignedInteger = helper.UnsignedInteger;
const Float = helper.Float;
const String = helper.String;
const Bytes = helper.Bytes;
const JSON = helper.JSON;
const Function = helper.Function;
const Array = helper.Array;
export fn greet(arg: String) String {
// Get the real value passed to us by javascript
const name = arg.value();
// Generate a small log message with the passed argument
const logMessage = std.fmt.allocPrint(gpa, "Greeting {s} directly from zig!", .{name}) catch @panic("Oops");
defer gpa.free(logMessage);
// Log the message to the console back to javascript
js.log(String.init(logMessage));
// Generate a new greet message that we can return
const greetMessage = std.fmt.allocPrint(gpa, "Hello {s}!", .{name}) catch @panic("Oops");
// Return the greet message as a compatible type
return String.init(greetMessage);
}
// Another example just as above
export fn blake2b(arg: String) String {
const input = arg.value();
var out: [32]u8 = undefined;
std.crypto.hash.blake2.Blake2b256.hash(input, out[0..32], .{});
const outHex = std.fmt.bytesToHex(out, .lower);
const outHexPtr = gpa.alloc(u8, outHex.len) catch @panic("Oops");
@memcpy(outHexPtr, &outHex);
return String.init(outHexPtr);
}
export fn silence() void {
_ = 1 + 1;
}
export fn testVoid() Void {
return Void.init();
}
export fn printVoid(arg: Void) void {
const message = std.fmt.allocPrint(gpa, "Void = {any}!", .{arg.value()}) catch @panic("Oops");
js.log(String.init(message));
}
export fn testBool() Bool {
return Bool.init(true);
}
export fn printBool(arg: Bool) void {
const message = std.fmt.allocPrint(gpa, "Bool = {any}!", .{arg.value()}) catch @panic("Oops");
js.log(String.init(message));
}
export fn testInt() Integer {
return Integer.init(-12345);
}
export fn printInt(arg: Integer) void {
const message = std.fmt.allocPrint(gpa, "Int = {any}!", .{arg.value()}) catch @panic("Oops");
js.log(String.init(message));
}
export fn testUint() UnsignedInteger {
return UnsignedInteger.init(12345);
}
export fn printUint(arg: UnsignedInteger) void {
const message = std.fmt.allocPrint(gpa, "Uint = {any}!", .{arg.value()}) catch @panic("Oops");
js.log(String.init(message));
}
export fn testFloat() Float {
return Float.init(1.2345);
}
export fn printFloat(arg: Float) void {
const message = std.fmt.allocPrint(gpa, "Float = {any}!", .{arg.value()}) catch @panic("Oops");
js.log(String.init(message));
}
export fn testBytes() Bytes {
return Bytes.init("Hello World");
}
export fn printBytes(arg: Bytes) void {
const message = std.fmt.allocPrint(gpa, "Bytes = {any}!", .{arg.value()}) catch @panic("Oops");
js.log(String.init(message));
}
export fn testString() String {
return String.init("Bye World");
}
export fn printString(arg: String) void {
const message = std.fmt.allocPrint(gpa, "String = {s}!", .{arg.value()}) catch @panic("Oops");
js.log(String.init(message));
}
export fn testJSON() JSON {
return JSON.init("{\"message\": \"Greetings\"}");
}
export fn printJSON(arg: JSON) void {
const message = std.fmt.allocPrint(gpa, "JSON = {s}!", .{arg.value()}) catch @panic("Oops");
js.log(String.init(message));
}
fn printHelloWorld(arg: Array) AnyType {
const message = std.fmt.allocPrint(gpa, "This Zig function was passed as an argument and received {d} argument(s)!", .{arg.len}) catch @panic("Oops");
js.log(String.init(message));
return String.init("Zig says hi!").asAny();
}
export fn testFunctionRef() Function {
return Function.init(printHelloWorld);
}
export fn testFunction(arg: Function) AnyType {
const args = Array.from(&.{ String.init("Hello").asAny(), String.init("World").asAny() });
return arg.call(args);
}
export fn testFunctionWithArgs(arg: Function, args: Array) AnyType {
return arg.call(args);
}