blob: 9394d8b27fe85f84bd90f34dc50203305034947c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
// 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.
define([
"mojo/public/bindings/js/core",
"mojo/public/bindings/js/codec",
], function(core, codec) {
{%- for struct in structs %}
function {{struct.name}}() {
{%- for packed_field in struct.packed.packed_fields %}
this.{{packed_field.field.name}} = {{packed_field.field|default_value}};
{%- endfor %}
}
{{struct.name}}.encodedSize = codec.kStructHeaderSize + {{struct.packed|payload_size}};
{{struct.name}}.decode = function(decoder) {
var packed;
var val = new {{struct.name}}();
var numberOfBytes = decoder.read32();
var numberOfFields = decoder.read32();
{%- for byte in struct.bytes %}
{%- if byte.packed_fields|length > 1 %}
packed = decoder.read8();
{%- for packed_field in byte.packed_fields %}
val.{{packed_field.field.name}} = (packed >> {{packed_field.bit}}) & 1 ? true : false;
{%- endfor %}
{%- else %}
{%- for packed_field in byte.packed_fields %}
val.{{packed_field.field.name}} = decoder.{{packed_field.field.kind|decode_snippet}};
{%- endfor %}
{%- endif %}
{%- if byte.is_padding %}
decoder.skip(1);
{%- endif %}
{%- endfor %}
return val;
};
{{struct.name}}.encode = function(encoder, val) {
var packed;
encoder.write32({{struct.name}}.encodedSize);
encoder.write32({{struct.packed.packed_fields|length}});
{%- for byte in struct.bytes %}
{%- if byte.packed_fields|length > 1 %}
packed = 0;
{%- for packed_field in byte.packed_fields %}
packed |= (val.{{packed_field.field.name}} & 1) << {{packed_field.bit}}
{%- endfor %}
encoder.write8(packed);
{%- else %}
{%- for packed_field in byte.packed_fields %}
encoder.{{packed_field.field.kind|encode_snippet}}val.{{packed_field.field.name}});
{%- endfor %}
{%- endif %}
{%- if byte.is_padding %}
encoder.skip(1);
{%- endif %}
{%- endfor %}
};
{%- endfor %}
{%- for interface in interfaces %}
{%- for method in interface.methods %}
var k{{interface.name}}_{{method.name}}_Name = {{method.ordinal}};
{%- endfor %}
function {{interface.name}}Proxy(receiver) {
this.receiver_ = receiver;
}
{%- for method in interface.methods %}
{{interface.name}}Proxy.prototype.{{method.name|stylize_method}} = function(
{%- for parameter in method.parameters -%}
{{parameter.name}}{% if not loop.last %}, {% endif %}
{%- endfor %}) {
var params = new {{interface.name}}_{{method.name}}_Params();
{%- for parameter in method.parameters %}
params.{{parameter.name}} = {{parameter.name}};
{%- endfor %}
var builder = new codec.MessageBuilder(
k{{interface.name}}_{{method.name}}_Name,
codec.align({{interface.name}}_{{method.name}}_Params.encodedSize));
builder.encodeStruct({{interface.name}}_{{method.name}}_Params, params);
var message = builder.finish();
this.receiver_.accept(message);
};
{%- endfor %}
function {{interface.name}}Stub() {
}
{{interface.name}}Stub.prototype.accept = function(message) {
var reader = new codec.MessageReader(message);
switch (reader.messageName) {
{%- for method in interface.methods %}
case k{{interface.name}}_{{method.name}}_Name:
var params = reader.decodeStruct({{interface.name}}_{{method.name}}_Params);
this.{{method.name|stylize_method}}(
{%- for parameter in method.parameters -%}
params.{{parameter.name}}{% if not loop.last %}, {% endif %}
{%- endfor %});
return true;
{%- endfor %}
default:
return false;
}
};
{%- endfor %}
var exports = {};
{%- for struct in structs if struct.exported %}
exports.{{struct.name}} = {{struct.name}};
{%- endfor %}
{%- for interface in interfaces %}
exports.{{interface.name}}Proxy = {{interface.name}}Proxy;
exports.{{interface.name}}Stub = {{interface.name}}Stub;
{%- endfor %}
return exports;
});
|