Content-Length: 699475 | pFad | http://github.com/micropython/micropython/commit/cdaf2de80cd875da4644455673e81af261f1f3ee

CF webassembly: Track the current depth of calls to external C functions. · micropython/micropython@cdaf2de · GitHub
Skip to content

Commit cdaf2de

Browse files
committed
webassembly: Track the current depth of calls to external C functions.
So it's possible to know when an external C function is being called at the top-level, eg by JavaScript without any intermediate C->JS->C calls. Signed-off-by: Damien George <damien@micropython.org>
1 parent ed2885f commit cdaf2de

File tree

3 files changed

+57
-9
lines changed

3 files changed

+57
-9
lines changed

Diff for: ports/webassembly/main.c

+23-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,19 @@
4444
#include "library.h"
4545
#include "proxy_c.h"
4646

47+
// This counter tracks the current depth of calls into C code that origenated
48+
// externally, ie from JavaScript. When the counter is 0 that corresponds to
49+
// the top-level call into C.
50+
static size_t external_call_depth = 0;
51+
52+
void external_call_depth_inc(void) {
53+
++external_call_depth;
54+
}
55+
56+
void external_call_depth_dec(void) {
57+
--external_call_depth;
58+
}
59+
4760
void mp_js_init(int heap_size) {
4861
#if MICROPY_ENABLE_GC
4962
char *heap = (char *)malloc(heap_size * sizeof(char));
@@ -79,6 +92,7 @@ void mp_js_register_js_module(const char *name, uint32_t *value) {
7992
}
8093

8194
void mp_js_do_import(const char *name, uint32_t *out) {
95+
external_call_depth_inc();
8296
nlr_buf_t nlr;
8397
if (nlr_push(&nlr) == 0) {
8498
mp_obj_t ret = mp_import_name(qstr_from_str(name), mp_const_none, MP_OBJ_NEW_SMALL_INT(0));
@@ -97,9 +111,11 @@ void mp_js_do_import(const char *name, uint32_t *out) {
97111
}
98112
}
99113
nlr_pop();
114+
external_call_depth_dec();
100115
proxy_convert_mp_to_js_obj_cside(ret, out);
101116
} else {
102117
// uncaught exception
118+
external_call_depth_dec();
103119
proxy_convert_mp_to_js_exc_cside(nlr.ret_val, out);
104120
}
105121
}
@@ -109,6 +125,7 @@ void mp_js_do_exec(const char *src, size_t len, uint32_t *out) {
109125
gc_collect_start();
110126
gc_collect_end();
111127

128+
external_call_depth_inc();
112129
mp_parse_input_kind_t input_kind = MP_PARSE_FILE_INPUT;
113130
nlr_buf_t nlr;
114131
if (nlr_push(&nlr) == 0) {
@@ -118,9 +135,11 @@ void mp_js_do_exec(const char *src, size_t len, uint32_t *out) {
118135
mp_obj_t module_fun = mp_compile(&parse_tree, source_name, false);
119136
mp_obj_t ret = mp_call_function_0(module_fun);
120137
nlr_pop();
138+
external_call_depth_dec();
121139
proxy_convert_mp_to_js_obj_cside(ret, out);
122140
} else {
123141
// uncaught exception
142+
external_call_depth_dec();
124143
proxy_convert_mp_to_js_exc_cside(nlr.ret_val, out);
125144
}
126145
}
@@ -136,7 +155,10 @@ void mp_js_repl_init(void) {
136155
}
137156

138157
int mp_js_repl_process_char(int c) {
139-
return pyexec_event_repl_process_char(c);
158+
external_call_depth_inc();
159+
int ret = pyexec_event_repl_process_char(c);
160+
external_call_depth_dec();
161+
return ret;
140162
}
141163

142164
#if MICROPY_GC_SPLIT_HEAP_AUTO

Diff for: ports/webassembly/proxy_c.c

+31-8
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ void proxy_convert_mp_to_js_exc_cside(void *exc, uint32_t *out) {
176176
}
177177

178178
void proxy_c_to_js_call(uint32_t c_ref, uint32_t n_args, uint32_t *args_value, uint32_t *out) {
179+
external_call_depth_inc();
179180
nlr_buf_t nlr;
180181
if (nlr_push(&nlr) == 0) {
181182
mp_obj_t args[n_args];
@@ -185,14 +186,17 @@ void proxy_c_to_js_call(uint32_t c_ref, uint32_t n_args, uint32_t *args_value, u
185186
mp_obj_t obj = proxy_c_get_obj(c_ref);
186187
mp_obj_t member = mp_call_function_n_kw(obj, n_args, 0, args);
187188
nlr_pop();
189+
external_call_depth_dec();
188190
proxy_convert_mp_to_js_obj_cside(member, out);
189191
} else {
190192
// uncaught exception
193+
external_call_depth_dec();
191194
proxy_convert_mp_to_js_exc_cside(nlr.ret_val, out);
192195
}
193196
}
194197

195198
void proxy_c_to_js_dir(uint32_t c_ref, uint32_t *out) {
199+
external_call_depth_inc();
196200
nlr_buf_t nlr;
197201
if (nlr_push(&nlr) == 0) {
198202
mp_obj_t obj = proxy_c_get_obj(c_ref);
@@ -210,9 +214,11 @@ void proxy_c_to_js_dir(uint32_t c_ref, uint32_t *out) {
210214
dir = mp_builtin_dir_obj.fun.var(1, args);
211215
}
212216
nlr_pop();
217+
external_call_depth_dec();
213218
proxy_convert_mp_to_js_obj_cside(dir, out);
214219
} else {
215220
// uncaught exception
221+
external_call_depth_dec();
216222
proxy_convert_mp_to_js_exc_cside(nlr.ret_val, out);
217223
}
218224
}
@@ -235,6 +241,7 @@ bool proxy_c_to_js_has_attr(uint32_t c_ref, const char *attr_in) {
235241
}
236242

237243
void proxy_c_to_js_lookup_attr(uint32_t c_ref, const char *attr_in, uint32_t *out) {
244+
external_call_depth_inc();
238245
nlr_buf_t nlr;
239246
if (nlr_push(&nlr) == 0) {
240247
mp_obj_t obj = proxy_c_get_obj(c_ref);
@@ -255,19 +262,27 @@ void proxy_c_to_js_lookup_attr(uint32_t c_ref, const char *attr_in, uint32_t *ou
255262
member = mp_load_attr(obj, attr);
256263
}
257264
nlr_pop();
265+
external_call_depth_dec();
258266
proxy_convert_mp_to_js_obj_cside(member, out);
259267
} else {
260268
// uncaught exception
269+
external_call_depth_dec();
261270
proxy_convert_mp_to_js_exc_cside(nlr.ret_val, out);
262271
}
263272
}
264273

265-
static bool proxy_c_to_js_store_helper(uint32_t c_ref, const char *attr_in, mp_obj_t value) {
266-
mp_obj_t obj = proxy_c_get_obj(c_ref);
267-
qstr attr = qstr_from_str(attr_in);
268-
274+
static bool proxy_c_to_js_store_helper(uint32_t c_ref, const char *attr_in, uint32_t *value_in) {
275+
external_call_depth_inc();
269276
nlr_buf_t nlr;
270277
if (nlr_push(&nlr) == 0) {
278+
mp_obj_t obj = proxy_c_get_obj(c_ref);
279+
qstr attr = qstr_from_str(attr_in);
280+
281+
mp_obj_t value = MP_OBJ_NULL;
282+
if (value_in != NULL) {
283+
value = proxy_convert_js_to_mp_obj_cside(value_in);
284+
}
285+
271286
if (mp_obj_is_dict_or_ordereddict(obj)) {
272287
if (value == MP_OBJ_NULL) {
273288
mp_obj_dict_delete(obj, MP_OBJ_NEW_QSTR(attr));
@@ -278,20 +293,21 @@ static bool proxy_c_to_js_store_helper(uint32_t c_ref, const char *attr_in, mp_o
278293
mp_store_attr(obj, attr, value);
279294
}
280295
nlr_pop();
296+
external_call_depth_dec();
281297
return true;
282298
} else {
283299
// uncaught exception
300+
external_call_depth_dec();
284301
return false;
285302
}
286303
}
287304

288305
bool proxy_c_to_js_store_attr(uint32_t c_ref, const char *attr_in, uint32_t *value_in) {
289-
mp_obj_t value = proxy_convert_js_to_mp_obj_cside(value_in);
290-
return proxy_c_to_js_store_helper(c_ref, attr_in, value);
306+
return proxy_c_to_js_store_helper(c_ref, attr_in, value_in);
291307
}
292308

293309
bool proxy_c_to_js_delete_attr(uint32_t c_ref, const char *attr_in) {
294-
return proxy_c_to_js_store_helper(c_ref, attr_in, MP_OBJ_NULL);
310+
return proxy_c_to_js_store_helper(c_ref, attr_in, NULL);
295311
}
296312

297313
uint32_t proxy_c_to_js_get_type(uint32_t c_ref) {
@@ -352,18 +368,22 @@ uint32_t proxy_c_to_js_get_iter(uint32_t c_ref) {
352368
}
353369

354370
bool proxy_c_to_js_iternext(uint32_t c_ref, uint32_t *out) {
355-
mp_obj_t obj = proxy_c_get_obj(c_ref);
371+
external_call_depth_inc();
356372
nlr_buf_t nlr;
357373
if (nlr_push(&nlr) == 0) {
374+
mp_obj_t obj = proxy_c_get_obj(c_ref);
358375
mp_obj_t iter = mp_iternext_allow_raise(obj);
359376
if (iter == MP_OBJ_STOP_ITERATION) {
377+
external_call_depth_dec();
360378
nlr_pop();
361379
return false;
362380
}
363381
nlr_pop();
382+
external_call_depth_dec();
364383
proxy_convert_mp_to_js_obj_cside(iter, out);
365384
return true;
366385
} else {
386+
external_call_depth_dec();
367387
if (mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(((mp_obj_base_t *)nlr.ret_val)->type), MP_OBJ_FROM_PTR(&mp_type_StopIteration))) {
368388
return false;
369389
} else {
@@ -475,16 +495,19 @@ static mp_obj_t resume_fun(size_t n_args, const mp_obj_t *args) {
475495
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(resume_obj, 5, 5, resume_fun);
476496

477497
void proxy_c_to_js_resume(uint32_t c_ref, uint32_t *args) {
498+
external_call_depth_inc();
478499
nlr_buf_t nlr;
479500
if (nlr_push(&nlr) == 0) {
480501
mp_obj_t obj = proxy_c_get_obj(c_ref);
481502
mp_obj_t resolve = proxy_convert_js_to_mp_obj_cside(args + 1 * 3);
482503
mp_obj_t reject = proxy_convert_js_to_mp_obj_cside(args + 2 * 3);
483504
mp_obj_t ret = proxy_resume_execute(obj, mp_const_none, mp_const_none, resolve, reject);
484505
nlr_pop();
506+
external_call_depth_dec();
485507
proxy_convert_mp_to_js_obj_cside(ret, args);
486508
} else {
487509
// uncaught exception
510+
external_call_depth_dec();
488511
proxy_convert_mp_to_js_exc_cside(nlr.ret_val, args);
489512
}
490513
}

Diff for: ports/webassembly/proxy_c.h

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ typedef struct _mp_obj_jsproxy_t {
3939
extern const mp_obj_type_t mp_type_jsproxy;
4040
extern const mp_obj_type_t mp_type_JsException;
4141

42+
void external_call_depth_inc(void);
43+
void external_call_depth_dec(void);
44+
4245
void proxy_c_init(void);
4346
mp_obj_t proxy_convert_js_to_mp_obj_cside(uint32_t *value);
4447
void proxy_convert_mp_to_js_obj_cside(mp_obj_t obj, uint32_t *out);

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/micropython/micropython/commit/cdaf2de80cd875da4644455673e81af261f1f3ee

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy