Labs/Jetpack/Binary Components: Difference between revisions
(→Flexible Membrane Functionality: added docs for membrane.enumerate()) |
(added docs for more membrane methods) |
||
| Line 18: | Line 18: | ||
The source code for this functionality is in [http://hg.mozilla.org/labs/jetpack/file/tip/components/src/wrapper.cpp wrapper.cpp]. | The source code for this functionality is in [http://hg.mozilla.org/labs/jetpack/file/tip/components/src/wrapper.cpp wrapper.cpp]. | ||
<code>nsJetpack.</code>'''wrap'''('' | <code>nsJetpack.</code>'''wrap'''(''wrappee'', ''membrane'') | ||
This function wraps '' | This function wraps ''wrappee'' with ''membrane'' (meaning that ''membrane'' mediates all access to and from ''wrappee''). The wrapped object is returned. | ||
''membrane'' should be a JavaScript object with any of the following methods defined: | ''membrane'' should be a JavaScript object with any of the following methods defined: | ||
| Line 31: | Line 31: | ||
::This is essentially a JavaScript version of [https://developer.mozilla.org/en/SpiderMonkey/JSAPI_Reference/JSClass.enumerate JSClass.enumerate]. It should return an iterator that iterates through all the property names in ''wrappee''. | ::This is essentially a JavaScript version of [https://developer.mozilla.org/en/SpiderMonkey/JSAPI_Reference/JSClass.enumerate JSClass.enumerate]. It should return an iterator that iterates through all the property names in ''wrappee''. | ||
::<code>membrane.</code>'''getProperty'''(''wrappee'', ''membrane'', ''name'', ''defaultValue'') | |||
::This is essentially a JavaScript version of [https://developer.mozilla.org/En/SpiderMonkey/JSAPI_Reference/JSPropertyOp JSClass.getProperty]; alternatively, it could be described as the analog of Python's <code>__getattr__</code> magic method. ''name'' is the name of the property being accessed, and ''defaultValue'' is the value that JavaScript would ordinarily return. This function should return the value of the property, which may be ''defaultValue'' or something different. | |||
=== unwrap() === | === unwrap() === | ||
Revision as of 20:28, 28 July 2009
nsJetpack
nsJetpack is a binary component used to provide services to Jetpack that aren't otherwise available to scripted chrome code in the Mozilla platform.
Accessing the Component
Because the goal of nsJetpack is to provide functionality to scripted code, and because much of its functionality is concerned with providing access to SpiderMonkey internals, the XPCOM interface for the component is rather trivial. To obtain the component, simply do:
var nsJetpack = Cc["@labs.mozilla.com/jetpackdi;1"]
.createInstance(Ci.nsIJetpack).get();
This will give you the nsJetpack native JavaScript object, which provides access to all the component's functionality.
Flexible Membrane Functionality
nsJetpack contains functionality that exposes many SpiderMonkey C API calls to JavaScript, allowing chrome code to create custom wrappers (aka membranes) that allow trusted and untrusted code to interoperate.
The source code for this functionality is in wrapper.cpp.
nsJetpack.wrap(wrappee, membrane)
This function wraps wrappee with membrane (meaning that membrane mediates all access to and from wrappee). The wrapped object is returned.
membrane should be a JavaScript object with any of the following methods defined:
membrane.resolve(wrappee, membrane, name)
- This is essentially a JavaScript version of JSClass.resolve. It's called when the property identified by name doesn't exist on wrappee. The membrane should either define name on wrappee and return wrappee, or—if name doesn't exist—it should return
undefined.
- This is essentially a JavaScript version of JSClass.resolve. It's called when the property identified by name doesn't exist on wrappee. The membrane should either define name on wrappee and return wrappee, or—if name doesn't exist—it should return
membrane.enumerate(wrappee, membrane)
- This is essentially a JavaScript version of JSClass.enumerate. It should return an iterator that iterates through all the property names in wrappee.
membrane.getProperty(wrappee, membrane, name, defaultValue)
- This is essentially a JavaScript version of JSClass.getProperty; alternatively, it could be described as the analog of Python's
__getattr__magic method. name is the name of the property being accessed, and defaultValue is the value that JavaScript would ordinarily return. This function should return the value of the property, which may be defaultValue or something different.
- This is essentially a JavaScript version of JSClass.getProperty; alternatively, it could be described as the analog of Python's
unwrap()
getWrapper()
Memory Profiling
nsJetpack contains functionality allowing chrome code to examine the JavaScript heap. The semantics of this are described at a high level in Atul's blog post entitled Fun with SpiderMonkey.
The source code for this functionality is in memory_profiler.cpp.
profileMemory()
Miscellaneous Functions
The source code for this functionality is in tcb.cpp.