Remove trailing whitespace (#46)

Not purely cosmetic because it breaks navigation with { and } in the
One True Editor.
This commit is contained in:
Ben Noordhuis 2023-11-12 10:01:40 +01:00 committed by GitHub
parent 15f798db27
commit 162a8b7409
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
33 changed files with 967 additions and 967 deletions

View file

@ -1,6 +1,6 @@
/* /*
* C utilities * C utilities
* *
* Copyright (c) 2017 Fabrice Bellard * Copyright (c) 2017 Fabrice Bellard
* Copyright (c) 2018 Charlie Gordon * Copyright (c) 2018 Charlie Gordon
* *
@ -174,7 +174,7 @@ int __attribute__((format(printf, 2, 3))) dbuf_printf(DynBuf *s,
va_list ap; va_list ap;
char buf[128]; char buf[128];
int len; int len;
va_start(ap, fmt); va_start(ap, fmt);
len = vsnprintf(buf, sizeof(buf), fmt, ap); len = vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap); va_end(ap);

View file

@ -1,6 +1,6 @@
/* /*
* C utilities * C utilities
* *
* Copyright (c) 2017 Fabrice Bellard * Copyright (c) 2017 Fabrice Bellard
* Copyright (c) 2018 Charlie Gordon * Copyright (c) 2018 Charlie Gordon
* *
@ -220,13 +220,13 @@ static inline uint32_t bswap32(uint32_t v)
static inline uint64_t bswap64(uint64_t v) static inline uint64_t bswap64(uint64_t v)
{ {
return ((v & ((uint64_t)0xff << (7 * 8))) >> (7 * 8)) | return ((v & ((uint64_t)0xff << (7 * 8))) >> (7 * 8)) |
((v & ((uint64_t)0xff << (6 * 8))) >> (5 * 8)) | ((v & ((uint64_t)0xff << (6 * 8))) >> (5 * 8)) |
((v & ((uint64_t)0xff << (5 * 8))) >> (3 * 8)) | ((v & ((uint64_t)0xff << (5 * 8))) >> (3 * 8)) |
((v & ((uint64_t)0xff << (4 * 8))) >> (1 * 8)) | ((v & ((uint64_t)0xff << (4 * 8))) >> (1 * 8)) |
((v & ((uint64_t)0xff << (3 * 8))) << (1 * 8)) | ((v & ((uint64_t)0xff << (3 * 8))) << (1 * 8)) |
((v & ((uint64_t)0xff << (2 * 8))) << (3 * 8)) | ((v & ((uint64_t)0xff << (2 * 8))) << (3 * 8)) |
((v & ((uint64_t)0xff << (1 * 8))) << (5 * 8)) | ((v & ((uint64_t)0xff << (1 * 8))) << (5 * 8)) |
((v & ((uint64_t)0xff << (0 * 8))) << (7 * 8)); ((v & ((uint64_t)0xff << (0 * 8))) << (7 * 8));
} }

View file

@ -1,6 +1,6 @@
/* /*
* QuickJS: Example of C module * QuickJS: Example of C module
* *
* Copyright (c) 2017-2018 Fabrice Bellard * Copyright (c) 2017-2018 Fabrice Bellard
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy

View file

@ -54,7 +54,7 @@ function calc_pi(prec) {
const CHUD_C = 640320n; const CHUD_C = 640320n;
const CHUD_C3 = 10939058860032000n; /* C^3/24 */ const CHUD_C3 = 10939058860032000n; /* C^3/24 */
const CHUD_BITS_PER_TERM = 47.11041313821584202247; /* log2(C/12)*3 */ const CHUD_BITS_PER_TERM = 47.11041313821584202247; /* log2(C/12)*3 */
/* return [P, Q, G] */ /* return [P, Q, G] */
function chud_bs(a, b, need_G) { function chud_bs(a, b, need_G) {
var c, P, Q, G, P1, Q1, G1, P2, Q2, G2; var c, P, Q, G, P1, Q1, G1, P2, Q2, G2;

View file

@ -1,6 +1,6 @@
/* /*
* QuickJS: Example of C module with a class * QuickJS: Example of C module with a class
* *
* Copyright (c) 2019 Fabrice Bellard * Copyright (c) 2019 Fabrice Bellard
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
@ -49,7 +49,7 @@ static JSValue js_point_ctor(JSContext *ctx,
JSPointData *s; JSPointData *s;
JSValue obj = JS_UNDEFINED; JSValue obj = JS_UNDEFINED;
JSValue proto; JSValue proto;
s = js_mallocz(ctx, sizeof(*s)); s = js_mallocz(ctx, sizeof(*s));
if (!s) if (!s)
return JS_EXCEPTION; return JS_EXCEPTION;
@ -112,7 +112,7 @@ static JSValue js_point_norm(JSContext *ctx, JSValueConst this_val,
static JSClassDef js_point_class = { static JSClassDef js_point_class = {
"Point", "Point",
.finalizer = js_point_finalizer, .finalizer = js_point_finalizer,
}; };
static const JSCFunctionListEntry js_point_proto_funcs[] = { static const JSCFunctionListEntry js_point_proto_funcs[] = {
JS_CGETSET_MAGIC_DEF("x", js_point_get_xy, js_point_set_xy, 0), JS_CGETSET_MAGIC_DEF("x", js_point_get_xy, js_point_set_xy, 0),
@ -123,19 +123,19 @@ static const JSCFunctionListEntry js_point_proto_funcs[] = {
static int js_point_init(JSContext *ctx, JSModuleDef *m) static int js_point_init(JSContext *ctx, JSModuleDef *m)
{ {
JSValue point_proto, point_class; JSValue point_proto, point_class;
/* create the Point class */ /* create the Point class */
JS_NewClassID(&js_point_class_id); JS_NewClassID(&js_point_class_id);
JS_NewClass(JS_GetRuntime(ctx), js_point_class_id, &js_point_class); JS_NewClass(JS_GetRuntime(ctx), js_point_class_id, &js_point_class);
point_proto = JS_NewObject(ctx); point_proto = JS_NewObject(ctx);
JS_SetPropertyFunctionList(ctx, point_proto, js_point_proto_funcs, countof(js_point_proto_funcs)); JS_SetPropertyFunctionList(ctx, point_proto, js_point_proto_funcs, countof(js_point_proto_funcs));
point_class = JS_NewCFunction2(ctx, js_point_ctor, "Point", 2, JS_CFUNC_constructor, 0); point_class = JS_NewCFunction2(ctx, js_point_ctor, "Point", 2, JS_CFUNC_constructor, 0);
/* set proto.constructor and ctor.prototype */ /* set proto.constructor and ctor.prototype */
JS_SetConstructor(ctx, point_class, point_proto); JS_SetConstructor(ctx, point_class, point_proto);
JS_SetClassProto(ctx, js_point_class_id, point_proto); JS_SetClassProto(ctx, js_point_class_id, point_proto);
JS_SetModuleExport(ctx, m, "Point", point_class); JS_SetModuleExport(ctx, m, "Point", point_class);
return 0; return 0;
} }

534
libbf.c

File diff suppressed because it is too large Load diff

20
libbf.h
View file

@ -1,6 +1,6 @@
/* /*
* Tiny arbitrary precision floating point library * Tiny arbitrary precision floating point library
* *
* Copyright (c) 2017-2021 Fabrice Bellard * Copyright (c) 2017-2021 Fabrice Bellard
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
@ -171,7 +171,7 @@ static inline bf_flags_t bf_set_exp_bits(int n)
#define BF_ST_UNDERFLOW (1 << 3) #define BF_ST_UNDERFLOW (1 << 3)
#define BF_ST_INEXACT (1 << 4) #define BF_ST_INEXACT (1 << 4)
/* indicate that a memory allocation error occured. NaN is returned */ /* indicate that a memory allocation error occured. NaN is returned */
#define BF_ST_MEM_ERROR (1 << 5) #define BF_ST_MEM_ERROR (1 << 5)
#define BF_RADIX_MAX 36 /* maximum radix for bf_atof() and bf_ftoa() */ #define BF_RADIX_MAX 36 /* maximum radix for bf_atof() and bf_ftoa() */
@ -284,7 +284,7 @@ int bf_sub(bf_t *r, const bf_t *a, const bf_t *b, limb_t prec, bf_flags_t flags)
int bf_add_si(bf_t *r, const bf_t *a, int64_t b1, limb_t prec, bf_flags_t flags); int bf_add_si(bf_t *r, const bf_t *a, int64_t b1, limb_t prec, bf_flags_t flags);
int bf_mul(bf_t *r, const bf_t *a, const bf_t *b, limb_t prec, bf_flags_t flags); int bf_mul(bf_t *r, const bf_t *a, const bf_t *b, limb_t prec, bf_flags_t flags);
int bf_mul_ui(bf_t *r, const bf_t *a, uint64_t b1, limb_t prec, bf_flags_t flags); int bf_mul_ui(bf_t *r, const bf_t *a, uint64_t b1, limb_t prec, bf_flags_t flags);
int bf_mul_si(bf_t *r, const bf_t *a, int64_t b1, limb_t prec, int bf_mul_si(bf_t *r, const bf_t *a, int64_t b1, limb_t prec,
bf_flags_t flags); bf_flags_t flags);
int bf_mul_2exp(bf_t *r, slimb_t e, limb_t prec, bf_flags_t flags); int bf_mul_2exp(bf_t *r, slimb_t e, limb_t prec, bf_flags_t flags);
int bf_div(bf_t *r, const bf_t *a, const bf_t *b, limb_t prec, bf_flags_t flags); int bf_div(bf_t *r, const bf_t *a, const bf_t *b, limb_t prec, bf_flags_t flags);
@ -341,12 +341,12 @@ int bf_mul_pow_radix(bf_t *r, const bf_t *T, limb_t radix,
/* fractional format: prec digits after the decimal point rounded with /* fractional format: prec digits after the decimal point rounded with
(flags & BF_RND_MASK) */ (flags & BF_RND_MASK) */
#define BF_FTOA_FORMAT_FRAC (1 << 16) #define BF_FTOA_FORMAT_FRAC (1 << 16)
/* free format: /* free format:
For binary radices with bf_ftoa() and for bfdec_ftoa(): use the minimum For binary radices with bf_ftoa() and for bfdec_ftoa(): use the minimum
number of digits to represent 'a'. The precision and the rounding number of digits to represent 'a'. The precision and the rounding
mode are ignored. mode are ignored.
For the non binary radices with bf_ftoa(): use as many digits as For the non binary radices with bf_ftoa(): use as many digits as
necessary so that bf_atof() return the same number when using necessary so that bf_atof() return the same number when using
precision 'prec', rounding to nearest and the subnormal precision 'prec', rounding to nearest and the subnormal
@ -373,7 +373,7 @@ char *bf_ftoa(size_t *plen, const bf_t *a, int radix, limb_t prec,
bf_flags_t flags); bf_flags_t flags);
/* modulo 2^n instead of saturation. NaN and infinity return 0 */ /* modulo 2^n instead of saturation. NaN and infinity return 0 */
#define BF_GET_INT_MOD (1 << 0) #define BF_GET_INT_MOD (1 << 0)
int bf_get_int32(int *pres, const bf_t *a, int flags); int bf_get_int32(int *pres, const bf_t *a, int flags);
int bf_get_int64(int64_t *pres, const bf_t *a, int flags); int bf_get_int64(int64_t *pres, const bf_t *a, int flags);
int bf_get_uint64(uint64_t *pres, const bf_t *a); int bf_get_uint64(uint64_t *pres, const bf_t *a);
@ -387,10 +387,10 @@ int bf_normalize_and_round(bf_t *r, limb_t prec1, bf_flags_t flags);
int bf_can_round(const bf_t *a, slimb_t prec, bf_rnd_t rnd_mode, slimb_t k); int bf_can_round(const bf_t *a, slimb_t prec, bf_rnd_t rnd_mode, slimb_t k);
slimb_t bf_mul_log2_radix(slimb_t a1, unsigned int radix, int is_inv, slimb_t bf_mul_log2_radix(slimb_t a1, unsigned int radix, int is_inv,
int is_ceil1); int is_ceil1);
int mp_mul(bf_context_t *s, limb_t *result, int mp_mul(bf_context_t *s, limb_t *result,
const limb_t *op1, limb_t op1_size, const limb_t *op1, limb_t op1_size,
const limb_t *op2, limb_t op2_size); const limb_t *op2, limb_t op2_size);
limb_t mp_add(limb_t *res, const limb_t *op1, const limb_t *op2, limb_t mp_add(limb_t *res, const limb_t *op1, const limb_t *op2,
limb_t n, limb_t carry); limb_t n, limb_t carry);
limb_t mp_add_ui(limb_t *tab, limb_t b, size_t n); limb_t mp_add_ui(limb_t *tab, limb_t b, size_t n);
int mp_sqrtrem(bf_context_t *s, limb_t *tabs, limb_t *taba, limb_t n); int mp_sqrtrem(bf_context_t *s, limb_t *tabs, limb_t *taba, limb_t n);

View file

@ -1,6 +1,6 @@
/* /*
* Regular Expression Engine * Regular Expression Engine
* *
* Copyright (c) 2017-2018 Fabrice Bellard * Copyright (c) 2017-2018 Fabrice Bellard
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy

View file

@ -1,6 +1,6 @@
/* /*
* Regular Expression Engine * Regular Expression Engine
* *
* Copyright (c) 2017-2018 Fabrice Bellard * Copyright (c) 2017-2018 Fabrice Bellard
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
@ -226,7 +226,7 @@ static int cr_init_char_range(REParseState *s, CharRange *cr, uint32_t c)
BOOL invert; BOOL invert;
const uint16_t *c_pt; const uint16_t *c_pt;
int len, i; int len, i;
invert = c & 1; invert = c & 1;
c_pt = char_range_table[c >> 1]; c_pt = char_range_table[c >> 1];
len = *c_pt++; len = *c_pt++;
@ -276,7 +276,7 @@ static __maybe_unused void lre_dump_bytecode(const uint8_t *buf,
{ {
int pos, len, opcode, bc_len, re_flags, i; int pos, len, opcode, bc_len, re_flags, i;
uint32_t val; uint32_t val;
assert(buf_len >= RE_HEADER_LEN); assert(buf_len >= RE_HEADER_LEN);
re_flags= buf[0]; re_flags= buf[0];
@ -448,7 +448,7 @@ static int parse_digits(const uint8_t **pp, BOOL allow_overflow)
const uint8_t *p; const uint8_t *p;
uint64_t v; uint64_t v;
int c; int c;
p = *pp; p = *pp;
v = 0; v = 0;
for(;;) { for(;;) {
@ -520,7 +520,7 @@ int lre_parse_escape(const uint8_t **pp, int allow_utf16)
{ {
int h, n, i; int h, n, i;
uint32_t c1; uint32_t c1;
if (*p == '{' && allow_utf16) { if (*p == '{' && allow_utf16) {
p++; p++;
c = 0; c = 0;
@ -714,7 +714,7 @@ static int get_class_atom(REParseState *s, CharRange *cr,
const uint8_t *p; const uint8_t *p;
uint32_t c; uint32_t c;
int ret; int ret;
p = *pp; p = *pp;
c = *p; c = *p;
@ -822,7 +822,7 @@ static int re_emit_range(REParseState *s, const CharRange *cr)
{ {
int len, i; int len, i;
uint32_t high; uint32_t high;
len = (unsigned)cr->len / 2; len = (unsigned)cr->len / 2;
if (len >= 65535) if (len >= 65535)
return re_parse_error(s, "too many ranges"); return re_parse_error(s, "too many ranges");
@ -863,7 +863,7 @@ static int re_parse_char_class(REParseState *s, const uint8_t **pp)
CharRange cr_s, *cr = &cr_s; CharRange cr_s, *cr = &cr_s;
CharRange cr1_s, *cr1 = &cr1_s; CharRange cr1_s, *cr1 = &cr1_s;
BOOL invert; BOOL invert;
cr_init(cr, s->opaque, lre_realloc); cr_init(cr, s->opaque, lre_realloc);
p = *pp; p = *pp;
p++; /* skip '[' */ p++; /* skip '[' */
@ -953,12 +953,12 @@ static int re_check_advance(const uint8_t *bc_buf, int bc_buf_len)
uint32_t val, last; uint32_t val, last;
BOOL has_back_reference; BOOL has_back_reference;
uint8_t capture_bitmap[CAPTURE_COUNT_MAX]; uint8_t capture_bitmap[CAPTURE_COUNT_MAX];
ret = -2; /* not known yet */ ret = -2; /* not known yet */
pos = 0; pos = 0;
has_back_reference = FALSE; has_back_reference = FALSE;
memset(capture_bitmap, 0, sizeof(capture_bitmap)); memset(capture_bitmap, 0, sizeof(capture_bitmap));
while (pos < bc_buf_len) { while (pos < bc_buf_len) {
opcode = bc_buf[pos]; opcode = bc_buf[pos];
len = reopcode_info[opcode].size; len = reopcode_info[opcode].size;
@ -1035,7 +1035,7 @@ static int re_is_simple_quantifier(const uint8_t *bc_buf, int bc_buf_len)
{ {
int pos, opcode, len, count; int pos, opcode, len, count;
uint32_t val; uint32_t val;
count = 0; count = 0;
pos = 0; pos = 0;
while (pos < bc_buf_len) { while (pos < bc_buf_len) {
@ -1195,7 +1195,7 @@ static int find_group_name(REParseState *s, const char *name)
const char *p, *buf_end; const char *p, *buf_end;
size_t len, name_len; size_t len, name_len;
int capture_index; int capture_index;
name_len = strlen(name); name_len = strlen(name);
p = (char *)s->group_names.buf; p = (char *)s->group_names.buf;
buf_end = (char *)s->group_names.buf + s->group_names.size; buf_end = (char *)s->group_names.buf + s->group_names.size;
@ -1218,7 +1218,7 @@ static int re_parse_term(REParseState *s, BOOL is_backward_dir)
int c, last_atom_start, quant_min, quant_max, last_capture_count; int c, last_atom_start, quant_min, quant_max, last_capture_count;
BOOL greedy, add_zero_advance_check, is_neg, is_backward_lookahead; BOOL greedy, add_zero_advance_check, is_neg, is_backward_lookahead;
CharRange cr_s, *cr = &cr_s; CharRange cr_s, *cr = &cr_s;
last_atom_start = -1; last_atom_start = -1;
last_capture_count = 0; last_capture_count = 0;
p = s->buf_ptr; p = s->buf_ptr;
@ -1341,15 +1341,15 @@ static int re_parse_term(REParseState *s, BOOL is_backward_dir)
capture_index = s->capture_count++; capture_index = s->capture_count++;
re_emit_op_u8(s, REOP_save_start + is_backward_dir, re_emit_op_u8(s, REOP_save_start + is_backward_dir,
capture_index); capture_index);
s->buf_ptr = p; s->buf_ptr = p;
if (re_parse_disjunction(s, is_backward_dir)) if (re_parse_disjunction(s, is_backward_dir))
return -1; return -1;
p = s->buf_ptr; p = s->buf_ptr;
re_emit_op_u8(s, REOP_save_start + 1 - is_backward_dir, re_emit_op_u8(s, REOP_save_start + 1 - is_backward_dir,
capture_index); capture_index);
if (re_parse_expect(s, &p, ')')) if (re_parse_expect(s, &p, ')'))
return -1; return -1;
} }
@ -1365,7 +1365,7 @@ static int re_parse_term(REParseState *s, BOOL is_backward_dir)
{ {
const uint8_t *p1; const uint8_t *p1;
int dummy_res; int dummy_res;
p1 = p; p1 = p;
if (p1[2] != '<') { if (p1[2] != '<') {
/* annex B: we tolerate invalid group names in non /* annex B: we tolerate invalid group names in non
@ -1418,10 +1418,10 @@ static int re_parse_term(REParseState *s, BOOL is_backward_dir)
goto normal_char; goto normal_char;
case '1': case '2': case '3': case '4': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '5': case '6': case '7': case '8':
case '9': case '9':
{ {
const uint8_t *q = ++p; const uint8_t *q = ++p;
c = parse_digits(&p, FALSE); c = parse_digits(&p, FALSE);
if (c < 0 || (c >= s->capture_count && c >= re_count_captures(s))) { if (c < 0 || (c >= s->capture_count && c >= re_count_captures(s))) {
if (!s->is_utf16) { if (!s->is_utf16) {
@ -1562,7 +1562,7 @@ static int re_parse_term(REParseState *s, BOOL is_backward_dir)
} }
if (greedy) { if (greedy) {
int len, pos; int len, pos;
if (quant_max > 0) { if (quant_max > 0) {
/* specific optimization for simple quantifiers */ /* specific optimization for simple quantifiers */
if (dbuf_error(&s->byte_code)) if (dbuf_error(&s->byte_code))
@ -1571,7 +1571,7 @@ static int re_parse_term(REParseState *s, BOOL is_backward_dir)
s->byte_code.size - last_atom_start); s->byte_code.size - last_atom_start);
if (len > 0) { if (len > 0) {
re_emit_op(s, REOP_match); re_emit_op(s, REOP_match);
if (dbuf_insert(&s->byte_code, last_atom_start, 17)) if (dbuf_insert(&s->byte_code, last_atom_start, 17))
goto out_of_memory; goto out_of_memory;
pos = last_atom_start; pos = last_atom_start;
@ -1588,7 +1588,7 @@ static int re_parse_term(REParseState *s, BOOL is_backward_dir)
goto done; goto done;
} }
} }
if (dbuf_error(&s->byte_code)) if (dbuf_error(&s->byte_code))
goto out_of_memory; goto out_of_memory;
add_zero_advance_check = (re_check_advance(s->byte_code.buf + last_atom_start, add_zero_advance_check = (re_check_advance(s->byte_code.buf + last_atom_start,
@ -1596,7 +1596,7 @@ static int re_parse_term(REParseState *s, BOOL is_backward_dir)
} else { } else {
add_zero_advance_check = FALSE; add_zero_advance_check = FALSE;
} }
{ {
int len, pos; int len, pos;
len = s->byte_code.size - last_atom_start; len = s->byte_code.size - last_atom_start;
@ -1631,7 +1631,7 @@ static int re_parse_term(REParseState *s, BOOL is_backward_dir)
atom (only works if the atom has no atom (only works if the atom has no
side effect) */ side effect) */
s->byte_code.buf[last_atom_start + 1 + 4] = REOP_push_char_pos; s->byte_code.buf[last_atom_start + 1 + 4] = REOP_push_char_pos;
re_emit_goto(s, REOP_bne_char_pos, last_atom_start); re_emit_goto(s, REOP_bne_char_pos, last_atom_start);
} else { } else {
re_emit_goto(s, REOP_goto, last_atom_start); re_emit_goto(s, REOP_goto, last_atom_start);
} }
@ -1682,7 +1682,7 @@ static int re_parse_term(REParseState *s, BOOL is_backward_dir)
re_emit_op_u32(s, REOP_split_goto_first + greedy, len + 5); re_emit_op_u32(s, REOP_split_goto_first + greedy, len + 5);
/* copy the atom */ /* copy the atom */
dbuf_put_self(&s->byte_code, last_atom_start, len); dbuf_put_self(&s->byte_code, last_atom_start, len);
re_emit_goto(s, REOP_loop, pos); re_emit_goto(s, REOP_loop, pos);
re_emit_op(s, REOP_drop); re_emit_op(s, REOP_drop);
} }
@ -1734,14 +1734,14 @@ static int re_parse_alternative(REParseState *s, BOOL is_backward_dir)
} }
return 0; return 0;
} }
static int re_parse_disjunction(REParseState *s, BOOL is_backward_dir) static int re_parse_disjunction(REParseState *s, BOOL is_backward_dir)
{ {
int start, len, pos; int start, len, pos;
if (lre_check_stack_overflow(s->opaque, 0)) if (lre_check_stack_overflow(s->opaque, 0))
return re_parse_error(s, "stack overflow"); return re_parse_error(s, "stack overflow");
start = s->byte_code.size; start = s->byte_code.size;
if (re_parse_alternative(s, is_backward_dir)) if (re_parse_alternative(s, is_backward_dir))
return -1; return -1;
@ -1761,7 +1761,7 @@ static int re_parse_disjunction(REParseState *s, BOOL is_backward_dir)
if (re_parse_alternative(s, is_backward_dir)) if (re_parse_alternative(s, is_backward_dir))
return -1; return -1;
/* patch the goto */ /* patch the goto */
len = s->byte_code.size - (pos + 4); len = s->byte_code.size - (pos + 4);
put_u32(s->byte_code.buf + pos, len); put_u32(s->byte_code.buf + pos, len);
@ -1774,7 +1774,7 @@ static int compute_stack_size(const uint8_t *bc_buf, int bc_buf_len)
{ {
int stack_size, stack_size_max, pos, opcode, len; int stack_size, stack_size_max, pos, opcode, len;
uint32_t val; uint32_t val;
stack_size = 0; stack_size = 0;
stack_size_max = 0; stack_size_max = 0;
bc_buf += RE_HEADER_LEN; bc_buf += RE_HEADER_LEN;
@ -1825,7 +1825,7 @@ uint8_t *lre_compile(int *plen, char *error_msg, int error_msg_size,
REParseState s_s, *s = &s_s; REParseState s_s, *s = &s_s;
int stack_size; int stack_size;
BOOL is_sticky; BOOL is_sticky;
memset(s, 0, sizeof(*s)); memset(s, 0, sizeof(*s));
s->opaque = opaque; s->opaque = opaque;
s->buf_ptr = (const uint8_t *)buf; s->buf_ptr = (const uint8_t *)buf;
@ -1839,7 +1839,7 @@ uint8_t *lre_compile(int *plen, char *error_msg, int error_msg_size,
s->capture_count = 1; s->capture_count = 1;
s->total_capture_count = -1; s->total_capture_count = -1;
s->has_named_captures = -1; s->has_named_captures = -1;
dbuf_init2(&s->byte_code, opaque, lre_realloc); dbuf_init2(&s->byte_code, opaque, lre_realloc);
dbuf_init2(&s->group_names, opaque, lre_realloc); dbuf_init2(&s->group_names, opaque, lre_realloc);
@ -1847,7 +1847,7 @@ uint8_t *lre_compile(int *plen, char *error_msg, int error_msg_size,
dbuf_putc(&s->byte_code, 0); /* second element is the number of captures */ dbuf_putc(&s->byte_code, 0); /* second element is the number of captures */
dbuf_putc(&s->byte_code, 0); /* stack size */ dbuf_putc(&s->byte_code, 0); /* stack size */
dbuf_put_u32(&s->byte_code, 0); /* bytecode length */ dbuf_put_u32(&s->byte_code, 0); /* bytecode length */
if (!is_sticky) { if (!is_sticky) {
/* iterate thru all positions (about the same as .*?( ... ) ) /* iterate thru all positions (about the same as .*?( ... ) )
. We do it without an explicit loop so that lock step . We do it without an explicit loop so that lock step
@ -1869,7 +1869,7 @@ uint8_t *lre_compile(int *plen, char *error_msg, int error_msg_size,
} }
re_emit_op_u8(s, REOP_save_end, 0); re_emit_op_u8(s, REOP_save_end, 0);
re_emit_op(s, REOP_match); re_emit_op(s, REOP_match);
if (*s->buf_ptr != '\0') { if (*s->buf_ptr != '\0') {
@ -1881,13 +1881,13 @@ uint8_t *lre_compile(int *plen, char *error_msg, int error_msg_size,
re_parse_out_of_memory(s); re_parse_out_of_memory(s);
goto error; goto error;
} }
stack_size = compute_stack_size(s->byte_code.buf, s->byte_code.size); stack_size = compute_stack_size(s->byte_code.buf, s->byte_code.size);
if (stack_size < 0) { if (stack_size < 0) {
re_parse_error(s, "too many imbricated quantifiers"); re_parse_error(s, "too many imbricated quantifiers");
goto error; goto error;
} }
s->byte_code.buf[RE_HEADER_CAPTURE_COUNT] = s->capture_count; s->byte_code.buf[RE_HEADER_CAPTURE_COUNT] = s->capture_count;
s->byte_code.buf[RE_HEADER_STACK_SIZE] = stack_size; s->byte_code.buf[RE_HEADER_STACK_SIZE] = stack_size;
put_u32(s->byte_code.buf + 3, s->byte_code.size - RE_HEADER_LEN); put_u32(s->byte_code.buf + 3, s->byte_code.size - RE_HEADER_LEN);
@ -1898,11 +1898,11 @@ uint8_t *lre_compile(int *plen, char *error_msg, int error_msg_size,
s->byte_code.buf[RE_HEADER_FLAGS] |= LRE_FLAG_NAMED_GROUPS; s->byte_code.buf[RE_HEADER_FLAGS] |= LRE_FLAG_NAMED_GROUPS;
} }
dbuf_free(&s->group_names); dbuf_free(&s->group_names);
#ifdef DUMP_REOP #ifdef DUMP_REOP
lre_dump_bytecode(s->byte_code.buf, s->byte_code.size); lre_dump_bytecode(s->byte_code.buf, s->byte_code.size);
#endif #endif
error_msg[0] = '\0'; error_msg[0] = '\0';
*plen = s->byte_code.size; *plen = s->byte_code.size;
return s->byte_code.buf; return s->byte_code.buf;
@ -2033,7 +2033,7 @@ typedef struct {
const uint8_t *cbuf; const uint8_t *cbuf;
const uint8_t *cbuf_end; const uint8_t *cbuf_end;
/* 0 = 8 bit chars, 1 = 16 bit chars, 2 = 16 bit chars, UTF-16 */ /* 0 = 8 bit chars, 1 = 16 bit chars, 2 = 16 bit chars, UTF-16 */
int cbuf_type; int cbuf_type;
int capture_count; int capture_count;
int stack_size_max; int stack_size_max;
BOOL multi_line; BOOL multi_line;
@ -2095,7 +2095,7 @@ static intptr_t lre_exec_backtrack(REExecContext *s, uint8_t **capture,
int cbuf_type; int cbuf_type;
uint32_t val, c; uint32_t val, c;
const uint8_t *cbuf_end; const uint8_t *cbuf_end;
cbuf_type = s->cbuf_type; cbuf_type = s->cbuf_type;
cbuf_end = s->cbuf_end; cbuf_end = s->cbuf_end;
@ -2193,7 +2193,7 @@ static intptr_t lre_exec_backtrack(REExecContext *s, uint8_t **capture,
case REOP_split_next_first: case REOP_split_next_first:
{ {
const uint8_t *pc1; const uint8_t *pc1;
val = get_u32(pc); val = get_u32(pc);
pc += 4; pc += 4;
if (opcode == REOP_split_next_first) { if (opcode == REOP_split_next_first) {
@ -2219,7 +2219,7 @@ static intptr_t lre_exec_backtrack(REExecContext *s, uint8_t **capture,
if (ret < 0) if (ret < 0)
return -1; return -1;
break; break;
case REOP_goto: case REOP_goto:
val = get_u32(pc); val = get_u32(pc);
pc += 4 + (int)val; pc += 4 + (int)val;
@ -2325,7 +2325,7 @@ static intptr_t lre_exec_backtrack(REExecContext *s, uint8_t **capture,
{ {
const uint8_t *cptr1, *cptr1_end, *cptr1_start; const uint8_t *cptr1, *cptr1_end, *cptr1_start;
uint32_t c1, c2; uint32_t c1, c2;
val = *pc++; val = *pc++;
if (val >= s->capture_count) if (val >= s->capture_count)
goto no_match; goto no_match;
@ -2368,7 +2368,7 @@ static intptr_t lre_exec_backtrack(REExecContext *s, uint8_t **capture,
{ {
int n; int n;
uint32_t low, high, idx_min, idx_max, idx; uint32_t low, high, idx_min, idx_max, idx;
n = get_u16(pc); /* n must be >= 1 */ n = get_u16(pc); /* n must be >= 1 */
pc += 2; pc += 2;
if (cptr >= cbuf_end) if (cptr >= cbuf_end)
@ -2408,7 +2408,7 @@ static intptr_t lre_exec_backtrack(REExecContext *s, uint8_t **capture,
{ {
int n; int n;
uint32_t low, high, idx_min, idx_max, idx; uint32_t low, high, idx_min, idx_max, idx;
n = get_u16(pc); /* n must be >= 1 */ n = get_u16(pc); /* n must be >= 1 */
pc += 2; pc += 2;
if (cptr >= cbuf_end) if (cptr >= cbuf_end)
@ -2453,14 +2453,14 @@ static intptr_t lre_exec_backtrack(REExecContext *s, uint8_t **capture,
size_t q; size_t q;
intptr_t res; intptr_t res;
const uint8_t *pc1; const uint8_t *pc1;
next_pos = get_u32(pc); next_pos = get_u32(pc);
quant_min = get_u32(pc + 4); quant_min = get_u32(pc + 4);
quant_max = get_u32(pc + 8); quant_max = get_u32(pc + 8);
pc += 16; pc += 16;
pc1 = pc; pc1 = pc;
pc += (int)next_pos; pc += (int)next_pos;
q = 0; q = 0;
for(;;) { for(;;) {
res = lre_exec_backtrack(s, capture, stack, stack_len, res = lre_exec_backtrack(s, capture, stack, stack_len,
@ -2503,7 +2503,7 @@ int lre_exec(uint8_t **capture,
REExecContext s_s, *s = &s_s; REExecContext s_s, *s = &s_s;
int re_flags, i, alloca_size, ret; int re_flags, i, alloca_size, ret;
StackInt *stack_buf; StackInt *stack_buf;
re_flags = bc_buf[RE_HEADER_FLAGS]; re_flags = bc_buf[RE_HEADER_FLAGS];
s->multi_line = (re_flags & LRE_FLAG_MULTILINE) != 0; s->multi_line = (re_flags & LRE_FLAG_MULTILINE) != 0;
s->ignore_case = (re_flags & LRE_FLAG_IGNORECASE) != 0; s->ignore_case = (re_flags & LRE_FLAG_IGNORECASE) != 0;
@ -2523,7 +2523,7 @@ int lre_exec(uint8_t **capture,
s->state_stack = NULL; s->state_stack = NULL;
s->state_stack_len = 0; s->state_stack_len = 0;
s->state_stack_size = 0; s->state_stack_size = 0;
for(i = 0; i < s->capture_count * 2; i++) for(i = 0; i < s->capture_count * 2; i++)
capture[i] = NULL; capture[i] = NULL;
alloca_size = s->stack_size_max * sizeof(stack_buf[0]); alloca_size = s->stack_size_max * sizeof(stack_buf[0]);
@ -2575,7 +2575,7 @@ int main(int argc, char **argv)
uint8_t *capture[CAPTURE_COUNT_MAX * 2]; uint8_t *capture[CAPTURE_COUNT_MAX * 2];
const char *input; const char *input;
int input_len, capture_count; int input_len, capture_count;
if (argc < 3) { if (argc < 3) {
printf("usage: %s regexp input\n", argv[0]); printf("usage: %s regexp input\n", argv[0]);
exit(1); exit(1);
@ -2589,7 +2589,7 @@ int main(int argc, char **argv)
input = argv[2]; input = argv[2];
input_len = strlen(input); input_len = strlen(input);
ret = lre_exec(capture, bc, (uint8_t *)input, 0, input_len, 0, NULL); ret = lre_exec(capture, bc, (uint8_t *)input, 0, input_len, 0, NULL);
printf("ret=%d\n", ret); printf("ret=%d\n", ret);
if (ret == 1) { if (ret == 1) {

View file

@ -1,6 +1,6 @@
/* /*
* Regular Expression Engine * Regular Expression Engine
* *
* Copyright (c) 2017-2018 Fabrice Bellard * Copyright (c) 2017-2018 Fabrice Bellard
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
@ -53,7 +53,7 @@ int lre_parse_escape(const uint8_t **pp, int allow_utf16);
LRE_BOOL lre_is_space(int c); LRE_BOOL lre_is_space(int c);
/* must be provided by the user */ /* must be provided by the user */
LRE_BOOL lre_check_stack_overflow(void *opaque, size_t alloca_size); LRE_BOOL lre_check_stack_overflow(void *opaque, size_t alloca_size);
void *lre_realloc(void *opaque, void *ptr, size_t size); void *lre_realloc(void *opaque, void *ptr, size_t size);
/* JS identifier test */ /* JS identifier test */

View file

@ -1,6 +1,6 @@
/* /*
* Unicode utilities * Unicode utilities
* *
* Copyright (c) 2017-2018 Fabrice Bellard * Copyright (c) 2017-2018 Fabrice Bellard
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
@ -49,9 +49,9 @@ enum {
}; };
/* conv_type: /* conv_type:
0 = to upper 0 = to upper
1 = to lower 1 = to lower
2 = case folding (= to lower with modifications) 2 = case folding (= to lower with modifications)
*/ */
int lre_case_conv(uint32_t *res, uint32_t c, int conv_type) int lre_case_conv(uint32_t *res, uint32_t c, int conv_type)
{ {
@ -68,7 +68,7 @@ int lre_case_conv(uint32_t *res, uint32_t c, int conv_type)
} else { } else {
uint32_t v, code, data, type, len, a, is_lower; uint32_t v, code, data, type, len, a, is_lower;
int idx, idx_min, idx_max; int idx, idx_min, idx_max;
is_lower = (conv_type != 0); is_lower = (conv_type != 0);
idx_min = 0; idx_min = 0;
idx_max = countof(case_conv_table1) - 1; idx_max = countof(case_conv_table1) - 1;
@ -204,7 +204,7 @@ static BOOL lre_is_in_table(uint32_t c, const uint8_t *table,
uint32_t code, b, bit; uint32_t code, b, bit;
int pos; int pos;
const uint8_t *p; const uint8_t *p;
pos = get_index_pos(&code, c, index_table, index_table_len); pos = get_index_pos(&code, c, index_table, index_table_len);
if (pos < 0) if (pos < 0)
return FALSE; /* outside the table */ return FALSE; /* outside the table */
@ -237,7 +237,7 @@ BOOL lre_is_cased(uint32_t c)
{ {
uint32_t v, code, len; uint32_t v, code, len;
int idx, idx_min, idx_max; int idx, idx_min, idx_max;
idx_min = 0; idx_min = 0;
idx_max = countof(case_conv_table1) - 1; idx_max = countof(case_conv_table1) - 1;
while (idx_min <= idx_max) { while (idx_min <= idx_max) {
@ -296,7 +296,7 @@ int cr_realloc(CharRange *cr, int size)
{ {
int new_size; int new_size;
uint32_t *new_buf; uint32_t *new_buf;
if (size > cr->size) { if (size > cr->size) {
new_size = max_int(size, cr->size * 3 / 2); new_size = max_int(size, cr->size * 3 / 2);
new_buf = cr->realloc_func(cr->mem_opaque, cr->points, new_buf = cr->realloc_func(cr->mem_opaque, cr->points,
@ -323,7 +323,7 @@ static void cr_compress(CharRange *cr)
{ {
int i, j, k, len; int i, j, k, len;
uint32_t *pt; uint32_t *pt;
pt = cr->points; pt = cr->points;
len = cr->len; len = cr->len;
i = 0; i = 0;
@ -353,7 +353,7 @@ int cr_op(CharRange *cr, const uint32_t *a_pt, int a_len,
{ {
int a_idx, b_idx, is_in; int a_idx, b_idx, is_in;
uint32_t v; uint32_t v;
a_idx = 0; a_idx = 0;
b_idx = 0; b_idx = 0;
for(;;) { for(;;) {
@ -654,7 +654,7 @@ static int unicode_decomp_char(uint32_t *res, uint32_t c, BOOL is_compat1)
{ {
uint32_t v, type, is_compat, code, len; uint32_t v, type, is_compat, code, len;
int idx_min, idx_max, idx; int idx_min, idx_max, idx;
idx_min = 0; idx_min = 0;
idx_max = countof(unicode_decomp_table1) - 1; idx_max = countof(unicode_decomp_table1) - 1;
while (idx_min <= idx_max) { while (idx_min <= idx_max) {
@ -684,7 +684,7 @@ static int unicode_compose_pair(uint32_t c0, uint32_t c1)
uint32_t code, len, type, v, idx1, d_idx, d_offset, ch; uint32_t code, len, type, v, idx1, d_idx, d_offset, ch;
int idx_min, idx_max, idx, d; int idx_min, idx_max, idx, d;
uint32_t pair[2]; uint32_t pair[2];
idx_min = 0; idx_min = 0;
idx_max = countof(unicode_comp_table) - 1; idx_max = countof(unicode_comp_table) - 1;
while (idx_min <= idx_max) { while (idx_min <= idx_max) {
@ -720,7 +720,7 @@ static int unicode_get_cc(uint32_t c)
uint32_t code, n, type, cc, c1, b; uint32_t code, n, type, cc, c1, b;
int pos; int pos;
const uint8_t *p; const uint8_t *p;
pos = get_index_pos(&code, c, pos = get_index_pos(&code, c,
unicode_cc_index, sizeof(unicode_cc_index) / 3); unicode_cc_index, sizeof(unicode_cc_index) / 3);
if (pos < 0) if (pos < 0)
@ -769,7 +769,7 @@ static int unicode_get_cc(uint32_t c)
static void sort_cc(int *buf, int len) static void sort_cc(int *buf, int len)
{ {
int i, j, k, cc, cc1, start, ch1; int i, j, k, cc, cc1, start, ch1;
for(i = 0; i < len; i++) { for(i = 0; i < len; i++) {
cc = unicode_get_cc(buf[i]); cc = unicode_get_cc(buf[i]);
if (cc != 0) { if (cc != 0) {
@ -808,7 +808,7 @@ static void to_nfd_rec(DynBuf *dbuf,
uint32_t c, v; uint32_t c, v;
int i, l; int i, l;
uint32_t res[UNICODE_DECOMP_LEN_MAX]; uint32_t res[UNICODE_DECOMP_LEN_MAX];
for(i = 0; i < src_len; i++) { for(i = 0; i < src_len; i++) {
c = src[i]; c = src[i];
if (c >= 0xac00 && c < 0xd7a4) { if (c >= 0xac00 && c < 0xd7a4) {
@ -853,7 +853,7 @@ int unicode_normalize(uint32_t **pdst, const uint32_t *src, int src_len,
int *buf, buf_len, i, p, starter_pos, cc, last_cc, out_len; int *buf, buf_len, i, p, starter_pos, cc, last_cc, out_len;
BOOL is_compat; BOOL is_compat;
DynBuf dbuf_s, *dbuf = &dbuf_s; DynBuf dbuf_s, *dbuf = &dbuf_s;
is_compat = n_type >> 1; is_compat = n_type >> 1;
dbuf_init2(dbuf, opaque, realloc_func); dbuf_init2(dbuf, opaque, realloc_func);
@ -881,15 +881,15 @@ int unicode_normalize(uint32_t **pdst, const uint32_t *src, int src_len,
} }
buf = (int *)dbuf->buf; buf = (int *)dbuf->buf;
buf_len = dbuf->size / sizeof(int); buf_len = dbuf->size / sizeof(int);
sort_cc(buf, buf_len); sort_cc(buf, buf_len);
if (buf_len <= 1 || (n_type & 1) != 0) { if (buf_len <= 1 || (n_type & 1) != 0) {
/* NFD / NFKD */ /* NFD / NFKD */
*pdst = (uint32_t *)buf; *pdst = (uint32_t *)buf;
return buf_len; return buf_len;
} }
i = 1; i = 1;
out_len = 1; out_len = 1;
while (i < buf_len) { while (i < buf_len) {
@ -926,7 +926,7 @@ static int unicode_find_name(const char *name_table, const char *name)
const char *p, *r; const char *p, *r;
int pos; int pos;
size_t name_len, len; size_t name_len, len;
p = name_table; p = name_table;
pos = 0; pos = 0;
name_len = strlen(name); name_len = strlen(name);
@ -959,13 +959,13 @@ int unicode_script(CharRange *cr,
CharRange cr1_s, *cr1; CharRange cr1_s, *cr1;
CharRange cr2_s, *cr2 = &cr2_s; CharRange cr2_s, *cr2 = &cr2_s;
BOOL is_common; BOOL is_common;
script_idx = unicode_find_name(unicode_script_name_table, script_name); script_idx = unicode_find_name(unicode_script_name_table, script_name);
if (script_idx < 0) if (script_idx < 0)
return -2; return -2;
/* Note: we remove the "Unknown" Script */ /* Note: we remove the "Unknown" Script */
script_idx += UNICODE_SCRIPT_Unknown + 1; script_idx += UNICODE_SCRIPT_Unknown + 1;
is_common = (script_idx == UNICODE_SCRIPT_Common || is_common = (script_idx == UNICODE_SCRIPT_Common ||
script_idx == UNICODE_SCRIPT_Inherited); script_idx == UNICODE_SCRIPT_Inherited);
if (is_ext) { if (is_ext) {
@ -1232,7 +1232,7 @@ static int unicode_case1(CharRange *cr, int case_mask)
} }
return 0; return 0;
} }
typedef enum { typedef enum {
POP_GC, POP_GC,
POP_PROP, POP_PROP,
@ -1252,7 +1252,7 @@ static int unicode_prop_ops(CharRange *cr, ...)
CharRange stack[POP_STACK_LEN_MAX]; CharRange stack[POP_STACK_LEN_MAX];
int stack_len, op, ret, i; int stack_len, op, ret, i;
uint32_t a; uint32_t a;
va_start(ap, cr); va_start(ap, cr);
stack_len = 0; stack_len = 0;
for(;;) { for(;;) {
@ -1338,7 +1338,7 @@ int unicode_general_category(CharRange *cr, const char *gc_name)
{ {
int gc_idx; int gc_idx;
uint32_t gc_mask; uint32_t gc_mask;
gc_idx = unicode_find_name(unicode_gc_name_table, gc_name); gc_idx = unicode_find_name(unicode_gc_name_table, gc_name);
if (gc_idx < 0) if (gc_idx < 0)
return -2; return -2;
@ -1356,7 +1356,7 @@ int unicode_general_category(CharRange *cr, const char *gc_name)
int unicode_prop(CharRange *cr, const char *prop_name) int unicode_prop(CharRange *cr, const char *prop_name)
{ {
int prop_idx, ret; int prop_idx, ret;
prop_idx = unicode_find_name(unicode_prop_name_table, prop_name); prop_idx = unicode_find_name(unicode_prop_name_table, prop_name);
if (prop_idx < 0) if (prop_idx < 0)
return -2; return -2;

View file

@ -1,6 +1,6 @@
/* /*
* Unicode utilities * Unicode utilities
* *
* Copyright (c) 2017-2018 Fabrice Bellard * Copyright (c) 2017-2018 Fabrice Bellard
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy

4
list.h
View file

@ -1,6 +1,6 @@
/* /*
* Linux klist like system * Linux klist like system
* *
* Copyright (c) 2016-2017 Fabrice Bellard * Copyright (c) 2016-2017 Fabrice Bellard
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
@ -46,7 +46,7 @@ static inline void init_list_head(struct list_head *head)
} }
/* insert 'el' between 'prev' and 'next' */ /* insert 'el' between 'prev' and 'next' */
static inline void __list_add(struct list_head *el, static inline void __list_add(struct list_head *el,
struct list_head *prev, struct list_head *next) struct list_head *prev, struct list_head *next)
{ {
prev->next = el; prev->next = el;

8
qjs.c
View file

@ -1,6 +1,6 @@
/* /*
* QuickJS stand alone interpreter * QuickJS stand alone interpreter
* *
* Copyright (c) 2017-2021 Fabrice Bellard * Copyright (c) 2017-2021 Fabrice Bellard
* Copyright (c) 2017-2021 Charlie Gordon * Copyright (c) 2017-2021 Charlie Gordon
* *
@ -77,7 +77,7 @@ static int eval_file(JSContext *ctx, const char *filename, int module)
uint8_t *buf; uint8_t *buf;
int ret, eval_flags; int ret, eval_flags;
size_t buf_len; size_t buf_len;
buf = js_load_file(ctx, &buf_len, filename); buf = js_load_file(ctx, &buf_len, filename);
if (!buf) { if (!buf) {
perror(filename); perror(filename);
@ -441,7 +441,7 @@ int main(int argc, char **argv)
JS_SetHostPromiseRejectionTracker(rt, js_std_promise_rejection_tracker, JS_SetHostPromiseRejectionTracker(rt, js_std_promise_rejection_tracker,
NULL); NULL);
} }
if (!empty_run) { if (!empty_run) {
js_std_add_helpers(ctx, argc - optind, argv + optind); js_std_add_helpers(ctx, argc - optind, argv + optind);
@ -477,7 +477,7 @@ int main(int argc, char **argv)
} }
js_std_loop(ctx); js_std_loop(ctx);
} }
if (dump_memory) { if (dump_memory) {
JSMemoryUsage stats; JSMemoryUsage stats;
JS_ComputeMemoryUsage(rt, &stats); JS_ComputeMemoryUsage(rt, &stats);

52
qjsc.c
View file

@ -1,6 +1,6 @@
/* /*
* QuickJS command line compiler * QuickJS command line compiler
* *
* Copyright (c) 2018-2021 Fabrice Bellard * Copyright (c) 2018-2021 Fabrice Bellard
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
@ -129,7 +129,7 @@ static void get_c_name(char *buf, size_t buf_size, const char *file)
size_t len, i; size_t len, i;
int c; int c;
char *q; char *q;
p = strrchr(file, '/'); p = strrchr(file, '/');
if (!p) if (!p)
p = file; p = file;
@ -187,8 +187,8 @@ static void output_object_code(JSContext *ctx,
} }
namelist_add(&cname_list, c_name, NULL, load_only); namelist_add(&cname_list, c_name, NULL, load_only);
fprintf(fo, "const uint32_t %s_size = %u;\n\n", fprintf(fo, "const uint32_t %s_size = %u;\n\n",
c_name, (unsigned int)out_buf_len); c_name, (unsigned int)out_buf_len);
fprintf(fo, "const uint8_t %s[%u] = {\n", fprintf(fo, "const uint8_t %s[%u] = {\n",
c_name, (unsigned int)out_buf_len); c_name, (unsigned int)out_buf_len);
@ -251,14 +251,14 @@ JSModuleDef *jsc_module_loader(JSContext *ctx,
uint8_t *buf; uint8_t *buf;
JSValue func_val; JSValue func_val;
char cname[1024]; char cname[1024];
buf = js_load_file(ctx, &buf_len, module_name); buf = js_load_file(ctx, &buf_len, module_name);
if (!buf) { if (!buf) {
JS_ThrowReferenceError(ctx, "could not load module filename '%s'", JS_ThrowReferenceError(ctx, "could not load module filename '%s'",
module_name); module_name);
return NULL; return NULL;
} }
/* compile the module */ /* compile the module */
func_val = JS_Eval(ctx, (char *)buf, buf_len, module_name, func_val = JS_Eval(ctx, (char *)buf, buf_len, module_name,
JS_EVAL_TYPE_MODULE | JS_EVAL_FLAG_COMPILE_ONLY); JS_EVAL_TYPE_MODULE | JS_EVAL_FLAG_COMPILE_ONLY);
@ -270,7 +270,7 @@ JSModuleDef *jsc_module_loader(JSContext *ctx,
find_unique_cname(cname, sizeof(cname)); find_unique_cname(cname, sizeof(cname));
} }
output_object_code(ctx, outfile, func_val, cname, TRUE); output_object_code(ctx, outfile, func_val, cname, TRUE);
/* the module is already referenced, so we must free it */ /* the module is already referenced, so we must free it */
m = JS_VALUE_GET_PTR(func_val); m = JS_VALUE_GET_PTR(func_val);
JS_FreeValue(ctx, func_val); JS_FreeValue(ctx, func_val);
@ -288,7 +288,7 @@ static void compile_file(JSContext *ctx, FILE *fo,
int eval_flags; int eval_flags;
JSValue obj; JSValue obj;
size_t buf_len; size_t buf_len;
buf = js_load_file(ctx, &buf_len, filename); buf = js_load_file(ctx, &buf_len, filename);
if (!buf) { if (!buf) {
fprintf(stderr, "Could not load '%s'\n", filename); fprintf(stderr, "Could not load '%s'\n", filename);
@ -382,7 +382,7 @@ int exec_cmd(char **argv)
if (pid == 0) { if (pid == 0) {
execvp(argv[0], argv); execvp(argv[0], argv);
exit(1); exit(1);
} }
for(;;) { for(;;) {
ret = waitpid(pid, &status, 0); ret = waitpid(pid, &status, 0);
@ -400,7 +400,7 @@ static int output_executable(const char *out_filename, const char *cfilename,
char libjsname[1024]; char libjsname[1024];
char exe_dir[1024], inc_dir[1024], lib_dir[1024], buf[1024], *p; char exe_dir[1024], inc_dir[1024], lib_dir[1024], buf[1024], *p;
int ret; int ret;
/* get the directory of the executable */ /* get the directory of the executable */
pstrcpy(exe_dir, sizeof(exe_dir), exename); pstrcpy(exe_dir, sizeof(exe_dir), exename);
p = strrchr(exe_dir, '/'); p = strrchr(exe_dir, '/');
@ -420,10 +420,10 @@ static int output_executable(const char *out_filename, const char *cfilename,
snprintf(inc_dir, sizeof(inc_dir), "%s/include/quickjs", CONFIG_PREFIX); snprintf(inc_dir, sizeof(inc_dir), "%s/include/quickjs", CONFIG_PREFIX);
snprintf(lib_dir, sizeof(lib_dir), "%s/lib/quickjs", CONFIG_PREFIX); snprintf(lib_dir, sizeof(lib_dir), "%s/lib/quickjs", CONFIG_PREFIX);
} }
lto_suffix = ""; lto_suffix = "";
bn_suffix = ""; bn_suffix = "";
arg = argv; arg = argv;
*arg++ = CONFIG_CC; *arg++ = CONFIG_CC;
*arg++ = "-O2"; *arg++ = "-O2";
@ -451,13 +451,13 @@ static int output_executable(const char *out_filename, const char *cfilename,
*arg++ = "-ldl"; *arg++ = "-ldl";
*arg++ = "-lpthread"; *arg++ = "-lpthread";
*arg = NULL; *arg = NULL;
if (verbose) { if (verbose) {
for(arg = argv; *arg != NULL; arg++) for(arg = argv; *arg != NULL; arg++)
printf("%s ", *arg); printf("%s ", *arg);
printf("\n"); printf("\n");
} }
ret = exec_cmd((char **)argv); ret = exec_cmd((char **)argv);
unlink(cfilename); unlink(cfilename);
return ret; return ret;
@ -492,7 +492,7 @@ int main(int argc, char **argv)
OutputTypeEnum output_type; OutputTypeEnum output_type;
size_t stack_size; size_t stack_size;
namelist_t dynamic_module_list; namelist_t dynamic_module_list;
out_filename = NULL; out_filename = NULL;
output_type = OUTPUT_EXECUTABLE; output_type = OUTPUT_EXECUTABLE;
cname = NULL; cname = NULL;
@ -503,7 +503,7 @@ int main(int argc, char **argv)
use_lto = FALSE; use_lto = FALSE;
stack_size = 0; stack_size = 0;
memset(&dynamic_module_list, 0, sizeof(dynamic_module_list)); memset(&dynamic_module_list, 0, sizeof(dynamic_module_list));
/* add system modules */ /* add system modules */
namelist_add(&cmodule_list, "std", "std", 0); namelist_add(&cmodule_list, "std", "std", 0);
namelist_add(&cmodule_list, "os", "os", 0); namelist_add(&cmodule_list, "os", "os", 0);
@ -610,24 +610,24 @@ int main(int argc, char **argv)
} else { } else {
pstrcpy(cfilename, sizeof(cfilename), out_filename); pstrcpy(cfilename, sizeof(cfilename), out_filename);
} }
fo = fopen(cfilename, "w"); fo = fopen(cfilename, "w");
if (!fo) { if (!fo) {
perror(cfilename); perror(cfilename);
exit(1); exit(1);
} }
outfile = fo; outfile = fo;
rt = JS_NewRuntime(); rt = JS_NewRuntime();
ctx = JS_NewContext(rt); ctx = JS_NewContext(rt);
/* loader for ES6 modules */ /* loader for ES6 modules */
JS_SetModuleLoaderFunc(rt, NULL, jsc_module_loader, NULL); JS_SetModuleLoaderFunc(rt, NULL, jsc_module_loader, NULL);
fprintf(fo, "/* File generated automatically by the QuickJS compiler. */\n" fprintf(fo, "/* File generated automatically by the QuickJS compiler. */\n"
"\n" "\n"
); );
if (output_type != OUTPUT_C) { if (output_type != OUTPUT_C) {
fprintf(fo, "#include \"quickjs-libc.h\"\n" fprintf(fo, "#include \"quickjs-libc.h\"\n"
"\n" "\n"
@ -651,7 +651,7 @@ int main(int argc, char **argv)
exit(1); exit(1);
} }
} }
if (output_type != OUTPUT_C) { if (output_type != OUTPUT_C) {
fprintf(fo, fprintf(fo,
"static JSContext *JS_NewCustomContext(JSRuntime *rt)\n" "static JSContext *JS_NewCustomContext(JSRuntime *rt)\n"
@ -673,7 +673,7 @@ int main(int argc, char **argv)
for(i = 0; i < init_module_list.count; i++) { for(i = 0; i < init_module_list.count; i++) {
namelist_entry_t *e = &init_module_list.array[i]; namelist_entry_t *e = &init_module_list.array[i];
/* initialize the static C modules */ /* initialize the static C modules */
fprintf(fo, fprintf(fo,
" {\n" " {\n"
" extern JSModuleDef *js_init_module_%s(JSContext *ctx, const char *name);\n" " extern JSModuleDef *js_init_module_%s(JSContext *ctx, const char *name);\n"
@ -691,19 +691,19 @@ int main(int argc, char **argv)
fprintf(fo, fprintf(fo,
" return ctx;\n" " return ctx;\n"
"}\n\n"); "}\n\n");
fputs(main_c_template1, fo); fputs(main_c_template1, fo);
if (stack_size != 0) { if (stack_size != 0) {
fprintf(fo, " JS_SetMaxStackSize(rt, %u);\n", fprintf(fo, " JS_SetMaxStackSize(rt, %u);\n",
(unsigned int)stack_size); (unsigned int)stack_size);
} }
/* add the module loader if necessary */ /* add the module loader if necessary */
if (feature_bitmap & (1 << FE_MODULE_LOADER)) { if (feature_bitmap & (1 << FE_MODULE_LOADER)) {
fprintf(fo, " JS_SetModuleLoaderFunc(rt, NULL, js_module_loader, NULL);\n"); fprintf(fo, " JS_SetModuleLoaderFunc(rt, NULL, js_module_loader, NULL);\n");
} }
fprintf(fo, fprintf(fo,
" ctx = JS_NewCustomContext(rt);\n" " ctx = JS_NewCustomContext(rt);\n"
" js_std_add_helpers(ctx, argc, argv);\n"); " js_std_add_helpers(ctx, argc, argv);\n");
@ -717,7 +717,7 @@ int main(int argc, char **argv)
} }
fputs(main_c_template2, fo); fputs(main_c_template2, fo);
} }
JS_FreeContext(ctx); JS_FreeContext(ctx);
JS_FreeRuntime(rt); JS_FreeRuntime(rt);

View file

@ -1,6 +1,6 @@
/* /*
* QuickJS atom definitions * QuickJS atom definitions
* *
* Copyright (c) 2017-2018 Fabrice Bellard * Copyright (c) 2017-2018 Fabrice Bellard
* Copyright (c) 2017-2018 Charlie Gordon * Copyright (c) 2017-2018 Charlie Gordon
* *
@ -195,7 +195,7 @@ DEF(RegExp, "RegExp")
DEF(ArrayBuffer, "ArrayBuffer") DEF(ArrayBuffer, "ArrayBuffer")
DEF(SharedArrayBuffer, "SharedArrayBuffer") DEF(SharedArrayBuffer, "SharedArrayBuffer")
/* must keep same order as class IDs for typed arrays */ /* must keep same order as class IDs for typed arrays */
DEF(Uint8ClampedArray, "Uint8ClampedArray") DEF(Uint8ClampedArray, "Uint8ClampedArray")
DEF(Int8Array, "Int8Array") DEF(Int8Array, "Int8Array")
DEF(Uint8Array, "Uint8Array") DEF(Uint8Array, "Uint8Array")
DEF(Int16Array, "Int16Array") DEF(Int16Array, "Int16Array")
@ -250,5 +250,5 @@ DEF(Symbol_hasInstance, "Symbol.hasInstance")
DEF(Symbol_species, "Symbol.species") DEF(Symbol_species, "Symbol.species")
DEF(Symbol_unscopables, "Symbol.unscopables") DEF(Symbol_unscopables, "Symbol.unscopables")
DEF(Symbol_asyncIterator, "Symbol.asyncIterator") DEF(Symbol_asyncIterator, "Symbol.asyncIterator")
#endif /* DEF */ #endif /* DEF */

View file

@ -1,6 +1,6 @@
/* /*
* QuickJS C library * QuickJS C library
* *
* Copyright (c) 2017-2021 Fabrice Bellard * Copyright (c) 2017-2021 Fabrice Bellard
* Copyright (c) 2017-2021 Charlie Gordon * Copyright (c) 2017-2021 Charlie Gordon
* *
@ -180,7 +180,7 @@ static JSValue js_printf_internal(JSContext *ctx,
break; break;
q = fmtbuf; q = fmtbuf;
*q++ = *fmt++; /* copy '%' */ *q++ = *fmt++; /* copy '%' */
/* flags */ /* flags */
for(;;) { for(;;) {
c = *fmt; c = *fmt;
@ -234,14 +234,14 @@ static JSValue js_printf_internal(JSContext *ctx,
if (*fmt == 'l') { if (*fmt == 'l') {
mod = *fmt++; mod = *fmt++;
} }
/* type */ /* type */
c = *fmt++; c = *fmt++;
if (q >= fmtbuf + sizeof(fmtbuf) - 1) if (q >= fmtbuf + sizeof(fmtbuf) - 1)
goto invalid; goto invalid;
*q++ = c; *q++ = c;
*q = '\0'; *q = '\0';
switch (c) { switch (c) {
case 'c': case 'c':
if (i >= argc) if (i >= argc)
@ -263,7 +263,7 @@ static JSValue js_printf_internal(JSContext *ctx,
len = unicode_to_utf8(cbuf, int32_arg); len = unicode_to_utf8(cbuf, int32_arg);
dbuf_put(&dbuf, cbuf, len); dbuf_put(&dbuf, cbuf, len);
break; break;
case 'd': case 'd':
case 'i': case 'i':
case 'o': case 'o':
@ -308,7 +308,7 @@ static JSValue js_printf_internal(JSContext *ctx,
dbuf_printf_fun(&dbuf, fmtbuf, string_arg); dbuf_printf_fun(&dbuf, fmtbuf, string_arg);
JS_FreeCString(ctx, string_arg); JS_FreeCString(ctx, string_arg);
break; break;
case 'e': case 'e':
case 'f': case 'f':
case 'g': case 'g':
@ -323,11 +323,11 @@ static JSValue js_printf_internal(JSContext *ctx,
goto fail; goto fail;
dbuf_printf_fun(&dbuf, fmtbuf, double_arg); dbuf_printf_fun(&dbuf, fmtbuf, double_arg);
break; break;
case '%': case '%':
dbuf_putc(&dbuf, '%'); dbuf_putc(&dbuf, '%');
break; break;
default: default:
/* XXX: should support an extension mechanism */ /* XXX: should support an extension mechanism */
invalid: invalid:
@ -364,7 +364,7 @@ uint8_t *js_load_file(JSContext *ctx, size_t *pbuf_len, const char *filename)
uint8_t *buf; uint8_t *buf;
size_t buf_len; size_t buf_len;
long lret; long lret;
f = fopen(filename, "rb"); f = fopen(filename, "rb");
if (!f) if (!f)
return NULL; return NULL;
@ -411,7 +411,7 @@ static JSValue js_loadScript(JSContext *ctx, JSValueConst this_val,
const char *filename; const char *filename;
JSValue ret; JSValue ret;
size_t buf_len; size_t buf_len;
filename = JS_ToCString(ctx, argv[0]); filename = JS_ToCString(ctx, argv[0]);
if (!filename) if (!filename)
return JS_EXCEPTION; return JS_EXCEPTION;
@ -436,7 +436,7 @@ static JSValue js_std_loadFile(JSContext *ctx, JSValueConst this_val,
const char *filename; const char *filename;
JSValue ret; JSValue ret;
size_t buf_len; size_t buf_len;
filename = JS_ToCString(ctx, argv[0]); filename = JS_ToCString(ctx, argv[0]);
if (!filename) if (!filename)
return JS_EXCEPTION; return JS_EXCEPTION;
@ -468,7 +468,7 @@ static JSModuleDef *js_module_loader_so(JSContext *ctx,
void *hd; void *hd;
JSInitModuleFunc *init; JSInitModuleFunc *init;
char *filename; char *filename;
if (!strchr(module_name, '/')) { if (!strchr(module_name, '/')) {
/* must add a '/' so that the DLL is not searched in the /* must add a '/' so that the DLL is not searched in the
system library paths */ system library paths */
@ -480,7 +480,7 @@ static JSModuleDef *js_module_loader_so(JSContext *ctx,
} else { } else {
filename = (char *)module_name; filename = (char *)module_name;
} }
/* C module */ /* C module */
hd = dlopen(filename, RTLD_NOW | RTLD_LOCAL); hd = dlopen(filename, RTLD_NOW | RTLD_LOCAL);
if (filename != module_name) if (filename != module_name)
@ -519,7 +519,7 @@ int js_module_set_import_meta(JSContext *ctx, JSValueConst func_val,
JSValue meta_obj; JSValue meta_obj;
JSAtom module_name_atom; JSAtom module_name_atom;
const char *module_name; const char *module_name;
assert(JS_VALUE_GET_TAG(func_val) == JS_TAG_MODULE); assert(JS_VALUE_GET_TAG(func_val) == JS_TAG_MODULE);
m = JS_VALUE_GET_PTR(func_val); m = JS_VALUE_GET_PTR(func_val);
@ -550,7 +550,7 @@ int js_module_set_import_meta(JSContext *ctx, JSValueConst func_val,
pstrcpy(buf, sizeof(buf), module_name); pstrcpy(buf, sizeof(buf), module_name);
} }
JS_FreeCString(ctx, module_name); JS_FreeCString(ctx, module_name);
meta_obj = JS_GetImportMeta(ctx, m); meta_obj = JS_GetImportMeta(ctx, m);
if (JS_IsException(meta_obj)) if (JS_IsException(meta_obj))
return -1; return -1;
@ -575,14 +575,14 @@ JSModuleDef *js_module_loader(JSContext *ctx,
size_t buf_len; size_t buf_len;
uint8_t *buf; uint8_t *buf;
JSValue func_val; JSValue func_val;
buf = js_load_file(ctx, &buf_len, module_name); buf = js_load_file(ctx, &buf_len, module_name);
if (!buf) { if (!buf) {
JS_ThrowReferenceError(ctx, "could not load module filename '%s'", JS_ThrowReferenceError(ctx, "could not load module filename '%s'",
module_name); module_name);
return NULL; return NULL;
} }
/* compile the module */ /* compile the module */
func_val = JS_Eval(ctx, (char *)buf, buf_len, module_name, func_val = JS_Eval(ctx, (char *)buf, buf_len, module_name,
JS_EVAL_TYPE_MODULE | JS_EVAL_FLAG_COMPILE_ONLY); JS_EVAL_TYPE_MODULE | JS_EVAL_FLAG_COMPILE_ONLY);
@ -752,7 +752,7 @@ static JSValue js_evalScript(JSContext *ctx, JSValueConst this_val,
JSValueConst options_obj; JSValueConst options_obj;
BOOL backtrace_barrier = FALSE; BOOL backtrace_barrier = FALSE;
int flags; int flags;
if (argc >= 2) { if (argc >= 2) {
options_obj = argv[1]; options_obj = argv[1];
if (get_bool_option(ctx, &backtrace_barrier, options_obj, if (get_bool_option(ctx, &backtrace_barrier, options_obj,
@ -767,7 +767,7 @@ static JSValue js_evalScript(JSContext *ctx, JSValueConst this_val,
/* install the interrupt handler */ /* install the interrupt handler */
JS_SetInterruptHandler(JS_GetRuntime(ctx), interrupt_handler, NULL); JS_SetInterruptHandler(JS_GetRuntime(ctx), interrupt_handler, NULL);
} }
flags = JS_EVAL_TYPE_GLOBAL; flags = JS_EVAL_TYPE_GLOBAL;
if (backtrace_barrier) if (backtrace_barrier)
flags |= JS_EVAL_FLAG_BACKTRACE_BARRIER; flags |= JS_EVAL_FLAG_BACKTRACE_BARRIER;
ret = JS_Eval(ctx, str, len, "<evalScript>", flags); ret = JS_Eval(ctx, str, len, "<evalScript>", flags);
@ -856,7 +856,7 @@ static JSValue js_std_open(JSContext *ctx, JSValueConst this_val,
const char *filename, *mode = NULL; const char *filename, *mode = NULL;
FILE *f; FILE *f;
int err; int err;
filename = JS_ToCString(ctx, argv[0]); filename = JS_ToCString(ctx, argv[0]);
if (!filename) if (!filename)
goto fail; goto fail;
@ -892,7 +892,7 @@ static JSValue js_std_popen(JSContext *ctx, JSValueConst this_val,
const char *filename, *mode = NULL; const char *filename, *mode = NULL;
FILE *f; FILE *f;
int err; int err;
filename = JS_ToCString(ctx, argv[0]); filename = JS_ToCString(ctx, argv[0]);
if (!filename) if (!filename)
goto fail; goto fail;
@ -1006,7 +1006,7 @@ static JSValue js_std_file_puts(JSContext *ctx, JSValueConst this_val,
if (!f) if (!f)
return JS_EXCEPTION; return JS_EXCEPTION;
} }
for(i = 0; i < argc; i++) { for(i = 0; i < argc; i++) {
str = JS_ToCStringLen(ctx, &len, argv[i]); str = JS_ToCStringLen(ctx, &len, argv[i]);
if (!str) if (!str)
@ -1137,7 +1137,7 @@ static JSValue js_std_file_read_write(JSContext *ctx, JSValueConst this_val,
uint64_t pos, len; uint64_t pos, len;
size_t size, ret; size_t size, ret;
uint8_t *buf; uint8_t *buf;
if (!f) if (!f)
return JS_EXCEPTION; return JS_EXCEPTION;
if (JS_ToIndex(ctx, &pos, argv[1])) if (JS_ToIndex(ctx, &pos, argv[1]))
@ -1164,7 +1164,7 @@ static JSValue js_std_file_getline(JSContext *ctx, JSValueConst this_val,
int c; int c;
DynBuf dbuf; DynBuf dbuf;
JSValue obj; JSValue obj;
if (!f) if (!f)
return JS_EXCEPTION; return JS_EXCEPTION;
@ -1203,7 +1203,7 @@ static JSValue js_std_file_readAsString(JSContext *ctx, JSValueConst this_val,
uint64_t max_size64; uint64_t max_size64;
size_t max_size; size_t max_size;
JSValueConst max_size_val; JSValueConst max_size_val;
if (!f) if (!f)
return JS_EXCEPTION; return JS_EXCEPTION;
@ -1267,7 +1267,7 @@ static int http_get_header_line(FILE *f, char *buf, size_t buf_size,
{ {
int c; int c;
char *p; char *p;
p = buf; p = buf;
for(;;) { for(;;) {
c = fgetc(f); c = fgetc(f);
@ -1303,21 +1303,21 @@ static JSValue js_std_urlGet(JSContext *ctx, JSValueConst this_val,
DynBuf cmd_buf; DynBuf cmd_buf;
DynBuf data_buf_s, *data_buf = &data_buf_s; DynBuf data_buf_s, *data_buf = &data_buf_s;
DynBuf header_buf_s, *header_buf = &header_buf_s; DynBuf header_buf_s, *header_buf = &header_buf_s;
char *buf; char *buf;
size_t i, len; size_t i, len;
int c, status; int c, status;
JSValue response = JS_UNDEFINED, ret_obj; JSValue response = JS_UNDEFINED, ret_obj;
JSValueConst options_obj; JSValueConst options_obj;
FILE *f; FILE *f;
BOOL binary_flag, full_flag; BOOL binary_flag, full_flag;
url = JS_ToCString(ctx, argv[0]); url = JS_ToCString(ctx, argv[0]);
if (!url) if (!url)
return JS_EXCEPTION; return JS_EXCEPTION;
binary_flag = FALSE; binary_flag = FALSE;
full_flag = FALSE; full_flag = FALSE;
if (argc >= 2) { if (argc >= 2) {
options_obj = argv[1]; options_obj = argv[1];
@ -1330,7 +1330,7 @@ static JSValue js_std_urlGet(JSContext *ctx, JSValueConst this_val,
return JS_EXCEPTION; return JS_EXCEPTION;
} }
} }
js_std_dbuf_init(ctx, &cmd_buf); js_std_dbuf_init(ctx, &cmd_buf);
dbuf_printf(&cmd_buf, "%s ''", URL_GET_PROGRAM); dbuf_printf(&cmd_buf, "%s ''", URL_GET_PROGRAM);
len = strlen(url); len = strlen(url);
@ -1356,7 +1356,7 @@ static JSValue js_std_urlGet(JSContext *ctx, JSValueConst this_val,
js_std_dbuf_init(ctx, data_buf); js_std_dbuf_init(ctx, data_buf);
js_std_dbuf_init(ctx, header_buf); js_std_dbuf_init(ctx, header_buf);
buf = js_malloc(ctx, URL_GET_BUF_SIZE); buf = js_malloc(ctx, URL_GET_BUF_SIZE);
if (!buf) if (!buf)
goto fail; goto fail;
@ -1370,7 +1370,7 @@ static JSValue js_std_urlGet(JSContext *ctx, JSValueConst this_val,
if (!full_flag && !(status >= 200 && status <= 299)) { if (!full_flag && !(status >= 200 && status <= 299)) {
goto bad_header; goto bad_header;
} }
/* wait until there is an empty line */ /* wait until there is an empty line */
for(;;) { for(;;) {
if (http_get_header_line(f, buf, URL_GET_BUF_SIZE, header_buf) < 0) { if (http_get_header_line(f, buf, URL_GET_BUF_SIZE, header_buf) < 0) {
@ -1446,7 +1446,7 @@ static JSValue js_std_urlGet(JSContext *ctx, JSValueConst this_val,
static JSClassDef js_std_file_class = { static JSClassDef js_std_file_class = {
"FILE", "FILE",
.finalizer = js_std_file_finalizer, .finalizer = js_std_file_finalizer,
}; };
static const JSCFunctionListEntry js_std_error_props[] = { static const JSCFunctionListEntry js_std_error_props[] = {
/* various errno values */ /* various errno values */
@ -1477,7 +1477,7 @@ static const JSCFunctionListEntry js_std_funcs[] = {
JS_CFUNC_DEF("urlGet", 1, js_std_urlGet ), JS_CFUNC_DEF("urlGet", 1, js_std_urlGet ),
JS_CFUNC_DEF("loadFile", 1, js_std_loadFile ), JS_CFUNC_DEF("loadFile", 1, js_std_loadFile ),
JS_CFUNC_DEF("strerror", 1, js_std_strerror ), JS_CFUNC_DEF("strerror", 1, js_std_strerror ),
/* FILE I/O */ /* FILE I/O */
JS_CFUNC_DEF("open", 2, js_std_open ), JS_CFUNC_DEF("open", 2, js_std_open ),
JS_CFUNC_DEF("popen", 2, js_std_popen ), JS_CFUNC_DEF("popen", 2, js_std_popen ),
@ -1491,7 +1491,7 @@ static const JSCFunctionListEntry js_std_funcs[] = {
JS_PROP_INT32_DEF("SEEK_END", SEEK_END, JS_PROP_CONFIGURABLE ), JS_PROP_INT32_DEF("SEEK_END", SEEK_END, JS_PROP_CONFIGURABLE ),
JS_OBJECT_DEF("Error", js_std_error_props, countof(js_std_error_props), JS_PROP_CONFIGURABLE), JS_OBJECT_DEF("Error", js_std_error_props, countof(js_std_error_props), JS_PROP_CONFIGURABLE),
}; };
static const JSCFunctionListEntry js_std_file_proto_funcs[] = { static const JSCFunctionListEntry js_std_file_proto_funcs[] = {
JS_CFUNC_DEF("close", 0, js_std_file_close ), JS_CFUNC_DEF("close", 0, js_std_file_close ),
JS_CFUNC_MAGIC_DEF("puts", 1, js_std_file_puts, 1 ), JS_CFUNC_MAGIC_DEF("puts", 1, js_std_file_puts, 1 ),
@ -1516,7 +1516,7 @@ static const JSCFunctionListEntry js_std_file_proto_funcs[] = {
static int js_std_init(JSContext *ctx, JSModuleDef *m) static int js_std_init(JSContext *ctx, JSModuleDef *m)
{ {
JSValue proto; JSValue proto;
/* FILE class */ /* FILE class */
/* the class ID is created once */ /* the class ID is created once */
JS_NewClassID(&js_std_file_class_id); JS_NewClassID(&js_std_file_class_id);
@ -1597,7 +1597,7 @@ static JSValue js_os_seek(JSContext *ctx, JSValueConst this_val,
int fd, whence; int fd, whence;
int64_t pos, ret; int64_t pos, ret;
BOOL is_bigint; BOOL is_bigint;
if (JS_ToInt32(ctx, &fd, argv[0])) if (JS_ToInt32(ctx, &fd, argv[0]))
return JS_EXCEPTION; return JS_EXCEPTION;
is_bigint = JS_IsBigInt(ctx, argv[1]); is_bigint = JS_IsBigInt(ctx, argv[1]);
@ -1622,7 +1622,7 @@ static JSValue js_os_read_write(JSContext *ctx, JSValueConst this_val,
size_t size; size_t size;
ssize_t ret; ssize_t ret;
uint8_t *buf; uint8_t *buf;
if (JS_ToInt32(ctx, &fd, argv[0])) if (JS_ToInt32(ctx, &fd, argv[0]))
return JS_EXCEPTION; return JS_EXCEPTION;
if (JS_ToIndex(ctx, &pos, argv[2])) if (JS_ToIndex(ctx, &pos, argv[2]))
@ -1662,7 +1662,7 @@ static JSValue js_os_ttyGetWinSize(JSContext *ctx, JSValueConst this_val,
if (JS_ToInt32(ctx, &fd, argv[0])) if (JS_ToInt32(ctx, &fd, argv[0]))
return JS_EXCEPTION; return JS_EXCEPTION;
handle = (HANDLE)_get_osfhandle(fd); handle = (HANDLE)_get_osfhandle(fd);
if (!GetConsoleScreenBufferInfo(handle, &info)) if (!GetConsoleScreenBufferInfo(handle, &info))
return JS_NULL; return JS_NULL;
obj = JS_NewArray(ctx); obj = JS_NewArray(ctx);
@ -1701,7 +1701,7 @@ static JSValue js_os_ttyGetWinSize(JSContext *ctx, JSValueConst this_val,
int fd; int fd;
struct winsize ws; struct winsize ws;
JSValue obj; JSValue obj;
if (JS_ToInt32(ctx, &fd, argv[0])) if (JS_ToInt32(ctx, &fd, argv[0]))
return JS_EXCEPTION; return JS_EXCEPTION;
if (ioctl(fd, TIOCGWINSZ, &ws) == 0 && if (ioctl(fd, TIOCGWINSZ, &ws) == 0 &&
@ -1730,10 +1730,10 @@ static JSValue js_os_ttySetRaw(JSContext *ctx, JSValueConst this_val,
{ {
struct termios tty; struct termios tty;
int fd; int fd;
if (JS_ToInt32(ctx, &fd, argv[0])) if (JS_ToInt32(ctx, &fd, argv[0]))
return JS_EXCEPTION; return JS_EXCEPTION;
memset(&tty, 0, sizeof(tty)); memset(&tty, 0, sizeof(tty));
tcgetattr(fd, &tty); tcgetattr(fd, &tty);
oldtty = tty; oldtty = tty;
@ -1760,7 +1760,7 @@ static JSValue js_os_remove(JSContext *ctx, JSValueConst this_val,
{ {
const char *filename; const char *filename;
int ret; int ret;
filename = JS_ToCString(ctx, argv[0]); filename = JS_ToCString(ctx, argv[0]);
if (!filename) if (!filename)
return JS_EXCEPTION; return JS_EXCEPTION;
@ -1786,7 +1786,7 @@ static JSValue js_os_rename(JSContext *ctx, JSValueConst this_val,
{ {
const char *oldpath, *newpath; const char *oldpath, *newpath;
int ret; int ret;
oldpath = JS_ToCString(ctx, argv[0]); oldpath = JS_ToCString(ctx, argv[0]);
if (!oldpath) if (!oldpath)
return JS_EXCEPTION; return JS_EXCEPTION;
@ -1838,7 +1838,7 @@ static JSValue js_os_setReadHandler(JSContext *ctx, JSValueConst this_val,
JSOSRWHandler *rh; JSOSRWHandler *rh;
int fd; int fd;
JSValueConst func; JSValueConst func;
if (JS_ToInt32(ctx, &fd, argv[0])) if (JS_ToInt32(ctx, &fd, argv[0]))
return JS_EXCEPTION; return JS_EXCEPTION;
func = argv[1]; func = argv[1];
@ -1912,7 +1912,7 @@ static JSValue js_os_signal(JSContext *ctx, JSValueConst this_val,
if (!is_main_thread(rt)) if (!is_main_thread(rt))
return JS_ThrowTypeError(ctx, "signal handler can only be set in the main thread"); return JS_ThrowTypeError(ctx, "signal handler can only be set in the main thread");
if (JS_ToUint32(ctx, &sig_num, argv[0])) if (JS_ToUint32(ctx, &sig_num, argv[0]))
return JS_EXCEPTION; return JS_EXCEPTION;
if (sig_num >= 64) if (sig_num >= 64)
@ -2044,7 +2044,7 @@ static JSClassDef js_os_timer_class = {
"OSTimer", "OSTimer",
.finalizer = js_os_timer_finalizer, .finalizer = js_os_timer_finalizer,
.gc_mark = js_os_timer_mark, .gc_mark = js_os_timer_mark,
}; };
static void call_handler(JSContext *ctx, JSValueConst func) static void call_handler(JSContext *ctx, JSValueConst func)
{ {
@ -2069,12 +2069,12 @@ static int js_os_poll(JSContext *ctx)
int64_t cur_time, delay; int64_t cur_time, delay;
JSOSRWHandler *rh; JSOSRWHandler *rh;
struct list_head *el; struct list_head *el;
/* XXX: handle signals if useful */ /* XXX: handle signals if useful */
if (list_empty(&ts->os_rw_handlers) && list_empty(&ts->os_timers)) if (list_empty(&ts->os_rw_handlers) && list_empty(&ts->os_timers))
return -1; /* no more events */ return -1; /* no more events */
/* XXX: only timers and basic console input are supported */ /* XXX: only timers and basic console input are supported */
if (!list_empty(&ts->os_timers)) { if (!list_empty(&ts->os_timers)) {
cur_time = get_time_ms(); cur_time = get_time_ms();
@ -2149,7 +2149,7 @@ static int handle_posted_message(JSRuntime *rt, JSContext *ctx,
struct list_head *el; struct list_head *el;
JSWorkerMessage *msg; JSWorkerMessage *msg;
JSValue obj, data_obj, func, retval; JSValue obj, data_obj, func, retval;
pthread_mutex_lock(&ps->mutex); pthread_mutex_lock(&ps->mutex);
if (!list_empty(&ps->msg_queue)) { if (!list_empty(&ps->msg_queue)) {
el = ps->msg_queue.next; el = ps->msg_queue.next;
@ -2176,7 +2176,7 @@ static int handle_posted_message(JSRuntime *rt, JSContext *ctx,
JS_READ_OBJ_SAB | JS_READ_OBJ_REFERENCE); JS_READ_OBJ_SAB | JS_READ_OBJ_REFERENCE);
js_free_message(msg); js_free_message(msg);
if (JS_IsException(data_obj)) if (JS_IsException(data_obj))
goto fail; goto fail;
obj = JS_NewObject(ctx); obj = JS_NewObject(ctx);
@ -2229,7 +2229,7 @@ static int js_os_poll(JSContext *ctx)
unlikely(os_pending_signals != 0)) { unlikely(os_pending_signals != 0)) {
JSOSSignalHandler *sh; JSOSSignalHandler *sh;
uint64_t mask; uint64_t mask;
list_for_each(el, &ts->os_signal_handlers) { list_for_each(el, &ts->os_signal_handlers) {
sh = list_entry(el, JSOSSignalHandler, link); sh = list_entry(el, JSOSSignalHandler, link);
mask = (uint64_t)1 << sh->sig_num; mask = (uint64_t)1 << sh->sig_num;
@ -2244,7 +2244,7 @@ static int js_os_poll(JSContext *ctx)
if (list_empty(&ts->os_rw_handlers) && list_empty(&ts->os_timers) && if (list_empty(&ts->os_rw_handlers) && list_empty(&ts->os_timers) &&
list_empty(&ts->port_list)) list_empty(&ts->port_list))
return -1; /* no more events */ return -1; /* no more events */
if (!list_empty(&ts->os_timers)) { if (!list_empty(&ts->os_timers)) {
cur_time = get_time_ms(); cur_time = get_time_ms();
min_delay = 10000; min_delay = 10000;
@ -2272,7 +2272,7 @@ static int js_os_poll(JSContext *ctx)
} else { } else {
tvp = NULL; tvp = NULL;
} }
FD_ZERO(&rfds); FD_ZERO(&rfds);
FD_ZERO(&wfds); FD_ZERO(&wfds);
fd_max = -1; fd_max = -1;
@ -2358,7 +2358,7 @@ static JSValue js_os_getcwd(JSContext *ctx, JSValueConst this_val,
{ {
char buf[PATH_MAX]; char buf[PATH_MAX];
int err; int err;
if (!getcwd(buf, sizeof(buf))) { if (!getcwd(buf, sizeof(buf))) {
buf[0] = '\0'; buf[0] = '\0';
err = errno; err = errno;
@ -2387,7 +2387,7 @@ static JSValue js_os_mkdir(JSContext *ctx, JSValueConst this_val,
{ {
int mode, ret; int mode, ret;
const char *path; const char *path;
if (argc >= 2) { if (argc >= 2) {
if (JS_ToInt32(ctx, &mode, argv[1])) if (JS_ToInt32(ctx, &mode, argv[1]))
return JS_EXCEPTION; return JS_EXCEPTION;
@ -2417,7 +2417,7 @@ static JSValue js_os_readdir(JSContext *ctx, JSValueConst this_val,
JSValue obj; JSValue obj;
int err; int err;
uint32_t len; uint32_t len;
path = JS_ToCString(ctx, argv[0]); path = JS_ToCString(ctx, argv[0]);
if (!path) if (!path)
return JS_EXCEPTION; return JS_EXCEPTION;
@ -2565,7 +2565,7 @@ static JSValue js_os_utimes(JSContext *ctx, JSValueConst this_val,
const char *path; const char *path;
int64_t atime, mtime; int64_t atime, mtime;
int ret; int ret;
if (JS_ToInt64(ctx, &atime, argv[1])) if (JS_ToInt64(ctx, &atime, argv[1]))
return JS_EXCEPTION; return JS_EXCEPTION;
if (JS_ToInt64(ctx, &mtime, argv[2])) if (JS_ToInt64(ctx, &mtime, argv[2]))
@ -2598,7 +2598,7 @@ static JSValue js_os_sleep(JSContext *ctx, JSValueConst this_val,
{ {
int64_t delay; int64_t delay;
int ret; int ret;
if (JS_ToInt64(ctx, &delay, argv[0])) if (JS_ToInt64(ctx, &delay, argv[0]))
return JS_EXCEPTION; return JS_EXCEPTION;
if (delay < 0) if (delay < 0)
@ -2662,7 +2662,7 @@ static JSValue js_os_symlink(JSContext *ctx, JSValueConst this_val,
{ {
const char *target, *linkpath; const char *target, *linkpath;
int err; int err;
target = JS_ToCString(ctx, argv[0]); target = JS_ToCString(ctx, argv[0]);
if (!target) if (!target)
return JS_EXCEPTION; return JS_EXCEPTION;
@ -2685,7 +2685,7 @@ static JSValue js_os_readlink(JSContext *ctx, JSValueConst this_val,
char buf[PATH_MAX]; char buf[PATH_MAX];
int err; int err;
ssize_t res; ssize_t res;
path = JS_ToCString(ctx, argv[0]); path = JS_ToCString(ctx, argv[0]);
if (!path) if (!path)
return JS_EXCEPTION; return JS_EXCEPTION;
@ -2709,7 +2709,7 @@ static char **build_envp(JSContext *ctx, JSValueConst obj)
const char *key, *str; const char *key, *str;
JSValue val; JSValue val;
size_t key_len, str_len; size_t key_len, str_len;
if (JS_GetOwnPropertyNames(ctx, &tab, &len, obj, if (JS_GetOwnPropertyNames(ctx, &tab, &len, obj,
JS_GPN_STRING_MASK | JS_GPN_ENUM_ONLY) < 0) JS_GPN_STRING_MASK | JS_GPN_ENUM_ONLY) < 0)
return NULL; return NULL;
@ -2767,7 +2767,7 @@ static int my_execvpe(const char *filename, char **argv, char **envp)
char buf[PATH_MAX]; char buf[PATH_MAX];
size_t filename_len, path_len; size_t filename_len, path_len;
BOOL eacces_error; BOOL eacces_error;
filename_len = strlen(filename); filename_len = strlen(filename);
if (filename_len == 0) { if (filename_len == 0) {
errno = ENOENT; errno = ENOENT;
@ -2775,7 +2775,7 @@ static int my_execvpe(const char *filename, char **argv, char **envp)
} }
if (strchr(filename, '/')) if (strchr(filename, '/'))
return execve(filename, argv, envp); return execve(filename, argv, envp);
path = getenv("PATH"); path = getenv("PATH");
if (!path) if (!path)
path = (char *)"/bin:/usr/bin"; path = (char *)"/bin:/usr/bin";
@ -2797,7 +2797,7 @@ static int my_execvpe(const char *filename, char **argv, char **envp)
buf[path_len] = '/'; buf[path_len] = '/';
memcpy(buf + path_len + 1, filename, filename_len); memcpy(buf + path_len + 1, filename, filename_len);
buf[path_len + 1 + filename_len] = '\0'; buf[path_len + 1 + filename_len] = '\0';
execve(buf, argv, envp); execve(buf, argv, envp);
switch(errno) { switch(errno) {
@ -2830,7 +2830,7 @@ static JSValue js_os_exec(JSContext *ctx, JSValueConst this_val,
static const char *std_name[3] = { "stdin", "stdout", "stderr" }; static const char *std_name[3] = { "stdin", "stdout", "stderr" };
int std_fds[3]; int std_fds[3];
uint32_t uid = -1, gid = -1; uint32_t uid = -1, gid = -1;
val = JS_GetPropertyStr(ctx, args, "length"); val = JS_GetPropertyStr(ctx, args, "length");
if (JS_IsException(val)) if (JS_IsException(val))
return JS_EXCEPTION; return JS_EXCEPTION;
@ -2859,7 +2859,7 @@ static JSValue js_os_exec(JSContext *ctx, JSValueConst this_val,
for(i = 0; i < 3; i++) for(i = 0; i < 3; i++)
std_fds[i] = i; std_fds[i] = i;
/* get the options, if any */ /* get the options, if any */
if (argc >= 2) { if (argc >= 2) {
options = argv[1]; options = argv[1];
@ -2868,7 +2868,7 @@ static JSValue js_os_exec(JSContext *ctx, JSValueConst this_val,
goto exception; goto exception;
if (get_bool_option(ctx, &use_path, options, "usePath")) if (get_bool_option(ctx, &use_path, options, "usePath"))
goto exception; goto exception;
val = JS_GetPropertyStr(ctx, options, "file"); val = JS_GetPropertyStr(ctx, options, "file");
if (JS_IsException(val)) if (JS_IsException(val))
goto exception; goto exception;
@ -2913,7 +2913,7 @@ static JSValue js_os_exec(JSContext *ctx, JSValueConst this_val,
if (!envp) if (!envp)
goto exception; goto exception;
} }
val = JS_GetPropertyStr(ctx, options, "uid"); val = JS_GetPropertyStr(ctx, options, "uid");
if (JS_IsException(val)) if (JS_IsException(val))
goto exception; goto exception;
@ -3020,7 +3020,7 @@ static JSValue js_os_waitpid(JSContext *ctx, JSValueConst this_val,
{ {
int pid, status, options, ret; int pid, status, options, ret;
JSValue obj; JSValue obj;
if (JS_ToInt32(ctx, &pid, argv[0])) if (JS_ToInt32(ctx, &pid, argv[0]))
return JS_EXCEPTION; return JS_EXCEPTION;
if (JS_ToInt32(ctx, &options, argv[1])) if (JS_ToInt32(ctx, &options, argv[1]))
@ -3040,7 +3040,7 @@ static JSValue js_os_waitpid(JSContext *ctx, JSValueConst this_val,
JS_DefinePropertyValueUint32(ctx, obj, 1, JS_NewInt32(ctx, status), JS_DefinePropertyValueUint32(ctx, obj, 1, JS_NewInt32(ctx, status),
JS_PROP_C_W_E); JS_PROP_C_W_E);
return obj; return obj;
} }
/* pipe() -> [read_fd, write_fd] or null if error */ /* pipe() -> [read_fd, write_fd] or null if error */
static JSValue js_os_pipe(JSContext *ctx, JSValueConst this_val, static JSValue js_os_pipe(JSContext *ctx, JSValueConst this_val,
@ -3048,7 +3048,7 @@ static JSValue js_os_pipe(JSContext *ctx, JSValueConst this_val,
{ {
int pipe_fds[2], ret; int pipe_fds[2], ret;
JSValue obj; JSValue obj;
ret = pipe(pipe_fds); ret = pipe(pipe_fds);
if (ret < 0) if (ret < 0)
return JS_NULL; return JS_NULL;
@ -3067,7 +3067,7 @@ static JSValue js_os_kill(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv) int argc, JSValueConst *argv)
{ {
int pid, sig, ret; int pid, sig, ret;
if (JS_ToInt32(ctx, &pid, argv[0])) if (JS_ToInt32(ctx, &pid, argv[0]))
return JS_EXCEPTION; return JS_EXCEPTION;
if (JS_ToInt32(ctx, &sig, argv[1])) if (JS_ToInt32(ctx, &sig, argv[1]))
@ -3081,7 +3081,7 @@ static JSValue js_os_dup(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv) int argc, JSValueConst *argv)
{ {
int fd, ret; int fd, ret;
if (JS_ToInt32(ctx, &fd, argv[0])) if (JS_ToInt32(ctx, &fd, argv[0]))
return JS_EXCEPTION; return JS_EXCEPTION;
ret = js_get_errno(dup(fd)); ret = js_get_errno(dup(fd));
@ -3093,7 +3093,7 @@ static JSValue js_os_dup2(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv) int argc, JSValueConst *argv)
{ {
int fd, fd2, ret; int fd, fd2, ret;
if (JS_ToInt32(ctx, &fd, argv[0])) if (JS_ToInt32(ctx, &fd, argv[0]))
return JS_EXCEPTION; return JS_EXCEPTION;
if (JS_ToInt32(ctx, &fd2, argv[1])) if (JS_ToInt32(ctx, &fd2, argv[1]))
@ -3167,7 +3167,7 @@ static JSWorkerMessagePipe *js_new_message_pipe(void)
{ {
JSWorkerMessagePipe *ps; JSWorkerMessagePipe *ps;
int pipe_fds[2]; int pipe_fds[2];
if (pipe(pipe_fds) < 0) if (pipe(pipe_fds) < 0)
return NULL; return NULL;
@ -3208,10 +3208,10 @@ static void js_free_message_pipe(JSWorkerMessagePipe *ps)
struct list_head *el, *el1; struct list_head *el, *el1;
JSWorkerMessage *msg; JSWorkerMessage *msg;
int ref_count; int ref_count;
if (!ps) if (!ps)
return; return;
ref_count = atomic_add_int(&ps->ref_count, -1); ref_count = atomic_add_int(&ps->ref_count, -1);
assert(ref_count >= 0); assert(ref_count >= 0);
if (ref_count == 0) { if (ref_count == 0) {
@ -3250,7 +3250,7 @@ static void js_worker_finalizer(JSRuntime *rt, JSValue val)
static JSClassDef js_worker_class = { static JSClassDef js_worker_class = {
"Worker", "Worker",
.finalizer = js_worker_finalizer, .finalizer = js_worker_finalizer,
}; };
static void *worker_func(void *opaque) static void *worker_func(void *opaque)
{ {
@ -3258,12 +3258,12 @@ static void *worker_func(void *opaque)
JSRuntime *rt; JSRuntime *rt;
JSThreadState *ts; JSThreadState *ts;
JSContext *ctx; JSContext *ctx;
rt = JS_NewRuntime(); rt = JS_NewRuntime();
if (rt == NULL) { if (rt == NULL) {
fprintf(stderr, "JS_NewRuntime failure"); fprintf(stderr, "JS_NewRuntime failure");
exit(1); exit(1);
} }
js_std_init_handlers(rt); js_std_init_handlers(rt);
JS_SetModuleLoaderFunc(rt, NULL, js_module_loader, NULL); JS_SetModuleLoaderFunc(rt, NULL, js_module_loader, NULL);
@ -3272,7 +3272,7 @@ static void *worker_func(void *opaque)
ts = JS_GetRuntimeOpaque(rt); ts = JS_GetRuntimeOpaque(rt);
ts->recv_pipe = args->recv_pipe; ts->recv_pipe = args->recv_pipe;
ts->send_pipe = args->send_pipe; ts->send_pipe = args->send_pipe;
/* function pointer to avoid linking the whole JS_NewContext() if /* function pointer to avoid linking the whole JS_NewContext() if
not needed */ not needed */
ctx = js_worker_new_context_func(rt); ctx = js_worker_new_context_func(rt);
@ -3304,7 +3304,7 @@ static JSValue js_worker_ctor_internal(JSContext *ctx, JSValueConst new_target,
{ {
JSValue obj = JS_UNDEFINED, proto; JSValue obj = JS_UNDEFINED, proto;
JSWorkerData *s; JSWorkerData *s;
/* create the object */ /* create the object */
if (JS_IsUndefined(new_target)) { if (JS_IsUndefined(new_target)) {
proto = JS_GetClassProto(ctx, js_worker_class_id); proto = JS_GetClassProto(ctx, js_worker_class_id);
@ -3341,7 +3341,7 @@ static JSValue js_worker_ctor(JSContext *ctx, JSValueConst new_target,
int ret; int ret;
const char *filename = NULL, *basename; const char *filename = NULL, *basename;
JSAtom basename_atom; JSAtom basename_atom;
/* XXX: in order to avoid problems with resource liberation, we /* XXX: in order to avoid problems with resource liberation, we
don't support creating workers inside workers */ don't support creating workers inside workers */
if (!is_main_thread(rt)) if (!is_main_thread(rt))
@ -3357,7 +3357,7 @@ static JSValue js_worker_ctor(JSContext *ctx, JSValueConst new_target,
JS_FreeAtom(ctx, basename_atom); JS_FreeAtom(ctx, basename_atom);
if (!basename) if (!basename)
goto fail; goto fail;
/* module name */ /* module name */
filename = JS_ToCString(ctx, argv[0]); filename = JS_ToCString(ctx, argv[0]);
if (!filename) if (!filename)
@ -3382,7 +3382,7 @@ static JSValue js_worker_ctor(JSContext *ctx, JSValueConst new_target,
args->send_pipe, args->recv_pipe); args->send_pipe, args->recv_pipe);
if (JS_IsException(obj)) if (JS_IsException(obj))
goto fail; goto fail;
pthread_attr_init(&attr); pthread_attr_init(&attr);
/* no join at the end */ /* no join at the end */
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
@ -3420,10 +3420,10 @@ static JSValue js_worker_postMessage(JSContext *ctx, JSValueConst this_val,
uint8_t *data; uint8_t *data;
JSWorkerMessage *msg; JSWorkerMessage *msg;
uint8_t **sab_tab; uint8_t **sab_tab;
if (!worker) if (!worker)
return JS_EXCEPTION; return JS_EXCEPTION;
data = JS_WriteObject2(ctx, &data_len, argv[0], data = JS_WriteObject2(ctx, &data_len, argv[0],
JS_WRITE_OBJ_SAB | JS_WRITE_OBJ_REFERENCE, JS_WRITE_OBJ_SAB | JS_WRITE_OBJ_REFERENCE,
&sab_tab, &sab_tab_len); &sab_tab, &sab_tab_len);
@ -3452,7 +3452,7 @@ static JSValue js_worker_postMessage(JSContext *ctx, JSValueConst this_val,
js_free(ctx, data); js_free(ctx, data);
js_free(ctx, sab_tab); js_free(ctx, sab_tab);
/* increment the SAB reference counts */ /* increment the SAB reference counts */
for(i = 0; i < msg->sab_tab_len; i++) { for(i = 0; i < msg->sab_tab_len; i++) {
js_sab_dup(NULL, msg->sab_tab[i]); js_sab_dup(NULL, msg->sab_tab[i]);
@ -3484,7 +3484,7 @@ static JSValue js_worker_postMessage(JSContext *ctx, JSValueConst this_val,
js_free(ctx, data); js_free(ctx, data);
js_free(ctx, sab_tab); js_free(ctx, sab_tab);
return JS_EXCEPTION; return JS_EXCEPTION;
} }
static JSValue js_worker_set_onmessage(JSContext *ctx, JSValueConst this_val, static JSValue js_worker_set_onmessage(JSContext *ctx, JSValueConst this_val,
@ -3494,7 +3494,7 @@ static JSValue js_worker_set_onmessage(JSContext *ctx, JSValueConst this_val,
JSThreadState *ts = JS_GetRuntimeOpaque(rt); JSThreadState *ts = JS_GetRuntimeOpaque(rt);
JSWorkerData *worker = JS_GetOpaque2(ctx, this_val, js_worker_class_id); JSWorkerData *worker = JS_GetOpaque2(ctx, this_val, js_worker_class_id);
JSWorkerMessageHandler *port; JSWorkerMessageHandler *port;
if (!worker) if (!worker)
return JS_EXCEPTION; return JS_EXCEPTION;
@ -3647,7 +3647,7 @@ static const JSCFunctionListEntry js_os_funcs[] = {
static int js_os_init(JSContext *ctx, JSModuleDef *m) static int js_os_init(JSContext *ctx, JSModuleDef *m)
{ {
os_poll_func = js_os_poll; os_poll_func = js_os_poll;
/* OSTimer class */ /* OSTimer class */
JS_NewClassID(&js_os_timer_class_id); JS_NewClassID(&js_os_timer_class_id);
JS_NewClass(JS_GetRuntime(ctx), js_os_timer_class_id, &js_os_timer_class); JS_NewClass(JS_GetRuntime(ctx), js_os_timer_class_id, &js_os_timer_class);
@ -3662,20 +3662,20 @@ static int js_os_init(JSContext *ctx, JSModuleDef *m)
JS_NewClass(JS_GetRuntime(ctx), js_worker_class_id, &js_worker_class); JS_NewClass(JS_GetRuntime(ctx), js_worker_class_id, &js_worker_class);
proto = JS_NewObject(ctx); proto = JS_NewObject(ctx);
JS_SetPropertyFunctionList(ctx, proto, js_worker_proto_funcs, countof(js_worker_proto_funcs)); JS_SetPropertyFunctionList(ctx, proto, js_worker_proto_funcs, countof(js_worker_proto_funcs));
obj = JS_NewCFunction2(ctx, js_worker_ctor, "Worker", 1, obj = JS_NewCFunction2(ctx, js_worker_ctor, "Worker", 1,
JS_CFUNC_constructor, 0); JS_CFUNC_constructor, 0);
JS_SetConstructor(ctx, obj, proto); JS_SetConstructor(ctx, obj, proto);
JS_SetClassProto(ctx, js_worker_class_id, proto); JS_SetClassProto(ctx, js_worker_class_id, proto);
/* set 'Worker.parent' if necessary */ /* set 'Worker.parent' if necessary */
if (ts->recv_pipe && ts->send_pipe) { if (ts->recv_pipe && ts->send_pipe) {
JS_DefinePropertyValueStr(ctx, obj, "parent", JS_DefinePropertyValueStr(ctx, obj, "parent",
js_worker_ctor_internal(ctx, JS_UNDEFINED, ts->recv_pipe, ts->send_pipe), js_worker_ctor_internal(ctx, JS_UNDEFINED, ts->recv_pipe, ts->send_pipe),
JS_PROP_C_W_E); JS_PROP_C_W_E);
} }
JS_SetModuleExport(ctx, m, "Worker", obj); JS_SetModuleExport(ctx, m, "Worker", obj);
} }
#endif /* USE_WORKER */ #endif /* USE_WORKER */
@ -3740,12 +3740,12 @@ void js_std_add_helpers(JSContext *ctx, int argc, char **argv)
} }
JS_SetPropertyStr(ctx, global_obj, "scriptArgs", args); JS_SetPropertyStr(ctx, global_obj, "scriptArgs", args);
} }
JS_SetPropertyStr(ctx, global_obj, "print", JS_SetPropertyStr(ctx, global_obj, "print",
JS_NewCFunction(ctx, js_print, "print", 1)); JS_NewCFunction(ctx, js_print, "print", 1));
JS_SetPropertyStr(ctx, global_obj, "__loadScript", JS_SetPropertyStr(ctx, global_obj, "__loadScript",
JS_NewCFunction(ctx, js_loadScript, "__loadScript", 1)); JS_NewCFunction(ctx, js_loadScript, "__loadScript", 1));
JS_FreeValue(ctx, global_obj); JS_FreeValue(ctx, global_obj);
} }
@ -3793,7 +3793,7 @@ void js_std_free_handlers(JSRuntime *rt)
JSOSSignalHandler *sh = list_entry(el, JSOSSignalHandler, link); JSOSSignalHandler *sh = list_entry(el, JSOSSignalHandler, link);
free_sh(rt, sh); free_sh(rt, sh);
} }
list_for_each_safe(el, el1, &ts->os_timers) { list_for_each_safe(el, el1, &ts->os_timers) {
JSOSTimer *th = list_entry(el, JSOSTimer, link); JSOSTimer *th = list_entry(el, JSOSTimer, link);
unlink_timer(rt, th); unlink_timer(rt, th);
@ -3814,7 +3814,7 @@ void js_std_free_handlers(JSRuntime *rt)
static void js_dump_obj(JSContext *ctx, FILE *f, JSValueConst val) static void js_dump_obj(JSContext *ctx, FILE *f, JSValueConst val)
{ {
const char *str; const char *str;
str = JS_ToCString(ctx, val); str = JS_ToCString(ctx, val);
if (str) { if (str) {
fprintf(f, "%s\n", str); fprintf(f, "%s\n", str);
@ -3828,7 +3828,7 @@ static void js_std_dump_error1(JSContext *ctx, JSValueConst exception_val)
{ {
JSValue val; JSValue val;
BOOL is_error; BOOL is_error;
is_error = JS_IsError(ctx, exception_val); is_error = JS_IsError(ctx, exception_val);
js_dump_obj(ctx, stderr, exception_val); js_dump_obj(ctx, stderr, exception_val);
if (is_error) { if (is_error) {
@ -3843,7 +3843,7 @@ static void js_std_dump_error1(JSContext *ctx, JSValueConst exception_val)
void js_std_dump_error(JSContext *ctx) void js_std_dump_error(JSContext *ctx)
{ {
JSValue exception_val; JSValue exception_val;
exception_val = JS_GetException(ctx); exception_val = JS_GetException(ctx);
js_std_dump_error1(ctx, exception_val); js_std_dump_error1(ctx, exception_val);
JS_FreeValue(ctx, exception_val); JS_FreeValue(ctx, exception_val);

View file

@ -1,6 +1,6 @@
/* /*
* QuickJS C library * QuickJS C library
* *
* Copyright (c) 2017-2018 Fabrice Bellard * Copyright (c) 2017-2018 Fabrice Bellard
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
@ -51,7 +51,7 @@ void js_std_promise_rejection_tracker(JSContext *ctx, JSValueConst promise,
JSValueConst reason, JSValueConst reason,
JS_BOOL is_handled, void *opaque); JS_BOOL is_handled, void *opaque);
void js_std_set_worker_new_context_func(JSContext *(*func)(JSRuntime *rt)); void js_std_set_worker_new_context_func(JSContext *(*func)(JSRuntime *rt));
#ifdef __cplusplus #ifdef __cplusplus
} /* extern "C" { */ } /* extern "C" { */
#endif #endif

View file

@ -1,6 +1,6 @@
/* /*
* QuickJS opcode definitions * QuickJS opcode definitions
* *
* Copyright (c) 2017-2018 Fabrice Bellard * Copyright (c) 2017-2018 Fabrice Bellard
* Copyright (c) 2017-2018 Charlie Gordon * Copyright (c) 2017-2018 Charlie Gordon
* *
@ -165,14 +165,14 @@ DEF( set_loc, 3, 1, 1, loc) /* must come after put_loc */
DEF( get_arg, 3, 0, 1, arg) DEF( get_arg, 3, 0, 1, arg)
DEF( put_arg, 3, 1, 0, arg) /* must come after get_arg */ DEF( put_arg, 3, 1, 0, arg) /* must come after get_arg */
DEF( set_arg, 3, 1, 1, arg) /* must come after put_arg */ DEF( set_arg, 3, 1, 1, arg) /* must come after put_arg */
DEF( get_var_ref, 3, 0, 1, var_ref) DEF( get_var_ref, 3, 0, 1, var_ref)
DEF( put_var_ref, 3, 1, 0, var_ref) /* must come after get_var_ref */ DEF( put_var_ref, 3, 1, 0, var_ref) /* must come after get_var_ref */
DEF( set_var_ref, 3, 1, 1, var_ref) /* must come after put_var_ref */ DEF( set_var_ref, 3, 1, 1, var_ref) /* must come after put_var_ref */
DEF(set_loc_uninitialized, 3, 0, 0, loc) DEF(set_loc_uninitialized, 3, 0, 0, loc)
DEF( get_loc_check, 3, 0, 1, loc) DEF( get_loc_check, 3, 0, 1, loc)
DEF( put_loc_check, 3, 1, 0, loc) /* must come after get_loc_check */ DEF( put_loc_check, 3, 1, 0, loc) /* must come after get_loc_check */
DEF( put_loc_check_init, 3, 1, 0, loc) DEF( put_loc_check_init, 3, 1, 0, loc)
DEF(get_var_ref_check, 3, 0, 1, var_ref) DEF(get_var_ref_check, 3, 0, 1, var_ref)
DEF(put_var_ref_check, 3, 1, 0, var_ref) /* must come after get_var_ref_check */ DEF(put_var_ref_check, 3, 1, 0, var_ref) /* must come after get_var_ref_check */
DEF(put_var_ref_check_init, 3, 1, 0, var_ref) DEF(put_var_ref_check_init, 3, 1, 0, var_ref)
DEF( close_loc, 3, 0, 0, loc) DEF( close_loc, 3, 0, 0, loc)
@ -259,7 +259,7 @@ DEF( strict_eq, 1, 2, 1, none)
DEF( strict_neq, 1, 2, 1, none) DEF( strict_neq, 1, 2, 1, none)
DEF(is_undefined_or_null, 1, 1, 1, none) DEF(is_undefined_or_null, 1, 1, 1, none)
/* must be the last non short and non temporary opcode */ /* must be the last non short and non temporary opcode */
DEF( nop, 1, 0, 0, none) DEF( nop, 1, 0, 0, none)
/* temporary opcodes: never emitted in the final bytecode */ /* temporary opcodes: never emitted in the final bytecode */
@ -280,7 +280,7 @@ def(scope_get_private_field2, 7, 1, 2, atom_u16) /* obj -> obj value, emitted in
def(scope_put_private_field, 7, 1, 1, atom_u16) /* obj value ->, emitted in phase 1, removed in phase 2 */ def(scope_put_private_field, 7, 1, 1, atom_u16) /* obj value ->, emitted in phase 1, removed in phase 2 */
def( set_class_name, 5, 1, 1, u32) /* emitted in phase 1, removed in phase 2 */ def( set_class_name, 5, 1, 1, u32) /* emitted in phase 1, removed in phase 2 */
def( line_num, 5, 0, 0, u32) /* emitted in phase 1, removed in phase 3 */ def( line_num, 5, 0, 0, u32) /* emitted in phase 1, removed in phase 3 */
#if SHORT_OPCODES #if SHORT_OPCODES

560
quickjs.c

File diff suppressed because it is too large Load diff

View file

@ -124,7 +124,7 @@ static inline JS_BOOL JS_VALUE_IS_NAN(JSValue v)
{ {
return 0; return 0;
} }
#elif defined(JS_NAN_BOXING) #elif defined(JS_NAN_BOXING)
typedef uint64_t JSValue; typedef uint64_t JSValue;
@ -189,7 +189,7 @@ static inline JS_BOOL JS_VALUE_IS_NAN(JSValue v)
tag = JS_VALUE_GET_TAG(v); tag = JS_VALUE_GET_TAG(v);
return tag == (JS_NAN >> 32); return tag == (JS_NAN >> 32);
} }
#else /* !JS_NAN_BOXING */ #else /* !JS_NAN_BOXING */
typedef union JSValueUnion { typedef union JSValueUnion {
@ -916,7 +916,7 @@ static inline JSValue JS_NewCFunctionMagic(JSContext *ctx, JSCFunctionMagic *fun
{ {
return JS_NewCFunction2(ctx, (JSCFunction *)func, name, length, cproto, magic); return JS_NewCFunction2(ctx, (JSCFunction *)func, name, length, cproto, magic);
} }
void JS_SetConstructor(JSContext *ctx, JSValueConst func_obj, void JS_SetConstructor(JSContext *ctx, JSValueConst func_obj,
JSValueConst proto); JSValueConst proto);
/* C property definition */ /* C property definition */

View file

@ -1,6 +1,6 @@
/* /*
* ECMA Test 262 Runner for QuickJS * ECMA Test 262 Runner for QuickJS
* *
* Copyright (c) 2017-2021 Fabrice Bellard * Copyright (c) 2017-2021 Fabrice Bellard
* Copyright (c) 2017-2021 Charlie Gordon * Copyright (c) 2017-2021 Charlie Gordon
* *
@ -322,7 +322,7 @@ void namelist_load(namelist_t *lp, const char *filename)
char *p = str_strip(buf); char *p = str_strip(buf);
if (*p == '#' || *p == ';' || *p == '\0') if (*p == '#' || *p == ';' || *p == '\0')
continue; /* line comment */ continue; /* line comment */
namelist_add(lp, base_name, p); namelist_add(lp, base_name, p);
} }
free(base_name); free(base_name);
@ -458,11 +458,11 @@ static void *agent_start(void *arg)
JSContext *ctx; JSContext *ctx;
JSValue ret_val; JSValue ret_val;
int ret; int ret;
rt = JS_NewRuntime(); rt = JS_NewRuntime();
if (rt == NULL) { if (rt == NULL) {
fatal(1, "JS_NewRuntime failure"); fatal(1, "JS_NewRuntime failure");
} }
ctx = JS_NewContext(rt); ctx = JS_NewContext(rt);
if (ctx == NULL) { if (ctx == NULL) {
JS_FreeRuntime(rt); JS_FreeRuntime(rt);
@ -471,7 +471,7 @@ static void *agent_start(void *arg)
JS_SetContextOpaque(ctx, agent); JS_SetContextOpaque(ctx, agent);
JS_SetRuntimeInfo(rt, "agent"); JS_SetRuntimeInfo(rt, "agent");
JS_SetCanBlock(rt, TRUE); JS_SetCanBlock(rt, TRUE);
add_helpers(ctx); add_helpers(ctx);
ret_val = JS_Eval(ctx, agent->script, strlen(agent->script), ret_val = JS_Eval(ctx, agent->script, strlen(agent->script),
"<evalScript>", JS_EVAL_TYPE_GLOBAL); "<evalScript>", JS_EVAL_TYPE_GLOBAL);
@ -480,7 +480,7 @@ static void *agent_start(void *arg)
if (JS_IsException(ret_val)) if (JS_IsException(ret_val))
js_std_dump_error(ctx); js_std_dump_error(ctx);
JS_FreeValue(ctx, ret_val); JS_FreeValue(ctx, ret_val);
for(;;) { for(;;) {
JSContext *ctx1; JSContext *ctx1;
ret = JS_ExecutePendingJob(JS_GetRuntime(ctx), &ctx1); ret = JS_ExecutePendingJob(JS_GetRuntime(ctx), &ctx1);
@ -492,12 +492,12 @@ static void *agent_start(void *arg)
break; break;
} else { } else {
JSValue args[2]; JSValue args[2];
pthread_mutex_lock(&agent_mutex); pthread_mutex_lock(&agent_mutex);
while (!agent->broadcast_pending) { while (!agent->broadcast_pending) {
pthread_cond_wait(&agent_cond, &agent_mutex); pthread_cond_wait(&agent_cond, &agent_mutex);
} }
agent->broadcast_pending = FALSE; agent->broadcast_pending = FALSE;
pthread_cond_signal(&agent_cond); pthread_cond_signal(&agent_cond);
@ -534,7 +534,7 @@ static JSValue js_agent_start(JSContext *ctx, JSValue this_val,
if (JS_GetContextOpaque(ctx) != NULL) if (JS_GetContextOpaque(ctx) != NULL)
return JS_ThrowTypeError(ctx, "cannot be called inside an agent"); return JS_ThrowTypeError(ctx, "cannot be called inside an agent");
script = JS_ToCString(ctx, argv[0]); script = JS_ToCString(ctx, argv[0]);
if (!script) if (!script)
return JS_EXCEPTION; return JS_EXCEPTION;
@ -553,7 +553,7 @@ static void js_agent_free(JSContext *ctx)
{ {
struct list_head *el, *el1; struct list_head *el, *el1;
Test262Agent *agent; Test262Agent *agent;
list_for_each_safe(el, el1, &agent_list) { list_for_each_safe(el, el1, &agent_list) {
agent = list_entry(el, Test262Agent, link); agent = list_entry(el, Test262Agent, link);
pthread_join(agent->tid, NULL); pthread_join(agent->tid, NULL);
@ -562,7 +562,7 @@ static void js_agent_free(JSContext *ctx)
free(agent); free(agent);
} }
} }
static JSValue js_agent_leaving(JSContext *ctx, JSValue this_val, static JSValue js_agent_leaving(JSContext *ctx, JSValue this_val,
int argc, JSValue *argv) int argc, JSValue *argv)
{ {
@ -594,16 +594,16 @@ static JSValue js_agent_broadcast(JSContext *ctx, JSValue this_val,
uint8_t *buf; uint8_t *buf;
size_t buf_size; size_t buf_size;
int32_t val; int32_t val;
if (JS_GetContextOpaque(ctx) != NULL) if (JS_GetContextOpaque(ctx) != NULL)
return JS_ThrowTypeError(ctx, "cannot be called inside an agent"); return JS_ThrowTypeError(ctx, "cannot be called inside an agent");
buf = JS_GetArrayBuffer(ctx, &buf_size, sab); buf = JS_GetArrayBuffer(ctx, &buf_size, sab);
if (!buf) if (!buf)
return JS_EXCEPTION; return JS_EXCEPTION;
if (JS_ToInt32(ctx, &val, argv[1])) if (JS_ToInt32(ctx, &val, argv[1]))
return JS_EXCEPTION; return JS_EXCEPTION;
/* broadcast the values and wait until all agents have started /* broadcast the values and wait until all agents have started
calling their callbacks */ calling their callbacks */
pthread_mutex_lock(&agent_mutex); pthread_mutex_lock(&agent_mutex);
@ -698,7 +698,7 @@ static JSValue js_agent_report(JSContext *ctx, JSValue this_val,
rep = malloc(sizeof(*rep)); rep = malloc(sizeof(*rep));
rep->str = strdup(str); rep->str = strdup(str);
JS_FreeCString(ctx, str); JS_FreeCString(ctx, str);
pthread_mutex_lock(&report_mutex); pthread_mutex_lock(&report_mutex);
list_add_tail(&rep->link, &report_list); list_add_tail(&rep->link, &report_list);
pthread_mutex_unlock(&report_mutex); pthread_mutex_unlock(&report_mutex);
@ -718,7 +718,7 @@ static const JSCFunctionListEntry js_agent_funcs[] = {
JS_CFUNC_DEF("sleep", 1, js_agent_sleep ), JS_CFUNC_DEF("sleep", 1, js_agent_sleep ),
JS_CFUNC_DEF("monotonicNow", 0, js_agent_monotonicNow ), JS_CFUNC_DEF("monotonicNow", 0, js_agent_monotonicNow ),
}; };
static JSValue js_new_agent(JSContext *ctx) static JSValue js_new_agent(JSContext *ctx)
{ {
JSValue agent; JSValue agent;
@ -734,7 +734,7 @@ static JSValue js_createRealm(JSContext *ctx, JSValue this_val,
{ {
JSContext *ctx1; JSContext *ctx1;
JSValue ret; JSValue ret;
ctx1 = JS_NewContext(JS_GetRuntime(ctx)); ctx1 = JS_NewContext(JS_GetRuntime(ctx));
if (!ctx1) if (!ctx1)
return JS_ThrowOutOfMemory(ctx); return JS_ThrowOutOfMemory(ctx);
@ -754,7 +754,7 @@ static JSValue add_helpers1(JSContext *ctx)
{ {
JSValue global_obj; JSValue global_obj;
JSValue obj262, obj; JSValue obj262, obj;
global_obj = JS_GetGlobalObject(ctx); global_obj = JS_GetGlobalObject(ctx);
JS_SetPropertyStr(ctx, global_obj, "print", JS_SetPropertyStr(ctx, global_obj, "print",
@ -785,7 +785,7 @@ static JSValue add_helpers1(JSContext *ctx)
JS_SetPropertyStr(ctx, obj262, "IsHTMLDDA", obj); JS_SetPropertyStr(ctx, obj262, "IsHTMLDDA", obj);
JS_SetPropertyStr(ctx, global_obj, "$262", JS_DupValue(ctx, obj262)); JS_SetPropertyStr(ctx, global_obj, "$262", JS_DupValue(ctx, obj262));
JS_FreeValue(ctx, global_obj); JS_FreeValue(ctx, global_obj);
return obj262; return obj262;
} }
@ -814,14 +814,14 @@ static JSModuleDef *js_module_loader_test(JSContext *ctx,
uint8_t *buf; uint8_t *buf;
JSModuleDef *m; JSModuleDef *m;
JSValue func_val; JSValue func_val;
buf = js_load_file(ctx, &buf_len, module_name); buf = js_load_file(ctx, &buf_len, module_name);
if (!buf) { if (!buf) {
JS_ThrowReferenceError(ctx, "could not load module filename '%s'", JS_ThrowReferenceError(ctx, "could not load module filename '%s'",
module_name); module_name);
return NULL; return NULL;
} }
/* compile the module */ /* compile the module */
func_val = JS_Eval(ctx, (char *)buf, buf_len, module_name, func_val = JS_Eval(ctx, (char *)buf, buf_len, module_name,
JS_EVAL_TYPE_MODULE | JS_EVAL_FLAG_COMPILE_ONLY); JS_EVAL_TYPE_MODULE | JS_EVAL_FLAG_COMPILE_ONLY);
@ -930,14 +930,14 @@ void load_config(const char *filename)
perror_exit(1, filename); perror_exit(1, filename);
} }
base_name = get_basename(filename); base_name = get_basename(filename);
while (fgets(buf, sizeof(buf), f) != NULL) { while (fgets(buf, sizeof(buf), f) != NULL) {
char *p, *q; char *p, *q;
lineno++; lineno++;
p = str_strip(buf); p = str_strip(buf);
if (*p == '#' || *p == ';' || *p == '\0') if (*p == '#' || *p == ';' || *p == '\0')
continue; /* line comment */ continue; /* line comment */
if (*p == "[]"[0]) { if (*p == "[]"[0]) {
/* new section */ /* new section */
p++; p++;
@ -1003,7 +1003,7 @@ void load_config(const char *filename)
test_mode = TEST_STRICT; test_mode = TEST_STRICT;
else if (str_equal(q, "all") || str_equal(q, "both")) else if (str_equal(q, "all") || str_equal(q, "both"))
test_mode = TEST_ALL; test_mode = TEST_ALL;
else else
fatal(2, "unknown test mode: %s", q); fatal(2, "unknown test mode: %s", q);
continue; continue;
} }
@ -1144,7 +1144,7 @@ int longest_match(const char *str, const char *find, int pos, int *ppos, int lin
int len, maxlen; int len, maxlen;
maxlen = 0; maxlen = 0;
if (*find) { if (*find) {
const char *p; const char *p;
for (p = str + pos; *p; p++) { for (p = str + pos; *p; p++) {
@ -1177,7 +1177,7 @@ static int eval_buf(JSContext *ctx, const char *buf, size_t buf_len,
int ret, error_line, pos, pos_line; int ret, error_line, pos, pos_line;
BOOL is_error, has_error_line; BOOL is_error, has_error_line;
const char *error_name; const char *error_name;
pos = skip_comments(buf, 1, &pos_line); pos = skip_comments(buf, 1, &pos_line);
error_line = pos_line; error_line = pos_line;
has_error_line = FALSE; has_error_line = FALSE;
@ -1244,7 +1244,7 @@ static int eval_buf(JSContext *ctx, const char *buf, size_t buf_len,
if (is_error) { if (is_error) {
JSValue name, stack; JSValue name, stack;
const char *stack_str; const char *stack_str;
name = JS_GetPropertyStr(ctx, exception_val, "name"); name = JS_GetPropertyStr(ctx, exception_val, "name");
error_name = JS_ToCString(ctx, name); error_name = JS_ToCString(ctx, name);
stack = JS_GetPropertyStr(ctx, exception_val, "stack"); stack = JS_GetPropertyStr(ctx, exception_val, "stack");
@ -1253,10 +1253,10 @@ static int eval_buf(JSContext *ctx, const char *buf, size_t buf_len,
if (stack_str) { if (stack_str) {
const char *p; const char *p;
int len; int len;
if (outfile) if (outfile)
fprintf(outfile, "%s", stack_str); fprintf(outfile, "%s", stack_str);
len = strlen(filename); len = strlen(filename);
p = strstr(stack_str, filename); p = strstr(stack_str, filename);
if (p != NULL && p[len] == ':') { if (p != NULL && p[len] == ':') {
@ -1274,7 +1274,7 @@ static int eval_buf(JSContext *ctx, const char *buf, size_t buf_len,
if (error_type) { if (error_type) {
char *error_class; char *error_class;
const char *msg; const char *msg;
msg = JS_ToCString(ctx, exception_val); msg = JS_ToCString(ctx, exception_val);
error_class = strdup_len(msg, strcspn(msg, ":")); error_class = strdup_len(msg, strcspn(msg, ":"));
if (!str_equal(error_class, error_type)) if (!str_equal(error_class, error_type))
@ -1398,7 +1398,7 @@ char *extract_desc(const char *buf, char style)
const char *p, *desc_start; const char *p, *desc_start;
char *desc; char *desc;
int len; int len;
p = buf; p = buf;
while (*p != '\0') { while (*p != '\0') {
if (p[0] == '/' && p[1] == '*' && p[2] == style && p[3] != '/') { if (p[0] == '/' && p[1] == '*' && p[2] == style && p[3] != '/') {
@ -1530,11 +1530,11 @@ int run_test_buf(const char *filename, char *harness, namelist_t *ip,
JSRuntime *rt; JSRuntime *rt;
JSContext *ctx; JSContext *ctx;
int i, ret; int i, ret;
rt = JS_NewRuntime(); rt = JS_NewRuntime();
if (rt == NULL) { if (rt == NULL) {
fatal(1, "JS_NewRuntime failure"); fatal(1, "JS_NewRuntime failure");
} }
ctx = JS_NewContext(rt); ctx = JS_NewContext(rt);
if (ctx == NULL) { if (ctx == NULL) {
JS_FreeRuntime(rt); JS_FreeRuntime(rt);
@ -1543,10 +1543,10 @@ int run_test_buf(const char *filename, char *harness, namelist_t *ip,
JS_SetRuntimeInfo(rt, filename); JS_SetRuntimeInfo(rt, filename);
JS_SetCanBlock(rt, can_block); JS_SetCanBlock(rt, can_block);
/* loader for ES6 modules */ /* loader for ES6 modules */
JS_SetModuleLoaderFunc(rt, NULL, js_module_loader_test, NULL); JS_SetModuleLoaderFunc(rt, NULL, js_module_loader_test, NULL);
add_helpers(ctx); add_helpers(ctx);
for (i = 0; i < ip->count; i++) { for (i = 0; i < ip->count; i++) {
@ -1558,7 +1558,7 @@ int run_test_buf(const char *filename, char *harness, namelist_t *ip,
ret = eval_buf(ctx, buf, buf_len, filename, TRUE, is_negative, ret = eval_buf(ctx, buf, buf_len, filename, TRUE, is_negative,
error_type, outfile, eval_flags, is_async); error_type, outfile, eval_flags, is_async);
ret = (ret != 0); ret = (ret != 0);
if (dump_memory) { if (dump_memory) {
update_stats(rt, filename); update_stats(rt, filename);
} }
@ -1591,7 +1591,7 @@ int run_test(const char *filename, int index)
BOOL is_negative, is_nostrict, is_onlystrict, is_async, is_module, skip; BOOL is_negative, is_nostrict, is_onlystrict, is_async, is_module, skip;
BOOL can_block; BOOL can_block;
namelist_t include_list = { 0 }, *ip = &include_list; namelist_t include_list = { 0 }, *ip = &include_list;
is_nostrict = is_onlystrict = is_negative = is_async = is_module = skip = FALSE; is_nostrict = is_onlystrict = is_negative = is_async = is_module = skip = FALSE;
can_block = TRUE; can_block = TRUE;
error_type = NULL; error_type = NULL;
@ -1823,13 +1823,13 @@ int run_test262_harness_test(const char *filename, BOOL is_module)
int eval_flags, ret_code, ret; int eval_flags, ret_code, ret;
JSValue res_val; JSValue res_val;
BOOL can_block; BOOL can_block;
outfile = stdout; /* for js_print */ outfile = stdout; /* for js_print */
rt = JS_NewRuntime(); rt = JS_NewRuntime();
if (rt == NULL) { if (rt == NULL) {
fatal(1, "JS_NewRuntime failure"); fatal(1, "JS_NewRuntime failure");
} }
ctx = JS_NewContext(rt); ctx = JS_NewContext(rt);
if (ctx == NULL) { if (ctx == NULL) {
JS_FreeRuntime(rt); JS_FreeRuntime(rt);
@ -1839,10 +1839,10 @@ int run_test262_harness_test(const char *filename, BOOL is_module)
can_block = TRUE; can_block = TRUE;
JS_SetCanBlock(rt, can_block); JS_SetCanBlock(rt, can_block);
/* loader for ES6 modules */ /* loader for ES6 modules */
JS_SetModuleLoaderFunc(rt, NULL, js_module_loader_test, NULL); JS_SetModuleLoaderFunc(rt, NULL, js_module_loader_test, NULL);
add_helpers(ctx); add_helpers(ctx);
buf = load_file(filename, &buf_len); buf = load_file(filename, &buf_len);
@ -2038,14 +2038,14 @@ int main(int argc, char **argv)
break; break;
} }
} }
if (optind >= argc && !test_list.count) if (optind >= argc && !test_list.count)
help(); help();
if (is_test262_harness) { if (is_test262_harness) {
return run_test262_harness_test(argv[optind], is_module); return run_test262_harness_test(argv[optind], is_module);
} }
error_out = stdout; error_out = stdout;
if (error_filename) { if (error_filename) {
error_file = load_file(error_filename, NULL); error_file = load_file(error_filename, NULL);

View file

@ -1,6 +1,6 @@
/* /*
* QuickJS: binary JSON module (test only) * QuickJS: binary JSON module (test only)
* *
* Copyright (c) 2017-2019 Fabrice Bellard * Copyright (c) 2017-2019 Fabrice Bellard
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
@ -32,7 +32,7 @@ static JSValue js_bjson_read(JSContext *ctx, JSValueConst this_val,
JSValue obj; JSValue obj;
size_t size; size_t size;
int flags; int flags;
if (JS_ToIndex(ctx, &pos, argv[1])) if (JS_ToIndex(ctx, &pos, argv[1]))
return JS_EXCEPTION; return JS_EXCEPTION;
if (JS_ToIndex(ctx, &len, argv[2])) if (JS_ToIndex(ctx, &len, argv[2]))
@ -56,7 +56,7 @@ static JSValue js_bjson_write(JSContext *ctx, JSValueConst this_val,
uint8_t *buf; uint8_t *buf;
JSValue array; JSValue array;
int flags; int flags;
flags = 0; flags = 0;
if (JS_ToBool(ctx, argv[1])) if (JS_ToBool(ctx, argv[1]))
flags |= JS_WRITE_OBJ_REFERENCE; flags |= JS_WRITE_OBJ_REFERENCE;

View file

@ -60,7 +60,7 @@ function toPrec(n, prec) {
s = s.substring(0, i) + "." + s.substring(i); s = s.substring(0, i) + "." + s.substring(i);
return s; return s;
} }
var ref_data; var ref_data;
var log_data; var log_data;
@ -972,13 +972,13 @@ function main(argc, argv, g)
]; ];
var tests = []; var tests = [];
var i, j, n, f, name; var i, j, n, f, name;
if (typeof BigInt == "function") { if (typeof BigInt == "function") {
/* BigInt test */ /* BigInt test */
test_list.push(bigint64_arith); test_list.push(bigint64_arith);
test_list.push(bigint256_arith); test_list.push(bigint256_arith);
} }
for (i = 1; i < argc;) { for (i = 1; i < argc;) {
name = argv[i++]; name = argv[i++];
if (name == "-a") { if (name == "-a") {
@ -1028,7 +1028,7 @@ function main(argc, argv, g)
log_line("total", "", total[2], total[3], total_score * 100 / total_scale); log_line("total", "", total[2], total[3], total_score * 100 / total_scale);
else else
log_line("total", "", total[2]); log_line("total", "", total[2]);
if (tests == test_list) if (tests == test_list)
save_result("microbench-new.txt", log_data); save_result("microbench-new.txt", log_data);
} }

View file

@ -94,7 +94,7 @@ function test_bigint1()
r = 1n << 31n; r = 1n << 31n;
assert(r, 2147483648n, "1 << 31n === 2147483648n"); assert(r, 2147483648n, "1 << 31n === 2147483648n");
r = 1n << 32n; r = 1n << 32n;
assert(r, 4294967296n, "1 << 32n === 4294967296n"); assert(r, 4294967296n, "1 << 32n === 4294967296n");
} }

View file

@ -34,7 +34,7 @@ function toHex(a)
function isArrayLike(a) function isArrayLike(a)
{ {
return Array.isArray(a) || return Array.isArray(a) ||
(a instanceof Uint8ClampedArray) || (a instanceof Uint8ClampedArray) ||
(a instanceof Uint8Array) || (a instanceof Uint8Array) ||
(a instanceof Uint16Array) || (a instanceof Uint16Array) ||
@ -146,7 +146,7 @@ function bjson_test_reference()
function bjson_test_all() function bjson_test_all()
{ {
var obj; var obj;
bjson_test({x:1, y:2, if:3}); bjson_test({x:1, y:2, if:3});
bjson_test([1, 2, 3]); bjson_test([1, 2, 3]);
bjson_test([1.0, "aa", true, false, undefined, null, NaN, -Infinity, -0.0]); bjson_test([1.0, "aa", true, false, undefined, null, NaN, -Infinity, -0.0]);
@ -159,7 +159,7 @@ function bjson_test_all()
bjson_test(new Int32Array([123123, 222111, -32222])); bjson_test(new Int32Array([123123, 222111, -32222]));
bjson_test(new Float64Array([123123, 222111.5])); bjson_test(new Float64Array([123123, 222111.5]));
/* tested with a circular reference */ /* tested with a circular reference */
obj = {}; obj = {};
obj.x = obj; obj.x = obj;

View file

@ -58,7 +58,7 @@ function test_function()
} }
var r, g; var r, g;
r = my_func.call(null, 1, 2); r = my_func.call(null, 1, 2);
assert(r, 3, "call"); assert(r, 3, "call");
@ -71,10 +71,10 @@ function test_function()
assert_throws(TypeError, (function() { assert_throws(TypeError, (function() {
Reflect.apply((function () { return 1; }), null, undefined); Reflect.apply((function () { return 1; }), null, undefined);
})); }));
r = new Function("a", "b", "return a + b;"); r = new Function("a", "b", "return a + b;");
assert(r(2,3), 5, "function"); assert(r(2,3), 5, "function");
g = f.bind(1, 2); g = f.bind(1, 2);
assert(g.length, 1); assert(g.length, 1);
assert(g.name, "bound f"); assert(g.name, "bound f");
@ -104,7 +104,7 @@ function test()
assert(a.z, 4, "get"); assert(a.z, 4, "get");
a.z = 5; a.z = 5;
assert(a.z_val, 5, "set"); assert(a.z_val, 5, "set");
a = { get z() { return 4; }, set z(val) { this.z_val = val; } }; a = { get z() { return 4; }, set z(val) { this.z_val = val; } };
assert(a.z, 4, "get"); assert(a.z, 4, "get");
a.z = 5; a.z = 5;
@ -208,7 +208,7 @@ function test_string()
assert(a.charAt(1), "b"); assert(a.charAt(1), "b");
assert(a.charAt(-1), ""); assert(a.charAt(-1), "");
assert(a.charAt(3), ""); assert(a.charAt(3), "");
a = "abcd"; a = "abcd";
assert(a.substring(1, 3), "bc", "substring"); assert(a.substring(1, 3), "bc", "substring");
a = String.fromCharCode(0x20ac); a = String.fromCharCode(0x20ac);
@ -217,7 +217,7 @@ function test_string()
assert(a, "\u20ac", "unicode"); assert(a, "\u20ac", "unicode");
assert(a, "\u{20ac}", "unicode"); assert(a, "\u{20ac}", "unicode");
assert("a", "\x61", "unicode"); assert("a", "\x61", "unicode");
a = "\u{10ffff}"; a = "\u{10ffff}";
assert(a.length, 2, "unicode"); assert(a.length, 2, "unicode");
assert(a, "\u{dbff}\u{dfff}", "unicode"); assert(a, "\u{dbff}\u{dfff}", "unicode");
@ -393,7 +393,7 @@ function test_eval()
assert(eval("if (0) 2; else 3;"), 3); assert(eval("if (0) 2; else 3;"), 3);
assert(f.call(1, "this"), 1); assert(f.call(1, "this"), 1);
a = 2; a = 2;
assert(eval("a"), 2); assert(eval("a"), 2);
@ -438,7 +438,7 @@ function test_typed_array()
a[2] = 0.5; a[2] = 0.5;
a[3] = 1233.5; a[3] = 1233.5;
assert(a.toString(), "0,2,0,255"); assert(a.toString(), "0,2,0,255");
buffer = new ArrayBuffer(16); buffer = new ArrayBuffer(16);
assert(buffer.byteLength, 16); assert(buffer.byteLength, 16);
a = new Uint32Array(buffer, 12, 1); a = new Uint32Array(buffer, 12, 1);
@ -450,7 +450,7 @@ function test_typed_array()
a = new Float32Array(buffer, 8, 1); a = new Float32Array(buffer, 8, 1);
a[0] = 1; a[0] = 1;
a = new Uint8Array(buffer); a = new Uint8Array(buffer);
str = a.toString(); str = a.toString();
@ -539,7 +539,7 @@ function test_regexp()
a = /(\.(?!com|org)|\/)/.exec("ah.com"); a = /(\.(?!com|org)|\/)/.exec("ah.com");
assert(a, null); assert(a, null);
a = /(?=(a+))/.exec("baaabac"); a = /(?=(a+))/.exec("baaabac");
assert(a.index === 1 && a[0] === "" && a[1] === "aaa"); assert(a.index === 1 && a[0] === "" && a[1] === "aaa");
@ -606,7 +606,7 @@ function test_map()
} }
i = 0; i = 0;
a.forEach(function (v, o) { a.forEach(function (v, o) {
assert(o, tab[i++][0]); assert(o, tab[i++][0]);
assert(a.has(o)); assert(a.has(o));
assert(a.delete(o)); assert(a.delete(o));
@ -629,7 +629,7 @@ function test_weak_map()
a.set(o, v); a.set(o, v);
} }
o = null; o = null;
n2 = n >> 1; n2 = n >> 1;
for(i = 0; i < n2; i++) { for(i = 0; i < n2; i++) {
a.delete(tab[i][0]); a.delete(tab[i][0]);

View file

@ -54,7 +54,7 @@ function test_closure1()
function f2() function f2()
{ {
var val = 1; var val = 1;
function set(a) { function set(a) {
val = a; val = a;
} }
@ -63,7 +63,7 @@ function test_closure1()
} }
return { "set": set, "get": get }; return { "set": set, "get": get };
} }
var obj = f2(); var obj = f2();
obj.set(10); obj.set(10);
var r; var r;

View file

@ -68,10 +68,10 @@ function test_op1()
r = 1 << 31; r = 1 << 31;
assert(r, -2147483648, "1 << 31 === -2147483648"); assert(r, -2147483648, "1 << 31 === -2147483648");
r = 1 << 32; r = 1 << 32;
assert(r, 1, "1 << 32 === 1"); assert(r, 1, "1 << 32 === 1");
r = (1 << 31) < 0; r = (1 << 31) < 0;
assert(r, true, "(1 << 31) < 0 === true"); assert(r, true, "(1 << 31) < 0 === true");
@ -113,7 +113,7 @@ function test_cvt()
assert(("12345" | 0) === 12345); assert(("12345" | 0) === 12345);
assert(("0x12345" | 0) === 0x12345); assert(("0x12345" | 0) === 0x12345);
assert(((4294967296 * 3 - 4) | 0) === -4); assert(((4294967296 * 3 - 4) | 0) === -4);
assert(("12345" >>> 0) === 12345); assert(("12345" >>> 0) === 12345);
assert(("0x12345" >>> 0) === 0x12345); assert(("0x12345" >>> 0) === 0x12345);
assert((NaN >>> 0) === 0); assert((NaN >>> 0) === 0);
@ -140,7 +140,7 @@ function test_eq()
function test_inc_dec() function test_inc_dec()
{ {
var a, r; var a, r;
a = 1; a = 1;
r = a++; r = a++;
assert(r === 1 && a === 2, true, "++"); assert(r === 1 && a === 2, true, "++");
@ -168,19 +168,19 @@ function test_inc_dec()
a = [true]; a = [true];
a[0]++; a[0]++;
assert(a[0], 2, "++"); assert(a[0], 2, "++");
a = {x:true}; a = {x:true};
r = a.x++; r = a.x++;
assert(r === 1 && a.x === 2, true, "++"); assert(r === 1 && a.x === 2, true, "++");
a = {x:true}; a = {x:true};
r = a.x--; r = a.x--;
assert(r === 1 && a.x === 0, true, "--"); assert(r === 1 && a.x === 0, true, "--");
a = [true]; a = [true];
r = a[0]++; r = a[0]++;
assert(r === 1 && a[0] === 2, true, "++"); assert(r === 1 && a[0] === 2, true, "++");
a = [true]; a = [true];
r = a[0]--; r = a[0]--;
assert(r === 1 && a[0] === 0, true, "--"); assert(r === 1 && a[0] === 0, true, "--");
@ -212,7 +212,7 @@ function test_op2()
assert((typeof Object), "function", "typeof"); assert((typeof Object), "function", "typeof");
assert((typeof null), "object", "typeof"); assert((typeof null), "object", "typeof");
assert((typeof unknown_var), "undefined", "typeof"); assert((typeof unknown_var), "undefined", "typeof");
a = {x: 1, if: 2, async: 3}; a = {x: 1, if: 2, async: 3};
assert(a.if === 2); assert(a.if === 2);
assert(a.async === 3); assert(a.async === 3);
@ -225,7 +225,7 @@ function test_delete()
a = {x: 1, y: 1}; a = {x: 1, y: 1};
assert((delete a.x), true, "delete"); assert((delete a.x), true, "delete");
assert(("x" in a), false, "delete"); assert(("x" in a), false, "delete");
/* the following are not tested by test262 */ /* the following are not tested by test262 */
assert(delete "abc"[100], true); assert(delete "abc"[100], true);
@ -310,7 +310,7 @@ function test_class()
o = new C(); o = new C();
assert(o.f() === 1); assert(o.f() === 1);
assert(o.x === 10); assert(o.x === 10);
assert(D.F() === -1); assert(D.F() === -1);
assert(D.G() === -2); assert(D.G() === -2);
assert(D.H() === -1); assert(D.H() === -1);
@ -364,7 +364,7 @@ function test_regexp_skip()
var a, b; var a, b;
[a, b = /abc\(/] = [1]; [a, b = /abc\(/] = [1];
assert(a === 1); assert(a === 1);
[a, b =/abc\(/] = [2]; [a, b =/abc\(/] = [2];
assert(a === 2); assert(a === 2);
} }
@ -409,7 +409,7 @@ function test_argument_scope()
{ {
var f; var f;
var c = "global"; var c = "global";
f = function(a = eval("var arguments")) {}; f = function(a = eval("var arguments")) {};
assert_throws(SyntaxError, f); assert_throws(SyntaxError, f);
@ -480,7 +480,7 @@ function test_function_expr_name()
/* non strict mode test : assignment to the function name silently /* non strict mode test : assignment to the function name silently
fails */ fails */
f = function myfunc() { f = function myfunc() {
myfunc = 1; myfunc = 1;
return myfunc; return myfunc;
@ -501,7 +501,7 @@ function test_function_expr_name()
return myfunc; return myfunc;
}; };
assert(f(), f); assert(f(), f);
/* strict mode test : assignment to the function name raises a /* strict mode test : assignment to the function name raises a
TypeError exception */ TypeError exception */

View file

@ -333,7 +333,7 @@ function test_try_catch7()
function test_try_catch8() function test_try_catch8()
{ {
var i, s; var i, s;
s = ""; s = "";
for(var i in {x:1, y:2}) { for(var i in {x:1, y:2}) {
try { try {

View file

@ -47,7 +47,7 @@ function test_file1()
f.seek(0, std.SEEK_SET); f.seek(0, std.SEEK_SET);
str1 = f.readAsString(); str1 = f.readAsString();
assert(str1 === str); assert(str1 === str);
f.seek(0, std.SEEK_END); f.seek(0, std.SEEK_END);
size = f.tell(); size = f.tell();
assert(size === str.length); assert(size === str.length);
@ -82,7 +82,7 @@ function test_file2()
function test_getline() function test_getline()
{ {
var f, line, line_count, lines, i; var f, line, line_count, lines, i;
lines = ["hello world", "line 1", "line 2" ]; lines = ["hello world", "line 1", "line 2" ];
f = std.tmpfile(); f = std.tmpfile();
for(i = 0; i < lines.length; i++) { for(i = 0; i < lines.length; i++) {
@ -116,7 +116,7 @@ function test_popen()
/* test loadFile */ /* test loadFile */
assert(std.loadFile(fname), content); assert(std.loadFile(fname), content);
/* execute the 'cat' shell command */ /* execute the 'cat' shell command */
f = std.popen("cat " + fname, "r"); f = std.popen("cat " + fname, "r");
str = f.readAsString(); str = f.readAsString();
@ -138,17 +138,17 @@ function test_os()
fname = "tmp_file.txt"; fname = "tmp_file.txt";
fpath = fdir + "/" + fname; fpath = fdir + "/" + fname;
link_path = fdir + "/test_link"; link_path = fdir + "/test_link";
os.remove(link_path); os.remove(link_path);
os.remove(fpath); os.remove(fpath);
os.remove(fdir); os.remove(fdir);
err = os.mkdir(fdir, 0o755); err = os.mkdir(fdir, 0o755);
assert(err === 0); assert(err === 0);
fd = os.open(fpath, os.O_RDWR | os.O_CREAT | os.O_TRUNC); fd = os.open(fpath, os.O_RDWR | os.O_CREAT | os.O_TRUNC);
assert(fd >= 0); assert(fd >= 0);
buf = new Uint8Array(10); buf = new Uint8Array(10);
for(i = 0; i < buf.length; i++) for(i = 0; i < buf.length; i++)
buf[i] = i; buf[i] = i;
@ -157,16 +157,16 @@ function test_os()
assert(os.seek(fd, 0, std.SEEK_SET) === 0); assert(os.seek(fd, 0, std.SEEK_SET) === 0);
buf2 = new Uint8Array(buf.length); buf2 = new Uint8Array(buf.length);
assert(os.read(fd, buf2.buffer, 0, buf2.length) === buf2.length); assert(os.read(fd, buf2.buffer, 0, buf2.length) === buf2.length);
for(i = 0; i < buf.length; i++) for(i = 0; i < buf.length; i++)
assert(buf[i] == buf2[i]); assert(buf[i] == buf2[i]);
if (typeof BigInt !== "undefined") { if (typeof BigInt !== "undefined") {
assert(os.seek(fd, BigInt(6), std.SEEK_SET), BigInt(6)); assert(os.seek(fd, BigInt(6), std.SEEK_SET), BigInt(6));
assert(os.read(fd, buf2.buffer, 0, 1) === 1); assert(os.read(fd, buf2.buffer, 0, 1) === 1);
assert(buf[6] == buf2[0]); assert(buf[6] == buf2[0]);
} }
assert(os.close(fd) === 0); assert(os.close(fd) === 0);
[files, err] = os.readdir(fdir); [files, err] = os.readdir(fdir);
@ -177,7 +177,7 @@ function test_os()
err = os.utimes(fpath, fdate, fdate); err = os.utimes(fpath, fdate, fdate);
assert(err, 0); assert(err, 0);
[st, err] = os.stat(fpath); [st, err] = os.stat(fpath);
assert(err, 0); assert(err, 0);
assert(st.mode & os.S_IFMT, os.S_IFREG); assert(st.mode & os.S_IFMT, os.S_IFREG);
@ -205,7 +205,7 @@ function test_os()
assert(err, 0); assert(err, 0);
assert(buf, buf2); assert(buf, buf2);
assert(os.remove(fpath) === 0); assert(os.remove(fpath) === 0);
fd = os.open(fpath, os.O_RDONLY); fd = os.open(fpath, os.O_RDONLY);
@ -223,7 +223,7 @@ function test_os_exec()
ret = os.exec(["/bin/sh", "-c", "exit 1"], { usePath: false }); ret = os.exec(["/bin/sh", "-c", "exit 1"], { usePath: false });
assert(ret, 1); assert(ret, 1);
fds = os.pipe(); fds = os.pipe();
pid = os.exec(["sh", "-c", "echo $FOO"], { pid = os.exec(["sh", "-c", "echo $FOO"], {
stdout: fds[1], stdout: fds[1],

View file

@ -21,10 +21,10 @@ function handle_msg(e) {
function worker_main() { function worker_main() {
var i; var i;
parent.onmessage = handle_msg; parent.onmessage = handle_msg;
for(i = 0; i < 10; i++) { for(i = 0; i < 10; i++) {
parent.postMessage({ type: "num", num: i }); parent.postMessage({ type: "num", num: i });
} }
} }

View file

@ -1,6 +1,6 @@
/* /*
* Generation of Unicode tables * Generation of Unicode tables
* *
* Copyright (c) 2017-2018 Fabrice Bellard * Copyright (c) 2017-2018 Fabrice Bellard
* Copyright (c) 2017-2018 Charlie Gordon * Copyright (c) 2017-2018 Charlie Gordon
* *
@ -289,7 +289,7 @@ void parse_unicode_data(const char *filename)
const char *p; const char *p;
int code, lc, uc, last_code; int code, lc, uc, last_code;
CCInfo *ci, *tab = unicode_db; CCInfo *ci, *tab = unicode_db;
f = fopen(filename, "rb"); f = fopen(filename, "rb");
if (!f) { if (!f) {
perror(filename); perror(filename);
@ -312,7 +312,7 @@ void parse_unicode_data(const char *filename)
code = strtoul(p, NULL, 16); code = strtoul(p, NULL, 16);
lc = 0; lc = 0;
uc = 0; uc = 0;
p = get_field(line, 12); p = get_field(line, 12);
if (p && *p != ';') { if (p && *p != ';') {
uc = strtoul(p, NULL, 16); uc = strtoul(p, NULL, 16);
@ -348,7 +348,7 @@ void parse_unicode_data(const char *filename)
} }
ci->general_category = i; ci->general_category = i;
} }
p = get_field(line, 3); p = get_field(line, 3);
if (p && *p != ';' && *p != '\0') { if (p && *p != ';' && *p != '\0') {
int cc; int cc;
@ -400,7 +400,7 @@ void parse_unicode_data(const char *filename)
if (p && *p == 'Y') { if (p && *p == 'Y') {
set_prop(code, PROP_Bidi_Mirrored, 1); set_prop(code, PROP_Bidi_Mirrored, 1);
} }
/* handle ranges */ /* handle ranges */
get_field_buf(buf1, sizeof(buf1), line, 1); get_field_buf(buf1, sizeof(buf1), line, 1);
if (strstr(buf1, " Last>")) { if (strstr(buf1, " Last>")) {
@ -414,7 +414,7 @@ void parse_unicode_data(const char *filename)
} }
last_code = code; last_code = code;
} }
fclose(f); fclose(f);
} }
@ -425,7 +425,7 @@ void parse_special_casing(CCInfo *tab, const char *filename)
const char *p; const char *p;
int code; int code;
CCInfo *ci; CCInfo *ci;
f = fopen(filename, "rb"); f = fopen(filename, "rb");
if (!f) { if (!f) {
perror(filename); perror(filename);
@ -456,8 +456,8 @@ void parse_special_casing(CCInfo *tab, const char *filename)
if (*p != '#' && *p != '\0') if (*p != '#' && *p != '\0')
continue; continue;
} }
p = get_field(line, 1); p = get_field(line, 1);
if (p && *p != ';') { if (p && *p != ';') {
ci->l_len = 0; ci->l_len = 0;
@ -490,7 +490,7 @@ void parse_special_casing(CCInfo *tab, const char *filename)
ci->u_len = 0; ci->u_len = 0;
} }
} }
fclose(f); fclose(f);
} }
@ -501,7 +501,7 @@ void parse_case_folding(CCInfo *tab, const char *filename)
const char *p; const char *p;
int code; int code;
CCInfo *ci; CCInfo *ci;
f = fopen(filename, "rb"); f = fopen(filename, "rb");
if (!f) { if (!f) {
perror(filename); perror(filename);
@ -532,14 +532,14 @@ void parse_case_folding(CCInfo *tab, const char *filename)
p++; p++;
if (*p != 'C' && *p != 'S') if (*p != 'C' && *p != 'S')
continue; continue;
p = get_field(line, 2); p = get_field(line, 2);
assert(p != 0); assert(p != 0);
assert(ci->f_code == 0); assert(ci->f_code == 0);
ci->f_code = strtoul(p, NULL, 16); ci->f_code = strtoul(p, NULL, 16);
assert(ci->f_code != 0 && ci->f_code != code); assert(ci->f_code != 0 && ci->f_code != code);
} }
fclose(f); fclose(f);
} }
@ -548,7 +548,7 @@ void parse_composition_exclusions(const char *filename)
FILE *f; FILE *f;
char line[4096], *p; char line[4096], *p;
uint32_t c0; uint32_t c0;
f = fopen(filename, "rb"); f = fopen(filename, "rb");
if (!f) { if (!f) {
perror(filename); perror(filename);
@ -576,7 +576,7 @@ void parse_derived_core_properties(const char *filename)
char line[4096], *p, buf[256], *q; char line[4096], *p, buf[256], *q;
uint32_t c0, c1, c; uint32_t c0, c1, c;
int i; int i;
f = fopen(filename, "rb"); f = fopen(filename, "rb");
if (!f) { if (!f) {
perror(filename); perror(filename);
@ -632,7 +632,7 @@ void parse_derived_norm_properties(const char *filename)
FILE *f; FILE *f;
char line[4096], *p, buf[256], *q; char line[4096], *p, buf[256], *q;
uint32_t c0, c1, c; uint32_t c0, c1, c;
f = fopen(filename, "rb"); f = fopen(filename, "rb");
if (!f) { if (!f) {
perror(filename); perror(filename);
@ -682,7 +682,7 @@ void parse_prop_list(const char *filename)
char line[4096], *p, buf[256], *q; char line[4096], *p, buf[256], *q;
uint32_t c0, c1, c; uint32_t c0, c1, c;
int i; int i;
f = fopen(filename, "rb"); f = fopen(filename, "rb");
if (!f) { if (!f) {
perror(filename); perror(filename);
@ -736,7 +736,7 @@ void parse_scripts(const char *filename)
char line[4096], *p, buf[256], *q; char line[4096], *p, buf[256], *q;
uint32_t c0, c1, c; uint32_t c0, c1, c;
int i; int i;
f = fopen(filename, "rb"); f = fopen(filename, "rb");
if (!f) { if (!f) {
perror(filename); perror(filename);
@ -791,7 +791,7 @@ void parse_script_extensions(const char *filename)
int i; int i;
uint8_t script_ext[255]; uint8_t script_ext[255];
int script_ext_len; int script_ext_len;
f = fopen(filename, "rb"); f = fopen(filename, "rb");
if (!f) { if (!f) {
perror(filename); perror(filename);
@ -947,7 +947,7 @@ void find_run_type(TableEntry *te, CCInfo *tab, int code)
ci1 = &tab[code + 1]; ci1 = &tab[code + 1];
ci2 = &tab[code + 2]; ci2 = &tab[code + 2];
te->code = code; te->code = code;
if (ci->l_len == 1 && ci->l_data[0] == code + 2 && if (ci->l_len == 1 && ci->l_data[0] == code + 2 &&
ci->f_code == ci->l_data[0] && ci->f_code == ci->l_data[0] &&
ci->u_len == 0 && ci->u_len == 0 &&
@ -1103,7 +1103,7 @@ void find_run_type(TableEntry *te, CCInfo *tab, int code)
te->data = 0; te->data = 0;
return; return;
} }
ci = &tab[code]; ci = &tab[code];
is_lower = ci->l_len > 0; is_lower = ci->l_len > 0;
len = 1; len = 1;
@ -1184,7 +1184,7 @@ void build_conv_table(CCInfo *tab)
int code, i, j; int code, i, j;
CCInfo *ci; CCInfo *ci;
TableEntry *te; TableEntry *te;
te = conv_table; te = conv_table;
for(code = 0; code <= CHARCODE_MAX; code++) { for(code = 0; code <= CHARCODE_MAX; code++) {
ci = &tab[code]; ci = &tab[code];
@ -1208,7 +1208,7 @@ void build_conv_table(CCInfo *tab)
for(i = 0; i < conv_table_len; i++) { for(i = 0; i < conv_table_len; i++) {
int data_index; int data_index;
te = &conv_table[i]; te = &conv_table[i];
switch(te->type) { switch(te->type) {
case RUN_TYPE_U: case RUN_TYPE_U:
case RUN_TYPE_L: case RUN_TYPE_L:
@ -1405,7 +1405,7 @@ void build_prop_table(FILE *f, int prop_index, BOOL add_index)
const uint32_t *buf; const uint32_t *buf;
int buf_len, block_end_pos, bit; int buf_len, block_end_pos, bit;
char cname[128]; char cname[128];
dbuf_init(dbuf1); dbuf_init(dbuf1);
for(i = 0; i <= CHARCODE_MAX;) { for(i = 0; i <= CHARCODE_MAX;) {
@ -1421,15 +1421,15 @@ void build_prop_table(FILE *f, int prop_index, BOOL add_index)
dbuf_put_u32(dbuf1, n - 1); dbuf_put_u32(dbuf1, n - 1);
i += n; i += n;
} }
dbuf_init(dbuf); dbuf_init(dbuf);
dbuf_init(dbuf2); dbuf_init(dbuf2);
buf = (uint32_t *)dbuf1->buf; buf = (uint32_t *)dbuf1->buf;
buf_len = dbuf1->size / sizeof(buf[0]); buf_len = dbuf1->size / sizeof(buf[0]);
/* the first value is assumed to be 0 */ /* the first value is assumed to be 0 */
assert(get_prop(0, prop_index) == 0); assert(get_prop(0, prop_index) == 0);
block_end_pos = PROP_BLOCK_LEN; block_end_pos = PROP_BLOCK_LEN;
i = 0; i = 0;
code = 0; code = 0;
@ -1490,7 +1490,7 @@ void build_prop_table(FILE *f, int prop_index, BOOL add_index)
snprintf(cname, sizeof(cname), "unicode_prop_%s_index", unicode_prop_name[prop_index]); snprintf(cname, sizeof(cname), "unicode_prop_%s_index", unicode_prop_name[prop_index]);
dump_byte_table(f, cname, dbuf2->buf, dbuf2->size); dump_byte_table(f, cname, dbuf2->buf, dbuf2->size);
} }
dbuf_free(dbuf); dbuf_free(dbuf);
dbuf_free(dbuf1); dbuf_free(dbuf1);
dbuf_free(dbuf2); dbuf_free(dbuf2);
@ -1605,7 +1605,7 @@ void build_general_category_table(FILE *f)
printf(" %d", cw_len_count[i]); printf(" %d", cw_len_count[i]);
printf(" ], length=%d bytes\n", (int)dbuf->size); printf(" ], length=%d bytes\n", (int)dbuf->size);
#endif #endif
dump_byte_table(f, "unicode_gc_table", dbuf->buf, dbuf->size); dump_byte_table(f, "unicode_gc_table", dbuf->buf, dbuf->size);
dbuf_free(dbuf); dbuf_free(dbuf);
@ -1675,7 +1675,7 @@ void build_script_table(FILE *f)
printf(" %d", cw_len_count[i]); printf(" %d", cw_len_count[i]);
printf(" ], length=%d bytes\n", (int)dbuf->size); printf(" ], length=%d bytes\n", (int)dbuf->size);
#endif #endif
dump_byte_table(f, "unicode_script_table", dbuf->buf, dbuf->size); dump_byte_table(f, "unicode_script_table", dbuf->buf, dbuf->size);
dbuf_free(dbuf); dbuf_free(dbuf);
@ -1725,7 +1725,7 @@ void build_script_ext_table(FILE *f)
cw_count); cw_count);
printf(", length=%d bytes\n", (int)dbuf->size); printf(", length=%d bytes\n", (int)dbuf->size);
#endif #endif
dump_byte_table(f, "unicode_script_ext_table", dbuf->buf, dbuf->size); dump_byte_table(f, "unicode_script_ext_table", dbuf->buf, dbuf->size);
dbuf_free(dbuf); dbuf_free(dbuf);
@ -1737,7 +1737,7 @@ void build_script_ext_table(FILE *f)
void build_prop_list_table(FILE *f) void build_prop_list_table(FILE *f)
{ {
int i; int i;
for(i = 0; i < PROP_TABLE_COUNT; i++) { for(i = 0; i < PROP_TABLE_COUNT; i++) {
if (i == PROP_ID_Start || if (i == PROP_ID_Start ||
i == PROP_Case_Ignorable || i == PROP_Case_Ignorable ||
@ -1747,7 +1747,7 @@ void build_prop_list_table(FILE *f)
build_prop_table(f, i, FALSE); build_prop_table(f, i, FALSE);
} }
} }
fprintf(f, "typedef enum {\n"); fprintf(f, "typedef enum {\n");
for(i = 0; i < PROP_COUNT; i++) for(i = 0; i < PROP_COUNT; i++)
fprintf(f, " UNICODE_PROP_%s,\n", unicode_prop_name[i]); fprintf(f, " UNICODE_PROP_%s,\n", unicode_prop_name[i]);
@ -1785,7 +1785,7 @@ void check_case_conv(void)
int l, error; int l, error;
CCInfo ci_s, *ci1, *ci = &ci_s; CCInfo ci_s, *ci1, *ci = &ci_s;
int code; int code;
for(code = 0; code <= CHARCODE_MAX; code++) { for(code = 0; code <= CHARCODE_MAX; code++) {
ci1 = &tab[code]; ci1 = &tab[code];
*ci = *ci1; *ci = *ci1;
@ -1899,7 +1899,7 @@ void build_cc_table(FILE *f)
DynBuf dbuf1_s, *dbuf1 = &dbuf1_s; DynBuf dbuf1_s, *dbuf1 = &dbuf1_s;
int cw_len_tab[3], cw_start, block_end_pos; int cw_len_tab[3], cw_start, block_end_pos;
uint32_t v; uint32_t v;
dbuf_init(dbuf); dbuf_init(dbuf);
dbuf_init(dbuf1); dbuf_init(dbuf1);
cc_table_len = 0; cc_table_len = 0;
@ -1971,7 +1971,7 @@ void build_cc_table(FILE *f)
dbuf_putc(dbuf1, v); dbuf_putc(dbuf1, v);
dbuf_putc(dbuf1, v >> 8); dbuf_putc(dbuf1, v >> 8);
dbuf_putc(dbuf1, v >> 16); dbuf_putc(dbuf1, v >> 16);
dump_byte_table(f, "unicode_cc_table", dbuf->buf, dbuf->size); dump_byte_table(f, "unicode_cc_table", dbuf->buf, dbuf->size);
dump_byte_table(f, "unicode_cc_index", dbuf1->buf, dbuf1->size); dump_byte_table(f, "unicode_cc_index", dbuf1->buf, dbuf1->size);
@ -2076,7 +2076,7 @@ const int decomp_incr_tab[4][4] = {
/* /*
entry size: entry size:
type bits type bits
code 18 code 18
len 7 len 7
compat 1 compat 1
type 5 type 5
@ -2185,7 +2185,7 @@ void find_decomp_run(DecompEntry *tab_de, int i)
DecompEntry de_s, *de = &de_s; DecompEntry de_s, *de = &de_s;
CCInfo *ci, *ci1, *ci2; CCInfo *ci, *ci1, *ci2;
int l, j, n, len_max; int l, j, n, len_max;
ci = &unicode_db[i]; ci = &unicode_db[i];
l = ci->decomp_len; l = ci->decomp_len;
if (l == 0) { if (l == 0) {
@ -2196,12 +2196,12 @@ void find_decomp_run(DecompEntry *tab_de, int i)
/* the offset for the compose table has only 6 bits, so we must /* the offset for the compose table has only 6 bits, so we must
limit if it can be used by the compose table */ limit if it can be used by the compose table */
if (!ci->is_compat && !ci->is_excluded && l == 2) if (!ci->is_compat && !ci->is_excluded && l == 2)
len_max = 64; len_max = 64;
else else
len_max = 127; len_max = 127;
tab_de[i].cost = 0x7fffffff; tab_de[i].cost = 0x7fffffff;
if (!is_16bit(ci->decomp_data, l)) { if (!is_16bit(ci->decomp_data, l)) {
assert(l <= 2); assert(l <= 2);
@ -2244,7 +2244,7 @@ void find_decomp_run(DecompEntry *tab_de, int i)
if (de->cost < tab_de[i].cost) { if (de->cost < tab_de[i].cost) {
tab_de[i] = *de; tab_de[i] = *de;
} }
if (!((i + n) <= CHARCODE_MAX && n < len_max)) if (!((i + n) <= CHARCODE_MAX && n < len_max))
break; break;
ci1 = &unicode_db[i + n]; ci1 = &unicode_db[i + n];
@ -2257,7 +2257,7 @@ void find_decomp_run(DecompEntry *tab_de, int i)
n++; n++;
} }
} }
if (l <= 8 || l == 18) { if (l <= 8 || l == 18) {
int c_min, c_max, c; int c_min, c_max, c;
c_min = c_max = -1; c_min = c_max = -1;
@ -2328,7 +2328,7 @@ void find_decomp_run(DecompEntry *tab_de, int i)
/* check if a single char is increasing */ /* check if a single char is increasing */
if (l <= 4) { if (l <= 4) {
int idx1, idx; int idx1, idx;
for(idx1 = 1; (idx = decomp_incr_tab[l - 1][idx1]) >= 0; idx1++) { for(idx1 = 1; (idx = decomp_incr_tab[l - 1][idx1]) >= 0; idx1++) {
n = 1; n = 1;
for(;;) { for(;;) {
@ -2412,7 +2412,7 @@ void find_decomp_run(DecompEntry *tab_de, int i)
if (l == 2) { if (l == 2) {
BOOL is_16bit; BOOL is_16bit;
n = 0; n = 0;
is_16bit = FALSE; is_16bit = FALSE;
for(;;) { for(;;) {
@ -2457,7 +2457,7 @@ void add_decomp_data(uint8_t *data_buf, int *pidx, DecompEntry *de)
{ {
int i, j, idx, c; int i, j, idx, c;
CCInfo *ci; CCInfo *ci;
idx = *pidx; idx = *pidx;
de->data_index = idx; de->data_index = idx;
if (de->type <= DECOMP_TYPE_C1) { if (de->type <= DECOMP_TYPE_C1) {
@ -2608,9 +2608,9 @@ void build_decompose_table(FILE *f)
int i, array_len, code_max, data_len, count; int i, array_len, code_max, data_len, count;
DecompEntry *tab_de, de_s, *de = &de_s; DecompEntry *tab_de, de_s, *de = &de_s;
uint8_t *data_buf; uint8_t *data_buf;
code_max = CHARCODE_MAX; code_max = CHARCODE_MAX;
tab_de = mallocz((code_max + 2) * sizeof(*tab_de)); tab_de = mallocz((code_max + 2) * sizeof(*tab_de));
for(i = code_max; i >= 0; i--) { for(i = code_max; i >= 0; i--) {
@ -2634,7 +2634,7 @@ void build_decompose_table(FILE *f)
/* dump */ /* dump */
{ {
int size, size1; int size, size1;
printf("START LEN TYPE L C SIZE\n"); printf("START LEN TYPE L C SIZE\n");
size = 0; size = 0;
for(i = 0; i <= code_max; i++) { for(i = 0; i <= code_max; i++) {
@ -2648,7 +2648,7 @@ void build_decompose_table(FILE *f)
size += size1; size += size1;
} }
} }
printf("array_len=%d estimated size=%d bytes actual=%d bytes\n", printf("array_len=%d estimated size=%d bytes actual=%d bytes\n",
array_len, size, array_len * 6 + data_len); array_len, size, array_len * 6 + data_len);
} }
@ -2686,7 +2686,7 @@ void build_decompose_table(FILE *f)
} }
} }
fprintf(f, "\n};\n\n"); fprintf(f, "\n};\n\n");
fprintf(f, "static const uint8_t unicode_decomp_data[%u] = {", fprintf(f, "static const uint8_t unicode_decomp_data[%u] = {",
data_len); data_len);
for(i = 0; i < data_len; i++) { for(i = 0; i < data_len; i++) {
@ -2699,7 +2699,7 @@ void build_decompose_table(FILE *f)
build_compose_table(f, tab_de); build_compose_table(f, tab_de);
free(data_buf); free(data_buf);
free(tab_de); free(tab_de);
} }
@ -2730,7 +2730,7 @@ static int get_decomp_pos(const DecompEntry *tab_de, int c)
{ {
int i, v, k; int i, v, k;
const DecompEntry *de; const DecompEntry *de;
k = 0; k = 0;
for(i = 0; i <= CHARCODE_MAX; i++) { for(i = 0; i <= CHARCODE_MAX; i++) {
de = &tab_de[i]; de = &tab_de[i];
@ -2753,14 +2753,14 @@ void build_compose_table(FILE *f, const DecompEntry *tab_de)
{ {
int i, v, tab_ce_len; int i, v, tab_ce_len;
ComposeEntry *ce, *tab_ce; ComposeEntry *ce, *tab_ce;
tab_ce = malloc(sizeof(*tab_ce) * COMPOSE_LEN_MAX); tab_ce = malloc(sizeof(*tab_ce) * COMPOSE_LEN_MAX);
tab_ce_len = 0; tab_ce_len = 0;
for(i = 0; i <= CHARCODE_MAX; i++) { for(i = 0; i <= CHARCODE_MAX; i++) {
CCInfo *ci = &unicode_db[i]; CCInfo *ci = &unicode_db[i];
if (ci->decomp_len == 2 && !ci->is_compat && if (ci->decomp_len == 2 && !ci->is_compat &&
!ci->is_excluded) { !ci->is_excluded) {
assert(tab_ce_len < COMPOSE_LEN_MAX); assert(tab_ce_len < COMPOSE_LEN_MAX);
ce = &tab_ce[tab_ce_len++]; ce = &tab_ce[tab_ce_len++];
ce->c[0] = ci->decomp_data[0]; ce->c[0] = ci->decomp_data[0];
ce->c[1] = ci->decomp_data[1]; ce->c[1] = ci->decomp_data[1];
@ -2778,7 +2778,7 @@ void build_compose_table(FILE *f, const DecompEntry *tab_de)
} }
} }
#endif #endif
fprintf(f, "static const uint16_t unicode_comp_table[%u] = {", fprintf(f, "static const uint16_t unicode_comp_table[%u] = {",
tab_ce_len); tab_ce_len);
for(i = 0; i < tab_ce_len; i++) { for(i = 0; i < tab_ce_len; i++) {
@ -2793,7 +2793,7 @@ void build_compose_table(FILE *f, const DecompEntry *tab_de)
fprintf(f, " 0x%04x,", v); fprintf(f, " 0x%04x,", v);
} }
fprintf(f, "\n};\n\n"); fprintf(f, "\n};\n\n");
free(tab_ce); free(tab_ce);
} }
@ -2842,7 +2842,7 @@ void check_compose_table(void)
} }
} }
} }
} }
@ -2882,7 +2882,7 @@ void check_cc_table(void)
#ifdef PROFILE #ifdef PROFILE
{ {
int64_t ti, count; int64_t ti, count;
ti = get_time_ns(); ti = get_time_ns();
count = 0; count = 0;
/* only do it on meaningful chars */ /* only do it on meaningful chars */
@ -2905,7 +2905,7 @@ void normalization_test(const char *filename)
int *in_str, *nfc_str, *nfd_str, *nfkc_str, *nfkd_str; int *in_str, *nfc_str, *nfd_str, *nfkc_str, *nfkd_str;
int in_len, nfc_len, nfd_len, nfkc_len, nfkd_len; int in_len, nfc_len, nfd_len, nfkc_len, nfkd_len;
int *buf, buf_len, pos; int *buf, buf_len, pos;
f = fopen(filename, "rb"); f = fopen(filename, "rb");
if (!f) { if (!f) {
perror(filename); perror(filename);
@ -2936,7 +2936,7 @@ void normalization_test(const char *filename)
buf_len = unicode_normalize((uint32_t **)&buf, (uint32_t *)in_str, in_len, UNICODE_NFKD, NULL, NULL); buf_len = unicode_normalize((uint32_t **)&buf, (uint32_t *)in_str, in_len, UNICODE_NFKD, NULL, NULL);
check_str("nfkd", pos, in_str, in_len, buf, buf_len, nfkd_str, nfkd_len); check_str("nfkd", pos, in_str, in_len, buf, buf_len, nfkd_str, nfkd_len);
free(buf); free(buf);
buf_len = unicode_normalize((uint32_t **)&buf, (uint32_t *)in_str, in_len, UNICODE_NFC, NULL, NULL); buf_len = unicode_normalize((uint32_t **)&buf, (uint32_t *)in_str, in_len, UNICODE_NFC, NULL, NULL);
check_str("nfc", pos, in_str, in_len, buf, buf_len, nfc_str, nfc_len); check_str("nfc", pos, in_str, in_len, buf, buf_len, nfc_str, nfc_len);
free(buf); free(buf);
@ -2959,7 +2959,7 @@ int main(int argc, char **argv)
{ {
const char *unicode_db_path, *outfilename; const char *unicode_db_path, *outfilename;
char filename[1024]; char filename[1024];
if (argc < 2) { if (argc < 2) {
printf("usage: %s unicode_db_path [output_file]\n" printf("usage: %s unicode_db_path [output_file]\n"
"\n" "\n"
@ -2980,13 +2980,13 @@ int main(int argc, char **argv)
snprintf(filename, sizeof(filename), "%s/SpecialCasing.txt", unicode_db_path); snprintf(filename, sizeof(filename), "%s/SpecialCasing.txt", unicode_db_path);
parse_special_casing(unicode_db, filename); parse_special_casing(unicode_db, filename);
snprintf(filename, sizeof(filename), "%s/CaseFolding.txt", unicode_db_path); snprintf(filename, sizeof(filename), "%s/CaseFolding.txt", unicode_db_path);
parse_case_folding(unicode_db, filename); parse_case_folding(unicode_db, filename);
snprintf(filename, sizeof(filename), "%s/CompositionExclusions.txt", unicode_db_path); snprintf(filename, sizeof(filename), "%s/CompositionExclusions.txt", unicode_db_path);
parse_composition_exclusions(filename); parse_composition_exclusions(filename);
snprintf(filename, sizeof(filename), "%s/DerivedCoreProperties.txt", unicode_db_path); snprintf(filename, sizeof(filename), "%s/DerivedCoreProperties.txt", unicode_db_path);
parse_derived_core_properties(filename); parse_derived_core_properties(filename);
@ -3002,7 +3002,7 @@ int main(int argc, char **argv)
snprintf(filename, sizeof(filename), "%s/ScriptExtensions.txt", snprintf(filename, sizeof(filename), "%s/ScriptExtensions.txt",
unicode_db_path); unicode_db_path);
parse_script_extensions(filename); parse_script_extensions(filename);
snprintf(filename, sizeof(filename), "%s/emoji-data.txt", snprintf(filename, sizeof(filename), "%s/emoji-data.txt",
unicode_db_path); unicode_db_path);
parse_prop_list(filename); parse_prop_list(filename);
@ -3010,7 +3010,7 @@ int main(int argc, char **argv)
// dump_data(unicode_db); // dump_data(unicode_db);
build_conv_table(unicode_db); build_conv_table(unicode_db);
// dump_table(); // dump_table();
if (!outfilename) { if (!outfilename) {
@ -3029,7 +3029,7 @@ int main(int argc, char **argv)
} else } else
{ {
FILE *fo = fopen(outfilename, "wb"); FILE *fo = fopen(outfilename, "wb");
if (!fo) { if (!fo) {
perror(outfilename); perror(outfilename);
exit(1); exit(1);