about summary refs log tree commit diff stats
path: root/src/js/jspropenumlist.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-05-08 18:39:27 +0200
committerbptato <nincsnevem662@gmail.com>2024-05-08 18:39:27 +0200
commit86f4c56507bc94d03e7d94be6ede884b9e7b5358 (patch)
tree929ce32ada67e8b1d57787d8e6d038fa603fc31e /src/js/jspropenumlist.nim
parent5c891488423aa7ccbef2fb4332ff6cb4e3c05154 (diff)
downloadchawan-86f4c56507bc94d03e7d94be6ede884b9e7b5358.tar.gz
js: refactor
* prefix to-be-separated modules with js
* remove dynstreams dependency
* untangle from EmptyPromise
* move typeptr into tojs
Diffstat (limited to 'src/js/jspropenumlist.nim')
-rw-r--r--src/js/jspropenumlist.nim42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/js/jspropenumlist.nim b/src/js/jspropenumlist.nim
new file mode 100644
index 00000000..65c16c2f
--- /dev/null
+++ b/src/js/jspropenumlist.nim
@@ -0,0 +1,42 @@
+import bindings/quickjs
+
+type
+  JSPropertyEnumArray* = ptr UncheckedArray[JSPropertyEnum]
+
+  JSPropertyEnumList* = object
+    buffer*: JSPropertyEnumArray
+    size: uint32
+    len*: uint32
+    ctx: JSContext
+
+  JSPropertyEnumWrapper* = object
+    is_enumerable: bool
+    name: string
+
+func newJSPropertyEnumList*(ctx: JSContext; size: uint32): JSPropertyEnumList =
+  let p = js_malloc(ctx, csize_t(sizeof(JSPropertyEnum)) * csize_t(size))
+  let buffer = cast[JSPropertyEnumArray](p)
+  return JSPropertyEnumList(
+    ctx: ctx,
+    buffer: buffer,
+    size: size
+  )
+
+proc grow(this: var JSPropertyEnumList) =
+  this.size *= 2
+  let p = js_realloc(this.ctx, this.buffer, csize_t(this.size))
+  this.buffer = cast[JSPropertyEnumArray](p)
+
+proc add*(this: var JSPropertyEnumList; val: uint32) =
+  let i = this.len
+  inc this.len
+  if this.size < this.len:
+    this.grow()
+  this.buffer[i].atom = JS_NewAtomUInt32(this.ctx, val)
+
+proc add*(this: var JSPropertyEnumList; val: string) =
+  let i = this.len
+  inc this.len
+  if this.size < this.len:
+    this.grow()
+  this.buffer[i].atom = JS_NewAtomLen(this.ctx, cstring(val), csize_t(val.len))