Jsctypes/api: Difference between revisions

→‎CData objects: try to clarify aliasing example
(→‎CData objects: try to clarify aliasing example)
Line 302: Line 302:
''(TODO: Pointer types might need some properties of their own.)''
''(TODO: Pointer types might need some properties of their own.)''


It is possible for multiple <code>CData</code> objects to refer to the same memory. (In this way they are sort of like C++ references.) For example:
Note that it is possible for several <code>CData</code> objects to refer to the same or overlapping memory. (In this way <code>CData</code> objects are like C++ references.) For example:


  const Point = new ctypes.StructType(
  const Point = new ctypes.StructType(
Line 310: Line 310:
   
   
  var r = Rect();    // a new CData object of type Rect
  var r = Rect();    // a new CData object of type Rect
  r.topLeft.x = 100;  // This works because r.topLeft is a CData object
var p = r.topLeft;  // refers to the topLeft member of r, not a copy
                    // that refers to the topLeft member of r, not a copy.
  r.topLeft.x = 100;  // This would not work if `r.topLeft` was a copy!
r.topLeft.x
  ===> 100          // It works...
p.x                // and p refers to the same C/C++ object...
  ===> 100          // so it sees the change as well.
   
   
  r.toSource()
  r.toSource()
  ===> "Rect({topLeft: Point({x: 100, y: 0}), bottomRight: Point({x: 0, y: 0})})"
  ===> "Rect({topLeft: {x: 100, y: 0}, bottomRight: {x: 0, y: 0}})"


Minutiae:
Minutiae:
638

edits