WebKit

Artifact Content
Login

Artifact 82b60ba480e1284ff54bc0c15c5576723fcdce8c:

Wiki page [Dealing with JavaScript Objects] by murphy 2011-12-11 16:33:56.
D 2011-12-11T16:33:56.166
L Dealing\swith\sJavaScript\sObjects
P bd5cd2de4b40197b1ba1c916733154148144064c
U murphy
W 6295
<h1>Dealing with JavaScript Objects</h1>

<h2>Value Conversions</h2>

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

<table>
  <tr><th> Scheme Value </th><th> → </th><th> JavaScript Value </th></tr>
  <tr><td> <code>(void)</code> </td><td/><td> <code>undefined</code> </td></tr>
  <tr><td> <code>'()</code> </td><td/><td><code> <code>null</code> </code></td></tr>
  <tr><td> satisfies <code>boolean?</code> </code></td><td/><td> a boolean </td></tr>
  <tr><td> satisfies <code>number?</code> </code></td><td/><td> a number </td></tr>
  <tr><td> satisfies <code>string?</code> </code></td><td/><td> a string </td></tr>
  <tr><td> satisfies <code>symbol?</code> </code></td><td/><td> a string </td></tr>
  <tr><td> satisfies <code>jso?</code> </code></td><td/><td> the wrapped object </td></tr>
  <tr><td> anything </code></td><td/><td> a wrapper object </td></tr>
</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 <tt>@</tt>.

Z 690749928b57885469c35dcfcf377063