コード例 #1
0
ファイル: StringConstructor.cs プロジェクト: KurtGokhan/jint
        /// <summary>
        /// https://tc39.es/ecma262/#sec-string-constructor-string-value
        /// </summary>
        ObjectInstance IConstructor.Construct(JsValue[] arguments, JsValue newTarget)
        {
            JsString s;

            if (arguments.Length == 0)
            {
                s = JsString.Empty;
            }
            else
            {
                var value = arguments.At(0);
                if (newTarget.IsUndefined() && value.IsSymbol())
                {
                    return(StringCreate(JsString.Create(((JsSymbol)value).ToString()), PrototypeObject));
                }
                s = TypeConverter.ToJsString(arguments[0]);
            }

            if (newTarget.IsUndefined())
            {
                return(StringCreate(s, PrototypeObject));
            }

            return(StringCreate(s, GetPrototypeFromConstructor(newTarget, static intrinsics => intrinsics.String.PrototypeObject)));
        }
コード例 #2
0
        public JsValue Stringify(JsValue thisObject, JsValue[] arguments)
        {
            JsValue
                value    = Undefined,
                replacer = Undefined,
                space    = Undefined;

            if (arguments.Length > 2)
            {
                space = arguments[2];
            }

            if (arguments.Length > 1)
            {
                replacer = arguments[1];
            }

            if (arguments.Length > 0)
            {
                value = arguments[0];
            }

            var serializer = new JsonSerializer(_engine);

            if (value.IsUndefined() && replacer.IsUndefined())
            {
                return(Undefined);
            }

            return(serializer.Serialize(value, replacer, space));
        }
コード例 #3
0
        internal static ObjectInstance ToBooleanOrObject(this JsValue x, out bool boolVal)
        {
            if (x.IsObject())
            {
                boolVal = false;
                return(x.AsObject());
            }

            switch (x.Type)
            {
            case Types.Boolean:
                boolVal = x.AsBoolean();
                break;

            case Types.Number:
                boolVal = x.AsNumber() != 0;
                break;

            default:
                boolVal = !x.IsNull() && !x.IsUndefined();
                break;
            }

            return(null);
        }
コード例 #4
0
        private ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
        {
            if (!ReferenceEquals(this, newTarget) && !newTarget.IsUndefined())
            {
                return(OrdinaryCreateFromConstructor(
                           newTarget,
                           static intrinsics => intrinsics.Object.PrototypeObject,
                           (engine, realm, state) => new ObjectInstance(engine)));
            }

            if (arguments.Length > 0)
            {
                var value = arguments[0];
                if (value is ObjectInstance oi)
                {
                    return(oi);
                }

                var type = value.Type;
                if (type == Types.String || type == Types.Number || type == Types.Boolean)
                {
                    return(TypeConverter.ToObject(_realm, value));
                }
            }


            return(new ObjectInstance(_engine));
        }
コード例 #5
0
        static public AssetReference FromJsValue(JsValue obj)
        {
            if (obj == null || obj.IsNull() || obj.IsUndefined())
            {
                return(None);
            }

            if (obj.IsObject())
            {
                var ob    = obj.AsObject();
                var v0    = ob.Get("type");
                var value = ob.Get("value");

                var type = (AssetReferenceType)v0.AsNumber();

                return(new AssetReference(type, value));
            }
            else
            {
                var ob = obj.ToObject();

                if (ob is Object)
                {
                    return(new AssetReference(AssetReferenceType.Object, obj));
                }

                return(new AssetReference(AssetReferenceType.Procedural, obj));
            }
        }
コード例 #6
0
            static TransformedEvent?HandleResponse(JsValue result, TransformEvent original)
            {
                if (result == null || result.IsUndefined())
                {
                    Log.Debug("Got empty response, ignoring");
                    return(null);
                }

                ObjectInstance obj = result.AsObject();

                if (!TryGetString("Stream", true, out var stream) ||
                    string.IsNullOrWhiteSpace(stream) ||
                    !TryGetString("EventType", true, out var eventType) ||
                    string.IsNullOrWhiteSpace(eventType))
                {
                    return(null);
                }

                var data = GetSerializedObject("Data");

                if (data == null)
                {
                    return(null);
                }

                var meta = GetSerializedObject("Meta");

                return(new TransformedEvent(stream, eventType, data, meta));

                byte[]? GetSerializedObject(string propName)
                {
                    var candidate = obj.Get(propName);

                    if (candidate == null || !candidate.IsObject())
                    {
                        return(null);
                    }

                    return(JsonSerializer.SerializeToUtf8Bytes(candidate.ToObject()));
                }

                bool TryGetString(string propName, bool log, out string value)
                {
                    var candidate = obj.Get(propName);

                    if (candidate == null || !candidate.IsString())
                    {
                        if (log)
                        {
                            Log.Debug("Transformed object property {Prop} is null or not a string", propName);
                        }
                        value = string.Empty;
                        return(false);
                    }

                    value = candidate.AsString();
                    return(true);
                }
            }
