v8/test/mjsunit/compiler/object-constructor.js
Benedikt Meurer 226e63fc13 [turbofan] Fold Object constructor calls with certain values.
When calling

  Object(value)

where the value is known to be a JSReceiver, we can just replace it with
value, as the Object constructor call is a no-op in that case. Otherwise
when value is known to be not null or undefined then we can replace the
Object constructor call with an invocation of ToObject.

This covers the common pattern found in bundles generated by Webpack,
where the Object constructor is used to call imported functions, i.e.

  Object(module.foo)(1, 2, 3)

There's a lot of detail in https://github.com/webpack/webpack/issues/5600
on this matter and why this pattern was chosen.

Bug: v8:6772
Change-Id: I2b4f0b4542b68b97b337ce571d6d79946c73d8bb
Reviewed-on: https://chromium-review.googlesource.com/643868
Reviewed-by: Yang Guo <yangguo@chromium.org>
Commit-Queue: Benedikt Meurer <bmeurer@chromium.org>
Cr-Commit-Position: refs/heads/master@{#47728}
2017-08-31 06:36:06 +00:00

33 lines
907 B
JavaScript

// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --allow-natives-syntax
// Common pattern in Webpack 3 generated bundles, see
// https://github.com/webpack/webpack/issues/5600 for details.
(function ObjectConstructorWithKnownFunction() {
"use strict";
class A {
bar() { return this; }
};
function foo(a) {
return Object(a.bar)();
}
assertEquals(undefined, foo(new A));
assertEquals(undefined, foo(new A));
%OptimizeFunctionOnNextCall(foo);
assertEquals(undefined, foo(new A));
})();
(function ObjectConstructorWithString() {
"use strict";
function foo() {
return Object("a");
}
assertEquals('object', typeof foo());
assertEquals('object', typeof foo());
%OptimizeFunctionOnNextCall(foo);
assertEquals('object', typeof foo());
})();