2008-08-13 09:32:07 +00:00
|
|
|
#!/usr/bin/env python
|
2008-09-03 19:15:48 +00:00
|
|
|
#
|
2008-09-09 20:08:45 +00:00
|
|
|
# Copyright 2006-2008 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.
|
|
|
|
|
|
|
|
import os, re, sys, string
|
2009-01-28 10:15:11 +00:00
|
|
|
import jsmin
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
def ToCArray(lines):
|
|
|
|
result = []
|
|
|
|
for chr in lines:
|
|
|
|
value = ord(chr)
|
|
|
|
assert value < 128
|
|
|
|
result.append(str(value))
|
|
|
|
result.append("0")
|
|
|
|
return ", ".join(result)
|
|
|
|
|
|
|
|
|
2009-08-31 15:02:54 +00:00
|
|
|
def RemoveCommentsAndTrailingWhitespace(lines):
|
|
|
|
lines = re.sub(r'//.*\n', '\n', lines) # end-of-line comments
|
|
|
|
lines = re.sub(re.compile(r'/\*.*?\*/', re.DOTALL), '', lines) # comments.
|
|
|
|
lines = re.sub(r'\s+\n+', '\n', lines) # trailing whitespace
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
def ReadLines(filename):
|
|
|
|
result = []
|
|
|
|
for line in open(filename, "rt"):
|
|
|
|
if '#' in line:
|
|
|
|
line = line[:line.index('#')]
|
|
|
|
line = line.strip()
|
|
|
|
if len(line) > 0:
|
|
|
|
result.append(line)
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def LoadConfigFrom(name):
|
|
|
|
import ConfigParser
|
|
|
|
config = ConfigParser.ConfigParser()
|
|
|
|
config.read(name)
|
|
|
|
return config
|
|
|
|
|
|
|
|
|
|
|
|
def ParseValue(string):
|
|
|
|
string = string.strip()
|
|
|
|
if string.startswith('[') and string.endswith(']'):
|
|
|
|
return string.lstrip('[').rstrip(']').split()
|
|
|
|
else:
|
|
|
|
return string
|
|
|
|
|
|
|
|
|
2009-08-31 15:02:54 +00:00
|
|
|
EVAL_PATTERN = re.compile(r'\beval\s*\(');
|
|
|
|
WITH_PATTERN = re.compile(r'\bwith\s*\(');
|
|
|
|
|
|
|
|
|
|
|
|
def Validate(lines, file):
|
|
|
|
lines = RemoveCommentsAndTrailingWhitespace(lines)
|
|
|
|
# Because of simplified context setup, eval and with is not
|
|
|
|
# allowed in the natives files.
|
|
|
|
eval_match = EVAL_PATTERN.search(lines)
|
|
|
|
if eval_match:
|
|
|
|
raise ("Eval disallowed in natives: %s" % file)
|
|
|
|
with_match = WITH_PATTERN.search(lines)
|
|
|
|
if with_match:
|
|
|
|
raise ("With statements disallowed in natives: %s" % file)
|
|
|
|
|
|
|
|
|
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
|
|
|
def ExpandMacros(lines, macros):
|
2010-07-05 14:28:37 +00:00
|
|
|
# We allow macros to depend on the previously declared macros, but
|
|
|
|
# we don't allow self-dependecies or recursion.
|
|
|
|
for name_pattern, macro in reversed(macros):
|
|
|
|
pattern_match = name_pattern.search(lines, 0)
|
|
|
|
while pattern_match is not None:
|
2008-07-03 15:10:15 +00:00
|
|
|
# Scan over the arguments
|
|
|
|
height = 1
|
2010-07-05 14:28:37 +00:00
|
|
|
start = pattern_match.start()
|
|
|
|
end = pattern_match.end()
|
|
|
|
assert lines[end - 1] == '('
|
2008-07-03 15:10:15 +00:00
|
|
|
last_match = end
|
|
|
|
arg_index = 0
|
|
|
|
mapping = { }
|
|
|
|
def add_arg(str):
|
|
|
|
# Remember to expand recursively in the arguments
|
|
|
|
replacement = ExpandMacros(str.strip(), macros)
|
|
|
|
mapping[macro.args[arg_index]] = replacement
|
|
|
|
while end < len(lines) and height > 0:
|
|
|
|
# We don't count commas at higher nesting levels.
|
|
|
|
if lines[end] == ',' and height == 1:
|
|
|
|
add_arg(lines[last_match:end])
|
|
|
|
last_match = end + 1
|
|
|
|
elif lines[end] in ['(', '{', '[']:
|
|
|
|
height = height + 1
|
|
|
|
elif lines[end] in [')', '}', ']']:
|
|
|
|
height = height - 1
|
|
|
|
end = end + 1
|
|
|
|
# Remember to add the last match.
|
|
|
|
add_arg(lines[last_match:end-1])
|
|
|
|
result = macro.expand(mapping)
|
|
|
|
# Replace the occurrence of the macro with the expansion
|
|
|
|
lines = lines[:start] + result + lines[end:]
|
2010-07-05 14:28:37 +00:00
|
|
|
pattern_match = name_pattern.search(lines, start + len(result))
|
2008-07-03 15:10:15 +00:00
|
|
|
return lines
|
|
|
|
|
|
|
|
class TextMacro:
|
|
|
|
def __init__(self, args, body):
|
|
|
|
self.args = args
|
|
|
|
self.body = body
|
|
|
|
def expand(self, mapping):
|
|
|
|
result = self.body
|
|
|
|
for key, value in mapping.items():
|
|
|
|
result = result.replace(key, value)
|
|
|
|
return result
|
|
|
|
|
|
|
|
class PythonMacro:
|
|
|
|
def __init__(self, args, fun):
|
|
|
|
self.args = args
|
|
|
|
self.fun = fun
|
|
|
|
def expand(self, mapping):
|
|
|
|
args = []
|
|
|
|
for arg in self.args:
|
|
|
|
args.append(mapping[arg])
|
|
|
|
return str(self.fun(*args))
|
|
|
|
|
2009-08-31 15:02:54 +00:00
|
|
|
CONST_PATTERN = re.compile(r'^const\s+([a-zA-Z0-9_]+)\s*=\s*([^;]*);$')
|
|
|
|
MACRO_PATTERN = re.compile(r'^macro\s+([a-zA-Z0-9_]+)\s*\(([^)]*)\)\s*=\s*([^;]*);$')
|
|
|
|
PYTHON_MACRO_PATTERN = re.compile(r'^python\s+macro\s+([a-zA-Z0-9_]+)\s*\(([^)]*)\)\s*=\s*([^;]*);$')
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2010-07-05 14:28:37 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
def ReadMacros(lines):
|
2010-07-05 14:28:37 +00:00
|
|
|
constants = []
|
|
|
|
macros = []
|
2008-07-03 15:10:15 +00:00
|
|
|
for line in lines:
|
|
|
|
hash = line.find('#')
|
|
|
|
if hash != -1: line = line[:hash]
|
|
|
|
line = line.strip()
|
|
|
|
if len(line) is 0: continue
|
|
|
|
const_match = CONST_PATTERN.match(line)
|
|
|
|
if const_match:
|
|
|
|
name = const_match.group(1)
|
|
|
|
value = const_match.group(2).strip()
|
2010-07-05 14:28:37 +00:00
|
|
|
constants.append((re.compile("\\b%s\\b" % name), value))
|
2008-07-03 15:10:15 +00:00
|
|
|
else:
|
|
|
|
macro_match = MACRO_PATTERN.match(line)
|
|
|
|
if macro_match:
|
|
|
|
name = macro_match.group(1)
|
|
|
|
args = map(string.strip, macro_match.group(2).split(','))
|
|
|
|
body = macro_match.group(3).strip()
|
2010-07-05 14:28:37 +00:00
|
|
|
macros.append((re.compile("\\b%s\\(" % name), TextMacro(args, body)))
|
2008-07-03 15:10:15 +00:00
|
|
|
else:
|
|
|
|
python_match = PYTHON_MACRO_PATTERN.match(line)
|
|
|
|
if python_match:
|
|
|
|
name = python_match.group(1)
|
|
|
|
args = map(string.strip, python_match.group(2).split(','))
|
|
|
|
body = python_match.group(3).strip()
|
|
|
|
fun = eval("lambda " + ",".join(args) + ': ' + body)
|
2010-07-05 14:28:37 +00:00
|
|
|
macros.append((re.compile("\\b%s\\(" % name), PythonMacro(args, fun)))
|
2008-07-03 15:10:15 +00:00
|
|
|
else:
|
|
|
|
raise ("Illegal line: " + line)
|
|
|
|
return (constants, macros)
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
// This file was generated from .js source files by SCons. If you
|
|
|
|
// want to make changes to this file you should either change the
|
|
|
|
// javascript source files or the SConstruct script.
|
|
|
|
|
|
|
|
#include "v8.h"
|
|
|
|
#include "natives.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
%(source_lines)s\
|
|
|
|
|
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 <>
|
2010-03-23 11:40:38 +00:00
|
|
|
int NativesCollection<%(type)s>::GetDebuggerCount() {
|
|
|
|
return %(debugger_count)i;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
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 <>
|
|
|
|
Vector<const char> NativesCollection<%(type)s>::GetScriptSource(int index) {
|
2008-07-03 15:10:15 +00:00
|
|
|
%(get_script_source_cases)s\
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // internal
|
|
|
|
} // v8
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
SOURCE_DECLARATION = """\
|
|
|
|
static const char %(id)s[] = { %(data)s };
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2010-03-23 11:40:38 +00:00
|
|
|
GET_DEBUGGER_INDEX_CASE = """\
|
2008-07-03 15:10:15 +00:00
|
|
|
if (strcmp(name, "%(id)s") == 0) return %(i)i;
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2010-03-23 11:40:38 +00:00
|
|
|
GET_DEBUGGER_SCRIPT_SOURCE_CASE = """\
|
2008-07-03 15:10:15 +00:00
|
|
|
if (index == %(i)i) return Vector<const char>(%(id)s, %(length)i);
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2010-03-23 11:40:38 +00:00
|
|
|
GET_DEBUGGER_SCRIPT_NAME_CASE = """\
|
2008-07-03 15:10:15 +00:00
|
|
|
if (index == %(i)i) return Vector<const char>("%(name)s", %(length)i);
|
|
|
|
"""
|
|
|
|
|
|
|
|
def JS2C(source, target, env):
|
|
|
|
ids = []
|
2010-03-23 11:40:38 +00:00
|
|
|
debugger_ids = []
|
2008-07-03 15:10:15 +00:00
|
|
|
modules = []
|
|
|
|
# Locate the macros file name.
|
2010-07-22 12:55:14 +00:00
|
|
|
consts = []
|
|
|
|
macros = []
|
2008-07-03 15:10:15 +00:00
|
|
|
for s in source:
|
|
|
|
if 'macros.py' == (os.path.split(str(s))[1]):
|
|
|
|
(consts, macros) = ReadMacros(ReadLines(str(s)))
|
|
|
|
else:
|
|
|
|
modules.append(s)
|
|
|
|
|
|
|
|
# Build source code lines
|
|
|
|
source_lines = [ ]
|
2009-09-23 12:32:24 +00:00
|
|
|
|
|
|
|
minifier = jsmin.JavaScriptMinifier()
|
|
|
|
|
2009-08-31 15:02:54 +00:00
|
|
|
for module in modules:
|
|
|
|
filename = str(module)
|
2010-03-23 11:40:38 +00:00
|
|
|
debugger = filename.endswith('-debugger.js')
|
2009-08-31 15:02:54 +00:00
|
|
|
lines = ReadFile(filename)
|
2008-07-03 15:10:15 +00:00
|
|
|
lines = ExpandConstants(lines, consts)
|
|
|
|
lines = ExpandMacros(lines, macros)
|
2009-08-31 15:02:54 +00:00
|
|
|
Validate(lines, filename)
|
2009-09-23 12:32:24 +00:00
|
|
|
lines = minifier.JSMinify(lines)
|
2008-07-03 15:10:15 +00:00
|
|
|
data = ToCArray(lines)
|
2009-08-31 15:02:54 +00:00
|
|
|
id = (os.path.split(filename)[1])[:-3]
|
2010-03-23 11:40:38 +00:00
|
|
|
if debugger: id = id[:-9]
|
|
|
|
if debugger:
|
|
|
|
debugger_ids.append((id, len(lines)))
|
2008-07-03 15:10:15 +00:00
|
|
|
else:
|
|
|
|
ids.append((id, len(lines)))
|
|
|
|
source_lines.append(SOURCE_DECLARATION % { 'id': id, 'data': data })
|
2009-08-31 15:02:54 +00:00
|
|
|
|
2010-03-23 11:40:38 +00:00
|
|
|
# Build debugger support functions
|
2008-07-03 15:10:15 +00:00
|
|
|
get_index_cases = [ ]
|
|
|
|
get_script_source_cases = [ ]
|
|
|
|
get_script_name_cases = [ ]
|
|
|
|
|
|
|
|
i = 0
|
2010-03-23 11:40:38 +00:00
|
|
|
for (id, length) in debugger_ids:
|
2008-07-03 15:10:15 +00:00
|
|
|
native_name = "native %s.js" % id
|
2010-03-23 11:40:38 +00:00
|
|
|
get_index_cases.append(GET_DEBUGGER_INDEX_CASE % { 'id': id, 'i': i })
|
|
|
|
get_script_source_cases.append(GET_DEBUGGER_SCRIPT_SOURCE_CASE % {
|
2008-07-03 15:10:15 +00:00
|
|
|
'id': id,
|
|
|
|
'length': length,
|
|
|
|
'i': i
|
|
|
|
})
|
2010-03-23 11:40:38 +00:00
|
|
|
get_script_name_cases.append(GET_DEBUGGER_SCRIPT_NAME_CASE % {
|
2008-07-03 15:10:15 +00:00
|
|
|
'name': native_name,
|
|
|
|
'length': len(native_name),
|
|
|
|
'i': i
|
|
|
|
});
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
for (id, length) in ids:
|
|
|
|
native_name = "native %s.js" % id
|
2010-03-23 11:40:38 +00:00
|
|
|
get_index_cases.append(GET_DEBUGGER_INDEX_CASE % { 'id': id, 'i': i })
|
|
|
|
get_script_source_cases.append(GET_DEBUGGER_SCRIPT_SOURCE_CASE % {
|
2008-07-03 15:10:15 +00:00
|
|
|
'id': id,
|
|
|
|
'length': length,
|
|
|
|
'i': i
|
|
|
|
})
|
2010-03-23 11:40:38 +00:00
|
|
|
get_script_name_cases.append(GET_DEBUGGER_SCRIPT_NAME_CASE % {
|
2008-07-03 15:10:15 +00:00
|
|
|
'name': native_name,
|
|
|
|
'length': len(native_name),
|
|
|
|
'i': i
|
|
|
|
});
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
# Emit result
|
|
|
|
output = open(str(target[0]), "w")
|
|
|
|
output.write(HEADER_TEMPLATE % {
|
2010-03-23 11:40:38 +00:00
|
|
|
'builtin_count': len(ids) + len(debugger_ids),
|
|
|
|
'debugger_count': len(debugger_ids),
|
2008-07-03 15:10:15 +00:00
|
|
|
'source_lines': "\n".join(source_lines),
|
|
|
|
'get_index_cases': "".join(get_index_cases),
|
|
|
|
'get_script_source_cases': "".join(get_script_source_cases),
|
2008-10-21 09:12:27 +00:00
|
|
|
'get_script_name_cases': "".join(get_script_name_cases),
|
|
|
|
'type': env['TYPE']
|
2008-07-03 15:10:15 +00:00
|
|
|
})
|
|
|
|
output.close()
|
|
|
|
|
|
|
|
def main():
|
2008-08-13 09:32:07 +00:00
|
|
|
natives = sys.argv[1]
|
2011-04-15 12:31:03 +00:00
|
|
|
type = sys.argv[2]
|
|
|
|
source_files = sys.argv[3:]
|
|
|
|
JS2C(source_files, [natives], { 'TYPE': type })
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|