|
| 1 | +// Converts web platform names for the different ArrayBufferViews |
| 2 | +// to the names that ASC understands. Currently, that only means |
| 3 | +// to cut off the `Big` in `BigInt64Array`. |
| 4 | +const stdlibTypedArrayPrefix = "~lib/typedarray/"; |
| 5 | +function normalizeArrayBufferViewTypeName(typeName) { |
| 6 | + // Don’t do anything if this is not a stdlib type. |
| 7 | + if (!typeName.startsWith(stdlibTypedArrayPrefix)) { |
| 8 | + return typeName; |
| 9 | + } |
| 10 | + typeName = typeName.slice(stdlibTypedArrayPrefix.length); |
| 11 | + if (typeName.startsWith("Big")) { |
| 12 | + // Slice off `Big` as the loader doesn’t have that prefix. |
| 13 | + typeName = typeName.slice(3); |
| 14 | + } |
| 15 | + return typeName; |
| 16 | +} |
| 17 | + |
| 18 | +function nop(asbindInstance, value, typeName) { |
| 19 | + return value; |
| 20 | +} |
| 21 | + |
| 22 | +function getString(asbindInstance, value, typeName) { |
| 23 | + return asbindInstance.exports.__getString(value); |
| 24 | +} |
| 25 | + |
| 26 | +function putString(asbindInstance, value, typeName) { |
| 27 | + return asbindInstance.exports.__newString(value); |
| 28 | +} |
| 29 | + |
| 30 | +function getArrayBuffer(asbindInstance, value, typeName) { |
| 31 | + return asbindInstance.exports.__getArrayBuffer(value); |
| 32 | +} |
| 33 | + |
| 34 | +function putArrayBuffer(asbindInstance, value, typeName) { |
| 35 | + const ptr = asbindInstance.exports.__new( |
| 36 | + value.byteLength, |
| 37 | + asbindInstance.getTypeId(typeName) |
| 38 | + ); |
| 39 | + new Uint8Array( |
| 40 | + asbindInstance.exports.memory.buffer, |
| 41 | + ptr, |
| 42 | + value.byteLength |
| 43 | + ).set(new Uint8Array(value)); |
| 44 | + return ptr; |
| 45 | +} |
| 46 | + |
| 47 | +function getArrayBufferView(asbindInstance, value, typeName) { |
| 48 | + return asbindInstance.exports[ |
| 49 | + `__get${normalizeArrayBufferViewTypeName(typeName)}View` |
| 50 | + ](value); |
| 51 | +} |
| 52 | +function putArrayBufferView(asbindInstance, value, typeName) { |
| 53 | + return asbindInstance.exports.__newArray( |
| 54 | + asbindInstance.getTypeId(typeName), |
| 55 | + value |
| 56 | + ); |
| 57 | +} |
| 58 | + |
| 59 | +const stdlibArray = "~lib/array/Array"; |
| 60 | +function arrayInnerType(typeName) { |
| 61 | + if (!typeName.startsWith(stdlibArray)) { |
| 62 | + throw Error(`${JSON.stringify(typeName)} is not an array type`); |
| 63 | + } |
| 64 | + // Cut off stdlib path + generic angle brackets. |
| 65 | + return typeName.slice(`${stdlibArray}<`.length, -1); |
| 66 | +} |
| 67 | + |
| 68 | +function getArray(asbindInstance, value, typeName) { |
| 69 | + const innerTypeName = arrayInnerType(typeName); |
| 70 | + const innerTypeConverter = getConverterForType(innerTypeName); |
| 71 | + const rawArray = asbindInstance.exports.__getArray(value); |
| 72 | + return rawArray.map(v => |
| 73 | + innerTypeConverter.ascToJs(asbindInstance, v, innerTypeName) |
| 74 | + ); |
| 75 | +} |
| 76 | + |
| 77 | +function putArray(asbindInstance, value, typeName) { |
| 78 | + const innerTypeName = arrayInnerType(typeName); |
| 79 | + const innerTypeConverter = getConverterForType(innerTypeName); |
| 80 | + const convertedValues = value.map(v => |
| 81 | + innerTypeConverter.jsToAsc(asbindInstance, v, innerTypeName) |
| 82 | + ); |
| 83 | + return asbindInstance.exports.__newArray( |
| 84 | + asbindInstance.getTypeId(typeName), |
| 85 | + convertedValues |
| 86 | + ); |
| 87 | +} |
| 88 | + |
| 89 | +export const converters = new Map([ |
| 90 | + ["void", { ascToJs: nop, jsToAsc: nop }], |
| 91 | + [/^(i|u|f)(8|16|32)$/, { ascToJs: nop, jsToAsc: nop }], |
| 92 | + ["~lib/string/String", { ascToJs: getString, jsToAsc: putString }], |
| 93 | + [ |
| 94 | + "~lib/typedarray/Int8Array", |
| 95 | + { ascToJs: getArrayBufferView, jsToAsc: putArrayBufferView } |
| 96 | + ], |
| 97 | + [ |
| 98 | + "~lib/typedarray/Int16Array", |
| 99 | + { ascToJs: getArrayBufferView, jsToAsc: putArrayBufferView } |
| 100 | + ], |
| 101 | + [ |
| 102 | + "~lib/typedarray/Int32Array", |
| 103 | + { ascToJs: getArrayBufferView, jsToAsc: putArrayBufferView } |
| 104 | + ], |
| 105 | + [ |
| 106 | + "~lib/typedarray/Uint8Array", |
| 107 | + { ascToJs: getArrayBufferView, jsToAsc: putArrayBufferView } |
| 108 | + ], |
| 109 | + [ |
| 110 | + "~lib/typedarray/Uint16Array", |
| 111 | + { ascToJs: getArrayBufferView, jsToAsc: putArrayBufferView } |
| 112 | + ], |
| 113 | + [ |
| 114 | + "~lib/typedarray/Uint32Array", |
| 115 | + { ascToJs: getArrayBufferView, jsToAsc: putArrayBufferView } |
| 116 | + ], |
| 117 | + [ |
| 118 | + "~lib/typedarray/Int64Array", |
| 119 | + { ascToJs: getArrayBufferView, jsToAsc: putArrayBufferView } |
| 120 | + ], |
| 121 | + [ |
| 122 | + "~lib/typedarray/Uint64Array", |
| 123 | + { ascToJs: getArrayBufferView, jsToAsc: putArrayBufferView } |
| 124 | + ], |
| 125 | + [ |
| 126 | + "~lib/typedarray/Uint8ClampedArray", |
| 127 | + { ascToJs: getArrayBufferView, jsToAsc: putArrayBufferView } |
| 128 | + ], |
| 129 | + [ |
| 130 | + "~lib/typedarray/Float32Array", |
| 131 | + { ascToJs: getArrayBuffer, jsToAsc: putArrayBufferView } |
| 132 | + ], |
| 133 | + [ |
| 134 | + "~lib/typedarray/Float64Array", |
| 135 | + { ascToJs: getArrayBuffer, jsToAsc: putArrayBufferView } |
| 136 | + ], |
| 137 | + [ |
| 138 | + "~lib/arraybuffer/ArrayBuffer", |
| 139 | + { ascToJs: getArrayBuffer, jsToAsc: putArrayBuffer } |
| 140 | + ], |
| 141 | + [/^~lib\/array\/Array<.+>$/, { ascToJs: getArray, jsToAsc: putArray }] |
| 142 | +]); |
| 143 | + |
| 144 | +const warned = new Set(); |
| 145 | +export function getConverterForType(typeName) { |
| 146 | + for (const [matcher, converter] of converters) { |
| 147 | + if (typeof matcher === "string") { |
| 148 | + if (matcher === typeName) { |
| 149 | + return converter; |
| 150 | + } |
| 151 | + continue; |
| 152 | + } else if (matcher.test(typeName)) { |
| 153 | + return converter; |
| 154 | + } |
| 155 | + } |
| 156 | + if (!warned.has(typeName)) { |
| 157 | + console.warn( |
| 158 | + `No converter for ${JSON.stringify(typeName)}, using pass-through` |
| 159 | + ); |
| 160 | + warned.add(typeName); |
| 161 | + } |
| 162 | + return { ascToJs: nop, jsToAsc: nop }; |
| 163 | +} |
| 164 | + |
| 165 | +export function getAscToJsConverterForType(typeName) { |
| 166 | + return getConverterForType(typeName)?.ascToJs; |
| 167 | +} |
| 168 | + |
| 169 | +export function getJsToAscConverterForType(typeName) { |
| 170 | + return getConverterForType(typeName)?.jsToAsc; |
| 171 | +} |
0 commit comments