示例#1
0
        public static EcmaValue ClearInterval(EcmaValue id)
        {
            RuntimeExecutionHandle handle = new RuntimeExecutionHandle(id == default ? 0 : id.ToNumber().ToInt32());

            RuntimeExecution.Cancel(handle);
            return(EcmaValue.Undefined);
        }
示例#2
0
        public static Promise FromTask <T>(Task <T> task)
        {
            Guard.ArgumentNotNull(task, "task");
            Promise promise = new Promise();

            RuntimeExecution.Enqueue(() => promise.HandleCompletedTask(task), task);
            return(promise);
        }
示例#3
0
 public static void It(string message, Action tests)
 {
     try {
         StaticHelper.Logs.Clear();
         Assert.message = "It " + message;
         tests();
         RuntimeExecution.ContinueUntilEnd();
     } finally {
         Assert.message = null;
     }
 }
示例#4
0
 private void SetStateFinalize(PromiseState state, EcmaValue value)
 {
     if (this.State == PromiseState.Pending)
     {
         this.State = state;
         this.Value = value;
         hooks?.Invoke(this);
         if (hooks == null && state == PromiseState.Rejected)
         {
             RuntimeExecution.SendUnhandledException(value);
         }
         hooks = null;
     }
 }
示例#5
0
        public static EcmaValue SetInterval(EcmaValue fn, EcmaValue milliseconds, params EcmaValue[] args)
        {
            int timeout = milliseconds == default ? 0 : milliseconds.ToNumber().ToInt32();

            if (timeout > 0)
            {
                timeout = 0;
            }
            if (timeout >= 0)
            {
                timeout = System.Math.Max(4, timeout);
            }
            RuntimeExecutionHandle handle = RuntimeExecution.Enqueue(() => fn.Call(Undefined, args), timeout, RuntimeExecutionFlags.Cancellable | RuntimeExecutionFlags.Recurring);

            return(handle.Id);
        }
示例#6
0
        private void SetState(PromiseState state, EcmaValue value, bool finalize)
        {
            PromiseCallback callback = state == PromiseState.Fulfilled ? fulfilledCallback : rejectedCallback;

            fulfilledCallback = null;
            rejectedCallback  = null;
            if (finalize || callback == null)
            {
                SetStateFinalize(state, value);
            }
            else
            {
                RuntimeExecution.Enqueue(() => {
                    try {
                        value = callback(value);
                        ResolveSelf(value, true);
                    } catch (Exception ex) {
                        SetStateFinalize(PromiseState.Rejected, EcmaValueUtility.GetValueFromException(ex));
                    }
                });
            }
        }
示例#7
0
        public void ObjectHosting()
        {
            It("should return different instances on different execution thread", () => {
                TestObject obj   = new TestObject();
                RuntimeObject v0 = RuntimeRealm.Current.ResolveRuntimeObject(obj);

                RuntimeExecution.CreateWorkerThread(parentRealm => {
                    Assume.That(parentRealm, Is.Not.EqualTo(RuntimeRealm.Current), "realm should be different");

                    RuntimeObject v1 = RuntimeRealm.Current.ResolveRuntimeObject(obj);
                    That(v0 != v1, "returned object should be different");
                    That(v0.Realm, Is.EqualTo(parentRealm));
                    That(v1.Realm, Is.EqualTo(RuntimeRealm.Current));

                    That(v0.GetPrototypeOf() != v1.GetPrototypeOf(), "the prototype of returned object should be different");
                    That(v0.GetPrototypeOf().Realm, Is.EqualTo(parentRealm));
                    That(v1.GetPrototypeOf().Realm, Is.EqualTo(RuntimeRealm.Current));

                    That(v0.GetPrototypeOf().GetPrototypeOf() != v1.GetPrototypeOf().GetPrototypeOf(), "all prototype ancestors of returned object should be different");

                    That(v0["MyMethod"], Is.TypeOf("function"));
                    That(v0["MyMethod"], Is.Not.EqualTo(v1["MyMethod"]), "property value of object type should be different");
                }, true).Thread.Join();
            });

            It("should return different instances on different realm of the same execution thread", () => {
                TestObject obj           = new TestObject();
                RuntimeObject v0         = RuntimeRealm.Current.ResolveRuntimeObject(obj);
                RuntimeRealm parentRealm = RuntimeRealm.Current;
                RuntimeRealm other       = new RuntimeRealm();

                other.Enqueue(() => {
                    Assume.That(RuntimeRealm.Current, Is.Not.EqualTo(parentRealm), "realm should be different");
                    Assume.That(RuntimeRealm.Current, Is.EqualTo(other));

                    RuntimeObject v1 = RuntimeRealm.Current.ResolveRuntimeObject(obj);
                    That(v0 != v1, "returned object should be different");
                    That(v0.Realm, Is.EqualTo(parentRealm));
                    That(v1.Realm, Is.EqualTo(RuntimeRealm.Current));

                    That(v0.GetPrototypeOf() != v1.GetPrototypeOf(), "the prototype of returned object should be different");
                    That(v0.GetPrototypeOf().Realm, Is.EqualTo(parentRealm));
                    That(v1.GetPrototypeOf().Realm, Is.EqualTo(RuntimeRealm.Current));

                    That(v0.GetPrototypeOf().GetPrototypeOf() != v1.GetPrototypeOf().GetPrototypeOf(), "all prototype ancestors of returned object should be different");

                    That(v0["MyMethod"], Is.TypeOf("function"));
                    That(v0["MyMethod"], Is.Not.EqualTo(v1["MyMethod"]), "property value of object type should be different");
                });
                RuntimeExecution.ContinueUntilEnd();
            });

            It("should expose native properties as getters and setters on protoype", () => {
                TestObject obj  = new TestObject();
                EcmaValue value = new EcmaValue(obj);
                value["Value"]  = 1;

                That(value["Value"], Is.EqualTo(1));
                That(obj.Value, Is.EqualTo(1), "Value should be reflected on native object");
                That(() => value["ReadOnlyValue"] = 2, Throws.Nothing, "Setting a readonly property does not throw exception");
                That(value["ReadOnlyValue"], Is.EqualTo(1), "Value should not be changed");

                RuntimeObject testObjectPrototype = value.ToObject().GetPrototypeOf();
                That(testObjectPrototype.GetOwnProperty("Value"), Is.Not.EqualTo(EcmaValue.Undefined), "Property should be defined on the reflected prototype object");
                That(testObjectPrototype.GetOwnProperty("Value").Get, Is.Not.EqualTo(EcmaValue.Undefined), "Property should be defined as getter/setter");
                That(testObjectPrototype.GetOwnProperty("ReadOnlyValue").Set, Is.Undefined, "Readonly property should have no setter");
            });

            It("should expose IList as an Array exotic object", () => {
                EcmaValue list = new EcmaValue(new List <object> {
                    1, 2, 3, new EcmaObject()
                });
                That(list["length"], Is.EqualTo(4));
                That(list.ToString(), Is.EqualTo("1,2,3,[object Object]"));
                That(Global.Json.Invoke("stringify", list), Is.EqualTo("[1,2,3,{}]"));
            });

            It("should expose IDictionary as an ordinary object for valid EcmaPropertyKey", () => {
                Hashtable ht  = new Hashtable();
                EcmaValue obj = new EcmaValue(ht);
                obj["prop"]   = 1;
                That(obj["prop"], Is.EqualTo(1));
                That(Global.Json.Invoke("stringify", obj), Is.EqualTo("{\"prop\":1}"));
            });
        }
示例#8
0
 public bool WaitOne(int milliseconds)
 {
     return(RuntimeExecution.Suspend(handle, milliseconds) || Interlocked.Decrement(ref counter) != 0);
 }