Skip to content

Commit

Permalink
support absolute paths in import map
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesdiacono committed Jan 6, 2025
1 parent d2d215a commit df04a12
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 29 deletions.
6 changes: 3 additions & 3 deletions lib/blob.asm
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
;

.import
dev: "https://ufork.org/lib/dev.asm"
std: "https://ufork.org/lib/std.asm"
list: "https://ufork.org/lib/list.asm"
dev: "./dev.asm"
std: "./std.asm"
list: "./list.asm"

;
; initialized blob builder
Expand Down
6 changes: 3 additions & 3 deletions lib/blob_io.asm
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
;

.import
dev: "https://ufork.org/lib/dev.asm"
std: "https://ufork.org/lib/std.asm"
blob: "https://ufork.org/lib/blob.asm"
dev: "./dev.asm"
std: "./std.asm"
blob: "./blob.asm"

;
; input stream-requestor interface to a blob
Expand Down
4 changes: 2 additions & 2 deletions lib/blob_peg.asm
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
;

.import
dev: "https://ufork.org/lib/dev.asm"
std: "https://ufork.org/lib/std.asm"
dev: "./dev.asm"
std: "./std.asm"

;; digit = [0-9]
digit_rng:
Expand Down
6 changes: 3 additions & 3 deletions lib/cell.asm
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
;;;

.import
dev: "https://ufork.org/lib/dev.asm"
std: "https://ufork.org/lib/std.asm"
lib: "https://ufork.org/lib/lib.asm"
dev: "./dev.asm"
std: "./std.asm"
lib: "./lib.asm"

read_op:
ref 0
Expand Down
4 changes: 2 additions & 2 deletions lib/deque.asm
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
;

.import
std: "https://ufork.org/lib/std.asm"
list: "https://ufork.org/lib/list.asm"
std: "./std.asm"
list: "./list.asm"

new: ; ( -- deque )
deque new ; k deque
Expand Down
2 changes: 1 addition & 1 deletion lib/dict.asm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
;

.import
std: "https://ufork.org/lib/std.asm"
std: "./std.asm"

has: ; ( dict key -- bool )
roll -3 ; k dict key
Expand Down
4 changes: 2 additions & 2 deletions lib/div_mod.asm
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
; Euclidean division and remainder/modulus

.import
std: "https://ufork.org/lib/std.asm"
dev: "https://ufork.org/lib/dev.asm"
std: "./std.asm"
dev: "./dev.asm"

; Euclidean division is a slow, but simple, algorithm.
; It solves the equations: <latex> n = dq + r </latex>,
Expand Down
2 changes: 1 addition & 1 deletion lib/lib.scm
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
;;; Actor idioms in Scheme
;;;

(import dev "https://ufork.org/lib/dev.asm") ; for testing...
(import dev "./dev.asm") ; for testing...

(define sink-beh (BEH _))

Expand Down
4 changes: 2 additions & 2 deletions lib/line_buffer.asm
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
; Line buffering utilities for the I/O device.

.import
std: "https://ufork.org/lib/std.asm"
dev: "https://ufork.org/lib/dev.asm"
std: "./std.asm"
dev: "./dev.asm"

; Accumulate characters one-at-a-time until '\n'.
; When a `line` is complete, send it to `cust`.
Expand Down
4 changes: 2 additions & 2 deletions lib/list.asm
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
;

.import
dev: "https://ufork.org/lib/dev.asm"
std: "https://ufork.org/lib/std.asm"
dev: "./dev.asm"
std: "./std.asm"

len: ; ( list -- len )
roll -2 ; k list
Expand Down
32 changes: 24 additions & 8 deletions vm/js/ufork.js
Original file line number Diff line number Diff line change
Expand Up @@ -1188,16 +1188,14 @@ function make_core({
return exports_object;
}

function h_map_src(src) {
function u_map_src(src) {
if (src !== undefined) {
const alias = Object.keys(import_map).find(function (key) {
return src.startsWith(key);
});
return (
alias !== undefined
? src.replace(alias, import_map[alias])
: src
);
if (alias !== undefined) {
return src.replace(alias, import_map[alias]);
}
}
}

Expand Down Expand Up @@ -1242,8 +1240,26 @@ function make_core({

return Promise.all(Object.values(ir.ast.import).map(
function (import_src) {
import_src = u_map_src(import_src) ?? import_src;
return h_import_promise(
new URL(h_map_src(import_src), src).href

// We need to resolve the import specifier if it is relative.

import_src.startsWith(".")
? (

// The URL constructor chokes when 'base' is an absolute path, rather than a
// fully qualified URL. We work around this using a dummy origin so that we can
// produce an absolute path if 'src' is an absolute path.

src.startsWith("/")
? new URL(
import_src,
new URL(src, "http://_")
).pathname
: new URL(import_src, src).href
)
: import_src
);
}
)).then(function (imported_modules) {
Expand All @@ -1265,7 +1281,7 @@ function make_core({
// imports.

return unpromise(function () {
return h_import_promise(h_map_src(src), content);
return h_import_promise(u_map_src(src) ?? src, content);
});
}

Expand Down

0 comments on commit df04a12

Please sign in to comment.