Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Tcl_CreateSlave

4 views
Skip to first unread message

Eric Windisch

unread,
Mar 19, 2010, 8:09:39 PM3/19/10
to tc...@perl.org
I feel it would be particularly useful to be able to create slave
interpreters from Perl. This patch includes this capability and allows
sandbox (MakeSafe) execution of Tcl code.

Usage as follows:

use Tcl;
my $interp=Tcl::new();
# Arbitrary name and a boolean 'safe' argument
my $safeslave=$interp->CreateSlave('name',1);
open (my $fh, 'script.tcl');
$safeslave->EvalFileHandle($fh);

Speaking to Jeff, he argued perhaps changing the name of the method and
accepting a hash argument:
<tclguy> my $interp = new Tcl;
<tclguy> my $safeslave = $interp->interp_create(-safe => 1);

I'm submitting this for review, thoughts, considerations, and possible
inclusion.

--
Regards,
Eric Windisch


diff -ru Tcl-0.98/Tcl.pm Tcl-0.98-vlw5iq/Tcl.pm
--- Tcl-0.98/Tcl.pm 2009-11-24 04:01:15.000000000 +0000
+++ Tcl-0.98-vlw5iq/Tcl.pm 2010-03-19 17:10:30.000000000 +0000
@@ -77,6 +77,15 @@

Invoke I<Tcl_Init> on the interpeter.

+=item $interp->CreateSlave (NAME, SAFE)
+
+Invoke I<Tcl_CreateSlave> on the interpeter. Name is arbitrary.
+The safe variable, if true, creates a safe sandbox interpreter.
+ See: http://www.tcl.tk/software/plugin/safetcl.html
+ http://www.tcl.tk/man/tcl8.4/TclCmd/safe.htm
+
+This command returns a new interpreter.
+
=item $interp->Eval (STRING, FLAGS)

Evaluate script STRING in the interpreter. If the script returns
diff -ru Tcl-0.98/Tcl.xs Tcl-0.98-vlw5iq/Tcl.xs
--- Tcl-0.98/Tcl.xs 2009-11-24 04:01:15.000000000 +0000
+++ Tcl-0.98-vlw5iq/Tcl.xs 2010-03-19 17:02:18.000000000 +0000
@@ -995,6 +995,31 @@
RETVAL

SV *
+Tcl_CreateSlave(master,name,safe)
+ Tcl master
+ char * name
+ int safe
+ CODE:
+ RETVAL = newSV(0);
+ /*
+ * We might consider Tcl_Preserve/Tcl_Release of the interp.
+ */
+ if (initialized) {
+ Tcl interp = Tcl_CreateSlave(master,name,safe);
+ /*
+ * Add to the global hash of live interps.
+ */
+ if (hvInterps) {
+ (void) hv_store(hvInterps, (const char *) &interp,
+ sizeof(Tcl), &PL_sv_undef, 0);
+ }
+ /* Create lets us set a class, should we do this too? */
+ sv_setref_pv(RETVAL, "Tcl", (void*)interp);
+ }
+ OUTPUT:
+ RETVAL
+
+SV *
Tcl_result(interp)
Tcl interp
CODE:


Jeff Hobbs

unread,
Mar 19, 2010, 9:03:26 PM3/19/10
to Eric Windisch, tc...@perl.org
On 19/03/2010 5:09 PM, Eric Windisch wrote:
> I feel it would be particularly useful to be able to create slave
> interpreters from Perl. This patch includes this capability and allows
> sandbox (MakeSafe) execution of Tcl code.

Patch seems to have been stripped, but can be found at
https://rt.cpan.org/Public/Bug/Display.html?id=55717.

> Usage as follows:
>
> use Tcl;
> my $interp=Tcl::new();
> # Arbitrary name and a boolean 'safe' argument
> my $safeslave=$interp->CreateSlave('name',1);
> open (my $fh, 'script.tcl');
> $safeslave->EvalFileHandle($fh);
>
> Speaking to Jeff, he argued perhaps changing the name of the method and
> accepting a hash argument:
> <tclguy> my $interp = new Tcl;
> <tclguy> my $safeslave = $interp->interp_create(-safe => 1);

My thought is that Tcl exposes the same functionality at the Tcl level.
Extending the C API should be kept to a minimum. However, after talking
to Jan it looks like you can't really bless the Tcl returned value in
Perl because it needs the C pointer to the structure. Thus the
CreateSlave might be the best way to go.

Strictly speaking, in the patch you wouldn't need to register the slave
in hvInterps because that is used to track destruction, and master
interps will tear down their slave interps in an orderly fashion when a
proper finalization occurs.

Jeff

Eric Windisch

unread,
Mar 19, 2010, 4:53:32 PM3/19/10
to tc...@perl.org
I feel it would be particularly useful to be able to create slave
interpreters from Perl. This patch includes this capability and allows
sandbox (MakeSafe) execution of Tcl code.

Usage as follows:

use Tcl;
my $interp=Tcl::new();
# Arbitrary name and a boolean 'safe' argument
my $safeslave=$interp->CreateSlave('name',1);
open (my $fh, 'script.tcl');
$safeslave->EvalFileHandle($fh);

Speaking to Jeff, he argued perhaps changing the name of the method and
accepting a hash argument:
<tclguy> my $interp = new Tcl;
<tclguy> my $safeslave = $interp->interp_create(-safe => 1);

I'm submitting this for review, thoughts, considerations, and possible

Konovalov, Vadim (Vadim)** CTR **

unread,
Mar 21, 2010, 7:26:30 AM3/21/10
to Jeff Hobbs, Eric Windisch, tc...@perl.org
> On 19/03/2010 5:09 PM, Eric Windisch wrote:
> > I feel it would be particularly useful to be able to create slave
> > interpreters from Perl. This patch includes this capability
> and allows
> > sandbox (MakeSafe) execution of Tcl code.
>
> Patch seems to have been stripped, but can be found at
> https://rt.cpan.org/Public/Bug/Display.html?id=55717.
>
> > Usage as follows:
> >
> > use Tcl;
> > my $interp=Tcl::new();
> > # Arbitrary name and a boolean 'safe' argument
> > my $safeslave=$interp->CreateSlave('name',1);
> > open (my $fh, 'script.tcl');
> > $safeslave->EvalFileHandle($fh);
> >
> > Speaking to Jeff, he argued perhaps changing the name of
> the method and
> > accepting a hash argument:
> > <tclguy> my $interp = new Tcl;
> > <tclguy> my $safeslave = $interp->interp_create(-safe => 1);
>

I am fine with the code,
especially if it will be updated in current repository, which is 'tcl.pm' at github.com
as we remember :)

Regards,
Vadim.

Eric Windisch

unread,
Jan 14, 2012, 11:09:07 PM1/14/12
to Konovalov, Vadim (Vadim)** CTR **, Jeff Hobbs, tc...@perl.org
I'm sending a pull request via github since this still hasn't been merged. I'll happily modify the patch if there are any unresolved concerns.

--
Eric Windisch
> especially if it will be updated in current repository, which is 'tcl.pm' at github.com (http://github.com)

Konovalov, Vadim (Vadim)** CTR **

unread,
Jan 16, 2012, 1:56:00 AM1/16/12
to Eric Windisch, Jeff Hobbs, tc...@perl.org
Thank you for your input,
I'll do the request soon.

(There is also a need to roll back few changes that were intended to provide better alloc/disposal of tcl bindings for code refs, scalar ref etc,
or may be finish these, after cleaning bit rot.)

Regards,
Vadim.

________________________________
From: Eric Windisch [mailto:er...@grokthis.net]
Sent: Sunday, January 15, 2012 7:09 AM
To: Konovalov, Vadim (Vadim)** CTR **
Cc: Jeff Hobbs; tc...@perl.org
Subject: Re: Tcl_CreateSlave
especially if it will be updated in current repository, which is 'tcl.pm' at github.com<http://github.com>
0 new messages