Skip to content

Commit a412cc8

Browse files
authored
Don't wrap RUNNABLE_EXAMPLE_COMPILE code in void main (#3561)
* [js] Don't wrap _COMPILE examples in `void main` Remove unused var `stripedText`. * Use 'Compile' button for _EXAMPLE_COMPILE Also add tooltip for Run explaining code is wrapped in main if missing. * Set button width in en, not px * Update dspec_tester to not wrap main with _COMPILE * Mention std.stdio.write auto imported in Run tooltip * Use -main switch instead of dummy main * [dspec_tester] Only auto-import std.stdio for _RUN * Avoid using local extern vars
1 parent fcbf0d4 commit a412cc8

16 files changed

+267
-182
lines changed

css/style.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1622,7 +1622,7 @@ input.runButton, input.resetButton, input.argsButton, input.inputButton, input.e
16221622
padding:3px 6px;
16231623
text-decoration:none;
16241624
text-shadow:1px 1px 0px #ffffff;
1625-
width: 50px;
1625+
width: 10en;
16261626
margin: 2px;
16271627
position: relative;
16281628
z-index: 2;

dlang.org.ddoc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,12 +398,16 @@ _=
398398
RELATIVE_LINK2=$(ALOCAL $1, $+)
399399
_=
400400

401+
_=These take a ddoc example enclosed in `---`
401402
RUNNABLE_EXAMPLE=<div class="runnable-examples">
402403
$1
403404
</div>
405+
RUNNABLE_EXAMPLE_COMPILE=<div class="runnable-examples" data-compile=''>
406+
$1
407+
</div>
408+
_=These go inside a RUNNABLE_EXAMPLE macro invocation before the first `---`
404409
RUNNABLE_EXAMPLE_STDIN=<code class="runnable-examples-stdin">$0</code>
405410
RUNNABLE_EXAMPLE_ARGS=<code class="runnable-examples-args">$0</code>
406-
RUNNABLE_EXAMPLE_COMPILE=$(RUNNABLE_EXAMPLE $0)
407411

408412
SAMPLESRC=$(SPANC sample_src, $(AHTTPS github.com/dlang/dmd/blob/master/samples/$0, /dmd/samples/d/$0))
409413
SCINI=$(TC pre, scini notranslate, $0)

