Skip to content

Commit 87957f0

Browse files
committed
Improve E.mapInPlace docs, and allow it to work with no map (eg pass straight through)
1 parent 772bf04 commit 87957f0

File tree

2 files changed

+42
-17
lines changed

2 files changed

+42
-17
lines changed

ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
Fix buffer overrun if we have to reallocate a pointer to argument lists when calling a function (fix #1491)
7474
Fix stack overflow if executing regex full of hundreds of open brackets (fix #1487)
7575
Fix issue where STM32F4 USB could lock up if TX during heavy RX
76+
Improve `E.mapInPlace` docs, and allow it to work with no map (eg pass straight through)
7677

7778
1v99 : Increase jslMatch error buffer size to handle "UNFINISHED TEMPLATE LITERAL" string (#1426)
7879
nRF5x: Make FlashWrite cope with flash writes > 4k

src/jswrap_espruino.c

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,20 +1183,40 @@ JsVarInt jswrap_espruino_getAddressOf(JsVar *v, bool flatAddress) {
11831183
"params" : [
11841184
["from","JsVar","An ArrayBuffer to read elements from"],
11851185
["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"]
11881188
]
11891189
}
11901190
Take each element of the `from` array, look it up in `map` (or call the
11911191
function with it as a first argument), and write it into the corresponding
11921192
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+
```
11931213
*/
11941214
void jswrap_espruino_mapInPlace(JsVar *from, JsVar *to, JsVar *map, JsVarInt bits) {
11951215
if (!jsvIsArrayBuffer(from) || !jsvIsArrayBuffer(to)) {
11961216
jsExceptionHere(JSET_ERROR, "First 2 arguments should be array buffers");
11971217
return;
11981218
}
1199-
if (!jsvIsArray(map) && !jsvIsArrayBuffer(map) && !jsvIsFunction(map)) {
1219+
if (map && !jsvIsArray(map) && !jsvIsArrayBuffer(map) && !jsvIsFunction(map)) {
12001220
jsExceptionHere(JSET_ERROR, "Third argument should be a function or array");
12011221
return;
12021222
}
@@ -1221,21 +1241,25 @@ void jswrap_espruino_mapInPlace(JsVar *from, JsVar *to, JsVar *map, JsVarInt bit
12211241
el = jsvArrayBufferIteratorGetIntegerValue(&itFrom);
12221242
}
12231243

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);
12361262
}
1237-
jsvArrayBufferIteratorSetValue(&itTo, v2);
1238-
jsvUnLock(v2);
12391263
jsvArrayBufferIteratorNext(&itTo);
12401264
}
12411265
jsvArrayBufferIteratorFree(&itFrom);

0 commit comments

Comments
 (0)