1 JSConversions
trac edited this page 2008-02-23 03:19:01 +00:00

JSConversions: A general system of converting between native objects and their JavaScript representations

implemented by MarkT; documented by janwas.

note: "native" denotes C++ types/values, as opposed to JS objects that wrap them.

ToNative/ToScript

  • Overview:
  • Converts C++ JSObject-derived class pointers to and from JSObject*.
  • Syntax:
  • template T* ToNative( JSContext* cx, JSObject* obj );
  • template T* ToNative( jsval v );
  • template JSObject* ToScript( T* Native );
  • Example: CDamageType* dmg = ToNative( argv[0] );
  • Parameters:
  • ToNative: optionally either JSContext* and JSObject*, or jsval (useful for converting argvi)
  • ToScript: C++ class pointer
  • Returns:
  • The C++ class pointer resp. JS object pointer.
  • Notes:

ToPrimitive/ToJSVal

  • Overview:
  • Converts C++ built-in types or classes (if the requisite specialization exists) to and from jsval.
  • Syntax:
  • template bool ToPrimitive( JSContext* cx, jsval v, T& Storage );
  • template bool ToPrimitive( JSContext* cx, jsval v, T*& Storage );
  • template inline T ToPrimitive( JSContext* cx, jsval v );
  • template inline T ToPrimitive( jsval v );
  • template jsval ToJSVal( T& Native );
  • template jsval ToJSVal( T*& Native );
  • template jsval ToJSVal( const T& Native );
  • Example: ToPrimitive( cx, argv[0], gain);
  • Parameters:
  • ToPrimitive: the script context (if desired); value to convert and destination.
  • ToScript: the C++ value to convert.
  • Returns:
  • bool to indicate success resp. resulting JS value.
  • Notes:
  • The T*& calls automatically dereference pointers-to-objects.

JSParseString

  • Overview:
  • Converts textual representation to jsval.
  • Syntax:
  • jsval JSParseString( const CStrW& Native );
  • Example: AddProperty( PropertyName, JSParseString( ValueString ) );
  • Parameters:
  • CStrW string holding a simple value (no expressions are permitted)
  • Returns:
  • The converted value.
  • Notes:
  • Does not perform any evaluation; merely converts text to jsval.