Thread overview
WebAssembly _start or crt_constructor not get called.
Aug 10, 2018
learnfirst1
Aug 10, 2018
learnfirst1
Aug 10, 2018
kinke
Aug 10, 2018
learnfirst1
Aug 10, 2018
kinke
August 10, 2018
extern(C) {
	__gshared int counter = 1;
	pragma(crt_constructor) void ginit(){
		counter++;
	}
	
	int getCounter(){
		return counter;
	}

	void _start() {
		counter++;
	}
}
----------------------

build with ldc2 -mtriple=wasm32-unknown-unknown-wasm -betterC test.d -link-internally

I expect the getCounter return 3, but it return 0, is this a bug  ?
August 10, 2018
On Friday, 10 August 2018 at 08:45:07 UTC, learnfirst1 wrote:
>
> extern(C) {
> 	__gshared int counter = 1;
> 	pragma(crt_constructor) void ginit(){
> 		counter++;
> 	}
> 	
> 	int getCounter(){
> 		return counter;
> 	}
>
> 	void _start() {
> 		counter++;
> 	}
> }
> ----------------------
>
> build with ldc2 -mtriple=wasm32-unknown-unknown-wasm -betterC test.d -link-internally
>
> I expect the getCounter return 3, but it return 0, is this a bug  ?

I write wrong, it return 1 not 0 on browser.
August 10, 2018
I don't know about _start(), but please don't expect `pragma(crt_constructor)` to work. As the name says, these 'C runtime constructors' are normally invoked by the C runtime entry point, before calling the C main, all of which doesn't apply to WebAssembly or any other bare metal target.
August 10, 2018
On Friday, 10 August 2018 at 10:43:09 UTC, kinke wrote:
> I don't know about _start(), but please don't expect `pragma(crt_constructor)` to work. As the name says, these 'C runtime constructors' are normally invoked by the C runtime entry point, before calling the C main, all of which doesn't apply to WebAssembly or any other bare metal target.

please take a look at this:  http://llvm.org/doxygen/WebAssemblyLowerGlobalDtors_8cpp_source.html


Since LLVM can support WebAssembly GlobalCtors,  LDC also append crt_constructor into llvm::GlobalCtors,  why not make ldc with WebAssembly work same way.
August 10, 2018
On Friday, 10 August 2018 at 11:22:39 UTC, learnfirst1 wrote:
> please take a look at this:  http://llvm.org/doxygen/WebAssemblyLowerGlobalDtors_8cpp_source.html
>
>
> Since LLVM can support WebAssembly GlobalCtors,  LDC also append crt_constructor into llvm::GlobalCtors,  why not make ldc with WebAssembly work same way.

Thx for the link, but I don't see a corresponding file for global *con*structors. LDC emits them into `llvm.global_{ctor,dtor}s` (see https://run.dlang.io/is/q8TIBU and hit the `IR` button), so I guess the lowering already works for `pragma(crt_destructor)`.