|
| 1 | +/* |
| 2 | + * Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * The Universal Permissive License (UPL), Version 1.0 |
| 6 | + * |
| 7 | + * Subject to the condition set forth below, permission is hereby granted to any |
| 8 | + * person obtaining a copy of this software, associated documentation and/or |
| 9 | + * data (collectively the "Software"), free of charge and under any and all |
| 10 | + * copyright rights in the Software, and any and all patent rights owned or |
| 11 | + * freely licensable by each licensor hereunder covering either (i) the |
| 12 | + * unmodified Software as contributed to or provided by such licensor, or (ii) |
| 13 | + * the Larger Works (as defined below), to deal in both |
| 14 | + * |
| 15 | + * (a) the Software, and |
| 16 | + * |
| 17 | + * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if |
| 18 | + * one is included with the Software each a "Larger Work" to which the Software |
| 19 | + * is contributed by such licensors), |
| 20 | + * |
| 21 | + * without restriction, including without limitation the rights to copy, create |
| 22 | + * derivative works of, display, perform, and distribute the Software and make, |
| 23 | + * use, sell, offer for sale, import, export, have made, and have sold the |
| 24 | + * Software and the Larger Work(s), and to sublicense the foregoing rights on |
| 25 | + * either these or other terms. |
| 26 | + * |
| 27 | + * This license is subject to the following condition: |
| 28 | + * |
| 29 | + * The above copyright notice and either this complete permission notice or at a |
| 30 | + * minimum a reference to the UPL must be included in all copies or substantial |
| 31 | + * portions of the Software. |
| 32 | + * |
| 33 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 34 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 35 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 36 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 37 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 38 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 39 | + * SOFTWARE. |
| 40 | + */ |
| 41 | +package com.oracle.graal.python.test.runtime; |
| 42 | + |
| 43 | +import static org.junit.Assert.assertEquals; |
| 44 | + |
| 45 | +import java.io.IOException; |
| 46 | +import java.util.function.Function; |
| 47 | + |
| 48 | +import org.graalvm.polyglot.Context; |
| 49 | +import org.graalvm.polyglot.Engine; |
| 50 | +import org.graalvm.polyglot.Instrument; |
| 51 | +import org.junit.Test; |
| 52 | + |
| 53 | +import com.oracle.truffle.api.ContextLocal; |
| 54 | +import com.oracle.truffle.api.TruffleContext; |
| 55 | +import com.oracle.truffle.api.instrumentation.ContextsListener; |
| 56 | +import com.oracle.truffle.api.instrumentation.TruffleInstrument; |
| 57 | +import com.oracle.truffle.api.nodes.LanguageInfo; |
| 58 | + |
| 59 | +public class ParseWithArgumentsInstrumentTests { |
| 60 | + private static final String TEST_INSTRUMENT_ID = "parse-with-arguments-instrument-tests"; |
| 61 | + |
| 62 | + @SuppressWarnings("unchecked") |
| 63 | + @Test |
| 64 | + public void instrumentParseWithArgumentsDuringLanguageContextInitializationExecutes() { |
| 65 | + try (Engine engine = Engine.create()) { |
| 66 | + Instrument instrument = engine.getInstruments().get(TEST_INSTRUMENT_ID); |
| 67 | + Function<org.graalvm.polyglot.Source, Void> registrar = instrument.lookup(Function.class); |
| 68 | + registrar.apply(org.graalvm.polyglot.Source.create("python", "x + 1")); |
| 69 | + try (Context context = Context.newBuilder("python").engine(engine).allowExperimentalOptions(true).allowAllAccess(true).build()) { |
| 70 | + assertEquals(42, context.eval("python", "42").asInt()); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @TruffleInstrument.Registration(id = TEST_INSTRUMENT_ID, services = Function.class) |
| 76 | + public static final class ParseWithArgumentsInitializationInstrument extends TruffleInstrument { |
| 77 | + private final ContextLocal<boolean[]> parsed = locals.createContextLocal((ctx) -> new boolean[1]); |
| 78 | + |
| 79 | + @Override |
| 80 | + protected void onCreate(Env env) { |
| 81 | + Function<org.graalvm.polyglot.Source, Void> registerSource = (polyglotSource) -> { |
| 82 | + com.oracle.truffle.api.source.Source source = com.oracle.truffle.api.source.Source.newBuilder( |
| 83 | + polyglotSource.getLanguage(), polyglotSource.getCharacters(), polyglotSource.getName()).build(); |
| 84 | + env.getInstrumenter().attachContextsListener(new ContextsListener() { |
| 85 | + @Override |
| 86 | + public void onContextCreated(TruffleContext context) { |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public void onLanguageContextCreate(TruffleContext context, LanguageInfo language) { |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public void onLanguageContextCreated(TruffleContext context, LanguageInfo language) { |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void onLanguageContextInitialize(TruffleContext context, LanguageInfo language) { |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public void onLanguageContextInitialized(TruffleContext context, LanguageInfo language) { |
| 103 | + if (!"python".equals(language.getId())) { |
| 104 | + return; |
| 105 | + } |
| 106 | + boolean[] seen = parsed.get(context); |
| 107 | + if (seen[0]) { |
| 108 | + return; |
| 109 | + } |
| 110 | + seen[0] = true; |
| 111 | + try { |
| 112 | + env.parse(source, "x").call(41); |
| 113 | + } catch (IOException e) { |
| 114 | + throw new AssertionError(e); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public void onLanguageContextFinalized(TruffleContext context, LanguageInfo language) { |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public void onLanguageContextDisposed(TruffleContext context, LanguageInfo language) { |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public void onContextClosed(TruffleContext context) { |
| 128 | + } |
| 129 | + }, true); |
| 130 | + return null; |
| 131 | + }; |
| 132 | + env.registerService(registerSource); |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments