<!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>[171390] 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/171390">171390</a></dd>
<dt>Author</dt> <dd>mark.lam@apple.com</dd>
<dt>Date</dt> <dd>2014-07-22 21:18:35 -0700 (Tue, 22 Jul 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Array.concat() should work on runtime arrays too.
&lt;https://webkit.org/b/135179&gt;

Reviewed by Geoffrey Garen.

* jsc.cpp:
(WTF::RuntimeArray::create):
(WTF::RuntimeArray::~RuntimeArray):
(WTF::RuntimeArray::destroy):
(WTF::RuntimeArray::getOwnPropertySlot):
(WTF::RuntimeArray::getOwnPropertySlotByIndex):
(WTF::RuntimeArray::put):
(WTF::RuntimeArray::deleteProperty):
(WTF::RuntimeArray::getLength):
(WTF::RuntimeArray::createPrototype):
(WTF::RuntimeArray::createStructure):
(WTF::RuntimeArray::finishCreation):
(WTF::RuntimeArray::RuntimeArray):
(WTF::RuntimeArray::lengthGetter):
(GlobalObject::finishCreation):
(functionCreateRuntimeArray):
- Added support to create a runtime array for testing purpose.
* runtime/ArrayPrototype.cpp:
(JSC::getLength):
- Added fast case for when the array object is a JSArray.
(JSC::arrayProtoFuncJoin):
- Added a needed but missing exception check.
(JSC::arrayProtoFuncConcat):
- Use getLength() to compute the array length instead of assuming that
  the array is a JSArray instance.
* tests/stress/regexp-matches-array.js: Added.
(testArrayConcat):
* tests/stress/runtime-array.js: Added.
(testArrayConcat):</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>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoretestsstressregexpmatchesarrayjs">trunk/Source/JavaScriptCore/tests/stress/regexp-matches-array.js</a></li>
<li><a href="#trunkSourceJavaScriptCoretestsstressruntimearrayjs">trunk/Source/JavaScriptCore/tests/stress/runtime-array.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (171389 => 171390)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2014-07-23 04:04:33 UTC (rev 171389)
+++ trunk/Source/JavaScriptCore/ChangeLog        2014-07-23 04:18:35 UTC (rev 171390)
</span><span class="lines">@@ -1,3 +1,40 @@
</span><ins>+2014-07-22  Mark Lam  &lt;mark.lam@apple.com&gt;
+
+        Array.concat() should work on runtime arrays too.
+        &lt;https://webkit.org/b/135179&gt;
+
+        Reviewed by Geoffrey Garen.
+
+        * jsc.cpp:
+        (WTF::RuntimeArray::create):
+        (WTF::RuntimeArray::~RuntimeArray):
+        (WTF::RuntimeArray::destroy):
+        (WTF::RuntimeArray::getOwnPropertySlot):
+        (WTF::RuntimeArray::getOwnPropertySlotByIndex):
+        (WTF::RuntimeArray::put):
+        (WTF::RuntimeArray::deleteProperty):
+        (WTF::RuntimeArray::getLength):
+        (WTF::RuntimeArray::createPrototype):
+        (WTF::RuntimeArray::createStructure):
+        (WTF::RuntimeArray::finishCreation):
+        (WTF::RuntimeArray::RuntimeArray):
+        (WTF::RuntimeArray::lengthGetter):
+        (GlobalObject::finishCreation):
+        (functionCreateRuntimeArray):
+        - Added support to create a runtime array for testing purpose.
+        * runtime/ArrayPrototype.cpp:
+        (JSC::getLength):
+        - Added fast case for when the array object is a JSArray.
+        (JSC::arrayProtoFuncJoin):
+        - Added a needed but missing exception check.
+        (JSC::arrayProtoFuncConcat):
+        - Use getLength() to compute the array length instead of assuming that
+          the array is a JSArray instance.
+        * tests/stress/regexp-matches-array.js: Added.
+        (testArrayConcat):
+        * tests/stress/runtime-array.js: Added.
+        (testArrayConcat):
+
</ins><span class="cx"> 2014-07-22  Brent Fulgham  &lt;bfulgham@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Fix Windows (return a value!)
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorejsccpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/jsc.cpp (171389 => 171390)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/jsc.cpp        2014-07-23 04:04:33 UTC (rev 171389)
+++ trunk/Source/JavaScriptCore/jsc.cpp        2014-07-23 04:18:35 UTC (rev 171390)
</span><span class="lines">@@ -22,6 +22,7 @@
</span><span class="cx"> 
</span><span class="cx"> #include &quot;config.h&quot;
</span><span class="cx"> 
</span><ins>+#include &quot;ArrayPrototype.h&quot;
</ins><span class="cx"> #include &quot;ButterflyInlines.h&quot;
</span><span class="cx"> #include &quot;BytecodeGenerator.h&quot;
</span><span class="cx"> #include &quot;Completion.h&quot;
</span><span class="lines">@@ -114,6 +115,7 @@
</span><span class="cx"> class ElementHandleOwner;
</span><span class="cx"> class Masuqerader;
</span><span class="cx"> class Root;
</span><ins>+class RuntimeArray;
</ins><span class="cx"> 
</span><span class="cx"> class Element : public JSNonFinalObject {
</span><span class="cx"> public:
</span><span class="lines">@@ -293,10 +295,117 @@
</span><span class="cx">     WriteBarrier&lt;JSObject&gt; m_delegate;
</span><span class="cx"> };
</span><span class="cx"> 
</span><ins>+class RuntimeArray : public JSArray {
+public:
+    typedef JSArray Base;
+
+    static RuntimeArray* create(ExecState* exec)
+    {
+        VM&amp; vm = exec-&gt;vm();
+        JSGlobalObject* globalObject = exec-&gt;lexicalGlobalObject();
+        Structure* structure = createStructure(vm, globalObject, createPrototype(vm, globalObject));
+        RuntimeArray* runtimeArray = new (NotNull, allocateCell&lt;RuntimeArray&gt;(*exec-&gt;heap())) RuntimeArray(exec, structure);
+        runtimeArray-&gt;finishCreation(exec);
+        vm.heap.addFinalizer(runtimeArray, destroy);
+        return runtimeArray;
+    }
+
+    ~RuntimeArray() { }
+
+    static void destroy(JSCell* cell)
+    {
+        static_cast&lt;RuntimeArray*&gt;(cell)-&gt;RuntimeArray::~RuntimeArray();
+    }
+
+    static const bool needsDestruction = false;
+
+    static bool getOwnPropertySlot(JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot&amp; slot)
+    {
+        RuntimeArray* thisObject = jsCast&lt;RuntimeArray*&gt;(object);
+        if (propertyName == exec-&gt;propertyNames().length) {
+            slot.setCacheableCustom(thisObject, DontDelete | ReadOnly | DontEnum, thisObject-&gt;lengthGetter);
+            return true;
+        }
+
+        unsigned index = propertyName.asIndex();
+        if (index &lt; thisObject-&gt;getLength()) {
+            ASSERT(index != PropertyName::NotAnIndex);
+            slot.setValue(thisObject, DontDelete | DontEnum, jsNumber(thisObject-&gt;m_vector[index]));
+            return true;
+        }
+
+        return JSObject::getOwnPropertySlot(thisObject, exec, propertyName, slot);
+    }
+
+    static bool getOwnPropertySlotByIndex(JSObject* object, ExecState* exec, unsigned index, PropertySlot&amp; slot)
+    {
+        RuntimeArray* thisObject = jsCast&lt;RuntimeArray*&gt;(object);
+        if (index &lt; thisObject-&gt;getLength()) {
+            slot.setValue(thisObject, DontDelete | DontEnum, jsNumber(thisObject-&gt;m_vector[index]));
+            return true;
+        }
+
+        return JSObject::getOwnPropertySlotByIndex(thisObject, exec, index, slot);
+    }
+
+    static NO_RETURN_DUE_TO_CRASH void put(JSCell*, ExecState*, PropertyName, JSValue, PutPropertySlot&amp;)
+    {
+        RELEASE_ASSERT_NOT_REACHED();
+    }
+
+    static NO_RETURN_DUE_TO_CRASH bool deleteProperty(JSCell*, ExecState*, PropertyName)
+    {
+        RELEASE_ASSERT_NOT_REACHED();
+    }
+
+    unsigned getLength() const { return m_vector.size(); }
+
+    DECLARE_INFO;
+
+    static ArrayPrototype* createPrototype(VM&amp;, JSGlobalObject* globalObject)
+    {
+        return globalObject-&gt;arrayPrototype();
+    }
+
+    static Structure* createStructure(VM&amp; vm, JSGlobalObject* globalObject, JSValue prototype)
+    {
+        return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info(), ArrayClass);
+    }
+
+protected:
+    void finishCreation(ExecState* exec)
+    {
+        Base::finishCreation(exec-&gt;vm());
+        ASSERT(inherits(info()));
+
+        for (size_t i = 0; i &lt; exec-&gt;argumentCount(); i++)
+            m_vector.append(exec-&gt;argument(i).toInt32(exec));
+    }
+
+    static const unsigned StructureFlags = OverridesGetOwnPropertySlot | InterceptsGetOwnPropertySlotByIndexEvenWhenLengthIsNotZero | OverridesGetPropertyNames | JSArray::StructureFlags;
+
+private:
+    RuntimeArray(ExecState* exec, Structure* structure)
+        : JSArray(exec-&gt;vm(), structure, 0)
+    {
+    }
+
+    static EncodedJSValue lengthGetter(ExecState* exec, JSObject*, EncodedJSValue thisValue, PropertyName)
+    {
+        RuntimeArray* thisObject = jsDynamicCast&lt;RuntimeArray*&gt;(JSValue::decode(thisValue));
+        if (!thisObject)
+            return throwVMTypeError(exec);
+        return JSValue::encode(jsNumber(thisObject-&gt;getLength()));
+    }
+
+    Vector&lt;int&gt; m_vector;
+};
+
</ins><span class="cx"> const ClassInfo Element::s_info = { &quot;Element&quot;, &amp;Base::s_info, 0, 0, CREATE_METHOD_TABLE(Element) };
</span><span class="cx"> const ClassInfo Masquerader::s_info = { &quot;Masquerader&quot;, &amp;Base::s_info, 0, 0, CREATE_METHOD_TABLE(Masquerader) };
</span><span class="cx"> const ClassInfo Root::s_info = { &quot;Root&quot;, &amp;Base::s_info, 0, 0, CREATE_METHOD_TABLE(Root) };
</span><span class="cx"> const ClassInfo ImpureGetter::s_info = { &quot;ImpureGetter&quot;, &amp;Base::s_info, 0, 0, CREATE_METHOD_TABLE(ImpureGetter) };
</span><ins>+const ClassInfo RuntimeArray::s_info = { &quot;RuntimeArray&quot;, &amp;Base::s_info, 0, 0, CREATE_METHOD_TABLE(RuntimeArray) };
</ins><span class="cx"> 
</span><span class="cx"> ElementHandleOwner* Element::handleOwner()
</span><span class="cx"> {
</span><span class="lines">@@ -317,6 +426,7 @@
</span><span class="cx"> static bool fillBufferWithContentsOfFile(const String&amp; fileName, Vector&lt;char&gt;&amp; buffer);
</span><span class="cx"> 
</span><span class="cx"> static EncodedJSValue JSC_HOST_CALL functionCreateProxy(ExecState*);
</span><ins>+static EncodedJSValue JSC_HOST_CALL functionCreateRuntimeArray(ExecState*);
</ins><span class="cx"> static EncodedJSValue JSC_HOST_CALL functionCreateImpureGetter(ExecState*);
</span><span class="cx"> static EncodedJSValue JSC_HOST_CALL functionSetImpureGetterDelegate(ExecState*);
</span><span class="cx"> 
</span><span class="lines">@@ -502,6 +612,7 @@
</span><span class="cx">         addFunction(vm, &quot;makeMasquerader&quot;, functionMakeMasquerader, 0);
</span><span class="cx"> 
</span><span class="cx">         addFunction(vm, &quot;createProxy&quot;, functionCreateProxy, 1);
</span><ins>+        addFunction(vm, &quot;createRuntimeArray&quot;, functionCreateRuntimeArray, 0);
</ins><span class="cx"> 
</span><span class="cx">         addFunction(vm, &quot;createImpureGetter&quot;, functionCreateImpureGetter, 1);
</span><span class="cx">         addFunction(vm, &quot;setImpureGetterDelegate&quot;, functionSetImpureGetterDelegate, 2);
</span><span class="lines">@@ -675,6 +786,13 @@
</span><span class="cx">     return JSValue::encode(proxy);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+EncodedJSValue JSC_HOST_CALL functionCreateRuntimeArray(ExecState* exec)
+{
+    JSLockHolder lock(exec);
+    RuntimeArray* array = RuntimeArray::create(exec);
+    return JSValue::encode(array);
+}
+
</ins><span class="cx"> EncodedJSValue JSC_HOST_CALL functionCreateImpureGetter(ExecState* exec)
</span><span class="cx"> {
</span><span class="cx">     JSLockHolder lock(exec);
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreruntimeArrayPrototypecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/runtime/ArrayPrototype.cpp (171389 => 171390)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/runtime/ArrayPrototype.cpp        2014-07-23 04:04:33 UTC (rev 171389)
+++ trunk/Source/JavaScriptCore/runtime/ArrayPrototype.cpp        2014-07-23 04:18:35 UTC (rev 171390)
</span><span class="lines">@@ -159,6 +159,8 @@
</span><span class="cx"> 
</span><span class="cx"> static ALWAYS_INLINE unsigned getLength(ExecState* exec, JSObject* obj)
</span><span class="cx"> {
</span><ins>+    if (isJSArray(obj))
+        return jsCast&lt;JSArray*&gt;(obj)-&gt;length();
</ins><span class="cx">     return obj-&gt;get(exec, exec-&gt;propertyNames().length).toUInt32(exec);
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -412,6 +414,8 @@
</span><span class="cx"> 
</span><span class="cx">     for (; k &lt; length; k++) {
</span><span class="cx">         JSValue element = thisObj-&gt;get(exec, k);
</span><ins>+        if (exec-&gt;hadException())
+            return JSValue::encode(jsUndefined());
</ins><span class="cx">         if (!element.isUndefinedOrNull())
</span><span class="cx">             stringJoiner.append(element.toWTFStringInline(exec));
</span><span class="cx">         else
</span><span class="lines">@@ -429,9 +433,11 @@
</span><span class="cx">     Checked&lt;unsigned, RecordOverflow&gt; finalArraySize = 0;
</span><span class="cx"> 
</span><span class="cx">     for (size_t i = 0;;) {
</span><del>-        if (JSArray* currentArray = jsDynamicCast&lt;JSArray*&gt;(curArg))
-            finalArraySize += currentArray-&gt;length();
-        else
</del><ins>+        if (JSArray* currentArray = jsDynamicCast&lt;JSArray*&gt;(curArg)) {
+            finalArraySize += getLength(exec, currentArray);
+            if (exec-&gt;hadException())
+                return JSValue::encode(jsUndefined());
+        } else
</ins><span class="cx">             finalArraySize++;
</span><span class="cx">         if (i == argCount)
</span><span class="cx">             break;
</span><span class="lines">@@ -450,7 +456,9 @@
</span><span class="cx">     unsigned n = 0;
</span><span class="cx">     for (size_t i = 0;;) {
</span><span class="cx">         if (JSArray* currentArray = jsDynamicCast&lt;JSArray*&gt;(curArg)) {
</span><del>-            unsigned length = currentArray-&gt;length();
</del><ins>+            unsigned length = getLength(exec, currentArray);
+            if (exec-&gt;hadException())
+                return JSValue::encode(jsUndefined());
</ins><span class="cx">             for (unsigned k = 0; k &lt; length; ++k) {
</span><span class="cx">                 JSValue v = getProperty(exec, currentArray, k);
</span><span class="cx">                 if (exec-&gt;hadException())
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoretestsstressregexpmatchesarrayjs"></a>
<div class="addfile"><h4>Added: trunk/Source/JavaScriptCore/tests/stress/regexp-matches-array.js (0 => 171390)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/tests/stress/regexp-matches-array.js                                (rev 0)
+++ trunk/Source/JavaScriptCore/tests/stress/regexp-matches-array.js        2014-07-23 04:18:35 UTC (rev 171390)
</span><span class="lines">@@ -0,0 +1,14 @@
</span><ins>+function testArrayConcat() {
+    var array = 'abc'.match(/(a)(b)(c)/);
+    var result = array.concat();
+    var expectedResult = [&quot;abc&quot;, &quot;a&quot;, &quot;b&quot;, &quot;c&quot;];
+
+    if (result.length != 4)
+        throw new Error(&quot;Runtime array length is incorrect&quot;);
+    for (var i = 0; i &lt; result.length; i++) {
+        if (result[i] != expectedResult[i])
+            throw new Error(&quot;Runtime array concat result is incorrect&quot;);
+    }
+};
+
+testArrayConcat();
</ins></span></pre></div>
<a id="trunkSourceJavaScriptCoretestsstressruntimearrayjs"></a>
<div class="addfile"><h4>Added: trunk/Source/JavaScriptCore/tests/stress/runtime-array.js (0 => 171390)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/tests/stress/runtime-array.js                                (rev 0)
+++ trunk/Source/JavaScriptCore/tests/stress/runtime-array.js        2014-07-23 04:18:35 UTC (rev 171390)
</span><span class="lines">@@ -0,0 +1,13 @@
</span><ins>+function testArrayConcat() {
+    var array = createRuntimeArray(1, 2, 3);
+    var result = array.concat();
+
+    if (result.length != 3)
+        throw new Error(&quot;Runtime array length is incorrect&quot;);
+    for (var i = 0; i &lt; result.length; i++) {
+        if (result[i] != i + 1)
+            throw new Error(&quot;Runtime array concat result is incorrect&quot;);
+    }
+};
+
+testArrayConcat();
</ins></span></pre>
</div>
</div>

</body>
</html>