Using Objective-C from Clozure CL. R. Matthew Emerson rme@clozure.com



Similar documents
Common Lisp ObjectiveC Interface

Objective-C and Cocoa User Guide and Reference Manual. Version 5.0

This documentation is made available before final release and is subject to change without notice and comes with no warranty express or implied.

INTRODUCTION TO OBJECTIVE-C CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 12 09/29/2011

Introduction to Objective-C. Kevin Cathey

Creating a Custom Class in Xcode

iphone SDK Enrolled students will be invited to developer program Login to Program Portal Request a Certificate Download and install the SDK

Nick lunes 3 de septiembre de 12

The Common Lisp Object System: An Overview

Developing Applications for ios

Objective-C Internals

17. November Übung 1 mit Swift. Architektur verteilter Anwendungen. Thorsten Kober Head Solutions ios/os X, SemVox GmbH

Objective-C for Experienced Programmers

From C++ to Objective-C

MA-WA1920: Enterprise iphone and ipad Programming

Object Oriented Programming and the Objective-C Programming Language 1.0. (Retired Document)

Predicate Dispatching in the Common Lisp Object System. Aaron Mark Ucko

5. Advanced Object-Oriented Programming Language-Oriented Programming

ios Development Tutorial Nikhil Yadav CSE 40816/60816: Pervasive Health 09/09/2011

Contents. About Testing with Xcode 4. Quick Start 7. Testing Basics 23. Writing Test Classes and Methods 28. At a Glance 5 Prerequisites 6 See Also 6

Tag Specification Document


Key-Value Coding Programming Guide

Beginner level: Modules 1 to 18. Advanced level: Quick review of modules 1 to 18, then following to module A Simple ios Application

ios Design Patterns Jackie Myrose CSCI 5448 Fall 2012

INTRODUCTION TO IOS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 13 02/22/2011

NSPersistentDocument Core Data Tutorial for Mac OS X v10.4. (Retired Document)

Programming Language Rankings. Lecture 15: Type Inference, polymorphism & Type Classes. Top Combined. Tiobe Index. CSC 131! Fall, 2014!

Chapter 5 Names, Bindings, Type Checking, and Scopes

ios Dev Fest Research Network Operations Center Thursday, February 7, 13

Data storage and retrieval in ios

Basic Lisp Operations

iphone Objective-C Exercises

ios Dev Crib Sheet In the Shadow of C

Semantic Analysis: Types and Type Checking

An Introduction to Modern Software Development Tools Creating A Simple GUI-Based Tool Appleʼs XCode Version 3.2.6

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

Mobile Application Development

Pico Lisp A Radical Approach to Application Development

ios Audio Programming Guide

TECHNOLOGY Computer Programming II Grade: 9-12 Standard 2: Technology and Society Interaction

ios App Development for Everyone

The C Programming Language course syllabus associate level

Porting Existing PhoneGap Apps to Tizen OS - Development Story

Assignment I Walkthrough

Object Oriented Software Design II

Magit-Popup User Manual

Using PyObjC for Developing Cocoa Applications with Python

Comp151. Definitions & Declarations

Problems and Measures Regarding Waste 1 Management and 3R Era of public health improvement Situation subsequent to the Meiji Restoration

Your First ios Application

Development of Computer Graphics and Digital Image Processing on the iphone Luciano Fagundes

2/1/2010. Background Why Python? Getting Our Feet Wet Comparing Python to Java Resources (Including a free textbook!) Hathaway Brown School

Form & Function in Software. Richard P. Gabriel phd mfa

Unit and Functional Testing for the ios Platform. Christopher M. Judd

6.s096. Introduction to C and C++

Programming Languages in Artificial Intelligence

Improving Your App with Instruments

C++ DATA STRUCTURES. Defining a Structure: Accessing Structure Members:

How To Understand And Understand Common Lisp

Multiple Dispatching. Alex Tritthart WS 12/13

Pentesting ios Apps Runtime Analysis and Manipulation. Andreas Kurtz

A Case Study in DSL Development

Praktikum Entwicklung von Mediensystemen mit

Kotlin for Android Developers

Develop a ios Mobile App Consuming an OData Service Running in SAP HANA Cloud Platform

Efficient Cross-Platform Method Dispatching for Interpreted Languages

Embedded Software Development with MPS

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Static vs. Dynamic. Lecture 10: Static Semantics Overview 1. Typical Semantic Errors: Java, C++ Typical Tasks of the Semantic Analyzer

Introduction to Data Structures

Chapter 6: Web Safari Finding Beta

Moving from CS 61A Scheme to CS 61B Java

Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Portability Study of Android and ios

Inline Variables. Document Number: N4424 Date: Hal Finkel and Richard Smith

Objective-C Tutorial

