Skip to content

Commit

Permalink
Add C++ compile test
Browse files Browse the repository at this point in the history
Check that quickjs.h parses without error when fed to a C++ compiler.

Co-authored-by: Saúl Ibarra Corretgé <s@saghul.net>
  • Loading branch information
bnoordhuis and saghul committed Oct 26, 2024
1 parent 82607f4 commit 5a089f6
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 8 deletions.
42 changes: 42 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ jobs:
run: |
make stats
- name: cxxtest
run: |
make cxxtest
- name: test
if: ${{ matrix.config.configType != 'examples' && matrix.config.configType != 'tcc' }}
run: |
Expand Down Expand Up @@ -211,6 +215,14 @@ jobs:
build\${{matrix.buildType}}\qjs.exe examples\test_point.js
build\${{matrix.buildType}}\run-test262.exe -c tests.conf
build\${{matrix.buildType}}\function_source.exe
- name: Set up Visual Studio shell
uses: egor-tensin/vs-shell@v2
with:
arch: ${{ matrix.arch == 'x64' && 'x64' || 'x86' }}
- name: cxxtest
run: |
cl.exe /DJS_NAN_BOXING=0 /Zs cxxtest.cc
cl.exe /DJS_NAN_BOXING=1 /Zs cxxtest.cc
windows-msvc-vs2019:
runs-on: windows-2019
Expand All @@ -228,6 +240,14 @@ jobs:
- name: stats
run: |
build\${{matrix.buildType}}\qjs.exe -qd
- name: Set up Visual Studio shell
uses: egor-tensin/vs-shell@v2
with:
arch: ${{ matrix.arch == 'x64' && 'x64' || 'x86' }}
- name: cxxtest
run: |
cl.exe /DJS_NAN_BOXING=0 /Zs cxxtest.cc
cl.exe /DJS_NAN_BOXING=1 /Zs cxxtest.cc
windows-clang:
runs-on: windows-latest
Expand All @@ -244,6 +264,10 @@ jobs:
- name: stats
run: |
build\${{matrix.buildType}}\qjs.exe -qd
- name: cxxtest
run: |
clang-cl.exe /DJS_NAN_BOXING=0 /Zs cxxtest.cc
clang-cl.exe /DJS_NAN_BOXING=1 /Zs cxxtest.cc
- name: test
run: |
cp build\${{matrix.buildType}}\fib.dll examples\
Expand Down Expand Up @@ -315,6 +339,14 @@ jobs:
run: |
make stats
ldd build/qjs
- name: cxxtest
if: ${{ matrix.sys != 'clang64' }}
run: |
make cxxtest
- name: cxxtest
if: ${{ matrix.sys == 'clang64' }}
run: |
make cxxtest CXX=clang++
- name: test
run: |
make test
Expand Down Expand Up @@ -395,6 +427,10 @@ jobs:
- name: stats
run: make stats

- name: cxxtest
run: |
make cxxtest
- name: test
run: make test

Expand Down Expand Up @@ -467,6 +503,9 @@ jobs:
- name: build
run: |
make
- name: cxxtest
run: |
make cxxtest
- name: test
run: |
make test
Expand All @@ -484,6 +523,9 @@ jobs:
- name: build
run: |
make
- name: cxxtest
run: |
make cxxtest
- name: test
run: |
make test
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ distclean:
stats: $(QJS)
$(QJS) -qd

# implicitly .PHONY because it doesn't generate output
cxxtest: CXXFLAGS+=-std=c++11 -fsyntax-only -Wall -Wextra -Werror -Wno-unused-parameter
cxxtest: cxxtest.cc quickjs.h
$(CXX) $(CXXFLAGS) -DJS_NAN_BOXING=0 $<
$(CXX) $(CXXFLAGS) -DJS_NAN_BOXING=1 $<

test: $(QJS)
$(RUN262) -c tests.conf

Expand Down Expand Up @@ -110,4 +116,4 @@ unicode_gen: $(BUILD_DIR)
libunicode-table.h: unicode_gen
$(BUILD_DIR)/unicode_gen unicode $@

.PHONY: all debug fuzz install clean codegen distclean stats test test262 test262-update test262-check microbench unicode_gen $(QJS) $(QJSC)
.PHONY: all cxxtest debug fuzz install clean codegen distclean stats test test262 test262-update test262-check microbench unicode_gen $(QJS) $(QJSC)
37 changes: 37 additions & 0 deletions cxxtest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// note: file is not actually compiled, only checked for C++ syntax errors
#include "quickjs.h"

