From 103e0543534decd5f17267202e178e7288283c38 Mon Sep 17 00:00:00 2001 From: Thomas Coratger <60488569+tcoratger@users.noreply.github.com> Date: Fri, 5 Jan 2024 16:28:07 +0100 Subject: [PATCH] make new zig master happy (#298) * make new zig master happy * update to new zig master * update cli.App creation * test ci * fix test --- build.zig | 12 +- build.zig.zon | 4 +- build_helpers.zig | 16 +- src/cmd/cmd.zig | 157 ++++++++++++------ .../builtins/builtin_runner/range_check.zig | 7 +- 5 files changed, 125 insertions(+), 71 deletions(-) diff --git a/build.zig b/build.zig index 69f28ae1..b29f946d 100644 --- a/build.zig +++ b/build.zig @@ -46,8 +46,8 @@ pub fn build(b: *std.Build) void { // ************************************************************** // expose ziggy-starkdust as a module _ = b.addModule(package_name, .{ - .source_file = .{ .path = package_path }, - .dependencies = deps, + .root_source_file = .{ .path = package_path }, + .imports = deps, }); // ************************************************************** @@ -62,7 +62,7 @@ pub fn build(b: *std.Build) void { .optimize = optimize, }); // Add dependency modules to the library. - for (deps) |mod| lib.addModule( + for (deps) |mod| lib.root_module.addImport( mod.name, mod.module, ); @@ -83,7 +83,7 @@ pub fn build(b: *std.Build) void { .optimize = optimize, }); // Add dependency modules to the executable. - for (deps) |mod| exe.addModule( + for (deps) |mod| exe.root_module.addImport( mod.name, mod.module, ); @@ -93,7 +93,7 @@ pub fn build(b: *std.Build) void { b.installArtifact(exe); exe.addIncludePath(.{ .path = "./src/math/crypto/starknet_crypto/" }); - exe.addObjectFile(std.build.LazyPath{ .path = "./src/math/crypto/starknet_crypto/libstarknet_crypto.a" }); + exe.addObjectFile(std.Build.LazyPath{ .path = "./src/math/crypto/starknet_crypto/libstarknet_crypto.a" }); exe.linkSystemLibrary("unwind"); // This *creates* a Run step in the build graph, to be executed when another @@ -138,7 +138,7 @@ pub fn build(b: *std.Build) void { }); // Add dependency modules to the tests. - for (deps) |mod| unit_tests.addModule( + for (deps) |mod| unit_tests.root_module.addImport( mod.name, mod.module, ); diff --git a/build.zig.zon b/build.zig.zon index 32045bd4..ee1fa329 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -10,8 +10,8 @@ }, .dependencies = .{ .@"zig-cli" = .{ - .url = "https://github.com/sam701/zig-cli/archive/f9e099bacee46b4776afbe7d6326656b33150b3b.tar.gz", - .hash = "1220837b956355c73ccd18f1cce2fc829d7de04a71434e796f9ff913642ac908a1e5", + .url = "https://github.com/sam701/zig-cli/archive/refs/heads/main.tar.gz", + .hash = "1220b1cc7c256080eb3229d95a8519d6a33035dc83558a8ca1ed6e407008a38fe2c2", }, }, } diff --git a/build_helpers.zig b/build_helpers.zig index 55c41a79..bbcc341f 100644 --- a/build_helpers.zig +++ b/build_helpers.zig @@ -6,19 +6,19 @@ pub const Dependency = struct { module_name: []const u8, }; -/// Generate an array of Build.ModuleDependency from the external dependencies. +/// Generate an array of Build.Module.Import from the external dependencies. /// # Arguments /// * `b` - The build object. /// * `external_dependencies` - The external dependencies. /// * `dependencies_opts` - The options to use when generating the dependency modules. /// # Returns -/// A new array of Build.ModuleDependency. +/// A new array of Build.Module.Import. pub fn generateModuleDependencies( b: *std.Build, external_dependencies: []const Dependency, dependencies_opts: anytype, -) ![]std.Build.ModuleDependency { - var dependency_modules = std.ArrayList(*std.build.Module).init(b.allocator); +) ![]std.Build.Module.Import { + var dependency_modules = std.ArrayList(*std.Build.Module).init(b.allocator); defer _ = dependency_modules.deinit(); // Populate dependency modules. @@ -36,19 +36,19 @@ pub fn generateModuleDependencies( ); } -/// Convert an array of Build.Module pointers to an array of Build.ModuleDependency. +/// Convert an array of Build.Module pointers to an array of Build.Module.Import. /// # Arguments /// * `allocator` - The allocator to use for the new array. /// * `modules` - The array of Build.Module pointers to convert. /// * `ext_deps` - The array of external dependencies. /// # Returns -/// A new array of Build.ModuleDependency. +/// A new array of Build.Module.Import. fn toModuleDependencyArray( allocator: std.mem.Allocator, modules: []const *std.Build.Module, ext_deps: []const Dependency, -) ![]std.Build.ModuleDependency { - var deps = std.ArrayList(std.Build.ModuleDependency).init(allocator); +) ![]std.Build.Module.Import { + var deps = std.ArrayList(std.Build.Module.Import).init(allocator); defer deps.deinit(); for ( diff --git a/src/cmd/cmd.zig b/src/cmd/cmd.zig index 675e746a..7cf83d4b 100644 --- a/src/cmd/cmd.zig +++ b/src/cmd/cmd.zig @@ -1,7 +1,3 @@ -// ************************************************************ -// * IMPORTS * -// ************************************************************ - // Core imports. const std = @import("std"); // Dependencies imports. @@ -17,111 +13,161 @@ const CairoRunner = cairo_runner.CairoRunner; const ProgramJson = @import("../vm/types/programjson.zig").ProgramJson; const cairo_run = @import("../vm/cairo_run.zig"); -// ************************************************************ -// * GLOBAL VARIABLES * -// ************************************************************ - var gpa = std.heap.GeneralPurposeAllocator(.{}){}; const gpa_allocator = gpa.allocator(); -// ************************************************************ -// * CLI OPTIONS * -// ************************************************************ - -var config = Config{ .proof_mode = false, .enable_trace = false }; +// Configuration settings for the CLI. +var config = Config{ + .proof_mode = false, + .enable_trace = false, +}; +// Command-line option for enabling proof mode. var execute_proof_mode_option = cli.Option{ + // The full name of the option. .long_name = "proof-mode", + // Description of the option's purpose. .help = "Whether to run in proof mode or not.", + // Short alias for the option. .short_alias = 'p', + // Reference to the proof mode configuration. .value_ref = cli.mkRef(&config.proof_mode), + // Indicates if the option is required. .required = false, }; +// Command-line option for specifying the filename. var program_option = cli.Option{ + // The full name of the option. .long_name = "filename", + // Description of the option's purpose. .help = "The location of the program to be evaluated.", + // Short alias for the option. .short_alias = 'f', + // Reference to the program filename. .value_ref = cli.mkRef(&config.filename), + // Indicates if the option is required. .required = true, }; +// Command-line option for enabling trace mode. var enable_trace = cli.Option{ + // The full name of the option. .long_name = "enable-trace", - .help = "Enable trace mode", + // Description of the option's purpose. + .help = "Enable trace mode.", + // Short alias for the option. .short_alias = 't', + // Reference to the trace mode configuration. .value_ref = cli.mkRef(&config.enable_trace), + // Indicates if the option is required. .required = false, }; +// Command-line option for specifying the output trace file. var output_trace = cli.Option{ + // The full name of the option. .long_name = "output-trace", - .help = "File where the register execution cycles are written. ", + // Description of the option's purpose. + .help = "File where the register execution cycles are written.", + // Short alias for the option. .short_alias = 'o', + // Reference to the output trace file. .value_ref = cli.mkRef(&config.output_trace), + // Indicates if the option is required. .required = false, }; +// Command-line option for specifying the output memory file. var output_memory = cli.Option{ + // The full name of the option. .long_name = "output-memory", + // Description of the option's purpose. .help = "File where the memory post-execution is written.", + // Short alias for the option. .short_alias = 'm', + // Reference to the output memory file. .value_ref = cli.mkRef(&config.output_memory), + // Indicates if the option is required. .required = false, }; +// Command-line option for specifying the memory layout. var layout = cli.Option{ + // The full name of the option. .long_name = "layout", + // Description of the option's purpose. .help = "The memory layout to use.", + // Short alias for the option. .short_alias = 'l', + // Reference to the memory layout. .value_ref = cli.mkRef(&config.layout), + // Indicates if the option is required. .required = false, }; -// ************************************************************ -// * CLI APP * -// ************************************************************ - -// Define the CLI app. +// Define the CLI app var app = &cli.App{ - .name = "ziggy-starkdust", - .description = - \\Cairo Virtual Machine written in Zig. - \\Highly experimental, use at your own risk. - , + // Version of the application. .version = "0.0.1", + // Author information for the application. .author = "StarkWare & Contributors", - .subcommands = &.{ - &cli.Command{ - .name = "execute", - .help = "Execute a cairo program.", - .description = - \\Execute a cairo program with the virtual machine. + // Configuration for the main command. + .command = .{ + // Name of the main command. + .name = "ziggy-starkdust", + // Description of the main command. + .description = .{ + // One-line summary of the main command. + .one_line = "Cairo VM written in Zig.", + // Detailed description of the main command. + .detailed = + \\Cairo Virtual Machine written in Zig. + \\Highly experimental, use at your own risk. , - .action = execute, - .options = &.{ - &execute_proof_mode_option, - &layout, - &enable_trace, - &program_option, - &output_trace, + }, + // Target subcommand details. + .target = .{ + // Subcommands of the main command. + .subcommands = &.{ + &.{ + // Name of the subcommand. + .name = "execute", + // Description of the subcommand. + .description = .{ + // One-line summary of the subcommand. + .one_line = "Execute a cairo program with the virtual machine.", + }, + // Options for the subcommand. + .options = &.{ + &execute_proof_mode_option, + &layout, + &enable_trace, + &program_option, + &output_trace, + }, + // Action to be executed for the subcommand. + .target = .{ .action = .{ .exec = execute } }, + }, }, }, }, }; -/// Run the CLI app. +/// Runs the Command-Line Interface application. +/// +/// This function initializes and executes the CLI app with the provided configuration +/// and allocator, handling errors and the application's execution flow. +/// +/// # Errors +/// +/// Returns an error of type `UsageError` if there's a misuse of the CLI by the user, +/// such as requesting a configuration not supported by the current build, +/// for example, when execution traces are globally disabled. pub fn run() !void { - return cli.run( - app, - gpa_allocator, - ); + return cli.run(app, gpa_allocator); } -// ************************************************************ -// * CLI COMMANDS * -// ************************************************************ - /// Errors that occur because the user misused the CLI. const UsageError = error{ /// Occurs when the user requests a config that is not supported by the current build. @@ -130,12 +176,17 @@ const UsageError = error{ IncompatibleBuildOptions, }; -// execute entrypoint -fn execute(_: []const []const u8) !void { - std.log.debug( - "Running Cairo VM...\n", - .{}, - ); +/// Entry point for the execution of the Cairo Virtual Machine. +/// +/// This function is responsible for executing the Cairo Virtual Machine based on the provided +/// configuration, allocator, and build options. +/// +/// # Errors +/// +/// Returns a `UsageError` if there's a misuse of the CLI, specifically if tracing is attempted +/// while it's disabled in the build. +fn execute() anyerror!void { + std.log.debug("Running Cairo VM...\n", .{}); if (build_options.trace_disable and config.enable_trace) { std.log.err("Tracing is disabled in this build.\n", .{}); diff --git a/src/vm/builtins/builtin_runner/range_check.zig b/src/vm/builtins/builtin_runner/range_check.zig index 84a2a919..710d42fd 100644 --- a/src/vm/builtins/builtin_runner/range_check.zig +++ b/src/vm/builtins/builtin_runner/range_check.zig @@ -409,9 +409,12 @@ test "Range Check: validation rule should return Relocatable in array successful try mem.memory.set(std.testing.allocator, relo, MaybeRelocatable.fromFelt(Felt252.zero())); defer mem.memory.deinitData(std.testing.allocator); - const result = try rangeCheckValidationRule(mem.memory, relo); // assert - try std.testing.expectEqual(relo, result[0]); + try std.testing.expectEqualSlices( + Relocatable, + &[_]Relocatable{relo}, + try rangeCheckValidationRule(mem.memory, relo), + ); } test "Range Check: validation rule should return erorr out of bounds" {