Skip to content

Commit e8b02f4

Browse files
authored
compiler/prelude: move prelude to separate .js files (#3)
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 to make editing the JavaScript prelude more straightforward. genmin.go becomes genprelude.go where these .js files are catted together into a single prelude.go file. The minified prelude_min.go is as before. Automated formatting of these .js files will follow in a later PR.
1 parent 51d17b2 commit e8b02f4

File tree

12 files changed

+526
-499
lines changed

12 files changed

+526
-499
lines changed

.circleci/config.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ jobs:
5151
npm install --global node-gyp
5252
cd node-syscall && node-gyp rebuild && mkdir -p $HOME/.node_libraries/ && cp build/Release/syscall.node $HOME/.node_libraries/syscall.node
5353
54+
# this gives us the repo-local binaries we require
55+
npm install
56+
5457
- run: |
5558
go get -t ./...
5659
go install github.com/gopherjs/gopherjs
@@ -59,6 +62,12 @@ jobs:
5962
# because this is a long-running test suite
6063
no_output_timeout: 30m
6164
command: |
65+
echo ">> Regenerate compiler/prelude"
66+
go generate github.com/gopherjs/gopherjs/compiler/prelude
67+
68+
echo ">> Ensure there are no variations from the git commit"
69+
diff -u <(echo -n) <(git status --porcelain)
70+
6271
echo ">> Ensure all go code is well formatted."
6372
diff -u <(echo -n) <(gofmt -d .)
6473

compiler/prelude/generate.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package prelude
2+
3+
//go:generate go run genprelude.go

compiler/prelude/genmin.go

Lines changed: 0 additions & 63 deletions
This file was deleted.

compiler/prelude/genprelude.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// +build ignore
2+
3+
package main
4+
5+
import (
6+
"bytes"
7+
"fmt"
8+
"go/build"
9+
"io/ioutil"
10+
"log"
11+
"os/exec"
12+
"path/filepath"
13+
"strings"
14+
)
15+
16+
const (
17+
fn = "prelude.go"
18+
minFn = "prelude_min.go"
19+
)
20+
21+
func main() {
22+
if err := run(); err != nil {
23+
log.Fatalln(err)
24+
}
25+
}
26+
27+
func run() error {
28+
bpkg, err := build.Import("github.com/gopherjs/gopherjs", "", build.FindOnly)
29+
if err != nil {
30+
return fmt.Errorf("failed to locate path for github.com/gopherjs/gopherjs: %v", err)
31+
}
32+
33+
preludeDir := filepath.Join(bpkg.Dir, "compiler", "prelude")
34+
35+
files := []string{
36+
"prelude.js",
37+
"numeric.js",
38+
"types.js",
39+
"goroutines.js",
40+
"jsmapping.js",
41+
}
42+
43+
prelude := new(bytes.Buffer)
44+
45+
for _, f := range files {
46+
p := filepath.Join(preludeDir, f)
47+
out, err := ioutil.ReadFile(p)
48+
if err != nil {
49+
return fmt.Errorf("failed to read from %v: %v", p, err)
50+
}
51+
if _, err := prelude.Write(out); err != nil {
52+
return fmt.Errorf("failed to append prelude: %v", err)
53+
}
54+
}
55+
56+
args := append([]string{
57+
filepath.Join(bpkg.Dir, "node_modules", ".bin", "uglifyjs"),
58+
"--config-file",
59+
filepath.Join(preludeDir, "uglifyjs_options.json"),
60+
}, files...)
61+
62+
stderr := new(bytes.Buffer)
63+
64+
cmd := exec.Command(args[0], args[1:]...)
65+
cmd.Stderr = stderr
66+
67+
minout, err := cmd.Output()
68+
if err != nil {
69+
return fmt.Errorf("failed to run %v: %v\n%s", strings.Join(args, " "), err, stderr.Bytes())
70+
}
71+
72+
err = writeOutput("prelude.go", ""+
73+
"// Prelude is the GopherJS JavaScript interop layer.\n"+
74+
"const Prelude = %q", prelude.Bytes())
75+
76+
if err != nil {
77+
return err
78+
}
79+
80+
err = writeOutput("prelude_min.go", ""+
81+
"// Minified is an uglifyjs-minified version of Prelude.\n"+
82+
"const Minified = %q", minout)
83+
84+
return err
85+
}
86+
87+
func writeOutput(fn string, format string, byts []byte) error {
88+
content := fmt.Sprintf("// Code generated by genprelude; DO NOT EDIT.\n\n"+
89+
"package prelude\n\n"+format+"\n", byts)
90+
91+
if err := ioutil.WriteFile(fn, []byte(content), 0644); err != nil {
92+
return fmt.Errorf("failed to write to %v: %v", fn, err)
93+
}
94+
95+
return nil
96+
}

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

Lines changed: 0 additions & 4 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();
@@ -355,4 +352,3 @@ var $select = function(comms) {
355352
$block();
356353
return f;
357354
};
358-
`

compiler/prelude/jsmapping.go renamed to compiler/prelude/jsmapping.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
package prelude
2-
3-
const jsmapping = `
41
var $jsObjectPtr, $jsErrorPtr;
52

63
var $needsExternalization = function(t) {
@@ -376,4 +373,3 @@ var $isASCII = function(s) {
376373
}
377374
return true;
378375
};
379-
`

compiler/prelude/jspmapping.js

Whitespace-only changes.

compiler/prelude/numeric.go renamed to compiler/prelude/numeric.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
package prelude
2-
3-
const numeric = `
41
var $min = Math.min;
52
var $mod = function(x, y) { return x % y; };
63
var $parseInt = parseInt;
@@ -193,4 +190,3 @@ var $divComplex = function(n, d) {
193190
var denom = d.$imag * ratio + d.$real;
194191
return new n.constructor((n.$imag * ratio + n.$real) / denom, (n.$imag - n.$real * ratio) / denom);
195192
};
196-
`

0 commit comments

Comments
 (0)