JSCFunctionListEntry funcs[] = {
JS_ALIAS_BASE_DEF("", "", 0),
JS_ALIAS_DEF("", ""),
JS_CFUNC_DEF("", 0, 0),
JS_CFUNC_DEF2("", 0, 0, 0),
JS_CFUNC_MAGIC_DEF("", 0, 0, 0),
JS_CFUNC_SPECIAL_DEF("", 0, f_f, 0),
JS_CFUNC_SPECIAL_DEF("", 0, f_f_f, 0),
JS_CGETSET_DEF("", 0, 0),
JS_CGETSET_DEF2("", 0, 0, 0),
JS_CGETSET_MAGIC_DEF("", 0, 0, 0),
JS_ITERATOR_NEXT_DEF("", 0, 0, 0),
JS_OBJECT_DEF("", 0, 0, 0),
JS_PROP_DOUBLE_DEF("", 0, 0),
JS_PROP_INT32_DEF("", 0, 0),
JS_PROP_INT64_DEF("", 0, 0),
JS_PROP_STRING_DEF("", "", 0),
JS_PROP_UNDEFINED_DEF("", 0),
};

int main(void)
{
JSRuntime *rt = JS_NewRuntime();
JSContext *ctx = JS_NewContext(rt);
JS_FreeValue(ctx, JS_NAN);
JS_FreeValue(ctx, JS_UNDEFINED);
JS_FreeValue(ctx, JS_NewFloat64(ctx, 42));
// not a legal way of using JS_MKPTR but this is here
// to have the compiler syntax-check its definition
JS_FreeValue(ctx, JS_MKPTR(JS_TAG_UNINITIALIZED, 0));
JS_FreeContext(ctx);
JS_FreeRuntime(rt);
return 0;
}
45 changes: 38 additions & 7 deletions quickjs.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ typedef uint32_t JSAtom;
- string contents is either pure ASCII or is UTF-8 encoded.
*/

/* Overridable purely for testing purposes; don't touch. */
#ifndef JS_NAN_BOXING
#if INTPTR_MAX < INT64_MAX
/* Use NAN boxing for 32bit builds. */
#define JS_NAN_BOXING
#define JS_NAN_BOXING 1 /* Use NAN boxing for 32bit builds. */
#endif
#endif

enum {
Expand All @@ -90,7 +92,7 @@ enum {
#define JS_FLOAT64_NAN NAN
#define JSValueConst JSValue /* For backwards compatibility. */

#if defined(JS_NAN_BOXING)
#if defined(JS_NAN_BOXING) && JS_NAN_BOXING

typedef uint64_t JSValue;

Expand Down Expand Up @@ -174,13 +176,41 @@ typedef struct JSValue {
#define JS_VALUE_GET_FLOAT64(v) ((v).u.float64)
#define JS_VALUE_GET_PTR(v) ((v).u.ptr)

/* msvc doesn't understand designated initializers without /std:c++20 */
#ifdef __cplusplus
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 = JS_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 */
#else
#define JS_MKPTR(tag, p) (JSValue){ (JSValueUnion){ .ptr = p }, tag }
#define JS_MKVAL(tag, val) (JSValue){ (JSValueUnion){ .int32 = val }, tag }
#define JS_MKPTR(tag, p) (JSValue){ (JSValueUnion){ .ptr = p }, tag }
#define JS_NAN (JSValue){ (JSValueUnion){ .float64 = JS_FLOAT64_NAN }, JS_TAG_FLOAT64 }
#endif

#define JS_TAG_IS_FLOAT64(tag) ((unsigned)(tag) == JS_TAG_FLOAT64)

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

static inline JSValue __JS_NewFloat64(double d)
{
JSValue v;
Expand Down Expand Up @@ -900,7 +930,8 @@ static inline JSValue JS_NewCFunctionMagic(JSContext *ctx, JSCFunctionMagic *fun
int length, JSCFunctionEnum cproto, int magic)
{
/* Used to squelch a -Wcast-function-type warning. */
JSCFunctionType ft = { .generic_magic = func };
JSCFunctionType ft;
ft.generic_magic = func;
return JS_NewCFunction2(ctx, ft.generic, name, length, cproto, magic);
}
JS_EXTERN void JS_SetConstructor(JSContext *ctx, JSValue func_obj,
Expand Down

0 comments on commit 5a089f6

Please sign in to comment.