This commit is contained in:
blueloveTH 2024-08-15 01:51:14 +08:00
parent 2f14689e2f
commit 1d319c4ab6
5 changed files with 49 additions and 61 deletions

View File

@ -615,6 +615,8 @@ enum py_PredefinedTypes {
tp_ellipsis, tp_ellipsis,
tp_generator, tp_generator,
/* builtin exceptions */ /* builtin exceptions */
tp_SystemExit,
tp_KeyboardInterrupt,
tp_StopIteration, tp_StopIteration,
tp_SyntaxError, tp_SyntaxError,
tp_StackOverflowError, tp_StackOverflowError,

View File

@ -161,32 +161,9 @@ FrameResult VM__run_top_frame(VM* self) {
DISPATCH(); DISPATCH();
} }
case OP_LOAD_NAME: { case OP_LOAD_NAME: {
assert(frame->is_dynamic);
py_Name name = byte.arg; py_Name name = byte.arg;
py_TValue* tmp; py_TValue* tmp;
if(!frame->is_dynamic) {
// locals
tmp = Frame__f_locals_try_get(frame, name);
if(tmp != NULL) {
if(py_isnil(tmp)) {
UnboundLocalError(name);
goto __ERROR;
}
PUSH(tmp);
DISPATCH();
}
// closure
tmp = Frame__f_closure_try_get(frame, name);
if(tmp != NULL) {
PUSH(tmp);
DISPATCH();
}
// globals
tmp = py_getdict(frame->module, name);
if(tmp != NULL) {
PUSH(tmp);
DISPATCH();
}
} else {
py_newstr(SP()++, py_name2str(name)); py_newstr(SP()++, py_name2str(name));
// locals // locals
if(!py_isnone(&frame->p0[1])) { if(!py_isnone(&frame->p0[1])) {
@ -212,7 +189,6 @@ FrameResult VM__run_top_frame(VM* self) {
goto __ERROR; goto __ERROR;
} }
} }
}
// builtins // builtins
tmp = py_getdict(&self->builtins, name); tmp = py_getdict(&self->builtins, name);
if(tmp != NULL) { if(tmp != NULL) {

View File

@ -136,30 +136,33 @@ void VM__ctor(VM* self) {
self->builtins = pk_builtins__register(); self->builtins = pk_builtins__register();
// inject some builtin expections // inject some builtin expections
#define INJECT_BUILTIN_EXC(name) \ #define INJECT_BUILTIN_EXC(name, TBase) \
do { \ do { \
py_Type type = pk_newtype(#name, tp_Exception, &self->builtins, NULL, false, true); \ py_Type type = pk_newtype(#name, TBase, &self->builtins, NULL, false, true); \
py_setdict(&self->builtins, py_name(#name), py_tpobject(type)); \ py_setdict(&self->builtins, py_name(#name), py_tpobject(type)); \
validate(tp_##name, type); \ validate(tp_##name, type); \
} while(0) } while(0)
INJECT_BUILTIN_EXC(StopIteration); INJECT_BUILTIN_EXC(SystemExit, tp_BaseException);
INJECT_BUILTIN_EXC(SyntaxError); INJECT_BUILTIN_EXC(KeyboardInterrupt, tp_BaseException);
INJECT_BUILTIN_EXC(StackOverflowError);
INJECT_BUILTIN_EXC(IOError); INJECT_BUILTIN_EXC(StopIteration, tp_Exception);
INJECT_BUILTIN_EXC(OSError); INJECT_BUILTIN_EXC(SyntaxError, tp_Exception);
INJECT_BUILTIN_EXC(NotImplementedError); INJECT_BUILTIN_EXC(StackOverflowError, tp_Exception);
INJECT_BUILTIN_EXC(TypeError); INJECT_BUILTIN_EXC(IOError, tp_Exception);
INJECT_BUILTIN_EXC(IndexError); INJECT_BUILTIN_EXC(OSError, tp_Exception);
INJECT_BUILTIN_EXC(ValueError); INJECT_BUILTIN_EXC(NotImplementedError, tp_Exception);
INJECT_BUILTIN_EXC(RuntimeError); INJECT_BUILTIN_EXC(TypeError, tp_Exception);
INJECT_BUILTIN_EXC(ZeroDivisionError); INJECT_BUILTIN_EXC(IndexError, tp_Exception);
INJECT_BUILTIN_EXC(NameError); INJECT_BUILTIN_EXC(ValueError, tp_Exception);
INJECT_BUILTIN_EXC(UnboundLocalError); INJECT_BUILTIN_EXC(RuntimeError, tp_Exception);
INJECT_BUILTIN_EXC(AttributeError); INJECT_BUILTIN_EXC(ZeroDivisionError, tp_Exception);
INJECT_BUILTIN_EXC(ImportError); INJECT_BUILTIN_EXC(NameError, tp_Exception);
INJECT_BUILTIN_EXC(AssertionError); INJECT_BUILTIN_EXC(UnboundLocalError, tp_Exception);
INJECT_BUILTIN_EXC(KeyError); INJECT_BUILTIN_EXC(AttributeError, tp_Exception);
INJECT_BUILTIN_EXC(ImportError, tp_Exception);
INJECT_BUILTIN_EXC(AssertionError, tp_Exception);
INJECT_BUILTIN_EXC(KeyError, tp_Exception);
#undef INJECT_BUILTIN_EXC #undef INJECT_BUILTIN_EXC
#undef validate #undef validate

View File

@ -169,9 +169,13 @@ static bool builtins_exit(int argc, py_Ref argv) {
PY_CHECK_ARG_TYPE(0, tp_int); PY_CHECK_ARG_TYPE(0, tp_int);
code = py_toint(argv); code = py_toint(argv);
} }
// return py_exception("SystemExit", "%d", code);
exit(code); exit(code);
return false; return false;
// py_TValue sso_code;
// py_newint(&sso_code, code);
// bool ok = py_tpcall(tp_SystemExit, 1, &sso_code);
// if(!ok) return false;
// return py_raise(py_retval());
} }
static bool builtins_len(int argc, py_Ref argv) { static bool builtins_len(int argc, py_Ref argv) {

View File

@ -128,3 +128,6 @@ assert f'{a:10}' == 'A '
assert f'{A()!r:10}' == 'A() ' assert f'{A()!r:10}' == 'A() '
assert f'{A():10}' == 'A ' assert f'{A():10}' == 'A '
assert f'{A():10}' == 'A ' assert f'{A():10}' == 'A '
a = ['1', '2', '3']
assert f'a = {'\n'.join(a)}' == 'a = 1\n2\n3'