Re: toStringz lifetime

2020-11-08 Thread Ali Çehreli via Digitalmars-d-learn

On 11/8/20 6:58 PM, rikki cattermole wrote:

> On 09/11/2020 2:58 PM, Ali Çehreli wrote:
>> Does the D GC know the complete function call stack of the C program
>> all the way up from 'main'? Is there the concept of "bottom of the
>> stack"

> 
https://github.com/dlang/druntime/blob/master/src/core/thread/osthread.d#L1455 



> I'm tired, so here is the code related to your questions.

Hey, I'm tired too! :p

Thank you. By the presence of getStackTop() and getStackBottom() above, 
I'm convinced that the entire stack is available. So, pointer returned 
by toStringz will be kept alive by the C caller during their immediate 
use. (They obviously cannot store for later use.)


Ali




Re: toStringz lifetime

2020-11-08 Thread rikki cattermole via Digitalmars-d-learn

On 09/11/2020 2:58 PM, Ali Çehreli wrote:
Does the D GC know the complete function call stack of the C program all 
the way up from 'main'? Is there the concept of "bottom of the stack" or 
does the D GC can only know the value of the stack pointer at the time 
rt_init() was called. If the latter, then I think a toStringz string may 
not be alive in a C function.


https://github.com/dlang/druntime/blob/master/src/core/thread/context.d#L16
https://github.com/dlang/druntime/blob/master/src/core/thread/threadbase.d#L469
https://github.com/dlang/druntime/blob/master/src/core/thread/osthread.d#L1455
https://github.com/dlang/druntime/blob/master/src/core/thread/osthread.d#L1208

I'm tired, so here is the code related to your questions.

Note: the GC will use this abstraction for dealing with stack frames 
(otherwise it would be duplicated).


Re: toStringz lifetime

2020-11-08 Thread Ali Çehreli via Digitalmars-d-learn

On 10/25/20 3:19 AM, rikki cattermole wrote:

On 25/10/2020 11:03 PM, Ali Çehreli wrote:
Does the GC see that local variable 'name' that is on the C side? What 
I don't know is whether the GC is aware only of the stack frames of D 
functions or the entire thread, which would include the C caller's 
'name'.


The thread stack frame that is registered with the D GC will know about 
the D side and may know about the C side.


It depends on what the C side is doing.

If the C side went ahead and made a new stack frame via a fiber... it 
won't know about it. But even if it did, the D stack frame is still 
alive and pinning that bit of memory.


Ultimately, if the C side puts that pointer some place like a global or 
send it to another thread, there are no guarantees that things will play 
out well.


Sorry to bring this up again but I want to understand this fully before 
I say something wrong during my DConf presentation. :)


The D code is a library. The actual program is e.g. written in C. When 
the D library is loaded into the program, the following function is 
executed and the D GC is initialized:


pragma (crt_constructor)
extern(C) int initialize() {
  return rt_init();
}

Does the D GC know the complete function call stack of the C program all 
the way up from 'main'? Is there the concept of "bottom of the stack" or 
does the D GC can only know the value of the stack pointer at the time 
rt_init() was called. If the latter, then I think a toStringz string may 
not be alive in a C function.


Imagine the C program dlopens our library from inside a function called 
from main. Then the program calls one of our library functions from 
another function in main:


// C program
int main() {
  initializeDlibrary();  // This does dlopen()
  useDlibrary();// This receives a string returned from
// toStringZ and uses that string.
}

So, the question is, does D GC only know initializeDlibrary's stack 
frame up because it was initialized there?


I know threads complicate matters and they need to be attached to the GC 
with core.thread.osthread.thread_attachThis but I am not there yet. :) I 
want to understand the basic single thread stack pointer issue first.


Thank you,
Ali



Re: asdf get first value from a json string.

2020-11-08 Thread frame via Digitalmars-d-learn

On Sunday, 8 November 2020 at 19:29:39 UTC, frame wrote:

