Closed Bug 560002 Opened 14 years ago Closed 14 years ago

Opaque struct and definition support in ctypes

Categories

(Core :: js-ctypes, defect)

defect
Not set
normal

Tracking

()

RESOLVED FIXED

People

(Reporter: dwitte, Assigned: dwitte)

References

Details

(Keywords: dev-doc-complete, Whiteboard: fixed-in-tracemonkey)

Attachments

(2 files)

On 04/16/2010 12:35 PM, Daniel Witte wrote:

    On Fri, Apr 16, 2010 at 10:23 AM, Eric Shepherd<eshepherd@mozilla.com>  wrote:

        If I have two declarations that need to be able to reference each other
        circularly using pointers, is there a way to define that without resorting
        to using a voidptr_t in the one declared first? This may be more of a
        general JavaScript question than a ctypes-specific one. :)

    Sadly, no. In C, a forward-declaration of a struct will get replaced
    by the full definition when it's provided. In ctypes, the first
    declaration must be opaque (as with C) but then remains that way.

    jorendorff: maybe we could solve this by allowing opaque structs, and
    making them mutable to a fully declared flavor? Like so:

    let opaque = ctypes.StructType("opaque");
    let struct1 = ctypes.StructType("struct1", [ { a : opaque.ptr } ]);
    opaque.redeclare([ { b: struct1.ptr } ]);

    (Note that I want to implement opaque structs anyway.)

Yes. Python's ctypes supports something similar, via assigning to _fields_, but a method is better.

Opaque structs are called "incomplete types", or (especially in C++) "declared" but not "defined". So maybe opaque.complete() or opaque.define()?
Adds a 't.define()' function to StructTypes, and refactors the resulting code.
Assignee: nobody → dwitte
Status: NEW → ASSIGNED
Attachment #440390 - Flags: review?(bnewman)
Kills off 'ctypes.PointerType("foo")'.
Attachment #440392 - Flags: review?(bnewman)
Blocks: 560137
Comment on attachment 440390 [details] [diff] [review]
part 1: opaque structs

>@@ -2481,18 +2501,17 @@ CType::ConstructBasic(JSContext* cx,
> JSObject*
> CType::Create(JSContext* cx,
>               JSObject* typeProto,
>               JSObject* dataProto,
>               TypeCode type,
>               JSString* name,
>               jsval size,
>               jsval align,
>-              ffi_type* ffiType,
>-              PropertySpec* ps)
>+              ffi_type* ffiType)

Now that you're passing in the ffi_type*, it seems you could avoid passing in the size, and use ffiType->size (when defined) instead.
Blocks: 561483
Comment on attachment 440392 [details] [diff] [review]
part 2: remove opaque pointers

Looks good.
Attachment #440392 - Flags: review?(mozilla+ben) → review+
Comment on attachment 440390 [details] [diff] [review]
part 1: opaque structs

Sorry for letting this sit for so long.
Attachment #440390 - Flags: review?(mozilla+ben) → review+
Status: ASSIGNED → RESOLVED
Closed: 14 years ago
Resolution: --- → FIXED
Whiteboard: fixed-in-tracemonkey
Documentation updated.
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: