コード例 #1
0
        public override PExpr Visit(IAccessorFrame frame)
        {
            var s = frame.GetState(c =>
                                   c.On("toString").OnCall((f, args) =>
            {
                return(f.SetResult(f.Global.CreateString(JSSupport.ToString(_value))));
            }
                                                           ));

            return(s != null?s.Visit() : frame.SetError());
        }
コード例 #2
0
        public override PExpr Visit(IAccessorFrame frame)
        {
            var s = frame.GetState(c =>
                                   c.On("toString").OnCall((f, args) =>
            {
                int radix = 10;
                if (args.Count == 1)
                {
                    radix = JSSupport.ToInt32(args[0].ToDouble());
                }
                if (radix < 2 || radix > 36)
                {
                    return(f.SetError("Radix must be between 2 and 36."));
                }
                return(f.SetResult(f.Global.CreateString(JSSupport.ToString(_value, radix))));
            }
                                                           ));

            return(s != null?s.Visit() : frame.SetError());
        }
コード例 #3
0
        public override PExpr Visit(IAccessorFrame frame)
        {
            var s = frame.GetState(c =>
                                   c.On("charAt").OnCall((f, args) =>
            {
                int idx = args.Count > 0 ? JSSupport.ToInt32(args[0].ToDouble()) : 0;
                if (idx < 0 || idx >= _value.Length)
                {
                    return(f.SetResult(JSEvalString.EmptyString));
                }
                return(f.SetResult(f.Global.CreateString(new String(_value[idx], 1))));
            })
                                   .On("toString").OnCall((f, args) =>
            {
                return(f.SetResult(this));
            }
                                                          ));

            return(s != null?s.Visit() : frame.SetError());
        }
コード例 #4
0
            public override PExpr Visit(IAccessorFrame frame)
            {
                var s = frame.GetState(c => c
                                       .On("AnIntrinsicArray").OnIndex((f, idx) =>
                {
                    if (idx.Type != "number")
                    {
                        return(f.SetError("Number expected."));
                    }
                    int i = JSSupport.ToInt32(idx.ToDouble());
                    if (i < 0 || i >= AnIntrinsicArray.Length)
                    {
                        return(f.SetError("Index out of range."));
                    }
                    return(f.SetResult(CreateNumber(AnIntrinsicArray[i])));
                })
                                       .On("An").On("array").On("with").On("one").On("cell").OnIndex((f, idx) =>
                {
                    return(f.SetResult(CreateString("An.array.with.one.cell[] => " + idx.ToString())));
                })
                                       .On("array").OnIndex((f, idx) =>
                {
                    throw new CKException("Accessing XXX.array other than 'An.Array' must not be found.");
                })
                                       .On("Ghost").On("M").OnCall((f, args) =>
                {
                    Console.WriteLine("Ghost.M() called with {0} arguments: {1} (=> returns {0}).",
                                      args.Count,
                                      String.Join(", ", args.Select(a => a.ToString())));
                    return(f.SetResult(f.Global.CreateNumber(args.Count)));
                })
                                       .On("Ghost").On("M").OnIndex((f, idx) =>
                {
                    Console.WriteLine("Ghost.M[{0}] called (=> returns {0}).", JSSupport.ToInt32(idx.ToDouble()));
                    return(f.SetResult(idx));
                })
                                       );

                return(s == null?base.Visit(frame) : s.Visit());
            }
コード例 #5
0
        /// <summary>
        /// Default implementation of <see cref="IAccessorVisitor.Visit"/> that supports evaluation of intrinsic
        /// functions Number(), String(), Boolean() and Date().
        /// By overriding this any binding to to external objects can be achieved (recall to call this base
        /// method when overriding).
        /// </summary>
        /// <param name="frame">The current frame (gives access to the next frame if any).</param>
        public virtual PExpr Visit(IAccessorFrame frame)
        {
            var s = frame.GetState(c =>
                                   c.On("Number").OnCall((f, args) =>
            {
                if (args.Count == 0)
                {
                    return(f.SetResult(JSEvalNumber.Zero));
                }
                return(f.SetResult(CreateNumber(args[0])));
            }
                                                         )
                                   .On("String").OnCall((f, args) =>
            {
                if (args.Count == 0)
                {
                    return(f.SetResult(JSEvalString.EmptyString));
                }
                return(f.SetResult(CreateString(args[0])));
            })
                                   .On("Boolean").OnCall((f, args) =>
            {
                if (args.Count == 0)
                {
                    return(f.SetResult(JSEvalBoolean.False));
                }
                return(f.SetResult(CreateBoolean(args[0])));
            })
                                   .On("Date").OnCall((f, args) =>
            {
                try
                {
                    int[] p = new int[7];
                    for (int i = 0; i < args.Count; ++i)
                    {
                        p[i] = (int)args[i].ToDouble();
                        if (p[i] < 0)
                        {
                            p[i] = 0;
                        }
                    }
                    if (p[0] > 9999)
                    {
                        p[0] = 9999;
                    }
                    if (p[1] < 1)
                    {
                        p[1] = 1;
                    }
                    else if (p[1] > 12)
                    {
                        p[1] = 12;
                    }
                    if (p[2] < 1)
                    {
                        p[2] = 1;
                    }
                    else if (p[2] > 31)
                    {
                        p[2] = 31;
                    }
                    DateTime d = new DateTime(p[0], p[1], p[2], p[3], p[4], p[5], p[6], DateTimeKind.Utc);
                    return(f.SetResult(CreateDateTime(d)));
                }
                catch (Exception ex)
                {
                    return(f.SetError(ex.Message));
                }
            })
                                   );

            return(s != null?s.Visit() : frame.SetError());
        }
