public NativeDescriptor MarshalFieldInfo( FieldInfo prop, JsDictionaryObject owner) { JsGetter getter; JsSetter setter; if (prop.IsLiteral) { JsInstance value = (JsInstance)null; getter = (JsGetter)(that => { if (value == null) { value = (JsInstance)typeof(Marshaller).GetMethod("MarshalClrValue").MakeGenericMethod(prop.FieldType).Invoke((object)this, new object[1] { prop.GetValue((object)null) }); } return(value); }); setter = (JsSetter)((that, v) => {}); } else { getter = this.WrapGetField(prop); setter = this.WrapSetField(prop); } NativeDescriptor nativeDescriptor = new NativeDescriptor(owner, prop.Name, getter, setter); nativeDescriptor.Enumerable = true; return(nativeDescriptor); }
/// <summary> /// Marshals a native field to a JS Descriptor /// </summary> /// <param name="prop">Field info to marshal</param> /// <param name="owner">Owner for the descriptor</param> /// <returns>Descriptor</returns> public NativeDescriptor MarshalFieldInfo(FieldInfo prop, JsDictionaryObject owner) { JsGetter getter; JsSetter setter; if (prop.IsLiteral) { JsInstance value = null; // this demand initization should prevent a stack overflow while reflecting types getter = delegate(JsDictionaryObject that) { if (value == null) { value = (JsInstance)typeof(Marshaller) .GetMethod("MarshalClrValue") .MakeGenericMethod(prop.FieldType) .Invoke(this, new object[] { prop.GetValue(null) }); } return(value); }; setter = delegate(JsDictionaryObject that, JsInstance v) { }; } else { getter = (JsGetter)WrapGetField(prop); setter = (JsSetter)WrapSetField(prop); } return(new NativeDescriptor(owner, prop.Name, getter, setter) { Enumerable = true }); }
/// <summary> /// Marshals a JsInstance to a native value. /// </summary> /// <typeparam name="T">A native object type</typeparam> /// <param name="value">A JsInstance to marshal</param> /// <returns>A converted native velue</returns> public T MarshalJsValue <T>(JsInstance value) { if (value.Value is T) { return((T)value.Value); } else { if (typeof(T).IsArray) { if (value == null || value == JsUndefined.Instance || value == JsNull.Instance) { return(default(T)); } if (m_global.ArrayClass.HasInstance(value as JsObject)) { Delegate marshller; if (!m_arrayMarshllers.TryGetValue(typeof(T), out marshller)) { m_arrayMarshllers[typeof(T)] = marshller = Delegate.CreateDelegate( typeof(JintFunc <JsObject, T>), this, typeof(Marshaller) .GetMethod("MarshalJsFunctionHelper") .MakeGenericMethod(typeof(T).GetElementType()) ); } return(((JintFunc <JsObject, T>)marshller)(value as JsObject)); } else { throw new JintException("Array is required"); } } else if (typeof(Delegate).IsAssignableFrom(typeof(T))) { if (value == null || value == JsUndefined.Instance || value == JsNull.Instance) { return(default(T)); } if (!(value is JsFunction)) { throw new JintException("Can't convert a non function object to a delegate type"); } return((T)MarshalJsFunctionHelper(value as JsFunction, typeof(T))); } else if (value != JsNull.Instance && value != JsUndefined.Instance && value is T) { return((T)(object)value); } else { // JsNull and JsUndefined will fall here and become a nulls return((T)Convert.ChangeType(value.Value, typeof(T))); } } }
public object MarshalJsValueBoxed <T>(JsInstance value) { if (value.Value is T) { return(value.Value); } return((object)null); }
static JsInstance[] GetStubs(params object[] args) { JsInstance[] p = new JsInstance[args.Length]; for (int i = 0; i < args.Length; i++) { p[i] = new JsStub(args[i]); } return(p); }
public object CallFunction(string name, params object[] args) { JsInstance result = this.visitor.Result; this.visitor.Visit(new Identifier(name)); object obj = this.CallFunction((JsFunction)this.visitor.Result, args); this.visitor.Result = result; return(obj); }
public object CallFunction(string name, params object[] args) { JsInstance oldResult = Visitor.Result; Visitor.Visit(new Identifier(name)); var returnValue = CallFunction((JsFunction)Visitor.Result, args); Visitor.Result = oldResult; return(returnValue); }
/// <summary> /// Marshals a JsInstance to a native value. /// </summary> /// <typeparam name="T">A native object type</typeparam> /// <param name="value">A JsInstance to marshal</param> /// <returns>A converted native velue</returns> public T MarshalJsValue <T>(JsInstance value) { if (value == JsNull.Instance || value == JsUndefined.Instance) { if (!typeof(T).IsValueType || typeof(T).IsGenericType && typeof(T).GetGenericTypeDefinition() == typeof(Nullable <>)) { return(default(T)); } throw new JintException(string.Format("Cannot cast null value to {0}", typeof(T).Name)); } if (value.Value is T) { return((T)value.Value); } if (typeof(T).IsArray) { if (!m_global.ArrayClass.HasInstance(value as JsObject)) { throw new JintException("Array is required"); } Delegate marshller; if (!m_arrayMarshllers.TryGetValue(typeof(T), out marshller)) { m_arrayMarshllers[typeof(T)] = marshller = Delegate.CreateDelegate( typeof(Delegates.Func <JsObject, T>), this, typeof(Marshaller) .GetMethod("MarshalJsFunctionHelper") .MakeGenericMethod(typeof(T).GetElementType()) ); } return(((Delegates.Func <JsObject, T>)marshller)(value as JsObject)); } if (typeof(Delegate).IsAssignableFrom(typeof(T))) { if (!(value is JsFunction)) { throw new JintException("Can't convert a non function object to a delegate type"); } return((T)MarshalJsFunctionHelper(value as JsFunction, typeof(T))); } if (value is T) { return((T)(object)value); } return((T)Convert.ChangeType(value.Value, typeof(T))); }
public Type GetInstanceType(JsInstance value) { if (value == null || value == JsUndefined.Instance || value == JsNull.Instance) { return((Type)null); } if (value.Value != null) { return(value.Value.GetType()); } return(value.GetType()); }
public object Invoke(object[] parameters) { JsInstance[] arguments = new JsInstance[parameters.Length]; for (int i = 0; i < parameters.Length; i++) { arguments[i] = Visitor.Global.WrapClr(parameters[i]); } Visitor.ExecuteFunction(Function, That, arguments); return(Visitor.Returned == null ? null : Visitor.Returned.Value); }
/// <summary> /// Marshals a JsInstance to a native value. /// </summary> /// <typeparam name="T">A native object type</typeparam> /// <param name="value">A JsInstance to marshal</param> /// <returns>A converted native velue</returns> public T MarshalJsValue <T>(JsInstance value) { if (value.Value is T) { return((T)value.Value); } else { if (typeof(T).IsArray) { if (value == null || value == JsUndefined.Instance || value == JsNull.Instance) { return(default(T)); } if (m_global.ArrayClass.HasInstance(value as JsObject)) { Delegate marshller; if (!m_arrayMarshllers.TryGetValue(typeof(T), out marshller)) { throw new NotSupportedException(); } return(((Jint.Delegates.Func <JsObject, T>)marshller)(value as JsObject)); } else { throw new JintException("Array is required"); } } else if (typeof(Delegate).IsAssignableFrom(typeof(T))) { if (value == null || value == JsUndefined.Instance || value == JsNull.Instance) { return(default(T)); } if (!(value is JsFunction)) { throw new JintException("Can't convert a non function object to a delegate type"); } throw new NotSupportedException(); } else if (value != JsNull.Instance && value != JsUndefined.Instance && value is T) { return((T)(object)value); } else { // JsNull and JsUndefined will fall here and become a nulls return((T)Convert.ChangeType(value.Value, typeof(T))); } } }
public bool IsVarInScope(string varName) { try { JsInstance i = Visitor.CurrentScope[varName]; if (i == null || i.Class.Equals("Undefined")) { return(false); } return(true); } catch (Exception) { } return(false); }
public T MarshalJsValue <T>(JsInstance value) { if (value.Value is T) { return((T)value.Value); } if (typeof(T).IsArray) { if (value == null || value == JsUndefined.Instance || value == JsNull.Instance) { return(default(T)); } if (!this.m_global.ArrayClass.HasInstance(value as JsObject)) { throw new JintException("Array is required"); } Delegate delegate1; if (!this.m_arrayMarshllers.TryGetValue(typeof(T), out delegate1)) { Dictionary <Type, Delegate> arrayMarshllers = this.m_arrayMarshllers; Type index = typeof(T); Type type = typeof(Func <JsObject, T>); MethodInfo method = typeof(Marshaller).GetMethod("MarshalJsFunctionHelper").MakeGenericMethod(typeof(T).GetElementType()); Delegate delegate2; delegate1 = delegate2 = Delegate.CreateDelegate(type, (object)this, method); arrayMarshllers[index] = delegate2; } return(((Func <JsObject, T>)delegate1)(value as JsObject)); } if (typeof(Delegate).IsAssignableFrom(typeof(T))) { if (value == null || value == JsUndefined.Instance || value == JsNull.Instance) { return(default(T)); } if (!(value is JsFunction)) { throw new JintException("Can't convert a non function object to a delegate type"); } return((T)this.MarshalJsFunctionHelper(value as JsFunction, typeof(T))); } if (value != JsNull.Instance && value != JsUndefined.Instance && value is T) { return((T)value); } return((T)Convert.ChangeType(value.Value, typeof(T))); }
public JsInstance Return(JsInstance result) { throw new NotImplementedException(); }
public void Serializer(XmlNode node, bool isSerialize) { XmlNode se = Project.GetChildNode(node, "scriptengine"); if (se == null) { return; } XmlNode p = Project.GetChildNode(se, "properties"); if (p == null) { return; } if (isSerialize) { p.RemoveAll(); foreach (string s in Visitor.CurrentScope.GetKeys()) { if (s != null) { try { if (String.IsNullOrWhiteSpace(s)) { continue; } Boolean ignore = false; foreach (string sv in SystemVars) { if (s.StartsWith(sv)) { ignore = true; break; } } if (ignore) { continue; } JsInstance i = Visitor.CurrentScope[s]; if (i != null && i.Value != null) { string v = i.Value.ToString(); // This might through an exception. XmlNode c = Project.GetChildNode(p, "property", s); if (c != null) { Project.SetNodeAttributeValue(c, "type", i.Class); Project.SetNodeAttributeValue(c, "value", v); } } } catch (Exception) { } } } } else { XmlNodeList nodeList = Project.GetNodes(se, "properties/*"); foreach (XmlNode c in nodeList) { if (c != null) { string id = Project.GetNodeAttributeValue(c, "name", ""); if (!String.IsNullOrWhiteSpace(id)) { string typeName = Project.GetNodeAttributeValue(c, "type", "String"); string value = Project.GetNodeAttributeValue(c, "value", null); if (value != null && typeName != null) { if (typeName.Equals("Number")) { double d = 0.0f; if (Double.TryParse(value, out d)) { SetParameter(id, d); } } else if (typeName.Equals("Int32")) { try { int n = Convert.ToInt32(value, 10); SetParameter(id, n); } catch (Exception) { } } else if (typeName.Equals("Boolean")) { try { bool n = Convert.ToBoolean(value); SetParameter(id, n); } catch (Exception) { } } else { SetParameter(id, value); } } } } } } }
private RavenJToken ToRavenJToken(JsInstance v) { switch (v.Class) { case JsInstance.TYPE_OBJECT: case JsInstance.CLASS_OBJECT: return(ToRavenJObject((JsObject)v)); case JsInstance.CLASS_DATE: var dt = (DateTime)v.Value; return(new RavenJValue(dt)); case JsInstance.TYPE_NUMBER: case JsInstance.CLASS_NUMBER: var num = (double)v.Value; var integer = Math.Truncate(num); if (Math.Abs(num - integer) < double.Epsilon) { return(new RavenJValue((long)integer)); } return(new RavenJValue(num)); case JsInstance.TYPE_STRING: case JsInstance.TYPE_BOOLEAN: case JsInstance.CLASS_STRING: case JsInstance.CLASS_BOOLEAN: return(new RavenJValue(v.Value)); case JsInstance.CLASS_NULL: case JsInstance.TYPE_NULL: return(RavenJValue.Null); case JsInstance.CLASS_UNDEFINED: case JsInstance.TYPE_UNDEFINED: return(RavenJValue.Null); case JsInstance.CLASS_ARRAY: var jsArray = ((JsArray)v); var rja = new RavenJArray(); for (int i = 0; i < jsArray.Length; i++) { var jsInstance = jsArray.get(i); var ravenJToken = ToRavenJToken(jsInstance); if (ravenJToken == null) { continue; } rja.Add(ravenJToken); } return(rja); case JsInstance.CLASS_REGEXP: case JsInstance.CLASS_ERROR: case JsInstance.CLASS_ARGUMENTS: case JsInstance.CLASS_DESCRIPTOR: case JsInstance.CLASS_FUNCTION: return(null); default: throw new NotSupportedException(v.Class); } }
public override void Set(JsDictionaryObject that, JsInstance value) { this.d.Set(that, value); }
public override void Set(JsDictionaryObject that, JsInstance value) { _entityAccessor.SetValue(that.Value, Name, value.Value); }
private static RavenJToken ToRavenJToken(JsInstance v, string propertyName) { switch (v.Class) { case JsInstance.TYPE_OBJECT: case JsInstance.CLASS_OBJECT: return(ToRavenJObject((JsObject)v)); case JsInstance.CLASS_DATE: var dt = (DateTime)v.Value; return(new RavenJValue(dt)); case JsInstance.TYPE_NUMBER: case JsInstance.CLASS_NUMBER: var num = (double)v.Value; JTokenType type; if (propertiesTypeByName.TryGetValue(propertyName, out type)) { if (type == JTokenType.Float) { return(new RavenJValue(num)); } if (type == JTokenType.Integer) { return(new RavenJValue((long)num)); } } // If we don't have the type, assume that if the number ending with ".0" it actually an integer. var integer = Math.Truncate(num); if (Math.Abs(num - integer) < double.Epsilon) { return(new RavenJValue((long)integer)); } return(new RavenJValue(num)); case JsInstance.TYPE_STRING: case JsInstance.TYPE_BOOLEAN: case JsInstance.CLASS_STRING: case JsInstance.CLASS_BOOLEAN: return(new RavenJValue(v.Value)); case JsInstance.CLASS_NULL: case JsInstance.TYPE_NULL: return(RavenJValue.Null); case JsInstance.CLASS_UNDEFINED: case JsInstance.TYPE_UNDEFINED: return(RavenJValue.Null); case JsInstance.CLASS_ARRAY: var jsArray = ((JsArray)v); var rja = new RavenJArray(); for (int i = 0; i < jsArray.Length; i++) { var jsInstance = jsArray.get(i); var ravenJToken = ToRavenJToken(jsInstance, propertyName); if (ravenJToken == null) { continue; } rja.Add(ravenJToken); } return(rja); case JsInstance.CLASS_REGEXP: case JsInstance.CLASS_ERROR: case JsInstance.CLASS_ARGUMENTS: case JsInstance.CLASS_DESCRIPTOR: case JsInstance.CLASS_FUNCTION: return(null); default: throw new NotSupportedException(v.Class); } }
private RavenJToken ToRavenJToken(JsInstance v) { switch (v.Class) { case JsInstance.TYPE_OBJECT: case JsInstance.CLASS_OBJECT: return(ToRavenJObject((JsObject)v)); case JsInstance.CLASS_DATE: var dt = (DateTime)v.Value; return(new RavenJValue(dt)); case JsInstance.TYPE_NUMBER: case JsInstance.CLASS_NUMBER: var num = (double)v.Value; KeyValuePair <RavenJValue, object> property; if (propertiesByValue.TryGetValue(v, out property)) { var originalValue = property.Key; if (originalValue.Type == JTokenType.Float) { return(new RavenJValue(num)); } if (originalValue.Type == JTokenType.Integer) { // If the current value is exactly as the original value, we can return the original value before we made the JS conversion, // which will convert a Int64 to jsFloat. var originalJsValue = property.Value; if (originalJsValue is double && Math.Abs(num - (double)originalJsValue) < double.Epsilon) { return(originalValue); } return(new RavenJValue((long)num)); } } // If we don't have the type, assume that if the number ending with ".0" it actually an integer. var integer = Math.Truncate(num); if (Math.Abs(num - integer) < double.Epsilon) { return(new RavenJValue((long)integer)); } return(new RavenJValue(num)); case JsInstance.TYPE_STRING: case JsInstance.TYPE_BOOLEAN: case JsInstance.CLASS_STRING: case JsInstance.CLASS_BOOLEAN: return(new RavenJValue(v.Value)); case JsInstance.CLASS_NULL: case JsInstance.TYPE_NULL: return(RavenJValue.Null); case JsInstance.CLASS_UNDEFINED: case JsInstance.TYPE_UNDEFINED: return(RavenJValue.Null); case JsInstance.CLASS_ARRAY: var jsArray = ((JsArray)v); var rja = new RavenJArray(); for (int i = 0; i < jsArray.Length; i++) { var jsInstance = jsArray.get(i); var ravenJToken = ToRavenJToken(jsInstance); if (ravenJToken == null) { continue; } rja.Add(ravenJToken); } return(rja); case JsInstance.CLASS_REGEXP: case JsInstance.CLASS_ERROR: case JsInstance.CLASS_ARGUMENTS: case JsInstance.CLASS_DESCRIPTOR: case JsInstance.CLASS_FUNCTION: return(null); default: throw new NotSupportedException(v.Class); } }
public override void Set(JsDictionaryObject that, JsInstance value) { _entityAccessor.SetValue(that.Value, Name, value.Value); }