V7_PRIVATE val_t rx_exec(struct v7 *v7, val_t rx, val_t str, int lind) { if (v7_is_regexp(rx)) { val_t s = to_string(v7, str); size_t len; struct slre_loot sub; struct slre_cap *ptok = sub.caps; char *const str = (char *) v7_to_string(v7, &s, &len); const char *const end = str + len; const char *begin = str; struct v7_regexp *rp = v7_to_regexp(rx); int flag_g = slre_get_flags(rp->compiled_regexp) & SLRE_FLAG_G; if (rp->lastIndex < 0) rp->lastIndex = 0; if (flag_g || lind) begin = utfnshift(str, rp->lastIndex); if (!slre_exec(rp->compiled_regexp, 0, begin, end, &sub)) { int i; val_t arr = v7_create_array(v7); for (i = 0; i < sub.num_captures; i++, ptok++) v7_array_push(v7, arr, v7_create_string(v7, ptok->start, ptok->end - ptok->start, 1)); if (flag_g) rp->lastIndex = utfnlen(str, sub.caps->end - str); v7_set_property(v7, arr, "index", 5, V7_PROPERTY_READ_ONLY, v7_create_number(utfnlen(str, sub.caps->start - str))); return arr; } else rp->lastIndex = 0; } return v7_create_null(); }
static void invoke_cb(struct user_data *ud, const char *name, v7_val_t ev) { struct v7 *v7 = ud->v7; v7_val_t met = v7_get(v7, ud->ws, name, ~0); if (!v7_is_undefined(met)) { v7_val_t res, args = v7_create_array(v7); v7_array_set(v7, args, 0, ev); if (v7_apply(v7, &res, met, v7_create_undefined(), args) != V7_OK) { /* TODO(mkm): make it print stack trace */ fprintf(stderr, "cb threw an exception\n"); } } }
static void call_sum(struct v7 *v7) { v7_val_t func, result, args; func = v7_get(v7, v7_get_global_object(v7), "sum", 3); args = v7_create_array(v7); v7_array_push(v7, args, v7_create_number(123.0)); v7_array_push(v7, args, v7_create_number(456.789)); v7_apply(v7, &result, func, v7_create_undefined(), args); printf("Result: %g\n", v7_to_number(result)); }
void sj_invoke_cb2(struct v7 *v7, v7_val_t cb, v7_val_t arg1, v7_val_t arg2) { v7_val_t args; v7_own(v7, &cb); v7_own(v7, &arg1); v7_own(v7, &arg2); args = v7_create_array(v7); v7_own(v7, &args); v7_array_push(v7, args, arg1); v7_array_push(v7, args, arg2); sj_invoke_cb(v7, cb, v7_get_global(v7), args); v7_disown(v7, &args); v7_disown(v7, &arg2); v7_disown(v7, &arg1); v7_disown(v7, &cb); }
static void gpio_intr_handler_proxy(int pin, int level) { char prop_name[15]; int len; v7_val_t args; len = snprintf(prop_name, sizeof(prop_name), "_ih_%d", (int) pin); v7_val_t cb = v7_get(s_v7, v7_get_global_object(s_v7), prop_name, len); if (!v7_is_function(cb)) { return; } args = v7_create_array(s_v7); v7_array_push(s_v7, args, v7_create_number(pin)); v7_array_push(s_v7, args, v7_create_number(level)); v7_apply(s_v7, cb, v7_create_undefined(), args); }
static void gpio_intr_handler_proxy(int pin, int level) { char prop_name[15]; int len; v7_val_t res, args; len = snprintf(prop_name, sizeof(prop_name), "_ih_%d", (int) pin); v7_val_t cb = v7_get(s_v7, v7_get_global(s_v7), prop_name, len); if (!v7_is_function(cb)) { return; } args = v7_create_array(s_v7); v7_array_push(s_v7, args, v7_create_number(pin)); v7_array_push(s_v7, args, v7_create_number(level)); if (v7_apply(s_v7, cb, v7_create_undefined(), args, &res) != V7_OK) { /* TODO(mkm): make it print stack trace */ fprintf(stderr, "cb threw an exception\n"); } }
void sj_wifi_scan_done(const char **ssids) { struct v7 *v7 = s_v7; v7_val_t cb = v7_get(v7, s_wifi, "_scb", ~0); v7_val_t res = v7_create_undefined(); const char **p; if (!v7_is_function(cb)) return; v7_own(v7, &res); if (ssids != NULL) { res = v7_create_array(v7); for (p = ssids; *p != NULL; p++) { v7_array_push(v7, res, v7_create_string(v7, *p, strlen(*p), 1)); } } else { res = v7_create_undefined(); } sj_invoke_cb1(v7, cb, res); v7_disown(v7, &res); v7_set(v7, s_wifi, "_scb", ~0, V7_PROPERTY_DONT_ENUM | V7_PROPERTY_HIDDEN, v7_create_undefined()); }