Skip to content

Commit c500cfe

Browse files
authored
Merge pull request #3976 from ryyppy/playground-reason-support
Add BS JS Playground reason support
2 parents 843897d + 4f9e376 commit c500cfe

19 files changed

+408407
-31819
lines changed

CONTRIBUTING.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,62 @@ load the data. Right now we don't provide any instructions inside here yet, but
312312
[here's how the official ReasonML playground did
313313
it](https://github.com/reasonml/reasonml.github.io/blob/source/website/setupSomeArtifacts.js#L65).
314314

315+
## Upgrading the Reason version within BuckleScript
316+
317+
Each BuckleScript release is coupled to a specific Reason syntax version, which currently needs to be updated manually once in a while.
318+
319+
It's important that we need to update two specific files:
320+
321+
- `jscomp/main/refmt_api.ml`: Contains the programmatic interface for the refmt syntax converter (responsible for transforming Reason string code to an OCaml AST) -> Only used in the BuckleScript JS Playground
322+
- `lib/4.06.1/refmt_main3.ml`: The refmt binary used within BuckleScript itself. The `3` corresponds to the corresponding major version of refmt -> Used to build the vendored `refmt`, aka. `bsrefmt`
323+
324+
Both files are generated by using the `jscomp/bin/bspack.exe` binary (which is also built automatically when you build the compiler inside this repository) on the refmt parser. In more detail, `bspack.exe` resolves all dependencies of one specific `.ml` input file, puts them in the right order and copies all the source code with the target input file in one huge `.ml` bundle.
325+
326+
So the two files mentioned above, `refmt_api.ml` and `refmt_main3.ml`, are bspacked within the Reason repository and then checked into the BuckleScript repository (we call this `vendoring` or `snapshotting`).
327+
Here are the instructions on building your own Reason snapshots (make sure you to have everything set up for building the playground bundle first, as mentioned above):
328+
329+
```
330+
# Let's go up one level and clone Reason in a sibling directory next to your `bucklescript` repo
331+
cd ..
332+
git clone https://github.com/facebook/reason
333+
334+
cd reason
335+
336+
# You should already have created this switch by now, see playground build setup instructions in "Contributing to the BS Playground Bundle"
337+
opam switch 4.06.1
338+
opam pin add -y reason .
339+
opam pin add -y rtop .
340+
341+
# Let's do the bspacking process for refmt_api.ml and refmt_binary.ml
342+
cd bspacks
343+
344+
# Initial setup of certain dependencies before we can bspack everthing in one file
345+
./downloadSomeDependencies.sh
346+
347+
# bspack and compile the files
348+
BSPACK_EXE=/path/to/bucklescript/jscomp/bin/bspack.exe ./reason_bspack406.sh
349+
```
350+
351+
Now copy the files to bucklescript and do a rebuild to verify the changes:
352+
353+
```
354+
# still in reason/bspacks directory
355+
cp build/refmt_api.ml ../../bucklescript/jscomp/main/refmt_api.ml
356+
cp build/refmt_binary.ml ../../bucklescript/lib/4.06.1/refmt_main3.ml
357+
358+
# Build the whole compiler
359+
node scripts/ninja.js config && node scripts/ninja.js build
360+
361+
# Build the playground
362+
BS_PLAYGROUND=../playground node scripts/repl.js
363+
```
364+
365+
You should now have the newest `refmt` binary for the actual compiler, and for the playground, a new `playground/exports.js` file with the new Reason version included.
366+
367+
**Important:** Always verify that the updated Reason version is in sync in the
368+
`refmt.exe` and the playground bundle. Use `lib/bsrefmt --version` and for the
369+
playground API `window.reason.version` (not final) to get the bundled
370+
version.
315371

316372
## Contributing to the Documentation
317373

jscomp/README.md

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,5 @@
11
Hello! This is the main directory for BuckleScript. `jscomp` is just a name that mirrors OCaml's own `bytecomp` and `asmcomp` (bytecode compilation and native compilation logic respectively). For building it, please see [CONTRIBUTING.md](../CONTRIBUTING.md).
22

3-
Extra info:
4-
5-
## Rebuilding the browser-based playground
6-
7-
For best results, you probably want to complete the full [Setup](../CONTRIBUTING.md#setup) before following the below guidelines.
8-
9-
### Get `js_of_ocaml` from the normal switch
10-
11-
```
12-
opam switch 4.02.3
13-
eval `opam config env`
14-
opam install js_of_ocaml
15-
which js_of_ocaml # symlink this into your $PATH, maybe /usr/local/bin or something
16-
```
17-
18-
### Do everything else from the bucklescript switch
19-
20-
You need to have [bucklescript-playground](https://github.com/BuckleScript/bucklescript-playground) cloned next to the Bucklescript directory for the following to work.
21-
22-
```
23-
opam switch 4.02.3+buckle-master
24-
eval `opam config env`
25-
opam install camlp4 ocp-ocamlres
26-
(node scripts/buildocaml.js)
27-
(cd jscomp && BS_RELEASE_BUILD=true BS_PLAYGROUND=../../bucklescript-playground node repl.js)
28-
```
29-
303
## Sub directories
314

325
### [stdlib](./stdlib)

jscomp/core/js_cmj_datasets.ml

Lines changed: 5 additions & 5 deletions
Large diffs are not rendered by default.

jscomp/main/jsoo_common.ml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module Js = struct
2+
module Unsafe = struct
3+
type any
4+
external inject : 'a -> any = "%identity"
5+
external get : 'a -> 'b -> 'c = "caml_js_get"
6+
external set : 'a -> 'b -> 'c -> unit = "caml_js_set"
7+
external pure_js_expr : string -> 'a = "caml_pure_js_expr"
8+
let global = pure_js_expr "joo_global_object"
9+
type obj
10+
external obj : (string * any) array -> obj = "caml_js_object"
11+
end
12+
type (-'a, +'b) meth_callback
13+
type 'a callback = (unit, 'a) meth_callback
14+
external wrap_callback : ('a -> 'b) -> ('c, 'a -> 'b) meth_callback = "caml_js_wrap_callback"
15+
external wrap_meth_callback : ('a -> 'b) -> ('a, 'b) meth_callback = "caml_js_wrap_meth_callback"
16+
type + 'a t
17+
type js_string
18+
external string : string -> js_string t = "caml_js_from_string"
19+
external to_string : js_string t -> string = "caml_js_to_string"
20+
external create_file : js_string t -> js_string t -> unit = "caml_create_file"
21+
external to_bytestring : js_string t -> string = "caml_js_to_byte_string"
22+
end
23+
24+
let mk_js_error (loc: Location.t) (msg: string) =
25+
let (file,line,startchar) = Location.get_pos_info loc.Location.loc_start in
26+
let (file,endline,endchar) = Location.get_pos_info loc.Location.loc_end in
27+
Js.Unsafe.(obj
28+
[|
29+
"js_error_msg",
30+
inject @@ Js.string (Printf.sprintf "Line %d, %d:\n %s" line startchar msg);
31+
"row" , inject (line - 1);
32+
"column" , inject startchar;
33+
"endRow" , inject (endline - 1);
34+
"endColumn" , inject endchar;
35+
"text" , inject @@ Js.string msg;
36+
"type" , inject @@ Js.string "error"
37+
|]
38+
)
39+

jscomp/main/jsoo_common.mli

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
(**
2+
This module is shared between different JSOO / Playground based modules
3+
*)
4+
module Js :
5+
sig
6+
module Unsafe :
7+
sig
8+
type any
9+
external inject : 'a -> any = "%identity"
10+
external get : 'a -> 'b -> 'c = "caml_js_get"
11+
external set : 'a -> 'b -> 'c -> unit = "caml_js_set"
12+
external pure_js_expr : string -> 'a = "caml_pure_js_expr"
13+
val global : 'a
14+
type obj
15+
external obj : (string * any) array -> obj = "caml_js_object"
16+
end
17+
type (-'a, +'b) meth_callback
18+
type 'a callback = (unit, 'a) meth_callback
19+
external wrap_callback : ('a -> 'b) -> ('c, 'a -> 'b) meth_callback
20+
= "caml_js_wrap_callback"
21+
external wrap_meth_callback : ('a -> 'b) -> ('a, 'b) meth_callback
22+
= "caml_js_wrap_meth_callback"
23+
type +'a t
24+
type js_string
25+
external string : string -> js_string t = "caml_js_from_string"
26+
external to_string : js_string t -> string = "caml_js_to_string"
27+
external create_file : js_string t -> js_string t -> unit
28+
= "caml_create_file"
29+
external to_bytestring : js_string t -> string = "caml_js_to_byte_string"
30+
end
31+
32+
(*
33+
Creates a Js Error object for given location with and a certain error message
34+
*)
35+
val mk_js_error : Location.t -> string -> Js.Unsafe.obj

jscomp/main/jsoo_main.ml

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,7 @@
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

2525
(** *)
26-
module Js = struct
27-
module Unsafe = struct
28-
type any
29-
external inject : 'a -> any = "%identity"
30-
external get : 'a -> 'b -> 'c = "caml_js_get"
31-
external set : 'a -> 'b -> 'c -> unit = "caml_js_set"
32-
external pure_js_expr : string -> 'a = "caml_pure_js_expr"
33-
let global = pure_js_expr "joo_global_object"
34-
type obj
35-
external obj : (string * any) array -> obj = "caml_js_object"
36-
end
37-
type (-'a, +'b) meth_callback
38-
type 'a callback = (unit, 'a) meth_callback
39-
external wrap_callback : ('a -> 'b) -> ('c, 'a -> 'b) meth_callback = "caml_js_wrap_callback"
40-
external wrap_meth_callback : ('a -> 'b) -> ('a, 'b) meth_callback = "caml_js_wrap_meth_callback"
41-
type + 'a t
42-
type js_string
43-
external string : string -> js_string t = "caml_js_from_string"
44-
external to_string : js_string t -> string = "caml_js_to_string"
45-
external create_file : js_string t -> js_string t -> unit = "caml_create_file"
46-
external to_bytestring : js_string t -> string = "caml_js_to_byte_string"
47-
end
48-
26+
module Js = Jsoo_common.Js
4927

5028
(*
5129
Error:
@@ -127,21 +105,7 @@ let implementation ~use_super_errors ?(react_ppx_version=V3) prefix impl str :
127105
begin match error_of_exn e with
128106
| Some error ->
129107
Location.report_error Format.err_formatter error;
130-
let (file,line,startchar) = Location.get_pos_info error.loc.loc_start in
131-
let (file,endline,endchar) = Location.get_pos_info error.loc.loc_end in
132-
Js.Unsafe.(obj
133-
[|
134-
"js_error_msg",
135-
inject @@ Js.string (Printf.sprintf "Line %d, %d:\n %s" line startchar error.msg);
136-
"row" , inject (line - 1);
137-
"column" , inject startchar;
138-
"endRow" , inject (endline - 1);
139-
"endColumn" , inject endchar;
140-
"text" , inject @@ Js.string error.msg;
141-
"type" , inject @@ Js.string "error"
142-
|]
143-
);
144-
108+
Jsoo_common.mk_js_error error.loc error.msg
145109
| None ->
146110
Js.Unsafe.(obj [|
147111
"js_error_msg" , inject @@ Js.string (Printexc.to_string e)

0 commit comments

Comments
 (0)