예제 #1
0
파일: Vm.cs 프로젝트: mbrezu/Shovel
 static void CallFunction(Callable callable, Vm vm, int numArgs, bool saveReturnAddress, bool inApply)
 {
     if (callable.Arity != null)
     {
         // FIXME: Find out why/when the arity can be null.
         if (callable.HasCollectParams)
         {
             if (callable.Arity.Value <= numArgs)
             {
                 var extraArgs = numArgs - callable.Arity.Value;
                 var values = new ArrayInstance();
                 for (int i = 0; i < extraArgs; i++) {
                     values.Add(vm.stack.Storage[i + vm.stack.Count - extraArgs]);
                 }
                 vm.stack.PopMany(extraArgs);
                 vm.stack.Push(Value.Make(values));
             }
             else
             {
                 ArityError(vm, callable.Arity.Value, numArgs, inApply);
             }
         }
         else if (callable.Arity.Value != numArgs)
         {
             ArityError(vm, callable.Arity.Value, numArgs, inApply);
         }
     }
     if (saveReturnAddress)
     {
         vm.stack.Push (Value.Make (new ReturnAddress () {
             ProgramCounter = vm.programCounter + 1,
             Environment = vm.currentEnvironment
         }));
     }
     vm.currentEnvironment = callable.Environment;
     vm.programCounter = callable.ProgramCounter.Value;
 }
예제 #2
0
파일: Prim0.cs 프로젝트: mbrezu/Shovel
 internal static void Keys(VmApi api, ref Value hash)
 {
     if (hash.Kind != Value.Kinds.Hash) {
         api.RaiseShovelError ("First argument must be a hash table.");
     }
     var result = new ArrayInstance ();
     var sizeIncrease = 1 + hash.hashValue.Count;
     api.CellsIncrementHerald (sizeIncrease);
     result.AddRange (hash.hashValue.Keys);
     hash.arrayValue = result;
     hash.Kind = Value.Kinds.Array;
     api.CellsIncrementer (sizeIncrease);
 }
예제 #3
0
파일: Prim0.cs 프로젝트: mbrezu/Shovel
 static Value ArrayConstructor(VmApi api, Value[] args, int start, int length)
 {
     var result = new ArrayInstance ();
     var sizeIncrease = 1 + length;
     api.CellsIncrementHerald (sizeIncrease);
     for (var i = start; i < start+length; i++) {
         result.Add (args [i]);
     }
     api.CellsIncrementer (sizeIncrease);
     return Value.Make (result);
 }
예제 #4
0
파일: Prim0.cs 프로젝트: mbrezu/Shovel
 internal static void Add(VmApi api, ref Value t1, ref Value t2)
 {
     switch (t1.Kind) {
     case Value.Kinds.Integer:
         switch (t2.Kind) {
         case Value.Kinds.Integer:
             t1.Kind = Value.Kinds.Integer;
             t1.integerValue = t1.integerValue + t2.integerValue;
             break;
         case Value.Kinds.Double:
             t1.Kind = Value.Kinds.Double;
             t1.doubleValue = t1.integerValue + t2.doubleValue;
             break;
         default:
             AddError (api);
             break;
         }
         break;
     case Value.Kinds.Double:
         switch (t2.Kind) {
         case Value.Kinds.Integer:
             t1.Kind = Value.Kinds.Double;
             t1.doubleValue = t1.doubleValue + t2.integerValue;
             break;
         case Value.Kinds.Double:
             t1.Kind = Value.Kinds.Double;
             t1.doubleValue = t1.doubleValue + t2.doubleValue;
             break;
         default:
             AddError (api);
             break;
         }
         break;
     case Value.Kinds.String:
         if (t2.Kind == Value.Kinds.String) {
             t1.Kind = Value.Kinds.String;
             t1.stringValue = t1.stringValue + t2.stringValue;
         } else {
             AddError (api);
         }
         break;
     case Value.Kinds.Array:
         if (t2.Kind == Value.Kinds.Array) {
             var result = new ArrayInstance ();
             result.AddRange (t1.arrayValue);
             result.AddRange (t2.arrayValue);
             t1.Kind = Value.Kinds.Array;
             t1.arrayValue = result;
         } else {
             AddError (api);
         }
         break;
     default:
         AddError (api);
         break;
     }
 }
