Skip to content

Commit 730c22c

Browse files
committed
实现import, 此时模块加载的path还有问题
1 parent 2d46389 commit 730c22c

14 files changed

+258
-25
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ ADD_EXECUTABLE(pythonvm main.cpp
2020
runtime/frameObject.cpp
2121
runtime/functionObject.cpp
2222
runtime/stringTable.cpp
23-
#runtime/module.cpp
23+
runtime/module.cpp
2424
#runtime/traceback.cpp
2525
#runtime/generator.cpp
2626
runtime/cellObject.cpp

object/hiObject.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ HiObject *HiObject::le(HiObject *rhs) {
7272
return klass()->le(this, rhs);
7373
}
7474

75-
HiObject *HiObject::get_attr(HiObject *x) {
75+
HiObject *HiObject::getattr(HiObject *x) {
7676
return klass()->getattr(this, x);
7777
}
7878

79-
HiObject *HiObject::set_attr(HiObject *x, HiObject *y) {
79+
HiObject *HiObject::setattr(HiObject *x, HiObject *y) {
8080
return klass()->setattr(this, x, y);
8181
}
8282

object/hiObject.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ class HiObject {
4848
HiObject *ge(HiObject *x);
4949
HiObject *le(HiObject *x);
5050

51-
HiObject *get_attr(HiObject *x);
52-
HiObject *set_attr(HiObject *x, HiObject *y);
51+
HiObject *getattr(HiObject *x);
52+
HiObject *setattr(HiObject *x, HiObject *y);
5353

5454
HiObject *subscr(HiObject *x);
5555
HiObject *contains(HiObject *x);

object/klass.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ HiObject *Klass::create_klass(HiObject *x, HiObject *supers, HiObject *name) {
6767
HiObject *Klass::allocate_instance(HiObject *callable, ArrayList<HiObject *> *args) {
6868
HiObject *instance = new HiObject();
6969
instance->set_klass(((HiTypeObject *) callable)->own_klass());
70-
HiObject *constructor = instance->get_attr(StringTable::get_instance()->init_str);
70+
HiObject *constructor = instance->getattr(StringTable::get_instance()->init_str);
7171
if (constructor != Universe::HiNone) {
7272
Interpreter::get_instance()->call_virtual(constructor, args);
7373
}
@@ -76,7 +76,7 @@ HiObject *Klass::allocate_instance(HiObject *callable, ArrayList<HiObject *> *ar
7676
}
7777

7878
HiObject *Klass::find_and_call(HiObject *lhs, ObjList args, HiObject *func_name) {
79-
HiObject *func = lhs->get_attr(func_name);
79+
HiObject *func = lhs->getattr(func_name);
8080
if (func != Universe::HiNone) {
8181
return Interpreter::get_instance()->call_virtual(func, args);
8282
}
@@ -152,12 +152,7 @@ HiObject *Klass::getattr(HiObject *x, HiObject *y) {
152152
}
153153
}
154154

155-
result = find_in_parents(x, y);
156-
if (MethodObject::is_function(result)) {
157-
result = new MethodObject((FunctionObject *) result, x);
158-
}
159-
160-
return result;
155+
return get_klass_attr(x, y);
161156
}
162157

163158
HiObject *Klass::setattr(HiObject *x, HiObject *y, HiObject *z) {
@@ -178,6 +173,17 @@ HiObject *Klass::setattr(HiObject *x, HiObject *y, HiObject *z) {
178173
return Universe::HiNone;
179174
}
180175

176+
HiObject* Klass::get_klass_attr(HiObject* x, HiObject* y) {
177+
HiObject* result = Universe::HiNone;
178+
179+
result = find_in_parents(x, y);
180+
if (MethodObject::is_function(result)) {
181+
result = new MethodObject((FunctionObject*)result, x);
182+
}
183+
184+
return result;
185+
}
186+
181187
void Klass::add_super(Klass *klass) {
182188
if (_super == NULL) {
183189
_super = new HiList();

object/klass.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class Klass {
8181
virtual HiObject *iter (HiObject* x) { return 0; }
8282

8383
virtual HiObject *allocate_instance(HiObject* callable, ArrayList<HiObject *> *args);
84+
virtual HiObject *get_klass_attr(HiObject *x, HiObject *y);
8485

8586
void* operator new(size_t size);
8687

runtime/interpreter.cpp

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "object/hiList.hpp"
1717
#include "object/hiDict.hpp"
1818
#include "memory/oopClosure.hpp"
19+
#include "runtime/module.hpp"
1920

2021
#define PUSH(x) _frame->stack()->append(x)
2122
#define POP() _frame->stack()->pop()
@@ -58,13 +59,30 @@ Interpreter::Interpreter() {
5859
_builtins->put(new HiString("dict"), DictKlass::get_instance()->type_object());
5960
}
6061

62+
void Interpreter::initialize() {
63+
_modules = new HiDict();
64+
_modules->put(new HiString("__builtins__"), _builtins);
65+
}
66+
6167
void Interpreter::run(CodeObject *codes) {
6268
_frame = new FrameObject(codes);
6369

6470
eval_frame();
6571
destroy_frame();
6672
}
6773

74+
HiDict *Interpreter::run_mod(CodeObject *codes, HiString *mod_name) {
75+
FrameObject *frame = new FrameObject(codes);
76+
frame->set_entry_frame(true);
77+
frame->locals()->put(ST(name), mod_name);
78+
79+
enter_frame(frame);
80+
eval_frame();
81+
HiDict *result = frame->locals();
82+
destroy_frame();
83+
return result;
84+
}
85+
6886
void Interpreter::eval_frame() {
6987
Block *b;
7088
FunctionObject *fo;
@@ -147,12 +165,25 @@ void Interpreter::eval_frame() {
147165
stack->append(u);
148166
break;
149167

168+
case ByteCode::BINARY_SUBTRACT:
169+
v = POP();
170+
w = POP();
171+
PUSH(w->sub(v));
172+
break;
173+
150174
case ByteCode::BINARY_SUBSCR:
151175
v = POP();
152176
w = POP();
153177
PUSH(w->subscr(v));
154178
break;
155179

180+
case ByteCode::STORE_MAP:
181+
w = POP();
182+
u = POP();
183+
v = TOP();
184+
((HiDict *) v)->put(w, u);
185+
break;
186+
156187
case ByteCode::STORE_SUBSCR:
157188
u = POP();
158189
v = POP();
@@ -173,7 +204,7 @@ void Interpreter::eval_frame() {
173204

174205
case ByteCode::FOR_ITER:
175206
v = TOP();
176-
w = v->get_attr(StringTable::get_instance()->next_str);
207+
w = v->getattr(StringTable::get_instance()->next_str);
177208
// TODO
178209
build_frame(w, NULL, 0);
179210

@@ -238,7 +269,7 @@ void Interpreter::eval_frame() {
238269
u = POP();
239270
v = _frame->names()->get(op_arg);
240271
w = POP();
241-
u->set_attr(v, w);
272+
u->setattr(v, w);
242273
break;
243274

244275
case ByteCode::DUP_TOPX:
@@ -291,17 +322,10 @@ void Interpreter::eval_frame() {
291322
PUSH(v);
292323
break;
293324

294-
case ByteCode::STORE_MAP:
295-
w = POP();
296-
u = POP();
297-
v = TOP();
298-
((HiDict *) v)->put(w, u);
299-
break;
300-
301325
case ByteCode::LOAD_ATTR:
302326
v = POP();
303327
w = _frame->_names->get(op_arg);
304-
PUSH(v->get_attr(w));
328+
PUSH(v->getattr(w));
305329
break;
306330

307331
case ByteCode::COMPARE_OP:
@@ -357,6 +381,21 @@ void Interpreter::eval_frame() {
357381
}
358382
break;
359383

384+
case ByteCode::IMPORT_NAME:
385+
POP();
386+
POP();
387+
v = _frame->names()->get(op_arg);
388+
w = _modules->get(v);
389+
if (w != Universe::HiNone) {
390+
PUSH(w);
391+
break;
392+
}
393+
394+
w = ModuleObject::import_module(v);
395+
_modules->put(v, w);
396+
PUSH(w);
397+
break;
398+
360399
case ByteCode::POP_JUMP_IF_FALSE:
361400
v = POP();
362401
if (v == Universe::HiFalse)
@@ -552,7 +591,7 @@ void Interpreter::build_frame(HiObject *callable, ObjList args, int op_arg) {
552591
PUSH(instance);
553592
} else {
554593
// __call__
555-
HiObject *m = callable->get_attr(ST(call));
594+
HiObject *m = callable->getattr(ST(call));
556595
if (m != Universe::HiNone) {
557596
build_frame(m, args, op_arg);
558597
} else {
@@ -595,6 +634,7 @@ void Interpreter::enter_frame(FrameObject *frame) {
595634

596635
void Interpreter::oops_do(OopClosure *f) {
597636
f->do_oop((HiObject **) &_builtins);
637+
f->do_oop((HiObject **) &_modules);
598638
f->do_oop((HiObject **) &_ret_value);
599639

600640
if (_frame) {

runtime/interpreter.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class OopClosure;
1717
class Interpreter {
1818
private:
1919
HiDict *_builtins;
20+
HiDict *_modules;
2021
FrameObject *_frame;
2122
HiObject *_ret_value;
2223

@@ -27,8 +28,12 @@ class Interpreter {
2728
public:
2829
static Interpreter *get_instance();
2930

31+
void initialize();
32+
3033
void run(CodeObject *codes);
3134

35+
HiDict *run_mod(CodeObject *codes, HiString *mod_name);
36+
3237
void build_frame(HiObject *pObject, ObjList args, int op_arg);
3338

3439
void enter_frame(FrameObject *frame);

runtime/module.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//
2+
// Created by Kosho on 2020/9/26.
3+
//
4+
5+
#include <unistd.h>
6+
#include <string>
7+
#include <sstream>
8+
9+
#include "object/hiDict.hpp"
10+
#include "object/hiList.hpp"
11+
#include "runtime/module.hpp"
12+
#include "runtime/universe.hpp"
13+
#include "runtime/interpreter.hpp"
14+
#include "runtime/stringTable.hpp"
15+
#include "util/bufferedInputStream.hpp"
16+
#include "code/binaryFileParser.hpp"
17+
#include "memory/oopClosure.hpp"
18+
19+
#define ST(x) StringTable::get_instance()->STR(x)
20+
#define STR(x) x##_str
21+
22+
ModuleKlass *ModuleKlass::_instance = NULL;
23+
24+
ModuleKlass *ModuleKlass::get_instance() {
25+
if (_instance == NULL)
26+
_instance = new ModuleKlass();
27+
28+
return _instance;
29+
}
30+
31+
ModuleKlass::ModuleKlass() {
32+
}
33+
34+
void ModuleKlass::initialize() {
35+
HiDict *dict = new HiDict();
36+
set_klass_dict(dict);
37+
set_name(new HiString("module"));
38+
(new HiTypeObject())->set_own_klass(this);
39+
add_super(ObjectKlass::get_instance());
40+
}
41+
42+
void ModuleKlass::oops_do(OopClosure *closure, HiObject *obj) {
43+
void *temp = &(((ModuleObject *) obj)->_mod_name);
44+
closure->do_oop((HiObject **) temp);
45+
}
46+
47+
size_t ModuleKlass::size() {
48+
return sizeof(ModuleObject);
49+
}
50+
51+
ModuleObject::ModuleObject(HiDict *x) {
52+
set_obj_dict(x);
53+
set_klass(ModuleKlass::get_instance());
54+
}
55+
56+
ModuleObject *ModuleObject::import_module(HiObject *x) {
57+
HiString *mod_name = (HiString *) x;
58+
HiString *file_name = (HiString *) (mod_name->add(ST(pyc_suf)));
59+
60+
// TODO path
61+
std::stringstream ss;
62+
ss << "F:\\myproject\\cpp\\pythonvm\\test\\" << file_name->value();
63+
const char *file_path = ss.str().c_str();
64+
if (access(file_path, R_OK) == -1) {
65+
return NULL;
66+
}
67+
68+
// BufferedInputStream stream(file_name->value());
69+
BufferedInputStream stream(file_path);
70+
BinaryFileParser parser(&stream);
71+
CodeObject *mod_code = parser.parse();
72+
HiDict *mod_dict = Interpreter::get_instance()->run_mod(mod_code, mod_name);
73+
return new ModuleObject(mod_dict);
74+
}
75+
76+
ModuleObject *ModuleObject::import_so(HiString *mod_name) {
77+
return nullptr;
78+
}
79+
80+
void ModuleObject::put(HiObject *x, HiObject *y) {
81+
82+
}
83+
84+
HiObject *ModuleObject::get(HiObject *x) {
85+
return nullptr;
86+
}
87+
88+
void ModuleObject::extend(ModuleObject *mo) {
89+
90+
}

runtime/module.hpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//
2+
// Created by Kosho on 2020/9/26.
3+
//
4+
5+
#ifndef PYTHONVM_MODULE_HPP
6+
#define PYTHONVM_MODULE_HPP
7+
8+
#include "object/hiObject.hpp"
9+
10+
class HiDict;
11+
12+
class OopClosure;
13+
14+
class ModuleKlass : public Klass {
15+
private:
16+
static ModuleKlass *_instance;
17+
18+
ModuleKlass();
19+
20+
public:
21+
static ModuleKlass *get_instance();
22+
23+
void initialize();
24+
25+
virtual void oops_do(OopClosure *closure, HiObject *obj);
26+
27+
virtual size_t size();
28+
};
29+
30+
class ModuleObject : public HiObject {
31+
friend class ModuleKlass;
32+
33+
private:
34+
HiString *_mod_name;
35+
36+
public:
37+
ModuleObject(HiDict *x);
38+
39+
static ModuleObject *import_module(HiObject *mod_name);
40+
41+
static ModuleObject *import_so(HiString *mod_name);
42+
43+
void put(HiObject *x, HiObject *y);
44+
45+
HiObject *get(HiObject *x);
46+
47+
void extend(ModuleObject *mo);
48+
};
49+
50+
#endif //PYTHONVM_MODULE_HPP

0 commit comments

Comments
 (0)