Re: Wrong module initialization order when building with Dub on Windows?

2018-10-01 Thread Nicholas Wilson via Digitalmars-d-learn
On Tuesday, 2 October 2018 at 01:57:00 UTC, Vladimir Panteleev 
wrote:
Ran into this today, don't have time to dig in now but maybe 
someone ran into this too.


Steps to reproduce:

- git clone https://github.com/CyberShadow/ae
- cd ae/demo/inputtiming
- (download/unpack 
https://www.libsdl.org/release/SDL2-2.0.8-win32-x86.zip or 
https://www.libsdl.org/release/SDL2-2.0.8-win32-x64.zip to 
current directory)

- dub

On Windows, I get a range violation (in ae.sys.log) which 
indicates that Runtime.args has length 0, which should never 
happen.


This doesn't happen if I build as usual with rdmd, or on Linux 
(either rdmd or Dub).


I'd take a look at what arguments are passed to DMD with rdmd vs. 
dub.

Maybe to module order is different?


Dlang tour - Unittesting example

2018-10-01 Thread Joe via Digitalmars-d-learn

There appears to be a problem with the example at

https://tour.dlang.org/tour/en/gems/unittesting

If compiled with -unittest, the resulting program crashes. It 
happens with ldc2 on Linux but it can also be seen if you click 
on "Export" and run it with dmd -unittest.


Use std.traits.getSymbolsByUDA to access members of instance.

2018-10-01 Thread Jonathan via Digitalmars-d-learn
I can use `std.traits.getSymbolsByUDA` to get all the members of 
a class that have a particular UDA `getSymbolsByUDA(ValueType, 
UDA)`.


But how do I get the values with it?

Is there a more convenient way than `__traits(getMember, value, 
getSymbolsByUDA(ValueType, UDA)[0].stringof)`?


Wrong module initialization order when building with Dub on Windows?

2018-10-01 Thread Vladimir Panteleev via Digitalmars-d-learn
Ran into this today, don't have time to dig in now but maybe 
someone ran into this too.


Steps to reproduce:

- git clone https://github.com/CyberShadow/ae
- cd ae/demo/inputtiming
- (download/unpack 
https://www.libsdl.org/release/SDL2-2.0.8-win32-x86.zip or 
https://www.libsdl.org/release/SDL2-2.0.8-win32-x64.zip to 
current directory)

- dub

On Windows, I get a range violation (in ae.sys.log) which 
indicates that Runtime.args has length 0, which should never 
happen.


This doesn't happen if I build as usual with rdmd, or on Linux 
(either rdmd or Dub).




Re: How to implement D to HTML pages ?

2018-10-01 Thread rjframe via Digitalmars-d-learn
On Mon, 01 Oct 2018 19:29:56 +, Aurélien Plazzotta wrote:
On Mon, 01 Oct 2018 19:29:56 +, Aurélien Plazzotta wrote:

> Hello guys,
> 
> I would like to implement a forum and a blog within my website
> (currently including only HTML, CSS and JS, written without CMS), using
> D and SQL but I really don't know how to proceed. How can I integrate D
> into HTML pages? Are there any kind of include's commands like PHP ?
> 
> Also, do I have to use vibe.d ? Or is it optional? I admit I didn't read
> the book yet (D Web Development), but only "Programming in D", which
> doesn't deal much with website workflow.

If you're most comfortable with PHP you may want to look at Adam Ruppe's 
arsd[0]; the readme has an overview of web-related and database-related 
modules.

vibe.d has more of a node.js feel. There's also DiamondMVC[1], which 
reminds me of ASP.NET (I'm not 100% sure whether that's intentional, and I 
haven't tried Diamond) and includes an ORM.


> Finally, what database system do you recommand to interact with for
> data-oriented website purpose? Perhaps, there are some standard or
> tierce-party librairies or ORM for PostgreSQL or SQLite ?
> 
> Thanks for all the light anyone will be willing to spread :)

There are low-level bindings for MySQL/MariaDB, Postgres, SqLite, and some 
people have made higher-level libraries and ORMs[2]. You may want to 
browse a bit and see what you like. I believe the Diamond ORM can be used 
without the rest of the framework, and the hunt-entity ORM is actively 
developed.


[0]: Repo: https://github.com/adamdruppe/arsd
 Docs: https://arsd-official.dpldocs.info/index.html (note that not 
everything is documented)
[1]: https://github.com/DiamondMVC/Diamond
[2]: http://code.dlang.org/?sort=updated=20=library.database


Re: Use nested functions as callbacks with Windows API functions?

2018-10-01 Thread spikespaz via Digitalmars-d-learn

On Monday, 1 October 2018 at 21:03:24 UTC, Boris-Barboris wrote:

On Monday, 1 October 2018 at 20:27:43 UTC, spikespaz wrote:
I was hoping I could use something more akin to JavaScript's 
syntax: (void* hWnd, long) => {}.


I tried this but I'm getting errors with the signature, it 
says the function is a delegate and apparently Windows API 
can't accept a delegate.


You can make it a non-delegate by passing a pointer to hWndList 
in lParams as it was supposed to by WinApi devs, instead of 
zero, and not implicitly capturing stack pointer by referencing 
hWndList directly from the body.


https://run.dlang.io/is/t4k4Nc


The problem with the code you have is that the callback needs to 
be extern (Windows). I don't know how to do that with a "lambda".


===

import core.sys.windows.windows: EnumWindows;
import std.stdio: writeln;

void main() {
void*[] hWndList;

EnumWindows((void* hWnd, void* lParam) nothrow {
*(cast(void*[] *) lParam) ~= hWnd;
return true;
}, );

writeln(hWndList);
}

===

source\cb.d(7): Error: function 
core.sys.windows.winuser.EnumWindows(extern (Windows) int 
function(void*, long) nothrow, long) is not callable using 
argument types (bool function(void* hWnd, void* lParam) pure 
nothrow @system, void*[]*)
source\cb.d(7):cannot pass argument __lambda1 of type 
bool function(void* hWnd, void* lParam) pure
nothrow @system to parameter extern (Windows) int function(void*, 
long) nothrow


Re: Use nested functions as callbacks with Windows API functions?

2018-10-01 Thread spikespaz via Digitalmars-d-learn

On Monday, 1 October 2018 at 21:03:24 UTC, Boris-Barboris wrote:

On Monday, 1 October 2018 at 20:27:43 UTC, spikespaz wrote:
I was hoping I could use something more akin to JavaScript's 
syntax: (void* hWnd, long) => {}.


I tried this but I'm getting errors with the signature, it 
says the function is a delegate and apparently Windows API 
can't accept a delegate.


You can make it a non-delegate by passing a pointer to hWndList 
in lParams as it was supposed to by WinApi devs, instead of 
zero, and not implicitly capturing stack pointer by referencing 
hWndList directly from the body.


https://run.dlang.io/is/t4k4Nc


I don't know how to do this. I'm not the best with pointers, I'm 
still learning D and I'm unfamiliar with functional programming.


==

import core.sys.windows.windows: EnumWindows;
import std.stdio: writeln;

extern (Windows) int callback(void* hWnd, long hWndList) nothrow {
hWndList ~= hWnd;

return true;
}

void main() {
void*[] hWndList;

EnumWindows(, );

writeln(hWndList);
}

==

Clearly I can't use  to pass the reference, how would I 
access the variable by the memory address inside the callback?




Re: Use nested functions as callbacks with Windows API functions?

2018-10-01 Thread Boris-Barboris via Digitalmars-d-learn

On Monday, 1 October 2018 at 20:27:43 UTC, spikespaz wrote:
I was hoping I could use something more akin to JavaScript's 
syntax: (void* hWnd, long) => {}.


I tried this but I'm getting errors with the signature, it says 
the function is a delegate and apparently Windows API can't 
accept a delegate.


You can make it a non-delegate by passing a pointer to hWndList 
in lParams as it was supposed to by WinApi devs, instead of zero, 
and not implicitly capturing stack pointer by referencing 
hWndList directly from the body.


https://run.dlang.io/is/t4k4Nc


Use nested functions as callbacks with Windows API functions?

2018-10-01 Thread spikespaz via Digitalmars-d-learn

I have the following code, this works.



import core.sys.windows.windows: EnumWindows;
import std.stdio: writeln;

void*[] hWndList;

extern (Windows) int callback(void* hWnd, long /* lParams */ ) 
nothrow {

hWndList ~= hWnd;

return true;
}

void main() {
EnumWindows(, 0);

writeln(hWndList);
}



I was hoping I could use something more akin to JavaScript's 
syntax: (void* hWnd, long) => {}.


I tried this but I'm getting errors with the signature, it says 
the function is a delegate and apparently Windows API can't 
accept a delegate.




import core.sys.windows.windows: EnumWindows;
import std.stdio: writeln;

void main() {
void*[] hWndList;

EnumWindows((void* hWnd, long /* lParams */ ) nothrow {
hWndList ~= hWnd; return true;
}, 0);

writeln(hWndList);
}



I'm not going to even paste the compiler error because I am very 
clearly going about this the wrong way.


Of course there is nothing wrong with defining each callback as a 
separate function, but then comes the issue of naming them. I 
also don't like the way it makes my code look.


Thanks.


Re: Prevent opening binary/other garbage files

2018-10-01 Thread Adam D. Ruppe via Digitalmars-d-learn

On Monday, 1 October 2018 at 15:21:24 UTC, helxi wrote:
I tried out https://dlang.org/library/std/utf/validate.html 
before manually checking for encoding myself so I ended up with 
the code below. I was fairly surprised that "*.o" (object) 
files are UTF encoded! Is it normal?


Yes. Any random collection of bytes <= 127 is valid utf-8. Lines 
will read until it sees a byte 10, and cut off from there.


Quite a few file formats have a 10 early on to detect text/binary 
transmission corruption, but even if they don't, it is a fairly 
common byte to see before too long and that cuts off your scan 
for later bytes.



You really are better off looking for those <32 bytes like I 
described earlier - a .o file will likely have some 1's and 3's 
early on which that will quickly detect, but those will also pass 
the validate test.




How to implement D to HTML pages ?

2018-10-01 Thread Aurélien Plazzotta via Digitalmars-d-learn

Hello guys,

I would like to implement a forum and a blog within my website 
(currently including only HTML, CSS and JS, written without CMS), 
using D and SQL but I really don't know how to proceed. How can I 
integrate D into HTML pages? Are there any kind of include's 
commands like PHP ?


Also, do I have to use vibe.d ? Or is it optional? I admit I 
didn't read the book yet (D Web Development), but only 
"Programming in D", which doesn't deal much with website workflow.


Finally, what database system do you recommand to interact with 
for data-oriented website purpose? Perhaps, there are some 
standard or tierce-party librairies or ORM for PostgreSQL or 
SQLite ?


Thanks for all the light anyone will be willing to spread :)


Re: Prevent opening binary/other garbage files

2018-10-01 Thread helxi via Digitalmars-d-learn

On Sunday, 30 September 2018 at 03:19:11 UTC, Adam D. Ruppe wrote:

On Saturday, 29 September 2018 at 23:46:26 UTC, helxi wrote:
Thanks. Would you say 
https://dlang.org/library/std/encoding/get_bom.html is useful 
in this context?


Eh, not really, most text files will not have one.


Hi,

I tried out https://dlang.org/library/std/utf/validate.html 
before manually checking for encoding myself so I ended up with 
the code below. I was fairly surprised that "*.o" (object) files 
are UTF encoded! Is it normal?


import std.stdio : File, lines, stdout;

void panic(in string message, int exitCode = 1) {
import core.stdc.stdlib : exit;
import std.stdio : stderr, writeln;

stderr.writeln(message);
exit(exitCode);
}

void writeFunc(ulong occerenceNumber, ulong lineNumber, in ref 
string fileName,

in ref string line, File ofile = stdout) {
import std.stdio : writef;

	ofile.writef("%s: L:%s: F:\"%s\":\n%s\n", occerenceNumber, 
lineNumber, fileName, line);

}

