summaryrefslogtreecommitdiffstats
path: root/chromium/build/fuchsia/fidlgen_js/runtime/fidl.mjs
blob: d611e61aa514251be2cbebda5f90a4ef0ba3d327 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Copyright 2018 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.

// This is the JS runtime support library for code generated by fidlgen_js. It
// mostly consists of helpers to facilitate encoding and decoding of FIDL
// messages.

const $fidl_kInitialBufferSize = 1024;

const $fidl_kMessageHeaderSize = 16;
const $fidl_kMessageTxidOffset = 0;
const $fidl_kMessageOrdinalOffset = 8;

const $fidl__kAlignment = 8;
const $fidl__kAlignmentMask = 0x7;

const $fidl__kLE = true;

const $fidl__kUserspaceTxidMask = 0x7fffffff;
const $fidl__kHandlePresent = 0xffffffff;
const $fidl__kInvalidUnionTag = 0xffffffff;
var $fidl__nextTxid = 1;

function $fidl__align(size) {
  return size + (($fidl__kAlignment - (size & $fidl__kAlignmentMask)) &
                 $fidl__kAlignmentMask);
}

function $fidl__setUint64LE(dataView, offset, value) {
  var high_bits = Number(BigInt.asUintN(32, value >> 32n))
  var low_bits = Number(BigInt.asUintN(32, value))
  dataView.setUint32(offset+4, high_bits, $fidl__kLE);
  dataView.setUint32(offset+0, low_bits, $fidl__kLE);
}


/**
 * @constructor
 * @param {number} ordinal
 */
function $fidl_Encoder(ordinal, has_response) {
  var buf = new ArrayBuffer($fidl_kInitialBufferSize);
  this.data = new DataView(buf);
  this.extent = 0;
  this.handles = [];
  this._encodeMessageHeader(ordinal, has_response);
}

/**
 * @param {number} ordinal
 */
$fidl_Encoder.prototype._encodeMessageHeader = function(ordinal, has_response) {
  this.alloc($fidl_kMessageHeaderSize);
  var txid = has_response ? ($fidl__nextTxid++ & $fidl__kUserspaceTxidMask) : 0;
  this.data.setUint32($fidl_kMessageTxidOffset, txid, $fidl__kLE);
  $fidl__setUint64LE(
      this.data, $fidl_kMessageOrdinalOffset, ordinal, $fidl__kLE);
};

/**
 * @param {number} size
 */
$fidl_Encoder.prototype.alloc = function(size) {
  var offset = this.extent;
  this._claimMemory($fidl__align(size));
  return offset;
};

/**
 * @param {number} claimSize
 */
$fidl_Encoder.prototype._claimMemory = function(claimSize) {
  this.extent += claimSize;
  if (this.extent > this.data.byteLength) {
    var newSize = this.data.byteLength + claimSize;
    newSize += newSize * 2;
    this._grow(newSize);
  }
};

/**
 * @param {number} newSize
 */
$fidl_Encoder.prototype._grow = function(newSize) {
  var newBuffer = new ArrayBuffer(newSize);
  new Uint8Array(newBuffer).set(new Uint8Array(this.data.buffer));
  this.data = new DataView(newBuffer);
};

/**
 * @param {number} handle
 */
$fidl_Encoder.prototype.addHandle = function(handle) {
  this.handles.push(handle);
};

$fidl_Encoder.prototype.messageData = function() {
  return new DataView(this.data.buffer, 0, this.extent);
};

$fidl_Encoder.prototype.messageHandles = function() {
  return this.handles;
};


/**
 * @constructor
 * @param {Array} data
 * @param {Array} handles
 */
function $fidl_Decoder(data, handles) {
  this.data = data;
  this.handles = handles;
  this.nextOffset = 0;
  this.nextHandle = 0;
  this.claimMemory($fidl_kMessageHeaderSize);
}

/**
 * @param {number} size
 */
$fidl_Decoder.prototype.claimMemory = function(size) {
  var result = this.nextOffset;
  this.nextOffset = $fidl__align(this.nextOffset + size);
  return result;
}

$fidl_Decoder.prototype.claimHandle = function() {
  if (this.nextHandle >= this.handles.length)
    throw "Attempt to claim more handles than are available";
  return this.handles[this.nextHandle++];
}


// Type tables and encoding helpers for generated Proxy code.
const _kTT_bool = {
  enc: function(e, o, v) { e.data.setInt8(o, v ? 1 : 0); },
  dec: function(d, o) { return d.data.getInt8(o) != 0; },
};

const _kTT_float32 = {
  enc: function(e, o, v) { e.data.setFloat32(o, v, $fidl__kLE); },
  dec: function(d, o) { return d.data.getFloat32(o, $fidl__kLE); },
};

const _kTT_float64 = {
  enc: function(e, o, v) { e.data.setFloat64(o, v, $fidl__kLE); },
  dec: function(d, o) { return d.data.getFloat64(o, $fidl__kLE); },
};

const _kTT_int8 = {
  enc: function(e, o, v) { e.data.setInt8(o, v); },
  dec: function(d, o) { return d.data.getInt8(o); },
};

const _kTT_int16 = {
  enc: function(e, o, v) { e.data.setInt16(o, v, $fidl__kLE); },
  dec: function(d, o) { return d.data.getInt16(o, $fidl__kLE); },
};

const _kTT_int32 = {
  enc: function(e, o, v) { e.data.setUint32(o, v, $fidl__kLE); },
  dec: function(d, o) { return d.data.getInt32(o, $fidl__kLE); },
};

const _kTT_int64 = {
  enc: function(e, o, v) {
    var bi = BigInt.asIntN(64, BigInt(v));
    var x = Number(bi & 0xffffffffn);
    var y = Number((bi >> 32n) & 0xffffffffn);
    e.data.setInt32(o, x, $fidl__kLE);
    e.data.setInt32(o + 4, y, $fidl__kLE);
  },
  dec: function(d, o) {
    var x = BigInt.asIntN(64, BigInt(d.data.getInt32(o, $fidl__kLE)));
    var y = BigInt.asIntN(64, BigInt(d.data.getInt32(o + 4, $fidl__kLE)));
    return x | (y << 32n);
  },
};

const _kTT_uint8 = {
  enc: function(e, o, v) { e.data.setUint8(o, v); },
  dec: function(d, o) { return d.data.getUint8(o); },
};

const _kTT_uint16 = {
  enc: function(e, o, v) { e.data.setUint16(o, v, $fidl__kLE); },
  dec: function(d, o) { return d.data.getUint16(o, $fidl__kLE); },
};

const _kTT_uint32 = {
  enc: function(e, o, v) { e.data.setUint32(o, v, $fidl__kLE); },
  dec: function(d, o) { return d.data.getUint32(o, $fidl__kLE); },
};

const _kTT_uint64 = {
  enc: function(e, o, v) {
    var bi = BigInt.asUintN(64, BigInt(v));
    var x = Number(bi & 0xffffffffn);
    var y = Number((bi >> 32n) & 0xffffffffn);
    e.data.setUint32(o, x, $fidl__kLE);
    e.data.setUint32(o + 4, y, $fidl__kLE);
  },
  dec: function(d, o) {
    var x = BigInt.asUintN(64, BigInt(d.data.getUint32(o, $fidl__kLE)));
    var y = BigInt.asUintN(64, BigInt(d.data.getUint32(o + 4, $fidl__kLE)));
    return x | (y << 32n);
  },
};

const _kTT_Handle = {
  enc: function(e, o, v) {
    if (v === null || v === undefined) {
      e.data.setUint32(o, 0, $fidl__kLE);
    } else {
      e.data.setUint32(o, $fidl__kHandlePresent, $fidl__kLE);
      e.addHandle(v);
    }
  },
  dec: function(d, o) {
    var $present = d.data.getUint32(o, $fidl__kLE);
    if ($present === 0) {
      return 0;
    } else {
      if ($present !== $fidl__kHandlePresent)
        throw "Expected UINT32_MAX to indicate handle presence";
      return d.claimHandle();
    }
  },
};

const _kTT_String = {
  enc: function(e, o, v) {
    if (v === null || v === undefined) throw "non-null string required";
    // Both size and data are uint64, but that's awkward in JS, so for now only
    // support a maximum of 32b lengths. The maximum length of a FIDL message is
    // shorter than 32b in any case.
    var asUtf8 = $FidlJsStrToUtf8Array(v);
    e.data.setUint32(o, asUtf8.length, $fidl__kLE);
    e.data.setUint32(o + 4, 0, $fidl__kLE);
    e.data.setUint32(o + 8, 0xffffffff, $fidl__kLE);
    e.data.setUint32(o + 12, 0xffffffff, $fidl__kLE);
    var body = e.alloc(asUtf8.length);
    for (var i = 0; i < asUtf8.length; i++) {
      e.data.setUint8(body + i, asUtf8[i], $fidl__kLE);
    }
  },
  dec: function(d, o) {
    var len = d.data.getUint32(o, $fidl__kLE);
    var pointer = d.data.getUint32(o + 8, $fidl__kLE);
    if (pointer === 0) throw "non-null string required";
    var dataOffset = d.claimMemory(len);
    return $FidlJsUtf8ArrayToStr(new DataView(d.data.buffer, dataOffset, len));
  }
};

const _kTT_String_Nullable = {
  enc: function(e, o, v) {
    if (v === null || v === undefined) {
      e.data.setUint32(o, 0, $fidl__kLE);
      e.data.setUint32(o + 4, 0, $fidl__kLE);
      e.data.setUint32(o + 8, 0, $fidl__kLE);
      e.data.setUint32(o + 12, 0, $fidl__kLE);
    } else {
      _kTT_String.enc(e, o, v);
    }
  },
  dec: function(d, o) {
    if (v === null || v === undefined) {
      var pointer = d.data.getUint32(o + 8, $fidl__kLE);
      if (pointer === 0) {
        return null;
      }
    } else {
      return _kTT_String.dec(e, o, v);
    }
  }
};