Author: toshok
Date: 2007-07-16 14:56:28 -0400 (Mon, 16 Jul 2007)
New Revision: 82055

Modified:
   trunk/moon/plugin/ChangeLog
   trunk/moon/plugin/moonlight.cs
   trunk/moon/plugin/plugin-class.cpp
   trunk/moon/plugin/plugin-class.h
   trunk/moon/plugin/plugin.cpp
Log:
2007-07-16  Chris Toshok  <[EMAIL PROTECTED]>

        * moonlight.cs: AppDomain.SetData the plugin instance so
        WebApplication can get at it.

        * plugin-class.cpp (variant_to_value): pseudo-inverse of
        value_to_variant.  used by the scriptable object stuff.

        Also, add the first pass at the scriptable object code.  it's
        really, really rough at this point.
        
        * plugin.cpp (PluginInstance::GetValue): switch this to calling
        getRootObject().
        (PluginInstance::getRootObject): put the old GetValue (create or
        retain) logic here.



Modified: trunk/moon/plugin/ChangeLog
===================================================================
--- trunk/moon/plugin/ChangeLog 2007-07-16 18:39:21 UTC (rev 82054)
+++ trunk/moon/plugin/ChangeLog 2007-07-16 18:56:28 UTC (rev 82055)
@@ -1,3 +1,19 @@
+2007-07-16  Chris Toshok  <[EMAIL PROTECTED]>
+
+       * moonlight.cs: AppDomain.SetData the plugin instance so
+       WebApplication can get at it.
+
+       * plugin-class.cpp (variant_to_value): pseudo-inverse of
+       value_to_variant.  used by the scriptable object stuff.
+
+       Also, add the first pass at the scriptable object code.  it's
+       really, really rough at this point.
+       
+       * plugin.cpp (PluginInstance::GetValue): switch this to calling
+       getRootObject().
+       (PluginInstance::getRootObject): put the old GetValue (create or
+       retain) logic here.
+
 2007-07-12  Chris Toshok  <[EMAIL PROTECTED]>
 
        * plugin-class.cpp: implement support for using a string as the

Modified: trunk/moon/plugin/moonlight.cs
===================================================================
--- trunk/moon/plugin/moonlight.cs      2007-07-16 18:39:21 UTC (rev 82054)
+++ trunk/moon/plugin/moonlight.cs      2007-07-16 18:56:28 UTC (rev 82055)
@@ -162,6 +162,9 @@
                {
                        plugin = _plugin;
                        surface = _surface;
+
+                       AppDomain.CurrentDomain.SetData ("PluginInstance", 
plugin);
+                       
                        this.filename = filename;
                        this.contents = contents;
 

Modified: trunk/moon/plugin/plugin-class.cpp
===================================================================
--- trunk/moon/plugin/plugin-class.cpp  2007-07-16 18:39:21 UTC (rev 82054)
+++ trunk/moon/plugin/plugin-class.cpp  2007-07-16 18:56:28 UTC (rev 82055)
@@ -160,7 +160,78 @@
        }
 }
 
+static void
+value_to_variant (NPObject *npobj, Value *v, NPVariant *result)
+{
+       switch (v->GetKind ()) {
+       case Type::BOOL:
+               BOOLEAN_TO_NPVARIANT (v->AsBool(), *result);
+               break;
 
+       case Type::INT32:
+               INT32_TO_NPVARIANT (v->AsInt32(), *result);
+               break;
+
+       case Type::DOUBLE:
+               DOUBLE_TO_NPVARIANT (v->AsDouble(), *result);
+               break;
+
+       case Type::STRING:
+               string_to_npvariant (v->AsString(), result);
+               break;
+
+       case Type::POINT: {
+               MoonlightPoint *point = (MoonlightPoint*)NPN_CreateObject 
(((MoonlightObject*)npobj)->instance, MoonlightPointClass);
+               point->point = *v->AsPoint ();
+               break;
+       }
+
+       case Type::RECT: {
+               MoonlightRect *rect = (MoonlightRect*)NPN_CreateObject 
(((MoonlightObject*)npobj)->instance, MoonlightRectClass);
+               rect->rect = *v->AsRect ();
+               break;
+       }
+
+       /* more builtins.. */
+       default:
+               if (v->GetKind () >= Type::DEPENDENCY_OBJECT) {
+                       MoonlightDependencyObjectObject *depobj =
+                               DependencyObjectCreateWrapper 
(((MoonlightObject*)npobj)->instance, v->AsDependencyObject ());
+                       OBJECT_TO_NPVARIANT (depobj, *result);
+               }
+               break;
+       }
+}
+
+static void
+variant_to_value (NPVariant *v, Value *result)
+{
+       switch (v->type) {
+       case NPVariantType_Void:
+               DEBUG_WARN_NOTIMPLEMENTED ();
+               break;
+       case NPVariantType_Null:
+               DEBUG_WARN_NOTIMPLEMENTED ();
+               *result = Value (Type::DEPENDENCY_OBJECT);
+               break;
+       case NPVariantType_Bool:
+               *result = Value (NPVARIANT_TO_BOOLEAN (*v));
+               break;
+       case NPVariantType_Int32:
+               *result = Value ((gint32)NPVARIANT_TO_INT32(*v));
+               break;
+       case NPVariantType_Double:
+               *result = Value (NPVARIANT_TO_DOUBLE(*v));
+               break;
+       case NPVariantType_String:
+               *result = Value (NPVARIANT_TO_STRING(*v).utf8characters);
+               break;
+       case NPVariantType_Object:
+               DEBUG_WARN_NOTIMPLEMENTED ();
+               break;
+       }
+}
+
 /*** Points ***/
 static NPObject*
 point_allocate (NPP instance, NPClass *)
