Skip to content

Instantly share code, notes, and snippets.

@alexover1
Last active February 5, 2025 05:59
Show Gist options
  • Save alexover1/cbcb0fe1d735c96714c7c3426fdd02f6 to your computer and use it in GitHub Desktop.
Save alexover1/cbcb0fe1d735c96714c7c3426fdd02f6 to your computer and use it in GitHub Desktop.
/*
The purpose of this plugin is to disable certain procedures that are intended to be
developer-only secret code that you don't want to end up in the final release build
of your program.
To use this plugin, compile your program with `jai main.jai -plug Developer_Only`
and then mark certain procedures with the `@Developer` note. These procedures will
have their body deleted when in developer builds.
Note: The way you specify whether or not you are a developer build is currently
through the use of a separate command-line argument, "-no_developer",
but this is pretty easy to change if you want!
*/
#import "Basic";
#import "Compiler";
#import "Metaprogram_Plugins";
get_plugin :: () -> *Metaprogram_Plugin {
p := New(Metaprogram_Plugin);
p.message = message;
p.handle_one_option = handle_one_option;
return p;
}
#scope_file
message :: (p: *Metaprogram_Plugin, message: *Message) {
if developer return;
if message.kind != .TYPECHECKED return;
tc := cast(*Message_Typechecked) message;
for tc.procedure_bodies {
procedure := it.expression;
if procedure.body_flags & .ALREADY_MODIFIED continue;
for procedure.header.notes {
if it.text == "Developer" {
new_statements: [..] *Code_Node;
procedure.block.statements = new_statements;
compiler_modify_procedure(message.workspace, procedure);
}
}
}
}
// @Important: This language has no concept of "debug" or "release" builds built into the compiler itself.
// Instead, it is completely flexible with what kinds of optimizations and runtime checks that you want to
// have enabled, which means you can define different types of builds _very_ easily and have complete
// control over your program.
// Unfortunately, that means we don't have a convient way of determining if we are a developer build from
// looking at the build options. So, as an example, we just define an extra command-line option for that.
developer := true;
handle_one_option :: (p: *Metaprogram_Plugin, options: [] string, cursor: int) -> (new_cursor: int) {
if options[cursor] == {
case "-developer";
developer = true;
case "-no_developer";
developer = false;
case;
return cursor;
}
return cursor + 1;
}
@alexover1
Copy link
Author

I recommend actually copying the body of the message procedure, and instead putting that in your own message loop instead of using a metaprogram plugin, because then it's easier to customize how the build is determined as being a developer build, I.E. through your own command-line flags.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment