1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* ext/tcltklib/tcltklib.c: fix SEGV

* ext/tk/lib/multi-tk.rb: improve safe-level handling of argument proc
* ext/tk/sample/multi-ip_sample.rb: rename of old 'safe-tk.rb'
* ext/tk/sample/safe-tk.rb: new sample script


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6905 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nagai 2004-09-14 08:01:55 +00:00
parent 07f245da37
commit 1130f2cbfa
6 changed files with 249 additions and 103 deletions

View file

@ -1,3 +1,13 @@
Tue Sep 14 16:59:37 2004 Hidetoshi NAGAI <nagai@ai.kyutech.ac.jp>
* ext/tcltklib/tcltklib.c: fix SEGV
* ext/tk/lib/multi-tk.rb: improve safe-level handling of argument proc
* ext/tk/sample/multi-ip_sample.rb: rename of old 'safe-tk.rb'
* ext/tk/sample/safe-tk.rb: new sample script
Mon Sep 13 21:33:40 2004 GOTOU Yuuzou <gotoyuzo@notwork.org> Mon Sep 13 21:33:40 2004 GOTOU Yuuzou <gotoyuzo@notwork.org>
* ext/openssl/ossl_x509name.c (ossl_x509name_to_s): add optional * ext/openssl/ossl_x509name.c (ossl_x509name_to_s): add optional

View file

