[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef PPAPI_C_PPB_VAR_H_ |
| 6 | #define PPAPI_C_PPB_VAR_H_ |
| 7 | |
[email protected] | 6f2e391 | 2010-11-05 14:45:44 | [diff] [blame^] | 8 | #include "ppapi/c/pp_bool.h" |
[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 9 | #include "ppapi/c/pp_instance.h" |
[email protected] | 6f2e391 | 2010-11-05 14:45:44 | [diff] [blame^] | 10 | #include "ppapi/c/pp_macros.h" |
[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 11 | #include "ppapi/c/pp_module.h" |
| 12 | #include "ppapi/c/pp_resource.h" |
| 13 | #include "ppapi/c/pp_stdint.h" |
| 14 | #include "ppapi/c/pp_var.h" |
| 15 | |
[email protected] | 6f2e391 | 2010-11-05 14:45:44 | [diff] [blame^] | 16 | #define PPB_VAR_INTERFACE "PPB_Var;0.2" |
[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 17 | |
| 18 | /** |
| 19 | * @file |
| 20 | * Defines the PPB_Var struct. |
| 21 | * See http://code.google.com/p/ppapi/wiki/InterfacingWithJavaScript |
| 22 | * for general information on using this interface. |
| 23 | * {PENDING: Should the generated doc really be pointing to methods?} |
| 24 | * |
| 25 | * @addtogroup PPB |
| 26 | * @{ |
| 27 | */ |
| 28 | |
| 29 | enum PP_ObjectProperty_Modifier { |
| 30 | PP_OBJECTPROPERTY_MODIFIER_NONE = 0, |
| 31 | PP_OBJECTPROPERTY_MODIFIER_READONLY = 1 << 0, |
| 32 | PP_OBJECTPROPERTY_MODIFIER_DONTENUM = 1 << 1, |
| 33 | PP_OBJECTPROPERTY_MODIFIER_DONTDELETE = 1 << 2, |
| 34 | PP_OBJECTPROPERTY_MODIFIER_HASVALUE = 1 << 3 |
| 35 | }; |
| 36 | |
| 37 | struct PP_ObjectProperty { |
[email protected] | 6f2e391 | 2010-11-05 14:45:44 | [diff] [blame^] | 38 | struct PP_Var name; |
| 39 | struct PP_Var value; |
| 40 | struct PP_Var getter; |
| 41 | struct PP_Var setter; |
[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 42 | uint32_t modifiers; |
| 43 | }; |
| 44 | |
| 45 | /** |
| 46 | * PPB_Var API |
| 47 | * |
| 48 | * JavaScript specification: |
| 49 | * |
| 50 | * When referencing JS specification, we will refer to ECMAScript, 5th edition, |
| 51 | * and we will put section numbers in square brackets. |
| 52 | * |
| 53 | * Exception handling: |
| 54 | * |
| 55 | * If an exception parameter is NULL, then any exceptions that happen during the |
| 56 | * execution of the function will be ignored. If it is non-NULL, and has a type |
| 57 | * of PP_VARTYPE_UNDEFINED, then if an exception is thrown it will be stored in |
| 58 | * the exception variable. It it is non-NULL and not PP_VARTYPE_UNDEFINED, then |
| 59 | * the function is a no-op, and, if it returns a value, it will return |
| 60 | * PP_VARTYPE_UNDEFINED. This can be used to chain together multiple calls and |
| 61 | * only check the exception at the end. |
| 62 | * |
| 63 | * Make sure not to intermix non-JS with JS calls when relying on this behavior |
| 64 | * to catch JS exceptions, as non-JS functions will still execute! |
| 65 | |
| 66 | * JS engine's exceptions will always be of type PP_VARTYPE_OBJECT. However, |
| 67 | * PP_Var interface can also throw PP_VARTYPE_STRING exceptions, in situations |
| 68 | * where there's no JS execution context defined. These are usually invalid |
| 69 | * parameter errors - passing an invalid PP_Var value, for example, will always |
| 70 | * result in an PP_VARTYPE_STRING exception. Exceptions will not be of any other |
| 71 | * type. |
| 72 | * TODO(neb): Specify the exception for ill-formed PP_Vars, invalid module, |
| 73 | * instance, resource, string and object ids. |
| 74 | */ |
| 75 | struct PPB_Var { |
| 76 | /** |
| 77 | * Adds a reference to the given var. If this is not a refcounted object, |
| 78 | * this function will do nothing so you can always call it no matter what the |
| 79 | * type. |
| 80 | */ |
| 81 | void (*AddRef)(struct PP_Var var); |
| 82 | |
| 83 | /** |
| 84 | * Removes a reference to given var, deleting it if the internal refcount |
| 85 | * becomes 0. If the given var is not a refcounted object, this function will |
| 86 | * do nothing so you can always call it no matter what the type. |
| 87 | */ |
| 88 | void (*Release)(struct PP_Var var); |
| 89 | |
| 90 | /** |
| 91 | * Creates a string var from a string. The string must be encoded in valid |
| 92 | * UTF-8 and is NOT NULL-terminated, the length must be specified in |len|. |
| 93 | * It is an error if the string is not valid UTF-8. |
| 94 | * |
| 95 | * If the length is 0, the |data| pointer will not be dereferenced and may |
| 96 | * be NULL. Note, however, that if you do this, the "NULL-ness" will not be |
| 97 | * preserved, as VarToUtf8 will never return NULL on success, even for empty |
| 98 | * strings. |
| 99 | * |
| 100 | * The resulting object will be a refcounted string object. It will be |
| 101 | * AddRef()ed for the caller. When the caller is done with it, it should be |
| 102 | * Release()d. |
| 103 | * |
| 104 | * On error (basically out of memory to allocate the string, or input that |
| 105 | * is not valid UTF-8), this function will return a Null var. |
| 106 | */ |
| 107 | struct PP_Var (*VarFromUtf8)(PP_Module module, |
| 108 | const char* data, uint32_t len); |
| 109 | |
| 110 | /** |
| 111 | * Converts a string-type var to a char* encoded in UTF-8. This string is NOT |
| 112 | * NULL-terminated. The length will be placed in |*len|. If the string is |
| 113 | * valid but empty the return value will be non-NULL, but |*len| will still |
| 114 | * be 0. |
| 115 | * |
| 116 | * If the var is not a string, this function will return NULL and |*len| will |
| 117 | * be 0. |
| 118 | * |
| 119 | * The returned buffer will be valid as long as the underlying var is alive. |
| 120 | * If the plugin frees its reference, the string will be freed and the pointer |
| 121 | * will be to random memory. |
| 122 | */ |
| 123 | const char* (*VarToUtf8)(struct PP_Var var, uint32_t* len); |
| 124 | |
| 125 | /** |
| 126 | * Convert a variable to a different type using rules from ECMAScript |
| 127 | * specification, section [9]. |
| 128 | * |
| 129 | * For conversions from/to PP_VARTYPE_OBJECT, the instance must be specified, |
| 130 | * or an exception of type PP_VARTYPE_STRING will be thrown. |
| 131 | */ |
[email protected] | 6f2e391 | 2010-11-05 14:45:44 | [diff] [blame^] | 132 | struct PP_Var (*ConvertType)(PP_Instance instance, |
| 133 | struct PP_Var var, |
| 134 | PP_VarType new_type, |
| 135 | struct PP_Var* exception); |
[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 136 | |
| 137 | /** |
| 138 | * Sets a property on the object, similar to Object.prototype.defineProperty. |
| 139 | * |
| 140 | * First, if object is not PP_VARTYPE_OBJECT, throw an exception. |
| 141 | * TODO(neb): Specify the exception. Ideally, it would be a TypeError, but |
| 142 | * don't have the JS context to create new objects, we might throw a string. |
| 143 | * Then, the property's 'name' field is converted to string using |
| 144 | * ConvertType (ToString [9.8]). |
| 145 | * After that, defineOwnProperty [8.12.9, 15.4.5.1] is called with the |
| 146 | * property. |
| 147 | * To set a simple property, set the value and set modifiers to default |
| 148 | * (Writable|Enumerable|Configurable|HasValue), see [8.12.15] and |
| 149 | * function PPB_MakeSimpleProperty. |
| 150 | */ |
| 151 | void (*DefineProperty)(struct PP_Var object, |
| 152 | struct PP_ObjectProperty property, |
[email protected] | 6f2e391 | 2010-11-05 14:45:44 | [diff] [blame^] | 153 | struct PP_Var* exception); |
[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 154 | |
| 155 | /** |
| 156 | * Tests whether an object has a property with a given name. |
| 157 | * |
| 158 | * First, if object is not PP_VARTYPE_OBJECT, throw an exception. |
| 159 | * TODO(neb): Specify the exception. Ideally, it would be a TypeError, but |
| 160 | * don't have the JS context to create new objects, we might throw a string. |
| 161 | * Then, convert 'property' to string using ConvertType (ToString [9.8]). |
| 162 | * Then return true if the given property exists on the object [8.12.6]. |
| 163 | */ |
[email protected] | 6f2e391 | 2010-11-05 14:45:44 | [diff] [blame^] | 164 | PP_Bool (*HasProperty)(struct PP_Var object, |
| 165 | struct PP_Var property, |
| 166 | struct PP_Var* exception); |
[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 167 | |
| 168 | /** |
| 169 | * Returns a given property of the object. |
| 170 | * |
| 171 | * First, if object is not PP_VARTYPE_OBJECT, throw an exception. |
| 172 | * TODO(neb): Specify the exception. Ideally, it would be a TypeError, but |
| 173 | * don't have the JS context to create new objects, we might throw a string. |
| 174 | * Then, convert 'property' to string using ConvertType (ToString [9.8]). |
| 175 | * Then return the given property of the object [8.12.2]. |
| 176 | */ |
[email protected] | 6f2e391 | 2010-11-05 14:45:44 | [diff] [blame^] | 177 | struct PP_Var (*GetProperty)(struct PP_Var object, |
| 178 | struct PP_Var property, |
| 179 | struct PP_Var* exception); |
[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 180 | |
| 181 | /** |
| 182 | * Delete a property from the object, return true if succeeded. |
| 183 | * |
| 184 | * True is returned if the property didn't exist in the first place. |
| 185 | * |
| 186 | * First, if object is not PP_VARTYPE_OBJECT, throw an exception. |
| 187 | * TODO(neb): Specify the exception. Ideally, it would be a TypeError, but |
| 188 | * don't have the JS context to create new objects, we might throw a string. |
| 189 | * Then, convert 'property' to string using ConvertType (ToString [9.8]). |
| 190 | * Then delete the given property of the object [8.12.7]. |
| 191 | */ |
[email protected] | 6f2e391 | 2010-11-05 14:45:44 | [diff] [blame^] | 192 | PP_Bool (*DeleteProperty)(struct PP_Var object, |
| 193 | struct PP_Var property, |
| 194 | struct PP_Var* exception); |
[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 195 | |
| 196 | /** |
| 197 | * Retrieves all property names on the given object. Property names include |
| 198 | * methods. |
| 199 | * |
| 200 | * If object is not PP_VARTYPE_OBJECT, throw an exception. |
| 201 | * TODO(neb): Specify the exception. Ideally, it would be a TypeError, but |
| 202 | * don't have the JS context to create new objects, we might throw a string. |
| 203 | * |
| 204 | * If there is a failure, the given exception will be set (if it is non-NULL). |
| 205 | * On failure, |*properties| will be set to NULL and |*property_count| will be |
| 206 | * set to 0. |
| 207 | * |
| 208 | * A pointer to the array of property names will be placed in |*properties|. |
| 209 | * The caller is responsible for calling Release() on each of these properties |
| 210 | * (as per normal refcounted memory management) as well as freeing the array |
| 211 | * pointer with PPB_Core.MemFree(). |
| 212 | * |
| 213 | * This function returns all "enumerable" properties. Some JavaScript |
| 214 | * properties are "hidden" and these properties won't be retrieved by this |
| 215 | * function, yet you can still set and get them. You can use JS |
| 216 | * Object.getOwnPropertyNames() to access these properties. |
| 217 | * |
| 218 | * Example: |
| 219 | * <pre> uint32_t count; |
| 220 | * PP_Var* properties; |
| 221 | * ppb_var.EnumerateProperties(object, &count, &properties); |
| 222 | * |
| 223 | * ...use the properties here... |
| 224 | * |
| 225 | * for (uint32_t i = 0; i < count; i++) |
| 226 | * ppb_var.Release(properties[i]); |
| 227 | * ppb_core.MemFree(properties); </pre> |
| 228 | */ |
| 229 | void (*EnumerateProperties)(struct PP_Var object, |
| 230 | uint32_t* property_count, |
| 231 | struct PP_Var** properties, |
| 232 | struct PP_Var* exception); |
| 233 | |
| 234 | /** |
| 235 | * Check if an object is a JS Function [9.11]. |
| 236 | */ |
[email protected] | 6f2e391 | 2010-11-05 14:45:44 | [diff] [blame^] | 237 | PP_Bool (*IsCallable)(struct PP_Var object); |
[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 238 | |
| 239 | /** |
| 240 | * Call the functions. |
| 241 | * |
| 242 | * Similar to Function.prototype.call [15.3.4.4]. It will throw a TypeError |
| 243 | * and return undefined if object is not PP_VARTYPE_OBJECT, or is not |
| 244 | * callable. |
| 245 | * |
| 246 | * Pass the arguments to the function in order in the |argv| array, and the |
| 247 | * number of arguments in the |argc| parameter. |argv| can be NULL if |argc| |
| 248 | * is zero. |
| 249 | * |
| 250 | * Example: |
| 251 | * Call(obj.GetProperty("DoIt"), obj, 0, NULL, NULL) |
| 252 | * Equivalent to obj.DoIt() in JavaScript. |
| 253 | * |
| 254 | * Call(obj, PP_MakeUndefined(), 0, NULL, NULL) |
| 255 | * Equivalent to obj() in JavaScript. |
| 256 | */ |
| 257 | struct PP_Var (*Call)(struct PP_Var object, |
| 258 | struct PP_Var this_object, |
| 259 | uint32_t argc, |
| 260 | struct PP_Var* argv, |
| 261 | struct PP_Var* exception); |
| 262 | |
| 263 | /** |
| 264 | * Invoke the object as a constructor. It will throw a |TypeError| and return |
| 265 | * |undefined| if |object| is not PP_VARTYPE_OBJECT, or cannot be used as a |
| 266 | * constructor. |
| 267 | * |
| 268 | * Pass the arguments to the function in order in the |argv| array, and the |
| 269 | * number of arguments in the |argc| parameter. |argv| can be NULL if |argc| |
| 270 | * is zero. |
| 271 | * |
| 272 | * For example, if |object| is |String|, this is like saying |new String| in |
| 273 | * JavaScript. Similar to the [[Construct]] internal method [13.2.2]. |
| 274 | * |
| 275 | * For examples, to construct an empty object, do: |
| 276 | * GetWindow().GetProperty("Object").Construct(0, NULL); |
| 277 | */ |
| 278 | struct PP_Var (*Construct)(struct PP_Var object, |
| 279 | uint32_t argc, |
| 280 | struct PP_Var* argv, |
| 281 | struct PP_Var* exception); |
| 282 | }; |
| 283 | |
[email protected] | 6f2e391 | 2010-11-05 14:45:44 | [diff] [blame^] | 284 | PP_INLINE struct PP_ObjectProperty PP_MakeSimpleProperty(struct PP_Var name, |
| 285 | struct PP_Var value) { |
| 286 | struct PP_ObjectProperty result; |
| 287 | result.name = name; |
| 288 | result.value = value; |
| 289 | result.getter = PP_MakeUndefined(); |
| 290 | result.setter = PP_MakeUndefined(); |
| 291 | result.modifiers = PP_OBJECTPROPERTY_MODIFIER_HASVALUE; |
[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 292 | return result; |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * @} |
| 297 | * End addtogroup PPB |
| 298 | */ |
| 299 | #endif // PPAPI_C_PPB_VAR_H_ |