diff options
Diffstat (limited to 'Source/JavaScriptCore/Scripts/make-js-file-arrays.py')
| -rwxr-xr-x | Source/JavaScriptCore/Scripts/make-js-file-arrays.py | 57 |
1 files changed, 34 insertions, 23 deletions
diff --git a/Source/JavaScriptCore/Scripts/make-js-file-arrays.py b/Source/JavaScriptCore/Scripts/make-js-file-arrays.py index 65056646a..d9ffb602e 100755 --- a/Source/JavaScriptCore/Scripts/make-js-file-arrays.py +++ b/Source/JavaScriptCore/Scripts/make-js-file-arrays.py @@ -21,11 +21,13 @@ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +from __future__ import print_function import io import os from optparse import OptionParser -from StringIO import StringIO -from jsmin import JavascriptMinify +import sys +from jsmin import jsmin +is_3 = sys.version_info >= (3, 0) def stringifyCodepoint(code): @@ -36,7 +38,7 @@ def stringifyCodepoint(code): def chunk(list, chunkSize): - for i in xrange(0, len(list), chunkSize): + for i in range(0, len(list), chunkSize): yield list[i:i + chunkSize] @@ -46,11 +48,11 @@ def main(): parser.add_option('-n', '--namespace', help='Namespace to use') (options, arguments) = parser.parse_args() if not options.namespace: - print 'Error: must provide a namespace' + print('Error: must provide a namespace') parser.print_usage() exit(-1) if len(arguments) < 3: - print 'Error: must provide at least 3 arguments' + print('Error: must provide at least 3 arguments') parser.print_usage() exit(-1) @@ -60,38 +62,47 @@ def main(): inputPaths = arguments[2:] headerFile = open(headerPath, 'w') - print >> headerFile, 'namespace {0:s} {{'.format(namespace) + print('namespace {0:s} {{'.format(namespace), file=headerFile) sourceFile = open(sourcePath, 'w') - print >> sourceFile, '#include "{0:s}"'.format(os.path.basename(headerPath)) - print >> sourceFile, 'namespace {0:s} {{'.format(namespace) - - jsm = JavascriptMinify() + print('#include "{0:s}"'.format(os.path.basename(headerPath)), file=sourceFile) + print('namespace {0:s} {{'.format(namespace), file=sourceFile) for inputFileName in inputPaths: - inputStream = io.FileIO(inputFileName) - outputStream = StringIO() + + if is_3: + inputStream = io.open(inputFileName, encoding='utf-8') + else: + inputStream = io.FileIO(inputFileName) + + data = inputStream.read() if not options.no_minify: - jsm.minify(inputStream, outputStream) - characters = outputStream.getvalue() + characters = jsmin(data) else: - characters = inputStream.read() + characters = data + + if is_3: + codepoints = bytearray(characters, encoding='utf-8') + else: + codepoints = list(map(ord, characters)) + + # Use the size of codepoints instead of the characters + # because UTF-8 characters may need more than one byte. + size = len(codepoints) - size = len(characters) variableName = os.path.splitext(os.path.basename(inputFileName))[0] - print >> headerFile, 'extern const char {0:s}JavaScript[{1:d}];'.format(variableName, size) - print >> sourceFile, 'const char {0:s}JavaScript[{1:d}] = {{'.format(variableName, size) + print('extern const char {0:s}JavaScript[{1:d}];'.format(variableName, size), file=headerFile) + print('const char {0:s}JavaScript[{1:d}] = {{'.format(variableName, size), file=sourceFile) - codepoints = map(ord, characters) for codepointChunk in chunk(codepoints, 16): - print >> sourceFile, ' {0:s},'.format(','.join(map(stringifyCodepoint, codepointChunk))) + print(' {0:s},'.format(','.join(map(stringifyCodepoint, codepointChunk))), file=sourceFile) - print >> sourceFile, '};' + print('};', file=sourceFile) - print >> headerFile, '}} // namespace {0:s}'.format(namespace) - print >> sourceFile, '}} // namespace {0:s}'.format(namespace) + print('}} // namespace {0:s}'.format(namespace), file=headerFile) + print('}} // namespace {0:s}'.format(namespace), file=sourceFile) if __name__ == '__main__': main() |