Chapter 15 Functional Programming Languages

Implementation Aspects of OO-Languages

Assistive Application Programming Guide For OS X 2.0

Flexible Object Layouts: enabling lightweight language extensions by intercepting slot access

CORBA Programming with TAOX11. The C++11 CORBA Implementation

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Lab Experience 17. Programming Language Translation

Parameter passing in LISP

Designing A Smart Phone Application With Xcode

ios Application Development &

Glossary of Object Oriented Terms

Xamarin.Forms. Hands On

Illustration 1: Diagram of program function and data flow

Pemrograman Dasar. Basic Elements Of Java

ios App Programming Guide

Tizen Web Runtime Update. Ming Jin Samsung Electronics

A Modern Objective-C Runtime

INF4820, Algorithms for AI and NLP: More Common Lisp Vector Spaces

CSI 402 Lecture 13 (Unix Process Related System Calls) 13 1 / 17

ios App Development for Everyone

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

Programming languages C

Objective C and iphone App

Transcription:

Using Objective-C from Clozure CL R. Matthew Emerson rme@clozure.com

Messaging id dict =... [dict addobject:@"tang" forkey:@"beverage"]; printf("%d\n", [dict count]); receiver message

Messaging id dict =... [dict addobject:@"tang" forkey:@"beverage"]; addobject:forkey:

Messaging id dict = [[NSMutableArray alloc] init];

Low-level Messaging [dict addobject:@"tang" forkey:@"beverage"]; /* The compiler would emit something like this */ SEL selector = sel_getuid("addobject:forkey:"); obj_msgsend(dict, selector, @"tang", @"beverage"); ;; We can call C from Lisp, but it's not exactly nice... (with-cstrs ((s "addobject:forkey:")) (let ((sel (#_sel_getuid s))) (#_objc_msgsend dict sel :id #@"tang" :id @#"beverage")))

Higher-level Messaging from Lisp [dict addobject:@"tang" forkey:@"beverage"]; ;deprecated old way (send dict "addobject:forkey:" #@"tang" #@"beverage") ;or (send dict :add-object #@"tang" :for-key #@"beverage") ;new way (#/addobject:forkey: dict #@"tang" #@"beverage")

FFI Notation and Reader Macro Zoo (#_getpid) (with-cstrs ((s "/dev/null")) (let ((fd (#_open s #$O_RDWR))) (#_close fd))) #&NSZeroRect #&sys_errlist (rlet ((tv (:struct :timeval))) (#_gettimeofday tv +null-ptr+) (format t "~d.~6,'0d" (pref tv :timeval.tv_sec) (pref tv :timeval.tv_usec))) (rlet ((r :<NS>Rect)) (setf (pref r :<NS>Rect.origin.x) 0d0)...) (rlet ((r #>NSRect)) (setf (pref r #>NSRect.origin.x) 0d0)...)

Semi-Real-Life Example (defun make-standard-window (x y w h) (ns:with-ns-rect (rect x y w h) (#/initwithcontentrect:stylemask:backing:defer: (#/alloc (objc:@class "NSWindow")) rect (logior #$NSTitledWindowMask! #$NSClosableWindowMask! #$NSMiniaturizableWindowMask! #$NSResizableWindowMask) #$NSBackingStoreBuffered nil)))

Demo

Semi-Real-Life Example (defun browse-url (string) (with-cfstring (s string) (let ((url (#/URLWithString: ns:ns-url s))) (if (%null-ptr-p url) (error "~s is not a valid URL" string) (gui:execute-in-gui #'(lambda () (let* ((w (make-standard-window 100 100 600 700)) (content-view (#/contentview w)) (web-view (#/initwithframe:framename:groupname: (#/alloc ns:web-view) (#/bounds content-view) +null-ptr+ +null-ptr+)) (request (#/requestwithurl: ns:ns-url-request url))) (#/setautoresizingmask: web-view (logior #$NSViewWidthSizable #$NSViewHeightSizable)) (#/addsubview: content-view web-view) (#/release web-view) (#/settitle: w (#_CFURLGetString url)) (#/orderfront: w +null-ptr+) (#/loadrequest: (#/mainframe web-view) request))))))))

Demo

Semi-Real-Life Example (defun browse-url (string) (with-cfstring (s string) (let ((url (#/URLWithString: ns:ns-url s))) (if (%null-ptr-p url) (error "~s is not a valid URL" string) (gui:execute-in-gui #'(lambda () (let* ((w (make-standard-window 100 100 600 700)) (content-view (#/contentview w)) (web-view (#/initwithframe:framename:groupname: (#/alloc ns:web-view) (#/bounds content-view) +null-ptr+ +null-ptr+)) (request (#/requestwithurl: ns:ns-url-request url))) (#/setautoresizingmask: web-view (logior #$NSViewWidthSizable #$NSViewHeightSizable)) (#/addsubview: content-view web-view) (#/release web-view) (#/settitle: w (#_CFURLGetString url)) (#/orderfront: w +null-ptr+) (#/loadrequest: (#/mainframe web-view) request))))))))

NSURL ns-array NSURLRequest ns-url-request WebView web-view Name Translation (compute-lisp-name "MIDIView") m-id-i-view (define-special-objc-word "MIDI") (compute-lisp-name "MIDIView") midi-view (compute-objc-classname 'ns-mutable-dictionary) "NSMutableDictionary"

More about #/ '#/addobject:forkey: NEXTSTEP-FUNCTIONS: addobject:forkey: (objc:@selector #/addobject:forkey:)

Bonus #/ Side Effect! '#/addobject:forkey: NEXTSTEP-FUNCTIONS: addobject:forkey: (objc:@selector #/addobject:forkey:) Interning a symbol into this package triggers an interface database lookup for Objective-C methods of the same name. A successful lookup results in the creation of a special type of dispatching function. The symbol is then given that dispatching function as its function definition. #'#/dealloc #<OBJC-DISPATCH-FUNCTION NSFUN: dealloc #x3020014369cf>

Very nice, but so? (objc-message-send view "bounds")? (#/bounds view) #<NS-RECT 600 X 700 @ 0,0 [gcable] (#x290a4e40) #x302002b5d37d>

Typed Foreign Pointers (objc-message-send view "bounds")? (#/bounds view) #<NS-RECT 600 X 700 @ 0,0 [gcable] (#x290a4e40) #x302002b5d37d> Certain foreign types can be treated as instances of built-in classes. (class-of (ns:make-ns-rect 0 0 10 10)) #<BUILT-IN-CLASS NS:NS-RECT> (class-of (ns:make-ns-point 0 10)) #<BUILT-IN-CLASS NS:NS-POINT> (class-of (ns:make-ns-size 300 110)) #<BUILT-IN-CLASS NS:NS-SIZE> (class-of (ns:make-ns-range 11 17)) #<BUILT-IN-CLASS NS:NS-RANGE> Objective-C objects can also be recognized as instances of built-in classes and printed, used with typep, and so forth.

Defining an Objective-C Class @interface Wand : NSObject { id wood; NSString *core; float lengthininches; } - (id)initwithwood:(id)w core:(nsstring *)c length:(float)inches; - (id)wood; - (void)setwood:(id)w; @end

@implementation Wand - (id)wood { return wood; } - (void)setwood:(id)w { [wood release]; wood = [w copy]; } - (void)dealloc { [wood release]; [core release]; [super dealloc]; } @end Defining Objective-C methods

And in Lisp Style... (defclass wand (ns:ns-object) ((wood :foreign-type :id) (core :foreign-type :id) (length-in-inches :foreign-type :float)) (:metaclass ns:+ns-object)) (objc:defmethod #/wood ((self wand)) (slot-value self 'wood)) (objc:defmethod (#/setwood: :void) ((self wand) w) (#/release (slot-value self 'wood)) (setf (slot-value self 'wood) (#/copy w))) (objc:defmethod (#/dealloc :void) ((self wand)) (#/release (slot-value self 'wood)) (#/release (slot-value self 'core)) (call-next-method))

A Custom NSView (defclass sample-view (ns:ns-view) () (:metaclass ns:+ns-object)) (objc:defmethod (#/drawrect: :void) ((self sample-view) (dirty #>NSRect)) (#/set (#/redcolor ns:ns-color)) (#_NSRectFill (#/bounds self))) (defun sample-view-window () (gui:execute-in-gui (lambda () (let* ((w (make-standard-window 100 100 300 300)) (content-view (#/contentview w)) (v (#/initwithframe: (#/alloc sample-view) (#/bounds content-view)))) (#/setautoresizingmask: v (logior #$NSViewWidthSizable #$NSViewHeightSizable)) (#/addsubview: content-view v) (#/release v) (#/orderfront: w +null-ptr+) w))))

Demo

Hybrid Classes (defclass card-pile-view (ns:ns-view) ((deck :accessor card-deck)) (:metaclass ns:+ns-object)) (defmethod initialize-instance :after ((v card-pile-view) &rest initargs) (declare (ignore initargs)) (setf (slot-value v 'deck) (make-shuffled-deck)) (unless *images* (make-card-images))) (objc:defmethod (#/dealloc :void) ((self card-pile-view)) (objc:remove-lisp-slots self) (call-next-method)) (objc:defmethod (#/drawrect: :void) ((self card-pile-view) (dirty #>NSRect))...)

Demo

See it in action! In the next talk, all of the user interface that you see was made in Lisp using the Objective-C interface just described.