Пример #1
0
        public void PocosCanReturnObjectInstanceDirectly()
        {
            var x = new ObjectInstance(_engine) { Extensible = true};
            x.Put("foo", new JsValue("bar"), false);

            var o = new
            {
                x
            };

            _engine.SetValue("o", o);

            RunTest(@"
                assert(o.x.foo === 'bar');
            ");
        }
Пример #2
0
		public JsValue ToJsObject(Engine engine, RavenJObject doc, string propertyName = null)
        {
            var jsObject = new ObjectInstance(engine)
                           {
                               Extensible = true
                           };

            foreach (var prop in doc)
            {
	            var propertyKey = CreatePropertyKey(prop.Key, propertyName);
				var jsValue = ToJsInstance(engine, prop.Value, propertyKey);

                var value = prop.Value as RavenJValue;
                if (value != null)
					propertiesByValue[propertyKey] = new KeyValuePair<RavenJValue, JsValue>(value, jsValue);

                jsObject.Put(prop.Key, jsValue, true);
            }
            return jsObject;
        }
Пример #3
0
        /// <summary>
        /// Loads this plugin
        /// </summary>
        public override void Load()
        {
            // Load the plugin
            LoadSource();
            if (JavaScriptEngine.GetValue(Name).TryCast<ObjectInstance>() == null) throw new Exception("Plugin is missing main object");
            Class = JavaScriptEngine.GetValue(Name).AsObject();
            if (!Class.HasProperty("Name"))
                Class.FastAddProperty("Name", Name, true, false, true);
            else
                Class.Put("Name", Name, true);

            // Read plugin attributes
            if (!Class.HasProperty("Title") || string.IsNullOrEmpty(Class.Get("Title").AsString())) throw new Exception("Plugin is missing title");
            if (!Class.HasProperty("Author") || string.IsNullOrEmpty(Class.Get("Author").AsString())) throw new Exception("Plugin is missing author");
            if (!Class.HasProperty("Version") || Class.Get("Version").ToObject() == null) throw new Exception("Plugin is missing version");
            Title = Class.Get("Title").AsString();
            Author = Class.Get("Author").AsString();
            Version = (VersionNumber) Class.Get("Version").ToObject();
            if (Class.HasProperty("Description")) Description = Class.Get("Description").AsString();
            if (Class.HasProperty("ResourceId")) ResourceId = (int)Class.Get("ResourceId").AsNumber();
            HasConfig = Class.HasProperty("HasConfig") && Class.Get("HasConfig").AsBoolean();

            // Set attributes
            Class.FastAddProperty("Plugin", JsValue.FromObject(JavaScriptEngine, this), true, false, true);

            Globals = new Dictionary<string, ICallable>();
            foreach (var property in Class.GetOwnProperties())
            {
                var callable = property.Value.Value?.TryCast<ICallable>();
                if (callable != null) Globals.Add(property.Key, callable);
            }
            foreach (var property in Class.Prototype.GetOwnProperties())
            {
                var callable = property.Value.Value?.TryCast<ICallable>();
                if (callable != null) Globals.Add(property.Key, callable);
            }
            if (!HasConfig) HasConfig = Globals.ContainsKey("LoadDefaultConfig");

            // Bind any base methods (we do it here because we don't want them to be hooked)
            BindBaseMethods();
        }