@ -64,7 +64,7 @@ fprintf(stderr, ARG1, ARG2); fprintf(stderr, "\n"); fflush(stderr); }
#define DUMP2(ARG1, ARG2) #define DUMP2(ARG1, ARG2)
*/ */
/* finalize_proc_name */ /*finalize_proc_name */
static char *finalize_hook_name = "INTERP_FINALIZE_HOOK"; static char *finalize_hook_name = "INTERP_FINALIZE_HOOK";
/* for callback break & continue */ /* for callback break & continue */
@ -629,7 +629,7 @@ lib_eventloop_core(check_root, update_flag, check_var)
int update_flag; int update_flag;
int *check_var; int *check_var;
{ {
VALUE current = eventloop_thread; volatile VALUE current = eventloop_thread;
int found_event = 1; int found_event = 1;
int event_flag; int event_flag;
struct timeval t; struct timeval t;
@ -1321,6 +1321,7 @@ ip_ruby_eval(clientData, interp, argc, argv)
#endif #endif
/* arg.failed = 0; */ /* arg.failed = 0; */
RARRAY(exception)->ptr[0] = Qnil; RARRAY(exception)->ptr[0] = Qnil;
RARRAY(exception)->len = 1;
arg->failed = exception; arg->failed = exception;
/* evaluate the argument string by ruby */ /* evaluate the argument string by ruby */
@ -1717,6 +1718,7 @@ ip_ruby_cmd(clientData, interp, argc, argv)
rb_thread_critical = thr_crit_bup; rb_thread_critical = thr_crit_bup;
RARRAY(exception)->ptr[0] = Qnil; RARRAY(exception)->ptr[0] = Qnil;
RARRAY(exception)->len = 1;
arg->receiver = receiver; arg->receiver = receiver;
arg->method = method; arg->method = method;
@ -3878,9 +3880,11 @@ eval_queue_handler(evPtr, flags)
rb_bug("cross-thread violation on eval_queue_handler()"); rb_bug("cross-thread violation on eval_queue_handler()");
} }
#endif #endif
/* q_dat = Data_Wrap_Struct(rb_cData,0,0,q); */
q_dat = Data_Wrap_Struct(rb_cData,eval_queue_mark,0,q); q_dat = Data_Wrap_Struct(rb_cData,eval_queue_mark,0,q);
ret = rb_funcall(rb_proc_new(evq_safelevel_handler, q_dat), ret = rb_funcall(rb_proc_new(evq_safelevel_handler, q_dat),
ID_call, 0); ID_call, 0);
rb_gc_force_recycle(q_dat);
} else { } else {
DUMP2("call eval_real (for caller thread:%lx)", q->thread); DUMP2("call eval_real (for caller thread:%lx)", q->thread);
DUMP2("call eval_real (current thread:%lx)", rb_thread_current()); DUMP2("call eval_real (current thread:%lx)", rb_thread_current());
@ -3912,9 +3916,9 @@ ip_eval(self, str)
char *eval_str; char *eval_str;
int *alloc_done; int *alloc_done;
int thr_crit_bup; int thr_crit_bup;
VALUE current = rb_thread_current(); volatile VALUE current = rb_thread_current();
volatile VALUE ip_obj = self; volatile VALUE ip_obj = self;
volatile VALUE result = rb_ary_new2(1); volatile VALUE result;
volatile VALUE ret; volatile VALUE ret;
Tcl_QueuePosition position; Tcl_QueuePosition position;
@ -3952,6 +3956,11 @@ ip_eval(self, str)
/* allocate memory (freed by Tcl_ServiceEvent) */ /* allocate memory (freed by Tcl_ServiceEvent) */
evq = (struct eval_queue *)Tcl_Alloc(sizeof(struct eval_queue)); evq = (struct eval_queue *)Tcl_Alloc(sizeof(struct eval_queue));
/* allocate result obj */
result = rb_ary_new2(1);
RARRAY(result)->ptr[0] = Qnil;
RARRAY(result)->len = 1;
/* construct event data */ /* construct event data */
evq->done = alloc_done; evq->done = alloc_done;
evq->str = eval_str; evq->str = eval_str;
@ -4772,9 +4781,11 @@ invoke_queue_handler(evPtr, flags)
/* check safe-level */ /* check safe-level */
if (rb_safe_level() != q->safe_level) { if (rb_safe_level() != q->safe_level) {
/* q_dat = Data_Wrap_Struct(rb_cData,0,0,q); */
q_dat = Data_Wrap_Struct(rb_cData,invoke_queue_mark,0,q); q_dat = Data_Wrap_Struct(rb_cData,invoke_queue_mark,0,q);
ret = rb_funcall(rb_proc_new(ivq_safelevel_handler, q_dat), ret = rb_funcall(rb_proc_new(ivq_safelevel_handler, q_dat),
ID_call, 0); ID_call, 0);
rb_gc_force_recycle(q_dat);
} else { } else {
DUMP2("call invoke_real (for caller thread:%lx)", q->thread); DUMP2("call invoke_real (for caller thread:%lx)", q->thread);
DUMP2("call invoke_real (current thread:%lx)", rb_thread_current()); DUMP2("call invoke_real (current thread:%lx)", rb_thread_current());
@ -4810,10 +4821,9 @@ ip_invoke_with_position(argc, argv, obj, position)
int i; int i;
int *alloc_done; int *alloc_done;
int thr_crit_bup; int thr_crit_bup;
VALUE v; volatile VALUE current = rb_thread_current();
VALUE current = rb_thread_current();
volatile VALUE ip_obj = obj; volatile VALUE ip_obj = obj;
volatile VALUE result = rb_ary_new2(1); volatile VALUE result;
volatile VALUE ret; volatile VALUE ret;
#if TCL_MAJOR_VERSION >= 8 #if TCL_MAJOR_VERSION >= 8
@ -4853,6 +4863,11 @@ ip_invoke_with_position(argc, argv, obj, position)
/* allocate memory (freed by Tcl_ServiceEvent) */ /* allocate memory (freed by Tcl_ServiceEvent) */
ivq = (struct invoke_queue *)Tcl_Alloc(sizeof(struct invoke_queue)); ivq = (struct invoke_queue *)Tcl_Alloc(sizeof(struct invoke_queue));
/* allocate result obj */
result = rb_ary_new2(1);
RARRAY(result)->ptr[0] = Qnil;
RARRAY(result)->len = 1;
/* construct event data */ /* construct event data */
ivq->done = alloc_done; ivq->done = alloc_done;
ivq->argc = argc; ivq->argc = argc;

View file

@ -267,6 +267,7 @@ sample/encstr_usage.rb
sample/iso2022-kr.txt sample/iso2022-kr.txt
sample/menubar1.rb sample/menubar1.rb
sample/menubar2.rb sample/menubar2.rb
sample/multi-ip_sample.rb
sample/optobj_sample.rb sample/optobj_sample.rb
sample/propagate.rb sample/propagate.rb
sample/remote-ip_sample.rb sample/remote-ip_sample.rb

View file

@ -140,16 +140,17 @@ class MultiTkIp
###################################### ######################################
def set_safe_level(safe) def set_safe_level(safe)
@safe_level[0] = safe if safe > @safe_level[0]
@cmd_queue.enq([@system, 'set_safe_level', safe]) @safe_level[0] = safe
self @cmd_queue.enq([@system, 'set_safe_level', safe])
end
@safe_level[0]
end end
def safe_level=(safe) def safe_level=(safe)
set_safe_level(safe) set_safe_level(safe)
end end
def self.set_safe_level(safe) def self.set_safe_level(safe)
__getip.set_safe_level(safe) __getip.set_safe_level(safe)
self
end end
def self.safe_level=(safe) def self.safe_level=(safe)
self.set_safe_level(safe) self.set_safe_level(safe)
@ -222,7 +223,8 @@ class MultiTkIp
else else
# procedure # procedure
begin begin
ret = proc{$SAFE = safe_level; cmd.call(*args)}.call #ret = proc{$SAFE = safe_level; cmd.call(*args)}.call
ret = cmd.call(safe_level, *args)
rescue SystemExit => e rescue SystemExit => e
# delete IP # delete IP
unless @interp.deleted? unless @interp.deleted?
@ -1183,7 +1185,10 @@ class MultiTkIp
def cb_eval(cmd, *args) def cb_eval(cmd, *args)
#self.eval_callback{ TkComm._get_eval_string(TkUtil.eval_cmd(cmd, *args)) } #self.eval_callback{ TkComm._get_eval_string(TkUtil.eval_cmd(cmd, *args)) }
ret = self.eval_callback{ TkComm._get_eval_string(TkUtil.eval_cmd(cmd, *args)) } #ret = self.eval_callback{ TkComm._get_eval_string(TkUtil.eval_cmd(cmd, *args)) }
ret = self.eval_callback(*args){|safe, *params|
$SAFE=safe; TkComm._get_eval_string(TkUtil.eval_cmd(cmd, *params))
}
if ret.kind_of?(Exception) if ret.kind_of?(Exception)
raise ret raise ret
end end
@ -1268,9 +1273,7 @@ class MultiTkIp
end end
private :eval_proc_core private :eval_proc_core
#def eval_callback(cmd = proc{$SAFE=@safe_level[0]; Proc.new}.call, *args) =begin
# eval_proc_core(false, cmd, *args)
#end
def eval_callback(*args) def eval_callback(*args)
if block_given? if block_given?
eval_proc_core(false, proc{$SAFE=@safe_level[0]; Proc.new}.call, *args) eval_proc_core(false, proc{$SAFE=@safe_level[0]; Proc.new}.call, *args)
@ -1278,10 +1281,16 @@ class MultiTkIp
eval_proc_core(false, *args) eval_proc_core(false, *args)
end end
end end
=end
def eval_callback(*args)
if block_given?
eval_proc_core(false, Proc.new, *args)
else
eval_proc_core(false, *args)
end
end
#def eval_proc(cmd = proc{$SAFE=@safe_level[0]; Proc.new}.call, *args) =begin
# eval_proc_core(true, cmd, *args)
#end
def eval_proc(*args) def eval_proc(*args)
if block_given? if block_given?
eval_proc_core(true, proc{$SAFE=@safe_level[0]; Proc.new}.call, *args) eval_proc_core(true, proc{$SAFE=@safe_level[0]; Proc.new}.call, *args)
@ -1289,15 +1298,34 @@ class MultiTkIp
eval_proc_core(true, *args) eval_proc_core(true, *args)
end end
end end
=end
def eval_proc(*args)
if block_given?
cmd = Proc.new
else
unless (cmd = args.shift)
fail ArgumentError, "A Proc or Method object is expected for 1st argument"
end
end
eval_proc_core(true,
proc{|safe, *params|
$SAFE=safe; Thread.new(*params, &cmd).value
},
*args)
end
alias call eval_proc alias call eval_proc
alias eval_string eval_proc alias eval_string eval_proc
end end
class << MultiTkIp class << MultiTkIp
# class method # class method
# def eval_proc(cmd = proc{$SAFE=__getip.safe_level; Proc.new}.call, *args) =begin
# # class ==> interp object def eval_proc(cmd = proc{$SAFE=__getip.safe_level; Proc.new}.call, *args)
# __getip.eval_proc(cmd, *args) # class ==> interp object
# end __getip.eval_proc(cmd, *args)
end
=end
=begin
def eval_proc(*args) def eval_proc(*args)
# class ==> interp object # class ==> interp object
if block_given? if block_given?
@ -1306,6 +1334,13 @@ class << MultiTkIp
__getip.eval_proc(*args) __getip.eval_proc(*args)
end end
end end
=end
#=begin
def eval_proc(*args, &blk)
# class ==> interp object
__getip.eval_proc(*args, &blk)
end
#=end
alias call eval_proc alias call eval_proc
alias eval_string eval_proc alias eval_string eval_proc
end end

View file

@ -0,0 +1,102 @@
#!/usr/bin/env ruby
# This script is a sample of MultiTkIp class
require "multi-tk"
# create slave interpreters
trusted_slave = MultiTkIp.new_slave
safe_slave1 = MultiTkIp.new_safeTk
safe_slave2 = MultiTkIp.new_safeTk('fill'=>:none, 'expand'=>false)
#safe_slave2 = MultiTkIp.new_safeTk('fill'=>:none)
#safe_slave2 = MultiTkIp.new_safeTk('expand'=>false)
cmd = Proc.new{|txt|
#####################
## from TkTimer2.rb
if TkCore::INTERP.safe?
# safeTk doesn't have permission to call 'wm' command
else
root = TkRoot.new(:title=>'timer sample')
end
label = TkLabel.new(:parent=>root, :relief=>:raised, :width=>10) \
.pack(:side=>:bottom, :fill=>:both)
tick = proc{|aobj|
cnt = aobj.return_value + 5
label.text format("%d.%02d", *(cnt.divmod(100)))
cnt
}
timer = TkTimer.new(50, -1, tick).start(0, proc{ label.text('0.00'); 0 })
=begin
TkButton.new(:text=>'Start') {
command proc{ timer.continue unless timer.running? }
pack(:side=>:left, :fill=>:both, :expand=>true)
}
TkButton.new(:text=>'Restart') {
command proc{ timer.restart(0, proc{ label.text('0.00'); 0 }) }
pack('side'=>'right','fill'=>'both','expand'=>'yes')
}
TkButton.new(:text=>'Stop') {
command proc{ timer.stop if timer.running? }
pack('side'=>'right','fill'=>'both','expand'=>'yes')
}
=end
b_start = TkButton.new(:text=>'Start', :state=>:disabled) {
pack(:side=>:left, :fill=>:both, :expand=>true)
}
b_stop = TkButton.new(:text=>'Stop', :state=>:normal) {
pack('side'=>'left', 'fill'=>'both', 'expand'=>'yes')
}
b_start.command {
timer.continue
b_stop.state(:normal)
b_start.state(:disabled)
}
b_stop.command {
timer.stop
b_start.state(:normal)
b_stop.state(:disabled)
}
TkButton.new(:text=>'Reset', :state=>:normal) {
command { timer.reset }
pack('side'=>'right', 'fill'=>'both', 'expand'=>'yes')
}
ev_quit = TkVirtualEvent.new('Control-c', 'Control-q')
Tk.root.bind(ev_quit, proc{Tk.exit}).focus
}
# call on the default master interpreter
trusted_slave.eval_proc(cmd, 'trusted') # label -> .w00012
safe_slave1.eval_proc(cmd, 'safe1') # label -> .w00016
safe_slave2.eval_proc(cmd, 'safe2') # label -> .w00020
cmd.call('master') # label -> .w00024
#second_master = MultiTkIp.new(&cmd)
TkTimer.new(2000, -1, proc{p ['safe1', safe_slave1.deleted?]}).start
TkTimer.new(2000, -1, proc{p ['safe2', safe_slave2.deleted?]}).start
TkTimer.new(2000, -1, proc{p ['trusted', trusted_slave.deleted?]}).start
TkTimer.new(5000, 1,
proc{
safe_slave1.eval_proc{Tk.root.destroy}
safe_slave1.delete
print "*** The safe_slave1 is deleted by the timer.\n"
}).start
TkTimer.new(10000, 1,
proc{
trusted_slave.eval_proc{Tk.root.destroy}
trusted_slave.delete
print "*** The trusted_slave is deleted by the timer.\n"
}).start
Tk.mainloop

View file

@ -3,100 +3,83 @@
require "multi-tk" require "multi-tk"
# create slave interpreters ###############################
trusted_slave = MultiTkIp.new_slave
safe_slave1 = MultiTkIp.new_safeTk
safe_slave2 = MultiTkIp.new_safeTk('fill'=>:none, 'expand'=>false)
#safe_slave2 = MultiTkIp.new_safeTk('fill'=>:none)
#safe_slave2 = MultiTkIp.new_safeTk('expand'=>false)
cmd = Proc.new{|txt| TkLabel.new(:text=>'Default Master Ipnterpreter').pack(:padx=>5, :pady=>7)
##################### TkButton.new(:text=>'QUIT', :command=>proc{exit}).pack(:pady=>3)
## from TkTimer2.rb
if TkCore::INTERP.safe? ###############################
# safeTk doesn't have permission to call 'wm' command
else
root = TkRoot.new(:title=>'timer sample')
end
label = TkLabel.new(:parent=>root, :relief=>:raised, :width=>10) \ puts "---- create a safe slave IP with Ruby's safe-level == 1 ----------"
.pack(:side=>:bottom, :fill=>:both) ip = MultiTkIp.new_safe_slave(1)
tick = proc{|aobj| puts "\n---- create procs ----------"
cnt = aobj.return_value + 5 puts 'x = proc{p [\'proc x\', "$SAFE==#{$SAFE}"]; exit}'
label.text format("%d.%02d", *(cnt.divmod(100))) x = proc{p ['proc x', "$SAFE==#{$SAFE}"]; exit}
cnt
}
timer = TkTimer.new(50, -1, tick).start(0, proc{ label.text('0.00'); 0 }) puts 'y = proc{|label| p [\'proc y\', "$SAFE==#{$SAFE}", label]; label.text($SAFE)}'
y = proc{|label| p ['proc y', "$SAFE==#{$SAFE}", label]; label.text($SAFE)}
=begin puts 'z = proc{p [\'proc z\', "$SAFE==#{$SAFE}"]; exit}'
TkButton.new(:text=>'Start') { z = proc{p ['proc z', "$SAFE==#{$SAFE}"]; exit}
command proc{ timer.continue unless timer.running? }
pack(:side=>:left, :fill=>:both, :expand=>true)
}
TkButton.new(:text=>'Restart') {
command proc{ timer.restart(0, proc{ label.text('0.00'); 0 }) }
pack('side'=>'right','fill'=>'both','expand'=>'yes')
}
TkButton.new(:text=>'Stop') {
command proc{ timer.stop if timer.running? }
pack('side'=>'right','fill'=>'both','expand'=>'yes')
}
=end
b_start = TkButton.new(:text=>'Start', :state=>:disabled) {
pack(:side=>:left, :fill=>:both, :expand=>true)
}
b_stop = TkButton.new(:text=>'Stop', :state=>:normal) {
pack('side'=>'left', 'fill'=>'both', 'expand'=>'yes')
}
b_start.command { puts "\n---- call 1st eval_proc ----------"
timer.continue print 'lbl = '
b_stop.state(:normal) p lbl = ip.eval_proc{
b_start.state(:disabled) TkLabel.new(:text=>"1st eval_prpc : $SAFE == #{$SAFE}").pack
}
b_stop.command { f = TkFrame.new.pack
timer.stop TkLabel.new(f, :text=>"$SAFE == ").pack(:side=>:left)
b_start.state(:normal) l = TkLabel.new(f).pack(:side=>:right)
b_stop.state(:disabled)
}
TkButton.new(:text=>'Reset', :state=>:normal) { TkButton.new(:text=>':command=>proc{l.text($SAFE)}',
command { timer.reset } :command=>proc{l.text($SAFE)}).pack(:fill=>:x, :padx=>5)
pack('side'=>'right', 'fill'=>'both', 'expand'=>'yes') TkButton.new(:text=>':command=>x', :command=>x).pack(:fill=>:x, :padx=>5)
} TkButton.new(:text=>':command=>proc{exit}',
:command=>proc{exit}).pack(:fill=>:x, :padx=>5)
ev_quit = TkVirtualEvent.new('Control-c', 'Control-q') TkFrame.new(:borderwidth=>2, :height=>3,
Tk.root.bind(ev_quit, proc{Tk.exit}).focus :relief=>:sunken).pack(:fill=>:x, :expand=>true,
:padx=>10, :pady=>7)
l # return the label widget
} }
# call on the default master interpreter puts "\n---- change the safe slave IP's safe-level ==> 3 ----------"
trusted_slave.eval_proc(cmd, 'trusted') # label -> .w00012 ip.safe_level = 3
safe_slave1.eval_proc(cmd, 'safe1') # label -> .w00016
safe_slave2.eval_proc(cmd, 'safe2') # label -> .w00020
cmd.call('master') # label -> .w00024
#second_master = MultiTkIp.new(&cmd) puts "\n---- call 2nd eval_proc ----------"
p ip.eval_proc(proc{
TkLabel.new(:text=>"2nd eval_prpc : $SAFE == #{$SAFE}").pack
f = TkFrame.new.pack
TkLabel.new(f, :text=>"$SAFE == ").pack(:side=>:left)
l = TkLabel.new(f, :text=>$SAFE).pack(:side=>:right)
TkButton.new(:text=>':command=>proc{l.text($SAFE)}',
:command=>proc{l.text($SAFE)}).pack(:fill=>:x,
:padx=>5)
TkButton.new(:text=>':command=>proc{y.call(l)}',
:command=>proc{y.call(l)}).pack(:fill=>:x,
:padx=>5)
TkButton.new(:text=>':command=>proc{Thread.new(l, &y).value}',
:command=>proc{
Thread.new(l, &y).value
}).pack(:fill=>:x, :padx=>5)
TkButton.new(:text=>':command=>proc{z.call}',
:command=>proc{z.call}).pack(:fill=>:x, :padx=>5)
TkFrame.new(:borderwidth=>2, :height=>3,
:relief=>:sunken).pack(:fill=>:x, :expand=>true,
:padx=>10, :pady=>7)
})
TkTimer.new(2000, -1, proc{p ['safe1', safe_slave1.deleted?]}).start
TkTimer.new(2000, -1, proc{p ['safe2', safe_slave2.deleted?]}).start
TkTimer.new(2000, -1, proc{p ['trusted', trusted_slave.deleted?]}).start
TkTimer.new(7000, 1, puts "\n---- change the safe slave IP's safe-level ==> 4 ----------"
proc{ ip.safe_level = 4
safe_slave1.eval_proc{Tk.root.destroy}
safe_slave1.delete
print "*** The safe_slave1 is deleted by the timer.\n"
}).start
TkTimer.new(10000, 1, puts "\n---- call 3rd and 4th eval_proc ----------"
proc{ p ip.eval_proc{ TkLabel.new(:text=>"3rd+ eval_prpc : $SAFE == #{$SAFE}").pack }
trusted_slave.eval_proc{Tk.root.destroy} p ip.eval_proc{
trusted_slave.delete TkButton.new(:text=>':command=>proc{ lbl.text = $SAFE }',
print "*** The trusted_slave is deleted by the timer.\n" :command=>proc{ lbl.text = $SAFE }).pack(:fill=>:x, :padx=>5)
}).start }
puts "\n---- start event-loop ( current $SAFE == #{$SAFE} ) ----------"
Tk.mainloop Tk.mainloop