@pkmn/engine 0.1.0-dev.645b4750 → 0.1.0-dev.6ba42cd5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,7 +10,7 @@ const path = require('path');
10
10
  const {pipeline} = require('stream/promises');
11
11
  const tty = require('tty');
12
12
 
13
- const ZIG_VERSION = {major: 0, minor: 11, patch: 0};
13
+ const ZIG_VERSION = {major: 0, minor: 16, patch: 0, dev: '2193+fc517bd01'};
14
14
  const DEV = ZIG_VERSION.dev ? `-dev.${ZIG_VERSION.dev}` : '';
15
15
  const VERSION = `${ZIG_VERSION.major}.${ZIG_VERSION.minor}.${ZIG_VERSION.patch}${DEV}`;
16
16
  const INDEX = 'https://ziglang.org/download/index.json';
@@ -111,15 +111,31 @@ const unpack = async (input, output) => {
111
111
  }
112
112
  };
113
113
 
114
+ const parse = s => {
115
+ try {
116
+ return JSON.parse(s);
117
+ } catch (e) {
118
+ if (!(e instanceof SyntaxError)) throw e;
119
+ const json = s
120
+ // "key": -> "key": (ensures keys are quoted)
121
+ .replace(/\.([a-zA-Z_][a-zA-Z0-9_]*)\s*=/g, '"$1":')
122
+ // .{ -> { (converts ZON struct to JSON object)
123
+ .replace(/\.\{/g, '{')
124
+ // Remove trailing commas, which are invalid in JSON
125
+ .replace(/,\s*([}\]])/g, '$1');
126
+ return JSON.parse(json);
127
+ }
128
+ };
129
+
114
130
  const find = zig => {
115
131
  try {
116
- const {zig_exe, version} = JSON.parse(sh(zig, ['env']));
132
+ const {zig_exe, version} = parse(sh(zig, ['env']));
117
133
  const [major, minor, patch, dev] = version.split('.').map(n => parseInt(n));
118
134
 
119
135
  const compatible = major > ZIG_VERSION.major ||
120
136
  (major >= ZIG_VERSION.major && minor > ZIG_VERSION.minor) ||
121
137
  (major >= ZIG_VERSION.major && minor >= ZIG_VERSION.minor && patch >= ZIG_VERSION.patch &&
122
- (!ZIG_VERSION.dev || (dev && dev >= ZIG_VERSION.dev)));
138
+ (!ZIG_VERSION.dev || (dev && dev >= parseInt(ZIG_VERSION.dev))));
123
139
 
124
140
  if (compatible) {
125
141
  log('Found existing compatible Zig executable:', zig_exe);
@@ -163,10 +179,9 @@ const install = async () => {
163
179
  }
164
180
 
165
181
  const index = JSON.parse(await fetch(INDEX));
166
- // TODO: ziglang/zig#17768
167
- // const version = Object.keys(index)[+!ZIG_VERSION.dev];
168
- const version = VERSION;
169
- const release = index[VERSION][`${arch}-${platform}`];
182
+ const version = Object.keys(index)[+!ZIG_VERSION.dev];
183
+ // const version = VERSION; // TODO
184
+ const release = index[version][`${arch}-${platform}`];
170
185
  const url = release.tarball;
171
186
 
172
187
  let base = path.basename(url);
@@ -5,9 +5,6 @@ const assert = std.debug.assert;
5
5
 
6
6
  const ERROR: u8 = 0b1100;
7
7
 
8
- const Enum = if (@hasField(std.builtin.Type, "enum")) .@"enum" else .Enum;
9
- const Strong = if (@hasField(std.builtin.GlobalLinkage, "strong")) .strong else .Strong;
10
-
11
8
  pub const OPTIONS: extern struct {
12
9
  showdown: bool,
13
10
  log: bool,
@@ -25,59 +22,59 @@ pub const CHOICES_SIZE = pkmn.CHOICES_SIZE;
25
22
  pub const MAX_LOGS = pkmn.MAX_LOGS;
26
23
  pub const LOGS_SIZE = pkmn.LOGS_SIZE;
27
24
 
28
- pub fn choice_init(choice: u8, data: u8) callconv(.C) u8 {
29
- assert(choice <= @field(@typeInfo(pkmn.Choice.Type), @tagName(Enum)).fields.len);
25
+ pub fn choice_init(choice: u8, data: u8) callconv(.c) u8 {
26
+ assert(choice <= @typeInfo(pkmn.Choice.Type).@"enum".fields.len);
30
27
  assert(data <= 6);
31
28
  return @bitCast(pkmn.Choice{ .type = @enumFromInt(choice), .data = @intCast(data) });
32
29
  }
33
30
 
34
- pub fn choice_type(choice: u8) callconv(.C) u8 {
31
+ pub fn choice_type(choice: u8) callconv(.c) u8 {
35
32
  return @intFromEnum(@as(pkmn.Choice, @bitCast(choice)).type);
36
33
  }
37
34
 
38
- pub fn choice_data(choice: u8) callconv(.C) u8 {
35
+ pub fn choice_data(choice: u8) callconv(.c) u8 {
39
36
  return @as(u8, @as(pkmn.Choice, @bitCast(choice)).data);
40
37
  }
41
38
 
42
- pub fn result_type(result: u8) callconv(.C) u8 {
39
+ pub fn result_type(result: u8) callconv(.c) u8 {
43
40
  return @intFromEnum(@as(pkmn.Result, @bitCast(result)).type);
44
41
  }
45
42
 
46
- pub fn result_p1(result: u8) callconv(.C) u8 {
43
+ pub fn result_p1(result: u8) callconv(.c) u8 {
47
44
  assert(!err(result));
48
45
  return @intFromEnum(@as(pkmn.Result, @bitCast(result)).p1);
49
46
  }
50
47
 
51
- pub fn result_p2(result: u8) callconv(.C) u8 {
48
+ pub fn result_p2(result: u8) callconv(.c) u8 {
52
49
  assert(!err(result));
53
50
  return @intFromEnum(@as(pkmn.Result, @bitCast(result)).p2);
54
51
  }
55
52
 
56
- pub fn err(result: u8) callconv(.C) bool {
53
+ pub fn err(result: u8) callconv(.c) bool {
57
54
  return result == ERROR;
58
55
  }
59
56
 
60
- pub fn psrng_init(prng: *pkmn.PSRNG, seed: u64) callconv(.C) void {
57
+ pub fn psrng_init(prng: *pkmn.PSRNG, seed: u64) callconv(.c) void {
61
58
  prng.src = .{ .seed = seed };
62
59
  }
63
60
 
64
- pub fn psrng_next(prng: *pkmn.PSRNG) callconv(.C) u32 {
61
+ pub fn psrng_next(prng: *pkmn.PSRNG) callconv(.c) u32 {
65
62
  return prng.next();
66
63
  }
67
64
 
68
- pub fn rational_init(rational: *pkmn.Rational(f64)) callconv(.C) void {
65
+ pub fn rational_init(rational: *pkmn.Rational(f64)) callconv(.c) void {
69
66
  rational.reset();
70
67
  }
71
68
 
72
- pub fn rational_reduce(rational: *pkmn.Rational(f64)) callconv(.C) void {
69
+ pub fn rational_reduce(rational: *pkmn.Rational(f64)) callconv(.c) void {
73
70
  rational.reduce();
74
71
  }
75
72
 
76
- pub fn rational_numerator(rational: *const pkmn.Rational(f64)) callconv(.C) f64 {
73
+ pub fn rational_numerator(rational: *const pkmn.Rational(f64)) callconv(.c) f64 {
77
74
  return rational.p;
78
75
  }
79
76
 
80
- pub fn rational_denominator(rational: *const pkmn.Rational(f64)) callconv(.C) f64 {
77
+ pub fn rational_denominator(rational: *const pkmn.Rational(f64)) callconv(.c) f64 {
81
78
  return rational.q;
82
79
  }
83
80
 
@@ -106,7 +103,7 @@ pub fn gen(comptime num: comptime_int) type {
106
103
  };
107
104
 
108
105
  const battle_options = struct {
109
- stream: pkmn.protocol.ByteStream,
106
+ writer: pkmn.protocol.Writer,
110
107
  log: pkmn.protocol.FixedLog,
111
108
  chance: g.Chance(pkmn.Rational(f64)),
112
109
  calc: g.Calc,
@@ -121,13 +118,13 @@ pub fn gen(comptime num: comptime_int) type {
121
118
  log: ?*const log_options,
122
119
  chance: ?*const chance_options,
123
120
  calc: ?*const calc_options,
124
- ) callconv(.C) void {
121
+ ) callconv(.c) void {
125
122
  if (pkmn.options.log) {
126
123
  if (log) |l| {
127
- options.stream = .{ .buffer = l.buf[0..l.len] };
128
- options.log = .{ .writer = options.stream.writer() };
124
+ options.writer = .{ .buffer = l.buf[0..l.len] };
125
+ options.log = .{ .writer = &options.writer };
129
126
  } else {
130
- options.stream.reset();
127
+ options.writer.reset();
131
128
  }
132
129
  }
133
130
  if (pkmn.options.chance) {
@@ -152,25 +149,25 @@ pub fn gen(comptime num: comptime_int) type {
152
149
 
153
150
  pub fn battle_options_chance_probability(
154
151
  options: *battle_options,
155
- ) callconv(.C) *pkmn.Rational(f64) {
152
+ ) callconv(.c) *pkmn.Rational(f64) {
156
153
  return &options.chance.probability;
157
154
  }
158
155
 
159
156
  pub fn battle_options_chance_actions(
160
157
  options: *battle_options,
161
- ) callconv(.C) *g.chance.Actions {
158
+ ) callconv(.c) *g.chance.Actions {
162
159
  return &options.chance.actions;
163
160
  }
164
161
 
165
162
  pub fn battle_options_chance_durations(
166
163
  options: *battle_options,
167
- ) callconv(.C) *g.chance.Durations {
164
+ ) callconv(.c) *g.chance.Durations {
168
165
  return &options.chance.durations;
169
166
  }
170
167
 
171
168
  pub fn battle_options_calc_summaries(
172
169
  options: *battle_options,
173
- ) callconv(.C) *g.calc.Summaries {
170
+ ) callconv(.c) *g.calc.Summaries {
174
171
  return &options.calc.summaries;
175
172
  }
176
173
 
@@ -179,7 +176,7 @@ pub fn gen(comptime num: comptime_int) type {
179
176
  c1: pkmn.Choice,
180
177
  c2: pkmn.Choice,
181
178
  opts: ?*battle_options,
182
- ) callconv(.C) pkmn.Result {
179
+ ) callconv(.c) pkmn.Result {
183
180
  if ((pkmn.options.log or pkmn.options.chance or pkmn.options.calc) and opts != null) {
184
181
  return battle.update(c1, c2, opts.?) catch return @bitCast(ERROR);
185
182
  }
@@ -192,64 +189,12 @@ pub fn gen(comptime num: comptime_int) type {
192
189
  request: u8,
193
190
  out: [*]u8,
194
191
  n: usize,
195
- ) callconv(.C) u8 {
196
- assert(player <= @field(@typeInfo(pkmn.Player), @tagName(Enum)).fields.len);
197
- assert(request <= @field(@typeInfo(pkmn.Choice.Type), @tagName(Enum)).fields.len);
192
+ ) callconv(.c) u8 {
193
+ assert(player <= @typeInfo(pkmn.Player).@"enum".fields.len);
194
+ assert(request <= @typeInfo(pkmn.Choice.Type).@"enum".fields.len);
198
195
 
199
196
  assert(!pkmn.options.showdown or n > 0);
200
197
  return battle.choices(@enumFromInt(player), @enumFromInt(request), @ptrCast(out[0..n]));
201
198
  }
202
199
  };
203
200
  }
204
-
205
- pub fn exports() type {
206
- @export(choice_init, .{ .name = "pkmn_choice_init", .linkage = Strong });
207
- @export(choice_type, .{ .name = "pkmn_choice_type", .linkage = Strong });
208
- @export(choice_data, .{ .name = "pkmn_choice_data", .linkage = Strong });
209
-
210
- @export(result_type, .{ .name = "pkmn_result_type", .linkage = Strong });
211
- @export(result_p1, .{ .name = "pkmn_result_p1", .linkage = Strong });
212
- @export(result_p2, .{ .name = "pkmn_result_p2", .linkage = Strong });
213
-
214
- @export(err, .{ .name = "pkmn_error", .linkage = Strong });
215
-
216
- @export(psrng_init, .{ .name = "pkmn_psrng_init", .linkage = Strong });
217
- @export(psrng_next, .{ .name = "pkmn_psrng_next", .linkage = Strong });
218
-
219
- @export(rational_init, .{ .name = "pkmn_rational_init", .linkage = Strong });
220
- @export(rational_reduce, .{ .name = "pkmn_rational_reduce", .linkage = Strong });
221
- @export(rational_numerator, .{ .name = "pkmn_rational_numerator", .linkage = Strong });
222
- @export(rational_denominator, .{ .name = "pkmn_rational_denominator", .linkage = Strong });
223
-
224
- @export(
225
- gen(1).battle_options_set,
226
- .{ .name = "pkmn_gen1_battle_options_set", .linkage = Strong },
227
- );
228
- @export(
229
- gen(1).battle_options_chance_probability,
230
- .{ .name = "pkmn_gen1_battle_options_chance_probability", .linkage = Strong },
231
- );
232
- @export(
233
- gen(1).battle_options_chance_actions,
234
- .{ .name = "pkmn_gen1_battle_options_chance_actions", .linkage = Strong },
235
- );
236
- @export(
237
- gen(1).battle_options_chance_durations,
238
- .{ .name = "pkmn_gen1_battle_options_chance_durations", .linkage = Strong },
239
- );
240
- @export(
241
- gen(1).battle_options_calc_summaries,
242
- .{ .name = "pkmn_gen1_battle_options_calc_summaries", .linkage = Strong },
243
- );
244
-
245
- @export(
246
- gen(1).battle_update,
247
- .{ .name = "pkmn_gen1_battle_update", .linkage = Strong },
248
- );
249
- @export(
250
- gen(1).battle_choices,
251
- .{ .name = "pkmn_gen1_battle_choices", .linkage = Strong },
252
- );
253
-
254
- return struct {};
255
- }
@@ -1,14 +1,10 @@
1
+ const c = @import("napi");
1
2
  const js = @import("../common/js.zig");
2
3
  const pkmn = @import("../pkmn.zig");
3
4
  const std = @import("std");
4
5
 
5
6
  const assert = std.debug.assert;
6
7
 
7
- const c = @cImport({
8
- @cDefine("NAPI_VERSION", "8");
9
- @cInclude("node_api.h");
10
- });
11
-
12
8
  pub fn register(env: c.napi_env) c.napi_value {
13
9
  const object = js.Object.init(env);
14
10
  const properties = [_]c.napi_property_descriptor{
@@ -53,7 +49,7 @@ fn bind(env: c.napi_env, gen: anytype) c.napi_value {
53
49
 
54
50
  fn update(gen: anytype) c.napi_callback {
55
51
  return struct {
56
- fn call(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
52
+ fn call(env: c.napi_env, info: c.napi_callback_info) callconv(.c) c.napi_value {
57
53
  var argc: usize = 4;
58
54
  var argv: [4]c.napi_value = undefined;
59
55
  assert(c.napi_get_cb_info(env, info, &argc, &argv, null, null) == c.napi_ok);
@@ -64,7 +60,7 @@ fn update(gen: anytype) c.napi_callback {
64
60
  assert(c.napi_get_arraybuffer_info(env, argv[0], &data, &len) == c.napi_ok);
65
61
  assert(len == @sizeOf(gen.Battle(gen.PRNG)));
66
62
 
67
- var battle: *gen.Battle(gen.PRNG) = @alignCast(@ptrCast(data.?));
63
+ var battle: *gen.Battle(gen.PRNG) = @ptrCast(@alignCast(data.?));
68
64
  const c1: pkmn.Choice = @bitCast(js.Number.get(env, argv[1], u8));
69
65
  const c2: pkmn.Choice = @bitCast(js.Number.get(env, argv[2], u8));
70
66
 
@@ -77,10 +73,10 @@ fn update(gen: anytype) c.napi_callback {
77
73
  assert(len == gen.LOGS_SIZE);
78
74
 
79
75
  const buf = @as([*]u8, @ptrCast(data.?))[0..gen.LOGS_SIZE];
80
- var stream: pkmn.protocol.ByteStream = .{ .buffer = buf };
76
+ var writer: pkmn.protocol.Writer = .{ .buffer = buf };
81
77
  // TODO: extract out
82
78
  var opts = pkmn.battle.options(
83
- pkmn.protocol.FixedLog{ .writer = stream.writer() },
79
+ pkmn.protocol.FixedLog{ .writer = &writer },
84
80
  gen.chance.NULL,
85
81
  gen.calc.NULL,
86
82
  );
@@ -95,7 +91,7 @@ fn update(gen: anytype) c.napi_callback {
95
91
 
96
92
  fn choices(gen: anytype) c.napi_callback {
97
93
  return struct {
98
- fn call(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
94
+ fn call(env: c.napi_env, info: c.napi_callback_info) callconv(.c) c.napi_value {
99
95
  var argc: usize = 4;
100
96
  var argv: [4]c.napi_value = undefined;
101
97
  assert(c.napi_get_cb_info(env, info, &argc, &argv, null, null) == c.napi_ok);
@@ -106,7 +102,7 @@ fn choices(gen: anytype) c.napi_callback {
106
102
  assert(c.napi_get_arraybuffer_info(env, argv[0], &data, &len) == c.napi_ok);
107
103
  assert(len == @sizeOf(gen.Battle(gen.PRNG)));
108
104
 
109
- var battle: *gen.Battle(gen.PRNG) = @alignCast(@ptrCast(data.?));
105
+ var battle: *gen.Battle(gen.PRNG) = @ptrCast(@alignCast(data.?));
110
106
  const player: pkmn.Player = @enumFromInt(js.Number.get(env, argv[1], u8));
111
107
  const request: pkmn.Choice.Type = @enumFromInt(js.Number.get(env, argv[2], u8));
112
108
 
@@ -3,9 +3,6 @@ const std = @import("std");
3
3
 
4
4
  const assert = std.debug.assert;
5
5
 
6
- const Enum = if (@hasField(std.builtin.Type, "enum")) .@"enum" else .Enum;
7
- const Strong = if (@hasField(std.builtin.GlobalLinkage, "strong")) .strong else .Strong;
8
-
9
6
  pub const options = pkmn.options;
10
7
 
11
8
  pub fn gen(comptime num: comptime_int) type {
@@ -19,13 +16,13 @@ pub fn gen(comptime num: comptime_int) type {
19
16
  c1: pkmn.Choice,
20
17
  c2: pkmn.Choice,
21
18
  options_: ?[*]u8,
22
- ) callconv(.C) pkmn.Result {
19
+ ) callconv(.c) pkmn.Result {
23
20
  return (if (options_) |o| result: {
24
21
  const buf = @as([*]u8, @ptrCast(o))[0..g.LOGS_SIZE];
25
- var stream: pkmn.protocol.ByteStream = .{ .buffer = buf };
22
+ var writer: pkmn.protocol.Writer = .{ .buffer = buf };
26
23
  // TODO: extract out
27
24
  var opts = pkmn.battle.options(
28
- pkmn.protocol.FixedLog{ .writer = stream.writer() },
25
+ pkmn.protocol.FixedLog{ .writer = &writer },
29
26
  g.chance.NULL,
30
27
  g.calc.NULL,
31
28
  );
@@ -38,9 +35,9 @@ pub fn gen(comptime num: comptime_int) type {
38
35
  player: u8,
39
36
  request: u8,
40
37
  buf: [*]u8,
41
- ) callconv(.C) u8 {
42
- assert(player <= @field(@typeInfo(pkmn.Player), @tagName(Enum)).fields.len);
43
- assert(request <= @field(@typeInfo(pkmn.Choice.Type), @tagName(Enum)).fields.len);
38
+ ) callconv(.c) u8 {
39
+ assert(player <= @typeInfo(pkmn.Player).@"enum".fields.len);
40
+ assert(request <= @typeInfo(pkmn.Choice.Type).@"enum".fields.len);
44
41
 
45
42
  const n = CHOICES_SIZE;
46
43
  return battle.choices(@enumFromInt(player), @enumFromInt(request), @ptrCast(buf[0..n]));
@@ -48,12 +45,6 @@ pub fn gen(comptime num: comptime_int) type {
48
45
  };
49
46
  }
50
47
 
51
- pub fn exports() type {
52
- @export(gen(1).update, .{ .name = "GEN1_update", .linkage = Strong });
53
- @export(gen(1).choices, .{ .name = "GEN1_choices", .linkage = Strong });
54
- return struct {};
55
- }
56
-
57
48
  fn size(n: usize) u32 {
58
49
  return std.math.ceilPowerOfTwo(u32, @as(u32, @intCast(n))) catch unreachable;
59
50
  }
@@ -3,19 +3,12 @@ const std = @import("std");
3
3
  const expectEqual = std.testing.expectEqual;
4
4
  const assert = std.debug.assert;
5
5
 
6
- const Int = if (@hasField(std.builtin.Type, "int")) .int else .Int;
7
- const Enum = if (@hasField(std.builtin.Type, "enum")) .@"enum" else .Enum;
8
-
9
6
  /// Helpers for working with bit-packed arrays of non-powers-of-2 types.
10
7
  /// NOTE: ziglang/zig#12547
11
8
  pub fn Array(comptime n: comptime_int, comptime U: type) type {
12
9
  return struct {
13
10
  const size = @bitSizeOf(U);
14
- const options: std.builtin.Type.Int = .{ .signedness = .unsigned, .bits = size * n };
15
- pub const T = @Type(if (@hasField(std.builtin.Type, "int"))
16
- .{ .int = options }
17
- else
18
- .{ .Int = options });
11
+ pub const T = @Int(.unsigned, size * n);
19
12
 
20
13
  /// Returns the value stored at index `i` in the array `a`.
21
14
  pub fn get(a: T, i: usize) U {
@@ -24,8 +17,8 @@ pub fn Array(comptime n: comptime_int, comptime U: type) type {
24
17
  const mask: T = (1 << size) - 1;
25
18
  const result = (a >> @intCast(shift)) & mask;
26
19
  return switch (@typeInfo(U)) {
27
- Enum => @enumFromInt(result),
28
- Int => @intCast(result),
20
+ .@"enum" => @enumFromInt(result),
21
+ .int => @intCast(result),
29
22
  else => unreachable,
30
23
  };
31
24
  }
@@ -34,8 +27,8 @@ pub fn Array(comptime n: comptime_int, comptime U: type) type {
34
27
  pub fn set(a: T, i: usize, val: U) T {
35
28
  assert(i < n);
36
29
  const v: T = switch (@typeInfo(U)) {
37
- Enum => @intFromEnum(val),
38
- Int => @intCast(val),
30
+ .@"enum" => @intFromEnum(val),
31
+ .int => @intCast(val),
39
32
  else => unreachable,
40
33
  };
41
34
  const shift = i * size;
@@ -3,19 +3,9 @@ const std = @import("std");
3
3
  const debug = std.debug;
4
4
  const io = std.io;
5
5
 
6
- const Array = if (@hasField(std.builtin.Type, "array")) .array else .Array;
7
- const Enum = if (@hasField(std.builtin.Type, "enum")) .@"enum" else .Enum;
8
- const Optional = if (@hasField(std.builtin.Type, "optional")) .optional else .Optional;
9
- const Pointer = if (@hasField(std.builtin.Type, "pointer")) .pointer else .Pointer;
10
- const Struct = if (@hasField(std.builtin.Type, "struct")) .@"struct" else .Struct;
11
- const Union = if (@hasField(std.builtin.Type, "union")) .@"union" else .Union;
12
-
13
6
  pub fn print(value: anytype) void {
14
- if (@hasDecl(debug, "lockStdErr")) debug.lockStdErr() else debug.getStderrMutex().lock();
15
- defer if (@hasDecl(debug, "unlockStdErr"))
16
- debug.unlockStdErr()
17
- else
18
- debug.getStderrMutex().unlock();
7
+ debug.lockStdErr();
8
+ defer debug.unlockStdErr();
19
9
  const stderr = io.getStdErr().writer();
20
10
 
21
11
  nosuspend {
@@ -29,7 +19,7 @@ pub fn print(value: anytype) void {
29
19
  }) catch return;
30
20
  } else {
31
21
  switch (@typeInfo(@TypeOf(value))) {
32
- Struct => |info| {
22
+ .@"struct" => |info| {
33
23
  if (info.is_tuple) {
34
24
  inline for (info.fields, 0..) |f, i| {
35
25
  inspect(@field(value, f.name));
@@ -52,32 +42,32 @@ fn inspect(value: anytype) void {
52
42
  nosuspend {
53
43
  const err = "Unable to format type '" ++ @typeName(@TypeOf(value)) ++ "'";
54
44
  switch (@typeInfo(@TypeOf(value))) {
55
- Array => |info| {
45
+ .array => |info| {
56
46
  if (info.child == u8) return stderr.print("{s}", .{value}) catch return;
57
47
  @compileError(err);
58
48
  },
59
- Pointer => |ptr_info| switch (ptr_info.size) {
60
- .One => switch (@typeInfo(ptr_info.child)) {
61
- Array => |info| {
49
+ .pointer => |ptr_info| switch (ptr_info.size) {
50
+ .one => switch (@typeInfo(ptr_info.child)) {
51
+ .array => |info| {
62
52
  if (info.child == u8) return stderr.print("{s}", .{value}) catch return;
63
53
  @compileError(err);
64
54
  },
65
- Enum, Union, Struct => return inspect(value.*),
55
+ .@"enum", .@"union", .@"struct" => return inspect(value.*),
66
56
  else => @compileError(err),
67
57
  },
68
- .Many, .C => {
58
+ .many, .c => {
69
59
  if (ptr_info.sentinel) |_| return inspect(std.mem.span(value));
70
60
  if (ptr_info.child == u8) {
71
61
  return stderr.print("{s}", .{std.mem.span(value)}) catch return;
72
62
  }
73
63
  @compileError(err);
74
64
  },
75
- .Slice => {
65
+ .slice => {
76
66
  if (ptr_info.child == u8) return stderr.print("{s}", .{value}) catch return;
77
67
  @compileError(err);
78
68
  },
79
69
  },
80
- Optional => stderr.print("{?}", .{value}) catch return,
70
+ .optional => stderr.print("{?}", .{value}) catch return,
81
71
  else => stderr.print("{}", .{value}) catch return,
82
72
  }
83
73
  }
@@ -1,14 +1,8 @@
1
1
  const std = @import("std");
2
+ const c = @import("napi");
2
3
 
3
4
  const assert = std.debug.assert;
4
5
 
5
- const Int = if (@hasField(std.builtin.Type, "int")) .int else .Int;
6
-
7
- const c = @cImport({
8
- @cDefine("NAPI_VERSION", "8");
9
- @cInclude("node_api.h");
10
- });
11
-
12
6
  pub const Array = struct {
13
7
  pub fn init(env: c.napi_env, o: struct { length: ?usize }) c.napi_value {
14
8
  var result: c.napi_value = undefined;
@@ -37,7 +31,7 @@ pub const Number = struct {
37
31
  const T = @TypeOf(value);
38
32
  var result: c.napi_value = undefined;
39
33
  assert(c.napi_ok == switch (@typeInfo(T)) {
40
- Int => |info| switch (info.bits) {
34
+ .int => |info| switch (info.bits) {
41
35
  0...32 => switch (info.signedness) {
42
36
  .signed => c.napi_create_int32(env, @as(i32, value), &result),
43
37
  .unsigned => c.napi_create_uint32(env, @as(u32, value), &result),
@@ -52,7 +46,7 @@ pub const Number = struct {
52
46
 
53
47
  pub fn get(env: c.napi_env, value: c.napi_value, comptime T: type) T {
54
48
  switch (@typeInfo(T)) {
55
- Int => |info| switch (info.bits) {
49
+ .int => |info| switch (info.bits) {
56
50
  0...32 => switch (info.signedness) {
57
51
  .signed => {
58
52
  var result: i32 = undefined;
@@ -3,38 +3,28 @@ const std = @import("std");
3
3
  const expect = std.testing.expect;
4
4
  const assert = std.debug.assert;
5
5
 
6
- const Bool = if (@hasField(std.builtin.Type, "bool")) .bool else .Bool;
7
-
8
- // TODO: ziglang/zig#104
6
+ /// Optimized optional representation which stores the empty None value as a sentinel.
7
+ /// NOTE: ziglang/zig#104
9
8
  pub fn Optional(comptime T: type) type {
10
9
  const fields = std.meta.fields(switch (@typeInfo(T)) {
11
- Bool => enum { false, true },
10
+ .bool => enum { false, true },
12
11
  else => T,
13
12
  });
14
13
 
15
- var enumFields: [fields.len + 1]std.builtin.Type.EnumField = undefined;
16
- var decls = [_]std.builtin.Type.Declaration{};
14
+ const tagType = std.math.IntFittingRange(0, fields.len);
15
+ var fieldNames: [fields.len + 1][]const u8 = undefined;
16
+ var fieldValues: [fields.len + 1]tagType = undefined;
17
17
 
18
- enumFields[0] = .{ .name = "None", .value = 0 };
18
+ fieldNames[0] = "None";
19
+ fieldValues[0] = 0;
19
20
 
20
21
  inline for (fields, 1..) |field, i| {
21
22
  assert(!std.mem.eql(u8, field.name, "None"));
22
- enumFields[i] = .{
23
- .name = field.name,
24
- .value = i,
25
- };
23
+ fieldNames[i] = field.name;
24
+ fieldValues[i] = i;
26
25
  }
27
26
 
28
- const options: std.builtin.Type.Enum = .{
29
- .tag_type = std.math.IntFittingRange(0, fields.len),
30
- .fields = &enumFields,
31
- .decls = &decls,
32
- .is_exhaustive = true,
33
- };
34
- return @Type(if (@hasField(std.builtin.Type, "enum"))
35
- .{ .@"enum" = options }
36
- else
37
- .{ .Enum = options });
27
+ return @Enum(tagType, .exhaustive, &fieldNames, &fieldValues);
38
28
  }
39
29
 
40
30
  test Optional {