@@ -1183,20 +1183,40 @@ JsVarInt jswrap_espruino_getAddressOf(JsVar *v, bool flatAddress) {
1183
1183
"params" : [
1184
1184
["from","JsVar","An ArrayBuffer to read elements from"],
1185
1185
["to","JsVar","An ArrayBuffer to write elements too"],
1186
- ["map","JsVar","An array or function to use to map one element to another"],
1187
- ["bits","int","If specified, the number of bits per element"]
1186
+ ["map","JsVar","An array or function to use to map one element to another, or undefined to provide no mapping "],
1187
+ ["bits","int","If specified, the number of bits per element - otherwise use a 1:1 mapping "]
1188
1188
]
1189
1189
}
1190
1190
Take each element of the `from` array, look it up in `map` (or call the
1191
1191
function with it as a first argument), and write it into the corresponding
1192
1192
element in the `to` array.
1193
+
1194
+ You can use an array to map:
1195
+
1196
+ ```
1197
+ var a = new Uint8Array([1,2,3,1,2,3]);
1198
+ var lut = new Uint8Array([128,129,130,131]);
1199
+ E.mapInPlace(a, a, lut);
1200
+ // a = [129, 130, 131, 129, 130, 131]
1201
+ ```
1202
+
1203
+ Or `undefined` to pass straight through, or a function to do a normal 'mapping':
1204
+
1205
+ ```
1206
+ var a = new Uint8Array([0x12,0x34,0x56,0x78]);
1207
+ var b = new Uint8Array(8);
1208
+ E.mapInPlace(a, b, undefined, 4); // 4 bits from 8 bit input -> 2x as many outputs
1209
+ // b = [1, 2, 3, 4, 5, 6, 7, 8]
1210
+ E.mapInPlace(a, b, a=>a+2, 4);
1211
+ // b = [3, 4, 5, 6, 7, 8, 9, 10]
1212
+ ```
1193
1213
*/
1194
1214
void jswrap_espruino_mapInPlace (JsVar * from , JsVar * to , JsVar * map , JsVarInt bits ) {
1195
1215
if (!jsvIsArrayBuffer (from ) || !jsvIsArrayBuffer (to )) {
1196
1216
jsExceptionHere (JSET_ERROR , "First 2 arguments should be array buffers" );
1197
1217
return ;
1198
1218
}
1199
- if (!jsvIsArray (map ) && !jsvIsArrayBuffer (map ) && !jsvIsFunction (map )) {
1219
+ if (map && !jsvIsArray (map ) && !jsvIsArrayBuffer (map ) && !jsvIsFunction (map )) {
1200
1220
jsExceptionHere (JSET_ERROR , "Third argument should be a function or array" );
1201
1221
return ;
1202
1222
}
@@ -1221,21 +1241,25 @@ void jswrap_espruino_mapInPlace(JsVar *from, JsVar *to, JsVar *map, JsVarInt bit
1221
1241
el = jsvArrayBufferIteratorGetIntegerValue (& itFrom );
1222
1242
}
1223
1243
1224
- JsVar * v2 = 0 ;
1225
- if (isFn ) {
1226
- JsVar * args [2 ];
1227
- args [0 ] = jsvNewFromInteger (v );
1228
- args [1 ] = jsvArrayBufferIteratorGetIndex (& itFrom ); // child is a variable name, create a new variable for the index
1229
- v2 = jspeFunctionCall (map , 0 , 0 , false, 2 , args );
1230
- jsvUnLockMany (2 ,args );
1231
- } else if (jsvIsArray (map )) {
1232
- v2 = jsvGetArrayItem (map , v );
1233
- } else {
1234
- assert (jsvIsArrayBuffer (map ));
1235
- v2 = jsvArrayBufferGet (map , (size_t )v );
1244
+ if (map ) {
1245
+ JsVar * v2 = 0 ;
1246
+ if (isFn ) {
1247
+ JsVar * args [2 ];
1248
+ args [0 ] = jsvNewFromInteger (v );
1249
+ args [1 ] = jsvArrayBufferIteratorGetIndex (& itFrom ); // child is a variable name, create a new variable for the index
1250
+ v2 = jspeFunctionCall (map , 0 , 0 , false, 2 , args );
1251
+ jsvUnLockMany (2 ,args );
1252
+ } else if (jsvIsArray (map )) {
1253
+ v2 = jsvGetArrayItem (map , v );
1254
+ } else {
1255
+ assert (jsvIsArrayBuffer (map ));
1256
+ v2 = jsvArrayBufferGet (map , (size_t )v );
1257
+ }
1258
+ jsvArrayBufferIteratorSetValue (& itTo , v2 );
1259
+ jsvUnLock (v2 );
1260
+ } else { // no map - push right through
1261
+ jsvArrayBufferIteratorSetIntegerValue (& itTo , v );
1236
1262
}
1237
- jsvArrayBufferIteratorSetValue (& itTo , v2 );
1238
- jsvUnLock (v2 );
1239
1263
jsvArrayBufferIteratorNext (& itTo );
1240
1264
}
1241
1265
jsvArrayBufferIteratorFree (& itFrom );
0 commit comments