예제 #5
0
파일: Prim0.cs 프로젝트: mbrezu/Shovel
 static Value SizedArrayConstructor(VmApi api, Value size)
 {
     if (size.Kind != Value.Kinds.Integer) {
         api.RaiseShovelError ("Argument must be an integer.");
     }
     var result = new ArrayInstance ();
     var sizeIncrease = 1 + (int)size.integerValue;
     api.CellsIncrementHerald (sizeIncrease);
     for (var i = 0; i < size.integerValue; i++) {
         result.Add (Value.Make ());
     }
     api.CellsIncrementer (sizeIncrease);
     return Value.Make (result);
 }
예제 #6
0
        ArrayInstance RebuildShovelValueList(
            object[] objects, int index, Composite composite, Func<int, object> reader, int version)
        {
            var result = new ArrayInstance ();
            objects [index] = result;

            var length = composite.Elements.Length;
            if (version > 4)
            {
                length -= 2;
            }

            for (var i = 0; i < length; i++) {
                result.Add (RebuildShovelValue (reader (composite.Elements [i])));
            }

            if (version > 4)
            {
                // The indirect get/set are stored as the last elements of the array.
                result.IndirectGet = RebuildShovelValue(reader(composite.Elements[length]));
                result.IndirectSet = RebuildShovelValue(reader(composite.Elements[length + 1]));
            }
            return result;
        }
예제 #7
0
 int SerializeList(ArrayInstance list, object obj)
 {
     var composite = new Composite {
         Kind = ObjectTypes.ShovelValueList,
         Elements = new int[list.Count + 2]
     };
     var result = SerializeOneHashed (composite, obj);
     var i = 0;
     for (i = 0; i < list.Count; i++) {
         composite.Elements [i] = Serialize (list [i]);
     }
     composite.Elements[i] = Serialize(list.IndirectGet);
     i++;
     composite.Elements[i] = Serialize(list.IndirectSet);
     i++;
     return result;
 }
예제 #8
0
파일: Avatar.cs 프로젝트: hollow87/Zorbo
 public Avatar Construct(ArrayInstance smallbytes, object largebytes)
 {
     return(Call(smallbytes, largebytes));
 }
예제 #9
0
파일: Avatar.cs 프로젝트: hollow87/Zorbo
        public Avatar(JScript script, ObjectInstance prototype, ArrayInstance smallbytes, ArrayInstance largebytes)
            : base(script.Engine, prototype)
        {
            this.PopulateFunctions();

            this.smallbytes = smallbytes;
            this.largebytes = largebytes;
        }
예제 #10
0
        private JsValue ConstructGrouping(List <BlittableJsonReaderObject> values)
        {
            var jsValues = ConstructValues();
            var jsKey    = ConstructKey();

            var result = new ObjectInstance(Engine);

            result.Set("values", jsValues, false);
            result.Set("key", jsKey, false);

            return(result);

            JsValue ConstructKey()
            {
                if (_singleField)
                {
                    var index = values[0].GetPropertyIndex(_groupByFields[0].Name);
                    if (index != -1)
                    {
                        BlittableJsonReaderObject.PropertyDetails prop = default;
                        values[0].GetPropertyByIndex(index, ref prop);

                        return(JsValue.FromObject(Engine, prop.Value));
                    }

                    return(JsValue.Null);
                }

                var key = new ObjectInstance(Engine);

                foreach (var groupByField in _groupByFields)
                {
                    var index = values[0].GetPropertyIndex(groupByField.Name);
                    if (index != -1)
                    {
                        BlittableJsonReaderObject.PropertyDetails prop = default;
                        values[0].GetPropertyByIndex(index, ref prop);

                        var propertyName = groupByField.Name;
                        if (groupByField is JsNestedField jsnf)
                        {
                            propertyName = jsnf.PropertyName;
                        }

                        var value = groupByField.GetValue(null, prop.Value);

                        JsValue jsValue = value switch
                        {
                            BlittableJsonReaderObject bjro => new BlittableObjectInstance(Engine, null, bjro, null, null, null),
                            Document doc => new BlittableObjectInstance(Engine, null, doc.Data, doc),
                            LazyStringValue lsv => new JsString(lsv.ToString()),
                            LazyCompressedStringValue lcsv => new JsString(lcsv.ToString()),
                            LazyNumberValue lnv => new JsNumber(lnv.ToDouble(CultureInfo.InvariantCulture)),
                            _ => JsValue.FromObject(Engine, value)
                        };

                        key.Set(propertyName, jsValue, throwOnError: false);
                    }
                }

                return(key);
            }

            ArrayInstance ConstructValues()
            {
                var items = new PropertyDescriptor[values.Count];

                for (var i = 0; i < values.Count; i++)
                {
                    var val = values[i];

                    if (JavaScriptIndexUtils.GetValue(Engine, val, out var jsValue, isMapReduce: true) == false)
                    {
                        continue;
                    }

                    items[i] = new PropertyDescriptor(jsValue, true, true, true);
                }

                var jsArray = new ArrayInstance(Engine, items);

                jsArray.SetPrototypeOf(Engine.Array.PrototypeObject);
                jsArray.PreventExtensions();

                return(jsArray);
            }
        }