コード例 #7
0
 public bool AsNativeBool(JsValue jsValue)
 {
     if (jsValue.IsNull() || jsValue.IsUndefined())
     {
         return(false);
     }
     return(jsValue.AsBoolean());
 }
コード例 #8
0
 private static string?ConvertToStringHandlingNulls(JsonInstance json, JsValue value)
 {
     if (value.IsNull() || value.IsUndefined())
     {
         return(null);
     }
     return(json.Stringify(JsValue.Undefined, new[] { value }).AsString());
 }
コード例 #9
0
 public int AsNativeInt(JsValue value)
 {
     if (value.IsNull() || value.IsUndefined())
     {
         return(0);
     }
     return((int)Math.Round(value.AsNumber()));
 }
コード例 #10
0
 public Decimal?AsNativeDecimal(JsValue value)
 {
     if (value.IsNull() || value.IsUndefined())
     {
         return(null);
     }
     return(Decimal.Parse(value.ToString()));
 }
コード例 #11
0
        public override JsValue Call(JsValue thisObject, JsValue[] arguments)
        {
            if (thisObject.IsUndefined())
            {
                throw new JavaScriptException(_engine.TypeError, "Constructor requires 'new'", null);
            }

            return(Construct(arguments, thisObject));
        }
コード例 #12
0
        ObjectInstance IConstructor.Construct(JsValue[] arguments, JsValue newTarget)
        {
            if (newTarget.IsUndefined())
            {
                ExceptionHelper.ThrowTypeError(_realm);
            }

            return(Construct(arguments.At(0), arguments.At(1)));
        }
コード例 #13
0
ファイル: SetConstructor.cs プロジェクト: rockyjvec/SpaceJS
        public override JsValue Call(JsValue thisObject, JsValue[] arguments)
        {
            if (thisObject.IsUndefined())
            {
                ExceptionHelper.ThrowTypeError(_engine, "Constructor Set requires 'new'");
            }

            return(Construct(arguments));
        }
コード例 #14
0
        /// <summary>
        /// https://tc39.es/ecma262/#sec-set-iterable
        /// </summary>
        ObjectInstance IConstructor.Construct(JsValue[] arguments, JsValue newTarget)
        {
            if (newTarget.IsUndefined())
            {
                ExceptionHelper.ThrowTypeError(_engine.Realm);
            }

            var set = OrdinaryCreateFromConstructor(
                newTarget,
                static intrinsics => intrinsics.Set.PrototypeObject,
コード例 #15
0
        private object ConvertValue(JsValue value, Type targetType)
        {
            if (value.IsUndefined())
            {
                return(null);
            }

            if (value.IsNull())
            {
                return(default);
コード例 #16
0
        public virtual bool TryPropertyReference(Engine engine, Reference reference, ref JsValue value)
        {
            if (reference.GetReferencedName() == "reduce" &&
                value.IsArray() && value.AsArray().GetLength() == 0)
            {
                value = Null.Instance;
                return(true);
            }

            return(value.IsNull() || value.IsUndefined());
        }
コード例 #17
0
        public void setEventListener(IReactComponent element, string eventType, JsValue value)
        {
            var hasValue = value != null && !value.IsNull() && !value.IsUndefined() && !value.IsBoolean();
            var callback = value.As <FunctionInstance>();

            if (hasValue && callback == null)
            {
                throw new Exception("The callback for an event must be a function.");
            }

            element.SetEventListener(eventType, new Callback(callback));
        }
コード例 #18
0
        /// <summary>
        /// https://tc39.es/ecma262/#sec-number-constructor-number-value
        /// </summary>
        ObjectInstance IConstructor.Construct(JsValue[] arguments, JsValue newTarget)
        {
            var n = ProcessFirstParameter(arguments);

            if (newTarget.IsUndefined())
            {
                return(Construct(n));
            }

            var o = OrdinaryCreateFromConstructor(
                newTarget,
                static intrinsics => intrinsics.Number.PrototypeObject,
コード例 #19
0
 public string AsNativeString(JsValue jsValue)
 {
     if (jsValue.IsNull() || jsValue.IsUndefined())
     {
         return(null);
     }
     if (jsValue.IsString())
     {
         return(jsValue.AsString());
     }
     return(jsValue.ToString());
 }
コード例 #20
0
        public static RectOffset FromJsValue(JsValue obj)
        {
            if (obj == null || obj.IsNull() || obj.IsUndefined())
            {
                return(null);
            }

            if (obj.IsNumber())
            {
                var num = (int)obj.AsNumber();
                return(new RectOffset(num, num, num, num));
            }

            if (obj.IsArray())
            {
                var len = obj.AsArray().Length;

                var v0 = obj.AsArray()[0];
                var v1 = obj.AsArray()[1];
                var v2 = obj.AsArray()[2];
                var v3 = obj.AsArray()[3];

                var top    = v0.IsNumber() ? (int)v0.AsNumber() : 0;
                var right  = v1.IsNumber() ? (int)v1.AsNumber() : (len < 2 ? top : 0);
                var bottom = v2.IsNumber() ? (int)v2.AsNumber() : top;
                var left   = v3.IsNumber() ? (int)v3.AsNumber() : right;
                return(new RectOffset(left, right, top, bottom));
            }

            if (obj.IsObject())
            {
                var ob   = obj.AsObject();
                var vert = ob.Get("vertical");
                var hor  = ob.Get("horizontal");

                var v0 = ob.Get("top");
                var v1 = ob.Get("right");
                var v2 = ob.Get("bottom");
                var v3 = ob.Get("left");

                var val1 = vert.IsNumber() ? (int)vert.AsNumber() : 0;
                var val2 = hor.IsNumber() ? (int)hor.AsNumber() : 0;

                var top    = v0.IsNumber() ? (int)v0.AsNumber() : val1;
                var right  = v1.IsNumber() ? (int)v1.AsNumber() : val2;
                var bottom = v2.IsNumber() ? (int)v2.AsNumber() : val1;
                var left   = v3.IsNumber() ? (int)v3.AsNumber() : val2;

                return(new RectOffset(left, right, top, bottom));
            }

            return(null);
        }
コード例 #21
0
        /// <summary>
        /// https://tc39.es/ecma262/#sec-boolean-constructor-boolean-value
        /// </summary>
        ObjectInstance IConstructor.Construct(JsValue[] arguments, JsValue newTarget)
        {
            var b = TypeConverter.ToBoolean(arguments.At(0))
                ? JsBoolean.True
                : JsBoolean.False;

            if (newTarget.IsUndefined())
            {
                return(Construct(b));
            }

            var o = OrdinaryCreateFromConstructor(
                newTarget,
                static intrinsics => intrinsics.Boolean.PrototypeObject,
コード例 #22
0
            static MessageRoute AsRoute(JsValue result, TransformEvent evt)
            {
                if (result == null || result.IsUndefined() || !result.IsObject())
                {
                    return(DefaultRouters.RouteByCategory(evt.Stream));
                }

                var obj = result.AsObject();

                return(new MessageRoute(
                           obj.Get("topic").AsString(),
                           obj.Get("partitionKey").AsString()
                           ));
            }
コード例 #23
0
ファイル: DateConstructor.cs プロジェクト: KurtGokhan/jint
        /// <summary>
        /// https://tc39.es/ecma262/#sec-date
        /// </summary>
        private ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
        {
            double dv;

            if (arguments.Length == 0 || newTarget.IsUndefined())
            {
                dv = FromDateTime(DateTime.UtcNow);
            }
            else if (arguments.Length == 1)
            {
                if (arguments[0] is DateInstance date)
                {
                    return(Construct(date.DateValue));
                }

                var v = TypeConverter.ToPrimitive(arguments[0]);
                if (v.IsString())
                {
                    return(Construct(((JsNumber)Parse(Undefined, Arguments.From(v)))._value));
                }

                dv = TimeClip(TypeConverter.ToNumber(v));
            }
            else
            {
                var y     = TypeConverter.ToNumber(arguments.At(0));
                var m     = TypeConverter.ToNumber(arguments.At(1));
                var dt    = TypeConverter.ToNumber(arguments.At(2, JsNumber.PositiveOne));
                var h     = TypeConverter.ToNumber(arguments.At(3, JsNumber.PositiveZero));
                var min   = TypeConverter.ToNumber(arguments.At(4, JsNumber.PositiveZero));
                var s     = TypeConverter.ToNumber(arguments.At(5, JsNumber.PositiveZero));
                var milli = TypeConverter.ToNumber(arguments.At(6, JsNumber.PositiveZero));

                var yInteger = TypeConverter.ToInteger(y);
                if (!double.IsNaN(y) && 0 <= yInteger && yInteger <= 99)
                {
                    y += 1900;
                }

                var finalDate = DatePrototype.MakeDate(
                    DatePrototype.MakeDay(y, m, dt),
                    DatePrototype.MakeTime(h, min, s, milli));

                dv = TimeClip(PrototypeObject.Utc(finalDate));
            }

            var o = OrdinaryCreateFromConstructor(
                newTarget,
                static intrinsics => intrinsics.Date.PrototypeObject,
コード例 #24
0
 private string GetDebugValue(JsValue obj, bool recursive)
 {
     if (obj.IsString())
     {
         var debugValue = obj.ToString();
         return(recursive ? '"' + debugValue + '"' : debugValue);
     }
     if (obj.IsArray())
     {
         var sb      = new StringBuilder("[");
         var array   = obj.AsArray();
         var jsValue = (int)array.Get("length").AsNumber();
         for (var i = 0; i < jsValue; i++)
         {
             if (i != 0)
             {
                 sb.Append(",");
             }
             sb.Append(GetDebugValue(array.Get(i.ToString()), true));
         }
         sb.Append("]");
         return(sb.ToString());
     }
     if (obj.IsObject())
     {
         var result = new ScriptRunnerResult(this, obj);
         using (var jsonObj = result.TranslateToObject(_jsonCtx))
         {
             return(jsonObj.ToString());
         }
     }
     if (obj.IsBoolean())
     {
         return(obj.AsBoolean().ToString());
     }
     if (obj.IsNumber())
     {
         return(obj.AsNumber().ToString(CultureInfo.InvariantCulture));
     }
     if (obj.IsNull())
     {
         return("null");
     }
     if (obj.IsUndefined())
     {
         return("undefined");
     }
     return(obj.ToString());
 }
コード例 #25
0
ファイル: DomConverter.cs プロジェクト: RusKnyaz/Optimus
        public object ConvertFromJs(JsValue jsValue)
        {
            if (jsValue == _getEngine().Global)
            {
                return(_global);
            }

            if (jsValue.IsUndefined())
            {
                return(null);
            }

            if (jsValue.IsObject())
            {
                switch (jsValue.AsObject())
                {
                case IObjectWrapper wrapper:
                    return(wrapper.Target);

                case ClrPrototype proto:
                    return(proto);

                case ObjectInstance objInst:

                    //todo: instantiate object instead of distionary
                    return(objInst.GetOwnProperties().ToDictionary(x => x.Key, x =>
                                                                   ConvertFromJs(objInst.Get(x.Key))));
                }

                return(jsValue);
            }

            if (jsValue.IsBoolean())
            {
                return(jsValue.AsBoolean());
            }

            if (jsValue.IsString())
            {
                return(jsValue.AsString());
            }

            if (jsValue.IsNumber())
            {
                return(jsValue.AsNumber());
            }

            return(null);
        }
コード例 #26
0
        private static JsValue DivideComplex(JsValue lval, JsValue rval)
        {
            if (lval.IsUndefined() || rval.IsUndefined())
            {
                return(Undefined.Instance);
            }
            else
            {
                var lN = TypeConverter.ToNumber(lval);
                var rN = TypeConverter.ToNumber(rval);

                if (double.IsNaN(rN) || double.IsNaN(lN))
                {
                    return(JsNumber.DoubleNaN);
                }

                if (double.IsInfinity(lN) && double.IsInfinity(rN))
                {
                    return(JsNumber.DoubleNaN);
                }

                if (double.IsInfinity(lN) && rN == 0)
                {
                    if (NumberInstance.IsNegativeZero(rN))
                    {
                        return(-lN);
                    }

                    return(lN);
                }

                if (lN == 0 && rN == 0)
                {
                    return(JsNumber.DoubleNaN);
                }

                if (rN == 0)
                {
                    if (NumberInstance.IsNegativeZero(rN))
                    {
                        return(lN > 0 ? -double.PositiveInfinity : -double.NegativeInfinity);
                    }

                    return(lN > 0 ? double.PositiveInfinity : double.NegativeInfinity);
                }

                return(lN / rN);
            }
        }
コード例 #27
0
        public static Vector4?FromJsValue(JsValue obj)
        {
            if (obj == null || obj.IsNull() || obj.IsUndefined())
            {
                return(null);
            }

            if (obj.IsNumber())
            {
                var num = (float)obj.AsNumber();
                return(new Vector4(num, num, num, num));
            }

            if (obj.IsArray())
            {
                var len = obj.AsArray().Length;

                var v0 = obj.AsArray()[0];
                var v1 = obj.AsArray()[1];
                var v2 = obj.AsArray()[2];
                var v3 = obj.AsArray()[3];

                var x = v0.IsNumber() ? (float)v0.AsNumber() : 0;
                var y = v1.IsNumber() ? (float)v1.AsNumber() : 0;
                var z = v2.IsNumber() ? (float)v2.AsNumber() : 0;
                var w = v3.IsNumber() ? (float)v3.AsNumber() : 0;
                return(new Vector4(x, y, z, w));
            }

            if (obj.IsObject())
            {
                var ob = obj.AsObject();

                var v0 = ob.Get("x");
                var v1 = ob.Get("y");
                var v2 = ob.Get("z");
                var v3 = ob.Get("w");

                var x = v0.IsNumber() ? (float)v0.AsNumber() : 0;
                var y = v1.IsNumber() ? (float)v1.AsNumber() : 0;
                var z = v2.IsNumber() ? (float)v2.AsNumber() : 0;
                var w = v3.IsNumber() ? (float)v3.AsNumber() : 0;

                return(new Vector4(x, y, z, w));
            }

            return(null);
        }
コード例 #28
0
        public IDictionary <String, Object> AsNativeObject(JsValue jsValue)
        {
            if (jsValue.IsNull() || jsValue.IsUndefined())
            {
                return(null);
            }
            var            ret   = new Dictionary <String, Object>();
            ObjectInstance value = jsValue.AsObject();

            foreach (var prop in value.GetOwnProperties())
            {
                var v = value.GetOwnProperty(prop.Key);
                if (v != null)
                {
                    var jsv = v.Value;
                    if (!jsv.HasValue || jsv.Value.IsNull())
                    {
                        ret[prop.Key] = null;
                    }
                    else if (jsv.Value.IsBoolean())
                    {
                        ret[prop.Key] = jsv.Value.AsBoolean();
                    }
                    else if (jsv.Value.IsNumber())
                    {
                        double d = jsv.Value.AsNumber();
                        if (d % 1 == 0)
                        {
                            ret[prop.Key] = (int)d;
                        }
                        else
                        {
                            ret[prop.Key] = d;
                        }
                    }
                    else if (jsv.Value.IsObject())
                    {
                        // TODO protect against circular reference
                        ret[prop.Key] = AsNativeObject(jsv.Value);
                    }
                    else if (jsv.Value.IsString())
                    {
                        ret[prop.Key] = jsv.Value.AsString();
                    }
                }
            }
            return(ret);
        }
コード例 #29
0
 public void Log(JsValue value, params JsValue[] values)
 {
     if (values == null || values.Length == 0)
     {
         if (value == null || value.IsNull())
         {
             Logger.Log("null");
             return;
         }
         else if (value.IsUndefined())
         {
             Logger.Log("undefined");
         }
     }
     Logger.Log(value + " " + JintUnity.Utils.Join(" ", values));
 }
コード例 #30
0
        long ConvertAndCheckForInfinity(JsValue jsValue, long defaultValue)
        {
            if (jsValue.IsUndefined())
            {
                return(defaultValue);
            }

            var num = TypeConverter.ToNumber(jsValue);

            if (double.IsPositiveInfinity(num))
            {
                return(long.MaxValue);
            }

            return((long)num);
        }