about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-05-30 17:36:53 +0200
committerbptato <nincsnevem662@gmail.com>2024-05-30 17:40:21 +0200
commit29cf3549a62878cc1315da5cf3e93891173e2d4d (patch)
tree3b74bd8fb5643d3cf09053546192fec94cdc987c
parentb4cbdb8e1eb8af2a55d6a5b22316d59c87612578 (diff)
downloadchawan-29cf3549a62878cc1315da5cf3e93891173e2d4d.tar.gz
Fix GCC 14 compilation
TODO: find the exact flags we need instead of -fpermissive.

See also:
https://todo.sr.ht/~bptato/chawan/12
https://forum.nim-lang.org/t/11587
-rw-r--r--config.nims5
-rw-r--r--nim.cfg2
-rw-r--r--src/bindings/quickjs.nim6
-rw-r--r--src/js/tojs.nim37
4 files changed, 32 insertions, 18 deletions
diff --git a/config.nims b/config.nims
new file mode 100644
index 00000000..35c1754b
--- /dev/null
+++ b/config.nims
@@ -0,0 +1,5 @@
+switch("import", "utils/eprint")
+
+# workaround for GCC 14
+when defined(gcc):
+  switch("passC", "-fpermissive")
diff --git a/nim.cfg b/nim.cfg
index 736646a8..5e252ec2 100644
--- a/nim.cfg
+++ b/nim.cfg
@@ -6,7 +6,7 @@
 --experimental:"overloadableEnums"
 --mm:refc
 --include:"utils/myaddr"
---import:"utils/eprint"
+#--import:"utils/eprint"
 --styleCheck:usages
 --styleCheck:hint
 --threads:off
diff --git a/src/bindings/quickjs.nim b/src/bindings/quickjs.nim
index 06d8087f..836bedac 100644
--- a/src/bindings/quickjs.nim
+++ b/src/bindings/quickjs.nim
@@ -233,6 +233,8 @@ type
       {.cdecl.}
     js_malloc_usable_size*: proc(p: pointer) {.cdecl.}
 
+func `==`*(a, b: JSAtom): bool {.borrow.}
+
 converter toBool*(js: JS_BOOL): bool {.inline.} =
   cast[cint](js) != 0
 
@@ -412,6 +414,8 @@ proc JS_NewBigInt64*(ctx: JSContext; val: int64): JSValue
 proc JS_NewBigUInt64*(ctx: JSContext; val: uint64): JSValue
 proc JS_NewFloat64*(ctx: JSContext; val: cdouble): JSValue
 
+const JS_ATOM_NULL* = JSAtom(0)
+
 proc JS_NewAtomLen*(ctx: JSContext; str: cstring; len: csize_t): JSAtom
 proc JS_NewAtomUInt32*(ctx: JSContext; u: uint32): JSAtom
 proc JS_ValueToAtom*(ctx: JSContext; val: JSValue): JSAtom
@@ -468,8 +472,6 @@ proc JS_DefinePropertyValue*(ctx: JSContext; this_obj: JSValue; prop: JSAtom;
   val: JSValue; flags: cint): cint
 proc JS_DefinePropertyValueUint32*(ctx: JSContext; this_obj: JSValue;
   idx: uint32; val: JSValue; flags: cint): cint
-proc JS_DefinePropertyValueInt64*(ctx: JSContext; this_obj: JSValue; idx: int64;
-  val: JSValue; flags: cint): cint
 proc JS_DefinePropertyValueStr*(ctx: JSContext; this_obj: JSValue;
   prop: cstring; val: JSValue; flags: cint): cint
 proc JS_DefinePropertyValueGetSet*(ctx: JSContext; this_obj: JSValue;
diff --git a/src/js/tojs.nim b/src/js/tojs.nim
index 7697ea4a..59109884 100644
--- a/src/js/tojs.nim
+++ b/src/js/tojs.nim
@@ -106,6 +106,15 @@ proc defineProperty(ctx: JSContext; this: JSValue; name: JSAtom;
   if JS_DefinePropertyValue(ctx, this, name, prop, flags) <= 0:
     raise newException(Defect, "Failed to define property string")
 
+proc defineProperty(ctx: JSContext; this, name, prop: JSValue;
+    flags = cint(0)) =
+  let atom = JS_ValueToAtom(ctx, prop);
+  JS_FreeValue(ctx, prop);
+  if unlikely(atom == JS_ATOM_NULL):
+    raise newException(Defect, "Failed to define property string")
+  ctx.defineProperty(this, atom, prop, flags)
+  JS_FreeAtom(ctx, atom);
+
 proc definePropertyC*(ctx: JSContext; this: JSValue; name: JSAtom;
     prop: JSValue) =
   ctx.defineProperty(this, name, prop, JS_PROP_CONFIGURABLE)
@@ -195,7 +204,7 @@ proc toJS*(ctx: JSContext; opt: Option): JSValue =
     return toJS(ctx, opt.get)
   return JS_NULL
 
-proc toJS[T, E](ctx: JSContext; opt: Result[T, E]): JSValue =
+proc toJS*[T, E](ctx: JSContext; opt: Result[T, E]): JSValue =
   if opt.isSome:
     when not (T is void):
       return toJS(ctx, opt.get)
@@ -207,16 +216,15 @@ proc toJS[T, E](ctx: JSContext; opt: Result[T, E]): JSValue =
         return JS_Throw(ctx, toJS(ctx, opt.error))
     return JS_EXCEPTION
 
-proc toJS(ctx: JSContext; s: seq): JSValue =
+proc toJS*(ctx: JSContext; s: seq): JSValue =
   let a = JS_NewArray(ctx)
   if not JS_IsException(a):
-    for i in 0..s.high:
-      let j = toJS(ctx, s[i])
-      if JS_IsException(j):
-        return j
-      if JS_DefinePropertyValueInt64(ctx, a, int64(i), j,
-          JS_PROP_C_W_E or JS_PROP_THROW) < 0:
-        return JS_EXCEPTION
+    for i, x in s:
+      let val = toJS(ctx, x)
+      if JS_IsException(val):
+        return val
+      ctx.defineProperty(a, JS_NewInt64(ctx, int64(i)), val,
+        JS_PROP_C_W_E or JS_PROP_THROW)
   return a
 
 proc toJS*[T](ctx: JSContext; s: set[T]): JSValue =
@@ -237,12 +245,11 @@ proc toJS(ctx: JSContext; t: tuple): JSValue =
   if not JS_IsException(a):
     var i = 0
     for f in t.fields:
-      let j = toJS(ctx, f)
-      if JS_IsException(j):
-        return j
-      if JS_DefinePropertyValueInt64(ctx, a, int64(i), j,
-          JS_PROP_C_W_E or JS_PROP_THROW) < 0:
-        return JS_EXCEPTION
+      let val = toJS(ctx, f)
+      if JS_IsException(val):
+        return val
+      ctx.defineProperty(a, JS_NewInt64(ctx, int64(i)), val,
+        JS_PROP_C_W_E or JS_PROP_THROW)
       inc i
   return a