Skip to content

Commit cc3e314

Browse files
committed
Add all relevant changes from previous 2020 bundle API
For reference on the previous work in 2020, refer to #4518
1 parent 891cc36 commit cc3e314

File tree

6 files changed

+846
-118
lines changed

6 files changed

+846
-118
lines changed

CONTRIBUTING.md

+30-22
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,17 @@ This is usually the file you want to create to test certain compile behavior wit
142142
- Verify the output, check in the `jscomp/test/my_file_test.ml` and `jscomp/test/my_file_test.js` to version control. The checked in `.js` file is essential for verifying regressions later on.
143143
- Eventually check in other relevant files changed during the rebuild (depends on your compiler changes).
144144

145-
## Contribute to the BS Playground Bundle
145+
## Contribute to the ReScript Playground Bundle
146146

147147
> Note: These instructions are designed for building the 4.06 based version of ReScript (ReScript v6).
148148
149149
The "Playground bundle" is the BS compiler compiled to JavaScript, including all necessary dependency files (stdlib / belt etc). It is useful for building tools where you want to compile and execute arbitrary Reason / OCaml in the browser.
150150

151-
The ReScript source code is compiled with a tool called [JSOO (js_of_ocaml)](https://ocsigen.org/js_of_ocaml/3.5.1/manual/overview), which uses OCaml bytecode to compile to JavaScript and is part of the bigger OCaml ecosystem. Before we can compile anything, we need to install the required tools (requires [`opam`](https://opam.ocaml.org/doc/Install.html) to be installed):
151+
The ReScript source code is compiled with a tool called [JSOO (js_of_ocaml)](https://ocsigen.org/js_of_ocaml/3.6.0/manual/overview), which uses OCaml bytecode to compile to JavaScript and is part of the bigger OCaml ecosystem.
152+
153+
We actually ship a bytecode version of `js_of_ocaml`, so you actually don't need to install any third party dependencies. This is usually for basic usage if you don't care about the compilation speed or a fast feedback loop (e.g. CI).
154+
155+
For faster compilation times, we recommend to install a proper `jsoo` executable:
152156

153157
```sh
154158
# Create the right switch, if not created yet (first install)
@@ -158,18 +162,25 @@ opam switch create 4.06.1
158162
opam switch 4.06.1
159163
eval `opam config env`
160164

161-
opam install js_of_ocaml.3.5.1
165+
opam install js_of_ocaml.3.6.0
166+
```
167+
168+
Additionally, make sure to adapt the `scripts/repl.js` file to use the `js_of_ocaml` binary instead (otherwise you'll continue using the slower bytecode version):
169+
170+
```diff
171+
- e(`${OCAMLRUN} ${JSOO} compile jsc.byte ${jsooFlag}-o exports.js`);
172+
+ e(`js_of_ocaml compile jsc.byte ${jsooFlag}-o exports.js`);
162173
```
163174

164-
### Build the Bundle
175+
### Building the Bundle
165176

166-
The entry point of the JSOO bundle is located in `jscomp/main/jsoo_main.ml` and the script for running JSOO can be found in `scripts/repl.js`. A full clean build can be done like this:
177+
The entry point of the JSOO bundle is located in `jscomp/main/jsoo_refmt_main.ml` and the script for running JSOO can be found in `scripts/repl.js`. A full clean build can be done like this:
167178

168179
```
169180
# We create a target directory for storing the bundle / stdlib files
170181
mkdir playground && mkdir playground/stdlib
171182
172-
# We build the ReScript source code and also the bytecode for jsoo_main.ml
183+
# We build the ReScript source code and also the bytecode for jsoo_refmt_main.ml
173184
node scripts/ninja.js config && node scripts/ninja.js build
174185
175186
# Now we run the repl.js script pointing to our playground directory (note how it needs to be relative to the repl.js file)
@@ -178,7 +189,7 @@ BS_PLAYGROUND=../playground node scripts/repl.js
178189

179190
_Troubleshooting: if ninja build step failed with `Error: cannot find file '+runtime.js'`, make sure `ocamlfind` is installed with `opam install ocamlfind`._
180191

181-
**You should now find following files:**
192+
After a successful compilation, you will find following files in your project:
182193

183194
- `playground/exports.js` -> This is the ReScript compiler, which binds the ReScript API to the `window` object.
184195
- `playground/stdlib/*.js` -> All the ReScript runtime files.
@@ -188,31 +199,28 @@ You can now use the `exports.js` file either directly by using a `<script src="/
188199
```
189200
$ node
190201
> require("./exports.js");
191-
undefined
192-
> let compile_result = ocaml.compile(`Js.log Sys.ocaml_version`); // You can change the code here
193-
undefined
194-
> eval(compile_result);
202+
> let compiler = rescript_compiler.make()
203+
> let result = compiler.rescript.compile(`Js.log(Sys.ocaml_version)`);
204+
> eval(result);
195205
4.06.2+BS
196-
undefined
197206
```
198207

199208
### Playground JS bundle API
200209

201-
As soon as the bundle is loaded, you will get access to following functions (as seen in [`jsoo_main.ml`](jscomp/main/jsoo_main.ml)):
210+
As soon as the bundle is loaded, you will get access to the functions exposed in [`jsoo_refmt_main.ml`](jscomp/refmt/jsoo_refmt_main.ml). Best way to check out the API is by inspecting a compiler instance it either in node, or in the browser:
202211

203-
- `window.ocaml`:
204-
- `compile(code: string)`: Compiles given code
205-
- `shake_compile(code: string)`: Compiles given code with tree-shaking
206-
- `compile_super_errors(code: string)`: Compiles given code and outputs `super_errors` related messages on `console.error`
207-
- `compile_super_errors_ppx_v2(code: string)`: Compiles given code with the React v2 syntax
208-
- `compile_super_errors_ppx_v3(code: string)`: Compiles given code with the React v3 syntax
209-
- `load_module(cmi_path: string, cmi_content: string, cmj_name: string, cmj_content: string)`: Loads a module into the compiler (see notes on `cmj` / `cmi` below)
212+
```
213+
$ node
214+
require('./exports.js')
215+
216+
> let compiler = rescript_compiler.make()
217+
> console.log(compiler)
218+
```
210219

211-
For each compile every successful operation will return `{js_code: string}`. On compile errors, the returned object will be `{js_error_msg: string}`.
212220

213221
### Working on the Playground JS API
214222

215-
Whenever you are modifying any files in the ReScript compiler, or in the `jsoo_main.ml` file, you'll need to rebuild the source and recreate the JS bundle.
223+
Whenever you are modifying any files in the ReScript compiler, or in the `jsoo_refmt_main.ml` file, you'll need to rebuild the source and recreate the JS bundle.
216224

217225
```sh
218226
node scripts/ninja.js config && node scripts/ninja.js build

jscomp/main/jsoo_common.ml

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,57 @@
1+
(*
2+
* This module extracts most JSOO externals from the upstream jsoo `js.ml` file.
3+
* See: https://github.com/ocsigen/js_of_ocaml/blob/master/lib/js_of_ocaml/js.ml
4+
*)
15
module Js = struct
26
module Unsafe = struct
37
type any
48
external inject : 'a -> any = "%identity"
59
external get : 'a -> 'b -> 'c = "caml_js_get"
610
external set : 'a -> 'b -> 'c -> unit = "caml_js_set"
711
external pure_js_expr : string -> 'a = "caml_pure_js_expr"
12+
external fun_call : 'a -> any array -> 'b = "caml_js_fun_call"
813
let global = pure_js_expr "joo_global_object"
914
type obj
1015
external obj : (string * any) array -> obj = "caml_js_object"
1116
end
1217
type (-'a, +'b) meth_callback
1318
type 'a callback = (unit, 'a) meth_callback
19+
1420
external wrap_callback : ('a -> 'b) -> ('c, 'a -> 'b) meth_callback = "caml_js_wrap_callback"
1521
external wrap_meth_callback : ('a -> 'b) -> ('a, 'b) meth_callback = "caml_js_wrap_meth_callback"
22+
1623
type + 'a t
24+
1725
type js_string
1826
external string : string -> js_string t = "caml_js_from_string"
1927
external to_string : js_string t -> string = "caml_js_to_string"
28+
29+
type 'a js_array
30+
external array : 'a array -> 'a js_array = "caml_js_from_array"
31+
32+
external bool : bool -> bool t = "caml_js_from_bool"
33+
external to_bool : bool t -> bool = "caml_js_to_bool"
34+
35+
type number
36+
external number_of_float : float -> number t = "caml_js_from_float"
37+
external float_of_number : number t -> float = "caml_js_to_float"
38+
2039
external create_file : js_string t -> js_string t -> unit = "caml_create_file"
2140
external to_bytestring : js_string t -> string = "caml_js_to_byte_string"
2241
end
2342

43+
module Sys_js = struct
44+
external set_channel_output' :
45+
out_channel -> (Js.js_string Js.t -> unit) Js.callback -> unit
46+
= "caml_ml_set_channel_output"
47+
48+
let set_channel_flusher (out_channel : out_channel) (f : string -> unit) =
49+
let f' : (Js.js_string Js.t -> unit) Js.callback =
50+
Js.wrap_callback (fun s -> f (Js.to_bytestring s))
51+
in
52+
set_channel_output' out_channel f'
53+
end
54+
2455
let mk_js_error (loc: Location.t) (msg: string) =
2556
let (_file,line,startchar) = Location.get_pos_info loc.Location.loc_start in
2657
let (_file,endline,endchar) = Location.get_pos_info loc.Location.loc_end in
@@ -36,4 +67,4 @@ let mk_js_error (loc: Location.t) (msg: string) =
3667
"type" , inject @@ Js.string "error"
3768
|]
3869
)
39-
70+

jscomp/main/jsoo_common.mli

+25
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,50 @@ module Js :
1010
external get : 'a -> 'b -> 'c = "caml_js_get"
1111
external set : 'a -> 'b -> 'c -> unit = "caml_js_set"
1212
external pure_js_expr : string -> 'a = "caml_pure_js_expr"
13+
external fun_call : 'a -> any array -> 'b = "caml_js_fun_call"
1314
val global : 'a
1415
type obj
1516
external obj : (string * any) array -> obj = "caml_js_object"
1617
end
18+
1719
type (-'a, +'b) meth_callback
1820
type 'a callback = (unit, 'a) meth_callback
21+
1922
external wrap_callback : ('a -> 'b) -> ('c, 'a -> 'b) meth_callback
2023
= "caml_js_wrap_callback"
2124
external wrap_meth_callback : ('a -> 'b) -> ('a, 'b) meth_callback
2225
= "caml_js_wrap_meth_callback"
26+
2327
type +'a t
28+
2429
type js_string
2530
external string : string -> js_string t = "caml_js_from_string"
2631
external to_string : js_string t -> string = "caml_js_to_string"
32+
33+
type 'a js_array
34+
external array : 'a array -> 'a js_array = "caml_js_from_array"
35+
36+
external bool : bool -> bool t = "caml_js_from_bool"
37+
external to_bool : bool t -> bool = "caml_js_to_bool"
38+
39+
type number
40+
external number_of_float : float -> number t = "caml_js_from_float"
41+
external float_of_number : number t -> float = "caml_js_to_float"
42+
2743
external create_file : js_string t -> js_string t -> unit
2844
= "caml_create_file"
2945
external to_bytestring : js_string t -> string = "caml_js_to_byte_string"
46+
3047
end
3148

49+
module Sys_js :
50+
sig
51+
val set_channel_flusher : out_channel -> (string -> unit) -> unit
52+
(** Set a callback to be called when an out_channel flush its buffer.
53+
[set_channel_flusher chan cb] install the callback [cb] for [chan] out_channel.
54+
[cb] will be called with the string to flush. *)
55+
end
56+
3257
(*
3358
Creates a Js Error object for given location with and a certain error message
3459
*)

0 commit comments

Comments
 (0)