comparison truffle/com.oracle.truffle.api.vm/src/com/oracle/truffle/api/vm/TruffleVM.java @ 22141:46384e637592

Make sure the proper TruffleVM execution context is re-set before invoking an operation on a JavaInterop wrapper obtained via Symbol.as method.
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Mon, 14 Sep 2015 11:02:52 +0200
parents 1957c49a979d
children dc83cc1f94f2
comparison
equal deleted inserted replaced
22140:92906003d607 22141:46384e637592
32 import com.oracle.truffle.api.instrument.*; 32 import com.oracle.truffle.api.instrument.*;
33 import com.oracle.truffle.api.interop.TruffleObject; 33 import com.oracle.truffle.api.interop.TruffleObject;
34 import com.oracle.truffle.api.interop.java.JavaInterop; 34 import com.oracle.truffle.api.interop.java.JavaInterop;
35 import com.oracle.truffle.api.source.*; 35 import com.oracle.truffle.api.source.*;
36 import java.io.*; 36 import java.io.*;
37 import java.lang.reflect.InvocationHandler;
38 import java.lang.reflect.Method;
37 import java.net.*; 39 import java.net.*;
38 import java.nio.file.*; 40 import java.nio.file.*;
39 import java.util.*; 41 import java.util.*;
40 import java.util.concurrent.CountDownLatch; 42 import java.util.concurrent.CountDownLatch;
41 import java.util.concurrent.Executor; 43 import java.util.concurrent.Executor;
584 public <T> T as(Class<T> representation) throws IOException { 586 public <T> T as(Class<T> representation) throws IOException {
585 Object obj = get(); 587 Object obj = get();
586 if (representation.isInstance(obj)) { 588 if (representation.isInstance(obj)) {
587 return representation.cast(obj); 589 return representation.cast(obj);
588 } 590 }
589 return JavaInterop.asJavaObject(representation, (TruffleObject) obj); 591 T wrapper = JavaInterop.asJavaObject(representation, (TruffleObject) obj);
592 return JavaWrapper.create(representation, wrapper, this);
590 } 593 }
591 594
592 /** 595 /**
593 * Invokes the symbol. If the symbol represents a function, then it should be invoked with 596 * Invokes the symbol. If the symbol represents a function, then it should be invoked with
594 * provided arguments. If the symbol represents a field, then first argument (if provided) 597 * provided arguments. If the symbol represents a field, then first argument (if provided)
609 final Object[] res = {null, null}; 612 final Object[] res = {null, null};
610 executor.execute(new Runnable() { 613 executor.execute(new Runnable() {
611 @Override 614 @Override
612 public void run() { 615 public void run() {
613 invokeImpl(fillIn, thiz, args, res, done); 616 invokeImpl(fillIn, thiz, args, res, done);
617 }
618 });
619 exceptionCheck(res);
620 return new Symbol(language, res, done);
621 }
622
623 @SuppressWarnings("try")
624 final Symbol invokeProxy(final InvocationHandler chain, final Object wrapper, final Method method, final Object[] args) throws IOException {
625 final Debugger[] fillIn = {debugger};
626 final CountDownLatch done = new CountDownLatch(1);
627 final Object[] res = {null, null};
628 executor.execute(new Runnable() {
629 @Override
630 public void run() {
631 try (final Closeable c = SPI.executionStart(TruffleVM.this, fillIn, null)) {
632 if (debugger == null) {
633 debugger = fillIn[0];
634 }
635 res[0] = chain.invoke(wrapper, method, args);
636 } catch (IOException ex) {
637 res[1] = ex;
638 } catch (Throwable ex) {
639 res[1] = ex;
640 } finally {
641 done.countDown();
642 }
614 } 643 }
615 }); 644 });
616 exceptionCheck(res); 645 exceptionCheck(res);
617 return new Symbol(language, res, done); 646 return new Symbol(language, res, done);
618 } 647 }