Skip to content

Commit 376ff32

Browse files
committed
compiler/prelude: format .js files using prettier
This PR introduces automatic formatting of the prelude's now separate .js files. Using prettier has the same argument as using gofmt on .go source code. As part of this we make the go generate step for the prelude include a formatpreludejs.go step. The resulting prelude_min.go file is slightly different now, but post #791 we can see these differences are entirely and solely attributable to prettier's adjusting of the .js files.
1 parent f08bf7f commit 376ff32

11 files changed

+1066
-809
lines changed

compiler/prelude/formatpreludejs.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// +build ignore
2+
3+
package main
4+
5+
import (
6+
"fmt"
7+
"go/build"
8+
"io/ioutil"
9+
"log"
10+
"os/exec"
11+
"path/filepath"
12+
"strings"
13+
)
14+
15+
func main() {
16+
if err := run(); err != nil {
17+
log.Fatalln(err)
18+
}
19+
}
20+
21+
func run() error {
22+
bpkg, err := build.Import("github.com/gopherjs/gopherjs", "", build.FindOnly)
23+
if err != nil {
24+
return fmt.Errorf("failed to locate path for github.com/gopherjs/gopherjs/compiler/prelude: %v", err)
25+
}
26+
27+
preludeDir := filepath.Join(bpkg.Dir, "compiler", "prelude")
28+
29+
args := []string{
30+
filepath.Join(bpkg.Dir, "node_modules", ".bin", "prettier"),
31+
"--config",
32+
filepath.Join(preludeDir, "prettier_options.json"),
33+
"--write",
34+
}
35+
36+
fis, err := ioutil.ReadDir(preludeDir)
37+
if err != nil {
38+
return fmt.Errorf("failed to list contents of %v: %v", preludeDir, err)
39+
}
40+
for _, fi := range fis {
41+
fn := fi.Name()
42+
if !strings.HasSuffix(fn, ".js") || strings.HasSuffix(fn, ".min.js") {
43+
continue
44+
}
45+
args = append(args, fn)
46+
}
47+
48+
cmd := exec.Command(args[0], args[1:]...)
49+
50+
out, err := cmd.CombinedOutput()
51+
if err != nil {
52+
return fmt.Errorf("failed to run %v: %v\n%s", strings.Join(args, " "), err, string(out))
53+
}
54+
55+
return nil
56+
}

compiler/prelude/generate.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
package prelude
22

3+
//go:generate go run formatpreludejs.go
34
//go:generate go run genprelude.go

compiler/prelude/goroutines.js

Lines changed: 76 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ var $getStackDepth = function() {
77
return $stackDepthOffset + err.stack.split("\n").length;
88
};
99

