Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(121)

Unified Diff: tools/js2c.py

Issue 7066048: Compress sources of JS libraries in addition to the snapshot. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Make decompressor class public Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « tools/gyp/v8.gyp ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/js2c.py
diff --git a/tools/js2c.py b/tools/js2c.py
index 8211ec5880249e1ccaea1c5f5add65da7347801c..a2ea8eacc79186313e3af025f98c67e429294578 100644
--- a/tools/js2c.py
+++ b/tools/js2c.py
@@ -33,15 +33,22 @@
import os, re, sys, string
import jsmin
+import bz2
-def ToCArray(lines):
+def ToCAsciiArray(lines):
result = []
for chr in lines:
value = ord(chr)
assert value < 128
result.append(str(value))
- result.append("0")
+ return ", ".join(result)
+
+
+def ToCArray(lines):
+ result = []
+ for chr in lines:
+ result.append(str(ord(chr)))
return ", ".join(result)
@@ -87,8 +94,8 @@ def ParseValue(string):
return string
-EVAL_PATTERN = re.compile(r'\beval\s*\(');
-WITH_PATTERN = re.compile(r'\bwith\s*\(');
+EVAL_PATTERN = re.compile(r'\beval\s*\(')
+WITH_PATTERN = re.compile(r'\bwith\s*\(')
def Validate(lines, file):
@@ -212,11 +219,14 @@ HEADER_TEMPLATE = """\
#include "v8.h"
#include "natives.h"
+#include "utils.h"
namespace v8 {
namespace internal {
-%(source_lines)s\
+ static const byte sources[] = { %(sources_data)s };
+
+%(raw_sources_declaration)s\
template <>
int NativesCollection<%(type)s>::GetBuiltinsCount() {
@@ -235,8 +245,13 @@ namespace internal {
}
template <>
- Vector<const char> NativesCollection<%(type)s>::GetScriptSource(int index) {
-%(get_script_source_cases)s\
+ int NativesCollection<%(type)s>::GetRawScriptsSize() {
+ return %(raw_total_length)i;
+ }
+
+ template <>
+ Vector<const char> NativesCollection<%(type)s>::GetRawScriptSource(int index) {
+%(get_raw_script_source_cases)s\
return Vector<const char>("", 0);
}
@@ -246,27 +261,43 @@ namespace internal {
return Vector<const char>("", 0);
}
+ template <>
+ Vector<const byte> NativesCollection<%(type)s>::GetScriptsSource() {
+ return Vector<const byte>(sources, %(total_length)i);
+ }
+
+ template <>
+ void NativesCollection<%(type)s>::SetRawScriptsSource(Vector<const char> raw_source) {
+ ASSERT(%(raw_total_length)i == raw_source.length());
+ raw_sources = raw_source.start();
+ }
+
} // internal
} // v8
"""
-SOURCE_DECLARATION = """\
- static const char %(id)s[] = { %(data)s };
+RAW_SOURCES_COMPRESSION_DECLARATION = """\
+ static const char* raw_sources = NULL;
+"""
+
+
+RAW_SOURCES_DECLARATION = """\
+ static const char* raw_sources = reinterpret_cast<const char*>(sources);
"""
-GET_DEBUGGER_INDEX_CASE = """\
+GET_INDEX_CASE = """\
if (strcmp(name, "%(id)s") == 0) return %(i)i;
"""
-GET_DEBUGGER_SCRIPT_SOURCE_CASE = """\
- if (index == %(i)i) return Vector<const char>(%(id)s, %(length)i);
+GET_RAW_SCRIPT_SOURCE_CASE = """\
+ if (index == %(i)i) return Vector<const char>(raw_sources + %(offset)i, %(raw_length)i);
"""
-GET_DEBUGGER_SCRIPT_NAME_CASE = """\
+GET_SCRIPT_NAME_CASE = """\
if (index == %(i)i) return Vector<const char>("%(name)s", %(length)i);
"""
@@ -283,11 +314,10 @@ def JS2C(source, target, env):
else:
modules.append(s)
- # Build source code lines
- source_lines = [ ]
-
minifier = jsmin.JavaScriptMinifier()
+ module_offset = 0
+ all_sources = []
for module in modules:
filename = str(module)
debugger = filename.endswith('-debugger.js')
@@ -296,49 +326,46 @@ def JS2C(source, target, env):
lines = ExpandMacros(lines, macros)
Validate(lines, filename)
lines = minifier.JSMinify(lines)
- data = ToCArray(lines)
id = (os.path.split(filename)[1])[:-3]
if debugger: id = id[:-9]
+ raw_length = len(lines)
if debugger:
- debugger_ids.append((id, len(lines)))
+ debugger_ids.append((id, raw_length, module_offset))
else:
- ids.append((id, len(lines)))
- source_lines.append(SOURCE_DECLARATION % { 'id': id, 'data': data })
+ ids.append((id, raw_length, module_offset))
+ all_sources.append(lines)
+ module_offset += raw_length
+ total_length = raw_total_length = module_offset
+
+ if env['COMPRESSION'] == 'off':
+ raw_sources_declaration = RAW_SOURCES_DECLARATION
+ sources_data = ToCAsciiArray("".join(all_sources))
+ else:
+ raw_sources_declaration = RAW_SOURCES_COMPRESSION_DECLARATION
+ if env['COMPRESSION'] == 'bz2':
+ all_sources = bz2.compress("".join(all_sources))
+ total_length = len(all_sources)
+ sources_data = ToCArray(all_sources)
# Build debugger support functions
get_index_cases = [ ]
- get_script_source_cases = [ ]
+ get_raw_script_source_cases = [ ]
get_script_name_cases = [ ]
i = 0
- for (id, length) in debugger_ids:
- native_name = "native %s.js" % id
- get_index_cases.append(GET_DEBUGGER_INDEX_CASE % { 'id': id, 'i': i })
- get_script_source_cases.append(GET_DEBUGGER_SCRIPT_SOURCE_CASE % {
- 'id': id,
- 'length': length,
- 'i': i
- })
- get_script_name_cases.append(GET_DEBUGGER_SCRIPT_NAME_CASE % {
- 'name': native_name,
- 'length': len(native_name),
- 'i': i
- });
- i = i + 1
-
- for (id, length) in ids:
+ for (id, raw_length, module_offset) in debugger_ids + ids:
native_name = "native %s.js" % id
- get_index_cases.append(GET_DEBUGGER_INDEX_CASE % { 'id': id, 'i': i })
- get_script_source_cases.append(GET_DEBUGGER_SCRIPT_SOURCE_CASE % {
- 'id': id,
- 'length': length,
- 'i': i
- })
- get_script_name_cases.append(GET_DEBUGGER_SCRIPT_NAME_CASE % {
- 'name': native_name,
- 'length': len(native_name),
- 'i': i
- });
+ get_index_cases.append(GET_INDEX_CASE % { 'id': id, 'i': i })
+ get_raw_script_source_cases.append(GET_RAW_SCRIPT_SOURCE_CASE % {
+ 'offset': module_offset,
+ 'raw_length': raw_length,
+ 'i': i
+ })
+ get_script_name_cases.append(GET_SCRIPT_NAME_CASE % {
+ 'name': native_name,
+ 'length': len(native_name),
+ 'i': i
+ })
i = i + 1
# Emit result
@@ -346,9 +373,12 @@ def JS2C(source, target, env):
output.write(HEADER_TEMPLATE % {
'builtin_count': len(ids) + len(debugger_ids),
'debugger_count': len(debugger_ids),
- 'source_lines': "\n".join(source_lines),
+ 'sources_data': sources_data,
+ 'raw_sources_declaration': raw_sources_declaration,
+ 'raw_total_length': raw_total_length,
+ 'total_length': total_length,
'get_index_cases': "".join(get_index_cases),
- 'get_script_source_cases': "".join(get_script_source_cases),
+ 'get_raw_script_source_cases': "".join(get_raw_script_source_cases),
'get_script_name_cases': "".join(get_script_name_cases),
'type': env['TYPE']
})
@@ -357,8 +387,9 @@ def JS2C(source, target, env):
def main():
natives = sys.argv[1]
type = sys.argv[2]
- source_files = sys.argv[3:]
- JS2C(source_files, [natives], { 'TYPE': type })
+ compression = sys.argv[3]
+ source_files = sys.argv[4:]
+ JS2C(source_files, [natives], { 'TYPE': type, 'COMPRESSION': compression })
if __name__ == "__main__":
main()
« no previous file with comments | « tools/gyp/v8.gyp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698