Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/forbid-proto-field-name.breaking.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- A record field, object field or `@tag` can no longer have the JavaScript name `__proto__`, which sets an object's prototype instead of a property.
15 changes: 0 additions & 15 deletions compiler/backend/js_exp_make.ml
Original file line number Diff line number Diff line change
Expand Up @@ -761,21 +761,6 @@ let obj ?dup properties : t =
let obj_entries entries : t =
{expression_desc = Object entries; comment = None; source_loc = None}

let define_own_property (e : t) (name : string) (value : t) : t =
call ~info:Js_call_info.ml_full_call
(dot (js_global "Object") "defineProperty")
[
e;
str name;
obj
[
(Js_op.Lit "value", value);
(Js_op.Lit "writable", true_);
(Js_op.Lit "enumerable", true_);
(Js_op.Lit "configurable", true_);
];
]

let rec triple_equal ?comment (e0 : t) (e1 : t) : t =
match (e0.expression_desc, e1.expression_desc) with
| ( (Null | Undefined _),
Expand Down
5 changes: 0 additions & 5 deletions compiler/backend/js_exp_make.mli
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,6 @@ val obj : ?dup:J.expression -> J.property_map -> t

val obj_entries : J.object_entry list -> t

val define_own_property : t -> string -> t -> t
(** [Object.defineProperty(e, name, {value, ...})]: defines [name] as an own
data property of [e], as a property of an object literal does, where an
assignment would run a setter that [e] inherits *)

val true_ : t

val false_ : t
Expand Down
15 changes: 3 additions & 12 deletions compiler/backend/lam_compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2239,24 +2239,15 @@ let compile output_prefix =
the statements of every override have run, so they could mutate
the base before the copy. Copy first, then set each field. *)
let copy = E.var id in
(* Each override is an own property of the copy, as it is of the
literal. Assigning a field makes one, except for [__proto__]
when [init] has no own [__proto__]: the assignment runs the
setter the copy inherits from [Object.prototype], which sets
the prototype of the copy, or does nothing. *)
let set_field fld_info i value =
match set_field_name fld_info with
| "__proto__" -> E.define_own_property copy "__proto__" value
| _ ->
Js_of_lam_block.set_field fld_info copy (Int32.of_int i) value
in
let assignments =
List.concat_map
(fun (i, fld_info, set_loc, (output : Js_output.t), value) ->
output.block
@ [
S.exp
(with_source_loc set_loc (set_field fld_info i value));
(with_source_loc set_loc
(Js_of_lam_block.set_field fld_info copy
(Int32.of_int i) value));
])
overrides
in
Expand Down
16 changes: 15 additions & 1 deletion compiler/typing/ast_untagged_variants.ml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type error =
| InvalidVariantTagAnnotation
| InvalidUntaggedVariantDefinition of untagged_error
| TagFieldNameConflict of string * string * string
| ProtoFieldName
| UnboxedOnConstructor of {constructor: string; single_field_record: bool}
| InvalidInstanceOf
| InvalidModuleClass of string
Expand Down Expand Up @@ -109,6 +110,11 @@ let report_error ppf =
value of inline record field \"%s\". Use a different @tag name or \
rename the field."
constructor_name runtime_value field_name
| ProtoFieldName ->
fprintf ppf
"A field's JavaScript name can't be __proto__: in JavaScript, it sets \
the object's prototype instead of a property. Use another name, or a \
dict."
| UnboxedOnConstructor {constructor; single_field_record} ->
fprintf ppf
"@@unboxed applies to a whole variant: write it on the type, as in \
Expand Down Expand Up @@ -562,6 +568,12 @@ let check_runtime_annotation ~loc ~is_abstract_or_abbreviation
ClassLocationWithoutClass (txt, missing) )))
attrs

(* [__proto__] is an accessor that every object inherits from
[Object.prototype], so a field or tag with that runtime name can't be read
or written like other properties. *)
let check_runtime_field_name ~loc name =
if name = "__proto__" then raise (Error (loc, ProtoFieldName))

let process_tag_name (attrs : Parsetree.attributes) =
let st = ref None in
List.iter
Expand All @@ -571,7 +583,9 @@ let process_tag_name (attrs : Parsetree.attributes) =
if !st = None then (
(match Ast_payload.semantic_string_of_payload payload with
| None -> ()
| Some s -> st := Some s);
| Some s ->
check_runtime_field_name ~loc s;
st := Some s);
if !st = None then raise (Error (loc, InvalidVariantTagAnnotation)))
else raise (Error (loc, Duplicated_bs_as))
| _ -> ())
Expand Down
6 changes: 6 additions & 0 deletions compiler/typing/typedecl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,12 @@ let transl_labels ?record_name env closed lbls =
(Ast_untagged_variants.Error
(loc, Ast_untagged_variants.Duplicated_bs_as))
| None -> ());
(match runtime_name with
| Some {txt; loc} ->
Ast_untagged_variants.check_runtime_field_name ~loc
(String_literal.string_semantic txt)
| None ->
Ast_untagged_variants.check_runtime_field_name ~loc:name.loc name.txt);
Builtin_attributes.warning_scope attrs (fun () ->
let arg = Ast_helper.Typ.force_poly arg in
let cty = transl_simple_type env closed arg in
Expand Down
1 change: 1 addition & 0 deletions compiler/typing/typetexp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,7 @@ and transl_fields env policy o fields =
in
let add_field = function
| Otag (s, a, ty1) ->
Ast_untagged_variants.check_runtime_field_name ~loc:s.loc s.txt;
let ty1 =
Builtin_attributes.warning_scope a (fun () ->
transl_poly_type env policy ty1)
Expand Down
Loading
Loading