From d990e54a52b5bd9298028e7eb08908581a62adb6 Mon Sep 17 00:00:00 2001 From: skalogryz Date: Thu, 2 Apr 2009 11:37:35 +0000 Subject: [PATCH] added objcrtl test application git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@755 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- bindings/objc/objcrtltest.pas | 116 ++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 bindings/objc/objcrtltest.pas diff --git a/bindings/objc/objcrtltest.pas b/bindings/objc/objcrtltest.pas new file mode 100644 index 000000000..9eedb4eab --- /dev/null +++ b/bindings/objc/objcrtltest.pas @@ -0,0 +1,116 @@ +{ + Objective-C rtl Test application. by dmitry boyarintsev s2009 + + Should compile and run with no problems + program output should look like: + + Objective-C runtime initialized successfuly + -init method + called newMethod1 + called newMethod2, a = 5; b = 4 + get double = 1.33300000000000E+000 + get float = 3.12500000000000E+000 + test successfully complete +} + +program objcrtltest; + +{$mode objfpc}{$H+} + +uses + objcrtl20, objcrtl10, objcrtl, objcrtlutils; + +{.$linkframework AppKit} +{.$linkframework Foundation} + +const + newClassName = 'NSMyObject'; + overrideMethod = 'init'; + overrideMethodEnc = '@@:'; + + newMethod1 = 'newMethod1'; + newMethod1Enc = 'v@:'; + + newMethod2 = 'newMethod2::'; + newMethod2Enc = 'v@:ii'; + + newMethod3 = 'getDouble'; + newMethod3Enc = 'd@:'; + + newMethod4 = 'getFloat'; + newMethod4Enc = 'f@:'; + + +function imp_init(self: id; _cmd: SEL): id; cdecl; +var + sp : objc_super; +begin + writeln('-init method'); + sp := super(self); + Result := objc_msgSendSuper(@sp, selector(overrideMethod), []); +end; + +procedure imp_newMethod1(self: id; _cmd: SEL); cdecl; +begin + writeln('called newMethod1'); +end; +procedure imp_newMethod2(self: id; _cmd: SEL; a, b: Integer); cdecl; +begin + writeln('called newMethod2, a = ', a, '; b = ', b); +end; + +function imp_newMethod3(self: id; _cmd: SEL): Double; cdecl; +begin + Result := 1.333; +end; + +function imp_newMethod4(self: id; _cmd: SEL): Single; cdecl; +begin + Result := 3.125; +end; + + +procedure RegisterSubclass(NewClassName: PChar); +var + cl : _Class; + b : Boolean; +begin + cl := objc_allocateClassPair(objcclass('NSObject'), NewClassName, 0); + b := class_addMethod(cl, selector(OverrideMethod), @imp_init, overrideMethodEnc) and + class_addMethod(cl, selector(newMethod1), @imp_newMethod1, newMethod1Enc) and + class_addMethod(cl, selector(newMethod2), @imp_newMethod2, newMethod2Enc) and + class_addMethod(cl, selector(newMethod3), @imp_newMethod3, newMethod3Enc) and + class_addMethod(cl, selector(newMethod4), @imp_newMethod4, newMethod4Enc); + if not b then writeln('failed to add/override some method(s)'); + objc_registerClassPair(cl); +end; + +var + obj : id; +begin + // if InitializeObjcRtl20(DefaultObjCLibName) then // should be used of OSX 10.5 and iPhoneOS + + if InitializeObjcRtl20(DefaultObjCLibName) then // should be used of OSX 10.4 and lower + writeln('Objective-C runtime initialized successfuly') + else begin + writeln('failed to initialize Objective-C runtime'); + Halt; + end; + + RegisterSubclass(newClassName); + + obj := AllocAndInit(newClassName); + {obj := alloc(newClassName); + objc_msgSend(obj, selector(overrideMethod), []);} + + objc_msgSend(obj, selector(newMethod1), []); + objc_msgSend(obj, selector(newMethod2), [5, 4]); + + writeln('get double = ', objc_msgSend_fpret(obj, selector(newMethod3), [])); + writeln('get float = ', objc_msgSend_fpret(obj, selector(newMethod4), [])); + + release( obj ); + writeln('test successfully complete'); +end. + +