@mikrojs/native 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CMakeLists.txt +198 -0
- package/LICENSE +21 -0
- package/README.md +49 -0
- package/cmake/mikrojs_bytecode.cmake +146 -0
- package/cmake.js +22 -0
- package/dist/index.d.ts +52 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +132 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +43 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/include/byteorder_apple.h +11 -0
- package/include/byteorder_windows.h +12 -0
- package/include/mikrojs/cbor_helpers.h +24 -0
- package/include/mikrojs/cutils_wrap.h +59 -0
- package/include/mikrojs/errors.h +144 -0
- package/include/mikrojs/mem.h +11 -0
- package/include/mikrojs/mik_color.h +32 -0
- package/include/mikrojs/mikrojs.h +331 -0
- package/include/mikrojs/platform.h +82 -0
- package/include/mikrojs/private.h +281 -0
- package/include/mikrojs/utils.h +125 -0
- package/package.json +100 -0
- package/prebuilds/darwin-arm64/mikrojs.napi.node +0 -0
- package/prebuilds/linux-arm64/mikrojs.napi.node +0 -0
- package/prebuilds/linux-x64/mikrojs.napi.node +0 -0
- package/runtime/ble/ble.ts +231 -0
- package/runtime/ble/types.ts +194 -0
- package/runtime/ble/uuid.ts +89 -0
- package/runtime/ble/validators.ts +61 -0
- package/runtime/cbor/cbor.ts +1 -0
- package/runtime/cbor/types.ts +8 -0
- package/runtime/console/types.ts +50 -0
- package/runtime/env/env.ts +17 -0
- package/runtime/env/types.ts +12 -0
- package/runtime/format/types.ts +4 -0
- package/runtime/fs/fs.ts +93 -0
- package/runtime/fs/types.ts +92 -0
- package/runtime/globals.d.ts +87 -0
- package/runtime/http/helpers.ts +222 -0
- package/runtime/http/native.ts +151 -0
- package/runtime/http/request.ts +25 -0
- package/runtime/i2c/i2c.ts +35 -0
- package/runtime/i2c/types.ts +55 -0
- package/runtime/inspect/types.ts +10 -0
- package/runtime/internal.d.ts +456 -0
- package/runtime/kv/nvs.ts +17 -0
- package/runtime/kv/rtc.ts +17 -0
- package/runtime/kv/shared.ts +107 -0
- package/runtime/kv/types.ts +150 -0
- package/runtime/neopixel/neopixel.ts +38 -0
- package/runtime/neopixel/types.ts +27 -0
- package/runtime/pin/pin.ts +51 -0
- package/runtime/pin/types.ts +49 -0
- package/runtime/pwm/pwm.ts +32 -0
- package/runtime/pwm/types.ts +29 -0
- package/runtime/reader/reader.ts +167 -0
- package/runtime/reader/types.ts +34 -0
- package/runtime/result/native-result.node-shim.ts +44 -0
- package/runtime/result/result.ts +26 -0
- package/runtime/result/types.ts +60 -0
- package/runtime/schema/schema.ts +321 -0
- package/runtime/schema/types.ts +152 -0
- package/runtime/sleep/sleep.ts +14 -0
- package/runtime/sleep/types.ts +44 -0
- package/runtime/sntp/sntp.ts +54 -0
- package/runtime/sntp/types.ts +38 -0
- package/runtime/spi/spi.ts +31 -0
- package/runtime/spi/types.ts +42 -0
- package/runtime/stdio/stdio.ts +44 -0
- package/runtime/stdio/types.ts +22 -0
- package/runtime/stream/stream.ts +150 -0
- package/runtime/stream/types.ts +47 -0
- package/runtime/sys/sys.ts +90 -0
- package/runtime/sys/types.ts +131 -0
- package/runtime/test/test.ts +595 -0
- package/runtime/test/types.ts +97 -0
- package/runtime/uart/types.ts +75 -0
- package/runtime/uart/uart.ts +51 -0
- package/runtime/wifi/types.ts +156 -0
- package/runtime/wifi/wifi.ts +208 -0
- package/scripts/bundle-runtime.js +149 -0
- package/scripts/compare-minifiers.js +189 -0
- package/scripts/compile-bytecode.sh +38 -0
- package/scripts/copy-prebuild.js +20 -0
- package/scripts/generate-symbol-map.js +146 -0
- package/src/builtins.cpp +82 -0
- package/src/cutils_compat.c +38 -0
- package/src/eval_bytecode.cpp +42 -0
- package/src/fs.cpp +878 -0
- package/src/mem.cpp +63 -0
- package/src/mik_abort.cpp +160 -0
- package/src/mik_app_config.cpp +358 -0
- package/src/mik_cbor.cpp +334 -0
- package/src/mik_color.cpp +46 -0
- package/src/mik_console.cpp +422 -0
- package/src/mik_inspect.cpp +850 -0
- package/src/mik_repl.cpp +1122 -0
- package/src/mik_result.cpp +344 -0
- package/src/mik_stdio.cpp +147 -0
- package/src/mik_sys.cpp +239 -0
- package/src/mik_text_encoding.cpp +443 -0
- package/src/mikrojs.cpp +942 -0
- package/src/modules.cpp +944 -0
- package/src/platform_posix.cpp +134 -0
- package/src/timers.cpp +208 -0
- package/src/utils.cpp +173 -0
package/src/mik_cbor.cpp
ADDED
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
#include <cmath>
|
|
2
|
+
#include <cstring>
|
|
3
|
+
|
|
4
|
+
#include <nanocbor/nanocbor.h>
|
|
5
|
+
#include <quickjs.h>
|
|
6
|
+
|
|
7
|
+
#include "mikrojs/cbor_helpers.h"
|
|
8
|
+
#include "mikrojs/mikrojs.h"
|
|
9
|
+
#include "mikrojs/utils.h"
|
|
10
|
+
|
|
11
|
+
/* ── Encoder ────────────────────────────────────────────────────────── */
|
|
12
|
+
|
|
13
|
+
int mik__cbor_encode_value(JSContext* ctx, nanocbor_encoder_t* enc, JSValue val, int depth) {
|
|
14
|
+
if (depth > MIK_CBOR_MAX_DEPTH) {
|
|
15
|
+
return -1;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
int tag = JS_VALUE_GET_NORM_TAG(val);
|
|
19
|
+
|
|
20
|
+
switch (tag) {
|
|
21
|
+
case JS_TAG_INT: {
|
|
22
|
+
int32_t v;
|
|
23
|
+
JS_ToInt32(ctx, &v, val);
|
|
24
|
+
nanocbor_fmt_int(enc, v);
|
|
25
|
+
return 0;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
case JS_TAG_FLOAT64: {
|
|
29
|
+
double d = JS_VALUE_GET_FLOAT64(val);
|
|
30
|
+
/* Encode integers compactly */
|
|
31
|
+
if (std::isfinite(d) && d == std::trunc(d) && d >= -2147483648.0 && d <= 2147483647.0) {
|
|
32
|
+
nanocbor_fmt_int(enc, static_cast<int64_t>(d));
|
|
33
|
+
} else {
|
|
34
|
+
nanocbor_fmt_double(enc, d);
|
|
35
|
+
}
|
|
36
|
+
return 0;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
case JS_TAG_BOOL: {
|
|
40
|
+
nanocbor_fmt_bool(enc, JS_ToBool(ctx, val) != 0);
|
|
41
|
+
return 0;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
case JS_TAG_NULL: {
|
|
45
|
+
nanocbor_fmt_null(enc);
|
|
46
|
+
return 0;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
case JS_TAG_UNDEFINED: {
|
|
50
|
+
nanocbor_fmt_undefined(enc);
|
|
51
|
+
return 0;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
case JS_TAG_STRING: {
|
|
55
|
+
size_t len = 0;
|
|
56
|
+
const char* str = JS_ToCStringLen(ctx, &len, val);
|
|
57
|
+
if (!str) return -1;
|
|
58
|
+
nanocbor_put_tstrn(enc, str, len);
|
|
59
|
+
JS_FreeCString(ctx, str);
|
|
60
|
+
return 0;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
case JS_TAG_OBJECT: {
|
|
64
|
+
/* Uint8Array -> bstr */
|
|
65
|
+
if (JS_GetTypedArrayType(val) == JS_TYPED_ARRAY_UINT8) {
|
|
66
|
+
size_t len = 0;
|
|
67
|
+
const uint8_t* data = JS_GetUint8Array(ctx, &len, val);
|
|
68
|
+
if (!data && len > 0) return -1;
|
|
69
|
+
nanocbor_put_bstr(enc, data, len);
|
|
70
|
+
return 0;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/* Array */
|
|
74
|
+
if (JS_IsArray(val)) {
|
|
75
|
+
JSValue length_val = JS_GetPropertyStr(ctx, val, "length");
|
|
76
|
+
uint32_t length = 0;
|
|
77
|
+
JS_ToUint32(ctx, &length, length_val);
|
|
78
|
+
JS_FreeValue(ctx, length_val);
|
|
79
|
+
|
|
80
|
+
nanocbor_fmt_array(enc, length);
|
|
81
|
+
for (uint32_t i = 0; i < length; i++) {
|
|
82
|
+
JSValue elem = JS_GetPropertyUint32(ctx, val, i);
|
|
83
|
+
int rc = mik__cbor_encode_value(ctx, enc, elem, depth + 1);
|
|
84
|
+
JS_FreeValue(ctx, elem);
|
|
85
|
+
if (rc < 0) return rc;
|
|
86
|
+
}
|
|
87
|
+
return 0;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/* Plain object -> map with string keys */
|
|
91
|
+
if (JS_IsFunction(ctx, val) || JS_IsError(val)) {
|
|
92
|
+
return -1;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
JSPropertyEnum* props = nullptr;
|
|
96
|
+
uint32_t prop_count = 0;
|
|
97
|
+
if (JS_GetOwnPropertyNames(ctx, &props, &prop_count, val,
|
|
98
|
+
JS_GPN_STRING_MASK | JS_GPN_ENUM_ONLY) < 0) {
|
|
99
|
+
return -1;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
nanocbor_fmt_map(enc, prop_count);
|
|
103
|
+
for (uint32_t i = 0; i < prop_count; i++) {
|
|
104
|
+
const char* key = JS_AtomToCString(ctx, props[i].atom);
|
|
105
|
+
if (!key) {
|
|
106
|
+
js_free(ctx, props);
|
|
107
|
+
return -1;
|
|
108
|
+
}
|
|
109
|
+
nanocbor_put_tstr(enc, key);
|
|
110
|
+
JS_FreeCString(ctx, key);
|
|
111
|
+
|
|
112
|
+
JSValue prop_val = JS_GetProperty(ctx, val, props[i].atom);
|
|
113
|
+
int rc = mik__cbor_encode_value(ctx, enc, prop_val, depth + 1);
|
|
114
|
+
JS_FreeValue(ctx, prop_val);
|
|
115
|
+
if (rc < 0) {
|
|
116
|
+
js_free(ctx, props);
|
|
117
|
+
return rc;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
js_free(ctx, props);
|
|
122
|
+
return 0;
|
|
123
|
+
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
default:
|
|
127
|
+
return -1;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
static JSValue mik__cbor_encode(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) {
|
|
132
|
+
if (argc < 1) {
|
|
133
|
+
return mik__result_err_named(ctx, "EncodeFailed",
|
|
134
|
+
"CBOR encode requires a value argument");
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
JSValue val = argv[0];
|
|
138
|
+
|
|
139
|
+
/* Pass 1: compute size with NULL buffer */
|
|
140
|
+
nanocbor_encoder_t enc;
|
|
141
|
+
nanocbor_encoder_init(&enc, nullptr, 0);
|
|
142
|
+
if (mik__cbor_encode_value(ctx, &enc, val, 0) < 0) {
|
|
143
|
+
return mik__result_err_named(
|
|
144
|
+
ctx, "EncodeFailed",
|
|
145
|
+
"CBOR encode failed: unsupported value type or max depth exceeded");
|
|
146
|
+
}
|
|
147
|
+
size_t needed = nanocbor_encoded_len(&enc);
|
|
148
|
+
|
|
149
|
+
/* Pass 2: encode into buffer */
|
|
150
|
+
auto* buf = static_cast<uint8_t*>(js_malloc(ctx, needed > 0 ? needed : 1));
|
|
151
|
+
if (!buf) return JS_EXCEPTION;
|
|
152
|
+
|
|
153
|
+
nanocbor_encoder_init(&enc, buf, needed);
|
|
154
|
+
if (mik__cbor_encode_value(ctx, &enc, val, 0) < 0) {
|
|
155
|
+
js_free(ctx, buf);
|
|
156
|
+
return mik__result_err_named(ctx, "EncodeFailed",
|
|
157
|
+
"CBOR encode failed during buffer write");
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
JSValue result = JS_NewUint8ArrayCopy(ctx, buf, needed);
|
|
161
|
+
js_free(ctx, buf);
|
|
162
|
+
|
|
163
|
+
if (JS_IsException(result)) return JS_EXCEPTION;
|
|
164
|
+
return mik__result_ok(ctx, result);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/* ── Decoder ────────────────────────────────────────────────────────── */
|
|
168
|
+
|
|
169
|
+
JSValue mik__cbor_decode_value(JSContext* ctx, nanocbor_value_t* val, int depth) {
|
|
170
|
+
if (depth > MIK_CBOR_MAX_DEPTH) {
|
|
171
|
+
return JS_EXCEPTION;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
int type = nanocbor_get_type(val);
|
|
175
|
+
if (type < 0) return JS_EXCEPTION;
|
|
176
|
+
|
|
177
|
+
switch (type) {
|
|
178
|
+
case NANOCBOR_TYPE_UINT: {
|
|
179
|
+
uint64_t v;
|
|
180
|
+
if (nanocbor_get_uint64(val, &v) < 0) return JS_EXCEPTION;
|
|
181
|
+
if (v <= INT64_MAX) {
|
|
182
|
+
return JS_NewInt64(ctx, static_cast<int64_t>(v));
|
|
183
|
+
}
|
|
184
|
+
return JS_NewFloat64(ctx, static_cast<double>(v));
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
case NANOCBOR_TYPE_NINT: {
|
|
188
|
+
int64_t v;
|
|
189
|
+
if (nanocbor_get_int64(val, &v) < 0) return JS_EXCEPTION;
|
|
190
|
+
return JS_NewInt64(ctx, v);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
case NANOCBOR_TYPE_BSTR: {
|
|
194
|
+
const uint8_t* buf = nullptr;
|
|
195
|
+
size_t len = 0;
|
|
196
|
+
if (nanocbor_get_bstr(val, &buf, &len) < 0) return JS_EXCEPTION;
|
|
197
|
+
return JS_NewUint8ArrayCopy(ctx, buf, len);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
case NANOCBOR_TYPE_TSTR: {
|
|
201
|
+
const uint8_t* buf = nullptr;
|
|
202
|
+
size_t len = 0;
|
|
203
|
+
if (nanocbor_get_tstr(val, &buf, &len) < 0) return JS_EXCEPTION;
|
|
204
|
+
return JS_NewStringLen(ctx, reinterpret_cast<const char*>(buf), len);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
case NANOCBOR_TYPE_ARR: {
|
|
208
|
+
nanocbor_value_t arr;
|
|
209
|
+
if (nanocbor_enter_array(val, &arr) < 0) return JS_EXCEPTION;
|
|
210
|
+
|
|
211
|
+
JSValue result = JS_NewArray(ctx);
|
|
212
|
+
uint32_t idx = 0;
|
|
213
|
+
while (!nanocbor_at_end(&arr)) {
|
|
214
|
+
JSValue elem = mik__cbor_decode_value(ctx, &arr, depth + 1);
|
|
215
|
+
if (JS_IsException(elem)) {
|
|
216
|
+
JS_FreeValue(ctx, result);
|
|
217
|
+
return JS_EXCEPTION;
|
|
218
|
+
}
|
|
219
|
+
JS_SetPropertyUint32(ctx, result, idx++, elem);
|
|
220
|
+
}
|
|
221
|
+
nanocbor_leave_container(val, &arr);
|
|
222
|
+
return result;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
case NANOCBOR_TYPE_MAP: {
|
|
226
|
+
nanocbor_value_t map;
|
|
227
|
+
if (nanocbor_enter_map(val, &map) < 0) return JS_EXCEPTION;
|
|
228
|
+
|
|
229
|
+
JSValue result = JS_NewObject(ctx);
|
|
230
|
+
while (!nanocbor_at_end(&map)) {
|
|
231
|
+
/* Key must be a text string */
|
|
232
|
+
if (nanocbor_get_type(&map) != NANOCBOR_TYPE_TSTR) {
|
|
233
|
+
JS_FreeValue(ctx, result);
|
|
234
|
+
return JS_EXCEPTION;
|
|
235
|
+
}
|
|
236
|
+
const uint8_t* key_buf = nullptr;
|
|
237
|
+
size_t key_len = 0;
|
|
238
|
+
if (nanocbor_get_tstr(&map, &key_buf, &key_len) < 0) {
|
|
239
|
+
JS_FreeValue(ctx, result);
|
|
240
|
+
return JS_EXCEPTION;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
JSValue prop_val = mik__cbor_decode_value(ctx, &map, depth + 1);
|
|
244
|
+
if (JS_IsException(prop_val)) {
|
|
245
|
+
JS_FreeValue(ctx, result);
|
|
246
|
+
return JS_EXCEPTION;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
JSAtom atom =
|
|
250
|
+
JS_NewAtomLen(ctx, reinterpret_cast<const char*>(key_buf), key_len);
|
|
251
|
+
JS_SetProperty(ctx, result, atom, prop_val);
|
|
252
|
+
JS_FreeAtom(ctx, atom);
|
|
253
|
+
}
|
|
254
|
+
nanocbor_leave_container(val, &map);
|
|
255
|
+
return result;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
case NANOCBOR_TYPE_TAG: {
|
|
259
|
+
/* Strip tags: skip tag number and decode inner value */
|
|
260
|
+
uint32_t tag_num;
|
|
261
|
+
if (nanocbor_get_tag(val, &tag_num) < 0) return JS_EXCEPTION;
|
|
262
|
+
return mik__cbor_decode_value(ctx, val, depth);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
case NANOCBOR_TYPE_FLOAT: {
|
|
266
|
+
/* Check for simple values first (bool, null, undefined) */
|
|
267
|
+
bool b;
|
|
268
|
+
if (nanocbor_get_bool(val, &b) >= 0) {
|
|
269
|
+
return JS_NewBool(ctx, b);
|
|
270
|
+
}
|
|
271
|
+
if (nanocbor_get_null(val) >= 0) {
|
|
272
|
+
return JS_NULL;
|
|
273
|
+
}
|
|
274
|
+
if (nanocbor_get_undefined(val) >= 0) {
|
|
275
|
+
return JS_UNDEFINED;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/* Float value */
|
|
279
|
+
double d;
|
|
280
|
+
if (nanocbor_get_double(val, &d) >= 0) {
|
|
281
|
+
return JS_NewFloat64(ctx, d);
|
|
282
|
+
}
|
|
283
|
+
return JS_EXCEPTION;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
default:
|
|
287
|
+
return JS_EXCEPTION;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
static JSValue mik__cbor_decode(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) {
|
|
292
|
+
if (argc < 1 || JS_GetTypedArrayType(argv[0]) != JS_TYPED_ARRAY_UINT8) {
|
|
293
|
+
return mik__result_err_named(ctx, "DecodeFailed",
|
|
294
|
+
"CBOR decode requires a Uint8Array argument");
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
size_t len = 0;
|
|
298
|
+
const uint8_t* data = JS_GetUint8Array(ctx, &len, argv[0]);
|
|
299
|
+
if (!data && len > 0) {
|
|
300
|
+
return mik__result_err_named(ctx, "DecodeFailed",
|
|
301
|
+
"CBOR decode failed to read input buffer");
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
nanocbor_value_t decoder;
|
|
305
|
+
nanocbor_decoder_init(&decoder, data, len);
|
|
306
|
+
|
|
307
|
+
JSValue result = mik__cbor_decode_value(ctx, &decoder, 0);
|
|
308
|
+
if (JS_IsException(result)) {
|
|
309
|
+
return mik__result_err_named(ctx, "DecodeFailed",
|
|
310
|
+
"CBOR decode failed: malformed or unsupported data");
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
return mik__result_ok(ctx, result);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/* ── Module registration ────────────────────────────────────────────── */
|
|
317
|
+
|
|
318
|
+
static int mik__cbor_module_init(JSContext* ctx, JSModuleDef* m) {
|
|
319
|
+
JS_SetModuleExport(ctx, m, "encode", JS_NewCFunction(ctx, mik__cbor_encode, "encode", 1));
|
|
320
|
+
JS_SetModuleExport(ctx, m, "decode", JS_NewCFunction(ctx, mik__cbor_decode, "decode", 1));
|
|
321
|
+
return 0;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
JSModuleDef* mik__cbor_init(JSContext* ctx) {
|
|
325
|
+
JSModuleDef* m = JS_NewCModule(ctx, "native:cbor", mik__cbor_module_init);
|
|
326
|
+
if (!m) return nullptr;
|
|
327
|
+
JS_AddModuleExport(ctx, m, "encode");
|
|
328
|
+
JS_AddModuleExport(ctx, m, "decode");
|
|
329
|
+
return m;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// Registered explicitly in mikrojs.cpp (MIK_NewRuntime) rather than via
|
|
333
|
+
// MIK_REGISTER_MODULE, which relies on __attribute__((constructor)) that
|
|
334
|
+
// doesn't survive static library linking into Node.js addons.
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
#include "mikrojs/mik_color.h"
|
|
2
|
+
|
|
3
|
+
struct AnsiCode {
|
|
4
|
+
const char* open;
|
|
5
|
+
const char* close;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
/* ANSI color codes matching colors.ts ANSI_COLORS */
|
|
9
|
+
static const AnsiCode THEME_CODES[] = {
|
|
10
|
+
[MIK_TOKEN_ANNOTATION] = {"36", "0"}, // cyan
|
|
11
|
+
[MIK_TOKEN_BOOLEAN] = {"35", "0"}, // magenta
|
|
12
|
+
[MIK_TOKEN_COMMENT] = {"90", "39"}, // grey
|
|
13
|
+
[MIK_TOKEN_DATE] = {"35", "0"}, // magenta (stylize uses 'date' -> magenta in STYLES)
|
|
14
|
+
[MIK_TOKEN_ERROR] = {"31;1", "0"}, // bright_red
|
|
15
|
+
[MIK_TOKEN_WARNING] = {"33", "0"}, // yellow
|
|
16
|
+
[MIK_TOKEN_FUNCTION] = {"36", "0"}, // cyan
|
|
17
|
+
[MIK_TOKEN_IDENTIFIER] = {"32;1", "0"}, // bright_green
|
|
18
|
+
[MIK_TOKEN_KEYWORD] = {"37;1", "0"}, // bright_white
|
|
19
|
+
[MIK_TOKEN_NULL] = {"1", "22"}, // bold
|
|
20
|
+
[MIK_TOKEN_NUMBER] = {"33", "0"}, // yellow
|
|
21
|
+
[MIK_TOKEN_OTHER] = {"37", "0"}, // white
|
|
22
|
+
[MIK_TOKEN_REGEXP] = {"31", "0"}, // red
|
|
23
|
+
[MIK_TOKEN_STRING] = {"32", "0"}, // green
|
|
24
|
+
[MIK_TOKEN_SYMBOL] = {"32", "0"}, // green
|
|
25
|
+
[MIK_TOKEN_TYPE] = {"35;1", "0"}, // bright_magenta
|
|
26
|
+
[MIK_TOKEN_UNDEFINED] = {"90", "39"}, // grey
|
|
27
|
+
[MIK_TOKEN_BIGINT] = {"33", "0"}, // yellow (bigint -> yellow in STYLES)
|
|
28
|
+
[MIK_TOKEN_RESULT] = {"90", "39"}, // grey
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
std::string mik_colorize(MikThemeToken token, std::string_view value) {
|
|
32
|
+
if (token < 0 || token >= MIK_TOKEN__COUNT) {
|
|
33
|
+
return std::string(value);
|
|
34
|
+
}
|
|
35
|
+
const auto& code = THEME_CODES[token];
|
|
36
|
+
std::string result;
|
|
37
|
+
result.reserve(value.size() + 12);
|
|
38
|
+
result += "\033[";
|
|
39
|
+
result += code.open;
|
|
40
|
+
result += 'm';
|
|
41
|
+
result += value;
|
|
42
|
+
result += "\033[";
|
|
43
|
+
result += code.close;
|
|
44
|
+
result += 'm';
|
|
45
|
+
return result;
|
|
46
|
+
}
|