Skip to content

Commit

Permalink
Merge branch 'master' into wpedantic-compat
Browse files Browse the repository at this point in the history
  • Loading branch information
andrjohns committed Dec 26, 2023
2 parents d86d095 + 9b587c4 commit 6f69b23
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 58 deletions.
7 changes: 3 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,9 @@ endif

# examples
ifeq ($(CROSS_PREFIX),)
ifdef CONFIG_ASAN
PROGS+=
else
PROGS+=examples/hello examples/hello_module examples/test_fib
PROGS+=examples/hello
ifndef CONFIG_ASAN
PROGS+=examples/hello_module examples/test_fib
ifndef CONFIG_DARWIN
PROGS+=examples/fib.so examples/point.so
endif
Expand Down
3 changes: 3 additions & 0 deletions cutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
#define countof(x) (sizeof(x) / sizeof((x)[0]))
#endif

/* return the pointer of type 'type *' containing 'ptr' as field 'member' */
#define container_of(ptr, type, member) ((type *)((uint8_t *)(ptr) - offsetof(type, member)))

typedef int BOOL;

#ifndef FALSE
Expand Down
2 changes: 1 addition & 1 deletion libbf.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include <stddef.h>
#include <stdint.h>

#if INTPTR_MAX >= INT64_MAX
#if defined(__SIZEOF_INT128__) && (INTPTR_MAX >= INT64_MAX)
#define LIMB_LOG2_BITS 6
#else
#define LIMB_LOG2_BITS 5
Expand Down
3 changes: 1 addition & 2 deletions list.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ struct list_head {
#define LIST_HEAD_INIT(el) { &(el), &(el) }

/* return the pointer of type 'type *' containing 'el' as field 'member' */
#define list_entry(el, type, member) \
((type *)((uint8_t *)(el) - offsetof(type, member)))
#define list_entry(el, type, member) container_of(el, type, member)

static inline void init_list_head(struct list_head *head)
{
Expand Down
1 change: 1 addition & 0 deletions qjsc.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ static const char main_c_template1[] =

static const char main_c_template2[] =
" js_std_loop(ctx);\n"
" js_std_free_handlers(rt);\n"
" JS_FreeContext(ctx);\n"
" JS_FreeRuntime(rt);\n"
" return 0;\n"
Expand Down
177 changes: 135 additions & 42 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ typedef enum JSErrorEnum {
JS_NATIVE_ERROR_COUNT, /* number of different NativeError objects */
} JSErrorEnum;

#define JS_MAX_LOCAL_VARS 65536
#define JS_MAX_LOCAL_VARS 65535
#define JS_STACK_SIZE_MAX 65534
#define JS_STRING_LEN_MAX ((1 << 30) - 1)

Expand Down Expand Up @@ -466,7 +466,6 @@ struct JSContext {
#endif
/* when the counter reaches zero, JSRutime.interrupt_handler is called */
int interrupt_counter;
BOOL is_error_property_enabled;

struct list_head loaded_modules; /* list of JSModuleDef.link */

Expand Down Expand Up @@ -4112,7 +4111,7 @@ void JS_FreeCString(JSContext *ctx, const char *ptr)
if (!ptr)
return;
/* purposely removing constness */
p = (JSString *)(void *)(ptr - offsetof(JSString, u));
p = container_of(ptr, JSString, u);
JS_FreeValue(ctx, JS_MKPTR(JS_TAG_STRING, p));
}

Expand Down Expand Up @@ -9153,15 +9152,19 @@ int JS_DefineProperty(JSContext *ctx, JSValueConst this_obj,
spaces. */
if (!js_same_value(ctx, val, *pr->u.var_ref->pvalue))
goto not_configurable;
} else {
/* update the reference */
set_value(ctx, pr->u.var_ref->pvalue,
JS_DupValue(ctx, val));
}
/* update the reference */
set_value(ctx, pr->u.var_ref->pvalue,
JS_DupValue(ctx, val));
}
/* if writable is set to false, no longer a
reference (for mapped arguments) */
if ((flags & (JS_PROP_HAS_WRITABLE | JS_PROP_WRITABLE)) == JS_PROP_HAS_WRITABLE) {
JSValue val1;
if (p->class_id == JS_CLASS_MODULE_NS) {
return JS_ThrowTypeErrorOrFalse(ctx, flags, "module namespace properties have writable = false");
}
if (js_shape_prepare_update(ctx, p, &prs))
return -1;
val1 = JS_DupValue(ctx, *pr->u.var_ref->pvalue);
Expand Down Expand Up @@ -10022,12 +10025,13 @@ static inline int to_digit(int c)
}

/* XXX: remove */
static double js_strtod(const char *p, int radix, BOOL is_float)
static double js_strtod(const char *str, int radix, BOOL is_float)
{
double d;
int c;

if (!is_float || radix != 10) {
const char *p = str;
uint64_t n_max, n;
int int_exp, is_neg;

Expand All @@ -10054,6 +10058,8 @@ static double js_strtod(const char *p, int radix, BOOL is_float)
if (n <= n_max) {
n = n * radix + c;
} else {
if (radix == 10)
goto strtod_case;
int_exp++;
}
p++;
Expand All @@ -10065,7 +10071,8 @@ static double js_strtod(const char *p, int radix, BOOL is_float)
if (is_neg)
d = -d;
} else {
d = strtod(p, NULL);
strtod_case:
d = strtod(str, NULL);
}
return d;
}
Expand Down Expand Up @@ -25220,7 +25227,6 @@ static __exception int js_parse_assign_expr2(JSParseState *s, int parse_flags)
/* OP_async_yield_star takes the value as parameter */
emit_op(s, OP_get_field);
emit_atom(s, JS_ATOM_value);
emit_op(s, OP_await);
emit_op(s, OP_async_yield_star);
} else {
/* OP_yield_star takes (value, done) as parameter */
Expand Down Expand Up @@ -25860,6 +25866,9 @@ static __exception int js_parse_for_in_of(JSParseState *s, int label_name,
emit_atom(s, var_name);
emit_u16(s, fd->scope_level);
}
} else if (!is_async && token_is_pseudo_keyword(s, JS_ATOM_async) &&
peek_token(s, FALSE) == TOK_OF) {
return js_parse_error(s, "'for of' expression cannot start with 'async'");
} else {
int skip_bits;
if ((s->token.val == '[' || s->token.val == '{')
Expand Down Expand Up @@ -41188,26 +41197,6 @@ static BOOL test_final_sigma(JSString *p, int sigma_pos)
return !lre_is_cased(c1);
}

static JSValue js_string_localeCompare(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
JSValue a, b;
int cmp;

a = JS_ToStringCheckObject(ctx, this_val);
if (JS_IsException(a))
return JS_EXCEPTION;
b = JS_ToString(ctx, argv[0]);
if (JS_IsException(b)) {
JS_FreeValue(ctx, a);
return JS_EXCEPTION;
}
cmp = js_string_compare(ctx, JS_VALUE_GET_STRING(a), JS_VALUE_GET_STRING(b));
JS_FreeValue(ctx, a);
JS_FreeValue(ctx, b);
return JS_NewInt32(ctx, cmp);
}

static JSValue js_string_toLowerCase(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv, int to_lower)
{
Expand Down Expand Up @@ -41293,23 +41282,38 @@ static JSValue JS_NewUTF32String(JSContext *ctx, const uint32_t *buf, int len)
return JS_EXCEPTION;
}

static int js_string_normalize1(JSContext *ctx, uint32_t **pout_buf,
JSValueConst val,
UnicodeNormalizationEnum n_type)
{
int buf_len, out_len;
uint32_t *buf, *out_buf;

buf_len = JS_ToUTF32String(ctx, &buf, val);
if (buf_len < 0)
return -1;
out_len = unicode_normalize(&out_buf, buf, buf_len, n_type,
ctx->rt, (DynBufReallocFunc *)js_realloc_rt);
js_free(ctx, buf);
if (out_len < 0)
return -1;
*pout_buf = out_buf;
return out_len;
}

static JSValue js_string_normalize(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
const char *form, *p;
size_t form_len;
int is_compat, buf_len, out_len;
int is_compat, out_len;
UnicodeNormalizationEnum n_type;
JSValue val;
uint32_t *buf, *out_buf;
uint32_t *out_buf;

val = JS_ToStringCheckObject(ctx, this_val);
if (JS_IsException(val))
return val;
buf_len = JS_ToUTF32String(ctx, &buf, val);
JS_FreeValue(ctx, val);
if (buf_len < 0)
return JS_EXCEPTION;

if (argc == 0 || JS_IsUndefined(argv[0])) {
n_type = UNICODE_NFC;
Expand All @@ -41335,22 +41339,96 @@ static JSValue js_string_normalize(JSContext *ctx, JSValueConst this_val,
JS_FreeCString(ctx, form);
JS_ThrowRangeError(ctx, "bad normalization form");
fail1:
js_free(ctx, buf);
JS_FreeValue(ctx, val);
return JS_EXCEPTION;
}
JS_FreeCString(ctx, form);
}

out_len = unicode_normalize(&out_buf, buf, buf_len, n_type,
ctx->rt, (DynBufReallocFunc *)js_realloc_rt);
js_free(ctx, buf);
out_len = js_string_normalize1(ctx, &out_buf, val, n_type);
JS_FreeValue(ctx, val);
if (out_len < 0)
return JS_EXCEPTION;
val = JS_NewUTF32String(ctx, out_buf, out_len);
js_free(ctx, out_buf);
return val;
}
#endif /* CONFIG_ALL_UNICODE */

/* return < 0, 0 or > 0 */
static int js_UTF32_compare(const uint32_t *buf1, int buf1_len,
const uint32_t *buf2, int buf2_len)
{
int i, len, c, res;
len = min_int(buf1_len, buf2_len);
for(i = 0; i < len; i++) {
/* Note: range is limited so a subtraction is valid */
c = buf1[i] - buf2[i];
if (c != 0)
return c;
}
if (buf1_len == buf2_len)
res = 0;
else if (buf1_len < buf2_len)
res = -1;
else
res = 1;
return res;
}

static JSValue js_string_localeCompare(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
JSValue a, b;
int cmp, a_len, b_len;
uint32_t *a_buf, *b_buf;

a = JS_ToStringCheckObject(ctx, this_val);
if (JS_IsException(a))
return JS_EXCEPTION;
b = JS_ToString(ctx, argv[0]);
if (JS_IsException(b)) {
JS_FreeValue(ctx, a);
return JS_EXCEPTION;
}
a_len = js_string_normalize1(ctx, &a_buf, a, UNICODE_NFC);
JS_FreeValue(ctx, a);
if (a_len < 0) {
JS_FreeValue(ctx, b);
return JS_EXCEPTION;
}

b_len = js_string_normalize1(ctx, &b_buf, b, UNICODE_NFC);
JS_FreeValue(ctx, b);
if (b_len < 0) {
js_free(ctx, a_buf);
return JS_EXCEPTION;
}
cmp = js_UTF32_compare(a_buf, a_len, b_buf, b_len);
js_free(ctx, a_buf);
js_free(ctx, b_buf);
return JS_NewInt32(ctx, cmp);
}
#else /* CONFIG_ALL_UNICODE */
static JSValue js_string_localeCompare(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv)
{
JSValue a, b;
int cmp;

a = JS_ToStringCheckObject(ctx, this_val);
if (JS_IsException(a))
return JS_EXCEPTION;
b = JS_ToString(ctx, argv[0]);
if (JS_IsException(b)) {
JS_FreeValue(ctx, a);
return JS_EXCEPTION;
}
cmp = js_string_compare(ctx, JS_VALUE_GET_STRING(a), JS_VALUE_GET_STRING(b));
JS_FreeValue(ctx, a);
JS_FreeValue(ctx, b);
return JS_NewInt32(ctx, cmp);
}
#endif /* !CONFIG_ALL_UNICODE */

/* also used for String.prototype.valueOf */
static JSValue js_string_toString(JSContext *ctx, JSValueConst this_val,
Expand Down Expand Up @@ -51191,11 +51269,26 @@ static void js_array_buffer_finalizer(JSRuntime *rt, JSValue val)
{
JSObject *p = JS_VALUE_GET_OBJ(val);
JSArrayBuffer *abuf = p->u.array_buffer;
struct list_head *el, *el1;

if (abuf) {
/* The ArrayBuffer finalizer may be called before the typed
array finalizers using it, so abuf->array_list is not
necessarily empty. */
// assert(list_empty(&abuf->array_list));
list_for_each_safe(el, el1, &abuf->array_list) {
JSTypedArray *ta;
JSObject *p1;

ta = list_entry(el, JSTypedArray, link);
ta->link.prev = NULL;
ta->link.next = NULL;
p1 = ta->obj;
/* Note: the typed array length and offset fields are not modified */
if (p1->class_id != JS_CLASS_DATAVIEW) {
p1->u.array.count = 0;
p1->u.array.u.ptr = NULL;
}
}
if (abuf->shared && rt->sab_funcs.sab_free) {
rt->sab_funcs.sab_free(rt->sab_funcs.sab_opaque, abuf->data);
} else {
Expand Down Expand Up @@ -53136,7 +53229,7 @@ static void js_typed_array_finalizer(JSRuntime *rt, JSValue val)
if (ta) {
/* during the GC the finalizers are called in an arbitrary
order so the ArrayBuffer finalizer may have been called */
if (JS_IsLiveObject(rt, JS_MKPTR(JS_TAG_OBJECT, ta->buffer))) {
if (ta->link.next) {
list_del(&ta->link);
}
JS_FreeValueRT(rt, JS_MKPTR(JS_TAG_OBJECT, ta->buffer));
Expand Down
9 changes: 0 additions & 9 deletions test262_errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ test262/test/built-ins/RegExp/lookahead-quantifier-match-groups.js:27: Test262Er
test262/test/built-ins/RegExp/lookahead-quantifier-match-groups.js:27: strict mode: Test262Error: Expected [a, abc] and [a, undefined] to have the same contents. ? quantifier
test262/test/built-ins/RegExp/unicode_full_case_folding.js:20: Test262Error: \u0390 does not match \u1fd3
test262/test/built-ins/RegExp/unicode_full_case_folding.js:20: strict mode: Test262Error: \u0390 does not match \u1fd3
test262/test/built-ins/String/prototype/localeCompare/15.5.4.9_CE.js:62: Test262Error: String.prototype.localeCompare considers ö (\u006f\u0308) ≠ ö (\u00f6).
test262/test/built-ins/String/prototype/localeCompare/15.5.4.9_CE.js:62: strict mode: Test262Error: String.prototype.localeCompare considers ö (\u006f\u0308) ≠ ö (\u00f6).
test262/test/built-ins/TypedArray/prototype/sort/sort-tonumber.js:30: TypeError: ArrayBuffer is detached (Testing with Float64Array.)
test262/test/built-ins/TypedArray/prototype/sort/sort-tonumber.js:30: strict mode: TypeError: ArrayBuffer is detached (Testing with Float64Array.)
test262/test/language/expressions/assignment/target-member-computed-reference-null.js:32: Test262Error: Expected a DummyError but got a TypeError
Expand All @@ -18,10 +16,3 @@ test262/test/language/expressions/dynamic-import/usage-from-eval.js:26: strict m
test262/test/language/expressions/optional-chaining/optional-call-preserves-this.js:21: TypeError: cannot read property 'c' of undefined
test262/test/language/expressions/optional-chaining/optional-call-preserves-this.js:15: strict mode: TypeError: cannot read property '_b' of undefined
test262/test/language/global-code/script-decl-lex-var-declared-via-eval-sloppy.js:13: Test262Error: variable Expected a SyntaxError to be thrown but no exception was thrown at all
test262/test/language/module-code/namespace/internals/define-own-property.js:30: Test262Error: Object.freeze: 1 Expected a TypeError to be thrown but no exception was thrown at all
test262/test/language/statements/async-generator/yield-star-promise-not-unwrapped.js:25: TypeError: $DONE() not called
test262/test/language/statements/async-generator/yield-star-promise-not-unwrapped.js:25: strict mode: TypeError: $DONE() not called
test262/test/language/statements/async-generator/yield-star-return-then-getter-ticks.js:131: TypeError: $DONE() not called
test262/test/language/statements/async-generator/yield-star-return-then-getter-ticks.js:131: strict mode: TypeError: $DONE() not called
test262/test/language/statements/for-of/head-lhs-async-invalid.js:14: unexpected error type: Test262: This statement should not be evaluated.
test262/test/language/statements/for-of/head-lhs-async-invalid.js:14: strict mode: unexpected error type: Test262: This statement should not be evaluated.
1 change: 1 addition & 0 deletions tests/test_language.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ function test_cvt()
assert((Infinity >>> 0) === 0);
assert(((-Infinity) >>> 0) === 0);
assert(((4294967296 * 3 - 4) >>> 0) === (4294967296 - 4));
assert((19686109595169230000).toString() === "19686109595169230000");
}

function test_eq()
Expand Down

0 comments on commit 6f69b23

Please sign in to comment.