コード例 #1
0
        public void RecognizeFaces(JavaScriptValue faces, JavaScriptValue callback)
        {
            var boundList = new List <BitmapBounds>();

            for (int i = 0; i < faces.Length().Value; ++i)
            {
                var jsBounds = faces.Get(i).Get("bounds");
                var bounds   = new BitmapBounds()
                {
                    X      = (uint)jsBounds.Get("x").ToInt32(),
                    Y      = (uint)jsBounds.Get("y").ToInt32(),
                    Width  = (uint)jsBounds.Get("width").ToInt32(),
                    Height = (uint)jsBounds.Get("height").ToInt32()
                };
                boundList.Add(bounds);
            }

            int frameID = faces.Get(0).Get("frame").Get("id").ToInt32();
            var frame   = SceneCameraManager.Inst.GetFrameFromCache(frameID);

            callback.AddRef();
            faces.AddRef();

            server.RecognizeFaces(frame.bitmap, boundList, (s) => {
                JsonObject json;
                if (!JsonObject.TryParse(s, out json))
                {
                    ProjectRuntime.Inst.DispatchRuntimeCode(() => {
                        for (int i = 0; i < faces.Length().Value; ++i)
                        {
                            faces.Get(i).SetProperty(JavaScriptPropertyId.FromString("name"), JavaScriptValue.FromString("Unknown"), true);
                        }
                        callback.CallFunction(callback, faces);
                        callback.Release();
                        faces.Release();
                    });
                    return;
                }

                var responses = json.GetNamedArray("ResponsePerFace");
                var names     = new List <string>();
                for (int i = 0; i < responses.Count; ++i)
                {
                    var faceResponse = responses.GetObjectAt((uint)i);
                    names.Add(faceResponse.GetNamedString("FaceRecognition"));
                }

                ProjectRuntime.Inst.DispatchRuntimeCode(() => {
                    for (int i = 0; i < faces.Length().Value; ++i)
                    {
                        faces.Get(i).SetProperty(JavaScriptPropertyId.FromString("name"), JavaScriptValue.FromString(names[i]), true);
                    }
                    callback.CallFunction(callback, faces);
                    callback.Release();
                    faces.Release();
                });
            });
        }
コード例 #2
0
        private static void ProcessIntervals()
        {
            // ToList will snapshot the keys, so we don't run new timers created by these timers in this loop
            foreach (string guid in intervalTimers.Keys.ToList())
            {
                // handle things in this current function call removing other timers
                if (!intervalTimers.ContainsKey(guid))
                {
                    continue;
                }

                TimerCallback timerCallback = intervalTimers[guid];
                if (!timerCallback.IsTimeUp())
                {
                    continue;
                }

                JavaScriptValue callback = timerCallback.callback;
                try {
                    callback.CallFunction(JavaScriptValue.Undefined);
                } catch (Exception e) {
                    UnityEngine.Debug.LogError(e);
                }

                timerCallback.stopwatch.Restart();
            }
        }
コード例 #3
0
        private static Func <JavaScriptValue, string> Inspect()
        {
            return((jsValue) => {
                switch (jsValue.ValueType)
                {
                case JavaScriptValueType.String:
                    return jsValue.ToString();

                case JavaScriptValueType.Boolean:
                case JavaScriptValueType.Null:
                case JavaScriptValueType.Number:
                case JavaScriptValueType.Symbol:
                case JavaScriptValueType.Undefined:
                    return JavaScript.API.Inspect.GetString(jsValue);

                default:
                    // It's safe to call GetProperty on all other value types
                    JavaScriptValue valueToPrint = jsValue;
                    JavaScriptValue toStringProperty = jsValue.GetProperty("toString");
                    if (
                        toStringProperty.IsValid &&
                        toStringProperty.ValueType == JavaScriptValueType.Function &&
                        // Object and array prototype toStrings are kinda useless; they just return eg [object Object] or array values joined with commas.
                        // Don't bother calling those, should use Inspect instead.
                        toStringProperty != JavaScriptValue.GlobalObject.GetProperty("Object").GetProperty("prototype").GetProperty("toString") &&
                        toStringProperty != JavaScriptValue.GlobalObject.GetProperty("Array").GetProperty("prototype").GetProperty("toString")
                        )
                    {
                        valueToPrint = toStringProperty.CallFunction(jsValue);
                    }
                    return JavaScript.API.Inspect.GetString(valueToPrint);
                }
            });
        }
コード例 #4
0
        private void CallInstanceMethod(string name)
        {
            if (!jsInstance.HasValue)
            {
                return;
            }


            Engine.WithIfRunning(() => {
                JavaScriptValue method = jsInstance.Value.GetProperty(name);
                if (method.ValueType == JavaScriptValueType.Function)
                {
                    method.CallFunction(jsInstance.Value);
                }

                JavaScriptValue _delegates = jsInstance.Value.GetProperty("_delegates");
                foreach (JavaScriptValue instance in _delegates.ToList())
                {
                    JavaScriptValue delegateMethod = instance.GetProperty(name);
                    if (delegateMethod.ValueType == JavaScriptValueType.Function)
                    {
                        delegateMethod.CallFunction(instance);
                    }
                }
            });
        }
コード例 #5
0
 public Object RunScript(String script, Object parameter)
 {
     try
     {
         JavaScriptValue jsScript = JavaScriptContext.ParseScript(script);
         if (jsScript.ValueType == JavaScriptValueType.Function)
         {
             var jsResult = jsScript.CallFunction(JavaScriptValue.Undefined);
             if (jsResult.ValueType == JavaScriptValueType.Function)
             {
                 jsResult = jsResult.CallFunction(JavaScriptValue.Undefined, ObjectToJavaScriptValue(parameter));
             }
             return(JavaScriptValueToObject(jsResult));
         }
         return(null);
     }
     catch (JavaScriptScriptException ex)
     {
         var msg = ex.Error.GetProperty(JavaScriptPropertyId.FromString("message"));
         throw new JSRuntimeException(msg.ToString());
     }
     catch (Exception)
     {
         throw;
     }
 }
コード例 #6
0
        public static void ProcessUpdate()
        {
            while (csharpCallback.Count != 0)
            {
                Action fn = csharpCallback.Dequeue();
                fn();
            }

            while (oneUpdateCallbacks.Count != 0)
            {
                JavaScriptValue fn = oneUpdateCallbacks.Dequeue();
                try {
                    fn.CallFunction(JavaScriptValue.Undefined);
                } catch (Exception e) {
                    Debug.LogError(e);
                } finally {
                    fn.Release();
                }
            }

            foreach (var pair in updateCallbacks)
            {
                try {
                    pair.Value.CallFunction(JavaScriptValue.Undefined);
                } catch (Exception e) {
                    Debug.LogError(e);
                }
            }
        }
コード例 #7
0
        public void Execute(JavaScriptValue function)
        {
            if (function.ValueType != JavaScriptValueType.Function)
            {
                return;
            }

            using (new JavaScriptContext.Scope(context)) {
                function.CallFunction(function);
            }
        }
コード例 #8
0
        /// <summary>
        ///     A promise continuation callback.
        /// </summary>
        /// <remarks>
        ///     The host can specify a promise continuation callback in <c>JsSetPromiseContinuationCallback</c>. If
        ///     a script creates a task to be run later, then the promise continuation callback will be called with
        ///     the task and the task should be put in a FIFO queue, to be run when the current script is
        ///     done executing.
        /// </remarks>
        /// <param name="task">The task, represented as a JavaScript function.</param>
        /// <param name="callbackState">The data argument to be passed to the callback.</param>
        /// <see cref="JavaScriptPromiseContinuationCallback"></see>
        private void PromiseContinuationCallback(JavaScriptValue task, IntPtr callbackState)
        {
            task.AddRef();

            try
            {
                task.CallFunction(JavaScriptValue.GlobalObject);
            }
            finally
            {
                task.Release();
            }
        }
コード例 #9
0
        public static void ProcessPromiseTaskQueue() // called inside With()
        {
            while (promiseTaskQueue.Count != 0)
            {
                JavaScriptValue task = promiseTaskQueue.Dequeue();

                try {
                    task.CallFunction(JavaScriptValue.Undefined);
                } catch (Exception e) {
                    Debug.LogError(e);
                }

                task.Release();
            }
        }
コード例 #10
0
 public void LoadLibrary(String script)
 {
     try
     {
         JavaScriptValue jsLib = JavaScriptContext.ParseScript(script);
         if (jsLib.ValueType == JavaScriptValueType.Function)
         {
             jsLib.CallFunction(JavaScriptValue.Undefined);
         }
     }
     catch (JavaScriptScriptException ex)
     {
         var msg = ex.Error.GetProperty(JavaScriptPropertyId.FromString("message"));
         throw new JSRuntimeException(msg.ToString());
     }
     catch (Exception)
     {
         throw;
     }
 }
コード例 #11
0
ファイル: JsCallback.cs プロジェクト: enklu/orchid
        /// <summary>
        /// Attempts to invoke the callback. Any exception will result in flagging a context error.
        /// </summary>
        private object TryInvoke(JavaScriptValue[] values)
        {
            try
            {
                var result = _callback.CallFunction(values);

                if (_interop.TryInferType(result, out var returnType))
                {
                    return(_interop.ToHostObject(result, returnType));
                }

                return(_interop.ToHostObject(result, typeof(object)));
            }
            catch (Exception e)
            {
                if (e is JavaScriptScriptException)
                {
                    var jse = (JavaScriptScriptException)e;
                    throw new Exception(jse.Error.GetProperty(JavaScriptPropertyId.FromString("message")).ToString());
                }

                throw e;
            }
        }
コード例 #12
0
 public override void Run()
 {
     taskFunc.CallFunction(JavaScriptValue.GlobalObject);
 }
コード例 #13
0
        public void GetEmotionForFaces(JavaScriptValue faces, JavaScriptValue callback)
        {
            if (RecognitionServer.Inst.IsBusy)
            {
                return;
            }

            var boundList = new List <BitmapBounds>();

            for (int i = 0; i < faces.Length().Value; ++i)
            {
                var jsBounds = faces.Get(i).Get("bounds");
                var bounds   = new BitmapBounds()
                {
                    X      = (uint)jsBounds.Get("x").ToInt32(),
                    Y      = (uint)jsBounds.Get("y").ToInt32(),
                    Width  = (uint)jsBounds.Get("width").ToInt32(),
                    Height = (uint)jsBounds.Get("height").ToInt32()
                };
                boundList.Add(bounds);
            }

            int frameID = faces.Get(0).Get("frame").Get("id").ToInt32();
            var frame   = SceneCameraManager.Inst.GetFrameFromCache(frameID);

            callback.AddRef();
            faces.AddRef();

            server.RecognizeEmotions(frame.bitmap, boundList, (s) => {
                JsonObject json;
                if (!JsonObject.TryParse(s, out json))
                {
                    ProjectRuntime.Inst.DispatchRuntimeCode(() => {
                        var emotions = JavaScriptValue.CreateArray(0);
                        var pushFunc = emotions.GetProperty(JavaScriptPropertyId.FromString("push"));
                        for (var i = 0; i < faces.Length().Value; ++i)
                        {
                            pushFunc.CallFunction(faces, JavaScriptValue.FromString("Unknown"));
                        }
                        callback.CallFunction(callback, faces);
                        callback.Release();
                        faces.Release();
                    });
                    return;
                }

                var responses = json.GetNamedArray("ResponsePerFace");
                ProjectRuntime.Inst.DispatchRuntimeCode(() => {
                    var emotions = JavaScriptValue.CreateArray(0);
                    var pushFunc = emotions.GetProperty(JavaScriptPropertyId.FromString("push"));
                    for (int i = 0; i < faces.Length().Value; ++i)
                    {
                        var emotion = responses.GetObjectAt((uint)i).GetNamedString("Emotion");
                        pushFunc.CallFunction(emotions, JavaScriptValue.FromString(emotion));
                    }
                    callback.CallFunction(callback, emotions);
                    callback.Release();
                    faces.Release();
                });
            });
        }
コード例 #14
0
        } // Register

        static Action <UnityWebRequest> MakeHttpCallback(JavaScriptValue callback, bool wantsArrayBuffer = false)
        {
            return((www) => {
                if (www.isNetworkError || www.isHttpError)
                {
                    Engine.With(() => {
                        try {
                            callback.CallFunction(
                                JavaScriptValue.Undefined,
                                JavaScriptValue.FromString(www.error),
                                JavaScriptValue.Null
                                );
                        } catch (Exception e) {
                            Debug.LogError(e);
                        } finally {
                            callback.Release();
                        }
                    });
                    return;
                }

                if (!wantsArrayBuffer)
                {
                    string text = www.downloadHandler.text;
                    Engine.With(() => {
                        try {
                            callback.CallFunction(
                                JavaScriptValue.Undefined,
                                JavaScriptValue.Null,
                                JavaScriptValue.FromString(text)
                                );
                        } catch (Exception e) {
                            Debug.LogError(e);
                        } finally {
                            callback.Release();
                        }
                    });
                }
                else // ArrayBuffer
                {
                    byte[] bytes = www.downloadHandler.data;
                    IntPtr unmanagedPointer = Marshal.AllocHGlobal(bytes.Length);
                    Marshal.Copy(bytes, 0, unmanagedPointer, bytes.Length);

                    Engine.With(() => {
                        Debug.Log("created ptr " + unmanagedPointer);
                        JavaScriptValue buffer = JavaScriptValue.CreateExternalArrayBuffer(
                            unmanagedPointer,
                            (uint)bytes.Length,
                            (ptr) => {
                            Debug.Log("freeing " + ptr);
                            Marshal.FreeHGlobal(ptr);
                        },
                            unmanagedPointer // pass it in since the upvalue will die! (probably)
                            );
                        try {
                            callback.CallFunction(
                                JavaScriptValue.Undefined,
                                JavaScriptValue.Null,
                                buffer
                                );
                        } catch (Exception e) {
                            Debug.LogError(e);
                        } finally {
                            callback.Release();
                        }
                    });
                }
            });
        }