10-
var $panicStackDepth = null, $panicValue;
10+
var $panicStackDepth = null,
11+
$panicValue;
1112
var $callDeferred = function(deferred, jsErr, fromPanic) {
1213
if (!fromPanic && deferred !== null && deferred.index >= $curGoroutine.deferStack.length) {
1314
throw jsErr;
@@ -106,10 +107,20 @@ var $recover = function() {
106107
$panicStackDepth = null;
107108
return $panicValue;
108109
};
109-
var $throw = function(err) { throw err; };
110+
var $throw = function(err) {
111+
throw err;
112+
};
110113

111-
var $noGoroutine = { asleep: false, exit: false, deferStack: [], panicStack: [] };
112-
var $curGoroutine = $noGoroutine, $totalGoroutines = 0, $awakeGoroutines = 0, $checkForDeadlock = true;
114+
var $noGoroutine = {
115+
asleep: false,
116+
exit: false,
117+
deferStack: [],
118+
panicStack: [],
119+
};
120+
var $curGoroutine = $noGoroutine,
121+
$totalGoroutines = 0,
122+
$awakeGoroutines = 0,
123+
$checkForDeadlock = true;
113124
var $mainFinished = false;
114125
var $go = function(fun, args) {
115126
$totalGoroutines++;
@@ -119,7 +130,9 @@ var $go = function(fun, args) {
119130
$curGoroutine = $goroutine;
120131
var r = fun.apply(undefined, args);
121132
if (r && r.$blk !== undefined) {
122-
fun = function() { return r.$blk(); };
133+
fun = function() {
134+
return r.$blk();
135+
};
123136
args = [];
124137
return;
125138
}
@@ -130,7 +143,8 @@ var $go = function(fun, args) {
130143
}
131144
} finally {
132145
$curGoroutine = $noGoroutine;
133-
if ($goroutine.exit) { /* also set by runtime.Goexit() */
146+
if ($goroutine.exit) {
147+
/* also set by runtime.Goexit() */
134148
$totalGoroutines--;
135149
$goroutine.asleep = true;
136150
}
@@ -219,7 +233,7 @@ var $send = function(chan, value) {
219233
if (closedDuringSend) {
220234
$throwRuntimeError("send on closed channel");
221235
}
222-
}
236+
},
223237
};
224238
};
225239
var $recv = function(chan) {
@@ -236,7 +250,11 @@ var $recv = function(chan) {
236250
}
237251

238252
var thisGoroutine = $curGoroutine;
239-
var f = { $blk: function() { return this.value; } };
253+
var f = {
254+
$blk: function() {
255+
return this.value;
256+
},
257+
};
240258
var queueEntry = function(v) {
241259
f.value = v;
242260
$schedule(thisGoroutine);
@@ -272,22 +290,22 @@ var $select = function(comms) {
272290
var comm = comms[i];
273291
var chan = comm[0];
274292
switch (comm.length) {
275-
case 0: /* default */
276-
selection = i;
277-
break;
278-
case 1: /* recv */
279-
if (chan.$sendQueue.length !== 0 || chan.$buffer.length !== 0 || chan.$closed) {
280-
ready.push(i);
281-
}
282-
break;
283-
case 2: /* send */
284-
if (chan.$closed) {
285-
$throwRuntimeError("send on closed channel");
286-
}
287-
if (chan.$recvQueue.length !== 0 || chan.$buffer.length < chan.$capacity) {
288-
ready.push(i);
289-
}
290-
break;
293+
case 0 /* default */:
294+
selection = i;
295+
break;
296+
case 1 /* recv */:
297+
if (chan.$sendQueue.length !== 0 || chan.$buffer.length !== 0 || chan.$closed) {
298+
ready.push(i);
299+
}
300+
break;
301+
case 2 /* send */:
302+
if (chan.$closed) {
303+
$throwRuntimeError("send on closed channel");
304+
}
305+
if (chan.$recvQueue.length !== 0 || chan.$buffer.length < chan.$capacity) {
306+
ready.push(i);
307+
}
308+
break;
291309
}
292310
}
293311

@@ -297,19 +315,23 @@ var $select = function(comms) {
297315
if (selection !== -1) {
298316
var comm = comms[selection];
299317
switch (comm.length) {
300-
case 0: /* default */
301-
return [selection];
302-
case 1: /* recv */
303-
return [selection, $recv(comm[0])];
304-
case 2: /* send */
305-
$send(comm[0], comm[1]);
306-
return [selection];
318+
case 0 /* default */:
319+
return [selection];
320+
case 1 /* recv */:
321+
return [selection, $recv(comm[0])];
322+
case 2 /* send */:
323+
$send(comm[0], comm[1]);
324+
return [selection];
307325
}
308326
}
309327

310328
var entries = [];
311329
var thisGoroutine = $curGoroutine;
312-
var f = { $blk: function() { return this.selection; } };
330+
var f = {
331+
$blk: function() {
332+
return this.selection;
333+
},
334+
};
313335
var removeFromQueues = function() {
314336
for (var i = 0; i < entries.length; i++) {
315337
var entry = entries[i];
@@ -324,28 +346,28 @@ var $select = function(comms) {
324346
(function(i) {
325347
var comm = comms[i];
326348
switch (comm.length) {
327-
case 1: /* recv */
328-
var queueEntry = function(value) {
329-
f.selection = [i, value];
330-
removeFromQueues();
331-
$schedule(thisGoroutine);
332-
};
333-
entries.push([comm[0].$recvQueue, queueEntry]);
334-
comm[0].$recvQueue.push(queueEntry);
335-
break;
336-
case 2: /* send */
337-
var queueEntry = function() {
338-
if (comm[0].$closed) {
339-
$throwRuntimeError("send on closed channel");
340-
}
341-
f.selection = [i];
342-
removeFromQueues();
343-
$schedule(thisGoroutine);
344-
return comm[1];
345-
};
346-
entries.push([comm[0].$sendQueue, queueEntry]);
347-
comm[0].$sendQueue.push(queueEntry);
348-
break;
349+
case 1 /* recv */:
350+
var queueEntry = function(value) {
351+
f.selection = [i, value];
352+
removeFromQueues();
353+
$schedule(thisGoroutine);
354+
};
355+
entries.push([comm[0].$recvQueue, queueEntry]);
356+
comm[0].$recvQueue.push(queueEntry);
357+
break;
358+
case 2 /* send */:
359+
var queueEntry = function() {
360+
if (comm[0].$closed) {
361+
$throwRuntimeError("send on closed channel");
362+
}
363+
f.selection = [i];
364+
removeFromQueues();
365+
$schedule(thisGoroutine);
366+
return comm[1];
367+
};
368+
entries.push([comm[0].$sendQueue, queueEntry]);
369+
comm[0].$sendQueue.push(queueEntry);
370+
break;
349371
}
350372
})(i);
351373
}

0 commit comments

Comments
 (0)