~tim/natalie

ref: f5a482b5ccd757b3cd29779e40922a6db95e541a natalie/include/natalie/value.hpp -rw-r--r-- 7.8 KiB
f5a482b5Tim Morgan Add Method#owner and Class#singleton_class? 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#pragma once

#include <assert.h>

#include "natalie/env.hpp"
#include "natalie/forward.hpp"
#include "natalie/gc.hpp"
#include "natalie/global_env.hpp"
#include "natalie/hashmap.hpp"
#include "natalie/macros.hpp"

namespace Natalie {

extern "C" {
#include "onigmo.h"
}

struct Value : public gc {
    enum class Type {
        Nil,
        Array,
        Class,
        Encoding,
        Exception,
        False,
        Fiber,
        Float,
        Hash,
        Integer,
        Io,
        MatchData,
        Method,
        Module,
        Object,
        Proc,
        Range,
        Regexp,
        String,
        Symbol,
        True,
        VoidP,
    };

    enum Flag {
        MainObject = 1,
        Frozen = 2,
        Break = 4,
    };

    enum class Conversion {
        Strict,
        NullAllowed,
    };

    enum class ConstLookupSearchMode {
        NotStrict,
        Strict,
    };

    enum class ConstLookupFailureMode {
        Null,
        Raise,
    };

    Value(Env *env)
        : m_klass { env->Object() } { }

    Value(ClassValue *klass)
        : m_klass { klass } {
        assert(klass);
    }

    Value(Type type, ClassValue *klass)
        : m_klass { klass }
        , m_type { type } {
        assert(klass);
    }

    Value(Env *env, Value &other)
        : m_klass { other.m_klass }
        , m_type { other.m_type } {
        if (other.m_ivars.table) {
            init_ivars();
            struct hashmap_iter *iter;
            for (iter = hashmap_iter(&other.m_ivars); iter; iter = hashmap_iter_next(&other.m_ivars, iter)) {
                char *name = (char *)hashmap_iter_get_key(iter);
                hashmap_put(env, &m_ivars, name, hashmap_iter_get_data(iter));
            }
        }
    }

    Value(const Value &) = delete;

    Value &operator=(const Value &) = delete;

    virtual ~Value() { }

    static ValuePtr _new(Env *, ValuePtr, size_t, ValuePtr *, Block *);

    Value(Env *, const Value &);

    Type type() { return m_type; }
    ClassValue *klass() { return m_klass; }

    ModuleValue *owner() { return m_owner; }
    void set_owner(ModuleValue *owner) { m_owner = owner; }

    int flags() { return m_flags; }

    ValuePtr initialize(Env *, size_t, ValuePtr *, Block *);

    bool is_nil() const { return m_type == Type::Nil; }
    bool is_true() const { return m_type == Type::True; }
    bool is_false() const { return m_type == Type::False; }
    bool is_fiber() const { return m_type == Type::Fiber; }
    bool is_array() const { return m_type == Type::Array; }
    bool is_method() const { return m_type == Type::Method; }
    bool is_module() const { return m_type == Type::Module || m_type == Type::Class; }
    bool is_class() const { return m_type == Type::Class; }
    bool is_encoding() const { return m_type == Type::Encoding; }
    bool is_exception() const { return m_type == Type::Exception; }
    bool is_float() const { return m_type == Type::Float; }
    bool is_hash() const { return m_type == Type::Hash; }
    bool is_integer() const { return m_type == Type::Integer; }
    bool is_io() const { return m_type == Type::Io; }
    bool is_match_data() const { return m_type == Type::MatchData; }
    bool is_proc() const { return m_type == Type::Proc; }
    bool is_range() const { return m_type == Type::Range; }
    bool is_regexp() const { return m_type == Type::Regexp; }
    bool is_symbol() const { return m_type == Type::Symbol; }
    bool is_string() const { return m_type == Type::String; }
    bool is_void_p() const { return m_type == Type::VoidP; }

    bool is_truthy() const { return !is_false() && !is_nil(); }
    bool is_falsey() const { return !is_truthy(); }
    bool is_numeric() const { return is_integer() || is_float(); }

