Skip to content

Commit

Permalink
[intl] Implement ECMA402 PR500 Use OrdinaryHasInstance
Browse files Browse the repository at this point in the history
Implement tc39/ecma402#500

For the legacy [optional]  Unwrap*Format steps, use OrdinaryHasInstance
instead of InstanceofOperator.

ECMA402 agree w/ PR500 on 2021-1-14

Bug: v8:10981
Change-Id: Ic697aa245b11fecaf998127c009e59a821aaa01e
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2444092
Reviewed-by: Jakob Kummerow <jkummerow@chromium.org>
Reviewed-by: Shu-yu Guo <syg@chromium.org>
Commit-Queue: Frank Tang <ftang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#72303}
  • Loading branch information
FrankYFTang authored and Commit Bot committed Jan 25, 2021
1 parent 89ea6ca commit c2795bd
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 18 deletions.
12 changes: 5 additions & 7 deletions src/builtins/builtins-intl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -280,16 +280,14 @@ Object LegacyFormatConstructor(BuiltinArguments args, Isolate* isolate,
// 4. Let this be the this value.
if (args.new_target()->IsUndefined(isolate)) {
Handle<Object> receiver = args.receiver();

// 5. If NewTarget is undefined and ? InstanceofOperator(this, %<T>%)
// 5. If NewTarget is undefined and ? OrdinaryHasInstance(%<T>%, this)
// is true, then Look up the intrinsic value that has been stored on
// the context.
Handle<Object> is_instance_of_obj;
Handle<Object> ordinary_has_instance_obj;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
isolate, is_instance_of_obj,
Object::InstanceOf(isolate, receiver, constructor));

if (is_instance_of_obj->BooleanValue(isolate)) {
isolate, ordinary_has_instance_obj,
Object::OrdinaryHasInstance(isolate, constructor, receiver));
if (ordinary_has_instance_obj->BooleanValue(isolate)) {
if (!receiver->IsJSReceiver()) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate,
Expand Down
14 changes: 7 additions & 7 deletions src/objects/intl-objects.cc
Original file line number Diff line number Diff line change
Expand Up @@ -617,15 +617,15 @@ MaybeHandle<Object> Intl::LegacyUnwrapReceiver(Isolate* isolate,
Handle<JSReceiver> receiver,
Handle<JSFunction> constructor,
bool has_initialized_slot) {
Handle<Object> obj_is_instance_of;
ASSIGN_RETURN_ON_EXCEPTION(isolate, obj_is_instance_of,
Object::InstanceOf(isolate, receiver, constructor),
Object);
bool is_instance_of = obj_is_instance_of->BooleanValue(isolate);
Handle<Object> obj_ordinary_has_instance;
ASSIGN_RETURN_ON_EXCEPTION(
isolate, obj_ordinary_has_instance,
Object::OrdinaryHasInstance(isolate, constructor, receiver), Object);
bool ordinary_has_instance = obj_ordinary_has_instance->BooleanValue(isolate);

// 2. If receiver does not have an [[Initialized...]] internal slot
// and ? InstanceofOperator(receiver, constructor) is true, then
if (!has_initialized_slot && is_instance_of) {
// and ? OrdinaryHasInstance(constructor, receiver) is true, then
if (!has_initialized_slot && ordinary_has_instance) {
// 2. a. Let new_receiver be ? Get(receiver, %Intl%.[[FallbackSymbol]]).
Handle<Object> new_receiver;
ASSIGN_RETURN_ON_EXCEPTION(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2020 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.

// Verify ECMA402 PR 500 Use OrdinaryHasInstance in normative optional steps
// https://github.com/tc39/ecma402/pull/500

Object.defineProperty(Intl.DateTimeFormat, Symbol.hasInstance, {
get() { throw new Error("Intl.DateTimeFormat[@@hasInstance] lookup"); }
});

var dtf;
assertDoesNotThrow(() => dtf = new Intl.DateTimeFormat());
assertDoesNotThrow(() => dtf.format(new Date()));
assertDoesNotThrow(() => dtf.resolvedOptions());
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2020 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.

// Verify ECMA402 PR 500 Use OrdinaryHasInstance in normative optional steps
// https://github.com/tc39/ecma402/pull/500

Object.defineProperty(Intl.NumberFormat, Symbol.hasInstance, {
get() { throw new Error("Intl.NumberFormat[@@hasInstance] lookup"); }
});

var nf;
assertDoesNotThrow(() => nf = new Intl.NumberFormat());
assertDoesNotThrow(() => nf.format(123));
assertDoesNotThrow(() => nf.resolvedOptions());
6 changes: 2 additions & 4 deletions test/intl/number-format/wont-crash-by-1-or-false.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
// Make sure passing 1 or false to patched construtor won't cause crash

Object.defineProperty(Intl.NumberFormat, Symbol.hasInstance, { value: _ => true });
assertThrows(() =>
Intl.NumberFormat.call(1), TypeError);
assertDoesNotThrow(() => Intl.NumberFormat.call(1));

assertThrows(() =>
Intl.NumberFormat.call(false), TypeError);
assertDoesNotThrow(() => Intl.NumberFormat.call(false));

0 comments on commit c2795bd

Please sign in to comment.