2008-08-13 09:32:07 +00:00
|
|
|
#!/usr/bin/env python
|
2008-09-03 19:15:48 +00:00
|
|
|
#
|
2012-05-21 10:19:28 +00:00
|
|
|
# Copyright 2012 the V8 project authors. All rights reserved.
|
2008-07-03 15:10:15 +00:00
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are
|
|
|
|
# met:
|
|
|
|
#
|
|
|
|
# * Redistributions of source code must retain the above copyright
|
|
|
|
# notice, this list of conditions and the following disclaimer.
|
|
|
|
# * Redistributions in binary form must reproduce the above
|
|
|
|
# copyright notice, this list of conditions and the following
|
|
|
|
# disclaimer in the documentation and/or other materials provided
|
|
|
|
# with the distribution.
|
|
|
|
# * Neither the name of Google Inc. nor the names of its
|
|
|
|
# contributors may be used to endorse or promote products derived
|
|
|
|
# from this software without specific prior written permission.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
# This is a utility for converting JavaScript source code into C-style
|
|
|
|
# char arrays. It is used for embedded JavaScript code in the V8
|
|
|
|
# library.
|
|
|
|
|
2015-09-25 13:57:31 +00:00
|
|
|
import os, re
|
2015-06-22 12:13:52 +00:00
|
|
|
import optparse
|
2014-04-10 14:53:32 +00:00
|
|
|
import textwrap
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2014-04-10 14:53:32 +00:00
|
|
|
class Error(Exception):
|
|
|
|
def __init__(self, msg):
|
|
|
|
Exception.__init__(self, msg)
|
2011-06-06 20:47:30 +00:00
|
|
|
|
|
|
|
|
2014-04-10 14:53:32 +00:00
|
|
|
def ToCArray(byte_sequence):
|
2011-06-06 20:47:30 +00:00
|
|
|
result = []
|
2014-04-10 14:53:32 +00:00
|
|
|
for chr in byte_sequence:
|
2011-06-06 20:47:30 +00:00
|
|
|
result.append(str(ord(chr)))
|
2014-04-10 14:53:32 +00:00
|
|
|
joined = ", ".join(result)
|
|
|
|
return textwrap.fill(joined, 80)
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2017-12-12 20:51:06 +00:00
|
|
|
def RemoveCommentsEmptyLinesAndWhitespace(lines):
|
|
|
|
lines = re.sub(r'\n+', '\n', lines) # empty lines
|
2009-08-31 15:02:54 +00:00
|
|
|
lines = re.sub(r'//.*\n', '\n', lines) # end-of-line comments
|
|
|
|
lines = re.sub(re.compile(r'/\*.*?\*/', re.DOTALL), '', lines) # comments.
|
2017-12-12 20:51:06 +00:00
|
|
|
lines = re.sub(r'\s+\n', '\n', lines) # trailing whitespace
|
|
|
|
lines = re.sub(r'\n\s+', '\n', lines) # initial whitespace
|
2009-08-31 15:02:54 +00:00
|
|
|
return lines
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
def ReadFile(filename):
|
|
|
|
file = open(filename, "rt")
|
|
|
|
try:
|
|
|
|
lines = file.read()
|
|
|
|
finally:
|
|
|
|
file.close()
|
|
|
|
return lines
|
|
|
|
|
|
|
|
|
2011-06-06 20:47:30 +00:00
|
|
|
EVAL_PATTERN = re.compile(r'\beval\s*\(')
|
|
|
|
WITH_PATTERN = re.compile(r'\bwith\s*\(')
|
2015-04-27 12:33:58 +00:00
|
|
|
INVALID_ERROR_MESSAGE_PATTERN = re.compile(
|
|
|
|
r'Make(?!Generic)\w*Error\(([kA-Z]\w+)')
|
|
|
|
NEW_ERROR_PATTERN = re.compile(r'new \$\w*Error\((?!\))')
|
2009-08-31 15:02:54 +00:00
|
|
|
|
2014-04-10 14:53:32 +00:00
|
|
|
def Validate(lines):
|
2009-08-31 15:02:54 +00:00
|
|
|
# Because of simplified context setup, eval and with is not
|
|
|
|
# allowed in the natives files.
|
2014-04-10 14:53:32 +00:00
|
|
|
if EVAL_PATTERN.search(lines):
|
|
|
|
raise Error("Eval disallowed in natives.")
|
|
|
|
if WITH_PATTERN.search(lines):
|
|
|
|
raise Error("With statements disallowed in natives.")
|
2015-04-16 07:01:20 +00:00
|
|
|
invalid_error = INVALID_ERROR_MESSAGE_PATTERN.search(lines)
|
|
|
|
if invalid_error:
|
|
|
|
raise Error("Unknown error message template '%s'" % invalid_error.group(1))
|
2015-04-24 10:22:00 +00:00
|
|
|
if NEW_ERROR_PATTERN.search(lines):
|
|
|
|
raise Error("Error constructed without message template.")
|
2014-04-10 14:53:32 +00:00
|
|
|
# Pass lines through unchanged.
|
|
|
|
return lines
|
2009-08-31 15:02:54 +00:00
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
def ExpandConstants(lines, constants):
|
2010-07-05 14:28:37 +00:00
|
|
|
for key, value in constants:
|
|
|
|
lines = key.sub(str(value), lines)
|
2008-07-03 15:10:15 +00:00
|
|
|
return lines
|
|
|
|
|
2008-07-30 08:49:36 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
HEADER_TEMPLATE = """\
|
2011-04-15 12:31:03 +00:00
|
|
|
// Copyright 2011 Google Inc. All Rights Reserved.
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2013-04-23 08:58:23 +00:00
|
|
|
// This file was generated from .js source files by GYP. If you
|
2008-07-03 15:10:15 +00:00
|
|
|
// want to make changes to this file you should either change the
|
2013-04-23 08:58:23 +00:00
|
|
|
// javascript source files or the GYP script.
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/v8.h"
|
2015-03-27 15:28:55 +00:00
|
|
|
#include "src/snapshot/natives.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/utils.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
2014-04-10 14:53:32 +00:00
|
|
|
%(sources_declaration)s\
|
2011-06-06 20:47:30 +00:00
|
|
|
|
2008-10-21 09:12:27 +00:00
|
|
|
template <>
|
|
|
|
int NativesCollection<%(type)s>::GetBuiltinsCount() {
|
2008-07-03 15:10:15 +00:00
|
|
|
return %(builtin_count)i;
|
|
|
|
}
|
|
|
|
|
2008-10-21 09:12:27 +00:00
|
|
|
template <>
|
|
|
|
int NativesCollection<%(type)s>::GetIndex(const char* name) {
|
2008-07-03 15:10:15 +00:00
|
|
|
%(get_index_cases)s\
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-10-21 09:12:27 +00:00
|
|
|
template <>
|
2014-12-04 18:51:15 +00:00
|
|
|
Vector<const char> NativesCollection<%(type)s>::GetScriptSource(int index) {
|
|
|
|
%(get_script_source_cases)s\
|
2008-07-03 15:10:15 +00:00
|
|
|
return Vector<const char>("", 0);
|
|
|
|
}
|
|
|
|
|
2008-10-21 09:12:27 +00:00
|
|
|
template <>
|
|
|
|
Vector<const char> NativesCollection<%(type)s>::GetScriptName(int index) {
|
2008-07-03 15:10:15 +00:00
|
|
|
%(get_script_name_cases)s\
|
|
|
|
return Vector<const char>("", 0);
|
|
|
|
}
|
|
|
|
|
2011-06-06 20:47:30 +00:00
|
|
|
template <>
|
2014-12-04 18:51:15 +00:00
|
|
|
Vector<const char> NativesCollection<%(type)s>::GetScriptsSource() {
|
|
|
|
return Vector<const char>(sources, %(total_length)i);
|
2011-06-06 20:47:30 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
} // internal
|
|
|
|
} // v8
|
|
|
|
"""
|
|
|
|
|
2014-04-10 14:53:32 +00:00
|
|
|
SOURCES_DECLARATION = """\
|
2014-12-04 18:51:15 +00:00
|
|
|
static const char sources[] = { %s };
|
2008-07-03 15:10:15 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
2011-06-06 20:47:30 +00:00
|
|
|
GET_INDEX_CASE = """\
|
2008-07-03 15:10:15 +00:00
|
|
|
if (strcmp(name, "%(id)s") == 0) return %(i)i;
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2014-12-04 18:51:15 +00:00
|
|
|
GET_SCRIPT_SOURCE_CASE = """\
|
|
|
|
if (index == %(i)i) return Vector<const char>(sources + %(offset)i, %(source_length)i);
|
2008-07-03 15:10:15 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
2011-06-06 20:47:30 +00:00
|
|
|
GET_SCRIPT_NAME_CASE = """\
|
2008-07-03 15:10:15 +00:00
|
|
|
if (index == %(i)i) return Vector<const char>("%(name)s", %(length)i);
|
|
|
|
"""
|
|
|
|
|
2014-04-10 14:53:32 +00:00
|
|
|
|
2019-01-08 12:42:41 +00:00
|
|
|
def BuildFilterChain():
|
2014-04-10 14:53:32 +00:00
|
|
|
"""Build the chain of filter functions to be applied to the sources.
|
|
|
|
|
|
|
|
Returns:
|
2015-05-08 08:44:47 +00:00
|
|
|
A function (string -> string) that processes a source file.
|
2014-04-10 14:53:32 +00:00
|
|
|
"""
|
2019-01-08 12:42:41 +00:00
|
|
|
filter_chain = [
|
2017-12-12 20:51:06 +00:00
|
|
|
RemoveCommentsEmptyLinesAndWhitespace,
|
2014-04-10 14:53:32 +00:00
|
|
|
Validate,
|
2019-01-08 12:42:41 +00:00
|
|
|
]
|
2014-04-10 14:53:32 +00:00
|
|
|
|
|
|
|
def chain(f1, f2):
|
|
|
|
return lambda x: f2(f1(x))
|
|
|
|
|
|
|
|
return reduce(chain, filter_chain)
|
|
|
|
|
2015-04-29 14:01:35 +00:00
|
|
|
def BuildExtraFilterChain():
|
2017-12-12 20:51:06 +00:00
|
|
|
return lambda x: RemoveCommentsEmptyLinesAndWhitespace(Validate(x))
|
2014-04-10 14:53:32 +00:00
|
|
|
|
|
|
|
class Sources:
|
|
|
|
def __init__(self):
|
|
|
|
self.names = []
|
|
|
|
self.modules = []
|
|
|
|
|
2015-05-08 08:44:47 +00:00
|
|
|
def PrepareSources(source_files, native_type, emit_js):
|
2014-04-10 14:53:32 +00:00
|
|
|
"""Read, prepare and assemble the list of source files.
|
|
|
|
|
|
|
|
Args:
|
2019-01-08 12:42:41 +00:00
|
|
|
source_files: List of JavaScript-ish source files.
|
2015-05-08 08:44:47 +00:00
|
|
|
native_type: String corresponding to a NativeType enum value, allowing us
|
|
|
|
to treat different types of sources differently.
|
2015-04-29 14:01:35 +00:00
|
|
|
emit_js: True if we should skip the byte conversion and just leave the
|
|
|
|
sources as JS strings.
|
2014-04-10 14:53:32 +00:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
An instance of Sources.
|
|
|
|
"""
|
2019-01-08 12:42:41 +00:00
|
|
|
filters = BuildFilterChain()
|
2014-04-10 14:53:32 +00:00
|
|
|
|
2015-05-08 08:44:47 +00:00
|
|
|
source_files_and_contents = [(f, ReadFile(f)) for f in source_files]
|
|
|
|
|
|
|
|
# Have a single not-quite-empty source file if there are none present;
|
|
|
|
# otherwise you get errors trying to compile an empty C++ array.
|
|
|
|
# It cannot be empty (or whitespace, which gets trimmed to empty), as
|
|
|
|
# the deserialization code assumes each file is nonempty.
|
|
|
|
if not source_files_and_contents:
|
2015-05-11 08:50:38 +00:00
|
|
|
source_files_and_contents = [("dummy.js", "(function() {})")]
|
2015-05-08 08:44:47 +00:00
|
|
|
|
2014-04-10 14:53:32 +00:00
|
|
|
result = Sources()
|
2015-04-29 14:01:35 +00:00
|
|
|
|
2015-05-08 08:44:47 +00:00
|
|
|
for (source, contents) in source_files_and_contents:
|
2014-04-10 14:53:32 +00:00
|
|
|
try:
|
2015-05-08 08:44:47 +00:00
|
|
|
lines = filters(contents)
|
2014-04-10 14:53:32 +00:00
|
|
|
except Error as e:
|
|
|
|
raise Error("In file %s:\n%s" % (source, str(e)))
|
|
|
|
|
2015-04-29 14:01:35 +00:00
|
|
|
result.modules.append(lines)
|
2014-04-10 14:53:32 +00:00
|
|
|
|
|
|
|
name = os.path.basename(source)[:-3]
|
2015-07-31 11:07:50 +00:00
|
|
|
result.names.append(name)
|
2015-04-29 14:01:35 +00:00
|
|
|
|
2014-04-10 14:53:32 +00:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
2014-06-23 13:52:17 +00:00
|
|
|
def BuildMetadata(sources, source_bytes, native_type):
|
2014-04-10 14:53:32 +00:00
|
|
|
"""Build the meta data required to generate a libaries file.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
sources: A Sources instance with the prepared sources.
|
|
|
|
source_bytes: A list of source bytes.
|
|
|
|
(The concatenation of all sources; might be compressed.)
|
|
|
|
native_type: The parameter for the NativesCollection template.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A dictionary for use with HEADER_TEMPLATE.
|
|
|
|
"""
|
|
|
|
total_length = len(source_bytes)
|
|
|
|
raw_sources = "".join(sources.modules)
|
|
|
|
|
|
|
|
# The sources are expected to be ASCII-only.
|
|
|
|
assert not filter(lambda value: ord(value) >= 128, raw_sources)
|
|
|
|
|
|
|
|
# Loop over modules and build up indices into the source blob:
|
|
|
|
get_index_cases = []
|
|
|
|
get_script_name_cases = []
|
2014-12-04 18:51:15 +00:00
|
|
|
get_script_source_cases = []
|
2014-04-10 14:53:32 +00:00
|
|
|
offset = 0
|
|
|
|
for i in xrange(len(sources.modules)):
|
|
|
|
native_name = "native %s.js" % sources.names[i]
|
|
|
|
d = {
|
|
|
|
"i": i,
|
|
|
|
"id": sources.names[i],
|
|
|
|
"name": native_name,
|
|
|
|
"length": len(native_name),
|
|
|
|
"offset": offset,
|
2014-12-04 18:51:15 +00:00
|
|
|
"source_length": len(sources.modules[i]),
|
2014-04-10 14:53:32 +00:00
|
|
|
}
|
|
|
|
get_index_cases.append(GET_INDEX_CASE % d)
|
|
|
|
get_script_name_cases.append(GET_SCRIPT_NAME_CASE % d)
|
2014-12-04 18:51:15 +00:00
|
|
|
get_script_source_cases.append(GET_SCRIPT_SOURCE_CASE % d)
|
2014-04-10 14:53:32 +00:00
|
|
|
offset += len(sources.modules[i])
|
|
|
|
assert offset == len(raw_sources)
|
|
|
|
|
|
|
|
metadata = {
|
|
|
|
"builtin_count": len(sources.modules),
|
|
|
|
"sources_declaration": SOURCES_DECLARATION % ToCArray(source_bytes),
|
|
|
|
"total_length": total_length,
|
|
|
|
"get_index_cases": "".join(get_index_cases),
|
2014-12-04 18:51:15 +00:00
|
|
|
"get_script_source_cases": "".join(get_script_source_cases),
|
2014-04-10 14:53:32 +00:00
|
|
|
"get_script_name_cases": "".join(get_script_name_cases),
|
|
|
|
"type": native_type,
|
|
|
|
}
|
|
|
|
return metadata
|
|
|
|
|
|
|
|
|
2014-06-23 13:52:17 +00:00
|
|
|
def PutInt(blob_file, value):
|
2014-10-04 10:30:24 +00:00
|
|
|
assert(value >= 0 and value < (1 << 28))
|
|
|
|
if (value < 1 << 6):
|
|
|
|
size = 1
|
|
|
|
elif (value < 1 << 14):
|
|
|
|
size = 2
|
|
|
|
elif (value < 1 << 22):
|
|
|
|
size = 3
|
|
|
|
else:
|
|
|
|
size = 4
|
|
|
|
value_with_length = (value << 2) | (size - 1)
|
2014-06-23 13:52:17 +00:00
|
|
|
|
|
|
|
byte_sequence = bytearray()
|
|
|
|
for i in xrange(size):
|
|
|
|
byte_sequence.append(value_with_length & 255)
|
|
|
|
value_with_length >>= 8;
|
|
|
|
blob_file.write(byte_sequence)
|
|
|
|
|
|
|
|
|
|
|
|
def PutStr(blob_file, value):
|
|
|
|
PutInt(blob_file, len(value));
|
|
|
|
blob_file.write(value);
|
|
|
|
|
|
|
|
|
|
|
|
def WriteStartupBlob(sources, startup_blob):
|
|
|
|
"""Write a startup blob, as expected by V8 Initialize ...
|
|
|
|
TODO(vogelheim): Add proper method name.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
sources: A Sources instance with the prepared sources.
|
|
|
|
startup_blob_file: Name of file to write the blob to.
|
|
|
|
"""
|
|
|
|
output = open(startup_blob, "wb")
|
|
|
|
|
2018-12-10 14:45:41 +00:00
|
|
|
PutInt(output, len(sources.names))
|
|
|
|
for i in xrange(len(sources.names)):
|
2014-06-23 13:52:17 +00:00
|
|
|
PutStr(output, sources.names[i]);
|
|
|
|
PutStr(output, sources.modules[i]);
|
|
|
|
|
|
|
|
output.close()
|
|
|
|
|
|
|
|
|
2015-05-08 08:44:47 +00:00
|
|
|
def JS2C(sources, target, native_type, raw_file, startup_blob, emit_js):
|
|
|
|
prepared_sources = PrepareSources(sources, native_type, emit_js)
|
2015-04-29 14:01:35 +00:00
|
|
|
sources_output = "".join(prepared_sources.modules)
|
|
|
|
metadata = BuildMetadata(prepared_sources, sources_output, native_type)
|
2014-04-10 14:53:32 +00:00
|
|
|
|
|
|
|
# Optionally emit raw file.
|
|
|
|
if raw_file:
|
|
|
|
output = open(raw_file, "w")
|
2015-04-29 14:01:35 +00:00
|
|
|
output.write(sources_output)
|
2014-04-10 14:53:32 +00:00
|
|
|
output.close()
|
|
|
|
|
2014-06-23 13:52:17 +00:00
|
|
|
if startup_blob:
|
2015-04-29 14:01:35 +00:00
|
|
|
WriteStartupBlob(prepared_sources, startup_blob)
|
2014-06-23 13:52:17 +00:00
|
|
|
|
2014-04-10 14:53:32 +00:00
|
|
|
# Emit resulting source file.
|
|
|
|
output = open(target, "w")
|
2015-05-08 08:44:47 +00:00
|
|
|
if emit_js:
|
2015-04-29 14:01:35 +00:00
|
|
|
output.write(sources_output)
|
|
|
|
else:
|
|
|
|
output.write(HEADER_TEMPLATE % metadata)
|
2008-07-03 15:10:15 +00:00
|
|
|
output.close()
|
|
|
|
|
2014-04-10 14:53:32 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
def main():
|
2015-06-22 12:13:52 +00:00
|
|
|
parser = optparse.OptionParser()
|
|
|
|
parser.add_option("--raw",
|
|
|
|
help="file to write the processed sources array to.")
|
|
|
|
parser.add_option("--startup_blob",
|
|
|
|
help="file to write the startup blob to.")
|
|
|
|
parser.add_option("--js",
|
|
|
|
help="writes a JS file output instead of a C file",
|
2015-09-08 15:24:45 +00:00
|
|
|
action="store_true", default=False, dest='js')
|
|
|
|
parser.add_option("--nojs", action="store_false", default=False, dest='js')
|
2015-06-22 12:13:52 +00:00
|
|
|
parser.set_usage("""js2c out.cc type sources.js ...
|
|
|
|
out.cc: C code to be generated.
|
|
|
|
type: type parameter for NativesCollection template.
|
2019-01-08 12:42:41 +00:00
|
|
|
sources.js: JS internal sources.""")
|
2015-06-22 12:13:52 +00:00
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
JS2C(args[2:],
|
|
|
|
args[0],
|
|
|
|
args[1],
|
|
|
|
options.raw,
|
|
|
|
options.startup_blob,
|
|
|
|
options.js)
|
2014-04-10 14:53:32 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|