Skip to content

Commit

Permalink
WIP declar an NSApplicationDelegate in crystal and use it from a nib …
Browse files Browse the repository at this point in the history
…file.

Issues regarding IMP signature due to crystal-lang/crystal#214
  • Loading branch information
Brian J. Cardiff committed Sep 21, 2014
1 parent 5b828b1 commit aef4965
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.crystal
*.app
*.nib
7 changes: 6 additions & 1 deletion samples/bundled_application/Application.xib
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="NSApplication"/>
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
<customObject id="-3" userLabel="Application"/>
<customObject id="-3" userLabel="Application">
<connections>
<outlet property="delegate" destination="u2J-Ey-SEf" id="zdm-x6-8ow"/>
</connections>
</customObject>
<customObject id="YLy-65-1bz" customClass="NSFontManager"/>
<customObject id="u2J-Ey-SEf" customClass="MyAppDelegate"/>
<menu title="Main Menu" systemMenu="main" id="AYu-sK-qS6">
<items>
<menuItem title="NewApplication" id="1Xt-HY-uBw">
Expand Down
39 changes: 39 additions & 0 deletions samples/bundled_application/app.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,43 @@
require "../../src/crocoa"
include Crocoa

objc_class :MyAppDelegate do
end

# $x_MyAppDelegate_objc_class_initWithCoder_imp = ->(obj : UInt8*, _cmd : LibObjC::SEL, _coder : UInt8*) {
# `touch /Users/bcardiff/Work/Manas/crystal/crocoa/samples/bundled_application/xx-init-coder.txt`
# obj
# }
# LibObjC.class_addMethod2($x_MyAppDelegate_objc_class.obj, "initWithCoder:".to_sel.to_objc, $x_MyAppDelegate_objc_class_initWithCoder_imp, "@@:@")

# $x_MyAppDelegate_objc_class_init_imp = ->(obj : UInt8*, _cmd : LibObjC::SEL) {
# `touch /Users/bcardiff/Work/Manas/crystal/crocoa/samples/bundled_application/xx-init.txt`
# obj
# }
# LibObjC.class_addMethod3($x_MyAppDelegate_objc_class.obj, "init".to_sel.to_objc, $x_MyAppDelegate_objc_class_init_imp, "@@:")


# a = LibObjC.objc_msgSend($x_MyAppDelegate_objc_class.obj, "alloc".to_sel.to_objc)
# LibObjC.objc_msgSend(a, "init".to_sel.to_objc)
# `touch /Users/bcardiff/Work/Manas/crystal/crocoa/samples/bundled_application/xx-foo.txt`

LibObjC.class_addProtocol($x_MyAppDelegate_objc_class.obj,LibObjC.objc_getProtocol("NSApplicationDelegate"))

$AppDel_didFinishLaunching = -> (obj : UInt8*, _cmd : LibObjC::SEL, aNotification : UInt8*) {
Crocoa.nslog "didFinishLaunching"
}

LibObjC.class_addMethod4($x_MyAppDelegate_objc_class.obj, "applicationDidFinishLaunching:".to_sel.to_objc, $AppDel_didFinishLaunching, "v@:@")


LibAppKit.ns_application_main 0u32, nil

# objc_class :Foo do
# def mySelector()
# $t2.value = $t1.value
# puts "Hi there"
# end

# objc_export "mySelector:"
# end

12 changes: 12 additions & 0 deletions spec/objc/export_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require "../spec_helper"

describe "export" do

objc_class :MyFooBar do
end

it "should register classes" do
NSClass.new("MyFooBar").name.should eq("MyFooBar")
end

end
2 changes: 2 additions & 0 deletions src/core_foundation/lib_cf.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ lib LibCF
origin : Point
size : Size
end

fun nslog = NSLog(UInt8*, ...)
end
4 changes: 4 additions & 0 deletions src/foundation/nscoder.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Crocoa
class NSCoder < NSObject
end
end
14 changes: 4 additions & 10 deletions src/objc/export.cr
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
macro objc_class(class_name)
class {{class_name.id}} < NSObject
property :obj
end
$_{{class_name.id}}_classPair = LibObjC.objc_allocateClassPair(NSClass.new("NSObject").obj, {{class_name.id.stringify}}, 0_u32)
LibObjC.objc_registerClassPair($_{{class_name.id}}_classPair)

$x_{{class_name.id}}_objc_class = NSClass.new(LibObjC.objc_allocateClassPair(NSClass.new("NSObject").obj, {{class_name.id.stringify}}, 0_u32))
$x_{{class_name.id}}_objc_class = NSClass.new($_{{class_name.id}}_classPair)
# TODO
# register instance variable for crystal self using class_addIvar
# use it in methods instead of creating new objects each time
# call objc_registerClassPair then mapped_class could be fixed

class {{class_name.id}}

def self.nsclass
$x_{{class_name.id}}_objc_class
end

class {{class_name.id}} < NSObject
objc_method "init", nil, :id, "initialize"

{{yield}}
Expand Down
12 changes: 11 additions & 1 deletion src/objc/lib_objc.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ lib LibObjC
# type Class : UInt8*
# type size_t : UInt32
type IMP = Pointer(UInt8), LibObjC::SEL ->
# type IMP : Void*
type IMP2 = Pointer(UInt8), LibObjC::SEL, Pointer(UInt8) ->
type IMP3 = Pointer(UInt8), LibObjC::SEL -> Pointer(UInt8)
type IMP4 = Pointer(UInt8), LibObjC::SEL, Pointer(UInt8) ->

type Protocol = Void*

fun objc_getClass(UInt8*) : UInt8*
fun class_getName(UInt8*) : UInt8*
Expand All @@ -15,7 +19,13 @@ lib LibObjC
fun sel_getName(SEL) : UInt8*

fun objc_allocateClassPair(UInt8*, UInt8*, UInt32) : UInt8*
fun objc_registerClassPair(UInt8*) : Void

fun class_addMethod(UInt8*, SEL, IMP, UInt8*) : UInt8
fun class_addMethod2 = class_addMethod(UInt8*, SEL, IMP2, UInt8*) : UInt8
fun class_addMethod3 = class_addMethod(UInt8*, SEL, IMP3, UInt8*) : UInt8
fun class_addMethod4 = class_addMethod(UInt8*, SEL, IMP4, UInt8*) : UInt8

fun objc_getProtocol(UInt8*) : Protocol
fun class_addProtocol(UInt8*, Protocol) : UInt8
end

0 comments on commit aef4965

Please sign in to comment.