Opaque struct and definition support in ctypes

RESOLVED FIXED

Status

()

RESOLVED FIXED
9 years ago
8 years ago

People

(Reporter: dwitte, Assigned: dwitte)

Tracking

({dev-doc-complete})

Trunk
dev-doc-complete
Points:
---
Dependency tree / graph

Firefox Tracking Flags

(Not tracked)

Details

(Whiteboard: fixed-in-tracemonkey)

Attachments

(2 attachments)

(Assignee)

Description

9 years ago
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()?
(Assignee)

Comment 1

9 years ago
Created attachment 440390 [details] [diff] [review]
part 1: opaque structs

Adds a 't.define()' function to StructTypes, and refactors the resulting code.
Assignee: nobody → dwitte
Status: NEW → ASSIGNED
Attachment #440390 - Flags: review?(bnewman)
(Assignee)

Comment 2

9 years ago
Created attachment 440392 [details] [diff] [review]
part 2: remove opaque pointers

Kills off 'ctypes.PointerType("foo")'.
Attachment #440392 - Flags: review?(bnewman)
(Assignee)

Updated

9 years ago
Blocks: 560137
Keywords: dev-doc-needed
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+
(Assignee)

Updated

9 years ago
Status: ASSIGNED → RESOLVED
Last Resolved: 9 years ago
Resolution: --- → FIXED
Whiteboard: fixed-in-tracemonkey
Documentation updated.
Keywords: dev-doc-needed → dev-doc-complete
You need to log in before you can comment on or make changes to this bug.