Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions racketscript-compiler/racketscript/compiler/expand.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,27 @@
(define unchecked? (syntax-property v 'racketscript-unchecked-lambda?))
(define fabsyn (formals->absyn #'formals))
(PlainLambda fabsyn (map to-absyn (syntax->list #'body)) unchecked?)]
;; NOTE: js require cannot be used without define (see next 4 cases)
[(define-values (name)
(#%plain-app (~datum #%js-ffi) (quote (~datum require)) (quote mod:str)))
;; HACK: Special case for JSRequire
(JSRequire (syntax-e #'name) (syntax-e #'mod) 'default)]
[(define-values (name)
(#%plain-app (~datum #%js-ffi) (quote (~datum require)) (quote *) (quote mod:str)))
;; HACK: Special case for JSRequire
(JSRequire (syntax-e #'name) (syntax-e #'mod) '*)]
[(define-values (name)
(#%plain-app (~datum #%js-ffi) (quote (~datum requirerkt)) (quote mod:str)))
;; js ids not part of dependency calculation, so manually add
(current-module-imports
(set-add (current-module-imports)
(path->complete-path (string->path (syntax-e #'mod)))))
(JSRequire (syntax-e #'name) (string-append "./" (syntax-e #'mod) ".js") 'default)]
[(define-values (name)
(#%plain-app (~datum #%js-ffi) (quote (~datum requirerkt)) (quote *) (quote mod:str)))
;; js ids not part of dependency calculation, so manually add
(current-module-imports
(set-add (current-module-imports)
(path->complete-path (string->path (syntax-e #'mod)))))
(JSRequire (syntax-e #'name) (string-append "./" (syntax-e #'mod) ".js") '*)]
[(define-values (id ...) b)
(DefineValues (syntax->datum #'(id ...)) (to-absyn #'b))]
[(#%top . x) (TopId (syntax-e #'x))]
Expand Down
8 changes: 8 additions & 0 deletions racketscript-compiler/racketscript/interop.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
$/array
$/require
$/require/*
$/require/rkt
$$
$>
$/:=
Expand Down Expand Up @@ -150,6 +151,13 @@
[(_ mod:str)
#`(#%js-ffi 'require '* mod)]))

(define-syntax ($/require/rkt stx)
(syntax-parse stx
[(_ mod:str)
#`(#%js-ffi 'requirerkt mod)]
[(_ mod:str (~datum *))
#`(#%js-ffi 'requirerkt '* mod)]))

(define-syntax ($> stx)
(define-syntax-class chaincall
(pattern [fieldname:id ρ:expr ...]))
Expand Down
21 changes: 20 additions & 1 deletion racketscript-doc/racketscript/scribblings/ffi.scrbl
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ which will expand to the appropriate call to @racket[#%js-ffi].
(#%js-ffi 'instanceof obj type)
(#%js-ffi 'string str)
(#%js-ffi 'require mod)
(#%js-ffi 'requirerkt mod)
(#%js-ffi 'operator 'op operand ...))
]{}

Expand All @@ -64,6 +65,7 @@ Summary of JavaScript operations supported by @racket[#%js-ffi]:
@item{@racket['instanceof]: JS @tt{instanceof} operation}
@item{@racket['string]: JS strings (incompatible with Racket/RacketScript strings, see @racket[$/str])}
@item{@racket['require]: JS @tt{import}, use to import JS libraries}
@item{@racket['requirerkt]: JS @tt{import}, use to import Racket libraries as JS}
@item{@racket['operator]: Use to call JS functions requiring infix notation}
]

Expand Down Expand Up @@ -135,7 +137,7 @@ Shorthand for multiple @racket[$]s. Allows more direct use of dot notation in Ra
([mod string?])]{
JavaScript import statement.

Often used with @racket[define], e.g., @racket[(define express ($/require "express"))] compiles to:
Must be used with @racket[define], e.g., @racket[(define express ($/require "express"))] compiles to:

@tt{import * as express from "express";}

Expand All @@ -149,6 +151,23 @@ Shorthand for multiple @racket[$]s. Allows more direct use of dot notation in Ra

Shorthand for @racket[($/require mod *)]}

@defform*[#:literals (*)
(($/require/rkt mod)
($/require/rkt mod *))
#:contracts
([mod string?])]{
JavaScript import statement, but for Racket files, i.e., @racket[mod]
is a Racket file name whose compiled JS file gets required in the output JS.

This form is needed because the RacketScript compiler's dependency
calculations skips JS identifiers.

Must be used with @racket[define], e.g., @racket[(define lib ($/require/rkt "lib.rkt"))] compiles to:

@tt{import * as lib from "lib.rkt.js";}

Equivalent to @racket[(#%js-ffi 'requirerkt mod)] or @racket[(#%js-ffi 'requirerkt '* mod)]}

@defform[($> e call ...)
#:grammar
([call id
Expand Down
1 change: 0 additions & 1 deletion racketscript-doc/racketscript/scribblings/start.scrbl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#lang scribble/manual

@title[#:tag "start"]{Getting Started}
}

@section[#:tag "install"]{Installation}

Expand Down
8 changes: 8 additions & 0 deletions tests/ffi/requirerkt.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#lang racketscript/base
(require racketscript/interop)

;; fixes pr#278

(define lib ($/require/rkt "rktlib.rkt"))

(#js.lib.f 10)
4 changes: 4 additions & 0 deletions tests/ffi/rktlib.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#lang racket/base
;; test for pr#278
(provide f)
(define (f x) x)