Skip to content

Commit 7383e12

Browse files
committed
FEAT: added RL_Make_Vector into RL api, so one can make a VECTOR from extension
1 parent f59f31e commit 7383e12

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

src/core/a-lib.c

+24-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
** Module: a-lib.c
2323
** Summary: exported REBOL library functions
2424
** Section: environment
25-
** Author: Carl Sassenrath
25+
** Author: Carl Sassenrath, Oldes
2626
** Notes:
2727
**
2828
***********************************************************************/
@@ -559,6 +559,29 @@ RL_API void *RL_Make_Image(u32 width, u32 height)
559559
return Make_Image(width, height, FALSE);
560560
}
561561

562+
RL_API void *RL_Make_Vector(REBINT type, REBINT sign, REBINT dims, REBINT bits, REBINT size)
563+
/*
564+
** Allocate a new vector of the given attributes.
565+
**
566+
** Returns:
567+
** A pointer to a vector series or zero.
568+
** Arguments:
569+
** type: the datatype
570+
** sign: signed or unsigned
571+
** dims: number of dimensions
572+
** bits: number of bits per unit (8, 16, 32, 64)
573+
** size: number of values
574+
** Notes:
575+
** Allocated with REBOL's internal memory manager.
576+
** Vectors are automatically garbage collected if there are
577+
** no references to them from REBOL code (C code does nothing.)
578+
*/
579+
{
580+
// check if bits is valid
581+
if(!(bits == 8 || bits == 16 || bits == 32 || bits == 64)) return 0;
582+
return Make_Vector(type, sign, dims, bits, size);
583+
}
584+
562585
RL_API void RL_Protect_GC(REBSER *series, u32 flags)
563586
/*
564587
** Protect memory from garbage collection.

src/os/host-ext-test.c

+36-1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ char *RX_Spec =
7272
"hndl1: command [{creates a handle}]\n"
7373
"hndl2: command [{return handle's internal value as integer} hnd [handle!]]\n"
7474
"vec0: command [{return vector size in bytes} v [vector!]]\n"
75+
"vec1: command [{return vector size in bytes (from object)} o [object!]]\n"
76+
"blk1: command [{print type ids of all values in a block} b [block!]]\n"
7577

7678
"a: b: c: h: none\n"
7779
"xtest: does [\n"
@@ -99,6 +101,8 @@ char *RX_Spec =
99101
"[c: do-commands [a: xarg0 b: xarg1 333 xobj1 system 'version] reduce [a b c]]\n"
100102
"[cec0 [a: cec1 b: cec1 c: cec1] reduce [a b c]]\n"
101103
"[vec0 make vector! [integer! 16 [1 2 3]]]\n"
104+
"[vec1 object [v: make vector! [integer! 16 [1 2 3]]]]\n"
105+
"[blk1 [read %img /at 1]]\n"
102106
"][\n"
103107
"print [{^[[7mtest:^[[0m} mold blk]\n"
104108
"prin { } \n"
@@ -174,6 +178,8 @@ REBCNT Test_Async_Callback(REBSER *obj, REBCNT word)
174178
RXIEXT int RX_Call(int cmd, RXIFRM *frm, void *ctx) {
175179
REBYTE *str;
176180

181+
printf("Context ptr: %08X\n", ctx);
182+
177183
switch (cmd) {
178184

179185
case 0: //command [{return zero}]
@@ -246,7 +252,7 @@ RXIEXT int RX_Call(int cmd, RXIFRM *frm, void *ctx) {
246252
}
247253
break;
248254

249-
case 12: //command [{return handle's internal value as integer} hnd [handle!]]"
255+
case 12: //command [{return handle's internal value as integer} hnd [handle!]]
250256
{
251257
i64 i = (i64)RXA_HANDLE(frm, 1);
252258
RXA_INT64(frm, 1) = i;
@@ -261,6 +267,35 @@ RXIEXT int RX_Call(int cmd, RXIFRM *frm, void *ctx) {
261267
RXA_INT64(frm, 1) = vec->info * vec->tail;
262268
}
263269
break;
270+
case 14: //command [{return vector size in values (from object)} o [object!]]
271+
{
272+
RXIARG vec;
273+
REBCNT type = RL_GET_FIELD(RXA_OBJECT(frm, 1), RL_MAP_WORD("v"), &vec);
274+
if(type == RXT_VECTOR) {
275+
REBSER *vecs = (REBSER*)vec.series;
276+
u16* data = (u16*)vecs->data;
277+
printf("data[0-2]: %i, %i, %i\n", data[0], data[1], data[2]);
278+
//RXA_TYPE(frm, 1) = RXT_INTEGER;
279+
//RXA_INT64(frm, 1) = vecs->tail;
280+
} else {
281+
return RXR_FALSE;
282+
}
283+
}
284+
break;
285+
case 15: //command [{print type ids of all values in a block} b [block!]]
286+
{
287+
REBSER *blk = RXA_SERIES(frm, 1);
288+
REBCNT n, type;
289+
RXIARG val;
290+
printf("\nBlock with %i values:\n", RL_SERIES(blk, RXI_SER_TAIL));
291+
for(n = 0; type = RL_GET_VALUE(blk, n, &val); n++) {
292+
if(type == RXT_END) break;
293+
printf("\t%i -> %i\n", n, type);
294+
}
295+
RL_MAP_WORDS(RXA_SERIES(frm, 1));
296+
return RXR_UNSET;
297+
}
298+
break;
264299

265300
default:
266301
return RXR_NO_COMMAND;

0 commit comments

Comments
 (0)