@@ -186,6 +186,9 @@ fn workloadWidth(name: []const u8) !usize {
186186 if (std .mem .eql (u8 , name , "representative_frontend_bigint_hex_1024" )) return 1024 ;
187187 if (std .mem .eql (u8 , name , "representative_frontend_bigint_hex_2048" )) return 2048 ;
188188 if (std .mem .eql (u8 , name , "representative_frontend_bigint_hex_4096" )) return 4096 ;
189+ if (std .mem .eql (u8 , name , "representative_frontend_modules_1024" )) return 1024 ;
190+ if (std .mem .eql (u8 , name , "representative_frontend_modules_2048" )) return 2048 ;
191+ if (std .mem .eql (u8 , name , "representative_frontend_modules_4096" )) return 4096 ;
189192 return error .InvalidWorkload ;
190193}
191194
@@ -254,6 +257,10 @@ fn isRadixBigIntWorkload(name: []const u8) bool {
254257 return std .mem .startsWith (u8 , name , "representative_frontend_bigint_hex_" );
255258}
256259
260+ fn isModuleWorkload (name : []const u8 ) bool {
261+ return std .mem .startsWith (u8 , name , "representative_frontend_modules_" );
262+ }
263+
257264fn strictFunctionSource (allocator : std.mem.Allocator , width : usize , escaped : bool ) ! []const u8 {
258265 var source : std .ArrayListUnmanaged (u8 ) = .empty ;
259266 try source .appendSlice (allocator , "function strictWidth(" );
@@ -437,6 +444,33 @@ fn radixBigIntSource(allocator: std.mem.Allocator, width: usize) !RadixBigIntInp
437444 };
438445}
439446
447+ fn moduleSource (allocator : std.mem.Allocator , width : usize ) ! []const u8 {
448+ var source : std .ArrayListUnmanaged (u8 ) = .empty ;
449+ try source .appendSlice (allocator , "import {" );
450+ for (0.. width ) | index | {
451+ if (index != 0 ) try source .append (allocator , ',' );
452+ try source .appendSlice (allocator , "value" );
453+ try source .appendSlice (allocator , try std .fmt .allocPrint (allocator , "{d}" , .{index }));
454+ try source .appendSlice (allocator , " as imported" );
455+ try source .appendSlice (allocator , try std .fmt .allocPrint (allocator , "{d}" , .{index }));
456+ }
457+ try source .appendSlice (allocator , "} from './dependency.js'; export {" );
458+ for (0.. width ) | index | {
459+ if (index != 0 ) try source .append (allocator , ',' );
460+ try source .appendSlice (allocator , "imported" );
461+ try source .appendSlice (allocator , try std .fmt .allocPrint (allocator , "{d}" , .{index }));
462+ try source .appendSlice (allocator , " as value" );
463+ try source .appendSlice (allocator , try std .fmt .allocPrint (allocator , "{d}" , .{index }));
464+ }
465+ try source .appendSlice (allocator , "}; export const marker = 1;\n " );
466+ return source .items ;
467+ }
468+
469+ fn indexedName (name : []const u8 , prefix : []const u8 , index : usize ) ! bool {
470+ if (! std .mem .startsWith (u8 , name , prefix )) return false ;
471+ return (try std .fmt .parseUnsigned (usize , name [prefix .len .. ], 10 )) == index ;
472+ }
473+
440474fn validatedRadixBigIntChecksum (text : []const u8 , expected : []const u8 ) ! usize {
441475 if (! std .mem .eql (u8 , text , expected )) return error .InvalidProgram ;
442476 var checksum : u32 = 2_166_136_261 ;
@@ -465,7 +499,46 @@ fn parseOnce(
465499 };
466500 const parser_allocator = if (observation != null ) measured .allocator () else arena .allocator ();
467501 var parser = try js .Parser .init (parser_allocator , source );
468- const program = try parser .parseProgram ();
502+ const program = if (isModuleWorkload (workload )) try parser .parseModule () else try parser .parseProgram ();
503+ if (isModuleWorkload (workload )) {
504+ const width = try workloadWidth (workload );
505+ if (program .program .len != 3 or parser .statement_locations .items .len != 1 )
506+ return error .InvalidProgram ;
507+ const import_node = program .program [0 ];
508+ const export_node = program .program [1 ];
509+ const marker_node = program .program [2 ];
510+ if (import_node .* != .import_decl or export_node .* != .export_decl or marker_node .* != .export_decl )
511+ return error .InvalidProgram ;
512+ const import_decl = import_node .import_decl ;
513+ const export_decl = export_node .export_decl ;
514+ if (! std .mem .eql (u8 , import_decl .specifier , "./dependency.js" ) or
515+ import_decl .entries .len != width or export_decl .entries .len != width or export_decl .from .len != 0 )
516+ return error .InvalidProgram ;
517+ var checksum = program .program .len + parser .statement_locations .items .len +
518+ import_decl .specifier .len + import_decl .entries .len + export_decl .entries .len ;
519+ for (0.. width ) | index | {
520+ const imported = import_decl .entries [index ];
521+ const exported = export_decl .entries [index ];
522+ if (! try indexedName (imported .imported , "value" , index ) or
523+ ! try indexedName (imported .local , "imported" , index ) or imported .namespace or
524+ ! try indexedName (exported .local , "imported" , index ) or
525+ ! try indexedName (exported .exported , "value" , index ) or exported .imported .len != 0 )
526+ return error .InvalidProgram ;
527+ checksum += imported .imported .len + imported .local .len + exported .local .len + exported .exported .len + index ;
528+ }
529+ const marker = marker_node .export_decl .declaration orelse return error .InvalidProgram ;
530+ if (marker .* != .var_decl or marker .var_decl .kind != .@"const" or
531+ ! std .mem .eql (u8 , marker .var_decl .name , "marker" )) return error .InvalidProgram ;
532+ const initializer = marker .var_decl .init orelse return error .InvalidProgram ;
533+ if (initializer .* != .number or initializer .number != 1 ) return error .InvalidProgram ;
534+ const location = parser .statement_locations .items [0 ];
535+ if (location .node != marker or location .location .line != 1 or
536+ location .location .column <= 1 or location .location .byte_offset == 0 )
537+ return error .InvalidProgram ;
538+ checksum += marker .var_decl .name .len + @as (usize , @intFromFloat (initializer .number )) +
539+ location .location .byte_offset + location .location .column ;
540+ return checksum ;
541+ }
469542 const declaration = program .program [0 ];
470543 if (isRadixBigIntWorkload (workload )) {
471544 if (declaration .* != .var_decl ) return error .InvalidProgram ;
@@ -652,8 +725,11 @@ pub fn main(init: std.process.Init) !void {
652725 const numeric_workload = isNumericWorkload (workload );
653726 const decimal_bigint_workload = isDecimalBigIntWorkload (workload );
654727 const radix_bigint_workload = isRadixBigIntWorkload (workload );
728+ const module_workload = isModuleWorkload (workload );
655729 var expected_radix_bigint : ? []const u8 = null ;
656- const source = if (isTaggedSubstitutionWorkload (workload ))
730+ const source = if (module_workload )
731+ try moduleSource (init .arena .allocator (), width )
732+ else if (isTaggedSubstitutionWorkload (workload ))
657733 try taggedSubstitutionSource (init .arena .allocator (), width )
658734 else if (private_name_workload )
659735 try privateClassSource (
0 commit comments