예제 #11
0
        public static object GetListItemVersionObject(ScriptEngine engine, SPListItemVersion version, SPField field)
        {
            switch (field.Type)
            {
            case SPFieldType.Integer:
            {
                int value;
                if (version.TryGetSPFieldValue(field.InternalName, out value))
                {
                    return(value);
                }
            }
            break;

            case SPFieldType.Boolean:
            {
                bool value;
                if (version.TryGetSPFieldValue(field.InternalName, out value))
                {
                    return(value);
                }
            }
            break;

            case SPFieldType.Number:
            {
                double value;
                if (version.TryGetSPFieldValue(field.InternalName, out value))
                {
                    return(value);
                }
            }
            break;

            case SPFieldType.DateTime:
            {
                DateTime value;
                if (version.TryGetSPFieldValue(field.InternalName, out value))
                {
                    return(JurassicHelper.ToDateInstance(engine, new DateTime(value.Ticks, DateTimeKind.Local)));
                }
            }
            break;

            case SPFieldType.URL:
            {
                string urlFieldValue;
                if (version.TryGetSPFieldValue(field.InternalName, out urlFieldValue))
                {
                    var urlValue = new SPFieldUrlValue(urlFieldValue);

                    var item = engine.Object.Construct();
                    item.SetPropertyValue("description", urlValue.Description, false);
                    item.SetPropertyValue("url", urlValue.Url, false);

                    return(item);
                }
            }
            break;

            case SPFieldType.User:
            {
                string userToken;
                if (version.TryGetSPFieldValue(field.InternalName, out userToken))
                {
                    var fieldUserValue = new SPFieldUserValue(version.ListItem.Web, userToken);
                    var userInstance   = new SPUserInstance(engine, fieldUserValue.User);
                    return(userInstance);
                }
            }
            break;

            case SPFieldType.Lookup:
            {
                var fieldType = field as SPFieldLookup;
                if (fieldType == null)
                {
                    return(null);
                }

                if (fieldType.AllowMultipleValues)
                {
                    object fv;
                    if (!version.TryGetSPFieldValue(field.InternalName, out fv))
                    {
                        return(null);
                    }

                    var fieldValue = fv as SPFieldLookupValueCollection;

                    var array = engine.Array.Construct();

                    if (fieldValue != null)
                    {
                        foreach (var lookupValue in fieldValue)
                        {
                            var item = engine.Object.Construct();
                            item.SetPropertyValue("lookupId", lookupValue.LookupId, false);
                            item.SetPropertyValue("lookupValue", lookupValue.LookupValue, false);

                            ArrayInstance.Push(array, item);
                        }
                    }

                    return(array);
                }
                else
                {
                    object fieldValue;
                    if (!version.TryGetSPFieldValue(field.InternalName, out fieldValue))
                    {
                        return(null);
                    }

                    var fieldUrlValue = fieldValue as SPFieldUrlValue;
                    if (fieldUrlValue != null)
                    {
                        var urlValue = fieldUrlValue;
                        var item     = engine.Object.Construct();
                        item.SetPropertyValue("description", urlValue.Description, false);
                        item.SetPropertyValue("url", urlValue.Url, false);

                        return(item);
                    }

                    if (fieldValue is DateTime)
                    {
                        var value = (DateTime)fieldValue;
                        return(JurassicHelper.ToDateInstance(engine,
                                                             new DateTime(value.Ticks, DateTimeKind.Local)));
                    }

                    var userValue = fieldValue as SPFieldUserValue;
                    if (userValue != null)
                    {
                        var fieldUserValue = userValue;
                        var userInstance   = new SPUserInstance(engine, fieldUserValue.User);
                        return(userInstance);
                    }

                    if (fieldValue is Guid)
                    {
                        var guidValue    = (Guid)fieldValue;
                        var guidInstance = new GuidInstance(engine.Object.InstancePrototype, guidValue);
                        return(guidInstance);
                    }

                    var s = fieldValue as string;
                    if (s == null)
                    {
                        return(fieldValue);
                    }

                    //Attempt to create a new SPFieldLookupValue from the string
                    if (!DigitRegex.IsMatch(s, 0))
                    {
                        return(fieldValue);
                    }

                    try
                    {
                        var lookupValue = new SPFieldLookupValue(s);

                        var item = engine.Object.Construct();
                        item.SetPropertyValue("lookupId", lookupValue.LookupId, false);
                        item.SetPropertyValue("lookupValue", lookupValue.LookupValue, false);
                        return(item);
                    }
                    catch (ArgumentException)
                    {
                        return(fieldValue);
                    }
                }
            }

            default:
            {
                object value;
                if (version.TryGetSPFieldValue(field.InternalName, out value))
                {
                    var stringValue = field.GetFieldValueAsText(value);

                    return(stringValue);
                }
            }
            break;
            }

            return(null);
        }
