forked from rsenn/qjs-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickjs-textcode.c
695 lines (560 loc) · 20.4 KB
/
quickjs-textcode.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
#include "defines.h"
#include "quickjs-textcode.h"
#include "utils.h"
#include "buffer-utils.h"
#include "debug.h"
#include <libutf.h>
#include <libutf.h>
#include "tutf8e/include/tutf8e.h"
/**
* \addtogroup quickjs-textdecoder
* @{
*/
VISIBLE JSClassID js_decoder_class_id = 0, js_encoder_class_id = 0;
VISIBLE JSValue textdecoder_proto = {{0}, JS_TAG_UNDEFINED}, textdecoder_ctor = {{0}, JS_TAG_UNDEFINED}, textencoder_proto = {{0}, JS_TAG_UNDEFINED},
textencoder_ctor = {{0}, JS_TAG_UNDEFINED};
const TUTF8encoder* tutf8e_coders[] = {
/* 0, 0, 0, 0, 0, 0, 0, 0, */
&tutf8e_encoder_iso_8859_1, &tutf8e_encoder_iso_8859_2, &tutf8e_encoder_iso_8859_3, &tutf8e_encoder_iso_8859_4, &tutf8e_encoder_iso_8859_5,
&tutf8e_encoder_iso_8859_6, &tutf8e_encoder_iso_8859_7, &tutf8e_encoder_iso_8859_8, &tutf8e_encoder_iso_8859_9, &tutf8e_encoder_iso_8859_10,
&tutf8e_encoder_iso_8859_11, &tutf8e_encoder_iso_8859_13, &tutf8e_encoder_iso_8859_14, &tutf8e_encoder_iso_8859_15, &tutf8e_encoder_iso_8859_16,
&tutf8e_encoder_windows_1250, &tutf8e_encoder_windows_1251, &tutf8e_encoder_windows_1252, &tutf8e_encoder_windows_1253, &tutf8e_encoder_windows_1254,
&tutf8e_encoder_windows_1255, &tutf8e_encoder_windows_1256, &tutf8e_encoder_windows_1257, &tutf8e_encoder_windows_1258,
};
const char* const textcode_encodings[] = {
"unknown", "UTF-8", "UTF-16", "UTF-32", "unknown", "UTF-8", "UTF-16BE", "UTF-32BE",
"ISO-8859-1", "ISO-8859-2", "ISO-8859-3", "ISO-8859-4", "ISO-8859-5", "ISO-8859-6", "ISO-8859-7", "ISO-8859-8",
"ISO-8859-9", "ISO-8859-10", "ISO-8859-11", "ISO-8859-13", "ISO-8859-14", "ISO-8859-15", "ISO-8859-16", "WINDOWS-1250",
"WINDOWS-1251", "WINDOWS-1252", "WINDOWS-1253", "WINDOWS-1254", "WINDOWS-1255", "WINDOWS-1256", "WINDOWS-1257", "WINDOWS-1258",
};
enum {
DECODER_DECODE,
DECODER_END,
};
enum {
DECODER_ENCODING,
DECODER_ENDIANNESS,
DECODER_BUFFERED,
};
static size_t
textdecoder_try(const void* in, size_t len) {
const uint8_t *x, *y, *end;
size_t r = 0;
int ch;
x = in;
end = x + len;
while(x < end) {
y = x;
if((ch = unicode_from_utf8(x, end - x, &x)) == -1)
break;
r += x - y;
}
return r;
}
size_t
textdecoder_length(TextDecoder* td) {
size_t len = 0, r;
r = textdecoder_try(ringbuffer_begin(&td->buffer), ringbuffer_continuous(&td->buffer));
len += r;
if(len == ringbuffer_continuous(&td->buffer))
if(td->buffer.head < td->buffer.tail) {
r = textdecoder_try(td->buffer.data, ringbuffer_head(&td->buffer));
len += r;
}
return len;
}
JSValue
textdecoder_decode(TextDecoder* dec, JSContext* ctx) {
JSValue ret = JS_UNDEFINED;
DynBuf dbuf;
size_t i = 0, blen;
uint_least32_t cp;
char tmp[UTF8_CHAR_LEN_MAX];
int len = 0;
js_dbuf_init(ctx, &dbuf);
blen = ringbuffer_length(&dec->buffer);
if(blen)
switch(dec->encoding) {
case UTF8: {
size_t blen, rlen = ringbuffer_length(&dec->buffer);
if((blen = textdecoder_length(dec)) < rlen) {
ringbuffer_normalize(&dec->buffer);
blen = textdecoder_length(dec);
}
ret = JS_NewStringLen(ctx, (const char*)ringbuffer_begin(&dec->buffer), blen);
ringbuffer_skip(&dec->buffer, blen);
break;
}
case UTF16: {
uint_least16_t* ptr = ringbuffer_begin(&dec->buffer);
size_t n = blen & ~(0x1);
for(i = 0; i < n; ptr = ringbuffer_next(&dec->buffer, ptr), i += 2) {
uint_least16_t u16[2] = {uint16_get_endian(ptr, dec->endian), 0};
size_t ns = 2;
if(utf16_multiword(u16)) {
if(i + 2 >= n)
break;
u16[1] = uint16_get_endian(ptr + 1, dec->endian == BIG);
ns += 2;
}
if(!libutf_c16_to_c32(u16, &cp)) {
ret = JS_ThrowInternalError(ctx,
"%s: TextDecoder: not a valid utf-16 code at (%llu: 0x%04x, 0x%04x): %lu",
__func__,
(long long unsigned int)i,
(unsigned int)ptr[0],
(unsigned int)ptr[1],
(unsigned long)cp);
break;
}
len = unicode_to_utf8((void*)tmp, cp);
if(dbuf_put(&dbuf, (const void*)tmp, len))
return JS_EXCEPTION;
}
break;
}
case UTF32: {
const uint_least32_t* ptr = ringbuffer_begin(&dec->buffer);
size_t n = blen & ~(0x3);
for(i = 0; i < n; ptr = ringbuffer_next(&dec->buffer, ptr), i += 4) {
cp = uint32_get_endian(ptr, dec->endian);
if(!libutf_c32_to_c8(cp, &len, tmp)) {
ret = JS_ThrowInternalError(ctx,
"%s: TextDecoder: not a valid utf-32 code at (%llu: 0x%04x, 0x%04x): %lu",
__func__,
(long long unsigned int)i,
(unsigned int)ptr[0],
(unsigned int)ptr[1],
(unsigned long)cp);
break;
}
if(dbuf_put(&dbuf, (const void*)tmp, len))
return JS_EXCEPTION;
}
break;
}
default: {
TUTF8encoder encoder;
if((encoder = *tutf8e_coders[dec->encoding - 8])) {
const char* ptr = ringbuffer_begin(&dec->buffer);
size_t n = 0;
if(TUTF8E_OK == tutf8e_encoder_buffer_length(encoder, ptr, 0, blen, &n)) {
uint8_t* dst = dbuf_reserve(&dbuf, n);
if(TUTF8E_OK == tutf8e_encoder_buffer_encode(encoder, ptr, blen, 0, (char*)dst, &n)) {
dbuf.size += n;
i = blen;
}
}
} else {
ret = JS_ThrowInternalError(ctx, "%s: TextDecoder: unknown encoding: %s", __func__, textcode_encodings[dec->encoding]);
}
break;
}
}
ringbuffer_skip(&dec->buffer, i);
if(JS_IsUndefined(ret) && dbuf.size > 0)
ret = JS_NewStringLen(ctx, (const char*)dbuf.buf, dbuf.size);
dbuf_free(&dbuf);
return ret;
}
static JSValue
js_decoder_get(JSContext* ctx, JSValueConst this_val, int magic) {
TextDecoder* dec;
JSValue ret = JS_UNDEFINED;
if(!(dec = js_decoder_data(ctx, this_val)))
return ret;
switch(magic) {
case DECODER_ENCODING: {
ret = JS_NewString(ctx, textcode_encodings[dec->type_code]);
break;
}
case DECODER_ENDIANNESS: {
ret = JS_NewBool(ctx, dec->endian == BIG);
break;
}
case DECODER_BUFFERED: {
ret = JS_NewUint32(ctx, ringbuffer_length(&dec->buffer));
break;
}
}
return ret;
}
static JSValue
js_decoder_constructor(JSContext* ctx, JSValueConst new_target, int argc, JSValueConst argv[]) {
JSValue obj = JS_UNDEFINED;
JSValue proto;
TextDecoder* dec;
if(!(dec = js_mallocz(ctx, sizeof(TextDecoder))))
return JS_EXCEPTION;
/* using new_target to get the prototype is necessary when the class is extended. */
proto = JS_GetPropertyStr(ctx, new_target, "prototype");
if(JS_IsException(proto))
goto fail;
if(!JS_IsObject(proto))
proto = textdecoder_proto;
/* using new_target to get the prototype is necessary when the class is extended. */
obj = JS_NewObjectProtoClass(ctx, proto, js_decoder_class_id);
JS_FreeValue(ctx, proto);
if(JS_IsException(obj))
goto fail;
ringbuffer_init(&dec->buffer, ctx);
if(argc >= 1) {
const char* s = JS_ToCString(ctx, argv[0]);
if(s[case_finds(s, "utf32")] || s[case_finds(s, "utf-32")])
dec->encoding = UTF32;
else if(s[case_finds(s, "utf16")] || s[case_finds(s, "utf-16")])
dec->encoding = UTF16;
else if(s[case_finds(s, "utf8")] || s[case_finds(s, "utf-8")])
dec->encoding = UTF8;
else {
return JS_ThrowInternalError(ctx, "%s: TextDecoder: '%s' is invalid s", __func__, s);
}
if(s[case_finds(s, "be")] || s[case_finds(s, "be")])
dec->endian = BIG;
JS_FreeCString(ctx, s);
} else {
dec->encoding = UTF8;
}
JS_SetOpaque(obj, dec);
return obj;
fail:
js_free(ctx, dec);
JS_FreeValue(ctx, obj);
return JS_EXCEPTION;
}
static JSValue
js_decoder_decode(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst argv[], int magic) {
TextDecoder* dec;
JSValue ret = JS_UNDEFINED;
if(!(dec = js_decoder_data(ctx, this_val)))
return JS_EXCEPTION;
switch(magic) {
case DECODER_END:
case DECODER_DECODE: {
InputBuffer in = js_input_chars(ctx, argv[0]);
// printf("js_decoder_decode (1) %s length=%zu in.size=%zu\n", magic == DECODER_DECODE ? "decode" :
// "end", ringbuffer_length(&dec->buffer), in.size);
if(ringbuffer_write(&dec->buffer, in.data, in.size) < 0)
return JS_ThrowInternalError(ctx, "%s: TextDecoder: ringbuffer %s failed", __func__, magic == DECODER_DECODE ? "decode" : "end");
if(ringbuffer_length(&dec->buffer) == 0)
ret = JS_NULL;
else
ret = textdecoder_decode(dec, ctx);
if(magic == DECODER_END)
ringbuffer_reset(&dec->buffer);
break;
}
}
return ret;
}
static JSValue
js_decoder_inspect(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst argv[]) {
TextDecoder* dec;
if(!(dec = js_decoder_data(ctx, this_val)))
return JS_EXCEPTION;
JSValue obj = JS_NewObjectClass(ctx, js_decoder_class_id);
JS_DefinePropertyValueStr(ctx, obj, "encoding", JS_NewString(ctx, textcode_encodings[dec->type_code]), JS_PROP_ENUMERABLE);
JS_DefinePropertyValueStr(ctx, obj, "buffered", JS_NewUint32(ctx, ringbuffer_length(&dec->buffer)), JS_PROP_ENUMERABLE);
return obj;
}
static void
js_decoder_finalizer(JSRuntime* rt, JSValue val) {
TextDecoder* dec = JS_GetOpaque(val, js_decoder_class_id);
if(dec) {
ringbuffer_free(&dec->buffer);
js_free_rt(rt, dec);
}
// JS_FreeValueRT(rt, val);
}
static JSClassDef js_decoder_class = {
.class_name = "TextDecoder",
.finalizer = js_decoder_finalizer,
};
static const JSCFunctionListEntry js_decoder_funcs[] = {
JS_CFUNC_MAGIC_DEF("decode", 1, js_decoder_decode, DECODER_DECODE),
JS_CFUNC_MAGIC_DEF("end", 1, js_decoder_decode, DECODER_END),
JS_CGETSET_ENUMERABLE_DEF("encoding", js_decoder_get, 0, DECODER_ENCODING),
JS_CGETSET_MAGIC_DEF("endian", js_decoder_get, 0, DECODER_ENDIANNESS),
JS_CGETSET_MAGIC_DEF("buffered", js_decoder_get, 0, DECODER_BUFFERED),
JS_PROP_STRING_DEF("[Symbol.toStringTag]", "TextDecoder", JS_PROP_CONFIGURABLE),
};
enum {
ENCODER_ENCODE,
ENCODER_END,
};
enum {
ENCODER_ENCODING,
ENCODER_ENDIANNESS,
ENCODER_BUFFERED,
};
/*static size_t
textencoder_try(const void* in, size_t len) {
const uint8_t *x, *y, *end;
size_t r = 0;
int ch;
for(x = in, end=x+len; x < end; ) {
y = x;
if((ch = unicode_from_utf8(x, end - x, &x)) == -1)
break;
r += x - y;
}
return r;
}
size_t
textencoder_length(TextEncoder* td) {
size_t r = 0;
r += textencoder_try(ringbuffer_begin(&td->buffer), ringbuffer_continuous(&td->buffer));
if(td->buffer.head < td->buffer.tail)
r += textencoder_try(td->buffer.data, ringbuffer_head(&td->buffer));
return r;
}*/
JSValue
textencoder_read(TextEncoder* te, JSContext* ctx) {
JSValue ret, buf;
int bits;
size_t len = ringbuffer_length(&te->buffer);
if(len > ringbuffer_continuous(&te->buffer))
ringbuffer_normalize(&te->buffer);
switch(te->encoding) {
case UTF8: bits = 8; break;
case UTF16: bits = 16; break;
case UTF32: bits = 32; break;
default: return JS_ThrowInternalError(ctx, "%s: TextEncoder: invalid encoding: %d", __func__, te->encoding);
}
buf = JS_NewArrayBufferCopy(ctx, ringbuffer_begin(&te->buffer), len);
ret = js_typedarray_new(ctx, bits, FALSE, FALSE, buf);
JS_FreeValue(ctx, buf);
te->buffer.tail += len;
return ret;
}
JSValue
textencoder_encode(TextEncoder* enc, InputBuffer in, JSContext* ctx) {
JSValue ret = JS_UNDEFINED;
size_t i;
uint32_t cp;
uint8_t u8[UTF8_CHAR_LEN_MAX];
const uint8_t *ptr, *end, *next;
switch(enc->encoding) {
case UTF8: {
if(ringbuffer_write(&enc->buffer, in.data, in.size) < 0)
return JS_ThrowInternalError(ctx, "%s: TextEncoder: ringbuffer write failed", __func__);
break;
}
case UTF16: {
ptr = block_begin(&in.block);
end = block_end(&in.block);
for(i = 0; ptr < end; ptr = next, i++) {
cp = unicode_from_utf8(ptr, end - ptr, &next);
{
uint_least16_t u16[2];
int len;
if(!libutf_c32_to_c16(cp, &len, u16))
return JS_ThrowInternalError(ctx,
"%s: TextEncoder: not a valid code point at (%llu) [%llu]: %lu",
__func__,
(long long unsigned int)i,
(long long unsigned int)(end - ptr),
(unsigned long)cp);
for(int j = 0; j < len; j++)
uint16_put_endian(u8 + j * 2, u16[j], enc->endian);
if(ringbuffer_append(&enc->buffer, u8, len * sizeof(uint16_t), ctx) < 0)
return JS_ThrowInternalError(ctx, "TextEncoder: ringbuffer write failed");
}
}
break;
}
case UTF32: {
ptr = block_begin(&in.block);
end = block_end(&in.block);
for(i = 0; ptr < end; ptr = next, i++) {
cp = unicode_from_utf8(ptr, end - ptr, &next);
if(cp == 0xffffffff)
return JS_ThrowInternalError(
ctx, "%s: TextEncoder: not a valid code point at (%llu): %lu", __func__, (long long unsigned int)(ptr - in.block.base), (long unsigned int)cp);
/*cp = 0;
if(!libutf_c8_to_c32(ptr, &cp))
return JS_ThrowInternalError(ctx, "No a valid code point at (%zu) [%zu]: %" PRIu32, i, end - ptr,
cp); next = ptr + libutf_c8_type(ptr); if(next == ptr) break;*/
uint32_put_endian(u8, cp, enc->endian);
if(ringbuffer_append(&enc->buffer, u8, sizeof(cp), ctx) < 0)
return JS_ThrowInternalError(ctx, "%s: TextEncoder: ringbuffer write failed", __func__);
}
break;
}
default: {
ret = JS_ThrowInternalError(ctx, "%s: TextEncoder: unknown encoding", __func__);
break;
}
}
return ret;
}
static JSValue
js_encoder_get(JSContext* ctx, JSValueConst this_val, int magic) {
TextEncoder* enc;
JSValue ret = JS_UNDEFINED;
if(!(enc = js_encoder_data(ctx, this_val)))
return ret;
switch(magic) {
case ENCODER_ENCODING: {
ret = JS_NewString(ctx, textcode_encodings[enc->type_code]);
break;
}
case ENCODER_ENDIANNESS: {
ret = JS_NewBool(ctx, enc->endian == BIG);
break;
}
case ENCODER_BUFFERED: {
ret = JS_NewUint32(ctx, ringbuffer_length(&enc->buffer));
break;
}
}
return ret;
}
static JSValue
js_encoder_constructor(JSContext* ctx, JSValueConst new_target, int argc, JSValueConst argv[]) {
JSValue obj = JS_UNDEFINED;
JSValue proto;
TextEncoder* enc;
if(!(enc = js_mallocz(ctx, sizeof(TextEncoder))))
return JS_EXCEPTION;
/* using new_target to get the prototype is necessary when the class is extended. */
proto = JS_GetPropertyStr(ctx, new_target, "prototype");
if(JS_IsException(proto))
goto fail;
if(!JS_IsObject(proto))
proto = textencoder_proto;
/* using new_target to get the prototype is necessary when the class is extended. */
obj = JS_NewObjectProtoClass(ctx, proto, js_encoder_class_id);
JS_FreeValue(ctx, proto);
if(JS_IsException(obj))
goto fail;
ringbuffer_init(&enc->buffer, ctx);
if(argc >= 1) {
const char* s = JS_ToCString(ctx, argv[0]);
if(s[case_finds(s, "utf32")] || s[case_finds(s, "utf-32")])
enc->encoding = UTF32;
else if(s[case_finds(s, "utf16")] || s[case_finds(s, "utf-16")])
enc->encoding = UTF16;
else if(s[case_finds(s, "utf8")] || s[case_finds(s, "utf-8")])
enc->encoding = UTF8;
else {
return JS_ThrowInternalError(ctx, "TextEncoder '%s' is invalid s", s);
}
if(enc->encoding > UTF8)
if(s[case_finds(s, "be")] || s[case_finds(s, "be")])
enc->endian = BIG;
JS_FreeCString(ctx, s);
} else {
enc->encoding = UTF8;
}
JS_SetOpaque(obj, enc);
return obj;
fail:
js_free(ctx, enc);
JS_FreeValue(ctx, obj);
return JS_EXCEPTION;
}
static JSValue
js_encoder_encode(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst argv[], int magic) {
TextEncoder* enc;
JSValue ret = JS_UNDEFINED;
if(!(enc = js_encoder_data(ctx, this_val)))
return JS_EXCEPTION;
switch(magic) {
case ENCODER_END:
case ENCODER_ENCODE: {
InputBuffer in = js_input_chars(ctx, argv[0]);
ret = textencoder_encode(enc, in, ctx);
if(JS_IsException(ret))
break;
if(ringbuffer_length(&enc->buffer) == 0)
ret = JS_NULL;
else
ret = textencoder_read(enc, ctx);
if(magic == ENCODER_END)
ringbuffer_reset(&enc->buffer);
break;
}
}
return ret;
}
static JSValue
js_encoder_inspect(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst argv[]) {
TextEncoder* enc;
if(!(enc = js_encoder_data(ctx, this_val)))
return JS_EXCEPTION;
JSValue obj = JS_NewObjectClass(ctx, js_encoder_class_id);
JS_DefinePropertyValueStr(ctx, obj, "encoding", JS_NewString(ctx, textcode_encodings[enc->type_code]), JS_PROP_ENUMERABLE);
JS_DefinePropertyValueStr(ctx, obj, "buffered", JS_NewUint32(ctx, ringbuffer_length(&enc->buffer)), JS_PROP_ENUMERABLE);
return obj;
}
static void
js_encoder_finalizer(JSRuntime* rt, JSValue val) {
TextEncoder* enc = JS_GetOpaque(val, js_encoder_class_id);
if(enc) {
ringbuffer_free(&enc->buffer);
js_free_rt(rt, enc);
}
// JS_FreeValueRT(rt, val);
}
static JSClassDef js_encoder_class = {
.class_name = "TextEncoder",
.finalizer = js_encoder_finalizer,
};
static const JSCFunctionListEntry js_encoder_funcs[] = {
JS_CFUNC_MAGIC_DEF("encode", 1, js_encoder_encode, ENCODER_ENCODE),
JS_CFUNC_MAGIC_DEF("end", 1, js_encoder_encode, ENCODER_END),
JS_CGETSET_ENUMERABLE_DEF("encoding", js_encoder_get, 0, ENCODER_ENCODING),
JS_CGETSET_MAGIC_DEF("endian", js_encoder_get, 0, ENCODER_ENDIANNESS),
JS_CGETSET_MAGIC_DEF("buffered", js_encoder_get, 0, ENCODER_BUFFERED),
JS_PROP_STRING_DEF("[Symbol.toStringTag]", "TextEncoder", JS_PROP_CONFIGURABLE),
};
int
js_code_init(JSContext* ctx, JSModuleDef* m) {
if(js_decoder_class_id == 0) {
JS_NewClassID(&js_decoder_class_id);
JS_NewClass(JS_GetRuntime(ctx), js_decoder_class_id, &js_decoder_class);
textdecoder_ctor = JS_NewCFunction2(ctx, js_decoder_constructor, "TextDecoder", 1, JS_CFUNC_constructor, 0);
textdecoder_proto = JS_NewObject(ctx);
JS_SetPropertyFunctionList(ctx, textdecoder_proto, js_decoder_funcs, countof(js_decoder_funcs));
JS_SetClassProto(ctx, js_decoder_class_id, textdecoder_proto);
JS_SetConstructor(ctx, textdecoder_ctor, textdecoder_proto);
JS_NewClassID(&js_encoder_class_id);
JS_NewClass(JS_GetRuntime(ctx), js_encoder_class_id, &js_encoder_class);
textencoder_ctor = JS_NewCFunction2(ctx, js_encoder_constructor, "TextEncoder", 1, JS_CFUNC_constructor, 0);
textencoder_proto = JS_NewObject(ctx);
JS_SetPropertyFunctionList(ctx, textencoder_proto, js_encoder_funcs, countof(js_encoder_funcs));
JS_SetClassProto(ctx, js_encoder_class_id, textencoder_proto);
JS_SetConstructor(ctx, textencoder_ctor, textencoder_proto);
// js_set_inspect_method(ctx, textdecoder_proto,
// js_decoder_inspect);
}
if(m) {
JS_SetModuleExport(ctx, m, "TextDecoder", textdecoder_ctor);
JS_SetModuleExport(ctx, m, "TextEncoder", textencoder_ctor);
/* const char* module_name = JS_AtomToCString(ctx, m->module_name);
if(!strcmp(module_name, "textdecoder"))
JS_SetModuleExport(ctx, m, "default", textdecoder_ctor);
JS_FreeCString(ctx, module_name);*/
}
return 0;
}
#ifdef JS_TEXTCODE_MODULE
#define JS_INIT_MODULE js_init_module
#else
#define JS_INIT_MODULE js_init_module_textdecoder
#endif
VISIBLE JSModuleDef*
JS_INIT_MODULE(JSContext* ctx, const char* module_name) {
JSModuleDef* m;
if((m = JS_NewCModule(ctx, module_name, js_code_init))) {
JS_AddModuleExport(ctx, m, "TextDecoder");
JS_AddModuleExport(ctx, m, "TextEncoder");
/*if(!strcmp(module_name, "textdecoder"))
JS_AddModuleExport(ctx, m, "default");*/
}
return m;
}
/**
* @}
*/