JSClass.resolve

  • Revision slug: SpiderMonkey/JSAPI_Reference/JSClass.resolve
  • Revision title: JSClass.resolve
  • Revision id: 139663
  • Created:
  • Creator: Jorend
  • Is current revision? No
  • Comment 10 words removed

Revision Content

The JSClass.resolve callback is called when a property is not found on an object. It can be used to implement lazy properties.

Syntax

typedef JSBool (*JSResolveOp)(JSContext *cx, JSObject *obj, jsval id);
Name Type Description
cx JSContext * Pointer to the JS context in which the property access is taking place.
obj JSObject * Pointer to the object whose properties are being accessed.
id jsval Name or index of the property being accessed.

Description

Resolve a lazy property named by id in obj by defining it directly in obj. Lazy properties are those reflected from some peer native property space (e.g., the DOM attributes for a given node reflected as obj) on demand.

JS looks for a property in an object, and if not found, tries to resolve the given id. After calling resolve, the engine looks again to see if resolve defined obj{{ mediawiki.external('id') }}. If so, the property lookup succeeds. If not, the process is repeated with obj's prototype.

The callback must return JS_TRUE if the property is resolved, or if the object has no lazy property with the given id; or JS_FALSE to indicate any other error.

Classes with lazy properties should also implement the JSClass.enumerate callback.

Note: JSNewResolveOp provides a cheaper way to resolve lazy properties.

Revision Source

<p>The <strong><code><a href="/en/SpiderMonkey/JSAPI_Reference/JSClass" title="en/SpiderMonkey/JSAPI_Reference/JSClass">JSClass</a>.resolve</code></strong> callback is called when a property is <em>not</em> found on an object. It can be used to implement lazy properties.</p>
<h2 name="Syntax">Syntax</h2>
<pre class="eval">typedef <a href="/En/SpiderMonkey/JSAPI_Reference/JSBool" title="En/SpiderMonkey/JSAPI_Reference/JSBool">JSBool</a> (*<strong>JSResolveOp</strong>)(<a href="/en/SpiderMonkey/JSAPI_Reference/JSRuntime" title="en/SpiderMonkey/JSAPI_Reference/JSRuntime">JSContext</a> *cx, <a href="/en/SpiderMonkey/JSAPI_Reference/JSObject" title="en/SpiderMonkey/JSAPI_Reference/JSObject">JSObject</a> *obj, <a href="/En/SpiderMonkey/JSAPI_Reference/Jsval" title="En/SpiderMonkey/JSAPI_Reference/Jsval">jsval</a> id);
</pre>
<table class="fullwidth-table"> <tbody> <tr> <th>Name</th> <th>Type</th> <th>Description</th> </tr> <tr> <td><code>cx</code></td> <td><code><a href="/en/SpiderMonkey/JSAPI_Reference/JSRuntime" title="en/SpiderMonkey/JSAPI_Reference/JSRuntime">JSContext</a> *</code></td> <td>Pointer to the JS context in which the property access is taking place.</td> </tr> <tr> <td><code>obj</code></td> <td><code><a href="/en/SpiderMonkey/JSAPI_Reference/JSObject" title="en/SpiderMonkey/JSAPI_Reference/JSObject">JSObject</a> *</code></td> <td>Pointer to the object whose properties are being accessed.</td> </tr> <tr> <td><code>id</code></td> <td><code><a href="/En/SpiderMonkey/JSAPI_Reference/Jsval" title="En/SpiderMonkey/JSAPI_Reference/Jsval">jsval</a></code></td> <td>Name or index of the property being accessed.</td> </tr> </tbody>
</table>
<h2 name="Description">Description</h2>
<p>Resolve a lazy property named by <code>id</code> in <code>obj</code> by defining it directly in <code>obj</code>. Lazy properties are those reflected from some peer native property space (e.g., the DOM attributes for a given node reflected as <code>obj</code>) on demand.</p>
<p>JS looks for a property in an object, and if not found, tries to <em>resolve</em> the given <code>id</code>. After calling <code>resolve</code>, the engine looks again to see if <code>resolve</code> defined <code>obj{{ mediawiki.external('id') }}</code>. If so, the property lookup succeeds. If not, the process is repeated with <code>obj</code>'s prototype.</p>
<p>The callback must return <code>JS_TRUE</code> if the property is resolved, or if the object has no lazy property with the given <code>id</code>; or <code>JS_FALSE</code> to indicate any other error.</p>
<p>Classes with lazy properties should also implement the <code><a href="/en/SpiderMonkey/JSAPI_Reference/JSClass.enumerate" title="en/SpiderMonkey/JSAPI_Reference/JSClass.enumerate">JSClass.enumerate</a></code> callback.</p>
<p><strong>Note:</strong> <code><a href="/En/SpiderMonkey/JSAPI_Reference/JSNewResolveOp" title="En/SpiderMonkey/JSAPI_Reference/JSNewResolveOp">JSNewResolveOp</a></code> provides a cheaper way to resolve lazy properties.</p>
Revert to this revision