예제 #12
0
        public static IEnumerable <KeyValuePair <JsValue, PropertyDescriptor> > GetOwnPropertiesWithoutLength(this ArrayInstance array)
        {
            foreach (var kvp in array.GetOwnProperties())
            {
                if (kvp.Key == "length")
                {
                    continue;
                }

                yield return(kvp);
            }
        }
예제 #13
0
        public static JsValue Show(JavaScriptUi ui, string title, JsValue fields, JsValue validate)
        {
            if (ui == null)
            {
                throw new ArgumentNullException(nameof(ui));
            }

            using (var form = new UiForm())
            {
                if (String.IsNullOrEmpty(title))
                {
                    form.Text = ui.Owner.Text;
                }
                else
                {
                    form.Text = title;
                }

                form.Icon = ui.Owner.Icon;

                if (!fields.IsArray())
                {
                    throw new JavaScriptException("fields must be an array");
                }

                var controls = new List <Field>();

                fields.AsArray().ForEach((index, value) =>
                {
                    var container = form._container;

                    while (container.RowStyles.Count <= index)
                    {
                        container.RowCount++;
                        container.RowStyles.Add(new RowStyle(SizeType.AutoSize));
                    }

                    var field          = value.AsObject();
                    string name        = field.Get("name").ConvertToString();
                    string label       = field.Get("label").ConvertToString();
                    var type           = ParseType(field.Get("type").ConvertToString());
                    ArrayInstance list = null;
                    if (field.HasOwnProperty("list"))
                    {
                        list = field.Get("list").AsArray();
                    }

                    Field control;
                    switch (type)
                    {
                    case FieldType.Text:
                        control = new TextField(name, label);
                        break;

                    case FieldType.CheckBox:
                        control = new CheckBoxField(name, label);
                        break;

                    case FieldType.Numeric:
                        control = new NumericField(name, label);
                        break;

                    case FieldType.Date:
                        control = new DateField(name, label);
                        break;

                    case FieldType.DateTime:
                        control = new DateTimeField(name, label);
                        break;

                    case FieldType.ComboBox:
                        control = new ComboBoxField(name, label, list);
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }

                    controls.Add(control);

                    if (field.HasOwnProperty("value"))
                    {
                        control.SetValue(field.Get("value"));
                    }

                    if (control.ShowLabel)
                    {
                        var labelControl = new Label
                        {
                            AutoSize  = true,
                            Text      = label,
                            Dock      = DockStyle.Fill,
                            TextAlign = ContentAlignment.MiddleLeft
                        };

                        container.SetRow(labelControl, index);
                        container.Controls.Add(labelControl);
                    }

                    control.Control.Dock     = DockStyle.Fill;
                    control.Control.AutoSize = true;
                    container.SetRow(control.Control, index);
                    container.SetColumn(control.Control, 1);
                    container.Controls.Add(control.Control);
                });

                form._acceptButton.Click += (s, e) =>
                {
                    try
                    {
                        if (validate.IsObject())
                        {
                            var values = BuildValues(ui.Engine, controls);
                            var result = validate.Invoke(values);
                            if (!result.ConvertToBoolean().GetValueOrDefault())
                            {
                                return;
                            }
                        }
                        form.DialogResult = DialogResult.OK;
                    }
                    catch (JavaScriptException exception)
                    {
                        JintDebugger.ExceptionForm.Show(form, exception);
                    }
                    catch (Exception exception)
                    {
                        MessageBox.Show(
                            form,
                            new StringBuilder()
                            .AppendLine("An exception occurred while executing the script:")
                            .AppendLine()
                            .Append(exception.Message).Append(" (").Append(exception.GetType().FullName).AppendLine(")")
                            .AppendLine()
                            .AppendLine(exception.StackTrace)
                            .ToString(),
                            "Error",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Error
                            );
                    }
                };

                var owner = ui.Owner;

                using (ui.PushOwner(form))
                {
                    if (form.ShowDialog(owner) == DialogResult.OK)
                    {
                        return(BuildValues(ui.Engine, controls));
                    }
                }
            }

            return(JsValue.Null);
        }
