1 module javascriptcore.Class;
2 
3 private import glib.Str;
4 private import gobject.ObjectG;
5 private import javascriptcore.Value;
6 private import javascriptcore.c.functions;
7 public  import javascriptcore.c.types;
8 
9 
10 /**
11  * A JSSClass represents a custom JavaScript class registered by the user in a #JSCContext.
12  * It allows to create new JavaScripts objects whose instances are created by the user using
13  * this API.
14  * It's possible to add constructors, properties and methods for a JSSClass by providing
15  * #GCallback<!-- -->s to implement them.
16  */
17 public class Class : ObjectG
18 {
19 	/** the main Gtk struct */
20 	protected JSCClass* jSCClass;
21 
22 	/** Get the main Gtk struct */
23 	public JSCClass* getClassStruct(bool transferOwnership = false)
24 	{
25 		if (transferOwnership)
26 			ownedRef = false;
27 		return jSCClass;
28 	}
29 
30 	/** the main Gtk struct as a void* */
31 	protected override void* getStruct()
32 	{
33 		return cast(void*)jSCClass;
34 	}
35 
36 	/**
37 	 * Sets our main struct and passes it to the parent class.
38 	 */
39 	public this (JSCClass* jSCClass, bool ownedRef = false)
40 	{
41 		this.jSCClass = jSCClass;
42 		super(cast(GObject*)jSCClass, ownedRef);
43 	}
44 
45 
46 	/** */
47 	public static GType getType()
48 	{
49 		return jsc_class_get_type();
50 	}
51 
52 	/**
53 	 * Add a constructor to @jsc_class. If @name is %NULL, the class name will be used. When <function>new</function>
54 	 * is used with the constructor or jsc_value_constructor_call() is called, @callback is invoked receiving
55 	 * a #GPtrArray of #JSCValue<!-- -->s as arguments and @user_data as the last parameter. When the constructor object
56 	 * is cleared in the #JSCClass context, @destroy_notify is called with @user_data as parameter.
57 	 *
58 	 * This function creates the constructor, which needs to be added to an object as a property to be able to use it. Use
59 	 * jsc_context_set_value() to make the constructor available in the global object.
60 	 *
61 	 * Note that the value returned by @callback is adopted by @jsc_class, and the #GDestroyNotify passed to
62 	 * jsc_context_register_class() is responsible for disposing of it.
63 	 *
64 	 * Params:
65 	 *     name = the constructor name or %NULL
66 	 *     callback = a #GCallback to be called to create an instance of @jsc_class
67 	 *     userData = user data to pass to @callback
68 	 *     destroyNotify = destroy notifier for @user_data
69 	 *     returnType = the #GType of the constructor return value
70 	 *
71 	 * Returns: a #JSCValue representing the class constructor.
72 	 */
73 	public Value addConstructorVariadic(string name, GCallback callback, void* userData, GDestroyNotify destroyNotify, GType returnType)
74 	{
75 		auto __p = jsc_class_add_constructor_variadic(jSCClass, Str.toStringz(name), callback, userData, destroyNotify, returnType);
76 
77 		if(__p is null)
78 		{
79 			return null;
80 		}
81 
82 		return ObjectG.getDObject!(Value)(cast(JSCValue*) __p, true);
83 	}
84 
85 	/**
86 	 * Add a constructor to @jsc_class. If @name is %NULL, the class name will be used. When <function>new</function>
87 	 * is used with the constructor or jsc_value_constructor_call() is called, @callback is invoked receiving the
88 	 * parameters and @user_data as the last parameter. When the constructor object is cleared in the #JSCClass context,
89 	 * @destroy_notify is called with @user_data as parameter.
90 	 *
91 	 * This function creates the constructor, which needs to be added to an object as a property to be able to use it. Use
92 	 * jsc_context_set_value() to make the constructor available in the global object.
93 	 *
94 	 * Note that the value returned by @callback is adopted by @jsc_class, and the #GDestroyNotify passed to
95 	 * jsc_context_register_class() is responsible for disposing of it.
96 	 *
97 	 * Params:
98 	 *     name = the constructor name or %NULL
99 	 *     callback = a #GCallback to be called to create an instance of @jsc_class
100 	 *     userData = user data to pass to @callback
101 	 *     destroyNotify = destroy notifier for @user_data
102 	 *     returnType = the #GType of the constructor return value
103 	 *     parameterTypes = a list of #GType<!-- -->s, one for each parameter, or %NULL
104 	 *
105 	 * Returns: a #JSCValue representing the class constructor.
106 	 */
107 	public Value addConstructorv(string name, GCallback callback, void* userData, GDestroyNotify destroyNotify, GType returnType, GType[] parameterTypes)
108 	{
109 		auto __p = jsc_class_add_constructorv(jSCClass, Str.toStringz(name), callback, userData, destroyNotify, returnType, cast(uint)parameterTypes.length, parameterTypes.ptr);
110 
111 		if(__p is null)
112 		{
113 			return null;
114 		}
115 
116 		return ObjectG.getDObject!(Value)(cast(JSCValue*) __p, true);
117 	}
118 
119 	/**
120 	 * Add method with @name to @jsc_class. When the method is called by JavaScript or jsc_value_object_invoke_method(),
121 	 * @callback is called receiving the class instance as first parameter, followed by a #GPtrArray of #JSCValue<!-- -->s
122 	 * with the method arguments and then @user_data as last parameter. When the method is cleared in the #JSCClass context,
123 	 * @destroy_notify is called with @user_data as parameter.
124 	 *
125 	 * Note that the value returned by @callback must be transfer full. In case of non-refcounted boxed types, you should use
126 	 * %G_TYPE_POINTER instead of the actual boxed #GType to ensure that the instance owned by #JSCClass is used.
127 	 * If you really want to return a new copy of the boxed type, use #JSC_TYPE_VALUE and return a #JSCValue created
128 	 * with jsc_value_new_object() that receives the copy as the instance parameter.
129 	 *
130 	 * Params:
131 	 *     name = the method name
132 	 *     callback = a #GCallback to be called to invoke method @name of @jsc_class
133 	 *     userData = user data to pass to @callback
134 	 *     destroyNotify = destroy notifier for @user_data
135 	 *     returnType = the #GType of the method return value, or %G_TYPE_NONE if the method is void.
136 	 */
137 	public void addMethodVariadic(string name, GCallback callback, void* userData, GDestroyNotify destroyNotify, GType returnType)
138 	{
139 		jsc_class_add_method_variadic(jSCClass, Str.toStringz(name), callback, userData, destroyNotify, returnType);
140 	}
141 
142 	/**
143 	 * Add method with @name to @jsc_class. When the method is called by JavaScript or jsc_value_object_invoke_method(),
144 	 * @callback is called receiving the class instance as first parameter, followed by the method parameters and then
145 	 * @user_data as last parameter. When the method is cleared in the #JSCClass context, @destroy_notify is called with
146 	 * @user_data as parameter.
147 	 *
148 	 * Note that the value returned by @callback must be transfer full. In case of non-refcounted boxed types, you should use
149 	 * %G_TYPE_POINTER instead of the actual boxed #GType to ensure that the instance owned by #JSCClass is used.
150 	 * If you really want to return a new copy of the boxed type, use #JSC_TYPE_VALUE and return a #JSCValue created
151 	 * with jsc_value_new_object() that receives the copy as the instance parameter.
152 	 *
153 	 * Params:
154 	 *     name = the method name
155 	 *     callback = a #GCallback to be called to invoke method @name of @jsc_class
156 	 *     userData = user data to pass to @callback
157 	 *     destroyNotify = destroy notifier for @user_data
158 	 *     returnType = the #GType of the method return value, or %G_TYPE_NONE if the method is void.
159 	 *     parameterTypes = a list of #GType<!-- -->s, one for each parameter, or %NULL
160 	 */
161 	public void addMethodv(string name, GCallback callback, void* userData, GDestroyNotify destroyNotify, GType returnType, GType[] parameterTypes)
162 	{
163 		jsc_class_add_methodv(jSCClass, Str.toStringz(name), callback, userData, destroyNotify, returnType, cast(uint)parameterTypes.length, parameterTypes.ptr);
164 	}
165 
166 	/**
167 	 * Add a property with @name to @jsc_class. When the property value needs to be getted, @getter is called
168 	 * receiving the the class instance as first parameter and @user_data as last parameter. When the property
169 	 * value needs to be set, @setter is called receiving the the class instance as first parameter, followed
170 	 * by the value to be set and then @user_data as the last parameter. When the property is cleared in the
171 	 * #JSCClass context, @destroy_notify is called with @user_data as parameter.
172 	 *
173 	 * Note that the value returned by @getter must be transfer full. In case of non-refcounted boxed types, you should use
174 	 * %G_TYPE_POINTER instead of the actual boxed #GType to ensure that the instance owned by #JSCClass is used.
175 	 * If you really want to return a new copy of the boxed type, use #JSC_TYPE_VALUE and return a #JSCValue created
176 	 * with jsc_value_new_object() that receives the copy as the instance parameter.
177 	 *
178 	 * Params:
179 	 *     name = the property name
180 	 *     propertyType = the #GType of the property value
181 	 *     getter = a #GCallback to be called to get the property value
182 	 *     setter = a #GCallback to be called to set the property value
183 	 *     userData = user data to pass to @getter and @setter
184 	 *     destroyNotify = destroy notifier for @user_data
185 	 */
186 	public void addProperty(string name, GType propertyType, GCallback getter, GCallback setter, void* userData, GDestroyNotify destroyNotify)
187 	{
188 		jsc_class_add_property(jSCClass, Str.toStringz(name), propertyType, getter, setter, userData, destroyNotify);
189 	}
190 
191 	/**
192 	 * Get the class name of @jsc_class
193 	 *
194 	 * Returns: the name of @jsc_class
195 	 */
196 	public string getName()
197 	{
198 		return Str.toString(jsc_class_get_name(jSCClass));
199 	}
200 
201 	/**
202 	 * Get the parent class of @jsc_class
203 	 *
204 	 * Returns: the parent class of @jsc_class
205 	 */
206 	public Class getParent()
207 	{
208 		auto __p = jsc_class_get_parent(jSCClass);
209 
210 		if(__p is null)
211 		{
212 			return null;
213 		}
214 
215 		return ObjectG.getDObject!(Class)(cast(JSCClass*) __p);
216 	}
217 }