On Sunday, 8 November 2020 at 16:30:40 UTC, Vino wrote:
   Request your help on how to get the first value of "type" 
from the below json, the expected output required is as below,


You need a data structure to work with, eg:

static struct S {
   string characteristicValue;
}

foreach (ref j; parseJson(data)["items"].byElement()) {
   auto sArr = j["type"].deserialize!(S[]);
   writefln("value: %s", s[0].characteristicValue);
}

Then split your S.characteristicValue into tokens with 
std.array.split


writefln("value: %s", sArr[0].characteristicValue);


Re: asdf get first value from a json string.

2020-11-08 Thread frame via Digitalmars-d-learn

On Sunday, 8 November 2020 at 16:30:40 UTC, Vino wrote:
   Request your help on how to get the first value of "type" 
from the below json, the expected output required is as below,


You need a data structure to work with, eg:

static struct S {
   string characteristicValue;
}

foreach (ref j; parseJson(data)["items"].byElement()) {
   auto sArr = j["type"].deserialize!(S[]);
   writefln("value: %s", s[0].characteristicValue);
}

Then split your S.characteristicValue into tokens with 
std.array.split





asdf get first value from a json string.

2020-11-08 Thread Vino via Digitalmars-d-learn

Hi All,

   Request your help on how to get the first value of "type" from 
the below json, the expected output required is as below,


{"characteristicValue":"TT,t...@dev.com,DEV"}

output1: TT
output2: t...@dev.com

Code:

/+dub.sdl:
dependency "asdf" version="~>0.6.6"
+/
import std;
import asdf;

void main()
{
 string data = `{"items":[{
  "hostname":"test01",
  "type":[
  {"characteristicValue":"TT,t...@dev.com,DEV"},
  {"characteristicValue":"000"}
]}]}`;

 foreach (ref j; parseJson(data)["items"].byElement()) {
writeln(j["type"]) ;
 }
}

From
Vino.B


Re: How add class or struct member after construction?

2020-11-08 Thread Jacob Carlborg via Digitalmars-d-learn

On 2020-11-05 23:48, Marcone wrote:
How add class or struct member after construction? Is it possible in D? 
How?


It depends on what needs you have. You can declare a free function that 
takes the class/struct as the first parameter and call it like a method [1]:


class Foo
{
int a;
}

void printA(Foo foo)
{
writeln(foo.a);
}

foo.printA();
printA(foo);

The two above lines are exactly the same.

[1] https://dlang.org/spec/function.html#pseudo-member

--
/Jacob Carlborg


Re: DMD: invalid UTF character `\U0000d800`

2020-11-08 Thread Jacob Carlborg via Digitalmars-d-learn

On 2020-11-08 13:39, Kagamin wrote:

Surrogate pairs are used in rules because java strings are utf-16 
encoded, it doesn't make much sense for other encodings.


D supports the UTF-16 encoding as well. The compiler doesn't accept the 
surrogate pairs even for UTF-16 strings.


--
/Jacob Carlborg


Re: How exactly does Tuple work?

2020-11-08 Thread Adam D. Ruppe via Digitalmars-d-learn

On Sunday, 8 November 2020 at 13:57:08 UTC, Jan Hönig wrote:
So it's like inheritance resolved at compile time. It's 
inheritance with virtual member functions without overhead.

I am guessing only one alias works.

And we use this, because struct can't do inheritance and 
interface is abstract.


yeah, basically.


Re: How exactly does Tuple work?

2020-11-08 Thread Jan Hönig via Digitalmars-d-learn

On Sunday, 8 November 2020 at 13:10:33 UTC, Adam D. Ruppe wrote:

On Sunday, 8 November 2020 at 10:03:46 UTC, Jan Hönig wrote:
Is there some recourse, which explains the `alias  
this`?


If your object is used in a way that doesn't compile, the 
compiler will change `obj` to `obj.whatever_alias_this_is` and 
try again.


