Skip to content

Commit b4fcf89

Browse files
committed
drop primitive polyfill syntax
1 parent 0036228 commit b4fcf89

24 files changed

+62
-1116
lines changed

jscomp/common/bs_warnings.ml

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
2-
*
2+
*
33
* This program is free software: you can redistribute it and/or modify
44
* it under the terms of the GNU Lesser General Public License as published by
55
* the Free Software Foundation, either version 3 of the License, or
@@ -17,14 +17,11 @@
1717
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1818
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1919
* GNU Lesser General Public License for more details.
20-
*
20+
*
2121
* You should have received a copy of the GNU Lesser General Public License
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

25-
let warn_missing_primitive loc txt =
26-
Location.prerr_warning loc (Bs_unimplemented_primitive txt)
27-
2825
let warn_literal_overflow loc =
2926
Location.prerr_warning loc Bs_integer_literal_overflow
3027

jscomp/common/bs_warnings.mli

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
2-
*
2+
*
33
* This program is free software: you can redistribute it and/or modify
44
* it under the terms of the GNU Lesser General Public License as published by
55
* the Free Software Foundation, either version 3 of the License, or
@@ -17,13 +17,11 @@
1717
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1818
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1919
* GNU Lesser General Public License for more details.
20-
*
20+
*
2121
* You should have received a copy of the GNU Lesser General Public License
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

25-
val warn_missing_primitive : Location.t -> string -> unit
26-
2725
val warn_literal_overflow : Location.t -> unit
2826

2927
val error_unescaped_delimiter : Location.t -> string -> unit

jscomp/core/js_exp_make.ml

+11-20
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1818
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1919
* GNU Lesser General Public License for more details.
20-
*
20+
*
2121
* You should have received a copy of the GNU Lesser General Public License
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
@@ -29,7 +29,7 @@ type t = J.expression
2929
(*
3030
[remove_pure_sub_exp x]
3131
Remove pure part of the expression (minor optimization)
32-
and keep the non-pure part while preserve the semantics
32+
and keep the non-pure part while preserve the semantics
3333
(modulo return value)
3434
It will return None if [x] is pure
3535
*)
@@ -86,8 +86,8 @@ let runtime_var_dot ?comment (x : string) (e1 : string) : J.expression =
8686
let ml_var_dot ?comment ?(dynamic_import = false) (id : Ident.t) e : J.expression =
8787
{ expression_desc = Var (Qualified ({ id; kind = Ml; dynamic_import }, Some e)); comment }
8888

89-
(**
90-
module as a value
89+
(**
90+
module as a value
9191
{[
9292
var http = require("http")
9393
]}
@@ -182,7 +182,7 @@ let instanceof ?comment (e0 : t) (e1: t) : t =
182182
let is_array (e0 : t) : t =
183183
let f = str "Array.isArray" ~delim:DNoQuotes in
184184
{ expression_desc = Call (f, [e0], Js_call_info.ml_full_call); comment=None }
185-
185+
186186
let new_ ?comment e0 args : t =
187187
{ expression_desc = New (e0, Some args); comment }
188188

@@ -455,7 +455,7 @@ let assign_by_exp (e : t) index value : t =
455455
| Array _
456456
(*
457457
Temporary block -- address not held
458-
Optimize cases like this which is really
458+
Optimize cases like this which is really
459459
rare {[
460460
(ref x) := 3
461461
]}
@@ -474,7 +474,7 @@ let record_assign (e : t) (pos : int32) (name : string) (value : t) =
474474
| Array _
475475
(*
476476
Temporary block -- address not held
477-
Optimize cases like this which is really
477+
Optimize cases like this which is really
478478
rare {[
479479
(ref x) := 3
480480
]}
@@ -492,7 +492,7 @@ let extension_assign (e : t) (pos : int32) name (value : t) =
492492
| Array _
493493
(*
494494
Temporary block -- address not held
495-
Optimize cases like this which is really
495+
Optimize cases like this which is really
496496
rare {[
497497
(ref x) := 3
498498
]}
@@ -809,7 +809,7 @@ let tag_type = function
809809
| Ast_untagged_variants.String s -> str s ~delim:DStarJ
810810
| Int i -> small_int i
811811
| Float f -> float f
812-
| BigInt i ->
812+
| BigInt i ->
813813
let sign, i = Bigint_utils.parse_bigint i in
814814
bigint sign i
815815
| Bool b -> bool b
@@ -841,7 +841,7 @@ let rec emit_check (check : t Ast_untagged_variants.DynamicChecks.t) = match che
841841
| IsInstanceOf (Array, x) -> is_array (emit_check x)
842842
| IsInstanceOf (instance, x) ->
843843
let instance_name = Ast_untagged_variants.Instance.to_string instance in
844-
instanceof (emit_check x) (str instance_name ~delim:DNoQuotes)
844+
instanceof (emit_check x) (str instance_name ~delim:DNoQuotes)
845845
| Not x -> not (emit_check x)
846846
| Expr x -> x
847847

@@ -1273,7 +1273,7 @@ let bigint_div ~checked ?comment (e0: t) (e1: t) =
12731273
runtime_call Js_runtime_modules.bigint "div" [e0; e1]
12741274
else
12751275
bigint_op ?comment Div e0 e1
1276-
1276+
12771277
let bigint_mod ~checked ?comment (e0: t) (e1: t) =
12781278
if checked then
12791279
runtime_call Js_runtime_modules.bigint "mod_" [e0; e1]
@@ -1344,14 +1344,5 @@ let neq_null_undefined_boolean ?comment (a : t) (b : t) =
13441344
| Null, Undefined _ | Undefined _, Null -> true_
13451345
| _ -> { expression_desc = Bin (NotEqEq, a, b); comment }
13461346

1347-
(** TODO: in the future add a flag
1348-
to set globalThis
1349-
*)
1350-
let resolve_and_apply (s : string) (args : t list) : t =
1351-
call ~info:Js_call_info.builtin_runtime_call
1352-
(runtime_call Js_runtime_modules.external_polyfill "resolve"
1353-
[ str (if s.[0] = '?' then String.sub s 1 (String.length s - 1) else s) ])
1354-
args
1355-
13561347
let make_exception (s : string) =
13571348
pure_runtime_call Js_runtime_modules.exceptions Literals.create [ str s ]

jscomp/core/js_exp_make.mli

+10-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(* Copyright (C) 2015 - 2016 Bloomberg Finance L.P.
2-
* Copyright (C) 2017 - Hongbo Zhang, Authors of ReScript
2+
* Copyright (C) 2017 - Hongbo Zhang, Authors of ReScript
33
* This program is free software: you can redistribute it and/or modify
44
* it under the terms of the GNU Lesser General Public License as published by
55
* the Free Software Foundation, either version 3 of the License, or
@@ -17,17 +17,17 @@
1717
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1818
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1919
* GNU Lesser General Public License for more details.
20-
*
20+
*
2121
* You should have received a copy of the GNU Lesser General Public License
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

2525
(** Creator utilities for the [J] module *)
2626

2727
type t = J.expression
28-
(** check if a javascript ast is constant
28+
(** check if a javascript ast is constant
2929
30-
The better signature might be
30+
The better signature might be
3131
{[
3232
J.expresssion -> Js_output.t
3333
]}
@@ -146,7 +146,7 @@ val function_length : ?comment:string -> t -> t
146146
val string_append : ?comment:string -> t -> t -> t
147147
(**
148148
When in ES6 mode, we can use Symbol to guarantee its uniquess,
149-
we can not tag [js] object, since it can be frozen
149+
we can not tag [js] object, since it can be frozen
150150
*)
151151

152152
(* val var_dot : ?comment:string -> Ident.t -> string -> t *)
@@ -181,11 +181,11 @@ val poly_var_value_access : t -> t
181181
val extension_assign : t -> int32 -> string -> t -> t
182182

183183
val assign_by_int : ?comment:string -> t -> int32 -> t -> t
184-
(**
184+
(**
185185
[assign_by_int e i v]
186-
if the expression [e] is a temporay block
186+
if the expression [e] is a temporay block
187187
which has no side effect,
188-
write to it does not really make sense,
188+
write to it does not really make sense,
189189
optimize it away *)
190190

191191
val assign_by_exp : t -> t -> t -> t
@@ -332,9 +332,9 @@ val undefined : t
332332

333333
val tag : ?comment:string -> ?name:string -> J.expression -> t
334334

335-
(** Note that this is coupled with how we encode block, if we use the
335+
(** Note that this is coupled with how we encode block, if we use the
336336
`Object.defineProperty(..)` since the array already hold the length,
337-
this should be a nop
337+
this should be a nop
338338
*)
339339

340340
val obj_length : ?comment:string -> J.expression -> t
@@ -363,6 +363,4 @@ val for_sure_js_null_undefined : J.expression -> bool
363363

364364
val is_null_undefined : ?comment:string -> t -> t
365365

366-
val resolve_and_apply : string -> t list -> t
367-
368366
val make_exception : string -> t

jscomp/core/lam_compile_primitive.ml

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(* Copyright (C) 2015 - 2016 Bloomberg Finance L.P.
2-
* Copyright (C) 2017 - Hongbo Zhang, Authors of ReScript
2+
* Copyright (C) 2017 - Hongbo Zhang, Authors of ReScript
33
* This program is free software: you can redistribute it and/or modify
44
* it under the terms of the GNU Lesser General Public License as published by
55
* the Free Software Foundation, either version 3 of the License, or
@@ -17,7 +17,7 @@
1717
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1818
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1919
* GNU Lesser General Public License for more details.
20-
*
20+
*
2121
* You should have received a copy of the GNU Lesser General Public License
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
@@ -400,7 +400,4 @@ let translate output_prefix loc (cxt : Lam_compile_context.t)
400400
Lfunction(Curried, [parm],
401401
Matching.inline_lazy_force (Lvar parm) Location.none)
402402
It is inlined, this should not appear here *) ->
403-
(*we dont use [throw] here, since [throw] is an statement *)
404-
let s = Lam_print.primitive_to_string prim in
405-
Bs_warnings.warn_missing_primitive loc s;
406-
E.resolve_and_apply s args
403+
assert false

jscomp/core/lam_dispatch_primitive.ml

+9-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(* Copyright (C) 2015 - 2016 Bloomberg Finance L.P.
2-
* Copyright (C) 2017 - Hongbo Zhang, Authors of ReScript
2+
* Copyright (C) 2017 - Hongbo Zhang, Authors of ReScript
33
* This program is free software: you can redistribute it and/or modify
44
* it under the terms of the GNU Lesser General Public License as published by
55
* the Free Software Foundation, either version 3 of the License, or
@@ -17,23 +17,23 @@
1717
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1818
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1919
* GNU Lesser General Public License for more details.
20-
*
20+
*
2121
* You should have received a copy of the GNU Lesser General Public License
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

2525
module E = Js_exp_make
2626
(* module S = Js_stmt_make *)
2727

28-
(**
28+
(**
2929
There are two things we need consider:
3030
1. For some primitives we can replace caml-primitive with js primitives directly
3131
2. For some standard library functions, we prefer to replace with javascript primitives
3232
For example [Pervasives["^"] -> ^]
3333
We can collect all mli files in OCaml and replace it with an efficient javascript runtime
3434
35-
TODO: return type to be expression is ugly,
36-
we should allow return block
35+
TODO: return type to be expression is ugly,
36+
we should allow return block
3737
*)
3838
let translate loc (prim_name : string) (args : J.expression list) : J.expression
3939
=
@@ -263,13 +263,7 @@ let translate loc (prim_name : string) (args : J.expression list) : J.expression
263263
| "?await" -> (
264264
match args with
265265
| [e] -> {e with expression_desc = Await e}
266-
| _ -> assert false
267-
)
268-
| _ ->
269-
Bs_warnings.warn_missing_primitive loc prim_name;
270-
E.resolve_and_apply prim_name args
271-
(*we dont use [throw] here, since [throw] is an statement
272-
so we wrap in IIFE
273-
TODO: we might provoide a hook for user to provide polyfill.
274-
For example `Bs_global.xxx`
275-
*)
266+
| _ -> assert false)
267+
| missing_impl ->
268+
let msg = Warnings.message (Bs_unimplemented_primitive missing_impl) in
269+
Location.raise_errorf ~loc "%s" msg

jscomp/ext/js_runtime_modules.ml

-2
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ let option = "Caml_option"
6565

6666
let module_ = "Caml_module"
6767

68-
let external_polyfill = "Caml_external_polyfill"
69-
7068
let caml_js_exceptions = "Caml_js_exceptions"
7169

7270
let caml_splice_call = "Caml_splice_call"

jscomp/ext/warnings.ml

+13-13
Original file line numberDiff line numberDiff line change
@@ -485,30 +485,30 @@ let message = function
485485
^ " : using an empty string as a shorthand to infer the external's name \
486486
from the value's name is dangerous when refactoring, and therefore \
487487
deprecated"
488-
| Bs_unimplemented_primitive s -> "Unimplemented primitive used:" ^ s
488+
| Bs_unimplemented_primitive s -> "Unimplemented primitive used: " ^ s
489489
| Bs_integer_literal_overflow ->
490490
"Integer literal exceeds the range of representable integers of type int"
491491
| Bs_uninterpreted_delimiters s -> "Uninterpreted delimiters " ^ s
492492
| Bs_toplevel_expression_unit help ->
493-
Printf.sprintf "This%sis at the top level and is expected to return `unit`. But it's returning %s.\n\n In ReScript, anything at the top level must evaluate to `unit`. You can fix this by assigning the expression to a value, or piping it into the `ignore` function.%s"
494-
(match help with
495-
| Some (_, FunctionCall) -> " function call "
496-
| _ -> " ")
493+
Printf.sprintf "This%sis at the top level and is expected to return `unit`. But it's returning %s.\n\n In ReScript, anything at the top level must evaluate to `unit`. You can fix this by assigning the expression to a value, or piping it into the `ignore` function.%s"
494+
(match help with
495+
| Some (_, FunctionCall) -> " function call "
496+
| _ -> " ")
497497

498-
(match help with
499-
| Some (return_type, _) -> Printf.sprintf "`%s`" return_type
498+
(match help with
499+
| Some (return_type, _) -> Printf.sprintf "`%s`" return_type
500500
| None -> "something that is not `unit`")
501501

502-
(match help with
502+
(match help with
503503
| Some (_, help_typ) ->
504-
let help_text = (match help_typ with
505-
| FunctionCall -> "yourFunctionCall()"
504+
let help_text = (match help_typ with
505+
| FunctionCall -> "yourFunctionCall()"
506506
| Other -> "yourExpression") in
507507
Printf.sprintf "\n\n Possible solutions:\n - Assigning to a value that is then ignored: `let _ = %s`\n - Piping into the built-in ignore function to ignore the result: `%s->ignore`" help_text help_text
508-
| _ -> "")
508+
| _ -> "")
509509
| Bs_todo maybe_text -> (
510-
match maybe_text with
511-
| None -> "Todo found."
510+
match maybe_text with
511+
| None -> "Todo found."
512512
| Some todo -> "Todo found: " ^ todo
513513
) ^ "\n\n This code is not implemented yet and will crash at runtime. Make sure you implement this before running the code."
514514

0 commit comments

Comments
 (0)