/// <summary> /// Get a global variable value from the JavaScript context /// </summary> /// <param name="name">The variable name</param> /// <param name="result">The output value</param> /// <returns></returns> private bool GetMember(string name, out object result) { if (name.EndsWith("()")) { var script = "{0}".format(name); result = this.Run(script); } else { result = _javaScriptContextImplementation.GetParameter(name); } if (_javaScriptContextImplementation.IsJavaScriptDate(result)) { result = _javaScriptContextImplementation.JavaScriptDateToNETDateTime(result); } // If the value returned is a IDictionary<string, object> AKA a JavaScript // object we return it as a JavaScriptInstance so we can handle the dynamic // aspect of it in C# else if (_javaScriptContextImplementation.IsJavaScriptObject(result)) { var dic = _javaScriptContextImplementation.JavaScriptObjectToNETDictionary(result); dynamic d = new DynamicJavaScriptInstance(dic); result = d; } else // Convert some value type into .net value type. Jurassic support string and concatenated string // which we need to convert into string on the fly. this is why we have this call { result = _javaScriptContextImplementation.JavaScriptValueTypeToNETValueType(result); } return(true); }
/// <summary> /// Run the script and return the last value evaluated. Executing a declaration function /// or a global object literal, will load the function or object in the JavaScript context. /// </summary> /// <param name="script"></param> /// <returns> /// </returns> public object Run(string script) { var o = this._javaScriptContextImplementation.Run(script); if (o != null) { if (_javaScriptContextImplementation.IsJavaScriptArray(o)) { o = _javaScriptContextImplementation.JavaScriptArrayToNETArray(o); } else if (_javaScriptContextImplementation.IsJavaScriptObject(o)) { var dic = _javaScriptContextImplementation.JavaScriptObjectToNETDictionary(o); dynamic d = new DynamicJavaScriptInstance(dic); o = d; } } return(o); }
public void SetParameter(string name, object value) { if (value == null) { this._javascriptContext.SetGlobalValue(name, Jurassic.Null.Value); } else if (value.GetType().IsArray) { var arrayValue = DynamicJavaScriptInstance.MakeDynamicObjectArray(value); var arrayInstance = new ArrayInstance(this._javascriptContext.Object.InstancePrototype, 0, 0); foreach (var v in arrayValue) { arrayInstance.Push(v); } this._javascriptContext.SetGlobalValue(name, arrayInstance); } else { this._javascriptContext.SetGlobalValue(name, value); } }
/// <summary> /// Get the value of a property using the property syntax /// </summary> /// <param name="binder"></param> /// <param name="result"></param> /// <returns></returns> public override bool TryGetMember(GetMemberBinder binder, out object result) { if (!_dic.ContainsKey(binder.Name)) { throw new ApplicationException(String.Format("property '{0}' not defined in object", binder.Name)); } result = _dic[binder.Name]; if (___globalJavaScriptContext.IsJavaScriptDate(result)) { result = ___globalJavaScriptContext.JavaScriptDateToNETDateTime(result); } // If we have to return an array we replace all the element of the array that // are an object by our wrapper JavaScriptInstance around the object. // So we can continue to get the C# dynamic syntax if (___globalJavaScriptContext.IsJavaScriptArray(result)) { result = MakeDynamicObjectArray( ___globalJavaScriptContext.JavaScriptArrayToNETArray(result) ); } // If the value returned a JavaScript object we return it as a // JavaScriptInstance so we can handle the dynamic aspect of it in C# else if (___globalJavaScriptContext.IsJavaScriptObject(result)) { var dic = ___globalJavaScriptContext.JavaScriptObjectToNETDictionary(result); dynamic d = new DynamicJavaScriptInstance(dic); result = d; } else { result = ___globalJavaScriptContext.JavaScriptValueTypeToNETValueType(result); } return(true); }
/// <summary> /// Get the value of a property using the property syntax /// </summary> /// <param name="binder"></param> /// <param name="result"></param> /// <returns></returns> public override bool TryGetMember(GetMemberBinder binder, out object result) { if(!_dic.ContainsKey(binder.Name)) throw new ApplicationException(String.Format("property '{0}' not defined in object",binder.Name)); result = _dic[binder.Name]; if(___globalJavaScriptContext.IsJavaScriptDate(result)){ result = ___globalJavaScriptContext.JavaScriptDateToNETDateTime(result); } // If we have to return an array we replace all the element of the array that // are an object by our wrapper JavaScriptInstance around the object. // So we can continue to get the C# dynamic syntax if(___globalJavaScriptContext.IsJavaScriptArray(result)){ result = MakeDynamicObjectArray( ___globalJavaScriptContext.JavaScriptArrayToNETArray(result) ); } // If the value returned a JavaScript object we return it as a // JavaScriptInstance so we can handle the dynamic aspect of it in C# else if(___globalJavaScriptContext.IsJavaScriptObject(result)){ var dic = ___globalJavaScriptContext.JavaScriptObjectToNETDictionary(result); dynamic d = new DynamicJavaScriptInstance(dic); result = d; } else{ result = ___globalJavaScriptContext.JavaScriptValueTypeToNETValueType(result); } return true; }
/// <summary> /// Run the script and return the last value evaluated. Executing a declaration function /// or a global object literal, will load the function or object in the JavaScript context. /// </summary> /// <param name="script"></param> /// <returns> /// </returns> public object Run(string script){ var o = this._javaScriptContextImplementation.Run(script); if(o!=null){ if(_javaScriptContextImplementation.IsJavaScriptArray(o)){ o = _javaScriptContextImplementation.JavaScriptArrayToNETArray(o); } else if (_javaScriptContextImplementation.IsJavaScriptObject(o)) { var dic = _javaScriptContextImplementation.JavaScriptObjectToNETDictionary(o); dynamic d = new DynamicJavaScriptInstance(dic); o = d; } } return o; }
/// <summary> /// Get a global variable value from the JavaScript context /// </summary> /// <param name="name">The variable name</param> /// <param name="result">The output value</param> /// <returns></returns> private bool GetMember(string name, out object result) { if(name.EndsWith("()")){ var script = "{0}".format(name); result = this.Run(script); } else{ result = _javaScriptContextImplementation.GetParameter(name); } if (_javaScriptContextImplementation.IsJavaScriptDate(result)) { result = _javaScriptContextImplementation.JavaScriptDateToNETDateTime(result); } // If the value returned is a IDictionary<string, object> AKA a JavaScript // object we return it as a JavaScriptInstance so we can handle the dynamic // aspect of it in C# else if (_javaScriptContextImplementation.IsJavaScriptObject(result)) { var dic = _javaScriptContextImplementation.JavaScriptObjectToNETDictionary(result); dynamic d = new DynamicJavaScriptInstance(dic); result = d; } else { // Convert some value type into .net value type. Jurassic support string and concatenated string // which we need to convert into string on the fly. this is why we have this call result = _javaScriptContextImplementation.JavaScriptValueTypeToNETValueType(result); } return true; }