-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathjerry_util.c
338 lines (282 loc) · 7.77 KB
/
jerry_util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <rtthread.h>
#include <dfs_posix.h>
#include "jerry_util.h"
#include <jerry_event.h>
#include <jerry_message.h>
#include "jerry_utf8.h"
extern int js_console_init(void);
extern int js_module_init(void);
extern int js_buffer_init(void);
extern int js_buffer_cleanup(void);
static void _js_value_dump(jerry_value_t value);
static rt_mutex_t _util_lock = NULL;
static js_util_user _user_init = NULL, _user_cleanup = NULL;
void js_set_property(const jerry_value_t obj, const char *name,
const jerry_value_t prop)
{
jerry_value_t str = jerry_create_string((const jerry_char_t *)name);
jerry_set_property(obj, str, prop);
jerry_release_value (str);
}
jerry_value_t js_get_property(const jerry_value_t obj, const char *name)
{
jerry_value_t ret;
const jerry_value_t str = jerry_create_string ((const jerry_char_t*)name);
ret = jerry_get_property(obj, str);
jerry_release_value (str);
return ret;
}
void js_set_string_property(const jerry_value_t obj, const char *name,
char* value)
{
jerry_value_t str = jerry_create_string((const jerry_char_t *)name);
jerry_value_t value_str = jerry_create_string((const jerry_char_t *)value);
jerry_set_property(obj, str, value_str);
jerry_release_value (str);
jerry_release_value (value_str);
}
void js_set_boolean_property(const jerry_value_t obj, const char *name,
bool value)
{
jerry_value_t str = jerry_create_string((const jerry_char_t *)name);
jerry_value_t value_bool = jerry_create_boolean(value);
jerry_set_property(obj, str, value_bool);
jerry_release_value(str);
jerry_release_value(value_bool);
}
void js_add_function(const jerry_value_t obj, const char *name,
jerry_external_handler_t func)
{
jerry_value_t str = jerry_create_string((const jerry_char_t *)name);
jerry_value_t jfunc = jerry_create_external_function(func);
jerry_set_property(obj, str, jfunc);
jerry_release_value(str);
jerry_release_value(jfunc);
}
jerry_value_t js_string_to_value(const char *value)
{
jerry_value_t str = 0;
if (jerry_str_is_utf8(value, strlen(value)))
{
str = jerry_create_string((const jerry_char_t *)value);
}
else
{
char *utf8 = NULL;
jerry_str2utf8((char *)value, strlen(value), &utf8);
str = jerry_create_string((const jerry_char_t *)utf8);
rt_free(utf8);
}
return str;
}
char *js_value_to_string(const jerry_value_t value)
{
int len;
char *str;
len = jerry_get_utf8_string_size(value);
str = (char*)malloc(len + 1);
if (str)
{
jerry_string_to_char_buffer(value, (jerry_char_t*)str, len);
str[len] = '\0';
}
return str;
}
jerry_value_t js_call_func_obj(const jerry_value_t func_obj_val, /**< function object to call */
const jerry_value_t this_val, /**< object for 'this' binding */
const jerry_value_t args_p[], /**< function's call arguments */
jerry_size_t args_count) /**< number of the arguments */
{
jerry_value_t ret;
js_util_lock();
ret = jerry_call_function(func_obj_val, this_val, args_p, args_count);
js_util_unlock();
return ret;
}
jerry_value_t js_call_function(const jerry_value_t obj, const char *name,
const jerry_value_t args[], jerry_size_t args_cnt)
{
jerry_value_t ret;
jerry_value_t function = js_get_property(obj, name);
if (jerry_value_is_function(function))
{
ret = js_call_func_obj(function, obj, args, args_cnt);
}
else
{
ret = jerry_create_null();
}
jerry_release_value(function);
return ret;
}
bool object_dump_foreach(const jerry_value_t property_name,
const jerry_value_t property_value, void *user_data_p)
{
char *str;
int str_size;
int *first_property;
first_property = (int *)user_data_p;
if (*first_property) *first_property = 0;
else
{
printf(", ");
}
if (jerry_value_is_string(property_name))
{
str_size = jerry_get_string_size(property_name);
str = (char*) malloc (str_size + 1);
RT_ASSERT(str != NULL);
jerry_string_to_char_buffer(property_name, (jerry_char_t*)str, str_size);
str[str_size] = '\0';
printf("%s : ", str);
free(str);
}
_js_value_dump(property_value);
return true;
}
static void _js_value_dump(jerry_value_t value)
{
if (jerry_value_is_undefined(value))
{
printf("undefined");
}
else if (jerry_value_is_boolean(value))
{
printf("%s", jerry_get_boolean_value(value)? "true" : "false");
}
else if (jerry_value_is_number(value))
{
printf("%f", jerry_get_number_value(value));
}
else if (jerry_value_is_null(value))
{
printf("null");
}
else if (jerry_value_is_string(value))
{
char *str;
int str_size;
str_size = jerry_get_string_size(value);
str = (char*) malloc (str_size + 1);
RT_ASSERT(str != NULL);
jerry_string_to_char_buffer(value, (jerry_char_t*)str, str_size);
str[str_size] = '\0';
printf("\"%s\"", str);
free(str);
}
else if (jerry_value_is_promise(value))
{
printf("promise??");
}
else if (jerry_value_is_function(value))
{
printf("[function]");
}
else if (jerry_value_is_constructor(value))
{
printf("constructor");
}
else if (jerry_value_is_array(value))
{
int index;
printf("[");
for (index = 0; index < jerry_get_array_length(value); index ++)
{
jerry_value_t item = jerry_get_property_by_index(value, index);
_js_value_dump(item);
printf(", ");
jerry_release_value(item);
}
printf("]");
}
else if (jerry_value_is_object(value))
{
int first_property = 1;
printf("{");
jerry_foreach_object_property(value, object_dump_foreach, &first_property);
printf("}");
}
else
{
printf("what?");
}
}
void js_value_dump(jerry_value_t value)
{
_js_value_dump(value);
printf("\n");
}
int js_read_file(const char* filename, char **script)
{
FILE *fp;
int length = 0;
struct stat statbuf;
if (!filename || !script) return 0;
stat(filename, &statbuf);
length = statbuf.st_size;
if(!length) return 0;
*script = (char *)rt_malloc(length + 1);
if(!(*script)) return 0;
(*script)[length] = '\0';
fp = fopen(filename, "rb");
if(!fp)
{
rt_free(*script);
*script = RT_NULL;
printf("open %s failed!\n", filename);
return 0;
}
if(fread(*script, length, 1, fp) != 1)
{
length = 0;
rt_free(*script);
*script = RT_NULL;
printf("read %s failed!\n", filename);
}
fclose(fp);
return length;
}
int js_util_init(void)
{
if (_util_lock == RT_NULL)
_util_lock = rt_mutex_create("call_func", RT_IPC_FLAG_FIFO);
js_console_init();
js_module_init();
js_buffer_init();
js_event_init();
js_message_init();
if (_user_init != NULL)
{
_user_init();
}
return 0;
}
int js_util_cleanup(void)
{
js_event_deinit();
js_buffer_cleanup();
if (_user_cleanup != NULL)
{
_user_cleanup();
}
return 0;
}
void js_util_user_init(js_util_user func)
{
_user_init = func;
}
void js_util_user_cleanup(js_util_user func)
{
_user_cleanup = func;
}
int js_util_lock(void)
{
return rt_mutex_take(_util_lock, RT_WAITING_FOREVER);
}
int js_util_unlock(void)
{
return rt_mutex_release(_util_lock);
}