| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.sdk.internal.wip; | |
| 6 | |
| 7 import static org.chromium.sdk.util.BasicUtil.getSafe; | |
| 8 | |
| 9 import java.util.ArrayList; | |
| 10 import java.util.Collection; | |
| 11 import java.util.Collections; | |
| 12 import java.util.HashMap; | |
| 13 import java.util.HashSet; | |
| 14 import java.util.List; | |
| 15 import java.util.Map; | |
| 16 import java.util.Set; | |
| 17 import java.util.concurrent.atomic.AtomicReference; | |
| 18 import java.util.logging.Level; | |
| 19 import java.util.logging.Logger; | |
| 20 | |
| 21 import org.chromium.sdk.Breakpoint; | |
| 22 import org.chromium.sdk.CallFrame; | |
| 23 import org.chromium.sdk.DebugContext; | |
| 24 import org.chromium.sdk.DebugContext.StepAction; | |
| 25 import org.chromium.sdk.ExceptionData; | |
| 26 import org.chromium.sdk.JavascriptVm; | |
| 27 import org.chromium.sdk.JsEvaluateContext; | |
| 28 import org.chromium.sdk.JsObject; | |
| 29 import org.chromium.sdk.JsScope; | |
| 30 import org.chromium.sdk.JsValue; | |
| 31 import org.chromium.sdk.JsVariable; | |
| 32 import org.chromium.sdk.RelayOk; | |
| 33 import org.chromium.sdk.RemoteValueMapping; | |
| 34 import org.chromium.sdk.SyncCallback; | |
| 35 import org.chromium.sdk.TextStreamPosition; | |
| 36 import org.chromium.sdk.internal.protocolparser.JsonProtocolParseException; | |
| 37 import org.chromium.sdk.internal.wip.WipExpressionBuilder.ValueNameBuilder; | |
| 38 import org.chromium.sdk.internal.wip.WipValueLoader.Getter; | |
| 39 import org.chromium.sdk.internal.wip.protocol.WipParserAccess; | |
| 40 import org.chromium.sdk.internal.wip.protocol.input.WipCommandResponse; | |
| 41 import org.chromium.sdk.internal.wip.protocol.input.debugger.CallFrameValue; | |
| 42 import org.chromium.sdk.internal.wip.protocol.input.debugger.EvaluateOnCallFrame
Data; | |
| 43 import org.chromium.sdk.internal.wip.protocol.input.debugger.PausedEventData; | |
| 44 import org.chromium.sdk.internal.wip.protocol.input.debugger.ResumedEventData; | |
| 45 import org.chromium.sdk.internal.wip.protocol.input.debugger.ScopeValue; | |
| 46 import org.chromium.sdk.internal.wip.protocol.input.runtime.EvaluateData; | |
| 47 import org.chromium.sdk.internal.wip.protocol.input.runtime.PropertyDescriptorVa
lue; | |
| 48 import org.chromium.sdk.internal.wip.protocol.input.runtime.RemoteObjectValue; | |
| 49 import org.chromium.sdk.internal.wip.protocol.output.WipParams; | |
| 50 import org.chromium.sdk.internal.wip.protocol.output.WipParamsWithResponse; | |
| 51 import org.chromium.sdk.internal.wip.protocol.output.debugger.EvaluateOnCallFram
eParams; | |
| 52 import org.chromium.sdk.internal.wip.protocol.output.debugger.ResumeParams; | |
| 53 import org.chromium.sdk.internal.wip.protocol.output.debugger.StepIntoParams; | |
| 54 import org.chromium.sdk.internal.wip.protocol.output.debugger.StepOutParams; | |
| 55 import org.chromium.sdk.internal.wip.protocol.output.debugger.StepOverParams; | |
| 56 import org.chromium.sdk.internal.wip.protocol.output.runtime.EvaluateParams; | |
| 57 import org.chromium.sdk.util.AsyncFutureRef; | |
| 58 import org.chromium.sdk.util.GenericCallback; | |
| 59 import org.chromium.sdk.util.LazyConstructable; | |
| 60 import org.chromium.sdk.util.MethodIsBlockingException; | |
| 61 import org.chromium.sdk.util.RelaySyncCallback; | |
| 62 import org.json.simple.JSONObject; | |
| 63 | |
| 64 /** | |
| 65 * Builder for {@link DebugContext} that works with Wip protocol. | |
| 66 */ | |
| 67 class WipContextBuilder { | |
| 68 private static final Logger LOGGER = Logger.getLogger(WipContextBuilder.class.
getName()); | |
| 69 | |
| 70 private final WipTabImpl tabImpl; | |
| 71 private final EvaluateHack evaluateHack; | |
| 72 private WipDebugContextImpl currentContext = null; | |
| 73 | |
| 74 WipContextBuilder(WipTabImpl tabImpl) { | |
| 75 this.tabImpl = tabImpl; | |
| 76 this.evaluateHack = new EvaluateHack(tabImpl); | |
| 77 } | |
| 78 | |
| 79 // Called from Dispatch Thread. | |
| 80 RelayOk updateStackTrace(List<CallFrameValue> callFrames, | |
| 81 GenericCallback<Void> callback, final SyncCallback syncCallback) { | |
| 82 if (currentContext == null) { | |
| 83 if (callback != null) { | |
| 84 callback.success(null); | |
| 85 } | |
| 86 return RelaySyncCallback.finish(syncCallback); | |
| 87 } else { | |
| 88 return currentContext.setFrames(callFrames, callback, syncCallback); | |
| 89 } | |
| 90 } | |
| 91 | |
| 92 void createContext(PausedEventData data) { | |
| 93 if (currentContext != null) { | |
| 94 LOGGER.severe("Context is already created"); | |
| 95 currentContext = null; | |
| 96 } | |
| 97 | |
| 98 final WipDebugContextImpl context = new WipDebugContextImpl(data); | |
| 99 currentContext = context; | |
| 100 | |
| 101 GenericCallback<Void> callback = new GenericCallback<Void>() { | |
| 102 @Override | |
| 103 public void success(Void value) { | |
| 104 tabImpl.getDebugListener().getDebugEventListener().suspended(context); | |
| 105 } | |
| 106 | |
| 107 @Override | |
| 108 public void failure(Exception exception) { | |
| 109 throw new RuntimeException(exception); | |
| 110 } | |
| 111 }; | |
| 112 | |
| 113 context.setFrames(data.callFrames(), callback, null); | |
| 114 } | |
| 115 | |
| 116 EvaluateHack getEvaluateHack() { | |
| 117 return evaluateHack; | |
| 118 } | |
| 119 | |
| 120 void onResumeReportedFromRemote(ResumedEventData event) { | |
| 121 if (currentContext == null) { | |
| 122 throw new IllegalStateException(); | |
| 123 } | |
| 124 WipDebugContextImpl context = currentContext; | |
| 125 currentContext = null; | |
| 126 this.tabImpl.getDebugListener().getDebugEventListener().resumed(); | |
| 127 context.reportClosed(); | |
| 128 } | |
| 129 | |
| 130 class WipDebugContextImpl implements DebugContext { | |
| 131 private volatile List<CallFrameImpl> frames = null; | |
| 132 private final ExceptionData exceptionData; | |
| 133 private final AtomicReference<CloseRequest> closeRequest = | |
| 134 new AtomicReference<CloseRequest>(null); | |
| 135 private final JsEvaluateContext globalContext; | |
| 136 | |
| 137 public WipDebugContextImpl(PausedEventData data) { | |
| 138 PausedEventData.Data additionalData = data.data(); | |
| 139 if (data.reason() == PausedEventData.Reason.EXCEPTION && additionalData !=
null) { | |
| 140 RemoteObjectValue exceptionRemoteObject; | |
| 141 try { | |
| 142 JSONObject additionalDataObject = additionalData.getUnderlyingObject()
; | |
| 143 exceptionRemoteObject = | |
| 144 WipParserAccess.get().parseRemoteObjectValue(additionalDataObject)
; | |
| 145 } catch (JsonProtocolParseException e) { | |
| 146 throw new RuntimeException("Failed to parse exception data", e); | |
| 147 } | |
| 148 JsValue exceptionValue = | |
| 149 valueLoader.getValueBuilder().wrap(exceptionRemoteObject, null); | |
| 150 exceptionData = new ExceptionDataImpl(exceptionValue); | |
| 151 } else { | |
| 152 exceptionData = null; | |
| 153 } | |
| 154 globalContext = new GlobalEvaluateContext(getValueLoader()); | |
| 155 } | |
| 156 | |
| 157 RelayOk setFrames(List<CallFrameValue> frameDataList, | |
| 158 final GenericCallback<Void> callback, final SyncCallback syncCallback) { | |
| 159 frames = new ArrayList<CallFrameImpl>(frameDataList.size()); | |
| 160 for (CallFrameValue frameData : frameDataList) { | |
| 161 frames.add(new CallFrameImpl(frameData)); | |
| 162 } | |
| 163 | |
| 164 return tabImpl.getScriptManager().loadScriptSourcesAsync(getScriptIds(), | |
| 165 new WipScriptManager.ScriptSourceLoadCallback() { | |
| 166 @Override | |
| 167 public void done(Map<String, WipScriptImpl> loadedScripts) { | |
| 168 setScripts(loadedScripts); | |
| 169 | |
| 170 if (callback != null) { | |
| 171 callback.success(null); | |
| 172 } | |
| 173 } | |
| 174 }, | |
| 175 syncCallback); | |
| 176 } | |
| 177 | |
| 178 void resetFrames(List<CallFrameValue> frameDataList) { | |
| 179 List<CallFrameImpl> newFrames = new ArrayList<CallFrameImpl>(frameDataList
.size()); | |
| 180 for (int i = 0; i < frameDataList.size(); i++) { | |
| 181 CallFrameImpl callFrameImpl = new CallFrameImpl(frameDataList.get(i)); | |
| 182 callFrameImpl.setScript(frames.get(i).getScript()); | |
| 183 newFrames.add(callFrameImpl); | |
| 184 } | |
| 185 frames = newFrames; | |
| 186 } | |
| 187 | |
| 188 WipValueLoader getValueLoader() { | |
| 189 return valueLoader; | |
| 190 } | |
| 191 | |
| 192 void reportClosed() { | |
| 193 CloseRequest request = this.closeRequest.get(); | |
| 194 if (request != null && request.callback != null) { | |
| 195 request.callback.success(); | |
| 196 } | |
| 197 } | |
| 198 | |
| 199 Set<String> getScriptIds() { | |
| 200 Set<String> scriptIds = new HashSet<String>(); | |
| 201 for (CallFrameImpl frame : frames) { | |
| 202 String sourceId = frame.getSourceId(); | |
| 203 if (sourceId != null) { | |
| 204 scriptIds.add(sourceId); | |
| 205 } | |
| 206 } | |
| 207 return scriptIds; | |
| 208 } | |
| 209 | |
| 210 void setScripts(Map<String, WipScriptImpl> loadedScripts) { | |
| 211 for (CallFrameImpl frame : frames) { | |
| 212 String sourceId = frame.getSourceId(); | |
| 213 if (sourceId != null) { | |
| 214 WipScriptImpl script = getSafe(loadedScripts, sourceId); | |
| 215 // Script can be null. | |
| 216 frame.setScript(script); | |
| 217 } | |
| 218 } | |
| 219 } | |
| 220 | |
| 221 @Override | |
| 222 public State getState() { | |
| 223 if (exceptionData == null) { | |
| 224 return State.NORMAL; | |
| 225 } else { | |
| 226 return State.EXCEPTION; | |
| 227 } | |
| 228 } | |
| 229 | |
| 230 @Override | |
| 231 public ExceptionData getExceptionData() { | |
| 232 return exceptionData; | |
| 233 } | |
| 234 | |
| 235 @Override | |
| 236 public Collection<? extends Breakpoint> getBreakpointsHit() { | |
| 237 if (frames.isEmpty()) { | |
| 238 return Collections.emptyList(); | |
| 239 } | |
| 240 CallFrameImpl topFrame = frames.get(0); | |
| 241 return tabImpl.getBreakpointManager().findRelatedBreakpoints(topFrame); | |
| 242 } | |
| 243 | |
| 244 @Override | |
| 245 public List<? extends CallFrame> getCallFrames() { | |
| 246 return frames; | |
| 247 } | |
| 248 | |
| 249 @Override | |
| 250 public void continueVm(StepAction stepAction, int stepCount, | |
| 251 ContinueCallback callback) { | |
| 252 continueVm(stepAction, stepCount, callback, null); | |
| 253 } | |
| 254 | |
| 255 @Override | |
| 256 public RelayOk continueVm(StepAction stepAction, int stepCount, | |
| 257 final ContinueCallback callback, SyncCallback syncCallback) { | |
| 258 { | |
| 259 boolean updated = closeRequest.compareAndSet(null, new CloseRequest(call
back)); | |
| 260 if (!updated) { | |
| 261 throw new IllegalStateException("Continue already requested"); | |
| 262 } | |
| 263 } | |
| 264 | |
| 265 WipParams params = sdkStepToProtocolStep(stepAction); | |
| 266 | |
| 267 WipCommandCallback commandCallback; | |
| 268 if (callback == null) { | |
| 269 commandCallback = null; | |
| 270 } else { | |
| 271 commandCallback = new WipCommandCallback() { | |
| 272 @Override public void messageReceived(WipCommandResponse response) { | |
| 273 callback.success(); | |
| 274 } | |
| 275 @Override public void failure(String message) { | |
| 276 callback.failure(message); | |
| 277 } | |
| 278 }; | |
| 279 } | |
| 280 return tabImpl.getCommandProcessor().send(params, commandCallback, syncCal
lback); | |
| 281 } | |
| 282 | |
| 283 @Override | |
| 284 public JsEvaluateContext getGlobalEvaluateContext() { | |
| 285 return globalContext; | |
| 286 } | |
| 287 | |
| 288 @Override | |
| 289 public RemoteValueMapping getDefaultRemoteValueMapping() { | |
| 290 return valueLoader; | |
| 291 } | |
| 292 | |
| 293 public WipTabImpl getTab() { | |
| 294 return tabImpl; | |
| 295 } | |
| 296 | |
| 297 public WipCommandProcessor getCommandProcessor() { | |
| 298 return tabImpl.getCommandProcessor(); | |
| 299 } | |
| 300 | |
| 301 private class CloseRequest { | |
| 302 final ContinueCallback callback; | |
| 303 | |
| 304 CloseRequest(ContinueCallback callback) { | |
| 305 this.callback = callback; | |
| 306 } | |
| 307 } | |
| 308 | |
| 309 class CallFrameImpl implements CallFrame { | |
| 310 private final String functionName; | |
| 311 private final String id; | |
| 312 private final LazyConstructable<List<JsScope>> scopeData; | |
| 313 private final JsVariable thisObject; | |
| 314 private final TextStreamPosition streamPosition; | |
| 315 private final String sourceId; | |
| 316 private WipScriptImpl scriptImpl; | |
| 317 | |
| 318 public CallFrameImpl(CallFrameValue frameData) { | |
| 319 functionName = frameData.functionName(); | |
| 320 id = frameData.callFrameId(); | |
| 321 sourceId = frameData.location().scriptId(); | |
| 322 final List<ScopeValue> scopeDataList = frameData.scopeChain(); | |
| 323 | |
| 324 scopeData = LazyConstructable.create(new LazyConstructable.Factory<List<
JsScope>>() { | |
| 325 @Override | |
| 326 public List<JsScope> construct() { | |
| 327 final List<JsScope> scopes = new ArrayList<JsScope>(scopeDataList.si
ze()); | |
| 328 | |
| 329 for (int i = 0; i < scopeDataList.size(); i++) { | |
| 330 ScopeValue scopeData = scopeDataList.get(i); | |
| 331 scopes.add(createScope(scopeData, valueLoader)); | |
| 332 } | |
| 333 return scopes; | |
| 334 } | |
| 335 }); | |
| 336 | |
| 337 RemoteObjectValue thisObjectData = frameData.getThis(); | |
| 338 if (thisObjectData == null) { | |
| 339 LOGGER.log(Level.SEVERE, "Missing local scope", new Exception()); | |
| 340 thisObject = null; | |
| 341 } else { | |
| 342 thisObject = createSimpleNameVariable("this", thisObjectData); | |
| 343 } | |
| 344 | |
| 345 // 0-based. | |
| 346 final int line = (int) frameData.location().lineNumber(); | |
| 347 | |
| 348 // 0-based. | |
| 349 // TODO: check documentation, whether it's 0-based | |
| 350 Long columnObject = frameData.location().columnNumber(); | |
| 351 final int column; | |
| 352 if (columnObject == null) { | |
| 353 column = 0; | |
| 354 } else { | |
| 355 column = columnObject.intValue(); | |
| 356 } | |
| 357 streamPosition = new TextStreamPosition() { | |
| 358 @Override public int getOffset() { | |
| 359 return WipBrowserImpl.throwUnsupported(); | |
| 360 } | |
| 361 @Override public int getLine() { | |
| 362 return line; | |
| 363 } | |
| 364 | |
| 365 @Override public int getColumn() { | |
| 366 return column; | |
| 367 } | |
| 368 }; | |
| 369 } | |
| 370 | |
| 371 String getSourceId() { | |
| 372 return sourceId; | |
| 373 } | |
| 374 | |
| 375 void setScript(WipScriptImpl scriptImpl) { | |
| 376 this.scriptImpl = scriptImpl; | |
| 377 } | |
| 378 | |
| 379 @Override | |
| 380 public List<? extends JsScope> getVariableScopes() { | |
| 381 return scopeData.get(); | |
| 382 } | |
| 383 | |
| 384 @Override | |
| 385 public JsVariable getReceiverVariable() { | |
| 386 return thisObject; | |
| 387 } | |
| 388 | |
| 389 @Override | |
| 390 public WipScriptImpl getScript() { | |
| 391 return scriptImpl; | |
| 392 } | |
| 393 | |
| 394 @Override | |
| 395 public TextStreamPosition getStatementStartPosition() { | |
| 396 return streamPosition; | |
| 397 } | |
| 398 | |
| 399 @Override | |
| 400 public String getFunctionName() { | |
| 401 return functionName; | |
| 402 } | |
| 403 | |
| 404 @Override | |
| 405 public JsEvaluateContext getEvaluateContext() { | |
| 406 return evaluateContext; | |
| 407 } | |
| 408 | |
| 409 private JsVariable createSimpleNameVariable(final String name, | |
| 410 RemoteObjectValue thisObjectData) { | |
| 411 ValueNameBuilder valueNameBuidler = WipExpressionBuilder.createRootName(
name, false); | |
| 412 return valueLoader.getValueBuilder().createVariable(thisObjectData, valu
eNameBuidler); | |
| 413 } | |
| 414 | |
| 415 private final WipEvaluateContextBase<?> evaluateContext = | |
| 416 new WipEvaluateContextBase<EvaluateOnCallFrameData>(getValueLoader())
{ | |
| 417 @Override | |
| 418 protected WipParamsWithResponse<EvaluateOnCallFrameData> createRequestPa
rams( | |
| 419 String expression, WipValueLoader destinationValueLoader) { | |
| 420 return new EvaluateOnCallFrameParams(id, expression, | |
| 421 destinationValueLoader.getObjectGroupId(), false, null, false); | |
| 422 } | |
| 423 | |
| 424 @Override protected RemoteObjectValue getRemoteObjectValue(EvaluateOnCal
lFrameData data) { | |
| 425 return data.result(); | |
| 426 } | |
| 427 | |
| 428 @Override protected Boolean getWasThrown(EvaluateOnCallFrameData data) { | |
| 429 return data.wasThrown(); | |
| 430 } | |
| 431 }; | |
| 432 } | |
| 433 | |
| 434 private class ExceptionDataImpl implements ExceptionData { | |
| 435 private final JsValue exceptionValue; | |
| 436 | |
| 437 ExceptionDataImpl(JsValue exceptionValue) { | |
| 438 this.exceptionValue = exceptionValue; | |
| 439 } | |
| 440 | |
| 441 @Override | |
| 442 public JsValue getExceptionValue() { | |
| 443 return exceptionValue; | |
| 444 } | |
| 445 | |
| 446 @Override | |
| 447 public boolean isUncaught() { | |
| 448 // TODO: implement. | |
| 449 return false; | |
| 450 } | |
| 451 | |
| 452 @Override | |
| 453 public String getSourceText() { | |
| 454 // Not supported. | |
| 455 return null; | |
| 456 } | |
| 457 | |
| 458 @Override | |
| 459 public String getExceptionMessage() { | |
| 460 // TODO: implement. | |
| 461 return exceptionValue.getValueString(); | |
| 462 } | |
| 463 } | |
| 464 | |
| 465 @Override | |
| 466 public JavascriptVm getJavascriptVm() { | |
| 467 return tabImpl; | |
| 468 } | |
| 469 | |
| 470 private final WipValueLoader valueLoader = new WipValueLoader(tabImpl) { | |
| 471 @Override | |
| 472 String getObjectGroupId() { | |
| 473 return null; | |
| 474 } | |
| 475 }; | |
| 476 } | |
| 477 | |
| 478 static JsScope createScope(ScopeValue scopeData, WipValueLoader valueLoader) { | |
| 479 JsScope.Type type = WIP_TO_SDK_SCOPE_TYPE.get(scopeData.type()); | |
| 480 if (type == null) { | |
| 481 type = JsScope.Type.UNKNOWN; | |
| 482 } | |
| 483 if (type == JsScope.Type.WITH || type == JsScope.Type.GLOBAL) { | |
| 484 return new ObjectScopeImpl(scopeData, type, valueLoader); | |
| 485 } else { | |
| 486 return new DeclarativeScopeImpl(scopeData, type, valueLoader); | |
| 487 } | |
| 488 } | |
| 489 | |
| 490 private static class DeclarativeScopeImpl implements JsScope.Declarative { | |
| 491 private final AsyncFutureRef<Getter<ScopeVariables>> propertiesRef = | |
| 492 new AsyncFutureRef<Getter<ScopeVariables>>(); | |
| 493 private final String objectId; | |
| 494 private final Type type; | |
| 495 private final WipValueLoader valueLoader; | |
| 496 | |
| 497 public DeclarativeScopeImpl(ScopeValue scopeData, Type type, WipValueLoader
valueLoader) { | |
| 498 this.type = type; | |
| 499 this.objectId = scopeData.object().objectId(); | |
| 500 this.valueLoader = valueLoader; | |
| 501 } | |
| 502 | |
| 503 @Override public Type getType() { | |
| 504 return type; | |
| 505 } | |
| 506 | |
| 507 @Override public Declarative asDeclarativeScope() { | |
| 508 return this; | |
| 509 } | |
| 510 | |
| 511 @Override public ObjectBased asObjectBased() { | |
| 512 return null; | |
| 513 } | |
| 514 | |
| 515 @Override public <R> R accept(Visitor<R> visitor) { | |
| 516 return visitor.visitDeclarative(this); | |
| 517 } | |
| 518 | |
| 519 @Override | |
| 520 public List<? extends JsVariable> getVariables() throws MethodIsBlockingExce
ption { | |
| 521 int currentCacheState = valueLoader.getCacheState(); | |
| 522 if (propertiesRef.isInitialized()) { | |
| 523 ScopeVariables result = propertiesRef.getSync().get(); | |
| 524 if (result.cacheState == currentCacheState) { | |
| 525 return result.variables; | |
| 526 } | |
| 527 startLoadOperation(true, currentCacheState); | |
| 528 } else { | |
| 529 startLoadOperation(false, currentCacheState); | |
| 530 } | |
| 531 | |
| 532 // This is blocking. | |
| 533 return propertiesRef.getSync().get().variables; | |
| 534 } | |
| 535 | |
| 536 /** | |
| 537 * Starts load operation that works synchronously, i.e. it may block the cal
ling method. | |
| 538 * This is done because some thread must take post-processing anyway and it
shouldn't | |
| 539 * be the Dispatch thread. | |
| 540 * The method may not be blocking, if another thread is already doing the sa
me operation. | |
| 541 */ | |
| 542 private void startLoadOperation(boolean reload, int currentCacheState) | |
| 543 throws MethodIsBlockingException { | |
| 544 WipValueLoader.LoadPostprocessor<Getter<ScopeVariables>> processor = | |
| 545 new WipValueLoader.LoadPostprocessor<Getter<ScopeVariables>>() { | |
| 546 @Override | |
| 547 public Getter<ScopeVariables> process( | |
| 548 List<? extends PropertyDescriptorValue> propertyList, int currentCac
heState) { | |
| 549 final List<JsVariable> properties = new ArrayList<JsVariable>(property
List.size()); | |
| 550 | |
| 551 WipValueBuilder valueBuilder = valueLoader.getValueBuilder(); | |
| 552 for (PropertyDescriptorValue property : propertyList) { | |
| 553 final String name = property.name(); | |
| 554 | |
| 555 ValueNameBuilder valueNameBuilder = | |
| 556 WipExpressionBuilder.createRootName(name, false); | |
| 557 | |
| 558 JsVariable variable; | |
| 559 if (objectId == null) { | |
| 560 variable = valueBuilder.createVariable(property.value(), valueName
Builder); | |
| 561 } else { | |
| 562 variable = valueBuilder.createObjectProperty(property, objectId, v
alueNameBuilder); | |
| 563 } | |
| 564 properties.add(variable); | |
| 565 } | |
| 566 final ScopeVariables scopeVariables = new ScopeVariables(properties, c
urrentCacheState); | |
| 567 return new Getter<ScopeVariables>() { | |
| 568 @Override | |
| 569 ScopeVariables get() { | |
| 570 return scopeVariables; | |
| 571 } | |
| 572 }; | |
| 573 } | |
| 574 | |
| 575 @Override | |
| 576 public Getter<ScopeVariables> getEmptyResult() { | |
| 577 return EMPTY_SCOPE_VARIABLES_OPTIONAL; | |
| 578 } | |
| 579 | |
| 580 @Override | |
| 581 public Getter<ScopeVariables> forException(Exception exception) { | |
| 582 return WipValueLoader.Getter.newFailure(exception); | |
| 583 } | |
| 584 }; | |
| 585 // This is blocking. | |
| 586 valueLoader.loadPropertiesInFuture(objectId, processor, reload, currentCac
heState, | |
| 587 propertiesRef); | |
| 588 } | |
| 589 } | |
| 590 | |
| 591 private static class ObjectScopeImpl implements JsScope.ObjectBased { | |
| 592 private final JsValue jsValue; | |
| 593 private final JsScope.Type type; | |
| 594 | |
| 595 ObjectScopeImpl(ScopeValue scopeData, JsScope.Type type, WipValueLoader valu
eLoader) { | |
| 596 jsValue = valueLoader.getValueBuilder().wrap(scopeData.object(), null); | |
| 597 this.type = type; | |
| 598 } | |
| 599 | |
| 600 @Override public Type getType() { | |
| 601 return type; | |
| 602 } | |
| 603 | |
| 604 @Override public Declarative asDeclarativeScope() { | |
| 605 return null; | |
| 606 } | |
| 607 | |
| 608 @Override public ObjectBased asObjectBased() { | |
| 609 return this; | |
| 610 } | |
| 611 | |
| 612 @Override public <R> R accept(Visitor<R> visitor) { | |
| 613 return visitor.visitObject(this); | |
| 614 } | |
| 615 | |
| 616 @Override | |
| 617 public JsObject getScopeObject() throws MethodIsBlockingException { | |
| 618 JsObject jsObject = jsValue.asObject(); | |
| 619 if (jsObject == null) { | |
| 620 throw new RuntimeException("Received scope object value is not an object
"); | |
| 621 } | |
| 622 return jsObject; | |
| 623 } | |
| 624 } | |
| 625 | |
| 626 static final class GlobalEvaluateContext extends WipEvaluateContextBase<Evalua
teData> { | |
| 627 | |
| 628 GlobalEvaluateContext(WipValueLoader valueLoader) { | |
| 629 super(valueLoader); | |
| 630 } | |
| 631 | |
| 632 @Override protected WipParamsWithResponse<EvaluateData> createRequestParams(
String expression, | |
| 633 WipValueLoader destinationValueLoader) { | |
| 634 boolean doNotPauseOnExceptions = true; | |
| 635 return new EvaluateParams(expression, destinationValueLoader.getObjectGrou
pId(), | |
| 636 false, doNotPauseOnExceptions, null, false); | |
| 637 } | |
| 638 | |
| 639 @Override protected RemoteObjectValue getRemoteObjectValue(EvaluateData data
) { | |
| 640 return data.result(); | |
| 641 } | |
| 642 | |
| 643 @Override protected Boolean getWasThrown(EvaluateData data) { | |
| 644 return data.wasThrown(); | |
| 645 } | |
| 646 } | |
| 647 | |
| 648 private static final Map<ScopeValue.Type, JsScope.Type> WIP_TO_SDK_SCOPE_TYPE; | |
| 649 static { | |
| 650 WIP_TO_SDK_SCOPE_TYPE = new HashMap<ScopeValue.Type, JsScope.Type>(); | |
| 651 | |
| 652 WIP_TO_SDK_SCOPE_TYPE.put(ScopeValue.Type.GLOBAL, JsScope.Type.GLOBAL); | |
| 653 WIP_TO_SDK_SCOPE_TYPE.put(ScopeValue.Type.LOCAL, JsScope.Type.LOCAL); | |
| 654 WIP_TO_SDK_SCOPE_TYPE.put(ScopeValue.Type.WITH, JsScope.Type.WITH); | |
| 655 WIP_TO_SDK_SCOPE_TYPE.put(ScopeValue.Type.CLOSURE, JsScope.Type.CLOSURE); | |
| 656 WIP_TO_SDK_SCOPE_TYPE.put(ScopeValue.Type.CATCH, JsScope.Type.CATCH); | |
| 657 | |
| 658 assert WIP_TO_SDK_SCOPE_TYPE.size() == ScopeValue.Type.values().length; | |
| 659 } | |
| 660 | |
| 661 private static class ScopeVariables { | |
| 662 final List<JsVariable> variables; | |
| 663 final int cacheState; | |
| 664 | |
| 665 ScopeVariables(List<JsVariable> variables, int cacheState) { | |
| 666 this.variables = variables; | |
| 667 this.cacheState = cacheState; | |
| 668 } | |
| 669 } | |
| 670 | |
| 671 private static final Getter<ScopeVariables> EMPTY_SCOPE_VARIABLES_OPTIONAL = | |
| 672 new Getter<ScopeVariables>() { | |
| 673 private final ScopeVariables value = | |
| 674 new ScopeVariables(Collections.<JsVariable>emptyList(), Integer.MAX_VA
LUE); | |
| 675 | |
| 676 @Override ScopeVariables get() { | |
| 677 return value; | |
| 678 } | |
| 679 }; | |
| 680 | |
| 681 | |
| 682 private WipParams sdkStepToProtocolStep(StepAction stepAction) { | |
| 683 switch (stepAction) { | |
| 684 case CONTINUE: | |
| 685 return RESUME_PARAMS; | |
| 686 case IN: | |
| 687 return STEP_INTO_PARAMS; | |
| 688 case OUT: | |
| 689 return STEP_OUT_PARAMS; | |
| 690 case OVER: | |
| 691 return STEP_OVER_PARAMS; | |
| 692 default: | |
| 693 throw new RuntimeException(); | |
| 694 } | |
| 695 } | |
| 696 | |
| 697 private static final ResumeParams RESUME_PARAMS = new ResumeParams(); | |
| 698 private static final StepIntoParams STEP_INTO_PARAMS = new StepIntoParams(); | |
| 699 private static final StepOutParams STEP_OUT_PARAMS = new StepOutParams(); | |
| 700 private static final StepOverParams STEP_OVER_PARAMS = new StepOverParams(); | |
| 701 } | |
| OLD | NEW |