Home | History | Annotate | Download | only in c
      1 /* Copyright (c) 2012 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 
      6 /* From pp_var.idl modified Thu Sep 12 16:41:36 2013. */
      7 
      8 #ifndef PPAPI_C_PP_VAR_H_
      9 #define PPAPI_C_PP_VAR_H_
     10 
     11 #include "ppapi/c/pp_bool.h"
     12 #include "ppapi/c/pp_macros.h"
     13 #include "ppapi/c/pp_stdint.h"
     14 
     15 /**
     16  * @file
     17  * This file defines the API for handling the passing of data types between
     18  * your module and the page.
     19  */
     20 
     21 
     22 /**
     23  * @addtogroup Enums
     24  * @{
     25  */
     26 /**
     27  * The <code>PP_VarType</code> is an enumeration of the different types that
     28  * can be contained within a <code>PP_Var</code> structure.
     29  */
     30 typedef enum {
     31   /**
     32    * An undefined value.
     33    */
     34   PP_VARTYPE_UNDEFINED = 0,
     35   /**
     36    * A NULL value. This is similar to undefined, but JavaScript differentiates
     37    * the two so it is exposed here as well.
     38    */
     39   PP_VARTYPE_NULL = 1,
     40   /**
     41    * A boolean value, use the <code>as_bool</code> member of the var.
     42    */
     43   PP_VARTYPE_BOOL = 2,
     44   /**
     45    * A 32-bit integer value. Use the <code>as_int</code> member of the var.
     46    */
     47   PP_VARTYPE_INT32 = 3,
     48   /**
     49    * A double-precision floating point value. Use the <code>as_double</code>
     50    * member of the var.
     51    */
     52   PP_VARTYPE_DOUBLE = 4,
     53   /**
     54    * The Var represents a string. The <code>as_id</code> field is used to
     55    * identify the string, which may be created and retrieved from the
     56    * <code>PPB_Var</code> interface. These objects are reference counted, so
     57    * AddRef and Release must be used properly to avoid memory leaks.
     58    */
     59   PP_VARTYPE_STRING = 5,
     60   /**
     61    * Represents a JavaScript object. This vartype is not currently usable
     62    * from modules, although it is used internally for some tasks. These objects
     63    * are reference counted, so AddRef and Release must be used properly to avoid
     64    * memory leaks.
     65    */
     66   PP_VARTYPE_OBJECT = 6,
     67   /**
     68    * Represents an array of Vars. The <code>as_id</code> field is used to
     69    * identify the array, which may be created and manipulated from the
     70    * <code>PPB_VarArray</code> interface. These objects are reference counted,
     71    * so AddRef and Release must be used properly to avoid memory leaks.
     72    */
     73   PP_VARTYPE_ARRAY = 7,
     74   /**
     75    * Represents a mapping from strings to Vars. The <code>as_id</code> field is
     76    * used to identify the dictionary, which may be created and manipulated from
     77    * the <code>PPB_VarDictionary</code> interface. These objects are reference
     78    * counted, so AddRef and Release must be used properly to avoid memory leaks.
     79    */
     80   PP_VARTYPE_DICTIONARY = 8,
     81   /**
     82    * ArrayBuffer represents a JavaScript ArrayBuffer. This is the type which
     83    * represents Typed Arrays in JavaScript. Unlike JavaScript 'Array', it is
     84    * only meant to contain basic numeric types, and is always stored
     85    * contiguously. See PPB_VarArrayBuffer_Dev for functions special to
     86    * ArrayBuffer vars. These objects are reference counted, so AddRef and
     87    * Release must be used properly to avoid memory leaks.
     88    */
     89   PP_VARTYPE_ARRAY_BUFFER = 9,
     90   /**
     91    * Resources are not currently supported but will be added in the future
     92    * These objects are reference counted, so AddRef and Release must be used
     93    * properly to avoid memory leaks.
     94    */
     95   PP_VARTYPE_RESOURCE = 10
     96 } PP_VarType;
     97 PP_COMPILE_ASSERT_SIZE_IN_BYTES(PP_VarType, 4);
     98 /**
     99  * @}
    100  */
    101 
    102 /**
    103  * @addtogroup Structs
    104  * @{
    105  */
    106 /**
    107  * The PP_VarValue union stores the data for any one of the types listed
    108  * in the PP_VarType enum.
    109  */
    110 union PP_VarValue {
    111   /**
    112    * If <code>type</code> is <code>PP_VARTYPE_BOOL</code>,
    113    * <code>as_bool</code> represents the value of this <code>PP_Var</code> as
    114    * <code>PP_Bool</code>.
    115    */
    116   PP_Bool as_bool;
    117   /**
    118    * If <code>type</code> is <code>PP_VARTYPE_INT32</code>,
    119    * <code>as_int</code> represents the value of this <code>PP_Var</code> as
    120    * <code>int32_t</code>.
    121    */
    122   int32_t as_int;
    123   /**
    124    * If <code>type</code> is <code>PP_VARTYPE_DOUBLE</code>,
    125    * <code>as_double</code> represents the value of this <code>PP_Var</code>
    126    * as <code>double</code>.
    127    */
    128   double as_double;
    129   /**
    130    * If <code>type</code> is <code>PP_VARTYPE_STRING</code>,
    131    * <code>PP_VARTYPE_OBJECT</code>, <code>PP_VARTYPE_ARRAY</code>,
    132    * <code>PP_VARTYPE_DICTIONARY</code>, <code>PP_VARTYPE_ARRAY_BUFFER</code>,
    133    * or <code>PP_VARTYPE_RESOURCE</code>, <code>as_id</code> represents the
    134    * value of this <code>PP_Var</code> as an opaque handle assigned by the
    135    * browser. This handle is guaranteed never to be 0, so a module can
    136    * initialize this ID to 0 to indicate a "NULL handle."
    137    */
    138   int64_t as_id;
    139 };
    140 
    141 /**
    142  * The <code>PP_VAR</code> struct is a variant data type and can contain any
    143  * value of one of the types named in the <code>PP_VarType</code> enum. This
    144  * structure is for passing data between native code which can be strongly
    145  * typed and the browser (JavaScript) which isn't strongly typed.
    146  *
    147  * JavaScript has a "number" type for holding a number, and does not
    148  * differentiate between floating point and integer numbers. The
    149  * JavaScript operations will try to optimize operations by using
    150  * integers when possible, but could end up with doubles. Therefore,
    151  * you can't assume a numeric <code>PP_Var</code> will be the type you expect.
    152  * Your code should be capable of handling either int32_t or double for numeric
    153  * PP_Vars sent from JavaScript.
    154  */
    155 struct PP_Var {
    156   PP_VarType type;
    157   /**
    158    * The <code>padding</code> ensures <code>value</code> is aligned on an
    159    * 8-byte boundary relative to the start of the struct. Some compilers
    160    * align doubles on 8-byte boundaries for 32-bit x86, and some align on
    161    * 4-byte boundaries.
    162    */
    163   int32_t padding;
    164   /**
    165    * This <code>value</code> represents the contents of the PP_Var. Only one of
    166    * the fields of <code>value</code> is valid at a time based upon
    167    * <code>type</code>.
    168    */
    169   union PP_VarValue value;
    170 };
    171 PP_COMPILE_ASSERT_STRUCT_SIZE_IN_BYTES(PP_Var, 16);
    172 /**
    173  * @}
    174  */
    175 
    176 /**
    177  * @addtogroup Functions
    178  * @{
    179  */
    180 
    181 /**
    182  * PP_MakeUndefined() is used to wrap an undefined value into a
    183  * <code>PP_Var</code> struct for passing to the browser.
    184  *
    185  * @return A <code>PP_Var</code> structure.
    186  */
    187 PP_INLINE struct PP_Var PP_MakeUndefined(void) {
    188   struct PP_Var result = { PP_VARTYPE_UNDEFINED, 0, {PP_FALSE} };
    189   return result;
    190 }
    191 
    192 /**
    193  * PP_MakeNull() is used to wrap a null value into a
    194  * <code>PP_Var</code> struct for passing to the browser.
    195  *
    196  * @return A <code>PP_Var</code> structure,
    197  */
    198 PP_INLINE struct PP_Var PP_MakeNull(void) {
    199   struct PP_Var result = { PP_VARTYPE_NULL, 0, {PP_FALSE} };
    200   return result;
    201 }
    202 
    203 /**
    204  * PP_MakeBool() is used to wrap a boolean value into a
    205  * <code>PP_Var</code> struct for passing to the browser.
    206  *
    207  * @param[in] value A <code>PP_Bool</code> enumeration to
    208  * wrap.
    209  *
    210  * @return A <code>PP_Var</code> structure.
    211  */
    212 PP_INLINE struct PP_Var PP_MakeBool(PP_Bool value) {
    213   struct PP_Var result = { PP_VARTYPE_BOOL, 0, {PP_FALSE} };
    214   result.value.as_bool = value;
    215   return result;
    216 }
    217 
    218 /**
    219  * PP_MakeInt32() is used to wrap a 32 bit integer value
    220  * into a <code>PP_Var</code> struct for passing to the browser.
    221  *
    222  * @param[in] value An int32 to wrap.
    223  *
    224  * @return A <code>PP_Var</code> structure.
    225  */
    226 PP_INLINE struct PP_Var PP_MakeInt32(int32_t value) {
    227   struct PP_Var result = { PP_VARTYPE_INT32, 0, {PP_FALSE} };
    228   result.value.as_int = value;
    229   return result;
    230 }
    231 
    232 /**
    233  * PP_MakeDouble() is used to wrap a double value into a
    234  * <code>PP_Var</code> struct for passing to the browser.
    235  *
    236  * @param[in] value A double to wrap.
    237  *
    238  * @return A <code>PP_Var</code> structure.
    239  */
    240 PP_INLINE struct PP_Var PP_MakeDouble(double value) {
    241   struct PP_Var result = { PP_VARTYPE_DOUBLE, 0, {PP_FALSE} };
    242   result.value.as_double = value;
    243   return result;
    244 }
    245 /**
    246  * @}
    247  */
    248 
    249 #endif  /* PPAPI_C_PP_VAR_H_ */
    250 
    251