package com.oracle.truffle.js.runtime.builtins;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.Cached.Shared;
import com.oracle.truffle.api.dsl.CachedContext;
import com.oracle.truffle.api.dsl.CachedLanguage;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.library.ExportLibrary;
import com.oracle.truffle.api.library.ExportMessage;
import com.oracle.truffle.api.object.DynamicObject;
import com.oracle.truffle.api.object.Shape;
import com.oracle.truffle.js.lang.JavaScriptLanguage;
import com.oracle.truffle.js.nodes.interop.ExportValueNode;
import com.oracle.truffle.js.nodes.interop.JSInteropExecuteNode;
import com.oracle.truffle.js.nodes.interop.JSInteropInstantiateNode;
import com.oracle.truffle.js.nodes.unary.IsCallableNode;
import com.oracle.truffle.js.runtime.JSRealm;
import com.oracle.truffle.js.runtime.JSRuntime;
import com.oracle.truffle.js.runtime.interop.JSMetaType;
import com.oracle.truffle.js.runtime.objects.JSClassObject;
import com.oracle.truffle.js.runtime.objects.Null;
import com.oracle.truffle.js.runtime.objects.Undefined;
@ExportLibrary(InteropLibrary.class)
public final class JSProxyObject extends JSClassObject {
private Object proxyTarget;
private DynamicObject proxyHandler;
protected JSProxyObject(Shape shape, Object proxyTarget, DynamicObject proxyHandler) {
super(shape);
this.proxyTarget = proxyTarget;
this.proxyHandler = proxyHandler;
}
public DynamicObject getProxyHandler() {
return proxyHandler;
}
public Object getProxyTarget() {
return proxyTarget;
}
public void revoke() {
this.proxyHandler = Null.instance;
this.proxyTarget = Null.instance;
}
public static DynamicObject create(JSRealm realm, JSObjectFactory factory, Object target, DynamicObject handler) {
return factory.initProto(new JSProxyObject(factory.getShape(realm), target, handler), realm);
}
@ExportMessage
public boolean isExecutable(
@Cached IsCallableNode isCallable) {
return isCallable.executeBoolean(this);
}
@ExportMessage
public Object execute(Object[] args,
@CachedLanguage JavaScriptLanguage language,
@CachedContext(JavaScriptLanguage.class) JSRealm realm,
@Cached JSInteropExecuteNode callNode,
@Shared("exportValue") @Cached ExportValueNode exportNode) throws UnsupportedMessageException {
language.interopBoundaryEnter(realm);
try {
Object result = callNode.execute(this, Undefined.instance, args);
return exportNode.execute(result);
} finally {
language.interopBoundaryExit(realm);
}
}
@ExportMessage
public boolean isInstantiable() {
return JSRuntime.isConstructor(this);
}
@ExportMessage
public Object instantiate(Object[] args,
@CachedLanguage JavaScriptLanguage language,
@CachedContext(JavaScriptLanguage.class) JSRealm realm,
@Cached JSInteropInstantiateNode callNode,
@Shared("exportValue") @Cached ExportValueNode exportNode) throws UnsupportedMessageException {
language.interopBoundaryEnter(realm);
try {
Object result = callNode.execute(this, args);
return exportNode.execute(result);
} finally {
language.interopBoundaryExit(realm);
}
}
@SuppressWarnings("static-method")
@ExportMessage
public boolean hasMetaObject() {
return true;
}
@SuppressWarnings("static-method")
@ExportMessage
public Object getMetaObject() {
return JSMetaType.JS_PROXY;
}
}