Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #309

Merged
merged 6 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 6 additions & 20 deletions ext/wxruby3/swig/custom/director.swg
Original file line number Diff line number Diff line change
Expand Up @@ -147,20 +147,14 @@ namespace Swig {

DirectorTypeMismatchException(VALUE self, const char *method, VALUE error, const char *msg="");

static inline void raise(VALUE error, const char *msg)
{
throw DirectorTypeMismatchException(error, msg);
}
static void raise(VALUE error, const char *msg);

static inline void raise(const char *msg)
{
throw DirectorTypeMismatchException(msg);
}
static void raise(const char *msg);

static inline void raise(VALUE self, const char* method, VALUE error, const char *msg)
{
throw DirectorTypeMismatchException(self, method, error, msg);
}
static void raise(VALUE self, const char* method, VALUE error, const char *msg);

private:
static void print(const DirectorTypeMismatchException& ex);
};

/* Any Ruby exception that occurs during a director method call */
Expand Down Expand Up @@ -195,14 +189,6 @@ namespace Swig {
}
};

/* wxRuby customization */
class WXRUBY_EXPORT DirectorRubyException : public DirectorException
{
public:
DirectorRubyException(VALUE error, VALUE rcvr, ID fn_id);

};

/* Simple thread abstraction for pthreads on win32 */
#ifdef __THREAD__
# define __PTHREAD__
Expand Down
2 changes: 1 addition & 1 deletion lib/wx/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
# This software is released under the MIT license.

module Wx
WXRUBY_VERSION = '1.2.1'
WXRUBY_VERSION = '1.3.0'
end
2 changes: 1 addition & 1 deletion rakelib/lib/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ def test(*tests, **options)
test = File.join(Config.instance.test_dir, test)
test = Dir.glob(test+'.rb').shift || test unless File.exist?(test)
end
Rake.sh(Config.instance.exec_env, *make_ruby_cmd(test)) { |ok,status| errors += 1 unless ok }
Rake.sh(Config.instance.exec_env.merge({'RUBYLIB'=>rb_lib_path}), FileUtils::RUBY, test) { |ok,status| errors += 1 unless ok }
end
end
fail "ERRORS: ##{errors} test scripts failed." if errors>0
Expand Down
33 changes: 9 additions & 24 deletions rakelib/lib/core/include/funcall.inc
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,20 @@ typedef VALUE (*RUBY_INVOKE_FUNC) (VALUE);
VALUE rb_exc_set_backtrace(VALUE, VALUE);
VALUE rb_get_backtrace(VALUE);

namespace Swig
{
class WXRB_EXPORT_FLAG DirectorRubyException : public DirectorException
{
public:
DirectorRubyException(VALUE error, VALUE rcvr, ID fn_id)
: DirectorException(Qnil)
{
VALUE msg = rb_sprintf("Caught exception in SWIG director method for %s#%s : ", rb_class2name(CLASS_OF(rcvr)), rb_id2name(fn_id));
rb_str_append(msg, rb_funcall(error, rb_intern("message"), 0));
this->swig_msg = StringValuePtr(msg);
swig_error = rb_exc_new_str(rb_eRuntimeError, msg);
VALUE bt = rb_funcall(error, rb_intern("backtrace"), 0);
rb_funcall(swig_error, rb_intern("set_backtrace"), 1, bt);
}
};
}
WXRB_EXPORT_FLAG void wxRuby_PrintException(VALUE err);

class WXRuby_RBFuncall
{
public:
WXRuby_RBFuncall (ID fnid, bool throw_on_ex=true)
WXRuby_RBFuncall (ID fnid, bool exit_on_ex=true)
: fn_id_ (fnid),
throw_on_ex_ (throw_on_ex),
exit_on_ex_ (exit_on_ex),
ex_caught_ (false)
{
}
WXRuby_RBFuncall (const char* fn, bool throw_on_ex=true)
WXRuby_RBFuncall (const char* fn, bool exit_on_ex=true)
: fn_id_ (rb_intern (fn)),
throw_on_ex_ (throw_on_ex),
exit_on_ex_ (exit_on_ex),
ex_caught_ (false)
{
}
Expand Down Expand Up @@ -89,11 +73,12 @@ protected:
&invoke_state);
if (invoke_state)
{
if (this->throw_on_ex_)
if (this->exit_on_ex_)
{
// handle exception
VALUE rexc = this->get_exception ();
throw Swig::DirectorRubyException(rexc, fa.receiver_, this->fn_id_);
wxRuby_PrintException(rexc);
::exit(255);
}
else
{
Expand Down Expand Up @@ -158,7 +143,7 @@ protected:

private:
ID fn_id_;
bool throw_on_ex_;
bool exit_on_ex_;
bool ex_caught_;
};

Expand Down
40 changes: 29 additions & 11 deletions rakelib/lib/core/include/swigdirector.inc
Original file line number Diff line number Diff line change
Expand Up @@ -188,23 +188,23 @@ namespace Swig

DirectorTypeMismatchException(VALUE self, const char *method, VALUE error, const char *msg="");

static inline void raise(VALUE error, const char *msg)
{
throw DirectorTypeMismatchException(error, msg);
}
static void raise(VALUE error, const char *msg);

static inline void raise(const char *msg)
{
throw DirectorTypeMismatchException(msg);
}
static void raise(const char *msg);

static inline void raise(VALUE self, const char* method, VALUE error, const char *msg)
static void raise(VALUE self, const char* method, VALUE error, const char *msg);

private:
static void print(const DirectorTypeMismatchException& ex)
{
throw DirectorTypeMismatchException(self, method, error, msg);
VALUE bt = rb_eval_string("caller");
bt = rb_funcall(bt, rb_intern("join"), 1, rb_str_new2("\n\tfrom "));
std::cerr << std::endl
<< ' ' << ex.getMessage() << '(' << rb_class2name(ex.getType()) << ')' << std::endl
<< "\tfrom " << StringValuePtr(bt) << std::endl << std::endl;
}
};


