use of com.oracle.truffle.tools.chromeinspector.types.CallArgument in project graal by oracle.
the class InspectorRuntime method callFunctionOn.
@Override
public Params callFunctionOn(String objectId, String functionDeclaration, JSONArray arguments, boolean silent, boolean returnByValue, boolean generatePreview, boolean awaitPromise, int executionContextId, String objectGroup) throws CommandProcessException {
if (objectId == null) {
throw new CommandProcessException("An objectId required.");
}
RemoteObject object = context.getRemoteObjectsHandler().getRemote(objectId);
JSONObject json = new JSONObject();
if (object != null) {
DebugValue value = object.getDebugValue();
DebugScope scope = object.getScope();
RemoteObject.IndexRange indexRange = object.getIndexRange();
DebuggerSuspendedInfo suspendedInfo = context.getSuspendedInfo();
if (suspendedInfo != null) {
try {
String functionTrimmed = functionDeclaration.trim();
String functionNoWS = eliminateWhiteSpaces(functionDeclaration);
context.executeInSuspendThread(new SuspendThreadExecutable<Void>() {
@Override
public Void executeCommand() throws CommandProcessException {
JSONObject result;
if (functionNoWS.startsWith(FUNCTION_COMPLETION)) {
result = createCodecompletion(value, scope, context, true);
} else if (functionNoWS.equals(FUNCTION_SET_PROPERTY)) {
// Set of an array element, or object property
if (arguments == null || arguments.length() < 2) {
throw new CommandProcessException("Insufficient number of arguments: " + (arguments != null ? arguments.length() : 0) + ", expecting: 2");
}
Object property = ((JSONObject) arguments.get(0)).get("value");
CallArgument newValue = CallArgument.get((JSONObject) arguments.get(1));
setPropertyValue(value, scope, property, object.getTypeMark(), newValue, suspendedInfo.lastEvaluatedValue.getAndSet(null));
result = new JSONObject();
} else if (functionNoWS.equals(FUNCTION_GET_ARRAY_NUM_PROPS)) {
if (!value.isArray()) {
throw new CommandProcessException("Expecting an Array the function is called on.");
}
JSONArray arr = new JSONArray();
if (indexRange != null && !indexRange.isNamed()) {
List<DebugValue> array = value.getArray();
if (indexRange.start() < 0 || indexRange.end() > array.size()) {
throw new CommandProcessException("Array range out of bounds.");
}
arr.put(indexRange.end() - indexRange.start());
} else {
arr.put(value.getArray().size());
}
Collection<DebugValue> props = value.getProperties();
if (props == null) {
arr.put(0);
} else if (indexRange != null && indexRange.isNamed()) {
ArrayList<DebugValue> list = new ArrayList<>(props);
if (indexRange.start() < 0 || indexRange.end() > list.size()) {
throw new CommandProcessException("Named range out of bounds.");
}
arr.put(indexRange.end() - indexRange.start());
} else if (LanguageChecks.isJS(value.getOriginalLanguage())) {
// +1 for __proto__
arr.put(props.size() + 1);
} else {
arr.put(props.size());
}
result = new JSONObject();
result.put("value", arr);
} else if (functionNoWS.equals(FUNCTION_GET_BUFFER_NUM_PROPS)) {
if (!value.isArray()) {
throw new CommandProcessException("Expecting a Buffer the function is called on.");
}
JSONArray arr = new JSONArray();
if (indexRange != null && !indexRange.isNamed()) {
List<DebugValue> array = value.getArray();
if (indexRange.start() < 0 || indexRange.end() > array.size()) {
throw new CommandProcessException("Array range out of bounds.");
}
arr.put(indexRange.end() - indexRange.start());
} else {
arr.put(value.getArray().size());
}
if (LanguageChecks.isJS(value.getOriginalLanguage())) {
// +1 for __proto__
arr.put(1);
} else {
arr.put(0);
}
result = new JSONObject();
result.put("value", arr);
} else if (functionNoWS.equals(FUNCTION_GET_COLLECTION_NUM_PROPS)) {
Collection<DebugValue> props = value.getProperties();
if (props == null) {
throw new CommandProcessException("Expecting an Object the function is called on.");
}
JSONArray arr = new JSONArray();
arr.put(0);
if (indexRange != null && indexRange.isNamed()) {
ArrayList<DebugValue> list = new ArrayList<>(props);
if (indexRange.start() < 0 || indexRange.end() > list.size()) {
throw new CommandProcessException("Named range out of bounds.");
}
arr.put(indexRange.end() - indexRange.start());
} else if (LanguageChecks.isJS(value.getOriginalLanguage())) {
// +1 for __proto__
arr.put(props.size() + 1);
} else {
arr.put(props.size());
}
result = new JSONObject();
result.put("value", arr);
} else if (FUNCTION_GETTER_PATTERN1.matcher(functionTrimmed).matches()) {
if (arguments == null || arguments.length() < 1) {
throw new CommandProcessException("Expecting an argument to invokeGetter function.");
}
String propertyNames = ((JSONObject) arguments.get(0)).getString("value");
JSONArray properties = new JSONArray(propertyNames);
DebugValue v = value;
for (int i = 0; i < properties.length() && (i == 0 || v != null); i++) {
String propertyName = properties.getString(i);
if (v != null) {
v = v.getProperty(propertyName);
} else {
v = scope.getDeclaredValue(propertyName);
}
}
result = asResult(v);
} else if (FUNCTION_GETTER_PATTERN2.matcher(functionTrimmed).matches()) {
if (arguments == null || arguments.length() < 1) {
throw new CommandProcessException("Expecting an argument to invokeGetter function.");
}
String propertyName = ((JSONObject) arguments.get(0)).getString("value");
DebugValue p;
if (value != null) {
p = value.getProperty(propertyName);
} else {
p = scope.getDeclaredValue(propertyName);
}
result = asResult(p);
} else if (FUNCTION_GET_INDEXED_VARS_PATTERN.matcher(functionTrimmed).matches()) {
if (!value.isArray()) {
throw new CommandProcessException("Expecting an Array the function is called on.");
}
if (arguments == null || arguments.length() < 2) {
throw new CommandProcessException("Insufficient number of arguments: " + (arguments != null ? arguments.length() : 0) + ", expecting: 2");
}
int start = ((JSONObject) arguments.get(0)).getInt("value");
int count = ((JSONObject) arguments.get(1)).getInt("value");
RemoteObject ro = new RemoteObject(value, true, generatePreview, context, new RemoteObject.IndexRange(start, start + count, false));
context.getRemoteObjectsHandler().register(ro, objectGroup);
result = ro.toJSON();
} else if (FUNCTION_GET_NAMED_VARS_PATTERN.matcher(functionTrimmed).matches()) {
Collection<DebugValue> props = value.getProperties();
if (props == null) {
throw new CommandProcessException("Expecting an Object the function is called on.");
}
if (arguments == null || arguments.length() < 2) {
throw new CommandProcessException("Insufficient number of arguments: " + (arguments != null ? arguments.length() : 0) + ", expecting: 2");
}
int start = ((JSONObject) arguments.get(0)).getInt("value");
int count = ((JSONObject) arguments.get(1)).getInt("value");
RemoteObject ro = new RemoteObject(value, true, generatePreview, context, new RemoteObject.IndexRange(start, start + count, true));
context.getRemoteObjectsHandler().register(ro, objectGroup);
result = ro.toJSON();
} else {
// Process CustomPreview body:
if (arguments != null && arguments.length() > 0) {
Object arg0 = arguments.get(0);
if (arg0 instanceof JSONObject) {
JSONObject argObj = (JSONObject) arg0;
Object id = argObj.opt("objectId");
if (id instanceof String) {
DebugValue body = context.getRemoteObjectsHandler().getCustomPreviewBody((String) id);
if (body != null) {
// The config is not provided as an argument.
// Get the cached config:
DebugValue config = context.getRemoteObjectsHandler().getCustomPreviewConfig(objectId);
DebugValue bodyML = (config != null) ? body.execute(object.getDebugValue(), config) : body.execute(object.getDebugValue());
Object bodyjson = CustomPreview.value2JSON(bodyML, context);
result = new JSONObject();
result.put("type", "object");
result.put("value", bodyjson);
json.put("result", result);
return null;
}
}
}
}
StringBuilder code = new StringBuilder();
code.append("(").append(functionTrimmed).append(").apply(").append(value != null ? value.getName() : "null");
if (arguments != null) {
code.append(",[");
for (int i = 0; i < arguments.length(); i++) {
JSONObject arg = arguments.getJSONObject(i);
if (i > 0) {
code.append(",");
}
Object id = arg.opt("objectId");
if (id instanceof String) {
RemoteObject remoteArg = context.getRemoteObjectsHandler().getRemote((String) id);
if (remoteArg == null) {
throw new CommandProcessException("Cannot resolve argument by its objectId: " + id);
}
code.append(remoteArg.getDebugValue().getName());
} else {
code.append(JSONObject.valueToString(arg.get("value")));
}
}
code.append("]");
}
code.append(")");
DebugValue eval = suspendedInfo.getSuspendedEvent().getTopStackFrame().eval(code.toString());
suspendedInfo.refreshFrames();
result = asResult(eval);
}
json.put("result", result);
return null;
}
@Override
public Void processException(DebugException ex) {
fillExceptionDetails(json, ex);
return null;
}
private JSONObject asResult(DebugValue v) {
JSONObject result;
if (v == null) {
LanguageInfo language = suspendedInfo.getSuspendedEvent().getTopStackFrame().getLanguage();
result = RemoteObject.createNullObject(context.getEnv(), language).toJSON();
} else {
if (!returnByValue) {
RemoteObject ro = new RemoteObject(v, true, generatePreview, context);
context.getRemoteObjectsHandler().register(ro, objectGroup);
result = ro.toJSON();
} else {
result = RemoteObject.createJSONResultValue(v, context.areToStringSideEffectsAllowed(), context.getErr());
}
}
return result;
}
});
} catch (NoSuspendedThreadException ex) {
json.put("result", new JSONObject());
}
}
}
return new Params(json);
}
Aggregations