예제 #14
0
        protected static ObjectInstance GetMemberDocumentationObject(ScriptEngine engine, MemberInfo member)
        {
            var jsDocAttributes = member.GetCustomAttributes(typeof(JSDocAttribute), false).OfType <JSDocAttribute>();

            var doc = engine.Object.Construct();

            foreach (var attribute in jsDocAttributes)
            {
                var tag = "summary";
                if (string.IsNullOrEmpty(attribute.Tag) == false)
                {
                    tag = attribute.Tag;
                }

                doc.SetPropertyValue(tag, attribute.Text, false);
            }

            if (member is MethodInfo)
            {
                var methodInfo   = member as MethodInfo;
                var methodParams = engine.Array.Construct();
                foreach (var parameter in methodInfo.GetParameters().OrderBy(p => p.Position))
                {
                    var parameterDoc = engine.Object.Construct();

                    var propertyJSDocAttributes =
                        parameter.GetCustomAttributes(typeof(JSDocAttribute), false).OfType <JSDocAttribute>();

                    foreach (var attribute in propertyJSDocAttributes)
                    {
                        var tag = "param";
                        if (string.IsNullOrEmpty(attribute.Tag) == false)
                        {
                            tag = attribute.Tag;
                        }

                        parameterDoc.SetPropertyValue(tag, attribute.Text, false);
                    }

                    parameterDoc.SetPropertyValue("name", parameter.Name, false);
                    parameterDoc.SetPropertyValue("type", parameter.ParameterType.ToString().Replace("System.", ""), false);


                    ArrayInstance.Push(methodParams, parameterDoc);
                }

                doc.SetPropertyValue("params", methodParams, false);
                if (methodInfo.ReturnType != typeof(void))
                {
                    doc.SetPropertyValue("returns", methodInfo.ReturnType.ToString().Replace("System.", ""), false);
                }
            }
            else if (member is PropertyInfo)
            {
                var propertyInfo = member as PropertyInfo;
                doc.SetPropertyValue("type", GetTypeString(propertyInfo.PropertyType), false);
                doc.SetPropertyValue("hasGetter", propertyInfo.CanRead, false);
                doc.SetPropertyValue("hasSetter", propertyInfo.CanWrite, false);
            }
            else if (member is FieldInfo)
            {
                var fieldInfo = member as FieldInfo;
                doc.SetPropertyValue("type", GetTypeString(fieldInfo.FieldType), false);
            }

            return(doc);
        }