Skip to content

Commit

Permalink
Add support for enums within structs and interfaces to mojom.
Browse files Browse the repository at this point in the history
BUG=320090
R=abarth@chromium.org, davemoore@chromium.org

Review URL: https://codereview.chromium.org/99623010

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@243341 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
mpcomplete@google.com committed Jan 7, 2014
1 parent 43593d7 commit 1e788d0
Show file tree
Hide file tree
Showing 16 changed files with 105 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
enum {{enum.name}} {
{%- for field in enum.fields %}
{%- if field.value %}
{{field.name}} = {{field.value}},
{%- else %}
{{field.name}},
{%- endif %}
{%- endfor %}
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ class {{interface.name}} {
typedef {{interface.name}}Stub _Stub;
typedef {{interface.peer}} _Peer;

{%- for method in interface.methods %}
{#--- Enums #}
{%- for enum in interface.enums %}
{% macro enum_def() %}{% include "enum_declaration.tmpl" %}{% endmacro %}
{{enum_def()|indent(2)}}
{%- endfor %}

{#--- Methods #}
{% for method in interface.methods %}
virtual void {{method.name}}(
{%- for param in method.parameters -%}
{{param.kind|cpp_const_wrapper_type}} {{param.name}}
Expand Down
21 changes: 6 additions & 15 deletions mojo/public/bindings/generators/cpp_templates/module.h.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,28 @@
namespace {{namespace}} {

{#--- Enums #}
{%- for enum in enums %}

enum {{enum.name}} {
{%- for field in enum.fields %}
{%- if field.value %}
{{field.name}} = {{field.value}},
{%- else %}
{{field.name}},
{%- endif %}
{%- endfor %}
};
{% for enum in enums %}
{% include "enum_declaration.tmpl" %}
{%- endfor %}

{#--- Structs #}
{% for struct in structs %}
{% include "wrapper_class_declaration.tmpl" -%}
{% include "wrapper_class_declaration.tmpl" %}
{%- endfor %}

{#--- Interfaces -#}
{% for interface in interfaces %}
{% include "interface_declaration.tmpl" -%}
{% include "interface_declaration.tmpl" %}
{%- endfor %}

{#--- Interface Proxies -#}
{% for interface in interfaces %}
{% include "interface_proxy_declaration.tmpl" -%}
{% include "interface_proxy_declaration.tmpl" %}
{%- endfor %}

{#--- Interface Stubs -#}
{% for interface in interfaces %}
{% include "interface_stub_declaration.tmpl" -%}
{% include "interface_stub_declaration.tmpl" %}
{%- endfor %}

} // namespace {{namespace}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace internal {

{#--- Class declarations #}
{% for struct in structs %}
{% include "struct_declaration.tmpl" -%}
{% include "struct_declaration.tmpl" %}
{%- endfor %}

#pragma pack(pop)
Expand All @@ -36,7 +36,7 @@ namespace internal {

{#--- Serialization traits #}
{% for struct in structs %}
{% include "struct_serialization_traits.tmpl" -%}
{% include "struct_serialization_traits.tmpl" %}
{%- endfor %}

} // namespace internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ class {{class_name}} {
public:
typedef {{struct.name}} Wrapper;

{#--- Enums #}
{%- for enum in struct.enums %}
{% macro enum_def() %}{% include "enum_declaration.tmpl" %}{% endmacro %}
{{enum_def()|indent(2)}}
{%- endfor %}

static {{class_name}}* New(mojo::Buffer* buf, mojo::Buffer::Destructor dtor = NULL);

{#--- Setters -#}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
class {{struct.name}} {
public:
typedef internal::{{struct.name}}_Data Data;
{%- for enum in struct.enums %}
typedef internal::{{enum.name}} {{enum.name}};
{%- endfor %}

{{struct.name}}() : data_(NULL) {
}
Expand Down
12 changes: 12 additions & 0 deletions mojo/public/bindings/generators/js_templates/enum_definition.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{%- macro enum_def(enum_init, enum) %}
{{enum_init}} = {
{%- set next_value = 0 %}
{%- for field in enum.fields %}
{%- if field.value %}
{%- set next_value = field.value|int %}
{%- endif %}
{{field.name}}: {{next_value}},
{%- set next_value = next_value + 1 %}
{%- endfor %}
};
{%- endmacro %}
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,10 @@ params.{{parameter.name}}{% if not loop.last %}, {% endif %}
return false;
}
};

{#--- Enums #}
{%- from "enum_definition.tmpl" import enum_def -%}
{% for enum in interface.enums %}
{{ enum_def("%sProxy.%s"|format(interface.name, enum.name), enum)}}
{{interface.name}}Stub.{{enum.name}} = {{interface.name}}Proxy.{{enum.name}};
{%- endfor %}
16 changes: 4 additions & 12 deletions mojo/public/bindings/generators/js_templates/module.js.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,13 @@ define([
], function(core, codec) {

{#--- Enums #}
{%- from "enum_definition.tmpl" import enum_def -%}
{% for enum in enums %}
var {{enum.name}} = {
{%- set next_value = 0 %}
{%- for field in enum.fields %}
{%- if field.value %}
{%- set next_value = field.value|int %}
{%- endif %}
{{field.name}}: {{next_value}},
{%- set next_value = next_value + 1 %}
{%- endfor %}
};
{% endfor %}
{{ enum_def("var %s"|format(enum.name), enum) }}
{%- endfor %}

{#--- Struct definitions #}
{%- for struct in structs %}
{% for struct in structs %}
{% include "struct_definition.tmpl" %}
{%- endfor %}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
{%- endfor %}
}

{#--- Enums #}
{%- from "enum_definition.tmpl" import enum_def -%}
{% for enum in struct.enums %}
{{ enum_def("%s.%s"|format(struct.name, enum.name), enum)}}
{% endfor %}

{#--- Encoding and decoding #}
{{struct.name}}.encodedSize = codec.kStructHeaderSize + {{struct.packed|payload_size}};

{{struct.name}}.decode = function(decoder) {
Expand Down
2 changes: 2 additions & 0 deletions mojo/public/bindings/generators/mojom_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def StructFromData(kinds, data):
struct.spec = 'x:' + struct.name
kinds[struct.spec] = struct
struct.fields = map(lambda field: FieldFromData(kinds, field), data['fields'])
struct.enums = map(lambda enum: EnumFromData(kinds, enum), data['enums'])
return struct

def FieldToData(field):
Expand Down Expand Up @@ -137,6 +138,7 @@ def InterfaceFromData(kinds, data):
interface.peer = data['peer']
interface.methods = map(
lambda method: MethodFromData(kinds, method), data['methods'])
interface.enums = map(lambda enum: EnumFromData(kinds, enum), data['enums'])
return interface

def EnumFieldFromData(kinds, data):
Expand Down
18 changes: 10 additions & 8 deletions mojo/public/bindings/parse/mojo_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,13 @@ def p_attribute(self, p):
p[0] = ('ATTRIBUTE', p[1], p[3])

def p_struct(self, p):
"""struct : attribute_section STRUCT NAME LCURLY fields RCURLY SEMICOLON"""
"""struct : attribute_section STRUCT NAME LCURLY struct_body RCURLY SEMICOLON"""
p[0] = ('STRUCT', p[3], p[1], p[5])

def p_fields(self, p):
"""fields : field fields
|"""
def p_struct_body(self, p):
"""struct_body : field struct_body
| enum struct_body
|"""
if len(p) > 1:
p[0] = ListFromConcat(p[1], p[2])

Expand All @@ -198,12 +199,13 @@ def p_field(self, p):
p[0] = ('FIELD', p[1], p[2], p[3])

def p_interface(self, p):
"""interface : attribute_section INTERFACE NAME LCURLY methods RCURLY SEMICOLON"""
"""interface : attribute_section INTERFACE NAME LCURLY interface_body RCURLY SEMICOLON"""
p[0] = ('INTERFACE', p[3], p[1], p[5])

def p_methods(self, p):
"""methods : method methods
| """
def p_interface_body(self, p):
"""interface_body : method interface_body
| enum interface_body
| """
if len(p) > 1:
p[0] = ListFromConcat(p[1], p[2])

Expand Down
25 changes: 19 additions & 6 deletions mojo/public/bindings/parse/mojo_translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def MapMethods(methods):
'ordinal': MapOrdinal(method[3])})
return out


def MapEnumFields(fields):
out = []
for field in fields:
Expand All @@ -85,24 +86,36 @@ def MapEnumFields(fields):
'value': field[2]})
return out


def MapEnums(enums):
out = []
for enum in enums:
if enum[0] == 'ENUM':
out.append({'name': enum[1],
'fields': MapEnumFields(enum[2])})
return out


class MojomBuilder():

def __init__(self):
self.mojom = {}

def AddStruct(self, name, attributes, fields):
def AddStruct(self, name, attributes, body):
struct = {}
struct['name'] = name
# TODO(darin): Add support for |attributes|
#struct['attributes'] = MapAttributes(attributes)
struct['fields'] = MapFields(fields)
struct['fields'] = MapFields(body)
struct['enums'] = MapEnums(body)
self.mojom['structs'].append(struct)

def AddInterface(self, name, attributes, methods):
def AddInterface(self, name, attributes, body):
interface = {}
interface['name'] = name
interface['peer'] = GetAttribute(attributes, 'Peer')
interface['methods'] = MapMethods(methods)
interface['methods'] = MapMethods(body)
interface['enums'] = MapEnums(body)
self.mojom['interfaces'].append(interface)

def AddEnum(self, name, fields):
Expand All @@ -121,9 +134,9 @@ def AddModule(self, name, namespace, contents):
self.mojom['enums'] = []
for item in contents:
if item[0] == 'STRUCT':
self.AddStruct(name=item[1], attributes=item[2], fields=item[3])
self.AddStruct(name=item[1], attributes=item[2], body=item[3])
elif item[0] == 'INTERFACE':
self.AddInterface(name=item[1], attributes=item[2], methods=item[3])
self.AddInterface(name=item[1], attributes=item[2], body=item[3])
elif item[0] == 'ENUM':
self.AddEnum(name=item[1], fields=item[2])

Expand Down
6 changes: 5 additions & 1 deletion mojo/public/bindings/sample/sample_service.mojom
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ struct Foo {

[Peer=ServiceClient]
interface Service {
void Frobinate(Foo foo @0, bool baz @1, handle<message_pipe> port @2) @0;
enum BazOptions {
BAZ_REGULAR = 0,
BAZ_EXTRA
};
void Frobinate(Foo foo @0, int32 baz @1, handle<message_pipe> port @2) @0;
};

[Peer=Service]
Expand Down
6 changes: 3 additions & 3 deletions mojo/public/bindings/sample/sample_service_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,14 @@ static void DumpHex(const uint8_t* bytes, uint32_t num_bytes) {

class ServiceImpl : public ServiceStub {
public:
virtual void Frobinate(const Foo& foo, bool baz,
virtual void Frobinate(const Foo& foo, int32_t baz,
mojo::ScopedMessagePipeHandle port)
MOJO_OVERRIDE {
// Users code goes here to handle the incoming Frobinate message.

// We mainly check that we're given the expected arguments.
CheckFoo(foo);
EXPECT_TRUE(baz);
EXPECT_EQ(BAZ_EXTRA, baz);

// Also dump the Foo structure and all of its members.
// TODO(vtl): Make it optional, so that the test spews less?
Expand Down Expand Up @@ -299,7 +299,7 @@ TEST(BindingsSampleTest, Basic) {
mojo::ScopedMessagePipeHandle port0, port1;
mojo::CreateMessagePipe(&port0, &port1);

service->Frobinate(foo, true, port0.Pass());
service->Frobinate(foo, Service::BAZ_EXTRA, port0.Pass());
}

} // namespace sample
4 changes: 2 additions & 2 deletions mojo/public/bindings/sample/sample_service_unittests.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ define([

ServiceImpl.prototype.frobinate = function(foo, baz, port) {
checkFoo(foo);
expect(baz).toBeTruthy();
expect(baz).toBe(sample.ServiceStub.BazOptions.BAZ_EXTRA);
expect(port).toBe(10);
global.result = "PASS";
};
Expand All @@ -115,5 +115,5 @@ define([
checkFoo(foo);

var port = 10;
serviceProxy.frobinate(foo, true, port);
serviceProxy.frobinate(foo, sample.ServiceProxy.BazOptions.BAZ_EXTRA, port);
});

0 comments on commit 1e788d0

Please sign in to comment.