js/run.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ void main(string[] args) {
3838
------
3939
4040
TL;DR
41-
All examples are replaced with custom form by default. You need to do additional work only if you wan't
42-
your example to have deafault standard input or default standard arguments.
41+
All examples are replaced with custom form by default. You need to do additional work only if you want
42+
your example to have default standard input or default standard arguments.
4343
*/
4444

4545
var nl2br = function()
@@ -102,7 +102,7 @@ var backends = {
102102
var req = {
103103
source: data.code,
104104
// always execute unittests and main for backwards compatibility with examples
105-
args: "-unittest",
105+
args: "-unittest -main",
106106
runtimeArgs: "--DRT-testmode=run-main",
107107
compiler: dmdCompilerBranch
108108
}
@@ -179,11 +179,11 @@ function parseOutput(res, o, oTitle)
179179
}
180180

181181
// wraps a unittest into a runnable script
182-
function wrapIntoMain(code) {
182+
function wrapIntoMain(code, compile) {
183183
var currentPackage = $('body')[0].id;
184184

185185
// dynamically wrap into main if needed
186-
if (code.indexOf("void main") >= 0 || code.indexOf("int main") >= 0) {
186+
if (compile || code.indexOf("void main") >= 0 || code.indexOf("int main") >= 0) {
187187
return code;
188188
}
189189
else {
@@ -207,7 +207,6 @@ $(document).ready(function()
207207
{
208208
var root = $(this);
209209
var el = root.children("pre");
210-
var stripedText = el.text().replace(/\s/gm,'');
211210

212211
var stdin = root.children(".runnable-examples-stdin").text();
213212
var args = root.children(".runnable-examples-args").text();
@@ -224,6 +223,11 @@ $(document).ready(function()
224223
+ '<textarea class="d_code_args">'+args+'</textarea></div>';
225224
}
226225

226+
var compile = el.parent()[0].hasAttribute('data-compile');
227+
var runAttrs = `value="${compile ? 'Compile' : 'Run'}"`;
228+
if (!compile)
229+
runAttrs += ' title="Note: Wraps code in `main` automatically if `main` is missing'
230+
+ ' & imports std.stdio.write[f][ln]"';
227231
var currentExample = el;
228232
var orig = currentExample.html();
229233

@@ -236,7 +240,7 @@ $(document).ready(function()
236240
+ '<input type="button" class="editButton" value="Edit">'
237241
+ (args.length > 0 ? '<input type="button" class="argsButton" value="Args">' : '')
238242
+ (stdin.length > 0 ? '<input type="button" class="inputButton" value="Input">' : '')
239-
+ '<input type="button" class="runButton" value="Run">'
243+
+ `<input type="button" class="runButton" ${runAttrs}>`
240244
+ '<input type="button" class="resetButton" value="Reset">'
241245
+ '<input type="button" class="openInEditorButton" value="Open in IDE"></div>'
242246
);
@@ -247,11 +251,13 @@ $(document).ready(function()
247251
var outputDiv = parent.children("div.d_code_output");
248252
var hasStdin = parent.children(".inputButton").length > 0;
249253
var hasArgs = parent.children(".argsButton").length > 0;
254+
var compile = parent.parent()[0].hasAttribute('data-compile');
250255
setupTextarea(this, {
251256
parent: parent,
252257
outputDiv: outputDiv,
253258
stdin: hasStdin,
254259
args: hasArgs,
260+
compile: compile,
255261
defaultOutput: "Succeed without output.",
256262
transformOutput: wrapIntoMain,
257263
});
@@ -421,7 +427,7 @@ function setupTextarea(el, opts)
421427
output.focus();
422428

423429
var data = {
424-
code: opts.transformOutput(editor.getValue())
430+
code: opts.transformOutput(editor.getValue(), opts.compile)
425431
};
426432
if (opts.stdin) {
427433
data.stdin = stdin.val();

spec/arrays.dd

Lines changed: 82 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -237,21 +237,16 @@ $(H2 $(LNAME2 slicing, Slicing))
237237
For example:
238238
)
239239

240-
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
240+
$(SPEC_RUNNABLE_EXAMPLE_RUN
241241
---------
242-
void foo(int value)
243-
{
244-
writeln("value=", value);
245-
}
246-
247242
int[10] a; // declare array of 10 ints
248243
int[] b;
249244

250245
b = a[1..3]; // a[1..3] is a 2 element array consisting of
251246
// a[1] and a[2]
252-
foo(b[1]); // equivalent to foo(0)
247+
assert(b[1] == 0);
253248
a[2] = 3;
254-
foo(b[1]); // equivalent to foo(3)
249+
assert(b[1] == 3);
255250
---------
256251
)
257252

@@ -348,10 +343,13 @@ $(H3 $(LNAME2 overlapping-copying, Overlapping Copying))
348343

349344
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
350345
---------
351-
int[3] s;
346+
void main()
347+
{
348+
int[3] s;
352349

353-
s[0..2] = s[1..3]; // error, overlapping copy
354-
s[1..3] = s[0..2]; // error, overlapping copy
350+
s[0..2] = s[1..3]; // error, overlapping copy
351+
s[1..3] = s[0..2]; // error, overlapping copy
352+
}
355353
---------
356354
)
357355

@@ -542,8 +540,14 @@ double[][] matrix;
542540

543541
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
544542
---------
543+
import std.stdio : writeln;
544+
545545
double[6][3] matrix = 0; // Sets all elements to 0.
546-
writeln(matrix); // [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]
546+
547+
void main()
548+
{
549+
writeln(matrix); // [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]
550+
}
547551
---------
548552
)
549553

@@ -554,11 +558,16 @@ writeln(matrix); // [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]
554558

555559
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
556560
---------
557-
double[6][3] matrix = 0;
558-
matrix[2][5] = 3.14; // Assignment to bottom right element.
559-
writeln(matrix); // [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 3.14]]
561+
import std.stdio : writeln;
562+
563+
void main()
564+
{
565+
double[6][3] matrix = 0;
566+
matrix[2][5] = 3.14; // Assignment to bottom right element.
567+
writeln(matrix); // [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 3.14]]
560568

561-
static assert(!__traits(compiles, matrix[5][2])); // Array index out of bounds.
569+
static assert(!__traits(compiles, matrix[5][2])); // Array index out of bounds.
570+
}
562571
---------
563572
)
564573

@@ -714,15 +723,18 @@ a[15] = 'z'; // does not affect c, because either a or c has reallocated.
714723

715724
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
716725
---------
717-
int[] array;
718-
while (1)
726+
void fun()
719727
{
720-
import core.stdc.stdio : getchar;
721-
auto c = getchar;
722-
if (!c)
723-
break;
724-
++array.length;
725-
array[array.length - 1] = c;
728+
int[] array;
729+
while (1)
730+
{
731+
import core.stdc.stdio : getchar;
732+
auto c = getchar;
733+
if (!c)
734+
break;
735+
++array.length;
736+
array[array.length - 1] = c;
737+
}
726738
}
727739
---------
728740
)
@@ -732,20 +744,23 @@ while (1)
732744

733745
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
734746
---------
735-
int[] array;
736-
array.length = 100; // guess
737-
int i;
738-
for (i = 0; ; i++)
747+
void fun()
739748
{
740-
import core.stdc.stdio : getchar;
741-
auto c = getchar;
742-
if (!c)
743-
break;
744-
if (i == array.length)
745-
array.length *= 2;
746-
array[i] = c;
749+
int[] array;
750+
array.length = 100; // guess
751+
int i;
752+
for (i = 0; ; i++)
753+
{
754+
import core.stdc.stdio : getchar;
755+
auto c = getchar;
756+
if (!c)
757+
break;
758+
if (i == array.length)
759+
array.length *= 2;
760+
array[i] = c;
761+
}
762+
array.length = i;
747763
}
748-
array.length = i;
749764
---------
750765
)
751766

@@ -853,18 +868,21 @@ $(H2 $(LNAME2 bounds, Array Bounds Checking))
853868

854869
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
855870
---------
856-
import core.exception;
857-
try
871+
void main()
858872
{
859-
auto array = [1, 2];
860-
for (auto i = 0; ; i++)
873+
import core.exception;
874+
try
861875
{
862-
array[i] = 5;
876+
auto array = [1, 2];
877+
for (auto i = 0; ; i++)
878+
{
879+
array[i] = 5;
880+
}
881+
}
882+
catch (ArrayIndexError)
883+
{
884+
// terminate loop
863885
}
864-
}
865-
catch (ArrayIndexError)
866-
{
867-
// terminate loop
868886
}
869887
---------
870888
)
@@ -873,10 +891,13 @@ catch (ArrayIndexError)
873891

874892
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
875893
---------
876-
auto array = [1, 2];
877-
for (auto i = 0; i < array.length; i++)
894+
void main()
878895
{
879-
array[i] = 5;
896+
auto array = [1, 2];
897+
for (auto i = 0; i < array.length; i++)
898+
{
899+
array[i] = 5;
900+
}
880901
}
881902
---------
882903
)
@@ -1118,16 +1139,19 @@ cast(immutable(wchar) [])"abc" // this is an array of wchar characters
11181139

11191140
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
11201141
---------
1121-
char c;
1122-
wchar w;
1123-
dchar d;
1124-
1125-
c = 'b'; // c is assigned the character 'b'
1126-
w = 'b'; // w is assigned the wchar character 'b'
1127-
//w = 'bc'; // error - only one wchar character at a time
1128-
w = "b"[0]; // w is assigned the wchar character 'b'
1129-
w = "\r"[0]; // w is assigned the carriage return wchar character
1130-
d = 'd'; // d is assigned the character 'd'
1142+
void fun()
1143+
{
1144+
char c;
1145+
wchar w;
1146+
dchar d;
1147+
1148+
c = 'b'; // c is assigned the character 'b'
1149+
w = 'b'; // w is assigned the wchar character 'b'
1150+
//w = 'bc'; // error - only one wchar character at a time
1151+
w = "b"[0]; // w is assigned the wchar character 'b'
1152+
w = "\r"[0]; // w is assigned the carriage return wchar character
1153+
d = 'd'; // d is assigned the character 'd'
1154+
}
11311155
---------
11321156
)
11331157

spec/attribute.dd

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ $(H2 $(LNAME2 static, $(D static) Attribute))
643643
other words, it means there is no $(D this) reference.
644644
)
645645

646-
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
646+
$(SPEC_RUNNABLE_EXAMPLE_RUN
647647
---------------
648648
class Foo
649649
{
@@ -1072,8 +1072,6 @@ alias TP = __traits(getAttributes, foo);
10721072

10731073
pragma(msg, TP); // prints tuple(3, 4, 7, (SSS))
10741074
pragma(msg, TP[2]); // prints 7
1075-
1076-
void main() {}
10771075
---
10781076
)
10791077

@@ -1142,8 +1140,6 @@ pragma(msg, __traits(getAttributes, Outer!int.Inner));
11421140
// prints tuple("foo", "bar")
11431141
pragma(msg, __traits(getAttributes, Outer!int.Inner!int.z));
11441142
// prints tuple("foo", "bar")
1145-
1146-
void main() {}
11471143
---
11481144
)
11491145

spec/const3.dd

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -571,8 +571,12 @@ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
571571
import core.atomic;
572572

573573
shared int x;
574-
//x++; // error, use atomicOp instead
575-
x.atomicOp!"+="(1);
574+
575+
void fun()
576+
{
577+
//x++; // error, use atomicOp instead
578+
x.atomicOp!"+="(1);
579+
}
576580
---
577581
)
578582
$(P $(RED Warning:) An individual read or write operation on shared
@@ -607,10 +611,13 @@ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
607611
struct T;
608612
shared T* x;
609613

610-
synchronized
614+
void fun()
611615
{
612-
T* p = cast(T*)x;
613-
// operate on `*p`
616+
synchronized
617+
{
618+
T* p = cast(T*)x;
619+
// operate on `*p`
620+
}
614621
}
615622
---
616623
)

0 commit comments

Comments
 (0)