@@ -759,7 +830,34 @@
 
 
 /*** MoonlightContentClass 
************************************************************/
+static NPObject*
+moonlight_content_allocate (NPP instance, NPClass*)
+{
+       return new MoonlightContentObject (instance);
+}
 
+static void
+moonlight_content_deallocate (NPObject *npobj)
+{
+       // XXX is delete broken in plugins?
+       // delete (MoonlightContentObject*)npobj;
+}
+
+static void
+moonlight_content_invalidate (NPObject *npobj)
+{
+       MoonlightContentObject *content = (MoonlightContentObject*)npobj;
+
+       /* XXX free the registered_scriptable_objects hash */
+
+       /* XXX free resizeMethodName */
+
+
+       if (content->resizeScript)
+               NPN_ReleaseObject (content->resizeScript);
+       content->resizeScript = NULL;
+}
+
 static const char *const
 moonlight_content_properties[] = {
        "actualHeight", // read only
@@ -779,7 +877,13 @@
 static bool
 moonlight_content_has_property (NPObject *npobj, NPIdentifier name)
 {
-       return HAS_PROPERTY (moonlight_content_properties, name);
+       if (HAS_PROPERTY (moonlight_content_properties, name))
+               return true;
+
+       MoonlightContentObject *content = (MoonlightContentObject*)npobj;
+
+       return g_hash_table_lookup (content->registered_scriptable_objects,
+                                   name) != NULL;
 }
 
 static bool
@@ -813,8 +917,17 @@
                NULL_TO_NPVARIANT (*result);
                return true;
        }
+       else {
+               MoonlightContentObject *content = 
(MoonlightContentObject*)npobj;
+               MoonlightScriptableObjectObject *obj =
+                       (MoonlightScriptableObjectObject*)g_hash_table_lookup 
(content->registered_scriptable_objects, name);
+               if (obj == NULL)
+                       return false;
 
-       return false;
+               NPN_RetainObject (obj);
+               OBJECT_TO_NPVARIANT (obj, *result);
+               return true;
+       }
 }
 
 static bool
