Closed Bug 732936 Opened 9 years ago Closed 9 years ago

Feeding ArrayBuffer to js-ctypes


(Core :: js-ctypes, enhancement)

Not set





(Reporter: Yoric, Assigned: Yoric)



(Keywords: dev-doc-needed)


(1 file, 3 obsolete files)

It would be nice to be able to use a TypedArray as a C array/pointer when performing a js-ctypes call. I believe it would be rather simple to adapt ImplicitConvert for that purpose.
Severity: normal → enhancement
OS: Mac OS X → All
Hardware: x86 → All
Attached patch First prototype (obsolete) — Splinter Review
Attachment #621509 - Flags: feedback?(jorendorff)
Comment on attachment 621509 [details] [diff] [review]
First prototype

Hmm. I like everything here except that nothing is holding on to the TypedArray, so it would be easy for this pointer to be taken, implicitly, and then for the buffer to be GC'd, leaving a dangling ctypes pointer.

One way around that is to move this code to ExplicitConvert. Another is to create utility functions:
  ctypes.bufferPtr(buffer or typedarray) 
  ctypes.addressOfElement(typedarray, index)
Another is to create a property on the pointer CData object that refers back to the TypedArray (but that's lame).

Another possibility is to allow this conversion only if isArgument is true, similar to what we do for strings. For arguments, the conversion is safe at least for the duration of the C function call, since all arguments are rooted on the JS stack.

Or we can leave it like it is and be really careful. It's up to you, you're the main ctypes user right now.
Attachment #621509 - Flags: feedback?(jorendorff)
Actually, this is just the same thing we already do for js-ctypes arrays. It should be totally fine.
Comment on attachment 621509 [details] [diff] [review]
First prototype

no changes needed, post an updated patch for review

One note, there are a few bits in here that seem to belong to a different patch: the change to dom/system/OSFileConstants.cpp, and the one to toolkit/components/osfile/osfile_unix_front.jsm.
Attachment #621509 - Flags: feedback+
Same patch, with a few more comments and without parasite code.
Assignee: nobody → dteller
Attachment #621509 - Attachment is obsolete: true
Attachment #624009 - Flags: review?(jorendorff)
Comment on attachment 624009 [details] [diff] [review]
mplicitConvert ArrayBuffer to array or pointer

Review of attachment 624009 [details] [diff] [review]:

::: toolkit/components/ctypes/tests/unit/
@@ +61,1 @@
>      }

Did you want these new print calls checked in? They don't hurt anything, afaik, so it's fine with me.

@@ +1910,5 @@
>    }
>  }
>  function run_ArrayType_tests() {
> +  do_print("run_ArrayType_tests");

Here's another do_print(), just in case you meant to remove them.

@@ +2051,5 @@
> +    c.value = c_arraybuffer;
> +  } catch(x) {
> +    exn = x;
> +  }
> +  do_check_true(!!exn);

Use do_check_throws.
Attachment #624009 - Flags: review?(jorendorff) → review+
Flags: in-testsuite+
Keywords: checkin-needed
Target Milestone: --- → mozilla15
Closed: 9 years ago
Resolution: --- → FIXED
Duplicate of this bug: 710830
Title is actually a misnomer.
Summary: Feeding TypedArray to js-ctypes → Feeding ArrayBuffer to js-ctypes
Documentation started here:

Probalby needs some serious technical review.
You need to log in before you can comment on or make changes to this bug.