Search in sources :

Example 1 with CArray

use of com.laytonsmith.core.constructs.CArray in project CommandHelper by EngineHub.

the class ConfigRuntimeException method DoReport.

/**
 * If the Reaction returned by GetReaction is to report the exception, this function should be used to standardize
 * the report format. If the error message wouldn't be very useful by itself, or if a hint is desired, an optional
 * message may be provided (null otherwise).
 *
 * @param e
 * @param optionalMessage
 */
@SuppressWarnings("ThrowableResultIgnored")
private static void DoReport(String message, String exceptionType, ConfigRuntimeException ex, List<StackTraceElement> stacktrace, MCPlayer currentPlayer) {
    String type = exceptionType;
    if (exceptionType == null) {
        type = "FATAL";
    }
    List<StackTraceElement> st = new ArrayList<>(stacktrace);
    if (message == null) {
        message = "";
    }
    if (!"".equals(message.trim())) {
        message = ": " + message;
    }
    Target top = Target.UNKNOWN;
    for (StackTraceElement e : st) {
        Target t = e.getDefinedAt();
        if (top == Target.UNKNOWN) {
            top = t;
        }
    }
    StringBuilder log = new StringBuilder();
    StringBuilder console = new StringBuilder();
    StringBuilder player = new StringBuilder();
    PrintMessage(log, console, player, type, message, ex, st);
    if (ex != null) {
        // Otherwise, a CCE
        if (ex.getCause() != null && ex.getCause() instanceof ConfigRuntimeException) {
            ex = (ConfigRuntimeException) ex.getCause();
        }
        while (ex instanceof CRECausedByWrapper) {
            Target t = ex.getTarget();
            log.append("Caused by:\n");
            console.append(TermColors.CYAN).append("Caused by:\n");
            player.append(MCChatColor.AQUA).append("Caused by:\n");
            CArray exception = ((CRECausedByWrapper) ex).getException();
            CArray stackTrace = Static.getArray(exception.get("stackTrace", t), t);
            List<StackTraceElement> newSt = new ArrayList<>();
            for (Construct consElement : stackTrace.asList()) {
                CArray element = Static.getArray(consElement, t);
                int line = Static.getInt32(element.get("line", t), t);
                File file = new File(element.get("file", t).val());
                int col = element.getColumn();
                Target stElementTarget = new Target(line, file, col);
                newSt.add(new StackTraceElement(element.get("id", t).val(), stElementTarget));
            }
            String nType = exception.get("classType", t).val();
            String nMessage = exception.get("message", t).val();
            if (!"".equals(nMessage.trim())) {
                nMessage = ": " + nMessage;
            }
            PrintMessage(log, console, player, nType, nMessage, ex, newSt);
            ex = (ConfigRuntimeException) ex.getCause();
        }
    }
    // Log
    // Don't log to screen though, since we're ALWAYS going to do that ourselves.
    CHLog.GetLogger().Log("COMPILE ERROR".equals(exceptionType) ? CHLog.Tags.COMPILER : CHLog.Tags.RUNTIME, LogLevel.ERROR, log.toString(), top, false);
    // Console
    StreamUtils.GetSystemOut().println(console.toString() + TermColors.reset());
    // Player
    if (currentPlayer != null) {
        currentPlayer.sendMessage(player.toString());
    }
}
Also used : Target(com.laytonsmith.core.constructs.Target) CRECausedByWrapper(com.laytonsmith.core.exceptions.CRE.CRECausedByWrapper) ArrayList(java.util.ArrayList) CArray(com.laytonsmith.core.constructs.CArray) Construct(com.laytonsmith.core.constructs.Construct) File(java.io.File)

Example 2 with CArray

use of com.laytonsmith.core.constructs.CArray in project CommandHelper by EngineHub.

the class Web method getCookieJar.

private static void getCookieJar(CArray arrayJar, CookieJar cookieJar, Target t) {
    CArray ret = arrayJar;
    for (Cookie cookie : cookieJar.getAllCookies()) {
        boolean update = false;
        CArray aCookie = null;
        for (Construct ac : arrayJar.asList()) {
            aCookie = Static.getArray(ac, t);
            if (cookie.getName().equals(aCookie.get("name", t).val()) && cookie.getDomain().equals(aCookie.get("domain", t).val()) && cookie.getPath().equals(aCookie.get("path", t).val())) {
                // This is just an update, not a new cookie
                update = true;
                break;
            }
        }
        CArray c;
        if (!update) {
            c = CArray.GetAssociativeArray(t);
        } else {
            c = aCookie;
        }
        c.set("name", cookie.getName());
        c.set("value", cookie.getValue());
        c.set("domain", cookie.getDomain());
        c.set("path", cookie.getPath());
        c.set("expiration", new CInt(cookie.getExpiration(), t), t);
        c.set("httpOnly", CBoolean.get(cookie.isHttpOnly()), t);
        c.set("secureOnly", CBoolean.get(cookie.isSecureOnly()), t);
        if (!update) {
            ret.push(c, t);
        }
    }
}
Also used : Cookie(com.laytonsmith.PureUtilities.Web.Cookie) CInt(com.laytonsmith.core.constructs.CInt) CArray(com.laytonsmith.core.constructs.CArray) Construct(com.laytonsmith.core.constructs.Construct)

Example 3 with CArray

use of com.laytonsmith.core.constructs.CArray in project CommandHelper by EngineHub.

the class AbstractFunction method profileMessage.

@Override
public String profileMessage(Construct... args) {
    StringBuilder b = new StringBuilder();
    boolean first = true;
    for (Construct ccc : args) {
        if (!first) {
            b.append(", ");
        }
        first = false;
        if (ccc instanceof CArray) {
            // Arrays take too long to toString, so we don't want to actually toString them here if
            // we don't need to.
            b.append("<arrayNotShown size:" + ((CArray) ccc).size() + ">");
        } else if (ccc instanceof CClosure) {
            // The toString of a closure is too long, so let's not output them either.
            b.append("<closureNotShown>");
        } else if (ccc instanceof CString) {
            String val = ccc.val().replace("\\", "\\\\").replace("'", "\\'");
            int max = 1000;
            if (val.length() > max) {
                val = val.substring(0, max) + "... (" + (val.length() - max) + " more characters hidden)";
            }
            b.append("'").append(val).append("'");
        } else if (ccc instanceof IVariable) {
            b.append(((IVariable) ccc).getVariableName());
        } else {
            b.append(ccc.val());
        }
    }
    return "Executing function: " + this.getName() + "(" + b.toString() + ")";
}
Also used : CClosure(com.laytonsmith.core.constructs.CClosure) IVariable(com.laytonsmith.core.constructs.IVariable) CArray(com.laytonsmith.core.constructs.CArray) Construct(com.laytonsmith.core.constructs.Construct) CString(com.laytonsmith.core.constructs.CString) CString(com.laytonsmith.core.constructs.CString)

Example 4 with CArray

use of com.laytonsmith.core.constructs.CArray in project CommandHelper by EngineHub.

the class Math method doIncrementDecrement.

/**
 * If we have the case {@code @array[0]++}, we have to increment it as though it were a variable, so we have to do
 * that with execs. This method consolidates the code to do so.
 *
 * @return
 */
