Jsctypes/api: Difference between revisions

Line 59: Line 59:
:''(Array types with 0 elements are allowed. Rationale: C/C++ allow them, and it is convenient to be able to pass an array to a foreign function, and have it autoconverted to a C array, without worrying about the special case where the array is empty.)''
:''(Array types with 0 elements are allowed. Rationale: C/C++ allow them, and it is convenient to be able to pass an array to a foreign function, and have it autoconverted to a C array, without worrying about the special case where the array is empty.)''


:'''<code>new ctypes.StructType(''name'', ''fields'')</code>''' - Create a new struct type with the given ''name'' and ''fields''. ''fields'' is an array of field descriptors. js-ctypes calculates the offsets of the fields from its encyclopedic knowledge of the architecture's struct layout rules. If ''name'' is not a string, or ''fields'' contains a field descriptor with a type ''t'' such that <code>''t''.size</code> is <code>undefined</code>, throw a <code>TypeError</code>. If the size of the struct, in bytes, would not be exactly representable both as a <code>size_t</code> and as a JavaScript number, throw a <code>RangeError</code>.
:'''<code>new ctypes.StructType(''name'', ''fields'')</code>''' - Create a new struct type with the given ''name'' and ''fields''. ''fields'' is an array of field descriptors, of the format
 
:<code>[ { field1: type1 }, { field2: type2 }, ... ]</code>
 
:where <code>field''n''</code> is a string denoting the name of the field, and <code>type''n''</code> is a ctypes type. js-ctypes calculates the offsets of the fields from its encyclopedic knowledge of the architecture's struct layout rules. If ''name'' is not a string, or any <code>type''n''</code> is such that <code>type''n''.size</code> is <code>undefined</code>, throw a <code>TypeError</code>. If the size of the struct, in bytes, would not be exactly representable both as a <code>size_t</code> and as a JavaScript number, throw a <code>RangeError</code>.


''(Open issue: Specify a way to tell <code>ctypes.StructType</code> to use <code>#pragma pack(n)</code>.)''
''(Open issue: Specify a way to tell <code>ctypes.StructType</code> to use <code>#pragma pack(n)</code>.)''
''(TODO: Finish specifying field descriptors.)''


These constructors behave exactly the same way when called without the <code>new</code> keyword.
These constructors behave exactly the same way when called without the <code>new</code> keyword.
Line 75: Line 77:
  const IOBuf = new ctypes.ArrayType(ctypes.uint8_t, 4096);
  const IOBuf = new ctypes.ArrayType(ctypes.uint8_t, 4096);
   
   
  const struct_tm = new ctypes.StructType('tm', [[ctypes.int, 'tm_sec'], ...]);
  const struct_tm = new ctypes.StructType('tm', [{'tm_sec': ctypes.int}, ...]);


== Properties of types ==
== Properties of types ==
148

edits