Commit 8f2397f2 authored by Yuki Shiino's avatar Yuki Shiino Committed by Commit Bot

bind-gen: Implement IDL interface code generation (1 of N)

Splits the contents of example.py into interface.py and
several files.

blink_v8_bridge.py will support Blink-V8 type and value
conversions.

codegen_utils.py is currently a collection of misc util
functions, and should be better organized later.

Bug: 839389
Change-Id: I609be076533106e4e98865add162f40f965398dd
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1892974Reviewed-by: default avatarHitoshi Yoshida <peria@chromium.org>
Commit-Queue: Yuki Shiino <yukishiino@chromium.org>
Cr-Commit-Position: refs/heads/master@{#712554}
parent fd871bde
......@@ -16,6 +16,9 @@ def _setup_sys_path():
root_dir = os.path.join(this_dir, *(['..'] * expected_path.count('/')))
sys.path = [
# //third_party/blink/renderer/bindings/scripts/web_idl
os.path.join(root_dir, 'third_party', 'blink', 'renderer', 'bindings',
'scripts'),
# //third_party/blink/renderer/build/scripts/blinkbuild
os.path.join(root_dir, 'third_party', 'blink', 'renderer', 'build',
'scripts'),
......@@ -47,7 +50,9 @@ def _setup_clang_format():
_setup_clang_format()
from .example import run_example
from .interface import generate_interfaces
__all__ = [
"generate_interfaces",
"run_example",
]
# Copyright 2019 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
from .code_node import CodeNode
from .code_node import SymbolDefinitionNode
from .code_node import SymbolNode
from .code_node import SymbolScopeNode
from .code_node import TextNode
from .code_node import UnlikelyExitNode
import web_idl
_format = CodeNode.format_template
def make_v8_to_blink_value(blink_var_name, v8_value_expr, idl_type):
"""
Returns a SymbolNode whose definition converts a v8::Value to a Blink value.
"""
assert isinstance(blink_var_name, str)
assert isinstance(v8_value_expr, str)
assert isinstance(idl_type, web_idl.IdlType)
pattern = "NativeValueTraits<{_1}>::NativeValue({_2})"
_1 = "IDL{}".format(idl_type.type_name)
_2 = ["${isolate}", v8_value_expr, "${exception_state}"]
blink_value = _format(pattern, _1=_1, _2=", ".join(_2))
idl_type_tag = _1
pattern = "{_1}& ${{{_2}}} = {_3};"
_1 = "NativeValueTraits<{}>::ImplType".format(idl_type_tag)
_2 = blink_var_name
_3 = blink_value
text = _format(pattern, _1=_1, _2=_2, _3=_3)
def create_definition(symbol_node):
return SymbolDefinitionNode(symbol_node, [
TextNode(text),
UnlikelyExitNode(
cond=TextNode("${exception_state}.HadException()"),
body=SymbolScopeNode([TextNode("return;")])),
])
return SymbolNode(blink_var_name, definition_constructor=create_definition)
......@@ -12,24 +12,17 @@ from .code_node import SymbolNode
from .code_node import SymbolScopeNode
from .code_node import TextNode
from .code_node import UnlikelyExitNode
from .codegen_utils import render_code_node
from .mako_renderer import MakoRenderer
class CodeNodeTest(unittest.TestCase):
def render(self, node):
prev = ""
current = str(node)
while current != prev:
prev = current
current = str(node)
return current
def assertRenderResult(self, node, expected):
def simplify(text):
return "\n".join(
[" ".join(line.split()) for line in text.split("\n")])
actual = simplify(self.render(node))
actual = simplify(render_code_node(node))
expected = simplify(expected)
self.assertEqual(actual, expected)
......
# Copyright 2019 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
from .clang_format import clang_format
from .code_node import CodeNode
from .code_node import LiteralNode
from .code_node import SymbolScopeNode
def make_copyright_header():
return LiteralNode("""\
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.\
""")
def enclose_with_header_guard(code_node, header_guard):
assert isinstance(code_node, CodeNode)
assert isinstance(header_guard, str)
return SymbolScopeNode([
LiteralNode("#ifndef {}".format(header_guard)),
LiteralNode("#define {}".format(header_guard)),
LiteralNode(""),
code_node,
LiteralNode(""),
LiteralNode("#endif // {}".format(header_guard)),
])
def enclose_with_namespace(code_node, namespace):
assert isinstance(code_node, CodeNode)
assert isinstance(namespace, str)
return SymbolScopeNode([
LiteralNode("namespace {} {{".format(namespace)),
LiteralNode(""),
code_node,
LiteralNode(""),
LiteralNode("}} // namespace {}".format(namespace)),
])
def render_code_node(code_node):
"""
Renders |code_node| and turns it into text letting |code_node| apply all
necessary changes (side effects). Returns the resulting text.
"""
prev = "_"
current = ""
while current != prev:
prev = current
current = str(code_node)
return current
def write_code_node_to_file(code_node, filepath):
"""Renders |code_node| and then write the result to |filepath|."""
assert isinstance(code_node, CodeNode)
assert isinstance(filepath, str)
rendered_text = render_code_node(code_node)
format_result = clang_format(rendered_text, filename=filepath)
with open(filepath, "w") as output_file:
output_file.write(format_result.contents)
......@@ -40,6 +40,7 @@ def main():
dispatch_table = {
'example': bind_gen.run_example,
'interface': bind_gen.generate_interfaces,
}
for task in tasks:
......
......@@ -27,11 +27,15 @@
../../build/scripts/blinkbuild/__init__.py
../../build/scripts/blinkbuild/name_style_converter.py
bind_gen/__init__.py
bind_gen/blink_v8_bridge.py
bind_gen/clang_format.py
bind_gen/code_generation_context.py
bind_gen/code_node.py
bind_gen/codegen_utils.py
bind_gen/example.py
bind_gen/interface.py
bind_gen/mako_renderer.py
bind_gen/name_style.py
generate_bindings.py
web_idl/__init__.py
web_idl/argument.py
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment