⚠ Archived content — this site is no longer maintained.   Current WebKit documentation is at docs.webkit.org.

Changeset 249095 in webkit


Ignore:
Timestamp:
Aug 26, 2019, 7:36:47 AM (7 years ago)
Author:
clopez@igalia.com
Message:

Missing media controls when WebKit is built with Python3
https://bugs.webkit.org/show_bug.cgi?id=194367

Reviewed by Carlos Garcia Campos.

The JavaScript minifier script jsmin.py expects a text stream
with text type as input, but the script make-js-file-arrays.py
was passing to it a FileIO() object. So, when the jsmin script
called read() over this object, python3 was returning a type of
bytes, but for python2 it returns type str.

This caused two problems: first that jsmin failed to do any minifying
because it was comparing strings with a variable of type bytes.
The second major problem was in the write() function, when the
jsmin script tried to convert a byte character to text by calling
str() on it. Because what this does is not to convert from byte
type to string, but to simply generate a string with the format b'c'.
So the jsmin script was returning back as minified JS complete
garbage in the form of "b't'b'h'b'h'b'i" for python3.

Therefore, when WebKit was built with python3 this broke everything
that depended on the embedded JS code that make-js-file-arrays.py
was supposed to generate, like the media controls and the WebDriver
atoms.

Fix this by reworking the code in make-js-file-arrays script to
read the data from the file using a TextIOWrapper in python 3
with decoding for 'utf-8'. This ensures that the jsmin receives
a text type. For python2 keep using the same FileIO class.

On the jsmin.py script remove the problematic call to str() inside
the write() function when running with python3.
On top of that, add an extra check in jsmin.py script to make it
fail if the character type read is not the one expected. This
will cause the build to fail instead of failing silently like
now. I did some tests and the runtime cost of this extra check
is almost zero.

  • Scripts/jsmin.py:

(JavascriptMinify.minify.write):
(JavascriptMinify):

  • Scripts/make-js-file-arrays.py:

(main):

Location:
trunk/Source/JavaScriptCore
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/ChangeLog

    r249078 r249095  
     12019-08-26  Carlos Alberto Lopez Perez  <clopez@igalia.com>
     2
     3        Missing media controls when WebKit is built with Python3
     4        https://bugs.webkit.org/show_bug.cgi?id=194367
     5
     6        Reviewed by Carlos Garcia Campos.
     7
     8        The JavaScript minifier script jsmin.py expects a text stream
     9        with text type as input, but the script make-js-file-arrays.py
     10        was passing to it a FileIO() object. So, when the jsmin script
     11        called read() over this object, python3 was returning a type of
     12        bytes, but for python2 it returns type str.
     13
     14        This caused two problems: first that jsmin failed to do any minifying
     15        because it was comparing strings with a variable of type bytes.
     16        The second major problem was in the write() function, when the
     17        jsmin script tried to convert a byte character to text by calling
     18        str() on it. Because what this does is not to convert from byte
     19        type to string, but to simply generate a string with the format b'c'.
     20        So the jsmin script was returning back as minified JS complete
     21        garbage in the form of "b't'b'h'b'h'b'i" for python3.
     22
     23        Therefore, when WebKit was built with python3 this broke everything
     24        that depended on the embedded JS code that make-js-file-arrays.py
     25        was supposed to generate, like the media controls and the WebDriver
     26        atoms.
     27
     28        Fix this by reworking the code in make-js-file-arrays script to
     29        read the data from the file using a TextIOWrapper in python 3
     30        with decoding for 'utf-8'. This ensures that the jsmin receives
     31        a text type. For python2 keep using the same FileIO class.
     32
     33        On the jsmin.py script remove the problematic call to str() inside
     34        the write() function when running with python3.
     35        On top of that, add an extra check in jsmin.py script to make it
     36        fail if the character type read is not the one expected. This
     37        will cause the build to fail instead of failing silently like
     38        now. I did some tests and the runtime cost of this extra check
     39        is almost zero.
     40
     41        * Scripts/jsmin.py:
     42        (JavascriptMinify.minify.write):
     43        (JavascriptMinify):
     44        * Scripts/make-js-file-arrays.py:
     45        (main):
     46
    1472019-08-23  Devin Rousso  <drousso@apple.com>
    248
  • trunk/Source/JavaScriptCore/Scripts/jsmin.py

    r236321 r249095  
    2929if is_3:
    3030    import io
     31    python_text_type = str
    3132else:
    3233    import StringIO
     
    3536    except ImportError:
    3637        cStringIO = None
     38    python_text_type = basestring
    3739
    3840
     
    8385                self.return_buf += char
    8486                self.is_return = self.return_buf == 'return'
    85             if sys.version_info.major == 2:
    86                 self.outs.write(char)
    87             else:
    88                 self.outs.write(str(char))
     87            self.outs.write(char)
    8988            if self.is_return:
    9089                self.return_buf = ''
    9190
    92         read = self.ins.read
     91        def read(n):
     92            char = self.ins.read(n)
     93            if not isinstance(char, python_text_type):
     94                raise ValueError("ERROR: The script jsmin.py can only handle text input, but it received input of type %s" % type(char))
     95            return char
    9396
    9497        space_strings = "abcdefghijklmnopqrstuvwxyz"\
  • trunk/Source/JavaScriptCore/Scripts/make-js-file-arrays.py

    r236321 r249095  
    2727from optparse import OptionParser
    2828import sys
    29 if sys.version_info.major == 2:
    30     from StringIO import StringIO
    31 else:
    32     from io import StringIO
    33 from jsmin import JavascriptMinify
     29from jsmin import jsmin
     30is_3 = sys.version_info >= (3, 0)
    3431
    3532
     
    7269    print('namespace {0:s} {{'.format(namespace), file=sourceFile)
    7370
    74     jsm = JavascriptMinify()
     71    for inputFileName in inputPaths:
    7572
    76     for inputFileName in inputPaths:
    77         inputStream = io.FileIO(inputFileName)
    78         outputStream = StringIO()
     73        if is_3:
     74            inputStream = io.open(inputFileName, encoding='utf-8')
     75        else:
     76            inputStream = io.FileIO(inputFileName)
     77
     78        data = inputStream.read()
    7979
    8080        if not options.no_minify:
    81             jsm.minify(inputStream, outputStream)
    82             characters = outputStream.getvalue()
     81            characters = jsmin(data)
    8382        else:
    84             characters = inputStream.read()
     83            characters = data
    8584
    86         size = len(characters)
     85        if is_3:
     86            codepoints = bytearray(characters, encoding='utf-8')
     87        else:
     88            codepoints = list(map(ord, characters))
     89
     90        # Use the size of codepoints instead of the characters
     91        # because UTF-8 characters may need more than one byte.
     92        size = len(codepoints)
     93
    8794        variableName = os.path.splitext(os.path.basename(inputFileName))[0]
    8895
     
    9097        print('const char {0:s}JavaScript[{1:d}] = {{'.format(variableName, size), file=sourceFile)
    9198
    92         codepoints = list(map(ord, characters))
    9399        for codepointChunk in chunk(codepoints, 16):
    94100            print('    {0:s},'.format(','.join(map(stringifyCodepoint, codepointChunk))), file=sourceFile)
Note: See TracChangeset for help on using the changeset viewer.