Title: [205880] trunk
Revision
205880
Author
[email protected]
Date
2016-09-13 14:53:11 -0700 (Tue, 13 Sep 2016)

Log Message

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/_javascript_Core:

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
"Just Work".

  [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.

Modified Paths

Added Paths

Diff

Modified: trunk/JSTests/ChangeLog (205879 => 205880)


--- trunk/JSTests/ChangeLog	2016-09-13 21:25:39 UTC (rev 205879)
+++ trunk/JSTests/ChangeLog	2016-09-13 21:53:11 UTC (rev 205880)
@@ -1,3 +1,13 @@
+2016-09-13  JF Bastien  <[email protected]>
+
+        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.
+
 2016-09-12  Skachkov Oleksandr  <[email protected]>
 
         ES6: Classes: Should be allowed to create a static method with name "arguments"

Added: trunk/JSTests/stress/jsc-read.js (0 => 205880)


--- trunk/JSTests/stress/jsc-read.js	                        (rev 0)
+++ trunk/JSTests/stress/jsc-read.js	2016-09-13 21:53:11 UTC (rev 205880)
@@ -0,0 +1,36 @@
+(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 => {
+        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 => {
+        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 => {
+        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);
+})();

Modified: trunk/Source/_javascript_Core/ChangeLog (205879 => 205880)


--- trunk/Source/_javascript_Core/ChangeLog	2016-09-13 21:25:39 UTC (rev 205879)
+++ trunk/Source/_javascript_Core/ChangeLog	2016-09-13 21:53:11 UTC (rev 205880)
@@ -1,3 +1,33 @@
+2016-09-13  JF Bastien  <[email protected]>
+
+        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
+        "Just Work".
+
+          [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.
+
 2016-09-12  Skachkov Oleksandr  <[email protected]>
 
         ES6: Classes: Should be allowed to create a static method with name "arguments"

Modified: trunk/Source/_javascript_Core/jsc.cpp (205879 => 205880)


--- trunk/Source/_javascript_Core/jsc.cpp	2016-09-13 21:25:39 UTC (rev 205879)
+++ trunk/Source/_javascript_Core/jsc.cpp	2016-09-13 21:53:11 UTC (rev 205880)
@@ -22,6 +22,7 @@
 
 #include "config.h"
 
+#include "ArrayBuffer.h"
 #include "ArrayPrototype.h"
 #include "BuiltinExecutableCreator.h"
 #include "ButterflyInlines.h"
@@ -48,6 +49,7 @@
 #include "JSONObject.h"
 #include "JSProxy.h"
 #include "JSString.h"
+#include "JSTypedArrays.h"
 #include "JSWASMModule.h"
 #include "LLIntData.h"
 #include "ParserError.h"
@@ -803,7 +805,8 @@
         addFunction(vm, "runString", functionRunString, 1);
         addFunction(vm, "load", functionLoad, 1);
         addFunction(vm, "loadString", functionLoadString, 1);
-        addFunction(vm, "readFile", functionReadFile, 1);
+        addFunction(vm, "readFile", functionReadFile, 2);
+        addFunction(vm, "read", functionReadFile, 2);
         addFunction(vm, "checkSyntax", functionCheckSyntax, 1);
         addFunction(vm, "jscStack", functionJSCStack, 1);
         addFunction(vm, "readline", functionReadline, 0);
@@ -1555,11 +1558,31 @@
     String fileName = exec->argument(0).toWTFString(exec);
     if (UNLIKELY(scope.exception()))
         return JSValue::encode(jsUndefined());
-    Vector<char> script;
-    if (!fillBufferWithContentsOfFile(fileName, script))
-        return JSValue::encode(throwException(exec, scope, createError(exec, ASCIILiteral("Could not open file."))));
 
-    return JSValue::encode(jsString(exec, stringFromUTF(script)));
+    bool isBinary = false;
+    if (exec->argumentCount() > 1) {
+        String type = exec->argument(1).toWTFString(exec);
+        if (UNLIKELY(scope.exception()))
+            return EncodedJSValue();
+        if (type != "binary")
+            return throwVMError(exec, scope, "Expected 'binary' as second argument.");
+        isBinary = true;
+    }
+
+    Vector<char> content;
+    if (!fillBufferWithContentsOfFile(fileName, content))
+        return throwVMError(exec, scope, "Could not open file.");
+
+    if (!isBinary)
+        return JSValue::encode(jsString(exec, stringFromUTF(content)));
+
+    Structure* structure = exec->lexicalGlobalObject()->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);
 }
 
 EncodedJSValue JSC_HOST_CALL functionCheckSyntax(ExecState* exec)

Modified: trunk/Source/_javascript_Core/runtime/Error.h (205879 => 205880)


--- trunk/Source/_javascript_Core/runtime/Error.h	2016-09-13 21:25:39 UTC (rev 205879)
+++ trunk/Source/_javascript_Core/runtime/Error.h	2016-09-13 21:53:11 UTC (rev 205880)
@@ -82,6 +82,7 @@
 // Convenience wrappers, wrap result as an EncodedJSValue.
 inline void throwVMError(ExecState* exec, ThrowScope& scope, Exception* exception) { throwException(exec, scope, exception); }
 inline EncodedJSValue throwVMError(ExecState* exec, ThrowScope& scope, JSValue error) { return JSValue::encode(throwException(exec, scope, error)); }
+inline EncodedJSValue throwVMError(ExecState* exec, ThrowScope& scope, const char* errorMessage) { return JSValue::encode(throwException(exec, scope, createError(exec, ASCIILiteral(errorMessage)))); }
 inline EncodedJSValue throwVMTypeError(ExecState* exec, ThrowScope& scope) { return JSValue::encode(throwTypeError(exec, scope)); }
 inline EncodedJSValue throwVMTypeError(ExecState* exec, ThrowScope& scope, ASCIILiteral errorMessage) { return JSValue::encode(throwTypeError(exec, scope, errorMessage)); }
 inline EncodedJSValue throwVMTypeError(ExecState* exec, ThrowScope& scope, const String& errorMessage) { return JSValue::encode(throwTypeError(exec, scope, errorMessage)); }

Modified: trunk/Source/_javascript_Core/runtime/JSTypedArrays.cpp (205879 => 205880)


--- trunk/Source/_javascript_Core/runtime/JSTypedArrays.cpp	2016-09-13 21:25:39 UTC (rev 205879)
+++ trunk/Source/_javascript_Core/runtime/JSTypedArrays.cpp	2016-09-13 21:53:11 UTC (rev 205880)
@@ -49,5 +49,11 @@
 MAKE_S_INFO(Float32);
 MAKE_S_INFO(Float64);
 
+JSUint8Array* createUint8TypedArray(ExecState* exec, Structure* structure, RefPtr<ArrayBuffer>&& passedBuffer, unsigned byteOffset, unsigned length)
+{
+    return JSUint8Array::create(exec, structure, std::forward<RefPtr<ArrayBuffer>>(passedBuffer), byteOffset, length);
+}
+
+
 } // namespace JSC
 

Modified: trunk/Source/_javascript_Core/runtime/JSTypedArrays.h (205879 => 205880)


--- trunk/Source/_javascript_Core/runtime/JSTypedArrays.h	2016-09-13 21:25:39 UTC (rev 205879)
+++ trunk/Source/_javascript_Core/runtime/JSTypedArrays.h	2016-09-13 21:53:11 UTC (rev 205880)
@@ -42,6 +42,8 @@
 typedef JSGenericTypedArrayView<Float32Adaptor> JSFloat32Array;
 typedef JSGenericTypedArrayView<Float64Adaptor> JSFloat64Array;
 
+JS_EXPORT_PRIVATE JSUint8Array* createUint8TypedArray(ExecState*, Structure*, RefPtr<ArrayBuffer>&&, unsigned byteOffset, unsigned length);
+
 } // namespace JSC
 
 #endif // JSTypedArrays_h
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to