On this page:
$
$$
$>
$/+
8.18
top
up next →

2The RacketScript-JavaScript FFIπŸ”— i

RacketScript supports direct interoperability with most JavaScript features. This section explains how to invoke plain JavaScript in a RacketScript program.

2.1RacketScript’s JavaScript FFI PrimitiveπŸ”— i

RacketScript’s #%js-ffi form compiles directly to various JavaScript features. The first argument is a symbol that indicates the kind of JavaScript code to be generated and the rest are the arguments for that kind of operation.

NOTE: Users most likely should not be using this form. Instead, use the API described in the RacketScript’s JavaScript FFI API section, which will expand to the appropriate call to #%js-ffi .

syntax

( #%js-ffi 'var)
(#%js-ffi 'refobjprop-id)
(#%js-ffi 'indexobjprop-expr)
(#%js-ffi 'assignxe)
(#%js-ffi 'newexpr)
(#%js-ffi 'throwexn )
(#%js-ffi 'undefined)
(#%js-ffi 'null)
(#%js-ffi 'this)
(#%js-ffi 'arguments)
(#%js-ffi 'object[fldv]...)
(#%js-ffi 'arrayargs...)
(#%js-ffi 'typeofobj)
(#%js-ffi 'instanceofobjtype)
(#%js-ffi 'stringstr)
(#%js-ffi 'requiremod)
(#%js-ffi 'operator'opoperand...)

Summary of JavaScript operations supported by #%js-ffi :

  • 'var: Use to access variable in the JavaScript namespace

  • 'ref: JavaScript object property reference, i.e., dot notation

  • 'index: JavaScript index operation, i.e., bracket notation

  • 'assign: JavaScript assignment

  • 'new: JavaScript object constructor

  • 'throw: Throw JavaScript exception

  • 'undefined: JS undefined value

  • 'null: JS null object value

  • 'this: JS this object self reference

  • 'arguments: implicit JS arguments variable containing function args

  • 'object: JS object literals, i.e, curly brace notation

  • 'array: JS array literals, i.e, bracket notation

  • 'typeof: JS typeof operation

  • 'instanceof: JS instanceof operation

  • 'string: JS strings (incompatible with Racket/RacketScript strings, see $/str )

  • 'require: JS import, use to import JS libraries

  • 'operator: Use to call JS functions requiring infix notation

2.2RacketScript’s JavaScript FFI APIπŸ”— i

syntax

( $ jsid)

($ exprsym)
($ exprexpr)
($ exprexpr...)
jsid = validJSidentifier(alphanumericunderscoreanddollarchars)
sym : symbol?
Syntax for accessing Javascript variables and properties.

  • Using the $ operator with a single identifier references a JavaScript variable.

    Example: ($ JSON)

    Note: the identifier be a valid JavaScript identifier (underscore, dollar, and alphanumeric characters only), and not Racket or RacketScript one.

    Equivalent to (#%js-ffi 'varjsid).

  • Supplying a second argument that is a symbol corresponds to accessing a JavaScript object property using dot notation, where the symbol name is the property name.

    Example: If handling a web request named req, getting the body of the request could be written ($ req'body) which compiles to req.body in JavaScript.

    Equivalent to (#%js-ffi 'refreq'body).

    Note: The above assumes that req is a RacketScript variable. If the variable is in the JavaScript namespace only, then an additional $ is needed to first access the variable (see first $ case above).

    Example: ($ ($ JSON)'parse) compiles to the JavaScript JSON.parse function.

    Equivalent to (#%js-ffi 'ref(#%js-ffi 'varJSON)'parse).

  • A second argument that is an arbitrary expression is treated as JavaScript bracket notation.

    Example: ($ req"body") compiles to req["body"] in JavaScript.

    Equivalent to (#%js-ffi 'indexreq"body").

  • Supplying more than two arguments corresponds to a series of bracket lookups.

syntax

( $$ dot-chaine...)

dot-chain = symboloridentifierconsistingofmultipledot-separatednames
Shorthand for multiple $ s. Allows more direct use of dot notation in RacketScript. E.g., ($$ window.document.write) corresponds to window.document.write in JavaScript.

syntax

( $/new constructor-expr)

JavaScript object construction. Equivalent to (#%js-ffi 'newconstructor-expr).

syntax

( $/throw exn)

Throw a JavaScript exception. Equivalent to (#%js-ffi 'throwexn).

syntax

$/undefined

The JavaScript undefined value. Equivalent to (#%js-ffi 'undefined)

syntax

$/null

The JavaScript null object. Equivalent to (#%js-ffi 'null).

syntax

$/this

The JavaScript this keyword. Equivalent to (#%js-ffi 'this).

syntax

$/arguments

The JavaScript arguments object containing the arguments passed to a function. Equivalent to (#%js-ffi 'arguments).

syntax

( $/obj [fldv]...)

fld = identifier
JavaScript object literal notation, i.e., brace notation, where fld are identifiers representing the object’s properties, and v ... are values assigned to those properties. Equivalent to (#%js-ffi 'objectfld... v... )

syntax

( $/:= ev)

JavaScript assignment statement. Equivalent to (#%js-ffi 'assignev). e should be a symbol, or a #%js-ffi 'var, 'ref, or 'index call.

syntax

( $/array e...)

JavaScript array literal notation, where ($/array 123) compiles to [1,2,3]. Equivalent to (#%js-ffi 'arraye... )

syntax

( $/require mod)

($/require mod* )
mod : string?
JavaScript import statement.

Often used with define , e.g., (define express($/require "express")) compiles to:

import * as express from "express";

Equivalent to (#%js-ffi 'requiremod) or (#%js-ffi 'require'*mod)

syntax

( $/require/* mod)

mod : string?
JavaScript import all statement.

Shorthand for ($/require mod* )

syntax

( $> ecall...)

call = id
| (metharg...)
JavaScript chaincall.

For example:

($> (#js.res.status 400) (send #js"Bad Request"))

is compiles to res.status(400).send("Bad Request")

Equivalent to nested #%js-ffi calls (with 'var, 'ref, or 'index).

syntax

( $/typeof e)

($/typeof etype)
type :
(or/c "undefined""object""boolean""number""string""function"))
JavaScript typeof operator.

The first form returns a string representing the typeof the given JavaScript value. Equivalent to (#%js-ffi 'typeofe).

The second form is shorthand for checking the type of a value. For example, ($/typeof 11"number") is compiles to

typeof 11 === "number";

Equivalent to ($/binop ===(#%js-ffi 'typeofe)($/str v))

syntax

( $/instanceof etype)

e = JavaScriptObject
Returns a boolean indicating whether JavaScript object e is an instance of type.

Equivalent to (#%js-ffi 'instanceofetype)

syntax

( $/binop opoperand1operand2)

JavaScript infix binary function call.

Equivalent to (#%js-ffi 'operator'opoperand1operand2)

syntax

( $/+ operand...)

Multi-argument infix calls to JavaScript + (can be used as either concat or addition). Equivalent to multiple nested calls to $/binop .

procedure

( js-string->string jsstr)string?

jsstr:JSstring
Converts a JS string to a RacketScript string.

procedure

( js-string str)JSstring

str:string?
Converts a RacketScript string to a JS string.

syntax

( $/str s)

Converts a Racket string to a JS string, or vice versa, using js-string->string or js-string .

2.3Reader ExtensionsπŸ”— i

#lang racketscript/base includes reader extensions that make it easier to interoperate with JavaScript. Specifically, RacketScript’s reader recognizes three delimiters:

  • #js

    Used to access JavaScript object properties via dot notation.

    Example:

    #js.req.body

    where req is a RacketScript variable.

    Equivalent to a series of #%js-ffi 'ref calls.

  • #js*

    Used to access JavaScript object properties via dot notation. The difference with "#js" is that "#js*" wraps the first identifier in a #%js-ffi 'var form, i.e., it is used to access properties of JavaScript variables rather than RacketScript variables.

    Example:

    #js*.JSON.parse

    where JSON is a JavaScript variable.

    Equivalent to a series of #%js-ffi 'ref calls where the first id is wrapped in a #%js-ffi 'var.

  • #js"some js string"

    Used to create JS strings.

    Note: JS strings are not compatible with Racket/RacketScript strings. Use $/str and other related API functions to convert between the two when needed.

    Example:

    (#js*.console.warn #js"Error!")

    Equivalent to a #%js-ffi call with 'string.

top
up next →

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /