Exemple #1
0
 static RuntimeRealm()
 {
     current = sharedRealm;
     RegisterModule <IntrinsicModule>();
     current = null;
     inited  = true;
 }
Exemple #2
0
 public void Enqueue(RuntimeRealm realm, Action action, Task task)
 {
     Guard.ArgumentNotNull(realm, "realm");
     Guard.ArgumentNotNull(task, "task");
     Guard.ArgumentNotNull(action, "action");
     if (realm.ExecutionContext != this)
     {
         throw new InvalidOperationException("Realm must be belong to the same execution context");
     }
     if (!task.IsCompleted)
     {
         ManualResetEvent handle = new ManualResetEvent(false);
         lock (this) {
             handles.Add(handle);
         }
         task.ContinueWith(_ => {
             Enqueue(realm, action, 0, RuntimeExecutionFlags.None);
             handle.Set();
         });
     }
     else
     {
         Enqueue(realm, action, 0, RuntimeExecutionFlags.None);
     }
     WakeImmediately();
 }
Exemple #3
0
 private RuntimeExecution()
 {
     current         = this;
     defaultRealm    = new RuntimeRealm();
     this.Thread     = Thread.CurrentThread;
     this.CanSuspend = true;
     this.AutoExit   = true;
 }
Exemple #4
0
 private static bool IsValidReference(object value, out SharedObjectHandle handle)
 {
     if (value is Enum p1)
     {
         handle = RuntimeRealm.GetSharedObjectHandle(p1);
         return(true);
     }
     handle = default;
     return(false);
 }
Exemple #5
0
 public void Enqueue(RuntimeRealm realm, Action action)
 {
     Guard.ArgumentNotNull(realm, "realm");
     Guard.ArgumentNotNull(action, "action");
     if (realm.ExecutionContext != this)
     {
         throw new InvalidOperationException("Realm must be belong to the same execution context");
     }
     Enqueue(realm, action, 0, RuntimeExecutionFlags.None);
     WakeImmediately();
 }
Exemple #6
0
 public void Dispose()
 {
     if (!this.Disposed)
     {
         try {
             BeforeDisposed(this, EventArgs.Empty);
         } finally {
             if (current == this)
             {
                 current = null;
             }
             this.Disposed = true;
         }
     }
 }
Exemple #7
0
        public static T CreateFromConstructor <T>(RuntimeObject constructor, SharedObjectHandle defaultProto) where T : RuntimeObject, new()
        {
            RuntimeObject proto = GetPrototypeFromConstructor(constructor, defaultProto);

            if (proto != null)
            {
                currentRealm = proto.Realm;
            }
            T obj;

            try {
                obj = new T();
            } finally {
                currentRealm = null;
            }
            obj.prototype = proto;
            return(obj);
        }
Exemple #8
0
        public void Execute(Action action)
        {
            if (this == sharedRealm)
            {
                throw new InvalidOperationException("Execution in shared realm is disallowed");
            }
            if (Thread.CurrentThread != this.ExecutionContext.Thread)
            {
                throw new InvalidOperationException("Execution in another thread is disallowed");
            }
            RuntimeRealm previous = current;

            try {
                current = this;
                action();
            } finally {
                current = previous;
            }
        }
Exemple #9
0
        public NativeRuntimeFunction(string name, MethodInfo method, bool containUseStrict, WellKnownObject proto)
            : base(proto)
        {
            Guard.ArgumentNotNull(method, "method");
            this.method = method;

            Type runtimeObjectType = null;

            if (method.HasAttribute(out IntrinsicConstructorAttribute attribute))
            {
                constraint        = attribute.Constraint;
                containUseStrict  = true;
                runtimeObjectType = attribute.ObjectType;
                if (attribute.Prototype is Enum protoEnum)
                {
                    defaultProto = RuntimeRealm.GetSharedObjectHandle(protoEnum);
                }
            }
            else if (method.HasAttribute(out IntrinsicMemberAttribute a2))
            {
                constraint       = NativeRuntimeFunctionConstraint.DenyConstruct;
                containUseStrict = true;
                if (name == null)
                {
                    name = a2.Name;
                }
            }
            else
            {
                SetPrototypeInternal(new EcmaObject());
            }
            if (name == null)
            {
                name = "";
            }
            this.Source = "function " + name + "() { [native code] }";
            InitProperty(name, GetFuncLength(method), containUseStrict);
            constructThisValue = runtimeObjectType == null || runtimeObjectType == typeof(EcmaObject) ? createFromConstructorDefault : createFromConstructor.MakeGenericMethod(runtimeObjectType);
        }
Exemple #10
0
        public RuntimeExecutionHandle Enqueue(RuntimeRealm realm, Action action, int milliseconds, RuntimeExecutionFlags flags)
        {
            Guard.ArgumentNotNull(realm, "realm");
            Guard.ArgumentNotNull(action, "action");
            if (realm.ExecutionContext != this)
            {
                throw new InvalidOperationException("Realm must be belong to the same execution context");
            }
            if (milliseconds < 0)
            {
                throw new ArgumentOutOfRangeException("milliseconds", "Delay or interval in milliseconds cannot be negative");
            }
            Job job = new Job {
                Id                    = (flags & RuntimeExecutionFlags.Cancellable) != 0 ? ++nextId : 0,
                Realm                 = realm,
                Action                = action,
                TriggerTime           = milliseconds > 0 ? DateTime.Now.AddMilliseconds(milliseconds) : DateTime.MinValue,
                RecurringMilliseconds = (flags & RuntimeExecutionFlags.Recurring) != 0 ? milliseconds : -1
            };

            queue.Add(job);
            return(new RuntimeExecutionHandle(job.Id));
        }
Exemple #11
0
        public static RuntimeExecution CreateWorkerThread(RuntimeExecutionStart action, bool autoExit)
        {
            ManualResetEvent waitHandle  = new ManualResetEvent(false);
            RuntimeRealm     parentRealm = RuntimeRealm.Current;
            RuntimeExecution execution   = null;
            Thread           thread      = new Thread(() => {
                execution = EnsureInstance();
                execution.parentWaitHandle = waitHandle;
                execution.AutoExit         = autoExit;
                try {
                    action(parentRealm);
                    execution.AwakeParentThread();
                    ContinueUntilEnd();
                } catch (Exception ex) {
                    SendUnhandledException(ex);
                    execution.AwakeParentThread();
                }
            });

            thread.IsBackground = true;
            thread.Start();
            waitHandle.WaitOne();
            return(execution);
        }