about summary refs log tree commit diff stats
path: root/src/js/jsmodule.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/jsmodule.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/jsmodule.nim')
-rw-r--r--src/js/jsmodule.nim27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/js/jsmodule.nim b/src/js/jsmodule.nim
new file mode 100644
index 00000000..636065b8
--- /dev/null
+++ b/src/js/jsmodule.nim
@@ -0,0 +1,27 @@
+import bindings/constcharp
+import bindings/quickjs
+import js/javascript
+import js/tojs
+
+proc setImportMeta(ctx: JSContext; funcVal: JSValue; isMain: bool) =
+  let m = cast[JSModuleDef](JS_VALUE_GET_PTR(funcVal))
+  let moduleNameAtom = JS_GetModuleName(ctx, m)
+  let metaObj = JS_GetImportMeta(ctx, m)
+  definePropertyCWE(ctx, metaObj, "url", JS_AtomToValue(ctx, moduleNameAtom))
+  definePropertyCWE(ctx, metaObj, "main", isMain)
+  JS_FreeValue(ctx, metaObj)
+  JS_FreeAtom(ctx, moduleNameAtom)
+
+proc finishLoadModule*(ctx: JSContext; f: string; name: cstring): JSModuleDef =
+  let funcVal = compileModule(ctx, f, name)
+  if JS_IsException(funcVal):
+    return nil
+  setImportMeta(ctx, funcVal, false)
+  # "the module is already referenced, so we must free it"
+  # idk how this works, so for now let's just do what qjs does
+  result = cast[JSModuleDef](JS_VALUE_GET_PTR(funcVal))
+  JS_FreeValue(ctx, funcVal)
+
+proc normalizeModuleName*(ctx: JSContext; base_name, name: cstringConst;
+    opaque: pointer): cstring {.cdecl.} =
+  return js_strdup(ctx, cstring(name))