diff options
author | bptato <nincsnevem662@gmail.com> | 2023-05-27 17:10:31 +0200 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2023-06-01 13:01:40 +0200 |
commit | e38f831d4d369eacc7e721f4f82e4124003cc61e (patch) | |
tree | 86ed57b658cdff554dd42acff469ab3ec7466be6 /src/display/client.nim | |
parent | 2c4f1b5a9a879bbefdfa6f9cf8bb72c12cd486d7 (diff) | |
download | chawan-e38f831d4d369eacc7e721f4f82e4124003cc61e.tar.gz |
Add module loading to client
Files only, for now.
Diffstat (limited to 'src/display/client.nim')
-rw-r--r-- | src/display/client.nim | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/display/client.nim b/src/display/client.nim index de09885c..d705eab1 100644 --- a/src/display/client.nim +++ b/src/display/client.nim @@ -4,6 +4,7 @@ import options import os import selectors import streams +import cstrutils import tables import terminal @@ -31,6 +32,7 @@ import ips/serialize import ips/serversocket import ips/socketstream import js/javascript +import js/module import js/timeout import types/cookie import types/dispatcher @@ -377,6 +379,27 @@ proc headlessLoop(client: Client) = client.runJSJobs() client.acceptBuffers() +proc clientLoadJSModule(ctx: JSContext, module_name: cstring, + opaque: pointer): JSModuleDef {.cdecl.} = + let global = JS_GetGlobalObject(ctx) + JS_FreeValue(ctx, global) + var x: Option[URL] + if module_name.startsWith("/") or module_name.startsWith("./") or + module_name.startsWith("../"): + let cur = getCurrentDir() + x = parseURL($module_name, parseURL("file://" & cur)) + else: + x = parseURL($module_name) + if x.isNone or x.get.scheme != "file": + JS_ThrowTypeError(ctx, "Invalid URL: %s", module_name) + return nil + try: + let f = readFile($x.get.path) + return finishLoadModule(ctx, f, module_name) + except IOError: + JS_ThrowTypeError(ctx, "Failed to open file %s", module_name) + return nil + #TODO this is dumb proc readFile(client: Client, path: string): string {.jsfunc.} = try: @@ -496,6 +519,7 @@ proc newClient*(config: Config, dispatcher: Dispatcher): Client = result.loader = dispatcher.forkserver.newFileLoader() result.jsrt = newJSRuntime() result.jsrt.setInterruptHandler(interruptHandler, cast[pointer](result)) + JS_SetModuleLoaderFunc(result.jsrt, nil, clientLoadJSModule, nil) let ctx = result.jsrt.newJSContext() result.jsctx = ctx result.pager = newPager(config, result.attrs, dispatcher, ctx) |