protected static Construct doIncrementDecrement(ParseTree[] nodes, Script parent, Environment env, Target t, Function func, boolean pre, boolean inc) {
    if (nodes[0].getData() instanceof CFunction) {
        Function f;
        try {
            f = ((CFunction) nodes[0].getData()).getFunction();
        } catch (ConfigCompileException ex) {
            // This can't really happen, as the compiler would have already caught this
            throw new Error(ex);
        }
        if (f.getName().equals(new ArrayHandling.array_get().getName())) {
            // Ok, so, this is it, we're in charge here.
            long temp;
            long newVal;
            // First, pull out the current value. We're gonna do this manually though, and we will actually
            // skip the whole array_get execution.
            ParseTree eval = nodes[0];
            Construct array = parent.seval(eval.getChildAt(0), env);
            Construct index = parent.seval(eval.getChildAt(1), env);
            Construct cdelta = new CInt(1, t);
            if (nodes.length == 2) {
                cdelta = parent.seval(nodes[1], env);
            }
            long delta = Static.getInt(cdelta, t);
            // First, error check, then get the old value, and store it in temp.
            if (!(array instanceof CArray) && !(array instanceof ArrayAccess)) {
                // Let's just evaluate this like normal with array_get, so it will
                // throw the appropriate exception.
                new ArrayHandling.array_get().exec(t, env, array, index);
                throw ConfigRuntimeException.CreateUncatchableException("Shouldn't have gotten here. Please report this error, and how you got here.", t);
            } else if (!(array instanceof CArray)) {
                // own exception.
                throw new CRECastException("Cannot increment/decrement a non-array array" + " accessed value. (The value passed in was \"" + array.val() + "\")", t);
            } else {
                // Ok, we're good. Data types should all be correct.
                CArray myArray = ((CArray) array);
                Construct value = myArray.get(index, t);
                if (value instanceof CInt || value instanceof CDouble) {
                    temp = Static.getInt(value, t);
                    if (inc) {
                        newVal = temp + delta;
                    } else {
                        newVal = temp - delta;
                    }
                    new ArrayHandling.array_set().exec(t, env, array, index, new CInt(newVal, t));
                } else {
                    throw new CRECastException("Cannot increment/decrement a non numeric value.", t);
                }
            }
            long valueToReturn;
            if (pre) {
                valueToReturn = newVal;
            } else {
                valueToReturn = temp;
            }
            return new CInt(valueToReturn, t);
        }
    }
    Construct[] args = new Construct[nodes.length];
    for (int i = 0; i < args.length; i++) {
        args[i] = parent.eval(nodes[i], env);
    }
    return func.exec(t, env, args);
}
Also used : CRECastException(com.laytonsmith.core.exceptions.CRE.CRECastException) CArray(com.laytonsmith.core.constructs.CArray) CFunction(com.laytonsmith.core.constructs.CFunction) CDouble(com.laytonsmith.core.constructs.CDouble) ConfigCompileException(com.laytonsmith.core.exceptions.ConfigCompileException) CFunction(com.laytonsmith.core.constructs.CFunction) ArrayAccess(com.laytonsmith.core.natives.interfaces.ArrayAccess) CInt(com.laytonsmith.core.constructs.CInt) Construct(com.laytonsmith.core.constructs.Construct) ParseTree(com.laytonsmith.core.ParseTree)

Example 5 with CArray

use of com.laytonsmith.core.constructs.CArray in project CommandHelper by EngineHub.

the class AbstractCREException method getExceptionObject.

/**
 * Returns a standardized CArray given this exception.
 *
 * @return
 */
public CArray getExceptionObject() {
    CArray ret = new CArray(Target.UNKNOWN);
    ret.set("classType", this.getExceptionType(), Target.UNKNOWN);
    ret.set("message", this.getMessage());
    CArray stackTrace = new CArray(Target.UNKNOWN);
    ret.set("stackTrace", stackTrace, Target.UNKNOWN);
    for (StackTraceElement e : this.getCREStackTrace()) {
        CArray element = e.getObjectFor();
        stackTrace.push(element, Target.UNKNOWN);
    }
    ret.set("causedBy", getCausedBy(this.getCause()), Target.UNKNOWN);
    return ret;
}
Also used : CArray(com.laytonsmith.core.constructs.CArray)

Aggregations

CArray (com.laytonsmith.core.constructs.CArray)50 CString (com.laytonsmith.core.constructs.CString)27 Construct (com.laytonsmith.core.constructs.Construct)23 CInt (com.laytonsmith.core.constructs.CInt)17 CNull (com.laytonsmith.core.constructs.CNull)12 CREFormatException (com.laytonsmith.core.exceptions.CRE.CREFormatException)12 HashMap (java.util.HashMap)9 CDouble (com.laytonsmith.core.constructs.CDouble)8 IVariable (com.laytonsmith.core.constructs.IVariable)8 GlobalEnv (com.laytonsmith.core.environments.GlobalEnv)6 ConfigRuntimeException (com.laytonsmith.core.exceptions.ConfigRuntimeException)6 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6 CBoolean (com.laytonsmith.core.constructs.CBoolean)5 Target (com.laytonsmith.core.constructs.Target)5 CRECastException (com.laytonsmith.core.exceptions.CRE.CRECastException)5 FunctionReturnException (com.laytonsmith.core.exceptions.FunctionReturnException)5 MCEnchantment (com.laytonsmith.abstraction.MCEnchantment)4 MCItemMeta (com.laytonsmith.abstraction.MCItemMeta)4 MCItemStack (com.laytonsmith.abstraction.MCItemStack)4