Search in sources :

Example 6 with IVariable

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

the class BoundEvent method trigger.

/**
 * When the event actually occurs, this should be run, after translating the original event object (of whatever type
 * it may be) into a standard map, which contains the event object data. It is converted into a CArray here, and
 * then the script is executed with the driver's execute function.
 *
 * @param activeEvent
 */
public void trigger(ActiveEvent activeEvent) throws EventException {
    try {
        // GenericTree<Construct> root = new GenericTree<Construct>();
        // root.setRoot(tree);
        Environment env = originalEnv.clone();
        CArray ca = CArray.GetAssociativeArray(Target.UNKNOWN);
        for (Map.Entry<String, Construct> entry : activeEvent.parsedEvent.entrySet()) {
            ca.set(new CString(entry.getKey(), Target.UNKNOWN), entry.getValue(), Target.UNKNOWN);
        }
        env.getEnv(GlobalEnv.class).GetVarList().set(new IVariable(CArray.TYPE, eventObjName, ca, Target.UNKNOWN));
        env.getEnv(GlobalEnv.class).SetEvent(activeEvent);
        activeEvent.addHistory("Triggering bound event: " + this);
        try {
            ProfilePoint p = env.getEnv(GlobalEnv.class).GetProfiler().start("Executing event handler for " + this.getEventName() + " defined at " + this.getTarget(), LogLevel.ERROR);
            try {
                this.execute(env, activeEvent);
            } finally {
                p.stop();
            }
        } catch (ConfigRuntimeException e) {
            // We don't know how to handle this, but we need to set the env,
            // then pass it up the chain
            e.setEnv(env);
            throw e;
        }
    } catch (CloneNotSupportedException ex) {
        Logger.getLogger(BoundEvent.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Also used : IVariable(com.laytonsmith.core.constructs.IVariable) CArray(com.laytonsmith.core.constructs.CArray) CString(com.laytonsmith.core.constructs.CString) ConfigRuntimeException(com.laytonsmith.core.exceptions.ConfigRuntimeException) CString(com.laytonsmith.core.constructs.CString) ProfilePoint(com.laytonsmith.core.profiler.ProfilePoint) Environment(com.laytonsmith.core.environments.Environment) Construct(com.laytonsmith.core.constructs.Construct) GlobalEnv(com.laytonsmith.core.environments.GlobalEnv) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with IVariable

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

the class MathTest method setUp.

@Before
public void setUp() throws Exception {
    StaticTest.InstallFakeServerFrontend();
    fakePlayer = GetOnlinePlayer();
    fakeServer = GetFakeServer();
    varList = new IVariableList();
    varList.set(new IVariable(Auto.TYPE, "var", C.onstruct(1), Target.UNKNOWN));
    varList.set(new IVariable(Auto.TYPE, "var2", C.onstruct(2.5), Target.UNKNOWN));
    env = Static.GenerateStandaloneEnvironment();
    env.getEnv(GlobalEnv.class).SetVarList(varList);
    env.getEnv(CommandHelperEnvironment.class).SetPlayer(fakePlayer);
}
Also used : IVariable(com.laytonsmith.core.constructs.IVariable) IVariableList(com.laytonsmith.core.constructs.IVariableList) CommandHelperEnvironment(com.laytonsmith.core.environments.CommandHelperEnvironment) GlobalEnv(com.laytonsmith.core.environments.GlobalEnv) Before(org.junit.Before)

Example 8 with IVariable

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

the class MathTest method testInc.

@Test(timeout = 10000)
public void testInc() throws Exception {
    Math.inc a = new Math.inc();
    IVariable v = (IVariable) a.exec(Target.UNKNOWN, env, new IVariable(Auto.TYPE, "var", C.onstruct(1), Target.UNKNOWN));
    IVariable v2 = (IVariable) a.exec(Target.UNKNOWN, env, new IVariable(Auto.TYPE, "var2", C.onstruct(2.5), Target.UNKNOWN));
    assertCEquals(C.onstruct(2), v.ival());
    assertCEquals(C.onstruct(3.5), v2.ival());
    StaticTest.SRun("assign(@var, 0) inc(@var, 2) msg(@var)", fakePlayer);
    verify(fakePlayer).sendMessage("2");
}
Also used : IVariable(com.laytonsmith.core.constructs.IVariable) Test(org.junit.Test) StaticTest(com.laytonsmith.testing.StaticTest)

Example 9 with IVariable

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

the class MathTest method testDec.

@Test(timeout = 10000)
public void testDec() throws Exception {
    Math.dec a = new Math.dec();
    IVariable v = (IVariable) a.exec(Target.UNKNOWN, env, new IVariable(Auto.TYPE, "var", C.onstruct(1), Target.UNKNOWN));
    IVariable v2 = (IVariable) a.exec(Target.UNKNOWN, env, new IVariable(Auto.TYPE, "var2", C.onstruct(2.5), Target.UNKNOWN));
    assertCEquals(C.onstruct(0), v.ival());
    assertCEquals(C.onstruct(1.5), v2.ival());
    StaticTest.SRun("assign(@var, 0) dec(@var, 2) msg(@var)", fakePlayer);
    verify(fakePlayer).sendMessage("-2");
}
Also used : IVariable(com.laytonsmith.core.constructs.IVariable) Test(org.junit.Test) StaticTest(com.laytonsmith.testing.StaticTest)

Example 10 with IVariable

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

the class RandomTests method testClone.

@Test
public void testClone() throws CloneNotSupportedException {
    CArray c1 = C.Array(C.Void(), C.Void()).clone();
    CBoolean c2 = C.Boolean(true).clone();
    CDouble c4 = C.Double(1).clone();
    CFunction c5 = new CFunction("", Target.UNKNOWN).clone();
    CInt c6 = C.Int(1).clone();
    CNull c7 = C.Null().clone();
    CString c8 = C.String("").clone();
    Construct c9 = C.Void().clone();
    Command c10 = new Command("/c", Target.UNKNOWN).clone();
    IVariable c12 = new IVariable(Auto.TYPE, "@name", C.Null(), Target.UNKNOWN).clone();
    Variable c13 = new Variable("$name", "", false, false, Target.UNKNOWN);
}
Also used : CInt(com.laytonsmith.core.constructs.CInt) IVariable(com.laytonsmith.core.constructs.IVariable) Variable(com.laytonsmith.core.constructs.Variable) Command(com.laytonsmith.core.constructs.Command) CBoolean(com.laytonsmith.core.constructs.CBoolean) IVariable(com.laytonsmith.core.constructs.IVariable) CArray(com.laytonsmith.core.constructs.CArray) CDouble(com.laytonsmith.core.constructs.CDouble) CFunction(com.laytonsmith.core.constructs.CFunction) Construct(com.laytonsmith.core.constructs.Construct) CNull(com.laytonsmith.core.constructs.CNull) CString(com.laytonsmith.core.constructs.CString) Test(org.junit.Test)

Aggregations

IVariable (com.laytonsmith.core.constructs.IVariable)16 Construct (com.laytonsmith.core.constructs.Construct)10 CString (com.laytonsmith.core.constructs.CString)9 CArray (com.laytonsmith.core.constructs.CArray)8 GlobalEnv (com.laytonsmith.core.environments.GlobalEnv)8 CFunction (com.laytonsmith.core.constructs.CFunction)6 ConfigRuntimeException (com.laytonsmith.core.exceptions.ConfigRuntimeException)6 ConfigCompileException (com.laytonsmith.core.exceptions.ConfigCompileException)5 Variable (com.laytonsmith.core.constructs.Variable)4 Environment (com.laytonsmith.core.environments.Environment)4 ArrayList (java.util.ArrayList)4 ParseTree (com.laytonsmith.core.ParseTree)3 CDouble (com.laytonsmith.core.constructs.CDouble)3 CInt (com.laytonsmith.core.constructs.CInt)3 CNull (com.laytonsmith.core.constructs.CNull)3 IVariableList (com.laytonsmith.core.constructs.IVariableList)3 CommandHelperEnvironment (com.laytonsmith.core.environments.CommandHelperEnvironment)3 ConfigCompileGroupException (com.laytonsmith.core.exceptions.ConfigCompileGroupException)3 FunctionReturnException (com.laytonsmith.core.exceptions.FunctionReturnException)3 FunctionList (com.laytonsmith.core.functions.FunctionList)3