| 4809 | } |
| 4810 | } |
| 4811 | static __encode__(source, encoding) { |
| 4812 | if (source === undefined) { |
| 4813 | return 0; |
| 4814 | } |
| 4815 | if (Number.isInteger(source)) { |
| 4816 | return source; |
| 4817 | } |
| 4818 | if (Array.isArray(source) || source instanceof Uint8Array) { |
| 4819 | return source; |
| 4820 | } |
| 4821 | if (typeof source === 'string') { |
| 4822 | switch (encoding) { |
| 4823 | case 'latin1': |
| 4824 | case 'latin-1': |
| 4825 | return source; |
| 4826 | case 'utf8': |
| 4827 | case 'utf-8': |
| 4828 | return new TextEncoder('utf-8').encode(source); |
| 4829 | case undefined: |
| 4830 | throw new python.Error('Unsupported string argument without an encoding.'); |
| 4831 | default: |
| 4832 | throw new python.Error(`Unsupported encoding '${encoding}'.`); |
| 4833 | } |
| 4834 | } |
| 4835 | throw new python.Error('Unsupported source.'); |
| 4836 | } |
| 4837 | }); |
| 4838 | this.registerType('builtins.bytes', class extends Uint8Array { |
| 4839 | constructor(source, encoding /*, errors */) { |