Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 58e2ee7

Browse filesBrowse files
committed
compiler/prelude: move prelude to separate formatted .js files
**DO NOT MERGE** _Based on gopherjs#784 - this will need a rebase once gopherjs#784 is merged._ See the diff this PR introduces [here](regen_min...myitcv:js_file_prelude) I've been working with the prelude quite a lot recently. One of the really painful things with the current implementation is that the JavaScript for the prelude exists as a `const` string in a `.go` file, which makes properly writing and formatting the Javascript itself very difficult. This PR splits the prelude into separate `.js` files therefore and adds a `formatpreludejs.go` `go generate`-er to format those `.js` files in place.
1 parent 9a83340 commit 58e2ee7
Copy full SHA for 58e2ee7

14 files changed

+1926
-1657
lines changed

‎compiler/prelude/formatpreludejs.go

Copy file name to clipboard
+56Lines 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/genmin.go

Copy file name to clipboardExpand all lines: compiler/prelude/genmin.go
+6-11Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ import (
1414
"github.com/gopherjs/gopherjs/compiler/prelude"
1515
)
1616

17+
const (
18+
fn = "prelude.min.js"
19+
)
20+
1721
func main() {
1822
if err := run(); err != nil {
1923
log.Fatalln(err)
@@ -41,18 +45,9 @@ func run() error {
4145
return fmt.Errorf("failed to run %v: %v\n%s", strings.Join(args, " "), err, string(out))
4246
}
4347

44-
fn := "prelude_min.go"
45-
46-
safeOut := strings.Replace(string(out), "`", "\\0060", -1)
47-
48-
err = ioutil.WriteFile(fn, []byte(`// Code generated by genmin. DO NOT EDIT.
48+
out = append([]byte("// Code generated by genmin. DO NOT EDIT.\n\n"), out...)
4949

50-
package prelude
51-
52-
// Minified is an uglifyjs-minified version of Prelude
53-
const Minified = `+"`"+safeOut+"`\n"), 0644)
54-
55-
if err != nil {
50+
if err := ioutil.WriteFile(fn, out, 0644); err != nil {
5651
return fmt.Errorf("failed to write to %v: %v", fn, err)
5752
}
5853

‎compiler/prelude/goroutines.go renamed to ‎compiler/prelude/goroutines.js

Copy file name to clipboardExpand all lines: compiler/prelude/goroutines.js
+76-58Lines changed: 76 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
package prelude
2-
3-
const goroutines = `
41
var $stackDepthOffset = 0;
52
var $getStackDepth = function() {
63
var err = new Error();
@@ -10,7 +7,8 @@ var $getStackDepth = function() {
107
return $stackDepthOffset + err.stack.split("\n").length;
118
};
129

13-
var $panicStackDepth = null, $panicValue;
10+
var $panicStackDepth = null,
11+
$panicValue;
1412
var $callDeferred = function(deferred, jsErr, fromPanic) {
1513
if (!fromPanic && deferred !== null && deferred.index >= $curGoroutine.deferStack.length) {
1614
throw jsErr;
@@ -109,10 +107,20 @@ var $recover = function() {
109107
$panicStackDepth = null;
110108
return $panicValue;
111109
};
112-
var $throw = function(err) { throw err; };
110+
var $throw = function(err) {
111+
throw err;
112+
};
113113

114-
var $noGoroutine = { asleep: false, exit: false, deferStack: [], panicStack: [] };
115-
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;
116124
var $mainFinished = false;
117125
var $go = function(fun, args) {
118126
$totalGoroutines++;
@@ -122,7 +130,9 @@ var $go = function(fun, args) {
122130
$curGoroutine = $goroutine;
123131
var r = fun.apply(undefined, args);
124132
if (r && r.$blk !== undefined) {
125-
fun = function() { return r.$blk(); };
133+
fun = function() {
134+
return r.$blk();
135+
};
126136
args = [];
127137
return;
128138
}
@@ -133,7 +143,8 @@ var $go = function(fun, args) {
133143
}
134144
} finally {
135145
$curGoroutine = $noGoroutine;
136-
if ($goroutine.exit) { /* also set by runtime.Goexit() */
146+
if ($goroutine.exit) {
147+
/* also set by runtime.Goexit() */
137148
$totalGoroutines--;
138149
$goroutine.asleep = true;
139150
}
@@ -222,7 +233,7 @@ var $send = function(chan, value) {
222233
if (closedDuringSend) {
223234
$throwRuntimeError("send on closed channel");
224235
}
225-
}
236+
},
226237
};
227238
};
228239
var $recv = function(chan) {
@@ -239,7 +250,11 @@ var $recv = function(chan) {
239250
}
240251

241252
var thisGoroutine = $curGoroutine;
242-
var f = { $blk: function() { return this.value; } };
253+
var f = {
254+
$blk: function() {
255+
return this.value;
256+
},
257+
};
243258
var queueEntry = function(v) {
244259
f.value = v;
245260
$schedule(thisGoroutine);
@@ -275,22 +290,22 @@ var $select = function(comms) {
275290
var comm = comms[i];
276291
var chan = comm[0];
277292
switch (comm.length) {
278-
case 0: /* default */
279-
selection = i;
280-
break;
281-
case 1: /* recv */
282-
if (chan.$sendQueue.length !== 0 || chan.$buffer.length !== 0 || chan.$closed) {
283-
ready.push(i);
284-
}
285-
break;
286-
case 2: /* send */
287-
if (chan.$closed) {
288-
$throwRuntimeError("send on closed channel");
289-
}
290-
if (chan.$recvQueue.length !== 0 || chan.$buffer.length < chan.$capacity) {
291-
ready.push(i);
292-
}
293-
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;
294309
}
295310
}
296311

@@ -300,19 +315,23 @@ var $select = function(comms) {
300315
if (selection !== -1) {
301316
var comm = comms[selection];
302317
switch (comm.length) {
303-
case 0: /* default */
304-
return [selection];
305-
case 1: /* recv */
306-
return [selection, $recv(comm[0])];
307-
case 2: /* send */
308-
$send(comm[0], comm[1]);
309-
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];
310325
}
311326
}
312327

313328
var entries = [];
314329
var thisGoroutine = $curGoroutine;
315-
var f = { $blk: function() { return this.selection; } };
330+
var f = {
331+
$blk: function() {
332+
return this.selection;
333+
},
334+
};
316335
var removeFromQueues = function() {
317336
for (var i = 0; i < entries.length; i++) {
318337
var entry = entries[i];
@@ -327,32 +346,31 @@ var $select = function(comms) {
327346
(function(i) {
328347
var comm = comms[i];
329348
switch (comm.length) {
330-
case 1: /* recv */
331-
var queueEntry = function(value) {
332-
f.selection = [i, value];
333-
removeFromQueues();
334-
$schedule(thisGoroutine);
335-
};
336-
entries.push([comm[0].$recvQueue, queueEntry]);
337-
comm[0].$recvQueue.push(queueEntry);
338-
break;
339-
case 2: /* send */
340-
var queueEntry = function() {
341-
if (comm[0].$closed) {
342-
$throwRuntimeError("send on closed channel");
343-
}
344-
f.selection = [i];
345-
removeFromQueues();
346-
$schedule(thisGoroutine);
347-
return comm[1];
348-
};
349-
entries.push([comm[0].$sendQueue, queueEntry]);
350-
comm[0].$sendQueue.push(queueEntry);
351-
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;
352371
}
353372
})(i);
354373
}
355374
$block();
356375
return f;
357376
};
358-
`

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.