Search in sources :

Example 31 with CString

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

the class MethodScriptExecutionQueue method getExceptionHandler.

private Thread.UncaughtExceptionHandler getExceptionHandler() {
    Thread.UncaughtExceptionHandler uceh = new Thread.UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(Thread t, Throwable e) {
            Environment env = Environment.createEnvironment(MethodScriptExecutionQueue.this.env);
            if (e instanceof ConfigRuntimeException) {
                // This should be handled by the default UEH
                ConfigRuntimeException.HandleUncaughtException(((ConfigRuntimeException) e), env);
            } else if (e instanceof FunctionReturnException) {
                // ignored, so we want to warn them, but not trigger a flat out error.
                if (!(((FunctionReturnException) e).getReturn() instanceof CVoid)) {
                    ConfigRuntimeException.DoWarning("Closure is returning a value in an execution queue task," + " which is unexpected behavior. It may return void however, which will" + " simply stop that one task. " + ((FunctionReturnException) e).getTarget().toString());
                }
            } else if (e instanceof CancelCommandException) {
                // Ok. If there's a message, echo it to console.
                String msg = ((CancelCommandException) e).getMessage().trim();
                if (!"".equals(msg)) {
                    Target tt = ((CancelCommandException) e).getTarget();
                    new Echoes.console().exec(tt, env, new CString(msg, tt));
                }
            } else {
                // handle, so let it bubble up further.
                throw new RuntimeException(e);
            }
        }
    };
    return uceh;
}
Also used : ConfigRuntimeException(com.laytonsmith.core.exceptions.ConfigRuntimeException) CString(com.laytonsmith.core.constructs.CString) CVoid(com.laytonsmith.core.constructs.CVoid) CString(com.laytonsmith.core.constructs.CString) Echoes(com.laytonsmith.core.functions.Echoes) Target(com.laytonsmith.core.constructs.Target) ConfigRuntimeException(com.laytonsmith.core.exceptions.ConfigRuntimeException) CancelCommandException(com.laytonsmith.core.exceptions.CancelCommandException) Environment(com.laytonsmith.core.environments.Environment) FunctionReturnException(com.laytonsmith.core.exceptions.FunctionReturnException)

Example 32 with CString

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

the class Static method getMSObject.

/**
 * Given a java object, returns a MethodScript object.
 *
 * @param object
 * @param t
 * @return
 */
public static Construct getMSObject(Object object, Target t) {
    if (object == null) {
        return CNull.NULL;
    } else if (object instanceof Boolean) {
        return CBoolean.get((boolean) object);
    } else if ((object instanceof Byte) || (object instanceof Short) || (object instanceof Integer) || (object instanceof Long)) {
        return new CInt((long) object, t);
    } else if ((object instanceof Float) || (object instanceof Double)) {
        return new CDouble((double) object, t);
    } else if (object instanceof Character) {
        return new CString((char) object, t);
    } else if (object instanceof String) {
        return new CString((String) object, t);
    } else if (object instanceof StringBuffer) {
        return new CResource<>((StringBuffer) object, new CResource.ResourceToString() {

            @Override
            public String getString(CResource res) {
                return res.getResource().toString();
            }
        }, t);
    } else if (object instanceof XMLDocument) {
        return new CResource<>((XMLDocument) object, t);
    } else if (object instanceof Construct) {
        return (Construct) object;
    } else if (object instanceof boolean[]) {
        boolean[] array = (boolean[]) object;
        CArray r = new CArray(t);
        for (boolean b : array) {
            r.push(CBoolean.get(b), t);
        }
        return r;
    } else if (object instanceof byte[]) {
        return CByteArray.wrap((byte[]) object, t);
    } else if (object instanceof char[]) {
        char[] array = (char[]) object;
        CArray r = new CArray(t);
        for (char c : array) {
            r.push(new CString(c, t), t);
        }
        return r;
    } else if (object instanceof short[]) {
        short[] array = (short[]) object;
        CArray r = new CArray(t);
        for (short s : array) {
            r.push(new CInt(s, t), t);
        }
        return r;
    } else if (object instanceof int[]) {
        int[] array = (int[]) object;
        CArray r = new CArray(t);
        for (int i : array) {
            r.push(new CInt(i, t), t);
        }
        return r;
    } else if (object instanceof long[]) {
        long[] array = (long[]) object;
        CArray r = new CArray(t);
        for (long l : array) {
            r.push(new CInt(l, t), t);
        }
        return r;
    } else if (object instanceof float[]) {
        float[] array = (float[]) object;
        CArray r = new CArray(t);
        for (float f : array) {
            r.push(new CDouble(f, t), t);
        }
        return r;
    } else if (object instanceof double[]) {
        double[] array = (double[]) object;
        CArray r = new CArray(t);
        for (double d : array) {
            r.push(new CDouble(d, t), t);
        }
        return r;
    } else if (object instanceof Object[]) {
        CArray r = new CArray(t);
        for (Object o : (Object[]) object) {
            r.push((o == object) ? r : getMSObject(o, t), t);
        }
        return r;
    } else if (object instanceof Collection) {
        return getMSObject(((Collection) object).toArray(), t);
    } else if (object instanceof Map) {
        Map map = ((Map) object);
        CArray r = new CArray(t);
        for (Object key : map.keySet()) {
            Object o = map.get(key);
            r.set(key.toString(), (o == object) ? r : getMSObject(o, t), t);
        }
        return r;
    } else {
        return new CString(object.toString(), t);
    }
}
Also used : CArray(com.laytonsmith.core.constructs.CArray) CString(com.laytonsmith.core.constructs.CString) XMLDocument(com.laytonsmith.PureUtilities.XMLDocument) CString(com.laytonsmith.core.constructs.CString) CBoolean(com.laytonsmith.core.constructs.CBoolean) CResource(com.laytonsmith.core.constructs.CResource) CDouble(com.laytonsmith.core.constructs.CDouble) CDouble(com.laytonsmith.core.constructs.CDouble) CInt(com.laytonsmith.core.constructs.CInt) Construct(com.laytonsmith.core.constructs.Construct) Collection(java.util.Collection) Map(java.util.Map) HashMap(java.util.HashMap)

