(writer, coders, values)
| 6 | * @_ignore |
| 7 | */ |
| 8 | export function pack(writer, coders, values) { |
| 9 | let arrayValues = []; |
| 10 | if (Array.isArray(values)) { |
| 11 | arrayValues = values; |
| 12 | } |
| 13 | else if (values && typeof (values) === "object") { |
| 14 | let unique = {}; |
| 15 | arrayValues = coders.map((coder) => { |
| 16 | const name = coder.localName; |
| 17 | assert(name, "cannot encode object for signature with missing names", "INVALID_ARGUMENT", { argument: "values", info: { coder }, value: values }); |
| 18 | assert(!unique[name], "cannot encode object for signature with duplicate names", "INVALID_ARGUMENT", { argument: "values", info: { coder }, value: values }); |
| 19 | unique[name] = true; |
| 20 | return values[name]; |
| 21 | }); |
| 22 | } |
| 23 | else { |
| 24 | assertArgument(false, "invalid tuple value", "tuple", values); |
| 25 | } |
| 26 | assertArgument(coders.length === arrayValues.length, "types/value length mismatch", "tuple", values); |
| 27 | let staticWriter = new Writer(); |
| 28 | let dynamicWriter = new Writer(); |
| 29 | let updateFuncs = []; |
| 30 | coders.forEach((coder, index) => { |
| 31 | let value = arrayValues[index]; |
| 32 | if (coder.dynamic) { |
| 33 | // Get current dynamic offset (for the future pointer) |
| 34 | let dynamicOffset = dynamicWriter.length; |
| 35 | // Encode the dynamic value into the dynamicWriter |
| 36 | coder.encode(dynamicWriter, value); |
| 37 | // Prepare to populate the correct offset once we are done |
| 38 | let updateFunc = staticWriter.writeUpdatableValue(); |
| 39 | updateFuncs.push((baseOffset) => { |
| 40 | updateFunc(baseOffset + dynamicOffset); |
| 41 | }); |
| 42 | } |
| 43 | else { |
| 44 | coder.encode(staticWriter, value); |
| 45 | } |
| 46 | }); |
| 47 | // Backfill all the dynamic offsets, now that we know the static length |
| 48 | updateFuncs.forEach((func) => { func(staticWriter.length); }); |
| 49 | let length = writer.appendWriter(staticWriter); |
| 50 | length += writer.appendWriter(dynamicWriter); |
| 51 | return length; |
| 52 | } |
| 53 | /** |
| 54 | * @_ignore |
| 55 | */ |
no test coverage detected
searching dependent graphs…