comparison graal/com.oracle.truffle.api/src/com/oracle/truffle/api/impl/Accessor.java @ 21468:99942eac9c6d

Introducing TruffleVM - a central place to invoke code in any registered TruffleLanguage.
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Fri, 22 May 2015 13:41:10 +0200
parents
children bb51b9a142b3
comparison
equal deleted inserted replaced
21467:d4db9d812c8d 21468:99942eac9c6d
1 /*
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package com.oracle.truffle.api.impl;
26
27 import com.oracle.truffle.api.TruffleLanguage;
28 import com.oracle.truffle.api.TruffleLanguage.Env;
29 import com.oracle.truffle.api.vm.TruffleVM;
30 import com.oracle.truffle.api.source.Source;
31 import java.io.IOException;
32 import java.util.ServiceLoader;
33
34 /**
35 * Communication between TruffleVM and TruffleLanguage API/SPI.
36 */
37 public abstract class Accessor {
38 private static Accessor API;
39 private static Accessor SPI;
40 static {
41 TruffleLanguage lng = new TruffleLanguage() {
42 @Override
43 protected Object eval(Source code) throws IOException {
44 return null;
45 }
46
47 @Override
48 protected Object findExportedSymbol(String globalName) {
49 return null;
50 }
51
52 @Override
53 protected Object getLanguageGlobal() {
54 return null;
55 }
56
57 @Override
58 protected boolean isObjectOfLanguage(Object object) {
59 return false;
60 }
61 };
62 lng.hashCode();
63 }
64
65 protected Accessor() {
66 if (this.getClass().getSimpleName().endsWith("API")) {
67 if (API != null) {
68 throw new IllegalStateException();
69 }
70 API = this;
71 } else {
72 if (SPI != null) {
73 throw new IllegalStateException();
74 }
75 SPI = this;
76 }
77 }
78
79 protected Env attachEnv(TruffleVM vm, TruffleLanguage l) {
80 return API.attachEnv(vm, l);
81 }
82
83 protected Object eval(TruffleLanguage l, Source s) throws IOException {
84 return API.eval(l, s);
85 }
86
87 protected Object importSymbol(TruffleVM vm, TruffleLanguage queryingLang, String globalName) {
88 return SPI.importSymbol(vm, queryingLang, globalName);
89 }
90
91 protected Object findExportedSymbol(TruffleLanguage l, String globalName) {
92 return API.findExportedSymbol(l, globalName);
93 }
94
95 protected Object languageGlobal(TruffleLanguage l) {
96 return API.languageGlobal(l);
97 }
98
99 protected Object invoke(Object obj, Object[] args) throws IOException {
100 for (SymbolInvoker si : ServiceLoader.load(SymbolInvoker.class)) {
101 return si.invoke(obj, args);
102 }
103 throw new IOException("No symbol invoker found!");
104 }
105
106 }