blueloveTH 2 anni fa
parent
commit
d204c67d62
3 ha cambiato i file con 18 aggiunte e 13 eliminazioni
  1. 1 1
      run_profile.sh
  2. 2 12
      src/pocketpy.h
  3. 15 0
      src/vm.h

+ 1 - 1
run_profile.sh

@@ -1,5 +1,5 @@
 clang++ -pg -O2 -std=c++17 -fno-rtti -stdlib=libc++ -Wall -o pocketpy src/main.cpp
-time ./pocketpy benchmarks/sort.py
+time ./pocketpy benchmarks/primes.py
 mv benchmarks/gmon.out .
 gprof pocketpy gmon.out > gprof.txt
 rm gmon.out

+ 2 - 12
src/pocketpy.h

@@ -389,19 +389,9 @@ inline void init_builtins(VM* _vm) {
     });
 
     _vm->bind_method<1>("str", "__getitem__", [](VM* vm, ArgsView args) {
-        const Str& self = _CAST(Str&, args[0]);
-
-        if(is_type(args[1], vm->tp_slice)){
-            const Slice& s = _CAST(Slice&, args[1]);
-            int start, stop, step;
-            vm->parse_int_slice(s, self.u8_length(), start, stop, step);
-            return VAR(self.u8_slice(start, stop, step));
-        }
-
-        int index = CAST(int, args[1]);
-        index = vm->normalized_index(index, self.u8_length());
-        return VAR(self.u8_getitem(index));
+        return PyStrGetItem(vm, args[0], args[1]);
     });
+    _vm->_type_info("str")->m__getitem__ = PyStrGetItem;
 
     _vm->bind_method<1>("str", "__gt__", [](VM* vm, ArgsView args) {
         const Str& self = _CAST(Str&, args[0]);

+ 15 - 0
src/vm.h

@@ -1263,4 +1263,19 @@ inline PyObject* PyListSetItem(VM* vm, PyObject* obj, PyObject* index, PyObject*
     return vm->None;
 }
 
+inline PyObject* PyStrGetItem(VM* vm, PyObject* obj, PyObject* index){
+    const Str& self = _CAST(Str&, obj);
+
+    if(is_type(index, vm->tp_slice)){
+        const Slice& s = _CAST(Slice&, index);
+        int start, stop, step;
+        vm->parse_int_slice(s, self.u8_length(), start, stop, step);
+        return VAR(self.u8_slice(start, stop, step));
+    }
+
+    int i = CAST(int, index);
+    i = vm->normalized_index(i, self.u8_length());
+    return VAR(self.u8_getitem(i));
+}
+
 }   // namespace pkpy