Commit bd60a551 authored by Scott Graham's avatar Scott Graham Committed by Commit Bot

Fuchsia: Implement handling of arrays in FIDL/JS

Bug: 883496
Change-Id: I4981cf288566aed5f95e3ca3b8632e5462203ee3
Reviewed-on: https://chromium-review.googlesource.com/c/1311262
Commit-Queue: Scott Graham <scottmg@chromium.org>
Reviewed-by: default avatarWez <wez@chromium.org>
Cr-Commit-Position: refs/heads/master@{#605455}
parent 76fd8f42
...@@ -133,12 +133,19 @@ class Compiler(object): ...@@ -133,12 +133,19 @@ class Compiler(object):
''') ''')
def _CompileConst(self, c): def _CompileConst(self, const):
compound = _ParseCompoundIdentifier(const.name)
name = _CompileCompoundIdentifier(compound)
value = _CompileConstant(const.value)
self.f.write('''/** self.f.write('''/**
* @const {%(type)s} * @const
*/ */
const %(name)s = %(value)s; const %(name)s = %(value)s;
''' % c.to_dict())
''' % {
'name': name,
'value': value
})
def _CompileEnum(self, enum): def _CompileEnum(self, enum):
compound = _ParseCompoundIdentifier(enum.name) compound = _ParseCompoundIdentifier(enum.name)
...@@ -227,6 +234,34 @@ function %(name)s(%(param_names)s) { ...@@ -227,6 +234,34 @@ function %(name)s(%(param_names)s) {
return name return name
elif t.kind == fidl.TypeKind.HANDLE: elif t.kind == fidl.TypeKind.HANDLE:
return 'Handle' return 'Handle'
elif t.kind == fidl.TypeKind.ARRAY:
element_ttname = self._CompileType(t.element_type)
ttname = 'ARR_%d_%s' % (t.element_count, element_ttname)
if ttname not in self.type_table_defined:
self.type_table_defined.add(ttname)
self.output_deferred_to_eof += ('''\
const _kTT_%(ttname)s = {
enc: function(e, o, v) {
for (var i = 0; i < %(element_count)s; i++) {
_kTT_%(element_ttname)s.enc(e, o + (i * %(element_size)s), v[i]);
}
},
dec: function(d, o) {
var result = [];
for (var i = 0; i < %(element_count)s; i++) {
result.push(_kTT_%(element_ttname)s.dec(d, o + (i * %(element_size)s)));
}
return result;
},
};
''' % {
'ttname': ttname,
'element_ttname': element_ttname,
'element_count': t.element_count,
'element_size': _InlineSizeOfType(t.element_type),
})
return ttname
elif t.kind == fidl.TypeKind.VECTOR: elif t.kind == fidl.TypeKind.VECTOR:
element_ttname = self._CompileType(t.element_type) element_ttname = self._CompileType(t.element_type)
ttname = ( ttname = (
...@@ -287,7 +322,7 @@ const _kTT_%(ttname)s = { ...@@ -287,7 +322,7 @@ const _kTT_%(ttname)s = {
}) })
return ttname return ttname
else: else:
raise NotImplementedError() raise NotImplementedError(t.kind)
def _GenerateJsInterfaceForInterface(self, name, interface): def _GenerateJsInterfaceForInterface(self, name, interface):
"""Generates a JS @interface for the given FIDL interface.""" """Generates a JS @interface for the given FIDL interface."""
......
...@@ -205,6 +205,9 @@ class TestolaImpl : public fidljstest::Testola { ...@@ -205,6 +205,9 @@ class TestolaImpl : public fidljstest::Testola {
sat.basic.u16 = basic_struct.u16 * 2; sat.basic.u16 = basic_struct.u16 * 2;
sat.basic.u32 = basic_struct.u32 * 2; sat.basic.u32 = basic_struct.u32 * 2;
sat.later_string = "ⓣⓔⓡⓜⓘⓝⓐⓣⓞⓡ"; sat.later_string = "ⓣⓔⓡⓜⓘⓝⓐⓣⓞⓡ";
for (uint64_t i = 0; i < fidljstest::ARRRR_SIZE; ++i) {
sat.arrrr[i] = static_cast<int32_t>(i * 5) - 10;
}
resp(std::move(sat)); resp(std::move(sat));
} }
...@@ -513,6 +516,7 @@ TEST_F(FidlGenJsTest, RawReceiveFidlNestedStructsAndRespond) { ...@@ -513,6 +516,7 @@ TEST_F(FidlGenJsTest, RawReceiveFidlNestedStructsAndRespond) {
this.result_basic_u16 = sat.basic.u16; this.result_basic_u16 = sat.basic.u16;
this.result_basic_u32 = sat.basic.u32; this.result_basic_u32 = sat.basic.u32;
this.result_later_string = sat.later_string; this.result_later_string = sat.later_string;
this.result_arrrr = sat.arrrr;
}) })
.catch((e) => log('FAILED: ' + e)); .catch((e) => log('FAILED: ' + e));
)"; )";
...@@ -537,6 +541,14 @@ TEST_F(FidlGenJsTest, RawReceiveFidlNestedStructsAndRespond) { ...@@ -537,6 +541,14 @@ TEST_F(FidlGenJsTest, RawReceiveFidlNestedStructsAndRespond) {
EXPECT_EQ(helper.Get<unsigned int>("result_basic_u16"), 64000u); EXPECT_EQ(helper.Get<unsigned int>("result_basic_u16"), 64000u);
EXPECT_EQ(helper.Get<unsigned int>("result_basic_u32"), 4000000000u); EXPECT_EQ(helper.Get<unsigned int>("result_basic_u32"), 4000000000u);
EXPECT_EQ(helper.Get<std::string>("result_later_string"), "ⓣⓔⓡⓜⓘⓝⓐⓣⓞⓡ"); EXPECT_EQ(helper.Get<std::string>("result_later_string"), "ⓣⓔⓡⓜⓘⓝⓐⓣⓞⓡ");
// Retrieve as a vector as there's no difference in representation in JS (and
// gin already supports vector), and verify the length matches the expected
// length of the fidl array.
auto result_arrrr = helper.Get<std::vector<int32_t>>("result_arrrr");
ASSERT_EQ(result_arrrr.size(), fidljstest::ARRRR_SIZE);
for (uint64_t i = 0; i < fidljstest::ARRRR_SIZE; ++i) {
EXPECT_EQ(result_arrrr[i], static_cast<int32_t>(i * 5) - 10);
}
} }
TEST_F(FidlGenJsTest, HandlePassing) { TEST_F(FidlGenJsTest, HandlePassing) {
......
...@@ -22,12 +22,15 @@ struct BasicStruct { ...@@ -22,12 +22,15 @@ struct BasicStruct {
uint32 u32 = 4000000000; uint32 u32 = 4000000000;
}; };
const uint64 ARRRR_SIZE = 32;
struct StuffAndThings { struct StuffAndThings {
int32 count; int32 count;
string id; string id;
vector<int32> a_vector; vector<int32> a_vector;
BasicStruct basic; BasicStruct basic;
string later_string; string later_string;
array<int32>:ARRRR_SIZE arrrr;
}; };
interface Testola { interface Testola {
......
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