[Prev][Next][Index][Thread]

Objective-C from Dylan



Here's a toy program to call Objective-C from Gwydion Dylan code on MacOS X, written quickly last night.
Usable code can be generated using Macros to declare Objective-C methods.
If anyone can think of a useful example (for example using NSString or even a graphical object), let me know. :-)

- Rob

objective-c.lid --------------------------------------------------------------------
library: objective-c
executable: objective-c
linker-options: -framework Cocoa
files: objective-c-exports
  objective-c
objective-c-exports.dylan -----------------------------------------------------
module: dylan-user

define library objective-c
  use common-dylan;
  use melange-support;
  use io;
end library;

define module objective-c
  use common-dylan;
  use melange-support,
    import:{ c-include, call-out, 
            <statically-typed-pointer>, raw-value,
            <c-string> };
  use format-out;
end module;
objective-c.dylan ----------------------------------------------------------------
module: objective-c


// c-include for Apple Objective-C

c-include( "objc/objc.h" );


// Objective-C Types

define constant <objective-c-id> = <statically-typed-pointer>;
define constant <objective-c-class> = <statically-typed-pointer>;
define constant <objective-c-selector> = <statically-typed-pointer>;


// Methods


// get-objective-c-selector

define method get-objective-c-selector( selector-name :: <string> )
=> ( result :: false-or( <objective-c-selector> ) )
    let selector-name-c-string = as( <c-string>, selector-name  );
    let result = call-out( "sel_getUid", ptr:, ptr: selector-name-c-string.raw-value );
    if( result = 0 )
        #f;
    else
        as( <objective-c-selector>, result );
    end if;
end method get-objective-c-selector;


// get-objective-c-class

define method get-objective-c-class( class-name :: <string> )
=> ( result :: false-or( <objective-c-class> ) )
    let class-name-c-string = as( <c-string>, class-name  );
    let result = call-out( "objc_getClass", ptr:, ptr: class-name-c-string.raw-value );
    if( result = 0 )
        #f;
    else
        as( <objective-c-class>, result );
    end if;
end method get-objective-c-class;


// obj_msg_send

define method send-objective-c-message( class :: <objective-c-class>, message :: <objective-c-selector> )
=> ( result :: <objective-c-id> )
    let result = call-out( "objc_msgSend", ptr:, ptr: class.raw-value, ptr: message.raw-value );
    as( <objective-c-id>, result );
end method send-objective-c-message;


// main

define function main(name, arguments)
    let alloc = get-objective-c-selector( "alloc" );
    let init = get-objective-c-selector( "init" );
    let object-class = get-objective-c-class( "NSObject" );
    let my-instance = send-objective-c-message( object-class, alloc );
    send-objective-c-message( my-instance, init );

    format-out("Allocated and initialized NSObject instance.\n");
  exit-application(0);
end function main;

// Invoke our main() function.
main(application-name(), application-arguments());


Follow-Ups: