comparison graal/com.oracle.truffle.api/src/com/oracle/truffle/api/vm/TruffleVM.java @ 21716:2f9e4d984d16

Give languages a chance to do implicit exports. Prefer explicit exports over implicit ones.
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Thu, 04 Jun 2015 08:08:05 +0200
parents 1c76a5662753
children 45083be8a812
comparison
equal deleted inserted replaced
21715:67e28e817d32 21716:2f9e4d984d16
102 } 102 }
103 } catch (IOException ex) { 103 } catch (IOException ex) {
104 LOG.log(Level.CONFIG, "Cannot process " + u + " as language definition", ex); 104 LOG.log(Level.CONFIG, "Cannot process " + u + " as language definition", ex);
105 continue; 105 continue;
106 } 106 }
107 Language l = new Language(p); 107 for (int cnt = 1;; cnt++) {
108 for (String mimeType : l.getMimeTypes()) { 108 String prefix = "language" + cnt + ".";
109 langs.put(mimeType, l); 109 if (p.getProperty(prefix + "name") == null) {
110 break;
111 }
112 Language l = new Language(prefix, p);
113 for (String mimeType : l.getMimeTypes()) {
114 langs.put(mimeType, l);
115 }
110 } 116 }
111 } 117 }
112 } 118 }
113 119
114 static ClassLoader loader() { 120 static ClassLoader loader() {
325 checkThread(); 331 checkThread();
326 Object obj = null; 332 Object obj = null;
327 Object global = null; 333 Object global = null;
328 for (Language dl : langs.values()) { 334 for (Language dl : langs.values()) {
329 TruffleLanguage l = dl.getImpl(); 335 TruffleLanguage l = dl.getImpl();
330 obj = SPI.findExportedSymbol(l, globalName); 336 obj = SPI.findExportedSymbol(l, globalName, true);
331 if (obj != null) { 337 if (obj != null) {
332 global = SPI.languageGlobal(l); 338 global = SPI.languageGlobal(l);
333 break; 339 break;
340 }
341 }
342 if (obj == null) {
343 for (Language dl : langs.values()) {
344 TruffleLanguage l = dl.getImpl();
345 obj = SPI.findExportedSymbol(l, globalName, false);
346 if (obj != null) {
347 global = SPI.languageGlobal(l);
348 break;
349 }
334 } 350 }
335 } 351 }
336 return obj == null ? null : new Symbol(obj, global); 352 return obj == null ? null : new Symbol(obj, global);
337 } 353 }
338 354
397 * {@link TruffleVM#eval(java.lang.String, java.lang.String) a code is evaluated} in it. 413 * {@link TruffleVM#eval(java.lang.String, java.lang.String) a code is evaluated} in it.
398 */ 414 */
399 public final class Language { 415 public final class Language {
400 private final Properties props; 416 private final Properties props;
401 private TruffleLanguage impl; 417 private TruffleLanguage impl;
402 418 private final String prefix;
403 Language(Properties props) { 419
420 Language(String prefix, Properties props) {
421 this.prefix = prefix;
404 this.props = props; 422 this.props = props;
405 } 423 }
406 424
407 /** 425 /**
408 * MIME types recognized by the language. 426 * MIME types recognized by the language.
410 * @return returns immutable set of recognized MIME types 428 * @return returns immutable set of recognized MIME types
411 */ 429 */
412 public Set<String> getMimeTypes() { 430 public Set<String> getMimeTypes() {
413 TreeSet<String> ts = new TreeSet<>(); 431 TreeSet<String> ts = new TreeSet<>();
414 for (int i = 0;; i++) { 432 for (int i = 0;; i++) {
415 String mt = props.getProperty("mimeType." + i); 433 String mt = props.getProperty(prefix + "mimeType." + i);
416 if (mt == null) { 434 if (mt == null) {
417 break; 435 break;
418 } 436 }
419 ts.add(mt); 437 ts.add(mt);
420 } 438 }
425 * Human readable name of the language. Think of C, Ruby, JS, etc. 443 * Human readable name of the language. Think of C, Ruby, JS, etc.
426 * 444 *
427 * @return string giving the language a name 445 * @return string giving the language a name
428 */ 446 */
429 public String getName() { 447 public String getName() {
430 return props.getProperty("name"); 448 return props.getProperty(prefix + "name");
431 } 449 }
432 450
433 TruffleLanguage getImpl() { 451 TruffleLanguage getImpl() {
434 if (impl == null) { 452 if (impl == null) {
435 String n = props.getProperty("className"); 453 String n = props.getProperty(prefix + "className");
436 try { 454 try {
437 Class<?> langClazz = Class.forName(n, true, loader()); 455 Class<?> langClazz = Class.forName(n, true, loader());
438 Constructor<?> constructor = langClazz.getConstructor(Env.class); 456 Constructor<?> constructor = langClazz.getConstructor(Env.class);
439 impl = SPI.attachEnv(TruffleVM.this, constructor, out, err, in); 457 impl = SPI.attachEnv(TruffleVM.this, constructor, out, err, in);
440 } catch (Exception ex) { 458 } catch (Exception ex) {
457 for (Language dl : uniqueLang) { 475 for (Language dl : uniqueLang) {
458 TruffleLanguage l = dl.getImpl(); 476 TruffleLanguage l = dl.getImpl();
459 if (l == ownLang) { 477 if (l == ownLang) {
460 continue; 478 continue;
461 } 479 }
462 Object obj = SPI.findExportedSymbol(l, globalName); 480 Object obj = SPI.findExportedSymbol(l, globalName, true);
463 if (obj != null) { 481 if (obj != null) {
464 return obj; 482 return obj;
465 } 483 }
466 } 484 }
485 for (Language dl : uniqueLang) {
486 TruffleLanguage l = dl.getImpl();
487 if (l == ownLang) {
488 continue;
489 }
490 Object obj = SPI.findExportedSymbol(l, globalName, false);
491 if (obj != null) {
492 return obj;
493 }
494 }
467 return null; 495 return null;
468 } 496 }
469 497
470 @Override 498 @Override
471 public TruffleLanguage attachEnv(TruffleVM vm, Constructor<?> langClazz, Writer stdOut, Writer stdErr, Reader stdIn) { 499 public TruffleLanguage attachEnv(TruffleVM vm, Constructor<?> langClazz, Writer stdOut, Writer stdErr, Reader stdIn) {
476 public Object eval(TruffleLanguage l, Source s) throws IOException { 504 public Object eval(TruffleLanguage l, Source s) throws IOException {
477 return super.eval(l, s); 505 return super.eval(l, s);
478 } 506 }
479 507
480 @Override 508 @Override
481 public Object findExportedSymbol(TruffleLanguage l, String globalName) { 509 public Object findExportedSymbol(TruffleLanguage l, String globalName, boolean onlyExplicit) {
482 return super.findExportedSymbol(l, globalName); 510 return super.findExportedSymbol(l, globalName, onlyExplicit);
483 } 511 }
484 512
485 @Override 513 @Override
486 public Object languageGlobal(TruffleLanguage l) { 514 public Object languageGlobal(TruffleLanguage l) {
487 return super.languageGlobal(l); 515 return super.languageGlobal(l);