Skip to content

Commit 9511bdf

Browse files
committed
Allow optional chaining in JS library code
This change verifies that closure can successfully lower the optional chaining away on older browsers. This also requires bumping of chrome and safari minimum versions.. so maybe its not worth it?
1 parent be5643d commit 9511bdf

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

emcc.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2313,6 +2313,8 @@ def phase_linker_setup(options, state, newargs):
23132313
settings.MIN_CHROME_VERSION < 49 or
23142314
settings.MIN_SAFARI_VERSION < 110000 or
23152315
settings.MIN_IE_VERSION != 0x7FFFFFFF)
2316+
if not feature_matrix.caniuse(feature_matrix.Feature.OPTIONAL_CHAINING):
2317+
settings.TRANSPILE_TO_ES5 = True
23162318

23172319
if options.use_closure_compiler is None and settings.TRANSPILE_TO_ES5:
23182320
diagnostics.warning('transpile', 'enabling transpilation via closure due to browser version settings. This warning can be suppressed by passing `--closure=1` or `--closure=0` to opt into our explicitly.')

src/settings.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1764,10 +1764,10 @@ var AUTO_NATIVE_LIBRARIES = true;
17641764
// versions >= MIN_FIREFOX_VERSION
17651765
// are desired to work. Pass -sMIN_FIREFOX_VERSION=majorVersion to drop support
17661766
// for Firefox versions older than < majorVersion.
1767-
// Firefox ESR 68 was released on July 9, 2019.
1767+
// Firefox ESR 74 was released on March 10, 2020
17681768
// MAX_INT (0x7FFFFFFF, or -1) specifies that target is not supported.
17691769
// [link]
1770-
var MIN_FIREFOX_VERSION = 68;
1770+
var MIN_FIREFOX_VERSION = 74;
17711771

17721772
// Specifies the oldest version of desktop Safari to target. Version is encoded
17731773
// in MMmmVV, e.g. 70101 denotes Safari 7.1.1.
@@ -1806,10 +1806,10 @@ var MIN_EDGE_VERSION = 0x7FFFFFFF;
18061806

18071807
// Specifies the oldest version of Chrome. E.g. pass -sMIN_CHROME_VERSION=58 to
18081808
// drop support for Chrome 57 and older.
1809-
// Chrome 75.0.3770 was released on 2019-06-04
1809+
// Chrome 80 was released on February 4, 2020
18101810
// MAX_INT (0x7FFFFFFF, or -1) specifies that target is not supported.
18111811
// [link]
1812-
var MIN_CHROME_VERSION = 75;
1812+
var MIN_CHROME_VERSION = 80;
18131813

18141814
// Specifies minimum node version to target for the generated code. This is
18151815
// distinct from the minimum version required run the emscripten compiler.

test/test_other.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12819,6 +12819,10 @@ def test_es5_transpile(self, args):
1281912819
};
1282012820
global['foo'] = obj3;
1282112821
err('value2: ' + obj3.myMethod());
12822+
12823+
// optional chaining
12824+
let result = Module.foo?.doSomething();
12825+
err('result: ' + result);
1282212826
}
1282312827
});
1282412828
''')
@@ -12833,13 +12837,15 @@ def check_for_es6(filename, expect):
1283312837
self.assertContained(['() => 2', '()=>2'], js)
1283412838
self.assertContained('const ', js)
1283512839
self.assertContained('let ', js)
12840+
self.assertContained('foo?.doSomething()', js)
1283612841
else:
1283712842
self.verify_es5(filename)
1283812843
self.assertNotContained('foo(arg=', js)
1283912844
self.assertNotContained('() => 2', js)
1284012845
self.assertNotContained('()=>2', js)
1284112846
self.assertNotContained('const ', js)
1284212847
self.assertNotContained('let ', js)
12848+
self.assertNotContained('foo?.doSomething()', js)
1284312849

1284412850
# Check that under normal circumstances none of these features get
1284512851
# removed / transpiled.
@@ -13336,10 +13342,10 @@ def test_reproduce(self):
1333613342
self.assertTextDataIdentical(expected, response)
1333713343

1333813344
def test_min_browser_version(self):
13339-
err = self.expect_fail([EMCC, test_file('hello_world.c'), '-Werror', '-sWASM_BIGINT', '-sMIN_SAFARI_VERSION=120000'])
13345+
err = self.expect_fail([EMCC, test_file('hello_world.c'), '-Werror', '-sWASM_BIGINT', '-sMIN_SAFARI_VERSION=120000', '-Wno-transpile'])
1334013346
self.assertContained('emcc: error: MIN_SAFARI_VERSION=120000 is not compatible with WASM_BIGINT (150000 or above required)', err)
1334113347

13342-
err = self.expect_fail([EMCC, test_file('hello_world.c'), '-Werror', '-pthread', '-sMIN_CHROME_VERSION=73'])
13348+
err = self.expect_fail([EMCC, test_file('hello_world.c'), '-Werror', '-pthread', '-sMIN_CHROME_VERSION=73', '-Wno-transpile'])
1334313349
self.assertContained('emcc: error: MIN_CHROME_VERSION=73 is not compatible with pthreads (74 or above required)', err)
1334413350

1334513351
def test_signext_lowering(self):

tools/feature_matrix.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Feature(IntEnum):
2323
THREADS = auto()
2424
GLOBALTHIS = auto()
2525
PROMISE_ANY = auto()
26+
OPTIONAL_CHAINING = auto()
2627

2728

2829
default_features = {Feature.SIGN_EXT, Feature.MUTABLE_GLOBALS}
@@ -71,6 +72,12 @@ class Feature(IntEnum):
7172
'safari': 140000,
7273
'node': 150000,
7374
},
75+
Feature.OPTIONAL_CHAINING: {
76+
'chrome': 80,
77+
'firefox': 74,
78+
'safari': 130400,
79+
'node': 140000,
80+
},
7481
}
7582

7683

0 commit comments

Comments
 (0)