コード例 #6
0
 public override PExpr Visit( IAccessorFrame frame )
 {
     var s = frame.GetState( c =>
         c.On("toString").OnCall( (f,args) =>
         {
             int radix = 10;
             if( args.Count == 1 ) radix = JSSupport.ToInt32( args[0].ToDouble() );
             if( radix < 2 || radix > 36 ) return f.SetError( "Radix must be between 2 and 36." );
             return f.SetResult( f.Global.CreateString( JSSupport.ToString( _value, radix ) ) );
         }
         ) );
     return s != null ? s.Visit() : frame.SetError();
 }
コード例 #7
0
        /// <summary>
        /// Default implementation of <see cref="IAccessorVisitor.Visit"/> that supports evaluation of intrinsic 
        /// functions Number(), String(), Boolean() and Date().
        /// By overriding this any binding to to external objects can be achieved (recall to call this base
        /// method when overriding).
        /// </summary>
        /// <param name="frame">The current frame (gives access to the next frame if any).</param>
        public virtual PExpr Visit( IAccessorFrame frame )
        {
            var s = frame.GetState( c =>
                c.On( "Number" ).OnCall( ( f, args ) =>
                {
                    if( args.Count == 0 ) return f.SetResult( JSEvalNumber.Zero );
                    return f.SetResult( CreateNumber( args[0] ) );
                }
                )
                .On( "String" ).OnCall( ( f, args ) =>
                {
                    if( args.Count == 0 ) return f.SetResult( JSEvalString.EmptyString );
                    return f.SetResult( CreateString( args[0] ) );

                } )
                .On( "Boolean" ).OnCall( ( f, args ) =>
                {
                    if( args.Count == 0 ) return f.SetResult( JSEvalBoolean.False );
                    return f.SetResult( CreateBoolean( args[0] ) );
                } )
                .On( "Date" ).OnCall( ( f, args ) =>
                {
                    try
                    {
                        int[] p = new int[7];
                        for( int i = 0; i < args.Count; ++i )
                        {
                            p[i] = (int)args[i].ToDouble();
                            if( p[i] < 0 ) p[i] = 0;
                        }
                        if( p[0] > 9999 ) p[0] = 9999;
                        if( p[1] < 1 ) p[1] = 1;
                        else if( p[1] > 12 ) p[1] = 12;
                        if( p[2] < 1 ) p[2] = 1;
                        else if( p[2] > 31 ) p[2] = 31;
                        DateTime d = new DateTime( p[0], p[1], p[2], p[3], p[4], p[5], p[6], DateTimeKind.Utc );
                        return f.SetResult( CreateDateTime( d ) );
                    }
                    catch( Exception ex )
                    {
                        return f.SetError( ex.Message );
                    }
                } )
            );
            return s != null ? s.Visit() : frame.SetError();
        }
コード例 #8
0
 public override PExpr Visit( IAccessorFrame frame )
 {
     var s = frame.GetState( c => c
         .On( "AnIntrinsicArray" ).OnIndex( ( f, idx ) =>
         {
             if( idx.Type != "number" ) return f.SetError( "Number expected." );
             int i = JSSupport.ToInt32( idx.ToDouble() );
             if( i < 0 || i >= AnIntrinsicArray.Length ) return f.SetError( "Index out of range." );
             return f.SetResult( CreateNumber( AnIntrinsicArray[i] ) );
         } )
         .On( "An" ).On( "array" ).On( "with" ).On( "one" ).On( "cell" ).OnIndex( ( f, idx ) =>
         {
             return f.SetResult( CreateString( "An.array.with.one.cell[] => " + idx.ToString() ) );
         } )
         .On( "array" ).OnIndex( ( f, idx ) =>
         {
             throw new CKException( "Accessing XXX.array other than 'An.Array' must not be found." );
         } )
         .On( "Ghost" ).On( "M" ).OnCall( ( f, args ) =>
         {
             Console.WriteLine( "Ghost.M() called with {0} arguments: {1} (=> returns {0}).",
                                     args.Count,
                                     String.Join( ", ", args.Select( a => a.ToString() )) );
             return f.SetResult( f.Global.CreateNumber( args.Count ) );
         } )
         .On( "Ghost" ).On( "M" ).OnIndex( ( f, idx ) =>
         {
             Console.WriteLine( "Ghost.M[{0}] called (=> returns {0}).", JSSupport.ToInt32( idx.ToDouble() ) );
             return f.SetResult( idx );
         } )
         );
     return s == null ? base.Visit( frame ) : s.Visit();
 }
コード例 #9
0
 public override PExpr Visit( IAccessorFrame frame )
 {
     var s = frame.GetState( c =>
         c.On( "charAt" ).OnCall( ( f, args ) =>
         {
             int idx = args.Count > 0 ? JSSupport.ToInt32( args[0].ToDouble() ) : 0;
             if( idx < 0 || idx >= _value.Length ) return f.SetResult( JSEvalString.EmptyString );
             return f.SetResult( f.Global.CreateString( new String( _value[idx], 1 ) ) );
         } )
         .On( "toString" ).OnCall( ( f, args ) =>
         {
             return f.SetResult( this );
         }
         ) );
     return s != null ? s.Visit() : frame.SetError();
 }