{"record":{"id":"0ed821b8c3cb3912","repo":"oracle/graal","slug":"expected-an-espressoexternalobjectconstant-got-0ed821","errorCode":null,"errorMessage":"Expected an EspressoExternalObjectConstant, got ","messagePattern":"Expected an EspressoExternalObjectConstant, got ","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"espresso-compiler-stub/src/com.oracle.truffle.espresso.vmaccess/src/com/oracle/truffle/espresso/vmaccess/EspressoExternalVMAccess.java","lineNumber":630,"sourceCode":"            throw new IllegalArgumentException(\"Expected an array constant for src, got \" + srcArray.getType());\n        }\n        if (!destArray.getType().isArray()) {\n            throw new IllegalArgumentException(\"Expected an array constant for dest, got \" + destArray.getType());\n        }\n        try {\n            invoke(java_lang_System_arraycopy, null, srcArray, JavaConstant.forInt(srcPos), destArray, JavaConstant.forInt(destPos), JavaConstant.forInt(length));\n        } catch (InvocationException e) {\n            if (e.getCause() instanceof PolyglotException polyglotException) {\n                throw throwHostException(polyglotException);\n            }\n            throw e;\n        }\n    }\n\n    @Override\n    public void writeArrayElement(JavaConstant array, int index, JavaConstant element) {\n        if (!(array instanceof EspressoExternalObjectConstant espressoArray)) {\n            throw new IllegalArgumentException(\"Expected an EspressoExternalObjectConstant, got \" + safeGetClass(array));\n        }\n        EspressoResolvedObjectType arrayType = espressoArray.getType();\n        if (!arrayType.isArray()) {\n            throw new IllegalArgumentException(\"Expected an array type, got \" + arrayType);\n        }\n        ResolvedJavaType componentType = arrayType.getComponentType();\n        Object unwrappedValue;\n        if (componentType.isPrimitive()) {\n            if (componentType.getJavaKind() != element.getJavaKind()) {\n                throw new IllegalArgumentException(\"Element \" + element + \" should be a \" + componentType.getJavaKind() + \", got \" + element.getJavaKind());\n            }\n            unwrappedValue = element.asBoxedPrimitive();\n        } else if (element.isNull()) {\n            unwrappedValue = null;\n        } else {\n            if (!(element instanceof EspressoExternalObjectConstant objectElement)) {\n                throw new IllegalArgumentException(\"Element \" + element + \" should be an espresso object constant, got \" + safeGetClass(element));\n            }","sourceCodeStart":612,"sourceCodeEnd":648,"githubUrl":"https://github.com/oracle/graal/blob/a66e9ccd1d7bf2552883939aa0788dfd0e294aab/espresso-compiler-stub/src/com.oracle.truffle.espresso.vmaccess/src/com/oracle/truffle/espresso/vmaccess/EspressoExternalVMAccess.java#L612-L648","documentation":"writeArrayElement(array, index, element) performs a guest array store via setArrayElement, which requires the array constant to be an EspressoExternalObjectConstant wrapping guest memory. Constants from other JVMCI backends have no guest Value, so the call fails fast with IllegalArgumentException naming the actual class.","triggerScenarios":"Calling writeArrayElement with a host array constant (JavaConstant.forObject(int[])) or a constant from a different Espresso context.","commonSituations":"Test code building arrays with host factories; constants restored from serialization or caches tied to an older context; refactors mixing host and guest buffers.","solutions":["Create the array via this Espresso provider (createPrimitiveArray / asArrayConstant) before writing elements.","Guard array instanceof EspressoExternalObjectConstant; write to host arrays with plain Java instead.","Regenerate cached constants when the owning context is recreated."],"exampleFix":"// before\nvmAccess.writeArrayElement(hostArrayConstant, 0, JavaConstant.forInt(7));\n\n// after\nJavaConstant guestArr = vmAccess.createPrimitiveArray(JavaKind.Int, len);\nvmAccess.writeArrayElement(guestArr, 0, JavaConstant.forInt(7));","handlingStrategy":"type-guard","validationCode":"if (!(array instanceof EspressoExternalObjectConstant)) {\n    array = vmAccess.createPrimitiveArray(kind, length); // ensure guest array\n}","typeGuard":"static boolean isEspressoConstant(JavaConstant c) {\n    return c instanceof EspressoExternalObjectConstant;\n}","tryCatchPattern":"catch (IllegalArgumentException e) with 'Expected an EspressoExternalObjectConstant' at writeArrayElement -> allocate via the provider and retry the write.","preventionTips":["Create arrays via createPrimitiveArray/asArrayConstant before writing elements.","Write to host arrays with plain Java, not the guest write API.","Invalidate serialized/cached constants tied to disposed contexts."],"tags":["jvmci","espresso","graalvm","arrays","constants"],"backgroundTag":null,"analyzedSha":"a66e9ccd1d7bf2552883939aa0788dfd0e294aab","analyzedAt":"2026-08-14T13:58:47.161Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}