public async void ProcessFrame(FrameData frame) { if (callbacks.Count == 0 || busy) { return; } else { busy = true; } RecognitionServer.Inst.RecognizePose(frame.bitmap, (x) => { var o = JsonObject.Parse(x); var detectedPoses = o["PoseDetection"].GetArray(); ProjectRuntime.Inst.DispatchRuntimeCode(() => { var poses = JavaScriptValue.CreateArray(0); var pushFunc = poses.GetProperty(JavaScriptPropertyId.FromString("push")); foreach (var item in detectedPoses) { var pose = item.GetObject(); var id = pose["PoseID"].GetNumber(); var score = pose["PoseScore"].GetNumber(); var keypoints = pose["Keypoints"].GetArray(); var newKeypoints = new JsonObject(); foreach (var jtem in keypoints) { var keypoint = jtem.GetObject().First(); var defaultPosition = new JsonArray(); defaultPosition.Add(JsonValue.CreateNumberValue(0)); defaultPosition.Add(JsonValue.CreateNumberValue(0)); defaultPosition.Add(JsonValue.CreateNumberValue(0)); var position = keypoint.Value.GetObject().GetNamedArray("Position", defaultPosition); var pos = GetEstimatedPositionFromPosition(new Vector3((float)position.GetNumberAt(0), (float)position.GetNumberAt(1), (float)position.GetNumberAt(2)), frame.bitmap); //var pos = GetEstimatedPositionFromPosition(new Vector3(1, 1, 1), frame.bitmap); var newKeypoint = new JsonObject(); var newPosition = new JsonArray(); newPosition.Add(JsonValue.CreateNumberValue(pos.X)); newPosition.Add(JsonValue.CreateNumberValue(pos.Y)); newPosition.Add(JsonValue.CreateNumberValue(pos.Z)); newKeypoint.Add("position", newPosition); newKeypoint.Add("score", JsonValue.CreateNumberValue(keypoint.Value.GetObject().GetNamedNumber("Score", 0.5))); //newKeypoint.Add("score", JsonValue.CreateNumberValue(0)); newKeypoints.Add(keypoint.Key, newKeypoint); } var jsPose = JavaScriptContext.RunScript($"new Pose({id}, {score}, {newKeypoints.Stringify()});"); pushFunc.CallFunction(poses, jsPose); } foreach (var callback in callbacks) { callback.CallFunction(callback, poses); } }); busy = false; }); }
public void Init() { JavaScriptNativeFunction printFunc = PrintFunc; registeredFunctions.Add(printFunc); JavaScriptNativeFunction httpFunc = JsXmlHttpRequest.JsConstructor; registeredFunctions.Add(httpFunc); using (new JavaScriptContext.Scope(context)) { var printFuncName = JavaScriptPropertyId.FromString("print"); var printFuncObj = JavaScriptValue.CreateFunction(printFunc); JavaScriptValue.GlobalObject.SetProperty(printFuncName, printFuncObj, true); var logObj = JavaScriptValue.CreateObject(); var logFuncName = JavaScriptPropertyId.FromString("log"); logObj.SetProperty(logFuncName, printFuncObj, true); var consoleName = JavaScriptPropertyId.FromString("console"); JavaScriptValue.GlobalObject.SetProperty(consoleName, logObj, true); var xmlHttpRequestName = JavaScriptPropertyId.FromString("XMLHttpRequest"); var httpFuncObj = JavaScriptValue.CreateFunction(httpFunc); JavaScriptValue.GlobalObject.SetProperty(xmlHttpRequestName, httpFuncObj, true); } }
public static oResult test_2(Dictionary <string, object> request = null) { oResult rs = new oResult() { ok = false, request = request }; using (new JavaScriptContext.Scope(js_context)) { string script = "(()=>{ var val = ___api.test('TEST_2: Hello world'); ___api.log('api-js.test-2', 'key-2', 'TEST_2: This log called by JS...'); return val; })()"; try { JavaScriptValue result = JavaScriptContext.RunScript(script, CURRENT_SOURCE_CONTEXT++, string.Empty); rs.data = result.ConvertToString().ToString(); rs.ok = true; } catch (JavaScriptScriptException e) { var messageName = JavaScriptPropertyId.FromString("message"); rs.error = "ERROR_JS_EXCEPTION: " + e.Error.GetProperty(messageName).ConvertToString().ToString(); } catch (Exception e) { rs.error = "ERROR_CHAKRA: failed to run script: " + e.Message; } } return(rs); }
/// <summary> /// For object constructed in JS, we need to create a C# representation and bind it to that /// object. For this, we'll use a <see cref="Dictionary{TKey,TValue}"/>. Possibly a later addition /// could use a custom serialization process and allow the user to control. /// </summary> private Dictionary <string, object> NewHostObject(JavaScriptValue arg) { // Create new Dictionary mapping to hold the properties, Create a new bindable JS object to replace // the existing one. Note: This could cause issues if these objects are used as keys. var d = new Dictionary <string, object>(); var replacement = _binder.BindObject(d); var propNames = (string[])ToHostArray(arg.GetOwnPropertyNames(), typeof(string[])); for (var i = 0; i < propNames.Length; ++i) { var propName = propNames[i]; var propId = JavaScriptPropertyId.FromString(propName); var jsProp = arg.GetProperty(propId); // Copy Properties into Replacement replacement.SetProperty(propId, jsProp, true); Type propType; if (!TryInferType(jsProp, out propType)) { throw new Exception($"Failed to create Host representation of JS object. Property: {propName}"); } d[propName] = ToHostObject(jsProp, propType); } return(d); }
private static void DefineHostCallback(JavaScriptValue anObject, string callbackName, JavaScriptNativeFunction callback, IntPtr callbackData) { JavaScriptPropertyId propertyId = JavaScriptPropertyId.FromString(callbackName); JavaScriptValue function = JavaScriptValue.CreateFunction(callback, callbackData); anObject.SetProperty(propertyId, function, true); }
public object CallFunctionWithCustomThis(object thiz, string functionName, params object[] parameters) { //ChakraHost.EnterContext(); if (!(thiz is JavaScriptValue javaScriptValue)) { return(null); } var func = ChakraHost.GlobalObject.GetProperty( JavaScriptPropertyId.FromString(functionName)); object result = null; switch (func.ValueType) { case JavaScriptValueType.Function: { var param = new [] { javaScriptValue }.Concat(parameters.Select(it => it.ToJavaScriptValue())).ToArray(); result = func.CallFunction(param).ToNative(); } break; } //ChakraHost.LeaveContext(); return(result); }
/// <summary> /// /// </summary> /// <param name="name"></param> /// <param name="value"></param> /// <param name="type"></param> public void SetValue(string name, object value, Type type) { // TODO: Manage these cases in JsInterop? Possibly another "Binding Filter" to strip down the // TODO: core and wrapper instances into pure js objects if (typeof(JavaScriptNativeFunction) == type) { AddFunction(name, (JavaScriptNativeFunction)value); return; } if (typeof(JsBinding) == type) { var binding = (JsBinding)value; _scope.Run(() => { _value.SetProperty(JavaScriptPropertyId.FromString(name), binding.Object, true); }); return; } _scope.Run(() => { var jsValue = _interop.ToJsObject(value, type); _value.SetProperty(JavaScriptPropertyId.FromString(name), jsValue, true); }); }
JavaScriptValue ObjectToJavaScriptValue(Object parameter) { if (parameter == null) { return(JavaScriptValue.Null); } else if (parameter is String) { return(JavaScriptValue.FromString(parameter.ToString())); } else if (parameter is Boolean) { return(JavaScriptValue.FromBoolean((Boolean)parameter)); } else if (parameter is Int32) { return(JavaScriptValue.FromInt32((Int32)parameter)); } else if (parameter is Double) { return(JavaScriptValue.FromDouble((Int32)parameter)); } else if (parameter is Object) { String json = JsonConvert.SerializeObject(parameter); var glob = JavaScriptValue.GlobalObject.GetProperty(JavaScriptPropertyId.FromString("JSON")); var parse = glob.GetProperty(JavaScriptPropertyId.FromString("parse")); return(parse.CallFunction(JavaScriptValue.Undefined, JavaScriptValue.FromString(json))); } return(JavaScriptValue.Undefined); }
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; } }
public void AddType(JavaScriptValue parent, string name, Type type) { // Create the function that will call the class constructor var constructor = JavaScriptValue.CreateFunction(new JavaScriptNativeFunction((callee, isConstructCall, arguments, argumentCount, callbackData) => { return(ToJavaScriptValue(Activator.CreateInstance(type))); })); // Add static functions (e.g Console.WriteLine) if (type.IsClass && type != typeof(string)) { // Get the properties and methods var methods = type.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Static) .Where(m => !m.IsSpecialName && m.IsPrivate == false).ToList(); // Loop through all methods foreach (var method in methods) { SetMethod(constructor, method, null); } } // Set the property var childPropertyId = JavaScriptPropertyId.FromString(name); parent.SetProperty(childPropertyId, constructor, true); }
public void RegisterObject(string name, object obj) { var type = obj.GetType(); using (new JavaScriptContext.Scope(context)) { var jsObj = JavaScriptValue.CreateObject(); foreach (var m in type.GetMethods()) { var attrib = m.GetCustomAttribute <JsExport>(); if (attrib == null) { continue; } var funcName = attrib.JavaScriptMethodName ?? m.Name; var funcNameProp = JavaScriptPropertyId.FromString(funcName); var funcDelegate = (JavaScriptNativeFunction)Delegate.CreateDelegate(typeof(JavaScriptNativeFunction), obj, m); registeredFunctions.Add(funcDelegate); var funcObj = JavaScriptValue.CreateFunction(funcDelegate); jsObj.SetProperty(funcNameProp, funcObj, true); } var nameProp = JavaScriptPropertyId.FromString(name); JavaScriptValue.GlobalObject.SetProperty(nameProp, jsObj, true); } }
private static bool IsNotFatalJsException(JavaScriptValue exception) { var code = JavaScriptPropertyId.FromString("Code"); return(exception.ValueType == JavaScriptValueType.Object && exception.HasProperty(code) && exception.GetProperty(code).ConvertToString().ToString().Equals("ArmLanguageFunction")); }
private static bool IsPromise(JavaScriptValue value) { var id = JavaScriptPropertyId.FromString("then"); return(value.HasProperty(id) && value.GetProperty(id).ValueType == JavaScriptValueType.Function); }
private static string GetExceptionString(JavaScriptValue exception) { var code = JavaScriptPropertyId.FromString("Code"); if (exception.ValueType == JavaScriptValueType.Error) { JavaScriptPropertyId messageName = JavaScriptPropertyId.FromString("message"); JavaScriptValue messageValue = exception.GetProperty(messageName); string message = messageValue.ToString(); double column = -1, line = -1; var lineProp = JavaScriptPropertyId.FromString("line"); var colProp = JavaScriptPropertyId.FromString("column"); if (exception.HasProperty(lineProp)) { line = exception.GetProperty(lineProp).ConvertToNumber().ToDouble(); } if (exception.HasProperty(colProp)) { column = exception.GetProperty(lineProp).ConvertToNumber().ToDouble(); } return(string.Format("{0}, at {1}:{2}", message, (int)line, (int)column)); } if (exception.ValueType == JavaScriptValueType.Object && exception.HasProperty(code)) { return(exception.GetProperty(code).ConvertToString().ToString()); } return(string.Format("{0}", exception.ConvertToString().ToString())); }
static void _define(JavaScriptValue globalObject, string callbackName, JavaScriptNativeFunction callback) { JavaScriptPropertyId propertyId = JavaScriptPropertyId.FromString(callbackName); JavaScriptValue function = JavaScriptValue.CreateFunction(callback, IntPtr.Zero); globalObject.SetProperty(propertyId, function, true); m_apis.Add(callbackName); }
public static void AttachProperty(JavaScriptValue module, JavaScriptValue property, string id) { if (Native.JsSetProperty(module, JavaScriptPropertyId.FromString(id), property, false) != JavaScriptErrorCode.NoError) { throw new Exception("Failed to attach property"); } }
public static void AttachModule(JavaScriptValue module, AbstractJSModule subModule) { if (Native.JsSetProperty(module, JavaScriptPropertyId.FromString(subModule.GetId()), subModule.GetModule(), false) != JavaScriptErrorCode.NoError) { throw new Exception("Failed to attach module"); } }
private void SetConsoleLogObject(JavaScriptValue globalObject) { Native.JsCreateObject(out var consoleObject); Native.JsCreateFunction(ConsoleLog, IntPtr.Zero, out var functionValue); consoleObject.SetProperty(JavaScriptPropertyId.FromString("log"), functionValue, true); globalObject.SetProperty(JavaScriptPropertyId.FromString("console"), consoleObject, true); }
private void SetTimeoutFunctions(JavaScriptValue globalObject) { Native.JsCreateFunction(SetTimeout, IntPtr.Zero, out var setTimeoutFunction); globalObject.SetProperty(JavaScriptPropertyId.FromString("setTimeout"), setTimeoutFunction, true); Native.JsCreateFunction(ClearTimeout, IntPtr.Zero, out var clearTimeoutFunction); globalObject.SetProperty(JavaScriptPropertyId.FromString("clearTimeout"), clearTimeoutFunction, true); }
private static JavaScriptValue Format(JavaScriptValue callee, bool isConstructCall, JavaScriptValue[] arguments, ushort argumentCount, IntPtr callbackData) { JavaScriptValue obj = arguments[1]; string pathName = JSValToString(obj.GetProperty(JavaScriptPropertyId.FromString("pathname"))).Replace("\\", "/"); string protocol = JSValToString(obj.GetProperty(JavaScriptPropertyId.FromString("protocol"))); return(JavaScriptValue.FromString(protocol + "//" + pathName)); }
private void InstallNativeRequire() { _nativeRequire = NativeRequire; EnsureGlobalObject().SetProperty( JavaScriptPropertyId.FromString("nativeRequire"), JavaScriptValue.CreateFunction(_nativeRequire), true); }
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(); }); }); }
public void Send(string body) { if (client == null || requestMessage == null) { return; } if (requestTask != null) { return; } requestTask = Task.Run(async() => { try { var responseHeaders = await client.SendRequestAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead); ProjectRuntime.Inst.DispatchRuntimeCode(() => { objRef.SetProperty(JavaScriptPropertyId.FromString("status"), JavaScriptValue.FromInt32((int)responseHeaders.StatusCode), true); string statusText = $"{(int)responseHeaders.StatusCode} {responseHeaders.ReasonPhrase}"; objRef.SetProperty(JavaScriptPropertyId.FromString("statusText"), JavaScriptValue.FromString(statusText), true); }); ReadyState = ReadyStates.Headers_Received; // ReadyState = ReadyStates.Loading; // ignore, we don't do partial responses var content = await responseHeaders.Content.ReadAsStringAsync(); ProjectRuntime.Inst.DispatchRuntimeCode(() => { objRef.SetProperty(JavaScriptPropertyId.FromString("responseType"), JavaScriptValue.FromString("text"), true); objRef.SetProperty(JavaScriptPropertyId.FromString("response"), JavaScriptValue.FromString(content), true); objRef.SetProperty(JavaScriptPropertyId.FromString("responseText"), JavaScriptValue.FromString(content), true); var callback = objRef.Get("onload"); if (callback.ValueType == JavaScriptValueType.Function) { callback.CallFunction(callback); } }); } catch (Exception e) { ProjectRuntime.Inst.DispatchRuntimeCode(() => { var callback = objRef.Get("onerror"); if (callback.ValueType == JavaScriptValueType.Function) { callback.CallFunction(callback); } }); } ReadyState = ReadyStates.Done; requestMessage = null; requestTask = null; }); /* * cancellationTokenSource = new CancellationTokenSource(); * requestTask = Task.Run(async () => { * var response = await request;//.AsTask(cancellationTokenSource.Token); * //response. * int dsa = 0; * }); */ }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="name"></param> /// <returns></returns> public T GetValue <T>(string name) { return(_scope.Run(() => { var property = _value.GetProperty(JavaScriptPropertyId.FromString(name)); return (T)_interop.ToHostObject(property, typeof(T)); })); }
/// <summary> /// This method binds a function to a specific field on a JavaScript object. /// </summary> /// <param name="name">The name of the field.</param> /// <param name="jsFunction">The callback function to execute when the JavaScript function is invoked.</param> public void AddFunction(string name, JavaScriptNativeFunction jsFunction) { _scope.Run(() => { var jsValue = _binder.BindFunction(jsFunction); _value.SetProperty(JavaScriptPropertyId.FromString(name), jsValue, true); }); }
private void PrintScriptException(JavaScriptValue exception) { // Get message. JavaScriptPropertyId messageName = JavaScriptPropertyId.FromString("message"); JavaScriptValue messageValue = exception.GetProperty(messageName); string message = messageValue.ToString(); Log(string.Format("chakrahost: exception: {0}", message)); }
private void DefineCallback (JavaScriptValue hostObject, string callbackName, JavaScriptNativeFunction callbackDelegate) { var propertyId = JavaScriptPropertyId.FromString(callbackName); var function = JavaScriptValue.CreateFunction(callbackDelegate); hostObject.SetProperty(propertyId, function, true); }
public void RegisterGlobalFunction(string name, JavaScriptNativeFunction func) { registeredFunctions.Add(func); using (new JavaScriptContext.Scope(context)) { var funcName = JavaScriptPropertyId.FromString(name); var funcObj = JavaScriptValue.CreateFunction(func); JavaScriptValue.GlobalObject.SetProperty(funcName, funcObj, true); } }
private JToken VisitError(JavaScriptValue value) { return(new JObject { { "message", Visit(value.GetProperty(JavaScriptPropertyId.FromString("message"))) }, { "description", Visit(value.GetProperty(JavaScriptPropertyId.FromString("description"))) }, { "stack", Visit(value.GetProperty(JavaScriptPropertyId.FromString("stack"))) }, }); }
private static string GetMessage(JavaScriptValue error) { var messageProperty = JavaScriptPropertyId.FromString("message"); var message = error.HasProperty(messageProperty) ? error.GetProperty(messageProperty).ConvertToString().ToString() : "Promise rejected."; return(message); }