Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan-MV committed Jul 18, 2024
1 parent 6cfc0f1 commit 59e4c96
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
44 changes: 30 additions & 14 deletions ext/rubyraylib/rubyraylib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ static void initializeRubyInterpreter() {
}

static void initializeRubyArguments() {
std::vector<const char *> args{"RUBY", "-e ", "--yjit", "--parser=prism"};
std::vector<const char *> args{"ruby", "-e ", "--yjit", "--parser=prism"};
void *rubyNode = ruby_options(args.size(), const_cast<char **>(args.data()));
int executionState = 0;
bool success = ruby_executable_node(rubyNode, &executionState);
Expand All @@ -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) {
Expand All @@ -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] << " <directory_or_main_rb_path>"
<< 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<VALUE>(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;
}
1 change: 1 addition & 0 deletions ext/rubyraylib/rubyraylib.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef RUBYRAYLIB_H
#define RUBYRAYLIB_H 1

#include <filesystem>
#include <fstream>
#include <iostream>
#include <stdexcept>
Expand Down

0 comments on commit 59e4c96

Please sign in to comment.