diff options
27 files changed, 184 insertions, 1284 deletions
diff --git a/mojo/public/bindings/generators/cpp_templates/enum_declaration b/mojo/public/bindings/generators/cpp_templates/enum_declaration deleted file mode 100644 index b7cf30b..0000000 --- a/mojo/public/bindings/generators/cpp_templates/enum_declaration +++ /dev/null @@ -1,3 +0,0 @@ -enum $NAME { -$ENUM_FIELDS -}; diff --git a/mojo/public/bindings/generators/cpp_templates/interface_declaration b/mojo/public/bindings/generators/cpp_templates/interface_declaration deleted file mode 100644 index 3fa1b34..0000000 --- a/mojo/public/bindings/generators/cpp_templates/interface_declaration +++ /dev/null @@ -1,11 +0,0 @@ -class ${CLASS}Proxy; -class ${CLASS}Stub; -class $PEER; - -class $CLASS { - public: - typedef ${CLASS}Proxy _Proxy; - typedef ${CLASS}Stub _Stub; - typedef $PEER _Peer; -$METHODS -}; diff --git a/mojo/public/bindings/generators/cpp_templates/interface_definition b/mojo/public/bindings/generators/cpp_templates/interface_definition deleted file mode 100644 index ac357c5..0000000 --- a/mojo/public/bindings/generators/cpp_templates/interface_definition +++ /dev/null @@ -1,6 +0,0 @@ -$CLASS::$CLASS(mojo::MessageReceiver* receiver) - : receiver_(receiver) { -} - -$PROXY_DEFINITIONS -$STUB_DEFINITION diff --git a/mojo/public/bindings/generators/cpp_templates/interface_proxy_declaration b/mojo/public/bindings/generators/cpp_templates/interface_proxy_declaration deleted file mode 100644 index a49e004..0000000 --- a/mojo/public/bindings/generators/cpp_templates/interface_proxy_declaration +++ /dev/null @@ -1,9 +0,0 @@ -class ${CLASS}Proxy : public $CLASS { - public: - explicit ${CLASS}Proxy(mojo::MessageReceiver* receiver); - -$METHODS - - private: - mojo::MessageReceiver* receiver_; -}; diff --git a/mojo/public/bindings/generators/cpp_templates/interface_stub_case b/mojo/public/bindings/generators/cpp_templates/interface_stub_case deleted file mode 100644 index 808cdb3..0000000 --- a/mojo/public/bindings/generators/cpp_templates/interface_stub_case +++ /dev/null @@ -1,11 +0,0 @@ - case internal::k${CLASS}_${METHOD}_Name: { - internal::${CLASS}_${METHOD}_Params_Data* params = - reinterpret_cast<internal::${CLASS}_${METHOD}_Params_Data*>( - message->data->payload); - - if (!mojo::internal::DecodePointersAndHandles(params, message)) - return false; - $METHOD_CALL - mojo::internal::CloseHandles(params); - break; - } diff --git a/mojo/public/bindings/generators/cpp_templates/interface_stub_declaration b/mojo/public/bindings/generators/cpp_templates/interface_stub_declaration deleted file mode 100644 index 735f3a47..0000000 --- a/mojo/public/bindings/generators/cpp_templates/interface_stub_declaration +++ /dev/null @@ -1,4 +0,0 @@ -class ${CLASS}Stub : public $CLASS, public mojo::MessageReceiver { - public: - virtual bool Accept(mojo::Message* message) MOJO_OVERRIDE; -}; diff --git a/mojo/public/bindings/generators/cpp_templates/interface_stub_definition b/mojo/public/bindings/generators/cpp_templates/interface_stub_definition deleted file mode 100644 index e9a8340..0000000 --- a/mojo/public/bindings/generators/cpp_templates/interface_stub_definition +++ /dev/null @@ -1,6 +0,0 @@ -bool ${CLASS}Stub::Accept(mojo::Message* message) { - switch (message->data->header.name) { -$CASES - } - return true; -} diff --git a/mojo/public/bindings/generators/cpp_templates/module.cc-template b/mojo/public/bindings/generators/cpp_templates/module.cc-template deleted file mode 100644 index e8100fb..0000000 --- a/mojo/public/bindings/generators/cpp_templates/module.cc-template +++ /dev/null @@ -1,46 +0,0 @@ -// Copyright $YEAR 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. - -#include "$HEADER" - -#include "mojo/public/bindings/lib/message_builder.h" -#include "$HEADER" - -namespace $NAMESPACE { -namespace internal { -namespace { - -#pragma pack(push, 1) -#if defined(__clang__) -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wunused-private-field" -#endif -$PARAM_DEFINITIONS -#if defined(__clang__) -#pragma clang diagnostic pop -#endif -#pragma pack(pop) - -} // namespace - -$STRUCT_DEFINITIONS - -$STRUCT_DESTRUCTORS - -} // namespace internal - -$STRUCT_BUILDER_DEFINITIONS - -$INTERFACE_DEFINITIONS -} // namespace $NAMESPACE - -namespace mojo { -namespace internal { - -$STRUCT_SERIALIZATION_DEFINITIONS - -$INTERFACE_SERIALIZATION_DEFINITIONS - -} // namespace internal -} // namespace mojo diff --git a/mojo/public/bindings/generators/cpp_templates/module.h-template b/mojo/public/bindings/generators/cpp_templates/module.h-template deleted file mode 100644 index c6800cc..0000000 --- a/mojo/public/bindings/generators/cpp_templates/module.h-template +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright $YEAR 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. - -#ifndef $HEADER_GUARD -#define $HEADER_GUARD - -#include "mojo/public/bindings/lib/bindings.h" -#include "mojo/public/bindings/lib/message.h" -#include "$INTERNAL_HEADER" - -namespace $NAMESPACE { - -$ENUM_DECLARATIONS -$WRAPPER_CLASS_DECLARATIONS -$INTERFACE_CLASS_DECLARATIONS -$INTERFACE_PROXY_DECLARATIONS -$INTERFACE_STUB_DECLARATIONS - -} // namespace $NAMESPACE - -#endif // $HEADER_GUARD diff --git a/mojo/public/bindings/generators/cpp_templates/module_internal.h-template b/mojo/public/bindings/generators/cpp_templates/module_internal.h-template deleted file mode 100644 index 4799a8a..0000000 --- a/mojo/public/bindings/generators/cpp_templates/module_internal.h-template +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright $YEAR 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. - -#ifndef $HEADER_GUARD -#define $HEADER_GUARD - -#include "mojo/public/bindings/lib/bindings_serialization.h" - -namespace $NAMESPACE { - -$WRAPPER_FORWARD_DECLARATIONS - -namespace internal { - -#pragma pack(push, 1) - -$STRUCT_CLASS_DECLARATIONS -#pragma pack(pop) - -} // namespace internal -} // namespace $NAMESPACE - -namespace mojo { -namespace internal { - -$TRAITS - -} // namespace internal -} // namespace mojo - -#endif // $HEADER_GUARD diff --git a/mojo/public/bindings/generators/cpp_templates/params_definition b/mojo/public/bindings/generators/cpp_templates/params_definition deleted file mode 100644 index b0d646b..0000000 --- a/mojo/public/bindings/generators/cpp_templates/params_definition +++ /dev/null @@ -1,25 +0,0 @@ -const uint32_t $METHOD_NAME = $METHOD_ID; -class $CLASS { - public: - static $CLASS* New(mojo::Buffer* buf) { - return new (buf->Allocate(sizeof($CLASS))) - $CLASS(); - } - -$SETTERS - -$GETTERS - - private: - friend class mojo::internal::ObjectTraits<$CLASS>; - - $CLASS() { - _header_.num_bytes = sizeof(*this); - _header_.num_fields = 3; - } - - mojo::internal::StructHeader _header_; -$FIELDS -}; -MOJO_COMPILE_ASSERT(sizeof($CLASS) == $SIZE, - bad_sizeof_${CLASS}); diff --git a/mojo/public/bindings/generators/cpp_templates/params_serialization b/mojo/public/bindings/generators/cpp_templates/params_serialization deleted file mode 100644 index 0a89cd9..0000000 --- a/mojo/public/bindings/generators/cpp_templates/params_serialization +++ /dev/null @@ -1,22 +0,0 @@ -template <> -class ObjectTraits<$CLASS> { - public: - static void CloseHandles($CLASS* $NAME) { -$CLOSES - } - - static void EncodePointersAndHandles( - $CLASS* $NAME, - std::vector<Handle>* handles) { -$ENCODES -$ENCODE_HANDLES - } - - static bool DecodePointersAndHandles( - $CLASS* $NAME, - Message* message) { -$DECODES -$DECODE_HANDLES - return true; - } -}; diff --git a/mojo/public/bindings/generators/cpp_templates/proxy_implementation b/mojo/public/bindings/generators/cpp_templates/proxy_implementation deleted file mode 100644 index 96cbce1..0000000 --- a/mojo/public/bindings/generators/cpp_templates/proxy_implementation +++ /dev/null @@ -1,20 +0,0 @@ -void ${CLASS}Proxy::$METHOD($PARAMS_LIST) { - size_t payload_size = - mojo::internal::Align(sizeof($PARAMS)); -$COMPUTES - - mojo::internal::MessageBuilder builder($NAME, payload_size); - - $PARAMS* params = - $PARAMS::New(builder.buffer()); - -$SETS - - mojo::Message message; - mojo::internal::EncodePointersAndHandles(params, &message.handles); - - message.data = builder.Finish(); - - receiver_->Accept(&message); -} - diff --git a/mojo/public/bindings/generators/cpp_templates/struct_builder_definition b/mojo/public/bindings/generators/cpp_templates/struct_builder_definition deleted file mode 100644 index 0bc36d7..0000000 --- a/mojo/public/bindings/generators/cpp_templates/struct_builder_definition +++ /dev/null @@ -1,9 +0,0 @@ -$CLASS::Builder::Builder(mojo::Buffer* buf) - : data_(Data::New(buf, $DTOR)) { -} - -$CLASS $CLASS::Builder::Finish() { - Data* data = NULL; - std::swap(data, data_); - return mojo::internal::Wrap(data); -} diff --git a/mojo/public/bindings/generators/cpp_templates/struct_declaration b/mojo/public/bindings/generators/cpp_templates/struct_declaration deleted file mode 100644 index a780d65..0000000 --- a/mojo/public/bindings/generators/cpp_templates/struct_declaration +++ /dev/null @@ -1,21 +0,0 @@ -class $CLASS { - public: - typedef $WRAPPER Wrapper; - - static $CLASS* New(mojo::Buffer* buf, mojo::Buffer::Destructor dtor = NULL); - -$SETTERS - -$GETTERS - - private: - friend class mojo::internal::ObjectTraits<$CLASS>; - - $CLASS(); - ~$CLASS(); // NOT IMPLEMENTED - - mojo::internal::StructHeader _header_; -$FIELDS -}; -MOJO_COMPILE_ASSERT(sizeof($CLASS) == $SIZE, - bad_sizeof_$CLASS); diff --git a/mojo/public/bindings/generators/cpp_templates/struct_definition b/mojo/public/bindings/generators/cpp_templates/struct_definition deleted file mode 100644 index e69d787..0000000 --- a/mojo/public/bindings/generators/cpp_templates/struct_definition +++ /dev/null @@ -1,9 +0,0 @@ -// static -$CLASS* $CLASS::New(mojo::Buffer* buf, mojo::Buffer::Destructor dtor) { - return new (buf->Allocate(sizeof($CLASS), dtor)) $CLASS(); -} - -$CLASS::$CLASS() { - _header_.num_bytes = sizeof(*this); - _header_.num_fields = $NUM_FIELDS; -} diff --git a/mojo/public/bindings/generators/cpp_templates/struct_destructor b/mojo/public/bindings/generators/cpp_templates/struct_destructor deleted file mode 100644 index 6b5e67e..0000000 --- a/mojo/public/bindings/generators/cpp_templates/struct_destructor +++ /dev/null @@ -1,4 +0,0 @@ -static void ${CLASS}_Data_Destructor(void* address) { - ${CLASS}_Data* data = static_cast<${CLASS}_Data*>(address); - $BODY -} diff --git a/mojo/public/bindings/generators/cpp_templates/struct_serialization b/mojo/public/bindings/generators/cpp_templates/struct_serialization deleted file mode 100644 index c321c82..0000000 --- a/mojo/public/bindings/generators/cpp_templates/struct_serialization +++ /dev/null @@ -1,14 +0,0 @@ -// static -void ObjectTraits<$CLASS>::EncodePointersAndHandles( - $CLASS* $NAME, std::vector<mojo::Handle>* handles) { -$ENCODES -$ENCODE_HANDLES -} - -// static -bool ObjectTraits<$CLASS>::DecodePointersAndHandles( - $CLASS* $NAME, mojo::Message* message) { -$DECODES -$DECODE_HANDLES - return true; -} diff --git a/mojo/public/bindings/generators/cpp_templates/struct_serialization_definition b/mojo/public/bindings/generators/cpp_templates/struct_serialization_definition deleted file mode 100644 index b404276..0000000 --- a/mojo/public/bindings/generators/cpp_templates/struct_serialization_definition +++ /dev/null @@ -1,24 +0,0 @@ -// static -size_t ObjectTraits<$CLASS>::ComputeSizeOf( - const $CLASS* $NAME) { -$SIZES -} - -// static -$CLASS* ObjectTraits<$CLASS>::Clone( - const $CLASS* $NAME, Buffer* buf) { - $CLASS* clone = $CLASS::New(buf); - memcpy(clone, $NAME, sizeof(*$NAME)); -$CLONES -$HANDLE_RELEASES - return clone; -} - -// static -void ObjectTraits<$CLASS>::CloseHandles( - $CLASS* $NAME) { -$CLOSES -$DTOR -} - -$SERIALIZATION diff --git a/mojo/public/bindings/generators/cpp_templates/struct_serialization_traits b/mojo/public/bindings/generators/cpp_templates/struct_serialization_traits deleted file mode 100644 index 9975742..0000000 --- a/mojo/public/bindings/generators/cpp_templates/struct_serialization_traits +++ /dev/null @@ -1,11 +0,0 @@ -template <> -class ObjectTraits<$FULL_CLASS> { - public: - static size_t ComputeSizeOf(const $FULL_CLASS* $NAME); - static $FULL_CLASS* Clone(const $FULL_CLASS* $NAME, Buffer* buf); - static void CloseHandles($FULL_CLASS* $NAME); - static void EncodePointersAndHandles($FULL_CLASS* $NAME, - std::vector<mojo::Handle>* handles); - static bool DecodePointersAndHandles($FULL_CLASS* $NAME, - mojo::Message* message); -}; diff --git a/mojo/public/bindings/generators/cpp_templates/template_declaration b/mojo/public/bindings/generators/cpp_templates/template_declaration deleted file mode 100644 index fe9c82c..0000000 --- a/mojo/public/bindings/generators/cpp_templates/template_declaration +++ /dev/null @@ -1,12 +0,0 @@ -template <> -class ObjectTraits<$NAMESPACE::internal::$CLASS> { - public: - static void CloseHandles( - $NAMESPACE::internal::$CLASS* params); - static void EncodePointersAndHandles( - $NAMESPACE::internal::$CLASS* params, - std::vector<mojo::Handle>* handles); - static bool DecodePointersAndHandles( - $NAMESPACE::internal::$CLASS* params, - mojo::Message* message); -}; diff --git a/mojo/public/bindings/generators/cpp_templates/wrapper_class_declaration b/mojo/public/bindings/generators/cpp_templates/wrapper_class_declaration deleted file mode 100644 index f387607..0000000 --- a/mojo/public/bindings/generators/cpp_templates/wrapper_class_declaration +++ /dev/null @@ -1,54 +0,0 @@ -class $CLASS { - public: - typedef internal::${CLASS}_Data Data; - - $CLASS() : data_(NULL) { - } - - template <typename U> - $CLASS(const U& u, mojo::Buffer* buf = mojo::Buffer::current()) { - *this = mojo::TypeConverter<$CLASS,U>::ConvertFrom(u, buf); - } - - template <typename U> - $CLASS& operator=(const U& u) { - *this = - mojo::TypeConverter<$CLASS,U>::ConvertFrom(u, mojo::Buffer::current()); - return *this; - } - - template <typename U> - operator U() const { - return To<U>(); - } - - template <typename U> - U To() const { - return mojo::TypeConverter<$CLASS,U>::ConvertTo(*this); - } - - bool is_null() const { return !data_; } - -$GETTERS - - class Builder { - public: - explicit Builder(mojo::Buffer* buf = mojo::Buffer::current()); - -$SETTERS - - $CLASS Finish(); - - private: - $CLASS::Data* data_; - MOJO_DISALLOW_COPY_AND_ASSIGN(Builder); - }; - - private: - friend class mojo::internal::WrapperHelper<$CLASS>; - - struct Wrap {}; - $CLASS(Wrap, const Data* data) : data_(data) {} - - const Data* data_; -}; diff --git a/mojo/public/bindings/generators/mojom_cpp_generator.py b/mojo/public/bindings/generators/mojom_cpp_generator.py deleted file mode 100644 index 1c6a2ec..0000000 --- a/mojo/public/bindings/generators/mojom_cpp_generator.py +++ /dev/null @@ -1,677 +0,0 @@ -# Copyright 2013 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. - -"""Generates C++ source files from a mojom.Module.""" - -import datetime -import mojom -import mojom_generator -import mojom_pack -import os -import re -import sys - -from string import Template - -# mojom_cpp_generator provides a way to generate c++ code from a mojom.Module. -# cpp = mojom_cpp_generator.CPPGenerator(module) -# cpp.GenerateFiles("/tmp/g") - -class Lines(object): - """Helper class to maintain list of template expanded lines.""" - def __init__(self, template, indent = None): - self.template = template - self.indent = indent - self.lines = [] - - def Add(self, map = {}, **substitutions): - if len(substitutions) > 0: - map = map.copy() - map.update(substitutions) - - self.lines.append(self.template.substitute(map)) - - def __repr__(self): - if self.indent is not None: - prefix = "".ljust(self.indent, ' ') - repr = '\n'.join(self.lines) - self.lines = map(lambda l: prefix + l, repr.splitlines()) - return '\n'.join(self.lines) - - -class CPPGenerator(mojom_generator.Generator): - - struct_serialization_compute_template = \ - Template(" +\n mojo::internal::ComputeSizeOf($NAME->$FIELD())") - struct_serialization_clone_template = Template( - " clone->set_$FIELD(mojo::internal::Clone($NAME->$FIELD(), buf));") - struct_serialization_handle_release_template = Template( - " mojo::internal::ResetIfNonNull($NAME->$FIELD());") - struct_serialization_encode_template = Template( - " Encode(&$NAME->${FIELD}_, handles);") - struct_serialization_encode_handle_template = Template( - " EncodeHandle(&$NAME->${FIELD}_, handles);") - struct_serialization_decode_template = Template( - " if (!Decode(&$NAME->${FIELD}_, message))\n" - " return false;") - struct_serialization_decode_handle_template = Template( - " if (!DecodeHandle(&$NAME->${FIELD}_, &message->handles))\n" - " return false;") - struct_destructor_body_template = Template( - "(void) mojo::MakePassable(data->$FIELD()).Pass();") - close_handles_template = Template( - " mojo::internal::CloseHandles($NAME->${FIELD}_.ptr);") - param_set_template = Template( - " params->set_$NAME($NAME);") - param_handle_set_template = Template( - " params->set_$NAME($NAME.release());") - param_struct_set_template = Template( - " params->set_$NAME(\n" - " mojo::internal::Clone(mojo::internal::Unwrap($NAME),\n" - " builder.buffer()));") - param_struct_compute_template = Template( - " payload_size += mojo::internal::ComputeSizeOf(\n" - " mojo::internal::Unwrap($NAME));") - field_template = Template(" $TYPE ${FIELD}_;") - bool_field_template = Template(" uint8_t ${FIELD}_ : 1;") - handle_field_template = Template(" mutable $TYPE ${FIELD}_;") - setter_template = \ - Template(" void set_$FIELD($TYPE $FIELD) { ${FIELD}_ = $FIELD; }") - ptr_setter_template = \ - Template(" void set_$FIELD($TYPE $FIELD) { ${FIELD}_.ptr = $FIELD; }") - getter_template = \ - Template(" $TYPE $FIELD() const { return ${FIELD}_; }") - ptr_getter_template = \ - Template(" const $TYPE $FIELD() const { return ${FIELD}_.ptr; }") - handle_getter_template = \ - Template(" $TYPE* $FIELD() const { return &${FIELD}_; }") - enum_field_template = Template(" $NAME$EQUALS$VALUE,") - wrapper_setter_template = \ - Template(" void set_$FIELD($TYPE $FIELD) { " \ - "data_->set_$FIELD($FIELD); }") - wrapper_obj_setter_template = \ - Template(" void set_$FIELD($TYPE $FIELD) { " \ - "data_->set_$FIELD(mojo::internal::Unwrap($FIELD)); }") - wrapper_handle_setter_template = \ - Template(" void set_$FIELD($TYPE $FIELD) { " \ - "data_->set_$FIELD($FIELD.release()); }") - wrapper_getter_template = \ - Template(" $TYPE $FIELD() const { return data_->$FIELD(); }") - wrapper_obj_getter_template = \ - Template(" const $TYPE $FIELD() const { " \ - "return mojo::internal::Wrap(data_->$FIELD()); }") - wrapper_handle_getter_template = \ - Template(" $TYPE $FIELD() const { " \ - "return mojo::MakePassable(data_->$FIELD()); }") - pad_template = Template(" uint8_t _pad${COUNT}_[$PAD];") - templates = {} - HEADER_SIZE = 8 - - kind_to_type = { - mojom.BOOL: "bool", - mojom.INT8: "int8_t", - mojom.UINT8: "uint8_t", - mojom.INT16: "int16_t", - mojom.UINT16: "uint16_t", - mojom.INT32: "int32_t", - mojom.UINT32: "uint32_t", - mojom.FLOAT: "float", - mojom.HANDLE: "mojo::Handle", - mojom.MSGPIPE: "mojo::MessagePipeHandle", - mojom.INT64: "int64_t", - mojom.UINT64: "uint64_t", - mojom.DOUBLE: "double", - } - - @classmethod - def GetTemplate(cls, template_name): - if template_name not in cls.templates: - dir = os.path.dirname(__file__) - filename = os.path.join(dir, 'cpp_templates', template_name) - filename = filename.replace('.h', '.h-template') - filename = filename.replace('.cc', '.cc-template') - with open(filename, 'r') as file: - template = Template(file.read()) - cls.templates[template_name] = template - return cls.templates[template_name] - - @classmethod - def GetType(cls, kind): - if isinstance(kind, mojom.Struct): - return "%s_Data*" % kind.name - if isinstance(kind, mojom.Array): - return "mojo::internal::Array_Data<%s>*" % cls.GetType(kind.kind) - if kind.spec == 's': - return "mojo::internal::String_Data*" - return cls.kind_to_type[kind] - - @classmethod - def GetConstType(cls, kind): - if isinstance(kind, mojom.Struct): - return "const %s_Data*" % kind.name - if isinstance(kind, mojom.Array): - return "const mojo::internal::Array_Data<%s>*" % \ - cls.GetConstType(kind.kind) - if kind.spec == 's': - return "const mojo::internal::String_Data*" - return cls.kind_to_type[kind] - - @classmethod - def GetGetterLine(cls, field): - subs = {'FIELD': field.name, 'TYPE': cls.GetType(field.kind)} - if mojom_generator.IsObjectKind(field.kind): - return cls.ptr_getter_template.substitute(subs) - elif mojom_generator.IsHandleKind(field.kind): - return cls.handle_getter_template.substitute(subs) - else: - return cls.getter_template.substitute(subs) - - @classmethod - def GetSetterLine(cls, field): - subs = {'FIELD': field.name, 'TYPE': cls.GetType(field.kind)} - if mojom_generator.IsObjectKind(field.kind): - return cls.ptr_setter_template.substitute(subs) - else: - return cls.setter_template.substitute(subs) - - @classmethod - def GetWrapperType(cls, kind): - if isinstance(kind, mojom.Struct): - return "%s" % kind.name - if isinstance(kind, mojom.Array): - return "mojo::Array<%s >" % cls.GetWrapperType(kind.kind) - if kind.spec == 's': - return "mojo::String" - if kind.spec == 'h': - return "mojo::Passable<mojo::Handle>" - if kind.spec == 'h:m': - return "mojo::Passable<mojo::MessagePipeHandle>" - return cls.kind_to_type[kind] - - @classmethod - def GetConstWrapperType(cls, kind): - if isinstance(kind, mojom.Struct): - return "const %s&" % kind.name - if isinstance(kind, mojom.Array): - return "const mojo::Array<%s >&" % cls.GetWrapperType(kind.kind) - if kind.spec == 's': - return "const mojo::String&" - if kind.spec == 'h': - return "mojo::ScopedHandle" - if kind.spec == 'h:m': - return "mojo::ScopedMessagePipeHandle" - return cls.kind_to_type[kind] - - @classmethod - def GetWrapperGetterLine(cls, field): - subs = {'FIELD': field.name, 'TYPE': cls.GetWrapperType(field.kind)} - if mojom_generator.IsObjectKind(field.kind): - return cls.wrapper_obj_getter_template.substitute(subs) - elif mojom_generator.IsHandleKind(field.kind): - return cls.wrapper_handle_getter_template.substitute(subs) - else: - return cls.wrapper_getter_template.substitute(subs) - - @classmethod - def GetWrapperSetterLine(cls, field): - subs = {'FIELD': field.name, 'TYPE': cls.GetConstWrapperType(field.kind)} - if mojom_generator.IsObjectKind(field.kind): - return cls.wrapper_obj_setter_template.substitute(subs) - elif mojom_generator.IsHandleKind(field.kind): - return cls.wrapper_handle_setter_template.substitute(subs) - else: - return cls.wrapper_setter_template.substitute(subs) - - @classmethod - def GetFieldLine(cls, field): - kind = field.kind - if kind.spec == 'b': - return cls.bool_field_template.substitute(FIELD=field.name) - if mojom_generator.IsHandleKind(kind): - return cls.handle_field_template.substitute(FIELD=field.name, - TYPE=cls.kind_to_type[kind]) - itype = None - if isinstance(kind, mojom.Struct): - itype = "mojo::internal::StructPointer<%s_Data>" % kind.name - elif isinstance(kind, mojom.Array): - itype = "mojo::internal::ArrayPointer<%s>" % cls.GetType(kind.kind) - elif kind.spec == 's': - itype = "mojo::internal::StringPointer" - else: - itype = cls.kind_to_type[kind] - return cls.field_template.substitute(FIELD=field.name, TYPE=itype) - - @classmethod - def GetParamLine(cls, name, kind): - line = None - if mojom_generator.IsObjectKind(kind): - line = "mojo::internal::Wrap(params->%s())" % (name) - elif mojom_generator.IsHandleKind(kind): - line = "mojo::MakePassable(params->%s()).Pass()" % (name) - else: - line = "params->%s()" % name - return line - - @classmethod - def GetCaseLine(cls, interface, method): - params = map( - lambda param: cls.GetParamLine( - param.name, - param.kind), - method.parameters) - method_call = "%s(%s);" % (method.name, ", ".join(params)) - - return cls.GetTemplate("interface_stub_case").substitute( - CLASS = interface.name, - METHOD = method.name, - METHOD_CALL = method_call); - - @classmethod - def GetSerializedFields(cls, ps): - fields = [] - for pf in ps.packed_fields: - if mojom_generator.IsObjectKind(pf.field.kind): - fields.append(pf.field) - return fields - - @classmethod - def GetHandleFields(cls, ps): - fields = [] - for pf in ps.packed_fields: - if mojom_generator.IsHandleKind(pf.field.kind): - fields.append(pf.field) - return fields - - @classmethod - def IsStructWithHandles(cls, struct): - for field in struct.fields: - if mojom_generator.IsHandleKind(field.kind): - return True - return False - - @classmethod - def GetStructsWithHandles(cls, structs): - result = [] - for struct in structs: - if cls.IsStructWithHandles(struct): - result.append(struct) - return result - - def GetHeaderGuard(self, name): - return "MOJO_GENERATED_BINDINGS_%s_%s_H_" % \ - (self.module.name.upper(), name.upper()) - - def GetHeaderFile(self, *components): - components = map(mojom_generator.CamelToUnderscores, components) - component_string = '_'.join(components) - return os.path.join(self.header_dir, "%s.h" % component_string) - - def WriteTemplateToFile(self, template_name, **substitutions): - template = self.GetTemplate(template_name) - filename = template_name.replace("module", self.module.name) - substitutions['YEAR'] = datetime.date.today().year - substitutions['NAMESPACE'] = self.module.namespace - if self.output_dir is None: - file = sys.stdout - else: - file = open(os.path.join(self.output_dir, filename), "w+") - try: - file.write(template.substitute(substitutions)) - finally: - if self.output_dir is not None: - file.close() - - def GetStructDeclaration(self, name, ps, template, subs = {}): - fields = [] - setters = [] - getters = [] - pad_count = 0 - num_fields = len(ps.packed_fields) - for i in xrange(num_fields): - pf = ps.packed_fields[i] - field = pf.field - fields.append(self.GetFieldLine(field)) - if i < (num_fields - 1): - next_pf = ps.packed_fields[i+1] - pad = next_pf.offset - (pf.offset + pf.size) - if pad > 0: - fields.append(self.pad_template.substitute(COUNT=pad_count, PAD=pad)) - pad_count += 1 - setters.append(self.GetSetterLine(field)) - getters.append(self.GetGetterLine(field)) - - if num_fields > 0: - last_field = ps.packed_fields[num_fields - 1] - offset = last_field.offset + last_field.size - pad = mojom_pack.GetPad(offset, 8) - if pad > 0: - fields.append(self.pad_template.substitute(COUNT=pad_count, PAD=pad)) - pad_count += 1 - - subs.update( - CLASS = name + '_Data', - WRAPPER = name, - SETTERS = '\n'.join(setters), - GETTERS = '\n'.join(getters), - FIELDS = '\n'.join(fields), - SIZE = ps.GetTotalSize() + self.HEADER_SIZE) - return template.substitute(subs) - - def GetStructSerialization( - self, class_name, param_name, ps, template, indent = None): - struct = ps.struct - closes = Lines(self.close_handles_template, indent) - encodes = Lines(self.struct_serialization_encode_template, indent) - encode_handles = \ - Lines(self.struct_serialization_encode_handle_template, indent) - decodes = Lines(self.struct_serialization_decode_template, indent) - decode_handles = \ - Lines(self.struct_serialization_decode_handle_template, indent) - fields = self.GetSerializedFields(ps) - handle_fields = self.GetHandleFields(ps) - for field in fields: - substitutions = {'NAME': param_name, 'FIELD': field.name} - encodes.Add(substitutions) - decodes.Add(substitutions) - closes.Add(substitutions) - for field in handle_fields: - substitutions = {'NAME': param_name, 'FIELD': field.name} - encode_handles.Add(substitutions) - decode_handles.Add(substitutions) - return template.substitute( - CLASS = \ - "%s::internal::%s" % (self.module.namespace, class_name + '_Data'), - NAME = param_name, - CLOSES = closes, - ENCODES = encodes, - DECODES = decodes, - ENCODE_HANDLES = encode_handles, - DECODE_HANDLES = decode_handles) - - def GetStructClassDeclarations(self): - struct_decls = map( - lambda s: self.GetStructDeclaration( - s.name, - mojom_pack.PackedStruct(s), - self.GetTemplate("struct_declaration"), - {}), - self.module.structs) - return '\n'.join(struct_decls) - - def GetEnumFields(self, enum): - fields = Lines(self.enum_field_template) - for field in enum.fields: - if field.value: - fields.Add(NAME=field.name, EQUALS = " = ", VALUE = field.value) - else: - fields.Add(NAME=field.name, EQUALS = "", VALUE = "") - return fields - - def GetEnumDeclarations(self): - decls = Lines(self.GetTemplate("enum_declaration")) - for enum in self.module.enums: - decls.Add(NAME = enum.name, ENUM_FIELDS = self.GetEnumFields(enum)) - return decls - - def GetWrapperDeclaration(self, name, ps, template, subs = {}): - setters = [] - getters = [] - num_fields = len(ps.packed_fields) - for i in xrange(num_fields): - field = ps.packed_fields[i].field - setters.append(self.GetWrapperSetterLine(field)) - getters.append(self.GetWrapperGetterLine(field)) - subs.update( - CLASS = name, - SETTERS = '\n'.join(setters), - GETTERS = '\n'.join(getters)) - return template.substitute(subs) - - def GetWrapperClassDeclarations(self): - wrapper_decls = map( - lambda s: self.GetWrapperDeclaration( - s.name, - mojom_pack.PackedStruct(s), - self.GetTemplate("wrapper_class_declaration"), - {}), - self.module.structs) - return '\n'.join(wrapper_decls) - - def GetWrapperForwardDeclarations(self): - wrapper_fwds = map(lambda s: "class " + s.name + ";", self.module.structs) - return '\n'.join(wrapper_fwds) - - def GetInterfaceClassDeclaration(self, interface, template, method_postfix): - methods = [] - for method in interface.methods: - params = [] - for param in method.parameters: - params.append("%s %s" % - (self.GetConstWrapperType(param.kind), param.name)) - methods.append( - " virtual void %s(%s) %s;" % - (method.name, ", ".join(params), method_postfix)) - return template.substitute( - CLASS=interface.name, - PEER=interface.peer, - METHODS='\n'.join(methods)) - - def GetInterfaceClassDeclarations(self): - template = self.GetTemplate("interface_declaration") - interface_decls = \ - map(lambda i: - self.GetInterfaceClassDeclaration(i, template, " = 0"), - self.module.interfaces) - return '\n'.join(interface_decls) - - def GetInterfaceProxyDeclarations(self): - template = self.GetTemplate("interface_proxy_declaration") - interface_decls = \ - map(lambda i: - self.GetInterfaceClassDeclaration(i, template, "MOJO_OVERRIDE"), - self.module.interfaces) - return '\n'.join(interface_decls) - - def GetInterfaceStubDeclarations(self): - template = self.GetTemplate("interface_stub_declaration") - interface_decls = \ - map(lambda i: template.substitute(CLASS=i.name), self.module.interfaces) - return '\n'.join(interface_decls) - - def GenerateModuleHeader(self): - self.WriteTemplateToFile("module.h", - HEADER_GUARD = self.GetHeaderGuard(self.module.name), - INTERNAL_HEADER = self.GetHeaderFile(self.module.name, "internal"), - ENUM_DECLARATIONS = self.GetEnumDeclarations(), - WRAPPER_CLASS_DECLARATIONS = self.GetWrapperClassDeclarations(), - INTERFACE_CLASS_DECLARATIONS = self.GetInterfaceClassDeclarations(), - INTERFACE_PROXY_DECLARATIONS = self.GetInterfaceProxyDeclarations(), - INTERFACE_STUB_DECLARATIONS = self.GetInterfaceStubDeclarations()) - - def GetParamsDefinition(self, interface, method): - struct = mojom_generator.GetStructFromMethod(interface, method) - method_name = "k%s_%s_Name" % (interface.name, method.name) - assert method.ordinal is not None - params_def = self.GetStructDeclaration( - struct.name, - mojom_pack.PackedStruct(struct), - self.GetTemplate("params_definition"), - {'METHOD_NAME': method_name, 'METHOD_ID': method.ordinal}) - return params_def - - def GetStructDefinitions(self): - template = self.GetTemplate("struct_definition") - return '\n'.join(map( - lambda s: template.substitute( - CLASS = s.name + '_Data', - NUM_FIELDS = len(s.fields)), - self.module.structs)); - - def GetStructDestructorBody(self, struct): - body = Lines(self.struct_destructor_body_template) - for field in struct.fields: - if mojom_generator.IsHandleKind(field.kind): - body.Add(FIELD=field.name) - return body - - def GetStructDestructors(self): - template = self.GetTemplate("struct_destructor") - return '\n'.join(map( - lambda s: template.substitute( - CLASS = s.name, - BODY = self.GetStructDestructorBody(s)), - self.GetStructsWithHandles(self.module.structs))); - - def GetStructDestructorAddress(self, struct): - if self.IsStructWithHandles(struct): - return '&internal::' + struct.name + '_Data_Destructor'; - return 'NULL' - - def GetStructBuilderDefinitions(self): - template = self.GetTemplate("struct_builder_definition") - dtor = 'NULL' - return '\n'.join(map( - lambda s: template.substitute( - CLASS = s.name, - DTOR = self.GetStructDestructorAddress(s)), - self.module.structs)); - - def GetInterfaceDefinition(self, interface): - cases = [] - implementations = Lines(self.GetTemplate("proxy_implementation")) - - for method in interface.methods: - cases.append(self.GetCaseLine(interface, method)) - sets = [] - computes = Lines(self.param_struct_compute_template) - for param in method.parameters: - if mojom_generator.IsObjectKind(param.kind): - sets.append( - self.param_struct_set_template.substitute(NAME=param.name)) - computes.Add(NAME=param.name) - elif mojom_generator.IsHandleKind(param.kind): - sets.append( - self.param_handle_set_template.substitute(NAME=param.name)) - else: - sets.append(self.param_set_template.substitute(NAME=param.name)) - params_list = map( - lambda param: "%s %s" % - (self.GetConstWrapperType(param.kind), param.name), - method.parameters) - name = "internal::k%s_%s_Name" % (interface.name, method.name) - params_name = \ - "internal::%s_%s_Params_Data" % (interface.name, method.name) - - implementations.Add( - CLASS = interface.name, - METHOD = method.name, - NAME = name, - PARAMS = params_name, - PARAMS_LIST = ', '.join(params_list), - COMPUTES = computes, - SETS = '\n'.join(sets)) - stub_definition = self.GetTemplate("interface_stub_definition").substitute( - CLASS = interface.name, - CASES = '\n'.join(cases)) - return self.GetTemplate("interface_definition").substitute( - CLASS = interface.name + "Proxy", - PROXY_DEFINITIONS = implementations, - STUB_DEFINITION = stub_definition) - - def GetInterfaceDefinitions(self): - return '\n'.join( - map(lambda i: self.GetInterfaceDefinition(i), self.module.interfaces)) - - def GetStructSerializationDefinition(self, struct): - ps = mojom_pack.PackedStruct(struct) - param_name = mojom_generator.CamelToUnderscores(struct.name) - - dtor = '' - closes = Lines(self.close_handles_template) - clones = Lines(self.struct_serialization_clone_template) - handle_releases = Lines(self.struct_serialization_handle_release_template) - sizes = " return sizeof(*%s)" % param_name - fields = self.GetSerializedFields(ps) - for field in fields: - substitutions = {'NAME': param_name, 'FIELD': field.name} - sizes += \ - self.struct_serialization_compute_template.substitute(substitutions) - clones.Add(substitutions) - closes.Add(substitutions) - handle_fields = self.GetHandleFields(ps) - for field in handle_fields: - substitutions = {'NAME': param_name, 'FIELD': field.name} - handle_releases.Add(substitutions) - if len(handle_fields) > 0: - dtor = " %s_Data_Destructor(%s);" % (struct.name, param_name) - sizes += ";" - serialization = self.GetStructSerialization( - struct.name, param_name, ps, self.GetTemplate("struct_serialization")) - return self.GetTemplate("struct_serialization_definition").substitute( - NAME = param_name, - CLASS = "%s::internal::%s_Data" % (self.module.namespace, struct.name), - SIZES = sizes, - CLONES = clones, - HANDLE_RELEASES = handle_releases, - CLOSES = closes, - DTOR = dtor, - SERIALIZATION = serialization) - - def GetStructSerializationDefinitions(self): - return '\n'.join( - map(lambda i: self.GetStructSerializationDefinition(i), - self.module.structs)) - - def GetInterfaceSerializationDefinitions(self): - serializations = [] - for interface in self.module.interfaces: - for method in interface.methods: - struct = mojom_generator.GetStructFromMethod(interface, method) - ps = mojom_pack.PackedStruct(struct) - serializations.append(self.GetStructSerialization( - struct.name, - "params", - ps, - self.GetTemplate("params_serialization"), - 2)) - return '\n'.join(serializations) - - def GetParamsDefinitions(self): - params_defs = [] - for interface in self.module.interfaces: - for method in interface.methods: - params_def = self.GetParamsDefinition(interface, method) - params_defs.append(params_def) - return '\n'.join(params_defs) - - def GenerateModuleSource(self): - self.WriteTemplateToFile("module.cc", - HEADER = self.GetHeaderFile(self.module.name), - PARAM_DEFINITIONS = self.GetParamsDefinitions(), - STRUCT_DEFINITIONS = self.GetStructDefinitions(), - STRUCT_DESTRUCTORS = self.GetStructDestructors(), - STRUCT_BUILDER_DEFINITIONS = self.GetStructBuilderDefinitions(), - INTERFACE_DEFINITIONS = self.GetInterfaceDefinitions(), - STRUCT_SERIALIZATION_DEFINITIONS = - self.GetStructSerializationDefinitions(), - INTERFACE_SERIALIZATION_DEFINITIONS = - self.GetInterfaceSerializationDefinitions()) - - def GenerateModuleInternalHeader(self): - traits = map( - lambda s: self.GetTemplate("struct_serialization_traits").substitute( - NAME = mojom_generator.CamelToUnderscores(s.name), - FULL_CLASS = \ - "%s::internal::%s" % (self.module.namespace, s.name + '_Data')), - self.module.structs); - self.WriteTemplateToFile("module_internal.h", - HEADER_GUARD = self.GetHeaderGuard(self.module.name + "_INTERNAL"), - WRAPPER_FORWARD_DECLARATIONS = self.GetWrapperForwardDeclarations(), - STRUCT_CLASS_DECLARATIONS = self.GetStructClassDeclarations(), - TRAITS = '\n'.join(traits)) - - def GenerateFiles(self): - self.GenerateModuleHeader() - self.GenerateModuleInternalHeader() - self.GenerateModuleSource() diff --git a/mojo/public/bindings/generators/mojom_jinja_generator.py b/mojo/public/bindings/generators/mojom_jinja_generator.py index d00b253..86952f1 100644 --- a/mojo/public/bindings/generators/mojom_jinja_generator.py +++ b/mojo/public/bindings/generators/mojom_jinja_generator.py @@ -13,6 +13,126 @@ from functools import partial from template_expander import UseJinja +_kind_to_javascript_default_value = { + mojom.BOOL: "false", + mojom.INT8: "0", + mojom.UINT8: "0", + mojom.INT16: "0", + mojom.UINT16: "0", + mojom.INT32: "0", + mojom.UINT32: "0", + mojom.FLOAT: "0", + mojom.HANDLE: "core.kInvalidHandle", + mojom.MSGPIPE: "core.kInvalidHandle", + mojom.INT64: "0", + mojom.UINT64: "0", + mojom.DOUBLE: "0", + mojom.STRING: '""', +} + + +def JavaScriptDefaultValue(field): + if field.default: + return field.default + if field.kind in mojom.PRIMITIVES: + return _kind_to_javascript_default_value[field.kind] + if isinstance(field.kind, mojom.Struct): + return "null"; + if isinstance(field.kind, mojom.Array): + return "[]"; + + +def JavaScriptPayloadSize(packed): + packed_fields = packed.packed_fields + if not packed_fields: + return 0; + last_field = packed_fields[-1] + offset = last_field.offset + last_field.size + pad = mojom_pack.GetPad(offset, 8) + return offset + pad; + + +_kind_to_javascript_type = { + mojom.BOOL: "codec.Uint8", + mojom.INT8: "codec.Int8", + mojom.UINT8: "codec.Uint8", + mojom.INT16: "codec.Int16", + mojom.UINT16: "codec.Uint16", + mojom.INT32: "codec.Int32", + mojom.UINT32: "codec.Uint32", + mojom.FLOAT: "codec.Float", + mojom.HANDLE: "codec.Handle", + mojom.MSGPIPE: "codec.Handle", + mojom.INT64: "codec.Int64", + mojom.UINT64: "codec.Uint64", + mojom.DOUBLE: "codec.Double", + mojom.STRING: "codec.String", +} + + +def GetJavaScriptType(kind): + if kind in mojom.PRIMITIVES: + return _kind_to_javascript_type[kind] + if isinstance(kind, mojom.Struct): + return "new codec.PointerTo(%s)" % GetJavaScriptType(kind.name) + if isinstance(kind, mojom.Array): + return "new codec.ArrayOf(%s)" % GetJavaScriptType(kind.kind) + return kind + + +_kind_to_javascript_decode_snippet = { + mojom.BOOL: "read8() & 1", + mojom.INT8: "read8()", + mojom.UINT8: "read8()", + mojom.INT16: "read16()", + mojom.UINT16: "read16()", + mojom.INT32: "read32()", + mojom.UINT32: "read32()", + mojom.FLOAT: "decodeFloat()", + mojom.HANDLE: "decodeHandle()", + mojom.MSGPIPE: "decodeHandle()", + mojom.INT64: "read64()", + mojom.UINT64: "read64()", + mojom.DOUBLE: "decodeDouble()", + mojom.STRING: "decodeStringPointer()", +} + + +def JavaScriptDecodeSnippet(kind): + if kind in mojom.PRIMITIVES: + return _kind_to_javascript_decode_snippet[kind] + if isinstance(kind, mojom.Struct): + return "decodeStructPointer(%s)" % GetJavaScriptType(kind.name); + if isinstance(kind, mojom.Array): + return "decodeArrayPointer(%s)" % GetJavaScriptType(kind.kind); + + +_kind_to_javascript_encode_snippet = { + mojom.BOOL: "write8(1 & ", + mojom.INT8: "write8(", + mojom.UINT8: "write8(", + mojom.INT16: "write16(", + mojom.UINT16: "write16(", + mojom.INT32: "write32(", + mojom.UINT32: "write32(", + mojom.FLOAT: "encodeFloat(", + mojom.HANDLE: "encodeHandle(", + mojom.MSGPIPE: "encodeHandle(", + mojom.INT64: "write64(", + mojom.UINT64: "write64(", + mojom.DOUBLE: "encodeDouble(", + mojom.STRING: "encodeStringPointer(", +} + + +def JavaScriptEncodeSnippet(kind): + if kind in mojom.PRIMITIVES: + return _kind_to_javascript_encode_snippet[kind] + if isinstance(kind, mojom.Struct): + return "encodeStructPointer(%s, " % GetJavaScriptType(kind.name); + if isinstance(kind, mojom.Array): + return "encodeArrayPointer(%s, " % GetJavaScriptType(kind.kind); + _kind_to_cpp_type = { mojom.BOOL: "bool", mojom.INT8: "int8_t", @@ -29,20 +149,21 @@ _kind_to_cpp_type = { mojom.DOUBLE: "double", } -def GetType(kind): + +def GetCppType(kind): if isinstance(kind, mojom.Struct): return "%s_Data*" % kind.name if isinstance(kind, mojom.Array): - return "mojo::internal::Array_Data<%s>*" % GetType(kind.kind) + return "mojo::internal::Array_Data<%s>*" % GetCppType(kind.kind) if kind.spec == 's': return "mojo::internal::String_Data*" return _kind_to_cpp_type[kind] -def GetWrapperType(kind): +def GetCppWrapperType(kind): if isinstance(kind, mojom.Struct): return "%s" % kind.name if isinstance(kind, mojom.Array): - return "mojo::Array<%s >" % GetWrapperType(kind.kind) + return "mojo::Array<%s >" % GetCppWrapperType(kind.kind) if kind.spec == 's': return "mojo::String" if kind.spec == 'h': @@ -51,11 +172,11 @@ def GetWrapperType(kind): return "mojo::Passable<mojo::MessagePipeHandle>" return _kind_to_cpp_type[kind] -def GetConstWrapperType(kind): +def GetCppConstWrapperType(kind): if isinstance(kind, mojom.Struct): return "const %s&" % kind.name if isinstance(kind, mojom.Array): - return "const mojo::Array<%s >&" % GetWrapperType(kind.kind) + return "const mojo::Array<%s >&" % GetCppWrapperType(kind.kind) if kind.spec == 's': return "const mojo::String&" if kind.spec == 'h': @@ -64,13 +185,13 @@ def GetConstWrapperType(kind): return "mojo::ScopedMessagePipeHandle" return _kind_to_cpp_type[kind] -def GetFieldType(kind): +def GetCppFieldType(kind): if mojom_generator.IsHandleKind(kind): return _kind_to_cpp_type[kind] if isinstance(kind, mojom.Struct): return "mojo::internal::StructPointer<%s_Data>" % kind.name if isinstance(kind, mojom.Array): - return "mojo::internal::ArrayPointer<%s>" % GetType(kind.kind) + return "mojo::internal::ArrayPointer<%s>" % GetCppType(kind.kind) if kind.spec == 's': return "mojo::internal::StringPointer" return _kind_to_cpp_type[kind] @@ -78,6 +199,7 @@ def GetFieldType(kind): def GetStructInfo(exported, struct): struct.packed = mojom_pack.PackedStruct(struct) struct.exported = exported + struct.bytes = mojom_pack.GetByteLayout(struct.packed) return struct def GetStructFromMethod(interface, method): @@ -95,12 +217,12 @@ _HEADER_SIZE = 8 class JinjaGenerator(mojom_generator.Generator): - filters = { + cpp_filters = { "camel_to_underscores": mojom_generator.CamelToUnderscores, - "cpp_const_wrapper_type": GetConstWrapperType, - "cpp_field_type": GetFieldType, - "cpp_type": GetType, - "cpp_wrapper_type": GetWrapperType, + "cpp_const_wrapper_type": GetCppConstWrapperType, + "cpp_field_type": GetCppFieldType, + "cpp_type": GetCppType, + "cpp_wrapper_type": GetCppWrapperType, "get_pad": mojom_pack.GetPad, "is_handle_kind": mojom_generator.IsHandleKind, "is_object_kind": mojom_generator.IsObjectKind, @@ -110,10 +232,25 @@ class JinjaGenerator(mojom_generator.Generator): "stylize_method": mojom_generator.StudlyCapsToCamel, } + js_filters = { + "default_value": JavaScriptDefaultValue, + "payload_size": JavaScriptPayloadSize, + "decode_snippet": JavaScriptDecodeSnippet, + "encode_snippet": JavaScriptEncodeSnippet, + "stylize_method": mojom_generator.StudlyCapsToCamel, + } + + def GetStructsFromMethods(self): + result = [] + for interface in self.module.interfaces: + for method in interface.methods: + result.append(mojom_generator.GetStructFromMethod(interface, method)) + return map(partial(GetStructInfo, False), result) + def GetStructs(self): return map(partial(GetStructInfo, True), self.module.structs) - @UseJinja("cpp_templates/module.h.tmpl", filters=filters) + @UseJinja("cpp_templates/module.h.tmpl", filters=cpp_filters) def GenerateModuleHeader(self): return { "module_name": self.module.name, @@ -123,7 +260,7 @@ class JinjaGenerator(mojom_generator.Generator): "interfaces": self.module.interfaces, } - @UseJinja("cpp_templates/module_internal.h.tmpl", filters=filters) + @UseJinja("cpp_templates/module_internal.h.tmpl", filters=cpp_filters) def GenerateModuleInternalHeader(self): return { "module_name": self.module.name, @@ -133,7 +270,7 @@ class JinjaGenerator(mojom_generator.Generator): "interfaces": self.module.interfaces, } - @UseJinja("cpp_templates/module.cc.tmpl", filters=filters) + @UseJinja("cpp_templates/module.cc.tmpl", filters=cpp_filters) def GenerateModuleSource(self): return { "module_name": self.module.name, @@ -143,6 +280,14 @@ class JinjaGenerator(mojom_generator.Generator): "interfaces": self.module.interfaces, } + @UseJinja("js_templates/module.js.tmpl", filters=js_filters) + def GenerateJsModule(self): + return { + "enums": self.module.enums, + "structs": self.GetStructs() + self.GetStructsFromMethods(), + "interfaces": self.module.interfaces, + } + def Write(self, contents, filename): if self.output_dir is None: print contents @@ -151,7 +296,8 @@ class JinjaGenerator(mojom_generator.Generator): f.write(contents) def GenerateFiles(self): - self.Write(self.GenerateModuleHeader(), "%s.j.h" % self.module.name) + self.Write(self.GenerateModuleHeader(), "%s.h" % self.module.name) self.Write(self.GenerateModuleInternalHeader(), - "%s_internal.j.h" % self.module.name) - self.Write(self.GenerateModuleSource(), "%s.j.cc" % self.module.name) + "%s_internal.h" % self.module.name) + self.Write(self.GenerateModuleSource(), "%s.cc" % self.module.name) + self.Write(self.GenerateJsModule(), "%s.js" % self.module.name) diff --git a/mojo/public/bindings/generators/mojom_js_generator.py b/mojo/public/bindings/generators/mojom_js_generator.py deleted file mode 100644 index d2a4e58..0000000 --- a/mojo/public/bindings/generators/mojom_js_generator.py +++ /dev/null @@ -1,182 +0,0 @@ -# Copyright 2013 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. - -"""Generates JS source files from a mojom.Module.""" - -import os -import mojom -import mojom_pack -import mojom_generator - -from functools import partial -from template_expander import UseJinja - -_kind_to_default_value = { - mojom.BOOL: "false", - mojom.INT8: "0", - mojom.UINT8: "0", - mojom.INT16: "0", - mojom.UINT16: "0", - mojom.INT32: "0", - mojom.UINT32: "0", - mojom.FLOAT: "0", - mojom.HANDLE: "core.kInvalidHandle", - mojom.MSGPIPE: "core.kInvalidHandle", - mojom.INT64: "0", - mojom.UINT64: "0", - mojom.DOUBLE: "0", - mojom.STRING: '""', -} - - -def DefaultValue(field): - if field.default: - return field.default - if field.kind in mojom.PRIMITIVES: - return _kind_to_default_value[field.kind] - if isinstance(field.kind, mojom.Struct): - return "null"; - if isinstance(field.kind, mojom.Array): - return "[]"; - - -def PayloadSize(packed): - packed_fields = packed.packed_fields - if not packed_fields: - return 0; - last_field = packed_fields[-1] - offset = last_field.offset + last_field.size - pad = mojom_pack.GetPad(offset, 8) - return offset + pad; - - -_kind_to_javascript_type = { - mojom.BOOL: "codec.Uint8", - mojom.INT8: "codec.Int8", - mojom.UINT8: "codec.Uint8", - mojom.INT16: "codec.Int16", - mojom.UINT16: "codec.Uint16", - mojom.INT32: "codec.Int32", - mojom.UINT32: "codec.Uint32", - mojom.FLOAT: "codec.Float", - mojom.HANDLE: "codec.Handle", - mojom.MSGPIPE: "codec.Handle", - mojom.INT64: "codec.Int64", - mojom.UINT64: "codec.Uint64", - mojom.DOUBLE: "codec.Double", - mojom.STRING: "codec.String", -} - - -def GetJavaScriptType(kind): - if kind in mojom.PRIMITIVES: - return _kind_to_javascript_type[kind] - if isinstance(kind, mojom.Struct): - return "new codec.PointerTo(%s)" % GetJavaScriptType(kind.name) - if isinstance(kind, mojom.Array): - return "new codec.ArrayOf(%s)" % GetJavaScriptType(kind.kind) - return kind - - -_kind_to_decode_snippet = { - mojom.BOOL: "read8() & 1", - mojom.INT8: "read8()", - mojom.UINT8: "read8()", - mojom.INT16: "read16()", - mojom.UINT16: "read16()", - mojom.INT32: "read32()", - mojom.UINT32: "read32()", - mojom.FLOAT: "decodeFloat()", - mojom.HANDLE: "decodeHandle()", - mojom.MSGPIPE: "decodeHandle()", - mojom.INT64: "read64()", - mojom.UINT64: "read64()", - mojom.DOUBLE: "decodeDouble()", - mojom.STRING: "decodeStringPointer()", -} - - -def DecodeSnippet(kind): - if kind in mojom.PRIMITIVES: - return _kind_to_decode_snippet[kind] - if isinstance(kind, mojom.Struct): - return "decodeStructPointer(%s)" % GetJavaScriptType(kind.name); - if isinstance(kind, mojom.Array): - return "decodeArrayPointer(%s)" % GetJavaScriptType(kind.kind); - - -_kind_to_encode_snippet = { - mojom.BOOL: "write8(1 & ", - mojom.INT8: "write8(", - mojom.UINT8: "write8(", - mojom.INT16: "write16(", - mojom.UINT16: "write16(", - mojom.INT32: "write32(", - mojom.UINT32: "write32(", - mojom.FLOAT: "encodeFloat(", - mojom.HANDLE: "encodeHandle(", - mojom.MSGPIPE: "encodeHandle(", - mojom.INT64: "write64(", - mojom.UINT64: "write64(", - mojom.DOUBLE: "encodeDouble(", - mojom.STRING: "encodeStringPointer(", -} - - -def EncodeSnippet(kind): - if kind in mojom.PRIMITIVES: - return _kind_to_encode_snippet[kind] - if isinstance(kind, mojom.Struct): - return "encodeStructPointer(%s, " % GetJavaScriptType(kind.name); - if isinstance(kind, mojom.Array): - return "encodeArrayPointer(%s, " % GetJavaScriptType(kind.kind); - - -def GetStructInfo(exported, struct): - packed_struct = mojom_pack.PackedStruct(struct) - return { - "name": struct.name, - "packed": packed_struct, - "bytes": mojom_pack.GetByteLayout(packed_struct), - "exported": exported, - } - - -class JSGenerator(mojom_generator.Generator): - filters = { - "default_value": DefaultValue, - "payload_size": PayloadSize, - "decode_snippet": DecodeSnippet, - "encode_snippet": EncodeSnippet, - "stylize_method": mojom_generator.StudlyCapsToCamel, - } - - def GetStructsFromMethods(self): - result = [] - for interface in self.module.interfaces: - for method in interface.methods: - result.append(mojom_generator.GetStructFromMethod(interface, method)) - return map(partial(GetStructInfo, False), result) - - def GetStructs(self): - return map(partial(GetStructInfo, True), self.module.structs) - - @UseJinja("js_templates/module.js.tmpl", filters=filters) - def GenerateModule(self): - return { - "enums": self.module.enums, - "structs": self.GetStructs() + self.GetStructsFromMethods(), - "interfaces": self.module.interfaces, - } - - def Write(self, contents): - if self.output_dir is None: - print contents - return - filename = "%s.js" % self.module.name - with open(os.path.join(self.output_dir, filename), "w+") as f: - f.write(contents) - - def GenerateFiles(self): - self.Write(self.GenerateModule()) diff --git a/mojo/public/bindings/mojom_bindings_generator.gypi b/mojo/public/bindings/mojom_bindings_generator.gypi index 4c0d514a..f6d90a8 100644 --- a/mojo/public/bindings/mojom_bindings_generator.gypi +++ b/mojo/public/bindings/mojom_bindings_generator.gypi @@ -18,35 +18,30 @@ '<(mojom_bindings_generator)', '<(DEPTH)/mojo/public/bindings/parse/mojo_parser.py', '<(DEPTH)/mojo/public/bindings/parse/mojo_translate.py', - '<(DEPTH)/mojo/public/bindings/generators/cpp_templates/interface_declaration', - '<(DEPTH)/mojo/public/bindings/generators/cpp_templates/interface_definition', - '<(DEPTH)/mojo/public/bindings/generators/cpp_templates/interface_proxy_declaration', - '<(DEPTH)/mojo/public/bindings/generators/cpp_templates/interface_stub_case', - '<(DEPTH)/mojo/public/bindings/generators/cpp_templates/interface_stub_declaration', - '<(DEPTH)/mojo/public/bindings/generators/cpp_templates/interface_stub_definition', - '<(DEPTH)/mojo/public/bindings/generators/cpp_templates/module.cc-template', - '<(DEPTH)/mojo/public/bindings/generators/cpp_templates/module.h-template', - '<(DEPTH)/mojo/public/bindings/generators/cpp_templates/module_internal.h-template', - '<(DEPTH)/mojo/public/bindings/generators/cpp_templates/params_definition', - '<(DEPTH)/mojo/public/bindings/generators/cpp_templates/params_serialization', - '<(DEPTH)/mojo/public/bindings/generators/cpp_templates/proxy_implementation', - '<(DEPTH)/mojo/public/bindings/generators/cpp_templates/struct_builder_definition', - '<(DEPTH)/mojo/public/bindings/generators/cpp_templates/struct_declaration', - '<(DEPTH)/mojo/public/bindings/generators/cpp_templates/struct_destructor', - '<(DEPTH)/mojo/public/bindings/generators/cpp_templates/struct_definition', - '<(DEPTH)/mojo/public/bindings/generators/cpp_templates/struct_serialization', - '<(DEPTH)/mojo/public/bindings/generators/cpp_templates/struct_serialization_definition', - '<(DEPTH)/mojo/public/bindings/generators/cpp_templates/struct_serialization_traits', - '<(DEPTH)/mojo/public/bindings/generators/cpp_templates/template_declaration', - '<(DEPTH)/mojo/public/bindings/generators/cpp_templates/wrapper_class_declaration', + '<(DEPTH)/mojo/public/bindings/generators/cpp_templates/interface_declaration.tmpl', + '<(DEPTH)/mojo/public/bindings/generators/cpp_templates/interface_definition.tmpl', + '<(DEPTH)/mojo/public/bindings/generators/cpp_templates/interface_proxy_declaration.tmpl', + '<(DEPTH)/mojo/public/bindings/generators/cpp_templates/interface_stub_declaration.tmpl', + '<(DEPTH)/mojo/public/bindings/generators/cpp_templates/module.cc.tmpl', + '<(DEPTH)/mojo/public/bindings/generators/cpp_templates/module.h.tmpl', + '<(DEPTH)/mojo/public/bindings/generators/cpp_templates/module_internal.h.tmpl', + '<(DEPTH)/mojo/public/bindings/generators/cpp_templates/params_definition.tmpl', + '<(DEPTH)/mojo/public/bindings/generators/cpp_templates/params_serialization.tmpl', + '<(DEPTH)/mojo/public/bindings/generators/cpp_templates/struct_builder_definition.tmpl', + '<(DEPTH)/mojo/public/bindings/generators/cpp_templates/struct_declaration.tmpl', + '<(DEPTH)/mojo/public/bindings/generators/cpp_templates/struct_definition.tmpl', + '<(DEPTH)/mojo/public/bindings/generators/cpp_templates/struct_destructor.tmpl', + '<(DEPTH)/mojo/public/bindings/generators/cpp_templates/struct_macros.tmpl', + '<(DEPTH)/mojo/public/bindings/generators/cpp_templates/struct_serialization_definition.tmpl', + '<(DEPTH)/mojo/public/bindings/generators/cpp_templates/struct_serialization_traits.tmpl', + '<(DEPTH)/mojo/public/bindings/generators/cpp_templates/wrapper_class_declaration.tmpl', '<(DEPTH)/mojo/public/bindings/generators/js_templates/interface_definition.tmpl', '<(DEPTH)/mojo/public/bindings/generators/js_templates/module.js.tmpl', '<(DEPTH)/mojo/public/bindings/generators/js_templates/struct_definition.tmpl', '<(DEPTH)/mojo/public/bindings/generators/mojom.py', - '<(DEPTH)/mojo/public/bindings/generators/mojom_cpp_generator.py', '<(DEPTH)/mojo/public/bindings/generators/mojom_data.py', '<(DEPTH)/mojo/public/bindings/generators/mojom_generator.py', - '<(DEPTH)/mojo/public/bindings/generators/mojom_js_generator.py', + '<(DEPTH)/mojo/public/bindings/generators/mojom_jinja_generator.py', '<(DEPTH)/mojo/public/bindings/generators/mojom_pack.py', '<(DEPTH)/mojo/public/bindings/generators/template_expander.py', ], diff --git a/mojo/public/bindings/mojom_bindings_generator.py b/mojo/public/bindings/mojom_bindings_generator.py index 0255ae2c..f62fa3a 100755 --- a/mojo/public/bindings/mojom_bindings_generator.py +++ b/mojo/public/bindings/mojom_bindings_generator.py @@ -12,8 +12,7 @@ from optparse import OptionParser from parse import mojo_parser from parse import mojo_translate from generators import mojom_data -from generators import mojom_js_generator -from generators import mojom_cpp_generator +from generators import mojom_jinja_generator from generators import mojom_jinja_generator @@ -39,12 +38,6 @@ def Main(): tree = mojo_parser.Parse(filename) mojom = mojo_translate.Translate(tree, name) module = mojom_data.OrderedModuleFromData(mojom) - cpp = mojom_cpp_generator.CPPGenerator( - module, options.include_dir, options.output_dir) - cpp.GenerateFiles() - js = mojom_js_generator.JSGenerator( - module, options.include_dir, options.output_dir) - js.GenerateFiles() jinja = mojom_jinja_generator.JinjaGenerator( module, options.include_dir, options.output_dir) jinja.GenerateFiles() |