1/*
2 * Copyright (C) 2020-2022 The opuntiaOS Project Authors.
3 * + Contributed by Nikita Melekhin <nimelehin@gmail.com>
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#include <libfoundation/NSObject.h>
10#include <libobjc/class.h>
11#include <libobjc/memory.h>
12#include <libobjc/objc.h>
13#include <libobjc/runtime.h>
14
15OBJC_EXPORT IMP objc_msg_lookup(id receiver, SEL sel)
16{
17 IMP impl = class_get_implementation(receiver->get_isa(), sel);
18 return impl;
19}
20
21static inline id call_alloc(Class cls, bool checkNil, bool allocWithZone = false)
22{
23 if (allocWithZone) {
24 return ((id(*)(id, SEL, void*))objc_msgSend)(cls, @selector(allocWithZone:), NULL);
25 }
26 return ((id(*)(id, SEL))objc_msgSend)(cls, @selector(alloc));
27}
28
29// Called with [Class alloc]
30OBJC_EXPORT id objc_alloc(Class cls)
31{
32 return call_alloc(cls, true, false);
33}
34
35// Called with [[Class alloc] init]
36OBJC_EXPORT id objc_alloc_init(Class cls)
37{
38 return [call_alloc(cls, true, false) init];
39}
40
41@implementation NSObject
42
43+ (id)init
44{
45 return (id)self;
46}
47
48- (id)init
49{
50 // Init root classes
51 return (id)self;
52}
53
54+ (id)alloc
55{
56 return alloc_instance(self);
57}
58
59+ (id)new
60{
61 return [call_alloc(self, false) init];
62}
63
64- (Class)class
65{
66 return object_getClass(self);
67}
68
69@end