[quickjs-devel] [patch] C++ support

  • From: "Fotyev V." <fotevvd@xxxxxxxxx>
  • To: quickjs-devel@xxxxxxxxxxxxx
  • Date: Fri, 16 Aug 2019 12:04:25 +0300

Allows using quickjs in C++

diff --git a/quickjs/quickjs.h b/quickjs/quickjs.h
index 6317068..9861f09 100644
--- a/quickjs/quickjs.h
+++ b/quickjs/quickjs.h
@@ -28,6 +28,10 @@
 #include <stdio.h>
 #include <stdint.h>

+#ifdef __cplusplus
+extern "C" {
+#endif
+
 #if defined(__GNUC__) || defined(__clang__)
 #define js_likely(x)          __builtin_expect(!!(x), 1)
 #define js_unlikely(x)        __builtin_expect(!!(x), 0)
@@ -201,8 +205,8 @@ typedef struct JSValue {
 #define JS_VALUE_GET_FLOAT64(v) ((v).u.float64)
 #define JS_VALUE_GET_PTR(v) ((v).u.ptr)

-#define JS_MKVAL(tag, val) (JSValue){ .u.int32 = val, tag }
-#define JS_MKPTR(tag, p) (JSValue){ .u.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_TAG_IS_FLOAT64(tag) ((unsigned)(tag) == JS_TAG_FLOAT64)

@@ -556,7 +560,7 @@ void __JS_FreeValue(JSContext *ctx, JSValue v);
 static inline void JS_FreeValue(JSContext *ctx, JSValue v)
 {
     if (JS_VALUE_HAS_REF_COUNT(v)) {
-        JSRefCountHeader *p = JS_VALUE_GET_PTR(v);
+        JSRefCountHeader *p = (JSRefCountHeader *)JS_VALUE_GET_PTR(v);
         if (--p->ref_count <= 0) {
             __JS_FreeValue(ctx, v);
         }
@@ -566,7 +570,7 @@ void __JS_FreeValueRT(JSRuntime *rt, JSValue v);
 static inline void JS_FreeValueRT(JSRuntime *rt, JSValue v)
 {
     if (JS_VALUE_HAS_REF_COUNT(v)) {
-        JSRefCountHeader *p = JS_VALUE_GET_PTR(v);
+        JSRefCountHeader *p = (JSRefCountHeader *)JS_VALUE_GET_PTR(v);
         if (--p->ref_count <= 0) {
             __JS_FreeValueRT(rt, v);
         }
@@ -576,7 +580,7 @@ static inline void JS_FreeValueRT(JSRuntime *rt, JSValue v)
 static inline JSValue JS_DupValue(JSContext *ctx, JSValueConst v)
 {
     if (JS_VALUE_HAS_REF_COUNT(v)) {
-        JSRefCountHeader *p = JS_VALUE_GET_PTR(v);
+        JSRefCountHeader *p = (JSRefCountHeader *)JS_VALUE_GET_PTR(v);
         p->ref_count++;
     }
     return (JSValue)v;
@@ -585,7 +589,7 @@ static inline JSValue JS_DupValue(JSContext *ctx, JSValueConst v)
 static inline JSValue JS_DupValueRT(JSRuntime *rt, JSValueConst v)
 {
     if (JS_VALUE_HAS_REF_COUNT(v)) {
-        JSRefCountHeader *p = JS_VALUE_GET_PTR(v);
+        JSRefCountHeader *p = (JSRefCountHeader *)JS_VALUE_GET_PTR(v);
         p->ref_count++;
     }
     return (JSValue)v;
@@ -895,4 +899,9 @@ int JS_SetModuleExportList(JSContext *ctx, JSModuleDef *m,
 #undef js_unlikely
 #undef js_force_inline

+#ifdef __cplusplus
+} // extern "C" {
+#endif
+
+
 #endif /* QUICKJS_H */


Other related posts:

  • » [quickjs-devel] [patch] C++ support - Fotyev V.