Skip to content

Commit

Permalink
make new zig master happy (keep-starknet-strange#298)
Browse files Browse the repository at this point in the history
* make new zig master happy

* update to new zig master

* update cli.App creation

* test ci

* fix test
  • Loading branch information
tcoratger authored Jan 5, 2024
1 parent fd67919 commit 103e054
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 71 deletions.
12 changes: 6 additions & 6 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});

// **************************************************************
Expand All @@ -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,
);
Expand All @@ -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,
);
Expand All @@ -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
Expand Down Expand Up @@ -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,
);
Expand Down
4 changes: 2 additions & 2 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
},
}
16 changes: 8 additions & 8 deletions build_helpers.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 (
Expand Down
157 changes: 104 additions & 53 deletions src/cmd/cmd.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
// ************************************************************
// * IMPORTS *
// ************************************************************

// Core imports.
const std = @import("std");
// Dependencies imports.
Expand All @@ -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.
Expand All @@ -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", .{});
Expand Down
7 changes: 5 additions & 2 deletions src/vm/builtins/builtin_runner/range_check.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down

0 comments on commit 103e054

Please sign in to comment.