diff --git a/ext/rubyraylib/rubyraylib.cpp b/ext/rubyraylib/rubyraylib.cpp index 7a58bdf..f0a439e 100644 --- a/ext/rubyraylib/rubyraylib.cpp +++ b/ext/rubyraylib/rubyraylib.cpp @@ -43,7 +43,7 @@ static void initializeRubyInterpreter() { } static void initializeRubyArguments() { - std::vector args{"RUBY", "-e ", "--yjit", "--parser=prism"}; + std::vector args{"ruby", "-e ", "--yjit", "--parser=prism"}; void *rubyNode = ruby_options(args.size(), const_cast(args.data())); int executionState = 0; bool success = ruby_executable_node(rubyNode, &executionState); @@ -55,10 +55,9 @@ static void initializeRubyArguments() { } } -static VALUE script_run(VALUE) { - rb_ary_push(rb_gv_get("$LOAD_PATH"), rb_str_new_literal("./script")); - rb_load(rb_str_new_literal("./main.rb"), 0); - return Qnil; +void load_script(const std::string &script_path) { + VALUE script = rb_str_new_cstr(script_path.c_str()); + rb_load(script, 0); } static VALUE script_rescue(VALUE, VALUE exc) { @@ -72,21 +71,38 @@ static VALUE script_rescue(VALUE, VALUE exc) { return Qnil; } -int main() { +int main(int argc, char *argv[]) { + if (argc < 2) { + std::cerr << "Usage: " << argv[0] << " " + << std::endl; + return 1; + } + + std::string path = argv[1]; + std::filesystem::path script_path = std::filesystem::path(path); + + if (std::filesystem::is_directory(script_path)) { + script_path /= "main.rb"; + } + + if (!std::filesystem::exists(script_path)) { + std::cerr << "Error: " << script_path << " does not exist." << std::endl; + return 1; + } + // Initialize components initializeRubyInterpreter(); initializeRubyArguments(); Init_rubyraylib(); try { - rb_rescue2(script_run, Qnil, script_rescue, Qnil, rb_eException, - static_cast(0)); - } catch (std::exception &e) { - std::string msg = "Error:\n"; - std::cerr << msg + e.what() << std::endl; + rb_rescue2(RUBY_METHOD_FUNC(load_script), + rb_str_new_cstr(script_path.string().c_str()), + RUBY_METHOD_FUNC(script_rescue), Qnil, rb_eException, (VALUE)0); + } catch (const std::exception &e) { + std::cerr << "Error:\n" << e.what() << std::endl; } ruby_finalize(); - // Return success or failure based on script evaluation - return Qnil; -} + return 0; +} \ No newline at end of file diff --git a/ext/rubyraylib/rubyraylib.hpp b/ext/rubyraylib/rubyraylib.hpp index 8b3c213..8297c18 100644 --- a/ext/rubyraylib/rubyraylib.hpp +++ b/ext/rubyraylib/rubyraylib.hpp @@ -1,6 +1,7 @@ #ifndef RUBYRAYLIB_H #define RUBYRAYLIB_H 1 +#include #include #include #include