protected override PhpCallable BindCore(Context ctx) { PhpTypeInfo tinfo; object target = null; if ((target = _item1.AsObject()) != null) { tinfo = target.GetPhpTypeInfo(); } else { tinfo = ctx.ResolveType(_item1.ToString(ctx), _callerCtx, true); } if (tinfo != null) { var method = _item2.ToString(ctx); var routine = (PhpMethodInfo)tinfo.RuntimeMethods[method]; if (routine != null) { if (target != null) { return(routine.PhpInvokable.Bind(target)); } else { // calling the method statically if (routine.Methods.All(TypeMembersUtils.IsStatic)) { return(routine.PhpCallable); } else { // consider: compiler (and this binder) creates dummy instance of self; // can we create a special singleton instance marked as "null" so use of $this inside the method will fail ? // TODO: use caller instance or warning (calling instance method statically) return(routine.PhpInvokable.Bind(tinfo.GetUninitializedInstance(ctx))); } } } else { // __call // __callStatic routine = (PhpMethodInfo)tinfo.RuntimeMethods[(target != null) ? TypeMethods.MagicMethods.__call : TypeMethods.MagicMethods.__callstatic]; if (routine != null) { return(routine.PhpInvokable.BindMagicCall(target, method)); } } } return(null); }
/// <summary> /// Invoked by runtime when PHP assertion fails. /// </summary> protected virtual void AssertFailed(PhpValue action = default) { // exception to be thrown var userexception = action.IsSet ? action.AsObject() as Exception : null; var usermessage = action.IsSet ? action.AsString() : null; var exception = userexception ?? PhpException.AssertionErrorException(usermessage ?? string.Empty); throw exception; }
void ResolveType(Context ctx, out PhpTypeInfo tinfo, out object target) { if ((target = _item1.AsObject()) != null) { tinfo = target.GetPhpTypeInfo(); } else { tinfo = ctx.ResolveType(_item1.ToString(ctx), _callerCtx, true); } }
static bool EqualsObj(PhpValue a, PhpValue b) { // avoid incomparable object comparison var targetSelf = a.AsObject(); var targetOther = b.AsObject(); if (targetSelf != null) { return(ReferenceEquals(targetSelf, targetOther)); } if (targetOther != null) { return(false); } // return(a == b); }
public static PhpCallback Create(PhpValue item1, PhpValue item2, RuntimeTypeHandle callerCtx = default, object callerObj = null) { if (item2.IsString(out var method)) { if (item1.AsObject() != null || item1.IsString()) { // creates callback from an array, // array entries must be dereferenced so they cannot be changed gainst return(new ArrayCallback(item1.GetValue(), method, callerCtx) { Target = callerObj }); } } // return(CreateInvalid()); }
/// <summary> /// Gets <see cref="PhpTypeInfo"/> from a string or an object instance. /// </summary> /// <param name="ctx">Current runtime context.</param> /// <param name="object">String or object. Other value types cause an exception.</param> /// <returns>Corresponding <see cref="PhpTypeInfo"/> descriptor. Cannot be <c>null</c>.</returns> public static PhpTypeInfo TypeNameOrObjectToType(Context ctx, PhpValue @object) { object obj; string str; if ((obj = (@object.AsObject())) != null) { return(obj.GetType().GetPhpTypeInfo()); } else if ((str = PhpVariable.AsString(@object)) != null) { return(ctx.GetDeclaredType(str, true)); } else { throw new ArgumentException(); } }
/// <summary> /// Invoked by runtime when PHP assertion fails. /// </summary> protected virtual void AssertFailed(PhpValue action = default(PhpValue)) { const string AssertionErrorName = "AssertionError"; var t_assertex = GetDeclaredType(AssertionErrorName); Debug.Assert(t_assertex != null); Exception exception; // exception to be thrown if (action.IsSet && !action.IsEmpty) { var description = action.AsString(); exception = action.AsObject() as Exception ?? (Exception)t_assertex.Creator(this, (PhpValue)description); } else { exception = (Exception)t_assertex.Creator(this); } // throw exception; }
/// <summary> /// In case value is a resource, gets its reference. /// </summary> public static PhpResource AsResource(this PhpValue value) { return(value.AsObject() as PhpResource); }
/// <summary> /// Gets underlaying class instance or <c>null</c>. /// </summary> public static object AsObject(PhpValue value) => value.AsObject(); // TOOD: ?? throw new InvalidCastException();
/// <summary> /// Gets underlaying class instance or <c>null</c>. /// </summary> public static object AsObject(PhpValue value) => value.AsObject();
public object AsObject() => Value.AsObject();