mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 19:40:18 +00:00
intern ascii literals
This commit is contained in:
parent
60802a8e36
commit
ffbc7e9ade
@ -27,6 +27,8 @@ typedef struct VM {
|
|||||||
|
|
||||||
py_Callbacks callbacks;
|
py_Callbacks callbacks;
|
||||||
|
|
||||||
|
py_TValue ascii_literals[128+1];
|
||||||
|
|
||||||
py_TValue last_retval;
|
py_TValue last_retval;
|
||||||
py_TValue curr_exception;
|
py_TValue curr_exception;
|
||||||
volatile bool is_signal_interrupted;
|
volatile bool is_signal_interrupted;
|
||||||
|
@ -83,6 +83,12 @@ void VM__ctor(VM* self) {
|
|||||||
ValueStack__ctor(&self->stack);
|
ValueStack__ctor(&self->stack);
|
||||||
|
|
||||||
/* Init Builtin Types */
|
/* Init Builtin Types */
|
||||||
|
for(int i = 0; i < 128; i++) {
|
||||||
|
char* p = py_newstrn(&self->ascii_literals[i], 1);
|
||||||
|
*p = i;
|
||||||
|
}
|
||||||
|
py_newstrn(&self->ascii_literals[128], 0);
|
||||||
|
|
||||||
// 0: unused
|
// 0: unused
|
||||||
void* placeholder = TypeList__emplace(&self->types);
|
void* placeholder = TypeList__emplace(&self->types);
|
||||||
memset(placeholder, 0, sizeof(py_TypeInfo));
|
memset(placeholder, 0, sizeof(py_TypeInfo));
|
||||||
@ -640,6 +646,10 @@ void ManagedHeap__mark(ManagedHeap* self) {
|
|||||||
for(py_TValue* p = vm->stack.begin; p != vm->stack.end; p++) {
|
for(py_TValue* p = vm->stack.begin; p != vm->stack.end; p++) {
|
||||||
pk__mark_value(p);
|
pk__mark_value(p);
|
||||||
}
|
}
|
||||||
|
// mark ascii literals
|
||||||
|
for(int i = 0; i < c11__count_array(vm->ascii_literals); i++) {
|
||||||
|
pk__mark_value(&vm->ascii_literals[i]);
|
||||||
|
}
|
||||||
// mark modules
|
// mark modules
|
||||||
ModuleDict__apply_mark(&vm->modules, mark_object);
|
ModuleDict__apply_mark(&vm->modules, mark_object);
|
||||||
// mark types
|
// mark types
|
||||||
|
@ -456,8 +456,7 @@ static bool builtins_chr(int argc, py_Ref argv) {
|
|||||||
PY_CHECK_ARG_TYPE(0, tp_int);
|
PY_CHECK_ARG_TYPE(0, tp_int);
|
||||||
py_i64 val = py_toint(py_arg(0));
|
py_i64 val = py_toint(py_arg(0));
|
||||||
if(val < 0 || val > 128) { return ValueError("chr() arg not in range(128)"); }
|
if(val < 0 || val > 128) { return ValueError("chr() arg not in range(128)"); }
|
||||||
char* data = py_newstrn(py_retval(), 1);
|
py_assign(py_retval(), &pk_current_vm->ascii_literals[val]);
|
||||||
data[0] = (char)val;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,6 +21,17 @@ char* py_newstrn(py_Ref out, int size) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void py_newstrv(py_OutRef out, c11_sv sv) {
|
void py_newstrv(py_OutRef out, c11_sv sv) {
|
||||||
|
if(sv.size == 0) {
|
||||||
|
*out = pk_current_vm->ascii_literals[128];
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if(sv.size == 1) {
|
||||||
|
int c = sv.data[0];
|
||||||
|
if(c >= 0 && c < 128) {
|
||||||
|
*out = pk_current_vm->ascii_literals[c];
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
char* data = py_newstrn(out, sv.size);
|
char* data = py_newstrn(out, sv.size);
|
||||||
memcpy(data, sv.data, sv.size);
|
memcpy(data, sv.data, sv.size);
|
||||||
}
|
}
|
||||||
|
@ -190,6 +190,8 @@ assert (1 != '1') is True
|
|||||||
assert (1 == '1') is False
|
assert (1 == '1') is False
|
||||||
assert 1 == 1.0
|
assert 1 == 1.0
|
||||||
|
|
||||||
|
assert chr(97) is 'a'
|
||||||
|
|
||||||
exit()
|
exit()
|
||||||
|
|
||||||
# test format()
|
# test format()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user