Closed
Bug 696035
Opened 14 years ago
Closed 5 years ago
ctypes documentation doesn't make the semantics of ArrayTypes, [n], and pointer to array casts very clear
Categories
(Developer Documentation Graveyard :: Mozilla Platform, defect, P5)
Developer Documentation Graveyard
Mozilla Platform
Tracking
(Not tracked)
RESOLVED
WONTFIX
People
(Reporter: gkrizsanits, Unassigned)
Details
So the problem is if I import a function that returns a pointer, and I know that it points to the first element of an array, there is simply no way to get the let's say second element of that array.
Casting a pointer to an array should be possible.
var blah = new ctypes.voidptr_t(0);
var blaharr = ctypes.cast(blah, ctypes.ArrayType(ctypes.voidptr_t));
Error: target CType has undefined or larger size than source CType
Note: it is also not possible to define a function with an array as its return type.
Comment 1•14 years ago
|
||
In general, it should be possible to convert T* to T[] for any type T. (a better example for the above is var blah = ctypes.voidptr_t.ptr(0) so that we'll try to convert void** to void*[]).
Another alternative is probably to allow addressOfElement (or some sort of equivalent) to be called on any pointer type.
Comment 2•14 years ago
|
||
(In reply to Gabor Krizsanits [:krizsa :gabor] from comment #0)
> So the problem is if I import a function that returns a pointer, and I know
> that it points to the first element of an array, there is simply no way to
> get the let's say second element of that array.
>
> Casting a pointer to an array should be possible.
>
> var blah = new ctypes.voidptr_t(0);
> var blaharr = ctypes.cast(blah, ctypes.ArrayType(ctypes.voidptr_t));
>
> Error: target CType has undefined or larger size than source CType
You need to be casting to ArrayType.ptr here. You should also probably include the size in your type construction, otherwise the getter will fail when you try to access an element by index.
Does that work? We should probably update the docs to make this more explicit...
Comment 3•14 years ago
|
||
Ah yeah, that would make sense (since addressOfElement(0) is the same as address()). I guess I misunderstood the array types. It would be nice if the docs covered this case as an example.
Comment 4•14 years ago
|
||
So the main problem in my understanding was that in C, the [n] operator performs a dereference (i.e. writing *foo is the same as writing foo[0]). With js-ctypes that isn't true and you'd have to write foo.contents[n] instead. It makes sense once you know about this, but it would still be useful to make this clear in the documentation. :)
Updated•14 years ago
|
Summary: Cannot cast pointer type to array → ctypes documentation doesn't make the semantics of ArrayTypes, [n], and pointer to array casts very clear
Reporter | ||
Comment 5•14 years ago
|
||
(In reply to Bobby Holley (:bholley) from comment #2)
> You need to be casting to ArrayType.ptr here. You should also probably
> include the size in your type construction, otherwise the getter will fail
> when you try to access an element by index.
>
> Does that work? We should probably update the docs to make this more
> explicit...
Thank you, this solved my problem. Here is a working example as a summary of this discussion:
var pointer = new ctypes.int32_t(9).address();
var array = ctypes.cast(pointer, ctypes.ArrayType(ctypes.int32_t, 1).ptr);
var value = array.contents[0];
By the way, I think it still would make sense to have a pointer incrementer/decrementer function in the API. Since you might not know upfront the length of the container you receive as a pointer in some cases (it can be a list for example where you have to iterate over until you find a special element or a null). However it is possible to implement a pointer incrementer helper function in javascript on top of the current API but such an implementation is far from natural or fast. Anyway it's just an idea...
Updated•14 years ago
|
Keywords: dev-doc-needed
Comment 6•14 years ago
|
||
(In reply to Gabor Krizsanits [:krizsa :gabor] from comment #5)
> (In reply to Bobby Holley (:bholley) from comment #2)
> By the way, I think it still would make sense to have a pointer
> incrementer/decrementer function in the API. Since you might not know
> upfront the length of the container you receive as a pointer in some cases
> (it can be a list for example where you have to iterate over until you find
> a special element or a null).
That seems reasonable. Filed bug 696450.
Updated•14 years ago
|
Component: js-ctypes → Documentation Requests
Product: Core → Mozilla Developer Network
QA Contact: js-ctypes → doc-request
Assignee | ||
Updated•13 years ago
|
Component: Documentation Requests → Documentation
Updated•12 years ago
|
Component: Documentation → General
Product: Mozilla Developer Network → Developer Documentation
Updated•12 years ago
|
Component: General → Mozilla Platform
Keywords: dev-doc-needed
Hardware: x86 → All
Whiteboard: u=mozdev p=0
Updated•12 years ago
|
Priority: -- → P4
Whiteboard: u=mozdev p=0 → u=mozdev p=0 c=Platform
Updated•8 years ago
|
Priority: P4 → P5
Whiteboard: u=mozdev p=0 c=Platform
Comment 7•5 years ago
|
||
MDN Web Docs' bug reporting has now moved to GitHub. From now on, please file content bugs at https://github.com/mdn/sprints/issues/ and platform bugs at https://github.com/mdn/kuma/issues/.
Status: NEW → RESOLVED
Closed: 5 years ago
Resolution: --- → WONTFIX
You need to log in
before you can comment on or make changes to this bug.
Description
•