So say you have

struct S {
int a;
alias a this;
}

S obj;

obj += 5;


It will see that obj +=5 doesn't compile on its own, but it has 
alias a this so it changes `obj` to `obj.a` and tries again.


So `obj.a += 5;` is the end result.


So it's like inheritance resolved at compile time. It's 
inheritance with virtual member functions without overhead.

I am guessing only one alias works.

And we use this, because struct can't do inheritance and 
interface is abstract.





Re: DMD: invalid UTF character `\U0000d800`

2020-11-08 Thread Steven Schveighoffer via Digitalmars-d-learn

On 11/8/20 5:47 AM, Per Nordlöw wrote:

On Saturday, 7 November 2020 at 17:49:54 UTC, Jacob Carlborg wrote:

[1] https://en.wikipedia.org/wiki/UTF-16#U+D800_to_U+DFFF


Thanks!

I'm only using these UTF characters to create ranges that source code 
characters as checked against during parsing. Therefore I would like to 
just convert these to a `dchar` for now using a `cast`. Can I just do, 
for instance,


     cast(dchar)0xd8000

for

     `\Ud800`

to accomplish this?


Yes, use the cast. It should work.

It's just the D grammar that is stopping you, a dchar is just an integer 
under the hood, so the cast should be fine.


-Steve


Re: How exactly does Tuple work?

2020-11-08 Thread Adam D. Ruppe via Digitalmars-d-learn

On Sunday, 8 November 2020 at 10:03:46 UTC, Jan Hönig wrote:
Is there some recourse, which explains the `alias  
this`?


If your object is used in a way that doesn't compile, the 
compiler will change `obj` to `obj.whatever_alias_this_is` and 
try again.


So say you have

struct S {
int a;
alias a this;
}

S obj;

obj += 5;


It will see that obj +=5 doesn't compile on its own, but it has 
alias a this so it changes `obj` to `obj.a` and tries again.


So `obj.a += 5;` is the end result.


Re: DMD: invalid UTF character `\U0000d800`

2020-11-08 Thread Kagamin via Digitalmars-d-learn

On Sunday, 8 November 2020 at 10:47:34 UTC, Per Nordlöw wrote:

dchar


Surrogate pairs are used in rules because java strings are utf-16 
encoded, it doesn't make much sense for other encodings.


Re: DMD: invalid UTF character `\U0000d800`

2020-11-08 Thread Per Nordlöw via Digitalmars-d-learn

On Sunday, 8 November 2020 at 10:47:34 UTC, Per Nordlöw wrote:

cast(dchar)0xd8000


To clarify,

enum dch1 = cast(dchar)0xa0a0;
enum dch2 = '\ua0a0';
assert(dch1 == dch2);

works. Can I use the first-variant if I want to postpone these 
encoding questions for now?


Re: DMD: invalid UTF character `\U0000d800`

2020-11-08 Thread Per Nordlöw via Digitalmars-d-learn
On Saturday, 7 November 2020 at 17:49:54 UTC, Jacob Carlborg 
wrote:

[1] https://en.wikipedia.org/wiki/UTF-16#U+D800_to_U+DFFF


Thanks!

I'm only using these UTF characters to create ranges that source 
code characters as checked against during parsing. Therefore I 
would like to just convert these to a `dchar` for now using a 
`cast`. Can I just do, for instance,


cast(dchar)0xd8000

for

`\Ud800`

to accomplish this?


Re: How exactly does Tuple work?

2020-11-08 Thread Jan Hönig via Digitalmars-d-learn

On Saturday, 7 November 2020 at 18:31:18 UTC, Paul Backus wrote:
Indexing and slicing are implemented with `alias expand this`, 
which causes `t[i]` to be lowered to `t.expand[i]`.


Is there some recourse, which explains the `alias  
this`? I still don't understand what it does. I can't imagine 
what it does to my my class/struct.