91 92 // JSObject methods 93 94 @Override 95 public Object call(final Object thiz, final Object... args) { 96 final ScriptObject oldGlobal = Context.getGlobal(); 97 final boolean globalChanged = (oldGlobal != global); 98 99 try { 100 if (globalChanged) { 101 Context.setGlobal(global); 102 } 103 104 if (sobj instanceof ScriptFunction) { 105 final Object[] modArgs = globalChanged? wrapArray(args, oldGlobal) : args; 106 final Object self = globalChanged? wrap(thiz, oldGlobal) : thiz; 107 return wrap(ScriptRuntime.apply((ScriptFunction)sobj, unwrap(self, global), unwrapArray(modArgs, global)), global); 108 } 109 110 throw new RuntimeException("not a function: " + toString()); 111 } catch (final RuntimeException | Error e) { 112 throw e; 113 } catch (final Throwable t) { 114 throw new RuntimeException(t); 115 } finally { 116 if (globalChanged) { 117 Context.setGlobal(oldGlobal); 118 } 119 } 120 } 121 122 @Override 123 public Object newObject(final Object... args) { 124 final ScriptObject oldGlobal = Context.getGlobal(); 125 final boolean globalChanged = (oldGlobal != global); 126 127 try { 128 if (globalChanged) { 129 Context.setGlobal(global); 130 } 131 132 if (sobj instanceof ScriptFunction) { 133 final Object[] modArgs = globalChanged? wrapArray(args, oldGlobal) : args; 134 return wrap(ScriptRuntime.construct((ScriptFunction)sobj, unwrapArray(modArgs, global)), global); 135 } 136 137 throw new RuntimeException("not a constructor: " + toString()); 138 } catch (final RuntimeException | Error e) { 139 throw e; 140 } catch (final Throwable t) { 141 throw new RuntimeException(t); 142 } finally { 143 if (globalChanged) { 144 Context.setGlobal(oldGlobal); 145 } 146 } 147 } 148 149 @Override 150 public Object eval(final String s) { 151 return inGlobal(new Callable<Object>() { 152 @Override 153 public Object call() { 154 final Context context = AccessController.doPrivileged( 155 new PrivilegedAction<Context>() { 156 @Override 157 public Context run() { 165 166 public Object callMember(final String functionName, final Object... args) { 167 functionName.getClass(); // null check 168 final ScriptObject oldGlobal = Context.getGlobal(); 169 final boolean globalChanged = (oldGlobal != global); 170 171 try { 172 if (globalChanged) { 173 Context.setGlobal(global); 174 } 175 176 final Object val = sobj.get(functionName); 177 if (val instanceof ScriptFunction) { 178 final Object[] modArgs = globalChanged? wrapArray(args, oldGlobal) : args; 179 return wrap(ScriptRuntime.apply((ScriptFunction)val, sobj, unwrapArray(modArgs, global)), global); 180 } else if (val instanceof JSObject && ((JSObject)val).isFunction()) { 181 return ((JSObject)val).call(sobj, args); 182 } 183 184 throw new NoSuchMethodException("No such function " + functionName); 185 } catch (final RuntimeException | Error e) { 186 throw e; 187 } catch (final Throwable t) { 188 throw new RuntimeException(t); 189 } finally { 190 if (globalChanged) { 191 Context.setGlobal(oldGlobal); 192 } 193 } 194 } 195 196 @Override 197 public Object getMember(final String name) { 198 name.getClass(); 199 return inGlobal(new Callable<Object>() { 200 @Override public Object call() { 201 return wrap(sobj.get(name), global); 202 } 203 }); 204 } 700 return sobj; 701 } 702 703 ScriptObject getHomeGlobal() { 704 return global; 705 } 706 707 static Object translateUndefined(Object obj) { 708 return (obj == ScriptRuntime.UNDEFINED)? null : obj; 709 } 710 711 // internals only below this. 712 private <V> V inGlobal(final Callable<V> callable) { 713 final ScriptObject oldGlobal = Context.getGlobal(); 714 final boolean globalChanged = (oldGlobal != global); 715 if (globalChanged) { 716 Context.setGlobal(global); 717 } 718 try { 719 return callable.call(); 720 } catch (final RuntimeException e) { 721 throw e; 722 } catch (final Exception e) { 723 throw new AssertionError("Cannot happen", e); 724 } finally { 725 if (globalChanged) { 726 Context.setGlobal(oldGlobal); 727 } 728 } 729 } 730 731 @Override 732 public double toNumber() { 733 return inGlobal(new Callable<Double>() { 734 @Override public Double call() { 735 return JSType.toNumber(sobj); 736 } 737 }); 738 } 739 } | 91 92 // JSObject methods 93 94 @Override 95 public Object call(final Object thiz, final Object... args) { 96 final ScriptObject oldGlobal = Context.getGlobal(); 97 final boolean globalChanged = (oldGlobal != global); 98 99 try { 100 if (globalChanged) { 101 Context.setGlobal(global); 102 } 103 104 if (sobj instanceof ScriptFunction) { 105 final Object[] modArgs = globalChanged? wrapArray(args, oldGlobal) : args; 106 final Object self = globalChanged? wrap(thiz, oldGlobal) : thiz; 107 return wrap(ScriptRuntime.apply((ScriptFunction)sobj, unwrap(self, global), unwrapArray(modArgs, global)), global); 108 } 109 110 throw new RuntimeException("not a function: " + toString()); 111 } catch (final NashornException ne) { 112 throw ne.initEcmaError(global); 113 } catch (final RuntimeException | Error e) { 114 throw e; 115 } catch (final Throwable t) { 116 throw new RuntimeException(t); 117 } finally { 118 if (globalChanged) { 119 Context.setGlobal(oldGlobal); 120 } 121 } 122 } 123 124 @Override 125 public Object newObject(final Object... args) { 126 final ScriptObject oldGlobal = Context.getGlobal(); 127 final boolean globalChanged = (oldGlobal != global); 128 129 try { 130 if (globalChanged) { 131 Context.setGlobal(global); 132 } 133 134 if (sobj instanceof ScriptFunction) { 135 final Object[] modArgs = globalChanged? wrapArray(args, oldGlobal) : args; 136 return wrap(ScriptRuntime.construct((ScriptFunction)sobj, unwrapArray(modArgs, global)), global); 137 } 138 139 throw new RuntimeException("not a constructor: " + toString()); 140 } catch (final NashornException ne) { 141 throw ne.initEcmaError(global); 142 } catch (final RuntimeException | Error e) { 143 throw e; 144 } catch (final Throwable t) { 145 throw new RuntimeException(t); 146 } finally { 147 if (globalChanged) { 148 Context.setGlobal(oldGlobal); 149 } 150 } 151 } 152 153 @Override 154 public Object eval(final String s) { 155 return inGlobal(new Callable<Object>() { 156 @Override 157 public Object call() { 158 final Context context = AccessController.doPrivileged( 159 new PrivilegedAction<Context>() { 160 @Override 161 public Context run() { 169 170 public Object callMember(final String functionName, final Object... args) { 171 functionName.getClass(); // null check 172 final ScriptObject oldGlobal = Context.getGlobal(); 173 final boolean globalChanged = (oldGlobal != global); 174 175 try { 176 if (globalChanged) { 177 Context.setGlobal(global); 178 } 179 180 final Object val = sobj.get(functionName); 181 if (val instanceof ScriptFunction) { 182 final Object[] modArgs = globalChanged? wrapArray(args, oldGlobal) : args; 183 return wrap(ScriptRuntime.apply((ScriptFunction)val, sobj, unwrapArray(modArgs, global)), global); 184 } else if (val instanceof JSObject && ((JSObject)val).isFunction()) { 185 return ((JSObject)val).call(sobj, args); 186 } 187 188 throw new NoSuchMethodException("No such function " + functionName); 189 } catch (final NashornException ne) { 190 throw ne.initEcmaError(global); 191 } catch (final RuntimeException | Error e) { 192 throw e; 193 } catch (final Throwable t) { 194 throw new RuntimeException(t); 195 } finally { 196 if (globalChanged) { 197 Context.setGlobal(oldGlobal); 198 } 199 } 200 } 201 202 @Override 203 public Object getMember(final String name) { 204 name.getClass(); 205 return inGlobal(new Callable<Object>() { 206 @Override public Object call() { 207 return wrap(sobj.get(name), global); 208 } 209 }); 210 } 706 return sobj; 707 } 708 709 ScriptObject getHomeGlobal() { 710 return global; 711 } 712 713 static Object translateUndefined(Object obj) { 714 return (obj == ScriptRuntime.UNDEFINED)? null : obj; 715 } 716 717 // internals only below this. 718 private <V> V inGlobal(final Callable<V> callable) { 719 final ScriptObject oldGlobal = Context.getGlobal(); 720 final boolean globalChanged = (oldGlobal != global); 721 if (globalChanged) { 722 Context.setGlobal(global); 723 } 724 try { 725 return callable.call(); 726 } catch (final NashornException ne) { 727 throw ne.initEcmaError(global); 728 } catch (final RuntimeException e) { 729 throw e; 730 } catch (final Exception e) { 731 throw new AssertionError("Cannot happen", e); 732 } finally { 733 if (globalChanged) { 734 Context.setGlobal(oldGlobal); 735 } 736 } 737 } 738 739 @Override 740 public double toNumber() { 741 return inGlobal(new Callable<Double>() { 742 @Override public Double call() { 743 return JSType.toNumber(sobj); 744 } 745 }); 746 } 747 } |