Commit 29bd8286 authored by littledan's avatar littledan Committed by Commit bot

Use stricter type checks in Intl's bound methods

This patch ensures that Intl bound method getters can't be retargeted
to other Intl types. If that were to happen, then a RUNTIME_ASSERT
would trigger later. This patch throws a TypeError instead, as the
specification requires.

BUG=v8:4870

Review-Url: https://codereview.chromium.org/1986763003
Cr-Commit-Position: refs/heads/master@{#36330}
parent 517b6599
......@@ -87,11 +87,11 @@ function InstallConstructor(object, name, func) {
/**
* Adds bound method to the prototype of the given object.
*/
function AddBoundMethod(obj, methodName, implementation, length) {
function AddBoundMethod(obj, methodName, implementation, length, type) {
%CheckIsBootstrapping();
var internalName = %CreatePrivateSymbol(methodName);
var getter = function() {
if (!%IsInitializedIntlObject(this)) {
if (!%IsInitializedIntlObjectOfType(this, type)) {
throw MakeTypeError(kMethodCalledOnWrongObject, methodName);
}
if (IS_UNDEFINED(this[internalName])) {
......@@ -1082,7 +1082,7 @@ function compare(collator, x, y) {
};
AddBoundMethod(Intl.Collator, 'compare', compare, 2);
AddBoundMethod(Intl.Collator, 'compare', compare, 2, 'collator');
/**
* Verifies that the input is a well-formed ISO 4217 currency code.
......@@ -1351,7 +1351,7 @@ function IntlParseNumber(formatter, value) {
GlobalString(value));
}
AddBoundMethod(Intl.NumberFormat, 'format', formatNumber, 1);
AddBoundMethod(Intl.NumberFormat, 'format', formatNumber, 1, 'numberformat');
/**
* Returns a string that matches LDML representation of the options object.
......@@ -1775,7 +1775,7 @@ function IntlParseDate(formatter, value) {
// 0 because date is optional argument.
AddBoundMethod(Intl.DateTimeFormat, 'format', formatDate, 0);
AddBoundMethod(Intl.DateTimeFormat, 'format', formatDate, 0, 'dateformat');
/**
......@@ -1963,11 +1963,13 @@ function breakType(iterator) {
}
AddBoundMethod(Intl.v8BreakIterator, 'adoptText', adoptText, 1);
AddBoundMethod(Intl.v8BreakIterator, 'first', first, 0);
AddBoundMethod(Intl.v8BreakIterator, 'next', next, 0);
AddBoundMethod(Intl.v8BreakIterator, 'current', current, 0);
AddBoundMethod(Intl.v8BreakIterator, 'breakType', breakType, 0);
AddBoundMethod(Intl.v8BreakIterator, 'adoptText', adoptText, 1,
'breakiterator');
AddBoundMethod(Intl.v8BreakIterator, 'first', first, 0, 'breakiterator');
AddBoundMethod(Intl.v8BreakIterator, 'next', next, 0, 'breakiterator');
AddBoundMethod(Intl.v8BreakIterator, 'current', current, 0, 'breakiterator');
AddBoundMethod(Intl.v8BreakIterator, 'breakType', breakType, 0,
'breakiterator');
// Save references to Intl objects and methods we use, for added security.
var savedObjects = {
......
......@@ -14,7 +14,9 @@ var AddBoundMethod = utils.ImportNow("AddBoundMethod");
var IntlParseDate = utils.ImportNow("IntlParseDate");
var IntlParseNumber = utils.ImportNow("IntlParseNumber");
AddBoundMethod(GlobalIntl.DateTimeFormat, 'v8Parse', IntlParseDate, 1);
AddBoundMethod(GlobalIntl.NumberFormat, 'v8Parse', IntlParseNumber, 1);
AddBoundMethod(GlobalIntl.DateTimeFormat, 'v8Parse', IntlParseDate, 1,
'dateformat');
AddBoundMethod(GlobalIntl.NumberFormat, 'v8Parse', IntlParseNumber, 1,
'numberformat');
})
// Copyright 2016 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.
assertThrows(() =>
Object.getOwnPropertyDescriptor(Intl.Collator.prototype, 'compare')
.get.call(new Intl.DateTimeFormat())('a', 'b'),
TypeError);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment