<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[186037] trunk/Source/JavaScriptCore</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/186037">186037</a></dd>
<dt>Author</dt> <dd>darin@apple.com</dd>
<dt>Date</dt> <dd>2015-06-27 15:53:12 -0700 (Sat, 27 Jun 2015)</dd>
</dl>
<h3>Log Message</h3>
<pre>Make converting JSString to StringView idiomatically safe
https://bugs.webkit.org/show_bug.cgi?id=146387
Reviewed by Anders Carlsson.
* jsc.cpp:
(functionPrint): Add explicit call to SafeView::get, needed since there
is no StringView temporary.
(functionDebug): Ditto.
* runtime/ArrayPrototype.cpp:
(JSC::holesMustForwardToPrototype): Refactored into helper function.
(JSC::join): Refactored so that StringView is a function argument, making
the lifetime simpler.
(JSC::arrayProtoFuncJoin): Ditto.
(JSC::arrayProtoFuncReverse): Use new holesMustForwardToPrototype helper.
* runtime/JSGlobalObjectFunctions.cpp:
(JSC::encode): Add explicit call to SafeView::get.
* runtime/JSString.h: Moved declarations of functions to the top of the
file instead of mixing them in with the function definitions. Changed
return type of the view function to return a JSString::SafeView so that
the JSString's lifetime will last as long as the StringView does in
typical coding idioms.
(JSC::JSString::getIndex): Use unsafeView so we can index into the
view; could also have used view.get but here in this class this seems fine.
(JSC::JSRopeString::unsafeView): Renamed existing view function to this.
(JSC::JSString::unsafeView): Ditto.
(JSC::JSString::SafeView::SafeView): Contains reference to an ExecState
and a JSString. The ExecState is needed to create the StringView, and the
JSString needs to be kept alive as long as the StringView is.
(JSC::JSString::SafeView::operator StringView): Call unsafeView.
(JSC::JSString::SafeView::get): Convenience for when we want to call
StringView member functions.
(JSC::JSString::view): Added. Returns a SafeView.
* runtime/StringPrototype.cpp:
(JSC::stringProtoFuncIndexOf): Add explicit call to SafeView::get.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCorejsccpp">trunk/Source/JavaScriptCore/jsc.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoreruntimeArrayPrototypecpp">trunk/Source/JavaScriptCore/runtime/ArrayPrototype.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoreruntimeJSGlobalObjectFunctionscpp">trunk/Source/JavaScriptCore/runtime/JSGlobalObjectFunctions.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoreruntimeJSStringh">trunk/Source/JavaScriptCore/runtime/JSString.h</a></li>
<li><a href="#trunkSourceJavaScriptCoreruntimeStringPrototypecpp">trunk/Source/JavaScriptCore/runtime/StringPrototype.cpp</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (186036 => 186037)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2015-06-27 22:52:22 UTC (rev 186036)
+++ trunk/Source/JavaScriptCore/ChangeLog        2015-06-27 22:53:12 UTC (rev 186037)
</span><span class="lines">@@ -1,3 +1,45 @@
</span><ins>+2015-06-27 Darin Adler <darin@apple.com>
+
+ Make converting JSString to StringView idiomatically safe
+ https://bugs.webkit.org/show_bug.cgi?id=146387
+
+ Reviewed by Anders Carlsson.
+
+ * jsc.cpp:
+ (functionPrint): Add explicit call to SafeView::get, needed since there
+ is no StringView temporary.
+ (functionDebug): Ditto.
+
+ * runtime/ArrayPrototype.cpp:
+ (JSC::holesMustForwardToPrototype): Refactored into helper function.
+ (JSC::join): Refactored so that StringView is a function argument, making
+ the lifetime simpler.
+ (JSC::arrayProtoFuncJoin): Ditto.
+ (JSC::arrayProtoFuncReverse): Use new holesMustForwardToPrototype helper.
+
+ * runtime/JSGlobalObjectFunctions.cpp:
+ (JSC::encode): Add explicit call to SafeView::get.
+
+ * runtime/JSString.h: Moved declarations of functions to the top of the
+ file instead of mixing them in with the function definitions. Changed
+ return type of the view function to return a JSString::SafeView so that
+ the JSString's lifetime will last as long as the StringView does in
+ typical coding idioms.
+ (JSC::JSString::getIndex): Use unsafeView so we can index into the
+ view; could also have used view.get but here in this class this seems fine.
+ (JSC::JSRopeString::unsafeView): Renamed existing view function to this.
+ (JSC::JSString::unsafeView): Ditto.
+ (JSC::JSString::SafeView::SafeView): Contains reference to an ExecState
+ and a JSString. The ExecState is needed to create the StringView, and the
+ JSString needs to be kept alive as long as the StringView is.
+ (JSC::JSString::SafeView::operator StringView): Call unsafeView.
+ (JSC::JSString::SafeView::get): Convenience for when we want to call
+ StringView member functions.
+ (JSC::JSString::view): Added. Returns a SafeView.
+
+ * runtime/StringPrototype.cpp:
+ (JSC::stringProtoFuncIndexOf): Add explicit call to SafeView::get.
+
</ins><span class="cx"> 2015-06-26 Csaba Osztrogonác <ossy@webkit.org>
</span><span class="cx">
</span><span class="cx"> Remove ARMv7Assembler.cpp
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorejsccpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/jsc.cpp (186036 => 186037)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/jsc.cpp        2015-06-27 22:52:22 UTC (rev 186036)
+++ trunk/Source/JavaScriptCore/jsc.cpp        2015-06-27 22:53:12 UTC (rev 186037)
</span><span class="lines">@@ -701,7 +701,7 @@
</span><span class="cx"> if (i)
</span><span class="cx"> putchar(' ');
</span><span class="cx">
</span><del>- printf("%s", exec->uncheckedArgument(i).toString(exec)->view(exec).utf8().data());
</del><ins>+ printf("%s", exec->uncheckedArgument(i).toString(exec)->view(exec).get().utf8().data());
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> putchar('\n');
</span><span class="lines">@@ -722,7 +722,7 @@
</span><span class="cx">
</span><span class="cx"> EncodedJSValue JSC_HOST_CALL functionDebug(ExecState* exec)
</span><span class="cx"> {
</span><del>- fprintf(stderr, "--> %s\n", exec->argument(0).toString(exec)->view(exec).utf8().data());
</del><ins>+ fprintf(stderr, "--> %s\n", exec->argument(0).toString(exec)->view(exec).get().utf8().data());
</ins><span class="cx"> return JSValue::encode(jsUndefined());
</span><span class="cx"> }
</span><span class="cx">
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreruntimeArrayPrototypecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/runtime/ArrayPrototype.cpp (186036 => 186037)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/runtime/ArrayPrototype.cpp        2015-06-27 22:52:22 UTC (rev 186036)
+++ trunk/Source/JavaScriptCore/runtime/ArrayPrototype.cpp        2015-06-27 22:53:12 UTC (rev 186037)
</span><span class="lines">@@ -40,7 +40,6 @@
</span><span class="cx"> #include "ObjectConstructor.h"
</span><span class="cx"> #include "ObjectPrototype.h"
</span><span class="cx"> #include "StringRecursionChecker.h"
</span><del>-#include "StrongInlines.h"
</del><span class="cx"> #include <algorithm>
</span><span class="cx"> #include <wtf/Assertions.h>
</span><span class="cx"> #include <wtf/HashSet.h>
</span><span class="lines">@@ -380,120 +379,126 @@
</span><span class="cx"> return false;
</span><span class="cx"> }
</span><span class="cx">
</span><del>-EncodedJSValue JSC_HOST_CALL arrayProtoFuncJoin(ExecState* exec)
</del><ins>+static inline bool holesMustForwardToPrototype(ExecState& state, JSObject* object)
</ins><span class="cx"> {
</span><del>- JSObject* thisObject = exec->thisValue().toThis(exec, StrictMode).toObject(exec);
</del><ins>+ auto& vm = state.vm();
+ return object->structure(vm)->holesMustForwardToPrototype(vm);
+}
</ins><span class="cx">
</span><del>- unsigned length = getLength(exec, thisObject);
- if (exec->hadException())
- return JSValue::encode(jsUndefined());
</del><ins>+static inline JSValue join(ExecState& state, JSObject* thisObject, StringView separator)
+{
+ unsigned length = getLength(&state, thisObject);
+ if (state.hadException())
+ return jsUndefined();
</ins><span class="cx">
</span><del>- StringRecursionChecker checker(exec, thisObject);
- if (JSValue earlyReturnValue = checker.earlyReturnValue())
- return JSValue::encode(earlyReturnValue);
-
- auto& vm = exec->vm();
-
- Strong<JSString> separatorOnHeap;
- StringView separator;
- if (exec->argument(0).isUndefined()) {
- static const LChar comma = ',';
- separator = { &comma, 1 };
- } else {
- separatorOnHeap.set(vm, exec->argument(0).toString(exec));
- if (exec->hadException())
- return JSValue::encode(jsUndefined());
- separator = separatorOnHeap->view(exec);
- }
-
</del><span class="cx"> switch (thisObject->indexingType()) {
</span><span class="cx"> case ALL_CONTIGUOUS_INDEXING_TYPES:
</span><span class="cx"> case ALL_INT32_INDEXING_TYPES: {
</span><span class="cx"> auto& butterfly = *thisObject->butterfly();
</span><span class="cx"> if (length > butterfly.publicLength())
</span><span class="cx"> break;
</span><del>- JSStringJoiner joiner(*exec, separator, length);
- if (exec->hadException())
- return JSValue::encode(jsUndefined());
</del><ins>+ JSStringJoiner joiner(state, separator, length);
+ if (state.hadException())
+ return jsUndefined();
</ins><span class="cx"> auto data = butterfly.contiguous().data();
</span><span class="cx"> bool holesKnownToBeOK = false;
</span><span class="cx"> for (unsigned i = 0; i < length; ++i) {
</span><span class="cx"> if (JSValue value = data[i].get()) {
</span><del>- joiner.append(*exec, value);
- if (exec->hadException())
- return JSValue::encode(jsUndefined());
</del><ins>+ joiner.append(state, value);
+ if (state.hadException())
+ return jsUndefined();
</ins><span class="cx"> } else {
</span><span class="cx"> if (!holesKnownToBeOK) {
</span><del>- if (thisObject->structure(vm)->holesMustForwardToPrototype(vm))
</del><ins>+ if (holesMustForwardToPrototype(state, thisObject))
</ins><span class="cx"> goto generalCase;
</span><span class="cx"> holesKnownToBeOK = true;
</span><span class="cx"> }
</span><span class="cx"> joiner.appendEmptyString();
</span><span class="cx"> }
</span><span class="cx"> }
</span><del>- return JSValue::encode(joiner.join(*exec));
</del><ins>+ return joiner.join(state);
</ins><span class="cx"> }
</span><span class="cx"> case ALL_DOUBLE_INDEXING_TYPES: {
</span><span class="cx"> auto& butterfly = *thisObject->butterfly();
</span><span class="cx"> if (length > butterfly.publicLength())
</span><span class="cx"> break;
</span><del>- JSStringJoiner joiner(*exec, separator, length);
- if (exec->hadException())
- return JSValue::encode(jsUndefined());
</del><ins>+ JSStringJoiner joiner(state, separator, length);
+ if (state.hadException())
+ return jsUndefined();
</ins><span class="cx"> auto data = butterfly.contiguousDouble().data();
</span><span class="cx"> bool holesKnownToBeOK = false;
</span><span class="cx"> for (unsigned i = 0; i < length; ++i) {
</span><span class="cx"> double value = data[i];
</span><span class="cx"> if (!isHole(value))
</span><del>- joiner.append(*exec, jsDoubleNumber(value));
</del><ins>+ joiner.append(state, jsDoubleNumber(value));
</ins><span class="cx"> else {
</span><span class="cx"> if (!holesKnownToBeOK) {
</span><del>- if (thisObject->structure(vm)->holesMustForwardToPrototype(vm))
</del><ins>+ if (thisObject->structure(state.vm())->holesMustForwardToPrototype(state.vm()))
</ins><span class="cx"> goto generalCase;
</span><span class="cx"> holesKnownToBeOK = true;
</span><span class="cx"> }
</span><span class="cx"> joiner.appendEmptyString();
</span><span class="cx"> }
</span><span class="cx"> }
</span><del>- return JSValue::encode(joiner.join(*exec));
</del><ins>+ return joiner.join(state);
</ins><span class="cx"> }
</span><span class="cx"> case ALL_ARRAY_STORAGE_INDEXING_TYPES: {
</span><span class="cx"> auto& storage = *thisObject->butterfly()->arrayStorage();
</span><span class="cx"> if (length > storage.vectorLength())
</span><span class="cx"> break;
</span><del>- if (storage.hasHoles() && thisObject->structure(vm)->holesMustForwardToPrototype(vm))
</del><ins>+ if (storage.hasHoles() && thisObject->structure(state.vm())->holesMustForwardToPrototype(state.vm()))
</ins><span class="cx"> break;
</span><del>- JSStringJoiner joiner(*exec, separator, length);
- if (exec->hadException())
- return JSValue::encode(jsUndefined());
</del><ins>+ JSStringJoiner joiner(state, separator, length);
+ if (state.hadException())
+ return jsUndefined();
</ins><span class="cx"> auto data = storage.vector().data();
</span><span class="cx"> for (unsigned i = 0; i < length; ++i) {
</span><span class="cx"> if (JSValue value = data[i].get()) {
</span><del>- joiner.append(*exec, value);
- if (exec->hadException())
- return JSValue::encode(jsUndefined());
</del><ins>+ joiner.append(state, value);
+ if (state.hadException())
+ return jsUndefined();
</ins><span class="cx"> } else
</span><span class="cx"> joiner.appendEmptyString();
</span><span class="cx"> }
</span><del>- return JSValue::encode(joiner.join(*exec));
</del><ins>+ return joiner.join(state);
</ins><span class="cx"> }
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> generalCase:
</span><del>- JSStringJoiner joiner(*exec, separator, length);
- if (exec->hadException())
- return JSValue::encode(jsUndefined());
</del><ins>+ JSStringJoiner joiner(state, separator, length);
+ if (state.hadException())
+ return jsUndefined();
</ins><span class="cx"> for (unsigned i = 0; i < length; ++i) {
</span><del>- JSValue element = thisObject->getIndex(exec, i);
- if (exec->hadException())
- return JSValue::encode(jsUndefined());
- joiner.append(*exec, element);
- if (exec->hadException())
- return JSValue::encode(jsUndefined());
</del><ins>+ JSValue element = thisObject->getIndex(&state, i);
+ if (state.hadException())
+ return jsUndefined();
+ joiner.append(state, element);
+ if (state.hadException())
+ return jsUndefined();
</ins><span class="cx"> }
</span><del>- return JSValue::encode(joiner.join(*exec));
</del><ins>+ return joiner.join(state);
</ins><span class="cx"> }
</span><span class="cx">
</span><ins>+EncodedJSValue JSC_HOST_CALL arrayProtoFuncJoin(ExecState* exec)
+{
+ JSObject* thisObject = exec->thisValue().toThis(exec, StrictMode).toObject(exec);
+
+ StringRecursionChecker checker(exec, thisObject);
+ if (JSValue earlyReturnValue = checker.earlyReturnValue())
+ return JSValue::encode(earlyReturnValue);
+
+ JSValue separatorValue = exec->argument(0);
+ if (separatorValue.isUndefined()) {
+ const LChar comma = ',';
+ return JSValue::encode(join(*exec, thisObject, { &comma, 1 }));
+ }
+
+ JSString* separator = separatorValue.toString(exec);
+ if (exec->hadException())
+ return JSValue::encode(jsUndefined());
+ return JSValue::encode(join(*exec, thisObject, separator->view(exec)));
+}
+
</ins><span class="cx"> EncodedJSValue JSC_HOST_CALL arrayProtoFuncConcat(ExecState* exec)
</span><span class="cx"> {
</span><span class="cx"> JSValue thisValue = exec->thisValue().toThis(exec, StrictMode);
</span><span class="lines">@@ -619,8 +624,6 @@
</span><span class="cx"> if (exec->hadException())
</span><span class="cx"> return JSValue::encode(jsUndefined());
</span><span class="cx">
</span><del>- auto& vm = exec->vm();
-
</del><span class="cx"> switch (thisObject->indexingType()) {
</span><span class="cx"> case ALL_CONTIGUOUS_INDEXING_TYPES:
</span><span class="cx"> case ALL_INT32_INDEXING_TYPES: {
</span><span class="lines">@@ -628,7 +631,7 @@
</span><span class="cx"> if (length > butterfly.publicLength())
</span><span class="cx"> break;
</span><span class="cx"> auto data = butterfly.contiguous().data();
</span><del>- if (containsHole(data, length) && thisObject->structure(vm)->holesMustForwardToPrototype(vm))
</del><ins>+ if (containsHole(data, length) && holesMustForwardToPrototype(*exec, thisObject))
</ins><span class="cx"> break;
</span><span class="cx"> std::reverse(data, data + length);
</span><span class="cx"> return JSValue::encode(thisObject);
</span><span class="lines">@@ -638,7 +641,7 @@
</span><span class="cx"> if (length > butterfly.publicLength())
</span><span class="cx"> break;
</span><span class="cx"> auto data = butterfly.contiguousDouble().data();
</span><del>- if (containsHole(data, length) && thisObject->structure(vm)->holesMustForwardToPrototype(vm))
</del><ins>+ if (containsHole(data, length) && holesMustForwardToPrototype(*exec, thisObject))
</ins><span class="cx"> break;
</span><span class="cx"> std::reverse(data, data + length);
</span><span class="cx"> return JSValue::encode(thisObject);
</span><span class="lines">@@ -647,7 +650,7 @@
</span><span class="cx"> auto& storage = *thisObject->butterfly()->arrayStorage();
</span><span class="cx"> if (length > storage.vectorLength())
</span><span class="cx"> break;
</span><del>- if (storage.hasHoles() && thisObject->structure(vm)->holesMustForwardToPrototype(vm))
</del><ins>+ if (storage.hasHoles() && holesMustForwardToPrototype(*exec, thisObject))
</ins><span class="cx"> break;
</span><span class="cx"> auto data = storage.vector().data();
</span><span class="cx"> std::reverse(data, data + length);
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreruntimeJSGlobalObjectFunctionscpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/runtime/JSGlobalObjectFunctions.cpp (186036 => 186037)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/runtime/JSGlobalObjectFunctions.cpp        2015-06-27 22:52:22 UTC (rev 186036)
+++ trunk/Source/JavaScriptCore/runtime/JSGlobalObjectFunctions.cpp        2015-06-27 22:53:12 UTC (rev 186037)
</span><span class="lines">@@ -64,7 +64,7 @@
</span><span class="cx">
</span><span class="cx"> static JSValue encode(ExecState* exec, const Bitmap<256>& doNotEscape)
</span><span class="cx"> {
</span><del>- CString cstr = exec->argument(0).toString(exec)->view(exec).utf8(StrictConversion);
</del><ins>+ CString cstr = exec->argument(0).toString(exec)->view(exec).get().utf8(StrictConversion);
</ins><span class="cx"> if (!cstr.data())
</span><span class="cx"> return exec->vm().throwException(exec, createURIError(exec, ASCIILiteral("String contained an illegal UTF-16 sequence.")));
</span><span class="cx">
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreruntimeJSStringh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/runtime/JSString.h (186036 => 186037)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/runtime/JSString.h        2015-06-27 22:52:22 UTC (rev 186036)
+++ trunk/Source/JavaScriptCore/runtime/JSString.h        2015-06-27 22:53:12 UTC (rev 186037)
</span><span class="lines">@@ -47,6 +47,8 @@
</span><span class="cx"> JSString* jsSingleCharacterString(ExecState*, UChar);
</span><span class="cx"> JSString* jsSubstring(VM*, const String&, unsigned offset, unsigned length);
</span><span class="cx"> JSString* jsSubstring(ExecState*, const String&, unsigned offset, unsigned length);
</span><ins>+JSString* jsSubstring8(VM*, const String&, unsigned offset, unsigned length);
+JSString* jsSubstring8(ExecState*, const String&, unsigned offset, unsigned length);
</ins><span class="cx">
</span><span class="cx"> // Non-trivial strings are two or more characters long.
</span><span class="cx"> // These functions are faster than just calling jsString.
</span><span class="lines">@@ -62,6 +64,9 @@
</span><span class="cx">
</span><span class="cx"> JSRopeString* jsStringBuilder(VM*);
</span><span class="cx">
</span><ins>+bool isJSString(JSValue);
+JSString* asString(JSValue);
+
</ins><span class="cx"> struct StringViewWithUnderlyingString {
</span><span class="cx"> StringView view;
</span><span class="cx"> String underlyingString;
</span><span class="lines">@@ -148,8 +153,11 @@
</span><span class="cx"> Identifier toIdentifier(ExecState*) const;
</span><span class="cx"> AtomicString toAtomicString(ExecState*) const;
</span><span class="cx"> RefPtr<AtomicStringImpl> toExistingAtomicString(ExecState*) const;
</span><del>- StringView view(ExecState*) const;
</del><ins>+
+ class SafeView;
+ SafeView view(ExecState*) const;
</ins><span class="cx"> StringViewWithUnderlyingString viewWithUnderlyingString(ExecState&) const;
</span><ins>+
</ins><span class="cx"> const String& value(ExecState*) const;
</span><span class="cx"> const String& tryGetValue() const;
</span><span class="cx"> const StringImpl* tryGetValueImpl() const;
</span><span class="lines">@@ -220,6 +228,7 @@
</span><span class="cx"> static JSValue toThis(JSCell*, ExecState*, ECMAMode);
</span><span class="cx">
</span><span class="cx"> String& string() { ASSERT(!isRope()); return m_value; }
</span><ins>+ StringView unsafeView(ExecState&) const;
</ins><span class="cx">
</span><span class="cx"> friend JSValue jsString(ExecState*, JSString*, JSString*);
</span><span class="cx"> friend JSString* jsSubstring(ExecState*, JSString*, unsigned offset, unsigned length);
</span><span class="lines">@@ -389,7 +398,7 @@
</span><span class="cx"> void resolveRopeInternal16(UChar*) const;
</span><span class="cx"> void resolveRopeInternal16NoSubstring(UChar*) const;
</span><span class="cx"> void clearFibers() const;
</span><del>- StringView view(ExecState*) const;
</del><ins>+ StringView unsafeView(ExecState&) const;
</ins><span class="cx"> StringViewWithUnderlyingString viewWithUnderlyingString(ExecState&) const;
</span><span class="cx">
</span><span class="cx"> WriteBarrierBase<JSString>& fiber(unsigned i) const
</span><span class="lines">@@ -435,14 +444,30 @@
</span><span class="cx"> } u[s_maxInternalRopeLength];
</span><span class="cx"> };
</span><span class="cx">
</span><ins>+class JSString::SafeView {
+public:
+ SafeView();
+ explicit SafeView(ExecState&, const JSString&);
+ operator StringView() const;
+ StringView get() const;
</ins><span class="cx">
</span><ins>+private:
+ ExecState* m_state { nullptr };
+
+ // The following pointer is marked "volatile" to make the compiler leave it on the stack
+ // or in a register as long as this object is alive, even after the last use of the pointer.
+ // That's needed to prevent garbage collecting the string and possibly deleting the block
+ // with the characters in it, and then using the StringView after that.
+ const JSString* volatile m_string { nullptr };
+};
+
+JS_EXPORT_PRIVATE JSString* jsStringWithCacheSlowCase(VM&, StringImpl&);
+
</ins><span class="cx"> inline const StringImpl* JSString::tryGetValueImpl() const
</span><span class="cx"> {
</span><span class="cx"> return m_value.impl();
</span><span class="cx"> }
</span><span class="cx">
</span><del>-JSString* asString(JSValue);
-
</del><span class="cx"> inline JSString* asString(JSValue value)
</span><span class="cx"> {
</span><span class="cx"> ASSERT(value.asCell()->isString());
</span><span class="lines">@@ -511,7 +536,7 @@
</span><span class="cx"> inline JSString* JSString::getIndex(ExecState* exec, unsigned i)
</span><span class="cx"> {
</span><span class="cx"> ASSERT(canGetIndex(i));
</span><del>- return jsSingleCharacterString(exec, view(exec)[i]);
</del><ins>+ return jsSingleCharacterString(exec, unsafeView(*exec)[i]);
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> inline JSString* jsString(VM* vm, const String& s)
</span><span class="lines">@@ -597,8 +622,6 @@
</span><span class="cx"> inline JSString* jsNontrivialString(ExecState* exec, String&& s) { return jsNontrivialString(&exec->vm(), WTF::move(s)); }
</span><span class="cx"> inline JSString* jsOwnedString(ExecState* exec, const String& s) { return jsOwnedString(&exec->vm(), s); }
</span><span class="cx">
</span><del>-JS_EXPORT_PRIVATE JSString* jsStringWithCacheSlowCase(VM&, StringImpl&);
-
</del><span class="cx"> ALWAYS_INLINE JSString* jsStringWithCache(ExecState* exec, const String& s)
</span><span class="cx"> {
</span><span class="cx"> VM& vm = exec->vm();
</span><span class="lines">@@ -651,44 +674,20 @@
</span><span class="cx"> return false;
</span><span class="cx"> }
</span><span class="cx">
</span><del>-inline bool isJSString(JSValue v) { return v.isCell() && v.asCell()->type() == StringType; }
-
-// --- JSValue inlines ----------------------------
-
-inline bool JSValue::toBoolean(ExecState* exec) const
</del><ins>+inline bool isJSString(JSValue v)
</ins><span class="cx"> {
</span><del>- if (isInt32())
- return asInt32();
- if (isDouble())
- return asDouble() > 0.0 || asDouble() < 0.0; // false for NaN
- if (isCell())
- return asCell()->toBoolean(exec);
- return isTrue(); // false, null, and undefined all convert to false.
</del><ins>+ return v.isCell() && v.asCell()->type() == StringType;
</ins><span class="cx"> }
</span><span class="cx">
</span><del>-inline JSString* JSValue::toString(ExecState* exec) const
</del><ins>+ALWAYS_INLINE StringView JSRopeString::unsafeView(ExecState& state) const
</ins><span class="cx"> {
</span><del>- if (isString())
- return jsCast<JSString*>(asCell());
- return toStringSlowCase(exec);
-}
-
-inline String JSValue::toWTFString(ExecState* exec) const
-{
- if (isString())
- return static_cast<JSString*>(asCell())->value(exec);
- return toWTFStringSlowCase(exec);
-}
-
-ALWAYS_INLINE StringView JSRopeString::view(ExecState* exec) const
-{
</del><span class="cx"> if (isSubstring()) {
</span><span class="cx"> if (is8Bit())
</span><span class="cx"> return StringView(substringBase()->m_value.characters8() + substringOffset(), m_length);
</span><span class="cx"> return StringView(substringBase()->m_value.characters16() + substringOffset(), m_length);
</span><span class="cx"> }
</span><del>- resolveRope(exec);
- return StringView(m_value);
</del><ins>+ resolveRope(&state);
+ return m_value;
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> ALWAYS_INLINE StringViewWithUnderlyingString JSRopeString::viewWithUnderlyingString(ExecState& state) const
</span><span class="lines">@@ -703,11 +702,11 @@
</span><span class="cx"> return { m_value, m_value };
</span><span class="cx"> }
</span><span class="cx">
</span><del>-ALWAYS_INLINE StringView JSString::view(ExecState* exec) const
</del><ins>+ALWAYS_INLINE StringView JSString::unsafeView(ExecState& state) const
</ins><span class="cx"> {
</span><span class="cx"> if (isRope())
</span><del>- return static_cast<const JSRopeString*>(this)->view(exec);
- return StringView(m_value);
</del><ins>+ return static_cast<const JSRopeString*>(this)->unsafeView(state);
+ return m_value;
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> ALWAYS_INLINE StringViewWithUnderlyingString JSString::viewWithUnderlyingString(ExecState& state) const
</span><span class="lines">@@ -722,6 +721,58 @@
</span><span class="cx"> return isRope() && static_cast<const JSRopeString*>(this)->isSubstring();
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+inline JSString::SafeView::SafeView()
+{
+}
+
+inline JSString::SafeView::SafeView(ExecState& state, const JSString& string)
+ : m_state(&state)
+ , m_string(&string)
+{
+}
+
+inline JSString::SafeView::operator StringView() const
+{
+ return m_string->unsafeView(*m_state);
+}
+
+inline StringView JSString::SafeView::get() const
+{
+ return *this;
+}
+
+ALWAYS_INLINE JSString::SafeView JSString::view(ExecState* exec) const
+{
+ return SafeView(*exec, *this);
+}
+
+// --- JSValue inlines ----------------------------
+
+inline bool JSValue::toBoolean(ExecState* exec) const
+{
+ if (isInt32())
+ return asInt32();
+ if (isDouble())
+ return asDouble() > 0.0 || asDouble() < 0.0; // false for NaN
+ if (isCell())
+ return asCell()->toBoolean(exec);
+ return isTrue(); // false, null, and undefined all convert to false.
+}
+
+inline JSString* JSValue::toString(ExecState* exec) const
+{
+ if (isString())
+ return jsCast<JSString*>(asCell());
+ return toStringSlowCase(exec);
+}
+
+inline String JSValue::toWTFString(ExecState* exec) const
+{
+ if (isString())
+ return static_cast<JSString*>(asCell())->value(exec);
+ return toWTFStringSlowCase(exec);
+}
+
</ins><span class="cx"> } // namespace JSC
</span><span class="cx">
</span><span class="cx"> #endif // JSString_h
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreruntimeStringPrototypecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/runtime/StringPrototype.cpp (186036 => 186037)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/runtime/StringPrototype.cpp        2015-06-27 22:52:22 UTC (rev 186036)
+++ trunk/Source/JavaScriptCore/runtime/StringPrototype.cpp        2015-06-27 22:53:12 UTC (rev 186037)
</span><span class="lines">@@ -896,7 +896,7 @@
</span><span class="cx"> if (thisJSString->length() < otherJSString->length() + pos)
</span><span class="cx"> return JSValue::encode(jsNumber(-1));
</span><span class="cx">
</span><del>- size_t result = thisJSString->view(exec).find(otherJSString->view(exec), pos);
</del><ins>+ size_t result = thisJSString->view(exec).get().find(otherJSString->view(exec), pos);
</ins><span class="cx"> if (result == notFound)
</span><span class="cx"> return JSValue::encode(jsNumber(-1));
</span><span class="cx"> return JSValue::encode(jsNumber(result));
</span></span></pre>
</div>
</div>
</body>
</html>