[js] Remove macros.py and simplify js2c.py
- Remove macros.py - Inlines macros into d8.js - Remove dead code (macros and message templates) from js2c.py - Remove empty src/js directory Bug: v8:7624 Change-Id: I8dfb69f2f8cb3746b67de816ffc8eb305cbcdee6 Reviewed-on: https://chromium-review.googlesource.com/c/1400150 Commit-Queue: Peter Wong <peter.wm.wong@gmail.com> Reviewed-by: Yang Guo <yangguo@chromium.org> Reviewed-by: Jakob Gruber <jgruber@chromium.org> Cr-Commit-Position: refs/heads/master@{#58631}
This commit is contained in:
parent
ac2fb66b65
commit
7164251aec
1
BUILD.gn
1
BUILD.gn
@ -738,7 +738,6 @@ action("d8_js2c") {
|
||||
# NOSORT
|
||||
inputs = [
|
||||
"src/d8.js",
|
||||
"src/js/macros.py",
|
||||
]
|
||||
|
||||
outputs = [
|
||||
|
@ -44,7 +44,7 @@ function Stringify(x, depth) {
|
||||
case "bigint":
|
||||
return x.toString() + "n";
|
||||
case "object":
|
||||
if (IS_NULL(x)) return "null";
|
||||
if (x === null) return "null";
|
||||
if (x.constructor && x.constructor.name === "Array") {
|
||||
var elems = [];
|
||||
for (var i = 0; i < x.length; ++i) {
|
||||
@ -63,8 +63,8 @@ function Stringify(x, depth) {
|
||||
for (var i in names) {
|
||||
var name = names[i];
|
||||
var desc = Object.getOwnPropertyDescriptor(x, name);
|
||||
if (IS_UNDEFINED(desc)) continue;
|
||||
if (IS_SYMBOL(name)) name = "[" + Stringify(name) + "]";
|
||||
if (desc === (void 0)) continue;
|
||||
if (typeof name === 'symbol') name = "[" + Stringify(name) + "]";
|
||||
if ("value" in desc) {
|
||||
props.push(name + ": " + Stringify(desc.value, depth - 1));
|
||||
}
|
||||
|
@ -1,14 +0,0 @@
|
||||
set noparent
|
||||
|
||||
adamk@chromium.org
|
||||
bmeurer@chromium.org
|
||||
cbruni@chromium.org
|
||||
gsathya@chromium.org
|
||||
ishell@chromium.org
|
||||
jgruber@chromium.org
|
||||
jkummerow@chromium.org
|
||||
littledan@chromium.org
|
||||
verwaest@chromium.org
|
||||
yangguo@chromium.org
|
||||
|
||||
# COMPONENT: Blink>JavaScript>Language
|
@ -1,53 +0,0 @@
|
||||
# Copyright 2006-2009 the V8 project authors. All rights reserved.
|
||||
# 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.
|
||||
|
||||
# Dictionary that is passed as defines for js2c.py.
|
||||
# Used for defines that must be defined for all native JS files.
|
||||
|
||||
define NONE = 0;
|
||||
define READ_ONLY = 1;
|
||||
define DONT_ENUM = 2;
|
||||
define DONT_DELETE = 4;
|
||||
|
||||
# Type query macros.
|
||||
macro IS_ARRAY(arg) = (%_IsArray(arg));
|
||||
macro IS_NULL(arg) = (arg === null);
|
||||
macro IS_NULL_OR_UNDEFINED(arg) = (arg == null);
|
||||
macro IS_NUMBER(arg) = (typeof(arg) === 'number');
|
||||
macro IS_SYMBOL(arg) = (typeof(arg) === 'symbol');
|
||||
macro IS_UNDEFINED(arg) = (arg === (void 0));
|
||||
macro IS_CALLABLE(arg) = (typeof(arg) === 'function');
|
||||
|
||||
# Inline macros.
|
||||
macro TO_LENGTH(arg) = (%_ToLength(arg));
|
||||
macro TO_STRING(arg) = (%_ToString(arg));
|
||||
|
||||
macro DEFINE_METHOD_LEN(obj, method_def, len) = %DefineMethodsInternal(obj, class { method_def }, len);
|
||||
macro DEFINE_METHOD(obj, method_def) = DEFINE_METHOD_LEN(obj, method_def, -1);
|
||||
|
||||
# Constants. The compiler constant folds them.
|
||||
define UNDEFINED = (void 0);
|
209
tools/js2c.py
209
tools/js2c.py
@ -95,161 +95,6 @@ def ExpandConstants(lines, constants):
|
||||
return lines
|
||||
|
||||
|
||||
def ExpandMacroDefinition(lines, pos, name_pattern, macro, expander):
|
||||
pattern_match = name_pattern.search(lines, pos)
|
||||
while pattern_match is not None:
|
||||
# Scan over the arguments
|
||||
height = 1
|
||||
start = pattern_match.start()
|
||||
end = pattern_match.end()
|
||||
assert lines[end - 1] == '('
|
||||
last_match = end
|
||||
arg_index = [0] # Wrap state into array, to work around Python "scoping"
|
||||
mapping = { }
|
||||
def add_arg(str):
|
||||
# Remember to expand recursively in the arguments
|
||||
if arg_index[0] >= len(macro.args):
|
||||
lineno = lines.count(os.linesep, 0, start) + 1
|
||||
raise Error('line %s: Too many arguments for macro "%s"' % (lineno, name_pattern.pattern))
|
||||
replacement = expander(str.strip())
|
||||
mapping[macro.args[arg_index[0]]] = replacement
|
||||
arg_index[0] += 1
|
||||
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])
|
||||
if arg_index[0] < len(macro.args) -1:
|
||||
lineno = lines.count(os.linesep, 0, start) + 1
|
||||
raise Error('line %s: Too few arguments for macro "%s"' % (lineno, name_pattern.pattern))
|
||||
result = macro.expand(mapping)
|
||||
# Replace the occurrence of the macro with the expansion
|
||||
lines = lines[:start] + result + lines[end:]
|
||||
pattern_match = name_pattern.search(lines, start + len(result))
|
||||
return lines
|
||||
|
||||
def ExpandMacros(lines, macros):
|
||||
# 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):
|
||||
def expander(s):
|
||||
return ExpandMacros(s, macros)
|
||||
lines = ExpandMacroDefinition(lines, 0, name_pattern, macro, expander)
|
||||
return lines
|
||||
|
||||
class TextMacro:
|
||||
def __init__(self, args, body):
|
||||
self.args = args
|
||||
self.body = body
|
||||
def expand(self, mapping):
|
||||
# Keys could be substrings of earlier values. To avoid unintended
|
||||
# clobbering, apply all replacements simultaneously.
|
||||
any_key_pattern = "|".join(re.escape(k) for k in mapping.iterkeys())
|
||||
def replace(match):
|
||||
return mapping[match.group(0)]
|
||||
return re.sub(any_key_pattern, replace, self.body)
|
||||
|
||||
CONST_PATTERN = re.compile(r'^define\s+([a-zA-Z0-9_]+)\s*=\s*([^;]*);$')
|
||||
MACRO_PATTERN = re.compile(r'^macro\s+([a-zA-Z0-9_]+)\s*\(([^)]*)\)\s*=\s*([^;]*);$')
|
||||
|
||||
|
||||
def ReadMacros(lines):
|
||||
constants = []
|
||||
macros = []
|
||||
for line in lines.split('\n'):
|
||||
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()
|
||||
constants.append((re.compile("\\b%s\\b" % name), value))
|
||||
else:
|
||||
macro_match = MACRO_PATTERN.match(line)
|
||||
if macro_match:
|
||||
name = macro_match.group(1)
|
||||
args = [match.strip() for match in macro_match.group(2).split(',')]
|
||||
body = macro_match.group(3).strip()
|
||||
macros.append((re.compile("\\b%s\\(" % name), TextMacro(args, body)))
|
||||
else:
|
||||
raise Error("Illegal line: " + line)
|
||||
return (constants, macros)
|
||||
|
||||
|
||||
TEMPLATE_PATTERN = re.compile(r'^\s+T\(([A-Z][a-zA-Z0-9]*),')
|
||||
|
||||
def ReadMessageTemplates(lines):
|
||||
templates = []
|
||||
index = 0
|
||||
for line in lines.split('\n'):
|
||||
template_match = TEMPLATE_PATTERN.match(line)
|
||||
if template_match:
|
||||
name = "k%s" % template_match.group(1)
|
||||
value = index
|
||||
index = index + 1
|
||||
templates.append((re.compile("\\b%s\\b" % name), value))
|
||||
return templates
|
||||
|
||||
INLINE_MACRO_PATTERN = re.compile(r'macro\s+([a-zA-Z0-9_]+)\s*\(([^)]*)\)\s*\n')
|
||||
INLINE_MACRO_END_PATTERN = re.compile(r'endmacro\s*\n')
|
||||
|
||||
def ExpandInlineMacros(lines):
|
||||
pos = 0
|
||||
while True:
|
||||
macro_match = INLINE_MACRO_PATTERN.search(lines, pos)
|
||||
if macro_match is None:
|
||||
# no more macros
|
||||
return lines
|
||||
name = macro_match.group(1)
|
||||
args = [match.strip() for match in macro_match.group(2).split(',')]
|
||||
end_macro_match = INLINE_MACRO_END_PATTERN.search(lines, macro_match.end());
|
||||
if end_macro_match is None:
|
||||
raise Error("Macro %s unclosed" % name)
|
||||
body = lines[macro_match.end():end_macro_match.start()]
|
||||
|
||||
# remove macro definition
|
||||
lines = lines[:macro_match.start()] + lines[end_macro_match.end():]
|
||||
name_pattern = re.compile("\\b%s\\(" % name)
|
||||
macro = TextMacro(args, body)
|
||||
|
||||
# advance position to where the macro definition was
|
||||
pos = macro_match.start()
|
||||
|
||||
def non_expander(s):
|
||||
return s
|
||||
lines = ExpandMacroDefinition(lines, pos, name_pattern, macro, non_expander)
|
||||
|
||||
|
||||
INLINE_CONSTANT_PATTERN = re.compile(r'define\s+([a-zA-Z0-9_]+)\s*=\s*([^;\n]+);\n')
|
||||
|
||||
def ExpandInlineConstants(lines):
|
||||
pos = 0
|
||||
while True:
|
||||
const_match = INLINE_CONSTANT_PATTERN.search(lines, pos)
|
||||
if const_match is None:
|
||||
# no more constants
|
||||
return lines
|
||||
name = const_match.group(1)
|
||||
replacement = const_match.group(2)
|
||||
name_pattern = re.compile("\\b%s\\b" % name)
|
||||
|
||||
# remove constant definition and replace
|
||||
lines = (lines[:const_match.start()] +
|
||||
re.sub(name_pattern, replacement, lines[const_match.end():]))
|
||||
|
||||
# advance position to where the constant definition was
|
||||
pos = const_match.start()
|
||||
|
||||
|
||||
HEADER_TEMPLATE = """\
|
||||
// Copyright 2011 Google Inc. All Rights Reserved.
|
||||
|
||||
@ -317,32 +162,16 @@ GET_SCRIPT_NAME_CASE = """\
|
||||
"""
|
||||
|
||||
|
||||
def BuildFilterChain(macro_filename, message_template_file):
|
||||
def BuildFilterChain():
|
||||
"""Build the chain of filter functions to be applied to the sources.
|
||||
|
||||
Args:
|
||||
macro_filename: Name of the macro file, if any.
|
||||
|
||||
Returns:
|
||||
A function (string -> string) that processes a source file.
|
||||
"""
|
||||
filter_chain = []
|
||||
|
||||
if macro_filename:
|
||||
(consts, macros) = ReadMacros(ReadFile(macro_filename))
|
||||
filter_chain.append(lambda l: ExpandMacros(l, macros))
|
||||
filter_chain.append(lambda l: ExpandConstants(l, consts))
|
||||
|
||||
if message_template_file:
|
||||
message_templates = ReadMessageTemplates(ReadFile(message_template_file))
|
||||
filter_chain.append(lambda l: ExpandConstants(l, message_templates))
|
||||
|
||||
filter_chain.extend([
|
||||
filter_chain = [
|
||||
RemoveCommentsEmptyLinesAndWhitespace,
|
||||
ExpandInlineMacros,
|
||||
ExpandInlineConstants,
|
||||
Validate,
|
||||
])
|
||||
]
|
||||
|
||||
def chain(f1, f2):
|
||||
return lambda x: f2(f1(x))
|
||||
@ -357,19 +186,11 @@ class Sources:
|
||||
self.names = []
|
||||
self.modules = []
|
||||
|
||||
def IsMacroFile(filename):
|
||||
return filename.endswith("macros.py")
|
||||
|
||||
def IsMessageTemplateFile(filename):
|
||||
return filename.endswith("message-template.h")
|
||||
|
||||
|
||||
def PrepareSources(source_files, native_type, emit_js):
|
||||
"""Read, prepare and assemble the list of source files.
|
||||
|
||||
Args:
|
||||
source_files: List of JavaScript-ish source files. A file named macros.py
|
||||
will be treated as a list of macros.
|
||||
source_files: List of JavaScript-ish source files.
|
||||
native_type: String corresponding to a NativeType enum value, allowing us
|
||||
to treat different types of sources differently.
|
||||
emit_js: True if we should skip the byte conversion and just leave the
|
||||
@ -378,25 +199,7 @@ def PrepareSources(source_files, native_type, emit_js):
|
||||
Returns:
|
||||
An instance of Sources.
|
||||
"""
|
||||
macro_file = None
|
||||
macro_files = filter(IsMacroFile, source_files)
|
||||
assert len(macro_files) in [0, 1]
|
||||
if macro_files:
|
||||
source_files.remove(macro_files[0])
|
||||
macro_file = macro_files[0]
|
||||
|
||||
message_template_file = None
|
||||
message_template_files = filter(IsMessageTemplateFile, source_files)
|
||||
assert len(message_template_files) in [0, 1]
|
||||
if message_template_files:
|
||||
source_files.remove(message_template_files[0])
|
||||
message_template_file = message_template_files[0]
|
||||
|
||||
filters = None
|
||||
if native_type in ("EXTRAS"):
|
||||
filters = BuildExtraFilterChain()
|
||||
else:
|
||||
filters = BuildFilterChain(macro_file, message_template_file)
|
||||
filters = BuildFilterChain()
|
||||
|
||||
source_files_and_contents = [(f, ReadFile(f)) for f in source_files]
|
||||
|
||||
@ -552,7 +355,7 @@ def main():
|
||||
parser.set_usage("""js2c out.cc type sources.js ...
|
||||
out.cc: C code to be generated.
|
||||
type: type parameter for NativesCollection template.
|
||||
sources.js: JS internal sources or macros.py.""")
|
||||
sources.js: JS internal sources.""")
|
||||
(options, args) = parser.parse_args()
|
||||
JS2C(args[2:],
|
||||
args[0],
|
||||
|
Loading…
Reference in New Issue
Block a user