| 1 | = JSConversions: A general system of converting between native objects and their !JavaScript representations = |
| 2 | implemented by MarkT; documented by janwas. |
| 3 | |
| 4 | note: "native" denotes C++ types/values, as opposed to JS objects that wrap them. |
| 5 | |
| 6 | == !ToNative/!ToScript == |
| 7 | * '''Overview:''' |
| 8 | * Converts C++ JSObject-derived class pointers to and from JSObject*. |
| 9 | * '''Syntax:''' |
| 10 | * template<typename T> T* !ToNative( JSContext* cx, JSObject* obj ); |
| 11 | * template<typename T> T* !ToNative( jsval v ); |
| 12 | * template<typename T> JSObject* !ToScript( T* Native ); |
| 13 | * Example: CDamageType* dmg = !ToNative<CDamageType>( argv[0] ); |
| 14 | * '''Parameters:''' |
| 15 | * !ToNative: optionally either JSContext* and JSObject*, or jsval (useful for converting argv[i]) |
| 16 | * !ToScript: C++ class pointer |
| 17 | * '''Returns:''' |
| 18 | * The C++ class pointer resp. JS object pointer. |
| 19 | * '''Notes:''' |
| 20 | * |
| 21 | |
| 22 | == !ToPrimitive/ToJSVal == |
| 23 | * '''Overview:''' |
| 24 | * Converts C++ built-in types or classes (if the requisite specialization exists) to and from jsval. |
| 25 | * '''Syntax:''' |
| 26 | * template<typename T> bool !ToPrimitive( JSContext* cx, jsval v, T& Storage ); |
| 27 | * template<typename T> bool !ToPrimitive( JSContext* cx, jsval v, T*& Storage ); |
| 28 | * template<typename T> inline T !ToPrimitive( JSContext* cx, jsval v ); |
| 29 | * template<typename T> inline T !ToPrimitive( jsval v ); |
| 30 | * template<typename T> jsval ToJSVal( T& Native ); |
| 31 | * template<typename T> jsval ToJSVal( T*& Native ); |
| 32 | * template<typename T> jsval ToJSVal( const T& Native ); |
| 33 | * Example: !ToPrimitive<float>( cx, argv[0], gain); |
| 34 | * '''Parameters:''' |
| 35 | * !ToPrimitive: the script context (if desired); value to convert and destination. |
| 36 | * !ToScript: the C++ value to convert. |
| 37 | * '''Returns:''' |
| 38 | * bool to indicate success resp. resulting JS value. |
| 39 | * '''Notes:''' |
| 40 | * The T*& calls automatically dereference pointers-to-objects. |
| 41 | |
| 42 | == JSParseString == |
| 43 | * '''Overview:''' |
| 44 | * Converts textual representation to jsval. |
| 45 | * '''Syntax:''' |
| 46 | * jsval JSParseString( const CStrW& Native ); |
| 47 | * Example: !AddProperty( !PropertyName, JSParseString( !ValueString ) ); |
| 48 | * '''Parameters:''' |
| 49 | * CStrW string holding a simple value (no expressions are permitted) |
| 50 | * '''Returns:''' |
| 51 | * The converted value. |
| 52 | * '''Notes:''' |
| 53 | * Does not perform any evaluation; merely converts text to jsval. |