Skip to content

Commit 6b1b9f5

Browse files
committed
Added non-standard Uint24Array, because it's very useful for RGB. Also sped up ArrayBuffer read/write
1 parent 87957f0 commit 6b1b9f5

File tree

4 files changed

+42
-13
lines changed

4 files changed

+42
-13
lines changed

ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
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
7676
Improve `E.mapInPlace` docs, and allow it to work with no map (eg pass straight through)
77+
Added non-standard Uint24Array, because it's very useful for RGB
7778

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

src/jsvar.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ typedef enum {
112112
ARRAYBUFFERVIEW_INT8 = 1 | ARRAYBUFFERVIEW_SIGNED,
113113
ARRAYBUFFERVIEW_UINT16 = 2,
114114
ARRAYBUFFERVIEW_INT16 = 2 | ARRAYBUFFERVIEW_SIGNED,
115+
ARRAYBUFFERVIEW_UINT24 = 3,
115116
ARRAYBUFFERVIEW_UINT32 = 4,
116117
ARRAYBUFFERVIEW_INT32 = 4 | ARRAYBUFFERVIEW_SIGNED,
117118
ARRAYBUFFERVIEW_FLOAT32 = 4 | ARRAYBUFFERVIEW_FLOAT,

src/jsvariterator.c

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -345,13 +345,12 @@ static void jsvArrayBufferIteratorGetValueData(JsvArrayBufferIterator *it, char
345345

346346
static JsVarInt jsvArrayBufferIteratorDataToInt(JsvArrayBufferIterator *it, char *data) {
347347
unsigned int dataLen = JSV_ARRAYBUFFER_GET_SIZE(it->type);
348-
JsVarInt v = 0;
349-
if (dataLen==1) v = *(int8_t*)data;
350-
else if (dataLen==2) v = *(short*)data;
351-
else if (dataLen==4) return *(int*)data;
352-
else assert(0);
353-
if ((!JSV_ARRAYBUFFER_IS_SIGNED(it->type)))
354-
v = v & (JsVarInt)((1UL << (8*dataLen))-1);
348+
unsigned int bits = 8*dataLen;
349+
JsVarInt mask = (JsVarInt)((1UL << bits)-1);
350+
JsVarInt v = *(int*)data;
351+
v = v & mask;
352+
if (JSV_ARRAYBUFFER_IS_SIGNED(it->type) && (v&(JsVarInt)(1UL<<(bits-1))))
353+
v |= ~mask;
355354
return v;
356355
}
357356

@@ -415,12 +414,9 @@ static void jsvArrayBufferIteratorIntToData(char *data, unsigned int dataLen, in
415414
if (v<0) v=0;
416415
if (v>255) v=255;
417416
}
418-
// we don't care about sign when writing - as it gets truncated
419-
if (dataLen==1) { data[0] = (char)v; }
420-
else if (dataLen==2) { *(short*)data = (short)v; }
421-
else if (dataLen==4) { *(int*)data = (int)v; }
422-
else if (dataLen==8) { *(long long*)data = (long long)v; }
423-
else assert(0);
417+
// we don't care about sign when writing (or any extra bytes!) - as it gets truncated
418+
if (dataLen==8) *(long long*)data = (long long)v;
419+
else *(int*)data = (int)v;
424420
}
425421

426422
static void jsvArrayBufferIteratorFloatToData(char *data, unsigned int dataLen, int type, JsVarFloat v) {

src/jswrap_arraybuffer.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ This is the built-in JavaScript class that is the prototype for:
4040
* [Int8Array](/Reference#Int8Array)
4141
* [Uint16Array](/Reference#Uint16Array)
4242
* [Int16Array](/Reference#Int16Array)
43+
* [Uint24Array](/Reference#Uint24Array) (Espruino-specific - not standard JS)
4344
* [Uint32Array](/Reference#Uint32Array)
4445
* [Int32Array](/Reference#Int32Array)
4546
* [Float32Array](/Reference#Float32Array)
@@ -112,6 +113,20 @@ This is the built-in JavaScript class for a typed array of 16 bit signed integer
112113
113114
Instantiate this in order to efficiently store arrays of data (Espruino's normal arrays store data in a map, which is inefficient for non-sparse arrays).
114115
116+
Arrays of this type include all the methods from [ArrayBufferView](/Reference#ArrayBufferView)
117+
*/
118+
/*JSON{
119+
"type" : "class",
120+
"class" : "Uint24Array",
121+
"ifndef" : "SAVE_ON_FLASH",
122+
"prototype" : "ArrayBufferView",
123+
"check" : "jsvIsArrayBuffer(var) && var->varData.arraybuffer.type==ARRAYBUFFERVIEW_UINT24",
124+
"not_real_object" : "Don't treat this as a real object - it's handled differently internally"
125+
}
126+
This is the built-in JavaScript class for a typed array of 24 bit unsigned integers.
127+
128+
Instantiate this in order to efficiently store arrays of data (Espruino's normal arrays store data in a map, which is inefficient for non-sparse arrays).
129+
115130
Arrays of this type include all the methods from [ArrayBufferView](/Reference#ArrayBufferView)
116131
*/
117132
/*JSON{
@@ -298,12 +313,28 @@ Create a typed array based on the given input. Either an existing Array Buffer,
298313
"return" : ["JsVar","A typed array"],
299314
"return_object" : "ArrayBufferView"
300315
}
316+
Create a typed array based on the given input. Either an existing Array Buffer, an Integer as a Length, or a simple array. If an ArrayBuffer view (eg. Uint8Array rather than ArrayBuffer) is given, it will be completely copied rather than referenced.
317+
*/
318+
/*JSON{
319+
"type" : "constructor",
320+
"class" : "Uint24Array",
321+
"name" : "Uint24Array",
322+
"generate_full" : "jswrap_typedarray_constructor(ARRAYBUFFERVIEW_UINT24, arr, byteOffset, length)",
323+
"params" : [
324+
["arr","JsVar","The array or typed array to base this off, or an integer which is the array length"],
325+
["byteOffset","int","The byte offset in the ArrayBuffer (ONLY IF the first argument was an ArrayBuffer)"],
326+
["length","int","The length (ONLY IF the first argument was an ArrayBuffer)"]
327+
],
328+
"return" : ["JsVar","A typed array"],
329+
"return_object" : "ArrayBufferView"
330+
}
301331
Create a typed array based on the given input. Either an existing Array Buffer, an Integer as a Length, or a simple array. If an ArrayBuffer view (eg. Uint8Array rather than ArrayBuffer) is given, it will be completely copied rather than referenced.
302332
*/
303333
/*JSON{
304334
"type" : "constructor",
305335
"class" : "Uint32Array",
306336
"name" : "Uint32Array",
337+
"ifndef" : "SAVE_ON_FLASH",
307338
"generate_full" : "jswrap_typedarray_constructor(ARRAYBUFFERVIEW_UINT32, arr, byteOffset, length)",
308339
"params" : [
309340
["arr","JsVar","The array or typed array to base this off, or an integer which is the array length"],

0 commit comments

Comments
 (0)