DirectorTypeMismatchException::DirectorTypeMismatchException(VALUE self, const char *method, VALUE error, const char *msg)
: DirectorException(Qnil)
{
Expand All @@ -217,6 +217,24 @@ namespace Swig
this->setup_error(rb_eTypeError);
}

void DirectorTypeMismatchException::raise(VALUE error, const char *msg)
{
print(DirectorTypeMismatchException(error, msg));
::exit(254);
}

void DirectorTypeMismatchException::raise(const char *msg)
{
print(DirectorTypeMismatchException(msg));
::exit(254);
}

void DirectorTypeMismatchException::raise(VALUE self, const char* method, VALUE error, const char *msg)
{
print(DirectorTypeMismatchException(self, method, error, msg));
::exit(254);
}

/* Any Ruby exception that occurs during a director method call */
class WXRB_EXPORT_FLAG DirectorMethodException : public DirectorException
{
Expand Down
49 changes: 7 additions & 42 deletions rakelib/lib/director/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,8 @@ def setup

wxObject* wxRubyValidator::Clone() const
{
bool ex_caught = false;
VALUE self = const_cast<wxRubyValidator*> (this)->get_self();
VALUE rc = wxRuby_Funcall(ex_caught, self, clone_id(), 0);
if (ex_caught)
{
throw Swig::DirectorRubyException(rc, self, clone_id());
}
VALUE rc = wxRuby_Funcall(self, clone_id(), 0);
void *ptr;
int res = SWIG_ConvertPtr(rc, &ptr, SWIGTYPE_p_wxValidator, 0);
if (!SWIG_IsOK(res))
Expand Down Expand Up @@ -77,22 +72,12 @@ def setup

VALUE wxRubyValidator::DoTransferFromWindow()
{
bool ex_caught = false;
VALUE rc = wxRuby_Funcall(ex_caught, this->get_self(), do_transfer_from_window_id(), 0);
if (ex_caught)
{
throw Swig::DirectorRubyException(rc, this->get_self(), do_transfer_from_window_id());
}
VALUE rc = wxRuby_Funcall(this->get_self(), do_transfer_from_window_id(), 0);
return rc;
}
bool wxRubyValidator::DoTransferToWindow(VALUE data)
{
bool ex_caught = false;
VALUE rc = wxRuby_Funcall(ex_caught, this->get_self(), do_transfer_to_window_id(), 1, data);
if (ex_caught)
{
throw Swig::DirectorRubyException(rc, this->get_self(), do_transfer_to_window_id());
}
VALUE rc = wxRuby_Funcall(this->get_self(), do_transfer_to_window_id(), 1, data);
return (rc == Qtrue);
}

Expand All @@ -111,48 +96,28 @@ def setup

bool wxRubyValidatorBinding::DoOnTransferFromWindow(VALUE data)
{
bool ex_caught = false;
VALUE rc = wxRuby_Funcall(ex_caught, this->get_self(), do_on_transfer_from_window_id(), 1, data);
if (ex_caught)
{
throw Swig::DirectorRubyException(rc, this->get_self(), do_on_transfer_from_window_id());
}
VALUE rc = wxRuby_Funcall(this->get_self(), do_on_transfer_from_window_id(), 1, data);
return (rc == Qtrue);
}
VALUE wxRubyValidatorBinding::DoOnTransferToWindow()
{
bool ex_caught = false;
VALUE rc = wxRuby_Funcall(ex_caught, this->get_self(), do_on_transfer_to_window_id(), 0);
if (ex_caught)
{
throw Swig::DirectorRubyException(rc, this->get_self(), do_on_transfer_to_window_id());
}
VALUE rc = wxRuby_Funcall(this->get_self(), do_on_transfer_to_window_id(), 0);
return rc;
}

bool wxRubyValidatorBinding::OnTransferFromWindow(VALUE data)
{
if (!NIL_P(this->on_transfer_from_win_proc_))
{
bool ex_caught = false;
VALUE rc = wxRuby_Funcall(ex_caught, this->on_transfer_from_win_proc_, call_id(), 1, data);
if (ex_caught)
{
throw Swig::DirectorRubyException(rc, this->on_transfer_from_win_proc_, call_id());
}
wxRuby_Funcall(this->on_transfer_from_win_proc_, call_id(), 1, data);
}
return true;
}
VALUE wxRubyValidatorBinding::OnTransferToWindow()
{
if (!NIL_P(this->on_transfer_to_win_proc_))
{
bool ex_caught = false;
VALUE rc = wxRuby_Funcall(ex_caught, this->on_transfer_to_win_proc_, call_id(), 0);
if (ex_caught)
{
throw Swig::DirectorRubyException(rc, this->on_transfer_to_win_proc_, call_id());
}
VALUE rc = wxRuby_Funcall(this->on_transfer_to_win_proc_, call_id(), 0);
return rc;
}
return Qnil;
Expand Down
25 changes: 25 additions & 0 deletions tests/lib/leaked_overload_exception_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright (c) 2023 M.J.N. Corino, The Netherlands
#
# This software is released under the MIT license.

require_relative './wxframe_runner'

class DirectorOverloadExceptionTests < WxRuby::Test::GUITests

class ExceptionSizer < Wx::BoxSizer

def calc_min
raise RuntimeError, 'AnyThing'
end

end

def test_exception_in_overload
szr = ExceptionSizer.new(Wx::Orientation::VERTICAL)
szr.add(Wx::Button.new(frame_win, name: 'button'), Wx::Direction::TOP)
frame_win.sizer = szr
assert_raise_kind_of(RuntimeError) { frame_win.layout }
frame_win.sizer = nil
end

end
33 changes: 33 additions & 0 deletions tests/lib/leaked_process_event_exception_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright (c) 2023 M.J.N. Corino, The Netherlands
#
# This software is released under the MIT license.

require_relative './wxapp_runner'

class ProcessEventHandlingExceptionTests < Test::Unit::TestCase

class TestEvent < Wx::Event
EVT_TEST_EVENT = Wx::EvtHandler.register_class(self, nil, 'evt_test_event', 0)
def initialize(id=0)
super(EVT_TEST_EVENT, id)
end
end

class TestFrame < Wx::Frame

def initialize
super(nil, size: [300,300])

evt_test_event { |_evt| raise RuntimeError, 'Whatever' }
end

end

def test_process_event
win = TestFrame.new
win.process_event(TestEvent.new)
win.destroy
10.times { Wx.get_app.yield }
end

end
34 changes: 34 additions & 0 deletions tests/lib/leaked_queued_event_exception_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright (c) 2023 M.J.N. Corino, The Netherlands
#
# This software is released under the MIT license.

require_relative './wxapp_runner'

class QueuedEventHandlingExceptionTests < Test::Unit::TestCase

class TestEvent < Wx::Event
EVT_TEST_EVENT = Wx::EvtHandler.register_class(self, nil, 'evt_test_event', 0)
def initialize(id=0)
super(EVT_TEST_EVENT, id)
end
end

class TestFrame < Wx::Frame

def initialize
super(nil, size: [300,300])

evt_test_event { |_evt| raise RuntimeError, 'Whatever' }
end

end

def test_queue_event
win = TestFrame.new
win.queue_event(TestEvent.new)
Wx.get_app.yield
win.destroy
10.times { Wx.get_app.yield }
end

end
25 changes: 25 additions & 0 deletions tests/lib/overload_type_exception_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright (c) 2023 M.J.N. Corino, The Netherlands
#
# This software is released under the MIT license.

require_relative './wxframe_runner'

class DirectorTypeExceptionTests < WxRuby::Test::GUITests

class InvalidOutputSizer < Wx::BoxSizer

def calc_min
nil #Wx::Point.new(1,1) # expects Wx::Size
end

end

def test_invalid_output
szr = InvalidOutputSizer.new(Wx::Orientation::VERTICAL)
szr.add(Wx::Button.new(frame_win, name: 'button'), Wx::Direction::TOP)
frame_win.sizer = szr
assert_raise_kind_of(TypeError) { frame_win.layout }
frame_win.sizer = nil
end

end
Loading
Loading