Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 31 additions & 16 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,28 @@ set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")

message(STATUS "QuickJS Library: Begin")

# For now, avoid including the quickjs-ng CMake configuration directly, and explicitly specify our own
# add_subdirectory(quickjs-ng)

set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS ON)
set(CMAKE_C_STANDARD 11)

set(QUICKJS_HEADERS
quickjs-ng/cutils.h
quickjs-ng/dtoa.h
quickjs-ng/libregexp.h
quickjs-ng/libunicode.h
quickjs-ng/list.h
quickjs-ng/quickjs.h
quickjs/cutils.h
quickjs/dtoa.h
quickjs/libregexp.h
quickjs/libunicode.h
quickjs/list.h
quickjs/quickjs.h
# extensions
quickjs-wz-extensions/quickjs-debugger.h
quickjs-wz-extensions/quickjs-limitedcontext.h
)

set(QUICKJS_SOURCES
quickjs-ng/cutils.c
quickjs-ng/dtoa.c
quickjs-ng/libregexp.c
quickjs-ng/libunicode.c
quickjs-ng/quickjs.c
quickjs/cutils.c
quickjs/dtoa.c
quickjs/libregexp.c
quickjs/libunicode.c
quickjs/quickjs.c
# extensions
# INCLUDED DIRECTLY IN QUICKJS.C: quickjs-wz-extensions/quickjs-debugger.c
# INCLUDED DIRECTLY IN QUICKJS.C: quickjs-wz-extensions/quickjs-limitedcontext.c
Expand All @@ -52,10 +49,20 @@ if(WIN32)
list(APPEND qjs_defines WIN32_LEAN_AND_MEAN _WIN32_WINNT=0x0601)
endif()

file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/quickjs/VERSION.txt" QUICKJS_VERSION_STR)
list(APPEND qjs_defines CONFIG_VERSION="${QUICKJS_VERSION_STR}")

include(CheckIncludeFiles)
CHECK_INCLUDE_FILES("sys/time.h" HAVE_SYS_TIME_H LANGUAGE C)
include(CheckSymbolExists)
check_symbol_exists(bswap16 "stdlib.h" HAVE_BSWAP16)
check_symbol_exists(bswap32 "stdlib.h" HAVE_BSWAP32)
check_symbol_exists(bswap64 "stdlib.h" HAVE_BSWAP64)

add_library(qjs STATIC ${QUICKJS_HEADERS} ${QUICKJS_SOURCES})
target_compile_definitions(qjs PRIVATE ${qjs_defines})
target_include_directories(qjs PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/quickjs-ng
${CMAKE_CURRENT_SOURCE_DIR}/quickjs
${CMAKE_CURRENT_SOURCE_DIR}/quickjs-wz-extensions
)

Expand All @@ -64,6 +71,10 @@ set(THREADS_PREFER_PTHREAD_FLAG TRUE)
find_package(Threads REQUIRED)
target_link_libraries(qjs PRIVATE Threads::Threads)

if(HAVE_SYS_TIME_H)
target_compile_definitions(qjs PRIVATE QUICKJS_HAVE_SYS_TIME_H)
endif()

if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
# GCC 9.3.0+ has been tested to be fine at -O2
if((CMAKE_C_COMPILER_VERSION VERSION_LESS 9.3) AND (CMAKE_C_COMPILER_VERSION VERSION_GREATER 7.0))
Expand All @@ -73,7 +84,7 @@ if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
endif()
endif()

# Alias the quickjs-ng "qjs" library name to the previous library name that WZ used
# Alias the "qjs" library name to the previous library name that WZ used
add_library(quickjs ALIAS qjs)

include(CheckCCompilerFlag)
Expand Down Expand Up @@ -111,7 +122,11 @@ if(MSVC)
xcheck_add_c_compiler_flag(/wd4996) # -Wdeprecated-declarations
xcheck_add_c_compiler_flag(/wd5045) # Compiler will insert Spectre mitigation for memory load if /Qspectre switch specified

xcheck_add_c_compiler_flag(/wd4115) # warning C4115: named type definition in parentheses
xcheck_add_c_compiler_flag(/wd4127) # warning C4127: conditional expression is constant
xcheck_add_c_compiler_flag(/wd4132) # warning C4132: const object should be initialized
xcheck_add_c_compiler_flag(/wd4146) # warning C4146: unary minus operator applied to unsigned type, result still unsigned
xcheck_add_c_compiler_flag(/wd4295) # warning C4295: 'digits': array is too small to include a terminating null character
xcheck_add_c_compiler_flag(/wd4456) # warning C4456: declaration of 'var' hides previous local declaration
xcheck_add_c_compiler_flag(/wd4457) # warning C4457: declaration of 'var' hides function parameter

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# QuickJS (modified)

This repo contains [quickjs-ng](https://github.com/quickjs-ng/quickjs), with modifications and patches required by Warzone 2100.
This repo contains [quickjs](https://github.com/bellard/quickjs), with modifications and patches required by Warzone 2100.

For a breakdown of the patches applied for Warzone 2100, see the commit history and/or the [`patches/`](patches/) subdirectory.

For more information on upstream QuickJS, see:
https://github.com/quickjs-ng/quickjs
https://github.com/bellard/quickjs
14 changes: 7 additions & 7 deletions patches/001-add-extensions.patch
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
diff --git a/quickjs-ng/quickjs.c b/quickjs-ng/quickjs.c
--- a/quickjs-ng/quickjs.c
+++ b/quickjs-ng/quickjs.c
@@ -58122,3 +58122,6 @@ uintptr_t js_std_cmd(int cmd, ...) {
#undef malloc
#undef free
#undef realloc
diff --git a/quickjs/quickjs.c b/quickjs/quickjs.c
--- a/quickjs/quickjs.c
+++ b/quickjs/quickjs.c
@@ -58552,3 +58552,6 @@ int JS_AddIntrinsicWeakRef(JSContext *ctx)
JS_FreeValue(ctx, obj);
return 0;
}
+
+#include <quickjs-debugger.c>
+#include <quickjs-limitedcontext.c>
18 changes: 9 additions & 9 deletions patches/002-add-disable-atomics-define.patch
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
diff --git a/quickjs-ng/quickjs.c b/quickjs-ng/quickjs.c
--- a/quickjs-ng/quickjs.c
+++ b/quickjs-ng/quickjs.c
diff --git a/quickjs/quickjs.c b/quickjs/quickjs.c
--- a/quickjs/quickjs.c
+++ b/quickjs/quickjs.c
@@ -68,7 +68,7 @@
// atomic_store etc. are completely busted in recent versions of tcc;
// somehow the compiler forgets to load |ptr| into %rdi when calling
// the __atomic_*() helpers in its lib/stdatomic.c and lib/atomic.S
-#if !defined(__TINYC__) && !defined(EMSCRIPTEN) && !defined(__wasi__) && !__STDC_NO_ATOMICS__
+#if !defined(__TINYC__) && !defined(EMSCRIPTEN) && !defined(__wasi__) && !defined(QJS_DISABLE_ATOMICS) && !__STDC_NO_ATOMICS__
#include "quickjs-c-atomics.h"

/* define to include Atomics.* operations which depend on the OS
threads */
-#if !defined(EMSCRIPTEN)
+#if !defined(EMSCRIPTEN) && !defined(QJS_DISABLE_ATOMICS)
#define CONFIG_ATOMICS
#endif

42 changes: 42 additions & 0 deletions patches/003-fix-pedantic-cxx-warnings.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
diff --git a/quickjs/quickjs.h b/quickjs/quickjs.h
--- a/quickjs/quickjs.h
+++ b/quickjs/quickjs.h
@@ -248,6 +248,38 @@ typedef struct JSValue {

#define JS_NAN (JSValue){ .u.float64 = JS_FLOAT64_NAN, JS_TAG_FLOAT64 }

+/* msvc doesn't understand designated initializers without /std:c++20 */
+#ifdef __cplusplus
+#undef JS_MKVAL
+#undef JS_MKPTR
+#undef JS_NAN
+static inline JSValue JS_MKPTR(int64_t tag, void *ptr)
+{
+ JSValue v;
+ v.u.ptr = ptr;
+ v.tag = tag;
+ return v;
+}
+static inline JSValue JS_MKVAL(int64_t tag, int32_t int32)
+{
+ JSValue v;
+ v.u.int32 = int32;
+ v.tag = tag;
+ return v;
+}
+static inline JSValue JS_MKNAN(void)
+{
+ JSValue v;
+ v.u.float64 = NAN;
+ v.tag = JS_TAG_FLOAT64;
+ return v;
+}
+/* provide as macros for consistency and backward compat reasons */
+#define JS_MKPTR(tag, ptr) JS_MKPTR(tag, ptr)
+#define JS_MKVAL(tag, val) JS_MKVAL(tag, val)
+#define JS_NAN JS_MKNAN() /* alas, not a constant expression */
+#endif
+
static inline JSValue __JS_NewFloat64(JSContext *ctx, double d)
{
JSValue v;
39 changes: 0 additions & 39 deletions patches/003-freeruntime2.patch

This file was deleted.

99 changes: 99 additions & 0 deletions patches/004-local-math-wrappers.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
diff --git a/quickjs/quickjs.c b/quickjs/quickjs.c
--- a/quickjs/quickjs.c
+++ b/quickjs/quickjs.c
@@ -45907,39 +45907,69 @@ static JSValue js_math_random(JSContext *ctx, JSValueConst this_val,
return __JS_NewFloat64(ctx, u.d - 1.0);
}

+/* use local wrappers for math functions to
+ - avoid initializing data with dynamic library entry points.
+ - avoid some overhead if the call can be inlined at compile or link time.
+ */
+static double js_math_fabs(double d) { return fabs(d); }
+static double js_math_floor(double d) { return floor(d); }
+static double js_math_ceil(double d) { return ceil(d); }
+static double js_math_sqrt(double d) { return sqrt(d); }
+static double js_math_acos(double d) { return acos(d); }
+static double js_math_asin(double d) { return asin(d); }
+static double js_math_atan(double d) { return atan(d); }
+static double js_math_atan2(double a, double b) { return atan2(a, b); }
+static double js_math_cos(double d) { return cos(d); }
+static double js_math_exp(double d) { return exp(d); }
+static double js_math_log(double d) { return log(d); }
+static double js_math_sin(double d) { return sin(d); }
+static double js_math_tan(double d) { return tan(d); }
+static double js_math_trunc(double d) { return trunc(d); }
+static double js_math_cosh(double d) { return cosh(d); }
+static double js_math_sinh(double d) { return sinh(d); }
+static double js_math_tanh(double d) { return tanh(d); }
+static double js_math_acosh(double d) { return acosh(d); }
+static double js_math_asinh(double d) { return asinh(d); }
+static double js_math_atanh(double d) { return atanh(d); }
+static double js_math_expm1(double d) { return expm1(d); }
+static double js_math_log1p(double d) { return log1p(d); }
+static double js_math_log2(double d) { return log2(d); }
+static double js_math_log10(double d) { return log10(d); }
+static double js_math_cbrt(double d) { return cbrt(d); }
+
static const JSCFunctionListEntry js_math_funcs[] = {
JS_CFUNC_MAGIC_DEF("min", 2, js_math_min_max, 0 ),
JS_CFUNC_MAGIC_DEF("max", 2, js_math_min_max, 1 ),
- JS_CFUNC_SPECIAL_DEF("abs", 1, f_f, fabs ),
- JS_CFUNC_SPECIAL_DEF("floor", 1, f_f, floor ),
- JS_CFUNC_SPECIAL_DEF("ceil", 1, f_f, ceil ),
+ JS_CFUNC_SPECIAL_DEF("abs", 1, f_f, js_math_fabs ),
+ JS_CFUNC_SPECIAL_DEF("floor", 1, f_f, js_math_floor ),
+ JS_CFUNC_SPECIAL_DEF("ceil", 1, f_f, js_math_ceil ),
JS_CFUNC_SPECIAL_DEF("round", 1, f_f, js_math_round ),
- JS_CFUNC_SPECIAL_DEF("sqrt", 1, f_f, sqrt ),
-
- JS_CFUNC_SPECIAL_DEF("acos", 1, f_f, acos ),
- JS_CFUNC_SPECIAL_DEF("asin", 1, f_f, asin ),
- JS_CFUNC_SPECIAL_DEF("atan", 1, f_f, atan ),
- JS_CFUNC_SPECIAL_DEF("atan2", 2, f_f_f, atan2 ),
- JS_CFUNC_SPECIAL_DEF("cos", 1, f_f, cos ),
- JS_CFUNC_SPECIAL_DEF("exp", 1, f_f, exp ),
- JS_CFUNC_SPECIAL_DEF("log", 1, f_f, log ),
+ JS_CFUNC_SPECIAL_DEF("sqrt", 1, f_f, js_math_sqrt ),
+
+ JS_CFUNC_SPECIAL_DEF("acos", 1, f_f, js_math_acos ),
+ JS_CFUNC_SPECIAL_DEF("asin", 1, f_f, js_math_asin ),
+ JS_CFUNC_SPECIAL_DEF("atan", 1, f_f, js_math_atan ),
+ JS_CFUNC_SPECIAL_DEF("atan2", 2, f_f_f, js_math_atan2 ),
+ JS_CFUNC_SPECIAL_DEF("cos", 1, f_f, js_math_cos ),
+ JS_CFUNC_SPECIAL_DEF("exp", 1, f_f, js_math_exp ),
+ JS_CFUNC_SPECIAL_DEF("log", 1, f_f, js_math_log ),
JS_CFUNC_SPECIAL_DEF("pow", 2, f_f_f, js_pow ),
- JS_CFUNC_SPECIAL_DEF("sin", 1, f_f, sin ),
- JS_CFUNC_SPECIAL_DEF("tan", 1, f_f, tan ),
+ JS_CFUNC_SPECIAL_DEF("sin", 1, f_f, js_math_sin ),
+ JS_CFUNC_SPECIAL_DEF("tan", 1, f_f, js_math_tan ),
/* ES6 */
- JS_CFUNC_SPECIAL_DEF("trunc", 1, f_f, trunc ),
+ JS_CFUNC_SPECIAL_DEF("trunc", 1, f_f, js_math_trunc ),
JS_CFUNC_SPECIAL_DEF("sign", 1, f_f, js_math_sign ),
- JS_CFUNC_SPECIAL_DEF("cosh", 1, f_f, cosh ),
- JS_CFUNC_SPECIAL_DEF("sinh", 1, f_f, sinh ),
- JS_CFUNC_SPECIAL_DEF("tanh", 1, f_f, tanh ),
- JS_CFUNC_SPECIAL_DEF("acosh", 1, f_f, acosh ),
- JS_CFUNC_SPECIAL_DEF("asinh", 1, f_f, asinh ),
- JS_CFUNC_SPECIAL_DEF("atanh", 1, f_f, atanh ),
- JS_CFUNC_SPECIAL_DEF("expm1", 1, f_f, expm1 ),
- JS_CFUNC_SPECIAL_DEF("log1p", 1, f_f, log1p ),
- JS_CFUNC_SPECIAL_DEF("log2", 1, f_f, log2 ),
- JS_CFUNC_SPECIAL_DEF("log10", 1, f_f, log10 ),
- JS_CFUNC_SPECIAL_DEF("cbrt", 1, f_f, cbrt ),
+ JS_CFUNC_SPECIAL_DEF("cosh", 1, f_f, js_math_cosh ),
+ JS_CFUNC_SPECIAL_DEF("sinh", 1, f_f, js_math_sinh ),
+ JS_CFUNC_SPECIAL_DEF("tanh", 1, f_f, js_math_tanh ),
+ JS_CFUNC_SPECIAL_DEF("acosh", 1, f_f, js_math_acosh ),
+ JS_CFUNC_SPECIAL_DEF("asinh", 1, f_f, js_math_asinh ),
+ JS_CFUNC_SPECIAL_DEF("atanh", 1, f_f, js_math_atanh ),
+ JS_CFUNC_SPECIAL_DEF("expm1", 1, f_f, js_math_expm1 ),
+ JS_CFUNC_SPECIAL_DEF("log1p", 1, f_f, js_math_log1p ),
+ JS_CFUNC_SPECIAL_DEF("log2", 1, f_f, js_math_log2 ),
+ JS_CFUNC_SPECIAL_DEF("log10", 1, f_f, js_math_log10 ),
+ JS_CFUNC_SPECIAL_DEF("cbrt", 1, f_f, js_math_cbrt ),
JS_CFUNC_DEF("hypot", 2, js_math_hypot ),
JS_CFUNC_DEF("random", 0, js_math_random ),
JS_CFUNC_SPECIAL_DEF("f16round", 1, f_f, js_math_f16round ),
Loading