private JsValue ToString(JsValue thisObj, JsValue[] arguments) { var func = thisObj.TryCast<FunctionInstance>(); if (func == null) throw new JavaScriptException(Engine.TypeError, "Function object expected."); var officialName = _method.GetOfficialName(); return string.Format("function {0} () {{ [native code] }}", officialName); }
public static PropertyDescriptor ToPropertyDescriptor(Engine engine, JsValue o) { var obj = o.TryCast<ObjectInstance>(); if (obj == null) { throw new JavaScriptException(engine.TypeError); } if ((obj.HasProperty("value") || obj.HasProperty("writable")) && (obj.HasProperty("get") || obj.HasProperty("set"))) { throw new JavaScriptException(engine.TypeError); } var desc = new PropertyDescriptor(); if (obj.HasProperty("enumerable")) { desc.Enumerable = TypeConverter.ToBoolean(obj.Get("enumerable")); } if (obj.HasProperty("configurable")) { desc.Configurable = TypeConverter.ToBoolean(obj.Get("configurable")); } if (obj.HasProperty("value")) { var value = obj.Get("value"); desc.Value = value; } if (obj.HasProperty("writable")) { desc.Writable = TypeConverter.ToBoolean(obj.Get("writable")); } if (obj.HasProperty("get")) { var getter = obj.Get("get"); if (getter != JsValue.Undefined && getter.TryCast<ICallable>() == null) { throw new JavaScriptException(engine.TypeError); } desc.Get = getter; } if (obj.HasProperty("set")) { var setter = obj.Get("set"); if (setter != Native.Undefined.Instance && setter.TryCast<ICallable>() == null) { throw new JavaScriptException(engine.TypeError); } desc.Set = setter; } if (desc.Get.HasValue || desc.Get.HasValue) { if (desc.Value.HasValue || desc.Writable.HasValue) { throw new JavaScriptException(engine.TypeError); } } return desc; }
public static Types GetPrimitiveType(JsValue value) { if (value.IsObject()) { var primitive = value.TryCast<IPrimitiveInstance>(); if (primitive != null) { return primitive.Type; } return Types.Object; } return value.Type; }
/// <summary> /// Invoke the current value as function. /// </summary> /// <param name="value">The function to call.</param> /// <param name="thisObj">The this value inside the function call.</param> /// <param name="arguments">The arguments of the function call.</param> /// <returns>The value returned by the function call.</returns> public JsValue Invoke(JsValue value, object thisObj, object[] arguments) { var callable = value.TryCast<ICallable>(); if (callable == null) { throw new ArgumentException("Can only invoke functions"); } return callable.Call(JsValue.FromObject(this, thisObj), arguments.Select(x => JsValue.FromObject(this, x)).ToArray()); }