    ArrayValue *as_array();
    ClassValue *as_class();
    EncodingValue *as_encoding();
    ExceptionValue *as_exception();
    FalseValue *as_false();
    FiberValue *as_fiber();
    FileValue *as_file();
    FloatValue *as_float();
    HashValue *as_hash();
    IntegerValue *as_integer();
    IoValue *as_io();
    MatchDataValue *as_match_data();
    MethodValue *as_method();
    ModuleValue *as_module();
    NilValue *as_nil();
    ProcValue *as_proc();
    RangeValue *as_range();
    RegexpValue *as_regexp();
    StringValue *as_string();
    SymbolValue *as_symbol();
    TrueValue *as_true();
    VoidPValue *as_void_p();

    KernelModule *as_kernel_module_for_method_binding();
    EnvValue *as_env_value_for_method_binding();
    ParserValue *as_parser_value_for_method_binding();
    SexpValue *as_sexp_value_for_method_binding();

    SymbolValue *to_symbol(Env *, Conversion);

    const char *identifier_str(Env *, Conversion);

    ClassValue *singleton_class() { return m_singleton_class; }
    ClassValue *singleton_class(Env *);

    void set_singleton_class(ClassValue *);

    virtual ValuePtr const_get(Env *, const char *);
    virtual ValuePtr const_fetch(Env *, const char *);
    virtual ValuePtr const_find(Env *, const char *, ConstLookupSearchMode = ConstLookupSearchMode::Strict, ConstLookupFailureMode = ConstLookupFailureMode::Raise);
    virtual ValuePtr const_set(Env *, const char *, ValuePtr);

    ValuePtr ivar_get(Env *, const char *);
    ValuePtr ivar_set(Env *, const char *, ValuePtr);

    ValuePtr instance_variables(Env *);

    ValuePtr cvar_get(Env *, const char *);
    virtual ValuePtr cvar_get_or_null(Env *, const char *);
    virtual ValuePtr cvar_set(Env *, const char *, ValuePtr);

    virtual void define_method(Env *, const char *, MethodFnPtr);
    virtual void define_method_with_block(Env *, const char *, Block *);
    virtual void undefine_method(Env *, const char *);

    void define_singleton_method(Env *, const char *, ValuePtr (*)(Env *, ValuePtr, size_t, ValuePtr *, Block *block));
    void define_singleton_method_with_block(Env *, const char *, Block *);
    void undefine_singleton_method(Env *, const char *);

    virtual void alias(Env *, const char *, const char *);

    nat_int_t object_id() { return reinterpret_cast<nat_int_t>(this); }

    const char *pointer_id() {
        char buf[100]; // ought to be enough for anybody ;-)
        snprintf(buf, 100, "%p", this);
        return GC_STRDUP(buf);
    }

    // TODO: rename this find_method and the one on ModuleValue to something else
    Method *locate_method(Env *, const char *);

    ValuePtr send(Env *, const char *, size_t = 0, ValuePtr * = nullptr, Block * = nullptr);
    ValuePtr send(Env *, size_t, ValuePtr *, Block *);

    ValuePtr dup(Env *);

    bool is_a(Env *, ValuePtr);
    bool respond_to(Env *, const char *);
    bool respond_to(Env *, ValuePtr);

    const char *defined(Env *, const char *, bool);
    ValuePtr defined_obj(Env *, const char *, bool = false);

    virtual ProcValue *to_proc(Env *);

    bool is_main_object() { return (m_flags & Flag::MainObject) == Flag::MainObject; }
    void add_main_object_flag() { m_flags = m_flags | Flag::MainObject; }

    bool is_frozen() { return is_integer() || is_float() || (m_flags & Flag::Frozen) == Flag::Frozen; }
    void freeze() { m_flags = m_flags | Flag::Frozen; }

    void add_break_flag() { m_flags = m_flags | Flag::Break; }
    void remove_break_flag() { m_flags = m_flags & ~Flag::Break; }
    bool has_break_flag() { return (m_flags & Flag::Break) == Flag::Break; }

    bool eq(Env *, ValuePtr other) {
        return this == other;
    }

    bool neq(Env *env, ValuePtr other) {
        return send(env, "==", 1, &other)->is_falsey();
    }

    ValuePtr instance_eval(Env *, ValuePtr, Block *);

    void assert_type(Env *, Value::Type, const char *);
    void assert_not_frozen(Env *);

    const char *inspect_str(Env *);

protected:
    ClassValue *m_klass { nullptr };

private:
    void init_ivars();

    Type m_type { Type::Object };

    ClassValue *m_singleton_class { nullptr };

    ModuleValue *m_owner { nullptr };
    int m_flags { 0 };

    hashmap m_ivars {};
};

}