<!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>[205880] trunk</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/205880">205880</a></dd>
<dt>Author</dt> <dd>jfbastien@apple.com</dd>
<dt>Date</dt> <dd>2016-09-13 14:53:11 -0700 (Tue, 13 Sep 2016)</dd>
</dl>

<h3>Log Message</h3>
<pre>Support jsc shell builtin `read`
https://bugs.webkit.org/show_bug.cgi?id=161662

Reviewed by Keith Miller.

JSTests:

* stress/jsc-read.js: Added.
(test): test `read` and `readFile` shell builtins, in string and binary mode.

Source/JavaScriptCore:

The jsc shell currently supports a `readFile` method which returns
a string. SpiderMonkey's js shell and V8's d8 shell both support
similar file-to-string functions, as well as a
binary-file-to-Uint8Array function. jsc should support a similar
binary file method to simplify testing, including testing of
WebAssembly blobs.

Emscripten's shell.js (which is also used for some WebAssembly
things) has a polyfill [1] for a builtin called `read`. jsc should
therefore have a builtin with the same name if we want things to
&quot;Just Work&quot;.

  [1]: https://github.com/kripken/emscripten/blob/5f0918409a1407dd168f57cfa34b109cd1770a8a/src/shell.js#L138

* jsc.cpp:
(GlobalObject::finishCreation): add `read`, make `readFile` take up to 2 arguments.
(functionReadFile): support binary files, as per SpiderMonkey.
* runtime/Error.h:
(JSC::throwVMError): convenience function, I'll add more uses in a follow-up
* runtime/JSTypedArrays.cpp:
(JSC::createUint8TypedArray): JS private export of JSUint8Array::create.
* runtime/JSTypedArrays.h: expose private export.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkJSTestsChangeLog">trunk/JSTests/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCorejsccpp">trunk/Source/JavaScriptCore/jsc.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoreruntimeErrorh">trunk/Source/JavaScriptCore/runtime/Error.h</a></li>
<li><a href="#trunkSourceJavaScriptCoreruntimeJSTypedArrayscpp">trunk/Source/JavaScriptCore/runtime/JSTypedArrays.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoreruntimeJSTypedArraysh">trunk/Source/JavaScriptCore/runtime/JSTypedArrays.h</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkJSTestsstressjscreadjs">trunk/JSTests/stress/jsc-read.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkJSTestsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/JSTests/ChangeLog (205879 => 205880)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/ChangeLog        2016-09-13 21:25:39 UTC (rev 205879)
+++ trunk/JSTests/ChangeLog        2016-09-13 21:53:11 UTC (rev 205880)
</span><span class="lines">@@ -1,3 +1,13 @@
</span><ins>+2016-09-13  JF Bastien  &lt;jfbastien@apple.com&gt;
+
+        Support jsc shell builtin `read`
+        https://bugs.webkit.org/show_bug.cgi?id=161662
+
+        Reviewed by Keith Miller.
+
+        * stress/jsc-read.js: Added.
+        (test): test `read` and `readFile` shell builtins, in string and binary mode.
+
</ins><span class="cx"> 2016-09-12  Skachkov Oleksandr  &lt;gskachkov@gmail.com&gt;
</span><span class="cx"> 
</span><span class="cx">         ES6: Classes: Should be allowed to create a static method with name &quot;arguments&quot;
</span></span></pre></div>
<a id="trunkJSTestsstressjscreadjs"></a>
<div class="addfile"><h4>Added: trunk/JSTests/stress/jsc-read.js (0 => 205880)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/stress/jsc-read.js                                (rev 0)
+++ trunk/JSTests/stress/jsc-read.js        2016-09-13 21:53:11 UTC (rev 205880)
</span><span class="lines">@@ -0,0 +1,36 @@
</span><ins>+(function test() {
+    // Read this test file using jsc shell's builtins, and check that its content is as expected.
+    const in_file = 'jsc-read.js';
+
+    const check = content_read =&gt; {
+        let expect = '(' + test.toString() + ')();\n';
+        if (content_read !== expect)
+            throw Error('Expected to read this file as-is, instead read:\n==========\n' + content_read + '\n==========');
+    };
+
+    const test_arraybuffer = read_function =&gt; {
+        let file = read_function(in_file, 'binary');
+        if (typeof file.buffer !== 'object' || file.byteLength === undefined || file.length === undefined || file.BYTES_PER_ELEMENT !== 1 || file.byteOffset !== 0)
+            throw Error('Expected a Uint8Array');
+        let str = '';
+        for (var i = 0; i != file.length; ++i)
+            str += String.fromCharCode(file[i]);  // Assume ASCII.
+        check(str);
+    };
+
+    const test_string = read_function =&gt; {
+        let str = read_function(in_file);
+        if (typeof str !== 'string')
+            throw Error('Expected a string');
+        check(str);
+    };
+
+    // jsc's original file reading function is `readFile`, whereas SpiderMonkey
+    // shell's file reading function is `read`. The latter is used by
+    // emscripten's shell.js (d8 calls it `readbuffer`, which shell.js
+    // polyfills).
+    test_arraybuffer(readFile);
+    test_arraybuffer(read);
+    test_string(readFile);
+    test_string(read);
+})();
</ins></span></pre></div>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (205879 => 205880)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2016-09-13 21:25:39 UTC (rev 205879)
+++ trunk/Source/JavaScriptCore/ChangeLog        2016-09-13 21:53:11 UTC (rev 205880)
</span><span class="lines">@@ -1,3 +1,33 @@
</span><ins>+2016-09-13  JF Bastien  &lt;jfbastien@apple.com&gt;
+
+        Support jsc shell builtin `read`
+        https://bugs.webkit.org/show_bug.cgi?id=161662
+
+        Reviewed by Keith Miller.
+
+        The jsc shell currently supports a `readFile` method which returns
+        a string. SpiderMonkey's js shell and V8's d8 shell both support
+        similar file-to-string functions, as well as a
+        binary-file-to-Uint8Array function. jsc should support a similar
+        binary file method to simplify testing, including testing of
+        WebAssembly blobs.
+
+        Emscripten's shell.js (which is also used for some WebAssembly
+        things) has a polyfill [1] for a builtin called `read`. jsc should
+        therefore have a builtin with the same name if we want things to
+        &quot;Just Work&quot;.
+
+          [1]: https://github.com/kripken/emscripten/blob/5f0918409a1407dd168f57cfa34b109cd1770a8a/src/shell.js#L138
+
+        * jsc.cpp:
+        (GlobalObject::finishCreation): add `read`, make `readFile` take up to 2 arguments.
+        (functionReadFile): support binary files, as per SpiderMonkey.
+        * runtime/Error.h:
+        (JSC::throwVMError): convenience function, I'll add more uses in a follow-up
+        * runtime/JSTypedArrays.cpp:
+        (JSC::createUint8TypedArray): JS private export of JSUint8Array::create.
+        * runtime/JSTypedArrays.h: expose private export.
+
</ins><span class="cx"> 2016-09-12  Skachkov Oleksandr  &lt;gskachkov@gmail.com&gt;
</span><span class="cx"> 
</span><span class="cx">         ES6: Classes: Should be allowed to create a static method with name &quot;arguments&quot;
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorejsccpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/jsc.cpp (205879 => 205880)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/jsc.cpp        2016-09-13 21:25:39 UTC (rev 205879)
+++ trunk/Source/JavaScriptCore/jsc.cpp        2016-09-13 21:53:11 UTC (rev 205880)
</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;ArrayBuffer.h&quot;
</ins><span class="cx"> #include &quot;ArrayPrototype.h&quot;
</span><span class="cx"> #include &quot;BuiltinExecutableCreator.h&quot;
</span><span class="cx"> #include &quot;ButterflyInlines.h&quot;
</span><span class="lines">@@ -48,6 +49,7 @@
</span><span class="cx"> #include &quot;JSONObject.h&quot;
</span><span class="cx"> #include &quot;JSProxy.h&quot;
</span><span class="cx"> #include &quot;JSString.h&quot;
</span><ins>+#include &quot;JSTypedArrays.h&quot;
</ins><span class="cx"> #include &quot;JSWASMModule.h&quot;
</span><span class="cx"> #include &quot;LLIntData.h&quot;
</span><span class="cx"> #include &quot;ParserError.h&quot;
</span><span class="lines">@@ -803,7 +805,8 @@
</span><span class="cx">         addFunction(vm, &quot;runString&quot;, functionRunString, 1);
</span><span class="cx">         addFunction(vm, &quot;load&quot;, functionLoad, 1);
</span><span class="cx">         addFunction(vm, &quot;loadString&quot;, functionLoadString, 1);
</span><del>-        addFunction(vm, &quot;readFile&quot;, functionReadFile, 1);
</del><ins>+        addFunction(vm, &quot;readFile&quot;, functionReadFile, 2);
+        addFunction(vm, &quot;read&quot;, functionReadFile, 2);
</ins><span class="cx">         addFunction(vm, &quot;checkSyntax&quot;, functionCheckSyntax, 1);
</span><span class="cx">         addFunction(vm, &quot;jscStack&quot;, functionJSCStack, 1);
</span><span class="cx">         addFunction(vm, &quot;readline&quot;, functionReadline, 0);
</span><span class="lines">@@ -1555,11 +1558,31 @@
</span><span class="cx">     String fileName = exec-&gt;argument(0).toWTFString(exec);
</span><span class="cx">     if (UNLIKELY(scope.exception()))
</span><span class="cx">         return JSValue::encode(jsUndefined());
</span><del>-    Vector&lt;char&gt; script;
-    if (!fillBufferWithContentsOfFile(fileName, script))
-        return JSValue::encode(throwException(exec, scope, createError(exec, ASCIILiteral(&quot;Could not open file.&quot;))));
</del><span class="cx"> 
</span><del>-    return JSValue::encode(jsString(exec, stringFromUTF(script)));
</del><ins>+    bool isBinary = false;
+    if (exec-&gt;argumentCount() &gt; 1) {
+        String type = exec-&gt;argument(1).toWTFString(exec);
+        if (UNLIKELY(scope.exception()))
+            return EncodedJSValue();
+        if (type != &quot;binary&quot;)
+            return throwVMError(exec, scope, &quot;Expected 'binary' as second argument.&quot;);
+        isBinary = true;
+    }
+
+    Vector&lt;char&gt; content;
+    if (!fillBufferWithContentsOfFile(fileName, content))
+        return throwVMError(exec, scope, &quot;Could not open file.&quot;);
+
+    if (!isBinary)
+        return JSValue::encode(jsString(exec, stringFromUTF(content)));
+
+    Structure* structure = exec-&gt;lexicalGlobalObject()-&gt;typedArrayStructure(TypeUint8);
+    auto length = content.size();
+    JSObject* result = createUint8TypedArray(exec, structure, ArrayBuffer::createFromBytes(content.releaseBuffer().leakPtr(), length, [] (void* p) { fastFree(p); }), 0, length);
+    if (UNLIKELY(scope.exception()))
+        return EncodedJSValue();
+
+    return JSValue::encode(result);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> EncodedJSValue JSC_HOST_CALL functionCheckSyntax(ExecState* exec)
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreruntimeErrorh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/runtime/Error.h (205879 => 205880)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/runtime/Error.h        2016-09-13 21:25:39 UTC (rev 205879)
+++ trunk/Source/JavaScriptCore/runtime/Error.h        2016-09-13 21:53:11 UTC (rev 205880)
</span><span class="lines">@@ -82,6 +82,7 @@
</span><span class="cx"> // Convenience wrappers, wrap result as an EncodedJSValue.
</span><span class="cx"> inline void throwVMError(ExecState* exec, ThrowScope&amp; scope, Exception* exception) { throwException(exec, scope, exception); }
</span><span class="cx"> inline EncodedJSValue throwVMError(ExecState* exec, ThrowScope&amp; scope, JSValue error) { return JSValue::encode(throwException(exec, scope, error)); }
</span><ins>+inline EncodedJSValue throwVMError(ExecState* exec, ThrowScope&amp; scope, const char* errorMessage) { return JSValue::encode(throwException(exec, scope, createError(exec, ASCIILiteral(errorMessage)))); }
</ins><span class="cx"> inline EncodedJSValue throwVMTypeError(ExecState* exec, ThrowScope&amp; scope) { return JSValue::encode(throwTypeError(exec, scope)); }
</span><span class="cx"> inline EncodedJSValue throwVMTypeError(ExecState* exec, ThrowScope&amp; scope, ASCIILiteral errorMessage) { return JSValue::encode(throwTypeError(exec, scope, errorMessage)); }
</span><span class="cx"> inline EncodedJSValue throwVMTypeError(ExecState* exec, ThrowScope&amp; scope, const String&amp; errorMessage) { return JSValue::encode(throwTypeError(exec, scope, errorMessage)); }
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreruntimeJSTypedArrayscpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/runtime/JSTypedArrays.cpp (205879 => 205880)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/runtime/JSTypedArrays.cpp        2016-09-13 21:25:39 UTC (rev 205879)
+++ trunk/Source/JavaScriptCore/runtime/JSTypedArrays.cpp        2016-09-13 21:53:11 UTC (rev 205880)
</span><span class="lines">@@ -49,5 +49,11 @@
</span><span class="cx"> MAKE_S_INFO(Float32);
</span><span class="cx"> MAKE_S_INFO(Float64);
</span><span class="cx"> 
</span><ins>+JSUint8Array* createUint8TypedArray(ExecState* exec, Structure* structure, RefPtr&lt;ArrayBuffer&gt;&amp;&amp; passedBuffer, unsigned byteOffset, unsigned length)
+{
+    return JSUint8Array::create(exec, structure, std::forward&lt;RefPtr&lt;ArrayBuffer&gt;&gt;(passedBuffer), byteOffset, length);
+}
+
+
</ins><span class="cx"> } // namespace JSC
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreruntimeJSTypedArraysh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/runtime/JSTypedArrays.h (205879 => 205880)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/runtime/JSTypedArrays.h        2016-09-13 21:25:39 UTC (rev 205879)
+++ trunk/Source/JavaScriptCore/runtime/JSTypedArrays.h        2016-09-13 21:53:11 UTC (rev 205880)
</span><span class="lines">@@ -42,6 +42,8 @@
</span><span class="cx"> typedef JSGenericTypedArrayView&lt;Float32Adaptor&gt; JSFloat32Array;
</span><span class="cx"> typedef JSGenericTypedArrayView&lt;Float64Adaptor&gt; JSFloat64Array;
</span><span class="cx"> 
</span><ins>+JS_EXPORT_PRIVATE JSUint8Array* createUint8TypedArray(ExecState*, Structure*, RefPtr&lt;ArrayBuffer&gt;&amp;&amp;, unsigned byteOffset, unsigned length);
+
</ins><span class="cx"> } // namespace JSC
</span><span class="cx"> 
</span><span class="cx"> #endif // JSTypedArrays_h
</span></span></pre>
</div>
</div>

</body>
</html>