@@ -904,6 +1017,10 @@
 
 MoonlightContentType::MoonlightContentType ()
 {
+       allocate = moonlight_content_allocate;
+       deallocate = moonlight_content_deallocate;
+       invalidate = moonlight_content_invalidate;
+
        hasProperty = moonlight_content_has_property;
        getProperty = moonlight_content_get_property;
        setProperty = moonlight_content_set_property;
@@ -918,49 +1035,6 @@
 
 /*** MoonlightDependencyObjectClass 
***************************************************/
 
-static void
-value_to_variant (NPObject *npobj, Value *v, NPVariant *result)
-{
-       switch (v->GetKind ()) {
-       case Type::BOOL:
-               BOOLEAN_TO_NPVARIANT (v->AsBool(), *result);
-               break;
-
-       case Type::INT32:
-               INT32_TO_NPVARIANT (v->AsInt32(), *result);
-               break;
-
-       case Type::DOUBLE:
-               DOUBLE_TO_NPVARIANT (v->AsDouble(), *result);
-               break;
-
-       case Type::STRING:
-               string_to_npvariant (v->AsString(), result);
-               break;
-
-       case Type::POINT: {
-               MoonlightPoint *point = (MoonlightPoint*)NPN_CreateObject 
(((MoonlightObject*)npobj)->instance, MoonlightPointClass);
-               point->point = *v->AsPoint ();
-               break;
-       }
-
-       case Type::RECT: {
-               MoonlightRect *rect = (MoonlightRect*)NPN_CreateObject 
(((MoonlightObject*)npobj)->instance, MoonlightRectClass);
-               rect->rect = *v->AsRect ();
-               break;
-       }
-
-       /* more builtins.. */
-       default:
-               if (v->GetKind () >= Type::DEPENDENCY_OBJECT) {
-                       MoonlightDependencyObjectObject *depobj =
-                               DependencyObjectCreateWrapper 
(((MoonlightObject*)npobj)->instance, v->AsDependencyObject ());
-                       OBJECT_TO_NPVARIANT (depobj, *result);
-               }
-               break;
-       }
-}
-
 static const char *const
 moonlight_dependency_object_methods [] = {
        "getHost",
@@ -1639,7 +1713,21 @@
 MoonlightDownloaderType* MoonlightDownloaderClass;
 
 /*** MoonlightScriptableObjectClass 
***************************************************/
+struct ScriptableProperty {
+       gpointer property_handle;
+       int property_type;
+       bool can_read;
+       bool can_write;
+};
 
+struct ScriptableMethod {
+       gpointer method_handle;
+       int method_return_type;
+       int *method_parameter_types;
+       int parameter_count;
+};
+
+
 static NPObject*
 moonlight_scriptable_object_allocate (NPP instance, NPClass*)
 {
@@ -1658,22 +1746,34 @@
 {
        MoonlightScriptableObjectObject *sobj = 
(MoonlightScriptableObjectObject*)npobj;
 
-       if (sobj->scriptable) {
+       if (sobj->managed_scriptable) {
                // XXX unref the scriptable object however we need to.
        }
-       sobj->scriptable = NULL;
+       sobj->managed_scriptable = NULL;
+
+       // XXX free the properties and methods hashes.
 }
 
 static bool
 moonlight_scriptable_object_has_property (NPObject *npobj, NPIdentifier name)
 {
-       DEBUG_WARN_NOTIMPLEMENTED ();
-       return false;
+       MoonlightScriptableObjectObject *sobj = 
(MoonlightScriptableObjectObject*)npobj;
+
+       return g_hash_table_lookup (sobj->properties, name) != NULL;
 }
 
 static bool
 moonlight_scriptable_object_get_property (NPObject *npobj, NPIdentifier name, 
NPVariant *result)
 {
+       MoonlightScriptableObjectObject *sobj = 
(MoonlightScriptableObjectObject*)npobj;
+       ScriptableProperty *prop = (ScriptableProperty*)g_hash_table_lookup 
(sobj->properties, name);
+       if (!prop)
+               return false;
+
+       NPUTF8 *strname = NPN_UTF8FromIdentifier (name);
+       DEBUGMSG ("***************** getting scriptable object property %s", 
strname);
+       NPN_MemFree (strname);
+
        DEBUG_WARN_NOTIMPLEMENTED ();
        return true;
 }
@@ -1681,6 +1781,15 @@
 static bool 
 moonlight_scriptable_object_set_property (NPObject *npobj, NPIdentifier name, 
const NPVariant *value)
 {
+       MoonlightScriptableObjectObject *sobj = 
(MoonlightScriptableObjectObject*)npobj;
+       ScriptableProperty *prop = (ScriptableProperty*)g_hash_table_lookup 
(sobj->properties, name);
+       if (!prop)
+               return false;
+
+       NPUTF8 *strname = NPN_UTF8FromIdentifier (name);
+       DEBUGMSG ("***************** setting scriptable object property %s", 
strname);
+       NPN_MemFree (strname);
+
        DEBUG_WARN_NOTIMPLEMENTED ();
        return true;
 }
@@ -1688,8 +1797,9 @@
 static bool
 moonlight_scriptable_object_has_method (NPObject *npobj, NPIdentifier name)
 {
-       DEBUG_WARN_NOTIMPLEMENTED ();
-       return false;
+       MoonlightScriptableObjectObject *sobj = 
(MoonlightScriptableObjectObject*)npobj;
+
+       return g_hash_table_lookup (sobj->methods, name) != NULL;
 }
 
 static bool
@@ -1697,6 +1807,20 @@
                                    const NPVariant *args, uint32_t argCount,
                                    NPVariant *result)
 {
+       MoonlightScriptableObjectObject *sobj = 
(MoonlightScriptableObjectObject*)npobj;
+       ScriptableMethod *method = (ScriptableMethod*)g_hash_table_lookup 
(sobj->methods, name);
+       if (!method)
+               return false;
+
+       NPUTF8 *strname = NPN_UTF8FromIdentifier (name);
+       DEBUGMSG ("***************** invoking scriptable object method %s", 
strname);
+       NPN_MemFree (strname);
+
+       Value rv;
+
+       sobj->invoke (sobj->managed_scriptable, method->method_handle, NULL, 0, 
&rv);
+
+       /* XXX do something with rv here if the return type is something other 
than void */
        DEBUG_WARN_NOTIMPLEMENTED ();
        return true;
 }
@@ -1718,7 +1842,86 @@
 
 MoonlightScriptableObjectType* MoonlightScriptableObjectClass;
 
+MoonlightScriptableObjectObject*
+moonlight_scriptable_object_wrapper_create (PluginInstance *plugin, gpointer 
scriptable,
+                                           InvokeDelegate invoke_func,
+                                           SetPropertyDelegate setprop_func,
+                                           GetPropertyDelegate getprop_func)
+{
+       MoonlightControlObject *root_object = plugin->getRootObject ();
+
+       MoonlightScriptableObjectObject* obj = 
(MoonlightScriptableObjectObject*)NPN_CreateObject 
(((MoonlightObject*)root_object)->instance,
+                                                                               
                   MoonlightScriptableObjectClass);
+
+       obj->managed_scriptable = scriptable;
+       obj->invoke = invoke_func;
+       obj->setprop = setprop_func;
+       obj->getprop = getprop_func;
+
+       DEBUGMSG ("creating scriptable object wrapper => %p", obj);
+       return obj;
+}
+
 void
+moonlight_scriptable_object_add_property (PluginInstance *plugin,
+                                         MoonlightScriptableObjectObject *obj,
+                                         gpointer property_handle,
+                                         char *property_name,
+                                         int property_type,
+                                         bool can_read,
+                                         bool can_write)
+{
+       DEBUGMSG ("adding property named %s to scriptable object %p\n", 
property_name, obj);
+
+       ScriptableProperty *prop = new ScriptableProperty ();
+       prop->property_handle = property_handle;
+       prop->property_type = property_type;
+       prop->can_read = can_read;
+       prop->can_write = can_write;
+
+       g_hash_table_insert (obj->properties, NPID(property_name), prop);
+}
+
+void
+moonlight_scriptable_object_add_method (PluginInstance *plugin,
+                                       MoonlightScriptableObjectObject *obj,
+                                       gpointer method_handle,
+                                       char *method_name,
+                                       int method_return_type,
+                                       int *method_parameter_types,
+                                       int parameter_count)
+
+{
+       DEBUGMSG ("adding method named %s to scriptable object %p\n", 
method_name, obj);
+
+       ScriptableMethod *method = new ScriptableMethod ();
+       method->method_handle = method_handle;
+       method->method_return_type = method_return_type;
+       method->method_parameter_types = new int[parameter_count];
+       memcpy (method->method_parameter_types, method_parameter_types, sizeof 
(int) * parameter_count);
+       method->parameter_count = parameter_count;
+
+       g_hash_table_insert (obj->methods, NPID(method_name), method);
+
+}
+
+void
+moonlight_scriptable_object_register (PluginInstance *plugin,
+                                     char *name,
+                                     MoonlightScriptableObjectObject *obj)
+{
+       DEBUGMSG ("registering scriptable object '%s' => %p", name, obj);
+
+       MoonlightContentObject* content = 
(MoonlightContentObject*)plugin->getRootObject()->content;
+
+       g_hash_table_insert (content->registered_scriptable_objects,
+                            NPID (name),
+                            obj);
+
+       DEBUGMSG (" => done");
+}
+
+void
 plugin_init_classes ()
 {
        MoonlightPointClass = new MoonlightPointType ();

Modified: trunk/moon/plugin/plugin-class.h
===================================================================
--- trunk/moon/plugin/plugin-class.h    2007-07-16 18:39:21 UTC (rev 82054)
+++ trunk/moon/plugin/plugin-class.h    2007-07-16 18:56:28 UTC (rev 82055)
@@ -100,8 +100,11 @@
            resizeScript (NULL), resizeMethodName (NULL),
            resizeIsScript (false), resizeSet (false)
        {
+               registered_scriptable_objects = g_hash_table_new 
(g_direct_hash, g_direct_equal);
        }
 
+       GHashTable *registered_scriptable_objects;
+
        NPObject *resizeScript;
        char *resizeMethodName;
        bool resizeIsScript;
@@ -187,6 +190,10 @@
 
 /*** MoonlightScriptableObject 
***************************************************/
 
+typedef void (*InvokeDelegate) (gpointer obj_handle, gpointer method_handle, 
Value** args, int arg_count, Value* return_value);
+typedef void (*SetPropertyDelegate) (gpointer obj_handle, gpointer 
property_handle, Value value);
+typedef void (*GetPropertyDelegate) (gpointer obj_handle, gpointer 
property_handle, Value *value);
+
 struct MoonlightScriptableObjectType : MoonlightObjectType {
        MoonlightScriptableObjectType ();
 };
@@ -197,12 +204,47 @@
 {
        MoonlightScriptableObjectObject (NPP instance) : MoonlightObject 
(instance)
        {
-               scriptable = NULL;
+               managed_scriptable = NULL;
+               properties = g_hash_table_new (g_direct_hash, g_direct_equal);
+               methods = g_hash_table_new (g_direct_hash, g_direct_equal);
        }
 
-       // XXX this should be a MonoObject?  what does a GCHandle in
-       // p/invoke result in on the unmanaged side?
-       void *scriptable;
+       gpointer managed_scriptable;
+       GHashTable *properties;
+       GHashTable *methods;
+
+       InvokeDelegate invoke;
+       SetPropertyDelegate setprop;
+       GetPropertyDelegate getprop;
 };
 
+extern "C" {
+       // These are meant to be called by System.Silverlight.dll
+
+       MoonlightScriptableObjectObject* 
moonlight_scriptable_object_wrapper_create (PluginInstance *plugin, gpointer 
scriptable,
+                                                                               
     InvokeDelegate invoke,
+                                                                               
     SetPropertyDelegate setprop,
+                                                                               
     GetPropertyDelegate getprop);
+
+       void moonlight_scriptable_object_add_property (PluginInstance *plugin,
+                                                      
MoonlightScriptableObjectObject *obj,
+                                                      gpointer property_handle,
+                                                      char *property_name,
+                                                      int property_type,
+                                                      bool can_read,
+                                                      bool can_write);
+
+       void moonlight_scriptable_object_add_method (PluginInstance *plugin,
+                                                    
MoonlightScriptableObjectObject *obj,
+                                                    gpointer method_handle,
+                                                    char *method_name,
+                                                    int method_return_type,
+                                                    int 
*method_parameter_types,
+                                                    int parameter_count);
+
+       void moonlight_scriptable_object_register (PluginInstance *plugin,
+                                                  char *name,
+                                                  
MoonlightScriptableObjectObject *obj);
+}
+
 #endif /* PLUGIN_CLASS */

Modified: trunk/moon/plugin/plugin.cpp
===================================================================
--- trunk/moon/plugin/plugin.cpp        2007-07-16 18:39:21 UTC (rev 82054)
+++ trunk/moon/plugin/plugin.cpp        2007-07-16 18:56:28 UTC (rev 82055)
@@ -164,12 +164,7 @@
                        break;
 
                case NPPVpluginScriptableNPObject:
-                       if (rootobject == NULL)
-                               rootobject = NPN_CreateObject (instance, 
MoonlightControlClass);
-                       else
-                               NPN_RetainObject (rootobject);
-
-                       *((NPObject **) result) = rootobject;
+                       *((NPObject**) result) = getRootObject ();
                        break;
                default:
                        err = NPERR_INVALID_PARAM;
@@ -550,7 +545,10 @@
 MoonlightControlObject *
 PluginInstance::getRootObject ()
 {
-       NPN_RetainObject (rootobject);
+       if (rootobject == NULL)
+               rootobject = NPN_CreateObject (instance, MoonlightControlClass);
+       else
+               NPN_RetainObject (rootobject);
        return (MoonlightControlObject*)rootobject;
 }
 

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to