about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-12-30 18:32:30 +0100
committerbptato <nincsnevem662@gmail.com>2024-12-30 18:32:30 +0100
commit799a5783864c9312d1ef5e6d039d08010d766b95 (patch)
tree6ec06dc20c149d62c26c75c1423d8688df90d177
parent1f37f1047a6c8a9731ae3990f7053bd1afdb0cdc (diff)
downloadchawan-799a5783864c9312d1ef5e6d039d08010d766b95.tar.gz
script: improve defineConsts
Well, I guess this works for now, but something is still wrong.
In Gecko, document.__proto__ === Document.__proto__, but in Chawan,
it isn't.
-rw-r--r--src/html/dom.nim2
-rw-r--r--src/html/event.nim2
-rw-r--r--src/html/script.nim13
-rw-r--r--src/html/xmlhttprequest.nim2
-rw-r--r--test/js/document.html2
-rw-r--r--test/net/xhr.html5
6 files changed, 21 insertions, 5 deletions
diff --git a/src/html/dom.nim b/src/html/dom.nim
index e175cc91..49882b1b 100644
--- a/src/html/dom.nim
+++ b/src/html/dom.nim
@@ -5197,7 +5197,7 @@ proc registerElements(ctx: JSContext; nodeCID: JSClassID) =
 proc addDOMModule*(ctx: JSContext) =
   let eventTargetCID = ctx.getClass("EventTarget")
   let nodeCID = ctx.registerType(Node, parent = eventTargetCID)
-  ctx.defineConsts(nodeCID, NodeType, uint16)
+  ctx.defineConsts(nodeCID, NodeType)
   let nodeListCID = ctx.registerType(NodeList)
   let htmlCollectionCID = ctx.registerType(HTMLCollection)
   ctx.registerType(HTMLAllCollection, ishtmldda = true)
diff --git a/src/html/event.nim b/src/html/event.nim
index 16772c28..0625001d 100644
--- a/src/html/event.nim
+++ b/src/html/event.nim
@@ -335,5 +335,5 @@ proc dispatchEvent(ctx: JSContext; this: EventTarget; event: Event):
 proc addEventModule*(ctx: JSContext) =
   let eventCID = ctx.registerType(Event)
   ctx.registerType(CustomEvent, parent = eventCID)
-  ctx.defineConsts(eventCID, EventPhase, uint16)
+  ctx.defineConsts(eventCID, EventPhase)
   ctx.registerType(EventTarget)
diff --git a/src/html/script.nim b/src/html/script.nim
index 8d1816ff..b465eb7e 100644
--- a/src/html/script.nim
+++ b/src/html/script.nim
@@ -1,4 +1,5 @@
 import monoucha/javascript
+import monoucha/jsopaque
 import monoucha/quickjs
 import types/referrer
 import types/url
@@ -147,3 +148,15 @@ func uninitIfNull*(val: JSValue): JSValue =
   if JS_IsNull(val):
     return JS_UNINITIALIZED
   return val
+
+proc defineConsts*(ctx: JSContext; classid: JSClassID; consts: typedesc[enum]) =
+  let proto = JS_GetClassProto(ctx, classid)
+  let ctorProto = JS_GetPrototype(ctx, ctx.getOpaque().ctors[classid])
+  #TODO it should be enough to define on the proto only, but apparently
+  # it isn't...
+  for e in consts:
+    let s = $e
+    ctx.definePropertyE(proto, s, uint16(e))
+    ctx.definePropertyE(ctorProto, s, uint16(e))
+  JS_FreeValue(ctx, ctorProto)
+  JS_FreeValue(ctx, proto)
diff --git a/src/html/xmlhttprequest.nim b/src/html/xmlhttprequest.nim
index 9fff6c1d..6e6ca940 100644
--- a/src/html/xmlhttprequest.nim
+++ b/src/html/xmlhttprequest.nim
@@ -535,4 +535,4 @@ proc addXMLHttpRequestModule*(ctx: JSContext) =
   )]
   let xhrCID = ctx.registerType(XMLHttpRequest, xhretCID, hasExtraGetSet = true,
     extraGetSet = getset1)
-  ctx.defineConsts(xhrCID, XMLHttpRequestState, uint16)
+  ctx.defineConsts(xhrCID, XMLHttpRequestState)
diff --git a/test/js/document.html b/test/js/document.html
index 10ecf17d..8f15ab81 100644
--- a/test/js/document.html
+++ b/test/js/document.html
@@ -11,6 +11,8 @@
 assertEquals(document.toString, document.getElementById("toString"));
 document.toString.remove();
 document.testtest = "hi";
+assertEquals(document.ELEMENT_NODE, 1);
+assertEquals(Document.ELEMENT_NODE, 1);
 assertEquals(Object.getOwnPropertyNames(document).toString(), "testtest,location,img2,img3,asdf");
 /*
  * Note: the spec says Document == HTMLDocument, but in other browsers
diff --git a/test/net/xhr.html b/test/net/xhr.html
index 1897ebdc..5ab70601 100644
--- a/test/net/xhr.html
+++ b/test/net/xhr.html
@@ -9,8 +9,9 @@ function myFunction() {
 	;
 }
 x.onreadystatechange = myFunction;
-assert(myFunction === x.onreadystatechange);
-assert(x.readyState === XMLHttpRequest.UNSENT);
+assertEquals(myFunction, x.onreadystatechange);
+assertEquals(x.readyState, XMLHttpRequest.UNSENT);
+assertEquals(x.UNSENT, XMLHttpRequest.UNSENT);
 x.open("GET", "ping", false);
 assertThrows("x.responseType = 'document'", DOMException);
 x.overrideMimeType("text/plain");