<h1>Dealing with JavaScript Objects</h1>

<h2>Value Conversions</h2>

<table>
  <tr><th> JavaScript Value </th><th> → </th><th> Scheme Value
  <tr><td> <code>undefined</code> <code>(void)</code>
  <tr><td> <code>null</code> <code>'()</code>
  <tr><td> a boolean satisfies <code>boolean?</code>
  <tr><td> a number satisfies <code>number?</code>
  <tr><td> a string satisfies <code>string?</code>
  <tr><td> a wrapper object the wrapped value
  <tr><td> anything satisfies <code>jso?</code>
</table>

<table>
  <tr><th> Scheme Value </th><th> → </th><th> JavaScript Value
  <tr><td> <code>(void)</code> <code>undefined</code>
  <tr><td> <code>'()</code> <code>null</code>
  <tr><td> satisfies <code>boolean?</code> a boolean
  <tr><td> satisfies <code>number?</code> a number
  <tr><td> satisfies <code>string?</code> a string
  <tr><td> satisfies <code>symbol?</code> a string
  <tr><td> satisfies <code>jso?</code> the wrapped object
  <tr><td> anything a wrapper object
</table>

If any JavaScript operation invoked from Scheme throws an error, a Scheme condition is raised with an equivalent error message.

If any Scheme operation invoked from JavaScript raises a condition, a JavaScript error is thrown with an equivalent error message.

<h2>Wrapper Records in Scheme</h2>

Complex JavaScript objects are not converted to Scheme values but rather encapsulted in a wrapper that allows various natural forms of access from Scheme. If the JavaScript object is a function, the resulting wrapper is callable as a procedure in addition to being accessible using the procedures described below.

<verbatim>
  (jso? VALUE) => BOOLEAN
</verbatim>
Tests whether the <tt>VALUE</tt> is a wrapped JavaScript object.

<verbatim>
  (jso-new JSO ARGUMENT ...) => VALUE
</verbatim>
Converts or unwraps the <tt>ARGUMENT</tt>s to JavaScript values, calls the <tt>JSO</tt> as a constructor with those arguments and returns the newly instantiated object converted or wrapped into a Scheme value.

<verbatim>
  (jso-apply JSO ARGUMENT ... REST) => VALUE
</verbatim>
Converts or unwraps the <tt>ARGUMENT</tt>s (and the contents of <tt>REST</tt>) to JavaScript values, calls the <tt>JSO</tt> as a function with those arguments and returns the result converted or wrapped into a Scheme value.

<verbatim>
  (jso-exists? JSO KEY) => BOOLEAN
</verbatim>
Checks whether the <tt>JSO</tt> has a property with a name obtained by stringifying <tt>KEY</tt>.

<verbatim>
  (jso-ref JSO KEY) => VALUE
</verbatim>
Stringifies <tt>KEY</tt>, gets the property of <tt>JSO</tt> with the resulting name and returns the value converted or wrapped into a Scheme value.

<verbatim>
  (set! (jso-ref JSO KEY) VALUE) => VOID
  (jso-set! JSO KEY VALUE) => VOID
</verbatim>
Stringifies <tt>KEY</tt>, converts or unwraps <tt>VALUE</tt> to JavaScript and sets the property of <tt>JSO</tt> with the resulting name to the resulting value.

<verbatim>
  (jso-delete! JSO KEY)
</verbatim>
Stringifies <tt>KEY</tt>, tries to delete the property of <tt>JSO</tt> with the resulting name and returns whether the operation was successful.

<verbatim>
  (:jso KEY JSO [(index INDEX)])
</verbatim>
SRFI-42 generator syntax for the enumerable property names of a JavaScript object.

<verbatim>
  (jso-keys JSO) => LIST
</verbatim>
Retrieves the enumerable property names of a JavaScript object as a list of strings.

<h2>Wrapper Objects in JavaScript</h2>

Complex Scheme values are not converted to JavaScript but rather encapsulated in a wrapper that allows various natural forms of access from JavaScript. If the Scheme value is a procedure, the resulting wrapper is callable as a function in addition to supporting the generic properties described below.

<verbatim>
  SCO.type => STRING
</verbatim>
Returns a string describing the type of the wrapped Scheme value. Possible values are <tt>"hash-table"</tt>, <tt>"vector"</tt>, <tt>"pair"</tt>, <tt>"procedure"</tt>, the name of a SRFI-99 record type or the undefined value.

<verbatim>
  SCO.length => INTEGER
</verbatim>
Returns the size of a hash table or the length of a vector. For other Scheme values the result is the undefined value.

<verbatim>
  SCO[STRING] => VALUE
  SCO[STRING] = VALUE
  delete SCO[STRING]
</verbatim>
The string keys of a Scheme hash table are accessible as mutable, deletable properties.

<verbatim>
  SCO.car => VALUE
  SCO.cdr => VALUE
  SCO.c[ad]{2,5}r => VALUE
</verbatim>
These direct and indirect properties of a pair are accessible as immutable properties.

<verbatim>
  SCO[NUMBER] => VALUE
</verbatim>
The valid indices of a Scheme vector are accessible as mutable properties.

<verbatim>
  SCO[SYMBOL] => VALUE
  SCO[SYMBOL] = VALUE
</verbatim>
The fields of a transparent SRFI-99 record are accessible as (potentially mutable) properties.

<verbatim>
  SCO(ARGUMENT, ...) => VALUE
</verbatim>
Converts or unwraps the <tt>ARGUMENT</tt>s to Scheme, applies the Scheme procedure wrapped by <tt>SCO</tt> to the resulting values and returns the result converted or wrapped into a JavaScript value.

<verbatim>
  String(SCO) => STRING
</verbatim>
Stringifying a wrapper object produces the display representation of the Scheme value.

<h2>Utilities for DOM Objects</h2>

<verbatim>
  (xexpr->jso XEXPR DOCUMENT) => JSO
</verbatim>

Converts the X-expression <tt>XEXPR</tt> into a DOM object using factory functions of the given <tt>DOCUMENT</tt> instance.

<verbatim>
  (jso->xexpr JSO [AT-ATTRIBUTES?]) => XEXPR
</verbatim>

Converts the DOM object <tt>JSO</tt> into an X-expression. The optional
argument determines whether element attribute lists should be prefixed
with the symbol