Search in sources :

Example 86 with JSONArray

use of com.oracle.truffle.tools.utils.json.JSONArray in project graal by oracle.

the class CustomPreview method value2JSON.

public static Object value2JSON(DebugValue value, InspectorExecutionContext context) {
    if (value.isArray()) {
        JSONArray json = new JSONArray();
        List<DebugValue> array = value.getArray();
        if (array.size() == 2 && array.get(0).isReadable() && array.get(1).isReadable() && "object".equals(array.get(0).asString()) && array.get(1).getProperty("config") != null) {
            // Child object:
            json.put(value2JSON(array.get(0), context));
            DebugValue child = array.get(1);
            DebugValue object = child.getProperty("object");
            DebugValue config = child.getProperty("config");
            RemoteObject remoteConfig = context.getRemoteObjectsHandler().getRemote(object);
            JSONObject childJSON = remoteConfig.toJSON();
            JSONObject customPreview = create(object, config, object.getOriginalLanguage(), context);
            if (customPreview != null) {
                childJSON.put("customPreview", customPreview);
            }
            json.put(childJSON);
        } else {
            for (DebugValue element : array) {
                if (element.isReadable()) {
                    json.put(value2JSON(element, context));
                } else {
                    json.put(InspectorExecutionContext.VALUE_NOT_READABLE);
                }
            }
        }
        return json;
    } else {
        Collection<DebugValue> properties = value.getProperties();
        if (properties != null) {
            JSONObject json = new JSONObject();
            for (DebugValue property : properties) {
                try {
                    if (property.isReadable() && !property.canExecute() && !property.isInternal()) {
                        Object rawValue = getPrimitiveValue(property);
                        // Do not allow inner objects
                        if (rawValue != null) {
                            json.put(property.getName(), rawValue);
                        }
                    }
                } catch (DebugException ex) {
                    if (ex.isInternalError()) {
                        PrintWriter err = context.getErr();
                        if (err != null) {
                            ex.printStackTrace(err);
                        }
                    } else {
                        throw ex;
                    }
                }
            }
            return json;
        } else {
            return getPrimitiveValue(value);
        }
    }
}
Also used : DebugValue(com.oracle.truffle.api.debug.DebugValue) JSONObject(com.oracle.truffle.tools.utils.json.JSONObject) JSONArray(com.oracle.truffle.tools.utils.json.JSONArray) JSONObject(com.oracle.truffle.tools.utils.json.JSONObject) DebugException(com.oracle.truffle.api.debug.DebugException) PrintWriter(java.io.PrintWriter)

Example 87 with JSONArray

use of com.oracle.truffle.tools.utils.json.JSONArray in project graal by oracle.

the class Params method createConsoleAPICalled.

public static Params createConsoleAPICalled(String type, Object text, long contextId) {
    JSONObject params = new JSONObject();
    params.put("type", type);
    JSONArray args = new JSONArray();
    if (text != null) {
        JSONObject outObject = new JSONObject();
        if (InteropLibrary.getUncached().isString(text)) {
            outObject.put("type", "string");
        } else if (text instanceof Number) {
            outObject.put("type", "number");
        }
        outObject.put("value", text);
        args.put(outObject);
    }
    params.put("args", args);
    params.put("executionContextId", contextId);
    params.put("timestamp", System.nanoTime() / 1000_000.0);
    return new Params(params);
}
Also used : JSONObject(com.oracle.truffle.tools.utils.json.JSONObject) JSONArray(com.oracle.truffle.tools.utils.json.JSONArray)

Example 88 with JSONArray

use of com.oracle.truffle.tools.utils.json.JSONArray in project graal by oracle.

the class WorkspaceEdit method getChanges.

/**
 * Holds changes to existing resources.
 */
public Map<String, List<TextEdit>> getChanges() {
    final JSONObject json = jsonData.optJSONObject("changes");
    if (json == null) {
        return null;
    }
    final Map<String, List<TextEdit>> map = new HashMap<>(json.length());
    for (String key : json.keySet()) {
        final JSONArray jsonArr = json.getJSONArray(key);
        final List<TextEdit> list = new ArrayList<>(jsonArr.length());
        for (int i = 0; i < json.length(); i++) {
            list.add(new TextEdit(jsonArr.getJSONObject(i)));
        }
        map.put(key, Collections.unmodifiableList(list));
    }
    return map;
}
Also used : JSONObject(com.oracle.truffle.tools.utils.json.JSONObject) HashMap(java.util.HashMap) JSONArray(com.oracle.truffle.tools.utils.json.JSONArray) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Example 89 with JSONArray

use of com.oracle.truffle.tools.utils.json.JSONArray in project graal by oracle.

the class WorkspaceEditClientCapabilities method getResourceOperations.

/**
 * The resource operations the client supports. Clients should at least support 'create',
 * 'rename' and 'delete' files and folders.
 *
 * @since 3.13.0
 */
public List<ResourceOperationKind> getResourceOperations() {
    final JSONArray json = jsonData.optJSONArray("resourceOperations");
    if (json == null) {
        return null;
    }
    final List<ResourceOperationKind> list = new ArrayList<>(json.length());
    for (int i = 0; i < json.length(); i++) {
        list.add(ResourceOperationKind.get(json.getString(i)));
    }
    return Collections.unmodifiableList(list);
}
Also used : JSONArray(com.oracle.truffle.tools.utils.json.JSONArray) ArrayList(java.util.ArrayList)

Example 90 with JSONArray

use of com.oracle.truffle.tools.utils.json.JSONArray in project graal by oracle.

the class WorkspaceEditClientCapabilities method setResourceOperations.

public WorkspaceEditClientCapabilities setResourceOperations(List<ResourceOperationKind> resourceOperations) {
    if (resourceOperations != null) {
        final JSONArray json = new JSONArray();
        for (ResourceOperationKind resourceOperationKind : resourceOperations) {
            json.put(resourceOperationKind.getStringValue());
        }
        jsonData.put("resourceOperations", json);
    }
    return this;
}
Also used : JSONArray(com.oracle.truffle.tools.utils.json.JSONArray)

Aggregations

JSONArray (com.oracle.truffle.tools.utils.json.JSONArray)195 JSONObject (com.oracle.truffle.tools.utils.json.JSONObject)85 ArrayList (java.util.ArrayList)74 DebugValue (com.oracle.truffle.api.debug.DebugValue)8 Params (com.oracle.truffle.tools.chromeinspector.commands.Params)8 Collection (java.util.Collection)7 List (java.util.List)7 DebugException (com.oracle.truffle.api.debug.DebugException)6 Source (com.oracle.truffle.api.source.Source)5 SourceSection (com.oracle.truffle.api.source.SourceSection)4 CommandProcessException (com.oracle.truffle.tools.chromeinspector.server.CommandProcessException)4 HashMap (java.util.HashMap)4 Location (com.oracle.truffle.tools.chromeinspector.types.Location)3 Script (com.oracle.truffle.tools.chromeinspector.types.Script)3 CPUSampler (com.oracle.truffle.tools.profiler.CPUSampler)3 CPUSamplerData (com.oracle.truffle.tools.profiler.CPUSamplerData)3 PrintWriter (java.io.PrintWriter)3 Map (java.util.Map)3 TruffleContext (com.oracle.truffle.api.TruffleContext)2 Breakpoint (com.oracle.truffle.api.debug.Breakpoint)2