Exemplo n.º 1
0
        public void TestModules()
        {
            // This is a repeat of the C++ test, just for good measure.

            string brainUid    = System.Guid.NewGuid().ToString();
            string agentUid    = System.Guid.NewGuid().ToString();
            string brainSource = @"
function updateAgent(state) {
  state.floats[1] = getVoosModule('FooMath')['transform'](state.floats[0]);
  state.floats[2] = getVoosModule('BarMath')['transform'](state.floats[0]);
}
";

            Debug.Assert(V8InUnity.Native.ResetBrain(brainUid, brainSource));

            Debug.Assert(V8InUnity.Native.SetModule(brainUid, "FooMath", @"
export function transform(x) { return 2*x; }
"));

            Debug.Assert(V8InUnity.Native.SetModule(brainUid, "BarMath", @"
export function transform(x) { return 3*x; }
"));
            TestAgentRequest req;

            req.floats = new float[] { 4f };
            var maybeResponse = V8InUnity.Native.UpdateAgent <TestAgentRequest, TestAgentRequest>(brainUid, agentUid, req, new Native.UpdateCallbacks());

            Debug.Assert(!maybeResponse.IsEmpty());
            TestAgentRequest res = maybeResponse.Get();

            Debug.Assert(res.floats[1] == 8f);
            Debug.Assert(res.floats[2] == 12f);
        }
Exemplo n.º 2
0
        public void TestActorStringAccessors()
        {
            // Do this test several times, since there's lots of caching in this code path.
            for (int i = 0; i < 5; i++)
            {
                string brainUid = System.Guid.NewGuid().ToString();
                string agentUid = System.Guid.NewGuid().ToString();
                Debug.Log($"strting test for {brainUid}");
                actorStringValue = $"€ {brainUid} €";
                string brainSource = @"
          function updateAgent(obj) {
            const prev = getActorString(12, 34);
            sysLog(`got string: ${prev}`);
            if(prev != '€ " + brainUid + @" €') throw 'ahhh';
            setActorString(12, 34, 'from JS € " + agentUid + @" € euros €');
          }";
                Debug.Assert(V8InUnity.Native.ResetBrain(brainUid, brainSource));

                var req = new TestAgentRequest {
                    floats = new float[0]
                };
                var callbacks = new Native.UpdateCallbacks
                {
                    getActorString = TestActorStringGetter,
                    setActorString = TestActorStringSetter
                };
                var maybeResponse = V8InUnity.Native.UpdateAgent <TestAgentRequest, TestAgentResponse>(brainUid, agentUid, req, callbacks);
                Debug.Assert(!maybeResponse.IsEmpty());
                Assert.AreEqual("from JS € " + agentUid + " € euros €", actorStringValue);

                Debug.Log($"OK succeeded once");
            }
        }
Exemplo n.º 3
0
        public void TestSetActorStringUndefined()
        {
            string brainUid = System.Guid.NewGuid().ToString();
            string agentUid = System.Guid.NewGuid().ToString();

            actorStringValue = "overwrite me";
            string brainSource = @"
          function updateAgent(obj) {
            setActorString(12, 34, undefined);
          }";

            Debug.Assert(V8InUnity.Native.ResetBrain(brainUid, brainSource));

            var req = new TestAgentRequest {
                floats = new float[0]
            };
            var callbacks = new Native.UpdateCallbacks
            {
                getActorString = TestActorStringGetter,
                setActorString = TestActorStringSetter
            };
            var maybeResponse = V8InUnity.Native.UpdateAgent <TestAgentRequest, TestAgentResponse>(brainUid, agentUid, req, callbacks);

            Debug.Assert(!maybeResponse.IsEmpty());
            Assert.AreEqual("", actorStringValue);
        }
Exemplo n.º 4
0
        public void TestAgentUpdateByteArray()
        {
            string brainUid    = System.Guid.NewGuid().ToString();
            string agentUid    = System.Guid.NewGuid().ToString();
            string brainSource = @"
function updateAgent(dummy, buffer) {
  const v = new DataView(buffer);
  if(v.getInt8(0) != 43) throw 'ahhh';
  v.setInt8(0, 124);
}
";

            Debug.Assert(V8InUnity.Native.ResetBrain(brainUid, brainSource));

            byte[] bytes = new byte[1];
            bytes[0] = 43;
            TestAgentRequest req = new TestAgentRequest();
            var maybeResponse    = V8InUnity.Native.UpdateAgent <TestAgentRequest, TestAgentResponse>(brainUid, agentUid, req, bytes, new Native.UpdateCallbacks());

            Debug.Assert(!maybeResponse.IsEmpty());
            Assert.AreEqual(124, bytes[0]);
        }