Closed Bug 696450 Opened 13 years ago Closed 13 years ago

Implement pointer increment/decrement in js-ctypes

Categories

(Core :: js-ctypes, defect)

defect
Not set
normal

Tracking

()

RESOLVED FIXED
mozilla11

People

(Reporter: bholley, Assigned: jdm)

Details

(Keywords: dev-doc-needed)

Attachments

(1 file, 2 obsolete files)

In general we don't really want to support arbitrary pointer arithmetic, because people are likely hurt themselves. If you have var foo of type tType.ptr, and know it corresponds to a buffer of length 5, then you can cast it to tType.array(5).ptr and proceed from there. However, sometimes you can have buffers of unknown size where you need to iterate and and check for a sentinel value. For these, we should have: var myFooPtrPlusOne = myFooPtr.increment(); and a corresponding decrement.
jdm is going to take a crack at this. I'll do the initial review.
Assignee: bobbyholley+bmo → josh
Attachment #568830 - Flags: review?(bobbyholley+bmo)
Comment on attachment 568830 [details] [diff] [review] Add increment/decrement methods to PointerType jsctype objects. Wow, that's one speedy patch! :-) >+static JSBool >+IncrementOrDecrement(JSContext* cx, bool increment, jsval* vp) I think we could do this a little bit better: 1 - Add a PointerType::OffsetBy (or ::Shift, or ::ShiftBy, or any better name you think up), that takes an arbitrary offset and generates a new pointer. 2 - Add it to the namespace PointerType {} block, and add a comment explaining that we're deliberately not adding it to sPointerInstanceFunctions for the aforementioned reasons. 3 - Implement Increment() and Decrement() as simple wrappers that pass 1 and -1 to OffsetBy(). This way, if we ever do decide to support arbitrary pointer arithmetic, it's just a 1-line patch to add it to sPointerInstanceFunctions. >+ >+ JSObject* pointerType = PointerType::CreateInternal(cx, baseType); >+ if (!pointerType) >+ return JS_FALSE; It's not necessary to construct a new type object here, since the result is of the same type. Just use the one off the 'this' object.
Attachment #568830 - Flags: review?(bobbyholley+bmo) → review-
Attachment #572752 - Flags: review?(bobbyholley+bmo)
Attachment #568830 - Attachment is obsolete: true
Comment on attachment 572752 [details] [diff] [review] Add increment/decrement methods to PointerType jsctype objects. Looks good. r=bholley. Flagging jorendorff, since he's an actual peer.
Attachment #572752 - Flags: review?(jorendorff)
Attachment #572752 - Flags: review?(bobbyholley+bmo)
Attachment #572752 - Flags: review+
Comment on attachment 572752 [details] [diff] [review] Add increment/decrement methods to PointerType jsctype objects. Try says this patch is completely broken (a_p.contents is undefined?), so I'm going to investigate.
Attachment #572752 - Flags: review?(jorendorff)
Attachment #572752 - Attachment is obsolete: true
Comment on attachment 572976 [details] [diff] [review] Add increment/decrement methods to PointerType jsctype objects. dwitte made me a js-ctypes peer, so this can land!
Attachment #572976 - Flags: review?(jorendorff) → review+
Keywords: checkin-needed
Flags: in-testsuite?
Keywords: checkin-needed
Target Milestone: --- → mozilla11
Status: NEW → RESOLVED
Closed: 13 years ago
Resolution: --- → FIXED
FYI, I am starting to need something like OffsetBy in js code. The main scenario is calling Posix read/write-like functions in which you may need to advance your pointer by an arbitrary number of bytes if the call did not read/write everything at once. Right now, I am casting to an array then casting back, but this is rather awkward, so I have essentially reimplemented OffsetBy in JS code, see bug 771928.
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: