Repository navigation
Commit 32116b5
authored
Why:
An `@obj` external builds its object as a literal and assigns an
optional argument whose presence is not known after it, under
`if (tmp !== undefined)`. So that argument was evaluated after the
arguments that follow it: `make(~size=?size(), ~mode=mode(), ())`
called `mode()` first, and a later argument with side effects could
change what the optional argument read, such as a `@get` getter, a
`@val` global, `Dict.get`, an array element, a mutable field or a ref.
`hold_obj_args` now binds such an argument to a temporary after its
statements, unless evaluating it after the literal gives the same
values and effects.
As with the temporaries of #9217, `sequence_arguments` then evaluates
the arguments to the left of the temporary before it, with
`evaluate_now`. That bound an optional argument known to be present as
a whole, so that it lost its presence and was assigned after the
literal too, and it bound variables and fields of records although
defining a temporary as such a value cannot change them. `evaluate_now`
now evaluates the value in an `Optional_block` and keeps the block, and
`sequence_arguments` leaves an argument that runs no code, a variable,
a constant or a field of a record, in place when the statements after
it only define variables as such values. A JS property read may run a
getter, so it does not count.
An optional argument that is a JS property read, as from a `@get`
external or a `@val` name with a dot, was read twice, to test it for
`None` and to unwrap it, by a call, a JSX key and an `@obj` external
alike. A getter ran twice, and one that returned a value and then
`undefined` made the call throw a `TypeError`. Such an argument is now
read once: a call and a JSX key give it to
`Primitive_option.valFromOption`, which returns `undefined` for `None`
since the previous commit, and an `@obj` external binds it.
`Js_analyzer.is_okay_to_duplicate ~property_reads:false` tells these
arguments apart.
JS modified (optional argument with side effects):
Example:
@obj external make: (~size: int=?, ~mode: string, unit) => {..} = ""
let o = (size, mode) => make(~size=?size(), ~mode=mode(), ())
Before (js):
function o(size, mode) {
let tmp = {
mode: mode()
};
let tmp$1 = size();
if (tmp$1 !== undefined) {
tmp.size = tmp$1;
}
return tmp;
}
After (js):
function o(size, mode) {
let tmp = size();
let tmp$1 = {
mode: mode()
};
if (tmp !== undefined) {
tmp$1.size = tmp;
}
return tmp$1;
}
JS modified (optional argument that a later argument changes):
Example:
type source
@get external name: source => option<string> = "name"
@obj external makeName: (~name: string=?, ~mode: string, unit) => {..} = ""
let n = (s, rename) => makeName(~name=?name(s), ~mode=rename(s), ())
Before (js):
function n(s, rename) {
let tmp = {
mode: rename(s)
};
if (s.name !== undefined) {
tmp.name = s.name;
}
return tmp;
}
After (js):
function n(s, rename) {
let tmp = s.name;
let tmp$1 = {
mode: rename(s)
};
if (tmp !== undefined) {
tmp$1.name = tmp;
}
return tmp$1;
}
JS modified (arguments before a bound optional argument):
Example:
type props = {color: string, width?: string}
@obj
external style: (~color: string=?, ~icon: string=?, ~width: string=?, ~height: string, unit) => {..} = ""
let a = (props, height) => style(~color=props.color, ~width=?props.width, ~height=height(), ())
let b = (icon, width, height) => style(~icon=icon(), ~width=?width(), ~height=height(), ())
Before (js):
function a(props, height) {
let tmp = {
color: props.color,
height: height()
};
if (props.width !== undefined) {
tmp.width = props.width;
}
return tmp;
}
function b(icon, width, height) {
let tmp = {
icon: icon(),
height: height()
};
let tmp$1 = width();
if (tmp$1 !== undefined) {
tmp.width = tmp$1;
}
return tmp;
}
After (js):
function a(props, height) {
let tmp = props.width;
let tmp$1 = {
color: props.color,
height: height()
};
if (tmp !== undefined) {
tmp$1.width = tmp;
}
return tmp$1;
}
function b(icon, width, height) {
let tmp = icon();
let tmp$1 = width();
let tmp$2 = {
icon: tmp,
height: height()
};
if (tmp$1 !== undefined) {
tmp$2.width = tmp$1;
}
return tmp$2;
}
JS modified (optional argument known to be present before a temporary):
Example:
type item = {id: int}
@obj external mk: (~item: item=?, ~level: @int [#low | @as(10) #high], unit) => {..} = ""
let k = async (f, p) => mk(~item=f(), ~level=await p, ())
Before (js):
async function k(f, p) {
let tmp = f();
let tmp$1 = await p;
let tmp$2 = {
level: (() => {
switch (tmp$1) {
…
}
})()
};
if (tmp !== undefined) {
tmp$2.item = Primitive_option.valFromOption(tmp);
}
return tmp$2;
}
After (js):
async function k(f, p) {
let tmp = f();
let tmp$1 = await p;
return {
item: tmp,
level: (() => {
switch (tmp$1) {
…
}
})()
};
}
JS modified (optional property read):
Example:
type source
@get external name: source => option<string> = "name"
@Val external greet: (~name: string=?, unit) => string = "greet"
@Val external userName: option<string> = "window.userName"
@obj external makeName: (~name: string=?, ~mode: string, unit) => {..} = ""
let g = s => greet(~name=?name(s), ())
let u = () => greet(~name=?userName, ())
let m = (o: {"name": option<string>}) => makeName(~name=?o["name"], ~mode="dark", ())
Before (js):
function g(s) {
return greet(s.name !== undefined ? Primitive_option.valFromOption(s.name) : undefined);
}
function u() {
return greet(window.userName !== undefined ? Primitive_option.valFromOption(window.userName) : undefined);
}
function m(o) {
let tmp = {
mode: "dark"
};
if (o.name !== undefined) {
tmp.name = o.name;
}
return tmp;
}
After (js):
function g(s) {
return greet(Primitive_option.valFromOption(s.name));
}
function u() {
return greet(Primitive_option.valFromOption(window.userName));
}
function m(o) {
let tmp = {
mode: "dark"
};
let tmp$1 = o.name;
if (tmp$1 !== undefined) {
tmp.name = tmp$1;
}
return tmp;
}
JS modified (temporaries for optional arguments of an @obj external):
Before (representation): Lam_compile_external_obj.hold_obj_args
~x=?f(), ~m=g() ↦ after the literal
~x=?p.x, ~m=g() ↦ after the literal
~x=?f(), ~m=p.m ↦ after the literal
~x=?f(), g() ↦ after the literal
~x=?p.x, ~m=p.m ↦ after the literal
~x=?f(), ~m="dark" ↦ after the literal
~x=?v, ~m=g() ↦ after the literal
~x=?f(), ~y=?g() ↦ after the literal
note: p.x stands for a field, a global or a lookup, and v for an immutable variable
After (representation):
~x=?f(), ~m=g() ↦ temporary
~x=?p.x, ~m=g() ↦ temporary
~x=?f(), ~m=p.m ↦ temporary
~x=?f(), g() ↦ temporary
~x=?p.x, ~m=p.m ↦ after the literal
~x=?f(), ~m="dark" ↦ after the literal
~x=?v, ~m=g() ↦ after the literal
~x=?f(), ~y=?g() ↦ after the literal
note: p.x stands for a field, a global or a lookup, and v for an immutable variable
JS modified (arguments before a statement):
Before (representation): Lam_compile.sequence_arguments
r.c, then let tmp = r.x ↦ temporary
r.c, then let tmp = s.x ↦ temporary
s.c, then let tmp = r.x ↦ temporary
f(), then let tmp = r.x ↦ temporary
r.c, then let tmp = g() ↦ temporary
~c=f(), then a statement ↦ temporary for the option
note: r.c is a variable or a field of a record, s.x a JS property read, and ~c=f() an optional argument known to be present
After (representation):
r.c, then let tmp = r.x ↦ in place
r.c, then let tmp = s.x ↦ temporary
s.c, then let tmp = r.x ↦ temporary
f(), then let tmp = r.x ↦ temporary
r.c, then let tmp = g() ↦ temporary
~c=f(), then a statement ↦ temporary for f(), option in place
note: r.c is a variable or a field of a record, s.x a JS property read, and ~c=f() an optional argument known to be present
JS modified (optional arguments read twice):
Before (representation): Js_analyzer.is_okay_to_duplicate
x, M.x, globalName ↦ test, then unwrap
r.field ↦ test, then unwrap
s.prop, o["prop"], window.x ↦ test, then unwrap
f(), a[i], c ? x : y ↦ unwrap once
After (representation): Js_analyzer.is_okay_to_duplicate ~property_reads:false
x, M.x, globalName ↦ test, then unwrap
r.field ↦ test, then unwrap
s.prop, o["prop"], window.x ↦ unwrap once
f(), a[i], c ? x : y ↦ unwrap once
note: r.field is a field of a record, s.prop a JS property read, such as a `@get` getter, and window.x a `@val` name with a dot
Review: Which calls keep their output? An `@obj` call in which nothing
that the literal evaluates after the optional argument can change it or
be changed by it: constants, immutable variables and functions, reads
without side effects when the optional argument has none either, as in
`make(~x=?p.x, ~m=p.m, ())`, and other optional arguments whose
presence is not known, which are assigned after it, in order. Variables
and fields of records are still tested and unwrapped in place. No
checked-in output changes besides the new tests.
Review: Which arguments get a temporary they do not need? A later
argument is taken to change an optional one unless it is `delay_safe`
or neither has side effects, and the JS IR does not say whether a field
is mutable or a call pure. So `make(~a=?props.a, ~b=Int.toString(n), ())`
binds `props.a`, as `a` above binds `props.width`. A field of a JS
object type, `o["x"]`, cannot be told from a getter, so an `@obj`
external binds it even when no later argument can change it, as `m`
above, while a call no longer tests it inline.
Review: Why in `hold_obj_args` and not in `hold_arg`? Whether the
argument has to be bound depends on the arguments after it, which
`hold_arg` does not see, and only an object-creating external evaluates
an argument after the ones that follow it. The binding does not depend
on the type of the argument, so an optional polymorphic variant
argument keeps its place whether or not `hold_arg` binds a later
argument.
Review: Why is `delay_safe` passed to `hold_obj_args`? It is
`Lam_compile.delay_safe`, which needs the variables the function
assigns, and `Lam_compile` depends on this module.
Review: Why change `evaluate_now` and `sequence_arguments` here? Before
this commit only the temporaries of #9217 for polymorphic variant
arguments made `sequence_arguments` bind the arguments to their left,
as in `k` above, where `item` lost its place in `Object.keys`. Binding
optional arguments of any type would make that common: without the
two changes, `b` above would assign `icon` after the literal as well,
and `a` would bind `props.color`. Both functions are in
`lam_compile.ml`, which #9231 does not change.
Review: Why a flag on `is_okay_to_duplicate` and not another predicate?
The two differ only in property reads, and its other callers, `min`
and `max` of booleans and the object of a `Pjs_object_get`, keep the
default. `Js_ast_util.named_expression` takes the same flag, which
`assemble_obj_args` passes.
Review: Is a `@val` global still read twice? Yes, a plain one is, like a
value of a JS module: only a property read is taken to run a getter, as
a `@get` external is there to do. A `@val` name with a dot and a global
with `@scope` compile to a property read and are read once.
Review: What still runs out of order? The side effects of a `unit` or
`@ignore` argument are put before the literal or the call, so
`mk(~a=f(), g())` calls `g()` first, as before. A getter is taken to
have no side effects, so `make(~name=?name(s), ~count=count(s), ())`
with two `@get` externals still reads `count` first.
Signed-off-by: mununki <woonki.moon@gmail.com>
1 parent 0178eb0 commit 32116b5
11 files changed
Lines changed: 914 additions & 28 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
365 | 365 | | |
366 | 366 | | |
367 | 367 | | |
368 | | - | |
| 368 | + | |
369 | 369 | | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
370 | 374 | | |
371 | | - | |
| 375 | + | |
| 376 | + | |
372 | 377 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
70 | 70 | | |
71 | 71 | | |
72 | 72 | | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
29 | | - | |
30 | | - | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
31 | 32 | | |
32 | 33 | | |
33 | 34 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
74 | 74 | | |
75 | 75 | | |
76 | 76 | | |
77 | | - | |
| 77 | + | |
78 | 78 | | |
79 | 79 | | |
80 | 80 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
282 | 282 | | |
283 | 283 | | |
284 | 284 | | |
285 | | - | |
| 285 | + | |
| 286 | + | |
286 | 287 | | |
287 | 288 | | |
288 | 289 | | |
| |||
291 | 292 | | |
292 | 293 | | |
293 | 294 | | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
294 | 298 | | |
295 | 299 | | |
296 | 300 | | |
| |||
327 | 331 | | |
328 | 332 | | |
329 | 333 | | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
330 | 348 | | |
331 | 349 | | |
332 | 350 | | |
333 | 351 | | |
334 | 352 | | |
335 | | - | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
336 | 357 | | |
337 | 358 | | |
338 | 359 | | |
| |||
351 | 372 | | |
352 | 373 | | |
353 | 374 | | |
354 | | - | |
| 375 | + | |
| 376 | + | |
355 | 377 | | |
356 | 378 | | |
357 | 379 | | |
| |||
1908 | 1930 | | |
1909 | 1931 | | |
1910 | 1932 | | |
1911 | | - | |
| 1933 | + | |
| 1934 | + | |
| 1935 | + | |
| 1936 | + | |
1912 | 1937 | | |
1913 | 1938 | | |
1914 | 1939 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
39 | 63 | | |
40 | 64 | | |
41 | | - | |
42 | | - | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
43 | 74 | | |
44 | 75 | | |
45 | 76 | | |
46 | | - | |
| 77 | + | |
47 | 78 | | |
48 | | - | |
| 79 | + | |
49 | 80 | | |
50 | 81 | | |
51 | 82 | | |
52 | | - | |
| 83 | + | |
53 | 84 | | |
54 | | - | |
55 | | - | |
56 | | - | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
57 | 105 | | |
58 | 106 | | |
59 | 107 | | |
| |||
130 | 178 | | |
131 | 179 | | |
132 | 180 | | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
137 | 185 | | |
138 | 186 | | |
139 | 187 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| 29 | + | |
29 | 30 | | |
30 | 31 | | |
31 | 32 | | |
32 | 33 | | |
33 | | - | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
34 | 39 | | |
35 | 40 | | |
36 | 41 | | |
| |||
0 commit comments