this repo has no description
at trunk 66 lines 2.5 kB view raw
1#include "compile-utils.h" 2 3#include "handles.h" 4#include "objects.h" 5#include "runtime.h" 6#include "str-builtins.h" 7#include "thread.h" 8 9namespace py { 10 11RawObject compile(Thread* thread, const Object& source, const Object& filename, 12 SymbolId mode, word flags, int optimize) { 13 HandleScope scope(thread); 14 Runtime* runtime = thread->runtime(); 15 Object mode_str(&scope, runtime->symbols()->at(mode)); 16 Object flags_int(&scope, runtime->newInt(flags)); 17 Object optimize_int(&scope, SmallInt::fromWord(optimize)); 18 19 Object dunder_import(&scope, runtime->lookupNameInModule(thread, ID(builtins), 20 ID(__import__))); 21 if (dunder_import.isErrorException()) return *dunder_import; 22 Object compiler_name(&scope, runtime->symbols()->at(ID(_compiler))); 23 Object import_result( 24 &scope, Interpreter::call1(thread, dunder_import, compiler_name)); 25 if (import_result.isErrorException()) return *import_result; 26 Object none(&scope, NoneType::object()); 27 return thread->invokeFunction6(ID(_compiler), ID(compile), source, filename, 28 mode_str, flags_int, none, optimize_int); 29} 30 31RawObject mangle(Thread* thread, const Object& privateobj, const Str& ident) { 32 Runtime* runtime = thread->runtime(); 33 // Only mangle names that start with two underscores, but do not end with 34 // two underscores or contain a dot. 35 word ident_length = ident.length(); 36 if (ident_length < 2 || ident.byteAt(0) != '_' || ident.byteAt(1) != '_' || 37 (ident.byteAt(ident_length - 2) == '_' && 38 ident.byteAt(ident_length - 1) == '_') || 39 strFindAsciiChar(ident, '.') >= 0) { 40 return *ident; 41 } 42 43 if (!runtime->isInstanceOfStr(*privateobj)) { 44 return *ident; 45 } 46 HandleScope scope(thread); 47 Str privateobj_str(&scope, strUnderlying(*privateobj)); 48 word privateobj_length = privateobj_str.length(); 49 word begin = 0; 50 while (begin < privateobj_length && privateobj_str.byteAt(begin) == '_') { 51 begin++; 52 } 53 if (begin == privateobj_length) { 54 return *ident; 55 } 56 57 word length0 = privateobj_length - begin; 58 word length = length0 + ident_length + 1; 59 MutableBytes result(&scope, runtime->newMutableBytesUninitialized(length)); 60 result.byteAtPut(0, '_'); 61 result.replaceFromWithStrStartAt(1, *privateobj_str, length0, begin); 62 result.replaceFromWithStr(1 + length0, *ident, ident_length); 63 return result.becomeStr(); 64} 65 66} // namespace py