src/jdk/nashorn/api/scripting/ScriptObjectMirror.java
Print this page
rev 737 : 8029364: NashornException to expose thrown object
Reviewed-by: lagergren, jlaskey
rev 752 : 8011964: need indexed access to externally-managed ByteBuffer
Reviewed-by: lagergren, hannesw
rev 760 : 8037400: Remove getInitialMap getters and GlobalObject interface
Reviewed-by: lagergren, jlaskey, attila
*** 23,32 ****
--- 23,33 ----
* questions.
*/
package jdk.nashorn.api.scripting;
+ import java.nio.ByteBuffer;
import java.security.AccessControlContext;
import java.security.AccessController;
import java.security.Permissions;
import java.security.PrivilegedAction;
import java.security.ProtectionDomain;
*** 39,51 ****
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import javax.script.Bindings;
import jdk.nashorn.internal.runtime.ConsString;
import jdk.nashorn.internal.runtime.Context;
- import jdk.nashorn.internal.runtime.GlobalObject;
import jdk.nashorn.internal.runtime.JSType;
import jdk.nashorn.internal.runtime.ScriptFunction;
import jdk.nashorn.internal.runtime.ScriptObject;
import jdk.nashorn.internal.runtime.ScriptRuntime;
--- 40,53 ----
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import javax.script.Bindings;
+ import jdk.nashorn.internal.objects.Global;
+ import jdk.nashorn.internal.runtime.arrays.ArrayData;
import jdk.nashorn.internal.runtime.ConsString;
import jdk.nashorn.internal.runtime.Context;
import jdk.nashorn.internal.runtime.JSType;
import jdk.nashorn.internal.runtime.ScriptFunction;
import jdk.nashorn.internal.runtime.ScriptObject;
import jdk.nashorn.internal.runtime.ScriptRuntime;
*** 60,70 ****
}
private static final AccessControlContext GET_CONTEXT_ACC_CTXT = getContextAccCtxt();
private final ScriptObject sobj;
! private final ScriptObject global;
private final boolean strict;
@Override
public boolean equals(final Object other) {
if (other instanceof ScriptObjectMirror) {
--- 62,72 ----
}
private static final AccessControlContext GET_CONTEXT_ACC_CTXT = getContextAccCtxt();
private final ScriptObject sobj;
! private final Global global;
private final boolean strict;
@Override
public boolean equals(final Object other) {
if (other instanceof ScriptObjectMirror) {
*** 91,101 ****
// JSObject methods
@Override
public Object call(final Object thiz, final Object... args) {
! final ScriptObject oldGlobal = Context.getGlobal();
final boolean globalChanged = (oldGlobal != global);
try {
if (globalChanged) {
Context.setGlobal(global);
--- 93,103 ----
// JSObject methods
@Override
public Object call(final Object thiz, final Object... args) {
! final Global oldGlobal = Context.getGlobal();
final boolean globalChanged = (oldGlobal != global);
try {
if (globalChanged) {
Context.setGlobal(global);
*** 106,115 ****
--- 108,119 ----
final Object self = globalChanged? wrap(thiz, oldGlobal) : thiz;
return wrap(ScriptRuntime.apply((ScriptFunction)sobj, unwrap(self, global), unwrapArray(modArgs, global)), global);
}
throw new RuntimeException("not a function: " + toString());
+ } catch (final NashornException ne) {
+ throw ne.initEcmaError(global);
} catch (final RuntimeException | Error e) {
throw e;
} catch (final Throwable t) {
throw new RuntimeException(t);
} finally {
*** 119,129 ****
}
}
@Override
public Object newObject(final Object... args) {
! final ScriptObject oldGlobal = Context.getGlobal();
final boolean globalChanged = (oldGlobal != global);
try {
if (globalChanged) {
Context.setGlobal(global);
--- 123,133 ----
}
}
@Override
public Object newObject(final Object... args) {
! final Global oldGlobal = Context.getGlobal();
final boolean globalChanged = (oldGlobal != global);
try {
if (globalChanged) {
Context.setGlobal(global);
*** 133,142 ****
--- 137,148 ----
final Object[] modArgs = globalChanged? wrapArray(args, oldGlobal) : args;
return wrap(ScriptRuntime.construct((ScriptFunction)sobj, unwrapArray(modArgs, global)), global);
}
throw new RuntimeException("not a constructor: " + toString());
+ } catch (final NashornException ne) {
+ throw ne.initEcmaError(global);
} catch (final RuntimeException | Error e) {
throw e;
} catch (final Throwable t) {
throw new RuntimeException(t);
} finally {
*** 163,173 ****
});
}
public Object callMember(final String functionName, final Object... args) {
functionName.getClass(); // null check
! final ScriptObject oldGlobal = Context.getGlobal();
final boolean globalChanged = (oldGlobal != global);
try {
if (globalChanged) {
Context.setGlobal(global);
--- 169,179 ----
});
}
public Object callMember(final String functionName, final Object... args) {
functionName.getClass(); // null check
! final Global oldGlobal = Context.getGlobal();
final boolean globalChanged = (oldGlobal != global);
try {
if (globalChanged) {
Context.setGlobal(global);
*** 180,189 ****
--- 186,197 ----
} else if (val instanceof JSObject && ((JSObject)val).isFunction()) {
return ((JSObject)val).call(sobj, args);
}
throw new NoSuchMethodException("No such function " + functionName);
+ } catch (final NashornException ne) {
+ throw ne.initEcmaError(global);
} catch (final RuntimeException | Error e) {
throw e;
} catch (final Throwable t) {
throw new RuntimeException(t);
} finally {
*** 251,260 ****
--- 259,284 ----
return null;
}
});
}
+ /**
+ * Nashorn extension: setIndexedPropertiesToExternalArrayData.
+ * set indexed properties be exposed from a given nio ByteBuffer.
+ *
+ * @param buf external buffer - should be a nio ByteBuffer
+ */
+ public void setIndexedPropertiesToExternalArrayData(final ByteBuffer buf) {
+ inGlobal(new Callable<Void>() {
+ @Override public Void call() {
+ sobj.setArray(ArrayData.allocate(buf));
+ return null;
+ }
+ });
+ }
+
+
@Override
public boolean isInstance(final Object obj) {
if (! (obj instanceof ScriptObjectMirror)) {
return false;
}
*** 616,626 ****
* @param homeGlobal global to which this object belongs. Not used for ConsStrings.
* @return wrapped/converted object
*/
public static Object wrap(final Object obj, final Object homeGlobal) {
if(obj instanceof ScriptObject) {
! return homeGlobal instanceof ScriptObject ? new ScriptObjectMirror((ScriptObject)obj, (ScriptObject)homeGlobal) : obj;
}
if(obj instanceof ConsString) {
return obj.toString();
}
return obj;
--- 640,650 ----
* @param homeGlobal global to which this object belongs. Not used for ConsStrings.
* @return wrapped/converted object
*/
public static Object wrap(final Object obj, final Object homeGlobal) {
if(obj instanceof ScriptObject) {
! return homeGlobal instanceof Global ? new ScriptObjectMirror((ScriptObject)obj, (Global)homeGlobal) : obj;
}
if(obj instanceof ConsString) {
return obj.toString();
}
return obj;
*** 684,724 ****
return newArgs;
}
// package-privates below this.
! ScriptObjectMirror(final ScriptObject sobj, final ScriptObject global) {
assert sobj != null : "ScriptObjectMirror on null!";
! assert global instanceof GlobalObject : "global is not a GlobalObject";
this.sobj = sobj;
this.global = global;
! this.strict = ((GlobalObject)global).isStrictContext();
}
// accessors for script engine
ScriptObject getScriptObject() {
return sobj;
}
! ScriptObject getHomeGlobal() {
return global;
}
static Object translateUndefined(Object obj) {
return (obj == ScriptRuntime.UNDEFINED)? null : obj;
}
// internals only below this.
private <V> V inGlobal(final Callable<V> callable) {
! final ScriptObject oldGlobal = Context.getGlobal();
final boolean globalChanged = (oldGlobal != global);
if (globalChanged) {
Context.setGlobal(global);
}
try {
return callable.call();
} catch (final RuntimeException e) {
throw e;
} catch (final Exception e) {
throw new AssertionError("Cannot happen", e);
} finally {
--- 708,750 ----
return newArgs;
}
// package-privates below this.
! ScriptObjectMirror(final ScriptObject sobj, final Global global) {
assert sobj != null : "ScriptObjectMirror on null!";
! assert global != null : "home Global is null";
this.sobj = sobj;
this.global = global;
! this.strict = global.isStrictContext();
}
// accessors for script engine
ScriptObject getScriptObject() {
return sobj;
}
! Global getHomeGlobal() {
return global;
}
static Object translateUndefined(Object obj) {
return (obj == ScriptRuntime.UNDEFINED)? null : obj;
}
// internals only below this.
private <V> V inGlobal(final Callable<V> callable) {
! final Global oldGlobal = Context.getGlobal();
final boolean globalChanged = (oldGlobal != global);
if (globalChanged) {
Context.setGlobal(global);
}
try {
return callable.call();
+ } catch (final NashornException ne) {
+ throw ne.initEcmaError(global);
} catch (final RuntimeException e) {
throw e;
} catch (final Exception e) {
throw new AssertionError("Cannot happen", e);
} finally {