]>
Commit | Line | Data |
---|---|---|
769a03c3 MF |
1 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) |
2 | #define MAX(a, b) (((a) > (b)) ? (a) : (b)) | |
3 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a))) | |
4 | ||
5 | /** | |
6 | * Creates new string PP_Var from C string. The resulting object will be a | |
7 | * refcounted string object. It will be AddRef()ed for the caller. When the | |
8 | * caller is done with it, it should be Release()d. | |
9 | * @param[in] str C string to be converted to PP_Var | |
10 | * @return PP_Var containing string. | |
11 | */ | |
12 | static struct PP_Var CStrToVar(const char *str) | |
13 | { | |
14 | if (ppb_var_interface) | |
15 | return ppb_var_interface->VarFromUtf8(str, strlen(str)); | |
16 | else | |
17 | return PP_MakeUndefined(); | |
18 | } | |
19 | ||
20 | /** | |
21 | * Convert a PP_Var to a C string, given a buffer. | |
22 | * @param[in] var The PP_Var to convert. | |
23 | * @param[out] buffer The buffer to write to. | |
24 | * @param[in] length The length of |buffer|. | |
25 | * @return The number of characters written. | |
26 | */ | |
27 | static uint32_t VarToCStr(struct PP_Var var, char *buffer, uint32_t length) | |
28 | { | |
29 | if (ppb_var_interface == NULL) | |
30 | return 0; | |
31 | ||
32 | uint32_t var_length; | |
33 | const char* str = ppb_var_interface->VarToUtf8(var, &var_length); | |
34 | /* str is NOT NULL-terminated. Copy using memcpy. */ | |
35 | uint32_t min_length = MIN(var_length, length - 1); | |
36 | memcpy(buffer, str, min_length); | |
37 | buffer[min_length] = 0; | |
38 | ||
39 | return min_length; | |
40 | } | |
41 | ||
42 | /** | |
43 | * Printf to a newly allocated C string. | |
44 | * @param[in] format A printf format string. | |
45 | * @param[in] args The printf arguments. | |
46 | * @return The newly constructed string. Caller takes ownership. */ | |
47 | static char *VprintfToNewString(const char *format, va_list args) | |
48 | { | |
49 | va_list args_copy; | |
50 | int length; | |
51 | char *buffer; | |
52 | int result; | |
53 | ||
54 | va_copy(args_copy, args); | |
55 | length = vsnprintf(NULL, 0, format, args); | |
56 | buffer = malloc(length + 1); /* +1 for NULL-terminator. */ | |
57 | result = vsnprintf(&buffer[0], length + 1, format, args_copy); | |
58 | if (result != length) { | |
59 | assert(0); | |
60 | return NULL; | |
61 | } | |
62 | return buffer; | |
63 | } | |
64 | ||
65 | /** | |
66 | * Send a message to the JavaScript Console | |
67 | */ | |
68 | static void console_log(const char *str, PP_LogLevel level) | |
69 | { | |
70 | if (ppb_console_interface) { | |
71 | struct PP_Var var = CStrToVar(str); | |
72 | ppb_console_interface->Log(pp_instance, level, var); | |
73 | ppb_var_interface->Release(var); | |
74 | } | |
75 | } | |
76 | #define _Console(lvl) \ | |
77 | static void _console_##lvl(const char *s) { console_log(s, PP_LOGLEVEL_##lvl); } | |
78 | _Console(TIP) | |
79 | _Console(LOG) | |
80 | _Console(WARNING) | |
81 | _Console(ERROR) | |
82 | #define Console(lvl) _console_##lvl | |
83 | ||
84 | /* | |
85 | * Create a javaascript like console object: | |
86 | * console.log('foo'); | |
87 | */ | |
88 | static struct { | |
89 | void (*tip)(const char *); | |
90 | void (*log)(const char *); | |
91 | void (*warn)(const char *); | |
92 | void (*error)(const char *); | |
93 | } console = { | |
94 | .tip = Console(TIP), | |
95 | .log = Console(LOG), | |
96 | .warn = Console(WARNING), | |
97 | .error = Console(ERROR), | |
98 | }; | |
99 | ||
100 | static bool VarToCArr(struct PP_Var var_array, char ***array, uint32_t *length) | |
101 | { | |
102 | char buf[1024]; | |
103 | uint32_t i; | |
104 | ||
105 | if (ppb_var_array_interface == NULL) { | |
106 | fail: | |
107 | *length = 0; | |
108 | return false; | |
109 | } | |
110 | ||
111 | *length = ppb_var_array_interface->GetLength(var_array); | |
112 | if (*length > (1 << 16)) | |
113 | goto fail; | |
114 | *array = malloc(*length * sizeof(*array)); | |
115 | if (!*array) | |
116 | goto fail; | |
117 | ||
118 | for (i = 0; i < *length; ++i) { | |
119 | struct PP_Var var_ele = ppb_var_array_interface->Get(var_array, i);; | |
120 | uint32_t len = VarToCStr(var_ele, buf, sizeof(buf)); | |
121 | (*array)[i] = len ? strdup(buf) : NULL; | |
122 | } | |
123 | ||
124 | return true; | |
125 | } | |
126 | ||
127 | static void CArrFree(char **array, uint32_t length) | |
128 | { | |
129 | uint32_t i; | |
130 | for (i = 0; i < length; ++i) | |
131 | free(array[i]); | |
132 | free(array); | |
133 | } |