Skip to content

Commit

Permalink
Stop IsReadableStream* and IsWritableStream* throwing
Browse files Browse the repository at this point in the history
Previously IsReadableStream, IsReadableStreamDefaultWriter,
IsReadableStreamDefaultController, IsWritableStream,
IsWritableStreamDefaultWriter and IsWritableStreamDefaultController
would throw when passed a null or undefined value.

Make them return false instead.

Also add CommonOperations.js file. Currently it only contains the function
hasOwnPropertyNoThrow() but more will be added later.

TBR=brettw@chromium.org

Bug: 747772
Change-Id: I06aa2c92de994d547c875c20d54fd50c0551662a
Reviewed-on: https://chromium-review.googlesource.com/585030
Commit-Queue: Adam Rice <ricea@chromium.org>
Reviewed-by: Takeshi Yoshino <tyoshino@chromium.org>
Cr-Commit-Position: refs/heads/master@{#490375}
  • Loading branch information
ricea authored and Commit Bot committed Jul 28, 2017
1 parent f079fe4 commit be92b2d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gn
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ default_args = {
v8_extra_library_files = [
# Dependencies used by the extra libraries. Putting them here causes them
# to be executed first during snapshot creation.
"//third_party/WebKit/Source/core/streams/CommonOperations.js",
"//third_party/WebKit/Source/core/streams/CommonStrings.js",
"//third_party/WebKit/Source/core/streams/SimpleQueue.js",

Expand Down
25 changes: 25 additions & 0 deletions third_party/WebKit/Source/core/streams/CommonOperations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Implementation of functions that are shared between ReadableStream and
// WritableStream.

(function(global, binding, v8) {
'use strict';

// Javascript functions. It is important to use these copies for security and
// robustness. See "V8 Extras Design Doc", section "Security Considerations".
// https://docs.google.com/document/d/1AT5-T0aHGp7Lt29vPWFr2-qG8r3l9CByyvKwEuA8Ec0/edit#heading=h.9yixony1a18r
const Boolean = global.Boolean;
const hasOwnProperty = v8.uncurryThis(global.Object.hasOwnProperty);

function hasOwnPropertyNoThrow(x, property) {
// The cast of |x| to Boolean will eliminate undefined and null, which would
// cause hasOwnProperty to throw a TypeError, as well as some other values
// that can't be objects and so will fail the check anyway.
return Boolean(x) && hasOwnProperty(x, property);
}

binding.streamOperations = { hasOwnPropertyNoThrow };
});
10 changes: 6 additions & 4 deletions third_party/WebKit/Source/core/streams/ReadableStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
const Infinity = global.Infinity;

const defineProperty = global.Object.defineProperty;
const hasOwnProperty = v8.uncurryThis(global.Object.hasOwnProperty);
const callFunction = v8.uncurryThis(global.Function.prototype.call);
const applyFunction = v8.uncurryThis(global.Function.prototype.apply);

Expand All @@ -63,6 +62,9 @@
const Promise_resolve = v8.simpleBind(Promise.resolve, Promise);
const Promise_reject = v8.simpleBind(Promise.reject, Promise);

// From CommonOperations.js
const { hasOwnPropertyNoThrow } = binding.streamOperations;

const streamErrors = binding.streamErrors;
const errCancelLockedStream =
'Cannot cancel a readable stream that is locked to a reader';
Expand Down Expand Up @@ -778,7 +780,7 @@
}

function IsReadableStream(x) {
return hasOwnProperty(x, _controller);
return hasOwnPropertyNoThrow(x, _controller);
}

function IsReadableStreamDisturbed(stream) {
Expand All @@ -790,11 +792,11 @@
}

function IsReadableStreamDefaultController(x) {
return hasOwnProperty(x, _controlledReadableStream);
return hasOwnPropertyNoThrow(x, _controlledReadableStream);
}

function IsReadableStreamDefaultReader(x) {
return hasOwnProperty(x, _readRequests);
return hasOwnPropertyNoThrow(x, _readRequests);
}

function IsReadableStreamReadable(stream) {
Expand Down
9 changes: 6 additions & 3 deletions third_party/WebKit/Source/core/streams/WritableStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@
const Promise_resolve = v8.simpleBind(Promise.resolve, Promise);
const Promise_reject = v8.simpleBind(Promise.reject, Promise);

// From CommonOperations.js
const { hasOwnPropertyNoThrow } = binding.streamOperations;

// User-visible strings.
const streamErrors = binding.streamErrors;
const errAbortLockedStream = 'Cannot abort a writable stream that is locked to a writer';
Expand Down Expand Up @@ -203,7 +206,7 @@
}

function IsWritableStream(x) {
return hasOwnProperty(x, _writableStreamController);
return hasOwnPropertyNoThrow(x, _writableStreamController);
}

function IsWritableStreamLocked(stream) {
Expand Down Expand Up @@ -616,7 +619,7 @@
// Writable Stream Writer Abstract Operations

function IsWritableStreamDefaultWriter(x) {
return hasOwnProperty(x, _ownerWritableStream);
return hasOwnPropertyNoThrow(x, _ownerWritableStream);
}

function WritableStreamDefaultWriterAbort(writer, reason) {
Expand Down Expand Up @@ -838,7 +841,7 @@
// Writable Stream Default Controller Abstract Operations

function IsWritableStreamDefaultController(x) {
return hasOwnProperty(x, _underlyingSink);
return hasOwnPropertyNoThrow(x, _underlyingSink);
}

function WritableStreamDefaultControllerClose(controller) {
Expand Down

0 comments on commit be92b2d

Please sign in to comment.