[Parrot-users] how do I pass obj** to a C function from parrot?

Vadim Konovalov vadrer at gmail.com
Mon Nov 30 01:03:53 UTC 2009


On Sunday 29 November 2009 13:21:04 Allison Randal wrote:
> Vadim Konovalov wrote:
> > I get an error
> > set_pmc_keyed() not implemented in class 'ManagedStruct'
...
> There are several ways to do it, so it partly depends on how you plan to
> use the array. If you want to be able to use it as an array in PIR, and
> do things like insert (or delete) elements like:
>
>     tcl_obj_array[0;i] = obj
>
> Then, your array needs to be a PMC array type, like FixedPMCArray (if
> you know how many elements you'll have) or ResizablePMCArray (if you
> don't know how many elements you'll have). A ManagedStruct can't act as
> a generic array of PMCs, which is why you're getting the error about
> "set_pmc_keyed() not implemented".

How can I have a number of TclObj* in a row using something other 
than "ManagedStruct" ?

I'll explain my question.

func2 must get a continuous row of TclObj*
Using FixedPMCArray I will get a number of PMC*, rather than TclObj*

Ot the parrot VM will do needed transformation for me?
But how it will understand on what final type it will get?

I am using "ManagedStruct" instead of UnManagedStruct because 1) the parrot 
allocates this number of elements for me and 2) I've tried ManagedStruct 
also :)

The code I'm trying is here:
http://github.com/vadrer/tcl-bridge/blob/master/TclLibrary.pir
Line 176 is my failed attempt.

(I've modified this code to have a struct of repeated TclObj* rather than a 
struct containing 1 field - array of TclObj*, but this haven't changed 
anything)

>
> Could we get a little more detail on the Tcl side of the code? It looks
> like you're embedding a Tcl interpreter inside Parrot? In which case,
> the key thing is building a variable that the 'Tcl_EvalObjv' function
> can understand.

Actually the idea was to provide tcl/tk for parrot to do GUI, similar to 
perl/Tk, or rather Tkx or Tcl::Tk in perl5. It is 'tcl-bridge' in plumage, 
and on github.

The module is capable to do the main work. Basically it can instantiate tcl 
interpreter (not partcl, rather tcl/tk binary on a system, such 
as /usr/bin/wish or /opt/ActiveTCL) and do evals, which allows for some GUI 
already.

>
> Tcl_Obj is a core Tcl type, so has to be created using Tcl's constructor
> functions. Looks like you're using 'Tcl_NewStringObj' for that. But the
> Tcl_Obj** you need to pass to 'Tcl_EvalObjv' also has to be created by
> Tcl. (Parrot can pass around a pointer to the chunk of memory that holds
> the Tcl_Obj**, but it can't know how to structure it internally so Tcl
> can read it.)

yes, you're right.
These TclObj* are created by 'tcl' by calls to 'func1' in my example.

The idea is following.
In the current module shape, it is already possible to create widgets using 
evals:

  .local pmc interp
  # ..say, interp is created
  interp.'eval'('pack [button .b -text {foo button}]')

This works.

What I am trying to do right now, is to use another way of doing the same 
thing, i.e. I want to use 

  .local pmc button, interp
 # ...
 button = interp.'widget'('.b')
 button.'call'('configure', -text, 'this is a new caption for the button')

Now the last statement does not work.  

It is even possible to wrap this last sentence to produce an eval
".b configure -text {this is a new caption for the button}"
but there will be a need to escape strings and do some protect of chars {,},
[,], etc, so this is why very much prefered way is to have a call for already 
parsed arguments.

>
> It looks like you can do this by calling the functions 'Tcl_NewListObj'
> to create a new Tcl list of Tcl_Obj's, then call
> 'Tcl_ListObjAppendElement' to add each Tcl_Obj* to the list, and then
> call 'Tcl_ListObjGetElements' to get back a Tcl_Obj** array of the
> elements of the list. That Tcl_Obj** array should be able to be passed
> directly to 'Tcl_EvalObjv'.

no :)
Actually I want quite generic thing - I have a number of C objects  of 
type "xxx*", and I want to pass a number of them via array, so we have 
type "xxx**"

>
> (BTW, from the documentation on Tcl_EvalObjv, it looks like you may also
> need to import the Tcl functions for manual reference counting
> ('Tcl_IncrRefCount' and 'Tcl_DecrRefCount'), since Tcl_EvalObjv expects
> the caller to handle incrementing and decrementing the refcounts on the
> objects passed in.)

Managing TclObj* by parrot is not the goal.
But maybe it would be reasonable to have "c" implementation? Up to now, this 
was not necessary. But if implementing on C via "pmc" types of files, this 
will allow to re-use Tcl.xs code.

Best regards,
Vadim.


More information about the Parrot-users mailing list