void treverseDirectories(in string path, in string term)
in {
import std.file : isDir;

if (!isDir(path))
panic("Cannot access directory: " ~ path);
}
do {
import std.file : dirEntries, SpanMode;

ulong occerenceNumber, filesChecked, filesIgnored; // = 0;
File currentFile;
foreach (string fileName; dirEntries(path, SpanMode.breadth)) {
try {
currentFile = File(fileName, "r");
++filesChecked;
			foreach (ulong lineNumber, string currentLine; 
lines(currentFile)) {

if (lineNumber == 0) {
// check if the file is encoded with 
proper UTF
// if Line 0 is not UTF encoded, move 
on to the next file

// I hope the compiler unrolls this if 
condition
import std.utf : validate;

validate(currentLine);
// throws exception if 
the file is not UTF encoded

}
import std.algorithm : canFind;

if (canFind(currentLine, term)) {
	writeFunc(++occerenceNumber, lineNumber, fileName, 
currentLine);

}
}
}
catch (Exception e) {
filesIgnored++;
}
}
//summarize
import std.stdio : writefln;

	writefln("Total match found:\t%s\nTotal files 
checked:\t%s\nTotal files ignored:\t%s\n",

occerenceNumber, filesChecked, filesIgnored);
}

void main(string[] args) {
import std.getopt : getopt;

string term, directory;
getopt(args, "term|t", , "directory|d", );

if (!directory) {
		// if directory not specified, start working with the current 
directory

import std.file : getcwd;

directory = getcwd();
}

if (!term)
panic("Term not specified.");

treverseDirectories(directory, term);
}


