Skip to content
Open
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
8 changes: 7 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ jobs:
- { os: ubuntu-latest, configType: ubsan, runTest262: true }
- { os: ubuntu-latest, configType: msan }
- { os: ubuntu-latest, configType: tcc }
- { os: ubuntu-latest, configType: cpp }
- { os: ubuntu-latest, arch: x86 }
- { os: ubuntu-latest, arch: riscv64 }
- { os: ubuntu-latest, arch: s390x }
Expand All @@ -58,13 +59,15 @@ jobs:
- { os: macos-14, configType: shared }
- { os: macos-14, configType: asan }
- { os: macos-14, configType: ubsan }
- { os: macos-14, configType: cpp }

- { os: macos-12, configType: Debug }
- { os: macos-12, configType: Release }
- { os: macos-12, configType: examples }
- { os: macos-12, configType: shared }
- { os: macos-12, configType: asan }
- { os: macos-12, configType: ubsan }
- { os: macos-12, configType: cpp }
steps:
- uses: actions/checkout@v4
with:
Expand Down Expand Up @@ -109,6 +112,8 @@ jobs:
echo "CONFIG_ASAN=ON" >> $GITHUB_ENV;
elif [ "${{ matrix.config.configType }}" = "ubsan" ]; then
echo "CONFIG_UBSAN=ON" >> $GITHUB_ENV;
elif [ "${{ matrix.config.configType }}" = "cpp" ]; then
echo "CONFIG_CPP=ON" >> $GITHUB_ENV;
elif [ "${{ matrix.config.configType }}" = "msan" ]; then
echo "CONFIG_MSAN=ON" >> $GITHUB_ENV;
echo "CC=clang" >> $GITHUB_ENV;
Expand All @@ -122,7 +127,8 @@ jobs:
BUILD_SHARED_LIBS=$BUILD_SHARED_LIBS \
CONFIG_ASAN=$CONFIG_ASAN \
CONFIG_UBSAN=$CONFIG_UBSAN \
CONFIG_MSAN=$CONFIG_MSAN
CONFIG_MSAN=$CONFIG_MSAN \
CONFIG_CPP=$CONFIG_CPP

- name: stats
if: ${{ matrix.config.configType != 'examples' }}
Expand Down
43 changes: 28 additions & 15 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
cmake_minimum_required(VERSION 3.9)

project(quickjs LANGUAGES C)
project(quickjs LANGUAGES C CXX)

include(CheckCCompilerFlag)
include(GNUInstallDirs)

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

macro(xoption OPTION_NAME OPTION_TEXT OPTION_DEFAULT)
option(${OPTION_NAME} ${OPTION_TEXT} ${OPTION_DEFAULT})
if(DEFINED ENV{${OPTION_NAME}})
# Allow setting the option through an environment variable.
set(${OPTION_NAME} $ENV{${OPTION_NAME}})
endif()
if(${OPTION_NAME})
add_definitions(-D${OPTION_NAME})
endif()
message(STATUS " ${OPTION_NAME}: ${${OPTION_NAME}}")
endmacro()
xoption(CONFIG_CPP "Compile as C++" OFF)

if (NOT CONFIG_CPP)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS ON)
set(CMAKE_C_STANDARD 11)
endif()

if(NOT CMAKE_BUILD_TYPE)
message(STATUS "No build type selected, default to Release")
Expand Down Expand Up @@ -76,17 +92,6 @@ if(CMAKE_BUILD_TYPE MATCHES "Debug")
xcheck_add_c_compiler_flag(-fno-omit-frame-pointer)
endif()

macro(xoption OPTION_NAME OPTION_TEXT OPTION_DEFAULT)
option(${OPTION_NAME} ${OPTION_TEXT} ${OPTION_DEFAULT})
if(DEFINED ENV{${OPTION_NAME}})
# Allow setting the option through an environment variable.
set(${OPTION_NAME} $ENV{${OPTION_NAME}})
endif()
if(${OPTION_NAME})
add_definitions(-D${OPTION_NAME})
endif()
message(STATUS " ${OPTION_NAME}: ${${OPTION_NAME}}")
endmacro()

