Created
May 12, 2014 18:19
-
-
Save zenAudio/67006968d584fe9ca8a7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This module gives low-level access to the Objective-C runtime, | |
# allowing classes to be created. | |
# Copyright 2014, Martin Percossi. | |
# | |
type | |
ObjectId* = object # represents id in Objective-C. | |
ClassRepr* = object # represents Class in Objective-C. | |
Selector* = object # represents SEL in Objective-C. | |
MethodRepr* = object # represents Method in Objective-C. | |
MethodImpl* = proc (self: ptr[ObjectId], sel: ptr[Selector]) {.cdecl.} | |
proc objc_getClass*(className: cstring): ptr[ClassRepr] | |
{.importc, dynlib: "objc".} | |
proc objc_allocateClassPair*(superclassObj: ptr[ClassRepr], name: cstring, extraBytes: int64): ptr[ClassRepr] | |
{.importc, dynlib: "objc".} | |
proc objc_registerClassPair*(cls: ptr[ClassRepr]) | |
{.importc, dynlib: "objc".} | |
proc sel_registerName*(str: cstring): ptr[Selector] | |
{.importc, dynlib: "objc".} | |
proc class_addMethod*(cls: ptr[ClassRepr], name: ptr[Selector], imp: MethodImpl, types: cstring): bool | |
{.importc, dynlib: "objc".} | |
proc class_getInstanceMethod*(cls: ptr[ClassRepr], selector: ptr[Selector]): ptr[MethodImpl] | |
{.importc, dynlib: "objc".} | |
proc objc_msgSend*(self: ptr[ObjectId], op: ptr[Selector]) | |
{.importc, dynlib: "objc", varargs.} | |
proc class_createInstance*(cls: ptr[ClassRepr], extraBytes: int64): ptr[ObjectId] | |
{.importc, dynlib: "objc", varargs.} | |
proc myImpl(self: ptr[ObjectId], sel: ptr[Selector]) {.cdecl, exportc.} = | |
echo("Hello, Objective-C!") | |
proc test() = | |
var | |
nsobjClass = objc_getClass("NSObject") | |
cls = objc_allocateClassPair(nsobjClass, "NSFoo", 0) | |
sel = sel_registerName("foo") | |
ok = class_addMethod(cls, sel, myImpl, "v@:") | |
objc_registerClassPair(cls) | |
var obj = class_createInstance(cls, 0) | |
objc_msgSend(obj, sel) | |
when isMainModule: | |
test() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is a higher level interface to Objective-C, | |
# allowing to create classes and methods at runtime. | |
import objcapi as llobjc | |
proc myImpl(self: ptr[llobjc.ObjectId], sel: ptr[llobjc.Selector]) {.cdecl, exportc.} = | |
echo("Hello, Objective-C!") | |
proc test() = | |
var | |
nsobjClass = llobjc.objc_getClass("NSObject") | |
cls = llobjc.objc_allocateClassPair(nsobjClass, "NSFoo", 0) | |
sel = llobjc.sel_registerName("foo") | |
ok = llobjc.class_addMethod(cls, sel, myImpl, "v@:") | |
llobjc.objc_registerClassPair(cls) | |
var obj = llobjc.class_createInstance(cls, 0) | |
llobjc.objc_msgSend(obj, sel) | |
when isMainModule: | |
test() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment