Rocksolid Light

Welcome to Rocksolid Light

mail  files  register  newsreader  groups  login

Message-ID:  

Life is a game. Money is how we keep score. -- Ted Turner


devel / comp.lang.tcl / What is the proper way to get the value of Tcl object via Tcl API

SubjectAuthor
* What is the proper way to get the value of Tcl object via Tcl APIclt.to.davebr
`* What is the proper way to get the value of Tcl object via Tcl APINicolas Robert
 `* What is the proper way to get the value of Tcl object via Tcl APIRich
  `- What is the proper way to get the value of Tcl object via Tcl APINicolas Robert

1
What is the proper way to get the value of Tcl object via Tcl API

<8551685977578@dlp>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=11657&group=comp.lang.tcl#11657

  copy link   Newsgroups: comp.lang.tcl
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: clt.to.davebr@dfgh.net
Newsgroups: comp.lang.tcl
Subject: What is the proper way to get the value of Tcl object via Tcl API
Date: Mon, 05 Jun 23 15:06:18 GMT
Organization: A noiseless patient Spider
Lines: 33
Message-ID: <8551685977578@dlp>
Injection-Info: dont-email.me; posting-host="fb80b61c31f697fc5fa849188b8221ed";
logging-data="400627"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX180CUxcETN7ZInRyu0mRtc4"
Cancel-Lock: sha1:KkkoxnKvQLVc6g5UMKe2UwMnLko=
 by: clt.to.davebr@dfgh.net - Mon, 5 Jun 2023 15:06 UTC

You might be better off trying to avoid having to guess how to process the list objects.

However given that you are creating and executing a command from Tcl_Objs, consider using Tcl_EvalObjv, and instead of checking that the string value looks like an object name just execute the get method on the Tcl_Obj and check if there was an error.

// set up command object array (consider doing this only once in your application)
// I think this is the correct way to initalize a C array to NULLs ??
Tcl_Obj* cmd[3] = {NULL, NULL, NULL};

// second word of command is "get"
cmd[1] = Tcl_NewStringObj("get", -1);

// make sure the "get" does not disappear at an inopportune time.
Tcl_IncrRefCount(cmd[1]);

// need to decrement the reference count (Tcl_DecrRefCount(cmd[1]);)
// just before the cmd[] array goes out of scope to avoid a memory leak

....

// try executing a get method on the current list entry
// if there is a chance the sub element has a zero reference count (unlikely for a list element),
// increment it here (and remember to decrement it later to avoid a memory leak)
cmd[0] = sub_elements[i];
if (TCL_OK == Tcl_EvalObjv(interp, cmd, 0)) {
DO SOMETHING with the result in interp
} else {
TRY SOMETHING ELSE
}

I did not compile or execute this YMMV

Dave B

Re: What is the proper way to get the value of Tcl object via Tcl API

<2dbda6eb-1571-4665-8d78-874f6aeae0b2n@googlegroups.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=11683&group=comp.lang.tcl#11683

  copy link   Newsgroups: comp.lang.tcl
X-Received: by 2002:ad4:4e68:0:b0:625:aa48:dddf with SMTP id ec8-20020ad44e68000000b00625aa48dddfmr1840qvb.11.1686070363152;
Tue, 06 Jun 2023 09:52:43 -0700 (PDT)
X-Received: by 2002:a05:620a:4628:b0:75d:54cd:80a5 with SMTP id
br40-20020a05620a462800b0075d54cd80a5mr104332qkb.2.1686070363022; Tue, 06 Jun
2023 09:52:43 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.tcl
Date: Tue, 6 Jun 2023 09:52:42 -0700 (PDT)
In-Reply-To: <8551685977578@dlp>
Injection-Info: google-groups.googlegroups.com; posting-host=77.145.154.41; posting-account=12Ku7QoAAABZfwpOT4Ksf001bDFF99F1
NNTP-Posting-Host: 77.145.154.41
References: <8551685977578@dlp>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <2dbda6eb-1571-4665-8d78-874f6aeae0b2n@googlegroups.com>
Subject: Re: What is the proper way to get the value of Tcl object via Tcl API
From: nicolasrobert.19000@gmail.com (Nicolas Robert)
Injection-Date: Tue, 06 Jun 2023 16:52:43 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 3776
 by: Nicolas Robert - Tue, 6 Jun 2023 16:52 UTC

Le lundi 5 juin 2023 à 17:05:22 UTC+2, clt.to...@dfgh.net a écrit :
> Create the list this way:
> set myList [list 1 2 3 4 [::oo::Obj10 get] foo 10 bar]

@Rich ,
I can't, the goal here is to check each type, so I need to keep the name of the object.

> You might be better off trying to avoid having to guess how to process the list objects.
>
> However given that you are creating and executing a command from Tcl_Objs, consider using Tcl_EvalObjv, and instead of checking that the string value looks like an object name just execute the get method on the Tcl_Obj and check if there was an error.
>
> // set up command object array (consider doing this only once in your application)
> // I think this is the correct way to initalize a C array to NULLs ??
> Tcl_Obj* cmd[3] = {NULL, NULL, NULL};
>
> // second word of command is "get"
> cmd[1] = Tcl_NewStringObj("get", -1);
>
> // make sure the "get" does not disappear at an inopportune time.
> Tcl_IncrRefCount(cmd[1]);
>
> // need to decrement the reference count (Tcl_DecrRefCount(cmd[1]);)
> // just before the cmd[] array goes out of scope to avoid a memory leak
>
> ...
>
> // try executing a get method on the current list entry
> // if there is a chance the sub element has a zero reference count (unlikely for a list element),
> // increment it here (and remember to decrement it later to avoid a memory leak)
> cmd[0] = sub_elements[i];
> if (TCL_OK == Tcl_EvalObjv(interp, cmd, 0)) {
> DO SOMETHING with the result in interp
> } else {
> TRY SOMETHING ELSE
> }
>
> I did not compile or execute this YMMV

@Dave

If I understand , In that way so in Tcl :
foreach obj $myList {
if {![catch {$obj get} value]} {
# Do something
}
} Maybe I'm wrong but in pure Tcl, I would never do like that, It's maybe different in C.
That said
Below my C code :
Tcl_Obj* cmd[2];
cmd[1] = Tcl_NewStringObj ("get", -1);
Tcl_IncrRefCount(cmd[1]);

for (int i = 0; i < count; ++i) {
cmd[0] = sub_elements[i];
Tcl_IncrRefCount(cmd[0]);

if (Tcl_EvalObjv(interp, 2, cmd, 0) == TCL_OK) {
Tcl_DecrRefCount(cmd[0]);
Tcl_ListObjAppendElement(interp, data, Tcl_GetObjResult(interp));
} else {
// Do something...
}
} Tcl_DecrRefCount(cmd[1]);

I’m not sure about myself, it works , but I tested on a list of 20000 items and my performance is bad, my C code is correct ?

Thanks
Nicolas

Re: What is the proper way to get the value of Tcl object via Tcl API

<u5nonl$pv9j$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=11684&group=comp.lang.tcl#11684

  copy link   Newsgroups: comp.lang.tcl
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: rich@example.invalid (Rich)
Newsgroups: comp.lang.tcl
Subject: Re: What is the proper way to get the value of Tcl object via Tcl API
Date: Tue, 6 Jun 2023 17:00:37 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 25
Message-ID: <u5nonl$pv9j$1@dont-email.me>
References: <8551685977578@dlp> <2dbda6eb-1571-4665-8d78-874f6aeae0b2n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 6 Jun 2023 17:00:37 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="ff58e24e25022d6167b44208d1ea529a";
logging-data="851251"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/gayCR72FUi/TNE4YzPNtV"
User-Agent: tin/2.6.1-20211226 ("Convalmore") (Linux/5.15.19 (x86_64))
Cancel-Lock: sha1:ZbxhVcQ4y6uJYktV4++8aQrQo9Q=
 by: Rich - Tue, 6 Jun 2023 17:00 UTC

Nicolas Robert <nicolasrobert.19000@gmail.com> wrote:
> Le lundi 5 juin 2023 à 17:05:22 UTC+2, clt.to...@dfgh.net a écrit :
>> Create the list this way:
>> set myList [list 1 2 3 4 [::oo::Obj10 get] foo 10 bar]
>
> I can't, the goal here is to check each type, so I need to keep the
> name of the object.

Then consider changing your list definition:

set myList [list val 1 val 2 val 3 val 4 obj ::oo::Obj10 val foo val 10 val bar]

Then (in Tcl) you could process something like this way:

foreach {type content} $myList {
switch -exact -- $type {
val { # do something with a value }
obj { # do a [$content get] to get the value, then do something
with the value and the object name }
}
}

A similar 'loop' in C could be crafted but I'm not going to attempt to
type that out here (it would likely contain many syntax errors).

Re: What is the proper way to get the value of Tcl object via Tcl API

<50fdc1b2-c62b-4a80-ac02-a66f02cf3e0an@googlegroups.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=11685&group=comp.lang.tcl#11685

  copy link   Newsgroups: comp.lang.tcl
X-Received: by 2002:a05:6214:1846:b0:62b:697f:80f8 with SMTP id d6-20020a056214184600b0062b697f80f8mr27118qvy.0.1686071732394;
Tue, 06 Jun 2023 10:15:32 -0700 (PDT)
X-Received: by 2002:a05:620a:1a0e:b0:759:3297:5774 with SMTP id
bk14-20020a05620a1a0e00b0075932975774mr102260qkb.8.1686071732215; Tue, 06 Jun
2023 10:15:32 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.tcl
Date: Tue, 6 Jun 2023 10:15:31 -0700 (PDT)
In-Reply-To: <u5nonl$pv9j$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=77.145.154.41; posting-account=12Ku7QoAAABZfwpOT4Ksf001bDFF99F1
NNTP-Posting-Host: 77.145.154.41
References: <8551685977578@dlp> <2dbda6eb-1571-4665-8d78-874f6aeae0b2n@googlegroups.com>
<u5nonl$pv9j$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <50fdc1b2-c62b-4a80-ac02-a66f02cf3e0an@googlegroups.com>
Subject: Re: What is the proper way to get the value of Tcl object via Tcl API
From: nicolasrobert.19000@gmail.com (Nicolas Robert)
Injection-Date: Tue, 06 Jun 2023 17:15:32 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 2536
 by: Nicolas Robert - Tue, 6 Jun 2023 17:15 UTC

Le mardi 6 juin 2023 à 19:00:41 UTC+2, Rich a écrit :
> Nicolas Robert <nicolasro...@gmail.com> wrote:
> > Le lundi 5 juin 2023 à 17:05:22 UTC+2, clt.to...@dfgh.net a écrit :
> >> Create the list this way:
> >> set myList [list 1 2 3 4 [::oo::Obj10 get] foo 10 bar]
> >
> > I can't, the goal here is to check each type, so I need to keep the
> > name of the object.
> Then consider changing your list definition:
>
> set myList [list val 1 val 2 val 3 val 4 obj ::oo::Obj10 val foo val 10 val bar]
>
> Then (in Tcl) you could process something like this way:
>
> foreach {type content} $myList {
> switch -exact -- $type {
> val { # do something with a value }
> obj { # do a [$content get] to get the value, then do something
> with the value and the object name }
> }
> }
>
> A similar 'loop' in C could be crafted but I'm not going to attempt to
> type that out here (it would likely contain many syntax errors).

Rich,

I do not know the type of my strings before testing them via a loop.
So my loop in Tcl is like this :
foreach content $myList {
if {[string is ...]} {...}
if {[info object isa object ...] {...}
}

I’d like to do the same in C.

Nicolas


devel / comp.lang.tcl / What is the proper way to get the value of Tcl object via Tcl API

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor