~tim/natalie

ref: f5a482b5ccd757b3cd29779e40922a6db95e541a natalie/src/kernel_module.cpp -rw-r--r-- 10.2 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include "natalie.hpp"

#include <errno.h>
#include <fcntl.h>
#include <spawn.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/wait.h>

extern char **environ;

namespace Natalie {

ValuePtr KernelModule::Array(Env *env, ValuePtr value) {
    if (value->type() == Value::Type::Array) {
        return value;
    } else if (value->respond_to(env, "to_ary")) {
        return value->send(env, "to_ary");
    } else if (value == env->nil_obj()) {
        return new ArrayValue { env };
    } else {
        ArrayValue *ary = new ArrayValue { env };
        ary->push(value);
        return ary;
    }
}

ValuePtr KernelModule::at_exit(Env *env, Block *block) {
    ArrayValue *at_exit_handlers = env->global_get("$NAT_at_exit_handlers")->as_array();
    env->assert_block_given(block);
    ValuePtr proc = new ProcValue { env, block };
    at_exit_handlers->push(proc);
    return proc;
}

ValuePtr KernelModule::cur_dir(Env *env) {
    if (env->file() == nullptr) {
        env->raise("RuntimeError", "could not get current directory");
    } else if (strcmp(env->file(), "-e") == 0) {
        return new StringValue { env, "." };
    } else {
        ValuePtr relative = new StringValue { env, env->file() };
        StringValue *absolute = static_cast<StringValue *>(FileValue::expand_path(env, relative, nullptr));
        size_t last_slash = 0;
        bool found = false;
        for (size_t i = 0; i < absolute->length(); i++) {
            if (absolute->c_str()[i] == '/') {
                found = true;
                last_slash = i;
            }
        }
        if (found) {
            absolute->truncate(last_slash);
        }
        return absolute;
    }
}

ValuePtr KernelModule::define_singleton_method(Env *env, ValuePtr name, Block *block) {
    env->assert_block_given(block);
    SymbolValue *name_obj = name->to_symbol(env, Value::Conversion::Strict);
    define_singleton_method_with_block(env, name_obj->c_str(), block);
    return name_obj;
}

ValuePtr KernelModule::exit(Env *env, ValuePtr status) {
    if (!status || status->type() != Value::Type::Integer) {
        status = new IntegerValue { env, 0 };
    }
    ExceptionValue *exception = new ExceptionValue { env, env->Object()->const_find(env, "SystemExit")->as_class(), "exit" };
    exception->ivar_set(env, "@status", status);
    env->raise_exception(exception);
    return env->nil_obj();
}

ValuePtr KernelModule::get_usage(Env *env) {
    struct rusage usage;
    if (getrusage(RUSAGE_SELF, &usage) != 0) {
        return env->nil_obj();
    }
    HashValue *hash = new HashValue { env };
    hash->put(env, new StringValue { env, "maxrss" }, new IntegerValue { env, usage.ru_maxrss });
    hash->put(env, new StringValue { env, "ixrss" }, new IntegerValue { env, usage.ru_ixrss });
    hash->put(env, new StringValue { env, "idrss" }, new IntegerValue { env, usage.ru_idrss });
    hash->put(env, new StringValue { env, "isrss" }, new IntegerValue { env, usage.ru_isrss });
    hash->put(env, new StringValue { env, "minflt" }, new IntegerValue { env, usage.ru_minflt });
    hash->put(env, new StringValue { env, "majflt" }, new IntegerValue { env, usage.ru_majflt });
    hash->put(env, new StringValue { env, "nswap" }, new IntegerValue { env, usage.ru_nswap });
    hash->put(env, new StringValue { env, "inblock" }, new IntegerValue { env, usage.ru_inblock });
    hash->put(env, new StringValue { env, "oublock" }, new IntegerValue { env, usage.ru_oublock });
    hash->put(env, new StringValue { env, "msgsnd" }, new IntegerValue { env, usage.ru_msgsnd });
    hash->put(env, new StringValue { env, "msgrcv" }, new IntegerValue { env, usage.ru_msgrcv });
    hash->put(env, new StringValue { env, "nsignals" }, new IntegerValue { env, usage.ru_nsignals });
    hash->put(env, new StringValue { env, "nvcsw" }, new IntegerValue { env, usage.ru_nvcsw });
    hash->put(env, new StringValue { env, "nivcsw" }, new IntegerValue { env, usage.ru_nivcsw });
    return hash;
}

ValuePtr KernelModule::hash(Env *env) {
    StringValue *inspected = send(env, "inspect")->as_string();
    nat_int_t hash_value = hashmap_hash_string(inspected->c_str());
    return new IntegerValue { env, hash_value };
}

ValuePtr KernelModule::inspect(Env *env) {
    if (is_module() && as_module()->class_name()) {
        return new StringValue { env, as_module()->class_name() };
    } else {
        StringValue *str = new StringValue { env, "#<" };
        assert(klass());
        StringValue *inspected = klass()->send(env, "inspect")->as_string();
        str->append_string(env, inspected);
        str->append_char(env, ':');
        str->append(env, pointer_id());
        str->append_char(env, '>');
        return str;
    }
}

// Note: this method is only defined here in the C++ -- the method is actually attached directly to `main` in Ruby.
ValuePtr KernelModule::main_obj_inspect(Env *env) {
    return new StringValue { env, "main" };
}

ValuePtr KernelModule::instance_variable_get(Env *env, ValuePtr name_val) {
    if (is_integer() || is_float()) {
        return env->nil_obj();
    }
    const char *name = name_val->identifier_str(env, Value::Conversion::Strict);
    return ivar_get(env, name);
}

ValuePtr KernelModule::instance_variable_set(Env *env, ValuePtr name_val, ValuePtr value) {
    this->assert_not_frozen(env);
    const char *name = name_val->identifier_str(env, Value::Conversion::Strict);
    ivar_set(env, name, value);
    return value;
}

bool KernelModule::is_a(Env *env, ValuePtr module) {
    if (!module->is_module()) {
        env->raise("TypeError", "class or module required");
    }
    return Value::is_a(env, module->as_module());
}

ValuePtr KernelModule::lambda(Env *env, Block *block) {
    if (block) {
        return new ProcValue(env, block, ProcValue::ProcType::Lambda);
    } else {
        env->raise("ArgumentError", "tried to create Proc object without a block");
    }
}

ValuePtr KernelModule::loop(Env *env, Block *block) {
    if (block) {
        for (;;) {
            NAT_RUN_BLOCK_AND_POSSIBLY_BREAK(env, block, 0, nullptr, nullptr);
        }
        return env->nil_obj();
    } else {
        // TODO: Enumerator?
        env->raise("ArgumentError", "loop without block");
    }
}

ValuePtr KernelModule::method(Env *env, Value *name) {
    auto name_symbol = name->to_symbol(env, Conversion::Strict);
    auto singleton = singleton_class();
    if (singleton) {
        Method *method = singleton_class()->find_method(env, name_symbol->c_str());
        if (method) {
            if (method->is_undefined()) {
                env->raise("NoMethodError", "undefined method `%s' for %s:Class", name, m_klass->class_name());
            }
            return new MethodValue { env, this, method };
        }
    }
    Method *method = m_klass->find_method(env, name_symbol->c_str());
    if (method)
        return new MethodValue { env, this, method };
    env->raise("NoMethodError", "undefined method `%s' for %s:Class", name, m_klass->class_name());
}

ValuePtr KernelModule::methods(Env *env) {
    ArrayValue *array = new ArrayValue { env };
    if (singleton_class()) {
        singleton_class()->methods(env, array);
    } else { // FIXME: I don't think this else should be here. Shouldn't we *always* grab methods from the klass?
        klass()->methods(env, array);
    }
    return array;
}

ValuePtr KernelModule::p(Env *env, size_t argc, ValuePtr *args) {
    if (argc == 0) {
        return env->nil_obj();
    } else if (argc == 1) {
        ValuePtr arg = args[0]->send(env, "inspect");
        puts(env, 1, &arg);
        return arg;
    } else {
        ArrayValue *result = new ArrayValue { env };
        for (size_t i = 0; i < argc; i++) {
            result->push(args[i]);
            args[i] = args[i]->send(env, "inspect");
        }
        puts(env, argc, args);
        return result;
    }
}

ValuePtr KernelModule::print(Env *env, size_t argc, ValuePtr *args) {
    IoValue *_stdout = env->global_get("$stdout")->as_io();
    return _stdout->print(env, argc, args);
}

ValuePtr KernelModule::proc(Env *env, Block *block) {
    if (block) {
        return new ProcValue { env, block };
    } else {
        env->raise("ArgumentError", "tried to create Proc object without a block");
    }
}

ValuePtr KernelModule::puts(Env *env, size_t argc, ValuePtr *args) {
    IoValue *_stdout = env->global_get("$stdout")->as_io();
    return _stdout->puts(env, argc, args);
}

ValuePtr KernelModule::raise(Env *env, ValuePtr klass, ValuePtr message) {
    if (!message) {
        ValuePtr arg = klass;
        if (arg->is_class()) {
            klass = arg->as_class();
            message = new StringValue { env, arg->as_class()->class_name() };
        } else if (arg->is_string()) {
            klass = env->Object()->const_find(env, "RuntimeError")->as_class();
            message = arg;
        } else if (arg->is_exception()) {
            env->raise_exception(arg->as_exception());
        } else {
            env->raise("TypeError", "exception klass/object expected");
        }
    }
    env->raise(klass->as_class(), message->as_string());
}

ValuePtr KernelModule::sleep(Env *env, ValuePtr length) {
    if (!length) {
        while (true) {
            ::sleep(1000);
        }
        NAT_UNREACHABLE();
    }
    length->assert_type(env, Value::Type::Integer, "Integer"); // TODO: float supported also
    ::sleep(length->as_integer()->to_nat_int_t());
    return length;
}

ValuePtr KernelModule::spawn(Env *env, size_t argc, ValuePtr *args) {
    pid_t pid;
    auto command = new Vector<const char *> {};
    char *cmd[argc + 1];
    for (size_t i = 0; i < argc; i++) {
        auto arg = args[i];
        arg->assert_type(env, Value::Type::String, "String");
        cmd[i] = GC_STRDUP(arg->as_string()->c_str());
    }
    cmd[argc] = nullptr;
    int result = posix_spawnp(&pid, cmd[0], NULL, NULL, cmd, environ);
    if (result == 0) {
        return new IntegerValue { env, pid };
    } else {
        env->raise_errno();
    }
}

ValuePtr KernelModule::tap(Env *env, Block *block) {
    ValuePtr self = this;
    NAT_RUN_BLOCK_AND_POSSIBLY_BREAK(env, block, 1, &self, nullptr);
    return this;
}

ValuePtr KernelModule::this_method(Env *env) {
    const char *name = env->caller()->find_current_method_name();
    if (name) {
        return SymbolValue::intern(env, name);
    } else {
        return env->nil_obj();
    }
}
}