-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a very simple Javascript module system.
Any JS file written in the s-exp language (not the plain language, yet) exports a single binding called 'script', which contains the code as a Javascript structure. This structure is also placed in Javascript registry (javascript-registry.ss) where it can be later recalled using registry->string. As modules are always loded in topological order they will always be registered in the correct order so dependencies come before scripts that depend on them. I have one final change to make: require statements for Javascript inside a Javascript file should automatically prefix their import so it doesn't clash with the script binding created in the requiring module. Note that javascript-registry.ss is written in Typed Scheme.
- Loading branch information
Noel Welsh
committed
Jun 24, 2009
1 parent
3a064b6
commit 3ec6ef9
Showing
7 changed files
with
127 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#lang scheme/base | ||
|
||
(require "../test-base.ss" | ||
"javascript-registry.ss" | ||
"javascript.ss") | ||
|
||
(define/provide-test-suite javascript-registry-tests | ||
|
||
#:before (lambda () (registry-clear!)) | ||
|
||
(test-case | ||
"registered script returned from registry" | ||
(after | ||
(define script (js (+ 1 2))) | ||
(registry-add! script) | ||
(check-equal? (registry->string) (javascript->string script)) | ||
|
||
(registry-clear!))) | ||
|
||
(test-case | ||
"registered scripts returned in order of registering" | ||
(after | ||
(define s1 (js (+ 1 2))) | ||
(define s2 (js (/ 2 1))) | ||
(registry-add! s1) | ||
(registry-add! s2) | ||
(check-equal? (registry->string) | ||
(string-append | ||
(javascript->string s1) | ||
"\n" | ||
(javascript->string s2))) | ||
|
||
(registry-clear!))) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#lang typed-scheme | ||
|
||
(require/opaque-type Javascript javascript? "javascript.ss") | ||
(require/typed scheme/string [string-join ((Listof String) String -> String)]) | ||
(require/typed "javascript.ss" [javascript->string (Javascript -> String)]) | ||
|
||
(: registry (Listof Javascript)) | ||
(define registry null) | ||
|
||
(: registry-add! (Javascript -> Void)) | ||
(define (registry-add! s) | ||
(set! registry (cons s registry))) | ||
|
||
(: registry->string (-> String)) | ||
(define (registry->string) | ||
(string-join (map javascript->string (reverse registry)) "\n")) | ||
|
||
(: registry-clear! (-> Void)) | ||
(define (registry-clear!) | ||
(set! registry null)) | ||
|
||
(provide | ||
registry-add! | ||
registry->string | ||
registry-clear!) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#lang scheme/base | ||
|
||
(require scheme/match | ||
scheme/runtime-path | ||
"../../test-base.ss" | ||
"../javascript.ss" | ||
"../javascript-registry.ss") | ||
|
||
(define-runtime-path here ".") | ||
(define t1:script #f) | ||
|
||
(define/provide-test-suite module-tests | ||
|
||
;; Dynamically requiring the test1.ss module here makes | ||
;; this test immune to the effects of other tests that may | ||
;; play around with the registry. If we statically | ||
;; required test1.ss, another test suite might clear the | ||
;; registry before we get run. | ||
#:before (lambda () | ||
(registry-clear!) | ||
(set! t1:script (dynamic-require (build-path here "test1.ss") 'script))) | ||
|
||
(test-case | ||
"module exports correct script binding" | ||
(check-equal? t1:script | ||
(js (function dave (a b) (+ a b)) | ||
(function noel (a b) (/ a b))))) | ||
|
||
(test-case | ||
"registry contains module's script" | ||
(check | ||
regexp-match? | ||
(regexp-quote (javascript->string t1:script)) | ||
(registry->string))) | ||
|
||
(test-case | ||
"registry contains dependency's script" | ||
(match (regexp-match-positions | ||
(regexp-quote (javascript->string t1:script)) | ||
(registry->string)) | ||
[(list (cons start end)) | ||
(check > start 0)] | ||
[_ (fail "Registry does not contain test1.ss script")])) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#lang s-exp "module.ss" | ||
|
||
(require (prefix-in foo: "test2.ss")) | ||
|
||
(function dave (a b) (+ a b)) | ||
(function noel (a b) (/ a b)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#lang s-exp "module.ss" | ||
|
||
(function matt (a b) (* a b)) | ||
(function xian (a b) (- a b)) |