Skip to content

Commit

Permalink
compiler/prelude: format .js files using prettier (#4)
Browse files Browse the repository at this point in the history
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
gopherjs#791 we can see these
differences are entirely and solely attributable to prettier's adjusting
of the .js files.
  • Loading branch information
myitcv committed Jul 2, 2018
1 parent e8b02f4 commit 679d5dd
Show file tree
Hide file tree
Showing 11 changed files with 1,069 additions and 812 deletions.
56 changes: 56 additions & 0 deletions compiler/prelude/formatpreludejs.go
@@ -0,0 +1,56 @@
// +build ignore

package main

import (
"fmt"
"go/build"
"io/ioutil"
"log"
"os/exec"
"path/filepath"
"strings"
)

func main() {
if err := run(); err != nil {
log.Fatalln(err)
}
}

func run() error {
bpkg, err := build.Import("github.com/gopherjs/gopherjs", "", build.FindOnly)
if err != nil {
return fmt.Errorf("failed to locate path for github.com/gopherjs/gopherjs/compiler/prelude: %v", err)
}

preludeDir := filepath.Join(bpkg.Dir, "compiler", "prelude")

args := []string{
filepath.Join(bpkg.Dir, "node_modules", ".bin", "prettier"),
"--config",
filepath.Join(preludeDir, "prettier_options.json"),
"--write",
}

fis, err := ioutil.ReadDir(preludeDir)
if err != nil {
return fmt.Errorf("failed to list contents of %v: %v", preludeDir, err)
}
for _, fi := range fis {
fn := fi.Name()
if !strings.HasSuffix(fn, ".js") || strings.HasSuffix(fn, ".min.js") {
continue
}
args = append(args, fn)
}

cmd := exec.Command(args[0], args[1:]...)

out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to run %v: %v\n%s", strings.Join(args, " "), err, string(out))
}

return nil
}
1 change: 1 addition & 0 deletions compiler/prelude/generate.go
@@ -1,3 +1,4 @@
package prelude

//go:generate go run formatpreludejs.go
//go:generate go run genprelude.go
130 changes: 76 additions & 54 deletions compiler/prelude/goroutines.js
Expand Up @@ -7,7 +7,8 @@ var $getStackDepth = function() {
return $stackDepthOffset + err.stack.split("\n").length;
};

var $panicStackDepth = null, $panicValue;
var $panicStackDepth = null,
$panicValue;
var $callDeferred = function(deferred, jsErr, fromPanic) {
if (!fromPanic && deferred !== null && deferred.index >= $curGoroutine.deferStack.length) {
throw jsErr;
Expand Down Expand Up @@ -106,10 +107,20 @@ var $recover = function() {
$panicStackDepth = null;
return $panicValue;
};
var $throw = function(err) { throw err; };
var $throw = function(err) {
throw err;
};

var $noGoroutine = { asleep: false, exit: false, deferStack: [], panicStack: [] };
var $curGoroutine = $noGoroutine, $totalGoroutines = 0, $awakeGoroutines = 0, $checkForDeadlock = true;
var $noGoroutine = {
asleep: false,
exit: false,
deferStack: [],
panicStack: [],
};
var $curGoroutine = $noGoroutine,
$totalGoroutines = 0,
$awakeGoroutines = 0,
$checkForDeadlock = true;
var $mainFinished = false;
var $go = function(fun, args) {
$totalGoroutines++;
Expand All @@ -119,7 +130,9 @@ var $go = function(fun, args) {
$curGoroutine = $goroutine;
var r = fun.apply(undefined, args);
if (r && r.$blk !== undefined) {
fun = function() { return r.$blk(); };
fun = function() {
return r.$blk();
};
args = [];
return;
}
Expand All @@ -130,7 +143,8 @@ var $go = function(fun, args) {
}
} finally {
$curGoroutine = $noGoroutine;
if ($goroutine.exit) { /* also set by runtime.Goexit() */
if ($goroutine.exit) {
/* also set by runtime.Goexit() */
$totalGoroutines--;
$goroutine.asleep = true;
}
Expand Down Expand Up @@ -219,7 +233,7 @@ var $send = function(chan, value) {
if (closedDuringSend) {
$throwRuntimeError("send on closed channel");
}
}
},
};
};
var $recv = function(chan) {
Expand All @@ -236,7 +250,11 @@ var $recv = function(chan) {
}

var thisGoroutine = $curGoroutine;
var f = { $blk: function() { return this.value; } };
var f = {
$blk: function() {
return this.value;
},
};
var queueEntry = function(v) {
f.value = v;
$schedule(thisGoroutine);
Expand Down Expand Up @@ -272,22 +290,22 @@ var $select = function(comms) {
var comm = comms[i];
var chan = comm[0];
switch (comm.length) {
case 0: /* default */
selection = i;
break;
case 1: /* recv */
if (chan.$sendQueue.length !== 0 || chan.$buffer.length !== 0 || chan.$closed) {
ready.push(i);
}
break;
case 2: /* send */
if (chan.$closed) {
$throwRuntimeError("send on closed channel");
}
if (chan.$recvQueue.length !== 0 || chan.$buffer.length < chan.$capacity) {
ready.push(i);
}
break;
case 0 /* default */:
selection = i;
break;
case 1 /* recv */:
if (chan.$sendQueue.length !== 0 || chan.$buffer.length !== 0 || chan.$closed) {
ready.push(i);
}
break;
case 2 /* send */:
if (chan.$closed) {
$throwRuntimeError("send on closed channel");
}
if (chan.$recvQueue.length !== 0 || chan.$buffer.length < chan.$capacity) {
ready.push(i);
}
break;
}
}

Expand All @@ -297,19 +315,23 @@ var $select = function(comms) {
if (selection !== -1) {
var comm = comms[selection];
switch (comm.length) {
case 0: /* default */
return [selection];
case 1: /* recv */
return [selection, $recv(comm[0])];
case 2: /* send */
$send(comm[0], comm[1]);
return [selection];
case 0 /* default */:
return [selection];
case 1 /* recv */:
return [selection, $recv(comm[0])];
case 2 /* send */:
$send(comm[0], comm[1]);
return [selection];
}
}

var entries = [];
var thisGoroutine = $curGoroutine;
var f = { $blk: function() { return this.selection; } };
var f = {
$blk: function() {
return this.selection;
},
};
var removeFromQueues = function() {
for (var i = 0; i < entries.length; i++) {
var entry = entries[i];
Expand All @@ -324,28 +346,28 @@ var $select = function(comms) {
(function(i) {
var comm = comms[i];
switch (comm.length) {
case 1: /* recv */
var queueEntry = function(value) {
f.selection = [i, value];
removeFromQueues();
$schedule(thisGoroutine);
};
entries.push([comm[0].$recvQueue, queueEntry]);
comm[0].$recvQueue.push(queueEntry);
break;
case 2: /* send */
var queueEntry = function() {
if (comm[0].$closed) {
$throwRuntimeError("send on closed channel");
}
f.selection = [i];
removeFromQueues();
$schedule(thisGoroutine);
return comm[1];
};
entries.push([comm[0].$sendQueue, queueEntry]);
comm[0].$sendQueue.push(queueEntry);
break;
case 1 /* recv */:
var queueEntry = function(value) {
f.selection = [i, value];
removeFromQueues();
$schedule(thisGoroutine);
};
entries.push([comm[0].$recvQueue, queueEntry]);
comm[0].$recvQueue.push(queueEntry);
break;
case 2 /* send */:
var queueEntry = function() {
if (comm[0].$closed) {
$throwRuntimeError("send on closed channel");
}
f.selection = [i];
removeFromQueues();
$schedule(thisGoroutine);
return comm[1];
};
entries.push([comm[0].$sendQueue, queueEntry]);
comm[0].$sendQueue.push(queueEntry);
break;
}
})(i);
}
Expand Down

0 comments on commit 679d5dd

Please sign in to comment.