Skip to content

Commit 1fae03b

Browse files
authored
Merge branch 'quickjs-ng:master' into master
2 parents 1232d2e + ecf9b1a commit 1fae03b

6 files changed

Lines changed: 104 additions & 31 deletions

File tree

CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ if(NOT SUNOS)
108108
xcheck_add_c_compiler_flag(-funsigned-char)
109109
endif()
110110

111-
# Clang on Windows without MSVC command line fails because the codebase uses
111+
# Clang on Windows without MSVC command line fails because the codebase uses
112112
# functions like strcpy over strcpy_s
113113
if(CMAKE_C_COMPILER_ID STREQUAL "Clang" AND WIN32 AND NOT MSVC)
114114
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
@@ -555,11 +555,11 @@ if (QJS_ENABLE_INSTALL)
555555
install(TARGETS qjs_exe RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
556556
install(TARGETS qjsc RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
557557
endif()
558-
install(TARGETS qjs EXPORT quickjsConfig
558+
install(TARGETS qjs EXPORT qjsConfig
559559
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
560560
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
561561
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
562-
install(EXPORT quickjsConfig DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/quickjs)
562+
install(EXPORT qjsConfig DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/qjs)
563563
install(FILES LICENSE DESTINATION ${CMAKE_INSTALL_DOCDIR})
564564
install(DIRECTORY examples DESTINATION ${CMAKE_INSTALL_DOCDIR})
565565
endif()

api-test.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,17 @@ static void utf16_string(void)
486486
JS_FreeCStringUTF16(ctx, u);
487487
JS_FreeValue(ctx, v);
488488
}
489+
{
490+
JSValue v = JS_NewStringUTF16(ctx, NULL, (size_t)INT_MAX + 1);
491+
assert(JS_IsException(v));
492+
JSValue e = JS_GetException(ctx);
493+
assert(JS_IsError(e));
494+
const char *s = JS_ToCString(ctx, e);
495+
assert(s);
496+
assert(strstr(s, "invalid string length") != NULL);
497+
JS_FreeCString(ctx, s);
498+
JS_FreeValue(ctx, e);
499+
}
489500
JS_FreeContext(ctx);
490501
JS_FreeRuntime(rt);
491502
}
@@ -765,6 +776,37 @@ static void new_errors(void)
765776
JS_FreeRuntime(rt);
766777
}
767778

779+
static void backtrace_oom_current_exception(void)
780+
{
781+
static const char setup_code[] =
782+
"globalThis.f = function() { missing; };\n"
783+
"Object.defineProperty(f, 'name', { value: 'x'.repeat(2 * 1024 * 1024) });";
784+
JSMemoryUsage stats;
785+
JSValue ret, exception;
786+
JSRuntime *rt;
787+
JSContext *ctx;
788+
789+
rt = new_runtime();
790+
ctx = JS_NewContext(rt);
791+
792+
ret = eval(ctx, setup_code);
793+
assert(!JS_IsException(ret));
794+
JS_FreeValue(ctx, ret);
795+
796+
JS_ComputeMemoryUsage(rt, &stats);
797+
JS_SetMemoryLimit(rt, (size_t)stats.malloc_size + 128 * 1024);
798+
799+
ret = eval(ctx, "f()");
800+
assert(JS_IsException(ret));
801+
assert(JS_HasException(ctx));
802+
exception = JS_GetException(ctx);
803+
JS_FreeValue(ctx, exception);
804+
JS_SetMemoryLimit(rt, 0);
805+
806+
JS_FreeContext(ctx);
807+
JS_FreeRuntime(rt);
808+
}
809+
768810
static int gop_get_own_property(JSContext *ctx, JSPropertyDescriptor *desc,
769811
JSValueConst obj, JSAtom prop)
770812
{
@@ -1167,6 +1209,7 @@ int main(void)
11671209
promise_hook();
11681210
dump_memory_usage();
11691211
new_errors();
1212+
backtrace_oom_current_exception();
11701213
global_object_prototype();
11711214
slice_string_tocstring();
11721215
immutable_array_buffer();

docs/docs/diff.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ average of a new release every 2 months.
2323
Since its inception testing has been a focus. Each PR is tested in over 50 configurations,
2424
involving different operating systems, build types and sanitizers.
2525

26-
The `test262` suite is also ran for every change.
26+
The `test262` suite is also run for every change.
2727

2828
## Cross-platform support
2929

quickjs.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1549,6 +1549,12 @@ static JSValue js_number(double d)
15491549
return js_float64(d);
15501550
}
15511551

1552+
static JSValue __JS_NewShortBigInt(JSContext *ctx, int32_t d)
1553+
{
1554+
(void)&ctx;
1555+
return JS_MKVAL(JS_TAG_SHORT_BIG_INT, d);
1556+
}
1557+
15521558
JSValue JS_NewNumber(JSContext *ctx, double d)
15531559
{
15541560
return js_number(d);
@@ -4549,6 +4555,8 @@ JSValue JS_NewStringUTF16(JSContext *ctx, const uint16_t *buf, size_t len)
45494555

45504556
if (unlikely(!len))
45514557
return js_empty_string(ctx->rt);
4558+
if (unlikely(len > JS_STRING_LEN_MAX))
4559+
return JS_ThrowRangeError(ctx, "invalid string length");
45524560

45534561
str = js_alloc_string(ctx, len, 1);
45544562
if (unlikely(!str))
@@ -7899,7 +7907,7 @@ static void build_backtrace(JSContext *ctx, JSValueConst error_val,
78997907
int line_num, int col_num, int backtrace_flags)
79007908
{
79017909
JSStackFrame *sf, *sf_start;
7902-
JSValue stack, prepare, saved_exception;
7910+
JSValue stack, prepare, saved_exception, error_obj;
79037911
DynBuf dbuf;
79047912
const char *func_name_str;
79057913
const char *str1;
@@ -7916,6 +7924,7 @@ static void build_backtrace(JSContext *ctx, JSValueConst error_val,
79167924
if (rt->in_build_stack_trace)
79177925
return;
79187926
rt->in_build_stack_trace = true;
7927+
error_obj = js_dup(error_val);
79197928

79207929
// Save exception because conversion to double may fail.
79217930
saved_exception = JS_GetException(ctx);
@@ -8061,7 +8070,7 @@ static void build_backtrace(JSContext *ctx, JSValueConst error_val,
80618070
JS_FreeValue(ctx, csd[k].func_name);
80628071
}
80638072
JSValueConst args[] = {
8064-
error_val,
8073+
error_obj,
80658074
stack,
80668075
};
80678076
JSValue stack2 = JS_Call(ctx, prepare, ctx->error_ctor, countof(args), args);
@@ -8082,13 +8091,14 @@ static void build_backtrace(JSContext *ctx, JSValueConst error_val,
80828091

80838092
if (JS_IsUndefined(ctx->error_back_trace))
80848093
ctx->error_back_trace = js_dup(stack);
8085-
if (has_filter_func || can_add_backtrace(error_val)) {
8086-
JS_DefinePropertyValue(ctx, error_val, JS_ATOM_stack, stack,
8094+
if (has_filter_func || can_add_backtrace(error_obj)) {
8095+
JS_DefinePropertyValue(ctx, error_obj, JS_ATOM_stack, stack,
80878096
JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
80888097
} else {
80898098
JS_FreeValue(ctx, stack);
80908099
}
80918100

8101+
JS_FreeValue(ctx, error_obj);
80928102
rt->in_build_stack_trace = false;
80938103
}
80948104

@@ -40444,7 +40454,7 @@ JSValue JS_ToObject(JSContext *ctx, JSValueConst val)
4044440454
if (!JS_IsException(obj)) {
4044540455
JS_DefinePropertyValue(ctx, obj, JS_ATOM_length,
4044640456
JS_NewInt32(ctx, JS_VALUE_GET_STRING(str)->len), 0);
40447-
JS_SetObjectData(ctx, obj, JS_DupValue(ctx, str));
40457+
JS_SetObjectData(ctx, obj, js_dup(str));
4044840458
}
4044940459
JS_FreeValue(ctx, str);
4045040460
return obj;

quickjs.h

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,6 @@ static inline JSValue __JS_NewFloat64(double d)
224224
return JS_MKVAL(JS_TAG_FLOAT64, (int)d);
225225
}
226226

227-
static inline JSValue __JS_NewShortBigInt(JSContext *ctx, int32_t d)
228-
{
229-
(void)&ctx;
230-
return JS_MKVAL(JS_TAG_SHORT_BIG_INT, d);
231-
}
232-
233227
static inline bool JS_VALUE_IS_NAN(JSValue v)
234228
{
235229
(void)&v;
@@ -280,12 +274,6 @@ static inline JSValue __JS_NewFloat64(double d)
280274
return v;
281275
}
282276

283-
static inline JSValue __JS_NewShortBigInt(JSContext *ctx, int32_t d)
284-
{
285-
(void)&ctx;
286-
return JS_MKVAL(JS_TAG_SHORT_BIG_INT, d);
287-
}
288-
289277
#define JS_TAG_IS_FLOAT64(tag) ((unsigned)((tag) - JS_TAG_FIRST) >= (JS_TAG_FLOAT64 - JS_TAG_FIRST))
290278

291279
/* same as JS_VALUE_GET_TAG, but return JS_TAG_FLOAT64 with NaN boxing */
@@ -372,15 +360,6 @@ static inline JSValue __JS_NewFloat64(double d)
372360
return v;
373361
}
374362

375-
static inline JSValue __JS_NewShortBigInt(JSContext *ctx, int64_t d)
376-
{
377-
(void)&ctx;
378-
JSValue v;
379-
v.tag = JS_TAG_SHORT_BIG_INT;
380-
v.u.short_big_int = d;
381-
return v;
382-
}
383-
384363
static inline bool JS_VALUE_IS_NAN(JSValue v)
385364
{
386365
union {
@@ -679,7 +658,7 @@ JS_EXTERN void JS_FreeAtomRT(JSRuntime *rt, JSAtom v);
679658
JS_EXTERN JSValue JS_AtomToValue(JSContext *ctx, JSAtom atom);
680659
JS_EXTERN JSValue JS_AtomToString(JSContext *ctx, JSAtom atom);
681660
JS_EXTERN const char *JS_AtomToCStringLen(JSContext *ctx, size_t *plen, JSAtom atom);
682-
static inline const char *JS_AtomToCString(JSContext *ctx, JSAtom atom)
661+
static inline const char *JS_AtomToCString(JSContext *ctx, JSAtom atom)
683662
{
684663
return JS_AtomToCStringLen(ctx, NULL, atom);
685664
}
@@ -818,6 +797,17 @@ static inline JSValue JS_NewUint32(JSContext *ctx, uint32_t val)
818797
return v;
819798
}
820799

800+
static inline JSValue JS_NewUint64(JSContext *ctx, uint64_t val)
801+
{
802+
JSValue v;
803+
if (val <= INT32_MAX) {
804+
v = JS_NewInt32(ctx, (int32_t)val);
805+
} else {
806+
v = JS_NewFloat64(ctx, (double)val);
807+
}
808+
return v;
809+
}
810+
821811
JS_EXTERN JSValue JS_NewNumber(JSContext *ctx, double d);
822812
JS_EXTERN JSValue JS_NewBigInt64(JSContext *ctx, int64_t v);
823813
JS_EXTERN JSValue JS_NewBigUint64(JSContext *ctx, uint64_t v);

tests/bug1498.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { assert } from "./assert.js";
2+
3+
function test_invalid_number_literal_location()
4+
{
5+
let error;
6+
const source =
7+
"function fun() {\n" +
8+
" let a = 123bcd;\n" +
9+
"}\n";
10+
11+
try {
12+
eval(source);
13+
} catch (e) {
14+
error = e;
15+
}
16+
17+
assert(error instanceof SyntaxError);
18+
assert(error.message, "invalid number literal");
19+
assert(error.stack.length >= 1);
20+
assert(error.stack[0].getFileName(), "<input>");
21+
assert(error.stack[0].getLineNumber(), 2);
22+
assert(error.stack[0].getColumnNumber(), 13);
23+
}
24+
25+
Error.prepareStackTrace = (_, frames) => frames;
26+
try {
27+
test_invalid_number_literal_location();
28+
} finally {
29+
Error.prepareStackTrace = undefined;
30+
}

0 commit comments

Comments
 (0)