Skip to content

Commit 9a3e3b6

Browse files
bnoordhuissaghul
andcommitted
Add C++ compile test
Check that quickjs.h parses without error when fed to a C++ compiler. Co-authored-by: Saúl Ibarra Corretgé <s@saghul.net>
1 parent 3339ef7 commit 9a3e3b6

File tree

4 files changed

+94
-7
lines changed

4 files changed

+94
-7
lines changed

.github/workflows/ci.yml

+37
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ jobs:
135135
run: |
136136
make stats
137137
138+
- name: cxxtest
139+
run: |
140+
make cxxtest
141+
138142
- name: test
139143
if: ${{ matrix.config.configType != 'examples' && matrix.config.configType != 'tcc' }}
140144
run: |
@@ -211,6 +215,14 @@ jobs:
211215
build\${{matrix.buildType}}\qjs.exe examples\test_point.js
212216
build\${{matrix.buildType}}\run-test262.exe -c tests.conf
213217
build\${{matrix.buildType}}\function_source.exe
218+
- name: Set up Visual Studio shell
219+
uses: egor-tensin/vs-shell@v2
220+
with:
221+
arch: ${{ matrix.arch == 'x64' && 'x64' || 'x86' }}
222+
- name: cxxtest
223+
run: |
224+
cl.exe /std:c++11 /DJS_NAN_BOXING=0 /Zs cxxtest.cc
225+
cl.exe /std:c++11 /DJS_NAN_BOXING=1 /Zs cxxtest.cc
214226
215227
windows-msvc-vs2019:
216228
runs-on: windows-2019
@@ -228,6 +240,14 @@ jobs:
228240
- name: stats
229241
run: |
230242
build\${{matrix.buildType}}\qjs.exe -qd
243+
- name: Set up Visual Studio shell
244+
uses: egor-tensin/vs-shell@v2
245+
with:
246+
arch: ${{ matrix.arch == 'x64' && 'x64' || 'x86' }}
247+
- name: cxxtest
248+
run: |
249+
cl.exe /std:c++11 /DJS_NAN_BOXING=0 /Zs cxxtest.cc
250+
cl.exe /std:c++11 /DJS_NAN_BOXING=1 /Zs cxxtest.cc
231251
232252
windows-clang:
233253
runs-on: windows-latest
@@ -244,6 +264,10 @@ jobs:
244264
- name: stats
245265
run: |
246266
build\${{matrix.buildType}}\qjs.exe -qd
267+
- name: cxxtest
268+
run: |
269+
clang-cl.exe /std:c++11 /DJS_NAN_BOXING=0 /Zs cxxtest.cc
270+
clang-cl.exe /std:c++11 /DJS_NAN_BOXING=1 /Zs cxxtest.cc
247271
- name: test
248272
run: |
249273
cp build\${{matrix.buildType}}\fib.dll examples\
@@ -315,6 +339,9 @@ jobs:
315339
run: |
316340
make stats
317341
ldd build/qjs
342+
- name: cxxtest
343+
run: |
344+
make cxxtest
318345
- name: test
319346
run: |
320347
make test
@@ -395,6 +422,10 @@ jobs:
395422
- name: stats
396423
run: make stats
397424

425+
- name: cxxtest
426+
run: |
427+
make cxxtest
428+
398429
- name: test
399430
run: make test
400431

@@ -467,6 +498,9 @@ jobs:
467498
- name: build
468499
run: |
469500
make
501+
- name: cxxtest
502+
run: |
503+
make cxxtest
470504
- name: test
471505
run: |
472506
make test
@@ -484,6 +518,9 @@ jobs:
484518
- name: build
485519
run: |
486520
make
521+
- name: cxxtest
522+
run: |
523+
make cxxtest
487524
- name: test
488525
run: |
489526
make test

Makefile

+7-1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ distclean:
8383
stats: $(QJS)
8484
$(QJS) -qd
8585

86+
# implicitly .PHONY because it doesn't generate output
87+
cxxtest: CXXFLAGS+=-Wall -Wextra -Werror -Wno-unused-parameter -fsyntax-only
88+
cxxtest: cxxtest.cc quickjs.h
89+
$(CXX) $(CXXFLAGS) -DJS_NAN_BOXING=0 $<
90+
$(CXX) $(CXXFLAGS) -DJS_NAN_BOXING=1 $<
91+
8692
test: $(QJS)
8793
$(RUN262) -c tests.conf
8894

@@ -110,4 +116,4 @@ unicode_gen: $(BUILD_DIR)
110116
libunicode-table.h: unicode_gen
111117
$(BUILD_DIR)/unicode_gen unicode $@
112118

113-
.PHONY: all debug fuzz install clean codegen distclean stats test test262 test262-update test262-check microbench unicode_gen $(QJS) $(QJSC)
119+
.PHONY: all cxxtest debug fuzz install clean codegen distclean stats test test262 test262-update test262-check microbench unicode_gen $(QJS) $(QJSC)

