-
Notifications
You must be signed in to change notification settings - Fork 18
/
nodered.c
141 lines (120 loc) · 3.88 KB
/
nodered.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* Copyright (c) 2022-2023 Moddable Tech, Inc.
*
* This file is part of the Moddable SDK Runtime.
*
* The Moddable SDK Runtime is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Moddable SDK Runtime is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the Moddable SDK Runtime. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "xsmc.h"
#include "xsHost.h"
// 16 byte string. random hex values.
void xs_nodered_util_generateId(xsMachine *the)
{
char id[16];
size_t i;
static const char hex[] = "0123456789abcdef";
for (i = 0; i < sizeof(id); i+= 4) {
int r = c_rand();
id[i + 0] = hex[r & 0x0f];
id[i + 1] = hex[(r >> 4) & 0x0f];
id[i + 2] = hex[(r >> 8) & 0x0f];
id[i + 3] = hex[(r >> 12) & 0x0f];
}
xsmcSetStringBuffer(xsResult, id, sizeof(id));
}
void xs_nodered_util_debugging(xsMachine *the)
{
#ifdef mxDebug
extern xsBooleanValue fxIsConnected(xsMachine* the);
xsmcSetBoolean(xsResult, fxIsConnected(the));
#endif
}
void xs_buffer_prototype_indexOf(xsMachine *the)
{
int offset = 0;
uint8_t *needle, *haystack, *initialHaystack;
xsUnsignedValue needleLength, haystackLength;
if (xsmcArgc > 1) {
offset = xsmcToInteger(xsArg(1));
if (xsmcArgc > 2)
xsUnknownError("unsupported");
}
if (xsReferenceType != xsmcTypeOf(xsArg(0)))
xsUnknownError("unsupported"); // valid to pass string or number for arg(0)
xsmcGetBufferReadable(xsArg(0), (void **)&needle, &needleLength);
xsmcGetBufferReadable(xsThis, (void **)&haystack, &haystackLength);
initialHaystack = haystack;
if (0 == offset)
;
else if ((offset < 0) && (-offset < (int)haystackLength)) {
haystack += haystackLength + offset;
haystackLength = -offset;
}
else if ((offset > 0) && (offset < (int)haystackLength)) {
haystack += offset;
haystackLength -= offset;
}
else
xsUnknownError("invalid offset");
if (needleLength <= haystackLength) {
uint8_t *lastHaystack = haystack + haystackLength - needleLength;
for ( ; haystack <= lastHaystack; haystack++) {
if (0 == c_memcmp(haystack, needle, needleLength)) {
xsmcSetInteger(xsResult, haystack - initialHaystack);
return;
}
}
}
xsmcSetInteger(xsResult, -1);
}
void xs_buffer_prototype_lastIndexOf(xsMachine *the)
{
int offset;
uint8_t *needle, *haystack, *initialHaystack;
xsUnsignedValue needleLength, haystackLength;
if (xsmcArgc > 1) {
offset = xsmcToInteger(xsArg(1));
if (xsmcArgc > 2)
xsUnknownError("unsupported");
}
if (xsReferenceType != xsmcTypeOf(xsArg(0)))
xsUnknownError("unsupported"); // valid to pass string or number for arg(0)
xsmcGetBufferReadable(xsArg(0), (void **)&needle, &needleLength);
xsmcGetBufferReadable(xsThis, (void **)&haystack, &haystackLength);
initialHaystack = haystack;
if (xsmcArgc < 2)
haystack = initialHaystack + haystackLength - 1;
else if (0 == offset)
;
else if ((offset < 0) && (-offset < (int)haystackLength)) {
haystack += haystackLength + offset;
}
else if ((offset > 0) && (offset < (int)haystackLength)) {
haystack += offset;
}
else
xsUnknownError("invalid offset");
if (needleLength <= haystackLength) {
if ((haystack + needleLength) > (initialHaystack + haystackLength))
haystack = initialHaystack + haystackLength - needleLength;
for ( ; haystack >= initialHaystack; haystack--) {
if (0 == c_memcmp(haystack, needle, needleLength)) {
xsmcSetInteger(xsResult, haystack - initialHaystack);
return;
}
}
}
xsmcSetInteger(xsResult, -1);
}