/*

Output:  https://pastebin.com/PZ8nCaYf


Re: contracts in interfaces: do they work, do I have to do something to enable checking of contracts ?

2018-10-01 Thread Alex via Digitalmars-d-learn

On Monday, 1 October 2018 at 13:49:53 UTC, Emil wrote:
I am trying my hand at contracts and they work fine in plain 
functions and in methods, but I can't make them work in 
interfaces.


https://dlang.org/spec/interface.html#interface-contracts

$ dmd --version
DMD64 D Compiler v2.081.1
Copyright (C) 1999-2018 by The D Language Foundation, All 
Rights Reserved written by Walter Bright



/
void main()
{
WillBloop test = new WillBloop();
// also tried
// Blooper test = new WillBloop();
// it compiles but the in contract does not seem enforced
test.limited(-1); // this does not croak

auto no_interface = new NoInterface();
no_interface.limited(-10); // this works as expected
}

interface Blooper
{
void limited(int some_other_name)
in
{
assert(some_other_name > 0, "asssert failed in 
interface");

}
}

class WillBloop : Blooper
{
void limited(int a_name){
import std.stdio : writeln;
writeln(a_name);
}
}

class NoInterface
{
void limited(int another_name)
in
{
assert(another_name > 0, "assert failed in 
NoInterface");

}
do
{
import std.stdio: writeln;
writeln(another_name);
}
}


Yeah... had such problems too, a while ago.

https://forum.dlang.org/thread/sadsceubvapbeezjy...@forum.dlang.org

Seems, that one of the tickets is closed, but the pull request is 
not merged. Don't know how matters stand with this, actually.


Re: Performance of GC.collect() for single block of `byte`s

2018-10-01 Thread Steven Schveighoffer via Digitalmars-d-learn

On 10/1/18 3:21 AM, Rainer Schuetze wrote:



On 28/09/2018 14:21, Per Nordlöw wrote:

On Monday, 24 September 2018 at 14:31:45 UTC, Steven Schveighoffer wrote:

It's not scanning the blocks. But it is scanning the stack.

Each time you are increasing the space it must search for a given 
*target*. It also must *collect* any previous items at the end of the 
scan. Note that a collection is going to mark every single page and 
bitset that is contained in the item being collected (which gets 
increasingly larger).


Is this because of the potentially (many) slices referencing this 
large block?


I assume the GC doesn't scan the `byte`-array for pointer-values in 
this case, but that happens for `void`-arrays and class/pointer-arrays 
right?


Couldn't that scan be optimized by adding a bitset that indicates 
which pages need to be scanned?


Is it common for GC's to treat large objects in this way?


A profiler reveals that most of the time is spent in "sweeping" the 
memory, i.e. looking for allocations no longer referenced. The existing 
implementation checks every page which causes a linear growth of 
required CPU resources with used memory.


Ouch! I hadn't thought of that.  But we aren't checking actual *pages*, 
right, we are checking bits to see where allocations are present?


I also remember that the way the bitsets work, they are always allocated 
for every 16 bytes, regardless of the block size for that page/pool. I 
didn't love that feature but maybe it is fixed by now.


I would imagine that checking 64 pages at once should be possible by 
logic-anding the allocated and unmarked bits to check things as quickly 
as possible.




This version https://github.com/rainers/druntime/tree/gc_opt_sweep takes 
advantage of the known size of allocations to skip unnecessary checks. 
The last commit also adds support for keeping track of the size of 
blocks of consecutive free pages. With this your example has more or 
less constant collection time (note that most of the program time is 
spent setting the array to zero, though not measured, and that the 
allocation often triggers a collection, too).


Yes, especially when you are increasing the allocation size each time.

I also noticed a rather serious bug for huge allocations: 
https://issues.dlang.org/show_bug.cgi?id=19281


-Steve


contracts in interfaces: do they work, do I have to do something to enable checking of contracts ?

2018-10-01 Thread Emil via Digitalmars-d-learn
I am trying my hand at contracts and they work fine in plain 
functions and in methods, but I can't make them work in 
interfaces.


https://dlang.org/spec/interface.html#interface-contracts

$ dmd --version
DMD64 D Compiler v2.081.1
Copyright (C) 1999-2018 by The D Language Foundation, All Rights 
Reserved written by Walter Bright



/
void main()
{
WillBloop test = new WillBloop();
// also tried
// Blooper test = new WillBloop();
// it compiles but the in contract does not seem enforced
test.limited(-1); // this does not croak

auto no_interface = new NoInterface();
no_interface.limited(-10); // this works as expected
}

interface Blooper
{
void limited(int some_other_name)
in
{
assert(some_other_name > 0, "asssert failed in 
interface");

}
}

class WillBloop : Blooper
{
void limited(int a_name){
import std.stdio : writeln;
writeln(a_name);
}
}

class NoInterface
{
void limited(int another_name)
in
{
assert(another_name > 0, "assert failed in NoInterface");
}
do
{
import std.stdio: writeln;
writeln(another_name);
}
}


Re: -O flag ; automatic cast in a bitshift

2018-10-01 Thread Guillaume Lathoud via Digitalmars-d-learn
On Friday, 21 September 2018 at 01:44:33 UTC, Vladimir Panteleev 
wrote:
On Thursday, 20 September 2018 at 11:14:05 UTC, Guillaume FYI, 
it's undefined in D mainly because the behavior of the actual 
Intel CPU instruction is undefined in such cases:


https://c9x.me/x86/html/file_module_x86_id_285.html

"it is undefined for SHL and SHR instructions where the count 
is greater than or equal to the size (in bits) of the 
destination operand".


Thanks, good to know.


Re: Linking with a non-default druntime

2018-10-01 Thread Per Nordlöw via Digitalmars-d-learn

On Monday, 1 October 2018 at 08:27:54 UTC, Basile B. wrote:
I think so. Apparently it's registered with a string, e.g 
"manual" and you pass a special druntime option with your 
program to select.
Actually i would be interested to make the interface with 
assignable handlers since you don't seem to be very hot. Maybe 
tomorrow i can try. I'm almost sure that this could work but 
cant be 100% sure. Maybe there'll be issues with privacy and 
events to assign.


Be my guest :)

Thanks!


Re: Invalid string literal in ASM

2018-10-01 Thread Basile B. via Digitalmars-d-learn

On Monday, 1 October 2018 at 09:24:47 UTC, Basile B. wrote:

On Monday, 1 October 2018 at 08:14:07 UTC, dokutoku wrote:
I get a compiler error when I try to put non-ASCII characters 
in a string literal in the inline assembler.


Is this part of the specifications?


It's not clear, see https://dlang.org/spec/iasm.html#raw_data:

"if an operand is a string literal, it is as if there were 
length operands, where length is the number of characters in 
the string"


db "e"; // ok
db "é"; // error

it seems that the second case should be accepted as db 195 db 
169


BUG imo. length is 2. "é" should be interpreted as the 2 bytes of 
its data.

Something with decoding is wrong here.


Re: Invalid string literal in ASM

2018-10-01 Thread Basile B. via Digitalmars-d-learn

On Monday, 1 October 2018 at 08:14:07 UTC, dokutoku wrote:
I get a compiler error when I try to put non-ASCII characters 
in a string literal in the inline assembler.


Is this part of the specifications?


It's not clear, see https://dlang.org/spec/iasm.html#raw_data:

"if an operand is a string literal, it is as if there were length 
operands, where length is the number of characters in the string"


db "e"; // ok
db "é"; // error

it seems that the second case should be accepted as db 195 db 169



Re: Linking with a non-default druntime

2018-10-01 Thread Basile B. via Digitalmars-d-learn

On Monday, 1 October 2018 at 07:17:59 UTC, Per Nordlöw wrote:

On Sunday, 30 September 2018 at 19:53:02 UTC, Basile B. wrote:
this way you can very easily change-compile-test, without 
recompiling the whole runtime and phobos each time.


Ok, thanks.

Is it possible to register an extra GC in a separate program by 
overriding the logic in `gc.proxy` in druntime?


That would be the most effective way.


I think so. Apparently it's registered with a string, e.g 
"manual" and you pass a special druntime option with your program 
to select.
Actually i would be interested to make the interface with 
assignable handlers since you don't seem to be very hot. Maybe 
tomorrow i can try. I'm almost sure that this could work but cant 
be 100% sure. Maybe there'll be issues with privacy and events to 
assign.


Invalid string literal in ASM

2018-10-01 Thread dokutoku via Digitalmars-d-learn
I get a compiler error when I try to put non-ASCII characters in 
a string literal in the inline assembler.


Is this part of the specifications?


Re: Performance of GC.collect() for single block of `byte`s

2018-10-01 Thread Rainer Schuetze via Digitalmars-d-learn




On 28/09/2018 14:21, Per Nordlöw wrote:

On Monday, 24 September 2018 at 14:31:45 UTC, Steven Schveighoffer wrote:

It's not scanning the blocks. But it is scanning the stack.

Each time you are increasing the space it must search for a given 
*target*. It also must *collect* any previous items at the end of the 
scan. Note that a collection is going to mark every single page and 
bitset that is contained in the item being collected (which gets 
increasingly larger).


Is this because of the potentially (many) slices referencing this large 
block?


I assume the GC doesn't scan the `byte`-array for pointer-values in this 
case, but that happens for `void`-arrays and class/pointer-arrays right?


Couldn't that scan be optimized by adding a bitset that indicates which 
pages need to be scanned?


Is it common for GC's to treat large objects in this way?


A profiler reveals that most of the time is spent in "sweeping" the 
memory, i.e. looking for allocations no longer referenced. The existing 
implementation checks every page which causes a linear growth of 
required CPU resources with used memory.


This version https://github.com/rainers/druntime/tree/gc_opt_sweep takes 
advantage of the known size of allocations to skip unnecessary checks. 
The last commit also adds support for keeping track of the size of 
blocks of consecutive free pages. With this your example has more or 
less constant collection time (note that most of the program time is 
spent setting the array to zero, though not measured, and that the 
allocation often triggers a collection, too).


I also noticed a rather serious bug for huge allocations: 
https://issues.dlang.org/show_bug.cgi?id=19281


Re: Linking with a non-default druntime

2018-10-01 Thread Per Nordlöw via Digitalmars-d-learn

On Sunday, 30 September 2018 at 19:53:02 UTC, Basile B. wrote:
this way you can very easily change-compile-test, without 
recompiling the whole runtime and phobos each time.


Ok, thanks.

Is it possible to register an extra GC in a separate program by 
overriding the logic in `gc.proxy` in druntime?


That would be the most effective way.