static SLVAL sl_mysql_init(sl_vm_t* vm, SLVAL self, SLVAL host, SLVAL user, SLVAL password) { char *host_s = NULL, *user_s = NULL, *password_s = NULL, *db_s = NULL; int port_i = 3306, flag_i = CLIENT_IGNORE_SIGPIPE | CLIENT_MULTI_STATEMENTS; mysql_t* mysql = sl_data_get_ptr(vm, &mysql_data_type, self); if(sl_is_truthy(host)) { host_s = sl_to_cstr(vm, sl_expect(vm, host, vm->lib.String)); } if(sl_is_truthy(user)) { user_s = sl_to_cstr(vm, sl_expect(vm, user, vm->lib.String)); } if(sl_is_truthy(password)) { password_s = sl_to_cstr(vm, sl_expect(vm, password, vm->lib.String)); } if(!mysql_real_connect(&mysql->mysql, host_s, user_s, password_s, db_s, port_i, NULL, flag_i)) { sl_mysql_check_error(vm, &mysql->mysql); } if(!mysql_set_character_set(&mysql->mysql, "utf8")) { sl_mysql_check_error(vm, &mysql->mysql); } mysql->valid = 1; return self; }
static SLVAL response_set_header(sl_vm_t* vm, SLVAL self, SLVAL name, SLVAL value) { char* h; sl_response_internal_opts_t* resp = response(vm); (void)self; sl_expect(vm, name, vm->lib.String); sl_expect(vm, value, vm->lib.String); if(resp->header_count >= resp->header_cap) { resp->header_cap *= 2; resp->headers = sl_realloc(vm->arena, resp->headers, sizeof(sl_response_key_value_t) * resp->header_cap); } h = sl_to_cstr(vm, name); if(strchr(h, '\n') || strchr(h, '\r')) { return vm->lib._false; } resp->headers[resp->header_count].name = h; h = sl_to_cstr(vm, value); if(strchr(h, '\n') || strchr(h, '\r')) { return vm->lib._false; } resp->headers[resp->header_count].value = h; resp->header_count++; return vm->lib._true; }
static SLVAL sl_mysql_use(sl_vm_t* vm, SLVAL self, SLVAL db) { mysql_t* mysql = get_mysql(vm, self); mysql_select_db(&mysql->mysql, sl_to_cstr(vm, sl_expect(vm, db, vm->lib.String))); sl_mysql_check_error(vm, &mysql->mysql); return vm->lib._true; }
static SLVAL sl_string_to_f(sl_vm_t* vm, SLVAL self) { char* str = sl_to_cstr(vm, self); double dbl = 0; sscanf(str, "%lf", &dbl); return sl_make_float(vm, dbl); }
static int cap_index(sl_vm_t* vm, SLVAL regexp_match, SLVAL i) { sl_regexp_match_t* match = get_regexp_match(vm, regexp_match); int index; if(sl_is_a(vm, i, vm->lib.String)) { char* named_cap = sl_to_cstr(vm, i); index = pcre_get_stringnumber(match->re->re, named_cap); if(index < 0) { return -1; } } else { index = sl_get_int(sl_expect(vm, i, vm->lib.Int)); } if(index < 0 || index >= match->capture_count) { return -1; } return index * 2; }
size_t token_count; sl_token_t* tokens = sl_lex(vm, (uint8_t*)argv[1], (uint8_t*)source, size, &token_count, 0); sl_node_base_t* ast = sl_parse(vm, tokens, token_count, (uint8_t*)argv[1]); sl_vm_section_t* section = sl_compile(vm, ast, (uint8_t*)argv[1]); disassemble(vm, section); while(section_j < section_i) { disassemble(vm, section_queue[++section_j]); } }, err, { if(frame.as.handler_frame.unwind_type == SL_UNWIND_EXCEPTION) { fprintf(stderr, "%s\n", sl_to_cstr(vm, err)); exit(1); } if(frame.as.handler_frame.unwind_type == SL_UNWIND_EXIT) { int exit_code = sl_get_int(err); sl_free_gc_arena(vm->arena); exit(exit_code); } }); } #ifdef SL_HAS_COMPUTED_GOTO extern void* sl_vm_op_addresses[]; static sl_vm_opcode_t
SLVAL sl_string_encode2(sl_vm_t* vm, SLVAL self, SLVAL encoding) { return sl_string_encode(vm, self, sl_to_cstr(vm, encoding)); }