Thread overview
betterC shared static ctor
Jul 21, 2021
vit
Jul 21, 2021
Mike Parker
Jul 21, 2021
vit
July 21, 2021

Is it possible to call all shared static ctors in betterC?

//-betterC

static immutable int i;

shared static this(){
	i = 42;
}
extern(C) void main(){
    assert(i != 42);

}
July 21, 2021

On Wednesday, 21 July 2021 at 08:11:06 UTC, vit wrote:

>

Is it possible to call all shared static ctors in betterC?

//-betterC

static immutable int i;

shared static this(){
	i = 42;
}
extern(C) void main(){
    assert(i != 42);

}

These rely on DRuntime, which is not linked in betterC mode. You'll need to use the crt_constructor and crt_destructor pragmas:

https://dlang.org/spec/pragma.html#crtctor

Apply them to any `extern(C) function for static ctor/dtor behavior.

July 21, 2021

On Wednesday, 21 July 2021 at 08:28:22 UTC, Mike Parker wrote:

>

On Wednesday, 21 July 2021 at 08:11:06 UTC, vit wrote:

>

Is it possible to call all shared static ctors in betterC?

//-betterC

static immutable int i;

shared static this(){
	i = 42;
}
extern(C) void main(){
    assert(i != 42);

}

These rely on DRuntime, which is not linked in betterC mode. You'll need to use the crt_constructor and crt_destructor pragmas:

https://dlang.org/spec/pragma.html#crtctor

Apply them to any `extern(C) function for static ctor/dtor behavior.

Thanks, it works, but now I have different problem.
I need call static method for all instantions of template struct from crt_constructor.
Is there way to iterate over all instantions of template?

I need this to work but for all instantions of Foo, not only for Foo!1 a Foo!2

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

pragma(crt_constructor)
extern(C)void shared_static_this(){
    Foo!1.vtable_init();
    Foo!2.vtable_init();
    //Foo!3.vtable_init();
}

extern(C) void main(){
    auto foo1 = Foo!1(null);
    auto foo2 = Foo!2(null);
    auto foo3 = Foo!3(null);

    Base* b1 = &foo1.base;
    Base* b2 = &foo2.base;
    Base* b3 = &foo3.base;
    assert(b1.getId() == 1);
    assert(b2.getId() == 2);
    //assert(b3.getId() == 3);	//vtable is not initialized

}

struct Vtable{
    size_t offset;
	size_t function(void* ) getId;
}
struct Base{
    immutable Vtable* vptr;

    this(immutable Vtable* vptr){
        assert(vptr !is null);
    	this.vptr = vptr;
    }

    size_t getId(){
    	return vptr.getId((cast(void*)&this) - vptr.offset);
    }
}

struct Foo(size_t id){
    static immutable Vtable vtable;
Base base;


    this(typeof(null) nil){
        this.base = Base(&vtable);
    }

    static size_t virtual_getId(Foo* foo){
    	return id;
    }

    static void vtable_init(){
    	Vtable* vtable = cast(Vtable*)&vtable;
        vtable.offset = base.offsetof;
        vtable.getId = cast(typeof(Vtable.getId))&virtual_getId;
    }
}

July 21, 2021

On 7/21/21 5:07 AM, vit wrote:

>

Thanks, it works, but now I have different problem.
I need call static method for all instantions of template struct from crt_constructor.
Is there way to iterate over all instantions of template?

Not unless you register them somehow upon instantiation.

Or (I think) you can implement the constructor as a static method, and tag it with pragma(crt_constructor). Then it gets added when instantiated.

-Steve