Ignore:
Timestamp:
Aug 26, 2019, 7:36:47 AM (6 years ago)
Author:
[email protected]
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):

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.