@@ -26,79 +26,116 @@ module examples.hello_d_main;
26
26
// import core.stdc.stdio : printf;
27
27
// import core.stdc.stdlib : malloc, free;
28
28
29
- // /
30
- pragma (printf)
31
- extern (C ) int printf(scope const (char )* fmt, scope ... ) @nogc nothrow @trusted ;
32
- // /
33
- extern (C ) void * malloc(size_t size) @nogc nothrow @trusted ;
34
- // /
35
- extern (C ) void free(void * ptr) @nogc nothrow @trusted ;
29
+ version (D_BetterC ) // no DRT
30
+ {
31
+ // / if betterC and LDC, disable moduleinfo and typeinfo
32
+ version (LDC )
33
+ {
34
+ pragma (LDC_no_moduleinfo);
35
+ pragma (LDC_no_typeinfo);
36
+ }
37
+ }
38
+
39
+ // / use --d-version=NuttX_ImportC to define
40
+ version (NuttX_ImportC)
41
+ {
42
+ // / D compiler will not import C++ symbols
43
+ // / so we need to import them manually
44
+ // / @nogc, @trusted, @safe, nothrow - not allowed
45
+ // / https://issues.dlang.org/show_bug.cgi?id=23812
46
+ import nuttx_std : malloc, free, printf; // by default: @system (unsafe)
47
+ }
48
+ else
49
+ {
50
+ // / Correctly FFI-import C/C++ symbols (@safe compatibility)
51
+ // /
52
+ pragma (printf)
53
+ extern (C ) int printf(scope const (char )* fmt, scope ... ) @nogc nothrow @trusted ;
54
+ // /
55
+ extern (C ) void * malloc(size_t size) @nogc nothrow @trusted ;
56
+ // /
57
+ extern (C ) void free(scope void * ptr) @nogc nothrow @trusted ;
58
+ }
36
59
37
60
// ***************************************************************************
38
- // Private module content
61
+ // Private module content (default is public)
39
62
// ***************************************************************************
40
63
private :
41
64
42
65
// based heloxx class layout
43
66
extern (C++ ,class)
44
67
struct DHelloWorld
45
68
{
46
- @nogc nothrow :
47
-
48
69
@disable this ();
49
70
@disable this (this );
50
- this (int secret) @safe pure
51
- {
52
- mSecret = secret;
53
- debug printf(" Constructor\n " );
54
- }
55
71
56
- ~this () @safe pure
72
+ // / use --d-version=NuttX_ImportC to define
73
+ version (NuttX_ImportC)
57
74
{
58
- debug printf(" Destructor\n " );
59
- }
60
-
61
- bool HelloWorld () @safe
62
- {
63
- debug printf(" HelloWorld: mSecret=%d\n " , mSecret);
75
+ this (int secret)
76
+ {
77
+ mSecret = secret;
78
+ printf(" Constructor\n " );
79
+ }
64
80
65
- if (mSecret != 42 )
81
+ ~this ( )
66
82
{
67
- printf(" DHelloWorld.HelloWorld: CONSTRUCTION FAILED!\n " );
68
- return false ;
83
+ printf(" Destructor\n " );
69
84
}
70
- else
85
+
86
+ bool HelloWorld ()
71
87
{
72
- printf(" DHelloWorld.HelloWorld: Hello, World!!\n " );
73
- return true ;
88
+ printf(" HelloWorld: mSecret=%d\n " , mSecret);
89
+
90
+ if (mSecret != 42 )
91
+ {
92
+ printf(" DHelloWorld.HelloWorld: CONSTRUCTION FAILED!\n " );
93
+ return false ;
94
+ }
95
+ else
96
+ {
97
+ printf(" DHelloWorld.HelloWorld: Hello, World!!\n " );
98
+ return true ;
99
+ }
74
100
}
75
101
}
102
+ else
103
+ {
104
+ this (int secret) @safe nothrow @nogc
105
+ {
106
+ mSecret = secret;
107
+ printf(" Constructor\n " );
108
+ }
76
109
77
- private int mSecret;
78
- }
79
-
80
- // ***************************************************************************
81
- // Private Data
82
- // ***************************************************************************
110
+ ~this () @safe nothrow @nogc
111
+ {
112
+ printf(" Destructor\n " );
113
+ }
83
114
84
- // Define a statically constructed DHellowWorld instance if D static
85
- // initializers are supported by the platform
86
- // --d-version=D_Initialize
87
- version (D_Initialize)
88
- {
89
- static DHelloWorld g_HelloWorld;
115
+ bool HelloWorld () @safe nothrow @nogc
116
+ {
117
+ printf(" HelloWorld: mSecret=%d\n " , mSecret);
118
+
119
+ if (mSecret != 42 )
120
+ {
121
+ printf(" DHelloWorld.HelloWorld: CONSTRUCTION FAILED!\n " );
122
+ return false ;
123
+ }
124
+ else
125
+ {
126
+ printf(" DHelloWorld.HelloWorld: Hello, World!!\n " );
127
+ return true ;
128
+ }
129
+ }
130
+ }
131
+ private int mSecret = 0 ;
90
132
}
91
133
92
- // ***************************************************************************
93
- // Public Functions
94
- // ***************************************************************************
95
-
96
134
/* ***************************************************************************
97
135
* Name: hello_d_main
98
136
****************************************************************************/
99
137
// betterC need main function no-mangle.
100
- extern (C )
101
- int hello_d_main (int argc, char * [] argv) nothrow @nogc
138
+ extern (C ) int hello_d_main(int argc, char * [] argv)
102
139
{
103
140
version (LDC )
104
141
{
@@ -118,5 +155,6 @@ int hello_d_main(int argc, char*[] argv) nothrow @nogc
118
155
119
156
printf(" hello_d_main: Saying hello from the instance constructed on the stack\n " );
120
157
HelloWorld.HelloWorld();
158
+
121
159
return 0 ;
122
160
}
0 commit comments