2003-08-01 04:58:55 +00:00
#!/usr/bin/env ruby
2015-12-16 05:31:54 +00:00
# frozen_string_literal: false
2003-08-01 04:58:55 +00:00
require 'multi-tk'
TkMessage . new ( :text = > <<EOM).pack
This is a sample of the safe - Tk slave interpreter . \
2004-09-13 07:25:02 +00:00
On the slave interpreter , 'tkoptdb.rb' demo is running .
( NOTE :: a safe - Tk interpreter can ' t read options \
2003-09-07 07:10:44 +00:00
from a file . Options are given by the master interpreter \
in this script . )
2003-08-01 04:58:55 +00:00
The window shown this message is a root widget of \
the default master interpreter . The other window \
is a toplevel widget of the master interpreter , and it \
2004-09-13 07:25:02 +00:00
has a container frame of the safe - Tk slave interpreter .
'exit' on the slave interpreter exits the slave only . \
You can also delete the slave by the button on the toplevel widget .
2003-08-01 04:58:55 +00:00
EOM
2003-09-07 07:10:44 +00:00
if ENV [ 'LANG' ] =~ / ^ja /
# read Japanese resource
2009-03-06 03:56:38 +00:00
ent = TkOptionDB . read_entries ( File . expand_path ( 'resource.ja' ,
2004-10-11 04:51:21 +00:00
File . dirname ( __FILE__ ) ) ,
2011-09-14 17:25:37 +00:00
'utf-8' )
2003-09-07 07:10:44 +00:00
else
# read English resource
2009-03-06 03:56:38 +00:00
ent = TkOptionDB . read_entries ( File . expand_path ( 'resource.en' ,
2004-10-11 04:51:21 +00:00
File . dirname ( __FILE__ ) ) )
2003-09-07 07:10:44 +00:00
end
2004-09-15 11:24:22 +00:00
2003-08-01 04:58:55 +00:00
file = File . expand_path ( 'tkoptdb.rb' , File . dirname ( __FILE__ ) )
2004-09-02 17:17:20 +00:00
ip = MultiTkIp . new_safeTk {
2009-03-06 03:56:38 +00:00
# When a block is given to 'new_safeTk' method,
2015-09-24 01:37:14 +00:00
# the block is evaluated on $SAFE==1.
2004-09-02 17:17:20 +00:00
ent . each { | pat , val | Tk . tk_call ( 'option' , 'add' , pat , val ) }
}
2004-09-13 07:25:02 +00:00
print " ip.eval_proc{$SAFE} ==> " , ip . eval_proc { $SAFE } , " \n "
2004-09-17 06:05:33 +00:00
print " \n call 'ip.wait_on_mainloop = false' \n "
2009-03-06 03:56:38 +00:00
print " If 'ip.wait_on_mainloop? == true', " ,
" when 'mainloop' is called on 'ip.eval_proc', " ,
2015-12-27 09:57:30 +00:00
" 'ip.eval_proc' doesn't return while the root window exists. \n " ,
2009-03-06 03:56:38 +00:00
" If you want to avoid that, set wait_on_mainloop to false. " ,
" Then the mainloop in the eval_proc returns soon " ,
" and the following steps are evaluated. \n " ,
" If you hate the both of them, use 'ip.bg_eval_proc' or " ,
2004-09-17 06:05:33 +00:00
" wrap 'ip.eval_proc' by a thread. \n "
ip . wait_on_mainloop = false
2004-09-13 07:25:02 +00:00
ret = ip . eval_proc {
2009-03-06 03:56:38 +00:00
# When a block is given to 'eval_proc' method,
2004-09-02 17:17:20 +00:00
# the block is evaluated on the IP's current safe level.
2009-03-06 03:56:38 +00:00
# So, the followings raises an exception.
# An Exception object of the exception is returned as a
2004-09-13 07:25:02 +00:00
# return value of this method.
2003-09-07 07:10:44 +00:00
load file
}
2015-12-27 09:57:30 +00:00
print " \n ip.eval_proc{}, which includes insecure operation in the given block, returns an exception object: " , ret . inspect , " \n "
2004-09-13 07:25:02 +00:00
print " If a proc object is given, the proc is evaluated on the safe-level which is kept on the proc :: ip.eval_proc( proc{$SAFE} ) ==> " , ip . eval_proc ( proc { $SAFE } ) , " \n "
2004-09-02 17:17:20 +00:00
2004-09-13 07:25:02 +00:00
safe0_cmd = Proc . new {
2004-09-15 11:24:22 +00:00
print 'safe0_cmd safe-level == ' , $SAFE , " \n "
2009-03-06 03:56:38 +00:00
# This proc object keeps current safe-level ($SAFE==0).
2004-09-02 17:17:20 +00:00
load file
2004-09-13 07:25:02 +00:00
}
2004-09-15 11:24:22 +00:00
ip . eval_proc { safe0_cmd . call }
2004-09-02 17:17:20 +00:00
2003-09-07 07:10:44 +00:00
# Tk.mainloop is ignored on the slave-IP
Tk . mainloop