@@ -230,7 +230,7 @@ pub fn sendEvent(
230230}
231231
232232fn getParams (
233- alloc : std.mem.Allocator ,
233+ alloc : ? std.mem.Allocator ,
234234 comptime T : type ,
235235 scanner : * std.json.Scanner ,
236236 key : []const u8 ,
@@ -250,15 +250,17 @@ fn getParams(
250250 }
251251 if (finished == 0 ) break ;
252252 }
253- return void {};
253+ return null ;
254+ } else {
255+ std .debug .assert (alloc != null );
256+
257+ // parse "params"
258+ const options = std.json.ParseOptions {
259+ .max_value_len = scanner .input .len ,
260+ .allocate = .alloc_always ,
261+ };
262+ return try std .json .innerParse (T , alloc .? , scanner , options );
254263 }
255-
256- // parse "params"
257- const options = std.json.ParseOptions {
258- .max_value_len = scanner .input .len ,
259- .allocate = .alloc_if_needed ,
260- };
261- return try std .json .innerParse (T , alloc , scanner , options );
262264}
263265
264266fn getId (scanner : * std.json.Scanner , key : []const u8 ) ! ? u16 {
@@ -279,45 +281,78 @@ fn getSessionId(scanner: *std.json.Scanner, key: []const u8) !?[]const u8 {
279281 return try nextString (scanner );
280282}
281283
282- pub fn getMsg (
283- alloc : std.mem.Allocator ,
284- _id : ? u16 ,
285- comptime params_T : type ,
286- scanner : * std.json.Scanner ,
287- ) ! struct { id : u16 , params : ? params_T , sessionID : ? []const u8 } {
288- var id_msg : ? u16 = null ;
289- var params : ? params_T = null ;
290- var sessionID : ? []const u8 = null ;
291-
292- var t : std.json.Token = undefined ;
293-
294- while (true ) {
295- t = try scanner .next ();
296- if (t == .object_end ) break ;
297- if (t != .string ) {
298- return error .WrongTokenType ;
299- }
300- if (_id == null and id_msg == null ) {
301- id_msg = try getId (scanner , t .string );
302- if (id_msg != null ) continue ;
303- }
304- if (params == null ) {
305- params = try getParams (alloc , params_T , scanner , t .string );
306- if (params != null ) continue ;
307- }
308- if (sessionID == null ) {
309- sessionID = try getSessionId (scanner , t .string );
310- }
311- }
284+ pub fn Msg (T : type ) type {
285+ return struct {
286+ arena : ? * std.heap.ArenaAllocator = null ,
287+ id : u16 ,
288+ params : ? T ,
289+ sessionID : ? []const u8 ,
290+
291+ const Self = @This ();
292+
293+ pub fn get (
294+ alloc : std.mem.Allocator ,
295+ _id : ? u16 ,
296+ scanner : * std.json.Scanner ,
297+ ) ! Self {
298+ var arena : ? * std.heap.ArenaAllocator = null ;
299+ var allocator : ? std.mem.Allocator = null ;
300+
301+ if (T != void ) {
302+ arena = try alloc .create (std .heap .ArenaAllocator );
303+ errdefer alloc .destroy (arena .? );
304+ arena .?.* = std .heap .ArenaAllocator .init (alloc );
305+ errdefer arena .? .deinit ();
306+ allocator = arena .? .allocator ();
307+ }
308+ var id_msg : ? u16 = null ;
309+ var params : ? T = null ;
310+ var sessionID : ? []const u8 = null ;
311+
312+ var t : std.json.Token = undefined ;
313+
314+ while (true ) {
315+ t = try scanner .next ();
316+ if (t == .object_end ) break ;
317+ if (t != .string ) {
318+ return error .WrongTokenType ;
319+ }
320+ if (_id == null and id_msg == null ) {
321+ id_msg = try getId (scanner , t .string );
322+ if (id_msg != null ) continue ;
323+ }
324+ if (params == null ) {
325+ params = try getParams (allocator , T , scanner , t .string );
326+ if (params != null ) continue ;
327+ }
328+ if (sessionID == null ) {
329+ sessionID = try getSessionId (scanner , t .string );
330+ }
331+ }
312332
313- // end
314- t = try scanner .next ();
315- if (t != .end_of_document ) return error .CDPMsgEnd ;
333+ // end
334+ t = try scanner .next ();
335+ if (t != .end_of_document ) return error .CDPMsgEnd ;
316336
317- // check id
318- if (_id == null and id_msg == null ) return error .RequestWithoutID ;
337+ // check id
338+ if (_id == null and id_msg == null ) return error .RequestWithoutID ;
319339
320- return .{ .id = _id orelse id_msg .? , .params = params , .sessionID = sessionID };
340+ return .{
341+ .arena = arena ,
342+ .id = _id orelse id_msg .? ,
343+ .params = params ,
344+ .sessionID = sessionID ,
345+ };
346+ }
347+
348+ pub fn deinit (self : Self ) void {
349+ if (self .arena ) | arena | {
350+ const allocator = arena .child_allocator ;
351+ arena .deinit ();
352+ allocator .destroy (arena );
353+ }
354+ }
355+ };
321356}
322357
323358// Common
0 commit comments