Skip to content
This repository has been archived by the owner on Jul 17, 2024. It is now read-only.

Commit

Permalink
chore: Rename internal methods of PythonLikeObject to use '$' instead…
Browse files Browse the repository at this point in the history
… of '__'

- This prevents naming conflicts in Python since '$' is invalid in
  Python identifier
- Additionally, it make it consistent with the names of other
  generated methods/fields in the interpreter
  • Loading branch information
Christopher-Chianelli committed Mar 8, 2024
1 parent 5f902c4 commit e7574b6
Show file tree
Hide file tree
Showing 49 changed files with 276 additions and 276 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public static void updateJavaObjectFromPythonObject(PythonLikeObject javaObject,
OpaquePythonReference pythonObject,
Map<Number, PythonLikeObject> instanceMap) {
Map<String, PythonLikeObject> dict = getPythonReferenceDict(pythonObject, instanceMap);
dict.forEach(javaObject::__setAttribute);
dict.forEach(javaObject::$setAttribute);
}

public static PythonLikeObject callPythonReference(OpaquePythonReference object, List<PythonLikeObject> positionalArguments,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public static PythonLikeType translatePythonClass(PythonCompiledClass pythonComp
classWriter.visit(Opcodes.V11, Modifier.PUBLIC, internalClassName, null,
superClassType.getJavaTypeInternalName(), interfaces);

pythonCompiledClass.staticAttributeNameToObject.forEach(pythonLikeType::__setAttribute);
pythonCompiledClass.staticAttributeNameToObject.forEach(pythonLikeType::$setAttribute);

classWriter.visitField(Modifier.PUBLIC | Modifier.STATIC, TYPE_FIELD_NAME, Type.getDescriptor(PythonLikeType.class),
null, null);
Expand All @@ -154,7 +154,7 @@ public static PythonLikeType translatePythonClass(PythonCompiledClass pythonComp

for (Map.Entry<String, PythonLikeObject> staticAttributeEntry : pythonCompiledClass.staticAttributeNameToObject
.entrySet()) {
pythonLikeType.__setAttribute(staticAttributeEntry.getKey(), staticAttributeEntry.getValue());
pythonLikeType.$setAttribute(staticAttributeEntry.getKey(), staticAttributeEntry.getValue());
}

Map<String, PythonLikeType> attributeNameToTypeMap = new HashMap<>();
Expand Down Expand Up @@ -259,17 +259,17 @@ public static PythonLikeType translatePythonClass(PythonCompiledClass pythonComp
PythonBytecodeToJavaBytecodeTranslator.writeClassOutput(BuiltinTypes.classNameToBytecode, className,
classWriter.toByteArray());

pythonLikeType.__setAttribute("__name__", PythonString.valueOf(pythonCompiledClass.className));
pythonLikeType.__setAttribute("__qualname__", PythonString.valueOf(pythonCompiledClass.qualifiedName));
pythonLikeType.__setAttribute("__module__", PythonString.valueOf(pythonCompiledClass.module));
pythonLikeType.$setAttribute("__name__", PythonString.valueOf(pythonCompiledClass.className));
pythonLikeType.$setAttribute("__qualname__", PythonString.valueOf(pythonCompiledClass.qualifiedName));
pythonLikeType.$setAttribute("__module__", PythonString.valueOf(pythonCompiledClass.module));

PythonLikeDict annotations = new PythonLikeDict();
pythonCompiledClass.typeAnnotations.forEach((name, type) -> annotations.put(PythonString.valueOf(name), type));
pythonLikeType.__setAttribute("__annotations__", annotations);
pythonLikeType.$setAttribute("__annotations__", annotations);

PythonLikeTuple mro = new PythonLikeTuple();
mro.addAll(superTypeList);
pythonLikeType.__setAttribute("__mro__", mro);
pythonLikeType.$setAttribute("__mro__", mro);

Class<? extends PythonLikeObject> generatedClass;
try {
Expand Down Expand Up @@ -339,7 +339,7 @@ public static void setSelfStaticInstances(PythonCompiledClass pythonCompiledClas
objectInstance.$setCPythonId(PythonInteger.valueOf(pythonReferenceId.longValue()));
objectInstance.$setInstanceMap(instanceMap);
objectInstance.$readFieldsFromCPythonReference();
pythonLikeType.__setAttribute(attributeName, objectInstance);
pythonLikeType.$setAttribute(attributeName, objectInstance);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new RuntimeException("Unable to construct instance of class (" + generatedClass + ")", e);
}
Expand Down Expand Up @@ -442,7 +442,7 @@ private static Class<?> createBytecodeForMethodAndSetOnClass(String className, P

generatedClass.getField(getJavaMethodName(methodEntry.getKey()))
.set(null, functionInstance);
pythonLikeType.__setAttribute(methodEntry.getKey(), translatedPythonMethodWrapper);
pythonLikeType.$setAttribute(methodEntry.getKey(), translatedPythonMethodWrapper);
return functionClass;
} catch (IllegalAccessException | NoSuchFieldException e) {
throw new IllegalStateException("Impossible State: could not access method (" + methodEntry.getKey()
Expand Down Expand Up @@ -508,7 +508,7 @@ private static Class<?> createPythonWrapperMethod(String methodName, PythonCompi
Type.getDescriptor(PythonLikeType.class));
methodVisitor.visitLdcInsn(methodName);
methodVisitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(PythonLikeObject.class),
"__getAttributeOrError",
"$getAttributeOrError",
Type.getMethodDescriptor(Type.getType(PythonLikeObject.class), Type.getType(String.class)),
true);
methodVisitor.visitTypeInsn(Opcodes.CHECKCAST, Type.getInternalName(PythonLikeFunction.class));
Expand Down Expand Up @@ -805,7 +805,7 @@ public static Type getVirtualFunctionReturnType(PythonCompiledFunction function)
public static void createGetAttribute(ClassWriter classWriter, String classInternalName, String superInternalName,
Collection<String> instanceAttributes,
Map<String, PythonLikeType> fieldToType) {
MethodVisitor methodVisitor = classWriter.visitMethod(Modifier.PUBLIC, "__getAttributeOrNull",
MethodVisitor methodVisitor = classWriter.visitMethod(Modifier.PUBLIC, "$getAttributeOrNull",
Type.getMethodDescriptor(Type.getType(PythonLikeObject.class),
Type.getType(String.class)),
null, null);
Expand All @@ -823,7 +823,7 @@ public static void createGetAttribute(ClassWriter classWriter, String classInter
}, () -> {
methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
methodVisitor.visitVarInsn(Opcodes.ALOAD, 1);
methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superInternalName, "__getAttributeOrNull",
methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superInternalName, "$getAttributeOrNull",
Type.getMethodDescriptor(Type.getType(PythonLikeObject.class),
Type.getType(String.class)),
false);
Expand All @@ -837,7 +837,7 @@ public static void createGetAttribute(ClassWriter classWriter, String classInter
public static void createSetAttribute(ClassWriter classWriter, String classInternalName, String superInternalName,
Collection<String> instanceAttributes,
Map<String, PythonLikeType> fieldToType) {
MethodVisitor methodVisitor = classWriter.visitMethod(Modifier.PUBLIC, "__setAttribute",
MethodVisitor methodVisitor = classWriter.visitMethod(Modifier.PUBLIC, "$setAttribute",
Type.getMethodDescriptor(Type.VOID_TYPE,
Type.getType(String.class),
Type.getType(PythonLikeObject.class)),
Expand All @@ -860,7 +860,7 @@ public static void createSetAttribute(ClassWriter classWriter, String classInter
methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
methodVisitor.visitVarInsn(Opcodes.ALOAD, 1);
methodVisitor.visitVarInsn(Opcodes.ALOAD, 2);
methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superInternalName, "__setAttribute",
methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superInternalName, "$setAttribute",
Type.getMethodDescriptor(Type.VOID_TYPE,
Type.getType(String.class),
Type.getType(PythonLikeObject.class)),
Expand All @@ -875,7 +875,7 @@ public static void createSetAttribute(ClassWriter classWriter, String classInter
public static void createDeleteAttribute(ClassWriter classWriter, String classInternalName, String superInternalName,
Collection<String> instanceAttributes,
Map<String, PythonLikeType> fieldToType) {
MethodVisitor methodVisitor = classWriter.visitMethod(Modifier.PUBLIC, "__deleteAttribute",
MethodVisitor methodVisitor = classWriter.visitMethod(Modifier.PUBLIC, "$deleteAttribute",
Type.getMethodDescriptor(Type.VOID_TYPE,
Type.getType(String.class)),
null, null);
Expand All @@ -894,7 +894,7 @@ public static void createDeleteAttribute(ClassWriter classWriter, String classIn
}, () -> {
methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
methodVisitor.visitVarInsn(Opcodes.ALOAD, 1);
methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superInternalName, "__deleteAttribute",
methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superInternalName, "$deleteAttribute",
Type.getMethodDescriptor(Type.VOID_TYPE,
Type.getType(String.class)),
false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -656,12 +656,12 @@ private static void generateAdvanceGeneratorMethodForYieldFrom(ClassWriter class
methodVisitor.visitInsn(Opcodes.DUP);
methodVisitor.visitMethodInsn(Opcodes.INVOKEINTERFACE,
Type.getInternalName(PythonLikeObject.class),
"__getType", Type.getMethodDescriptor(Type.getType(PythonLikeType.class)),
"$getType", Type.getMethodDescriptor(Type.getType(PythonLikeType.class)),
true);
methodVisitor.visitLdcInsn("throw");
methodVisitor.visitMethodInsn(Opcodes.INVOKEINTERFACE,
Type.getInternalName(PythonLikeObject.class),
"__getAttributeOrNull",
"$getAttributeOrNull",
Type.getMethodDescriptor(Type.getType(PythonLikeObject.class),
Type.getType(String.class)),
true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public interface PythonLikeObject {
* @return The attribute of the object that corresponds with attributeName
* @throws AttributeError if the attribute does not exist
*/
PythonLikeObject __getAttributeOrNull(String attributeName);
PythonLikeObject $getAttributeOrNull(String attributeName);

/**
* Gets an attribute by name.
Expand All @@ -36,8 +36,8 @@ public interface PythonLikeObject {
* @return The attribute of the object that corresponds with attributeName
* @throws AttributeError if the attribute does not exist
*/
default PythonLikeObject __getAttributeOrError(String attributeName) {
PythonLikeObject out = this.__getAttributeOrNull(attributeName);
default PythonLikeObject $getAttributeOrError(String attributeName) {
PythonLikeObject out = this.$getAttributeOrNull(attributeName);
if (out == null) {
throw new AttributeError("object '" + this + "' does not have attribute '" + attributeName + "'");
}
Expand All @@ -50,52 +50,52 @@ default PythonLikeObject __getAttributeOrError(String attributeName) {
* @param attributeName Name of the attribute to set
* @param value Value to set the attribute to
*/
void __setAttribute(String attributeName, PythonLikeObject value);
void $setAttribute(String attributeName, PythonLikeObject value);

/**
* Delete an attribute by name.
*
* @param attributeName Name of the attribute to delete
*/
void __deleteAttribute(String attributeName);
void $deleteAttribute(String attributeName);

/**
* Returns the type describing the object
*
* @return the type describing the object
*/
PythonLikeType __getType();
PythonLikeType $getType();

/**
* Return a generic version of {@link PythonLikeObject#__getType()}. This is used in bytecode
* Return a generic version of {@link PythonLikeObject#$getType()}. This is used in bytecode
* generation and not at runtime. For example, for a list of integers, this return
* list[int], while getType returns list. Both methods are needed so type([1,2,3]) is type(['a', 'b', 'c'])
* return True.
*
* @return the generic version of this object's type. Must not be used in identity checks.
*/
default PythonLikeType __getGenericType() {
return __getType();
default PythonLikeType $getGenericType() {
return $getType();
}

default PythonLikeObject $method$__getattribute__(PythonString pythonName) {
String name = pythonName.value;
PythonLikeObject objectResult = __getAttributeOrNull(name);
PythonLikeObject objectResult = $getAttributeOrNull(name);
if (objectResult != null) {
return objectResult;
}

PythonLikeType type = __getType();
PythonLikeObject typeResult = type.__getAttributeOrNull(name);
PythonLikeType type = $getType();
PythonLikeObject typeResult = type.$getAttributeOrNull(name);
if (typeResult != null) {
PythonLikeObject maybeDescriptor = typeResult.__getAttributeOrNull(PythonTernaryOperator.GET.dunderMethod);
PythonLikeObject maybeDescriptor = typeResult.$getAttributeOrNull(PythonTernaryOperator.GET.dunderMethod);
if (maybeDescriptor == null) {
maybeDescriptor = typeResult.__getType().__getAttributeOrNull(PythonTernaryOperator.GET.dunderMethod);
maybeDescriptor = typeResult.$getType().$getAttributeOrNull(PythonTernaryOperator.GET.dunderMethod);
}

if (maybeDescriptor != null) {
if (!(maybeDescriptor instanceof PythonLikeFunction)) {
throw new UnsupportedOperationException("'" + maybeDescriptor.__getType() + "' is not callable");
throw new UnsupportedOperationException("'" + maybeDescriptor.$getType() + "' is not callable");
}
return TernaryDunderBuiltin.GET_DESCRIPTOR.invoke(typeResult, this, type);
}
Expand All @@ -107,13 +107,13 @@ default PythonLikeType __getGenericType() {

default PythonLikeObject $method$__setattr__(PythonString pythonName, PythonLikeObject value) {
String name = pythonName.value;
__setAttribute(name, value);
$setAttribute(name, value);
return PythonNone.INSTANCE;
}

default PythonLikeObject $method$__delattr__(PythonString pythonName) {
String name = pythonName.value;
__deleteAttribute(name);
$deleteAttribute(name);
return PythonNone.INSTANCE;
}

Expand Down Expand Up @@ -141,7 +141,7 @@ default PythonLikeType __getGenericType() {
} else {
position = String.valueOf(System.identityHashCode(this));
}
return PythonString.valueOf("<" + __getType().getTypeName() + " object at " + position + ">");
return PythonString.valueOf("<" + $getType().getTypeName() + " object at " + position + ">");
}

default PythonLikeObject $method$__format__() {
Expand All @@ -153,6 +153,6 @@ default PythonLikeType __getGenericType() {
}

default PythonLikeObject $method$__hash__() {
throw new TypeError("unhashable type: '" + __getType().getTypeName() + "'");
throw new TypeError("unhashable type: '" + $getType().getTypeName() + "'");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ public static void createDispatchesFor(PythonLikeType pythonLikeType) {
createDispatchForMethod(pythonLikeType, methodName, pythonLikeType.getMethodType(methodName).orElseThrow(),
pythonLikeType.getMethodKind(methodName)
.orElse(PythonClassTranslator.PythonMethodKind.VIRTUAL_METHOD));
pythonLikeType.__setAttribute(methodName, overloadDispatch);
pythonLikeType.$setAttribute(methodName, overloadDispatch);
}

if (pythonLikeType.getConstructorType().isPresent()) {
PythonLikeFunction overloadDispatch =
createDispatchForMethod(pythonLikeType, "__init__", pythonLikeType.getConstructorType().orElseThrow(),
PythonClassTranslator.PythonMethodKind.VIRTUAL_METHOD);
pythonLikeType.setConstructor(overloadDispatch);
pythonLikeType.__setAttribute("__init__", overloadDispatch);
pythonLikeType.$setAttribute("__init__", overloadDispatch);
}
}

Expand Down Expand Up @@ -142,7 +142,7 @@ private static PythonLikeFunction createDispatchForMethod(PythonLikeType pythonL
}

private static void createGetTypeFunction(PythonClassTranslator.PythonMethodKind kind, ClassWriter classWriter) {
MethodVisitor methodVisitor = classWriter.visitMethod(Modifier.PUBLIC, "__getType",
MethodVisitor methodVisitor = classWriter.visitMethod(Modifier.PUBLIC, "$getType",
Type.getMethodDescriptor(Type.getType(PythonLikeType.class)),
null,
null);
Expand Down Expand Up @@ -484,7 +484,7 @@ private static void createGenericDispatch(MethodVisitor methodVisitor,
public static String getCallErrorInfo(List<PythonLikeObject> positionalArgs,
Map<PythonString, PythonLikeObject> namedArgs) {
return "Could not find an overload that accept " + positionalArgs.stream()
.map(arg -> arg.__getType().getTypeName()).collect(Collectors.joining(", ", "(", ") argument types. "));
.map(arg -> arg.$getType().getTypeName()).collect(Collectors.joining(", ", "(", ") argument types. "));
}

private static SortedMap<PythonLikeType, List<PythonFunctionSignature>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ public BinaryDunderBuiltin(PythonBinaryOperator operator) {

PythonLikeObject object = positionalArguments.get(0);
PythonLikeObject arg = positionalArguments.get(1);
PythonLikeFunction dunderMethod = (PythonLikeFunction) object.__getType().__getAttributeOrError(DUNDER_METHOD_NAME);
PythonLikeFunction dunderMethod = (PythonLikeFunction) object.$getType().$getAttributeOrError(DUNDER_METHOD_NAME);
return dunderMethod.$call(List.of(object, arg), Map.of(), null);
}

public PythonLikeObject invoke(PythonLikeObject object, PythonLikeObject arg) {
PythonLikeFunction dunderMethod = (PythonLikeFunction) object.__getType().__getAttributeOrError(DUNDER_METHOD_NAME);
PythonLikeFunction dunderMethod = (PythonLikeFunction) object.$getType().$getAttributeOrError(DUNDER_METHOD_NAME);
return dunderMethod.$call(List.of(object, arg), Map.of(), null);
}
}
Loading

0 comments on commit e7574b6

Please sign in to comment.