Example 33 with CString

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

the class Static method getJavaObject.

/**
 * Given a MethodScript object, returns a java object.
 *
 * @param construct
 * @return
 */
public static Object getJavaObject(Construct construct) {
    if ((construct == null) || (construct instanceof CNull)) {
        return null;
    } else if (construct instanceof CVoid) {
        return "";
    } else if (construct instanceof CBoolean) {
        return ((CBoolean) construct).getBoolean();
    } else if (construct instanceof CInt) {
        return ((CInt) construct).getInt();
    } else if (construct instanceof CDouble) {
        return ((CDouble) construct).getDouble();
    } else if (construct instanceof CString) {
        return construct.val();
    } else if (construct instanceof CByteArray) {
        return ((CByteArray) construct).asByteArrayCopy();
    } else if (construct instanceof CResource) {
        return ((CResource) construct).getResource();
    } else if (construct instanceof CArray) {
        CArray array = (CArray) construct;
        if (array.isAssociative()) {
            HashMap<String, Object> map = new HashMap<>();
            for (Construct key : array.keySet()) {
                Construct c = array.get(key.val(), Target.UNKNOWN);
                map.put(key.val(), (c == array) ? map : getJavaObject(c));
            }
            return map;
        } else {
            Object[] a = new Object[(int) array.size()];
            boolean nullable = false;
            Class<?> clazz = null;
            for (int i = 0; i < array.size(); i++) {
                Construct c = array.get(i, Target.UNKNOWN);
                if (c == array) {
                    a[i] = a;
                } else {
                    a[i] = getJavaObject(array.get(i, Target.UNKNOWN));
                }
                if (a[i] != null) {
                    if (clazz == null) {
                        clazz = a[i].getClass();
                    } else if (!clazz.equals(Object.class)) {
                        // to test if it is possible to return something more specific than Object[]
                        Class<?> cl = a[i].getClass();
                        while (!clazz.isAssignableFrom(cl)) {
                            clazz = clazz.getSuperclass();
                        }
                    }
                } else {
                    nullable = true;
                }
            }
            if ((clazz != null) && (!clazz.equals(Object.class))) {
                if (clazz.equals(Boolean.class) && !nullable) {
                    boolean[] r = new boolean[a.length];
                    for (int i = 0; i < a.length; i++) {
                        r[i] = (boolean) a[i];
                    }
                    return r;
                }
                if (clazz.equals(Long.class) && !nullable) {
                    long[] r = new long[a.length];
                    for (int i = 0; i < a.length; i++) {
                        r[i] = (long) a[i];
                    }
                    return r;
                } else if (clazz.equals(Double.class) && !nullable) {
                    double[] r = new double[a.length];
                    for (int i = 0; i < a.length; i++) {
                        r[i] = (double) a[i];
                    }
                    return r;
                } else {
                    Object[] r = (Object[]) Array.newInstance(clazz, a.length);
                    System.arraycopy(a, 0, r, 0, a.length);
                    return r;
                }
            } else {
                return a;
            }
        }
    } else {
        return construct;
    }
}
Also used : HashMap(java.util.HashMap) CBoolean(com.laytonsmith.core.constructs.CBoolean) CArray(com.laytonsmith.core.constructs.CArray) CDouble(com.laytonsmith.core.constructs.CDouble) CString(com.laytonsmith.core.constructs.CString) CVoid(com.laytonsmith.core.constructs.CVoid) CString(com.laytonsmith.core.constructs.CString) CByteArray(com.laytonsmith.core.constructs.CByteArray) CInt(com.laytonsmith.core.constructs.CInt) Construct(com.laytonsmith.core.constructs.Construct) CBoolean(com.laytonsmith.core.constructs.CBoolean) CResource(com.laytonsmith.core.constructs.CResource) CNull(com.laytonsmith.core.constructs.CNull)