cxxtest.cc

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// note: file is not actually compiled, only checked for C++ syntax errors
2+
#include "quickjs.h"
3+
4+
JSCFunctionListEntry funcs[] = {
5+
JS_ALIAS_BASE_DEF("", "", 0),
6+
JS_ALIAS_DEF("", ""),
7+
JS_CFUNC_DEF("", 0, 0),
8+
JS_CFUNC_DEF2("", 0, 0, 0),
9+
JS_CFUNC_MAGIC_DEF("", 0, 0, 0),
10+
JS_CFUNC_SPECIAL_DEF("", 0, f_f, 0),
11+
JS_CFUNC_SPECIAL_DEF("", 0, f_f_f, 0),
12+
JS_CGETSET_DEF("", 0, 0),
13+
JS_CGETSET_DEF2("", 0, 0, 0),
14+
JS_CGETSET_MAGIC_DEF("", 0, 0, 0),
15+
JS_ITERATOR_NEXT_DEF("", 0, 0, 0),
16+
JS_OBJECT_DEF("", 0, 0, 0),
17+
JS_PROP_DOUBLE_DEF("", 0, 0),
18+
JS_PROP_INT32_DEF("", 0, 0),
19+
JS_PROP_INT64_DEF("", 0, 0),
20+
JS_PROP_STRING_DEF("", "", 0),
21+
JS_PROP_UNDEFINED_DEF("", 0),
22+
};
23+
24+
int main(void)
25+
{
26+
JSRuntime *rt = JS_NewRuntime();
27+
JSContext *ctx = JS_NewContext(rt);
28+
JS_FreeValue(ctx, JS_NAN);
29+
JS_FreeValue(ctx, JS_UNDEFINED);
30+
JS_FreeValue(ctx, JS_NewFloat64(ctx, 42));
31+
// not a legal way of using JS_MKPTR but this is here
32+
// to have the compiler syntax-check its definition
33+
JS_FreeValue(ctx, JS_MKPTR(JS_TAG_UNINITIALIZED, 0));
34+
JS_FreeContext(ctx);
35+
JS_FreeRuntime(rt);
36+
return 0;
37+
}

quickjs.h

+13-6
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,11 @@ typedef uint32_t JSAtom;
6161
- string contents is either pure ASCII or is UTF-8 encoded.
6262
*/
6363

64+
/* Overridable purely for testing purposes; don't touch. */
65+
#ifndef JS_NAN_BOXING
6466
#if INTPTR_MAX < INT64_MAX
65-
/* Use NAN boxing for 32bit builds. */
66-
#define JS_NAN_BOXING
67+
#define JS_NAN_BOXING 1 /* Use NAN boxing for 32bit builds. */
68+
#endif
6769
#endif
6870

6971
enum {
@@ -90,7 +92,7 @@ enum {
9092
#define JS_FLOAT64_NAN NAN
9193
#define JSValueConst JSValue /* For backwards compatibility. */
9294

93-
#if defined(JS_NAN_BOXING)
95+
#if defined(JS_NAN_BOXING) && JS_NAN_BOXING
9496

9597
typedef uint64_t JSValue;
9698

@@ -174,13 +176,18 @@ typedef struct JSValue {
174176
#define JS_VALUE_GET_FLOAT64(v) ((v).u.float64)
175177
#define JS_VALUE_GET_PTR(v) ((v).u.ptr)
176178

179+
#ifdef __cplusplus
180+
#define JS_MKPTR(tag, p) JSValue{ JSValueUnion{ .ptr = p }, tag }
181+
#define JS_MKVAL(tag, val) JSValue{ JSValueUnion{ .int32 = val }, tag }
182+
#define JS_NAN JSValue{ JSValueUnion{ .float64 = JS_FLOAT64_NAN }, JS_TAG_FLOAT64 }
183+
#else
184+
#define JS_MKPTR(tag, p) (JSValue){ (JSValueUnion){ .ptr = p }, tag }
177185
#define JS_MKVAL(tag, val) (JSValue){ (JSValueUnion){ .int32 = val }, tag }
178-
#define JS_MKPTR(tag, p) (JSValue){ (JSValueUnion){ .ptr = p }, tag }
186+
#define JS_NAN (JSValue){ (JSValueUnion){ .float64 = JS_FLOAT64_NAN }, JS_TAG_FLOAT64 }
187+
#endif
179188

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

182-
#define JS_NAN (JSValue){ (JSValueUnion){ .float64 = JS_FLOAT64_NAN }, JS_TAG_FLOAT64 }
183-
184191
static inline JSValue __JS_NewFloat64(double d)
185192
{
186193
JSValue v;

0 commit comments

Comments
 (0)