xoption(BUILD_SHARED_LIBS "Build a shared library" OFF)
if(BUILD_SHARED_LIBS)
Expand Down Expand Up @@ -159,6 +164,14 @@ set(qjs_sources
quickjs.c
)

if (CONFIG_CPP)
file(GLOB_RECURSE CFILES "*.c")
set_source_files_properties(${CFILES} PROPERTIES LANGUAGE CXX)
add_compile_options(
-x c++
)
endif()

if(BUILD_QJS_LIBC)
list(APPEND qjs_sources quickjs-libc.c)
endif()
Expand Down
6 changes: 3 additions & 3 deletions cutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ int dbuf_realloc(DynBuf *s, size_t new_size)
size = s->allocated_size * 3 / 2;
if (size > new_size)
new_size = size;
new_buf = s->realloc_func(s->opaque, s->buf, new_size);
new_buf = (uint8_t *)s->realloc_func(s->opaque, s->buf, new_size);
if (!new_buf) {
s->error = TRUE;
return -1;
Expand Down Expand Up @@ -588,7 +588,7 @@ size_t utf8_encode_buf16(char *dest, size_t dest_len, const uint16_t *src, size_
*/

/* 2 <= base <= 36 */
char const digits36[36] = "0123456789abcdefghijklmnopqrstuvwxyz";
char const digits36[37] = "0123456789abcdefghijklmnopqrstuvwxyz";

#define USE_SPECIAL_RADIX_10 1 // special case base 10 radix conversions
#define USE_SINGLE_CASE_FAST 1 // special case single digit numbers
Expand Down Expand Up @@ -1039,7 +1039,7 @@ void rqsort(void *base, size_t nmemb, size_t size, cmp_f cmp, void *opaque)
/* select median of 3 from 1/4, 1/2, 3/4 positions */
/* should use median of 5 or 9? */
m4 = (nmemb >> 2) * size;
m = med3(ptr + m4, ptr + 2 * m4, ptr + 3 * m4, cmp, opaque);
m = (uint8_t *)med3(ptr + m4, ptr + 2 * m4, ptr + 3 * m4, cmp, opaque);
swap(ptr, m, size); /* move the pivot to the start or the array */
i = lt = 1;
pi = plt = ptr + size;
Expand Down
4 changes: 2 additions & 2 deletions cutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ static inline int clz64(uint64_t a)
return clz32((unsigned)(a >> 32));
else
return clz32((unsigned)a) + 32;
#endif
#endif
#else
return __builtin_clzll(a);
#endif
Expand Down Expand Up @@ -465,7 +465,7 @@ static inline uint8_t to_upper_ascii(uint8_t c) {
return c >= 'a' && c <= 'z' ? c - 'a' + 'A' : c;
}

extern char const digits36[36];
extern char const digits36[37];
size_t u32toa(char buf[minimum_length(11)], uint32_t n);
size_t i32toa(char buf[minimum_length(12)], int32_t n);
size_t u64toa(char buf[minimum_length(21)], uint64_t n);
Expand Down
7 changes: 6 additions & 1 deletion gen/repl.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

#include <inttypes.h>

#ifdef __cplusplus
extern
#endif
const uint32_t qjsc_repl_size = 22701;

#ifdef __cplusplus
extern
#endif
const uint8_t qjsc_repl[22701] = {
0x0c, 0x91, 0x04, 0x0e, 0x72, 0x65, 0x70, 0x6c,
0x2e, 0x6a, 0x73, 0x06, 0x73, 0x74, 0x64, 0x04,
Expand Down Expand Up @@ -2844,4 +2850,3 @@ const uint8_t qjsc_repl[22701] = {
0x02, 0x29, 0xbe, 0x00, 0x38, 0x8a, 0x00, 0x00,
0x00, 0xee, 0x0e, 0x06, 0x2e,
};

Loading