Example 34 with CString

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

the class Script method compileLeft.

private boolean compileLeft() {
    cleft = new ArrayList<>();
    if (label != null && label.startsWith("!")) {
        if (label.length() > 1) {
            label = label.substring(1);
        }
        nolog = true;
    }
    for (int i = 0; i < left.size(); i++) {
        Token t = left.get(i);
        if (t.value.startsWith("/")) {
            cleft.add(new Command(t.val(), t.target));
        } else if (t.type == Token.TType.VARIABLE) {
            cleft.add(new Variable(t.val(), null, t.target));
        } else if (t.type.equals(TType.FINAL_VAR)) {
            Variable v = new Variable(t.val(), null, t.target);
            v.setFinal(true);
            cleft.add(v);
        } else if (t.type.equals(TType.LSQUARE_BRACKET)) {
            if (i + 2 < left.size() && left.get(i + 2).type.equals(TType.OPT_VAR_ASSIGN)) {
                Variable v = new Variable(left.get(i + 1).val(), left.get(i + 3).val(), t.target);
                v.setOptional(true);
                if (left.get(i + 1).type.equals(TType.FINAL_VAR)) {
                    v.setFinal(true);
                }
                cleft.add(v);
                i += 4;
            } else {
                t = left.get(i + 1);
                Variable v = new Variable(t.val(), null, t.target);
                v.setOptional(true);
                if (t.val().equals("$")) {
                    v.setFinal(true);
                }
                cleft.add(v);
                i += 2;
            }
        } else {
            cleft.add(new CString(t.val(), t.getTarget()));
        }
    }
    return true;
}
Also used : IVariable(com.laytonsmith.core.constructs.IVariable) Variable(com.laytonsmith.core.constructs.Variable) Command(com.laytonsmith.core.constructs.Command) Token(com.laytonsmith.core.constructs.Token) ProfilePoint(com.laytonsmith.core.profiler.ProfilePoint) CString(com.laytonsmith.core.constructs.CString)

Aggregations

CString (com.laytonsmith.core.constructs.CString)34 CArray (com.laytonsmith.core.constructs.CArray)23 Construct (com.laytonsmith.core.constructs.Construct)19 CInt (com.laytonsmith.core.constructs.CInt)12 HashMap (java.util.HashMap)10 IVariable (com.laytonsmith.core.constructs.IVariable)9 ConfigRuntimeException (com.laytonsmith.core.exceptions.ConfigRuntimeException)9 Map (java.util.Map)8 CDouble (com.laytonsmith.core.constructs.CDouble)7 CNull (com.laytonsmith.core.constructs.CNull)7 Variable (com.laytonsmith.core.constructs.Variable)6 CBoolean (com.laytonsmith.core.constructs.CBoolean)5 CFunction (com.laytonsmith.core.constructs.CFunction)5 Target (com.laytonsmith.core.constructs.Target)5 CREFormatException (com.laytonsmith.core.exceptions.CRE.CREFormatException)5 ConfigCompileException (com.laytonsmith.core.exceptions.ConfigCompileException)5 FunctionReturnException (com.laytonsmith.core.exceptions.FunctionReturnException)5 ArrayList (java.util.ArrayList)5 MCItemStack (com.laytonsmith.abstraction.MCItemStack)4 ParseTree (com.laytonsmith.core.ParseTree)4